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