Detach main thread.
[guacd.git] / src / daemon.c
index c734061..4364eee 100644 (file)
@@ -1,21 +1,39 @@
 
-/*
- *  Guacamole - Clientless Remote Desktop
- *  Copyright (C) 2010  Michael Jumper
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU Affero General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU Affero General Public License for more details.
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
  *
- *  You should have received a copy of the GNU Affero General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
+ * The Original Code is guacd.
+ *
+ * The Initial Developer of the Original Code is
+ * Michael Jumper.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * 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
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
 
 #include <unistd.h>
 #include <stdlib.h>
@@ -117,6 +135,8 @@ int main(int argc, char* argv[]) {
     int listen_port = 4822; /* Default port */
     int opt;
 
+    char* pidfile = NULL;
+
     /* Daemon Process */
     pid_t daemon_pid;
 
@@ -126,7 +146,7 @@ int main(int argc, char* argv[]) {
 #endif
 
     /* Parse arguments */
-    while ((opt = getopt(argc, argv, "l:")) != -1) {
+    while ((opt = getopt(argc, argv, "l:p:")) != -1) {
         if (opt == 'l') {
             listen_port = atoi(optarg);
             if (listen_port <= 0) {
@@ -134,8 +154,11 @@ int main(int argc, char* argv[]) {
                 exit(EXIT_FAILURE);
             }
         }
+        else if (opt == 'p') {
+            pidfile = strdup(optarg);
+        }
         else {
-            fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
+            fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
             exit(EXIT_FAILURE);
         }
     }
@@ -173,7 +196,7 @@ int main(int argc, char* argv[]) {
     } 
 
     /* Fork into background */
-#ifdef fork
+#ifdef HAVE_FORK
     daemon_pid = fork();
 
     /* If error, fail */
@@ -182,8 +205,26 @@ int main(int argc, char* argv[]) {
         exit(EXIT_FAILURE);
     }
 
-    /* If parent, exit */
+    /* If parent, write PID file and exit */
     else if (daemon_pid != 0) {
+
+        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 {
+                fprintf(stderr, "WARNING: Could not write PID file: %s\n", lasterror());
+                exit(EXIT_FAILURE);
+            }
+
+        }
+
         exit(EXIT_SUCCESS);
     }
 #else
@@ -224,6 +265,8 @@ int main(int argc, char* argv[]) {
             GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
             return 3;
         }
+
+        pthread_detach(thread);
 #elif __MINGW32__
         if (_beginthread(start_client_thread, 0, (void*) data) == -1L) { 
             GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());