2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * NT threaded server code by
6 * Chas Woodfield, Fretwell Downing Informatics.
8 * $Id: statserv.c,v 1.23 2005-03-01 20:37:01 adam Exp $
13 * \brief Implements GFS logic
26 #include <sys/types.h>
39 #include <libxml/parser.h>
40 #include <libxml/tree.h>
53 #include <yaz/comstack.h>
54 #include <yaz/tcpip.h>
55 #include <yaz/options.h>
57 #include <yaz/xmosi.h>
62 #include <yaz/statserv.h>
64 static IOCHAN pListener = NULL;
66 static struct gfs_server *gfs_server_list = 0;
67 static struct gfs_listen *gfs_listen_list = 0;
68 static NMEM gfs_nmem = 0;
70 static char *me = "statserver"; /* log prefix */
71 static char *programname="statserver"; /* full program name */
73 static pthread_key_t current_control_tls;
74 static int init_control_tls = 0;
76 static statserv_options_block *current_control_block = 0;
82 #define STAT_DEFAULT_LOG_LEVEL "none,fatal,warn,log,server,session,request"
83 /* the 'none' clears yaz' own default settings, including [log] */
85 int check_options(int argc, char **argv);
86 statserv_options_block control_block = {
88 0, /* threaded mode */
89 0, /* one shot (single session) */
90 YLOG_DEFAULT_LEVEL, /* log level */
92 "", /* diagnostic output to stderr */
93 "tcp:@:9999", /* default listener port */
94 PROTO_Z3950, /* default application protocol */
95 15, /* idle timeout (minutes) */
96 1024*1024, /* maximum PDU size (approx.) to allow */
97 "default-config", /* configuration name to pass to backend */
99 0, /* bend_start handler */
100 0, /* bend_stop handler */
101 check_options, /* Default routine, for checking the run-time arguments */
104 0, /* default value for inet deamon */
105 0, /* handle (for service, etc) */
106 0, /* bend_init handle */
107 0, /* bend_close handle */
109 "Z39.50 Server", /* NT Service Name */
110 "Server", /* NT application Name */
111 "", /* NT Service Dependencies */
112 "Z39.50 Server", /* NT Service Display Name */
114 0, /* SOAP handlers */
116 0, /* background daemon */
117 "", /* SSL certificate filename */
118 "" /* XML config filename */
121 static int max_sessions = 0;
123 static int logbits_set = 0;
124 static int log_session = 0;
125 static int log_server = 0;
127 /** get_logbits sets global loglevel bits */
128 static void get_logbits(int force)
129 { /* needs to be called after parsing cmd-line args that can set loglevels!*/
130 if (force || !logbits_set)
133 log_session = yaz_log_module_level("session");
134 log_server = yaz_log_module_level("server");
139 static int add_listener(char *where, int listen_id);
142 static xmlDocPtr xml_config_doc = 0;
146 static xmlNodePtr xml_config_get_root()
151 ptr = xmlDocGetRootElement(xml_config_doc);
152 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
153 strcmp((const char *) ptr->name, "yazgfs"))
155 yaz_log(YLOG_WARN, "Bad/missing root element for config %s",
156 control_block.xml_config);
166 static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
170 int len = 1; /* start with 1, because of trailing 0 */
172 int first = 1; /* whitespace lead flag .. */
173 /* determine length */
174 for (p = ptr; p; p = p->next)
176 if (p->type == XML_TEXT_NODE)
177 len += strlen(p->content);
179 /* now allocate for the string */
180 str = nmem_malloc(n, len);
181 *str = '\0'; /* so we can use strcat */
182 for (p = ptr; p; p = p->next)
184 if (p->type == XML_TEXT_NODE)
189 while(*cp && isspace(*cp))
192 first = 0; /* reset if we got non-whitespace out */
194 strcat(str, cp); /* append */
197 /* remove trailing whitespace */
198 cp = strlen(str) + str;
199 while ((char*) cp != str && isspace(cp[-1]))
202 /* return resulting string */
207 static struct gfs_server * gfs_server_new()
209 struct gfs_server *n = nmem_malloc(gfs_nmem, sizeof(*n));
210 memcpy(&n->cb, &control_block, sizeof(control_block));
214 n->cql_transform = 0;
218 static struct gfs_listen * gfs_listen_new(const char *id,
221 struct gfs_listen *n = nmem_malloc(gfs_nmem, sizeof(*n));
223 n->id = nmem_strdup(gfs_nmem, id);
227 n->address = nmem_strdup(gfs_nmem, address);
231 int control_association(association *assoc, const char *host, int force_open)
233 char vhost[128], *cp;
236 strncpy(vhost, host, 127);
238 cp = strchr(vhost, ':');
243 if (control_block.xml_config[0])
245 struct gfs_server *gfs;
246 for (gfs = gfs_server_list; gfs; gfs = gfs->next)
248 int listen_match = 0;
250 if ( !gfs->host || (host && gfs->host && !strcmp(host, gfs->host)))
252 if (!gfs->listen_ref ||
253 gfs->listen_ref == assoc->client_chan->chan_id)
255 if (listen_match && host_match)
258 (assoc->last_control != &gfs->cb && assoc->backend))
260 statserv_setcontrol(assoc->last_control);
261 if (assoc->backend && assoc->init)
262 (assoc->last_control->bend_close)(assoc->backend);
267 assoc->cql_transform = gfs->cql_transform;
268 assoc->last_control = &gfs->cb;
269 statserv_setcontrol(&gfs->cb);
273 statserv_setcontrol(0);
274 assoc->last_control = 0;
275 assoc->cql_transform = 0;
280 statserv_setcontrol(&control_block);
281 assoc->last_control = &control_block;
282 assoc->cql_transform = 0;
287 static void xml_config_read()
289 struct gfs_server **gfsp = &gfs_server_list;
290 struct gfs_listen **gfslp = &gfs_listen_list;
292 xmlNodePtr ptr = xml_config_get_root();
296 for (ptr = ptr->children; ptr; ptr = ptr->next)
298 if (ptr->type != XML_ELEMENT_NODE)
300 struct _xmlAttr *attr = ptr->properties;
301 if (!strcmp((const char *) ptr->name, "listen"))
304 <listen id="listenerid">tcp:@:9999</listen>
307 const char *address =
308 nmem_dup_xml_content(gfs_nmem, ptr->children);
309 for ( ; attr; attr = attr->next)
310 if (!strcmp(attr->name, "id")
311 && attr->children && attr->children->type == XML_TEXT_NODE)
312 id = nmem_dup_xml_content(gfs_nmem, attr->children);
315 *gfslp = gfs_listen_new(id, address);
316 gfslp = &(*gfslp)->next;
317 *gfslp = 0; /* make listener list consistent for search */
320 else if (!strcmp((const char *) ptr->name, "server"))
322 xmlNodePtr ptr_children = ptr->children;
324 const char *listenref = 0;
326 for ( ; attr; attr = attr->next)
327 if (!strcmp(attr->name, "listenref")
328 && attr->children && attr->children->type == XML_TEXT_NODE)
329 listenref = nmem_dup_xml_content(gfs_nmem, attr->children);
330 *gfsp = gfs_server_new();
334 struct gfs_listen *gl = gfs_listen_list;
335 for (id_no = 1; gl; gl = gl->next, id_no++)
336 if (gl->id && !strcmp(gl->id, listenref))
338 (*gfsp)->listen_ref = id_no;
342 yaz_log(YLOG_WARN, "Non-existent listenref '%s' in server "
343 "config element", listenref);
345 for (ptr = ptr_children; ptr; ptr = ptr->next)
347 if (ptr->type != XML_ELEMENT_NODE)
349 if (!strcmp((const char *) ptr->name, "host"))
351 (*gfsp)->host = nmem_dup_xml_content(gfs_nmem,
354 if (!strcmp((const char *) ptr->name, "config"))
356 strcpy((*gfsp)->cb.configname,
357 nmem_dup_xml_content(gfs_nmem, ptr->children));
359 if (!strcmp((const char *) ptr->name, "pqf2cql"))
361 (*gfsp)->cql_transform = cql_transform_open_fname(
362 nmem_dup_xml_content(gfs_nmem, ptr->children)
366 gfsp = &(*gfsp)->next;
373 static void xml_config_open()
375 gfs_nmem = nmem_create();
377 if (control_block.xml_config[0] == '\0')
382 xml_config_doc = xmlParseFile(control_block.xml_config);
385 yaz_log(YLOG_WARN, "Could not parse %s", control_block.xml_config);
394 static void xml_config_close()
399 xmlFreeDoc(xml_config_doc);
404 nmem_destroy(gfs_nmem);
407 static void xml_config_add_listeners()
409 struct gfs_listen *gfs = gfs_listen_list;
412 for (id_no = 1; gfs; gfs = gfs->next, id_no++)
415 add_listener(gfs->address, id_no);
419 static void xml_config_bend_start()
421 if (control_block.xml_config[0])
423 struct gfs_server *gfs = gfs_server_list;
424 for (; gfs; gfs = gfs->next)
426 yaz_log(YLOG_DEBUG, "xml_config_bend_start config=%s",
428 statserv_setcontrol(&gfs->cb);
429 if (control_block.bend_start)
430 (control_block.bend_start)(&gfs->cb);
435 yaz_log(YLOG_DEBUG, "xml_config_bend_start default config");
436 statserv_setcontrol(&control_block);
437 if (control_block.bend_start)
438 (*control_block.bend_start)(&control_block);
443 static void xml_config_bend_stop()
445 if (control_block.xml_config[0])
447 struct gfs_server *gfs = gfs_server_list;
448 for (; gfs; gfs = gfs->next)
450 yaz_log(YLOG_DEBUG, "xml_config_bend_stop config=%s",
452 statserv_setcontrol(&gfs->cb);
453 if (control_block.bend_stop)
454 (control_block.bend_stop)(&gfs->cb);
459 yaz_log(YLOG_DEBUG, "xml_config_bend_stop default config");
460 statserv_setcontrol(&control_block);
461 if (control_block.bend_stop)
462 (*control_block.bend_stop)(&control_block);
467 * handle incoming connect requests.
468 * The dynamic mode is a bit tricky mostly because we want to avoid
469 * doing all of the listening and accepting in the parent - it's
474 typedef struct _ThreadList ThreadList;
483 static ThreadList *pFirstThread;
484 static CRITICAL_SECTION Thread_CritSect;
485 static BOOL bInitialized = FALSE;
487 static void ThreadList_Initialize()
489 /* Initialize the critical Sections */
490 InitializeCriticalSection(&Thread_CritSect);
492 /* Set the first thraed */
495 /* we have been initialized */
499 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
501 /* Only one thread can go through this section at a time */
502 EnterCriticalSection(&Thread_CritSect);
505 /* Lets create our new object */
506 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
507 pNewThread->hThread = hThread;
508 pNewThread->pIOChannel = pIOChannel;
509 pNewThread->pNext = pFirstThread;
510 pFirstThread = pNewThread;
512 /* Lets let somebody else create a new object now */
513 LeaveCriticalSection(&Thread_CritSect);
517 void statserv_remove(IOCHAN pIOChannel)
519 /* Only one thread can go through this section at a time */
520 EnterCriticalSection(&Thread_CritSect);
523 ThreadList *pCurrentThread = pFirstThread;
524 ThreadList *pNextThread;
525 ThreadList *pPrevThread =NULL;
527 /* Step through all the threads */
528 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
530 /* We only need to compare on the IO Channel */
531 if (pCurrentThread->pIOChannel == pIOChannel)
533 /* We have found the thread we want to delete */
534 /* First of all reset the next pointers */
535 if (pPrevThread == NULL)
536 pFirstThread = pCurrentThread->pNext;
538 pPrevThread->pNext = pCurrentThread->pNext;
540 /* All we need todo now is delete the memory */
541 free(pCurrentThread);
543 /* No need to look at any more threads */
548 /* We need to look at another thread */
549 pNextThread = pCurrentThread->pNext;
550 pPrevThread = pCurrentThread;
554 /* Lets let somebody else remove an object now */
555 LeaveCriticalSection(&Thread_CritSect);
559 /* WIN32 statserv_closedown */
560 void statserv_closedown()
562 /* Shouldn't do anything if we are not initialized */
566 HANDLE *pThreadHandles = NULL;
568 /* We need to stop threads adding and removing while we */
569 /* start the closedown process */
570 EnterCriticalSection(&Thread_CritSect);
573 /* We have exclusive access to the thread stuff now */
574 /* Y didn't i use a semaphore - Oh well never mind */
575 ThreadList *pCurrentThread = pFirstThread;
577 /* Before we do anything else, we need to shutdown the listener */
578 if (pListener != NULL)
579 iochan_destroy(pListener);
581 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
583 /* Just destroy the IOCHAN, that should do the trick */
584 iochan_destroy(pCurrentThread->pIOChannel);
585 closesocket(pCurrentThread->pIOChannel->fd);
587 /* Keep a running count of our handles */
593 HANDLE *pCurrentHandle ;
595 /* Allocate the thread handle array */
596 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
597 pCurrentHandle = pThreadHandles;
599 for (pCurrentThread = pFirstThread;
600 pCurrentThread != NULL;
601 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
603 /* Just the handle */
604 *pCurrentHandle = pCurrentThread->hThread;
608 /* We can now leave the critical section */
609 LeaveCriticalSection(&Thread_CritSect);
612 /* Now we can really do something */
615 yaz_log(log_server, "waiting for %d to die", iHandles);
616 /* This will now wait, until all the threads close */
617 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
619 /* Free the memory we allocated for the handle array */
620 free(pThreadHandles);
623 xml_config_bend_stop();
624 /* No longer require the critical section, since all threads are dead */
625 DeleteCriticalSection(&Thread_CritSect);
630 void __cdecl event_loop_thread (IOCHAN iochan)
632 event_loop (&iochan);
636 static void listener(IOCHAN h, int event)
638 COMSTACK line = (COMSTACK) iochan_getdata(h);
639 IOCHAN parent_chan = line->user;
644 if (event == EVENT_INPUT)
646 if ((res = cs_listen(line, 0, 0)) < 0)
648 yaz_log(YLOG_FATAL, "cs_listen failed");
653 yaz_log(YLOG_DEBUG, "listen ok");
654 iochan_setevent(h, EVENT_OUTPUT);
655 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
657 else if (event == EVENT_OUTPUT)
659 COMSTACK new_line = cs_accept(line);
665 yaz_log(YLOG_FATAL, "Accept failed.");
666 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
669 yaz_log(YLOG_DEBUG, "Accept ok");
671 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
672 EVENT_INPUT, parent_chan->port)))
674 yaz_log(YLOG_FATAL, "Failed to create iochan");
679 yaz_log(YLOG_DEBUG, "Creating association");
680 if (!(newas = create_association(new_chan, new_line,
681 control_block.apdufile)))
683 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
687 newas->cs_get_mask = EVENT_INPUT;
688 newas->cs_put_mask = 0;
689 newas->cs_accept_mask = 0;
691 yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
692 iochan_setdata(new_chan, newas);
693 iochan_settimeout(new_chan, 60);
695 /* Now what we need todo is create a new thread with this iochan as
697 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
698 if (newHandle == (HANDLE) -1)
701 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
705 /* We successfully created the thread, so add it to the list */
706 statserv_add(newHandle, new_chan);
708 yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
709 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
713 yaz_log(YLOG_FATAL, "Bad event on listener.");
719 int statserv_must_terminate(void)
726 static int term_flag = 0;
727 /* To save having an #ifdef in event_loop we need to
728 define this empty function
730 int statserv_must_terminate(void)
735 void statserv_remove(IOCHAN pIOChannel)
739 void statserv_closedown()
743 xml_config_bend_stop();
744 for (p = pListener; p; p = p->next)
749 #if YAZ_POSIX_THREADS
750 pthread_key_delete(current_control_tls);
754 void sigterm(int sig)
759 static void *new_session (void *vp);
760 static int no_sessions = 0;
763 static void listener(IOCHAN h, int event)
765 COMSTACK line = (COMSTACK) iochan_getdata(h);
768 if (event == EVENT_INPUT)
771 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
772 control_block.daemon_name)) < 0)
774 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
779 yaz_log(YLOG_WARN, "cs_listen incomplete");
782 new_line = cs_accept(line);
785 yaz_log(YLOG_FATAL, "Accept failed.");
786 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
790 yaz_log(log_session, "Connect from %s", cs_addrstr(new_line));
793 if (control_block.dynamic)
795 if ((res = fork()) < 0)
797 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
801 else if (res == 0) /* child */
806 for (pp = pListener; pp; pp = iochan_getnext(pp))
808 COMSTACK l = (COMSTACK)iochan_getdata(pp);
812 sprintf(nbuf, "%s(%d)", me, no_sessions);
813 yaz_log_init(control_block.loglevel, nbuf, 0);
814 /* ensure that bend_stop is not called when each child exits -
815 only for the main process .. */
816 control_block.bend_stop = 0;
825 if (control_block.threads)
827 #if YAZ_POSIX_THREADS
828 pthread_t child_thread;
829 pthread_create (&child_thread, 0, new_session, new_line);
830 pthread_detach (child_thread);
831 #elif YAZ_GNU_THREADS
835 attr = pth_attr_new ();
836 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
837 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
838 pth_attr_set (attr, PTH_ATTR_NAME, "session");
839 yaz_log (YLOG_DEBUG, "pth_spawn begin");
840 child_thread = pth_spawn (attr, new_session, new_line);
841 yaz_log (YLOG_DEBUG, "pth_spawn finish");
842 pth_attr_destroy (attr);
844 new_session(new_line);
848 new_session(new_line);
850 else if (event == EVENT_TIMEOUT)
852 yaz_log(log_server, "Shutting down listener.");
857 yaz_log(YLOG_FATAL, "Bad event on listener.");
862 static void *new_session (void *vp)
867 COMSTACK new_line = (COMSTACK) vp;
868 IOCHAN parent_chan = new_line->user;
870 unsigned cs_get_mask, cs_accept_mask, mask =
871 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
872 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
876 cs_accept_mask = mask; /* accept didn't complete */
881 cs_accept_mask = 0; /* accept completed. */
882 cs_get_mask = mask = EVENT_INPUT;
885 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask,
886 parent_chan->chan_id)))
888 yaz_log(YLOG_FATAL, "Failed to create iochan");
891 if (!(newas = create_association(new_chan, new_line,
892 control_block.apdufile)))
894 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
897 newas->cs_accept_mask = cs_accept_mask;
898 newas->cs_get_mask = cs_get_mask;
900 iochan_setdata(new_chan, newas);
901 iochan_settimeout(new_chan, 60);
903 a = cs_addrstr(new_line);
907 yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
908 no_sessions, a ? a : "[Unknown]", (long) getpid());
909 if (max_sessions && no_sessions >= max_sessions)
910 control_block.one_shot = 1;
911 if (control_block.threads)
913 event_loop(&new_chan);
917 new_chan->next = pListener;
918 pListener = new_chan;
926 static void inetd_connection(int what)
933 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
935 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT,
938 if ((assoc = create_association(chan, line,
939 control_block.apdufile)))
941 iochan_setdata(chan, assoc);
942 iochan_settimeout(chan, 60);
943 addr = cs_addrstr(line);
944 yaz_log(log_session, "Inetd association from %s",
945 addr ? addr : "[UNKNOWN]");
946 assoc->cs_get_mask = EVENT_INPUT;
950 yaz_log(YLOG_FATAL, "Failed to create association structure");
952 chan->next = pListener;
957 yaz_log(YLOG_FATAL, "Failed to create iochan");
962 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
967 * Set up a listening endpoint, and give it to the event-handler.
969 static int add_listener(char *where, int listen_id)
976 if (control_block.dynamic)
978 else if (control_block.threads)
983 yaz_log(log_server, "Adding %s listener on %s id=%d", mode, where,
986 l = cs_create_host(where, 2, &ap);
989 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
992 if (*control_block.cert_fname)
993 cs_set_ssl_certificate_file(l, control_block.cert_fname);
995 if (cs_bind(l, ap, CS_SERVER) < 0)
997 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
1001 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
1002 EVENT_EXCEPT, listen_id)))
1004 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
1008 iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
1009 l->user = lst; /* user-defined data for COMSTACK is listener chan */
1011 /* Add listener to chain */
1012 lst->next = pListener;
1018 /* UNIX only (for windows we don't need to catch the signals) */
1019 static void catchchld(int num)
1021 while (waitpid(-1, 0, WNOHANG) > 0)
1023 signal(SIGCHLD, catchchld);
1027 statserv_options_block *statserv_getcontrol(void)
1029 #if YAZ_POSIX_THREADS
1030 if (init_control_tls)
1031 return pthread_getspecific(current_control_tls);
1033 return &control_block;
1035 if (current_control_block)
1036 return current_control_block;
1037 return &control_block;
1041 void statserv_setcontrol(statserv_options_block *block)
1043 #if YAZ_POSIX_THREADS
1044 if (init_control_tls)
1045 pthread_setspecific(current_control_tls, block);
1047 current_control_block = block;
1051 static void statserv_reset(void)
1055 int statserv_start(int argc, char **argv)
1059 /* We need to initialize the thread list */
1060 ThreadList_Initialize();
1070 if ((me = strrchr (argv[0], sep)))
1071 me++; /* get the basename */
1074 programname = argv[0];
1076 if (control_block.options_func(argc, argv))
1079 #if YAZ_POSIX_THREADS
1080 init_control_tls = 1;
1081 pthread_key_create(¤t_control_tls, 0);
1086 xml_config_bend_start();
1089 xml_config_add_listeners();
1091 yaz_log (log_server, "Starting server %s", me);
1092 if (!pListener && *control_block.default_listen)
1093 add_listener(control_block.default_listen, 0);
1096 if (control_block.inetd)
1097 inetd_connection(control_block.default_proto);
1101 if (control_block.background)
1103 /* create pipe so that parent waits until child has created
1107 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
1121 int res = read(hand[0], dummy, 1);
1122 if (res < 0 && yaz_errno() != EINTR)
1124 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
1141 open("/dev/null", O_RDWR);
1144 xml_config_add_listeners();
1146 if (!pListener && *control_block.default_listen)
1147 add_listener(control_block.default_listen, 0);
1152 if (*control_block.pid_fname)
1154 FILE *f = fopen(control_block.pid_fname, "w");
1157 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s",
1158 control_block.pid_fname);
1161 fprintf(f, "%ld", (long) getpid());
1165 if (control_block.background)
1169 yaz_log (log_server, "Starting server %s pid=%ld", programname,
1172 sigset_t sigs_to_block;
1174 sigemptyset(&sigs_to_block);
1175 sigaddset (&sigs_to_block, SIGTERM);
1176 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
1179 if (control_block.dynamic)
1180 signal(SIGCHLD, catchchld);
1182 signal (SIGPIPE, SIG_IGN);
1183 signal (SIGTERM, sigterm);
1184 if (*control_block.setuid)
1188 if (!(pw = getpwnam(control_block.setuid)))
1190 yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
1193 if (setuid(pw->pw_uid) < 0)
1195 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
1201 if (pListener == NULL)
1203 yaz_log(YLOG_DEBUG, "Entering event loop.");
1204 return event_loop(&pListener);
1207 static void option_copy(char *dst, const char *src)
1209 strncpy(dst, src ? src : "", 127);
1213 int check_options(int argc, char **argv)
1218 /* set default log level */
1219 control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
1220 yaz_log_init_level(control_block.loglevel);
1222 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:",
1223 argv, argc, &arg)) != -2)
1228 if (add_listener(arg, 0))
1229 return 1; /* failed to create listener */
1232 control_block.one_shot = 1;
1233 control_block.dynamic = 0;
1236 control_block.default_proto = PROTO_Z3950;
1239 fprintf (stderr, "%s: SR protocol no longer supported\n", me);
1243 control_block.dynamic = 0;
1246 #if YAZ_POSIX_THREADS
1247 control_block.dynamic = 0;
1248 control_block.threads = 1;
1249 #elif YAZ_GNU_THREADS
1250 control_block.dynamic = 0;
1251 control_block.threads = 1;
1253 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1258 option_copy(control_block.logfile, arg);
1259 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1262 control_block.loglevel =
1263 yaz_log_mask_str_x(arg,control_block.loglevel);
1264 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1267 option_copy(control_block.apdufile, arg);
1270 option_copy(control_block.setuid, arg);
1273 option_copy(control_block.configname, arg);
1276 option_copy(control_block.cert_fname, arg);
1279 option_copy(control_block.daemon_name, arg);
1282 if (!arg || !(r = atoi(arg)))
1284 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1287 control_block.idle_timeout = r;
1290 if (!arg || !(r = atoi(arg)))
1292 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1295 control_block.maxrecordsize = r * 1024;
1298 control_block.inetd = 1;
1308 max_sessions = atoi(arg);
1311 option_copy(control_block.pid_fname, arg);
1315 option_copy(control_block.xml_config, arg);
1317 fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);
1322 control_block.background = 1;
1325 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1326 " -l <logfile> -u <user> -c <config> -t <minutes>"
1327 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
1328 " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
1337 typedef struct _Args
1343 static Args ArgDetails;
1345 /* name of the executable */
1346 #define SZAPPNAME "server"
1348 /* list of service dependencies - "dep1\0dep2\0\0" */
1349 #define SZDEPENDENCIES ""
1351 int statserv_main(int argc, char **argv,
1352 bend_initresult *(*bend_init)(bend_initrequest *r),
1353 void (*bend_close)(void *handle))
1355 struct statserv_options_block *cb = &control_block;
1356 cb->bend_init = bend_init;
1357 cb->bend_close = bend_close;
1359 /* Lets setup the Arg structure */
1360 ArgDetails.argc = argc;
1361 ArgDetails.argv = argv;
1363 /* Now setup the service with the service controller */
1364 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1365 cb->service_name, /* internal service name */
1366 cb->service_display_name, /* displayed name */
1371 int StartAppService(void *pHandle, int argc, char **argv)
1373 /* Initializes the App */
1377 void RunAppService(void *pHandle)
1379 Args *pArgs = (Args *)pHandle;
1381 /* Starts the app running */
1382 statserv_start(pArgs->argc, pArgs->argv);
1385 void StopAppService(void *pHandle)
1388 statserv_closedown();
1394 int statserv_main(int argc, char **argv,
1395 bend_initresult *(*bend_init)(bend_initrequest *r),
1396 void (*bend_close)(void *handle))
1400 control_block.bend_init = bend_init;
1401 control_block.bend_close = bend_close;
1403 ret = statserv_start (argc, argv);
1404 statserv_closedown ();