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