-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid_file_holder.cpp
More file actions
39 lines (31 loc) · 917 Bytes
/
pid_file_holder.cpp
File metadata and controls
39 lines (31 loc) · 917 Bytes
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
#ifndef PID_FILE_HOLDER_H_
#define PID_FILE_HOLDER_H_
#include <unistd.h> //unlink
#include "pid_file_holder.h"
namespace schrandr {
PIDFileHolder::PIDFileHolder(
unsigned int pid,
const std::string pid_file /*= std::string("/tmp/schrandr.pid")*/)
: pid_file_(pid_file, std::ios_base::in),
pid_file_path_(pid_file)
{
if (!pid_file_.fail()) {
std::cout << "PID file exists?" << std::endl;
exit(EXIT_FAILURE);
}
pid_file_.close();
pid_file_.open(pid_file_path_, std::ios_base::out | std::ios_base::trunc);
pid_file_.exceptions(pid_file_.badbit | pid_file_.failbit);
pid_file_ << pid << std::flush;
}
PIDFileHolder::~PIDFileHolder()
{
pid_file_.close();
std::cout << "Now Closing" << std::endl;
if (unlink(pid_file_path_.c_str()) == -1) {
exit(EXIT_FAILURE);
}
std::cout << "Unlinking worked apparently." << std::endl;
}
}
#endif