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