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.14 2004-12-22 23:47:11 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 new_session(new_line);
469 #elif YAZ_GNU_THREADS
473 attr = pth_attr_new ();
474 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
475 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
476 pth_attr_set (attr, PTH_ATTR_NAME, "session");
477 yaz_log (YLOG_DEBUG, "pth_spawn begin");
478 child_thread = pth_spawn (attr, new_session, new_line);
479 yaz_log (YLOG_DEBUG, "pth_spawn finish");
480 pth_attr_destroy (attr);
482 new_session(new_line);
486 new_session(new_line);
488 else if (event == EVENT_TIMEOUT)
490 yaz_log(log_server, "Shutting down listener.");
495 yaz_log(YLOG_FATAL, "Bad event on listener.");
500 static void *new_session (void *vp)
505 COMSTACK new_line = (COMSTACK) vp;
507 unsigned cs_get_mask, cs_accept_mask, mask =
508 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
509 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
513 cs_accept_mask = mask; /* accept didn't complete */
518 cs_accept_mask = 0; /* accept completed. */
519 cs_get_mask = mask = EVENT_INPUT;
522 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
524 yaz_log(YLOG_FATAL, "Failed to create iochan");
527 if (!(newas = create_association(new_chan, new_line)))
529 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
532 newas->cs_accept_mask = cs_accept_mask;
533 newas->cs_get_mask = cs_get_mask;
535 iochan_setdata(new_chan, newas);
536 iochan_settimeout(new_chan, 60);
538 a = cs_addrstr(new_line);
542 yaz_log(log_session, "Starting session %d from %s (pid=%d)",
543 no_sessions, a ? a : "[Unknown]", getpid());
544 if (max_sessions && no_sessions >= max_sessions)
545 control_block.one_shot = 1;
546 if (control_block.threads)
548 event_loop(&new_chan);
552 new_chan->next = pListener;
553 pListener = new_chan;
561 static void inetd_connection(int what)
568 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
570 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
572 if ((assoc = create_association(chan, line)))
574 iochan_setdata(chan, assoc);
575 iochan_settimeout(chan, 60);
576 addr = cs_addrstr(line);
577 yaz_log(log_session, "Inetd association from %s",
578 addr ? addr : "[UNKNOWN]");
579 assoc->cs_get_mask = EVENT_INPUT;
583 yaz_log(YLOG_FATAL, "Failed to create association structure");
585 chan->next = pListener;
590 yaz_log(YLOG_FATAL, "Failed to create iochan");
595 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
600 * Set up a listening endpoint, and give it to the event-handler.
602 static int add_listener(char *where, int what)
609 if (control_block.dynamic)
611 else if (control_block.threads)
616 yaz_log(log_server, "Adding %s %s listener on %s", mode,
617 what == PROTO_SR ? "SR" : "Z3950", where);
619 l = cs_create_host(where, 2, &ap);
622 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
625 if (*control_block.cert_fname)
626 cs_set_ssl_certificate_file(l, control_block.cert_fname);
628 if (cs_bind(l, ap, CS_SERVER) < 0)
630 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
634 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
637 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
641 iochan_setdata(lst, l);
643 /* Ensure our listener chain is setup properly */
644 lst->next = pListener;
650 /* UNIX only (for windows we don't need to catch the signals) */
651 static void catchchld(int num)
653 while (waitpid(-1, 0, WNOHANG) > 0)
655 signal(SIGCHLD, catchchld);
659 statserv_options_block *statserv_getcontrol(void)
661 static statserv_options_block cb;
663 memcpy(&cb, &control_block, sizeof(cb));
667 void statserv_setcontrol(statserv_options_block *block)
669 memcpy(&control_block, block, sizeof(*block));
672 static void statserv_reset(void)
676 int statserv_start(int argc, char **argv)
681 /* We need to initialize the thread list */
682 ThreadList_Initialize();
691 if ((me = strrchr (argv[0], sep)))
692 me++; /* get the basename */
697 if (control_block.options_func(argc, argv))
700 if (control_block.bend_start)
701 (*control_block.bend_start)(&control_block);
703 yaz_log (log_server, "Starting server %s", me);
704 if (!pListener && *control_block.default_listen)
705 add_listener(control_block.default_listen,
706 control_block.default_proto);
712 if (control_block.inetd)
713 inetd_connection(control_block.default_proto);
716 if (control_block.background)
734 open("/dev/null",O_RDWR);
737 if (!pListener && *control_block.default_listen)
738 add_listener(control_block.default_listen,
739 control_block.default_proto);
744 if (*control_block.pid_fname)
746 FILE *f = fopen(control_block.pid_fname, "w");
749 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Couldn't create %s",
750 control_block.pid_fname);
753 fprintf(f, "%ld", (long) getpid());
757 yaz_log (log_server, "Starting server %s pid=%d", programname, getpid());
760 sigset_t sigs_to_block;
762 sigemptyset(&sigs_to_block);
763 sigaddset (&sigs_to_block, SIGTERM);
764 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
767 if (control_block.dynamic)
768 signal(SIGCHLD, catchchld);
770 signal (SIGPIPE, SIG_IGN);
771 signal (SIGTERM, sigterm);
772 if (*control_block.setuid)
776 if (!(pw = getpwnam(control_block.setuid)))
778 yaz_log(YLOG_FATAL, "%s: Unknown user", control_block.setuid);
781 if (setuid(pw->pw_uid) < 0)
783 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
789 if ((pListener == NULL) && *control_block.default_listen)
790 add_listener(control_block.default_listen,
791 control_block.default_proto);
793 if (pListener == NULL)
797 yaz_log(YLOG_DEBUG, "Entering event loop.");
798 ret = event_loop(&pListener);
803 int check_options(int argc, char **argv)
808 /* set default log level */
809 control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
810 yaz_log_init_level(control_block.loglevel);
812 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:",
813 argv, argc, &arg)) != -2)
818 if (add_listener(arg, control_block.default_proto))
819 return 1; /* failed to create listener */
822 control_block.one_shot = 1;
823 control_block.dynamic = 0;
826 control_block.default_proto = PROTO_Z3950;
829 fprintf (stderr, "%s: SR protocol no longer supported\n", me);
833 control_block.dynamic = 0;
836 #if YAZ_POSIX_THREADS
837 control_block.dynamic = 0;
838 control_block.threads = 1;
839 #elif YAZ_GNU_THREADS
840 control_block.dynamic = 0;
841 control_block.threads = 1;
843 fprintf(stderr, "%s: Threaded mode not available.\n", me);
848 strcpy(control_block.logfile, arg ? arg : "");
849 yaz_log_init(control_block.loglevel, me, control_block.logfile);
852 control_block.loglevel = yaz_log_mask_str_x(arg,control_block.loglevel);
853 yaz_log_init(control_block.loglevel, me, control_block.logfile);
856 strcpy(control_block.apdufile, arg ? arg : "");
859 strcpy(control_block.setuid, arg ? arg : "");
862 strcpy(control_block.configname, arg ? arg : "");
865 strcpy(control_block.cert_fname, arg ? arg : "");
868 strcpy(control_block.daemon_name, arg ? arg : "");
871 if (!arg || !(r = atoi(arg)))
873 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
876 control_block.idle_timeout = r;
879 if (!arg || !(r = atoi(arg)))
881 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
884 control_block.maxrecordsize = r * 1024;
887 control_block.inetd = 1;
897 max_sessions = atoi(arg);
900 if (strlen(arg) >= sizeof(control_block.pid_fname))
902 yaz_log(YLOG_FATAL, "pid fname too long");
905 strcpy(control_block.pid_fname, arg);
908 control_block.background = 1;
911 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
912 " -l <logfile> -u <user> -c <config> -t <minutes>"
913 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
914 " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
929 static Args ArgDetails;
931 /* name of the executable */
932 #define SZAPPNAME "server"
934 /* list of service dependencies - "dep1\0dep2\0\0" */
935 #define SZDEPENDENCIES ""
937 int statserv_main(int argc, char **argv,
938 bend_initresult *(*bend_init)(bend_initrequest *r),
939 void (*bend_close)(void *handle))
941 statserv_options_block *cb = statserv_getcontrol();
943 cb->bend_init = bend_init;
944 cb->bend_close = bend_close;
946 statserv_setcontrol(cb);
948 /* Lets setup the Arg structure */
949 ArgDetails.argc = argc;
950 ArgDetails.argv = argv;
952 /* Now setup the service with the service controller */
953 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
954 cb->service_name, /* internal service name */
955 cb->service_display_name, /* displayed name */
960 int StartAppService(void *pHandle, int argc, char **argv)
962 /* Initializes the App */
966 void RunAppService(void *pHandle)
968 Args *pArgs = (Args *)pHandle;
970 /* Starts the app running */
971 statserv_start(pArgs->argc, pArgs->argv);
974 void StopAppService(void *pHandle)
977 statserv_closedown();
983 int statserv_main(int argc, char **argv,
984 bend_initresult *(*bend_init)(bend_initrequest *r),
985 void (*bend_close)(void *handle))
988 statserv_options_block *cb = statserv_getcontrol();
990 cb->bend_init = bend_init;
991 cb->bend_close = bend_close;
993 statserv_setcontrol(cb);
994 ret = statserv_start (argc, argv);
995 statserv_closedown ();