Happy new year
[yaz-moved-to-github.git] / src / statserv.c
index 00ba31c..ab15c21 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2008 Index Data
+ * Copyright (C) 1995-2011 Index Data
  * See the file LICENSE for details.
  */
 
@@ -8,6 +8,10 @@
  * \brief Implements GFS logic
  */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -42,8 +46,6 @@
 
 #if YAZ_POSIX_THREADS
 #include <pthread.h>
-#elif YAZ_GNU_THREADS
-#include <pth.h>
 #endif
 
 #include <fcntl.h>
@@ -53,6 +55,7 @@
 #include <yaz/comstack.h>
 #include <yaz/tcpip.h>
 #include <yaz/options.h>
+#include <yaz/errno.h>
 #ifdef USE_XTIMOSI
 #include <yaz/xmosi.h>
 #endif
@@ -60,6 +63,7 @@
 #include "eventl.h"
 #include "session.h"
 #include <yaz/statserv.h>
+#include <yaz/daemon.h>
 
 static IOCHAN pListener = NULL;
 
@@ -94,7 +98,7 @@ statserv_options_block control_block = {
     "",                         /* diagnostic output to stderr */
     "tcp:@:9999",               /* default listener port */
     PROTO_Z3950,                /* default application protocol */
-    15,                         /* idle timeout (minutes) */
+    900,                        /* idle timeout (seconds) */
     1024*1024,                  /* maximum PDU size (approx.) to allow */
     "default-config",           /* configuration name to pass to backend */
     "",                         /* set user id */
@@ -117,7 +121,8 @@ statserv_options_block control_block = {
     "",                         /* PID fname */
     0,                          /* background daemon */
     "",                         /* SSL certificate filename */
-    ""                          /* XML config filename */
+    "",                         /* XML config filename */
+    1                           /* keepalive */
 };
 
 static int max_sessions = 0;
@@ -852,22 +857,11 @@ static void listener(IOCHAN h, int event)
     }
 }
 
-int statserv_must_terminate(void)
-{
-    return 0;
-}
-
 #else /* ! WIN32 */
 
-static int term_flag = 0;
 /* To save having an #ifdef in event_loop we need to
    define this empty function 
 */
-int statserv_must_terminate(void)
-{
-    return term_flag;
-}
-
 void statserv_remove(IOCHAN pIOChannel)
 {
 }
@@ -884,11 +878,6 @@ static void statserv_closedown(void)
     xml_config_close();
 }
 
-void sigterm(int sig)
-{
-    term_flag = 1;
-}
-
 static void *new_session(void *vp);
 static int no_sessions = 0;
 
@@ -961,18 +950,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);
-#elif YAZ_GNU_THREADS
-            pth_attr_t attr;
-            pth_t child_thread;
-
-            attr = pth_attr_new();
-            pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE);
-            pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 32*1024);
-            pth_attr_set(attr, PTH_ATTR_NAME, "session");
-            yaz_log(YLOG_DEBUG, "pth_spawn begin");
-            child_thread = pth_spawn(attr, new_session, new_line);
-            yaz_log(YLOG_DEBUG, "pth_spawn finish");
-            pth_attr_destroy(attr);
 #else
             new_session(new_line);
 #endif
