2 * Copyright (c) 1995-1999, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * NT server based on threads by
7 * Chas Woodfield, Fretwell Downing Datasystems.
10 * Revision 1.58 1999-08-27 09:40:32 adam
11 * Renamed logf function to yaz_log. Removed VC++ project files.
13 * Revision 1.57 1999/07/06 12:17:15 adam
14 * Added option -1 that runs server once (for profiling purposes).
16 * Revision 1.56 1999/06/10 11:45:30 adam
17 * Added bend_start, bend_stop handlers and removed pre_init.
18 * Handlers bend_start/bend_stop are called when service/daemon is
21 * Revision 1.55 1999/06/10 09:18:54 adam
22 * Modified so that pre_init is called when service/server is started.
24 * Revision 1.54 1999/04/16 14:45:55 adam
25 * Added interface for tcpd wrapper for access control.
27 * Revision 1.53 1999/02/02 13:57:39 adam
28 * Uses preprocessor define WIN32 instead of WINDOWS to build code
29 * for Microsoft WIN32.
31 * Revision 1.52 1998/08/21 14:13:34 adam
32 * Added GNU Configure script to build Makefiles.
34 * Revision 1.51 1998/07/07 15:51:03 adam
35 * Changed server so that it stops if bind fails - "address already in
36 * use" typically causes this.
38 * Revision 1.50 1998/06/22 11:32:39 adam
39 * Added 'conditional cs_listen' feature.
41 * Revision 1.49 1998/02/27 14:04:55 adam
42 * Fixed bug in statserv_remove.
44 * Revision 1.48 1998/02/11 11:53:36 adam
45 * Changed code so that it compiles as C++.
47 * Revision 1.47 1998/02/10 10:28:57 adam
48 * Added app_name, service_dependencies, service_display_name and
49 * options_func. options_func allows us to specify a different function
50 * to interogate the command line arguments. The other members allow us
51 * to pass the full service details accross to the service manager (CW).
54 * Revision 1.46 1998/01/30 15:24:57 adam
55 * Fixed bug in inetd code. The server listened on tcp:@:9999 even
56 * though it was started in inetd mode.
58 * Revision 1.45 1998/01/29 13:30:23 adam
59 * Better event handle system for NT/Unix.
61 * Revision 1.44 1997/11/07 13:31:52 adam
62 * Added NT Service name part of statserv_options_block. Moved NT
63 * service utility to server library.
65 * Revision 1.43 1997/10/31 12:20:09 adam
66 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
67 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
68 * Bug fix: missing fclose in data1_read_espec1.
70 * Revision 1.42 1997/10/27 14:03:02 adam
71 * Added new member to statserver_options_block, pre_init, which
72 * specifies a callback to be invoked after command line parsing and
73 * before the server listens for the first time.
75 * Revision 1.41 1997/09/29 07:19:32 adam
76 * Server library uses nmem_init/nmem_exit. The log prefix no longer
77 * includes leading path on NT.
79 * Revision 1.40 1997/09/17 12:10:41 adam
82 * Revision 1.39 1997/09/09 10:10:19 adam
83 * Another MSV5.0 port. Changed projects to include proper
84 * library/include paths.
85 * Server starts server in test-mode when no options are given.
87 * Revision 1.38 1997/09/04 14:19:14 adam
90 * Revision 1.37 1997/09/01 08:53:01 adam
91 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
92 * moved a separate directory. MSV5.0 project server.dsp created.
93 * As an option, the server can now operate as an NT service.
95 * Revision 1.36 1996/07/06 19:58:36 quinn
96 * System headerfiles gathered in yconfig
98 * Revision 1.35 1996/05/29 10:03:28 quinn
101 * Revision 1.34 1996/02/21 13:12:07 quinn
102 * *** empty log message ***
104 * Revision 1.33 1996/02/10 12:23:49 quinn
105 * Enable inetd operations fro TCP/IP stack
107 * Revision 1.32 1996/01/19 15:41:52 quinn
108 * *** empty log message ***
110 * Revision 1.31 1995/11/17 11:09:39 adam
111 * Added new option '-c' to specify configuration name in control block.
113 * Revision 1.30 1995/11/01 13:54:59 quinn
116 * Revision 1.29 1995/10/30 12:41:29 quinn
117 * Added hostname lookup for server.
119 * Revision 1.28 1995/09/29 17:12:30 quinn
122 * Revision 1.27 1995/09/27 15:03:02 quinn
123 * Modified function heads & prototypes.
125 * Revision 1.26 1995/08/29 14:44:51 quinn
128 * Revision 1.25 1995/08/29 11:18:02 quinn
129 * Added code to receive close
131 * Revision 1.24 1995/06/16 10:31:39 quinn
132 * Added session timeout.
134 * Revision 1.23 1995/06/15 12:30:48 quinn
137 * Revision 1.22 1995/06/15 07:45:17 quinn
140 * Revision 1.21 1995/06/06 08:15:40 quinn
143 * Revision 1.20 1995/05/29 08:12:09 quinn
146 * Revision 1.19 1995/05/16 09:37:27 quinn
149 * Revision 1.18 1995/05/16 08:51:09 quinn
150 * License, documentation, and memory fixes
152 * Revision 1.17 1995/05/15 11:56:42 quinn
153 * Asynchronous facilities. Restructuring of seshigh code.
155 * Revision 1.16 1995/04/10 10:23:40 quinn
156 * Some work to add scan and other things.
158 * Revision 1.15 1995/03/31 10:16:51 quinn
161 * Revision 1.14 1995/03/31 09:18:58 quinn
164 * Revision 1.13 1995/03/30 16:08:39 quinn
167 * Revision 1.12 1995/03/30 13:29:02 quinn
170 * Revision 1.11 1995/03/30 12:18:17 quinn
173 * Revision 1.10 1995/03/29 15:40:16 quinn
174 * Ongoing work. Statserv is now dynamic by default
176 * Revision 1.9 1995/03/27 08:34:30 quinn
177 * Added dynamic server functionality.
178 * Released bindings to session.c (is now redundant)
180 * Revision 1.8 1995/03/20 09:46:26 quinn
183 * Revision 1.7 1995/03/16 13:29:04 quinn
184 * Partitioned server.
186 * Revision 1.6 1995/03/15 15:18:52 quinn
187 * Little changes to better support nonblocking I/O
190 * Revision 1.5 1995/03/15 08:37:45 quinn
191 * Now we're pretty much set for nonblocking I/O.
193 * Revision 1.4 1995/03/14 16:59:48 quinn
196 * Revision 1.3 1995/03/14 11:30:15 quinn
199 * Revision 1.2 1995/03/14 10:28:03 quinn
200 * More work on demo server.
202 * Revision 1.1 1995/03/10 18:22:45 quinn
203 * The rudiments of an asynchronous server.
222 #include <comstack.h>
231 #include <statserv.h>
233 static IOCHAN pListener = NULL;
235 static char *me = "statserver";
239 int check_options(int argc, char **argv);
240 statserv_options_block control_block = {
241 1, /* dynamic mode */
242 0, /* one shot (single session) */
243 LOG_DEFAULT_LEVEL, /* log level */
245 "", /* diagnostic output to stderr */
246 "tcp:@:9999", /* default listener port */
247 PROTO_Z3950, /* default application protocol */
248 60, /* idle timeout (minutes) */
249 1024*1024, /* maximum PDU size (approx.) to allow */
250 "default-config", /* configuration name to pass to backend */
251 "", /* set user id */
252 0, /* bend_start handler */
253 0, /* bend_stop handler */
254 check_options, /* Default routine, for checking the run-time arguments */
257 0 /* default value for inet deamon */
260 ,"Z39.50 Server", /* NT Service Name */
261 "Server", /* NT application Name */
262 "", /* NT Service Dependencies */
263 "Z39.50 Server" /* NT Service Display Name */
268 * handle incoming connect requests.
269 * The dynamic mode is a bit tricky mostly because we want to avoid
270 * doing all of the listening and accepting in the parent - it's
275 typedef struct _ThreadList ThreadList;
284 static ThreadList *pFirstThread;
285 static CRITICAL_SECTION Thread_CritSect;
286 static BOOL bInitialized = FALSE;
288 static void ThreadList_Initialize()
290 /* Initialize the critical Sections */
291 InitializeCriticalSection(&Thread_CritSect);
293 /* Set the first thraed */
296 /* we have been initialized */
300 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
302 /* Only one thread can go through this section at a time */
303 EnterCriticalSection(&Thread_CritSect);
306 /* Lets create our new object */
307 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
308 pNewThread->hThread = hThread;
309 pNewThread->pIOChannel = pIOChannel;
310 pNewThread->pNext = pFirstThread;
311 pFirstThread = pNewThread;
313 /* Lets let somebody else create a new object now */
314 LeaveCriticalSection(&Thread_CritSect);
318 void statserv_remove(IOCHAN pIOChannel)
320 /* Only one thread can go through this section at a time */
321 EnterCriticalSection(&Thread_CritSect);
324 ThreadList *pCurrentThread = pFirstThread;
325 ThreadList *pNextThread;
326 ThreadList *pPrevThread =NULL;
328 /* Step through alll the threads */
329 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
331 /* We only need to compare on the IO Channel */
332 if (pCurrentThread->pIOChannel == pIOChannel)
334 /* We have found the thread we want to delete */
335 /* First of all reset the next pointers */
336 if (pPrevThread == NULL)
337 pFirstThread = pCurrentThread->pNext;
339 pPrevThread->pNext = pCurrentThread->pNext;
341 /* All we need todo now is delete the memory */
342 free(pCurrentThread);
344 /* No need to look at any more threads */
349 /* We need to look at another thread */
350 pNextThread = pCurrentThread->pNext;
351 pPrevThread = pCurrentThread;
355 /* Lets let somebody else remove an object now */
356 LeaveCriticalSection(&Thread_CritSect);
360 void statserv_closedown()
362 /* Shouldn't do anything if we are not initialized */
366 HANDLE *pThreadHandles = NULL;
368 /* We need to stop threads adding and removing while we */
369 /* start the closedown process */
370 EnterCriticalSection(&Thread_CritSect);
373 /* We have exclusive access to the thread stuff now */
374 /* Y didn't i use a semaphore - Oh well never mind */
375 ThreadList *pCurrentThread = pFirstThread;
377 /* Before we do anything else, we need to shutdown the listener */
378 if (pListener != NULL)
379 iochan_destroy(pListener);
381 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
383 /* Just destroy the IOCHAN, that should do the trick */
384 iochan_destroy(pCurrentThread->pIOChannel);
386 /* Keep a running count of our handles */
392 HANDLE *pCurrentHandle ;
394 /* Allocate the thread handle array */
395 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
396 pCurrentHandle = pThreadHandles;
398 for (pCurrentThread = pFirstThread;
399 pCurrentThread != NULL;
400 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
402 /* Just the handle */
403 *pCurrentHandle = pCurrentThread->hThread;
407 /* We can now leave the critical section */
408 LeaveCriticalSection(&Thread_CritSect);
411 /* Now we can really do something */
414 /* This will now wait, until all the threads close */
415 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
417 /* Free the memory we allocated for the handle array */
418 free(pThreadHandles);
421 if (control_block.bend_stop)
422 (*control_block.bend_stop)(&control_block);
423 /* No longer require the critical section, since all threads are dead */
424 DeleteCriticalSection(&Thread_CritSect);
428 int __stdcall event_loop_thread (IOCHAN iochan)
430 return event_loop (&iochan);
433 static void listener(IOCHAN h, int event)
435 COMSTACK line = (COMSTACK) iochan_getdata(h);
440 if (event == EVENT_INPUT)
442 if ((res = cs_listen(line, 0, 0)) < 0)
444 yaz_log(LOG_FATAL, "cs_listen failed");
449 yaz_log(LOG_DEBUG, "listen ok");
450 iochan_setevent(h, EVENT_OUTPUT);
451 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
453 else if (event == EVENT_OUTPUT)
460 if (!(new_line = cs_accept(line)))
462 yaz_log(LOG_FATAL, "Accept failed.");
463 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
466 yaz_log(LOG_DEBUG, "Accept ok");
468 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
471 yaz_log(LOG_FATAL, "Failed to create iochan");
476 yaz_log(LOG_DEBUG, "Creating association");
477 if (!(newas = create_association(new_chan, new_line)))
479 yaz_log(LOG_FATAL, "Failed to create new assoc.");
483 yaz_log(LOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
484 iochan_setdata(new_chan, newas);
485 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
487 yaz_log(LOG_DEBUG, "Determining client address");
488 a = cs_addrstr(new_line);
489 yaz_log(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
491 /* Now what we need todo is create a new thread with this iochan as
493 /* if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)event_loop_thread,
494 new_chan, 0, &ThreadId) == NULL) */
495 /* Somehow, somewhere we need to store this thread id, otherwise we
496 won't be able to close cleanly */
497 NewHandle = (HANDLE)_beginthreadex(NULL, 0, event_loop_thread,
498 new_chan, 0, &ThreadId);
499 if (NewHandle == (HANDLE)-1)
502 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
506 /* We successfully created the thread, so add it to the list */
507 statserv_add(NewHandle, new_chan);
509 yaz_log(LOG_DEBUG, "Created new thread, iochan %p", new_chan);
510 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
514 yaz_log(LOG_FATAL, "Bad event on listener.");
522 /* To save having an #ifdef in event_loop we need to define this empty function */
523 void statserv_remove(IOCHAN pIOChannel)
527 void statserv_closedown()
530 for (p = pListener; p; p = p->next)
534 static void listener(IOCHAN h, int event)
536 COMSTACK line = (COMSTACK) iochan_getdata(h);
539 static int child = 0;
542 if (event == EVENT_INPUT)
544 if (control_block.dynamic && !child)
550 yaz_log(LOG_FATAL|LOG_ERRNO, "pipe");
554 if ((res = fork()) < 0)
556 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
560 else if (res == 0) /* child */
567 for (pp = pListener; pp; pp = iochan_getnext(pp))
571 COMSTACK l = (COMSTACK)iochan_getdata(pp);
576 sprintf(nbuf, "%s(%d)", me, getpid());
577 log_init(control_block.loglevel, nbuf, 0);
582 /* wait for child to take the call */
588 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
590 yaz_log(LOG_FATAL|LOG_ERRNO, "handshake read");
596 yaz_log(LOG_DEBUG, "P: Child has taken the call");
601 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
602 control_block.daemon_name)) < 0)
604 yaz_log(LOG_WARN, "cs_listen failed");
609 yaz_log(LOG_DEBUG, "listen ok");
610 iochan_setevent(h, EVENT_OUTPUT);
611 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
613 /* in dynamic mode, only the child ever comes down here */
614 else if (event == EVENT_OUTPUT)
620 if (!(new_line = cs_accept(line)))
622 yaz_log(LOG_FATAL, "Accept failed.");
623 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
626 yaz_log(LOG_DEBUG, "accept ok");
627 if (control_block.dynamic)
630 /* close our half of the listener socket */
631 for (pp = pListener; pp; pp = iochan_getnext(pp))
633 COMSTACK l = (COMSTACK)iochan_getdata(pp);
638 yaz_log(LOG_DEBUG, "Releasing parent");
642 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
644 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
647 yaz_log(LOG_FATAL, "Failed to create iochan");
651 new_chan->next = pListener;
652 pListener = new_chan;
653 if (!(newas = create_association(new_chan, new_line)))
655 yaz_log(LOG_FATAL, "Failed to create new assoc.");
659 iochan_setdata(new_chan, newas);
660 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
661 a = cs_addrstr(new_line);
662 yaz_log(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
666 yaz_log(LOG_FATAL, "Bad event on listener.");
674 static void inetd_connection(int what)
681 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
683 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
685 if ((assoc = create_association(chan, line)))
687 iochan_setdata(chan, assoc);
688 iochan_settimeout(chan, control_block.idle_timeout * 60);
689 addr = cs_addrstr(line);
690 yaz_log(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
694 yaz_log(LOG_FATAL, "Failed to create association structure");
696 chan->next = pListener;
701 yaz_log(LOG_FATAL, "Failed to create iochan");
706 yaz_log(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
711 * Set up a listening endpoint, and give it to the event-handler.
713 static void add_listener(char *where, int what)
717 char mode[100], addr[100];
721 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
723 yaz_log (LOG_WARN, "%s: Address format: ('tcp'|'osi')':'<address>", me);
726 if (!strcmp(mode, "tcp"))
728 else if (!strcmp(mode, "osi"))
733 yaz_log (LOG_WARN, "OSI Transport not allowed by configuration.");
739 yaz_log (LOG_WARN, "You must specify either 'osi:' or 'tcp:'");
742 yaz_log(LOG_LOG, "Adding %s %s listener on %s",
743 control_block.dynamic ? "dynamic" : "static",
744 what == PROTO_SR ? "SR" : "Z3950", where);
745 if (!(l = cs_create(type, 0, what)))
747 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
750 ap = cs_straddr (l, addr);
753 fprintf(stderr, "Address resolution failed.\n");
757 if (cs_bind(l, ap, CS_SERVER) < 0)
759 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
763 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
766 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
770 iochan_setdata(lst, l);
772 /* Ensure our listener chain is setup properly */
773 lst->next = pListener;
778 /* For windows we don't need to catch the signals */
779 static void catchchld(int num)
781 while (waitpid(-1, 0, WNOHANG) > 0)
783 signal(SIGCHLD, catchchld);
787 statserv_options_block *statserv_getcontrol(void)
789 static statserv_options_block cb;
791 memcpy(&cb, &control_block, sizeof(cb));
795 void statserv_setcontrol(statserv_options_block *block)
797 memcpy(&control_block, block, sizeof(*block));
800 int statserv_start(int argc, char **argv)
806 /* We need to initialize the thread list */
807 ThreadList_Initialize();
811 if ((me = strrchr (argv[0], '\\')))
818 if (control_block.options_func(argc, argv))
821 if (control_block.bend_start)
822 (*control_block.bend_start)(&control_block);
824 if (control_block.inetd)
825 inetd_connection(control_block.default_proto);
828 if (control_block.dynamic)
829 signal(SIGCHLD, catchchld);
831 if (*control_block.setuid)
835 if (!(pw = getpwnam(control_block.setuid)))
837 yaz_log(LOG_FATAL, "%s: Unknown user", control_block.setuid);
840 if (setuid(pw->pw_uid) < 0)
842 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
848 if ((pListener == NULL) && *control_block.default_listen)
849 add_listener(control_block.default_listen,
850 control_block.default_proto);
852 if (pListener == NULL)
856 yaz_log(LOG_LOG, "Entering event loop.");
857 ret = event_loop(&pListener);
863 int check_options(int argc, char **argv)
868 while ((ret = options("1a:iszSl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
873 add_listener(arg, control_block.default_proto);
876 control_block.one_shot = 1;
877 control_block.dynamic = 0;
880 control_block.default_proto = PROTO_Z3950;
883 control_block.default_proto = PROTO_SR;
886 control_block.dynamic = 0;
889 strcpy(control_block.logfile, arg ? arg : "");
890 log_init(control_block.loglevel, me, control_block.logfile);
893 control_block.loglevel = log_mask_str(arg);
894 log_init(control_block.loglevel, me, control_block.logfile);
897 strcpy(control_block.apdufile, arg ? arg : "");
900 strcpy(control_block.setuid, arg ? arg : "");
903 strcpy(control_block.configname, arg ? arg : "");
906 strcpy(control_block.daemon_name, arg ? arg : "");
909 if (!arg || !(r = atoi(arg)))
911 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
914 control_block.idle_timeout = r;
917 if (!arg || !(r = atoi(arg)))
919 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
922 control_block.maxrecordsize = r * 1024;
925 control_block.inetd = 1;
935 fprintf(stderr, "Usage: %s [ -i -a <pdufile> -v <loglevel>"
936 " -l <logfile> -u <user> -c <config> -t <minutes>"
937 " -k <kilobytes> -d <daemon>"
938 " -zsS <listener-addr> -w <directory> ... ]\n", me);
952 static Args ArgDetails;
954 /* name of the executable */
955 #define SZAPPNAME "server"
957 /* list of service dependencies - "dep1\0dep2\0\0" */
958 #define SZDEPENDENCIES ""
960 int statserv_main(int argc, char **argv)
962 statserv_options_block *cb = statserv_getcontrol();
964 /* Lets setup the Arg structure */
965 ArgDetails.argc = argc;
966 ArgDetails.argv = argv;
968 /* Now setup the service with the service controller */
969 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
970 cb->service_name, /* internal service name */
971 cb->service_name, /* displayed name of the service */
976 int StartAppService(void *pHandle, int argc, char **argv)
978 /* Initializes the App */
982 void RunAppService(void *pHandle)
984 Args *pArgs = (Args *)pHandle;
986 /* Starts the app running */
987 statserv_start(pArgs->argc, pArgs->argv);
990 void StopAppService(void *pHandle)
993 statserv_closedown();
996 int statserv_main(int argc, char **argv)
998 int ret = statserv_start (argc, argv);
999 statserv_closedown ();