Merge branch 'daemon-ext'
[libi2ncommon] / src / pidfile.cpp
CommitLineData
0e23f538
TJ
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*/
6bf07aed
TJ
20/**
21 Pid file handling
22
23 @copyright Intra2net AG
6bf07aed
TJ
24*/
25#include "pidfile.hpp"
6ecf3d57 26#include <sys/types.h>
6bf07aed 27
6ecf3d57
TJ
28#include <signal.h>
29#include <fstream>
30#include <sstream>
31#include <iostream>
32#include <stdexcept>
33
6bf07aed
TJ
34#include "filefunc.hxx"
35#include <unistd.h>
36
6ecf3d57 37using namespace std;
6bf07aed 38using namespace I2n;
6ecf3d57
TJ
39
40/**
6bf07aed 41 * Constructor.
6ecf3d57 42 * @param filename Full path to the pid file
6bf07aed 43 * @param remove_file_on_destruction Remove pid file on destruction of the object. Only useful if running as root.
6ecf3d57 44 */
6bf07aed 45PidFile::PidFile(const std::string &filename, bool remove_file_on_destruction)
6ecf3d57 46{
6bf07aed
TJ
47 Filename = filename;
48 RemoveFileOnDestruction = remove_file_on_destruction;
49 WroteFile = false;
6ecf3d57
TJ
50}
51
52/**
6bf07aed
TJ
53 * Destructor. We delete the pid file if #RemoveFileOnDestruction is set
54 * and we actually wrote a file (#WroteFile).
6ecf3d57
TJ
55 * This is only possible if we run with root priviledges as /var/run
56 * is root writable only.
57 */
6bf07aed 58PidFile::~PidFile()
6ecf3d57 59{
6bf07aed
TJ
60 if (RemoveFileOnDestruction && WroteFile)
61 unlink(Filename);
6ecf3d57
TJ
62}
63
64/**
6bf07aed
TJ
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
6ecf3d57 69 */
6bf07aed 70bool PidFile::check_already_running(pid_t *running_pid)
6ecf3d57 71{
6bf07aed
TJ
72 // Check if there is an existing pid file
73 ifstream in(Filename.c_str());
74 if (!in)
75 return false;
6ecf3d57 76
6bf07aed
TJ
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;
6ecf3d57
TJ
94}
95
96/**
6bf07aed
TJ
97 * Write pid file
98 * @return true if file was written, false otherwise
6ecf3d57 99 */
6bf07aed 100bool PidFile::write()
6ecf3d57 101{
6bf07aed
TJ
102 ofstream out(Filename.c_str());
103 if (!out)
104 return false;
6ecf3d57 105
6bf07aed 106 out << getpid() << endl;
6ecf3d57 107
6bf07aed 108 WroteFile = true;
6ecf3d57 109
6bf07aed 110 return true;
6ecf3d57 111}