Menu

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

Download this file

194 lines (167 with data), 4.7 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
///////////////////////////////////////////////////////////////////////////////
//
// 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"
#ifdef CPPCMS_WIN32
namespace cppcms {
namespace impl {
void daemonize(json::value const &conf) {}
void de_daemonize(json::value const &conf) {}
}
}
#else // POSIX OS
#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 <locale>
#include <cppcms/json.h>
#include <cppcms/cppcms_error.h>
namespace cppcms {
namespace impl {
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;
std::string chroot_dir = conf.get("daemon.chroot","");
if(!chroot_dir.empty()) {
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");
}
}
std::string lock_file = conf.get("daemon.lock","");
int lock_fd = -1;
if(!lock_file.empty()) {
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;
}
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);
if(setuid(pw->pw_uid) < 0) {
int err = errno;
throw cppcms_error(err,"Failed setuid to user " + user);
}
}
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);
if(setgid(gr->gr_gid) < 0) {
int err = errno;
throw cppcms_error(err,"Failed setgid to group " + group);
}
}
// 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();
int devnull_fd = open("/dev/null",O_RDWR);
if(devnull_fd < 0) {
int err = errno;
throw cppcms_error(err,"Failed to open /dev/null");
}
dup2(devnull_fd,0);
dup2(devnull_fd,1);
dup2(devnull_fd,2);
close(devnull_fd);
chdir("/");
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);
}
}
} // impl
} // cppcms
#endif // POSIX OS
MongoDB Logo MongoDB