Added Segment request for fronend server. Work on admin for client.
[yaz-moved-to-github.git] / server / statserv.c
index 000ebd8..5717e78 100644 (file)
@@ -7,7 +7,19 @@
  *   Chas Woodfield, Fretwell Downing Datasystems.
  *
  * $Log: statserv.c,v $
- * Revision 1.59  1999-11-30 13:47:12  adam
+ * Revision 1.63  2000-03-20 19:06:25  adam
+ * Added Segment request for fronend server. Work on admin for client.
+ *
+ * Revision 1.62  2000/03/17 12:47:02  adam
+ * Minor changes to admin client.
+ *
+ * Revision 1.61  2000/03/15 12:59:49  adam
+ * Added handle member to statserv_control.
+ *
+ * Revision 1.60  2000/03/14 09:06:11  adam
+ * Added POSIX threads support for frontend server.
+ *
+ * Revision 1.59  1999/11/30 13:47:12  adam
  * Improved installation. Moved header files to include/yaz.
  *
  * Revision 1.58  1999/08/27 09:40:32  adam
 #include <direct.h>
 #include "service.h"
 #else
+#if HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
 #include <unistd.h>
 #include <pwd.h>
 #endif
@@ -242,6 +257,7 @@ static char *me = "statserver";
 int check_options(int argc, char **argv);
 statserv_options_block control_block = {
     1,                          /* dynamic mode */
+    0,                          /* threaded mode */
     0,                          /* one shot (single session) */
     LOG_DEFAULT_LEVEL,          /* log level */
     "",                         /* no PDUs */
@@ -257,7 +273,8 @@ statserv_options_block control_block = {
     check_options,              /* Default routine, for checking the run-time arguments */
     check_ip_tcpd,
     "",
-    0                           /* default value for inet deamon */
+    0,                          /* default value for inet deamon */
+    0,                          /* handle (for service, etc) */
 
 #ifdef WIN32
     ,"Z39.50 Server",           /* NT Service Name */
@@ -530,14 +547,25 @@ void statserv_remove(IOCHAN pIOChannel)
 void statserv_closedown()
 {
     IOCHAN p;
+
+    if (control_block.bend_stop)
+        (*control_block.bend_stop)(&control_block);
+
     for (p = pListener; p; p = p->next)
         iochan_destroy(p);
 }
 
+void sigterm(int sig)
+{
+    statserv_closedown();
+    exit (0);
+}
+
+static void *new_session (void *vp);
+
 static void listener(IOCHAN h, int event)
 {
     COMSTACK line = (COMSTACK) iochan_getdata(h);
-    association *newas;
     static int hand[2];
     static int child = 0;
     int res;
@@ -617,8 +645,6 @@ static void listener(IOCHAN h, int event)
     else if (event == EVENT_OUTPUT)
     {
        COMSTACK new_line;
-       IOCHAN new_chan;
-       char *a;
 
        if (!(new_line = cs_accept(line)))
        {
@@ -643,26 +669,19 @@ static void listener(IOCHAN h, int event)
        }
        else
            iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
-       
-       if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
-           EVENT_INPUT)))
-       {
-           yaz_log(LOG_FATAL, "Failed to create iochan");
-            iochan_destroy(h);
-            return;
-       }
-        new_chan->next = pListener;
-        pListener = new_chan;
-       if (!(newas = create_association(new_chan, new_line)))
+
+#if HAVE_PTHREAD_H
+       if (control_block.threads)
        {
-           yaz_log(LOG_FATAL, "Failed to create new assoc.");
-            iochan_destroy(h);
-            return;
+           pthread_t child_thread;
+           pthread_create (&child_thread, 0, new_session, new_line);
+           pthread_detach (child_thread);
        }
-       iochan_setdata(new_chan, newas);
-       iochan_settimeout(new_chan, control_block.idle_timeout * 60);
-       a = cs_addrstr(new_line);
-       yaz_log(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
+       else
+           new_session(new_line);
+#else
+       new_session(new_line);
+#endif
     }
     else
     {
@@ -672,6 +691,39 @@ static void listener(IOCHAN h, int event)
     }
 }
 
