Bump year
[yaz-moved-to-github.git] / src / statserv.c
index fc9b7fc..ef149ac 100644 (file)
@@ -1,11 +1,11 @@
 /*
- * Copyright (c) 1995-2004, Index Data
+ * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
  *
  * NT threaded server code by
  *   Chas Woodfield, Fretwell Downing Informatics.
  *
- * $Id: statserv.c,v 1.14 2004-12-22 23:47:11 adam Exp $
+ * $Id: statserv.c,v 1.18 2005-01-15 19:47:14 adam Exp $
  */
 
 /**
@@ -465,7 +465,6 @@ static void listener(IOCHAN h, int event)
            pthread_t child_thread;
            pthread_create (&child_thread, 0, new_session, new_line);
            pthread_detach (child_thread);
-           new_session(new_line);
 #elif YAZ_GNU_THREADS
            pth_attr_t attr;
            pth_t child_thread;
@@ -539,8 +538,8 @@ static void *new_session (void *vp)
 #else
     a = 0;
 #endif
-    yaz_log(log_session, "Starting session %d from %s (pid=%d)",
-           no_sessions, a ? a : "[Unknown]", getpid());
+    yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
+           no_sessions, a ? a : "[Unknown]", (long) getpid());
     if (max_sessions && no_sessions >= max_sessions)
         control_block.one_shot = 1;
     if (control_block.threads)
@@ -713,25 +712,48 @@ int statserv_start(int argc, char **argv)
        inetd_connection(control_block.default_proto);
     else
     {
+       static int hand[2];
        if (control_block.background)
        {
+           /* create pipe so that parent waits until child has created
+              PID (or failed) */
+           if (pipe(hand) < 0)
+           {
+               yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
+               return 1;
+           }
            switch (fork())
            {
            case 0: 
                break;
            case -1:
                return 1;
-           default: 
-           _exit(0);
+           default:
+               close(hand[1]);
+               while(1)
+               {
+                   char dummy[1];
+                   int res = read(hand[0], dummy, 1);
+                   if (res < 0 && yaz_errno() != EINTR)
+                   {
+                       yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
+                       break;
+                   }
+                   else if (res >= 0)
+                       break;
+               }
+               close(hand[0]);
+               _exit(0);
            }
-           
+           /* child */
+           close(hand[0]);
            if (setsid() < 0)
                return 1;
            
            close(0);
            close(1);
            close(2);
-           open("/dev/null",O_RDWR);
+           open("/dev/null", O_RDWR);
            dup(0); dup(0);
        }
        if (!pListener && *control_block.default_listen)
@@ -754,7 +776,11 @@ int statserv_start(int argc, char **argv)
            fclose(f);
        }
 
-       yaz_log (log_server, "Starting server %s pid=%d", programname, getpid());
+       if (control_block.background)
+           close(hand[1]);
+
+       yaz_log (log_server, "Starting server %s pid=%ld", programname,
+                       (long) getpid());
         
 #if 0
        sigset_t sigs_to_block;