GFS: Allow multiple listenrefs YAZ-554
[yaz-moved-to-github.git] / src / statserv.c
index ab15c21..e3b09c5 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2011 Index Data
+ * Copyright (C) 1995-2013 Index Data
  * See the file LICENSE for details.
  */
 
@@ -15,7 +15,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 
 #ifdef WIN32
 #include <process.h>
@@ -24,6 +23,7 @@
 #endif
 
 #include <yaz/sc.h>
+#include <yaz/tpath.h>
 
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
@@ -64,6 +64,7 @@
 #include "session.h"
 #include <yaz/statserv.h>
 #include <yaz/daemon.h>
+#include <yaz/yaz-iconv.h>
 
 static IOCHAN pListener = NULL;
 
@@ -96,10 +97,9 @@ statserv_options_block control_block = {
     0,                          /* one shot (single session) */
     "",                         /* no PDUs */
     "",                         /* diagnostic output to stderr */
-    "tcp:@:9999",               /* default listener port */
     PROTO_Z3950,                /* default application protocol */
     900,                        /* idle timeout (seconds) */
-    1024*1024,                  /* maximum PDU size (approx.) to allow */
+    64*1024*1024,               /* maximum PDU size (approx.) to allow */
     "default-config",           /* configuration name to pass to backend */
     "",                         /* set user id */
     0,                          /* bend_start handler */
@@ -117,7 +117,6 @@ statserv_options_block control_block = {
     "",                         /* NT Service Dependencies */
     "Z39.50 Server",            /* NT Service Display Name */
 #endif /* WIN32 */
-    0,                          /* SOAP handlers */
     "",                         /* PID fname */
     0,                          /* background daemon */
     "",                         /* SSL certificate filename */
@@ -164,7 +163,7 @@ static xmlNodePtr xml_config_get_root(void)
             yaz_log(YLOG_WARN, "Bad/missing root element for config %s",
                     control_block.xml_config);
             return 0;
-        
+
         }
     }
     return ptr;
@@ -195,7 +194,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 */
@@ -205,7 +204,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 */
@@ -214,7 +213,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));
@@ -228,13 +227,14 @@ 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;
 }
 #endif
 
 #if YAZ_HAVE_XML2
