7d4af0b346bb3ea024300e982e8cc8f49bd4fa7e
[yaz-moved-to-github.git] / src / statserv.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * NT threaded server code by
6  *   Chas Woodfield, Fretwell Downing Informatics.
7  *
8  * $Id: statserv.c,v 1.40 2006-07-07 13:02:21 marc Exp $
9  */
10
11 /**
12  * \file statserv.c
13  * \brief Implements GFS logic
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <ctype.h>
20 #ifdef WIN32
21 #include <process.h>
22 #include <winsock.h>
23 #include <direct.h>
24 #include "service.h"
25 #endif
26 #if HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #if HAVE_SYS_WAIT_H
30 #include <sys/wait.h>
31 #endif
32 #if HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #if HAVE_PWD_H
36 #include <pwd.h>
37 #endif
38
39 #if YAZ_HAVE_XML2
40 #include <libxml/parser.h>
41 #include <libxml/tree.h>
42 #include <libxml/xinclude.h>
43 #endif
44
45 #if YAZ_POSIX_THREADS
46 #include <pthread.h>
47 #elif YAZ_GNU_THREADS
48 #include <pth.h>
49 #endif
50
51 #include <fcntl.h>
52 #include <signal.h>
53 #include <errno.h>
54
55 #include <yaz/comstack.h>
56 #include <yaz/tcpip.h>
57 #include <yaz/options.h>
58 #ifdef USE_XTIMOSI
59 #include <yaz/xmosi.h>
60 #endif
61 #include <yaz/log.h>
62 #include "eventl.h"
63 #include "session.h"
64 #include <yaz/statserv.h>
65
66 static IOCHAN pListener = NULL;
67
68 static char gfs_root_dir[FILENAME_MAX+1];
69 static struct gfs_server *gfs_server_list = 0;
70 static struct gfs_listen *gfs_listen_list = 0;
71 static NMEM gfs_nmem = 0;
72
73 static char *me = "statserver"; /* log prefix */
74 static char *programname="statserver"; /* full program name */
75 #ifdef WIN32
76 DWORD current_control_tls;
77 static int init_control_tls = 0;
78 #elif YAZ_POSIX_THREADS
79 static pthread_key_t current_control_tls;
80 static int init_control_tls = 0;
81 #else
82 static statserv_options_block *current_control_block = 0;
83 #endif
84
85 /*
86  * default behavior.
87  */
88 #define STAT_DEFAULT_LOG_LEVEL "none,fatal,warn,log,server,session,request"
89 /* the 'none' clears yaz' own default settings, including [log] */
90
91 int check_options(int argc, char **argv);
92 statserv_options_block control_block = {
93     1,                          /* dynamic mode */
94     0,                          /* threaded mode */
95     0,                          /* one shot (single session) */
96     0, /* __UNUSED_loglevel */
97     "",                         /* no PDUs */
98     "",                         /* diagnostic output to stderr */
99     "tcp:@:9999",               /* default listener port */
100     PROTO_Z3950,                /* default application protocol */
101     15,                         /* idle timeout (minutes) */
102     1024*1024,                  /* maximum PDU size (approx.) to allow */
103     "default-config",           /* configuration name to pass to backend */
104     "",                         /* set user id */
105     0,                          /* bend_start handler */
106     0,                          /* bend_stop handler */
107     check_options,              /* Default routine, for checking the run-time arguments */
108     check_ip_tcpd,
109     "",
110     0,                          /* default value for inet deamon */
111     0,                          /* handle (for service, etc) */
112     0,                          /* bend_init handle */
113     0,                          /* bend_close handle */
114 #ifdef WIN32
115     "Z39.50 Server",            /* NT Service Name */
116     "Server",                   /* NT application Name */
117     "",                         /* NT Service Dependencies */
118     "Z39.50 Server",            /* NT Service Display Name */
119 #endif /* WIN32 */
120     0,                          /* SOAP handlers */
121     "",                         /* PID fname */
122     0,                          /* background daemon */
123     "",                         /* SSL certificate filename */
124     ""                          /* XML config filename */
125 };
126
127 static int max_sessions = 0;
128
129 static int logbits_set = 0;
130 static int log_session = 0; /* one-line logs for session */
131 static int log_sessiondetail = 0; /* more detailed stuff */
132 static int log_server = 0;
133
134 /** get_logbits sets global loglevel bits */
135 static void get_logbits(int force)
136 { /* needs to be called after parsing cmd-line args that can set loglevels!*/
137     if (force || !logbits_set)
138     {
139         logbits_set = 1;
140         log_session = yaz_log_module_level("session");
141         log_sessiondetail = yaz_log_module_level("sessiondetail");
142         log_server = yaz_log_module_level("server");
143     }
144 }
145
146
147 static int add_listener(char *where, int listen_id);
148
149 #if YAZ_HAVE_XML2
150 static xmlDocPtr xml_config_doc = 0;
151 #endif
152
153 #if YAZ_HAVE_XML2
154 static xmlNodePtr xml_config_get_root()
155 {
156     xmlNodePtr ptr = 0;
157     if (xml_config_doc)
158     {
159         ptr = xmlDocGetRootElement(xml_config_doc);
160         if (!ptr || ptr->type != XML_ELEMENT_NODE ||
161             strcmp((const char *) ptr->name, "yazgfs"))
162         {
163             yaz_log(YLOG_WARN, "Bad/missing root element for config %s",
164                     control_block.xml_config);
165             return 0;
166         
167         }
168     }
169     return ptr;
170 }
171 #endif
172
173 #if YAZ_HAVE_XML2
174 static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
175 {
176     unsigned char *cp;
177     xmlNodePtr p;
178     int len = 1;  /* start with 1, because of trailing 0 */
179     unsigned char *str;
180     int first = 1; /* whitespace lead flag .. */
181     /* determine length */
182     for (p = ptr; p; p = p->next)
183     {
184         if (p->type == XML_TEXT_NODE)
185             len += xmlStrlen(p->content);
186     }
187     /* now allocate for the string */
188     str = nmem_malloc(n, len);
189     *str = '\0'; /* so we can use strcat */
190     for (p = ptr; p; p = p->next)
191     {
192         if (p->type == XML_TEXT_NODE)
193         {
194             cp = p->content;
195             if (first)
196             {
197                 while(*cp && isspace(*cp))
198                     cp++;
199                 if (*cp)
200                     first = 0;  /* reset if we got non-whitespace out */
201             }
202             strcat((char *)str, (const char *)cp); /* append */
203         }
204     }
205     /* remove trailing whitespace */
206     cp = strlen((const char *)str) + str;
207     while (cp != str && isspace(cp[-1]))
208         cp--;
209     *cp = '\0';
210     /* return resulting string */
211     return (char *) str;
212 }
213 #endif
214
215 static struct gfs_server * gfs_server_new()
216 {
217     struct gfs_server *n = nmem_malloc(gfs_nmem, sizeof(*n));
218     memcpy(&n->cb, &control_block, sizeof(control_block));
219     n->next = 0;
220     n->host = 0;
221     n->listen_ref = 0;
222     n->cql_transform = 0;
223     n->server_node_ptr = 0;
224     n->directory = 0;
225     n->docpath = 0;
226     n->stylesheet = 0;
227 #if YAZ_HAVE_XML2
228     n->retrieval = yaz_retrieval_create();
229 #endif
230     return n;
231 }
232
233 static struct gfs_listen * gfs_listen_new(const char *id, 
234                                           const char *address)
235 {
236     struct gfs_listen *n = nmem_malloc(gfs_nmem, sizeof(*n));
237     if (id)
238         n->id = nmem_strdup(gfs_nmem, id);
239     else
240         n->id = 0;
241     n->next = 0;
242     n->address = nmem_strdup(gfs_nmem, address);
243     return n;
244 }
245
246 static void gfs_server_chdir(struct gfs_server *gfs)
247 {
248     if (gfs_root_dir[0])
249     {
250         if (chdir(gfs_root_dir))
251             yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
252     }
253     if (gfs->directory)
254     {
255         if (chdir(gfs->directory))
256             yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s",
257                     gfs->directory);
258     }
259 }
260
261 int control_association(association *assoc, const char *host, int force_open)
262 {
263     char vhost[128], *cp;
264     if (host)
265     {
266         strncpy(vhost, host, 127);
267         vhost[127] = '\0';
268         cp = strchr(vhost, ':');
269         if (cp)
270             *cp = '\0';
271         host = vhost;
272     }
273     assoc->server = 0;
274     if (control_block.xml_config[0])
275     {
276         struct gfs_server *gfs;
277         for (gfs = gfs_server_list; gfs; gfs = gfs->next)
278         {
279             int listen_match = 0;
280             int host_match = 0;
281             if ( !gfs->host || (host && gfs->host && !strcmp(host, gfs->host)))
282                 host_match = 1;
283             if (!gfs->listen_ref ||
284                 gfs->listen_ref == assoc->client_chan->chan_id)
285                 listen_match = 1;
286             if (listen_match && host_match)
287             {
288                 if (force_open ||
289                     (assoc->last_control != &gfs->cb && assoc->backend))
290                 {
291                     statserv_setcontrol(assoc->last_control);
292                     if (assoc->backend && assoc->init)
293                     {
294                         gfs_server_chdir(gfs);
295                         (assoc->last_control->bend_close)(assoc->backend);
296                     }
297                     assoc->backend = 0;
298                     xfree(assoc->init);
299                     assoc->init = 0;
300                 }
301                 assoc->server = gfs;
302                 assoc->last_control = &gfs->cb;
303                 statserv_setcontrol(&gfs->cb);
304                 gfs_server_chdir(gfs);
305                 yaz_log(YLOG_DEBUG, "server select: %s", gfs->cb.configname);
306                 return 1;
307             }
308         }
309         statserv_setcontrol(0);
310         assoc->last_control = 0;
311         yaz_log(YLOG_DEBUG, "server select: no match");
312         return 0;
313     }
314     else
315     {
316         statserv_setcontrol(&control_block);
317         assoc->last_control = &control_block;
318         yaz_log(YLOG_DEBUG, "server select: config=%s", control_block.configname);
319         return 1;
320     }
321 }
322
323 static void xml_config_read()
324 {
325     struct gfs_server **gfsp = &gfs_server_list;
326     struct gfs_listen **gfslp = &gfs_listen_list;
327 #if YAZ_HAVE_XML2
328     xmlNodePtr ptr = xml_config_get_root();
329
330     if (!ptr)
331         return;
332     for (ptr = ptr->children; ptr; ptr = ptr->next)
333     {
334         struct _xmlAttr *attr;
335         if (ptr->type != XML_ELEMENT_NODE)
336             continue;
337         attr = ptr->properties;
338         if (!strcmp((const char *) ptr->name, "listen"))
339         {
340             /*
341               <listen id="listenerid">tcp:@:9999</listen>
342             */
343             const char *id = 0;
344             const char *address =
345                 nmem_dup_xml_content(gfs_nmem, ptr->children);
346             for ( ; attr; attr = attr->next)
347                 if (!xmlStrcmp(attr->name, BAD_CAST "id")
348                     && attr->children && attr->children->type == XML_TEXT_NODE)
349                     id = nmem_dup_xml_content(gfs_nmem, attr->children);
350             if (address)
351             {
352                 *gfslp = gfs_listen_new(id, address);
353                 gfslp = &(*gfslp)->next;
354                 *gfslp = 0; /* make listener list consistent for search */
355             }
356         }
357         else if (!strcmp((const char *) ptr->name, "server"))
358         {
359             xmlNodePtr ptr_server = ptr;
360             xmlNodePtr ptr;
361             const char *listenref = 0;
362             const char *id = 0;
363
364             for ( ; attr; attr = attr->next)
365                 if (!xmlStrcmp(attr->name, BAD_CAST "listenref") 
366                     && attr->children && attr->children->type == XML_TEXT_NODE)
367                     listenref = nmem_dup_xml_content(gfs_nmem, attr->children);
368                 else if (!xmlStrcmp(attr->name, BAD_CAST "id")
369                          && attr->children
370                          && attr->children->type == XML_TEXT_NODE)
371                     id = nmem_dup_xml_content(gfs_nmem, attr->children);
372                 else
373                     yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
374                             attr->name);
375             *gfsp = gfs_server_new();
376             (*gfsp)->server_node_ptr = ptr_server;
377             if (listenref)
378             {
379                 int id_no;
380                 struct gfs_listen *gl = gfs_listen_list;
381                 for (id_no = 1; gl; gl = gl->next, id_no++)
382                     if (gl->id && !strcmp(gl->id, listenref))
383                     {
384                         (*gfsp)->listen_ref = id_no;
385                         break;
386                     }
387                 if (!gl)
388                     yaz_log(YLOG_WARN, "Non-existent listenref '%s' in server "
389                             "config element", listenref);
390             }
391             for (ptr = ptr_server->children; ptr; ptr = ptr->next)
392             {
393                 if (ptr->type != XML_ELEMENT_NODE)
394                     continue;
395                 if (!strcmp((const char *) ptr->name, "host"))
396                 {
397                     (*gfsp)->host = nmem_dup_xml_content(gfs_nmem,
398                                                          ptr->children);
399                 }
400                 else if (!strcmp((const char *) ptr->name, "config"))
401                 {
402                     strcpy((*gfsp)->cb.configname,
403                            nmem_dup_xml_content(gfs_nmem, ptr->children));
404                 }
405                 else if (!strcmp((const char *) ptr->name, "cql2rpn"))
406                 {
407                     (*gfsp)->cql_transform = cql_transform_open_fname(
408                         nmem_dup_xml_content(gfs_nmem, ptr->children)
409                         );
410                 }
411                 else if (!strcmp((const char *) ptr->name, "directory"))
412                 {
413                     (*gfsp)->directory = 
414                         nmem_dup_xml_content(gfs_nmem, ptr->children);
415                 }
416                 else if (!strcmp((const char *) ptr->name, "docpath"))
417                 {
418                     (*gfsp)->docpath = 
419                         nmem_dup_xml_content(gfs_nmem, ptr->children);
420                 }
421                 else if (!strcmp((const char *) ptr->name, "stylesheet"))
422                 {
423                     char *s = nmem_dup_xml_content(gfs_nmem, ptr->children);
424                     (*gfsp)->stylesheet = 
425                         nmem_malloc(gfs_nmem, strlen(s) + 2);
426                     sprintf((*gfsp)->stylesheet, "/%s", s);
427                 }
428                 else if (!strcmp((const char *) ptr->name, "explain"))
429                 {
430                     ; /* being processed separately */
431                 }
432                 else if (!strcmp((const char *) ptr->name, "retrievalinfo"))
433                 {
434                     if (yaz_retrieval_configure((*gfsp)->retrieval, ptr))
435                     {       
436                         yaz_log(YLOG_FATAL, "%s in config %s",
437                                 yaz_retrieval_get_error((*gfsp)->retrieval),
438                                 control_block.xml_config);
439                         exit(1);
440                     }
441                 }
442                 else
443                 {
444                     yaz_log(YLOG_FATAL, "Unknown element '%s' in config %s",
445                             ptr->name, control_block.xml_config);
446                     exit(1);
447                 }
448             }
449             gfsp = &(*gfsp)->next;
450         }
451     }
452 #endif
453     *gfsp = 0;
454 }
455
456 static void xml_config_open()
457 {
458     if (!getcwd(gfs_root_dir, FILENAME_MAX))
459     {
460         yaz_log(YLOG_WARN|YLOG_ERRNO, "getcwd failed");
461         gfs_root_dir[0] = '\0';
462     }
463 #ifdef WIN32
464     init_control_tls = 1;
465     current_control_tls = TlsAlloc();
466 #elif YAZ_POSIX_THREADS
467     init_control_tls = 1;
468     pthread_key_create(&current_control_tls, 0);
469 #endif
470     
471     gfs_nmem = nmem_create();
472 #if YAZ_HAVE_XML2
473     if (control_block.xml_config[0] == '\0')
474         return;
475
476     if (!xml_config_doc)
477     {
478         xml_config_doc = xmlParseFile(control_block.xml_config);
479         if (!xml_config_doc)
480         {
481             yaz_log(YLOG_FATAL, "Could not parse %s", control_block.xml_config);
482             exit(1);
483         }
484         else
485         {
486             int noSubstitutions = xmlXIncludeProcess(xml_config_doc);
487             if (noSubstitutions == -1)
488             {
489                 yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
490                         control_block.xml_config);
491                 exit(1);
492             }
493         }
494     }
495     xml_config_read();
496 #endif
497 }
498
499 static void xml_config_close()
500 {
501 #if YAZ_HAVE_XML2
502     if (xml_config_doc)
503     {
504         xmlFreeDoc(xml_config_doc);
505         xml_config_doc = 0;
506     }
507 #endif
508     gfs_server_list = 0;
509     nmem_destroy(gfs_nmem);
510 #ifdef WIN32
511     if (init_control_tls)
512         TlsFree(current_control_tls);
513 #elif YAZ_POSIX_THREADS
514     if (init_control_tls)
515         pthread_key_delete(current_control_tls);
516 #endif
517 }
518
519 static void xml_config_add_listeners()
520 {
521     struct gfs_listen *gfs = gfs_listen_list;
522     int id_no;
523
524     for (id_no = 1; gfs; gfs = gfs->next, id_no++)
525     {
526         if (gfs->address)
527             add_listener(gfs->address, id_no);
528     }
529 }
530
531 static void xml_config_bend_start()
532 {
533     if (control_block.xml_config[0])
534     {
535         struct gfs_server *gfs = gfs_server_list;
536         for (; gfs; gfs = gfs->next)
537         {
538             yaz_log(YLOG_DEBUG, "xml_config_bend_start config=%s",
539                     gfs->cb.configname);
540             statserv_setcontrol(&gfs->cb);
541             if (control_block.bend_start)
542             {
543                 gfs_server_chdir(gfs);
544                 (control_block.bend_start)(&gfs->cb);
545             }
546         }
547     }
548     else
549     {
550         yaz_log(YLOG_DEBUG, "xml_config_bend_start default config");
551         statserv_setcontrol(&control_block);
552         if (control_block.bend_start)
553             (*control_block.bend_start)(&control_block);
554     }
555 }
556
557 static void xml_config_bend_stop()
558 {
559     if (control_block.xml_config[0])
560     {
561         struct gfs_server *gfs = gfs_server_list;
562         for (; gfs; gfs = gfs->next)
563         {
564             yaz_log(YLOG_DEBUG, "xml_config_bend_stop config=%s",
565                     gfs->cb.configname);
566             statserv_setcontrol(&gfs->cb);
567             if (control_block.bend_stop)
568                 (control_block.bend_stop)(&gfs->cb);
569         }
570     }
571     else
572     {
573         yaz_log(YLOG_DEBUG, "xml_config_bend_stop default config");
574         statserv_setcontrol(&control_block);
575         if (control_block.bend_stop)
576             (*control_block.bend_stop)(&control_block);
577     }
578 }
579
580 /*
581  * handle incoming connect requests.
582  * The dynamic mode is a bit tricky mostly because we want to avoid
583  * doing all of the listening and accepting in the parent - it's
584  * safer that way.
585  */
586 #ifdef WIN32
587
588 typedef struct _ThreadList ThreadList;
589
590 struct _ThreadList
591 {
592     HANDLE hThread;
593     IOCHAN pIOChannel;
594     ThreadList *pNext;
595 };
596
597 static ThreadList *pFirstThread;
598 static CRITICAL_SECTION Thread_CritSect;
599 static BOOL bInitialized = FALSE;
600
601 static void ThreadList_Initialize()
602 {
603     /* Initialize the critical Sections */
604     InitializeCriticalSection(&Thread_CritSect);
605
606      /* Set the first thraed */
607     pFirstThread = NULL;
608
609     /* we have been initialized */
610     bInitialized = TRUE;
611 }
612
613 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
614 {
615     /* Only one thread can go through this section at a time */
616     EnterCriticalSection(&Thread_CritSect);
617
618     {
619         /* Lets create our new object */
620         ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
621         pNewThread->hThread = hThread;
622         pNewThread->pIOChannel = pIOChannel;
623         pNewThread->pNext = pFirstThread;
624         pFirstThread = pNewThread;
625
626         /* Lets let somebody else create a new object now */
627         LeaveCriticalSection(&Thread_CritSect);
628     }
629 }
630
631 void statserv_remove(IOCHAN pIOChannel)
632 {
633     /* Only one thread can go through this section at a time */
634     EnterCriticalSection(&Thread_CritSect);
635
636     {
637         ThreadList *pCurrentThread = pFirstThread;
638         ThreadList *pNextThread;
639         ThreadList *pPrevThread =NULL;
640
641         /* Step through all the threads */
642         for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
643         {
644             /* We only need to compare on the IO Channel */
645             if (pCurrentThread->pIOChannel == pIOChannel)
646             {
647                 /* We have found the thread we want to delete */
648                 /* First of all reset the next pointers */
649                 if (pPrevThread == NULL)
650                     pFirstThread = pCurrentThread->pNext;
651                 else
652                     pPrevThread->pNext = pCurrentThread->pNext;
653
654                 /* All we need todo now is delete the memory */
655                 free(pCurrentThread);
656
657                 /* No need to look at any more threads */
658                 pNextThread = NULL;
659             }
660             else
661             {
662                 /* We need to look at another thread */
663                 pNextThread = pCurrentThread->pNext;
664                 pPrevThread = pCurrentThread;
665             }
666         }
667
668         /* Lets let somebody else remove an object now */
669         LeaveCriticalSection(&Thread_CritSect);
670     }
671 }
672
673 /* WIN32 statserv_closedown */
674 void statserv_closedown()
675 {
676     /* Shouldn't do anything if we are not initialized */
677     if (bInitialized)
678     {
679         int iHandles = 0;
680         HANDLE *pThreadHandles = NULL;
681
682         /* We need to stop threads adding and removing while we */
683         /* start the closedown process */
684         EnterCriticalSection(&Thread_CritSect);
685
686         {
687             /* We have exclusive access to the thread stuff now */
688             /* Y didn't i use a semaphore - Oh well never mind */
689             ThreadList *pCurrentThread = pFirstThread;
690
691             /* Before we do anything else, we need to shutdown the listener */
692             if (pListener != NULL)
693                 iochan_destroy(pListener);
694
695             for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
696             {
697                 /* Just destroy the IOCHAN, that should do the trick */
698                 iochan_destroy(pCurrentThread->pIOChannel);
699                 closesocket(pCurrentThread->pIOChannel->fd);
700
701                 /* Keep a running count of our handles */
702                 iHandles++;
703             }
704
705             if (iHandles > 0)
706             {
707                 HANDLE *pCurrentHandle ;
708
709                 /* Allocate the thread handle array */
710                 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
711                 pCurrentHandle = pThreadHandles; 
712
713                 for (pCurrentThread = pFirstThread;
714                      pCurrentThread != NULL;
715                      pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
716                 {
717                     /* Just the handle */
718                     *pCurrentHandle = pCurrentThread->hThread;
719                 }
720             }
721
722             /* We can now leave the critical section */
723             LeaveCriticalSection(&Thread_CritSect);
724         }
725
726         /* Now we can really do something */
727         if (iHandles > 0)
728         {
729             yaz_log(log_server, "waiting for %d to die", iHandles);
730             /* This will now wait, until all the threads close */
731             WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
732
733             /* Free the memory we allocated for the handle array */
734             free(pThreadHandles);
735         }
736
737         xml_config_bend_stop();
738         /* No longer require the critical section, since all threads are dead */
739         DeleteCriticalSection(&Thread_CritSect);
740     }
741     xml_config_close();
742 }
743
744 void __cdecl event_loop_thread (IOCHAN iochan)
745 {
746     event_loop (&iochan);
747 }
748
749 /* WIN32 listener */
750 static void listener(IOCHAN h, int event)   
751 {
752     COMSTACK line = (COMSTACK) iochan_getdata(h);
753     IOCHAN parent_chan = line->user;
754     association *newas;
755     int res;
756     HANDLE newHandle;
757
758     if (event == EVENT_INPUT)
759     {
760         if ((res = cs_listen(line, 0, 0)) < 0)
761         {
762             yaz_log(YLOG_FATAL, "cs_listen failed");
763             return;
764         }
765         else if (res == 1)
766             return;
767         yaz_log(YLOG_DEBUG, "listen ok");
768         iochan_setevent(h, EVENT_OUTPUT);
769         iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
770     }
771     else if (event == EVENT_OUTPUT)
772     {
773         COMSTACK new_line = cs_accept(line);
774         IOCHAN new_chan;
775         char *a = NULL;
776
777         if (!new_line)
778         {
779             yaz_log(YLOG_FATAL, "Accept failed.");
780             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
781             return;
782         }
783         yaz_log(YLOG_DEBUG, "Accept ok");
784
785         if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
786                                        EVENT_INPUT, parent_chan->chan_id)))
787         {
788             yaz_log(YLOG_FATAL, "Failed to create iochan");
789             iochan_destroy(h);
790             return;
791         }
792
793         yaz_log(YLOG_DEBUG, "Creating association");
794         if (!(newas = create_association(new_chan, new_line,
795                                          control_block.apdufile)))
796         {
797             yaz_log(YLOG_FATAL, "Failed to create new assoc.");
798             iochan_destroy(h);
799             return;
800         }
801         newas->cs_get_mask = EVENT_INPUT;
802         newas->cs_put_mask = 0;
803         newas->cs_accept_mask = 0;
804
805         yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
806         iochan_setdata(new_chan, newas);
807         iochan_settimeout(new_chan, 60);
808
809         /* Now what we need todo is create a new thread with this iochan as
810            the parameter */
811         newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
812         if (newHandle == (HANDLE) -1)
813         {
814             
815             yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
816             iochan_destroy(h);
817             return;
818         }
819         /* We successfully created the thread, so add it to the list */
820         statserv_add(newHandle, new_chan);
821
822         yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
823         iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
824     }
825     else
826     {
827         yaz_log(YLOG_FATAL, "Bad event on listener.");
828         iochan_destroy(h);
829         return;
830     }
831 }
832
833 int statserv_must_terminate(void)
834 {
835     return 0;
836 }
837
838 #else /* ! WIN32 */
839
840 static int term_flag = 0;
841 /* To save having an #ifdef in event_loop we need to
842    define this empty function 
843 */
844 int statserv_must_terminate(void)
845 {
846     return term_flag;
847 }
848
849 void statserv_remove(IOCHAN pIOChannel)
850 {
851 }
852
853 void statserv_closedown()
854 {
855     IOCHAN p;
856
857     xml_config_bend_stop();
858     for (p = pListener; p; p = p->next)
859     {
860         iochan_destroy(p);
861     }
862     xml_config_close();
863 }
864
865 void sigterm(int sig)
866 {
867     term_flag = 1;
868 }
869
870 static void *new_session (void *vp);
871 static int no_sessions = 0;
872
873 /* UNIX listener */
874 static void listener(IOCHAN h, int event)
875 {
876     COMSTACK line = (COMSTACK) iochan_getdata(h);
877     int res;
878
879     if (event == EVENT_INPUT)
880     {
881         COMSTACK new_line;
882         if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
883                                    control_block.daemon_name)) < 0)
884         {
885             yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
886             return;
887         }
888         else if (res == 1)
889         {
890             yaz_log(YLOG_WARN, "cs_listen incomplete");
891             return;
892         }
893         new_line = cs_accept(line);
894         if (!new_line)
895         {
896             yaz_log(YLOG_FATAL, "Accept failed.");
897             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
898             return;
899         }
900
901         yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line));
902
903         no_sessions++;
904         if (control_block.dynamic)
905         {
906             if ((res = fork()) < 0)
907             {
908                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
909                 iochan_destroy(h);
910                 return;
911             }
912             else if (res == 0) /* child */
913             {
914                 char nbuf[100];
915                 IOCHAN pp;
916
917                 for (pp = pListener; pp; pp = iochan_getnext(pp))
918                 {
919                     COMSTACK l = (COMSTACK)iochan_getdata(pp);
920                     cs_close(l);
921                     iochan_destroy(pp);
922                 }
923                 sprintf(nbuf, "%s(%d)", me, no_sessions);
924                 yaz_log_init_prefix(nbuf);
925                 /* ensure that bend_stop is not called when each child exits -
926                    only for the main process ..  */
927                 control_block.bend_stop = 0;
928             }
929             else /* parent */
930             {
931                 cs_close(new_line);
932                 return;
933             }
934         }
935
936         if (control_block.threads)
937         {
938 #if YAZ_POSIX_THREADS
939             pthread_t child_thread;
940             pthread_create (&child_thread, 0, new_session, new_line);
941             pthread_detach (child_thread);
942 #elif YAZ_GNU_THREADS
943             pth_attr_t attr;
944             pth_t child_thread;
945
946             attr = pth_attr_new ();
947             pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
948             pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
949             pth_attr_set (attr, PTH_ATTR_NAME, "session");
950             yaz_log (YLOG_DEBUG, "pth_spawn begin");
951             child_thread = pth_spawn (attr, new_session, new_line);
952             yaz_log (YLOG_DEBUG, "pth_spawn finish");
953             pth_attr_destroy (attr);
954 #else
955             new_session(new_line);
956 #endif
957         }
958         else
959             new_session(new_line);
960     }
961     else if (event == EVENT_TIMEOUT)
962     {
963         yaz_log(log_server, "Shutting down listener.");
964         iochan_destroy(h);
965     }
966     else
967     {
968         yaz_log(YLOG_FATAL, "Bad event on listener.");
969         iochan_destroy(h);
970     }
971 }
972
973 static void *new_session (void *vp)
974 {
975     char *a;
976     association *newas;
977     IOCHAN new_chan;
978     COMSTACK new_line = (COMSTACK) vp;
979     IOCHAN parent_chan = new_line->user;
980
981     unsigned cs_get_mask, cs_accept_mask, mask =  
982         ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
983         ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
984
985     if (mask)
986     {
987         cs_accept_mask = mask;  /* accept didn't complete */
988         cs_get_mask = 0;
989     }
990     else
991     {
992         cs_accept_mask = 0;     /* accept completed.  */
993         cs_get_mask = mask = EVENT_INPUT;
994     }
995
996     if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask,
997                                    parent_chan->chan_id)))
998     {
999         yaz_log(YLOG_FATAL, "Failed to create iochan");
1000         return 0;
1001     }
1002     if (!(newas = create_association(new_chan, new_line,
1003                                      control_block.apdufile)))
1004     {
1005         yaz_log(YLOG_FATAL, "Failed to create new assoc.");
1006         return 0;
1007     }
1008     newas->cs_accept_mask = cs_accept_mask;
1009     newas->cs_get_mask = cs_get_mask;
1010
1011     iochan_setdata(new_chan, newas);
1012     iochan_settimeout(new_chan, 60);
1013 #if 1
1014     a = cs_addrstr(new_line);
1015 #else
1016     a = 0;
1017 #endif
1018     yaz_log(log_session, "Session - OK %d %s %ld",
1019             no_sessions, a ? a : "[Unknown]", (long) getpid());
1020     if (max_sessions && no_sessions >= max_sessions)
1021         control_block.one_shot = 1;
1022     if (control_block.threads)
1023     {
1024         event_loop(&new_chan);
1025     }
1026     else
1027     {
1028         new_chan->next = pListener;
1029         pListener = new_chan;
1030     }
1031     return 0;
1032 }
1033
1034 /* UNIX */
1035 #endif
1036
1037 static void inetd_connection(int what)
1038 {
1039     COMSTACK line;
1040     IOCHAN chan;
1041     association *assoc;
1042     char *addr;
1043
1044     if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
1045     {
1046         if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT,
1047                                   0)))
1048         {
1049             if ((assoc = create_association(chan, line,
1050                                             control_block.apdufile)))
1051             {
1052                 iochan_setdata(chan, assoc);
1053                 iochan_settimeout(chan, 60);
1054                 addr = cs_addrstr(line);
1055                 yaz_log(log_sessiondetail, "Inetd association from %s",
1056                         addr ? addr : "[UNKNOWN]");
1057                 assoc->cs_get_mask = EVENT_INPUT;
1058             }
1059             else
1060             {
1061                 yaz_log(YLOG_FATAL, "Failed to create association structure");
1062             }
1063             chan->next = pListener;
1064             pListener = chan;
1065         }
1066         else
1067         {
1068             yaz_log(YLOG_FATAL, "Failed to create iochan");
1069         }
1070     }
1071     else
1072     {
1073         yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
1074     }
1075 }
1076
1077 /*
1078  * Set up a listening endpoint, and give it to the event-handler.
1079  */
1080 static int add_listener(char *where, int listen_id)
1081 {
1082     COMSTACK l;
1083     void *ap;
1084     IOCHAN lst = NULL;
1085     const char *mode;
1086
1087     if (control_block.dynamic)
1088         mode = "dynamic";
1089     else if (control_block.threads)
1090         mode = "threaded";
1091     else
1092         mode = "static";
1093
1094     yaz_log(log_server, "Adding %s listener on %s id=%d", mode, where,
1095             listen_id);
1096
1097     l = cs_create_host(where, 2, &ap);
1098     if (!l)
1099     {
1100         yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
1101         return -1;
1102     }
1103     if (*control_block.cert_fname)
1104         cs_set_ssl_certificate_file(l, control_block.cert_fname);
1105
1106     if (cs_bind(l, ap, CS_SERVER) < 0)
1107     {
1108         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
1109         cs_close (l);
1110         return -1;
1111     }
1112     if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
1113          EVENT_EXCEPT, listen_id)))
1114     {
1115         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
1116         cs_close (l);
1117         return -1;
1118     }
1119     iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
1120     l->user = lst;  /* user-defined data for COMSTACK is listener chan */
1121
1122     /* Add listener to chain */
1123     lst->next = pListener;
1124     pListener = lst;
1125     return 0; /* OK */
1126 }
1127
1128 #ifndef WIN32
1129 /* UNIX only (for windows we don't need to catch the signals) */
1130 static void catchchld(int num)
1131 {
1132     while (waitpid(-1, 0, WNOHANG) > 0)
1133         ;
1134     signal(SIGCHLD, catchchld);
1135 }
1136 #endif
1137
1138 statserv_options_block *statserv_getcontrol(void)
1139 {
1140 #ifdef WIN32
1141     if (init_control_tls)
1142         return (statserv_options_block *) TlsGetValue(current_control_tls);
1143     else
1144         return &control_block;
1145 #elif YAZ_POSIX_THREADS
1146     if (init_control_tls)
1147         return pthread_getspecific(current_control_tls);
1148     else
1149         return &control_block;
1150 #else
1151     if (current_control_block)
1152         return current_control_block;
1153     return &control_block;
1154 #endif
1155 }
1156
1157 void statserv_setcontrol(statserv_options_block *block)
1158 {
1159     chdir(gfs_root_dir);
1160 #ifdef WIN32
1161     if (init_control_tls)
1162         TlsSetValue(current_control_tls, block);
1163 #elif YAZ_POSIX_THREADS
1164     if (init_control_tls)
1165         pthread_setspecific(current_control_tls, block);
1166 #else
1167     current_control_block = block;
1168 #endif
1169 }
1170
1171 static void statserv_reset(void)
1172 {
1173 }
1174
1175 int statserv_start(int argc, char **argv)
1176 {
1177     char sep;
1178 #ifdef WIN32
1179     /* We need to initialize the thread list */
1180     ThreadList_Initialize();
1181 /* WIN32 */
1182 #endif
1183
1184
1185 #ifdef WIN32
1186     sep = '\\';
1187 #else
1188     sep = '/';
1189 #endif
1190     if ((me = strrchr (argv[0], sep)))
1191         me++; /* get the basename */
1192     else
1193         me = argv[0];
1194     programname = argv[0];
1195
1196     if (control_block.options_func(argc, argv))
1197         return 1;
1198
1199     xml_config_open();
1200     
1201     xml_config_bend_start();
1202
1203 #ifdef WIN32
1204     xml_config_add_listeners();
1205
1206     yaz_log (log_server, "Starting server %s", me);
1207     if (!pListener && *control_block.default_listen)
1208         add_listener(control_block.default_listen, 0);
1209 #else
1210 /* UNIX */
1211     if (control_block.inetd)
1212         inetd_connection(control_block.default_proto);
1213     else
1214     {
1215         static int hand[2];
1216         if (control_block.background)
1217         {
1218             /* create pipe so that parent waits until child has created
1219                PID (or failed) */
1220             if (pipe(hand) < 0)
1221             {
1222                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
1223                 return 1;
1224             }
1225             switch (fork())
1226             {
1227             case 0: 
1228                 break;
1229             case -1:
1230                 return 1;
1231             default:
1232                 close(hand[1]);
1233                 while(1)
1234                 {
1235                     char dummy[1];
1236                     int res = read(hand[0], dummy, 1);
1237                     if (res < 0 && yaz_errno() != EINTR)
1238                     {
1239                         yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
1240                         break;
1241                     }
1242                     else if (res >= 0)
1243                         break;
1244                 }
1245                 close(hand[0]);
1246                 _exit(0);
1247             }
1248             /* child */
1249             close(hand[0]);
1250             if (setsid() < 0)
1251                 return 1;
1252             
1253             close(0);
1254             close(1);
1255             close(2);
1256             open("/dev/null", O_RDWR);
1257             dup(0); dup(0);
1258         }
1259         xml_config_add_listeners();
1260
1261         if (!pListener && *control_block.default_listen)
1262             add_listener(control_block.default_listen, 0);
1263         
1264         if (!pListener)
1265             return 1;
1266
1267         if (*control_block.pid_fname)
1268         {
1269             FILE *f = fopen(control_block.pid_fname, "w");
1270             if (!f)
1271             {
1272                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s", 
1273                         control_block.pid_fname);
1274                 exit(0);
1275             }
1276             fprintf(f, "%ld", (long) getpid());
1277             fclose(f);
1278         }
1279         
1280         if (control_block.background)
1281             close(hand[1]);
1282
1283
1284         yaz_log (log_server, "Starting server %s pid=%ld", programname, 
1285                  (long) getpid());
1286 #if 0
1287         sigset_t sigs_to_block;
1288         
1289         sigemptyset(&sigs_to_block);
1290         sigaddset (&sigs_to_block, SIGTERM);
1291         pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
1292         /* missing... */
1293 #endif
1294         if (control_block.dynamic)
1295             signal(SIGCHLD, catchchld);
1296     }
1297     signal (SIGPIPE, SIG_IGN);
1298     signal (SIGTERM, sigterm);
1299     if (*control_block.setuid)
1300     {
1301         struct passwd *pw;
1302         
1303         if (!(pw = getpwnam(control_block.setuid)))
1304         {
1305             yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
1306             return(1);
1307         }
1308         if (setuid(pw->pw_uid) < 0)
1309         {
1310             yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
1311             exit(1);
1312         }
1313     }
1314 /* UNIX */
1315 #endif
1316     if (pListener == NULL)
1317         return 1;
1318     yaz_log(YLOG_DEBUG, "Entering event loop.");
1319     return event_loop(&pListener);
1320 }
1321
1322 static void option_copy(char *dst, const char *src)
1323 {
1324     strncpy(dst, src ? src : "", 127);
1325     dst[127] = '\0';
1326 }
1327
1328 int check_options(int argc, char **argv)
1329 {
1330     int ret = 0, r;
1331     char *arg;
1332
1333     if (getenv("YAZ_LOG") == 0) {
1334         /*
1335          * Set default log level.  We want to avoid doing this if the
1336          * user has already explicitly specified a preferred default
1337          * log-level, hence the inelegant peek at the YAZ_LOG
1338          * environment variable that will subsequently be interpreted
1339          * by the YAZ logging module itself.
1340          */
1341         yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL));
1342     }
1343
1344     get_logbits(1); 
1345     while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:m:",
1346                           argv, argc, &arg)) != -2)
1347     {
1348         switch (ret)
1349         {
1350         case 0:
1351             if (add_listener(arg, 0))
1352                 return 1;  /* failed to create listener */
1353             break;
1354         case '1':        
1355             control_block.one_shot = 1;
1356             control_block.dynamic = 0;
1357             break;
1358         case 'z':
1359             control_block.default_proto = PROTO_Z3950;
1360             break;
1361         case 's':
1362             fprintf (stderr, "%s: SR protocol no longer supported\n", me);
1363             exit (1);
1364             break;
1365         case 'S':
1366             control_block.dynamic = 0;
1367             break;
1368         case 'T':
1369 #if YAZ_POSIX_THREADS
1370             control_block.dynamic = 0;
1371             control_block.threads = 1;
1372 #elif YAZ_GNU_THREADS
1373             control_block.dynamic = 0;
1374             control_block.threads = 1;
1375 #else
1376             fprintf(stderr, "%s: Threaded mode not available.\n", me);
1377             return 1;
1378 #endif
1379             break;
1380         case 'l':
1381             option_copy(control_block.logfile, arg);
1382             yaz_log_init_file(control_block.logfile);
1383             break;
1384         case 'm':
1385             if (!arg) {
1386                 fprintf(stderr, "%s: Specify time format for log file.\n", me);
1387                 return(1);
1388             }
1389             yaz_log_time_format(arg);
1390             break;
1391         case 'v': {
1392             int default_level = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
1393             yaz_log_init_level(yaz_log_mask_str_x(arg, default_level));
1394             get_logbits(1); 
1395             break;
1396         }
1397         case 'a':
1398             option_copy(control_block.apdufile, arg);
1399             break;
1400         case 'u':
1401             option_copy(control_block.setuid, arg);
1402             break;
1403         case 'c':
1404             option_copy(control_block.configname, arg);
1405             break;
1406         case 'C':
1407             option_copy(control_block.cert_fname, arg);
1408             break;
1409         case 'd':
1410             option_copy(control_block.daemon_name, arg);
1411             break;
1412         case 't':
1413             if (!arg || !(r = atoi(arg)))
1414             {
1415                 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1416                 return(1);
1417             }
1418             control_block.idle_timeout = r;
1419             break;
1420         case  'k':
1421             if (!arg || !(r = atoi(arg)))
1422             {
1423                 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1424                 return(1);
1425             }
1426             control_block.maxrecordsize = r * 1024;
1427             break;
1428         case 'i':
1429             control_block.inetd = 1;
1430             break;
1431         case 'w':
1432             if (chdir(arg))
1433             {
1434                 perror(arg);            
1435                 return 1;
1436             }
1437             break;
1438         case 'A':
1439             max_sessions = atoi(arg);
1440             break;
1441         case 'p':
1442             option_copy(control_block.pid_fname, arg);
1443             break;
1444         case 'f':
1445 #if YAZ_HAVE_XML2
1446             option_copy(control_block.xml_config, arg);
1447 #else
1448             fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);
1449             exit(1);
1450 #endif
1451             break;
1452         case 'D':
1453             control_block.background = 1;
1454             break;
1455         default:
1456             fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1457                     " -l <logfile> -u <user> -c <config> -t <minutes>"
1458                     " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
1459                         " -ziDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
1460             return 1;
1461         }
1462     }
1463     return 0;
1464 }
1465
1466 #ifdef WIN32
1467 typedef struct _Args
1468 {
1469     char **argv;
1470     int argc;
1471 } Args; 
1472
1473 static Args ArgDetails;
1474
1475 /* name of the executable */
1476 #define SZAPPNAME            "server"
1477
1478 /* list of service dependencies - "dep1\0dep2\0\0" */
1479 #define SZDEPENDENCIES       ""
1480
1481 int statserv_main(int argc, char **argv,
1482                   bend_initresult *(*bend_init)(bend_initrequest *r),
1483                   void (*bend_close)(void *handle))
1484 {
1485     struct statserv_options_block *cb = &control_block;
1486     cb->bend_init = bend_init;
1487     cb->bend_close = bend_close;
1488
1489     /* Lets setup the Arg structure */
1490     ArgDetails.argc = argc;
1491     ArgDetails.argv = argv;
1492     
1493     /* Now setup the service with the service controller */
1494     SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1495                  cb->service_name, /* internal service name */
1496                  cb->service_display_name, /* displayed name */
1497                  SZDEPENDENCIES);
1498     return 0;
1499 }
1500
1501 int StartAppService(void *pHandle, int argc, char **argv)
1502 {
1503     /* Initializes the App */
1504     return 1;
1505 }
1506
1507 void RunAppService(void *pHandle)
1508 {
1509     Args *pArgs = (Args *)pHandle;
1510     
1511     /* Starts the app running */
1512     statserv_start(pArgs->argc, pArgs->argv);
1513 }
1514
1515 void StopAppService(void *pHandle)
1516 {
1517     /* Stops the app */
1518     statserv_closedown();
1519     statserv_reset();
1520 }
1521 /* WIN32 */
1522 #else
1523 /* UNIX */
1524 int statserv_main(int argc, char **argv,
1525                   bend_initresult *(*bend_init)(bend_initrequest *r),
1526                   void (*bend_close)(void *handle))
1527 {
1528     int ret;
1529
1530     control_block.bend_init = bend_init;
1531     control_block.bend_close = bend_close;
1532
1533     ret = statserv_start (argc, argv);
1534     statserv_closedown ();
1535     statserv_reset();
1536     return ret;
1537 }
1538 #endif
1539 /*
1540  * Local variables:
1541  * c-basic-offset: 4
1542  * indent-tabs-mode: nil
1543  * End:
1544  * vim: shiftwidth=4 tabstop=8 expandtab
1545  */
1546