record_conv: change construct prototype
[yaz-moved-to-github.git] / src / statserv.c
index 4def68a..720ec08 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2010 Index Data
+ * Copyright (C) 1995-2012 Index Data
  * See the file LICENSE for details.
  */
 
@@ -8,10 +8,13 @@
  * \brief Implements GFS logic
  */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 
 #ifdef WIN32
 #include <process.h>
@@ -60,6 +63,7 @@
 #include "session.h"
 #include <yaz/statserv.h>
 #include <yaz/daemon.h>
+#include <yaz/yaz-iconv.h>
 
 static IOCHAN pListener = NULL;
 
@@ -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;
@@ -190,7 +195,7 @@ static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
             cp = p->content;
             if (first)
             {
-                while(*cp && isspace(*cp))
+                while(*cp && yaz_isspace(*cp))
                     cp++;
                 if (*cp)
                     first = 0;  /* reset if we got non-whitespace out */
@@ -200,7 +205,7 @@ static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
     }
     /* remove trailing whitespace */
     cp = strlen((const char *)str) + str;
-    while (cp != str && isspace(cp[-1]))
+    while (cp != str && yaz_isspace(cp[-1]))
         cp--;
     *cp = '\0';
     /* return resulting string */
@@ -209,7 +214,7 @@ static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
 #endif
 
 #if YAZ_HAVE_XML2
-static struct gfs_server * gfs_server_new(void)
+static struct gfs_server * gfs_server_new(const char *id)
 {
     struct gfs_server *n = (struct gfs_server *)
         nmem_malloc(gfs_nmem, sizeof(*n));
@@ -223,6 +228,7 @@ static struct gfs_server * gfs_server_new(void)
     n->directory = 0;
     n->docpath = 0;
     n->stylesheet = 0;
+    n->id = nmem_strdup_null(gfs_nmem, id);
     n->retrieval = yaz_retrieval_create();
     return n;
 }
@@ -381,7 +387,7 @@ static void xml_config_read(void)
                 else
                     yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
                             attr->name);
-            gfs = *gfsp = gfs_server_new();
+            gfs = *gfsp = gfs_server_new(id);
             gfs->server_node_ptr = ptr_server;
             if (listenref)
             {
@@ -413,9 +419,14 @@ static void xml_config_read(void)
                 }
                 else if (!strcmp((const char *) ptr->name, "cql2rpn"))
                 {
-                    gfs->cql_transform = cql_transform_open_fname(
-                        nmem_dup_xml_content(gfs_nmem, ptr->children)
-                        );
+                    char *name = nmem_dup_xml_content(gfs_nmem, ptr->children);
+                    gfs->cql_transform = cql_transform_open_fname(name);
+                    if (!gfs->cql_transform)
+                    {
+                        yaz_log(YLOG_FATAL|YLOG_ERRNO,
+                                "open CQL transform file '%s'", name);
+                        exit(1);
+                    }
                 }
                 else if (!strcmp((const char *) ptr->name, "ccl2rpn"))
                 {
@@ -605,6 +616,8 @@ static void xml_config_bend_stop(void)
     }
 }
 
+static void remove_listeners(void);
+
 /*
  * handle incoming connect requests.
  * The dynamic mode is a bit tricky mostly because we want to avoid
@@ -904,6 +917,9 @@ static void listener(IOCHAN h, int event)
             return;
         }
 
+        if (control_block.one_shot)
+            remove_listeners();
+
         yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line));
 
         no_sessions++;
@@ -1124,6 +1140,13 @@ static int add_listener(char *where, int listen_id)
     return 0; /* OK */
 }
 
+static void remove_listeners(void)
+{
+    IOCHAN l = pListener;
+    for (; l; l = l->next)
+        iochan_destroy(l);
+}
+
 #ifndef WIN32
 /* UNIX only (for windows we don't need to catch the signals) */
 static void catchchld(int num)
@@ -1259,7 +1282,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)
@@ -1337,6 +1360,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;
@@ -1376,7 +1402,7 @@ int check_options(int argc, char **argv)
             fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
                     " -l <logfile> -u <user> -c <config> -t <minutes>"
                     " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
-                    " -ziDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
+                    " -zKiDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
             return 1;
         }
     }