Changed include/yaz/diagbib1.h and added include/yaz/diagsrw.h with
[yaz-moved-to-github.git] / src / statserv.c
index 19db7f4..dfba86b 100644 (file)
@@ -5,7 +5,7 @@
  * NT threaded server code by
  *   Chas Woodfield, Fretwell Downing Informatics.
  *
- * $Id: statserv.c,v 1.25 2005-03-05 12:14:12 adam Exp $
+ * $Id: statserv.c,v 1.29 2005-03-14 11:14:21 adam Exp $
  */
 
 /**
@@ -38,6 +38,7 @@
 #if HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
+#include <libxml/xinclude.h>
 #endif
 
 #if YAZ_POSIX_THREADS
@@ -63,6 +64,7 @@
 
 static IOCHAN pListener = NULL;
 
+static char gfs_root_dir[FILENAME_MAX+1];
 static struct gfs_server *gfs_server_list = 0;
 static struct gfs_listen *gfs_listen_list = 0;
 static NMEM gfs_nmem = 0;
@@ -216,6 +218,7 @@ static struct gfs_server * gfs_server_new()
     n->listen_ref = 0;
     n->cql_transform = 0;
     n->server_node_ptr = 0;
+    n->directory = 0;
     return n;
 }
 
@@ -232,6 +235,21 @@ static struct gfs_listen * gfs_listen_new(const char *id,
     return n;
 }
 
+static void gfs_server_chdir(struct gfs_server *gfs)
+{
+    if (gfs_root_dir[0])
+    {
+       if (chdir(gfs_root_dir))
+           yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
+    }
+    if (gfs->directory)
+    {
+       if (chdir(gfs->directory))
+           yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s",
+                   gfs->directory);
+    }
+}
+
 int control_association(association *assoc, const char *host, int force_open)
 {
     char vhost[128], *cp;
@@ -263,7 +281,10 @@ int control_association(association *assoc, const char *host, int force_open)
                {
                    statserv_setcontrol(assoc->last_control);
                    if (assoc->backend && assoc->init)
+                   {
+                       gfs_server_chdir(gfs);
                        (assoc->last_control->bend_close)(assoc->backend);
+                   }
                    assoc->backend = 0;
                    xfree(assoc->init);
                    assoc->init = 0;
@@ -272,6 +293,7 @@ int control_association(association *assoc, const char *host, int force_open)
                assoc->server_node_ptr = gfs->server_node_ptr;
                assoc->last_control = &gfs->cb;
                statserv_setcontrol(&gfs->cb);
+               gfs_server_chdir(gfs);
                yaz_log(YLOG_DEBUG, "server select: %s", gfs->cb.configname);
                return 1;
            }
@@ -333,11 +355,18 @@ static void xml_config_read()
            xmlNodePtr ptr_server = ptr;
            xmlNodePtr ptr;
            const char *listenref = 0;
+           const char *id = 0;
 
            for ( ; attr; attr = attr->next)
-               if (!strcmp(attr->name, "listenref")
-                   && attr->children && attr->children->type == XML_TEXT_NODE)
+               if (!strcmp(attr->name, "listenref") && attr->children &&
+                   attr->children->type == XML_TEXT_NODE)
                    listenref = nmem_dup_xml_content(gfs_nmem, attr->children);
+               else if (!strcmp(attr->name, "id") && attr->children &&
+                        attr->children->type == XML_TEXT_NODE)
+                   id = nmem_dup_xml_content(gfs_nmem, attr->children);
+               else
+                   yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
+                           attr->name);
            *gfsp = gfs_server_new();
            (*gfsp)->server_node_ptr = ptr_server;
            if (listenref)
@@ -363,17 +392,22 @@ static void xml_config_read()
                    (*gfsp)->host = nmem_dup_xml_content(gfs_nmem,
                                                         ptr->children);
                }
-               if (!strcmp((const char *) ptr->name, "config"))
+               else if (!strcmp((const char *) ptr->name, "config"))
                {
                    strcpy((*gfsp)->cb.configname,
                           nmem_dup_xml_content(gfs_nmem, ptr->children));
                }
-               if (!strcmp((const char *) ptr->name, "cql2rpn"))
+               else if (!strcmp((const char *) ptr->name, "cql2rpn"))
                {
                    (*gfsp)->cql_transform = cql_transform_open_fname(
                        nmem_dup_xml_content(gfs_nmem, ptr->children)
                        );
                }
+               else if (!strcmp((const char *) ptr->name, "directory"))
+               {
+                   (*gfsp)->directory = 
+                       nmem_dup_xml_content(gfs_nmem, ptr->children);
+               }
            }
            gfsp = &(*gfsp)->next;
        }
@@ -384,6 +418,11 @@ static void xml_config_read()
 
 static void xml_config_open()
 {
+    if (!getcwd(gfs_root_dir, FILENAME_MAX))
+    {
+       yaz_log(YLOG_WARN|YLOG_ERRNO, "getcwd failed");
+       gfs_root_dir[0] = '\0';
+    }
 #ifdef WIN32
     init_control_tls = 1;
     current_control_tls = TlsAlloc();
@@ -402,8 +441,18 @@ static void xml_config_open()
        xml_config_doc = xmlParseFile(control_block.xml_config);
        if (!xml_config_doc)
        {
-           yaz_log(YLOG_WARN, "Could not parse %s", control_block.xml_config);
-           return ;
+           yaz_log(YLOG_FATAL, "Could not parse %s", control_block.xml_config);
+           exit(1);
+       }
+       else
+       {
+           int noSubstitutions = xmlXIncludeProcess(xml_config_doc);
+           if (noSubstitutions == -1)
+           {
+               yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
+                       control_block.xml_config);
+               exit(1);
+           }
        }
     }
     xml_config_read();
@@ -453,7 +502,10 @@ static void xml_config_bend_start()
                    gfs->cb.configname);
            statserv_setcontrol(&gfs->cb);
            if (control_block.bend_start)
+           {
+               gfs_server_chdir(gfs);
                (control_block.bend_start)(&gfs->cb);
+           }
        }
     }
     else
@@ -463,7 +515,6 @@ static void xml_config_bend_start()
        if (control_block.bend_start)
            (*control_block.bend_start)(&control_block);
     }
-
 }
 
 static void xml_config_bend_stop()
@@ -1068,6 +1119,7 @@ statserv_options_block *statserv_getcontrol(void)
 
 void statserv_setcontrol(statserv_options_block *block)
 {
+    chdir(gfs_root_dir);
 #ifdef WIN32
     if (init_control_tls)
        TlsSetValue(current_control_tls, block);