mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-31 09:56:16 +02:00
91 lines
1.7 KiB
C
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
|
|
|
|
|