Fixed bug in Generic Frontend Server that could cause a server to stop
[yaz-moved-to-github.git] / src / statserv.c
index 1735314..bad214c 100644 (file)
@@ -5,7 +5,7 @@
  * NT threaded server code by
  *   Chas Woodfield, Fretwell Downing Informatics.
  *
- * $Id: statserv.c,v 1.38 2006-05-08 10:16:47 adam Exp $
+ * $Id: statserv.c,v 1.42 2006-09-14 13:50:24 adam Exp $
  */
 
 /**
@@ -36,7 +36,7 @@
 #include <pwd.h>
 #endif
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #include <libxml/xinclude.h>
@@ -127,7 +127,8 @@ statserv_options_block control_block = {
 static int max_sessions = 0;
 
 static int logbits_set = 0;
-static int log_session = 0;
+static int log_session = 0; /* one-line logs for session */
+static int log_sessiondetail = 0; /* more detailed stuff */
 static int log_server = 0;
 
 /** get_logbits sets global loglevel bits */
@@ -137,6 +138,7 @@ static void get_logbits(int force)
     {
         logbits_set = 1;
         log_session = yaz_log_module_level("session");
+        log_sessiondetail = yaz_log_module_level("sessiondetail");
         log_server = yaz_log_module_level("server");
     }
 }
@@ -144,11 +146,11 @@ static void get_logbits(int force)
 
 static int add_listener(char *where, int listen_id);
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 static xmlDocPtr xml_config_doc = 0;
 #endif
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 static xmlNodePtr xml_config_get_root()
 {
     xmlNodePtr ptr = 0;
@@ -168,7 +170,7 @@ static xmlNodePtr xml_config_get_root()
 }
 #endif
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
 {
     unsigned char *cp;
@@ -222,7 +224,7 @@ static struct gfs_server * gfs_server_new()
     n->directory = 0;
     n->docpath = 0;
     n->stylesheet = 0;
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
     n->retrieval = yaz_retrieval_create();
 #endif
     return n;
@@ -299,30 +301,37 @@ 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);
-                yaz_log(YLOG_DEBUG, "server select: %s", gfs->cb.configname);
-                return 1;
+                break;
             }
         }
-        statserv_setcontrol(0);
-        assoc->last_control = 0;
-        yaz_log(YLOG_DEBUG, "server select: no match");
-        return 0;
+        if (!gfs)
+        {
+            statserv_setcontrol(0);
+            assoc->last_control = 0;
+            return 0;
+        }
     }
     else
     {
         statserv_setcontrol(&control_block);
         assoc->last_control = &control_block;
-        yaz_log(YLOG_DEBUG, "server select: config=%s", control_block.configname);
-        return 1;
     }
