1 /* $NetBSD: multi_server.c,v 1.1.1.3 2011/03/02 19:32:21 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* multi_server 3 6 /* SUMMARY 7 /* skeleton multi-threaded mail subsystem 8 /* SYNOPSIS 9 /* #include <mail_server.h> 10 /* 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; 16 /* 17 /* void multi_server_disconnect(stream) 18 /* VSTREAM *stream; 19 /* 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. 26 /* 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. 32 /* 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_NBOOL_TABLE (CONFIG_NBOOL_TABLE *)" 79 /* A table with configurable parameters, to be loaded from the 80 /* global Postfix configuration file. Tables are loaded in the 81 /* order as specified, and multiple instances of the same type 82 /* are allowed. 83 /* .IP "MAIL_SERVER_PRE_INIT (void *(char *service_name, char **argv))" 84 /* A pointer to a function that is called once 85 /* by the skeleton after it has read the global configuration file 86 /* and after it has processed command-line arguments, but before 87 /* the skeleton has optionally relinquished the process privileges. 88 /* .sp 89 /* Only the last instance of this parameter type is remembered. 90 /* .IP "MAIL_SERVER_POST_INIT (void *(char *service_name, char **argv))" 91 /* A pointer to a function that is called once 92 /* by the skeleton after it has optionally relinquished the process 93 /* privileges, but before servicing client connection requests. 94 /* .sp 95 /* Only the last instance of this parameter type is remembered. 96 /* .IP "MAIL_SERVER_LOOP (int *(char *service_name, char **argv))" 97 /* A pointer to function that is executed from 98 /* within the event loop, whenever an I/O or timer event has happened, 99 /* or whenever nothing has happened for a specified amount of time. 100 /* The result value of the function specifies how long to wait until 101 /* the next event. Specify -1 to wait for "as long as it takes". 102 /* .sp 103 /* Only the last instance of this parameter type is remembered. 104 /* .IP "MAIL_SERVER_EXIT (void *(char *service_name, char **argv))" 105 /* A pointer to function that is executed immediately before normal 106 /* process termination. 107 /* .IP "MAIL_SERVER_PRE_ACCEPT (void *(char *service_name, char **argv))" 108 /* Function to be executed prior to accepting a new connection. 109 /* .sp 110 /* Only the last instance of this parameter type is remembered. 111 /* .IP "MAIL_SERVER_PRE_DISCONN (VSTREAM *, char *service_name, char **argv)" 112 /* A pointer to a function that is called 113 /* by the multi_server_disconnect() function (see below). 114 /* .sp 115 /* Only the last instance of this parameter type is remembered. 116 /* .IP "MAIL_SERVER_IN_FLOW_DELAY (none)" 117 /* Pause $in_flow_delay seconds when no "mail flow control token" 118 /* is available. A token is consumed for each connection request. 119 /* .IP MAIL_SERVER_SOLITARY 120 /* This service must be configured with process limit of 1. 121 /* .IP MAIL_SERVER_UNLIMITED 122 /* This service must be configured with process limit of 0. 123 /* .IP MAIL_SERVER_PRIVILEGED 124 /* This service must be configured as privileged. 125 /* .PP 126 /* multi_server_disconnect() should be called by the application 127 /* to close a client connection. 128 /* 129 /* multi_server_drain() should be called when the application 130 /* no longer wishes to accept new client connections. Existing 131 /* clients are handled in a background process, and the process 132 /* terminates when the last client is disconnected. A non-zero 133 /* result means this call should be tried again later. 134 /* 135 /* The var_use_limit variable limits the number of clients that 136 /* a server can service before it commits suicide. 137 /* This value is taken from the global \fBmain.cf\fR configuration 138 /* file. Setting \fBvar_use_limit\fR to zero disables the client limit. 139 /* 140 /* The var_idle_limit variable limits the time that a service 141 /* receives no client connection requests before it commits suicide. 142 /* This value is taken from the global \fBmain.cf\fR configuration 143 /* file. Setting \fBvar_idle_limit\fR to zero disables the idle limit. 144 /* DIAGNOSTICS 145 /* Problems and transactions are logged to \fBsyslogd\fR(8). 146 /* SEE ALSO 147 /* master(8), master process 148 /* syslogd(8) system logging 149 /* LICENSE 150 /* .ad 151 /* .fi 152 /* The Secure Mailer license must be distributed with this software. 153 /* AUTHOR(S) 154 /* Wietse Venema 155 /* IBM T.J. Watson Research 156 /* P.O. Box 704 157 /* Yorktown Heights, NY 10598, USA 158 /*--*/ 159 160 /* System library. */ 161 162 #include <sys_defs.h> 163 #include <sys/socket.h> 164 #include <sys/time.h> /* select() */ 165 #include <unistd.h> 166 #include <signal.h> 167 #include <syslog.h> 168 #include <stdlib.h> 169 #include <limits.h> 170 #include <string.h> 171 #include <errno.h> 172 #include <fcntl.h> 173 #include <stdarg.h> 174 #ifdef STRCASECMP_IN_STRINGS_H 175 #include <strings.h> 176 #endif 177 #include <time.h> 178 179 #ifdef USE_SYS_SELECT_H 180 #include <sys/select.h> /* select() */ 181 #endif 182 183 /* Utility library. */ 184 185 #include <msg.h> 186 #include <msg_syslog.h> 187 #include <msg_vstream.h> 188 #include <chroot_uid.h> 189 #include <listen.h> 190 #include <events.h> 191 #include <vstring.h> 192 #include <vstream.h> 193 #include <msg_vstream.h> 194 #include <mymalloc.h> 195 #include <iostuff.h> 196 #include <stringops.h> 197 #include <sane_accept.h> 198 #include <myflock.h> 199 #include <safe_open.h> 200 #include <listen.h> 201 #include <watchdog.h> 202 #include <split_at.h> 203 204 /* Global library. */ 205 206 #include <mail_task.h> 207 #include <debug_process.h> 208 #include <mail_params.h> 209 #include <mail_conf.h> 210 #include <mail_dict.h> 211 #include <timed_ipc.h> 212 #include <resolve_local.h> 213 #include <mail_flow.h> 214 215 /* Process manager. */ 216 217 #include "master_proto.h" 218 219 /* Application-specific */ 220 221 #include "mail_server.h" 222 223 /* 224 * Global state. 225 */ 226 static int client_count; 227 static int use_count; 228 static int socket_count = 1; 229 230 static void (*multi_server_service) (VSTREAM *, char *, char **); 231 static char *multi_server_name; 232 static char **multi_server_argv; 233 static void (*multi_server_accept) (int, char *); 234 static void (*multi_server_onexit) (char *, char **); 235 static void (*multi_server_pre_accept) (char *, char **); 236 static VSTREAM *multi_server_lock; 237 static int multi_server_in_flow_delay; 238 static unsigned multi_server_generation; 239 static void (*multi_server_pre_disconn) (VSTREAM *, char *, char **); 240 241 /* multi_server_exit - normal termination */ 242 243 static NORETURN multi_server_exit(void) 244 { 245 if (multi_server_onexit) 246 multi_server_onexit(multi_server_name, multi_server_argv); 247 exit(0); 248 } 249 250 /* multi_server_abort - terminate after abnormal master exit */ 251 252 static void multi_server_abort(int unused_event, char *unused_context) 253 { 254 if (msg_verbose) 255 msg_info("master disconnect -- exiting"); 256 multi_server_exit(); 257 } 258 259 /* multi_server_timeout - idle time exceeded */ 260 261 static void multi_server_timeout(int unused_event, char *unused_context) 262 { 263 if (msg_verbose) 264 msg_info("idle timeout -- exiting"); 265 multi_server_exit(); 266 } 267 268 /* multi_server_drain - stop accepting new clients */ 269 270 int multi_server_drain(void) 271 { 272 const char *myname = "multi_server_drain"; 273 int fd; 274 275 switch (fork()) { 276 /* Try again later. */ 277 case -1: 278 return (-1); 279 /* Finish existing clients in the background, then terminate. */ 280 case 0: 281 (void) msg_cleanup((MSG_CLEANUP_FN) 0); 282 event_fork(); 283 for (fd = MASTER_LISTEN_FD; fd < MASTER_LISTEN_FD + socket_count; fd++) { 284 event_disable_readwrite(fd); 285 (void) close(fd); 286 /* Play safe - don't reuse this file number. */ 287 if (DUP2(STDIN_FILENO, fd) < 0) 288 msg_warn("%s: dup2(%d, %d): %m", myname, STDIN_FILENO, fd); 289 } 290 var_use_limit = 1; 291 return (0); 292 /* Let the master start a new process. */ 293 default: 294 exit(0); 295 } 296 } 297 298 /* multi_server_disconnect - terminate client session */ 299 300 void multi_server_disconnect(VSTREAM *stream) 301 { 302 if (msg_verbose) 303 msg_info("connection closed fd %d", vstream_fileno(stream)); 304 if (multi_server_pre_disconn) 305 multi_server_pre_disconn(stream, multi_server_name, multi_server_argv); 306 event_disable_readwrite(vstream_fileno(stream)); 307 (void) vstream_fclose(stream); 308 client_count--; 309 /* Avoid integer wrap-around in a persistent process. */ 310 if (use_count < INT_MAX) 311 use_count++; 312 if (client_count == 0 && var_idle_limit > 0) 313 event_request_timer(multi_server_timeout, (char *) 0, var_idle_limit); 314 } 315 316 /* multi_server_execute - in case (char *) != (struct *) */ 317 318 static void multi_server_execute(int unused_event, char *context) 319 { 320 VSTREAM *stream = (VSTREAM *) context; 321 322 if (multi_server_lock != 0 323 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK, 324 MYFLOCK_OP_NONE) < 0) 325 msg_fatal("select unlock: %m"); 326 327 /* 328 * Do not bother the application when the client disconnected. Don't drop 329 * the already accepted client request after "postfix reload"; that would 330 * be rude. 331 */ 332 if (peekfd(vstream_fileno(stream)) > 0) { 333 if (master_notify(var_pid, multi_server_generation, MASTER_STAT_TAKEN) < 0) 334 /* void */ ; 335 multi_server_service(stream, multi_server_name, multi_server_argv); 336 if (master_notify(var_pid, multi_server_generation, MASTER_STAT_AVAIL) < 0) 337 multi_server_abort(EVENT_NULL_TYPE, EVENT_NULL_CONTEXT); 338 } else { 339 multi_server_disconnect(stream); 340 } 341 } 342 343 /* multi_server_enable_read - enable read events */ 344 345 static void multi_server_enable_read(int unused_event, char *context) 346 { 347 VSTREAM *stream = (VSTREAM *) context; 348 349 event_enable_read(vstream_fileno(stream), multi_server_execute, (char *) stream); 350 } 351 352 /* multi_server_wakeup - wake up application */ 353 354 static void multi_server_wakeup(int fd) 355 { 356 VSTREAM *stream; 357 char *tmp; 358 359 #if defined(F_DUPFD) && (EVENTS_STYLE != EVENTS_STYLE_SELECT) 360 #ifndef THRESHOLD_FD_WORKAROUND 361 #define THRESHOLD_FD_WORKAROUND 128 362 #endif 363 int new_fd; 364 365 /* 366 * Leave some handles < FD_SETSIZE for DBMS libraries, in the unlikely 367 * case of a multi-server with a thousand clients. 368 */ 369 if (fd < THRESHOLD_FD_WORKAROUND) { 370 if ((new_fd = fcntl(fd, F_DUPFD, THRESHOLD_FD_WORKAROUND)) < 0) 371 msg_fatal("fcntl F_DUPFD: %m"); 372 (void) close(fd); 373 fd = new_fd; 374 } 375 #endif 376 if (msg_verbose) 377 msg_info("connection established fd %d", fd); 378 non_blocking(fd, BLOCKING); 379 close_on_exec(fd, CLOSE_ON_EXEC); 380 client_count++; 381 stream = vstream_fdopen(fd, O_RDWR); 382 tmp = concatenate(multi_server_name, " socket", (char *) 0); 383 vstream_control(stream, VSTREAM_CTL_PATH, tmp, VSTREAM_CTL_END); 384 myfree(tmp); 385 timed_ipc_setup(stream); 386 if (multi_server_in_flow_delay && mail_flow_get(1) < 0) 387 event_request_timer(multi_server_enable_read, (char *) stream, 388 var_in_flow_delay); 389 else 390 multi_server_enable_read(0, (char *) stream); 391 } 392 393 /* multi_server_accept_local - accept client connection request */ 394 395 static void multi_server_accept_local(int unused_event, char *context) 396 { 397 int listen_fd = CAST_CHAR_PTR_TO_INT(context); 398 int time_left = -1; 399 int fd; 400 401 /* 402 * Be prepared for accept() to fail because some other process already 403 * got the connection (the number of processes competing for clients is 404 * kept small, so this is not a "thundering herd" problem). If the 405 * accept() succeeds, be sure to disable non-blocking I/O, in order to 406 * minimize confusion. 407 */ 408 if (client_count == 0 && var_idle_limit > 0) 409 time_left = event_cancel_timer(multi_server_timeout, (char *) 0); 410 411 if (multi_server_pre_accept) 412 multi_server_pre_accept(multi_server_name, multi_server_argv); 413 fd = LOCAL_ACCEPT(listen_fd); 414 if (multi_server_lock != 0 415 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK, 416 MYFLOCK_OP_NONE) < 0) 417 msg_fatal("select unlock: %m"); 418 if (fd < 0) { 419 if (errno != EAGAIN) 420 msg_error("accept connection: %m"); 421 if (time_left >= 0) 422 event_request_timer(multi_server_timeout, (char *) 0, time_left); 423 return; 424 } 425 multi_server_wakeup(fd); 426 } 427 428 #ifdef MASTER_XPORT_NAME_PASS 429 430 /* multi_server_accept_pass - accept descriptor */ 431 432 static void multi_server_accept_pass(int unused_event, char *context) 433 { 434 int listen_fd = CAST_CHAR_PTR_TO_INT(context); 435 int time_left = -1; 436 int fd; 437 438 /* 439 * Be prepared for accept() to fail because some other process already 440 * got the connection (the number of processes competing for clients is 441 * kept small, so this is not a "thundering herd" problem). If the 442 * accept() succeeds, be sure to disable non-blocking I/O, in order to 443 * minimize confusion. 444 */ 445 if (client_count == 0 && var_idle_limit > 0) 446 time_left = event_cancel_timer(multi_server_timeout, (char *) 0); 447 448 if (multi_server_pre_accept) 449 multi_server_pre_accept(multi_server_name, multi_server_argv); 450 fd = PASS_ACCEPT(listen_fd); 451 if (multi_server_lock != 0 452 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK, 453 MYFLOCK_OP_NONE) < 0) 454 msg_fatal("select unlock: %m"); 455 if (fd < 0) { 456 if (errno != EAGAIN) 457 msg_error("accept connection: %m"); 458 if (time_left >= 0) 459 event_request_timer(multi_server_timeout, (char *) 0, time_left); 460 return; 461 } 462 multi_server_wakeup(fd); 463 } 464 465 #endif 466 467 /* multi_server_accept_inet - accept client connection request */ 468 469 static void multi_server_accept_inet(int unused_event, char *context) 470 { 471 int listen_fd = CAST_CHAR_PTR_TO_INT(context); 472 int time_left = -1; 473 int fd; 474 475 /* 476 * Be prepared for accept() to fail because some other process already 477 * got the connection (the number of processes competing for clients is 478 * kept small, so this is not a "thundering herd" problem). If the 479 * accept() succeeds, be sure to disable non-blocking I/O, in order to 480 * minimize confusion. 481 */ 482 if (client_count == 0 && var_idle_limit > 0) 483 time_left = event_cancel_timer(multi_server_timeout, (char *) 0); 484 485 if (multi_server_pre_accept) 486 multi_server_pre_accept(multi_server_name, multi_server_argv); 487 fd = inet_accept(listen_fd); 488 if (multi_server_lock != 0 489 && myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK, 490 MYFLOCK_OP_NONE) < 0) 491 msg_fatal("select unlock: %m"); 492 if (fd < 0) { 493 if (errno != EAGAIN) 494 msg_error("accept connection: %m"); 495 if (time_left >= 0) 496 event_request_timer(multi_server_timeout, (char *) 0, time_left); 497 return; 498 } 499 multi_server_wakeup(fd); 500 } 501 502 /* multi_server_main - the real main program */ 503 504 NORETURN multi_server_main(int argc, char **argv, MULTI_SERVER_FN service,...) 505 { 506 const char *myname = "multi_server_main"; 507 VSTREAM *stream = 0; 508 char *root_dir = 0; 509 char *user_name = 0; 510 int debug_me = 0; 511 int daemon_mode = 1; 512 char *service_name = basename(argv[0]); 513 int delay; 514 int c; 515 int fd; 516 va_list ap; 517 MAIL_SERVER_INIT_FN pre_init = 0; 518 MAIL_SERVER_INIT_FN post_init = 0; 519 MAIL_SERVER_LOOP_FN loop = 0; 520 int key; 521 char *transport = 0; 522 523 #if 0 524 char *lock_path; 525 VSTRING *why; 526 527 #endif 528 int alone = 0; 529 int zerolimit = 0; 530 WATCHDOG *watchdog; 531 char *oname_val; 532 char *oname; 533 char *oval; 534 const char *err; 535 char *generation; 536 int msg_vstream_needed = 0; 537 int redo_syslog_init = 0; 538 539 /* 540 * Process environment options as early as we can. 541 */ 542 if (getenv(CONF_ENV_VERB)) 543 msg_verbose = 1; 544 if (getenv(CONF_ENV_DEBUG)) 545 debug_me = 1; 546 547 /* 548 * Don't die when a process goes away unexpectedly. 549 */ 550 signal(SIGPIPE, SIG_IGN); 551 552 /* 553 * Don't die for frivolous reasons. 554 */ 555 #ifdef SIGXFSZ 556 signal(SIGXFSZ, SIG_IGN); 557 #endif 558 559 /* 560 * May need this every now and then. 561 */ 562 var_procname = mystrdup(basename(argv[0])); 563 set_mail_conf_str(VAR_PROCNAME, var_procname); 564 565 /* 566 * Initialize logging and exit handler. Do the syslog first, so that its 567 * initialization completes before we enter the optional chroot jail. 568 */ 569 msg_syslog_init(mail_task(var_procname), LOG_PID, LOG_FACILITY); 570 if (msg_verbose) 571 msg_info("daemon started"); 572 573 /* 574 * Initialize from the configuration file. Allow command-line options to 575 * override compiled-in defaults or configured parameter values. 576 */ 577 mail_conf_suck(); 578 579 /* 580 * Register dictionaries that use higher-level interfaces and protocols. 581 */ 582 mail_dict_init(); 583 584 /* 585 * Pick up policy settings from master process. Shut up error messages to 586 * stderr, because no-one is going to see them. 587 */ 588 opterr = 0; 589 while ((c = GETOPT(argc, argv, "cdDi:lm:n:o:s:St:uvVz")) > 0) { 590 switch (c) { 591 case 'c': 592 root_dir = "setme"; 593 break; 594 case 'd': 595 daemon_mode = 0; 596 break; 597 case 'D': 598 debug_me = 1; 599 break; 600 case 'i': 601 mail_conf_update(VAR_MAX_IDLE, optarg); 602 break; 603 case 'l': 604 alone = 1; 605 break; 606 case 'm': 607 mail_conf_update(VAR_MAX_USE, optarg); 608 break; 609 case 'n': 610 service_name = optarg; 611 break; 612 case 'o': 613 oname_val = mystrdup(optarg); 614 if ((err = split_nameval(oname_val, &oname, &oval)) != 0) 615 msg_fatal("invalid \"-o %s\" option value: %s", optarg, err); 616 mail_conf_update(oname, oval); 617 if (strcmp(oname, VAR_SYSLOG_NAME) == 0) 618 redo_syslog_init = 1; 619 myfree(oname_val); 620 break; 621 case 's': 622 if ((socket_count = atoi(optarg)) <= 0) 623 msg_fatal("invalid socket_count: %s", optarg); 624 break; 625 case 'S': 626 stream = VSTREAM_IN; 627 break; 628 case 'u': 629 user_name = "setme"; 630 break; 631 case 't': 632 transport = optarg; 633 break; 634 case 'v': 635 msg_verbose++; 636 break; 637 case 'V': 638 if (++msg_vstream_needed == 1) 639 msg_vstream_init(mail_task(var_procname), VSTREAM_ERR); 640 break; 641 case 'z': 642 zerolimit = 1; 643 break; 644 default: 645 msg_fatal("invalid option: %c", c); 646 break; 647 } 648 } 649 650 /* 651 * Initialize generic parameters. 652 */ 653 mail_params_init(); 654 if (redo_syslog_init) 655 msg_syslog_init(mail_task(var_procname), LOG_PID, LOG_FACILITY); 656 657 /* 658 * If not connected to stdin, stdin must not be a terminal. 659 */ 660 if (daemon_mode && stream == 0 && isatty(STDIN_FILENO)) { 661 msg_vstream_init(var_procname, VSTREAM_ERR); 662 msg_fatal("do not run this command by hand"); 663 } 664 665 /* 666 * Application-specific initialization. 667 */ 668 va_start(ap, service); 669 while ((key = va_arg(ap, int)) != 0) { 670 switch (key) { 671 case MAIL_SERVER_INT_TABLE: 672 get_mail_conf_int_table(va_arg(ap, CONFIG_INT_TABLE *)); 673 break; 674 case MAIL_SERVER_STR_TABLE: 675 get_mail_conf_str_table(va_arg(ap, CONFIG_STR_TABLE *)); 676 break; 677 case MAIL_SERVER_BOOL_TABLE: 678 get_mail_conf_bool_table(va_arg(ap, CONFIG_BOOL_TABLE *)); 679 break; 680 case MAIL_SERVER_TIME_TABLE: 681 get_mail_conf_time_table(va_arg(ap, CONFIG_TIME_TABLE *)); 682 break; 683 case MAIL_SERVER_RAW_TABLE: 684 get_mail_conf_raw_table(va_arg(ap, CONFIG_RAW_TABLE *)); 685 break; 686 case MAIL_SERVER_NINT_TABLE: 687 get_mail_conf_nint_table(va_arg(ap, CONFIG_NINT_TABLE *)); 688 break; 689 case MAIL_SERVER_NBOOL_TABLE: 690 get_mail_conf_nbool_table(va_arg(ap, CONFIG_NBOOL_TABLE *)); 691 break; 692 case MAIL_SERVER_PRE_INIT: 693 pre_init = va_arg(ap, MAIL_SERVER_INIT_FN); 694 break; 695 case MAIL_SERVER_POST_INIT: 696 post_init = va_arg(ap, MAIL_SERVER_INIT_FN); 697 break; 698 case MAIL_SERVER_LOOP: 699 loop = va_arg(ap, MAIL_SERVER_LOOP_FN); 700 break; 701 case MAIL_SERVER_EXIT: 702 multi_server_onexit = va_arg(ap, MAIL_SERVER_EXIT_FN); 703 break; 704 case MAIL_SERVER_PRE_ACCEPT: 705 multi_server_pre_accept = va_arg(ap, MAIL_SERVER_ACCEPT_FN); 706 break; 707 case MAIL_SERVER_PRE_DISCONN: 708 multi_server_pre_disconn = va_arg(ap, MAIL_SERVER_DISCONN_FN); 709 break; 710 case MAIL_SERVER_IN_FLOW_DELAY: 711 multi_server_in_flow_delay = 1; 712 break; 713 case MAIL_SERVER_SOLITARY: 714 if (stream == 0 && !alone) 715 msg_fatal("service %s requires a process limit of 1", 716 service_name); 717 break; 718 case MAIL_SERVER_UNLIMITED: 719 if (stream == 0 && !zerolimit) 720 msg_fatal("service %s requires a process limit of 0", 721 service_name); 722 break; 723 case MAIL_SERVER_PRIVILEGED: 724 if (user_name) 725 msg_fatal("service %s requires privileged operation", 726 service_name); 727 break; 728 default: 729 msg_panic("%s: unknown argument type: %d", myname, key); 730 } 731 } 732 va_end(ap); 733 734 if (root_dir) 735 root_dir = var_queue_dir; 736 if (user_name) 737 user_name = var_mail_owner; 738 739 /* 740 * Can options be required? 741 */ 742 if (stream == 0) { 743 if (transport == 0) 744 msg_fatal("no transport type specified"); 745 if (strcasecmp(transport, MASTER_XPORT_NAME_INET) == 0) 746 multi_server_accept = multi_server_accept_inet; 747 else if (strcasecmp(transport, MASTER_XPORT_NAME_UNIX) == 0) 748 multi_server_accept = multi_server_accept_local; 749 #ifdef MASTER_XPORT_NAME_PASS 750 else if (strcasecmp(transport, MASTER_XPORT_NAME_PASS) == 0) 751 multi_server_accept = multi_server_accept_pass; 752 #endif 753 else 754 msg_fatal("unsupported transport type: %s", transport); 755 } 756 757 /* 758 * Retrieve process generation from environment. 759 */ 760 if ((generation = getenv(MASTER_GEN_NAME)) != 0) { 761 if (!alldig(generation)) 762 msg_fatal("bad generation: %s", generation); 763 OCTAL_TO_UNSIGNED(multi_server_generation, generation); 764 if (msg_verbose) 765 msg_info("process generation: %s (%o)", 766 generation, multi_server_generation); 767 } 768 769 /* 770 * Optionally start the debugger on ourself. 771 */ 772 if (debug_me) 773 debug_process(); 774 775 /* 776 * Traditionally, BSD select() can't handle multiple processes selecting 777 * on the same socket, and wakes up every process in select(). See TCP/IP 778 * Illustrated volume 2 page 532. We avoid select() collisions with an 779 * external lock file. 780 */ 781 782 /* 783 * XXX Can't compete for exclusive access to the listen socket because we 784 * also have to monitor existing client connections for service requests. 785 */ 786 #if 0 787 if (stream == 0 && !alone) { 788 lock_path = concatenate(DEF_PID_DIR, "/", transport, 789 ".", service_name, (char *) 0); 790 why = vstring_alloc(1); 791 if ((multi_server_lock = safe_open(lock_path, O_CREAT | O_RDWR, 0600, 792 (struct stat *) 0, -1, -1, why)) == 0) 793 msg_fatal("open lock file %s: %s", lock_path, vstring_str(why)); 794 close_on_exec(vstream_fileno(multi_server_lock), CLOSE_ON_EXEC); 795 myfree(lock_path); 796 vstring_free(why); 797 } 798 #endif 799 800 /* 801 * Set up call-back info. 802 */ 803 multi_server_service = service; 804 multi_server_name = service_name; 805 multi_server_argv = argv + optind; 806 807 /* 808 * Run pre-jail initialization. 809 */ 810 if (chdir(var_queue_dir) < 0) 811 msg_fatal("chdir(\"%s\"): %m", var_queue_dir); 812 if (pre_init) 813 pre_init(multi_server_name, multi_server_argv); 814 815 /* 816 * Optionally, restrict the damage that this process can do. 817 */ 818 resolve_local_init(); 819 tzset(); 820 chroot_uid(root_dir, user_name); 821 822 /* 823 * Run post-jail initialization. 824 */ 825 if (post_init) 826 post_init(multi_server_name, multi_server_argv); 827 828 /* 829 * Are we running as a one-shot server with the client connection on 830 * standard input? If so, make sure the output is written to stdout so as 831 * to satisfy common expectation. 832 */ 833 if (stream != 0) { 834 vstream_control(stream, 835 VSTREAM_CTL_DOUBLE, 836 VSTREAM_CTL_WRITE_FD, STDOUT_FILENO, 837 VSTREAM_CTL_END); 838 service(stream, multi_server_name, multi_server_argv); 839 vstream_fflush(stream); 840 multi_server_exit(); 841 } 842 843 /* 844 * Running as a semi-resident server. Service connection requests. 845 * Terminate when we have serviced a sufficient number of clients, when 846 * no-one has been talking to us for a configurable amount of time, or 847 * when the master process terminated abnormally. 848 */ 849 if (var_idle_limit > 0) 850 event_request_timer(multi_server_timeout, (char *) 0, var_idle_limit); 851 for (fd = MASTER_LISTEN_FD; fd < MASTER_LISTEN_FD + socket_count; fd++) { 852 event_enable_read(fd, multi_server_accept, CAST_INT_TO_CHAR_PTR(fd)); 853 close_on_exec(fd, CLOSE_ON_EXEC); 854 } 855 event_enable_read(MASTER_STATUS_FD, multi_server_abort, (char *) 0); 856 close_on_exec(MASTER_STATUS_FD, CLOSE_ON_EXEC); 857 close_on_exec(MASTER_FLOW_READ, CLOSE_ON_EXEC); 858 close_on_exec(MASTER_FLOW_WRITE, CLOSE_ON_EXEC); 859 watchdog = watchdog_create(var_daemon_timeout, (WATCHDOG_FN) 0, (char *) 0); 860 861 /* 862 * The event loop, at last. 863 */ 864 while (var_use_limit == 0 || use_count < var_use_limit || client_count > 0) { 865 if (multi_server_lock != 0) { 866 watchdog_stop(watchdog); 867 if (myflock(vstream_fileno(multi_server_lock), INTERNAL_LOCK, 868 MYFLOCK_OP_EXCLUSIVE) < 0) 869 msg_fatal("select lock: %m"); 870 } 871 watchdog_start(watchdog); 872 delay = loop ? loop(multi_server_name, multi_server_argv) : -1; 873 event_loop(delay); 874 } 875 multi_server_exit(); 876 } 877