-static struct gfs_listen * gfs_listen_new(const char *id, 
+static struct gfs_listen * gfs_listen_new(const char *id,
                                           const char *address)
 {
     struct gfs_listen *n = (struct gfs_listen *)
@@ -286,9 +286,15 @@ int control_association(association *assoc, const char *host, int force_open)
             int host_match = 0;
             if ( !gfs->host || (host && gfs->host && !strcmp(host, gfs->host)))
                 host_match = 1;
-            if (!gfs->listen_ref ||
-                gfs->listen_ref == assoc->client_chan->chan_id)
+            if (!gfs->listen_ref)
                 listen_match = 1;
+            else
+            {
+                int i;
+                for (i = 0; gfs->listen_ref[i] != -1; i++)
+                    if (gfs->listen_ref[i] == assoc->client_chan->chan_id)
+                        listen_match = 1;
+            }
             if (listen_match && host_match)
             {
                 if (force_open ||
@@ -307,7 +313,7 @@ int control_association(association *assoc, const char *host, int force_open)
                 assoc->server = gfs;
                 assoc->last_control = &gfs->cb;
                 statserv_setcontrol(&gfs->cb);
-                
+
                 gfs_server_chdir(gfs);
                 break;
             }
@@ -324,7 +330,7 @@ int control_association(association *assoc, const char *host, int force_open)
         statserv_setcontrol(&control_block);
         assoc->last_control = &control_block;
     }
-    yaz_log(YLOG_DEBUG, "server select: config=%s", 
+    yaz_log(YLOG_DEBUG, "server select: config=%s",
             assoc->last_control->configname);
 
     assoc->maximumRecordSize = assoc->last_control->maxrecordsize;
@@ -334,7 +340,7 @@ int control_association(association *assoc, const char *host, int force_open)
 }
 
 #if YAZ_HAVE_XML2
-static void xml_config_read(void)
+static void xml_config_read(const char *base_path)
 {
     struct gfs_server **gfsp = &gfs_server_list;
     struct gfs_listen **gfslp = &gfs_listen_list;
@@ -376,7 +382,7 @@ static void xml_config_read(void)
             struct gfs_server *gfs;
 
             for ( ; attr; attr = attr->next)
-                if (!xmlStrcmp(attr->name, BAD_CAST "listenref") 
+                if (!xmlStrcmp(attr->name, BAD_CAST "listenref")
                     && attr->children && attr->children->type == XML_TEXT_NODE)
                     listenref = nmem_dup_xml_content(gfs_nmem, attr->children);
                 else if (!xmlStrcmp(attr->name, BAD_CAST "id")
@@ -386,21 +392,31 @@ 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)
             {
-                int id_no;
-                struct gfs_listen *gl = gfs_listen_list;
-                for (id_no = 1; gl; gl = gl->next, id_no++)
-                    if (gl->id && !strcmp(gl->id, listenref))
-                    {
-                        gfs->listen_ref = id_no;
-                        break;
-                    }
-                if (!gl)
-                    yaz_log(YLOG_WARN, "Non-existent listenref '%s' in server "
-                            "config element", listenref);
+                char **refs;
+                int num, i;
+                nmem_strsplit(gfs_nmem, ",", listenref, &refs, &num);
+                gfs->listen_ref = (int*) nmem_malloc(gfs_nmem,
+                                                     sizeof(int) * (num + 1));
+                for (i = 0; i < num; i++)
+                {
+                    int id_no;
+                    struct gfs_listen *gl = gfs_listen_list;
+                    gfs->listen_ref[i] = 0;
+                    for (id_no = 1; gl; gl = gl->next, id_no++)
+                        if (gl->id && !strcmp(gl->id, refs[i]))
+                        {
+                            gfs->listen_ref[i] = id_no;
+                            break;
+                        }
+                    if (!gl)
+                        yaz_log(YLOG_WARN, "Non-existent listenref '%s' "
+                                "in server config element", refs[i]);
+                }
+                gfs->listen_ref[i] = -1;
             }
             for (ptr = ptr_server->children; ptr; ptr = ptr->next)
             {
@@ -413,23 +429,40 @@ static void xml_config_read(void)
                 }
                 else if (!strcmp((const char *) ptr->name, "config"))
                 {
+                    char fpath[1024];
                     strcpy(gfs->cb.configname,
                            nmem_dup_xml_content(gfs_nmem, ptr->children));
+
+                    if (yaz_filepath_resolve(gfs->cb.configname,
+                                             base_path, 0, fpath))
+                        strcpy(gfs->cb.configname, fpath);
                 }
                 else if (!strcmp((const char *) ptr->name, "cql2rpn"))
                 {
-                    gfs->cql_transform = cql_transform_open_fname(
-                        nmem_dup_xml_content(gfs_nmem, ptr->children)
-                        );
+                    char fpath[1024];
+                    char *fname = nmem_dup_xml_content(gfs_nmem, ptr->children);
+                    if (yaz_filepath_resolve(fname, base_path, 0, fpath))
+                        fname = fpath;
+
+                    gfs->cql_transform = cql_transform_open_fname(fname);
+                    if (!gfs->cql_transform)
+                    {
+                        yaz_log(YLOG_FATAL|YLOG_ERRNO,
+                                "open CQL transform file '%s'", fname);
+                        exit(1);
+                    }
                 }
                 else if (!strcmp((const char *) ptr->name, "ccl2rpn"))
                 {
-                    char *name;
+                    char *fname, fpath[1024];
                     FILE *f;
 
-                    name = nmem_dup_xml_content(gfs_nmem, ptr->children);
-                    if ((f = fopen(name, "r")) == 0) {
-                        yaz_log(YLOG_FATAL, "can't open CCL file '%s'", name);
+                    fname = nmem_dup_xml_content(gfs_nmem, ptr->children);
+                    if (yaz_filepath_resolve(fname, base_path, 0, fpath))
+                        fname = fpath;
+
+                    if ((f = fopen(fname, "r")) == 0) {
+                        yaz_log(YLOG_FATAL, "can't open CCL file '%s'", fname);
                         exit(1);
                     }
                     gfs->ccl_transform = ccl_qual_mk();
@@ -438,12 +471,12 @@ static void xml_config_read(void)
                 }
                 else if (!strcmp((const char *) ptr->name, "directory"))
                 {
-                    gfs->directory = 
+                    gfs->directory =
                         nmem_dup_xml_content(gfs_nmem, ptr->children);
                 }
                 else if (!strcmp((const char *) ptr->name, "docpath"))
                 {
-                    gfs->docpath = 
+                    gfs->docpath =
                         nmem_dup_xml_content(gfs_nmem, ptr->children);
                 }
                 else if (!strcmp((const char *) ptr->name, "maximumrecordsize"))
@@ -464,8 +497,10 @@ static void xml_config_read(void)
                 }
                 else if (!strcmp((const char *) ptr->name, "retrievalinfo"))
                 {
+                    if (base_path)
+                        yaz_retrieval_set_path(gfs->retrieval, base_path);
                     if (yaz_retrieval_configure(gfs->retrieval, ptr))
-                    {       
+                    {
                         yaz_log(YLOG_FATAL, "%s in config %s",
                                 yaz_retrieval_get_error(gfs->retrieval),
                                 control_block.xml_config);
@@ -488,6 +523,9 @@ static void xml_config_read(void)
 
 static void xml_config_open(void)
 {
+    WRBUF base_path;
+    const char *last_p;
+    const char *fname = control_block.xml_config;
     if (!getcwd(gfs_root_dir, FILENAME_MAX))
     {
         yaz_log(YLOG_WARN|YLOG_ERRNO, "getcwd failed");
@@ -500,18 +538,18 @@ static void xml_config_open(void)
     init_control_tls = 1;
     pthread_key_create(&current_control_tls, 0);
 #endif
-    
+
     gfs_nmem = nmem_create();
 #if YAZ_HAVE_XML2
-    if (control_block.xml_config[0] == '\0')
+    if (fname[0] == '\0')
         return;
 
     if (!xml_config_doc)
     {
-        xml_config_doc = xmlParseFile(control_block.xml_config);
+        xml_config_doc = xmlParseFile(fname);
         if (!xml_config_doc)
         {
-            yaz_log(YLOG_FATAL, "Could not parse %s", control_block.xml_config);
+            yaz_log(YLOG_FATAL, "Could not parse %s", fname);
             exit(1);
         }
         else
@@ -520,12 +558,25 @@ static void xml_config_open(void)
             if (noSubstitutions == -1)
             {
                 yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
-                        control_block.xml_config);
+                        fname);
                 exit(1);
             }
         }
     }
-    xml_config_read();
+    base_path = wrbuf_alloc();
+    last_p = strrchr(fname,
+#ifdef WIN32
+                     '\\'
+#else
+                     '/'
+#endif
+        );
+    if (last_p)
+        wrbuf_write(base_path, fname, last_p - fname);
+    else
+        wrbuf_puts(base_path, ".");
+    xml_config_read(wrbuf_cstr(base_path));
+    wrbuf_destroy(base_path);
 #endif
 }
 
@@ -610,6 +661,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
@@ -741,7 +794,7 @@ static void statserv_closedown()
 
                 /* Allocate the thread handle array */
                 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
-                pCurrentHandle = pThreadHandles; 
+                pCurrentHandle = pThreadHandles;
 
                 for (pCurrentThread = pFirstThread;
                      pCurrentThread != NULL;
@@ -780,7 +833,7 @@ void __cdecl event_loop_thread(IOCHAN iochan)
 }
 
 /* WIN32 listener */
-static void listener(IOCHAN h, int event)   
+static void listener(IOCHAN h, int event)
 {
     COMSTACK line = (COMSTACK) iochan_getdata(h);
     IOCHAN parent_chan = line->user;
@@ -838,7 +891,7 @@ static void listener(IOCHAN h, int event)
         newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
         if (newHandle == (HANDLE) -1)
         {
-            
+
             yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
             iochan_destroy(h);
             return;
@@ -860,7 +913,7 @@ static void listener(IOCHAN h, int event)
 #else /* ! WIN32 */
 
 /* To save having an #ifdef in event_loop we need to
-   define this empty function 
+   define this empty function
 */
 void statserv_remove(IOCHAN pIOChannel)
 {
@@ -909,6 +962,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++;
@@ -977,7 +1033,7 @@ static void *new_session(void *vp)
     COMSTACK new_line = (COMSTACK) vp;
     IOCHAN parent_chan = (IOCHAN) new_line->user;
 
-    unsigned cs_get_mask, cs_accept_mask, mask =  
+    unsigned cs_get_mask, cs_accept_mask, mask =
         ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
         ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
 
@@ -1015,7 +1071,7 @@ static void *new_session(void *vp)
     a = 0;
 #endif
     yaz_log_xml_errors(0, YLOG_WARN);
-    yaz_log(log_session, "Session - OK %d %s %ld",
+    yaz_log(log_session, "Session - OK %d %s PID=%ld",
             no_sessions, a ? a : "[Unknown]", (long) getpid());
     if (max_sessions && no_sessions >= max_sessions)
         control_block.one_shot = 1;
@@ -1091,8 +1147,8 @@ static int add_listener(char *where, int listen_id)
     else
         mode = "static";
 
-    yaz_log(log_server, "Adding %s listener on %s id=%d", mode, where,
-            listen_id);
+    yaz_log(log_server, "Adding %s listener on %s id=%d PID=%ld", mode, where,
+            listen_id, (long) getpid());
 
     l = cs_create_host(where, 2, &ap);
     if (!l)
@@ -1129,6 +1185,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)
@@ -1187,6 +1250,14 @@ static void daemon_handler(void *data)
     iochan_event_loop(pListener);
 }
 
+#ifndef WIN32
+static void normal_stop_handler(int num)
+{
+    yaz_log(log_server, "Received SIGTERM. PID=%ld", (long) getpid());
+    exit(0);
+}
+#endif
+
 static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
 {
     char sep;
@@ -1212,7 +1283,7 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
         return 1;
 
     xml_config_open();
-    
+
     xml_config_bend_start();
 
     if (control_block.inetd)
@@ -1227,8 +1298,8 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
     {
         xml_config_add_listeners();
 
-        if (!pListener && *control_block.default_listen)
-            add_listener(control_block.default_listen, 0);
+        if (!pListener)
+            add_listener("tcp:@:9999", 0);
 
 #ifndef WIN32
         if (control_block.dynamic)
@@ -1239,8 +1310,10 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
         return 1;
     if (s)
         yaz_sc_running(s);
-    yaz_log(YLOG_DEBUG, "Entering event loop.");
 
+#ifndef WIN32
+    signal(SIGTERM, normal_stop_handler);
+#endif
     yaz_daemon(programname,
                (control_block.background ? YAZ_DAEMON_FORK : 0),
                daemon_handler, &pListener,
@@ -1251,8 +1324,8 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
 
 static void option_copy(char *dst, const char *src)
 {
-    strncpy(dst, src ? src : "", 127);
-    dst[127] = '\0';
+    strncpy(dst, src ? src : "", BEND_NAME_MAX-1);
+    dst[BEND_NAME_MAX-1] = '\0';
 }
 
 int check_options(int argc, char **argv)
@@ -1260,9 +1333,9 @@ int check_options(int argc, char **argv)
     int ret = 0, r;
     char *arg;
 
-    yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL)); 
+    yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL));
 
-    get_logbits(1); 
+    get_logbits(1);
 
     while ((ret = options("1a:iszSTl:v:u:c:w:t:k:Kd:A:p:DC:f:m:r:",
                           argv, argc, &arg)) != -2)
@@ -1273,7 +1346,7 @@ int check_options(int argc, char **argv)
             if (add_listener(arg, 0))
                 return 1;  /* failed to create listener */
             break;
-        case '1':        
+        case '1':
             control_block.one_shot = 1;
             control_block.dynamic = 0;
             break;
@@ -1309,7 +1382,7 @@ int check_options(int argc, char **argv)
             break;
         case 'v':
             yaz_log_init_level(yaz_log_mask_str(arg));
-            get_logbits(1); 
+            get_logbits(1);
             break;
         case 'a':
             option_copy(control_block.apdufile, arg);
@@ -1351,7 +1424,7 @@ int check_options(int argc, char **argv)
         case 'w':
             if (chdir(arg))
             {
-                perror(arg);            
+                perror(arg);
                 return 1;
             }
             break;
@@ -1384,7 +1457,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;
         }
     }