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.20 2005-02-01 14:46: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);
600 if (event == EVENT_INPUT)
602 if ((res = cs_listen(line, 0, 0)) < 0)
604 yaz_log(YLOG_FATAL, "cs_listen failed");
609 yaz_log(YLOG_DEBUG, "listen ok");
610 iochan_setevent(h, EVENT_OUTPUT);
611 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
613 else if (event == EVENT_OUTPUT)
615 COMSTACK new_line = cs_accept(line);
621 yaz_log(YLOG_FATAL, "Accept failed.");
622 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
625 yaz_log(YLOG_DEBUG, "Accept ok");
627 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
630 yaz_log(YLOG_FATAL, "Failed to create iochan");
635 yaz_log(YLOG_DEBUG, "Creating association");
636 if (!(newas = create_association(new_chan, new_line,
637 control_block.apdu_file)))
639 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
643 newas->cs_get_mask = EVENT_INPUT;
644 newas->cs_put_mask = 0;
645 newas->cs_accept_mask = 0;
647 yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
648 iochan_setdata(new_chan, newas);
649 iochan_settimeout(new_chan, 60);
651 /* Now what we need todo is create a new thread with this iochan as
653 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
654 if (newHandle == (HANDLE) -1)
657 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
661 /* We successfully created the thread, so add it to the list */
662 statserv_add(newHandle, new_chan);
664 yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
665 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
669 yaz_log(YLOG_FATAL, "Bad event on listener.");
675 int statserv_must_terminate(void)
682 static int term_flag = 0;
683 /* To save having an #ifdef in event_loop we need to
684 define this empty function
686 int statserv_must_terminate(void)
691 void statserv_remove(IOCHAN pIOChannel)
695 void statserv_closedown()
699 xml_config_bend_stop();
700 for (p = pListener; p; p = p->next)
705 #if YAZ_POSIX_THREADS
706 pthread_key_delete(current_control_tls);
710 void sigterm(int sig)
715 static void *new_session (void *vp);
716 static int no_sessions = 0;
719 static void listener(IOCHAN h, int event)
721 COMSTACK line = (COMSTACK) iochan_getdata(h);
724 if (event == EVENT_INPUT)
727 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
728 control_block.daemon_name)) < 0)
730 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
735 yaz_log(YLOG_WARN, "cs_listen incomplete");
738 new_line = cs_accept(line);
741 yaz_log(YLOG_FATAL, "Accept failed.");
742 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
746 yaz_log(log_session, "Connect from %s", cs_addrstr(new_line));
749 if (control_block.dynamic)
751 if ((res = fork()) < 0)
753 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
757 else if (res == 0) /* child */
762 for (pp = pListener; pp; pp = iochan_getnext(pp))
764 COMSTACK l = (COMSTACK)iochan_getdata(pp);
768 sprintf(nbuf, "%s(%d)", me, no_sessions);
769 yaz_log_init(control_block.loglevel, nbuf, 0);
770 /* ensure that bend_stop is not called when each child exits -
771 only for the main process .. */
772 control_block.bend_stop = 0;
781 if (control_block.threads)
783 #if YAZ_POSIX_THREADS
784 pthread_t child_thread;
785 pthread_create (&child_thread, 0, new_session, new_line);
786 pthread_detach (child_thread);
787 #elif YAZ_GNU_THREADS
791 attr = pth_attr_new ();
792 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
793 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
794 pth_attr_set (attr, PTH_ATTR_NAME, "session");
795 yaz_log (YLOG_DEBUG, "pth_spawn begin");
796 child_thread = pth_spawn (attr, new_session, new_line);
797 yaz_log (YLOG_DEBUG, "pth_spawn finish");
798 pth_attr_destroy (attr);
800 new_session(new_line);
804 new_session(new_line);
806 else if (event == EVENT_TIMEOUT)
808 yaz_log(log_server, "Shutting down listener.");
813 yaz_log(YLOG_FATAL, "Bad event on listener.");
818 static void *new_session (void *vp)
823 COMSTACK new_line = (COMSTACK) vp;
824 IOCHAN parent_chan = new_line->user;
826 unsigned cs_get_mask, cs_accept_mask, mask =
827 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
828 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
832 cs_accept_mask = mask; /* accept didn't complete */
837 cs_accept_mask = 0; /* accept completed. */
838 cs_get_mask = mask = EVENT_INPUT;
841 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask,
844 yaz_log(YLOG_FATAL, "Failed to create iochan");
847 if (!(newas = create_association(new_chan, new_line,
848 control_block.apdufile)))
850 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
853 newas->cs_accept_mask = cs_accept_mask;
854 newas->cs_get_mask = cs_get_mask;
856 iochan_setdata(new_chan, newas);
857 iochan_settimeout(new_chan, 60);
859 a = cs_addrstr(new_line);
863 yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
864 no_sessions, a ? a : "[Unknown]", (long) getpid());
865 if (max_sessions && no_sessions >= max_sessions)
866 control_block.one_shot = 1;
867 if (control_block.threads)
869 event_loop(&new_chan);
873 new_chan->next = pListener;
874 pListener = new_chan;
882 static void inetd_connection(int what)
889 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
891 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT,
894 if ((assoc = create_association(chan, line,
895 control_block.apdufile)))
897 iochan_setdata(chan, assoc);
898 iochan_settimeout(chan, 60);
899 addr = cs_addrstr(line);
900 yaz_log(log_session, "Inetd association from %s",
901 addr ? addr : "[UNKNOWN]");
902 assoc->cs_get_mask = EVENT_INPUT;
906 yaz_log(YLOG_FATAL, "Failed to create association structure");
908 chan->next = pListener;
913 yaz_log(YLOG_FATAL, "Failed to create iochan");
918 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
923 * Set up a listening endpoint, and give it to the event-handler.
925 static int add_listener(char *where, int listen_id)
932 if (control_block.dynamic)
934 else if (control_block.threads)
939 yaz_log(log_server, "Adding %s listener on %s id=%d", mode, where,
942 l = cs_create_host(where, 2, &ap);
945 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
948 if (*control_block.cert_fname)
949 cs_set_ssl_certificate_file(l, control_block.cert_fname);
951 if (cs_bind(l, ap, CS_SERVER) < 0)
953 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
957 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
958 EVENT_EXCEPT, listen_id)))
960 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
964 iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
965 l->user = lst; /* user-defined data for COMSTACK is listener chan */
967 /* Add listener to chain */
968 lst->next = pListener;
974 /* UNIX only (for windows we don't need to catch the signals) */
975 static void catchchld(int num)
977 while (waitpid(-1, 0, WNOHANG) > 0)
979 signal(SIGCHLD, catchchld);
983 statserv_options_block *statserv_getcontrol(void)
985 #if YAZ_POSIX_THREADS
986 if (init_control_tls)
987 return pthread_getspecific(current_control_tls);
989 return &control_block;
991 return current_control_block;
995 void statserv_setcontrol(statserv_options_block *block)
997 #if YAZ_POSIX_THREADS
998 if (init_control_tls)
999 pthread_setspecific(current_control_tls, block);
1001 current_control_block = block;
1005 static void statserv_reset(void)
1009 int statserv_start(int argc, char **argv)
1013 /* We need to initialize the thread list */
1014 ThreadList_Initialize();
1024 if ((me = strrchr (argv[0], sep)))
1025 me++; /* get the basename */
1028 programname = argv[0];
1030 if (control_block.options_func(argc, argv))
1033 #if YAZ_POSIX_THREADS
1034 init_control_tls = 1;
1035 pthread_key_create(¤t_control_tls, 0);
1040 xml_config_bend_start();
1043 xml_config_add_listeners();
1045 yaz_log (log_server, "Starting server %s", me);
1046 if (!pListener && *control_block.default_listen)
1047 add_listener(control_block.default_listen, 0);
1050 if (control_block.inetd)
1051 inetd_connection(control_block.default_proto);
1055 if (control_block.background)
1057 /* create pipe so that parent waits until child has created
1061 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
1075 int res = read(hand[0], dummy, 1);
1076 if (res < 0 && yaz_errno() != EINTR)
1078 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
1095 open("/dev/null", O_RDWR);
1098 xml_config_add_listeners();
1100 if (!pListener && *control_block.default_listen)
1101 add_listener(control_block.default_listen, 0);
1106 if (*control_block.pid_fname)
1108 FILE *f = fopen(control_block.pid_fname, "w");
1111 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s",
1112 control_block.pid_fname);
1115 fprintf(f, "%ld", (long) getpid());
1119 if (control_block.background)
1123 yaz_log (log_server, "Starting server %s pid=%ld", programname,
1126 sigset_t sigs_to_block;
1128 sigemptyset(&sigs_to_block);
1129 sigaddset (&sigs_to_block, SIGTERM);
1130 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
1133 if (control_block.dynamic)
1134 signal(SIGCHLD, catchchld);
1136 signal (SIGPIPE, SIG_IGN);
1137 signal (SIGTERM, sigterm);
1138 if (*control_block.setuid)
1142 if (!(pw = getpwnam(control_block.setuid)))
1144 yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
1147 if (setuid(pw->pw_uid) < 0)
1149 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
1155 if (pListener == NULL)
1157 yaz_log(YLOG_DEBUG, "Entering event loop.");
1158 return event_loop(&pListener);
1161 static void option_copy(char *dst, const char *src)
1163 strncpy(dst, src ? src : "", 127);
1167 int check_options(int argc, char **argv)
1172 /* set default log level */
1173 control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
1174 yaz_log_init_level(control_block.loglevel);
1176 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:",
1177 argv, argc, &arg)) != -2)
1182 if (add_listener(arg, 0))
1183 return 1; /* failed to create listener */
1186 control_block.one_shot = 1;
1187 control_block.dynamic = 0;
1190 control_block.default_proto = PROTO_Z3950;
1193 fprintf (stderr, "%s: SR protocol no longer supported\n", me);
1197 control_block.dynamic = 0;
1200 #if YAZ_POSIX_THREADS
1201 control_block.dynamic = 0;
1202 control_block.threads = 1;
1203 #elif YAZ_GNU_THREADS
1204 control_block.dynamic = 0;
1205 control_block.threads = 1;
1207 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1212 option_copy(control_block.logfile, arg);
1213 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1216 control_block.loglevel =
1217 yaz_log_mask_str_x(arg,control_block.loglevel);
1218 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1221 option_copy(control_block.apdufile, arg);
1224 option_copy(control_block.setuid, arg);
1227 option_copy(control_block.configname, arg);
1230 option_copy(control_block.cert_fname, arg);
1233 option_copy(control_block.daemon_name, arg);
1236 if (!arg || !(r = atoi(arg)))
1238 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1241 control_block.idle_timeout = r;
1244 if (!arg || !(r = atoi(arg)))
1246 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1249 control_block.maxrecordsize = r * 1024;
1252 control_block.inetd = 1;
1262 max_sessions = atoi(arg);
1265 option_copy(control_block.pid_fname, arg);
1269 option_copy(control_block.xml_config, arg);
1271 fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);
1276 control_block.background = 1;
1279 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1280 " -l <logfile> -u <user> -c <config> -t <minutes>"
1281 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
1282 " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
1291 typedef struct _Args
1297 static Args ArgDetails;
1299 /* name of the executable */
1300 #define SZAPPNAME "server"
1302 /* list of service dependencies - "dep1\0dep2\0\0" */
1303 #define SZDEPENDENCIES ""
1305 int statserv_main(int argc, char **argv,
1306 bend_initresult *(*bend_init)(bend_initrequest *r),
1307 void (*bend_close)(void *handle))
1309 control_block.bend_init = bend_init;
1310 control_block.bend_close = bend_close;
1312 /* Lets setup the Arg structure */
1313 ArgDetails.argc = argc;
1314 ArgDetails.argv = argv;
1316 /* Now setup the service with the service controller */
1317 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1318 cb->service_name, /* internal service name */
1319 cb->service_display_name, /* displayed name */
1324 int StartAppService(void *pHandle, int argc, char **argv)
1326 /* Initializes the App */
1330 void RunAppService(void *pHandle)
1332 Args *pArgs = (Args *)pHandle;
1334 /* Starts the app running */
1335 statserv_start(pArgs->argc, pArgs->argv);
1338 void StopAppService(void *pHandle)
1341 statserv_closedown();
1347 int statserv_main(int argc, char **argv,
1348 bend_initresult *(*bend_init)(bend_initrequest *r),
1349 void (*bend_close)(void *handle))
1353 control_block.bend_init = bend_init;
1354 control_block.bend_close = bend_close;
1356 ret = statserv_start (argc, argv);
1357 statserv_closedown ();