/*** This file is part of snapcast Copyright (C) 2014-2016 Johannes Pohl This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ***/ #ifndef DAEMONIZE_H #define DAEMONIZE_H #include #include #include #include #include #include #include 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