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