YAZ version 1.4.
[yaz-moved-to-github.git] / server / statserv.c
1 /*
2  * Copyright (c) 1995-1997, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * NT server based on threads by
7  *   Chas Woodfield, Fretwell Downing Datasystem.
8  *
9  * $Log: statserv.c,v $
10  * Revision 1.40  1997-09-17 12:10:41  adam
11  * YAZ version 1.4.
12  *
13  * Revision 1.39  1997/09/09 10:10:19  adam
14  * Another MSV5.0 port. Changed projects to include proper
15  * library/include paths.
16  * Server starts server in test-mode when no options are given.
17  *
18  * Revision 1.38  1997/09/04 14:19:14  adam
19  * Added credits.
20  *
21  * Revision 1.37  1997/09/01 08:53:01  adam
22  * New windows NT/95 port using MSV5.0. The test server 'ztest' was
23  * moved a separate directory. MSV5.0 project server.dsp created.
24  * As an option, the server can now operate as an NT service.
25  *
26  * Revision 1.36  1996/07/06 19:58:36  quinn
27  * System headerfiles gathered in yconfig
28  *
29  * Revision 1.35  1996/05/29  10:03:28  quinn
30  * Options work
31  *
32  * Revision 1.34  1996/02/21  13:12:07  quinn
33  * *** empty log message ***
34  *
35  * Revision 1.33  1996/02/10  12:23:49  quinn
36  * Enable inetd operations fro TCP/IP stack
37  *
38  * Revision 1.32  1996/01/19  15:41:52  quinn
39  * *** empty log message ***
40  *
41  * Revision 1.31  1995/11/17  11:09:39  adam
42  * Added new option '-c' to specify configuration name in control block.
43  *
44  * Revision 1.30  1995/11/01  13:54:59  quinn
45  * Minor adjustments
46  *
47  * Revision 1.29  1995/10/30  12:41:29  quinn
48  * Added hostname lookup for server.
49  *
50  * Revision 1.28  1995/09/29  17:12:30  quinn
51  * Smallish
52  *
53  * Revision 1.27  1995/09/27  15:03:02  quinn
54  * Modified function heads & prototypes.
55  *
56  * Revision 1.26  1995/08/29  14:44:51  quinn
57  * Reset timeouts.
58  *
59  * Revision 1.25  1995/08/29  11:18:02  quinn
60  * Added code to receive close
61  *
62  * Revision 1.24  1995/06/16  10:31:39  quinn
63  * Added session timeout.
64  *
65  * Revision 1.23  1995/06/15  12:30:48  quinn
66  * Setuid-facility.
67  *
68  * Revision 1.22  1995/06/15  07:45:17  quinn
69  * Moving to v3.
70  *
71  * Revision 1.21  1995/06/06  08:15:40  quinn
72  * Cosmetic.
73  *
74  * Revision 1.20  1995/05/29  08:12:09  quinn
75  * Moved oid to util
76  *
77  * Revision 1.19  1995/05/16  09:37:27  quinn
78  * Fixed bug
79  *
80  * Revision 1.18  1995/05/16  08:51:09  quinn
81  * License, documentation, and memory fixes
82  *
83  * Revision 1.17  1995/05/15  11:56:42  quinn
84  * Asynchronous facilities. Restructuring of seshigh code.
85  *
86  * Revision 1.16  1995/04/10  10:23:40  quinn
87  * Some work to add scan and other things.
88  *
89  * Revision 1.15  1995/03/31  10:16:51  quinn
90  * Fixed logging.
91  *
92  * Revision 1.14  1995/03/31  09:18:58  quinn
93  * Added logging.
94  *
95  * Revision 1.13  1995/03/30  16:08:39  quinn
96  * Little mods.
97  *
98  * Revision 1.12  1995/03/30  13:29:02  quinn
99  * Smallish
100  *
101  * Revision 1.11  1995/03/30  12:18:17  quinn
102  * Fixed bug.
103  *
104  * Revision 1.10  1995/03/29  15:40:16  quinn
105  * Ongoing work. Statserv is now dynamic by default
106  *
107  * Revision 1.9  1995/03/27  08:34:30  quinn
108  * Added dynamic server functionality.
109  * Released bindings to session.c (is now redundant)
110  *
111  * Revision 1.8  1995/03/20  09:46:26  quinn
112  * Added osi support.
113  *
114  * Revision 1.7  1995/03/16  13:29:04  quinn
115  * Partitioned server.
116  *
117  * Revision 1.6  1995/03/15  15:18:52  quinn
118  * Little changes to better support nonblocking I/O
119  * Added backend.h
120  *
121  * Revision 1.5  1995/03/15  08:37:45  quinn
122  * Now we're pretty much set for nonblocking I/O.
123  *
124  * Revision 1.4  1995/03/14  16:59:48  quinn
125  * Bug-fixes
126  *
127  * Revision 1.3  1995/03/14  11:30:15  quinn
128  * Works better now.
129  *
130  * Revision 1.2  1995/03/14  10:28:03  quinn
131  * More work on demo server.
132  *
133  * Revision 1.1  1995/03/10  18:22:45  quinn
134  * The rudiments of an asynchronous server.
135  *
136  */
137
138 #include <yconfig.h>
139 #include <stdio.h>
140 #ifdef WINDOWS
141 #include <process.h>
142 #include <winsock.h>
143 #include <direct.h>
144 #else
145 #include <unistd.h>
146 #include <pwd.h>
147 #endif
148 #include <fcntl.h>
149 #include <signal.h>
150 #include <errno.h>
151
152 #include <options.h>
153 #include "eventl.h"
154 #include "session.h"
155 #include <comstack.h>
156 #include <tcpip.h>
157 #ifdef USE_XTIMOSI
158 #include <xmosi.h>
159 #endif
160 #include <log.h>
161 #include <statserv.h>
162
163 static IOCHAN pListener;
164
165 static char *me = "statserver";
166 /*
167  * default behavior.
168  */
169 static statserv_options_block control_block = {
170     1,                          /* dynamic mode */
171     LOG_DEFAULT_LEVEL,          /* log level */
172     "",                         /* no PDUs */
173     "",                         /* diagnostic output to stderr */
174     "tcp:@:9999",               /* default listener port */
175     PROTO_Z3950,                /* default application protocol */
176     60,                         /* idle timeout (minutes) */
177     1024*1024,                  /* maximum PDU size (approx.) to allow */
178     "default-config",           /* configuration name to pass to backend */
179     ""                          /* set user id */
180 };
181
182 /*
183  * handle incoming connect requests.
184  * The dynamic mode is a bit tricky mostly because we want to avoid
185  * doing all of the listening and accepting in the parent - it's
186  * safer that way.
187  */
188 #ifdef WINDOWS
189
190 typedef struct _ThreadList ThreadList;
191
192 typedef struct _ThreadList
193 {
194     HANDLE hThread;
195     IOCHAN pIOChannel;
196     ThreadList *pNext;
197 } ThreadList;
198
199 static ThreadList *pFirstThread;
200 static CRITICAL_SECTION Thread_CritSect;
201 static BOOL bInitialized = FALSE;
202
203 static void ThreadList_Initialize()
204 {
205     /* Initialize the critical Sections */
206      InitializeCriticalSection(&Thread_CritSect);
207
208      /* Set the first thraed */
209      pFirstThread = NULL;
210
211      /* we have been initialized */
212      bInitialized = TRUE;
213 }
214
215 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
216 {
217     /* Only one thread can go through this section at a time */
218     EnterCriticalSection(&Thread_CritSect);
219
220     {
221         /* Lets create our new object */
222         ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
223         pNewThread->hThread = hThread;
224         pNewThread->pIOChannel = pIOChannel;
225         pNewThread->pNext = pFirstThread;
226         pFirstThread = pNewThread;
227
228         /* Lets let somebody else create a new object now */
229         LeaveCriticalSection(&Thread_CritSect);
230     }
231 }
232
233 void statserv_remove(IOCHAN pIOChannel)
234 {
235     /* Only one thread can go through this section at a time */
236     EnterCriticalSection(&Thread_CritSect);
237
238     {
239         ThreadList *pCurrentThread = pFirstThread;
240         ThreadList *pNextThread;
241         ThreadList *pPrevThread =NULL;
242
243         /* Step through alll the threads */
244         for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
245         {
246             /* We only need to compare on the IO Channel */
247             if (pCurrentThread->pIOChannel == pIOChannel)
248             {
249                 /* We have found the thread we want to delete */
250                 /* First of all reset the next pointers */
251                 if (pPrevThread == NULL)
252                     pFirstThread = pCurrentThread->pNext;
253                 else
254                     pPrevThread->pNext = pCurrentThread->pNext;
255
256                 /* All we need todo now is delete the memory */
257                 free(pCurrentThread);
258
259                 /* No need to look at any more threads */
260                 pNextThread = NULL;
261             }
262             else
263             {
264                 /* We need to look at another thread */
265                 pNextThread = pCurrentThread->pNext;
266             }
267         }
268
269         /* Lets let somebody else remove an object now */
270         LeaveCriticalSection(&Thread_CritSect);
271     }
272 }
273
274 void statserv_closedown()
275 {
276     /* Shouldn't do anything if we are not initialized */
277     if (bInitialized)
278     {
279         int iHandles = 0;
280         HANDLE *pThreadHandles = NULL;
281
282         /* We need to stop threads adding and removing while we */
283         /* start the closedown process */
284         EnterCriticalSection(&Thread_CritSect);
285
286         {
287             /* We have exclusive access to the thread stuff now */
288             /* Y didn't i use a semaphore - Oh well never mind */
289             ThreadList *pCurrentThread = pFirstThread;
290
291             /* Before we do anything else, we need to shutdown the listener */
292             if (pListener != NULL)
293                 iochan_destroy(pListener);
294
295             for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
296             {
297                 /* Just destroy the IOCHAN, that should do the trick */
298                 iochan_destroy(pCurrentThread->pIOChannel);
299
300                 /* Keep a running count of our handles */
301                 iHandles++;
302             }
303
304             if (iHandles > 0)
305             {
306                 HANDLE *pCurrentHandle ;
307
308                 /* Allocate the thread handle array */
309                 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
310                 pCurrentHandle = pThreadHandles; 
311
312                 for (pCurrentThread = pFirstThread;
313                      pCurrentThread != NULL;
314                      pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
315                 {
316                     /* Just the handle */
317                     *pCurrentHandle = pCurrentThread->hThread;
318                 }
319             }
320
321             /* We can now leave the critical section */
322             LeaveCriticalSection(&Thread_CritSect);
323         }
324
325         /* Now we can really do something */
326         if (iHandles > 0)
327         {
328             /* This will now wait, until all the threads close */
329             WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
330
331             /* Free the memory we allocated for the handle array */
332             free(pThreadHandles);
333         }
334
335         /* No longer require the critical section, since all threads are dead */
336         DeleteCriticalSection(&Thread_CritSect);
337     }
338 }
339
340 static void listener(IOCHAN h, int event)
341 {
342     COMSTACK line = (COMSTACK) iochan_getdata(h);
343     association *newas;
344     int res;
345     HANDLE NewHandle;
346
347     if (event == EVENT_INPUT)
348     {
349         if ((res = cs_listen(line, 0, 0)) < 0)
350         {
351             logf(LOG_FATAL, "cs_listen failed.");
352             return;
353         }
354         else if (res == 1)
355             return;
356         logf(LOG_DEBUG, "listen ok");
357         iochan_setevent(h, EVENT_OUTPUT);
358         iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
359     }
360     else if (event == EVENT_OUTPUT)
361     {
362         COMSTACK new_line;
363         IOCHAN new_chan;
364         char *a = NULL;
365         DWORD ThreadId;
366
367         if (!(new_line = cs_accept(line)))
368         {
369             logf(LOG_FATAL, "Accept failed.");
370             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
371             return;
372         }
373         logf(LOG_DEBUG, "accept ok");
374
375         if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, EVENT_INPUT)))
376         {
377             logf(LOG_FATAL, "Failed to create iochan");
378             iochan_destroy(h);
379             return;
380         }
381
382         logf(LOG_DEBUG, "accept ok 2");
383         if (!(newas = create_association(new_chan, new_line)))
384         {
385             logf(LOG_FATAL, "Failed to create new assoc.");
386             iochan_destroy(h);
387             return;
388         }
389         logf(LOG_DEBUG, "accept ok 3");
390         iochan_setdata(new_chan, newas);
391         iochan_settimeout(new_chan, control_block.idle_timeout * 60);
392         a = cs_addrstr(new_line);
393         logf(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
394     /* Now what we need todo is create a new thread with this iochan as
395        the parameter */
396     /* if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)event_loop, new_chan,
397            0, &ThreadId) == NULL) */
398     /* Somehow, somewhere we need to store this thread id, otherwise we won't be
399        able to close cleanly */
400         NewHandle = (HANDLE)_beginthreadex(NULL, 0, event_loop, new_chan, 0, &ThreadId);
401         if (NewHandle == (HANDLE)-1)
402         {
403
404             logf(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
405             iochan_destroy(h);
406             return;
407         }
408         /* We successfully created the thread, so add it to the list */
409         statserv_add(NewHandle, new_chan);
410
411         logf(LOG_DEBUG, "Created new thread, iochan %p", new_chan);
412         iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
413     }
414     else
415     {
416         logf(LOG_FATAL, "Bad event on listener.");
417         iochan_destroy(h);
418         return;
419     }
420 }
421
422 #else /* WINDOWS */
423
424 /* To save having an #ifdef in event_loop we need to define this empty function */
425 void statserv_remove(IOCHAN pIOChannel)
426 {
427 }
428
429 void statserv_closedown()
430 {
431     /* We don't need todoanything here - or do we */
432     if (pListener != NULL)
433         iochan_destroy(pListener);
434 }
435
436 static void listener(IOCHAN h, int event)
437 {
438     COMSTACK line = (COMSTACK) iochan_getdata(h);
439     association *newas;
440     static int hand[2];
441     static int child = 0;
442     int res;
443
444     if (event == EVENT_INPUT)
445     {
446         if (control_block.dynamic && !child) 
447         {
448             int res;
449
450             if (pipe(hand) < 0)
451             {
452                 logf(LOG_FATAL|LOG_ERRNO, "pipe");
453                 iochan_destroy(h);
454                 return;
455             }
456             if ((res = fork()) < 0)
457             {
458                 logf(LOG_FATAL|LOG_ERRNO, "fork");
459                 iochan_destroy(h);
460                 return;
461             }
462             else if (res == 0) /* child */
463             {
464                 char nbuf[100];
465                 IOCHAN pp;
466
467                 close(hand[0]);
468                 child = 1;
469                 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
470                 {
471                     if (pp != h)
472                     {
473                         COMSTACK l = iochan_getdata(pp);
474                         cs_close(l);
475                         iochan_destroy(pp);
476                     }
477                 }
478                 sprintf(nbuf, "%s(%d)", me, getpid());
479                 log_init(control_block.loglevel, nbuf, 0);
480             }
481             else /* parent */
482             {
483                 close(hand[1]);
484                 /* wait for child to take the call */
485                 for (;;)
486                 {
487                     char dummy[1];
488                     int res;
489                     
490                     if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
491                     {
492                         logf(LOG_FATAL|LOG_ERRNO, "handshake read");
493                         return;
494                     }
495                     else if (res >= 0)
496                         break;
497                 }
498                 logf(LOG_DEBUG, "P: Child has taken the call");
499                 close(hand[0]);
500                 return;
501             }
502         }
503         if ((res = cs_listen(line, 0, 0)) < 0)
504         {
505             logf(LOG_FATAL, "cs_listen failed.");
506             return;
507         }
508         else if (res == 1)
509             return;
510         logf(LOG_DEBUG, "listen ok");
511         iochan_setevent(h, EVENT_OUTPUT);
512         iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
513     }
514     /* in dynamic mode, only the child ever comes down here */
515     else if (event == EVENT_OUTPUT)
516     {
517         COMSTACK new_line;
518         IOCHAN new_chan;
519         char *a;
520
521         if (!(new_line = cs_accept(line)))
522         {
523             logf(LOG_FATAL, "Accept failed.");
524             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
525             return;
526         }
527         logf(LOG_DEBUG, "accept ok");
528         if (control_block.dynamic)
529         {
530             IOCHAN pp;
531             /* close our half of the listener socket */
532             for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
533             {
534                 COMSTACK l = iochan_getdata(pp);
535                 cs_close(l);
536                 iochan_destroy(pp);
537             }
538             /* release dad */
539             logf(LOG_DEBUG, "Releasing parent");
540             close(hand[1]);
541         }
542         else
543             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
544
545         if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
546             EVENT_INPUT)))
547         {
548             logf(LOG_FATAL, "Failed to create iochan");
549             iochan_destroy(h);
550             return;
551         }
552         if (!(newas = create_association(new_chan, new_line)))
553         {
554             logf(LOG_FATAL, "Failed to create new assoc.");
555             iochan_destroy(h);
556             return;
557         }
558         iochan_setdata(new_chan, newas);
559         iochan_settimeout(new_chan, control_block.idle_timeout * 60);
560         a = cs_addrstr(new_line);
561         logf(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
562     }
563     else
564     {
565         logf(LOG_FATAL, "Bad event on listener.");
566         iochan_destroy(h);
567         return;
568     }
569 }
570
571 #endif /* WINDOWS */
572
573 static void inetd_connection(int what)
574 {
575     COMSTACK line;
576     IOCHAN chan;
577     association *assoc;
578     char *addr;
579
580     if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
581     {
582         if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
583         {
584             if ((assoc = create_association(chan, line)))
585             {
586                 iochan_setdata(chan, assoc);
587                 iochan_settimeout(chan, control_block.idle_timeout * 60);
588                 addr = cs_addrstr(line);
589                 logf(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
590             }
591             else
592             {
593                 logf(LOG_FATAL, "Failed to create association structure");
594             }
595         }
596         else
597         {
598             logf(LOG_FATAL, "Failed to create iochan");
599         }
600     }
601     else
602     {
603         logf(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
604     }
605 }
606
607 /*
608  * Set up a listening endpoint, and give it to the event-handler.
609  */
610 static void add_listener(char *where, int what)
611 {
612     COMSTACK l;
613     CS_TYPE type;
614     char mode[100], addr[100];
615     void *ap;
616     IOCHAN lst = NULL;
617
618     if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
619     {
620         logf (LOG_WARN, "%s: Address format: ('tcp'|'osi')':'<address>", me);
621         return;
622     }
623     if (!strcmp(mode, "tcp"))
624         type = tcpip_type;
625     else if (!strcmp(mode, "osi"))
626     {
627 #ifdef USE_XTIMOSI
628         type = mosi_type;
629 #else
630         logf (LOG_WARN, "OSI Transport not allowed by configuration.");
631         return;
632 #endif
633     }
634     else
635     {
636         logf (LOG_WARN, "You must specify either 'osi:' or 'tcp:'");
637         return;
638     }
639     logf(LOG_LOG, "Adding %s %s listener on %s",
640         control_block.dynamic ? "dynamic" : "static",
641         what == PROTO_SR ? "SR" : "Z3950", where);
642     if (!(l = cs_create(type, 0, what)))
643     {
644         logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
645     }
646     ap = cs_straddr (l, addr);
647     if (!ap)
648     {
649         fprintf(stderr, "Address resolution failed.\n");
650         cs_close (l);
651         return;
652     }
653     if (cs_bind(l, ap, CS_SERVER) < 0)
654     {
655         logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
656     }
657     if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
658          EVENT_EXCEPT)))
659     {
660         logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
661     }
662     iochan_setdata(lst, l);
663
664     /* Ensure our listener chain is setup properly */
665     lst->next = pListener;
666     pListener = lst;
667 }
668
669 #ifndef WINDOWS
670 /* For windows we don't need to catch the signals */
671 static void catchchld(int num)
672 {
673     while (waitpid(-1, 0, WNOHANG) > 0)
674         ;
675     signal(SIGCHLD, catchchld);
676 }
677 #endif /* WINDOWS */
678
679 statserv_options_block *statserv_getcontrol(void)
680 {
681     static statserv_options_block cb;
682
683     memcpy(&cb, &control_block, sizeof(cb));
684     return &cb;
685 }
686
687 void statserv_setcontrol(statserv_options_block *block)
688 {
689     memcpy(&control_block, block, sizeof(*block));
690 }
691
692 int statserv_main(int argc, char **argv)
693 {
694     int ret, listeners = 0, inetd = 0, r;
695     char *arg;
696     int protocol = control_block.default_proto;
697
698 #ifdef WINDOWS
699     /* We need to initialize the thread list */
700     ThreadList_Initialize();
701 #endif /* WINDOWS */
702
703     me = argv[0];
704     while ((ret = options("a:iszSl:v:u:c:w:t:k:", argv, argc, &arg)) != -2)
705     {
706         switch (ret)
707         {
708             case 0:
709                     add_listener(arg, protocol);
710                     listeners++;
711                 break;
712             case 'z': protocol = PROTO_Z3950; break;
713             case 's': protocol = PROTO_SR; break;
714             case 'S': control_block.dynamic = 0; break;
715             case 'l':
716                 strcpy(control_block.logfile, arg ? arg : "");
717                 log_init(control_block.loglevel, me, control_block.logfile);
718                 break;
719             case 'v':
720                 control_block.loglevel = log_mask_str(arg);
721                 log_init(control_block.loglevel, me, control_block.logfile);
722                 break;
723             case 'a':
724                 strcpy(control_block.apdufile, arg ? arg : ""); break;
725             case 'u':
726                 strcpy(control_block.setuid, arg ? arg : ""); break;
727             case 'c':
728                 strcpy(control_block.configname, arg ? arg : ""); break;
729             case 't':
730                 if (!arg || !(r = atoi(arg)))
731                 {
732                     fprintf(stderr, "%s: Specify positive timeout for -t.\n",
733                         me);
734                     return(1);
735                 }
736                 control_block.idle_timeout = r;
737                 break;
738             case  'k':
739                 if (!arg || !(r = atoi(arg)))
740                 {
741                     fprintf(stderr, "%s: Specify positive timeout for -t.\n",
742                         me);
743                     return(1);
744                 }
745                 control_block.maxrecordsize = r * 1024;
746                 break;
747             case 'i':
748                 inetd = 1; break;
749             case 'w':
750                 if (chdir(arg))
751                 {
752                     perror(arg);
753
754                     return(1);
755                 }
756                 break;
757             default:
758                 fprintf(stderr, "Usage: %s [ -i -a <pdufile> -v <loglevel>"
759                         " -l <logfile> -u <user> -c <config> -t <minutes>"
760                         " -k <kilobytes>"
761                         " -zsS <listener-addr> -w <directory> ... ]\n", me);
762                 return(1);
763             }
764     }
765
766 #if 0
767     log_init(control_block.loglevel, NULL, control_block.logfile);
768 #endif /* WINDOWS */
769
770     if ((pListener == NULL) && *control_block.default_listen)
771             add_listener(control_block.default_listen, protocol);
772
773 #ifndef WINDOWS
774     if (inetd)
775         inetd_connection(protocol);
776     else
777     {
778         if (control_block.dynamic)
779             signal(SIGCHLD, catchchld);
780     }
781     if (*control_block.setuid)
782     {
783         struct passwd *pw;
784         
785         if (!(pw = getpwnam(control_block.setuid)))
786         {
787             logf(LOG_FATAL, "%s: Unknown user", control_block.setuid);
788             return(1);
789         }
790         if (setuid(pw->pw_uid) < 0)
791         {
792             logf(LOG_FATAL|LOG_ERRNO, "setuid");
793             exit(1);
794         }
795     }
796 #endif /* WINDOWS */
797
798     logf(LOG_LOG, "Entering event loop.");
799         
800     if (pListener == NULL)
801         return(1);
802     else
803         return event_loop(pListener);
804 }