2 * Copyright (c) 1995-2004, Index Data
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.12 2004-11-18 15:18:13 heikki 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);
408 static int child = 0;
411 if (event == EVENT_INPUT)
413 if (control_block.dynamic && !child)
420 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
424 if ((res = fork()) < 0)
426 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
430 else if (res == 0) /* child */
437 for (pp = pListener; pp; pp = iochan_getnext(pp))
441 COMSTACK l = (COMSTACK)iochan_getdata(pp);
446 sprintf(nbuf, "%s(%d)", me, no_sessions);
447 yaz_log_init(control_block.loglevel, nbuf, 0);
448 /* ensure that bend_stop is not called when each child exits -
449 only for the main process .. */
450 control_block.bend_stop = 0;
455 /* wait for child to take the call */
461 if ((res = read(hand[0], dummy, 1)) < 0 &&
462 yaz_errno() != EINTR)
464 yaz_log(YLOG_FATAL|YLOG_ERRNO, "handshake read");
470 yaz_log(YLOG_DEBUG, "P: Child has taken the call");
475 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
476 control_block.daemon_name)) < 0)
478 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
483 yaz_log(YLOG_DEBUG, "listen ok");
484 iochan_setevent(h, EVENT_OUTPUT);
485 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
487 /* in dynamic mode, only the child ever comes down here */
488 else if (event == EVENT_OUTPUT)
490 COMSTACK new_line = cs_accept(line);
494 yaz_log(YLOG_FATAL, "Accept failed.");
495 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
498 yaz_log(YLOG_DEBUG, "accept ok");
499 if (control_block.dynamic)
502 /* close our half of the listener socket */
503 for (pp = pListener; pp; pp = iochan_getnext(pp))
505 COMSTACK l = (COMSTACK)iochan_getdata(pp);
510 yaz_log(YLOG_DEBUG, "Releasing parent");
515 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
518 #if YAZ_POSIX_THREADS
519 if (control_block.threads)
521 pthread_t child_thread;
522 pthread_create (&child_thread, 0, new_session, new_line);
523 pthread_detach (child_thread);
526 new_session(new_line);
527 #elif YAZ_GNU_THREADS
528 if (control_block.threads)
533 attr = pth_attr_new ();
534 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
535 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
536 pth_attr_set (attr, PTH_ATTR_NAME, "session");
537 yaz_log (YLOG_DEBUG, "pth_spawn begin");
538 child_thread = pth_spawn (attr, new_session, new_line);
539 yaz_log (YLOG_DEBUG, "pth_spawn finish");
540 pth_attr_destroy (attr);
543 new_session(new_line);
545 new_session(new_line);
548 else if (event == EVENT_TIMEOUT)
550 yaz_log(log_server, "Shutting down listener.");
555 yaz_log(YLOG_FATAL, "Bad event on listener.");
560 static void *new_session (void *vp)
565 COMSTACK new_line = (COMSTACK) vp;
567 unsigned cs_get_mask, cs_accept_mask, mask =
568 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
569 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
573 cs_accept_mask = mask; /* accept didn't complete */
578 cs_accept_mask = 0; /* accept completed. */
579 cs_get_mask = mask = EVENT_INPUT;
582 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
584 yaz_log(YLOG_FATAL, "Failed to create iochan");
587 if (!(newas = create_association(new_chan, new_line)))
589 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
592 newas->cs_accept_mask = cs_accept_mask;
593 newas->cs_get_mask = cs_get_mask;
595 iochan_setdata(new_chan, newas);
596 iochan_settimeout(new_chan, 60);
598 a = cs_addrstr(new_line);
602 yaz_log(log_session, "Starting session from %s (pid=%d)",
603 a ? a : "[Unknown]", getpid());
604 if (max_sessions && no_sessions == max_sessions)
605 control_block.one_shot = 1;
606 if (control_block.threads)
608 event_loop(&new_chan);
612 new_chan->next = pListener;
613 pListener = new_chan;
621 static void inetd_connection(int what)
628 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
630 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
632 if ((assoc = create_association(chan, line)))
634 iochan_setdata(chan, assoc);
635 iochan_settimeout(chan, 60);
636 addr = cs_addrstr(line);
637 yaz_log(log_session, "Inetd association from %s",
638 addr ? addr : "[UNKNOWN]");
639 assoc->cs_get_mask = EVENT_INPUT;
643 yaz_log(YLOG_FATAL, "Failed to create association structure");
645 chan->next = pListener;
650 yaz_log(YLOG_FATAL, "Failed to create iochan");
655 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
660 * Set up a listening endpoint, and give it to the event-handler.
662 static int add_listener(char *where, int what)
669 if (control_block.dynamic)
671 else if (control_block.threads)
676 yaz_log(log_server, "Adding %s %s listener on %s", mode,
677 what == PROTO_SR ? "SR" : "Z3950", where);
679 l = cs_create_host(where, 2, &ap);
682 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
685 if (*control_block.cert_fname)
686 cs_set_ssl_certificate_file(l, control_block.cert_fname);
688 if (cs_bind(l, ap, CS_SERVER) < 0)
690 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
694 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
697 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
701 iochan_setdata(lst, l);
703 /* Ensure our listener chain is setup properly */
704 lst->next = pListener;
710 /* UNIX only (for windows we don't need to catch the signals) */
711 static void catchchld(int num)
713 while (waitpid(-1, 0, WNOHANG) > 0)
715 signal(SIGCHLD, catchchld);
719 statserv_options_block *statserv_getcontrol(void)
721 static statserv_options_block cb;
723 memcpy(&cb, &control_block, sizeof(cb));
727 void statserv_setcontrol(statserv_options_block *block)
729 memcpy(&control_block, block, sizeof(*block));
732 static void statserv_reset(void)
736 int statserv_start(int argc, char **argv)
741 /* We need to initialize the thread list */
742 ThreadList_Initialize();
751 if ((me = strrchr (argv[0], sep)))
752 me++; /* get the basename */
757 if (control_block.options_func(argc, argv))
760 if (control_block.bend_start)
761 (*control_block.bend_start)(&control_block);
763 yaz_log (log_server, "Starting server %s", me);
764 if (!pListener && *control_block.default_listen)
765 add_listener(control_block.default_listen,
766 control_block.default_proto);
772 if (control_block.inetd)
773 inetd_connection(control_block.default_proto);
776 if (control_block.background)
794 open("/dev/null",O_RDWR);
797 if (!pListener && *control_block.default_listen)
798 add_listener(control_block.default_listen,
799 control_block.default_proto);
804 if (*control_block.pid_fname)
806 FILE *f = fopen(control_block.pid_fname, "w");
809 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s",
810 control_block.pid_fname);
813 fprintf(f, "%ld", (long) getpid());
817 yaz_log (log_server, "Starting server %s pid=%d", programname, getpid());
820 sigset_t sigs_to_block;
822 sigemptyset(&sigs_to_block);
823 sigaddset (&sigs_to_block, SIGTERM);
824 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
827 if (control_block.dynamic)
828 signal(SIGCHLD, catchchld);
830 signal (SIGPIPE, SIG_IGN);
831 signal (SIGTERM, sigterm);
832 if (*control_block.setuid)
836 if (!(pw = getpwnam(control_block.setuid)))
838 yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
841 if (setuid(pw->pw_uid) < 0)
843 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
849 if ((pListener == NULL) && *control_block.default_listen)
850 add_listener(control_block.default_listen,
851 control_block.default_proto);
853 if (pListener == NULL)
857 yaz_log(YLOG_DEBUG, "Entering event loop.");
858 ret = event_loop(&pListener);
863 int check_options(int argc, char **argv)
868 /* set default log level */
869 control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
870 yaz_log_init_level(control_block.loglevel);
872 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:",
873 argv, argc, &arg)) != -2)
878 if (add_listener(arg, control_block.default_proto))
879 return 1; /* failed to create listener */
882 control_block.one_shot = 1;
883 control_block.dynamic = 0;
886 control_block.default_proto = PROTO_Z3950;
889 fprintf (stderr, "%s: SR protocol no longer supported\n", me);
893 control_block.dynamic = 0;
896 #if YAZ_POSIX_THREADS
897 control_block.dynamic = 0;
898 control_block.threads = 1;
899 #elif YAZ_GNU_THREADS
900 control_block.dynamic = 0;
901 control_block.threads = 1;
903 fprintf(stderr, "%s: Threaded mode not available.\n", me);
908 strcpy(control_block.logfile, arg ? arg : "");
909 yaz_log_init(control_block.loglevel, me, control_block.logfile);
912 control_block.loglevel = yaz_log_mask_str_x(arg,control_block.loglevel);
913 yaz_log_init(control_block.loglevel, me, control_block.logfile);
916 strcpy(control_block.apdufile, arg ? arg : "");
919 strcpy(control_block.setuid, arg ? arg : "");
922 strcpy(control_block.configname, arg ? arg : "");
925 strcpy(control_block.cert_fname, arg ? arg : "");
928 strcpy(control_block.daemon_name, arg ? arg : "");
931 if (!arg || !(r = atoi(arg)))
933 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
936 control_block.idle_timeout = r;
939 if (!arg || !(r = atoi(arg)))
941 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
944 control_block.maxrecordsize = r * 1024;
947 control_block.inetd = 1;
957 max_sessions = atoi(arg);
960 if (strlen(arg) >= sizeof(control_block.pid_fname))
962 yaz_log(YLOG_FATAL, "pid fname too long");
965 strcpy(control_block.pid_fname, arg);
968 control_block.background = 1;
971 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
972 " -l <logfile> -u <user> -c <config> -t <minutes>"
973 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
974 " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
989 static Args ArgDetails;
991 /* name of the executable */
992 #define SZAPPNAME "server"
994 /* list of service dependencies - "dep1\0dep2\0\0" */
995 #define SZDEPENDENCIES ""
997 int statserv_main(int argc, char **argv,
998 bend_initresult *(*bend_init)(bend_initrequest *r),
999 void (*bend_close)(void *handle))
1001 statserv_options_block *cb = statserv_getcontrol();
1003 cb->bend_init = bend_init;
1004 cb->bend_close = bend_close;
1006 statserv_setcontrol(cb);
1008 /* Lets setup the Arg structure */
1009 ArgDetails.argc = argc;
1010 ArgDetails.argv = argv;
1012 /* Now setup the service with the service controller */
1013 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1014 cb->service_name, /* internal service name */
1015 cb->service_display_name, /* displayed name */
1020 int StartAppService(void *pHandle, int argc, char **argv)
1022 /* Initializes the App */
1026 void RunAppService(void *pHandle)
1028 Args *pArgs = (Args *)pHandle;
1030 /* Starts the app running */
1031 statserv_start(pArgs->argc, pArgs->argv);
1034 void StopAppService(void *pHandle)
1037 statserv_closedown();
1043 int statserv_main(int argc, char **argv,
1044 bend_initresult *(*bend_init)(bend_initrequest *r),
1045 void (*bend_close)(void *handle))
1048 statserv_options_block *cb = statserv_getcontrol();
1050 cb->bend_init = bend_init;
1051 cb->bend_close = bend_close;
1053 statserv_setcontrol(cb);
1054 ret = statserv_start (argc, argv);
1055 statserv_closedown ();