// daemonize.c // PUBLIC DOMAIN // http:///www.purposeful.co.uk/software/yabaplib /* I, Tom Vajzovic, am the author of this software and its documentation. I permanently abandon all intellectual property rights in them, including copyright, trademarks, design rights, database right, patents, and the right to be identified as the author. I am fairly certain that the software does what the documentation says it does, but I do not guarantee that it does, or that it does what you think it should. I do not guarantee that it will not have undesirable side effects. You are free to use, modify and distribute this software as you please, but you do so at your own risk. If you do not pass on this warning then you may be responsible for any problems encountered by those who obtain the software through you. */ #include #include #include #include #include #include #include #include #include #include #include #include "fileutil.h" #include "sigactions.h" #include "daemonize.h" struct message_error { const char *message; int error_number; }; // send a string pointer and errno value down the pipe static void send_message_error(int fd, const char *message, int error_number) { struct message_error to_send = { .message = message, .error_number = error_number }; (void)write_blocking(fd, &to_send, sizeof to_send); } // output error message received to stderr // return zero on empty pipe, positive on message received // on error output own error message to stderr and return negative // always close fd static int receive_message_error(int fd) { struct message_error sent; ssize_t readed = read_blocking_close(fd, &sent, sizeof sent); if (readed == 0) { return 0; } else if (readed < 0) { perror("read pipe"); return -1; } else if (readed < sizeof sent) { errno = ENOMSG; perror("read pipe"); return -1; } else { errno = sent.error_number; perror(sent.message); return 1; } } void daemonize(enum nochdir nochdir, enum noclose noclose) { /*######################################################################*/ /* */ /* Environment */ /* */ /*######################################################################*/ // NB: the envirnment is NOT sanitized at this point // but future calls to exec should specify a new environment /*######################################################################*/ /* */ /* Permissions, Capabilities etc. */ /* */ /*######################################################################*/ // NB: (uid,gid,euid,egid) are NOT checked or altered // see: `man 2 setuid` etc // see: `man prctl` for capabilities /*######################################################################*/ /* */ /* File system */ /* */ /*######################################################################*/ // umask is not changed, you may want to set it to zero // filsystem root is not changed, see `man chroot` if (nochdir == DAEMONIZE_CHDIR_ROOT) { if (chdir("/")) { perror("chdir \"/\""); exit(EXIT_FAILURE); } } if (noclose == DAEMONIZE_CLOSE_STDIO) { // open /dev/null on standard input if (null_input()) { perror("open /dev/null"); exit(EXIT_FAILURE); } // open /dev/null on standard output if (null_output()) { perror("open /dev/null"); exit(EXIT_FAILURE); } } /*######################################################################*/ /* */ /* First fork */ /* */ /*######################################################################*/ int pipe_fds[2]; if (pipe(pipe_fds)) { perror("create pipe"); exit(EXIT_FAILURE); } pid_t child_pid = fork(); if (child_pid < 0) { perror("fork"); exit(EXIT_FAILURE); } /*######################################################################*/ /* */ /* First fork (parent) */ /* */ /*######################################################################*/ if (child_pid > 0) { // THIS IS THE PARENT // just read the error stream until it closes // close the write end of the pipe in the parent if (close(pipe_fds[1])) { perror("close pipe"); (void)kill(+child_pid, SIGKILL); (void)kill(-child_pid, SIGKILL); _Exit(EXIT_FAILURE); } // copy from the pipe to the parent's standard error if (receive_message_error(pipe_fds[0])) { (void)kill(+child_pid, SIGKILL); (void)kill(-child_pid, SIGKILL); _Exit(EXIT_FAILURE); } int wait_status; pid_t wait_result = waitpid(child_pid, &wait_status, 0); if (wait_result != child_pid) { // this shouldn't happen, best not to kill the child in case its pid has been reused perror("wait for child"); _Exit(EXIT_FAILURE); } if (WIFEXITED(wait_status)) { int status = WEXITSTATUS(wait_status); if (status != EXIT_SUCCESS) { fputs("intermediate child process exited with failure status\n", stderr); } _Exit(status); } else if (WIFSIGNALED(wait_status)) { int signum = WTERMSIG(wait_status); fputs("intermediate child process killed by signal\n", stderr); _Exit(128 + signum); } else { // this shouldn't happen fputs("unexpected status from intermediate child\n", stderr); _Exit(EXIT_FAILURE); } } /*######################################################################*/ /* */ /* First fork (child - intermediate process) */ /* */ /*######################################################################*/ // close read end of error message pipe in child if (close(pipe_fds[0])) { send_message_error(pipe_fds[1], "close pipe", errno); _Exit(EXIT_FAILURE); } if (noclose == DAEMONIZE_CLOSE_STDIO) { // put /dev/null on stderr too if (copy_fd(STDOUT_FILENO, STDERR_FILENO)) { send_message_error(pipe_fds[1], "dup stdout to stderr", errno); _Exit(EXIT_FAILURE); } } // now that we are not a process group leader, we can become a session leader of a new session, // (and automatically leader of process group that belongs to that session) if (setsid() < 0) { send_message_error(pipe_fds[1], "set session-id", errno); _Exit(EXIT_FAILURE); } pid_t grandchild_pid = fork(); if (grandchild_pid < 0) { send_message_error(pipe_fds[1], "fork", errno); _Exit(EXIT_FAILURE); } if (grandchild_pid > 0) { // STILL IN CHILD // nothing to do _Exit(EXIT_SUCCESS); } /*######################################################################*/ /* */ /* Second fork (grandchild - new daemon process) */ /* */ /*######################################################################*/ (void)close(pipe_fds[1]); } void daemon_signals(void) { /*######################################################################*/ /* */ /* Signal handling */ /* */ /*######################################################################*/ // SIGKILL // SIGSTOP // can't be handled or blocked // SIGINT // SIGQUIT // SIGTSTP // SIGTTIN // SIGTTOU // SIGWINCH // all ignored (shouldn't get terminal signals because we close our controlling terminal) sig_ignore_unblock(SIGINT); sig_ignore_unblock(SIGQUIT); sig_ignore_unblock(SIGTSTP); sig_ignore_unblock(SIGTTIN); sig_ignore_unblock(SIGTTOU); sig_ignore_unblock(SIGWINCH); // SIGPIPE // ignored because pipe errors should be handled using errno sig_ignore_unblock(SIGPIPE); // SIGHUP // ignored for now, install custom handler later to reload configuration sig_ignore_unblock(SIGHUP); // SIGTERM // ignored for now, install handler later to gracefully exit sig_ignore_unblock(SIGTERM); // SIGCHILD // set to default behaviour (doesn't call a handler but keeps the child status until wait is called) sig_default_unblock(SIGCHLD); // SIGCONT // set to default behaviour (doesn't call a handler but wakes up the process if stopped) sig_default_unblock(SIGCONT); // SIGILL // SIGTRAP // SIGBUS // SIGFPE // SIGSEGV // SIGSTKFLT // SIGPWR // hardware error signals, leave at default behaviour (terminate) // SIGSYS // SIGXCPU // SIGXFSZ // SIGABRT // software error signals, leave at default behaviour (terminate) // SIGIO // SIGURG // SIGALRM // SIGVTALRM // SIGPROF // SIGUSR1 // SIGUSR2 // signals triggered by software features not used in this program, // leave at default behaviour (ignore or terminate) }