+static void *new_session (void *vp)
+{
+    char *a;
+    association *newas;
+    IOCHAN new_chan;
+    COMSTACK new_line = (COMSTACK) vp;
+    if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
+                                  EVENT_INPUT)))
+    {
+       yaz_log(LOG_FATAL, "Failed to create iochan");
+       return 0;
+    }
+    if (!(newas = create_association(new_chan, new_line)))
+    {
+       yaz_log(LOG_FATAL, "Failed to create new assoc.");
+       return 0;
+    }
+    iochan_setdata(new_chan, newas);
+    iochan_settimeout(new_chan, control_block.idle_timeout * 60);
+    a = cs_addrstr(new_line);
+    yaz_log(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
+    if (control_block.threads)
+    {
+       event_loop(&new_chan);
+    }
+    else
+    {
+       new_chan->next = pListener;
+       pListener = new_chan;
+    }
+    return 0;
+}
+
 #endif /* WIN32 */
 
 static void inetd_connection(int what)
@@ -743,7 +795,8 @@ static void add_listener(char *where, int what)
        return;
     }
     yaz_log(LOG_LOG, "Adding %s %s listener on %s",
-        control_block.dynamic ? "dynamic" : "static",
+           control_block.dynamic ? "dynamic" : 
+           (control_block.threads ? "threaded" : "static"),
        what == PROTO_SR ? "SR" : "Z3950", where);
     if (!(l = cs_create(type, 0, what)))
     {
@@ -815,8 +868,10 @@ int statserv_start(int argc, char **argv)
        me++;
     else
        me = argv[0];
+    logf (LOG_LOG, "Starting server %s", me);
 #else
     me = argv[0];
+    logf (LOG_LOG, "Starting server %s pid=%d", me, getpid());
 #endif
     if (control_block.options_func(argc, argv))
         return(1);
@@ -828,9 +883,21 @@ int statserv_start(int argc, char **argv)
        inetd_connection(control_block.default_proto);
     else
     {
+#if 0
+       sigset_t sigs_to_block;
+
+       sigemptyset(&sigs_to_block);
+       sigaddset (&sigs_to_block, SIGTERM);
+       pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
+       pthread_create (&
+
+
+#endif
        if (control_block.dynamic)
            signal(SIGCHLD, catchchld);
     }
+    
+    signal (SIGTERM, sigterm);
     if (*control_block.setuid)
     {
        struct passwd *pw;
@@ -868,7 +935,7 @@ int check_options(int argc, char **argv)
     int ret = 0, r;
     char *arg;
 
-    while ((ret = options("1a:iszSl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
+    while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
     {
        switch (ret)
        {
@@ -888,6 +955,15 @@ int check_options(int argc, char **argv)
        case 'S':
            control_block.dynamic = 0;
            break;
+       case 'T':
+#if HAVE_PTHREAD_H
+           control_block.dynamic = 0;
+           control_block.threads = 1;
+#else
+           fprintf(stderr, "%s: Threaded mode not available.\n", me);
+           return 1;
+#endif
+           break;
        case 'l':
            strcpy(control_block.logfile, arg ? arg : "");
            log_init(control_block.loglevel, me, control_block.logfile);
@@ -931,15 +1007,15 @@ int check_options(int argc, char **argv)
            if (chdir(arg))
            {
                perror(arg);            
-               return(1);
+               return 1;
            }
            break;
        default:
-           fprintf(stderr, "Usage: %s [ -i -a <pdufile> -v <loglevel>"
+           fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
                    " -l <logfile> -u <user> -c <config> -t <minutes>"
                    " -k <kilobytes> -d <daemon>"
-                        " -zsS <listener-addr> -w <directory> ... ]\n", me);
-           return(1);
+                        " -zsiST -w <directory> <listender-addr>... ]\n", me);
+           return 1;
         }
     }
     return 0;