forked from mickem/nscp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_file.cpp
42 lines (33 loc) · 844 Bytes
/
pid_file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <pid_file.hpp>
#include <fstream>
#include <boost/filesystem/operations.hpp>
#ifdef WIN32
#include <process.h>
#endif
pidfile::pidfile(boost::filesystem::path const & rundir, std::string const & process_name)
: filepath_((rundir.empty() ? get_default_rundir() : rundir) / (process_name + ".pid")) {}
pidfile::pidfile(boost::filesystem::path const & filepath) : filepath_(filepath) {}
pidfile::~pidfile() {
try {
remove();
} catch (...) {}
}
bool pidfile::create(pid_t const pid) const {
remove();
std::ofstream out(filepath_.string().c_str());
out << pid;
return out.good();
}
bool pidfile::create() const {
return create(get_pid());
}
pid_t pidfile::get_pid() const {
#ifdef WIN32
return ::_getpid();
#else
return ::getpid();
#endif
}
bool pidfile::remove() const {
return boost::filesystem::remove(filepath_);
}