X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fstatserv.c;h=a502bee2e0235f1489c866667552c54cbeb238ec;hb=8aa7ee02ee4abdda2f19b42236600f2954ce6c05;hp=fc9b7fcc7c88ebfb4a7b6d7a21808ab5e8c565d6;hpb=8071f0286b3c551df57bc22b11125d00d12725e6;p=yaz-moved-to-github.git diff --git a/src/statserv.c b/src/statserv.c index fc9b7fc..a502bee 100644 --- a/src/statserv.c +++ b/src/statserv.c @@ -5,7 +5,7 @@ * 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.17 2005-01-03 11:56:08 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;