snapcast/common/daemon.h
(no author) a51c000a2d pid file
git-svn-id: svn://elaine/murooma/trunk@329 d8a302eb-03bc-478d-80e4-98257eca68ef
2014-12-29 14:12:03 +00:00

91 lines
1.7 KiB
C

#ifndef DAEMONIZE_H
#define DAEMONIZE_H
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>
int pidFilehandle;
void daemonize(const char *pidfile)
{
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0)
exit(EXIT_SUCCESS);
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Ensure only one copy */
pidFilehandle = open(pidfile, O_RDWR|O_CREAT, 0600);
if (pidFilehandle == -1 )
{
/* Couldn't open lock file */
syslog(LOG_INFO, "Could not open PID lock file %s, exiting", pidfile);
exit(EXIT_FAILURE);
}
/* Try to lock file */
if (lockf(pidFilehandle,F_TLOCK,0) == -1)
{
/* Couldn't get lock on lock file */
syslog(LOG_INFO, "Could not lock PID lock file %s, exiting", pidfile);
exit(EXIT_FAILURE);
}
char str[10];
/* Get and format PID */
sprintf(str, "%d\n", getpid());
/* write pid to lockfile */
if (write(pidFilehandle, str, strlen(str)) != (int)strlen(str))
{
/* Couldn't get lock on lock file */
syslog(LOG_INFO, "Could write PID to lock file %s, exiting", pidfile);
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
void daemonShutdown()
{
close(pidFilehandle);
}
#endif