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.19 2005-01-16 21:51:50 adam Exp $
13 * \brief Implements GFS logic
25 #include <sys/types.h>
47 #include <yaz/comstack.h>
48 #include <yaz/tcpip.h>
49 #include <yaz/options.h>
51 #include <yaz/xmosi.h>
56 #include <yaz/statserv.h>
58 static IOCHAN pListener = NULL;
60 static char *me = "statserver"; /* log prefix */
61 static char *programname="statserver"; /* full program name */
65 #define STAT_DEFAULT_LOG_LEVEL "none,fatal,warn,log,server,session,request"
66 /* the 'none' clears yaz' own default settings, including [log] */
68 int check_options(int argc, char **argv);
69 statserv_options_block control_block = {
71 0, /* threaded mode */
72 0, /* one shot (single session) */
73 YLOG_DEFAULT_LEVEL, /* log level */
75 "", /* diagnostic output to stderr */
76 "tcp:@:9999", /* default listener port */
77 PROTO_Z3950, /* default application protocol */
78 15, /* idle timeout (minutes) */
79 1024*1024, /* maximum PDU size (approx.) to allow */
80 "default-config", /* configuration name to pass to backend */
82 0, /* bend_start handler */
83 0, /* bend_stop handler */
84 check_options, /* Default routine, for checking the run-time arguments */
87 0, /* default value for inet deamon */
88 0, /* handle (for service, etc) */
89 0, /* bend_init handle */
90 0, /* bend_close handle */
92 "Z39.50 Server", /* NT Service Name */
93 "Server", /* NT application Name */
94 "", /* NT Service Dependencies */
95 "Z39.50 Server", /* NT Service Display Name */
97 0, /* SOAP handlers */
99 0, /* background daemon */
100 "" /* SSL certificate filename */
103 static int max_sessions = 0;
105 static int logbits_set = 0;
106 static int log_session = 0;
107 static int log_server = 0;
109 /** get_logbits sets global loglevel bits */
110 static void get_logbits(int force)
111 { /* needs to be called after parsing cmd-line args that can set loglevels!*/
112 if (force || !logbits_set)
115 log_session = yaz_log_module_level("session");
116 log_server = yaz_log_module_level("server");
122 * handle incoming connect requests.
123 * The dynamic mode is a bit tricky mostly because we want to avoid
124 * doing all of the listening and accepting in the parent - it's
129 typedef struct _ThreadList ThreadList;
138 static ThreadList *pFirstThread;
139 static CRITICAL_SECTION Thread_CritSect;
140 static BOOL bInitialized = FALSE;
142 static void ThreadList_Initialize()
144 /* Initialize the critical Sections */
145 InitializeCriticalSection(&Thread_CritSect);
147 /* Set the first thraed */
150 /* we have been initialized */
154 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
156 /* Only one thread can go through this section at a time */
157 EnterCriticalSection(&Thread_CritSect);
160 /* Lets create our new object */
161 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
162 pNewThread->hThread = hThread;
163 pNewThread->pIOChannel = pIOChannel;
164 pNewThread->pNext = pFirstThread;
165 pFirstThread = pNewThread;
167 /* Lets let somebody else create a new object now */
168 LeaveCriticalSection(&Thread_CritSect);
172 void statserv_remove(IOCHAN pIOChannel)
174 /* Only one thread can go through this section at a time */
175 EnterCriticalSection(&Thread_CritSect);
178 ThreadList *pCurrentThread = pFirstThread;
179 ThreadList *pNextThread;
180 ThreadList *pPrevThread =NULL;
182 /* Step through alll the threads */
183 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
185 /* We only need to compare on the IO Channel */
186 if (pCurrentThread->pIOChannel == pIOChannel)
188 /* We have found the thread we want to delete */
189 /* First of all reset the next pointers */
190 if (pPrevThread == NULL)
191 pFirstThread = pCurrentThread->pNext;
193 pPrevThread->pNext = pCurrentThread->pNext;
195 /* All we need todo now is delete the memory */
196 free(pCurrentThread);
198 /* No need to look at any more threads */
203 /* We need to look at another thread */
204 pNextThread = pCurrentThread->pNext;
205 pPrevThread = pCurrentThread;
209 /* Lets let somebody else remove an object now */
210 LeaveCriticalSection(&Thread_CritSect);
214 /* WIN32 statserv_closedown */
215 void statserv_closedown()
217 /* Shouldn't do anything if we are not initialized */
221 HANDLE *pThreadHandles = NULL;
223 /* We need to stop threads adding and removing while we */
224 /* start the closedown process */
225 EnterCriticalSection(&Thread_CritSect);
228 /* We have exclusive access to the thread stuff now */
229 /* Y didn't i use a semaphore - Oh well never mind */
230 ThreadList *pCurrentThread = pFirstThread;
232 /* Before we do anything else, we need to shutdown the listener */
233 if (pListener != NULL)
234 iochan_destroy(pListener);
236 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
238 /* Just destroy the IOCHAN, that should do the trick */
239 iochan_destroy(pCurrentThread->pIOChannel);
240 closesocket(pCurrentThread->pIOChannel->fd);
242 /* Keep a running count of our handles */
248 HANDLE *pCurrentHandle ;
250 /* Allocate the thread handle array */
251 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
252 pCurrentHandle = pThreadHandles;
254 for (pCurrentThread = pFirstThread;
255 pCurrentThread != NULL;
256 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
258 /* Just the handle */
259 *pCurrentHandle = pCurrentThread->hThread;
263 /* We can now leave the critical section */
264 LeaveCriticalSection(&Thread_CritSect);
267 /* Now we can really do something */
270 logf (log_server, "waiting for %d to die", iHandles);
271 /* This will now wait, until all the threads close */
272 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
274 /* Free the memory we allocated for the handle array */
275 free(pThreadHandles);
278 if (control_block.bend_stop)
279 (*control_block.bend_stop)(&control_block);
280 /* No longer require the critical section, since all threads are dead */
281 DeleteCriticalSection(&Thread_CritSect);
285 void __cdecl event_loop_thread (IOCHAN iochan)
287 event_loop (&iochan);
291 static void listener(IOCHAN h, int event)
293 COMSTACK line = (COMSTACK) iochan_getdata(h);
298 if (event == EVENT_INPUT)
300 if ((res = cs_listen(line, 0, 0)) < 0)
302 yaz_log(YLOG_FATAL, "cs_listen failed");
307 yaz_log(YLOG_DEBUG, "listen ok");
308 iochan_setevent(h, EVENT_OUTPUT);
309 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
311 else if (event == EVENT_OUTPUT)
313 COMSTACK new_line = cs_accept(line);
319 yaz_log(YLOG_FATAL, "Accept failed.");
320 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
323 yaz_log(YLOG_DEBUG, "Accept ok");
325 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
328 yaz_log(YLOG_FATAL, "Failed to create iochan");
333 yaz_log(YLOG_DEBUG, "Creating association");
334 if (!(newas = create_association(new_chan, new_line)))
336 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
340 newas->cs_get_mask = EVENT_INPUT;
341 newas->cs_put_mask = 0;
342 newas->cs_accept_mask = 0;
344 yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
345 iochan_setdata(new_chan, newas);
346 iochan_settimeout(new_chan, 60);
348 /* Now what we need todo is create a new thread with this iochan as
350 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
351 if (newHandle == (HANDLE) -1)
354 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
358 /* We successfully created the thread, so add it to the list */
359 statserv_add(newHandle, new_chan);
361 yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
362 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
366 yaz_log(YLOG_FATAL, "Bad event on listener.");
372 int statserv_must_terminate(void)
379 static int term_flag = 0;
380 /* To save having an #ifdef in event_loop we need to
381 define this empty function
383 int statserv_must_terminate(void)
388 void statserv_remove(IOCHAN pIOChannel)
392 void statserv_closedown()
396 if (control_block.bend_stop)
397 (*control_block.bend_stop)(&control_block);
398 for (p = pListener; p; p = p->next)
404 void sigterm(int sig)
409 static void *new_session (void *vp);
410 static int no_sessions = 0;
413 static void listener(IOCHAN h, int event)
415 COMSTACK line = (COMSTACK) iochan_getdata(h);
418 if (event == EVENT_INPUT)
421 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
422 control_block.daemon_name)) < 0)
424 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
429 yaz_log(YLOG_WARN, "cs_listen incomplete");
432 new_line = cs_accept(line);
435 yaz_log(YLOG_FATAL, "Accept failed.");
436 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
440 if (control_block.dynamic)
442 if ((res = fork()) < 0)
444 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
448 else if (res == 0) /* child */
453 for (pp = pListener; pp; pp = iochan_getnext(pp))
455 COMSTACK l = (COMSTACK)iochan_getdata(pp);
459 sprintf(nbuf, "%s(%d)", me, no_sessions);
460 yaz_log_init(control_block.loglevel, nbuf, 0);
461 /* ensure that bend_stop is not called when each child exits -
462 only for the main process .. */
463 control_block.bend_stop = 0;
471 if (control_block.threads)
473 #if YAZ_POSIX_THREADS
474 pthread_t child_thread;
475 pthread_create (&child_thread, 0, new_session, new_line);
476 pthread_detach (child_thread);
477 #elif YAZ_GNU_THREADS
481 attr = pth_attr_new ();
482 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
483 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
484 pth_attr_set (attr, PTH_ATTR_NAME, "session");
485 yaz_log (YLOG_DEBUG, "pth_spawn begin");
486 child_thread = pth_spawn (attr, new_session, new_line);
487 yaz_log (YLOG_DEBUG, "pth_spawn finish");
488 pth_attr_destroy (attr);
490 new_session(new_line);
494 new_session(new_line);
496 else if (event == EVENT_TIMEOUT)
498 yaz_log(log_server, "Shutting down listener.");
503 yaz_log(YLOG_FATAL, "Bad event on listener.");
508 static void *new_session (void *vp)
513 COMSTACK new_line = (COMSTACK) vp;
515 unsigned cs_get_mask, cs_accept_mask, mask =
516 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
517 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
521 cs_accept_mask = mask; /* accept didn't complete */
526 cs_accept_mask = 0; /* accept completed. */
527 cs_get_mask = mask = EVENT_INPUT;
530 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
532 yaz_log(YLOG_FATAL, "Failed to create iochan");
535 if (!(newas = create_association(new_chan, new_line)))
537 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
540 newas->cs_accept_mask = cs_accept_mask;
541 newas->cs_get_mask = cs_get_mask;
543 iochan_setdata(new_chan, newas);
544 iochan_settimeout(new_chan, 60);
546 a = cs_addrstr(new_line);
550 yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
551 no_sessions, a ? a : "[Unknown]", (long) getpid());
552 if (max_sessions && no_sessions >= max_sessions)
553 control_block.one_shot = 1;
554 if (control_block.threads)
556 event_loop(&new_chan);
560 new_chan->next = pListener;
561 pListener = new_chan;
569 static void inetd_connection(int what)
576 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
578 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
580 if ((assoc = create_association(chan, line)))
582 iochan_setdata(chan, assoc);
583 iochan_settimeout(chan, 60);
584 addr = cs_addrstr(line);
585 yaz_log(log_session, "Inetd association from %s",
586 addr ? addr : "[UNKNOWN]");
587 assoc->cs_get_mask = EVENT_INPUT;
591 yaz_log(YLOG_FATAL, "Failed to create association structure");
593 chan->next = pListener;
598 yaz_log(YLOG_FATAL, "Failed to create iochan");
603 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
608 * Set up a listening endpoint, and give it to the event-handler.
610 static int add_listener(char *where, int what)
617 if (control_block.dynamic)
619 else if (control_block.threads)
624 yaz_log(log_server, "Adding %s %s listener on %s", mode,
625 what == PROTO_SR ? "SR" : "Z3950", where);
627 l = cs_create_host(where, 2, &ap);
630 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
633 if (*control_block.cert_fname)
634 cs_set_ssl_certificate_file(l, control_block.cert_fname);
636 if (cs_bind(l, ap, CS_SERVER) < 0)
638 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
642 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
645 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
649 iochan_setdata(lst, l);
651 /* Ensure our listener chain is setup properly */
652 lst->next = pListener;
658 /* UNIX only (for windows we don't need to catch the signals) */
659 static void catchchld(int num)
661 while (waitpid(-1, 0, WNOHANG) > 0)
663 signal(SIGCHLD, catchchld);
667 statserv_options_block *statserv_getcontrol(void)
669 static statserv_options_block cb;
671 memcpy(&cb, &control_block, sizeof(cb));
675 void statserv_setcontrol(statserv_options_block *block)
677 memcpy(&control_block, block, sizeof(*block));
680 static void statserv_reset(void)
684 int statserv_start(int argc, char **argv)
689 /* We need to initialize the thread list */
690 ThreadList_Initialize();
699 if ((me = strrchr (argv[0], sep)))
700 me++; /* get the basename */
703 programname = argv[0];
705 if (control_block.options_func(argc, argv))
708 if (control_block.bend_start)
709 (*control_block.bend_start)(&control_block);
711 yaz_log (log_server, "Starting server %s", me);
712 if (!pListener && *control_block.default_listen)
713 add_listener(control_block.default_listen,
714 control_block.default_proto);
720 if (control_block.inetd)
721 inetd_connection(control_block.default_proto);
725 if (control_block.background)
727 /* create pipe so that parent waits until child has created
731 yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
745 int res = read(hand[0], dummy, 1);
746 if (res < 0 && yaz_errno() != EINTR)
748 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
765 open("/dev/null", O_RDWR);
768 if (!pListener && *control_block.default_listen)
769 add_listener(control_block.default_listen,
770 control_block.default_proto);
775 if (*control_block.pid_fname)
777 FILE *f = fopen(control_block.pid_fname, "w");
780 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s",
781 control_block.pid_fname);
784 fprintf(f, "%ld", (long) getpid());
788 if (control_block.background)
791 yaz_log (log_server, "Starting server %s pid=%ld", programname,
795 sigset_t sigs_to_block;
797 sigemptyset(&sigs_to_block);
798 sigaddset (&sigs_to_block, SIGTERM);
799 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
802 if (control_block.dynamic)
803 signal(SIGCHLD, catchchld);
805 signal (SIGPIPE, SIG_IGN);
806 signal (SIGTERM, sigterm);
807 if (*control_block.setuid)
811 if (!(pw = getpwnam(control_block.setuid)))
813 yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
816 if (setuid(pw->pw_uid) < 0)
818 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
824 if ((pListener == NULL) && *control_block.default_listen)
825 add_listener(control_block.default_listen,
826 control_block.default_proto);
828 if (pListener == NULL)
832 yaz_log(YLOG_DEBUG, "Entering event loop.");
833 ret = event_loop(&pListener);
838 int check_options(int argc, char **argv)
843 /* set default log level */
844 control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
845 yaz_log_init_level(control_block.loglevel);
847 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:",
848 argv, argc, &arg)) != -2)
853 if (add_listener(arg, control_block.default_proto))
854 return 1; /* failed to create listener */
857 control_block.one_shot = 1;
858 control_block.dynamic = 0;
861 control_block.default_proto = PROTO_Z3950;
864 fprintf (stderr, "%s: SR protocol no longer supported\n", me);
868 control_block.dynamic = 0;
871 #if YAZ_POSIX_THREADS
872 control_block.dynamic = 0;
873 control_block.threads = 1;
874 #elif YAZ_GNU_THREADS
875 control_block.dynamic = 0;
876 control_block.threads = 1;
878 fprintf(stderr, "%s: Threaded mode not available.\n", me);
883 strcpy(control_block.logfile, arg ? arg : "");
884 yaz_log_init(control_block.loglevel, me, control_block.logfile);
887 control_block.loglevel = yaz_log_mask_str_x(arg,control_block.loglevel);
888 yaz_log_init(control_block.loglevel, me, control_block.logfile);
891 strcpy(control_block.apdufile, arg ? arg : "");
894 strcpy(control_block.setuid, arg ? arg : "");
897 strcpy(control_block.configname, arg ? arg : "");
900 strcpy(control_block.cert_fname, arg ? arg : "");
903 strcpy(control_block.daemon_name, arg ? arg : "");
906 if (!arg || !(r = atoi(arg)))
908 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
911 control_block.idle_timeout = r;
914 if (!arg || !(r = atoi(arg)))
916 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
919 control_block.maxrecordsize = r * 1024;
922 control_block.inetd = 1;
932 max_sessions = atoi(arg);
935 if (strlen(arg) >= sizeof(control_block.pid_fname))
937 yaz_log(YLOG_FATAL, "pid fname too long");
940 strcpy(control_block.pid_fname, arg);
943 control_block.background = 1;
946 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
947 " -l <logfile> -u <user> -c <config> -t <minutes>"
948 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
949 " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
964 static Args ArgDetails;
966 /* name of the executable */
967 #define SZAPPNAME "server"
969 /* list of service dependencies - "dep1\0dep2\0\0" */
970 #define SZDEPENDENCIES ""
972 int statserv_main(int argc, char **argv,
973 bend_initresult *(*bend_init)(bend_initrequest *r),
974 void (*bend_close)(void *handle))
976 statserv_options_block *cb = statserv_getcontrol();
978 cb->bend_init = bend_init;
979 cb->bend_close = bend_close;
981 statserv_setcontrol(cb);
983 /* Lets setup the Arg structure */
984 ArgDetails.argc = argc;
985 ArgDetails.argv = argv;
987 /* Now setup the service with the service controller */
988 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
989 cb->service_name, /* internal service name */
990 cb->service_display_name, /* displayed name */
995 int StartAppService(void *pHandle, int argc, char **argv)
997 /* Initializes the App */
1001 void RunAppService(void *pHandle)
1003 Args *pArgs = (Args *)pHandle;
1005 /* Starts the app running */
1006 statserv_start(pArgs->argc, pArgs->argv);
1009 void StopAppService(void *pHandle)
1012 statserv_closedown();
1018 int statserv_main(int argc, char **argv,
1019 bend_initresult *(*bend_init)(bend_initrequest *r),
1020 void (*bend_close)(void *handle))
1023 statserv_options_block *cb = statserv_getcontrol();
1025 cb->bend_init = bend_init;
1026 cb->bend_close = bend_close;
1028 statserv_setcontrol(cb);
1029 ret = statserv_start (argc, argv);
1030 statserv_closedown ();