summaryrefslogtreecommitdiff
path: root/src/kike.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2014-07-16 23:26:50 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2014-07-16 23:56:42 +0200
commita508f85bead4b86ceaf4464d8aba6230628a8722 (patch)
tree14553d883d8acc9adcbebe8d689844010316270e /src/kike.c
parent1842fa90dddd257b8b144a8592a893c8e101d3a1 (diff)
downloadxK-a508f85bead4b86ceaf4464d8aba6230628a8722.tar.gz
xK-a508f85bead4b86ceaf4464d8aba6230628a8722.tar.xz
xK-a508f85bead4b86ceaf4464d8aba6230628a8722.zip
Implement daemonization
Now we're a real daemon, yay.
Diffstat (limited to 'src/kike.c')
-rw-r--r--src/kike.c41
1 files changed, 36 insertions, 5 deletions
diff --git a/src/kike.c b/src/kike.c
index 974f80f..709d027 100644
--- a/src/kike.c
+++ b/src/kike.c
@@ -1304,6 +1304,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)
{
fprintf (stderr,
@@ -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)