@@ -994,7 +971,7 @@ static void listener(IOCHAN h, int event)
 
 static void *new_session(void *vp)
 {
-    char *a;
+    const char *a;
     association *newas;
     IOCHAN new_chan;
     COMSTACK new_line = (COMSTACK) vp;
@@ -1062,7 +1039,7 @@ static void inetd_connection(int what)
     COMSTACK line;
     IOCHAN chan;
     association *assoc;
-    char *addr;
+    const char *addr;
 
     if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
     {
@@ -1184,7 +1161,11 @@ statserv_options_block *statserv_getcontrol(void)
 
 void statserv_setcontrol(statserv_options_block *block)
 {
-    chdir(gfs_root_dir);
+    if (gfs_root_dir[0])
+    {
+        if (chdir(gfs_root_dir))
+            yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
+    }
 #ifdef WIN32
     if (init_control_tls)
         TlsSetValue(current_control_tls, block);
@@ -1200,6 +1181,12 @@ static void statserv_reset(void)
 {
 }
 
+static void daemon_handler(void *data)
+{
+    IOCHAN *pListener = data;
+    iochan_event_loop(pListener);
+}
+
 static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
 {
     char sep;
@@ -1228,125 +1215,38 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
     
     xml_config_bend_start();
 
+    if (control_block.inetd)
+    {
 #ifdef WIN32
-    xml_config_add_listeners();
-
-    yaz_log(log_server, "Starting server %s", me);
-    if (!pListener && *control_block.default_listen)
-        add_listener(control_block.default_listen, 0);
+        ; /* no inetd on Windows */
 #else
-/* UNIX */
-    if (control_block.inetd)
         inetd_connection(control_block.default_proto);
+#endif
+    }
     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:
-                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);
-            dup(0); dup(0);
-        }
         xml_config_add_listeners();
 
         if (!pListener && *control_block.default_listen)
             add_listener(control_block.default_listen, 0);
-        
-        if (!pListener)
-            return 1;
 
-        if (*control_block.pid_fname)
-        {
-            FILE *f = fopen(control_block.pid_fname, "w");
-            if (!f)
-            {
-                yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s", 
-                        control_block.pid_fname);
-                exit(0);
-            }
-            fprintf(f, "%ld", (long) getpid());
-            fclose(f);
-        }
-        
-        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;
-        
-        sigemptyset(&sigs_to_block);
-        sigaddset(&sigs_to_block, SIGTERM);
-        pthread_sigmask(SIG_BLOCK, &sigs_to_block, 0);
-        /* missing... */
-#endif
+#ifndef WIN32
         if (control_block.dynamic)
             signal(SIGCHLD, catchchld);
-    }
-    signal(SIGPIPE, SIG_IGN);
-    signal(SIGTERM, sigterm);
-    if (*control_block.setuid)
-    {
-        struct passwd *pw;
-        
-        if (!(pw = getpwnam(control_block.setuid)))
-        {
-            yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
-            return(1);
-        }
-        if (setuid(pw->pw_uid) < 0)
-        {
-            yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
-            exit(1);
-        }
-    }
-/* UNIX */
 #endif
+    }
     if (pListener == NULL)
         return 1;
     if (s)
         yaz_sc_running(s);
     yaz_log(YLOG_DEBUG, "Entering event loop.");
-    return iochan_event_loop(&pListener);
+
+    yaz_daemon(programname,
+               (control_block.background ? YAZ_DAEMON_FORK : 0),
+               daemon_handler, &pListener,
+               *control_block.pid_fname ? control_block.pid_fname : 0,
+               *control_block.setuid ? control_block.setuid : 0);
+    return 0;
 }
 
 static void option_copy(char *dst, const char *src)
@@ -1364,7 +1264,7 @@ int check_options(int argc, char **argv)
 
     get_logbits(1); 
 
-    while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:m:r:",
+    while ((ret = options("1a:iszSTl:v:u:c:w:t:k:Kd:A:p:DC:f:m:r:",
                           argv, argc, &arg)) != -2)
     {
         switch (ret)
@@ -1391,9 +1291,6 @@ int check_options(int argc, char **argv)
 #if YAZ_POSIX_THREADS
             control_block.dynamic = 0;
             control_block.threads = 1;
-#elif YAZ_GNU_THREADS
-            control_block.dynamic = 0;
-            control_block.threads = 1;
 #else
             fprintf(stderr, "%s: Threaded mode not available.\n", me);
             return 1;
@@ -1435,7 +1332,7 @@ int check_options(int argc, char **argv)
                 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
                 return(1);
             }
-            control_block.idle_timeout = r;
+            control_block.idle_timeout = strchr(arg, 's') ? r : 60 * r;
             break;
         case  'k':
             if (!arg || !(r = atoi(arg)))
@@ -1445,6 +1342,9 @@ int check_options(int argc, char **argv)
             }
             control_block.maxrecordsize = r * 1024;
             break;
+        case 'K':
+            control_block.keepalive = 0;
+            break;
         case 'i':
             control_block.inetd = 1;
             break;
@@ -1524,6 +1424,7 @@ int statserv_main(int argc, char **argv,
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab