]> git.stg.codes - ssmd.git/blob - src/pidfile.cpp
99cea9c9ee0e3f18619f8615222cacaeb6d9bdf9
[ssmd.git] / src / pidfile.cpp
1 #include <sys/types.h> // pid_t
2 #include <unistd.h> // getpid
3
4 #include <cerrno>
5 #include <cstring>
6 #include <fstream>
7
8 #include "pidfile.h"
9 #include "logger.h"
10
11 using SSMD::PIDFile;
12
13 PIDFile::PIDFile()
14     : fileName("/var/run/gssmd.pid")
15 {
16     _create();
17 }
18
19 PIDFile::PIDFile(const std::string & file)
20     : fileName(file)
21 {
22     _create();
23 }
24
25 PIDFile::~PIDFile()
26 {
27     if (unlink(fileName.c_str())) {
28         logger << "Failed to unlink pid-file: " << strerror(errno) << std::endl;
29         return;
30     }
31 }
32
33 void PIDFile::_create()
34 {
35     std::ofstream file(fileName.c_str());
36
37     if (!file.is_open()) {
38         logger << "Failed to create pid-file: " << strerror(errno) << std::endl;
39         return;
40     }
41
42     pid_t pid = getpid();
43     file << pid;
44     logger << "PID: " << pid << std::endl;
45 }