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