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