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.
mediator/daemonizer.c

51 lines
789 B

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.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;
int tmp;
setsid();
for(fds = getdtablesize(); fds >= 0; --fds)
{
if(fds != mediator_lockfd)
close(fds);
}
fds = open("/dev/null", O_RDWR); /* stdin */
tmp = dup(fds); /* stdout */
tmp = dup(fds); /* stderr */
umask(027);
tmp = chdir("/");
}
return 0;
}
int write_pid(const char *pidfile)
{
FILE *pfile = fopen(pidfile, "w");
if(pfile == NULL)
{
syslog(LOG_CRIT, "Error opening pid file '%s': %s", pidfile, strerror(errno));
return -1;
}
fprintf(pfile, "%d", getpid());
fclose(pfile);
return 0;
}