Fix XML config GFS do not fail if port is in use YAZ-709
[yaz-moved-to-github.git] / src / statserv.c
index f84c345..db50333 100644 (file)
@@ -23,6 +23,7 @@
 #endif
 
 #include <yaz/sc.h>
+#include <yaz/tpath.h>
 
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
@@ -96,7 +97,6 @@ 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) */
     64*1024*1024,               /* maximum PDU size (approx.) to allow */
@@ -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,6 @@ 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;
@@ -287,9 +285,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 ||
@@ -335,7 +339,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;
@@ -391,17 +395,27 @@ static void xml_config_read(void)
             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)
             {
@@ -414,28 +428,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"))
                 {
-                    char *name = nmem_dup_xml_content(gfs_nmem, ptr->children);
-                    gfs->cql_transform = cql_transform_open_fname(name);
+                    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'", name);
+                                "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();
@@ -470,6 +496,8 @@ 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",
@@ -492,8 +520,10 @@ static void xml_config_read(void)
 }
 #endif
 
-static void xml_config_open(void)
+static int xml_config_open(void)
 {
+    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");
@@ -509,16 +539,16 @@ static void xml_config_open(void)
 
     gfs_nmem = nmem_create();
 #if YAZ_HAVE_XML2
-    if (control_block.xml_config[0] == '\0')
-        return;
+    if (fname[0] == '\0')
+        return 0;
 
     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);
-            exit(1);
+            yaz_log(YLOG_FATAL, "Could not parse %s", fname);
+            return -1;
         }
         else
         {
@@ -526,13 +556,29 @@ static void xml_config_open(void)
             if (noSubstitutions == -1)
             {
                 yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
-                        control_block.xml_config);
-                exit(1);
+                        fname);
+                return -1;
             }
         }
     }
-    xml_config_read();
+    last_p = strrchr(fname,
+#ifdef WIN32
+                     '\\'
+#else
+                     '/'
 #endif
+        );
+    if (last_p)
+    {
+        WRBUF base_path = wrbuf_alloc();
+        wrbuf_write(base_path, fname, last_p - fname);
+        xml_config_read(wrbuf_cstr(base_path));
+        wrbuf_destroy(base_path);
+    }
+    else
+        xml_config_read(0);
+#endif
+    return 0;
 }
 
 static void xml_config_close(void)
@@ -555,16 +601,18 @@ static void xml_config_close(void)
 #endif
 }
 
-static void xml_config_add_listeners(void)
+static int xml_config_add_listeners(void)
 {
     struct gfs_listen *gfs = gfs_listen_list;
     int id_no;
+    int ret = 0;
 
     for (id_no = 1; gfs; gfs = gfs->next, id_no++)
     {
-        if (gfs->address)
-            add_listener(gfs->address, id_no);
+        if (!ret && gfs->address)
+            ret = add_listener(gfs->address, id_no);
     }
+    return ret;
 }
 
 static void xml_config_bend_start(void)
@@ -1213,6 +1261,17 @@ static void normal_stop_handler(int num)
 }
 #endif
 
+static void show_version(void)
+{
+    char vstr[20], sha1_str[41];
+
+    yaz_version(vstr, sha1_str);
+    printf("YAZ version: %s %s\n", YAZ_VERSION, YAZ_VERSION_SHA1);
+    if (strcmp(sha1_str, YAZ_VERSION_SHA1))
+        printf("YAZ DLL/SO: %s %s\n", vstr, sha1_str);
+    exit(0);
+}
+
 static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
 {
     char sep;
@@ -1237,7 +1296,8 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
     if (control_block.options_func(argc, argv))
         return 1;
 
-    xml_config_open();
+    if (xml_config_open())
+        return 1;
 
     xml_config_bend_start();
 
@@ -1251,10 +1311,11 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
     }
     else
     {
-        xml_config_add_listeners();
+        if (xml_config_add_listeners())
+            return 1;
 
-        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)
@@ -1279,8 +1340,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)
@@ -1292,7 +1353,7 @@ int check_options(int argc, char **argv)
 
     get_logbits(1);
 
-    while ((ret = options("1a:iszSTl:v:u:c:w:t:k:Kd:A:p:DC:f:m:r:",
+    while ((ret = options("1a:iszSTl:v:u:c:w:t:k:Kd:A:p:DC:f:m:r:V",
                           argv, argc, &arg)) != -2)
     {
         switch (ret)
@@ -1408,11 +1469,14 @@ int check_options(int argc, char **argv)
             }
             yaz_log_init_max_size(r * 1024);
             break;
+        case 'V':
+            show_version();
+            break;
         default:
             fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
                     " -l <logfile> -u <user> -c <config> -t <minutes>"
                     " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
-                    " -zKiDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
+                    " -zKiDSTV1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
             return 1;
         }
     }