From a508f85bead4b86ceaf4464d8aba6230628a8722 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Wed, 16 Jul 2014 23:26:50 +0200 Subject: Implement daemonization Now we're a real daemon, yay. --- src/kike.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/kike.c b/src/kike.c index 974f80f..709d027 100644 --- a/src/kike.c +++ b/src/kike.c @@ -1303,6 +1303,41 @@ on_signal_pipe_readable (const struct pollfd *fd, struct server_context *ctx) } } +static void +daemonize (void) +{ + // TODO: create and lock a PID file? + print_status ("daemonizing..."); + + if (chdir ("/")) + exit_fatal ("%s: %s", "chdir", strerror (errno)); + + pid_t pid; + if ((pid = fork ()) < 0) + exit_fatal ("%s: %s", "fork", strerror (errno)); + else if (pid) + exit (EXIT_SUCCESS); + + setsid (); + signal (SIGHUP, SIG_IGN); + + if ((pid = fork ()) < 0) + exit_fatal ("%s: %s", "fork", strerror (errno)); + else if (pid) + exit (EXIT_SUCCESS); + + openlog (PROGRAM_NAME, LOG_NDELAY | LOG_NOWAIT | LOG_PID, 0); + g_log_message_real = log_message_syslog; + + // XXX: we may close our own descriptors this way, crippling ourselves + for (int i = 0; i < 3; i++) + xclose (i); + + int tty = open ("/dev/null", O_RDWR); + if (tty != 0 || dup (0) != 1 || dup (0) != 2) + exit_fatal ("failed to reopen FD's: %s", strerror (errno)); +} + static void print_usage (const char *program_name) { @@ -1396,12 +1431,8 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - // TODO: daemonize if (!g_debug_mode) - { - openlog (PROGRAM_NAME, LOG_NDELAY | LOG_NOWAIT | LOG_PID, 0); - g_log_message_real = log_message_syslog; - } + daemonize (); ctx.polling = true; while (ctx.polling) -- cgit v1.2.3-70-g09d2