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.22 2005-02-07 11:23:47 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 NMEM gfs_nmem = 0;
69 static char *me = "statserver"; /* log prefix */
70 static char *programname="statserver"; /* full program name */
72 static pthread_key_t current_control_tls;
73 static int init_control_tls = 0;
75 static statserv_options_block *current_control_block = 0;
81 #define STAT_DEFAULT_LOG_LEVEL "none,fatal,warn,log,server,session,request"
82 /* the 'none' clears yaz' own default settings, including [log] */
84 int check_options(int argc, char **argv);
85 statserv_options_block control_block = {
87 0, /* threaded mode */
88 0, /* one shot (single session) */
89 YLOG_DEFAULT_LEVEL, /* log level */
91 "", /* diagnostic output to stderr */
92 "tcp:@:9999", /* default listener port */
93 PROTO_Z3950, /* default application protocol */
94 15, /* idle timeout (minutes) */
95 1024*1024, /* maximum PDU size (approx.) to allow */
96 "default-config", /* configuration name to pass to backend */
98 0, /* bend_start handler */
99 0, /* bend_stop handler */
100 check_options, /* Default routine, for checking the run-time arguments */
103 0, /* default value for inet deamon */
104 0, /* handle (for service, etc) */
105 0, /* bend_init handle */
106 0, /* bend_close handle */
108 "Z39.50 Server", /* NT Service Name */
109 "Server", /* NT application Name */
110 "", /* NT Service Dependencies */
111 "Z39.50 Server", /* NT Service Display Name */
113 0, /* SOAP handlers */
115 0, /* background daemon */
116 "", /* SSL certificate filename */
117 "", /* XML config filename */
120 static int max_sessions = 0;
122 static int logbits_set = 0;
123 static int log_session = 0;
124 static int log_server = 0;
126 /** get_logbits sets global loglevel bits */
127 static void get_logbits(int force)
128 { /* needs to be called after parsing cmd-line args that can set loglevels!*/
129 if (force || !logbits_set)
132 log_session = yaz_log_module_level("session");
133 log_server = yaz_log_module_level("server");
138 static int add_listener(char *where, int listen_id);
141 static xmlDocPtr xml_config_doc = 0;
145 static xmlNodePtr xml_config_get_root()
150 ptr = xmlDocGetRootElement(xml_config_doc);
151 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
152 strcmp((const char *) ptr->name, "yazgfs"))
154 yaz_log(YLOG_WARN, "Bad/missing root element for config %s",
155 control_block.xml_config);
165 static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
169 int len = 1; /* start with 1, because of trailing 0 */
171 int first = 1; /* whitespace lead flag .. */
172 /* determine length */
173 for (p = ptr; p; p = p->next)
175 if (p->type == XML_TEXT_NODE)
176 len += strlen(p->content);
178 /* now allocate for the string */
179 str = nmem_malloc(n, len);
180 *str = '\0'; /* so we can use strcat */
181 for (p = ptr; p; p = p->next)
183 if (p->type == XML_TEXT_NODE)
188 while(*cp && isspace(*cp))
191 first = 0; /* reset if we got non-whitespace out */
193 strcat(str, cp); /* append */
196 /* remove trailing whitespace */
197 cp = strlen(str) + str;
198 while ((char*) cp != str && isspace(cp[-1]))
201 /* return resulting string */
206 static struct gfs_server * gfs_server_new()
208 struct gfs_server *n = nmem_malloc(gfs_nmem, sizeof(*n));
209 memcpy(&n->cb, &control_block, sizeof(control_block));
216 int control_association(association *assoc, const char *host, int force_open)
218 char vhost[128], *cp;
221 strncpy(vhost, host, 127);
223 cp = strchr(vhost, ':');
228 if (control_block.xml_config[0])
230 struct gfs_server *gfs;
231 for (gfs = gfs_server_list; gfs; gfs = gfs->next)
235 if ( !gfs->host || (host && gfs->host && !strcmp(host, gfs->host)))
237 if (assoc->client_chan->port == gfs->port)
239 if (port_match && host_match)
242 (assoc->last_control != &gfs->cb && assoc->backend))
244 statserv_setcontrol(assoc->last_control);
245 if (assoc->backend && assoc->init)
246 (assoc->last_control->bend_close)(assoc->backend);
251 assoc->last_control = &gfs->cb;
252 statserv_setcontrol(&gfs->cb);
256 statserv_setcontrol(0);
257 assoc->last_control = 0;
262 statserv_setcontrol(&control_block);
263 assoc->last_control = &control_block;
268 static void xml_config_read()
270 struct gfs_server **gfsp = &gfs_server_list;
272 xmlNodePtr ptr = xml_config_get_root();
276 for (ptr = ptr->children; ptr; ptr = ptr->next)
278 if (ptr->type == XML_ELEMENT_NODE &&
279 !strcmp((const char *) ptr->name, "server"))
281 xmlNodePtr ptr_children = ptr->children;
284 *gfsp = gfs_server_new();
285 for (ptr = ptr_children; ptr; ptr = ptr->next)
287 if (ptr->type == XML_ELEMENT_NODE &&
288 !strcmp((const char *) ptr->name, "host"))
290 (*gfsp)->host = nmem_dup_xml_content(gfs_nmem,
293 if (ptr->type == XML_ELEMENT_NODE &&
294 !strcmp((const char *) ptr->name, "port"))
296 (*gfsp)->port = atoi(nmem_dup_xml_content(gfs_nmem,
299 if (ptr->type == XML_ELEMENT_NODE &&
300 !strcmp((const char *) ptr->name, "config"))
302 strcpy((*gfsp)->cb.configname,
303 nmem_dup_xml_content(gfs_nmem, ptr->children));
306 gfsp = &(*gfsp)->next;
313 static void xml_config_open()
315 gfs_nmem = nmem_create();
317 if (control_block.xml_config[0] == '\0')
322 xml_config_doc = xmlParseFile(control_block.xml_config);
325 yaz_log(YLOG_WARN, "Could not parse %s", control_block.xml_config);
334 static void xml_config_close()
339 xmlFreeDoc(xml_config_doc);
344 nmem_destroy(gfs_nmem);
347 static void xml_config_add_listeners()
349 #define MAX_PORTS 200
350 struct gfs_server *gfs = gfs_server_list;
351 int i, ports[MAX_PORTS];
352 for (i = 0; i<MAX_PORTS; i++)
355 for (; gfs; gfs = gfs->next)
357 int port = gfs->port;
360 for (i = 0; i<MAX_PORTS && ports[i] != port; i++)
368 for (i = 0; i<MAX_PORTS && ports[i]; i++)
371 sprintf(where, "@:%d", ports[i]);
372 add_listener(where, ports[i]);
376 static void xml_config_bend_start()
378 if (control_block.xml_config[0])
380 struct gfs_server *gfs = gfs_server_list;
381 for (; gfs; gfs = gfs->next)
383 yaz_log(YLOG_DEBUG, "xml_config_bend_start config=%s",
385 statserv_setcontrol(&gfs->cb);
386 if (control_block.bend_start)
387 (control_block.bend_start)(&gfs->cb);
392 yaz_log(YLOG_DEBUG, "xml_config_bend_start default config");
393 statserv_setcontrol(&control_block);
394 if (control_block.bend_start)
395 (*control_block.bend_start)(&control_block);
400 static void xml_config_bend_stop()
402 if (control_block.xml_config[0])
404 struct gfs_server *gfs = gfs_server_list;
405 for (; gfs; gfs = gfs->next)
407 yaz_log(YLOG_DEBUG, "xml_config_bend_stop config=%s",
409 statserv_setcontrol(&gfs->cb);
410 if (control_block.bend_stop)
411 (control_block.bend_stop)(&gfs->cb);
416 yaz_log(YLOG_DEBUG, "xml_config_bend_stop default config");
417 statserv_setcontrol(&control_block);
418 if (control_block.bend_stop)
419 (*control_block.bend_stop)(&control_block);
424 * handle incoming connect requests.
425 * The dynamic mode is a bit tricky mostly because we want to avoid
426 * doing all of the listening and accepting in the parent - it's
431 typedef struct _ThreadList ThreadList;
440 static ThreadList *pFirstThread;
441 static CRITICAL_SECTION Thread_CritSect;
442 static BOOL bInitialized = FALSE;
444 static void ThreadList_Initialize()
446 /* Initialize the critical Sections */
447 InitializeCriticalSection(&Thread_CritSect);
449 /* Set the first thraed */
452 /* we have been initialized */
456 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
458 /* Only one thread can go through this section at a time */
459 EnterCriticalSection(&Thread_CritSect);
462 /* Lets create our new object */
463 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
464 pNewThread->hThread = hThread;
465 pNewThread->pIOChannel = pIOChannel;
466 pNewThread->pNext = pFirstThread;
467 pFirstThread = pNewThread;
469 /* Lets let somebody else create a new object now */
470 LeaveCriticalSection(&Thread_CritSect);
474 void statserv_remove(IOCHAN pIOChannel)
476 /* Only one thread can go through this section at a time */
477 EnterCriticalSection(&Thread_CritSect);
480 ThreadList *pCurrentThread = pFirstThread;
481 ThreadList *pNextThread;
482 ThreadList *pPrevThread =NULL;
484 /* Step through all the threads */
485 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
487 /* We only need to compare on the IO Channel */
488 if (pCurrentThread->pIOChannel == pIOChannel)
490 /* We have found the thread we want to delete */
491 /* First of all reset the next pointers */
492 if (pPrevThread == NULL)
493 pFirstThread = pCurrentThread->pNext;
495 pPrevThread->pNext = pCurrentThread->pNext;
497 /* All we need todo now is delete the memory */
498 free(pCurrentThread);
500 /* No need to look at any more threads */
505 /* We need to look at another thread */
506 pNextThread = pCurrentThread->pNext;
507 pPrevThread = pCurrentThread;
511 /* Lets let somebody else remove an object now */
512 LeaveCriticalSection(&Thread_CritSect);
516 /* WIN32 statserv_closedown */
517 void statserv_closedown()
519 /* Shouldn't do anything if we are not initialized */
523 HANDLE *pThreadHandles = NULL;
525 /* We need to stop threads adding and removing while we */
526 /* start the closedown process */
527 EnterCriticalSection(&Thread_CritSect);
530 /* We have exclusive access to the thread stuff now */
531 /* Y didn't i use a semaphore - Oh well never mind */
532 ThreadList *pCurrentThread = pFirstThread;
534 /* Before we do anything else, we need to shutdown the listener */
535 if (pListener != NULL)
536 iochan_destroy(pListener);
538 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
540 /* Just destroy the IOCHAN, that should do the trick */
541 iochan_destroy(pCurrentThread->pIOChannel);
542 closesocket(pCurrentThread->pIOChannel->fd);
544 /* Keep a running count of our handles */
550 HANDLE *pCurrentHandle ;
552 /* Allocate the thread handle array */
553 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
554 pCurrentHandle = pThreadHandles;
556 for (pCurrentThread = pFirstThread;
557 pCurrentThread != NULL;
558 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
560 /* Just the handle */
561 *pCurrentHandle = pCurrentThread->hThread;
565 /* We can now leave the critical section */
566 LeaveCriticalSection(&Thread_CritSect);
569 /* Now we can really do something */
572 yaz_log(log_server, "waiting for %d to die", iHandles);
573 /* This will now wait, until all the threads close */
574 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
576 /* Free the memory we allocated for the handle array */
577 free(pThreadHandles);
580 xml_config_bend_stop();
581 /* No longer require the critical section, since all threads are dead */
582 DeleteCriticalSection(&Thread_CritSect);
587 void __cdecl event_loop_thread (IOCHAN iochan)
589 event_loop (&iochan);
593 static void listener(IOCHAN h, int event)
595 COMSTACK line = (COMSTACK) iochan_getdata(h);
596 IOCHAN parent_chan = line->user;
601 if (event == EVENT_INPUT)
603 if ((res = cs_listen(line, 0, 0)) < 0)
605 yaz_log(YLOG_FATAL, "cs_listen failed");
610 yaz_log(YLOG_DEBUG, "listen ok");
611 iochan_setevent(h, EVENT_OUTPUT);
612 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
614 else if (event == EVENT_OUTPUT)
616 COMSTACK new_line = cs_accept(line);
622 yaz_log(YLOG_FATAL, "Accept failed.");
623 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
626 yaz_log(YLOG_DEBUG, "Accept ok");
628 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
629 EVENT_INPUT, parent_chan->port)))
631 yaz_log(YLOG_FATAL, "Failed to create iochan");
636 yaz_log(YLOG_DEBUG, "Creating association");
637 if (!(newas = create_association(new_chan, new_line,
638 control_block.apdufile)))
640 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
644 newas->cs_get_mask = EVENT_INPUT;
645 newas->cs_put_mask = 0;
646 newas->cs_accept_mask = 0;
648 yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
649 iochan_setdata(new_chan, newas);
650 iochan_settimeout(new_chan, 60);
652 /* Now what we need todo is create a new thread with this iochan as
654 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
655 if (newHandle == (HANDLE) -1)
658 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
662 /* We successfully created the thread, so add it to the list */
663 statserv_add(newHandle, new_chan);
665 yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
666 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
670 yaz_log(YLOG_FATAL, "Bad event on listener.");
676 int statserv_must_terminate(void)
683 static int term_flag = 0;
684 /* To save having an #ifdef in event_loop we need to
685 define this empty function
687 int statserv_must_terminate(void)
692 void statserv_remove(IOCHAN pIOChannel)
696 void statserv_closedown()
700 xml_config_bend_stop();
701 for (p = pListener; p; p = p->next)
706 #if YAZ_POSIX_THREADS
707 pthread_key_delete(current_control_tls);
711 void sigterm(int sig)
716 static void *new_session (void *vp);
717 static int no_sessions = 0;
720 static void listener(IOCHAN h, int event)
722 COMSTACK line = (COMSTACK) iochan_getdata(h);
725 if (event == EVENT_INPUT)
728 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
729 control_block.daemon_name)) < 0)
731 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
736 yaz_log(YLOG_WARN, "cs_listen incomplete");
739 new_line = cs_accept(line);
742 yaz_log(YLOG_FATAL, "Accept failed.");
743 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
747 yaz_log(log_session, "Connect from %s", cs_addrstr(new_line));
750 if (control_block.dynamic)
752 if ((res = fork()) < 0)
754 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
758 else if (res == 0) /* child */
763 for (pp = pListener; pp; pp = iochan_getnext(pp))
765 COMSTACK l = (COMSTACK)iochan_getdata(pp);
769 sprintf(nbuf, "%s(%d)", me, no_sessions);
770 yaz_log_init(control_block.loglevel, nbuf, 0);
771 /* ensure that bend_stop is not called when each child exits -
772 only for the main process .. */
773 control_block.bend_stop = 0;
782 if (control_block.threads)
784 #if YAZ_POSIX_THREADS
785 pthread_t child_thread;
786 pthread_create (&child_thread, 0, new_session, new_line);
787 pthread_detach (child_thread);
788 #elif YAZ_GNU_THREADS
792 attr = pth_attr_new ();
793 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
794 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
795 pth_attr_set (attr, PTH_ATTR_NAME, "session");
796 yaz_log (YLOG_DEBUG, "pth_spawn begin");
797 child_thread = pth_spawn (attr, new_session, new_line);
798 yaz_log (YLOG_DEBUG, "pth_spawn finish");
799 pth_attr_destroy (attr);
801 new_session(new_line);
805 new_session(new_line);
807 else if (event == EVENT_TIMEOUT)
809 yaz_log(log_server, "Shutting down listener.");
814 yaz_log(YLOG_FATAL, "Bad event on listener.");
819 static void *new_session (void *vp)
824 COMSTACK new_line = (COMSTACK) vp;
825 IOCHAN parent_chan = new_line->user;
827 unsigned cs_get_mask, cs_accept_mask, mask =
828 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
829 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
833 cs_accept_mask = mask; /* accept didn't complete */
838 cs_accept_mask = 0; /* accept completed. */
839 cs_get_mask = mask = EVENT_INPUT;
842 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask,
845 yaz_log(YLOG_FATAL, "Failed to create iochan");
848 if (!(newas = create_association(new_chan, new_line,
849 control_block.apdufile)))
851 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
854 newas->cs_accept_mask = cs_accept_mask;
855 newas->cs_get_mask = cs_get_mask;
857 iochan_setdata(new_chan, newas);
858 iochan_settimeout(new_chan, 60);
860 a = cs_addrstr(new_line);
864 yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
865 no_sessions, a ? a : "[Unknown]", (long) getpid());
866 if (max_sessions && no_sessions >= max_sessions)
867 control_block.one_shot = 1;
868 if (control_block.threads)
870 event_loop(&new_chan);
874 new_chan->next = pListener;
875 pListener = new_chan;
883 static void inetd_connection(int what)
890 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
892 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT,
895 if ((assoc = create_association(chan, line,
896 control_block.apdufile)))
898 iochan_setdata(chan, assoc);
899 iochan_settimeout(chan, 60);
900 addr = cs_addrstr(line);
901 yaz_log(log_session, "Inetd association from %s",
902 addr ? addr : "[UNKNOWN]");
903 assoc->cs_get_mask = EVENT_INPUT;
907 yaz_log(YLOG_FATAL, "Failed to create association structure");
909 chan->next = pListener;
914 yaz_log(YLOG_FATAL, "Failed to create iochan");
919 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
924 * Set up a listening endpoint, and give it to the event-handler.
926 static int add_listener(char *where, int listen_id)
933 if (control_block.dynamic)
935 else if (control_block.threads)
940 yaz_log(log_server, "Adding %s listener on %s id=%d", mode, where,
943 l = cs_create_host(where, 2, &ap);
946 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
949 if (*control_block.cert_fname)
950 cs_set_ssl_certificate_file(l, control_block.cert_fname);
952 if (cs_bind(l, ap, CS_SERVER) < 0)
954 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
958 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
959 EVENT_EXCEPT, listen_id)))
961 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
965 iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
966 l->user = lst; /* user-defined data for COMSTACK is listener chan */
968 /* Add listener to chain */
969 lst->next = pListener;
975 /* UNIX only (for windows we don't need to catch the signals) */
976 static void catchchld(int num)
978 while (waitpid(-1, 0, WNOHANG) > 0)
980 signal(SIGCHLD, catchchld);
984 statserv_options_block *statserv_getcontrol(void)
986 #if YAZ_POSIX_THREADS
987 if (init_control_tls)
988 return pthread_getspecific(current_control_tls);
990 return &control_block;
992 if (current_control_block)
993 return current_control_block;
994 return &control_block;
998 void statserv_setcontrol(statserv_options_block *block)
1000 #if YAZ_POSIX_THREADS
1001 if (init_control_tls)
1002 pthread_setspecific(current_control_tls, block);
1004 current_control_block = block;
1008 static void statserv_reset(void)
1012 int statserv_start(int argc, char **argv)
1016 /* We need to initialize the thread list */
1017 ThreadList_Initialize();
1027 if ((me = strrchr (argv[0], sep)))
1028 me++; /* get the basename */
1031 programname = argv[0];
1033 if (control_block.options_func(argc, argv))
1036 #if YAZ_POSIX_THREADS
1037 init_control_tls = 1;
1038 pthread_key_create(¤t_control_tls, 0);
1043 xml_config_bend_start();
1046 xml_config_add_listeners();
1048 yaz_log (log_server, "Starting server %s", me);
1049 if (!pListener && *control_block.default_listen)
1050 add_listener(control_block.default_listen, 0);
1053 if (control_block.inetd)
1054 inetd_connection(control_block.default_proto);
1058 if (control_block.background)
1060 /* create pipe so that parent waits until child has created
1064 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
1078 int res = read(hand[0], dummy, 1);
1079 if (res < 0 && yaz_errno() != EINTR)
1081 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
1098 open("/dev/null", O_RDWR);
1101 xml_config_add_listeners();
1103 if (!pListener && *control_block.default_listen)
1104 add_listener(control_block.default_listen, 0);
1109 if (*control_block.pid_fname)
1111 FILE *f = fopen(control_block.pid_fname, "w");
1114 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s",
1115 control_block.pid_fname);
1118 fprintf(f, "%ld", (long) getpid());
1122 if (control_block.background)
1126 yaz_log (log_server, "Starting server %s pid=%ld", programname,
1129 sigset_t sigs_to_block;
1131 sigemptyset(&sigs_to_block);
1132 sigaddset (&sigs_to_block, SIGTERM);
1133 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
1136 if (control_block.dynamic)
1137 signal(SIGCHLD, catchchld);
1139 signal (SIGPIPE, SIG_IGN);
1140 signal (SIGTERM, sigterm);
1141 if (*control_block.setuid)
1145 if (!(pw = getpwnam(control_block.setuid)))
1147 yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
1150 if (setuid(pw->pw_uid) < 0)
1152 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
1158 if (pListener == NULL)
1160 yaz_log(YLOG_DEBUG, "Entering event loop.");
1161 return event_loop(&pListener);
1164 static void option_copy(char *dst, const char *src)
1166 strncpy(dst, src ? src : "", 127);
1170 int check_options(int argc, char **argv)
1175 /* set default log level */
1176 control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
1177 yaz_log_init_level(control_block.loglevel);
1179 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:",
1180 argv, argc, &arg)) != -2)
1185 if (add_listener(arg, 0))
1186 return 1; /* failed to create listener */
1189 control_block.one_shot = 1;
1190 control_block.dynamic = 0;
1193 control_block.default_proto = PROTO_Z3950;
1196 fprintf (stderr, "%s: SR protocol no longer supported\n", me);
1200 control_block.dynamic = 0;
1203 #if YAZ_POSIX_THREADS
1204 control_block.dynamic = 0;
1205 control_block.threads = 1;
1206 #elif YAZ_GNU_THREADS
1207 control_block.dynamic = 0;
1208 control_block.threads = 1;
1210 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1215 option_copy(control_block.logfile, arg);
1216 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1219 control_block.loglevel =
1220 yaz_log_mask_str_x(arg,control_block.loglevel);
1221 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1224 option_copy(control_block.apdufile, arg);
1227 option_copy(control_block.setuid, arg);
1230 option_copy(control_block.configname, arg);
1233 option_copy(control_block.cert_fname, arg);
1236 option_copy(control_block.daemon_name, arg);
1239 if (!arg || !(r = atoi(arg)))
1241 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1244 control_block.idle_timeout = r;
1247 if (!arg || !(r = atoi(arg)))
1249 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1252 control_block.maxrecordsize = r * 1024;
1255 control_block.inetd = 1;
1265 max_sessions = atoi(arg);
1268 option_copy(control_block.pid_fname, arg);
1272 option_copy(control_block.xml_config, arg);
1274 fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);
1279 control_block.background = 1;
1282 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1283 " -l <logfile> -u <user> -c <config> -t <minutes>"
1284 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
1285 " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
1294 typedef struct _Args
1300 static Args ArgDetails;
1302 /* name of the executable */
1303 #define SZAPPNAME "server"
1305 /* list of service dependencies - "dep1\0dep2\0\0" */
1306 #define SZDEPENDENCIES ""
1308 int statserv_main(int argc, char **argv,
1309 bend_initresult *(*bend_init)(bend_initrequest *r),
1310 void (*bend_close)(void *handle))
1312 struct statserv_options_block *cb = &control_block;
1313 cb->bend_init = bend_init;
1314 cb->bend_close = bend_close;
1316 /* Lets setup the Arg structure */
1317 ArgDetails.argc = argc;
1318 ArgDetails.argv = argv;
1320 /* Now setup the service with the service controller */
1321 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1322 cb->service_name, /* internal service name */
1323 cb->service_display_name, /* displayed name */
1328 int StartAppService(void *pHandle, int argc, char **argv)
1330 /* Initializes the App */
1334 void RunAppService(void *pHandle)
1336 Args *pArgs = (Args *)pHandle;
1338 /* Starts the app running */
1339 statserv_start(pArgs->argc, pArgs->argv);
1342 void StopAppService(void *pHandle)
1345 statserv_closedown();
1351 int statserv_main(int argc, char **argv,
1352 bend_initresult *(*bend_init)(bend_initrequest *r),
1353 void (*bend_close)(void *handle))
1357 control_block.bend_init = bend_init;
1358 control_block.bend_close = bend_close;
1360 ret = statserv_start (argc, argv);
1361 statserv_closedown ();