yaz-ztest: -V shows version (like yaz-client -V)
[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     const char *last_p;
527     const char *fname = control_block.xml_config;
528     if (!getcwd(gfs_root_dir, FILENAME_MAX))
529     {
530         yaz_log(YLOG_WARN|YLOG_ERRNO, "getcwd failed");
531         gfs_root_dir[0] = '\0';
532     }
533 #ifdef WIN32
534     init_control_tls = 1;
535     current_control_tls = TlsAlloc();
536 #elif YAZ_POSIX_THREADS
537     init_control_tls = 1;
538     pthread_key_create(&current_control_tls, 0);
539 #endif
540
541     gfs_nmem = nmem_create();
542 #if YAZ_HAVE_XML2
543     if (fname[0] == '\0')
544         return;
545
546     if (!xml_config_doc)
547     {
548         xml_config_doc = xmlParseFile(fname);
549         if (!xml_config_doc)
550         {
551             yaz_log(YLOG_FATAL, "Could not parse %s", fname);
552             exit(1);
553         }
554         else
555         {
556             int noSubstitutions = xmlXIncludeProcess(xml_config_doc);
557             if (noSubstitutions == -1)
558             {
559                 yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
560                         fname);
561                 exit(1);
562             }
563         }
564     }
565     last_p = strrchr(fname,
566 #ifdef WIN32
567                      '\\'
568 #else
569                      '/'
570 #endif
571         );
572     if (last_p)
573     {
574         WRBUF base_path = wrbuf_alloc();
575         wrbuf_write(base_path, fname, last_p - fname);
576         xml_config_read(wrbuf_cstr(base_path));
577         wrbuf_destroy(base_path);
578     }
579     else
580         xml_config_read(0);
581 #endif
582 }
583
584 static void xml_config_close(void)
585 {
586 #if YAZ_HAVE_XML2
587     if (xml_config_doc)
588     {
589         xmlFreeDoc(xml_config_doc);
590         xml_config_doc = 0;
591     }
592 #endif
593     gfs_server_list = 0;
594     nmem_destroy(gfs_nmem);
595 #ifdef WIN32
596     if (init_control_tls)
597         TlsFree(current_control_tls);
598 #elif YAZ_POSIX_THREADS
599     if (init_control_tls)
600         pthread_key_delete(current_control_tls);
601 #endif
602 }
603
604 static void xml_config_add_listeners(void)
605 {
606     struct gfs_listen *gfs = gfs_listen_list;
607     int id_no;
608
609     for (id_no = 1; gfs; gfs = gfs->next, id_no++)
610     {
611         if (gfs->address)
612             add_listener(gfs->address, id_no);
613     }
614 }
615
616 static void xml_config_bend_start(void)
617 {
618     if (control_block.xml_config[0])
619     {
620         struct gfs_server *gfs = gfs_server_list;
621         for (; gfs; gfs = gfs->next)
622         {
623             yaz_log(YLOG_DEBUG, "xml_config_bend_start config=%s",
624                     gfs->cb.configname);
625             statserv_setcontrol(&gfs->cb);
626             if (control_block.bend_start)
627             {
628                 gfs_server_chdir(gfs);
629                 (control_block.bend_start)(&gfs->cb);
630             }
631         }
632     }
633     else
634     {
635         yaz_log(YLOG_DEBUG, "xml_config_bend_start default config");
636         statserv_setcontrol(&control_block);
637         if (control_block.bend_start)
638             (*control_block.bend_start)(&control_block);
639     }
640 }
641
642 static void xml_config_bend_stop(void)
643 {
644     if (control_block.xml_config[0])
645     {
646         struct gfs_server *gfs = gfs_server_list;
647         for (; gfs; gfs = gfs->next)
648         {
649             yaz_log(YLOG_DEBUG, "xml_config_bend_stop config=%s",
650                     gfs->cb.configname);
651             statserv_setcontrol(&gfs->cb);
652             if (control_block.bend_stop)
653                 (control_block.bend_stop)(&gfs->cb);
654         }
655     }
656     else
657     {
658         yaz_log(YLOG_DEBUG, "xml_config_bend_stop default config");
659         statserv_setcontrol(&control_block);
660         if (control_block.bend_stop)
661             (*control_block.bend_stop)(&control_block);
662     }
663 }
664
665 static void remove_listeners(void);
666
667 /*
668  * handle incoming connect requests.
669  * The dynamic mode is a bit tricky mostly because we want to avoid
670  * doing all of the listening and accepting in the parent - it's
671  * safer that way.
672  */
673 #ifdef WIN32
674
675 typedef struct _ThreadList ThreadList;
676
677 struct _ThreadList
678 {
679     HANDLE hThread;
680     IOCHAN pIOChannel;
681     ThreadList *pNext;
682 };
683
684 static ThreadList *pFirstThread;
685 static CRITICAL_SECTION Thread_CritSect;
686 static BOOL bInitialized = FALSE;
687
688 static void ThreadList_Initialize()
689 {
690     /* Initialize the critical Sections */
691     InitializeCriticalSection(&Thread_CritSect);
692
693     /* Set the first thraed */
694     pFirstThread = NULL;
695
696     /* we have been initialized */
697     bInitialized = TRUE;
698 }
699
700 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
701 {
702     /* Only one thread can go through this section at a time */
703     EnterCriticalSection(&Thread_CritSect);
704
705     {
706         /* Lets create our new object */
707         ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
708         pNewThread->hThread = hThread;
709         pNewThread->pIOChannel = pIOChannel;
710         pNewThread->pNext = pFirstThread;
711         pFirstThread = pNewThread;
712
713         /* Lets let somebody else create a new object now */
714         LeaveCriticalSection(&Thread_CritSect);
715     }
716 }
717
718 void statserv_remove(IOCHAN pIOChannel)
719 {
720     /* Only one thread can go through this section at a time */
721     EnterCriticalSection(&Thread_CritSect);
722
723     {
724         ThreadList *pCurrentThread = pFirstThread;
725         ThreadList *pNextThread;
726         ThreadList *pPrevThread =NULL;
727
728         /* Step through all the threads */
729         for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
730         {
731             /* We only need to compare on the IO Channel */
732             if (pCurrentThread->pIOChannel == pIOChannel)
733             {
734                 /* We have found the thread we want to delete */
735                 /* First of all reset the next pointers */
736                 if (pPrevThread == NULL)
737                     pFirstThread = pCurrentThread->pNext;
738                 else
739                     pPrevThread->pNext = pCurrentThread->pNext;
740
741                 /* All we need todo now is delete the memory */
742                 free(pCurrentThread);
743
744                 /* No need to look at any more threads */
745                 pNextThread = NULL;
746             }
747             else
748             {
749                 /* We need to look at another thread */
750                 pNextThread = pCurrentThread->pNext;
751                 pPrevThread = pCurrentThread;
752             }
753         }
754
755         /* Lets let somebody else remove an object now */
756         LeaveCriticalSection(&Thread_CritSect);
757     }
758 }
759
760 /* WIN32 statserv_closedown */
761 static void statserv_closedown()
762 {
763     /* Shouldn't do anything if we are not initialized */
764     if (bInitialized)
765     {
766         int iHandles = 0;
767         HANDLE *pThreadHandles = NULL;
768
769         /* We need to stop threads adding and removing while we */
770         /* start the closedown process */
771         EnterCriticalSection(&Thread_CritSect);
772
773         {
774             /* We have exclusive access to the thread stuff now */
775             /* Y didn't i use a semaphore - Oh well never mind */
776             ThreadList *pCurrentThread = pFirstThread;
777
778             /* Before we do anything else, we need to shutdown the listener */
779             if (pListener != NULL)
780                 iochan_destroy(pListener);
781
782             for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
783             {
784                 /* Just destroy the IOCHAN, that should do the trick */
785                 iochan_destroy(pCurrentThread->pIOChannel);
786                 closesocket(pCurrentThread->pIOChannel->fd);
787
788                 /* Keep a running count of our handles */
789                 iHandles++;
790             }
791
792             if (iHandles > 0)
793             {
794                 HANDLE *pCurrentHandle ;
795
796                 /* Allocate the thread handle array */
797                 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
798                 pCurrentHandle = pThreadHandles;
799
800                 for (pCurrentThread = pFirstThread;
801                      pCurrentThread != NULL;
802                      pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
803                 {
804                     /* Just the handle */
805                     *pCurrentHandle = pCurrentThread->hThread;
806                 }
807             }
808
809             /* We can now leave the critical section */
810             LeaveCriticalSection(&Thread_CritSect);
811         }
812
813         /* Now we can really do something */
814         if (iHandles > 0)
815         {
816             yaz_log(log_server, "waiting for %d to die", iHandles);
817             /* This will now wait, until all the threads close */
818             WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
819
820             /* Free the memory we allocated for the handle array */
821             free(pThreadHandles);
822         }
823
824         xml_config_bend_stop();
825         /* No longer require the critical section, since all threads are dead */
826         DeleteCriticalSection(&Thread_CritSect);
827     }
828     xml_config_close();
829 }
830
831 void __cdecl event_loop_thread(IOCHAN iochan)
832 {
833     iochan_event_loop(&iochan);
834 }
835
836 /* WIN32 listener */
837 static void listener(IOCHAN h, int event)
838 {
839     COMSTACK line = (COMSTACK) iochan_getdata(h);
840     IOCHAN parent_chan = line->user;
841     association *newas;
842     int res;
843     HANDLE newHandle;
844
845     if (event == EVENT_INPUT)
846     {
847         COMSTACK new_line;
848         IOCHAN new_chan;
849
850         if ((res = cs_listen(line, 0, 0)) < 0)
851         {
852             yaz_log(YLOG_FATAL|YLOG_ERRNO, "cs_listen failed");
853             return;
854         }
855         else if (res == 1)
856             return; /* incomplete */
857         yaz_log(YLOG_DEBUG, "listen ok");
858         new_line = cs_accept(line);
859         if (!new_line)
860         {
861             yaz_log(YLOG_FATAL, "Accept failed.");
862             return;
863         }
864         yaz_log(YLOG_DEBUG, "Accept ok");
865
866         if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
867                                        EVENT_INPUT, parent_chan->chan_id)))
868         {
869             yaz_log(YLOG_FATAL, "Failed to create iochan");
870             iochan_destroy(h);
871             return;
872         }
873
874         yaz_log(YLOG_DEBUG, "Creating association");
875         if (!(newas = create_association(new_chan, new_line,
876                                          control_block.apdufile)))
877         {
878             yaz_log(YLOG_FATAL, "Failed to create new assoc.");
879             iochan_destroy(h);
880             return;
881         }
882         newas->cs_get_mask = EVENT_INPUT;
883         newas->cs_put_mask = 0;
884         newas->cs_accept_mask = 0;
885
886         yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
887         iochan_setdata(new_chan, newas);
888         iochan_settimeout(new_chan, 60);
889
890         /* Now what we need todo is create a new thread with this iochan as
891            the parameter */
892         newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
893         if (newHandle == (HANDLE) -1)
894         {
895
896             yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
897             iochan_destroy(h);
898             return;
899         }
900         /* We successfully created the thread, so add it to the list */
901         statserv_add(newHandle, new_chan);
902
903         yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
904         iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
905     }
906     else
907     {
908         yaz_log(YLOG_FATAL, "Bad event on listener.");
909         iochan_destroy(h);
910         return;
911     }
912 }
913
914 #else /* ! WIN32 */
915
916 /* To save having an #ifdef in event_loop we need to
917    define this empty function
918 */
919 void statserv_remove(IOCHAN pIOChannel)
920 {
921 }
922
923 static void statserv_closedown(void)
924 {
925     IOCHAN p;
926
927     xml_config_bend_stop();
928     for (p = pListener; p; p = p->next)
929     {
930         iochan_destroy(p);
931     }
932     xml_config_close();
933 }
934
935 static void *new_session(void *vp);
936 static int no_sessions = 0;
937
938 /* UNIX listener */
939 static void listener(IOCHAN h, int event)
940 {
941     COMSTACK line = (COMSTACK) iochan_getdata(h);
942     int res;
943
944     if (event == EVENT_INPUT)
945     {
946         COMSTACK new_line;
947         if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
948                                    control_block.daemon_name)) < 0)
949         {
950             yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
951             return;
952         }
953         else if (res == 1)
954         {
955             yaz_log(YLOG_WARN, "cs_listen incomplete");
956             return;
957         }
958         new_line = cs_accept(line);
959         if (!new_line)
960         {
961             yaz_log(YLOG_FATAL, "Accept failed.");
962             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
963             return;
964         }
965
966         if (control_block.one_shot)
967             remove_listeners();
968
969         yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line));
970
971         no_sessions++;
972         if (control_block.dynamic)
973         {
974             if ((res = fork()) < 0)
975             {
976                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
977                 iochan_destroy(h);
978                 return;
979             }
980             else if (res == 0) /* child */
981             {
982                 char nbuf[100];
983                 IOCHAN pp;
984
985                 for (pp = pListener; pp; pp = iochan_getnext(pp))
986                 {
987                     COMSTACK l = (COMSTACK)iochan_getdata(pp);
988                     cs_close(l);
989                     iochan_destroy(pp);
990                 }
991                 sprintf(nbuf, "%s(%d)", me, no_sessions);
992                 yaz_log_init_prefix(nbuf);
993                 /* ensure that bend_stop is not called when each child exits -
994                    only for the main process ..  */
995                 control_block.bend_stop = 0;
996             }
997             else /* parent */
998             {
999                 cs_close(new_line);
1000                 return;
1001             }
1002         }
1003
1004         if (control_block.threads)
1005         {
1006 #if YAZ_POSIX_THREADS
1007             pthread_t child_thread;
1008             pthread_create(&child_thread, 0, new_session, new_line);
1009             pthread_detach(child_thread);
1010 #else
1011             new_session(new_line);
1012 #endif
1013         }
1014         else
1015             new_session(new_line);
1016     }
1017     else if (event == EVENT_TIMEOUT)
1018     {
1019         yaz_log(log_server, "Shutting down listener.");
1020         iochan_destroy(h);
1021     }
1022     else
1023     {
1024         yaz_log(YLOG_FATAL, "Bad event on listener.");
1025         iochan_destroy(h);
1026     }
1027 }
1028
1029 static void *new_session(void *vp)
1030 {
1031     const char *a;
1032     association *newas;
1033     IOCHAN new_chan;
1034     COMSTACK new_line = (COMSTACK) vp;
1035     IOCHAN parent_chan = (IOCHAN) new_line->user;
1036
1037     unsigned cs_get_mask, cs_accept_mask, mask =
1038         ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
1039         ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
1040
1041     if (mask)
1042     {
1043         cs_accept_mask = mask;  /* accept didn't complete */
1044         cs_get_mask = 0;
1045     }
1046     else
1047     {
1048         cs_accept_mask = 0;     /* accept completed.  */
1049         cs_get_mask = mask = EVENT_INPUT;
1050     }
1051
1052     if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask,
1053                                    parent_chan->chan_id)))
1054     {
1055         yaz_log(YLOG_FATAL, "Failed to create iochan");
1056         return 0;
1057     }
1058     if (!(newas = create_association(new_chan, new_line,
1059                                      control_block.apdufile)))
1060     {
1061         yaz_log(YLOG_FATAL, "Failed to create new assoc.");
1062         return 0;
1063     }
1064     newas->cs_accept_mask = cs_accept_mask;
1065     newas->cs_get_mask = cs_get_mask;
1066
1067     iochan_setdata(new_chan, newas);
1068     iochan_settimeout(new_chan, 60);
1069 #if 1
1070     a = cs_addrstr(new_line);
1071 #else
1072     a = 0;
1073 #endif
1074     yaz_log_xml_errors(0, YLOG_WARN);
1075     yaz_log(log_session, "Session - OK %d %s PID=%ld",
1076             no_sessions, a ? a : "[Unknown]", (long) getpid());
1077     if (max_sessions && no_sessions >= max_sessions)
1078         control_block.one_shot = 1;
1079     if (control_block.threads)
1080     {
1081         iochan_event_loop(&new_chan);
1082     }
1083     else
1084     {
1085         new_chan->next = pListener;
1086         pListener = new_chan;
1087     }
1088     return 0;
1089 }
1090
1091 /* UNIX */
1092 #endif
1093
1094 static void inetd_connection(int what)
1095 {
1096     COMSTACK line;
1097     IOCHAN chan;
1098     association *assoc;
1099     const char *addr;
1100
1101     if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
1102     {
1103         if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT,
1104                                   0)))
1105         {
1106             if ((assoc = create_association(chan, line,
1107                                             control_block.apdufile)))
1108             {
1109                 iochan_setdata(chan, assoc);
1110                 iochan_settimeout(chan, 60);
1111                 addr = cs_addrstr(line);
1112                 yaz_log(log_sessiondetail, "Inetd association from %s",
1113                         addr ? addr : "[UNKNOWN]");
1114                 assoc->cs_get_mask = EVENT_INPUT;
1115             }
1116             else
1117             {
1118                 yaz_log(YLOG_FATAL, "Failed to create association structure");
1119             }
1120             chan->next = pListener;
1121             pListener = chan;
1122         }
1123         else
1124         {
1125             yaz_log(YLOG_FATAL, "Failed to create iochan");
1126         }
1127     }
1128     else
1129     {
1130         yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
1131     }
1132 }
1133
1134 /*
1135  * Set up a listening endpoint, and give it to the event-handler.
1136  */
1137 static int add_listener(char *where, int listen_id)
1138 {
1139     COMSTACK l;
1140     void *ap;
1141     IOCHAN lst = NULL;
1142     const char *mode;
1143
1144     if (control_block.dynamic)
1145         mode = "dynamic";
1146     else if (control_block.threads)
1147         mode = "threaded";
1148     else
1149         mode = "static";
1150
1151     yaz_log(log_server, "Adding %s listener on %s id=%d PID=%ld", mode, where,
1152             listen_id, (long) getpid());
1153
1154     l = cs_create_host(where, 2, &ap);
1155     if (!l)
1156     {
1157         yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
1158         return -1;
1159     }
1160     if (*control_block.cert_fname)
1161         cs_set_ssl_certificate_file(l, control_block.cert_fname);
1162
1163     if (cs_bind(l, ap, CS_SERVER) < 0)
1164     {
1165         if (cs_errno(l) == CSYSERR)
1166             yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
1167         else
1168             yaz_log(YLOG_FATAL, "Failed to bind to %s: %s", where,
1169                     cs_strerror(l));
1170         cs_close(l);
1171         return -1;
1172     }
1173     if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
1174                               EVENT_EXCEPT, listen_id)))
1175     {
1176         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
1177         cs_close(l);
1178         return -1;
1179     }
1180     iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
1181     l->user = lst;  /* user-defined data for COMSTACK is listener chan */
1182
1183     /* Add listener to chain */
1184     lst->next = pListener;
1185     pListener = lst;
1186     return 0; /* OK */
1187 }
1188
1189 static void remove_listeners(void)
1190 {
1191     IOCHAN l = pListener;
1192     for (; l; l = l->next)
1193         iochan_destroy(l);
1194 }
1195
1196 #ifndef WIN32
1197 /* UNIX only (for windows we don't need to catch the signals) */
1198 static void catchchld(int num)
1199 {
1200     while (waitpid(-1, 0, WNOHANG) > 0)
1201         ;
1202     signal(SIGCHLD, catchchld);
1203 }
1204 #endif
1205
1206 statserv_options_block *statserv_getcontrol(void)
1207 {
1208 #ifdef WIN32
1209     if (init_control_tls)
1210         return (statserv_options_block *) TlsGetValue(current_control_tls);
1211     else
1212         return &control_block;
1213 #elif YAZ_POSIX_THREADS
1214     if (init_control_tls)
1215         return (statserv_options_block *)
1216             pthread_getspecific(current_control_tls);
1217     else
1218         return &control_block;
1219 #else
1220     if (current_control_block)
1221         return current_control_block;
1222     return &control_block;
1223 #endif
1224 }
1225
1226 void statserv_setcontrol(statserv_options_block *block)
1227 {
1228     if (gfs_root_dir[0])
1229     {
1230         if (chdir(gfs_root_dir))
1231             yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
1232     }
1233 #ifdef WIN32
1234     if (init_control_tls)
1235         TlsSetValue(current_control_tls, block);
1236 #elif YAZ_POSIX_THREADS
1237     if (init_control_tls)
1238         pthread_setspecific(current_control_tls, block);
1239 #else
1240     current_control_block = block;
1241 #endif
1242 }
1243
1244 static void statserv_reset(void)
1245 {
1246 }
1247
1248 static void daemon_handler(void *data)
1249 {
1250     IOCHAN *pListener = data;
1251     iochan_event_loop(pListener);
1252 }
1253
1254 #ifndef WIN32
1255 static void normal_stop_handler(int num)
1256 {
1257     yaz_log(log_server, "Received SIGTERM. PID=%ld", (long) getpid());
1258     exit(0);
1259 }
1260 #endif
1261
1262 static void show_version(void)
1263 {
1264     char vstr[20], sha1_str[41];
1265
1266     yaz_version(vstr, sha1_str);
1267     printf("YAZ version: %s %s\n", YAZ_VERSION, YAZ_VERSION_SHA1);
1268     if (strcmp(sha1_str, YAZ_VERSION_SHA1))
1269         printf("YAZ DLL/SO: %s %s\n", vstr, sha1_str);
1270     exit(0);
1271 }
1272
1273 static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
1274 {
1275     char sep;
1276 #ifdef WIN32
1277     /* We need to initialize the thread list */
1278     ThreadList_Initialize();
1279 /* WIN32 */
1280 #endif
1281
1282
1283 #ifdef WIN32
1284     sep = '\\';
1285 #else
1286     sep = '/';
1287 #endif
1288     if ((me = strrchr(argv[0], sep)))
1289         me++; /* get the basename */
1290     else
1291         me = argv[0];
1292     programname = argv[0];
1293
1294     if (control_block.options_func(argc, argv))
1295         return 1;
1296
1297     xml_config_open();
1298
1299     xml_config_bend_start();
1300
1301     if (control_block.inetd)
1302     {
1303 #ifdef WIN32
1304         ; /* no inetd on Windows */
1305 #else
1306         inetd_connection(control_block.default_proto);
1307 #endif
1308     }
1309     else
1310     {
1311         xml_config_add_listeners();
1312
1313         if (!pListener)
1314             add_listener("tcp:@:9999", 0);
1315
1316 #ifndef WIN32
1317         if (control_block.dynamic)
1318             signal(SIGCHLD, catchchld);
1319 #endif
1320     }
1321     if (pListener == NULL)
1322         return 1;
1323     if (s)
1324         yaz_sc_running(s);
1325
1326 #ifndef WIN32
1327     signal(SIGTERM, normal_stop_handler);
1328 #endif
1329     yaz_daemon(programname,
1330                (control_block.background ? YAZ_DAEMON_FORK : 0),
1331                daemon_handler, &pListener,
1332                *control_block.pid_fname ? control_block.pid_fname : 0,
1333                *control_block.setuid ? control_block.setuid : 0);
1334     return 0;
1335 }
1336
1337 static void option_copy(char *dst, const char *src)
1338 {
1339     strncpy(dst, src ? src : "", BEND_NAME_MAX-1);
1340     dst[BEND_NAME_MAX-1] = '\0';
1341 }
1342
1343 int check_options(int argc, char **argv)
1344 {
1345     int ret = 0, r;
1346     char *arg;
1347
1348     yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL));
1349
1350     get_logbits(1);
1351
1352     while ((ret = options("1a:iszSTl:v:u:c:w:t:k:Kd:A:p:DC:f:m:r:V",
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 #else
1380             fprintf(stderr, "%s: Threaded mode not available.\n", me);
1381             return 1;
1382 #endif
1383             break;
1384         case 'l':
1385             option_copy(control_block.logfile, arg);
1386             yaz_log_init_file(control_block.logfile);
1387             break;
1388         case 'm':
1389             if (!arg) {
1390                 fprintf(stderr, "%s: Specify time format for log file.\n", me);
1391                 return(1);
1392             }
1393             yaz_log_time_format(arg);
1394             break;
1395         case 'v':
1396             yaz_log_init_level(yaz_log_mask_str(arg));
1397             get_logbits(1);
1398             break;
1399         case 'a':
1400             option_copy(control_block.apdufile, arg);
1401             break;
1402         case 'u':
1403             option_copy(control_block.setuid, arg);
1404             break;
1405         case 'c':
1406             option_copy(control_block.configname, arg);
1407             break;
1408         case 'C':
1409             option_copy(control_block.cert_fname, arg);
1410             break;
1411         case 'd':
1412             option_copy(control_block.daemon_name, arg);
1413             break;
1414         case 't':
1415             if (!arg || !(r = atoi(arg)))
1416             {
1417                 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1418                 return(1);
1419             }
1420             control_block.idle_timeout = strchr(arg, 's') ? r : 60 * r;
1421             break;
1422         case  'k':
1423             if (!arg || !(r = atoi(arg)))
1424             {
1425                 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1426                 return(1);
1427             }
1428             control_block.maxrecordsize = r * 1024;
1429             break;
1430         case 'K':
1431             control_block.keepalive = 0;
1432             break;
1433         case 'i':
1434             control_block.inetd = 1;
1435             break;
1436         case 'w':
1437             if (chdir(arg))
1438             {
1439                 perror(arg);
1440                 return 1;
1441             }
1442             break;
1443         case 'A':
1444             max_sessions = atoi(arg);
1445             break;
1446         case 'p':
1447             option_copy(control_block.pid_fname, arg);
1448             break;
1449         case 'f':
1450 #if YAZ_HAVE_XML2
1451             option_copy(control_block.xml_config, arg);
1452 #else
1453             fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);
1454             exit(1);
1455 #endif
1456             break;
1457         case 'D':
1458             control_block.background = 1;
1459             break;
1460         case 'r':
1461             if (!arg || !(r = atoi(arg)))
1462             {
1463                 fprintf(stderr, "%s: Specify positive size for -r.\n", me);
1464                 return(1);
1465             }
1466             yaz_log_init_max_size(r * 1024);
1467             break;
1468         case 'V':
1469             show_version();
1470             break;
1471         default:
1472             fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1473                     " -l <logfile> -u <user> -c <config> -t <minutes>"
1474                     " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
1475                     " -zKiDSTV1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
1476             return 1;
1477         }
1478     }
1479     return 0;
1480 }
1481
1482 void statserv_sc_stop(yaz_sc_t s)
1483 {
1484     statserv_closedown();
1485     statserv_reset();
1486 }
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     int ret;
1493     struct statserv_options_block *cb = &control_block;
1494
1495     /* control block does not have service_name member on Unix */
1496     yaz_sc_t s = yaz_sc_create(
1497 #ifdef WIN32
1498         cb->service_name, cb->service_display_name
1499 #else
1500         0, 0
1501 #endif
1502         );
1503
1504     cb->bend_init = bend_init;
1505     cb->bend_close = bend_close;
1506
1507     ret = yaz_sc_program(s, argc, argv, statserv_sc_main, statserv_sc_stop);
1508     yaz_sc_destroy(&s);
1509     return ret;
1510 }
1511
1512 /*
1513  * Local variables:
1514  * c-basic-offset: 4
1515  * c-file-style: "Stroustrup"
1516  * indent-tabs-mode: nil
1517  * End:
1518  * vim: shiftwidth=4 tabstop=8 expandtab
1519  */
1520