1 /*	$NetBSD: single_server.c,v 1.1.1.2 2010/06/17 18:06:55 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	single_server 3
6 /* SUMMARY
7 /*	skeleton single-threaded mail subsystem
8 /* SYNOPSIS
9 /*	#include <mail_server.h>
10 /*
11 /*	NORETURN single_server_main(argc, argv, service, key, value, ...)
12 /*	int	argc;
13 /*	char	**argv;
14 /*	void	(*service)(VSTREAM *stream, char *service_name, char **argv);
15 /*	int	key;
16 /* DESCRIPTION
17 /*	This module implements a skeleton for single-threaded
18 /*	mail subsystems: mail subsystem programs that service one
19 /*	client at a time. The resulting program expects to be run
20 /*	from the \fBmaster\fR process.
21 /*
22 /*	single_server_main() is the skeleton entry point. It should be
23 /*	called from the application main program.  The skeleton does the
24 /*	generic command-line options processing, initialization of
25 /*	configurable parameters, and connection management.
26 /*	The skeleton never returns.
27 /*
28 /*	Arguments:
29 /* .IP "void (*service)(VSTREAM *fp, char *service_name, char **argv)"
30 /*	A pointer to a function that is called by the skeleton each time
31 /*	a client connects to the program's service port. The function is
32 /*	run after the program has irrevocably dropped its privileges.
33 /*	The stream initial state is non-blocking mode.
34 /*	The service name argument corresponds to the service name in the
35 /*	master.cf file.
36 /*	The argv argument specifies command-line arguments left over
37 /*	after options processing.
38 /* .PP
39 /*	Optional arguments are specified as a null-terminated (key, value)
40 /*	list. Keys and expected values are:
41 /* .IP "MAIL_SERVER_INT_TABLE (CONFIG_INT_TABLE *)"
42 /*	A table with configurable parameters, to be loaded from the
43 /*	global Postfix configuration file. Tables are loaded in the
44 /*	order as specified, and multiple instances of the same type
45 /*	are allowed.
46 /* .IP "MAIL_SERVER_STR_TABLE (CONFIG_STR_TABLE *)"
47 /*	A table with configurable parameters, to be loaded from the
48 /*	global Postfix configuration file. Tables are loaded in the
49 /*	order as specified, and multiple instances of the same type
50 /*	are allowed.
51 /* .IP "MAIL_SERVER_BOOL_TABLE (CONFIG_BOOL_TABLE *)"
52 /*	A table with configurable parameters, to be loaded from the
53 /*	global Postfix configuration file. Tables are loaded in the
54 /*	order as specified, and multiple instances of the same type
55 /*	are allowed.
56 /* .IP "MAIL_SERVER_TIME_TABLE (CONFIG_TIME_TABLE *)"
57 /*	A table with configurable parameters, to be loaded from the
58 /*	global Postfix configuration file. Tables are loaded in the
59 /*	order as specified, and multiple instances of the same type
60 /*	are allowed.
61 /* .IP "MAIL_SERVER_RAW_TABLE (CONFIG_RAW_TABLE *)"
62 /*	A table with configurable parameters, to be loaded from the
63 /*	global Postfix configuration file. Tables are loaded in the
64 /*	order as specified, and multiple instances of the same type
65 /*	are allowed. Raw parameters are not subjected to $name
66 /*	evaluation.
67 /* .IP "MAIL_SERVER_NINT_TABLE (CONFIG_NINT_TABLE *)"
68 /*	A table with configurable parameters, to be loaded from the
69 /*	global Postfix configuration file. Tables are loaded in the
70 /*	order as specified, and multiple instances of the same type
71 /*	are allowed.
72 /* .IP "MAIL_SERVER_PRE_INIT (void *(char *service_name, char **argv))"
73 /*	A pointer to a function that is called once
74 /*	by the skeleton after it has read the global configuration file
75 /*	and after it has processed command-line arguments, but before
76 /*	the skeleton has optionally relinquished the process privileges.
77 /* .sp
78 /*	Only the last instance of this parameter type is remembered.
79 /* .IP "MAIL_SERVER_POST_INIT (void *(char *service_name, char **argv))"
80 /*	A pointer to a function that is called once
81 /*	by the skeleton after it has optionally relinquished the process
82 /*	privileges, but before servicing client connection requests.
83 /* .sp
84 /*	Only the last instance of this parameter type is remembered.
85 /* .IP "MAIL_SERVER_LOOP (int *(char *service_name, char **argv))"
86 /*	A pointer to function that is executed from
87 /*	within the event loop, whenever an I/O or timer event has happened,
88 /*	or whenever nothing has happened for a specified amount of time.
89 /*	The result value of the function specifies how long to wait until
90 /*	the next event. Specify -1 to wait for "as long as it takes".
91 /* .sp
92 /*	Only the last instance of this parameter type is remembered.
93 /* .IP "MAIL_SERVER_EXIT (void *(void))"
94 /*	A pointer to function that is executed immediately before normal
95 /*	process termination.
96 /* .sp
97 /*	Only the last instance of this parameter type is remembered.
98 /* .IP "MAIL_SERVER_PRE_ACCEPT (void *(char *service_name, char **argv))"
99 /*	Function to be executed prior to accepting a new connection.
100 /* .sp
101 /*	Only the last instance of this parameter type is remembered.
102 /* .IP "MAIL_SERVER_IN_FLOW_DELAY (none)"
103 /*	Pause $in_flow_delay seconds when no "mail flow control token"
104 /*	is available. A token is consumed for each connection request.
105 /* .IP MAIL_SERVER_SOLITARY
106 /*	This service must be configured with process limit of 1.
107 /* .IP MAIL_SERVER_UNLIMITED
108 /*	This service must be configured with process limit of 0.
109 /* .IP MAIL_SERVER_PRIVILEGED
110 /*	This service must be configured as privileged.
111 /* .PP
112 /*	The var_use_limit variable limits the number of clients that
113 /*	a server can service before it commits suicide.
114 /*	This value is taken from the global \fBmain.cf\fR configuration
115 /*	file. Setting \fBvar_idle_limit\fR to zero disables the client limit.
116 /*
117 /*	The var_idle_limit variable limits the time that a service
118 /*	receives no client connection requests before it commits suicide.
119 /*	This value is taken from the global \fBmain.cf\fR configuration
120 /*	file. Setting \fBvar_use_limit\fR to zero disables the idle limit.
121 /* DIAGNOSTICS
122 /*	Problems and transactions are logged to \fBsyslogd\fR(8).
123 /* BUGS
124 /* SEE ALSO
125 /*	master(8), master process
126 /*	syslogd(8) system logging
127 /* LICENSE
128 /* .ad
129 /* .fi
130 /*	The Secure Mailer license must be distributed with this software.
131 /* AUTHOR(S)
132 /*	Wietse Venema
133 /*	IBM T.J. Watson Research
134 /*	P.O. Box 704
135 /*	Yorktown Heights, NY 10598, USA
136 /*--*/
137 
138 /* System library. */
139 
140 #include <sys_defs.h>
141 #include <sys/socket.h>
142 #include <unistd.h>
143 #include <signal.h>
144 #include <syslog.h>
145 #include <stdlib.h>
146 #include <limits.h>
147 #include <string.h>
148 #include <errno.h>
149 #include <fcntl.h>
150 #include <stdarg.h>
151 #ifdef STRCASECMP_IN_STRINGS_H
152 #include <strings.h>
153 #endif
154 #include <time.h>
155 
156 /* Utility library. */
157 
158 #include <msg.h>
159 #include <msg_syslog.h>
160 #include <msg_vstream.h>
161 #include <chroot_uid.h>
162 #include <vstring.h>
163 #include <vstream.h>
164 #include <msg_vstream.h>
165 #include <mymalloc.h>
166 #include <events.h>
167 #include <iostuff.h>
168 #include <stringops.h>
169 #include <sane_accept.h>
170 #include <myflock.h>
171 #include <safe_open.h>
172 #include <listen.h>
173 #include <watchdog.h>
174 #include <split_at.h>
175 
176 /* Global library. */
177 
178 #include <mail_params.h>
179 #include <mail_task.h>
180 #include <debug_process.h>
181 #include <mail_conf.h>
182 #include <mail_dict.h>
183 #include <timed_ipc.h>
184 #include <resolve_local.h>
185 #include <mail_flow.h>
186 
187 /* Process manager. */
188 
189 #include "master_proto.h"
190 
191 /* Application-specific */
192 
193 #include "mail_server.h"
194 
195  /*
196   * Global state.
197   */
198 static int use_count;
199 
200 static void (*single_server_service) (VSTREAM *, char *, char **);
201 static char *single_server_name;
202 static char **single_server_argv;
203 static void (*single_server_accept) (int, char *);
204 static void (*single_server_onexit) (char *, char **);
205 static void (*single_server_pre_accept) (char *, char **);
206 static VSTREAM *single_server_lock;
207 static int single_server_in_flow_delay;
208 static unsigned single_server_generation;
209 
210 /* single_server_exit - normal termination */
211 
212 static NORETURN single_server_exit(void)
213 {
214     if (single_server_onexit)
215 	single_server_onexit(single_server_name, single_server_argv);
216     exit(0);
217 }
218 
219 /* single_server_abort - terminate after abnormal master exit */
220 
221 static void single_server_abort(int unused_event, char *unused_context)
222 {
223     if (msg_verbose)
224 	msg_info("master disconnect -- exiting");
225     single_server_exit();
226 }
227 
228 /* single_server_timeout - idle time exceeded */
229 
230 static void single_server_timeout(int unused_event, char *unused_context)
231 {
232     if (msg_verbose)
233 	msg_info("idle timeout -- exiting");
234     single_server_exit();
235 }
236 
237 /* single_server_wakeup - wake up application */
238 
239 static void single_server_wakeup(int fd)
240 {
241     VSTREAM *stream;
242     char   *tmp;
243 
244     /*
245      * If the accept() succeeds, be sure to disable non-blocking I/O, because
246      * the application is supposed to be single-threaded. Notice the master
247      * of our (un)availability to service connection requests. Commit suicide
248      * when the master process disconnected from us. Don't drop the already
249      * accepted client request after "postfix reload"; that would be rude.
250      */
251     if (msg_verbose)
252 	msg_info("connection established");
253     non_blocking(fd, BLOCKING);
254     close_on_exec(fd, CLOSE_ON_EXEC);
255     stream = vstream_fdopen(fd, O_RDWR);
256     tmp = concatenate(single_server_name, " socket", (char *) 0);
257     vstream_control(stream, VSTREAM_CTL_PATH, tmp, VSTREAM_CTL_END);
258     myfree(tmp);
259     timed_ipc_setup(stream);
260     if (master_notify(var_pid, single_server_generation, MASTER_STAT_TAKEN) < 0)
261 	 /* void */ ;
262     if (single_server_in_flow_delay && mail_flow_get(1) < 0)
263 	doze(var_in_flow_delay * 1000000);
264     single_server_service(stream, single_server_name, single_server_argv);
265     (void) vstream_fclose(stream);
266     if (master_notify(var_pid, single_server_generation, MASTER_STAT_AVAIL) < 0)
267 	single_server_abort(EVENT_NULL_TYPE, EVENT_NULL_CONTEXT);
268     if (msg_verbose)
269 	msg_info("connection closed");
270     /* Avoid integer wrap-around in a persistent process.  */
271     if (use_count < INT_MAX)
272 	use_count++;
273     if (var_idle_limit > 0)
274 	event_request_timer(single_server_timeout, (char *) 0, var_idle_limit);
275 }
276 
277 /* single_server_accept_local - accept client connection request */
278 
279 static void single_server_accept_local(int unused_event, char *context)
280 {
281     int     listen_fd = CAST_CHAR_PTR_TO_INT(context);
282     int     time_left = -1;
283     int     fd;
284 
285     /*
286      * Be prepared for accept() to fail because some other process already
287      * got the connection. We use select() + accept(), instead of simply
288      * blocking in accept(), because we must be able to detect that the
289      * master process has gone away unexpectedly.
290      */
291     if (var_idle_limit > 0)
292 	time_left = event_cancel_timer(single_server_timeout, (char *) 0);
293 
294     if (single_server_pre_accept)
295 	single_server_pre_accept(single_server_name, single_server_argv);
296     fd = LOCAL_ACCEPT(listen_fd);
297     if (single_server_lock != 0
298 	&& myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
299 		   MYFLOCK_OP_NONE) < 0)
300 	msg_fatal("select unlock: %m");
301     if (fd < 0) {
302 	if (errno != EAGAIN)
303 	    msg_error("accept connection: %m");
304 	if (time_left >= 0)
305 	    event_request_timer(single_server_timeout, (char *) 0, time_left);
306 	return;
307     }
308     single_server_wakeup(fd);
309 }
310 
311 #ifdef MASTER_XPORT_NAME_PASS
312 
313 /* single_server_accept_pass - accept descriptor */
314 
315 static void single_server_accept_pass(int unused_event, char *context)
316 {
317     int     listen_fd = CAST_CHAR_PTR_TO_INT(context);
318     int     time_left = -1;
319     int     fd;
320 
321     /*
322      * Be prepared for accept() to fail because some other process already
323      * got the connection. We use select() + accept(), instead of simply
324      * blocking in accept(), because we must be able to detect that the
325      * master process has gone away unexpectedly.
326      */
327     if (var_idle_limit > 0)
328 	time_left = event_cancel_timer(single_server_timeout, (char *) 0);
329 
330     if (single_server_pre_accept)
331 	single_server_pre_accept(single_server_name, single_server_argv);
332     fd = PASS_ACCEPT(listen_fd);
333     if (single_server_lock != 0
334 	&& myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
335 		   MYFLOCK_OP_NONE) < 0)
336 	msg_fatal("select unlock: %m");
337     if (fd < 0) {
338 	if (errno != EAGAIN)
339 	    msg_error("accept connection: %m");
340 	if (time_left >= 0)
341 	    event_request_timer(single_server_timeout, (char *) 0, time_left);
342 	return;
343     }
344     single_server_wakeup(fd);
345 }
346 
347 #endif
348 
349 /* single_server_accept_inet - accept client connection request */
350 
351 static void single_server_accept_inet(int unused_event, char *context)
352 {
353     int     listen_fd = CAST_CHAR_PTR_TO_INT(context);
354     int     time_left = -1;
355     int     fd;
356 
357     /*
358      * Be prepared for accept() to fail because some other process already
359      * got the connection. We use select() + accept(), instead of simply
360      * blocking in accept(), because we must be able to detect that the
361      * master process has gone away unexpectedly.
362      */
363     if (var_idle_limit > 0)
364 	time_left = event_cancel_timer(single_server_timeout, (char *) 0);
365 
366     if (single_server_pre_accept)
367 	single_server_pre_accept(single_server_name, single_server_argv);
368     fd = inet_accept(listen_fd);
369     if (single_server_lock != 0
370 	&& myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
371 		   MYFLOCK_OP_NONE) < 0)
372 	msg_fatal("select unlock: %m");
373     if (fd < 0) {
374 	if (errno != EAGAIN)
375 	    msg_error("accept connection: %m");
376 	if (time_left >= 0)
377 	    event_request_timer(single_server_timeout, (char *) 0, time_left);
378 	return;
379     }
380     single_server_wakeup(fd);
381 }
382 
383 /* single_server_main - the real main program */
384 
385 NORETURN single_server_main(int argc, char **argv, SINGLE_SERVER_FN service,...)
386 {
387     const char *myname = "single_server_main";
388     VSTREAM *stream = 0;
389     char   *root_dir = 0;
390     char   *user_name = 0;
391     int     debug_me = 0;
392     int     daemon_mode = 1;
393     char   *service_name = basename(argv[0]);
394     int     delay;
395     int     c;
396     int     socket_count = 1;
397     int     fd;
398     va_list ap;
399     MAIL_SERVER_INIT_FN pre_init = 0;
400     MAIL_SERVER_INIT_FN post_init = 0;
401     MAIL_SERVER_LOOP_FN loop = 0;
402     int     key;
403     char   *transport = 0;
404     char   *lock_path;
405     VSTRING *why;
406     int     alone = 0;
407     int     zerolimit = 0;
408     WATCHDOG *watchdog;
409     char   *oname_val;
410     char   *oname;
411     char   *oval;
412     const char *err;
413     char   *generation;
414     int     msg_vstream_needed = 0;
415     int     redo_syslog_init = 0;
416 
417     /*
418      * Process environment options as early as we can.
419      */
420     if (getenv(CONF_ENV_VERB))
421 	msg_verbose = 1;
422     if (getenv(CONF_ENV_DEBUG))
423 	debug_me = 1;
424 
425     /*
426      * Don't die when a process goes away unexpectedly.
427      */
428     signal(SIGPIPE, SIG_IGN);
429 
430     /*
431      * Don't die for frivolous reasons.
432      */
433 #ifdef SIGXFSZ
434     signal(SIGXFSZ, SIG_IGN);
435 #endif
436 
437     /*
438      * May need this every now and then.
439      */
440     var_procname = mystrdup(basename(argv[0]));
441     set_mail_conf_str(VAR_PROCNAME, var_procname);
442 
443     /*
444      * Initialize logging and exit handler. Do the syslog first, so that its
445      * initialization completes before we enter the optional chroot jail.
446      */
447     msg_syslog_init(mail_task(var_procname), LOG_PID, LOG_FACILITY);
448     if (msg_verbose)
449 	msg_info("daemon started");
450 
451     /*
452      * Initialize from the configuration file. Allow command-line options to
453      * override compiled-in defaults or configured parameter values.
454      */
455     mail_conf_suck();
456 
457     /*
458      * Register dictionaries that use higher-level interfaces and protocols.
459      */
460     mail_dict_init();
461 
462     /*
463      * Pick up policy settings from master process. Shut up error messages to
464      * stderr, because no-one is going to see them.
465      */
466     opterr = 0;
467     while ((c = GETOPT(argc, argv, "cdDi:lm:n:o:s:St:uvVz")) > 0) {
468 	switch (c) {
469 	case 'c':
470 	    root_dir = "setme";
471 	    break;
472 	case 'd':
473 	    daemon_mode = 0;
474 	    break;
475 	case 'D':
476 	    debug_me = 1;
477 	    break;
478 	case 'i':
479 	    mail_conf_update(VAR_MAX_IDLE, optarg);
480 	    break;
481 	case 'l':
482 	    alone = 1;
483 	    break;
484 	case 'm':
485 	    mail_conf_update(VAR_MAX_USE, optarg);
486 	    break;
487 	case 'n':
488 	    service_name = optarg;
489 	    break;
490 	case 'o':
491 	    oname_val = mystrdup(optarg);
492 	    if ((err = split_nameval(oname_val, &oname, &oval)) != 0)
493 		msg_fatal("invalid \"-o %s\" option value: %s", optarg, err);
494 	    mail_conf_update(oname, oval);
495 	    if (strcmp(oname, VAR_SYSLOG_NAME) == 0)
496 		redo_syslog_init = 1;
497 	    myfree(oname_val);
498 	    break;
499 	case 's':
500 	    if ((socket_count = atoi(optarg)) <= 0)
501 		msg_fatal("invalid socket_count: %s", optarg);
502 	    break;
503 	case 'S':
504 	    stream = VSTREAM_IN;
505 	    break;
506 	case 'u':
507 	    user_name = "setme";
508 	    break;
509 	case 't':
510 	    transport = optarg;
511 	    break;
512 	case 'v':
513 	    msg_verbose++;
514 	    break;
515 	case 'V':
516 	    if (++msg_vstream_needed == 1)
517 		msg_vstream_init(mail_task(var_procname), VSTREAM_ERR);
518 	    break;
519 	case 'z':
520 	    zerolimit = 1;
521 	    break;
522 	default:
523 	    msg_fatal("invalid option: %c", c);
524 	    break;
525 	}
526     }
527 
528     /*
529      * Initialize generic parameters.
530      */
531     mail_params_init();
532     if (redo_syslog_init)
533 	msg_syslog_init(mail_task(var_procname), LOG_PID, LOG_FACILITY);
534 
535     /*
536      * If not connected to stdin, stdin must not be a terminal.
537      */
538     if (daemon_mode && stream == 0 && isatty(STDIN_FILENO)) {
539 	msg_vstream_init(var_procname, VSTREAM_ERR);
540 	msg_fatal("do not run this command by hand");
541     }
542 
543     /*
544      * Application-specific initialization.
545      */
546     va_start(ap, service);
547     while ((key = va_arg(ap, int)) != 0) {
548 	switch (key) {
549 	case MAIL_SERVER_INT_TABLE:
550 	    get_mail_conf_int_table(va_arg(ap, CONFIG_INT_TABLE *));
551 	    break;
552 	case MAIL_SERVER_STR_TABLE:
553 	    get_mail_conf_str_table(va_arg(ap, CONFIG_STR_TABLE *));
554 	    break;
555 	case MAIL_SERVER_BOOL_TABLE:
556 	    get_mail_conf_bool_table(va_arg(ap, CONFIG_BOOL_TABLE *));
557 	    break;
558 	case MAIL_SERVER_TIME_TABLE:
559 	    get_mail_conf_time_table(va_arg(ap, CONFIG_TIME_TABLE *));
560 	    break;
561 	case MAIL_SERVER_RAW_TABLE:
562 	    get_mail_conf_raw_table(va_arg(ap, CONFIG_RAW_TABLE *));
563 	    break;
564 	case MAIL_SERVER_NINT_TABLE:
565 	    get_mail_conf_nint_table(va_arg(ap, CONFIG_NINT_TABLE *));
566 	    break;
567 	case MAIL_SERVER_PRE_INIT:
568 	    pre_init = va_arg(ap, MAIL_SERVER_INIT_FN);
569 	    break;
570 	case MAIL_SERVER_POST_INIT:
571 	    post_init = va_arg(ap, MAIL_SERVER_INIT_FN);
572 	    break;
573 	case MAIL_SERVER_LOOP:
574 	    loop = va_arg(ap, MAIL_SERVER_LOOP_FN);
575 	    break;
576 	case MAIL_SERVER_EXIT:
577 	    single_server_onexit = va_arg(ap, MAIL_SERVER_EXIT_FN);
578 	    break;
579 	case MAIL_SERVER_PRE_ACCEPT:
580 	    single_server_pre_accept = va_arg(ap, MAIL_SERVER_ACCEPT_FN);
581 	    break;
582 	case MAIL_SERVER_IN_FLOW_DELAY:
583 	    single_server_in_flow_delay = 1;
584 	    break;
585 	case MAIL_SERVER_SOLITARY:
586 	    if (stream == 0 && !alone)
587 		msg_fatal("service %s requires a process limit of 1",
588 			  service_name);
589 	    break;
590 	case MAIL_SERVER_UNLIMITED:
591 	    if (stream == 0 && !zerolimit)
592 		msg_fatal("service %s requires a process limit of 0",
593 			  service_name);
594 	    break;
595 	case MAIL_SERVER_PRIVILEGED:
596 	    if (user_name)
597 		msg_fatal("service %s requires privileged operation",
598 			  service_name);
599 	    break;
600 	default:
601 	    msg_panic("%s: unknown argument type: %d", myname, key);
602 	}
603     }
604     va_end(ap);
605 
606     if (root_dir)
607 	root_dir = var_queue_dir;
608     if (user_name)
609 	user_name = var_mail_owner;
610 
611     /*
612      * Can options be required?
613      */
614     if (stream == 0) {
615 	if (transport == 0)
616 	    msg_fatal("no transport type specified");
617 	if (strcasecmp(transport, MASTER_XPORT_NAME_INET) == 0)
618 	    single_server_accept = single_server_accept_inet;
619 	else if (strcasecmp(transport, MASTER_XPORT_NAME_UNIX) == 0)
620 	    single_server_accept = single_server_accept_local;
621 #ifdef MASTER_XPORT_NAME_PASS
622 	else if (strcasecmp(transport, MASTER_XPORT_NAME_PASS) == 0)
623 	    single_server_accept = single_server_accept_pass;
624 #endif
625 	else
626 	    msg_fatal("unsupported transport type: %s", transport);
627     }
628 
629     /*
630      * Retrieve process generation from environment.
631      */
632     if ((generation = getenv(MASTER_GEN_NAME)) != 0) {
633 	if (!alldig(generation))
634 	    msg_fatal("bad generation: %s", generation);
635 	OCTAL_TO_UNSIGNED(single_server_generation, generation);
636 	if (msg_verbose)
637 	    msg_info("process generation: %s (%o)",
638 		     generation, single_server_generation);
639     }
640 
641     /*
642      * Optionally start the debugger on ourself.
643      */
644     if (debug_me)
645 	debug_process();
646 
647     /*
648      * Traditionally, BSD select() can't handle multiple processes selecting
649      * on the same socket, and wakes up every process in select(). See TCP/IP
650      * Illustrated volume 2 page 532. We avoid select() collisions with an
651      * external lock file.
652      */
653     if (stream == 0 && !alone) {
654 	lock_path = concatenate(DEF_PID_DIR, "/", transport,
655 				".", service_name, (char *) 0);
656 	why = vstring_alloc(1);
657 	if ((single_server_lock = safe_open(lock_path, O_CREAT | O_RDWR, 0600,
658 				      (struct stat *) 0, -1, -1, why)) == 0)
659 	    msg_fatal("open lock file %s: %s", lock_path, vstring_str(why));
660 	close_on_exec(vstream_fileno(single_server_lock), CLOSE_ON_EXEC);
661 	myfree(lock_path);
662 	vstring_free(why);
663     }
664 
665     /*
666      * Set up call-back info.
667      */
668     single_server_service = service;
669     single_server_name = service_name;
670     single_server_argv = argv + optind;
671 
672     /*
673      * Run pre-jail initialization.
674      */
675     if (chdir(var_queue_dir) < 0)
676 	msg_fatal("chdir(\"%s\"): %m", var_queue_dir);
677     if (pre_init)
678 	pre_init(single_server_name, single_server_argv);
679 
680     /*
681      * Optionally, restrict the damage that this process can do.
682      */
683     resolve_local_init();
684     tzset();
685     chroot_uid(root_dir, user_name);
686 
687     /*
688      * Run post-jail initialization.
689      */
690     if (post_init)
691 	post_init(single_server_name, single_server_argv);
692 
693     /*
694      * Are we running as a one-shot server with the client connection on
695      * standard input? If so, make sure the output is written to stdout so as
696      * to satisfy common expectation.
697      */
698     if (stream != 0) {
699 	vstream_control(stream,
700 			VSTREAM_CTL_DOUBLE,
701 			VSTREAM_CTL_WRITE_FD, STDOUT_FILENO,
702 			VSTREAM_CTL_END);
703 	service(stream, single_server_name, single_server_argv);
704 	vstream_fflush(stream);
705 	single_server_exit();
706     }
707 
708     /*
709      * Running as a semi-resident server. Service connection requests.
710      * Terminate when we have serviced a sufficient number of clients, when
711      * no-one has been talking to us for a configurable amount of time, or
712      * when the master process terminated abnormally.
713      */
714     if (var_idle_limit > 0)
715 	event_request_timer(single_server_timeout, (char *) 0, var_idle_limit);
716     for (fd = MASTER_LISTEN_FD; fd < MASTER_LISTEN_FD + socket_count; fd++) {
717 	event_enable_read(fd, single_server_accept, CAST_INT_TO_CHAR_PTR(fd));
718 	close_on_exec(fd, CLOSE_ON_EXEC);
719     }
720     event_enable_read(MASTER_STATUS_FD, single_server_abort, (char *) 0);
721     close_on_exec(MASTER_STATUS_FD, CLOSE_ON_EXEC);
722     close_on_exec(MASTER_FLOW_READ, CLOSE_ON_EXEC);
723     close_on_exec(MASTER_FLOW_WRITE, CLOSE_ON_EXEC);
724     watchdog = watchdog_create(var_daemon_timeout, (WATCHDOG_FN) 0, (char *) 0);
725 
726     /*
727      * The event loop, at last.
728      */
729     while (var_use_limit == 0 || use_count < var_use_limit) {
730 	if (single_server_lock != 0) {
731 	    watchdog_stop(watchdog);
732 	    if (myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
733 			MYFLOCK_OP_EXCLUSIVE) < 0)
734 		msg_fatal("select lock: %m");
735 	}
736 	watchdog_start(watchdog);
737 	delay = loop ? loop(single_server_name, single_server_argv) : -1;
738 	event_loop(delay);
739     }
740     single_server_exit();
741 }
742