2#if defined(HAVE_CONFIG_H)
23#define RESIPROCATE_SUBSYSTEM resip::Subsystem::SIP
38 : mWaitPeriod(waitPeriod),
58 InfoLog(<<
"Received HUP signal, logger reset");
64 std::cerr <<
"Shutting down" << endl;
72 WarningLog(<<dependentSettings<<
" not supported on Windows, the setting will be ignored");
84 if ( signal( SIGPIPE, SIG_IGN) == SIG_ERR)
86 cerr <<
"Couldn't install signal handler for SIGPIPE" << endl;
91 cerr <<
"Couldn't install signal handler for SIGHUP" << endl;
98 cerr <<
"Couldn't install signal handler for SIGINT" << endl;
104 cerr <<
"Couldn't install signal handler for SIGTERM" << endl;
124 const char *username;
128 if(runAsUser.
empty())
130 ErrLog(<<
"Unable to drop privileges, username not specified");
131 throw std::runtime_error(
"Unable to drop privileges, username not specified");
133 username = runAsUser.
c_str();
134 pw = getpwnam(username);
137 ErrLog(<<
"Unable to drop privileges, user not found");
138 throw std::runtime_error(
"Unable to drop privileges, user not found");
140 new_uid = pw->pw_uid;
142 if(!runAsGroup.
empty())
144 gr = getgrnam(runAsGroup.
c_str());
147 ErrLog(<<
"Unable to drop privileges, group not found");
148 throw std::runtime_error(
"Unable to drop privileges, group not found");
150 new_gid = gr->gr_gid;
155 new_gid = pw->pw_gid;
159 if (cur_gid != new_gid)
163 ErrLog(<<
"Unable to drop privileges, not root!");
164 throw std::runtime_error(
"Unable to drop privileges, not root!");
167 rval = setgid(new_gid);
170 ErrLog(<<
"Unable to drop privileges, operation failed (setgid)");
171 throw std::runtime_error(
"Unable to drop privileges, operation failed");
175 if(initgroups(username, new_gid) < 0)
177 ErrLog(<<
"Unable to drop privileges, operation failed (initgroups)");
178 throw std::runtime_error(
"Unable to drop privileges, operation failed");
182 if (cur_uid != new_uid)
186 ErrLog(<<
"Unable to drop privileges, not root!");
187 throw std::runtime_error(
"Unable to drop privileges, not root!");
193 if(mPidFile.
size() > 0)
195 if(chown(mPidFile.
c_str(), new_uid, new_gid) < 0)
197 ErrLog(<<
"Failed to change ownership of PID file");
201 rval = setuid(new_uid);
204 ErrLog(<<
"Unable to drop privileges, operation failed (setuid)");
205 throw std::runtime_error(
"Unable to drop privileges, operation failed");
218 if(mPidFile.
size() == 0)
225 std::ifstream _pid(mPidFile.
c_str(), std::ios_base::in);
234 StackLog(<< mPidFile <<
" contains PID " << running_pid);
236 Data ourProc =
Data(
"/proc/self/exe");
238 char our_exe[513], other_exe[513];
241 buf_size = readlink(ourProc.
c_str(), our_exe, 512);
242 if(buf_size < 0 || buf_size == 512)
247 our_exe[buf_size] = 0;
249 buf_size = readlink(otherProc.
c_str(), other_exe, 512);
250 if(buf_size < 0 || buf_size == 512)
255 other_exe[buf_size] = 0;
257 if(strcmp(our_exe, other_exe) == 0)
259 ErrLog(<<
"already running PID: " << running_pid);
277 if ((pid = fork()) < 0)
281 throw std::runtime_error(
strError(errno));
291 throw std::runtime_error(
strError(errno));
295 close(STDOUT_FILENO);
296 close(STDERR_FILENO);
298 if(mPidFile.
size() > 0)
300 std::ofstream _pid(mPidFile.
c_str(), std::ios_base::out | std::ios_base::trunc);
323 mReceivedHUP =
false;
332 sleepMs((
unsigned int)mWaitPeriod.count());
#define WarningLog(args_)
#define resip_assert(x)
This file needs to work for both C and C++ code.
static ServerProcess * _instance
static void signalHandler(int signo)
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
const char * c_str() const
Returns a null-terminated string representing.
bool empty() const noexcept
Checks whether the Data is empty.
size_type size() const noexcept
Returns the number of bytes in this Data.
static void reset()
Frees logger stream.
static void droppingPrivileges(uid_t uid, pid_t pid)
void dropPrivileges(const Data &runAsUser, const Data &runAsGroup)
ServerProcess(std::chrono::milliseconds waitPeriod=std::chrono::milliseconds(50))
void setPidFile(const Data &pidFile)
void installSignalHandler()
bool checkPosixProcessControl(const Data &dependentSettings)
dcm! – add UnusedChecking(_enum) below;
std::string strError(int err)