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