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