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