-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDaemonize.hpp
More file actions
287 lines (246 loc) · 5.29 KB
/
Daemonize.hpp
File metadata and controls
287 lines (246 loc) · 5.29 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef SWITCH_DAEMONIZE_H
#define SWITCH_DAEMONIZE_H
/**
* v0.1
* 欢迎补充!
**/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string>
#include <vector>
#include <sstream>
#ifndef DAEMONIZE_NO_BOOST
#include <boost/serialization/singleton.hpp>
#include <boost/noncopyable.hpp>
#endif // DAEMONIZE_NO_BOOST
#ifndef NAMESPACE_SWITCH_TOOL
#define NAMESPACE_SWITCH_TOOL
#define OPEN_NAMESPACE_SWITCHTOOL namespace Switch { \
namespace Tool {
#define CLOSE_NAMESPACE_SWITCHTOOL }; \
};
#define USING_NAMESPACE_SWITCHTOOL using namespace Switch::Tool;
#endif
OPEN_NAMESPACE_SWITCHTOOL
static std::string strPidFileName = "";
static void DeletePidFile(void)
{
unlink(strPidFileName.c_str());
}
#ifndef DAEMONIZE_NO_BOOST
class Daemonize : public boost::noncopyable
#else
class Daemonize
#endif // DAEMONIZE_NO_BOOST
{
public:
typedef void (*SigHandler_t)(int);
Daemonize(const std::string& sBaseDir = "", const std::string& sPidFile = "")
{
m_sBaseDir = sBaseDir;
m_sPidFile = sPidFile;
m_nLockFd = -1;
m_bDaemoned = false;
}
std::string& BaseDir(void) { return m_sBaseDir;}
std::string& PidFile(void) { return m_sPidFile;}
int SetDefaultBaseDir(void)
{
std::stringstream linkPath;
char exePath[256];
char* ch;
memset(exePath, 0, 256);
linkPath << "/proc/" << getpid() << "/exe";
if (readlink(linkPath.str().c_str(), exePath, 256) == -1)
{
return -1;
}
exePath[255] = '\0';
ch = strrchr(exePath, '/');
if (ch != NULL)
{
*(ch + 1) = '\0';
}
else
{
return -1;
}
m_sBaseDir = exePath;
return 0;
}
int SetDefaultPidFile(void)
{
m_sPidFile = "pid.pid";
return 0;
}
/// 0 -> not exist
/// -1 -> exist
int CheckPidFile(void)
{
pid_t pid;
if (GetPidFromFile(pid) == -1)
{
return 0;
}
if (kill(pid, 0) == 0)
{
return -1;
}
if (errno == EPERM)
{
return -1;
}
return 0;
}
int WritePidFile(void)
{
int fd, ret;
std::string sFileName = m_sBaseDir + "/" + m_sPidFile;
if (CreateDir(sFileName.c_str()) == -1)
{
return -1;
}
fd = open(sFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0600);
if (fd == -1)
{
return -1;
}
std::stringstream sPid;
sPid << getpid();
ret = write(fd, sPid.str().c_str(), strlen(sPid.str().c_str()));
close(fd);
Switch::Tool::strPidFileName = sFileName;
atexit(&DeletePidFile);
return ret >= 0 ? 0 : -1;
}
int SetDaemonize(void)
{
pid_t pid;
int fd;
if (m_bDaemoned)
{
return -1;
}
if ((pid = fork()) < 0)
{
return -1;
}
else if (pid != 0)
{
exit(0);
}
setsid();
int ret = chdir("/");
umask(0);
fd = open("/dev/null", O_RDWR);
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
close(fd);
m_bDaemoned = true;
return ret;
}
int RegistSigHandler(const int sig, const SigHandler_t& hand)
{
m_vSigHandlers.push_back(std::make_pair(sig, hand));
return 0;
}
int RegistSigHandler(const std::vector< int >& vSigs, const std::vector< SigHandler_t >& vHands)
{
if (vSigs.size() != vHands.size())
{
return -1;
}
for (size_t i = 0; i < vHands.size(); ++i)
{
m_vSigHandlers.push_back(std::make_pair(vSigs[i], vHands[i]));
}
return 0;
}
int SetSigHandler(void)
{
struct sigaction sa;
for (size_t i = 0; i < m_vSigHandlers.size(); ++i)
{
sa.sa_handler = m_vSigHandlers[i].second;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(m_vSigHandlers[i].first, &sa, NULL);
}
return 0;
}
private:
int GetPidFromFile(pid_t& pid)
{
int fd;
char buf[8];
std::string sFileName = m_sBaseDir + "/" + m_sPidFile;
fd = open(sFileName.c_str(), O_RDONLY);
if (fd == -1)
{
return -1;
}
memset(buf, 0, 8);
if (read(fd, buf, 8) == -1)
{
close(fd);
return -1;
}
close(fd);
buf[7] = '\0';
std::stringstream convert;
convert << buf;
convert >> pid;
if (pid <= 0)
{
return -1;
}
return 0;
}
int CreateDir(const char *path)
{
int i, len;
len = strlen(path);
char dir_path[len+1];
dir_path[len] = '\0';
strncpy(dir_path, path, len);
for (i=0; i<len; i++)
{
if (dir_path[i] == '/' && i > 0)
{
dir_path[i]='\0';
if (access(dir_path, F_OK) < 0)
{
if (mkdir(dir_path, 0755) < 0)
{
return -1;
}
}
dir_path[i]='/';
}
}
return 0;
}
private:
std::string m_sBaseDir;
std::string m_sPidFile;
int m_nLockFd;
bool m_bDaemoned;
std::vector< std::pair< int, SigHandler_t > > m_vSigHandlers;
};
#ifndef DAEMONIZE_NO_BOOST
class DaemonizeSingleton : public Daemonize, public boost::serialization::singleton< DaemonizeSingleton >
{
protected:
DaemonizeSingleton(void) {}
~DaemonizeSingleton(void) {}
};
#endif // DAEMONIZE_NO_BOOST
CLOSE_NAMESPACE_SWITCHTOOL
#endif // SWITCH_DAEMONIZE_H