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