2 * Copyright (c) 1995-1997, 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 Datasystem.
10 * Revision 1.41 1997-09-29 07:19:32 adam
11 * Server library uses nmem_init/nmem_exit. The log prefix no longer
12 * includes leading path on NT.
14 * Revision 1.40 1997/09/17 12:10:41 adam
17 * Revision 1.39 1997/09/09 10:10:19 adam
18 * Another MSV5.0 port. Changed projects to include proper
19 * library/include paths.
20 * Server starts server in test-mode when no options are given.
22 * Revision 1.38 1997/09/04 14:19:14 adam
25 * Revision 1.37 1997/09/01 08:53:01 adam
26 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
27 * moved a separate directory. MSV5.0 project server.dsp created.
28 * As an option, the server can now operate as an NT service.
30 * Revision 1.36 1996/07/06 19:58:36 quinn
31 * System headerfiles gathered in yconfig
33 * Revision 1.35 1996/05/29 10:03:28 quinn
36 * Revision 1.34 1996/02/21 13:12:07 quinn
37 * *** empty log message ***
39 * Revision 1.33 1996/02/10 12:23:49 quinn
40 * Enable inetd operations fro TCP/IP stack
42 * Revision 1.32 1996/01/19 15:41:52 quinn
43 * *** empty log message ***
45 * Revision 1.31 1995/11/17 11:09:39 adam
46 * Added new option '-c' to specify configuration name in control block.
48 * Revision 1.30 1995/11/01 13:54:59 quinn
51 * Revision 1.29 1995/10/30 12:41:29 quinn
52 * Added hostname lookup for server.
54 * Revision 1.28 1995/09/29 17:12:30 quinn
57 * Revision 1.27 1995/09/27 15:03:02 quinn
58 * Modified function heads & prototypes.
60 * Revision 1.26 1995/08/29 14:44:51 quinn
63 * Revision 1.25 1995/08/29 11:18:02 quinn
64 * Added code to receive close
66 * Revision 1.24 1995/06/16 10:31:39 quinn
67 * Added session timeout.
69 * Revision 1.23 1995/06/15 12:30:48 quinn
72 * Revision 1.22 1995/06/15 07:45:17 quinn
75 * Revision 1.21 1995/06/06 08:15:40 quinn
78 * Revision 1.20 1995/05/29 08:12:09 quinn
81 * Revision 1.19 1995/05/16 09:37:27 quinn
84 * Revision 1.18 1995/05/16 08:51:09 quinn
85 * License, documentation, and memory fixes
87 * Revision 1.17 1995/05/15 11:56:42 quinn
88 * Asynchronous facilities. Restructuring of seshigh code.
90 * Revision 1.16 1995/04/10 10:23:40 quinn
91 * Some work to add scan and other things.
93 * Revision 1.15 1995/03/31 10:16:51 quinn
96 * Revision 1.14 1995/03/31 09:18:58 quinn
99 * Revision 1.13 1995/03/30 16:08:39 quinn
102 * Revision 1.12 1995/03/30 13:29:02 quinn
105 * Revision 1.11 1995/03/30 12:18:17 quinn
108 * Revision 1.10 1995/03/29 15:40:16 quinn
109 * Ongoing work. Statserv is now dynamic by default
111 * Revision 1.9 1995/03/27 08:34:30 quinn
112 * Added dynamic server functionality.
113 * Released bindings to session.c (is now redundant)
115 * Revision 1.8 1995/03/20 09:46:26 quinn
118 * Revision 1.7 1995/03/16 13:29:04 quinn
119 * Partitioned server.
121 * Revision 1.6 1995/03/15 15:18:52 quinn
122 * Little changes to better support nonblocking I/O
125 * Revision 1.5 1995/03/15 08:37:45 quinn
126 * Now we're pretty much set for nonblocking I/O.
128 * Revision 1.4 1995/03/14 16:59:48 quinn
131 * Revision 1.3 1995/03/14 11:30:15 quinn
134 * Revision 1.2 1995/03/14 10:28:03 quinn
135 * More work on demo server.
137 * Revision 1.1 1995/03/10 18:22:45 quinn
138 * The rudiments of an asynchronous server.
160 #include <comstack.h>
166 #include <statserv.h>
168 static IOCHAN pListener;
170 static char *me = "statserver";
174 static statserv_options_block control_block = {
175 1, /* dynamic mode */
176 LOG_DEFAULT_LEVEL, /* log level */
178 "", /* diagnostic output to stderr */
179 "tcp:@:9999", /* default listener port */
180 PROTO_Z3950, /* default application protocol */
181 60, /* idle timeout (minutes) */
182 1024*1024, /* maximum PDU size (approx.) to allow */
183 "default-config", /* configuration name to pass to backend */
188 * handle incoming connect requests.
189 * The dynamic mode is a bit tricky mostly because we want to avoid
190 * doing all of the listening and accepting in the parent - it's
195 typedef struct _ThreadList ThreadList;
197 typedef struct _ThreadList
204 static ThreadList *pFirstThread;
205 static CRITICAL_SECTION Thread_CritSect;
206 static BOOL bInitialized = FALSE;
208 static void ThreadList_Initialize()
210 /* Initialize the critical Sections */
211 InitializeCriticalSection(&Thread_CritSect);
213 /* Set the first thraed */
216 /* we have been initialized */
220 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
222 /* Only one thread can go through this section at a time */
223 EnterCriticalSection(&Thread_CritSect);
226 /* Lets create our new object */
227 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
228 pNewThread->hThread = hThread;
229 pNewThread->pIOChannel = pIOChannel;
230 pNewThread->pNext = pFirstThread;
231 pFirstThread = pNewThread;
233 /* Lets let somebody else create a new object now */
234 LeaveCriticalSection(&Thread_CritSect);
238 void statserv_remove(IOCHAN pIOChannel)
240 /* Only one thread can go through this section at a time */
241 EnterCriticalSection(&Thread_CritSect);
244 ThreadList *pCurrentThread = pFirstThread;
245 ThreadList *pNextThread;
246 ThreadList *pPrevThread =NULL;
248 /* Step through alll the threads */
249 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
251 /* We only need to compare on the IO Channel */
252 if (pCurrentThread->pIOChannel == pIOChannel)
254 /* We have found the thread we want to delete */
255 /* First of all reset the next pointers */
256 if (pPrevThread == NULL)
257 pFirstThread = pCurrentThread->pNext;
259 pPrevThread->pNext = pCurrentThread->pNext;
261 /* All we need todo now is delete the memory */
262 free(pCurrentThread);
264 /* No need to look at any more threads */
269 /* We need to look at another thread */
270 pNextThread = pCurrentThread->pNext;
274 /* Lets let somebody else remove an object now */
275 LeaveCriticalSection(&Thread_CritSect);
279 void statserv_closedown()
281 /* Shouldn't do anything if we are not initialized */
285 HANDLE *pThreadHandles = NULL;
287 /* We need to stop threads adding and removing while we */
288 /* start the closedown process */
289 EnterCriticalSection(&Thread_CritSect);
292 /* We have exclusive access to the thread stuff now */
293 /* Y didn't i use a semaphore - Oh well never mind */
294 ThreadList *pCurrentThread = pFirstThread;
296 /* Before we do anything else, we need to shutdown the listener */
297 if (pListener != NULL)
298 iochan_destroy(pListener);
300 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
302 /* Just destroy the IOCHAN, that should do the trick */
303 iochan_destroy(pCurrentThread->pIOChannel);
305 /* Keep a running count of our handles */
311 HANDLE *pCurrentHandle ;
313 /* Allocate the thread handle array */
314 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
315 pCurrentHandle = pThreadHandles;
317 for (pCurrentThread = pFirstThread;
318 pCurrentThread != NULL;
319 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
321 /* Just the handle */
322 *pCurrentHandle = pCurrentThread->hThread;
326 /* We can now leave the critical section */
327 LeaveCriticalSection(&Thread_CritSect);
330 /* Now we can really do something */
333 /* This will now wait, until all the threads close */
334 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
336 /* Free the memory we allocated for the handle array */
337 free(pThreadHandles);
340 /* No longer require the critical section, since all threads are dead */
341 DeleteCriticalSection(&Thread_CritSect);
345 static void listener(IOCHAN h, int event)
347 COMSTACK line = (COMSTACK) iochan_getdata(h);
352 if (event == EVENT_INPUT)
354 if ((res = cs_listen(line, 0, 0)) < 0)
356 logf(LOG_FATAL, "cs_listen failed.");
361 logf(LOG_DEBUG, "listen ok");
362 iochan_setevent(h, EVENT_OUTPUT);
363 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
365 else if (event == EVENT_OUTPUT)
372 if (!(new_line = cs_accept(line)))
374 logf(LOG_FATAL, "Accept failed.");
375 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
378 logf(LOG_DEBUG, "Accept ok");
380 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, EVENT_INPUT)))
382 logf(LOG_FATAL, "Failed to create iochan");
387 logf(LOG_DEBUG, "Creating association");
388 if (!(newas = create_association(new_chan, new_line)))
390 logf(LOG_FATAL, "Failed to create new assoc.");
394 logf(LOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
395 iochan_setdata(new_chan, newas);
396 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
397 logf(LOG_DEBUG, "Determining client address");
398 a = cs_addrstr(new_line);
399 logf(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
400 /* Now what we need todo is create a new thread with this iochan as
402 /* if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)event_loop, new_chan,
403 0, &ThreadId) == NULL) */
404 /* Somehow, somewhere we need to store this thread id, otherwise we won't be
405 able to close cleanly */
406 NewHandle = (HANDLE)_beginthreadex(NULL, 0, event_loop, new_chan, 0, &ThreadId);
407 if (NewHandle == (HANDLE)-1)
410 logf(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
414 /* We successfully created the thread, so add it to the list */
415 statserv_add(NewHandle, new_chan);
417 logf(LOG_DEBUG, "Created new thread, iochan %p", new_chan);
418 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
422 logf(LOG_FATAL, "Bad event on listener.");
430 /* To save having an #ifdef in event_loop we need to define this empty function */
431 void statserv_remove(IOCHAN pIOChannel)
435 void statserv_closedown()
437 /* We don't need todoanything here - or do we */
438 if (pListener != NULL)
439 iochan_destroy(pListener);
442 static void listener(IOCHAN h, int event)
444 COMSTACK line = (COMSTACK) iochan_getdata(h);
447 static int child = 0;
450 if (event == EVENT_INPUT)
452 if (control_block.dynamic && !child)
458 logf(LOG_FATAL|LOG_ERRNO, "pipe");
462 if ((res = fork()) < 0)
464 logf(LOG_FATAL|LOG_ERRNO, "fork");
468 else if (res == 0) /* child */
475 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
479 COMSTACK l = iochan_getdata(pp);
484 sprintf(nbuf, "%s(%d)", me, getpid());
485 log_init(control_block.loglevel, nbuf, 0);
490 /* wait for child to take the call */
496 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
498 logf(LOG_FATAL|LOG_ERRNO, "handshake read");
504 logf(LOG_DEBUG, "P: Child has taken the call");
509 if ((res = cs_listen(line, 0, 0)) < 0)
511 logf(LOG_FATAL, "cs_listen failed.");
516 logf(LOG_DEBUG, "listen ok");
517 iochan_setevent(h, EVENT_OUTPUT);
518 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
520 /* in dynamic mode, only the child ever comes down here */
521 else if (event == EVENT_OUTPUT)
527 if (!(new_line = cs_accept(line)))
529 logf(LOG_FATAL, "Accept failed.");
530 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
533 logf(LOG_DEBUG, "accept ok");
534 if (control_block.dynamic)
537 /* close our half of the listener socket */
538 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
540 COMSTACK l = iochan_getdata(pp);
545 logf(LOG_DEBUG, "Releasing parent");
549 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
551 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
554 logf(LOG_FATAL, "Failed to create iochan");
558 if (!(newas = create_association(new_chan, new_line)))
560 logf(LOG_FATAL, "Failed to create new assoc.");
564 iochan_setdata(new_chan, newas);
565 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
566 a = cs_addrstr(new_line);
567 logf(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
571 logf(LOG_FATAL, "Bad event on listener.");
579 static void inetd_connection(int what)
586 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
588 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
590 if ((assoc = create_association(chan, line)))
592 iochan_setdata(chan, assoc);
593 iochan_settimeout(chan, control_block.idle_timeout * 60);
594 addr = cs_addrstr(line);
595 logf(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
599 logf(LOG_FATAL, "Failed to create association structure");
604 logf(LOG_FATAL, "Failed to create iochan");
609 logf(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
614 * Set up a listening endpoint, and give it to the event-handler.
616 static void add_listener(char *where, int what)
620 char mode[100], addr[100];
624 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
626 logf (LOG_WARN, "%s: Address format: ('tcp'|'osi')':'<address>", me);
629 if (!strcmp(mode, "tcp"))
631 else if (!strcmp(mode, "osi"))
636 logf (LOG_WARN, "OSI Transport not allowed by configuration.");
642 logf (LOG_WARN, "You must specify either 'osi:' or 'tcp:'");
645 logf(LOG_LOG, "Adding %s %s listener on %s",
646 control_block.dynamic ? "dynamic" : "static",
647 what == PROTO_SR ? "SR" : "Z3950", where);
648 if (!(l = cs_create(type, 0, what)))
650 logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
652 ap = cs_straddr (l, addr);
655 fprintf(stderr, "Address resolution failed.\n");
659 if (cs_bind(l, ap, CS_SERVER) < 0)
661 logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
663 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
666 logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
668 iochan_setdata(lst, l);
670 /* Ensure our listener chain is setup properly */
671 lst->next = pListener;
676 /* For windows we don't need to catch the signals */
677 static void catchchld(int num)
679 while (waitpid(-1, 0, WNOHANG) > 0)
681 signal(SIGCHLD, catchchld);
685 statserv_options_block *statserv_getcontrol(void)
687 static statserv_options_block cb;
689 memcpy(&cb, &control_block, sizeof(cb));
693 void statserv_setcontrol(statserv_options_block *block)
695 memcpy(&control_block, block, sizeof(*block));
698 int statserv_main(int argc, char **argv)
700 int ret, listeners = 0, inetd = 0, r;
702 int protocol = control_block.default_proto;
706 /* We need to initialize the thread list */
707 ThreadList_Initialize();
711 if ((me = strrchr (argv[0], '\\')))
718 while ((ret = options("a:iszSl:v:u:c:w:t:k:", argv, argc, &arg)) != -2)
723 add_listener(arg, protocol);
726 case 'z': protocol = PROTO_Z3950; break;
727 case 's': protocol = PROTO_SR; break;
728 case 'S': control_block.dynamic = 0; break;
730 strcpy(control_block.logfile, arg ? arg : "");
731 log_init(control_block.loglevel, me, control_block.logfile);
734 control_block.loglevel = log_mask_str(arg);
735 log_init(control_block.loglevel, me, control_block.logfile);
738 strcpy(control_block.apdufile, arg ? arg : ""); break;
740 strcpy(control_block.setuid, arg ? arg : ""); break;
742 strcpy(control_block.configname, arg ? arg : ""); break;
744 if (!arg || !(r = atoi(arg)))
746 fprintf(stderr, "%s: Specify positive timeout for -t.\n",
750 control_block.idle_timeout = r;
753 if (!arg || !(r = atoi(arg)))
755 fprintf(stderr, "%s: Specify positive timeout for -t.\n",
759 control_block.maxrecordsize = r * 1024;
772 fprintf(stderr, "Usage: %s [ -i -a <pdufile> -v <loglevel>"
773 " -l <logfile> -u <user> -c <config> -t <minutes>"
775 " -zsS <listener-addr> -w <directory> ... ]\n", me);
781 log_init(control_block.loglevel, NULL, control_block.logfile);
784 if ((pListener == NULL) && *control_block.default_listen)
785 add_listener(control_block.default_listen, protocol);
789 inetd_connection(protocol);
792 if (control_block.dynamic)
793 signal(SIGCHLD, catchchld);
795 if (*control_block.setuid)
799 if (!(pw = getpwnam(control_block.setuid)))
801 logf(LOG_FATAL, "%s: Unknown user", control_block.setuid);
804 if (setuid(pw->pw_uid) < 0)
806 logf(LOG_FATAL|LOG_ERRNO, "setuid");
812 logf(LOG_LOG, "Entering event loop.");
814 if (pListener == NULL)
817 ret = event_loop(pListener);