From e45c8be4da9b9500a749a97d4b0aaae9500f5e5c Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 17 Dec 2010 17:26:57 -0800 Subject: [PATCH] guacd should fork self into background, like any self-respecting daemon. --- guacd/src/daemon.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/guacd/src/daemon.c b/guacd/src/daemon.c index b8e545a1..b0bc350f 100644 --- a/guacd/src/daemon.c +++ b/guacd/src/daemon.c @@ -85,6 +85,9 @@ int main(int argc, char* argv[]) { int listen_port = 4822; /* Default port */ int opt; + /* Daemon Process */ + pid_t daemon_pid; + /* Parse arguments */ while ((opt = getopt(argc, argv, "l:")) != -1) { if (opt == 'l') { @@ -120,8 +123,22 @@ int main(int argc, char* argv[]) { exit(EXIT_FAILURE); } - syslog(LOG_INFO, "Started, listening on port %i", listen_port); + /* Fork into background */ + daemon_pid = fork(); + /* If error, fail */ + if (daemon_pid == -1) { + fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + /* If parent, exit */ + else if (daemon_pid != 0) { + exit(EXIT_SUCCESS); + } + + /* Otherwise, this is the daemon */ + syslog(LOG_INFO, "Started, listening on port %i", listen_port); /* Daemon loop */ for (;;) {