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