Replace inet_aton() with inet_pton() to parse IPs correctly (#8825)
[libi2ncommon] / src / pidfile.cpp
... / ...
CommitLineData
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
20/**
21 Pid file handling
22
23 @copyright Intra2net AG
24*/
25#include "pidfile.hpp"
26#include <sys/types.h>
27
28#include <signal.h>
29#include <fstream>
30#include <sstream>
31#include <iostream>
32#include <stdexcept>
33
34#include "filefunc.hxx"
35#include <unistd.h>
36
37using namespace std;
38using namespace I2n;
39
40/**
41 * Constructor.
42 * @param filename Full path to the pid file
43 * @param remove_file_on_destruction Remove pid file on destruction of the object. Only useful if running as root.
44 */
45PidFile::PidFile(const std::string &filename, bool remove_file_on_destruction)
46{
47 Filename = filename;
48 RemoveFileOnDestruction = remove_file_on_destruction;
49 WroteFile = false;
50}
51
52/**
53 * Destructor. We delete the pid file if #RemoveFileOnDestruction is set
54 * and we actually wrote a file (#WroteFile).
55 * This is only possible if we run with root priviledges as /var/run
56 * is root writable only.
57 */
58PidFile::~PidFile()
59{
60 if (RemoveFileOnDestruction && WroteFile)
61 unlink(Filename);
62}
63
64/**
65 * Checks if the pid file for this program already exists.
66 * If yes it does a "kill" probe to see if it is alive.
67 * @param running_pid[out] Store pid number of instance if already running
68 * @return true if running, false otherweise
69 */
70bool PidFile::check_already_running(pid_t *running_pid)
71{
72 // Check if there is an existing pid file
73 ifstream in(Filename.c_str());
74 if (!in)
75 return false;
76
77 pid_t read_pid = 0;
78 if (!(in >> read_pid))
79 return false;
80
81 // Ok, we got an existing pid file.
82 // Check if program is still alive
83 if (kill(read_pid, 0) == 0)
84 {
85 if (running_pid)
86 *running_pid = read_pid;
87
88 // State: running
89 return true;
90 }
91
92 // Default state: not running
93 return false;
94}
95
96/**
97 * Write pid file
98 * @return true if file was written, false otherwise
99 */
100bool PidFile::write()
101{
102 ofstream out(Filename.c_str());
103 if (!out)
104 return false;
105
106 out << getpid() << endl;
107
108 WroteFile = true;
109
110 return true;
111}