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