+    yaz_log(YLOG_DEBUG, "server select: config=%s", 
+            assoc->last_control->configname);
+
+    assoc->maximumRecordSize = assoc->last_control->maxrecordsize;
+    assoc->preferredMessageSize = assoc->last_control->maxrecordsize;
+    cs_set_max_recv_bytes(assoc->client_link, assoc->maximumRecordSize);
+    return 1;
 }
 
 static void xml_config_read()
 {
     struct gfs_server **gfsp = &gfs_server_list;
     struct gfs_listen **gfslp = &gfs_listen_list;
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
     xmlNodePtr ptr = xml_config_get_root();
 
     if (!ptr)
@@ -358,6 +367,7 @@ static void xml_config_read()
             xmlNodePtr ptr;
             const char *listenref = 0;
             const char *id = 0;
+            struct gfs_server *gfs;
 
             for ( ; attr; attr = attr->next)
                 if (!xmlStrcmp(attr->name, BAD_CAST "listenref") 
@@ -370,8 +380,8 @@ static void xml_config_read()
                 else
                     yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
                             attr->name);
-            *gfsp = gfs_server_new();
-            (*gfsp)->server_node_ptr = ptr_server;
+            gfs = *gfsp = gfs_server_new();
+            gfs->server_node_ptr = ptr_server;
             if (listenref)
             {
                 int id_no;
@@ -379,7 +389,7 @@ static void xml_config_read()
                 for (id_no = 1; gl; gl = gl->next, id_no++)
                     if (gl->id && !strcmp(gl->id, listenref))
                     {
-                        (*gfsp)->listen_ref = id_no;
+                        gfs->listen_ref = id_no;
                         break;
                     }
                 if (!gl)
@@ -392,36 +402,41 @@ static void xml_config_read()
                     continue;
                 if (!strcmp((const char *) ptr->name, "host"))
                 {
-                    (*gfsp)->host = nmem_dup_xml_content(gfs_nmem,
+                    gfs->host = nmem_dup_xml_content(gfs_nmem,
                                                          ptr->children);
                 }
                 else if (!strcmp((const char *) ptr->name, "config"))
                 {
-                    strcpy((*gfsp)->cb.configname,
+                    strcpy(gfs->cb.configname,
                            nmem_dup_xml_content(gfs_nmem, ptr->children));
                 }
                 else if (!strcmp((const char *) ptr->name, "cql2rpn"))
                 {
-                    (*gfsp)->cql_transform = cql_transform_open_fname(
+                    gfs->cql_transform = cql_transform_open_fname(
                         nmem_dup_xml_content(gfs_nmem, ptr->children)
                         );
                 }
                 else if (!strcmp((const char *) ptr->name, "directory"))
                 {
-                    (*gfsp)->directory = 
+                    gfs->directory = 
                         nmem_dup_xml_content(gfs_nmem, ptr->children);
                 }
                 else if (!strcmp((const char *) ptr->name, "docpath"))
                 {
-                    (*gfsp)->docpath = 
+                    gfs->docpath = 
                         nmem_dup_xml_content(gfs_nmem, ptr->children);
                 }
+                else if (!strcmp((const char *) ptr->name, "maximumrecordsize"))
+                {
+                    gfs->cb.maxrecordsize = atoi(
+                        nmem_dup_xml_content(gfs_nmem, ptr->children));
+                }
                 else if (!strcmp((const char *) ptr->name, "stylesheet"))
                 {
                     char *s = nmem_dup_xml_content(gfs_nmem, ptr->children);
-                    (*gfsp)->stylesheet = 
+                    gfs->stylesheet = 
                         nmem_malloc(gfs_nmem, strlen(s) + 2);
-                    sprintf((*gfsp)->stylesheet, "/%s", s);
+                    sprintf(gfs->stylesheet, "/%s", s);
                 }
                 else if (!strcmp((const char *) ptr->name, "explain"))
                 {
@@ -429,10 +444,10 @@ static void xml_config_read()
                 }
                 else if (!strcmp((const char *) ptr->name, "retrievalinfo"))
                 {
-                    if (yaz_retrieval_configure((*gfsp)->retrieval, ptr))
+                    if (yaz_retrieval_configure(gfs->retrieval, ptr))
                     {       
                         yaz_log(YLOG_FATAL, "%s in config %s",
-                                yaz_retrieval_get_error((*gfsp)->retrieval),
+                                yaz_retrieval_get_error(gfs->retrieval),
                                 control_block.xml_config);
                         exit(1);
                     }
@@ -467,7 +482,7 @@ static void xml_config_open()
 #endif
     
     gfs_nmem = nmem_create();
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
     if (control_block.xml_config[0] == '\0')
         return;
 
@@ -496,7 +511,7 @@ static void xml_config_open()
 
 static void xml_config_close()
 {
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
     if (xml_config_doc)
     {
         xmlFreeDoc(xml_config_doc);
@@ -755,27 +770,21 @@ static void listener(IOCHAN h, int event)
 
     if (event == EVENT_INPUT)
     {
+        COMSTACK new_line;
+        IOCHAN new_chan;
+
         if ((res = cs_listen(line, 0, 0)) < 0)
         {
-            yaz_log(YLOG_FATAL, "cs_listen failed");
+            yaz_log(YLOG_FATAL|YLOG_ERRNO, "cs_listen failed");
             return;
         }
         else if (res == 1)
-            return;
+            return; /* incomplete */
         yaz_log(YLOG_DEBUG, "listen ok");
-        iochan_setevent(h, EVENT_OUTPUT);
-        iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
-    }
-    else if (event == EVENT_OUTPUT)
-    {
-        COMSTACK new_line = cs_accept(line);
-        IOCHAN new_chan;
-        char *a = NULL;
-
-        if (!new_line)
+        new_line = cs_accept(line);
+       if (!new_line)
         {
             yaz_log(YLOG_FATAL, "Accept failed.");
-            iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
             return;
         }
         yaz_log(YLOG_DEBUG, "Accept ok");
@@ -896,7 +905,7 @@ static void listener(IOCHAN h, int event)
             return;
         }
 
-        yaz_log(log_session, "Connect from %s", cs_addrstr(new_line));
+        yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line));
 
         no_sessions++;
         if (control_block.dynamic)
@@ -1013,7 +1022,7 @@ static void *new_session (void *vp)
 #else
     a = 0;
 #endif
-    yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
+    yaz_log(log_session, "Session - OK %d %s %ld",
             no_sessions, a ? a : "[Unknown]", (long) getpid());
     if (max_sessions && no_sessions >= max_sessions)
         control_block.one_shot = 1;
@@ -1050,7 +1059,7 @@ static void inetd_connection(int what)
                 iochan_setdata(chan, assoc);
                 iochan_settimeout(chan, 60);
                 addr = cs_addrstr(line);
-                yaz_log(log_session, "Inetd association from %s",
+                yaz_log(log_sessiondetail, "Inetd association from %s",
                         addr ? addr : "[UNKNOWN]");
                 assoc->cs_get_mask = EVENT_INPUT;
             }
@@ -1440,7 +1449,7 @@ int check_options(int argc, char **argv)
             option_copy(control_block.pid_fname, arg);
             break;
         case 'f':
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
             option_copy(control_block.xml_config, arg);
 #else
             fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);