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.18 2005-01-15 19:47:14 adam Exp $
13 * \brief Implements GFS logic
38 #include <yaz/comstack.h>
39 #include <yaz/tcpip.h>
40 #include <yaz/options.h>
42 #include <yaz/xmosi.h>
47 #include <yaz/statserv.h>
49 static IOCHAN pListener = NULL;
51 static char *me = "statserver"; /* log prefix */
52 static char *programname="statserver"; /* full program name */
56 #define STAT_DEFAULT_LOG_LEVEL "none,fatal,warn,log,server,session,request"
57 /* the 'none' clears yaz' own default settings, including [log] */
59 int check_options(int argc, char **argv);
60 statserv_options_block control_block = {
62 0, /* threaded mode */
63 0, /* one shot (single session) */
64 YLOG_DEFAULT_LEVEL, /* log level */
66 "", /* diagnostic output to stderr */
67 "tcp:@:9999", /* default listener port */
68 PROTO_Z3950, /* default application protocol */
69 15, /* idle timeout (minutes) */
70 1024*1024, /* maximum PDU size (approx.) to allow */
71 "default-config", /* configuration name to pass to backend */
73 0, /* bend_start handler */
74 0, /* bend_stop handler */
75 check_options, /* Default routine, for checking the run-time arguments */
78 0, /* default value for inet deamon */
79 0, /* handle (for service, etc) */
80 0, /* bend_init handle */
81 0, /* bend_close handle */
83 "Z39.50 Server", /* NT Service Name */
84 "Server", /* NT application Name */
85 "", /* NT Service Dependencies */
86 "Z39.50 Server", /* NT Service Display Name */
88 0, /* SOAP handlers */
90 0, /* background daemon */
91 "" /* SSL certificate filename */
94 static int max_sessions = 0;
96 static int logbits_set=0;
97 static int log_session=0;
98 static int log_server=0;
100 /** get_logbits sets global loglevel bits */
101 static void get_logbits(int force)
102 { /* needs to be called after parsing cmd-line args that can set loglevels!*/
103 if (force || !logbits_set)
106 log_session=yaz_log_module_level("session");
107 log_server=yaz_log_module_level("server");
113 * handle incoming connect requests.
114 * The dynamic mode is a bit tricky mostly because we want to avoid
115 * doing all of the listening and accepting in the parent - it's
120 typedef struct _ThreadList ThreadList;
129 static ThreadList *pFirstThread;
130 static CRITICAL_SECTION Thread_CritSect;
131 static BOOL bInitialized = FALSE;
133 static void ThreadList_Initialize()
135 /* Initialize the critical Sections */
136 InitializeCriticalSection(&Thread_CritSect);
138 /* Set the first thraed */
141 /* we have been initialized */
145 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
147 /* Only one thread can go through this section at a time */
148 EnterCriticalSection(&Thread_CritSect);
151 /* Lets create our new object */
152 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
153 pNewThread->hThread = hThread;
154 pNewThread->pIOChannel = pIOChannel;
155 pNewThread->pNext = pFirstThread;
156 pFirstThread = pNewThread;
158 /* Lets let somebody else create a new object now */
159 LeaveCriticalSection(&Thread_CritSect);
163 void statserv_remove(IOCHAN pIOChannel)
165 /* Only one thread can go through this section at a time */
166 EnterCriticalSection(&Thread_CritSect);
169 ThreadList *pCurrentThread = pFirstThread;
170 ThreadList *pNextThread;
171 ThreadList *pPrevThread =NULL;
173 /* Step through alll the threads */
174 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
176 /* We only need to compare on the IO Channel */
177 if (pCurrentThread->pIOChannel == pIOChannel)
179 /* We have found the thread we want to delete */
180 /* First of all reset the next pointers */
181 if (pPrevThread == NULL)
182 pFirstThread = pCurrentThread->pNext;
184 pPrevThread->pNext = pCurrentThread->pNext;
186 /* All we need todo now is delete the memory */
187 free(pCurrentThread);
189 /* No need to look at any more threads */
194 /* We need to look at another thread */
195 pNextThread = pCurrentThread->pNext;
196 pPrevThread = pCurrentThread;
200 /* Lets let somebody else remove an object now */
201 LeaveCriticalSection(&Thread_CritSect);
205 /* WIN32 statserv_closedown */
206 void statserv_closedown()
208 /* Shouldn't do anything if we are not initialized */
212 HANDLE *pThreadHandles = NULL;
214 /* We need to stop threads adding and removing while we */
215 /* start the closedown process */
216 EnterCriticalSection(&Thread_CritSect);
219 /* We have exclusive access to the thread stuff now */
220 /* Y didn't i use a semaphore - Oh well never mind */
221 ThreadList *pCurrentThread = pFirstThread;
223 /* Before we do anything else, we need to shutdown the listener */
224 if (pListener != NULL)
225 iochan_destroy(pListener);
227 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
229 /* Just destroy the IOCHAN, that should do the trick */
230 iochan_destroy(pCurrentThread->pIOChannel);
231 closesocket(pCurrentThread->pIOChannel->fd);
233 /* Keep a running count of our handles */
239 HANDLE *pCurrentHandle ;
241 /* Allocate the thread handle array */
242 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
243 pCurrentHandle = pThreadHandles;
245 for (pCurrentThread = pFirstThread;
246 pCurrentThread != NULL;
247 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
249 /* Just the handle */
250 *pCurrentHandle = pCurrentThread->hThread;
254 /* We can now leave the critical section */
255 LeaveCriticalSection(&Thread_CritSect);
258 /* Now we can really do something */
261 logf (log_server, "waiting for %d to die", iHandles);
262 /* This will now wait, until all the threads close */
263 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
265 /* Free the memory we allocated for the handle array */
266 free(pThreadHandles);
269 if (control_block.bend_stop)
270 (*control_block.bend_stop)(&control_block);
271 /* No longer require the critical section, since all threads are dead */
272 DeleteCriticalSection(&Thread_CritSect);
276 void __cdecl event_loop_thread (IOCHAN iochan)
278 event_loop (&iochan);
282 static void listener(IOCHAN h, int event)
284 COMSTACK line = (COMSTACK) iochan_getdata(h);
289 if (event == EVENT_INPUT)
291 if ((res = cs_listen(line, 0, 0)) < 0)
293 yaz_log(YLOG_FATAL, "cs_listen failed");
298 yaz_log(YLOG_DEBUG, "listen ok");
299 iochan_setevent(h, EVENT_OUTPUT);
300 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
302 else if (event == EVENT_OUTPUT)
304 COMSTACK new_line = cs_accept(line);
310 yaz_log(YLOG_FATAL, "Accept failed.");
311 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
314 yaz_log(YLOG_DEBUG, "Accept ok");
316 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
319 yaz_log(YLOG_FATAL, "Failed to create iochan");
324 yaz_log(YLOG_DEBUG, "Creating association");
325 if (!(newas = create_association(new_chan, new_line)))
327 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
331 newas->cs_get_mask = EVENT_INPUT;
332 newas->cs_put_mask = 0;
333 newas->cs_accept_mask = 0;
335 yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
336 iochan_setdata(new_chan, newas);
337 iochan_settimeout(new_chan, 60);
339 /* Now what we need todo is create a new thread with this iochan as
341 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
342 if (newHandle == (HANDLE) -1)
345 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
349 /* We successfully created the thread, so add it to the list */
350 statserv_add(newHandle, new_chan);
352 yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
353 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
357 yaz_log(YLOG_FATAL, "Bad event on listener.");
363 int statserv_must_terminate(void)
370 static int term_flag = 0;
371 /* To save having an #ifdef in event_loop we need to
372 define this empty function
374 int statserv_must_terminate(void)
379 void statserv_remove(IOCHAN pIOChannel)
383 void statserv_closedown()
387 if (control_block.bend_stop)
388 (*control_block.bend_stop)(&control_block);
389 for (p = pListener; p; p = p->next)
395 void sigterm(int sig)
400 static void *new_session (void *vp);
401 static int no_sessions = 0;
404 static void listener(IOCHAN h, int event)
406 COMSTACK line = (COMSTACK) iochan_getdata(h);
409 if (event == EVENT_INPUT)
412 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
413 control_block.daemon_name)) < 0)
415 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
420 yaz_log(YLOG_WARN, "cs_listen incomplete");
423 new_line = cs_accept(line);
426 yaz_log(YLOG_FATAL, "Accept failed.");
427 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
431 if (control_block.dynamic)
433 if ((res = fork()) < 0)
435 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
439 else if (res == 0) /* child */
444 for (pp = pListener; pp; pp = iochan_getnext(pp))
446 COMSTACK l = (COMSTACK)iochan_getdata(pp);
450 sprintf(nbuf, "%s(%d)", me, no_sessions);
451 yaz_log_init(control_block.loglevel, nbuf, 0);
452 /* ensure that bend_stop is not called when each child exits -
453 only for the main process .. */
454 control_block.bend_stop = 0;
462 if (control_block.threads)
464 #if YAZ_POSIX_THREADS
465 pthread_t child_thread;
466 pthread_create (&child_thread, 0, new_session, new_line);
467 pthread_detach (child_thread);
468 #elif YAZ_GNU_THREADS
472 attr = pth_attr_new ();
473 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
474 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
475 pth_attr_set (attr, PTH_ATTR_NAME, "session");
476 yaz_log (YLOG_DEBUG, "pth_spawn begin");
477 child_thread = pth_spawn (attr, new_session, new_line);
478 yaz_log (YLOG_DEBUG, "pth_spawn finish");
479 pth_attr_destroy (attr);
481 new_session(new_line);
485 new_session(new_line);
487 else if (event == EVENT_TIMEOUT)
489 yaz_log(log_server, "Shutting down listener.");
494 yaz_log(YLOG_FATAL, "Bad event on listener.");
499 static void *new_session (void *vp)
504 COMSTACK new_line = (COMSTACK) vp;
506 unsigned cs_get_mask, cs_accept_mask, mask =
507 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
508 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
512 cs_accept_mask = mask; /* accept didn't complete */
517 cs_accept_mask = 0; /* accept completed. */
518 cs_get_mask = mask = EVENT_INPUT;
521 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
523 yaz_log(YLOG_FATAL, "Failed to create iochan");
526 if (!(newas = create_association(new_chan, new_line)))
528 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
531 newas->cs_accept_mask = cs_accept_mask;
532 newas->cs_get_mask = cs_get_mask;
534 iochan_setdata(new_chan, newas);
535 iochan_settimeout(new_chan, 60);
537 a = cs_addrstr(new_line);
541 yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
542 no_sessions, a ? a : "[Unknown]", (long) getpid());
543 if (max_sessions && no_sessions >= max_sessions)
544 control_block.one_shot = 1;
545 if (control_block.threads)
547 event_loop(&new_chan);
551 new_chan->next = pListener;
552 pListener = new_chan;
560 static void inetd_connection(int what)
567 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
569 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
571 if ((assoc = create_association(chan, line)))
573 iochan_setdata(chan, assoc);
574 iochan_settimeout(chan, 60);
575 addr = cs_addrstr(line);
576 yaz_log(log_session, "Inetd association from %s",
577 addr ? addr : "[UNKNOWN]");
578 assoc->cs_get_mask = EVENT_INPUT;
582 yaz_log(YLOG_FATAL, "Failed to create association structure");
584 chan->next = pListener;
589 yaz_log(YLOG_FATAL, "Failed to create iochan");
594 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
599 * Set up a listening endpoint, and give it to the event-handler.
601 static int add_listener(char *where, int what)
608 if (control_block.dynamic)
610 else if (control_block.threads)
615 yaz_log(log_server, "Adding %s %s listener on %s", mode,
616 what == PROTO_SR ? "SR" : "Z3950", where);
618 l = cs_create_host(where, 2, &ap);
621 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
624 if (*control_block.cert_fname)
625 cs_set_ssl_certificate_file(l, control_block.cert_fname);
627 if (cs_bind(l, ap, CS_SERVER) < 0)
629 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
633 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
636 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
640 iochan_setdata(lst, l);
642 /* Ensure our listener chain is setup properly */
643 lst->next = pListener;
649 /* UNIX only (for windows we don't need to catch the signals) */
650 static void catchchld(int num)
652 while (waitpid(-1, 0, WNOHANG) > 0)
654 signal(SIGCHLD, catchchld);
658 statserv_options_block *statserv_getcontrol(void)
660 static statserv_options_block cb;
662 memcpy(&cb, &control_block, sizeof(cb));
666 void statserv_setcontrol(statserv_options_block *block)
668 memcpy(&control_block, block, sizeof(*block));
671 static void statserv_reset(void)
675 int statserv_start(int argc, char **argv)
680 /* We need to initialize the thread list */
681 ThreadList_Initialize();
690 if ((me = strrchr (argv[0], sep)))
691 me++; /* get the basename */
696 if (control_block.options_func(argc, argv))
699 if (control_block.bend_start)
700 (*control_block.bend_start)(&control_block);
702 yaz_log (log_server, "Starting server %s", me);
703 if (!pListener && *control_block.default_listen)
704 add_listener(control_block.default_listen,
705 control_block.default_proto);
711 if (control_block.inetd)
712 inetd_connection(control_block.default_proto);
716 if (control_block.background)
718 /* create pipe so that parent waits until child has created
722 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
736 int res = read(hand[0], dummy, 1);
737 if (res < 0 && yaz_errno() != EINTR)
739 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
756 open("/dev/null", O_RDWR);
759 if (!pListener && *control_block.default_listen)
760 add_listener(control_block.default_listen,
761 control_block.default_proto);
766 if (*control_block.pid_fname)
768 FILE *f = fopen(control_block.pid_fname, "w");
771 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s",
772 control_block.pid_fname);
775 fprintf(f, "%ld", (long) getpid());
779 if (control_block.background)
782 yaz_log (log_server, "Starting server %s pid=%ld", programname,
786 sigset_t sigs_to_block;
788 sigemptyset(&sigs_to_block);
789 sigaddset (&sigs_to_block, SIGTERM);
790 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
793 if (control_block.dynamic)
794 signal(SIGCHLD, catchchld);
796 signal (SIGPIPE, SIG_IGN);
797 signal (SIGTERM, sigterm);
798 if (*control_block.setuid)
802 if (!(pw = getpwnam(control_block.setuid)))
804 yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
807 if (setuid(pw->pw_uid) < 0)
809 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
815 if ((pListener == NULL) && *control_block.default_listen)
816 add_listener(control_block.default_listen,
817 control_block.default_proto);
819 if (pListener == NULL)
823 yaz_log(YLOG_DEBUG, "Entering event loop.");
824 ret = event_loop(&pListener);
829 int check_options(int argc, char **argv)
834 /* set default log level */
835 control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
836 yaz_log_init_level(control_block.loglevel);
838 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:",
839 argv, argc, &arg)) != -2)
844 if (add_listener(arg, control_block.default_proto))
845 return 1; /* failed to create listener */
848 control_block.one_shot = 1;
849 control_block.dynamic = 0;
852 control_block.default_proto = PROTO_Z3950;
855 fprintf (stderr, "%s: SR protocol no longer supported\n", me);
859 control_block.dynamic = 0;
862 #if YAZ_POSIX_THREADS
863 control_block.dynamic = 0;
864 control_block.threads = 1;
865 #elif YAZ_GNU_THREADS
866 control_block.dynamic = 0;
867 control_block.threads = 1;
869 fprintf(stderr, "%s: Threaded mode not available.\n", me);
874 strcpy(control_block.logfile, arg ? arg : "");
875 yaz_log_init(control_block.loglevel, me, control_block.logfile);
878 control_block.loglevel = yaz_log_mask_str_x(arg,control_block.loglevel);
879 yaz_log_init(control_block.loglevel, me, control_block.logfile);
882 strcpy(control_block.apdufile, arg ? arg : "");
885 strcpy(control_block.setuid, arg ? arg : "");
888 strcpy(control_block.configname, arg ? arg : "");
891 strcpy(control_block.cert_fname, arg ? arg : "");
894 strcpy(control_block.daemon_name, arg ? arg : "");
897 if (!arg || !(r = atoi(arg)))
899 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
902 control_block.idle_timeout = r;
905 if (!arg || !(r = atoi(arg)))
907 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
910 control_block.maxrecordsize = r * 1024;
913 control_block.inetd = 1;
923 max_sessions = atoi(arg);
926 if (strlen(arg) >= sizeof(control_block.pid_fname))
928 yaz_log(YLOG_FATAL, "pid fname too long");
931 strcpy(control_block.pid_fname, arg);
934 control_block.background = 1;
937 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
938 " -l <logfile> -u <user> -c <config> -t <minutes>"
939 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
940 " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
955 static Args ArgDetails;
957 /* name of the executable */
958 #define SZAPPNAME "server"
960 /* list of service dependencies - "dep1\0dep2\0\0" */
961 #define SZDEPENDENCIES ""
963 int statserv_main(int argc, char **argv,
964 bend_initresult *(*bend_init)(bend_initrequest *r),
965 void (*bend_close)(void *handle))
967 statserv_options_block *cb = statserv_getcontrol();
969 cb->bend_init = bend_init;
970 cb->bend_close = bend_close;
972 statserv_setcontrol(cb);
974 /* Lets setup the Arg structure */
975 ArgDetails.argc = argc;
976 ArgDetails.argv = argv;
978 /* Now setup the service with the service controller */
979 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
980 cb->service_name, /* internal service name */
981 cb->service_display_name, /* displayed name */
986 int StartAppService(void *pHandle, int argc, char **argv)
988 /* Initializes the App */
992 void RunAppService(void *pHandle)
994 Args *pArgs = (Args *)pHandle;
996 /* Starts the app running */
997 statserv_start(pArgs->argc, pArgs->argv);
1000 void StopAppService(void *pHandle)
1003 statserv_closedown();
1009 int statserv_main(int argc, char **argv,
1010 bend_initresult *(*bend_init)(bend_initrequest *r),
1011 void (*bend_close)(void *handle))
1014 statserv_options_block *cb = statserv_getcontrol();
1016 cb->bend_init = bend_init;
1017 cb->bend_close = bend_close;
1019 statserv_setcontrol(cb);
1020 ret = statserv_start (argc, argv);
1021 statserv_closedown ();