Style fixes, fix usage string.
[guacd.git] / src / daemon.c
index d227501..a727dc6 100644 (file)
@@ -20,6 +20,7 @@
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
+ * David PHAM-VAN <d.pham-van@ulteo.com> Ulteo SAS - http://www.ulteo.com
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -48,6 +49,7 @@
 
 #include <errno.h>
 #include <syslog.h>
+#include <libgen.h>
 
 #include <guacamole/client.h>
 #include <guacamole/error.h>
@@ -198,6 +200,7 @@ int main(int argc, char* argv[]) {
     char* listen_port = "4822";  /* Default port */
     char* pidfile = NULL;
     int opt;
+    int foreground = 0;
 
     /* General */
     int retval;
@@ -206,13 +209,16 @@ int main(int argc, char* argv[]) {
     pid_t daemon_pid;
 
     /* Parse arguments */
-    while ((opt = getopt(argc, argv, "l:b:p:")) != -1) {
+    while ((opt = getopt(argc, argv, "l:b:p:f")) != -1) {
         if (opt == 'l') {
             listen_port = strdup(optarg);
         }
         else if (opt == 'b') {
             listen_address = strdup(optarg);
         }
+        else if (opt == 'f') {
+            foreground = 1;
+        }
         else if (opt == 'p') {
             pidfile = strdup(optarg);
         }
@@ -221,12 +227,17 @@ int main(int argc, char* argv[]) {
             fprintf(stderr, "USAGE: %s"
                     " [-l LISTENPORT]"
                     " [-b LISTENADDRESS]"
-                    " [-p PIDFILE]\n", argv[0]);
+                    " [-p PIDFILE]",
+                    " [-f]\n", argv[0]);
 
             exit(EXIT_FAILURE);
         }
     }
 
+    /* Set up logging prefix */
+    strncpy(log_prefix, basename(argv[0]), sizeof(log_prefix));
+
+
     /* Get addresses for binding */
     if ((retval = getaddrinfo(listen_address, listen_port, &hints, &addresses))) {
         guacd_log_error("Error parsing given address or port: %s",
@@ -266,7 +277,7 @@ int main(int argc, char* argv[]) {
                     current_address->ai_addr,
                     current_address->ai_addrlen) == 0) {
 
-            guacd_log_error("Successfully bound socket to "
+            guacd_log_info("Successfully bound socket to "
                     "host %s, port %s", bound_address, bound_port);
 
             /* Done if successful bind */
@@ -274,9 +285,9 @@ int main(int argc, char* argv[]) {
 
         }
 
-        /* Otherwise log error */
+        /* Otherwise log information regarding bind failure */
         else
-            guacd_log_error("Error binding socket to "
+            guacd_log_info("Unable to bind socket to "
                     "host %s, port %s: %s",
                     bound_address, bound_port, strerror(errno));
 
@@ -290,36 +301,39 @@ int main(int argc, char* argv[]) {
         exit(EXIT_FAILURE);
     }
 
-    /* Fork into background */
-    daemon_pid = fork();
+    if (!foreground) {
 
-    /* If error, fail */
-    if (daemon_pid == -1) {
-        guacd_log_error("Error forking daemon process: %s", strerror(errno));
-        exit(EXIT_FAILURE);
-    }
+        /* Fork into background */
+        daemon_pid = fork();
 
-    /* If parent, write PID file and exit */
-    else if (daemon_pid != 0) {
+        /* If error, fail */
+        if (daemon_pid == -1) {
+            guacd_log_error("Error forking daemon process: %s", strerror(errno));
+            exit(EXIT_FAILURE);
+        }
 
-        if (pidfile != NULL) {
+        /* If parent, write PID file and exit */
+        else if (daemon_pid != 0) {
 
-            /* Attempt to open pidfile and write PID */
-            FILE* pidf = fopen(pidfile, "w");
-            if (pidf) {
-                fprintf(pidf, "%d\n", daemon_pid);
-                fclose(pidf);
-            }
+            if (pidfile != NULL) {
+
+                /* Attempt to open pidfile and write PID */
+                FILE* pidf = fopen(pidfile, "w");
+                if (pidf) {
+                    fprintf(pidf, "%d\n", daemon_pid);
+                    fclose(pidf);
+                }
+
+                /* Warn on failure */
+                else {
+                    guacd_log_error("Could not write PID file: %s", strerror(errno));
+                    exit(EXIT_FAILURE);
+                }
 
-            /* Warn on failure */
-            else {
-                guacd_log_error("Could not write PID file: %s", strerror(errno));
-                exit(EXIT_FAILURE);
             }
 
+            exit(EXIT_SUCCESS);
         }
-
-        exit(EXIT_SUCCESS);
     }
 
     /* Open log */
@@ -327,12 +341,12 @@ int main(int argc, char* argv[]) {
 
     /* Ignore SIGPIPE */
     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
-        guacd_log_error("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
+        guacd_log_info("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
     }
 
     /* Ignore SIGCHLD (force automatic removal of children) */
     if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
-        guacd_log_error("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
+        guacd_log_info("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
     }
 
     /* Log listening status */