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