Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / master / multi_server.c
blob34999e3686090a5513557f9cb80084e986f6e83f
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* multi_server 3
6 /* SUMMARY
7 /* skeleton multi-threaded mail subsystem
8 /* SYNOPSIS
9 /* #include <mail_server.h>
11 /* NORETURN multi_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;
17 /* void multi_server_disconnect(stream)
18 /* VSTREAM *stream;
20 /* void multi_server_drain()
21 /* DESCRIPTION
22 /* This module implements a skeleton for multi-threaded
23 /* mail subsystems: mail subsystem programs that service multiple
24 /* clients at the same time. The resulting program expects to be run
25 /* from the \fBmaster\fR process.
27 /* multi_server_main() is the skeleton entry point. It should be
28 /* called from the application main program. The skeleton does all
29 /* the generic command-line processing, initialization of
30 /* configurable parameters, and connection management.
31 /* The skeleton never returns.
33 /* Arguments:
34 /* .IP "void (*service)(VSTREAM *stream, char *service_name, char **argv)"
35 /* A pointer to a function that is called by the skeleton each
36 /* time a client sends data to the program's service port. The
37 /* function is run after the program has optionally dropped its
38 /* privileges. This function should not attempt to preserve state
39 /* across calls. The stream initial state is non-blocking mode.
40 /* The service name argument corresponds to the service name in the
41 /* master.cf file.
42 /* The argv argument specifies command-line arguments left over
43 /* after options processing.
44 /* .PP
45 /* Optional arguments are specified as a null-terminated (key, value)
46 /* list. Keys and expected values are:
47 /* .IP "MAIL_SERVER_INT_TABLE (CONFIG_INT_TABLE *)"
48 /* A table with configurable parameters, to be loaded from the
49 /* global Postfix configuration file. Tables are loaded in the
50 /* order as specified, and multiple instances of the same type
51 /* are allowed.
52 /* .IP "MAIL_SERVER_STR_TABLE (CONFIG_STR_TABLE *)"
53 /* A table with configurable parameters, to be loaded from the
54 /* global Postfix configuration file. Tables are loaded in the
55 /* order as specified, and multiple instances of the same type
56 /* are allowed.
57 /* .IP "MAIL_SERVER_BOOL_TABLE (CONFIG_BOOL_TABLE *)"
58 /* A table with configurable parameters, to be loaded from the
59 /* global Postfix configuration file. Tables are loaded in the
60 /* order as specified, and multiple instances of the same type
61 /* are allowed.
62 /* .IP "MAIL_SERVER_TIME_TABLE (CONFIG_TIME_TABLE *)"
63 /* A table with configurable parameters, to be loaded from the
64 /* global Postfix configuration file. Tables are loaded in the
65 /* order as specified, and multiple instances of the same type
66 /* are allowed.
67 /* .IP "MAIL_SERVER_RAW_TABLE (CONFIG_RAW_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. Raw parameters are not subjected to $name
72 /* evaluation.
73 /* .IP "MAIL_SERVER_NINT_TABLE (CONFIG_NINT_TABLE *)"
74 /* A table with configurable parameters, to be loaded from the
75 /* global Postfix configuration file. Tables are loaded in the
76 /* order as specified, and multiple instances of the same type
77 /* are allowed.
78 /* .IP "MAIL_SERVER_PRE_INIT (void *(char *service_name, char **argv))"
79 /* A pointer to a function that is called once
80 /* by the skeleton after it has read the global configuration file
81 /* and after it has processed command-line arguments, but before
82 /* the skeleton has optionally relinquished the process privileges.
83 /* .sp
84 /* Only the last instance of this parameter type is remembered.
85 /* .IP "MAIL_SERVER_POST_INIT (void *(char *service_name, char **argv))"
86 /* A pointer to a function that is called once
87 /* by the skeleton after it has optionally relinquished the process
88 /* privileges, but before servicing client connection requests.
89 /* .sp
90 /* Only the last instance of this parameter type is remembered.
91 /* .IP "MAIL_SERVER_LOOP (int *(char *service_name, char **argv))"
92 /* A pointer to function that is executed from
93 /* within the event loop, whenever an I/O or timer event has happened,
94 /* or whenever nothing has happened for a specified amount of time.
95 /* The result value of the function specifies how long to wait until
96 /* the next event. Specify -1 to wait for "as long as it takes".
97 /* .sp
98 /* Only the last instance of this parameter type is remembered.
99 /* .IP "MAIL_SERVER_EXIT (void *(char *service_name, char **argv))"
100 /* A pointer to function that is executed immediately before normal
101 /* process termination.
102 /* .IP "MAIL_SERVER_PRE_ACCEPT (void *(char *service_name, char **argv))"
103 /* Function to be executed prior to accepting a new connection.
104 /* .sp
105 /* Only the last instance of this parameter type is remembered.
106 /* .IP "MAIL_SERVER_PRE_DISCONN (VSTREAM *, char *service_name, char **argv)"
107 /* A pointer to a function that is called
108 /* by the multi_server_disconnect() function (see below).
109 /* .sp
110 /* Only the last instance of this parameter type is remembered.
111 /* .IP "MAIL_SERVER_IN_FLOW_DELAY (none)"
112 /* Pause $in_flow_delay seconds when no "mail flow control token"
113 /* is available. A token is consumed for each connection request.
114 /* .IP MAIL_SERVER_SOLITARY
115 /* This service must be configured with process limit of 1.
116 /* .IP MAIL_SERVER_UNLIMITED
117 /* This service must be configured with process limit of 0.
118 /* .IP MAIL_SERVER_PRIVILEGED
119 /* This service must be configured as privileged.
120 /* .PP
121 /* multi_server_disconnect() should be called by the application
122 /* when a client disconnects.
124 /* multi_server_drain() should be called when the application
125 /* no longer wishes to accept new client connections. Existing
126 /* clients are handled in a background process. A non-zero
127 /* result means this call should be tried again later.
129 /* The var_use_limit variable limits the number of clients that
130 /* a server can service before it commits suicide.
131 /* This value is taken from the global \fBmain.cf\fR configuration
132 /* file. Setting \fBvar_use_limit\fR to zero disables the client limit.
134 /* The var_idle_limit variable limits the time that a service
135 /* receives no client connection requests before it commits suicide.
136 /* This value is taken from the global \fBmain.cf\fR configuration
137 /* file. Setting \fBvar_idle_limit\fR to zero disables the idle limit.
138 /* DIAGNOSTICS
139 /* Problems and transactions are logged to \fBsyslogd\fR(8).
140 /* SEE ALSO
141 /* master(8), master process
142 /* syslogd(8) system logging
143 /* LICENSE
144 /* .ad
145 /* .fi
146 /* The Secure Mailer license must be distributed with this software.
147 /* AUTHOR(S)
148 /* Wietse Venema
149 /* IBM T.J. Watson Research
150 /* P.O. Box 704
151 /* Yorktown Heights, NY 10598, USA
152 /*--*/
154 /* System library. */
156 #include <sys_defs.h>
157 #include <sys/socket.h>
158 #include <sys/time.h> /* select() */
159 #include <unistd.h>
160 #include <signal.h>
161 #include <syslog.h>
162 #include <stdlib.h>
163 #include <limits.h>
164 #include <string.h>
165 #include <errno.h>
166 #include <fcntl.h>
167 #include <stdarg.h>
168 #ifdef STRCASECMP_IN_STRINGS_H
169 #include <strings.h>
170 #endif
171 #include <time.h>
173 #ifdef USE_SYS_SELECT_H
174 #include <sys/select.h> /* select() */
175 #endif
177 /* Utility library. */
179 #include <msg.h>
180 #include <msg_syslog.h>
181 #include <msg_vstream.h>
182 #include <chroot_uid.h>
183 #include <listen.h>
184 #include <events.h>
185 #include <vstring.h>
186 #include <vstream.h>
187 #include <msg_vstream.h>
188 #include <mymalloc.h>
189 #include <iostuff.h>
190 #include <stringops.h>
191 #include <sane_accept.h>
192 #include <myflock.h>
193 #include <safe_open.h>
194 #include <listen.h>
195 #include <watchdog.h>
196 #include <split_at.h>
198 /* Global library. */
200 #include <mail_task.h>
201 #include <debug_process.h>
202 #include <mail_params.h>
203 #include <mail_conf.h>
204 #include <mail_dict.h>
205 #include <timed_ipc.h>
206 #include <resolve_local.h>
207 #include <mail_flow.h>
209 /* Process manager. */
211 #include "master_proto.h"
213 /* Application-specific */
215 #include "mail_server.h"
218 * Global state.
220 static int client_count;
221 static int use_count;
222 static int socket_count = 1;
224 static void (*multi_server_service) (VSTREAM *, char *, char **);
225 static char *multi_server_name;
226 static char **multi_server_argv;
227 static void (*multi_server_accept) (int, char *);
228 static void (*multi_server_onexit) (char *, char **);
229 static void (*multi_server_pre_accept) (char *, char **);
230 static VSTREAM *multi_server_lock;
231 static int multi_server_in_flow_delay;
232 static unsigned multi_server_generation;
233 static void (*multi_server_pre_disconn) (VSTREAM *, char *, char **);
235 /* multi_server_exit - normal termination */
237 static NORETURN multi_server_exit(void)
239 if (multi_server_onexit)
240 multi_server_onexit(multi_server_name, multi_server_argv);
241 exit(0);
244 /* multi_server_abort - terminate after abnormal master exit */
246 static void multi_server_abort(int unused_event, char *unused_context)
248 if (msg_verbose)
249 msg_info("master disconnect -- exiting");
250 multi_server_exit();
253 /* multi_server_timeout - idle time exceeded */
255 static void multi_server_timeout(int unused_event, char *unused_context)
257 if (msg_verbose)
258 msg_info("idle timeout -- exiting");
259 multi_server_exit();
262 /* multi_server_drain - stop accepting new clients */
264 int multi_server_drain(void)
266 int fd;
268 switch (fork()) {
269 /* Try again later. */
270 case -1:
271 return (-1);
272 /* Finish existing clients in the background, then terminate. */
273 case 0:
274 (void) msg_cleanup((MSG_CLEANUP_FN) 0);
275 for (fd = MASTER_LISTEN_FD; fd < MASTER_LISTEN_FD + socket_count; fd++)
276 event_disable_readwrite(fd);
277 var_use_limit = 1;
278 return (0);
279 /* Let the master start a new process. */
280 default:
281 exit(0);
285 /* multi_server_disconnect - terminate client session */
287 void multi_server_disconnect(VSTREAM *stream)
289 if (msg_verbose)
290 msg_info("connection closed fd %d", vstream_fileno(stream));
291 if (multi_server_pre_disconn)
292 multi_server_pre_disconn(stream, multi_server_name, multi_server_argv);
293 event_disable_readwrite(vstream_fileno(stream));
294 (void) vstream_fclose(stream);
295 client_count--;
296 use_count++;
299 /* multi_server_execute - in case (char *) != (struct *) */
301 static void multi_server_execute(int unused_event, char *context)
303 VSTREAM *stream = (VSTREAM *) context;
305 if (multi_server_lock != 0
306 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK,
307 MYFLOCK_OP_NONE) < 0)
308 msg_fatal("select unlock: %m");
311 * Do not bother the application when the client disconnected. Don't drop
312 * the already accepted client request after "postfix reload"; that would
313 * be rude.
315 if (peekfd(vstream_fileno(stream)) > 0) {
316 if (master_notify(var_pid, multi_server_generation, MASTER_STAT_TAKEN) < 0)
317 /* void */ ;
318 multi_server_service(stream, multi_server_name, multi_server_argv);
319 if (master_notify(var_pid, multi_server_generation, MASTER_STAT_AVAIL) < 0)
320 multi_server_abort(EVENT_NULL_TYPE, EVENT_NULL_CONTEXT);
321 } else {
322 multi_server_disconnect(stream);
324 if (client_count == 0 && var_idle_limit > 0)
325 event_request_timer(multi_server_timeout, (char *) 0, var_idle_limit);
328 /* multi_server_enable_read - enable read events */
330 static void multi_server_enable_read(int unused_event, char *context)
332 VSTREAM *stream = (VSTREAM *) context;
334 event_enable_read(vstream_fileno(stream), multi_server_execute, (char *) stream);
337 /* multi_server_wakeup - wake up application */
339 static void multi_server_wakeup(int fd)
341 VSTREAM *stream;
342 char *tmp;
344 #if defined(F_DUPFD) && (EVENTS_STYLE != EVENTS_STYLE_SELECT)
345 #ifndef THRESHOLD_FD_WORKAROUND
346 #define THRESHOLD_FD_WORKAROUND 128
347 #endif
348 int new_fd;
351 * Leave some handles < FD_SETSIZE for DBMS libraries, in the unlikely
352 * case of a multi-server with a thousand clients.
354 if (fd < THRESHOLD_FD_WORKAROUND) {
355 if ((new_fd = fcntl(fd, F_DUPFD, THRESHOLD_FD_WORKAROUND)) < 0)
356 msg_fatal("fcntl F_DUPFD: %m");
357 (void) close(fd);
358 fd = new_fd;
360 #endif
361 if (msg_verbose)
362 msg_info("connection established fd %d", fd);
363 non_blocking(fd, BLOCKING);
364 close_on_exec(fd, CLOSE_ON_EXEC);
365 client_count++;
366 stream = vstream_fdopen(fd, O_RDWR);
367 tmp = concatenate(multi_server_name, " socket", (char *) 0);
368 vstream_control(stream, VSTREAM_CTL_PATH, tmp, VSTREAM_CTL_END);
369 myfree(tmp);
370 timed_ipc_setup(stream);
371 if (multi_server_in_flow_delay && mail_flow_get(1) < 0)
372 event_request_timer(multi_server_enable_read, (char *) stream,
373 var_in_flow_delay);
374 else
375 multi_server_enable_read(0, (char *) stream);
378 /* multi_server_accept_local - accept client connection request */
380 static void multi_server_accept_local(int unused_event, char *context)
382 int listen_fd = CAST_CHAR_PTR_TO_INT(context);
383 int time_left = -1;
384 int fd;
387 * Be prepared for accept() to fail because some other process already
388 * got the connection (the number of processes competing for clients is
389 * kept small, so this is not a "thundering herd" problem). If the
390 * accept() succeeds, be sure to disable non-blocking I/O, in order to
391 * minimize confusion.
393 if (client_count == 0 && var_idle_limit > 0)
394 time_left = event_cancel_timer(multi_server_timeout, (char *) 0);
396 if (multi_server_pre_accept)
397 multi_server_pre_accept(multi_server_name, multi_server_argv);
398 fd = LOCAL_ACCEPT(listen_fd);
399 if (multi_server_lock != 0
400 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK,
401 MYFLOCK_OP_NONE) < 0)
402 msg_fatal("select unlock: %m");
403 if (fd < 0) {
404 if (errno != EAGAIN)
405 msg_error("accept connection: %m");
406 if (time_left >= 0)
407 event_request_timer(multi_server_timeout, (char *) 0, time_left);
408 return;
410 multi_server_wakeup(fd);
413 #ifdef MASTER_XPORT_NAME_PASS
415 /* multi_server_accept_pass - accept descriptor */
417 static void multi_server_accept_pass(int unused_event, char *context)
419 int listen_fd = CAST_CHAR_PTR_TO_INT(context);
420 int time_left = -1;
421 int fd;
424 * Be prepared for accept() to fail because some other process already
425 * got the connection (the number of processes competing for clients is
426 * kept small, so this is not a "thundering herd" problem). If the
427 * accept() succeeds, be sure to disable non-blocking I/O, in order to
428 * minimize confusion.
430 if (client_count == 0 && var_idle_limit > 0)
431 time_left = event_cancel_timer(multi_server_timeout, (char *) 0);
433 if (multi_server_pre_accept)
434 multi_server_pre_accept(multi_server_name, multi_server_argv);
435 fd = PASS_ACCEPT(listen_fd);
436 if (multi_server_lock != 0
437 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK,
438 MYFLOCK_OP_NONE) < 0)
439 msg_fatal("select unlock: %m");
440 if (fd < 0) {
441 if (errno != EAGAIN)
442 msg_error("accept connection: %m");
443 if (time_left >= 0)
444 event_request_timer(multi_server_timeout, (char *) 0, time_left);
445 return;
447 multi_server_wakeup(fd);
450 #endif
452 /* multi_server_accept_inet - accept client connection request */
454 static void multi_server_accept_inet(int unused_event, char *context)
456 int listen_fd = CAST_CHAR_PTR_TO_INT(context);
457 int time_left = -1;
458 int fd;
461 * Be prepared for accept() to fail because some other process already
462 * got the connection (the number of processes competing for clients is
463 * kept small, so this is not a "thundering herd" problem). If the
464 * accept() succeeds, be sure to disable non-blocking I/O, in order to
465 * minimize confusion.
467 if (client_count == 0 && var_idle_limit > 0)
468 time_left = event_cancel_timer(multi_server_timeout, (char *) 0);
470 if (multi_server_pre_accept)
471 multi_server_pre_accept(multi_server_name, multi_server_argv);
472 fd = inet_accept(listen_fd);
473 if (multi_server_lock != 0
474 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK,
475 MYFLOCK_OP_NONE) < 0)
476 msg_fatal("select unlock: %m");
477 if (fd < 0) {
478 if (errno != EAGAIN)
479 msg_error("accept connection: %m");
480 if (time_left >= 0)
481 event_request_timer(multi_server_timeout, (char *) 0, time_left);
482 return;
484 multi_server_wakeup(fd);
487 /* multi_server_main - the real main program */
489 NORETURN multi_server_main(int argc, char **argv, MULTI_SERVER_FN service,...)
491 const char *myname = "multi_server_main";
492 VSTREAM *stream = 0;
493 char *root_dir = 0;
494 char *user_name = 0;
495 int debug_me = 0;
496 int daemon_mode = 1;
497 char *service_name = basename(argv[0]);
498 int delay;
499 int c;
500 int fd;
501 va_list ap;
502 MAIL_SERVER_INIT_FN pre_init = 0;
503 MAIL_SERVER_INIT_FN post_init = 0;
504 MAIL_SERVER_LOOP_FN loop = 0;
505 int key;
506 char *transport = 0;
508 #if 0
509 char *lock_path;
510 VSTRING *why;
512 #endif
513 int alone = 0;
514 int zerolimit = 0;
515 WATCHDOG *watchdog;
516 char *oname;
517 char *oval;
518 char *generation;
519 int msg_vstream_needed = 0;
520 int redo_syslog_init = 0;
523 * Process environment options as early as we can.
525 if (getenv(CONF_ENV_VERB))
526 msg_verbose = 1;
527 if (getenv(CONF_ENV_DEBUG))
528 debug_me = 1;
531 * Don't die when a process goes away unexpectedly.
533 signal(SIGPIPE, SIG_IGN);
536 * Don't die for frivolous reasons.
538 #ifdef SIGXFSZ
539 signal(SIGXFSZ, SIG_IGN);
540 #endif
543 * May need this every now and then.
545 var_procname = mystrdup(basename(argv[0]));
546 set_mail_conf_str(VAR_PROCNAME, var_procname);
549 * Initialize logging and exit handler. Do the syslog first, so that its
550 * initialization completes before we enter the optional chroot jail.
552 msg_syslog_init(mail_task(var_procname), LOG_PID, LOG_FACILITY);
553 if (msg_verbose)
554 msg_info("daemon started");
557 * Initialize from the configuration file. Allow command-line options to
558 * override compiled-in defaults or configured parameter values.
560 mail_conf_suck();
563 * Register dictionaries that use higher-level interfaces and protocols.
565 mail_dict_init();
568 * Pick up policy settings from master process. Shut up error messages to
569 * stderr, because no-one is going to see them.
571 opterr = 0;
572 while ((c = GETOPT(argc, argv, "cdDi:lm:n:o:s:St:uvVz")) > 0) {
573 switch (c) {
574 case 'c':
575 root_dir = "setme";
576 break;
577 case 'd':
578 daemon_mode = 0;
579 break;
580 case 'D':
581 debug_me = 1;
582 break;
583 case 'i':
584 mail_conf_update(VAR_MAX_IDLE, optarg);
585 break;
586 case 'l':
587 alone = 1;
588 break;
589 case 'm':
590 mail_conf_update(VAR_MAX_USE, optarg);
591 break;
592 case 'n':
593 service_name = optarg;
594 break;
595 case 'o':
596 /* XXX Use split_nameval() */
597 oname = mystrdup(optarg);
598 if ((oval = split_at(oname, '=')) == 0)
599 oval = "";
600 mail_conf_update(oname, oval);
601 if (strcmp(oname, VAR_SYSLOG_NAME) == 0)
602 redo_syslog_init = 1;
603 break;
604 case 's':
605 if ((socket_count = atoi(optarg)) <= 0)
606 msg_fatal("invalid socket_count: %s", optarg);
607 break;
608 case 'S':
609 stream = VSTREAM_IN;
610 break;
611 case 'u':
612 user_name = "setme";
613 break;
614 case 't':
615 transport = optarg;
616 break;
617 case 'v':
618 msg_verbose++;
619 break;
620 case 'V':
621 if (++msg_vstream_needed == 1)
622 msg_vstream_init(mail_task(var_procname), VSTREAM_ERR);
623 break;
624 case 'z':
625 zerolimit = 1;
626 break;
627 default:
628 msg_fatal("invalid option: %c", c);
629 break;
634 * Initialize generic parameters.
636 mail_params_init();
637 if (redo_syslog_init)
638 msg_syslog_init(mail_task(var_procname), LOG_PID, LOG_FACILITY);
641 * If not connected to stdin, stdin must not be a terminal.
643 if (daemon_mode && stream == 0 && isatty(STDIN_FILENO)) {
644 msg_vstream_init(var_procname, VSTREAM_ERR);
645 msg_fatal("do not run this command by hand");
649 * Application-specific initialization.
651 va_start(ap, service);
652 while ((key = va_arg(ap, int)) != 0) {
653 switch (key) {
654 case MAIL_SERVER_INT_TABLE:
655 get_mail_conf_int_table(va_arg(ap, CONFIG_INT_TABLE *));
656 break;
657 case MAIL_SERVER_STR_TABLE:
658 get_mail_conf_str_table(va_arg(ap, CONFIG_STR_TABLE *));
659 break;
660 case MAIL_SERVER_BOOL_TABLE:
661 get_mail_conf_bool_table(va_arg(ap, CONFIG_BOOL_TABLE *));
662 break;
663 case MAIL_SERVER_TIME_TABLE:
664 get_mail_conf_time_table(va_arg(ap, CONFIG_TIME_TABLE *));
665 break;
666 case MAIL_SERVER_RAW_TABLE:
667 get_mail_conf_raw_table(va_arg(ap, CONFIG_RAW_TABLE *));
668 break;
669 case MAIL_SERVER_NINT_TABLE:
670 get_mail_conf_nint_table(va_arg(ap, CONFIG_NINT_TABLE *));
671 break;
672 case MAIL_SERVER_PRE_INIT:
673 pre_init = va_arg(ap, MAIL_SERVER_INIT_FN);
674 break;
675 case MAIL_SERVER_POST_INIT:
676 post_init = va_arg(ap, MAIL_SERVER_INIT_FN);
677 break;
678 case MAIL_SERVER_LOOP:
679 loop = va_arg(ap, MAIL_SERVER_LOOP_FN);
680 break;
681 case MAIL_SERVER_EXIT:
682 multi_server_onexit = va_arg(ap, MAIL_SERVER_EXIT_FN);
683 break;
684 case MAIL_SERVER_PRE_ACCEPT:
685 multi_server_pre_accept = va_arg(ap, MAIL_SERVER_ACCEPT_FN);
686 break;
687 case MAIL_SERVER_PRE_DISCONN:
688 multi_server_pre_disconn = va_arg(ap, MAIL_SERVER_DISCONN_FN);
689 break;
690 case MAIL_SERVER_IN_FLOW_DELAY:
691 multi_server_in_flow_delay = 1;
692 break;
693 case MAIL_SERVER_SOLITARY:
694 if (stream == 0 && !alone)
695 msg_fatal("service %s requires a process limit of 1",
696 service_name);
697 break;
698 case MAIL_SERVER_UNLIMITED:
699 if (stream == 0 && !zerolimit)
700 msg_fatal("service %s requires a process limit of 0",
701 service_name);
702 break;
703 case MAIL_SERVER_PRIVILEGED:
704 if (user_name)
705 msg_fatal("service %s requires privileged operation",
706 service_name);
707 break;
708 default:
709 msg_panic("%s: unknown argument type: %d", myname, key);
712 va_end(ap);
714 if (root_dir)
715 root_dir = var_queue_dir;
716 if (user_name)
717 user_name = var_mail_owner;
720 * Can options be required?
722 if (stream == 0) {
723 if (transport == 0)
724 msg_fatal("no transport type specified");
725 if (strcasecmp(transport, MASTER_XPORT_NAME_INET) == 0)
726 multi_server_accept = multi_server_accept_inet;
727 else if (strcasecmp(transport, MASTER_XPORT_NAME_UNIX) == 0)
728 multi_server_accept = multi_server_accept_local;
729 #ifdef MASTER_XPORT_NAME_PASS
730 else if (strcasecmp(transport, MASTER_XPORT_NAME_PASS) == 0)
731 multi_server_accept = multi_server_accept_pass;
732 #endif
733 else
734 msg_fatal("unsupported transport type: %s", transport);
738 * Retrieve process generation from environment.
740 if ((generation = getenv(MASTER_GEN_NAME)) != 0) {
741 if (!alldig(generation))
742 msg_fatal("bad generation: %s", generation);
743 OCTAL_TO_UNSIGNED(multi_server_generation, generation);
744 if (msg_verbose)
745 msg_info("process generation: %s (%o)",
746 generation, multi_server_generation);
750 * Optionally start the debugger on ourself.
752 if (debug_me)
753 debug_process();
756 * Traditionally, BSD select() can't handle multiple processes selecting
757 * on the same socket, and wakes up every process in select(). See TCP/IP
758 * Illustrated volume 2 page 532. We avoid select() collisions with an
759 * external lock file.
763 * XXX Can't compete for exclusive access to the listen socket because we
764 * also have to monitor existing client connections for service requests.
766 #if 0
767 if (stream == 0 && !alone) {
768 lock_path = concatenate(DEF_PID_DIR, "/", transport,
769 ".", service_name, (char *) 0);
770 why = vstring_alloc(1);
771 if ((multi_server_lock = safe_open(lock_path, O_CREAT | O_RDWR, 0600,
772 (struct stat *) 0, -1, -1, why)) == 0)
773 msg_fatal("open lock file %s: %s", lock_path, vstring_str(why));
774 close_on_exec(vstream_fileno(multi_server_lock), CLOSE_ON_EXEC);
775 myfree(lock_path);
776 vstring_free(why);
778 #endif
781 * Set up call-back info.
783 multi_server_service = service;
784 multi_server_name = service_name;
785 multi_server_argv = argv + optind;
788 * Run pre-jail initialization.
790 if (chdir(var_queue_dir) < 0)
791 msg_fatal("chdir(\"%s\"): %m", var_queue_dir);
792 if (pre_init)
793 pre_init(multi_server_name, multi_server_argv);
796 * Optionally, restrict the damage that this process can do.
798 resolve_local_init();
799 tzset();
800 chroot_uid(root_dir, user_name);
803 * Run post-jail initialization.
805 if (post_init)
806 post_init(multi_server_name, multi_server_argv);
809 * Are we running as a one-shot server with the client connection on
810 * standard input? If so, make sure the output is written to stdout so as
811 * to satisfy common expectation.
813 if (stream != 0) {
814 vstream_control(stream,
815 VSTREAM_CTL_DOUBLE,
816 VSTREAM_CTL_WRITE_FD, STDOUT_FILENO,
817 VSTREAM_CTL_END);
818 service(stream, multi_server_name, multi_server_argv);
819 vstream_fflush(stream);
820 multi_server_exit();
824 * Running as a semi-resident server. Service connection requests.
825 * Terminate when we have serviced a sufficient number of clients, when
826 * no-one has been talking to us for a configurable amount of time, or
827 * when the master process terminated abnormally.
829 if (var_idle_limit > 0)
830 event_request_timer(multi_server_timeout, (char *) 0, var_idle_limit);
831 for (fd = MASTER_LISTEN_FD; fd < MASTER_LISTEN_FD + socket_count; fd++) {
832 event_enable_read(fd, multi_server_accept, CAST_INT_TO_CHAR_PTR(fd));
833 close_on_exec(fd, CLOSE_ON_EXEC);
835 event_enable_read(MASTER_STATUS_FD, multi_server_abort, (char *) 0);
836 close_on_exec(MASTER_STATUS_FD, CLOSE_ON_EXEC);
837 close_on_exec(MASTER_FLOW_READ, CLOSE_ON_EXEC);
838 close_on_exec(MASTER_FLOW_WRITE, CLOSE_ON_EXEC);
839 watchdog = watchdog_create(var_daemon_timeout, (WATCHDOG_FN) 0, (char *) 0);
842 * The event loop, at last.
844 while (var_use_limit == 0 || use_count < var_use_limit || client_count > 0) {
845 if (multi_server_lock != 0) {
846 watchdog_stop(watchdog);
847 if (myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK,
848 MYFLOCK_OP_EXCLUSIVE) < 0)
849 msg_fatal("select lock: %m");
851 watchdog_start(watchdog);
852 delay = loop ? loop(multi_server_name, multi_server_argv) : -1;
853 event_loop(delay);
855 multi_server_exit();