Menu

[r2044]: / framework / trunk / src / daemonize.cpp  Maximize  Restore  History

Download this file

236 lines (201 with data), 5.6 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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include "daemonize.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sstream>
#include <vector>
#include <locale>
#include <cppcms/json.h>
#include <cppcms/cppcms_error.h>
namespace cppcms {
namespace impl {
int daemonizer::global_urandom_fd = -1;
daemonizer::daemonizer(json::value const &conf)
{
real_pid = getpid();
try {
daemonize(conf);
}
catch(...) {
cleanup();
throw;
}
}
daemonizer::~daemonizer()
{
cleanup();
}
void daemonizer::cleanup()
{
if(!unlink_file.empty() && real_pid == getpid())
unlink(unlink_file.c_str());
}
void daemonizer::daemonize(json::value const &conf)
{
if(getppid() == 1)
return;
if(!conf.get("daemon.enable",false))
return;
int gid = -1;
int uid = -1;
int devnull_fd = -1;
int lock_fd = -1;
try {
devnull_fd = open("/dev/null",O_RDWR);
if(devnull_fd < 0) {
int err = errno;
throw cppcms_error(err,"Failed to open /dev/null");
}
std::string group = conf.get("daemon.group","");
if(!group.empty()) {
struct group *gr = getgrnam(group.c_str());
if(!gr)
throw cppcms_error("Invalid group " + group);
gid = gr->gr_gid;
}
std::string user = conf.get("daemon.user","");
if(!user.empty()) {
struct passwd *pw = getpwnam(user.c_str());
if(!pw)
throw cppcms_error("Invalid user " + user);
uid = pw->pw_uid;
if(gid==-1)
gid = pw->pw_gid;
}
std::string chroot_dir = conf.get("daemon.chroot","");
if(!chroot_dir.empty()) {
global_urandom_fd = open("/dev/urandom",O_RDONLY);
if(global_urandom_fd < 0) {
int err = errno;
throw cppcms_error(err,"Failed to open /dev/urandom");
}
if(chdir(chroot_dir.c_str()) < 0) {
int err = errno;
throw cppcms_error(err,"Failed to chdir to " + chroot_dir);
}
if(chroot(chroot_dir.c_str()) < 0) {
int err = errno;
throw cppcms_error(err,"Failed to chroot to " + chroot_dir);
}
}
int fd_limit = conf.get("daemon.fdlimit",-1);
if(fd_limit != -1) {
struct rlimit lm;
lm.rlim_cur = fd_limit;
lm.rlim_max = fd_limit;
if(setrlimit(RLIMIT_NOFILE,&lm) < 0) {
int err = errno;
throw cppcms_error(err,"Failed to change daemon.fdlimit");
}
}
if(gid!=-1) {
if(setgid(gid) < 0) {
int err = errno;
throw cppcms_error(err,"Failed setgid to group " + group);
}
}
if(uid!=-1) {
if(setuid(uid) < 0) {
int err = errno;
throw cppcms_error(err,"Failed setuid to user " + user);
}
}
std::string lock_file = conf.get("daemon.lock","");
if(!lock_file.empty()) {
if(lock_file[0]!='/') {
char *wd = 0;
std::vector<char> buf(256);
while((wd=getcwd(&buf.front(),buf.size()))==0 && errno==ERANGE)
buf.resize(buf.size()*2);
if(!wd) {
int err=errno;
throw cppcms_error(err,"getcwd failed");
}
lock_file=wd + ("/" + lock_file);
}
lock_fd = ::open(lock_file.c_str(),O_RDWR | O_CREAT | O_EXCL,0666);
if(lock_fd < 0) {
int err = errno;
throw cppcms_error(err,"Failed to create a lock file");
}
unlink_file = lock_file;
}
// forking twice
int chid = fork();
if(chid < 0) {
int err = errno;
throw cppcms_error(err,"Fork failed");
}
if(chid != 0)
exit(0);
real_pid = getpid();
if(setsid() < 0) {
int err = errno;
throw cppcms_error(err,"setsid failed");
}
chid = fork();
if(chid < 0) {
int err = errno;
throw cppcms_error(err,"2nd fork failed");
}
if(chid != 0)
exit(0);
real_pid = getpid();
dup2(devnull_fd,0);
dup2(devnull_fd,1);
dup2(devnull_fd,2);
close(devnull_fd);
devnull_fd = -1;
int chdir_r = chdir("/");
(void)(chdir_r); // shut up GCC
if(lock_fd!=-1) {
std::ostringstream ss;
ss.imbue(std::locale::classic());
ss << real_pid;
std::string pid = ss.str();
int v = write(lock_fd,pid.c_str(),pid.size());
if(v!=int(pid.size())) {
throw cppcms_error("Failed to write to lock file");
}
close(lock_fd);
lock_fd = -1;
}
}
catch(...) {
if(lock_fd >= 0)
close(lock_fd);
if(devnull_fd >=0)
close(devnull_fd);
if(global_urandom_fd >= 0)
close(global_urandom_fd);
throw;
}
}
} // impl
} // cppcms
MongoDB Logo MongoDB