mirror of https://github.com/sipwise/mediator.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.4 KiB
63 lines
1.4 KiB
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
#include "config.h"
|
|
#include "daemonizer.h"
|
|
|
|
int daemonize()
|
|
{
|
|
pid_t pid = fork();
|
|
if(pid < 0)
|
|
{
|
|
return -1;
|
|
}
|
|
else if(pid > 0)
|
|
{
|
|
_exit(0);
|
|
}
|
|
else if(pid == 0)
|
|
{
|
|
int fds;
|
|
setsid();
|
|
for(fds = getdtablesize(); fds >= 3; --fds)
|
|
{
|
|
if(fds != mediator_lockfd)
|
|
close(fds);
|
|
}
|
|
if (freopen("/dev/null", "r", stdin) == NULL) {
|
|
L_CRITICAL("Failed to reopen stdin to /dev/null: %s", strerror(errno));
|
|
return -1;
|
|
}
|
|
if (freopen("/dev/null", "w", stdout) == NULL) {
|
|
L_CRITICAL("Failed to reopen stdout to /dev/null: %s", strerror(errno));
|
|
return -1;
|
|
}
|
|
if (freopen("/dev/null", "w", stderr) == NULL) {
|
|
L_CRITICAL("Failed to reopen stderr to /dev/null: %s", strerror(errno));
|
|
return -1;
|
|
}
|
|
umask(027);
|
|
if(chdir("/") < 0) {
|
|
L_CRITICAL("Failed to chdir to root: %s", strerror(errno));
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int write_pid(const char *pidfile)
|
|
{
|
|
FILE *pfile = fopen(pidfile, "w");
|
|
if(pfile == NULL)
|
|
{
|
|
L_CRITICAL("Error opening pid file '%s': %s", pidfile, strerror(errno));
|
|
return -1;
|
|
}
|
|
fprintf(pfile, "%d", getpid());
|
|
fclose(pfile);
|
|
return 0;
|
|
}
|