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