123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <unistd.h>
- #include <errno.h>
- #include "private-libwebsockets.h"
- int pid_daemon;
- static char *lock_path;
- int get_daemonize_pid()
- {
- return pid_daemon;
- }
- static void
- child_handler(int signum)
- {
- int fd, len, sent;
- char sz[20];
- switch (signum) {
- case SIGALRM:
- exit(0);
- break;
- case SIGUSR1:
- if (lock_path) {
-
- fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640);
- if (fd < 0) {
- fprintf(stderr,
- "unable to create lock file %s, code=%d (%s)\n",
- lock_path, errno, strerror(errno));
- exit(0);
- }
- len = sprintf(sz, "%u", pid_daemon);
- sent = write(fd, sz, len);
- if (sent != len)
- fprintf(stderr,
- "unable to write pid to lock file %s, code=%d (%s)\n",
- lock_path, errno, strerror(errno));
- close(fd);
- }
- exit(0);
-
- case SIGCHLD:
- exit(0);
- break;
- }
- }
- static void lws_daemon_closing(int sigact)
- {
- if (getpid() == pid_daemon)
- if (lock_path) {
- unlink(lock_path);
- lws_free_set_NULL(lock_path);
- }
- kill(getpid(), SIGKILL);
- }
- LWS_VISIBLE int
- lws_daemonize(const char *_lock_path)
- {
- struct sigaction act;
- pid_t sid, parent;
- int n, fd, ret;
- char buf[10];
-
- if (_lock_path) {
- fd = open(_lock_path, O_RDONLY);
- if (fd >= 0) {
- n = read(fd, buf, sizeof(buf));
- close(fd);
- if (n) {
- n = atoi(buf);
- ret = kill(n, 0);
- if (ret >= 0) {
- fprintf(stderr,
- "Daemon already running from pid %d\n", n);
- exit(1);
- }
- fprintf(stderr,
- "Removing stale lock file %s from dead pid %d\n",
- _lock_path, n);
- unlink(lock_path);
- }
- }
- n = strlen(_lock_path) + 1;
- lock_path = lws_malloc(n);
- if (!lock_path) {
- fprintf(stderr, "Out of mem in lws_daemonize\n");
- return 1;
- }
- strcpy(lock_path, _lock_path);
- }
-
- signal(SIGCHLD, child_handler);
- signal(SIGUSR1, child_handler);
- signal(SIGALRM, child_handler);
-
- pid_daemon = fork();
- if (pid_daemon < 0) {
- fprintf(stderr, "unable to fork daemon, code=%d (%s)",
- errno, strerror(errno));
- exit(9);
- }
-
- if (pid_daemon > 0) {
-
- alarm(2);
- pause();
-
- exit(1);
- }
-
- parent = getppid();
- pid_daemon = getpid();
-
- signal(SIGCHLD, SIG_DFL);
- signal(SIGTSTP, SIG_IGN);
- signal(SIGTTOU, SIG_IGN);
- signal(SIGTTIN, SIG_IGN);
- signal(SIGHUP, SIG_IGN);
-
- umask(0);
-
- sid = setsid();
- if (sid < 0) {
- fprintf(stderr,
- "unable to create a new session, code %d (%s)",
- errno, strerror(errno));
- exit(2);
- }
-
- if (chdir("/tmp") < 0) {
- fprintf(stderr,
- "unable to change directory to %s, code %d (%s)",
- "/", errno, strerror(errno));
- exit(3);
- }
-
- if (!freopen("/dev/null", "r", stdin))
- fprintf(stderr, "unable to freopen() stdin, code %d (%s)",
- errno, strerror(errno));
- if (!freopen("/dev/null", "w", stdout))
- fprintf(stderr, "unable to freopen() stdout, code %d (%s)",
- errno, strerror(errno));
- if (!freopen("/dev/null", "w", stderr))
- fprintf(stderr, "unable to freopen() stderr, code %d (%s)",
- errno, strerror(errno));
-
- kill(parent, SIGUSR1);
- act.sa_handler = lws_daemon_closing;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- sigaction(SIGTERM, &act, NULL);
-
- return 0;
- }
|