]> git.cworth.org Git - ttt/blobdiff - src/ttt-server.c
2005-12-08 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-server.c
index c94d3d23a44c5fc3d624b6d87bd30ddd7c9fa0b4..34159b547ddb0b0946ef97d5a655646259223c07 100644 (file)
@@ -162,7 +162,7 @@ ttt_error_t
 ttt_server_statistics (ttt_server_t *server, const char *username, char **response)
 {
     ttt_bool_t usernamefound = FALSE;
 ttt_server_statistics (ttt_server_t *server, const char *username, char **response)
 {
     ttt_bool_t usernamefound = FALSE;
-    char *client_username;
+    const char *client_username;
     int client_num_wins;
     int i;
 
     int client_num_wins;
     int i;
 
@@ -193,7 +193,7 @@ ttt_error_t
 ttt_server_verify_username (ttt_server_t *server, const char *username)
 {
     ttt_bool_t usernamefound = FALSE;
 ttt_server_verify_username (ttt_server_t *server, const char *username)
 {
     ttt_bool_t usernamefound = FALSE;
-    char *client_username;
+    const char *client_username;
     int i;
 
     pthread_mutex_lock (&server->mutex);
     int i;
 
     pthread_mutex_lock (&server->mutex);
@@ -248,6 +248,35 @@ _ttt_server_accept (void *closure, int client_socket)
     ttt_client_new (server, client_socket);
 }
 
     ttt_client_new (server, client_socket);
 }
 
+static void
+_detach_and_write_child_pid_to (const char *filename)
+{
+    pid_t pid;
+
+    /* Use the Unix double-fork trick to detach completely. See
+     * setsid(2) for some details as to why two forks are
+     * needed. */
+    pid = xfork ();
+    if (pid) {
+       /* First parent just exits */
+       exit (0);
+    }
+    
+    chdir ("/");
+    setsid ();
+    
+    pid = xfork ();
+    if (pid) {
+       /* Second parent exits after writing pid */
+       FILE *file = xfopen (filename, "w");
+       fprintf (file, "%d\n", pid);
+       fclose (file);
+       exit (0);
+    }
+    
+    /* Final, detached child returns. */
+}
+
 int 
 main (int argc, char **argv)
 {
 int 
 main (int argc, char **argv)
 {
@@ -257,12 +286,34 @@ main (int argc, char **argv)
 
     ttt_args_parse (&args, argc, argv);
 
 
     ttt_args_parse (&args, argc, argv);
 
-    if (args.log_file)
-       xfreopen (args.log_file, "a", stderr);
+    if (args.log_file || args.detach) {
+       FILE *log_file;
+       /* In the detach case, we force redirection to a log file. */
+       if (args.log_file == NULL)
+           args.log_file = "/var/log/ttt-server.log";
+       log_file = fopen (args.log_file, "a");
+       if (log_file == NULL) {
+           printf ("Warning: Failed to open log file %s: %s.\n",
+                   args.log_file, strerror (errno));
+           printf ("Logging will be disabled.\n");
+           xdup2 (1, 2);
+       } else {
+           xdup2 (fileno (log_file), 2);
+       }
+    }
+
+    if (args.detach)
+       _detach_and_write_child_pid_to (args.pid_file);
 
     socket = ttt_socket_create_server (args.host, args.port);
 
 
     socket = ttt_socket_create_server (args.host, args.port);
 
-    printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
+    if (args.detach)
+       printf ("Server started listening on %s:%s\n", args.host, args.port);
+    else
+       printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
+
+    fclose (stdout);
+    fclose (stdin);
 
     ttt_server_init (&server, args.host, args.port);
 
 
     ttt_server_init (&server, args.host, args.port);