1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * NOTES: To be expanded. 30 * 31 * The SMF inetd. 32 * 33 * Below are some high level notes of the operation of the SMF inetd. The 34 * notes don't go into any real detail, and the viewer of this file is 35 * encouraged to look at the code and its associated comments to better 36 * understand inetd's operation. This saves the potential for the code 37 * and these notes diverging over time. 38 * 39 * Inetd's major work is done from the context of event_loop(). Within this 40 * loop, inetd polls for events arriving from a number of different file 41 * descriptors, representing the following event types, and initiates 42 * any necessary event processing: 43 * - incoming network connections/datagrams. 44 * - notification of terminated processes (discovered via contract events). 45 * - instance specific events originating from the SMF master restarter. 46 * - stop/refresh requests from the inetd method processes (coming in on a 47 * Unix Domain socket). 48 * There's also a timeout set for the poll, which is set to the nearest 49 * scheduled timer in a timer queue that inetd uses to perform delayed 50 * processing, such as bind retries. 51 * The SIGHUP and SIGINT signals can also interrupt the poll, and will 52 * result in inetd being refreshed or stopped respectively, as was the 53 * behavior with the old inetd. 54 * 55 * Inetd implements a state machine for each instance. The states within the 56 * machine are: offline, online, disabled, maintenance, uninitialized and 57 * specializations of the offline state for when an instance exceeds one of 58 * its DOS limits. The state of an instance can be changed as a 59 * result/side-effect of one of the above events occurring, or inetd being 60 * started up. The ongoing state of an instance is stored in the SMF 61 * repository, as required of SMF restarters. This enables an administrator 62 * to view the state of each instance, and, if inetd was to terminate 63 * unexpectedly, it could use the stored state to re-commence where it left off. 64 * 65 * Within the state machine a number of methods are run (if provided) as part 66 * of a state transition to aid/ effect a change in an instance's state. The 67 * supported methods are: offline, online, disable, refresh and start. The 68 * latter of these is the equivalent of the server program and its arguments 69 * in the old inetd. 70 * 71 * Events from the SMF master restarter come in on a number of threads 72 * created in the registration routine of librestart, the delegated restarter 73 * library. These threads call into the restart_event_proxy() function 74 * when an event arrives. To serialize the processing of instances, these events 75 * are then written down a pipe to the process's main thread, which listens 76 * for these events via a poll call, with the file descriptor of the other 77 * end of the pipe in its read set, and processes the event appropriately. 78 * When the event has been processed (which may be delayed if the instance 79 * for which the event is for is in the process of executing one of its methods 80 * as part of a state transition) it writes an acknowledgement back down the 81 * pipe the event was received on. The thread in restart_event_proxy() that 82 * wrote the event will read the acknowledgement it was blocked upon, and will 83 * then be able to return to its caller, thus implicitly acknowledging the 84 * event, and allowing another event to be written down the pipe for the main 85 * thread to process. 86 */ 87 88 89 #include <netdb.h> 90 #include <stdio.h> 91 #include <stdio_ext.h> 92 #include <stdlib.h> 93 #include <strings.h> 94 #include <unistd.h> 95 #include <assert.h> 96 #include <sys/types.h> 97 #include <sys/socket.h> 98 #include <netinet/in.h> 99 #include <fcntl.h> 100 #include <signal.h> 101 #include <errno.h> 102 #include <locale.h> 103 #include <syslog.h> 104 #include <libintl.h> 105 #include <librestart.h> 106 #include <pthread.h> 107 #include <sys/stat.h> 108 #include <time.h> 109 #include <limits.h> 110 #include <libgen.h> 111 #include <tcpd.h> 112 #include <libscf.h> 113 #include <libuutil.h> 114 #include <stddef.h> 115 #include <bsm/adt_event.h> 116 #include <ucred.h> 117 #include "inetd_impl.h" 118 119 /* path to inetd's binary */ 120 #define INETD_PATH "/usr/lib/inet/inetd" 121 122 /* 123 * inetd's default configuration file paths. /etc/inetd/inetd.conf is set 124 * be be the primary file, so it is checked before /etc/inetd.conf. 125 */ 126 #define PRIMARY_DEFAULT_CONF_FILE "/etc/inet/inetd.conf" 127 #define SECONDARY_DEFAULT_CONF_FILE "/etc/inetd.conf" 128 129 /* Arguments passed to this binary to request which method to execute. */ 130 #define START_METHOD_ARG "start" 131 #define STOP_METHOD_ARG "stop" 132 #define REFRESH_METHOD_ARG "refresh" 133 134 /* connection backlog for unix domain socket */ 135 #define UDS_BACKLOG 2 136 137 /* number of retries to recv() a request on the UDS socket before giving up */ 138 #define UDS_RECV_RETRIES 10 139 140 /* enumeration of the different ends of a pipe */ 141 enum pipe_end { 142 PE_CONSUMER, 143 PE_PRODUCER 144 }; 145 146 typedef struct { 147 internal_inst_state_t istate; 148 const char *name; 149 restarter_instance_state_t smf_state; 150 instance_method_t method_running; 151 } state_info_t; 152 153 154 /* 155 * Collection of information for each state. 156 * NOTE: This table is indexed into using the internal_inst_state_t 157 * enumeration, so the ordering needs to be kept in synch. 158 */ 159 static state_info_t states[] = { 160 {IIS_UNINITIALIZED, "uninitialized", RESTARTER_STATE_UNINIT, 161 IM_NONE}, 162 {IIS_ONLINE, "online", RESTARTER_STATE_ONLINE, IM_START}, 163 {IIS_IN_ONLINE_METHOD, "online_method", RESTARTER_STATE_OFFLINE, 164 IM_ONLINE}, 165 {IIS_OFFLINE, "offline", RESTARTER_STATE_OFFLINE, IM_NONE}, 166 {IIS_IN_OFFLINE_METHOD, "offline_method", RESTARTER_STATE_OFFLINE, 167 IM_OFFLINE}, 168 {IIS_DISABLED, "disabled", RESTARTER_STATE_DISABLED, IM_NONE}, 169 {IIS_IN_DISABLE_METHOD, "disabled_method", RESTARTER_STATE_OFFLINE, 170 IM_DISABLE}, 171 {IIS_IN_REFRESH_METHOD, "refresh_method", RESTARTER_STATE_ONLINE, 172 IM_REFRESH}, 173 {IIS_MAINTENANCE, "maintenance", RESTARTER_STATE_MAINT, IM_NONE}, 174 {IIS_OFFLINE_CONRATE, "cr_offline", RESTARTER_STATE_OFFLINE, IM_NONE}, 175 {IIS_OFFLINE_BIND, "bind_offline", RESTARTER_STATE_OFFLINE, IM_NONE}, 176 {IIS_OFFLINE_COPIES, "copies_offline", RESTARTER_STATE_OFFLINE, 177 IM_NONE}, 178 {IIS_DEGRADED, "degraded", RESTARTER_STATE_DEGRADED, IM_NONE}, 179 {IIS_NONE, "none", RESTARTER_STATE_NONE, IM_NONE} 180 }; 181 182 /* 183 * Pipe used to send events from the threads created by restarter_bind_handle() 184 * to the main thread of control. 185 */ 186 static int rst_event_pipe[] = {-1, -1}; 187 /* 188 * Used to protect the critical section of code in restarter_event_proxy() that 189 * involves writing an event down the event pipe and reading an acknowledgement. 190 */ 191 static pthread_mutex_t rst_event_pipe_mtx = PTHREAD_MUTEX_INITIALIZER; 192 193 /* handle used in communication with the master restarter */ 194 static restarter_event_handle_t *rst_event_handle = NULL; 195 196 /* set to indicate a refresh of inetd is requested */ 197 static boolean_t refresh_inetd_requested = B_FALSE; 198 199 /* set by the SIGTERM handler to flag we got a SIGTERM */ 200 static boolean_t got_sigterm = B_FALSE; 201 202 /* 203 * Timer queue used to store timers for delayed event processing, such as 204 * bind retries. 205 */ 206 iu_tq_t *timer_queue = NULL; 207 208 /* 209 * fd of Unix Domain socket used to communicate stop and refresh requests 210 * to the inetd start method process. 211 */ 212 static int uds_fd = -1; 213 214 /* 215 * List of inetd's currently managed instances; each containing its state, 216 * and in certain states its configuration. 217 */ 218 static uu_list_pool_t *instance_pool = NULL; 219 uu_list_t *instance_list = NULL; 220 221 /* set to indicate we're being stopped */ 222 boolean_t inetd_stopping = B_FALSE; 223 224 /* TCP wrappers syslog globals. Consumed by libwrap. */ 225 int allow_severity = LOG_INFO; 226 int deny_severity = LOG_WARNING; 227 228 /* path of the configuration file being monitored by check_conf_file() */ 229 static char *conf_file = NULL; 230 231 /* Auditing session handle */ 232 static adt_session_data_t *audit_handle; 233 234 static void uds_fini(void); 235 static int uds_init(void); 236 static int run_method(instance_t *, instance_method_t, const proto_info_t *); 237 static void create_bound_fds(instance_t *); 238 static void destroy_bound_fds(instance_t *); 239 static void destroy_instance(instance_t *); 240 static void inetd_stop(void); 241 static void 242 exec_method(instance_t *instance, instance_method_t method, method_info_t *mi, 243 struct method_context *mthd_ctxt, const proto_info_t *pi) __NORETURN; 244 245 /* 246 * The following two functions are callbacks that libumem uses to determine 247 * inetd's desired debugging/logging levels. The interface they consume is 248 * exported by FMA and is consolidation private. The comments in the two 249 * functions give the environment variable that will effectively be set to 250 * their returned value, and thus whose behavior for this value, described in 251 * umem_debug(3MALLOC), will be followed. 252 */ 253 254 const char * 255 _umem_debug_init(void) 256 { 257 return ("default,verbose"); /* UMEM_DEBUG setting */ 258 } 259 260 const char * 261 _umem_logging_init(void) 262 { 263 return ("fail,contents"); /* UMEM_LOGGING setting */ 264 } 265 266 static void 267 log_invalid_cfg(const char *fmri) 268 { 269 error_msg(gettext( 270 "Invalid configuration for instance %s, placing in maintenance"), 271 fmri); 272 } 273 274 /* 275 * Returns B_TRUE if the instance is in a suitable state for inetd to stop. 276 */ 277 static boolean_t 278 instance_stopped(const instance_t *inst) 279 { 280 return ((inst->cur_istate == IIS_OFFLINE) || 281 (inst->cur_istate == IIS_MAINTENANCE) || 282 (inst->cur_istate == IIS_DISABLED) || 283 (inst->cur_istate == IIS_UNINITIALIZED)); 284 } 285 286 /* 287 * Updates the current and next repository states of instance 'inst'. If 288 * any errors occur an error message is output. 289 */ 290 static void 291 update_instance_states(instance_t *inst, internal_inst_state_t new_cur_state, 292 internal_inst_state_t new_next_state, restarter_error_t err) 293 { 294 internal_inst_state_t old_cur = inst->cur_istate; 295 internal_inst_state_t old_next = inst->next_istate; 296 scf_error_t sret; 297 int ret; 298 299 debug_msg("Entering update_instance_states: oldcur: %s, newcur: %s " 300 "oldnext: %s, newnext: %s", states[old_cur].name, 301 states[new_cur_state].name, states[old_next].name, 302 states[new_next_state].name); 303 304 305 /* update the repository/cached internal state */ 306 inst->cur_istate = new_cur_state; 307 inst->next_istate = new_next_state; 308 (void) set_single_rep_val(inst->cur_istate_rep, 309 (int64_t)new_cur_state); 310 (void) set_single_rep_val(inst->next_istate_rep, 311 (int64_t)new_next_state); 312 313 if (((sret = store_rep_vals(inst->cur_istate_rep, inst->fmri, 314 PR_NAME_CUR_INT_STATE)) != 0) || 315 ((sret = store_rep_vals(inst->next_istate_rep, inst->fmri, 316 PR_NAME_NEXT_INT_STATE)) != 0)) 317 error_msg(gettext("Failed to update state of instance %s in " 318 "repository: %s"), inst->fmri, scf_strerror(sret)); 319 320 /* update the repository SMF state */ 321 if ((ret = restarter_set_states(rst_event_handle, inst->fmri, 322 states[old_cur].smf_state, states[new_cur_state].smf_state, 323 states[old_next].smf_state, states[new_next_state].smf_state, 324 err, 0)) != 0) 325 error_msg(gettext("Failed to update state of instance %s in " 326 "repository: %s"), inst->fmri, strerror(ret)); 327 328 } 329 330 void 331 update_state(instance_t *inst, internal_inst_state_t new_cur, 332 restarter_error_t err) 333 { 334 update_instance_states(inst, new_cur, IIS_NONE, err); 335 } 336 337 /* 338 * Sends a refresh event to the inetd start method process and returns 339 * SMF_EXIT_OK if it managed to send it. If it fails to send the request for 340 * some reason it returns SMF_EXIT_ERR_OTHER. 341 */ 342 static int 343 refresh_method(void) 344 { 345 uds_request_t req = UR_REFRESH_INETD; 346 int fd; 347 348 debug_msg("Entering refresh_method"); 349 350 if ((fd = connect_to_inetd()) < 0) { 351 error_msg(gettext("Failed to connect to inetd: %s"), 352 strerror(errno)); 353 return (SMF_EXIT_ERR_OTHER); 354 } 355 356 /* write the request and return success */ 357 if (safe_write(fd, &req, sizeof (req)) == -1) { 358 error_msg( 359 gettext("Failed to send refresh request to inetd: %s"), 360 strerror(errno)); 361 (void) close(fd); 362 return (SMF_EXIT_ERR_OTHER); 363 } 364 365 (void) close(fd); 366 367 return (SMF_EXIT_OK); 368 } 369 370 /* 371 * Sends a stop event to the inetd start method process and wait till it goes 372 * away. If inetd is determined to have stopped SMF_EXIT_OK is returned, else 373 * SMF_EXIT_ERR_OTHER is returned. 374 */ 375 static int 376 stop_method(void) 377 { 378 uds_request_t req = UR_STOP_INETD; 379 int fd; 380 char c; 381 ssize_t ret; 382 383 debug_msg("Entering stop_method"); 384 385 if ((fd = connect_to_inetd()) == -1) { 386 debug_msg(gettext("Failed to connect to inetd: %s"), 387 strerror(errno)); 388 /* 389 * Assume connect_to_inetd() failed because inetd was already 390 * stopped, and return success. 391 */ 392 return (SMF_EXIT_OK); 393 } 394 395 /* 396 * This is safe to do since we're fired off in a separate process 397 * than inetd and in the case we get wedged, the stop method timeout 398 * will occur and we'd be killed by our restarter. 399 */ 400 enable_blocking(fd); 401 402 /* write the stop request to inetd and wait till it goes away */ 403 if (safe_write(fd, &req, sizeof (req)) != 0) { 404 error_msg(gettext("Failed to send stop request to inetd")); 405 (void) close(fd); 406 return (SMF_EXIT_ERR_OTHER); 407 } 408 409 /* wait until remote end of socket is closed */ 410 while (((ret = recv(fd, &c, sizeof (c), 0)) != 0) && (errno == EINTR)) 411 ; 412 413 (void) close(fd); 414 415 if (ret != 0) { 416 error_msg(gettext("Failed to determine whether inetd stopped")); 417 return (SMF_EXIT_ERR_OTHER); 418 } 419 420 return (SMF_EXIT_OK); 421 } 422 423 424 /* 425 * This function is called to handle restarter events coming in from the 426 * master restarter. It is registered with the master restarter via 427 * restarter_bind_handle() and simply passes a pointer to the event down 428 * the event pipe, which will be discovered by the poll in the event loop 429 * and processed there. It waits for an acknowledgement to be written back down 430 * the pipe before returning. 431 * Writing a pointer to the function's 'event' parameter down the pipe will 432 * be safe, as the thread in restarter_event_proxy() doesn't return until 433 * the main thread has finished its processing of the passed event, thus 434 * the referenced event will remain around until the function returns. 435 * To impose the limit of only one event being in the pipe and processed 436 * at once, a lock is taken on entry to this function and returned on exit. 437 * Always returns 0. 438 */ 439 static int 440 restarter_event_proxy(restarter_event_t *event) 441 { 442 restarter_event_type_t ev_type; 443 boolean_t processed; 444 445 debug_msg("Entering restarter_event_proxy"); 446 ev_type = restarter_event_get_type(event); 447 debug_msg("event: %x, event type: %d", event, ev_type); 448 449 (void) pthread_mutex_lock(&rst_event_pipe_mtx); 450 451 /* write the event to the main worker thread down the pipe */ 452 if (safe_write(rst_event_pipe[PE_PRODUCER], &event, 453 sizeof (event)) != 0) 454 goto pipe_error; 455 456 /* 457 * Wait for an acknowledgement that the event has been processed from 458 * the same pipe. In the case that inetd is stopping, any thread in 459 * this function will simply block on this read until inetd eventually 460 * exits. This will result in this function not returning success to 461 * its caller, and the event that was being processed when the 462 * function exited will be re-sent when inetd is next started. 463 */ 464 if (safe_read(rst_event_pipe[PE_PRODUCER], &processed, 465 sizeof (processed)) != 0) 466 goto pipe_error; 467 468 (void) pthread_mutex_unlock(&rst_event_pipe_mtx); 469 470 return (processed ? 0 : EAGAIN); 471 472 pipe_error: 473 /* 474 * Something's seriously wrong with the event pipe. Notify the 475 * worker thread by closing this end of the event pipe and pause till 476 * inetd exits. 477 */ 478 error_msg(gettext("Can't process restarter events: %s"), 479 strerror(errno)); 480 (void) close(rst_event_pipe[PE_PRODUCER]); 481 for (;;) 482 (void) pause(); 483 484 /* NOTREACHED */ 485 } 486 487 /* 488 * Let restarter_event_proxy() know we're finished with the event it's blocked 489 * upon. The 'processed' argument denotes whether we successfully processed the 490 * event. 491 */ 492 static void 493 ack_restarter_event(boolean_t processed) 494 { 495 debug_msg("Entering ack_restarter_event"); 496 497 /* 498 * If safe_write returns -1 something's seriously wrong with the event 499 * pipe, so start the shutdown proceedings. 500 */ 501 if (safe_write(rst_event_pipe[PE_CONSUMER], &processed, 502 sizeof (processed)) == -1) 503 inetd_stop(); 504 } 505 506 /* 507 * Switch the syslog identification string to 'ident'. 508 */ 509 static void 510 change_syslog_ident(const char *ident) 511 { 512 debug_msg("Entering change_syslog_ident: ident: %s", ident); 513 514 closelog(); 515 openlog(ident, LOG_PID|LOG_CONS, LOG_DAEMON); 516 } 517 518 /* 519 * Perform TCP wrappers checks on this instance. Due to the fact that the 520 * current wrappers code used in Solaris is taken untouched from the open 521 * source version, we're stuck with using the daemon name for the checks, as 522 * opposed to making use of instance FMRIs. Sigh. 523 * Returns B_TRUE if the check passed, else B_FALSE. 524 */ 525 static boolean_t 526 tcp_wrappers_ok(instance_t *instance) 527 { 528 boolean_t rval = B_TRUE; 529 char *daemon_name; 530 basic_cfg_t *cfg = instance->config->basic; 531 struct request_info req; 532 533 debug_msg("Entering tcp_wrappers_ok, instance: %s", instance->fmri); 534 535 /* 536 * Wrap the service using libwrap functions. The code below implements 537 * the functionality of tcpd. This is done only for stream,nowait 538 * services, following the convention of other vendors. udp/dgram and 539 * stream/wait can NOT be wrapped with this libwrap, so be wary of 540 * changing the test below. 541 */ 542 if (cfg->do_tcp_wrappers && !cfg->iswait && !cfg->istlx) { 543 544 daemon_name = instance->config->methods[ 545 IM_START]->exec_args_we.we_wordv[0]; 546 if (*daemon_name == '/') 547 daemon_name = strrchr(daemon_name, '/') + 1; 548 549 /* 550 * Change the syslog message identity to the name of the 551 * daemon being wrapped, as opposed to "inetd". 552 */ 553 change_syslog_ident(daemon_name); 554 555 (void) request_init(&req, RQ_DAEMON, daemon_name, RQ_FILE, 556 instance->conn_fd, NULL); 557 fromhost(&req); 558 559 if (strcasecmp(eval_hostname(req.client), paranoid) == 0) { 560 syslog(deny_severity, 561 "refused connect from %s (name/address mismatch)", 562 eval_client(&req)); 563 if (req.sink != NULL) 564 req.sink(instance->conn_fd); 565 rval = B_FALSE; 566 } else if (!hosts_access(&req)) { 567 syslog(deny_severity, 568 "refused connect from %s (access denied)", 569 eval_client(&req)); 570 if (req.sink != NULL) 571 req.sink(instance->conn_fd); 572 rval = B_FALSE; 573 } else { 574 syslog(allow_severity, "connect from %s", 575 eval_client(&req)); 576 } 577 578 /* Revert syslog identity back to "inetd". */ 579 change_syslog_ident(SYSLOG_IDENT); 580 } 581 return (rval); 582 } 583 584 /* 585 * Handler registered with the timer queue code to remove an instance from 586 * the connection rate offline state when it has been there for its allotted 587 * time. 588 */ 589 /* ARGSUSED */ 590 static void 591 conn_rate_online(iu_tq_t *tq, void *arg) 592 { 593 instance_t *instance = arg; 594 595 debug_msg("Entering conn_rate_online, instance: %s", 596 instance->fmri); 597 598 assert(instance->cur_istate == IIS_OFFLINE_CONRATE); 599 instance->timer_id = -1; 600 update_state(instance, IIS_OFFLINE, RERR_RESTART); 601 process_offline_inst(instance); 602 } 603 604 /* 605 * Check whether this instance in the offline state is in transition to 606 * another state and do the work to continue this transition. 607 */ 608 void 609 process_offline_inst(instance_t *inst) 610 { 611 debug_msg("Entering process_offline_inst"); 612 613 if (inst->disable_req) { 614 inst->disable_req = B_FALSE; 615 (void) run_method(inst, IM_DISABLE, NULL); 616 } else if (inst->maintenance_req) { 617 inst->maintenance_req = B_FALSE; 618 update_state(inst, IIS_MAINTENANCE, RERR_RESTART); 619 /* 620 * If inetd is in the process of stopping, we don't want to enter 621 * any states but offline, disabled and maintenance. 622 */ 623 } else if (!inetd_stopping) { 624 if (inst->conn_rate_exceeded) { 625 basic_cfg_t *cfg = inst->config->basic; 626 627 inst->conn_rate_exceeded = B_FALSE; 628 update_state(inst, IIS_OFFLINE_CONRATE, RERR_RESTART); 629 /* 630 * Schedule a timer to bring the instance out of the 631 * connection rate offline state. 632 */ 633 inst->timer_id = iu_schedule_timer(timer_queue, 634 cfg->conn_rate_offline, conn_rate_online, 635 inst); 636 if (inst->timer_id == -1) { 637 error_msg(gettext("%s unable to set timer, " 638 "won't be brought on line after %d " 639 "seconds."), inst->fmri, 640 cfg->conn_rate_offline); 641 } 642 643 } else if (copies_limit_exceeded(inst)) { 644 update_state(inst, IIS_OFFLINE_COPIES, RERR_RESTART); 645 } 646 } 647 } 648 649 /* 650 * Create a socket bound to the instance's configured address. If the 651 * bind fails, returns -1, else the fd of the bound socket. 652 */ 653 static int 654 create_bound_socket(const char *fmri, socket_info_t *sock_info) 655 { 656 int fd; 657 int on = 1; 658 rpc_info_t *rpc = sock_info->pr_info.ri; 659 const char *proto = sock_info->pr_info.proto; 660 661 debug_msg("Entering create_bound_socket"); 662 663 fd = socket(sock_info->local_addr.ss_family, sock_info->type, 664 sock_info->protocol); 665 if (fd < 0) { 666 error_msg(gettext( 667 "Socket creation failure for instance %s, proto %s: %s"), 668 fmri, proto, strerror(errno)); 669 return (-1); 670 } 671 672 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) == -1) { 673 error_msg(gettext("setsockopt SO_REUSEADDR failed for service " 674 "instance %s, proto %s: %s"), fmri, proto, strerror(errno)); 675 (void) close(fd); 676 return (-1); 677 } 678 if (sock_info->pr_info.v6only) { 679 /* restrict socket to IPv6 communications only */ 680 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, 681 sizeof (on)) == -1) { 682 error_msg(gettext("setsockopt IPV6_V6ONLY failed for " 683 "service instance %s, proto %s: %s"), fmri, proto, 684 strerror(errno)); 685 (void) close(fd); 686 return (-1); 687 } 688 } 689 690 if (rpc != NULL) 691 SS_SETPORT(sock_info->local_addr, 0); 692 693 if (bind(fd, (struct sockaddr *)&(sock_info->local_addr), 694 SS_ADDRLEN(sock_info->local_addr)) < 0) { 695 error_msg(gettext( 696 "Failed to bind to the port of service instance %s, " 697 "proto %s: %s"), fmri, proto, strerror(errno)); 698 (void) close(fd); 699 return (-1); 700 } 701 702 /* 703 * Retrieve and store the address bound to for RPC services. 704 */ 705 if (rpc != NULL) { 706 struct sockaddr_storage ss; 707 int ss_size = sizeof (ss); 708 709 if (getsockname(fd, (struct sockaddr *)&ss, &ss_size) < 0) { 710 error_msg(gettext("Failed getsockname for instance %s, " 711 "proto %s: %s"), fmri, proto, strerror(errno)); 712 (void) close(fd); 713 return (-1); 714 } 715 (void) memcpy(rpc->netbuf.buf, &ss, 716 sizeof (struct sockaddr_storage)); 717 rpc->netbuf.len = SS_ADDRLEN(ss); 718 rpc->netbuf.maxlen = SS_ADDRLEN(ss); 719 } 720 721 if (sock_info->type == SOCK_STREAM) 722 (void) listen(fd, CONNECTION_BACKLOG); 723 724 return (fd); 725 } 726 727 /* 728 * Handler registered with the timer queue code to retry the creation 729 * of a bound fd. 730 */ 731 /* ARGSUSED */ 732 static void 733 retry_bind(iu_tq_t *tq, void *arg) 734 { 735 instance_t *instance = arg; 736 737 debug_msg("Entering retry_bind, instance: %s", instance->fmri); 738 739 switch (instance->cur_istate) { 740 case IIS_OFFLINE_BIND: 741 case IIS_ONLINE: 742 case IIS_DEGRADED: 743 case IIS_IN_ONLINE_METHOD: 744 case IIS_IN_REFRESH_METHOD: 745 break; 746 default: 747 #ifndef NDEBUG 748 (void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n", 749 __FILE__, __LINE__, instance->cur_istate); 750 #endif 751 abort(); 752 } 753 754 instance->bind_timer_id = -1; 755 create_bound_fds(instance); 756 } 757 758 /* 759 * For each of the fds for the given instance that are bound, if 'listen' is 760 * set add them to the poll set, else remove them from it. If any additions 761 * fail, returns -1, else 0 on success. 762 */ 763 int 764 poll_bound_fds(instance_t *instance, boolean_t listen) 765 { 766 basic_cfg_t *cfg = instance->config->basic; 767 proto_info_t *pi; 768 int ret = 0; 769 770 debug_msg("Entering poll_bound_fds: instance: %s, on: %d", 771 instance->fmri, listen); 772 773 for (pi = uu_list_first(cfg->proto_list); pi != NULL; 774 pi = uu_list_next(cfg->proto_list, pi)) { 775 if (pi->listen_fd != -1) { /* fd bound */ 776 if (!listen) { 777 clear_pollfd(pi->listen_fd); 778 } else if (set_pollfd(pi->listen_fd, POLLIN) == -1) { 779 ret = -1; 780 } 781 } 782 } 783 784 return (ret); 785 } 786 787 /* 788 * Handle the case were we either fail to create a bound fd or we fail 789 * to add a bound fd to the poll set for the given instance. 790 */ 791 static void 792 handle_bind_failure(instance_t *instance) 793 { 794 basic_cfg_t *cfg = instance->config->basic; 795 796 debug_msg("Entering handle_bind_failure: instance: %s", instance); 797 798 /* 799 * We must be being called as a result of a failed poll_bound_fds() 800 * as a bind retry is already scheduled. Just return and let it do 801 * the work. 802 */ 803 if (instance->bind_timer_id != -1) 804 return; 805 806 /* 807 * Check if the rebind retries limit is operative and if so, 808 * if it has been reached. 809 */ 810 if (((cfg->bind_fail_interval <= 0) || /* no retries */ 811 ((cfg->bind_fail_max >= 0) && /* limit reached */ 812 (++instance->bind_fail_count > cfg->bind_fail_max))) || 813 ((instance->bind_timer_id = iu_schedule_timer(timer_queue, 814 cfg->bind_fail_interval, retry_bind, instance)) == -1)) { 815 proto_info_t *pi; 816 817 instance->bind_fail_count = 0; 818 819 switch (instance->cur_istate) { 820 case IIS_DEGRADED: 821 case IIS_ONLINE: 822 /* check if any of the fds are being poll'd upon */ 823 for (pi = uu_list_first(cfg->proto_list); pi != NULL; 824 pi = uu_list_next(cfg->proto_list, pi)) { 825 if ((pi->listen_fd != -1) && 826 (find_pollfd(pi->listen_fd) != NULL)) 827 break; 828 } 829 if (pi != NULL) { /* polling on > 0 fds */ 830 warn_msg(gettext("Failed to bind on " 831 "all protocols for instance %s, " 832 "transitioning to degraded"), 833 instance->fmri); 834 update_state(instance, IIS_DEGRADED, RERR_NONE); 835 instance->bind_retries_exceeded = B_TRUE; 836 break; 837 } 838 839 destroy_bound_fds(instance); 840 /* 841 * In the case we failed the 'bind' because set_pollfd() 842 * failed on all bound fds, use the offline handling. 843 */ 844 /* FALLTHROUGH */ 845 case IIS_OFFLINE: 846 case IIS_OFFLINE_BIND: 847 error_msg(gettext("Too many bind failures for instance " 848 "%s, transitioning to maintenance"), instance->fmri); 849 update_state(instance, IIS_MAINTENANCE, 850 RERR_FAULT); 851 break; 852 case IIS_IN_ONLINE_METHOD: 853 case IIS_IN_REFRESH_METHOD: 854 warn_msg(gettext("Failed to bind on all " 855 "protocols for instance %s, instance will go to " 856 "degraded"), instance->fmri); 857 /* 858 * Set the retries exceeded flag so when the method 859 * completes the instance goes to the degraded state. 860 */ 861 instance->bind_retries_exceeded = B_TRUE; 862 break; 863 default: 864 #ifndef NDEBUG 865 (void) fprintf(stderr, 866 "%s:%d: Unknown instance state %d.\n", 867 __FILE__, __LINE__, instance->cur_istate); 868 #endif 869 abort(); 870 } 871 } else if (instance->cur_istate == IIS_OFFLINE) { 872 /* 873 * bind re-scheduled, so if we're offline reflect this in the 874 * state. 875 */ 876 update_state(instance, IIS_OFFLINE_BIND, RERR_NONE); 877 } 878 } 879 880 881 /* 882 * Check if two transport protocols for RPC conflict. 883 */ 884 885 boolean_t 886 is_rpc_proto_conflict(const char *proto0, const char *proto1) { 887 if (strcmp(proto0, "tcp") == 0) { 888 if (strcmp(proto1, "tcp") == 0) 889 return (B_TRUE); 890 if (strcmp(proto1, "tcp6") == 0) 891 return (B_TRUE); 892 return (B_FALSE); 893 } 894 895 if (strcmp(proto0, "tcp6") == 0) { 896 if (strcmp(proto1, "tcp") == 0) 897 return (B_TRUE); 898 if (strcmp(proto1, "tcp6only") == 0) 899 return (B_TRUE); 900 if (strcmp(proto1, "tcp6") == 0) 901 return (B_TRUE); 902 return (B_FALSE); 903 } 904 905 if (strcmp(proto0, "tcp6only") == 0) { 906 if (strcmp(proto1, "tcp6only") == 0) 907 return (B_TRUE); 908 if (strcmp(proto1, "tcp6") == 0) 909 return (B_TRUE); 910 return (B_FALSE); 911 } 912 913 if (strcmp(proto0, "udp") == 0) { 914 if (strcmp(proto1, "udp") == 0) 915 return (B_TRUE); 916 if (strcmp(proto1, "udp6") == 0) 917 return (B_TRUE); 918 return (B_FALSE); 919 } 920 921 if (strcmp(proto0, "udp6") == 0) { 922 923 if (strcmp(proto1, "udp") == 0) 924 return (B_TRUE); 925 if (strcmp(proto1, "udp6only") == 0) 926 return (B_TRUE); 927 if (strcmp(proto1, "udp6") == 0) 928 return (B_TRUE); 929 return (B_FALSE); 930 } 931 932 if (strcmp(proto0, "udp6only") == 0) { 933 934 if (strcmp(proto1, "udp6only") == 0) 935 return (B_TRUE); 936 if (strcmp(proto1, "udp6") == 0) 937 return (B_TRUE); 938 return (0); 939 } 940 941 /* 942 * If the protocol isn't TCP/IP or UDP/IP assume that it has its own 943 * port namepsace and that conflicts can be detected by literal string 944 * comparison. 945 */ 946 947 if (strcmp(proto0, proto1)) 948 return (FALSE); 949 950 return (B_TRUE); 951 } 952 953 954 /* 955 * Check if inetd thinks this RPC program number is already registered. 956 * 957 * An RPC protocol conflict occurs if 958 * a) the program numbers are the same and, 959 * b) the version numbers overlap, 960 * c) the protocols (TCP vs UDP vs tic*) are the same. 961 */ 962 963 boolean_t 964 is_rpc_num_in_use(int rpc_n, char *proto, int lowver, int highver) { 965 instance_t *i; 966 basic_cfg_t *cfg; 967 proto_info_t *pi; 968 969 for (i = uu_list_first(instance_list); i != NULL; 970 i = uu_list_next(instance_list, i)) { 971 972 if (i->cur_istate != IIS_ONLINE) 973 continue; 974 cfg = i->config->basic; 975 976 for (pi = uu_list_first(cfg->proto_list); pi != NULL; 977 pi = uu_list_next(cfg->proto_list, pi)) { 978 979 if (pi->ri == NULL) 980 continue; 981 if (pi->ri->prognum != rpc_n) 982 continue; 983 if (!is_rpc_proto_conflict(pi->proto, proto)) 984 continue; 985 if ((lowver < pi->ri->lowver && 986 highver < pi->ri->lowver) || 987 (lowver > pi->ri->highver && 988 highver > pi->ri->highver)) 989 continue; 990 return (B_TRUE); 991 } 992 } 993 return (B_FALSE); 994 } 995 996 997 /* 998 * Independent of the transport, for each of the entries in the instance's 999 * proto list this function first attempts to create an associated network fd; 1000 * for RPC services these are then bound to a kernel chosen port and the 1001 * fd is registered with rpcbind; for non-RPC services the fds are bound 1002 * to the port associated with the instance's service name. On any successful 1003 * binds the instance is taken online. Failed binds are handled by 1004 * handle_bind_failure(). 1005 */ 1006 void 1007 create_bound_fds(instance_t *instance) 1008 { 1009 basic_cfg_t *cfg = instance->config->basic; 1010 boolean_t failure = B_FALSE; 1011 boolean_t success = B_FALSE; 1012 proto_info_t *pi; 1013 1014 debug_msg("Entering create_bound_fd: instance: %s", instance->fmri); 1015 1016 /* 1017 * Loop through and try and bind any unbound protos. 1018 */ 1019 for (pi = uu_list_first(cfg->proto_list); pi != NULL; 1020 pi = uu_list_next(cfg->proto_list, pi)) { 1021 if (pi->listen_fd != -1) 1022 continue; 1023 if (cfg->istlx) { 1024 pi->listen_fd = create_bound_endpoint(instance->fmri, 1025 (tlx_info_t *)pi); 1026 } else { 1027 /* 1028 * We cast pi to a void so we can then go on to cast 1029 * it to a socket_info_t without lint complaining 1030 * about alignment. This is done because the x86 1031 * version of lint thinks a lint suppression directive 1032 * is unnecessary and flags it as such, yet the sparc 1033 * version complains if it's absent. 1034 */ 1035 void *p = pi; 1036 pi->listen_fd = create_bound_socket(instance->fmri, 1037 (socket_info_t *)p); 1038 } 1039 if (pi->listen_fd == -1) { 1040 failure = B_TRUE; 1041 continue; 1042 } 1043 1044 if (pi->ri != NULL) { 1045 1046 /* 1047 * Don't register the same RPC program number twice. 1048 * Doing so silently discards the old service 1049 * without causing an error. 1050 */ 1051 if (is_rpc_num_in_use(pi->ri->prognum, pi->proto, 1052 pi->ri->lowver, pi->ri->highver)) { 1053 failure = B_TRUE; 1054 close_net_fd(instance, pi->listen_fd); 1055 pi->listen_fd = -1; 1056 continue; 1057 } 1058 1059 unregister_rpc_service(instance->fmri, pi->ri); 1060 if (register_rpc_service(instance->fmri, pi->ri) == 1061 -1) { 1062 close_net_fd(instance, pi->listen_fd); 1063 pi->listen_fd = -1; 1064 failure = B_TRUE; 1065 continue; 1066 } 1067 } 1068 1069 success = B_TRUE; 1070 } 1071 1072 switch (instance->cur_istate) { 1073 case IIS_OFFLINE: 1074 case IIS_OFFLINE_BIND: 1075 /* 1076 * If we've managed to bind at least one proto lets run the 1077 * online method, so we can start listening for it. 1078 */ 1079 if (success && run_method(instance, IM_ONLINE, NULL) == -1) 1080 return; /* instance gone to maintenance */ 1081 break; 1082 case IIS_ONLINE: 1083 case IIS_IN_REFRESH_METHOD: 1084 /* 1085 * We're 'online', so start polling on any bound fds we're 1086 * currently not. 1087 */ 1088 if (poll_bound_fds(instance, B_TRUE) != 0) { 1089 failure = B_TRUE; 1090 } else if (!failure) { 1091 /* 1092 * We've successfully bound and poll'd upon all protos, 1093 * so reset the failure count. 1094 */ 1095 instance->bind_fail_count = 0; 1096 } 1097 break; 1098 case IIS_IN_ONLINE_METHOD: 1099 /* 1100 * Nothing to do here as the method completion code will start 1101 * listening for any successfully bound fds. 1102 */ 1103 break; 1104 default: 1105 #ifndef NDEBUG 1106 (void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n", 1107 __FILE__, __LINE__, instance->cur_istate); 1108 #endif 1109 abort(); 1110 } 1111 1112 if (failure) 1113 handle_bind_failure(instance); 1114 } 1115 1116 /* 1117 * Counter to create_bound_fds(), for each of the bound network fds this 1118 * function unregisters the instance from rpcbind if it's an RPC service, 1119 * stops listening for new connections for it and then closes the listening fd. 1120 */ 1121 static void 1122 destroy_bound_fds(instance_t *instance) 1123 { 1124 basic_cfg_t *cfg = instance->config->basic; 1125 proto_info_t *pi; 1126 1127 debug_msg("Entering destroy_bound_fds: instance: %s", instance->fmri); 1128 1129 for (pi = uu_list_first(cfg->proto_list); pi != NULL; 1130 pi = uu_list_next(cfg->proto_list, pi)) { 1131 if (pi->listen_fd != -1) { 1132 if (pi->ri != NULL) 1133 unregister_rpc_service(instance->fmri, pi->ri); 1134 clear_pollfd(pi->listen_fd); 1135 close_net_fd(instance, pi->listen_fd); 1136 pi->listen_fd = -1; 1137 } 1138 } 1139 1140 /* cancel any bind retries */ 1141 if (instance->bind_timer_id != -1) 1142 cancel_bind_timer(instance); 1143 1144 instance->bind_retries_exceeded = B_FALSE; 1145 } 1146 1147 /* 1148 * Perform %A address expansion and return a pointer to a static string 1149 * array containing crafted arguments. This expansion is provided for 1150 * compatibility with 4.2BSD daemons, and as such we've copied the logic of 1151 * the legacy inetd to maintain this compatibility as much as possible. This 1152 * logic is a bit scatty, but it dates back at least as far as SunOS 4.x. 1153 */ 1154 static char ** 1155 expand_address(instance_t *inst, const proto_info_t *pi) 1156 { 1157 static char addrbuf[sizeof ("ffffffff.65536")]; 1158 static char *ret[3]; 1159 instance_cfg_t *cfg = inst->config; 1160 /* 1161 * We cast pi to a void so we can then go on to cast it to a 1162 * socket_info_t without lint complaining about alignment. This 1163 * is done because the x86 version of lint thinks a lint suppression 1164 * directive is unnecessary and flags it as such, yet the sparc 1165 * version complains if it's absent. 1166 */ 1167 const void *p = pi; 1168 1169 debug_msg("Entering expand_address"); 1170 1171 /* set ret[0] to the basename of exec path */ 1172 if ((ret[0] = strrchr(cfg->methods[IM_START]->exec_path, '/')) 1173 != NULL) { 1174 ret[0]++; 1175 } else { 1176 ret[0] = cfg->methods[IM_START]->exec_path; 1177 } 1178 1179 if (!cfg->basic->istlx && 1180 (((socket_info_t *)p)->type == SOCK_DGRAM)) { 1181 ret[1] = NULL; 1182 } else { 1183 addrbuf[0] = '\0'; 1184 if (!cfg->basic->iswait && 1185 (inst->remote_addr.ss_family == AF_INET)) { 1186 struct sockaddr_in *sp; 1187 1188 sp = (struct sockaddr_in *)&(inst->remote_addr); 1189 (void) snprintf(addrbuf, sizeof (addrbuf), "%x.%hu", 1190 ntohl(sp->sin_addr.s_addr), ntohs(sp->sin_port)); 1191 } 1192 ret[1] = addrbuf; 1193 ret[2] = NULL; 1194 } 1195 1196 return (ret); 1197 } 1198 1199 /* 1200 * Returns the state associated with the supplied method being run for an 1201 * instance. 1202 */ 1203 static internal_inst_state_t 1204 get_method_state(instance_method_t method) 1205 { 1206 state_info_t *sip; 1207 1208 for (sip = states; sip->istate != IIS_NONE; sip++) { 1209 if (sip->method_running == method) 1210 break; 1211 } 1212 assert(sip->istate != IIS_NONE); 1213 1214 return (sip->istate); 1215 } 1216 1217 /* 1218 * Store the method's PID and CID in the repository. If the store fails 1219 * we ignore it and just drive on. 1220 */ 1221 static void 1222 add_method_ids(instance_t *ins, pid_t pid, ctid_t cid, instance_method_t mthd) 1223 { 1224 debug_msg("Entering add_method_ids"); 1225 1226 if (cid != -1) 1227 (void) add_remove_contract(ins, B_TRUE, cid); 1228 1229 if (mthd == IM_START) { 1230 if (add_rep_val(ins->start_pids, (int64_t)pid) == 0) { 1231 (void) store_rep_vals(ins->start_pids, ins->fmri, 1232 PR_NAME_START_PIDS); 1233 } 1234 } else { 1235 if (add_rep_val(ins->non_start_pid, (int64_t)pid) == 0) { 1236 (void) store_rep_vals(ins->non_start_pid, ins->fmri, 1237 PR_NAME_NON_START_PID); 1238 } 1239 } 1240 } 1241 1242 /* 1243 * Remove the method's PID and CID from the repository. If the removal 1244 * fails we ignore it and drive on. 1245 */ 1246 void 1247 remove_method_ids(instance_t *inst, pid_t pid, ctid_t cid, 1248 instance_method_t mthd) 1249 { 1250 debug_msg("Entering remove_method_ids"); 1251 1252 if (cid != -1) 1253 (void) add_remove_contract(inst, B_FALSE, cid); 1254 1255 if (mthd == IM_START) { 1256 remove_rep_val(inst->start_pids, (int64_t)pid); 1257 (void) store_rep_vals(inst->start_pids, inst->fmri, 1258 PR_NAME_START_PIDS); 1259 } else { 1260 remove_rep_val(inst->non_start_pid, (int64_t)pid); 1261 (void) store_rep_vals(inst->non_start_pid, inst->fmri, 1262 PR_NAME_NON_START_PID); 1263 } 1264 } 1265 1266 static instance_t * 1267 create_instance(const char *fmri) 1268 { 1269 instance_t *ret; 1270 1271 debug_msg("Entering create_instance, instance: %s", fmri); 1272 1273 if (((ret = calloc(1, sizeof (instance_t))) == NULL) || 1274 ((ret->fmri = strdup(fmri)) == NULL)) 1275 goto alloc_fail; 1276 1277 ret->conn_fd = -1; 1278 1279 ret->copies = 0; 1280 1281 ret->conn_rate_count = 0; 1282 ret->fail_rate_count = 0; 1283 ret->bind_fail_count = 0; 1284 1285 if (((ret->non_start_pid = create_rep_val_list()) == NULL) || 1286 ((ret->start_pids = create_rep_val_list()) == NULL) || 1287 ((ret->start_ctids = create_rep_val_list()) == NULL)) 1288 goto alloc_fail; 1289 1290 ret->cur_istate = IIS_NONE; 1291 ret->next_istate = IIS_NONE; 1292 1293 if (((ret->cur_istate_rep = create_rep_val_list()) == NULL) || 1294 ((ret->next_istate_rep = create_rep_val_list()) == NULL)) 1295 goto alloc_fail; 1296 1297 ret->config = NULL; 1298 ret->new_config = NULL; 1299 1300 ret->timer_id = -1; 1301 ret->bind_timer_id = -1; 1302 1303 ret->disable_req = B_FALSE; 1304 ret->maintenance_req = B_FALSE; 1305 ret->conn_rate_exceeded = B_FALSE; 1306 ret->bind_retries_exceeded = B_FALSE; 1307 1308 ret->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID; 1309 1310 return (ret); 1311 1312 alloc_fail: 1313 error_msg(strerror(errno)); 1314 destroy_instance(ret); 1315 return (NULL); 1316 } 1317 1318 static void 1319 destroy_instance(instance_t *inst) 1320 { 1321 debug_msg("Entering destroy_instance"); 1322 1323 if (inst == NULL) 1324 return; 1325 1326 destroy_instance_cfg(inst->config); 1327 destroy_instance_cfg(inst->new_config); 1328 1329 destroy_rep_val_list(inst->cur_istate_rep); 1330 destroy_rep_val_list(inst->next_istate_rep); 1331 1332 destroy_rep_val_list(inst->start_pids); 1333 destroy_rep_val_list(inst->non_start_pid); 1334 destroy_rep_val_list(inst->start_ctids); 1335 1336 free(inst->fmri); 1337 1338 free(inst); 1339 } 1340 1341 /* 1342 * Retrieves the current and next states internal states. Returns 0 on success, 1343 * else returns one of the following on error: 1344 * SCF_ERROR_NO_MEMORY if memory allocation failed. 1345 * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken. 1346 * SCF_ERROR_TYPE_MISMATCH if the property was of an unexpected type. 1347 * SCF_ERROR_NO_RESOURCES if the server doesn't have adequate resources. 1348 * SCF_ERROR_NO_SERVER if the server isn't running. 1349 */ 1350 static scf_error_t 1351 retrieve_instance_state(instance_t *inst) 1352 { 1353 scf_error_t ret; 1354 1355 debug_msg("Entering retrieve_instance_state: instance: %s", 1356 inst->fmri); 1357 1358 /* retrieve internal states */ 1359 if (((ret = retrieve_rep_vals(inst->cur_istate_rep, inst->fmri, 1360 PR_NAME_CUR_INT_STATE)) != 0) || 1361 ((ret = retrieve_rep_vals(inst->next_istate_rep, inst->fmri, 1362 PR_NAME_NEXT_INT_STATE)) != 0)) { 1363 if (ret != SCF_ERROR_NOT_FOUND) { 1364 error_msg(gettext( 1365 "Failed to read state of instance %s: %s"), 1366 inst->fmri, scf_strerror(scf_error())); 1367 return (ret); 1368 } 1369 1370 debug_msg("instance with no previous int state - " 1371 "setting state to uninitialized"); 1372 1373 if ((set_single_rep_val(inst->cur_istate_rep, 1374 (int64_t)IIS_UNINITIALIZED) == -1) || 1375 (set_single_rep_val(inst->next_istate_rep, 1376 (int64_t)IIS_NONE) == -1)) { 1377 return (SCF_ERROR_NO_MEMORY); 1378 } 1379 } 1380 1381 /* update convenience states */ 1382 inst->cur_istate = get_single_rep_val(inst->cur_istate_rep); 1383 inst->next_istate = get_single_rep_val(inst->next_istate_rep); 1384 debug_msg("previous states: cur: %d, next: %d", inst->cur_istate, 1385 inst->next_istate); 1386 1387 return (0); 1388 } 1389 1390 /* 1391 * Retrieve stored process ids and register each of them so we process their 1392 * termination. 1393 */ 1394 static int 1395 retrieve_method_pids(instance_t *inst) 1396 { 1397 rep_val_t *rv; 1398 1399 debug_msg("Entering remove_method_pids"); 1400 1401 switch (retrieve_rep_vals(inst->start_pids, inst->fmri, 1402 PR_NAME_START_PIDS)) { 1403 case 0: 1404 break; 1405 case SCF_ERROR_NOT_FOUND: 1406 return (0); 1407 default: 1408 error_msg(gettext("Failed to retrieve the start pids of " 1409 "instance %s from repository: %s"), inst->fmri, 1410 scf_strerror(scf_error())); 1411 return (-1); 1412 } 1413 1414 rv = uu_list_first(inst->start_pids); 1415 while (rv != NULL) { 1416 if (register_method(inst, (pid_t)rv->val, (ctid_t)-1, 1417 IM_START) == 0) { 1418 inst->copies++; 1419 rv = uu_list_next(inst->start_pids, rv); 1420 } else if (errno == ENOENT) { 1421 pid_t pid = (pid_t)rv->val; 1422 1423 /* 1424 * The process must have already terminated. Remove 1425 * it from the list. 1426 */ 1427 rv = uu_list_next(inst->start_pids, rv); 1428 remove_rep_val(inst->start_pids, pid); 1429 } else { 1430 error_msg(gettext("Failed to listen for the completion " 1431 "of %s method of instance %s"), START_METHOD_NAME, 1432 inst->fmri); 1433 rv = uu_list_next(inst->start_pids, rv); 1434 } 1435 } 1436 1437 /* synch the repository pid list to remove any terminated pids */ 1438 (void) store_rep_vals(inst->start_pids, inst->fmri, PR_NAME_START_PIDS); 1439 1440 return (0); 1441 } 1442 1443 /* 1444 * Remove the passed instance from inetd control. 1445 */ 1446 static void 1447 remove_instance(instance_t *instance) 1448 { 1449 debug_msg("Entering remove_instance"); 1450 1451 switch (instance->cur_istate) { 1452 case IIS_ONLINE: 1453 case IIS_DEGRADED: 1454 /* stop listening for network connections */ 1455 destroy_bound_fds(instance); 1456 break; 1457 case IIS_OFFLINE_BIND: 1458 cancel_bind_timer(instance); 1459 break; 1460 case IIS_OFFLINE_CONRATE: 1461 cancel_inst_timer(instance); 1462 break; 1463 } 1464 1465 /* stop listening for terminated methods */ 1466 unregister_instance_methods(instance); 1467 1468 uu_list_remove(instance_list, instance); 1469 destroy_instance(instance); 1470 } 1471 1472 /* 1473 * Refresh the configuration of instance 'inst'. This method gets called as 1474 * a result of a refresh event for the instance from the master restarter, so 1475 * we can rely upon the instance's running snapshot having been updated from 1476 * its configuration snapshot. 1477 */ 1478 void 1479 refresh_instance(instance_t *inst) 1480 { 1481 instance_cfg_t *cfg; 1482 1483 debug_msg("Entering refresh_instance: inst: %s", inst->fmri); 1484 1485 switch (inst->cur_istate) { 1486 case IIS_MAINTENANCE: 1487 case IIS_DISABLED: 1488 case IIS_UNINITIALIZED: 1489 /* 1490 * Ignore any possible changes, we'll re-read the configuration 1491 * automatically when we exit these states. 1492 */ 1493 break; 1494 1495 case IIS_OFFLINE_COPIES: 1496 case IIS_OFFLINE_BIND: 1497 case IIS_OFFLINE: 1498 case IIS_OFFLINE_CONRATE: 1499 destroy_instance_cfg(inst->config); 1500 if ((inst->config = read_instance_cfg(inst->fmri)) == NULL) { 1501 log_invalid_cfg(inst->fmri); 1502 if (inst->cur_istate == IIS_OFFLINE_BIND) { 1503 cancel_bind_timer(inst); 1504 } else if (inst->cur_istate == IIS_OFFLINE_CONRATE) { 1505 cancel_inst_timer(inst); 1506 } 1507 update_state(inst, IIS_MAINTENANCE, RERR_FAULT); 1508 } else { 1509 switch (inst->cur_istate) { 1510 case IIS_OFFLINE_BIND: 1511 if (copies_limit_exceeded(inst)) { 1512 /* Cancel scheduled bind retries. */ 1513 cancel_bind_timer(inst); 1514 1515 /* 1516 * Take the instance to the copies 1517 * offline state, via the offline 1518 * state. 1519 */ 1520 update_state(inst, IIS_OFFLINE, 1521 RERR_RESTART); 1522 process_offline_inst(inst); 1523 } 1524 break; 1525 1526 case IIS_OFFLINE: 1527 process_offline_inst(inst); 1528 break; 1529 1530 case IIS_OFFLINE_CONRATE: 1531 /* 1532 * Since we're already in a DOS state, 1533 * don't bother evaluating the copies 1534 * limit. This will be evaluated when 1535 * we leave this state in 1536 * process_offline_inst(). 1537 */ 1538 break; 1539 1540 case IIS_OFFLINE_COPIES: 1541 /* 1542 * Check if the copies limit has been increased 1543 * above the current count. 1544 */ 1545 if (!copies_limit_exceeded(inst)) { 1546 update_state(inst, IIS_OFFLINE, 1547 RERR_RESTART); 1548 process_offline_inst(inst); 1549 } 1550 break; 1551 1552 default: 1553 assert(0); 1554 } 1555 } 1556 break; 1557 1558 case IIS_DEGRADED: 1559 case IIS_ONLINE: 1560 if ((cfg = read_instance_cfg(inst->fmri)) != NULL) { 1561 instance_cfg_t *ocfg = inst->config; 1562 1563 /* 1564 * Try to avoid the overhead of taking an instance 1565 * offline and back on again. We do this by limiting 1566 * this behavior to two eventualities: 1567 * - there needs to be a re-bind to listen on behalf 1568 * of the instance with its new configuration. This 1569 * could be because for example its service has been 1570 * associated with a different port, or because the 1571 * v6only protocol option has been newly applied to 1572 * the instance. 1573 * - one or both of the start or online methods of the 1574 * instance have changed in the new configuration. 1575 * Without taking the instance offline when the 1576 * start method changed the instance may be running 1577 * with unwanted parameters (or event an unwanted 1578 * binary); and without taking the instance offline 1579 * if its online method was to change, some part of 1580 * its running environment may have changed and would 1581 * not be picked up until the instance next goes 1582 * offline for another reason. 1583 */ 1584 if ((!bind_config_equal(ocfg->basic, cfg->basic)) || 1585 !method_info_equal(ocfg->methods[IM_ONLINE], 1586 cfg->methods[IM_ONLINE]) || 1587 !method_info_equal(ocfg->methods[IM_START], 1588 cfg->methods[IM_START])) { 1589 destroy_bound_fds(inst); 1590 1591 assert(inst->new_config == NULL); 1592 inst->new_config = cfg; 1593 1594 (void) run_method(inst, IM_OFFLINE, NULL); 1595 } else { /* no bind config / method changes */ 1596 1597 /* 1598 * swap the proto list over from the old 1599 * configuration to the new, so we retain 1600 * our set of network fds. 1601 */ 1602 destroy_proto_list(cfg->basic); 1603 cfg->basic->proto_list = 1604 ocfg->basic->proto_list; 1605 ocfg->basic->proto_list = NULL; 1606 destroy_instance_cfg(ocfg); 1607 inst->config = cfg; 1608 1609 /* re-evaluate copies limits based on new cfg */ 1610 if (copies_limit_exceeded(inst)) { 1611 destroy_bound_fds(inst); 1612 (void) run_method(inst, IM_OFFLINE, 1613 NULL); 1614 } else { 1615 /* 1616 * Since the instance isn't being 1617 * taken offline, where we assume it 1618 * would pick-up any configuration 1619 * changes automatically when it goes 1620 * back online, run its refresh method 1621 * to allow it to pick-up any changes 1622 * whilst still online. 1623 */ 1624 (void) run_method(inst, IM_REFRESH, 1625 NULL); 1626 } 1627 } 1628 } else { 1629 log_invalid_cfg(inst->fmri); 1630 1631 destroy_bound_fds(inst); 1632 1633 inst->maintenance_req = B_TRUE; 1634 (void) run_method(inst, IM_OFFLINE, NULL); 1635 } 1636 break; 1637 1638 default: 1639 debug_msg("Unhandled current state %d for instance in " 1640 "refresh_instance", inst->cur_istate); 1641 assert(0); 1642 } 1643 } 1644 1645 /* 1646 * Called by process_restarter_event() to handle a restarter event for an 1647 * instance. 1648 */ 1649 static void 1650 handle_restarter_event(instance_t *instance, restarter_event_type_t event, 1651 boolean_t send_ack) 1652 { 1653 debug_msg("Entering handle_restarter_event: inst: %s, event: %d, " 1654 "curr state: %d", instance->fmri, event, instance->cur_istate); 1655 1656 switch (event) { 1657 case RESTARTER_EVENT_TYPE_ADD_INSTANCE: 1658 /* 1659 * When startd restarts, it sends _ADD_INSTANCE to delegated 1660 * restarters for all those services managed by them. We should 1661 * acknowledge this event, as startd's graph needs to be updated 1662 * about the current state of the service, when startd is 1663 * restarting. 1664 * update_state() is ok to be called here, as commands for 1665 * instances in transition are deferred by 1666 * process_restarter_event(). 1667 */ 1668 update_state(instance, instance->cur_istate, RERR_NONE); 1669 goto done; 1670 case RESTARTER_EVENT_TYPE_ADMIN_REFRESH: 1671 refresh_instance(instance); 1672 goto done; 1673 case RESTARTER_EVENT_TYPE_ADMIN_RESTART: 1674 /* 1675 * We've got a restart event, so if the instance is online 1676 * in any way initiate taking it offline, and rely upon 1677 * our restarter to send us an online event to bring 1678 * it back online. 1679 */ 1680 switch (instance->cur_istate) { 1681 case IIS_ONLINE: 1682 case IIS_DEGRADED: 1683 destroy_bound_fds(instance); 1684 (void) run_method(instance, IM_OFFLINE, NULL); 1685 } 1686 goto done; 1687 case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE: 1688 remove_instance(instance); 1689 goto done; 1690 case RESTARTER_EVENT_TYPE_STOP: 1691 switch (instance->cur_istate) { 1692 case IIS_OFFLINE_CONRATE: 1693 case IIS_OFFLINE_BIND: 1694 case IIS_OFFLINE_COPIES: 1695 /* 1696 * inetd must be closing down as we wouldn't get this 1697 * event in one of these states from the master 1698 * restarter. Take the instance to the offline resting 1699 * state. 1700 */ 1701 if (instance->cur_istate == IIS_OFFLINE_BIND) { 1702 cancel_bind_timer(instance); 1703 } else if (instance->cur_istate == 1704 IIS_OFFLINE_CONRATE) { 1705 cancel_inst_timer(instance); 1706 } 1707 update_state(instance, IIS_OFFLINE, RERR_RESTART); 1708 goto done; 1709 } 1710 break; 1711 } 1712 1713 switch (instance->cur_istate) { 1714 case IIS_OFFLINE: 1715 switch (event) { 1716 case RESTARTER_EVENT_TYPE_START: 1717 /* 1718 * Dependencies are met, let's take the service online. 1719 * Only try and bind for a wait type service if 1720 * no process is running on its behalf. Otherwise, just 1721 * mark the service online and binding will be attempted 1722 * when the process exits. 1723 */ 1724 if (!(instance->config->basic->iswait && 1725 (uu_list_first(instance->start_pids) != NULL))) { 1726 create_bound_fds(instance); 1727 } else { 1728 update_state(instance, IIS_ONLINE, RERR_NONE); 1729 } 1730 break; 1731 case RESTARTER_EVENT_TYPE_DISABLE: 1732 case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 1733 /* 1734 * The instance should be disabled, so run the 1735 * instance's disabled method that will do the work 1736 * to take it there. 1737 */ 1738 (void) run_method(instance, IM_DISABLE, NULL); 1739 break; 1740 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 1741 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 1742 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 1743 /* 1744 * The master restarter has requested the instance 1745 * go to maintenance; since we're already offline 1746 * just update the state to the maintenance state. 1747 */ 1748 update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 1749 break; 1750 } 1751 break; 1752 1753 case IIS_OFFLINE_BIND: 1754 switch (event) { 1755 case RESTARTER_EVENT_TYPE_DISABLE: 1756 case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 1757 /* 1758 * The instance should be disabled. Firstly, as for 1759 * the above dependencies unmet comment, cancel 1760 * the bind retry timer and update the state to 1761 * offline. Then, run the disable method to do the 1762 * work to take the instance from offline to 1763 * disabled. 1764 */ 1765 cancel_bind_timer(instance); 1766 update_state(instance, IIS_OFFLINE, RERR_RESTART); 1767 (void) run_method(instance, IM_DISABLE, NULL); 1768 break; 1769 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 1770 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 1771 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 1772 /* 1773 * The master restarter has requested the instance 1774 * be placed in the maintenance state. Cancel the 1775 * outstanding retry timer, and since we're already 1776 * offline, update the state to maintenance. 1777 */ 1778 cancel_bind_timer(instance); 1779 update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 1780 break; 1781 } 1782 break; 1783 1784 case IIS_DEGRADED: 1785 case IIS_ONLINE: 1786 switch (event) { 1787 case RESTARTER_EVENT_TYPE_DISABLE: 1788 case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 1789 /* 1790 * The instance needs to be disabled. Do the same work 1791 * as for the dependencies unmet event below to 1792 * take the instance offline. 1793 */ 1794 destroy_bound_fds(instance); 1795 /* 1796 * Indicate that the offline method is being run 1797 * as part of going to the disabled state, and to 1798 * carry on this transition. 1799 */ 1800 instance->disable_req = B_TRUE; 1801 (void) run_method(instance, IM_OFFLINE, NULL); 1802 break; 1803 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 1804 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 1805 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 1806 /* 1807 * The master restarter has requested the instance be 1808 * placed in the maintenance state. This involves 1809 * firstly taking the service offline, so do the 1810 * same work as for the dependencies unmet event 1811 * below. We set the maintenance_req flag to 1812 * indicate that when we get to the offline state 1813 * we should be placed directly into the maintenance 1814 * state. 1815 */ 1816 instance->maintenance_req = B_TRUE; 1817 /* FALLTHROUGH */ 1818 case RESTARTER_EVENT_TYPE_STOP: 1819 /* 1820 * Dependencies have become unmet. Close and 1821 * stop listening on the instance's network file 1822 * descriptor, and run the offline method to do 1823 * any work required to take us to the offline state. 1824 */ 1825 destroy_bound_fds(instance); 1826 (void) run_method(instance, IM_OFFLINE, NULL); 1827 } 1828 break; 1829 1830 case IIS_UNINITIALIZED: 1831 if (event == RESTARTER_EVENT_TYPE_DISABLE || 1832 event == RESTARTER_EVENT_TYPE_ADMIN_DISABLE) { 1833 update_state(instance, IIS_DISABLED, RERR_NONE); 1834 break; 1835 } else if (event != RESTARTER_EVENT_TYPE_ENABLE) { 1836 /* 1837 * Ignore other events until we know whether we're 1838 * enabled or not. 1839 */ 1840 break; 1841 } 1842 1843 /* 1844 * We've got an enabled event; make use of the handling in the 1845 * disable case. 1846 */ 1847 /* FALLTHROUGH */ 1848 1849 case IIS_DISABLED: 1850 switch (event) { 1851 case RESTARTER_EVENT_TYPE_ENABLE: 1852 /* 1853 * The instance needs enabling. Commence reading its 1854 * configuration and if successful place the instance 1855 * in the offline state and let process_offline_inst() 1856 * take it from there. 1857 */ 1858 destroy_instance_cfg(instance->config); 1859 instance->config = read_instance_cfg(instance->fmri); 1860 if (instance->config != NULL) { 1861 update_state(instance, IIS_OFFLINE, 1862 RERR_RESTART); 1863 process_offline_inst(instance); 1864 } else { 1865 log_invalid_cfg(instance->fmri); 1866 update_state(instance, IIS_MAINTENANCE, 1867 RERR_RESTART); 1868 } 1869 1870 break; 1871 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 1872 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 1873 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 1874 /* 1875 * The master restarter has requested the instance be 1876 * placed in the maintenance state, so just update its 1877 * state to maintenance. 1878 */ 1879 update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 1880 break; 1881 } 1882 break; 1883 1884 case IIS_MAINTENANCE: 1885 switch (event) { 1886 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF: 1887 case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 1888 /* 1889 * The master restarter has requested that the instance 1890 * be taken out of maintenance. Read its configuration, 1891 * and if successful place the instance in the offline 1892 * state and call process_offline_inst() to take it 1893 * from there. 1894 */ 1895 destroy_instance_cfg(instance->config); 1896 instance->config = read_instance_cfg(instance->fmri); 1897 if (instance->config != NULL) { 1898 update_state(instance, IIS_OFFLINE, 1899 RERR_RESTART); 1900 process_offline_inst(instance); 1901 } else { 1902 boolean_t enabled; 1903 1904 /* 1905 * The configuration was invalid. If the 1906 * service has disabled requested, let's 1907 * just place the instance in disabled even 1908 * though we haven't been able to run its 1909 * disable method, as the slightly incorrect 1910 * state is likely to be less of an issue to 1911 * an administrator than refusing to move an 1912 * instance to disabled. If disable isn't 1913 * requested, re-mark the service's state 1914 * as maintenance, so the administrator can 1915 * see the request was processed. 1916 */ 1917 if ((read_enable_merged(instance->fmri, 1918 &enabled) == 0) && !enabled) { 1919 update_state(instance, IIS_DISABLED, 1920 RERR_RESTART); 1921 } else { 1922 log_invalid_cfg(instance->fmri); 1923 update_state(instance, IIS_MAINTENANCE, 1924 RERR_FAULT); 1925 } 1926 } 1927 break; 1928 } 1929 break; 1930 1931 case IIS_OFFLINE_CONRATE: 1932 switch (event) { 1933 case RESTARTER_EVENT_TYPE_DISABLE: 1934 /* 1935 * The instance wants disabling. Take the instance 1936 * offline as for the dependencies unmet event above, 1937 * and then from there run the disable method to do 1938 * the work to take the instance to the disabled state. 1939 */ 1940 cancel_inst_timer(instance); 1941 update_state(instance, IIS_OFFLINE, RERR_RESTART); 1942 (void) run_method(instance, IM_DISABLE, NULL); 1943 break; 1944 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 1945 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 1946 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 1947 /* 1948 * The master restarter has requested the instance 1949 * be taken to maintenance. Cancel the timer setup 1950 * when we entered this state, and go directly to 1951 * maintenance. 1952 */ 1953 cancel_inst_timer(instance); 1954 update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 1955 break; 1956 } 1957 break; 1958 1959 case IIS_OFFLINE_COPIES: 1960 switch (event) { 1961 case RESTARTER_EVENT_TYPE_DISABLE: 1962 /* 1963 * The instance wants disabling. Update the state 1964 * to offline, and run the disable method to do the 1965 * work to take it to the disabled state. 1966 */ 1967 update_state(instance, IIS_OFFLINE, RERR_RESTART); 1968 (void) run_method(instance, IM_DISABLE, NULL); 1969 break; 1970 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 1971 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 1972 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 1973 /* 1974 * The master restarter has requested the instance be 1975 * placed in maintenance. Since it's already offline 1976 * simply update the state. 1977 */ 1978 update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 1979 break; 1980 } 1981 break; 1982 1983 default: 1984 debug_msg("handle_restarter_event: instance in an " 1985 "unexpected state"); 1986 assert(0); 1987 } 1988 1989 done: 1990 if (send_ack) 1991 ack_restarter_event(B_TRUE); 1992 } 1993 1994 /* 1995 * Tries to read and process an event from the event pipe. If there isn't one 1996 * or an error occurred processing the event it returns -1. Else, if the event 1997 * is for an instance we're not already managing we read its state, add it to 1998 * our list to manage, and if appropriate read its configuration. Whether it's 1999 * new to us or not, we then handle the specific event. 2000 * Returns 0 if an event was read and processed successfully, else -1. 2001 */ 2002 static int 2003 process_restarter_event(void) 2004 { 2005 char *fmri; 2006 size_t fmri_size; 2007 restarter_event_type_t event_type; 2008 instance_t *instance; 2009 restarter_event_t *event; 2010 ssize_t sz; 2011 2012 debug_msg("Entering process_restarter_event"); 2013 2014 /* 2015 * Try to read an event pointer from the event pipe. 2016 */ 2017 errno = 0; 2018 switch (safe_read(rst_event_pipe[PE_CONSUMER], &event, 2019 sizeof (event))) { 2020 case 0: 2021 break; 2022 case 1: 2023 if (errno == EAGAIN) /* no event to read */ 2024 return (-1); 2025 2026 /* other end of pipe closed */ 2027 2028 /* FALLTHROUGH */ 2029 default: /* unexpected read error */ 2030 /* 2031 * There's something wrong with the event pipe. Let's 2032 * shutdown and be restarted. 2033 */ 2034 inetd_stop(); 2035 return (-1); 2036 } 2037 2038 /* 2039 * Check if we're currently managing the instance which the event 2040 * pertains to. If not, read its complete state and add it to our 2041 * list to manage. 2042 */ 2043 2044 fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); 2045 if ((fmri = malloc(fmri_size)) == NULL) { 2046 error_msg(strerror(errno)); 2047 goto fail; 2048 } 2049 sz = restarter_event_get_instance(event, fmri, fmri_size); 2050 if (sz >= fmri_size) 2051 assert(0); 2052 2053 for (instance = uu_list_first(instance_list); instance != NULL; 2054 instance = uu_list_next(instance_list, instance)) { 2055 if (strcmp(instance->fmri, fmri) == 0) 2056 break; 2057 } 2058 2059 if (instance == NULL) { 2060 int err; 2061 2062 debug_msg("New instance to manage: %s", fmri); 2063 2064 if (((instance = create_instance(fmri)) == NULL) || 2065 (retrieve_instance_state(instance) != 0) || 2066 (retrieve_method_pids(instance) != 0)) { 2067 destroy_instance(instance); 2068 free(fmri); 2069 goto fail; 2070 } 2071 2072 if (((err = iterate_repository_contracts(instance, 0)) 2073 != 0) && (err != ENOENT)) { 2074 error_msg(gettext( 2075 "Failed to adopt contracts of instance %s: %s"), 2076 instance->fmri, strerror(err)); 2077 destroy_instance(instance); 2078 free(fmri); 2079 goto fail; 2080 } 2081 2082 uu_list_node_init(instance, &instance->link, instance_pool); 2083 (void) uu_list_insert_after(instance_list, NULL, instance); 2084 2085 /* 2086 * Only read configuration for instances that aren't in any of 2087 * the disabled, maintenance or uninitialized states, since 2088 * they'll read it on state exit. 2089 */ 2090 if ((instance->cur_istate != IIS_DISABLED) && 2091 (instance->cur_istate != IIS_MAINTENANCE) && 2092 (instance->cur_istate != IIS_UNINITIALIZED)) { 2093 instance->config = read_instance_cfg(instance->fmri); 2094 if (instance->config == NULL) { 2095 log_invalid_cfg(instance->fmri); 2096 update_state(instance, IIS_MAINTENANCE, 2097 RERR_FAULT); 2098 } 2099 } 2100 } 2101 2102 free(fmri); 2103 2104 event_type = restarter_event_get_type(event); 2105 debug_msg("Event type: %d for instance: %s", event_type, 2106 instance->fmri); 2107 2108 /* 2109 * If the instance is currently running a method, don't process the 2110 * event now, but attach it to the instance for processing when 2111 * the instance finishes its transition. 2112 */ 2113 if (INST_IN_TRANSITION(instance)) { 2114 debug_msg("storing event %d for instance %s", event_type, 2115 instance->fmri); 2116 instance->pending_rst_event = event_type; 2117 } else { 2118 handle_restarter_event(instance, event_type, B_TRUE); 2119 } 2120 2121 return (0); 2122 2123 fail: 2124 ack_restarter_event(B_FALSE); 2125 return (-1); 2126 } 2127 2128 /* 2129 * Do the state machine processing associated with the termination of instance 2130 * 'inst''s start method. 2131 */ 2132 void 2133 process_start_term(instance_t *inst) 2134 { 2135 basic_cfg_t *cfg; 2136 2137 debug_msg("Entering process_start_term: inst: %s", inst->fmri); 2138 2139 inst->copies--; 2140 2141 if ((inst->cur_istate == IIS_MAINTENANCE) || 2142 (inst->cur_istate == IIS_DISABLED)) { 2143 /* do any further processing/checks when we exit these states */ 2144 return; 2145 } 2146 2147 cfg = inst->config->basic; 2148 2149 if (cfg->iswait) { 2150 proto_info_t *pi; 2151 2152 switch (inst->cur_istate) { 2153 case IIS_ONLINE: 2154 case IIS_DEGRADED: 2155 case IIS_IN_REFRESH_METHOD: 2156 /* 2157 * A wait type service's start method has exited. 2158 * Check if the method was fired off in this inetd's 2159 * lifetime, or a previous one; if the former, 2160 * re-commence listening on the service's behalf; if 2161 * the latter, mark the service offline and let bind 2162 * attempts commence. 2163 */ 2164 for (pi = uu_list_first(cfg->proto_list); pi != NULL; 2165 pi = uu_list_next(cfg->proto_list, pi)) { 2166 /* 2167 * If a bound fd exists, the method was fired 2168 * off during this inetd's lifetime. 2169 */ 2170 if (pi->listen_fd != -1) 2171 break; 2172 } 2173 if (pi != NULL) { 2174 if (poll_bound_fds(inst, B_TRUE) != 0) 2175 handle_bind_failure(inst); 2176 } else { 2177 update_state(inst, IIS_OFFLINE, RERR_RESTART); 2178 create_bound_fds(inst); 2179 } 2180 } 2181 } else { 2182 /* 2183 * Check if a nowait service should be brought back online 2184 * after exceeding its copies limit. 2185 */ 2186 if ((inst->cur_istate == IIS_OFFLINE_COPIES) && 2187 !copies_limit_exceeded(inst)) { 2188 update_state(inst, IIS_OFFLINE, RERR_NONE); 2189 process_offline_inst(inst); 2190 } 2191 } 2192 } 2193 2194 /* 2195 * If the instance has a pending event process it and initiate the 2196 * acknowledgement. 2197 */ 2198 static void 2199 process_pending_rst_event(instance_t *inst) 2200 { 2201 if (inst->pending_rst_event != RESTARTER_EVENT_TYPE_INVALID) { 2202 restarter_event_type_t re; 2203 2204 debug_msg("Injecting pending event %d for instance %s", 2205 inst->pending_rst_event, inst->fmri); 2206 re = inst->pending_rst_event; 2207 inst->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID; 2208 handle_restarter_event(inst, re, B_TRUE); 2209 } 2210 } 2211 2212 /* 2213 * Do the state machine processing associated with the termination 2214 * of the specified instance's non-start method with the specified status. 2215 * Once the processing of the termination is done, the function also picks up 2216 * any processing that was blocked on the method running. 2217 */ 2218 void 2219 process_non_start_term(instance_t *inst, int status) 2220 { 2221 boolean_t ran_online_method = B_FALSE; 2222 2223 debug_msg("Entering process_non_start_term: inst: %s, method: %s", 2224 inst->fmri, methods[states[inst->cur_istate].method_running].name); 2225 2226 if (status == IMRET_FAILURE) { 2227 error_msg(gettext("The %s method of instance %s failed, " 2228 "transitioning to maintenance"), 2229 methods[states[inst->cur_istate].method_running].name, 2230 inst->fmri); 2231 2232 if ((inst->cur_istate == IIS_IN_ONLINE_METHOD) || 2233 (inst->cur_istate == IIS_IN_REFRESH_METHOD)) 2234 destroy_bound_fds(inst); 2235 2236 update_state(inst, IIS_MAINTENANCE, RERR_FAULT); 2237 2238 inst->maintenance_req = B_FALSE; 2239 inst->conn_rate_exceeded = B_FALSE; 2240 2241 if (inst->new_config != NULL) { 2242 destroy_instance_cfg(inst->new_config); 2243 inst->new_config = NULL; 2244 } 2245 2246 if (!inetd_stopping) 2247 process_pending_rst_event(inst); 2248 2249 return; 2250 } 2251 2252 /* non-failure method return */ 2253 2254 if (status != IMRET_SUCCESS) { 2255 /* 2256 * An instance method never returned a supported return code. 2257 * We'll assume this means the method succeeded for now whilst 2258 * non-GL-cognizant methods are used - eg. pkill. 2259 */ 2260 debug_msg("The %s method of instance %s returned " 2261 "non-compliant exit code: %d, assuming success", 2262 methods[states[inst->cur_istate].method_running].name, 2263 inst->fmri, status); 2264 } 2265 2266 /* 2267 * Update the state from the in-transition state. 2268 */ 2269 switch (inst->cur_istate) { 2270 case IIS_IN_ONLINE_METHOD: 2271 ran_online_method = B_TRUE; 2272 /* FALLTHROUGH */ 2273 case IIS_IN_REFRESH_METHOD: 2274 /* 2275 * If we've exhausted the bind retries, flag that by setting 2276 * the instance's state to degraded. 2277 */ 2278 if (inst->bind_retries_exceeded) { 2279 update_state(inst, IIS_DEGRADED, RERR_NONE); 2280 break; 2281 } 2282 /* FALLTHROUGH */ 2283 default: 2284 update_state(inst, 2285 methods[states[inst->cur_istate].method_running].dst_state, 2286 RERR_NONE); 2287 } 2288 2289 if (inst->cur_istate == IIS_OFFLINE) { 2290 if (inst->new_config != NULL) { 2291 /* 2292 * This instance was found during refresh to need 2293 * taking offline because its newly read configuration 2294 * was sufficiently different. Now we're offline, 2295 * activate this new configuration. 2296 */ 2297 destroy_instance_cfg(inst->config); 2298 inst->config = inst->new_config; 2299 inst->new_config = NULL; 2300 } 2301 2302 /* continue/complete any transitions that are in progress */ 2303 process_offline_inst(inst); 2304 2305 } else if (ran_online_method) { 2306 /* 2307 * We've just successfully executed the online method. We have 2308 * a set of bound network fds that were created before running 2309 * this method, so now we're online start listening for 2310 * connections on them. 2311 */ 2312 if (poll_bound_fds(inst, B_TRUE) != 0) 2313 handle_bind_failure(inst); 2314 } 2315 2316 /* 2317 * If we're now out of transition (process_offline_inst() could have 2318 * fired off another method), carry out any jobs that were blocked by 2319 * us being in transition. 2320 */ 2321 if (!INST_IN_TRANSITION(inst)) { 2322 if (inetd_stopping) { 2323 if (!instance_stopped(inst)) { 2324 /* 2325 * inetd is stopping, and this instance hasn't 2326 * been stopped. Inject a stop event. 2327 */ 2328 handle_restarter_event(inst, 2329 RESTARTER_EVENT_TYPE_STOP, B_FALSE); 2330 } 2331 } else { 2332 process_pending_rst_event(inst); 2333 } 2334 } 2335 } 2336 2337 /* 2338 * Check if configuration file specified is readable. If not return B_FALSE, 2339 * else return B_TRUE. 2340 */ 2341 static boolean_t 2342 can_read_file(const char *path) 2343 { 2344 int ret; 2345 int serrno; 2346 2347 debug_msg("Entering can_read_file"); 2348 do { 2349 ret = access(path, R_OK); 2350 } while ((ret < 0) && (errno == EINTR)); 2351 if (ret < 0) { 2352 if (errno != ENOENT) { 2353 serrno = errno; 2354 error_msg(gettext("Failed to access configuration " 2355 "file %s for performing modification checks: %s"), 2356 path, strerror(errno)); 2357 errno = serrno; 2358 } 2359 return (B_FALSE); 2360 } 2361 return (B_TRUE); 2362 } 2363 2364 /* 2365 * Check whether the configuration file has changed contents since inetd 2366 * was last started/refreshed, and if so, log a message indicating that 2367 * inetconv needs to be run. 2368 */ 2369 static void 2370 check_conf_file(void) 2371 { 2372 char *new_hash; 2373 char *old_hash = NULL; 2374 scf_error_t ret; 2375 const char *file; 2376 2377 debug_msg("Entering check_conf_file"); 2378 2379 if (conf_file == NULL) { 2380 /* 2381 * No explicit config file specified, so see if one of the 2382 * default two are readable, checking the primary one first 2383 * followed by the secondary. 2384 */ 2385 if (can_read_file(PRIMARY_DEFAULT_CONF_FILE)) { 2386 file = PRIMARY_DEFAULT_CONF_FILE; 2387 } else if ((errno == ENOENT) && 2388 can_read_file(SECONDARY_DEFAULT_CONF_FILE)) { 2389 file = SECONDARY_DEFAULT_CONF_FILE; 2390 } else { 2391 return; 2392 } 2393 } else { 2394 file = conf_file; 2395 if (!can_read_file(file)) 2396 return; 2397 } 2398 2399 if (calculate_hash(file, &new_hash) == 0) { 2400 ret = retrieve_inetd_hash(&old_hash); 2401 if (((ret == SCF_ERROR_NONE) && 2402 (strcmp(old_hash, new_hash) != 0))) { 2403 /* modified config file */ 2404 warn_msg(gettext( 2405 "Configuration file %s has been modified since " 2406 "inetconv was last run. \"inetconv -i %s\" must be " 2407 "run to apply any changes to the SMF"), file, file); 2408 } else if ((ret != SCF_ERROR_NOT_FOUND) && 2409 (ret != SCF_ERROR_NONE)) { 2410 /* No message if hash not yet computed */ 2411 error_msg(gettext("Failed to check whether " 2412 "configuration file %s has been modified: %s"), 2413 file, scf_strerror(ret)); 2414 } 2415 free(old_hash); 2416 free(new_hash); 2417 } else { 2418 error_msg(gettext("Failed to check whether configuration file " 2419 "%s has been modified: %s"), file, strerror(errno)); 2420 } 2421 } 2422 2423 /* 2424 * Refresh all inetd's managed instances and check the configuration file 2425 * for any updates since inetconv was last run, logging a message if there 2426 * are. We call the SMF refresh function to refresh each instance so that 2427 * the refresh request goes through the framework, and thus results in the 2428 * running snapshot of each instance being updated from the configuration 2429 * snapshot. 2430 */ 2431 static void 2432 inetd_refresh(void) 2433 { 2434 instance_t *inst; 2435 2436 debug_msg("Entering inetd_refresh"); 2437 2438 /* call libscf to send refresh requests for all managed instances */ 2439 for (inst = uu_list_first(instance_list); inst != NULL; 2440 inst = uu_list_next(instance_list, inst)) { 2441 if (smf_refresh_instance(inst->fmri) < 0) { 2442 error_msg(gettext("Failed to refresh instance %s: %s"), 2443 inst->fmri, scf_strerror(scf_error())); 2444 } 2445 } 2446 2447 /* 2448 * Log a message if the configuration file has changed since inetconv 2449 * was last run. 2450 */ 2451 check_conf_file(); 2452 } 2453 2454 /* 2455 * Initiate inetd's shutdown. 2456 */ 2457 static void 2458 inetd_stop(void) 2459 { 2460 instance_t *inst; 2461 2462 debug_msg("Entering inetd_stop"); 2463 2464 /* Block handling signals for stop and refresh */ 2465 (void) sighold(SIGHUP); 2466 (void) sighold(SIGTERM); 2467 2468 /* Indicate inetd is coming down */ 2469 inetd_stopping = B_TRUE; 2470 2471 /* Stop polling on restarter events. */ 2472 clear_pollfd(rst_event_pipe[PE_CONSUMER]); 2473 2474 /* Stop polling for any more stop/refresh requests. */ 2475 clear_pollfd(uds_fd); 2476 2477 /* 2478 * Send a stop event to all currently unstopped instances that 2479 * aren't in transition. For those that are in transition, the 2480 * event will get sent when the transition completes. 2481 */ 2482 for (inst = uu_list_first(instance_list); inst != NULL; 2483 inst = uu_list_next(instance_list, inst)) { 2484 if (!instance_stopped(inst) && !INST_IN_TRANSITION(inst)) 2485 handle_restarter_event(inst, 2486 RESTARTER_EVENT_TYPE_STOP, B_FALSE); 2487 } 2488 } 2489 2490 /* 2491 * Sets up the intra-inetd-process Unix Domain Socket. 2492 * Returns -1 on error, else 0. 2493 */ 2494 static int 2495 uds_init(void) 2496 { 2497 struct sockaddr_un addr; 2498 2499 debug_msg("Entering uds_init"); 2500 2501 if ((uds_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { 2502 error_msg("socket: %s", strerror(errno)); 2503 return (-1); 2504 } 2505 2506 disable_blocking(uds_fd); 2507 2508 (void) unlink(INETD_UDS_PATH); /* clean-up any stale files */ 2509 2510 (void) memset(&addr, 0, sizeof (addr)); 2511 addr.sun_family = AF_UNIX; 2512 /* CONSTCOND */ 2513 assert(sizeof (INETD_UDS_PATH) <= sizeof (addr.sun_path)); 2514 (void) strlcpy(addr.sun_path, INETD_UDS_PATH, sizeof (addr.sun_path)); 2515 2516 if (bind(uds_fd, (struct sockaddr *)(&addr), sizeof (addr)) < 0) { 2517 error_msg(gettext("Failed to bind socket to %s: %s"), 2518 INETD_UDS_PATH, strerror(errno)); 2519 (void) close(uds_fd); 2520 return (-1); 2521 } 2522 2523 (void) listen(uds_fd, UDS_BACKLOG); 2524 2525 if ((set_pollfd(uds_fd, POLLIN)) == -1) { 2526 (void) close(uds_fd); 2527 (void) unlink(INETD_UDS_PATH); 2528 return (-1); 2529 } 2530 2531 return (0); 2532 } 2533 2534 static void 2535 uds_fini(void) 2536 { 2537 if (uds_fd != -1) 2538 (void) close(uds_fd); 2539 (void) unlink(INETD_UDS_PATH); 2540 } 2541 2542 /* 2543 * Handle an incoming request on the Unix Domain Socket. Returns -1 if there 2544 * was an error handling the event, else 0. 2545 */ 2546 static int 2547 process_uds_event(void) 2548 { 2549 uds_request_t req; 2550 int fd; 2551 struct sockaddr_un addr; 2552 socklen_t len = sizeof (addr); 2553 int ret; 2554 uint_t retries = 0; 2555 ucred_t *ucred = NULL; 2556 uid_t euid; 2557 2558 debug_msg("Entering process_uds_event"); 2559 2560 do { 2561 fd = accept(uds_fd, (struct sockaddr *)&addr, &len); 2562 } while ((fd < 0) && (errno == EINTR)); 2563 if (fd < 0) { 2564 if (errno != EWOULDBLOCK) 2565 error_msg("accept failed: %s", strerror(errno)); 2566 return (-1); 2567 } 2568 2569 if (getpeerucred(fd, &ucred) == -1) { 2570 error_msg("getpeerucred failed: %s", strerror(errno)); 2571 (void) close(fd); 2572 return (-1); 2573 } 2574 2575 /* Check peer credentials before acting on the request */ 2576 euid = ucred_geteuid(ucred); 2577 ucred_free(ucred); 2578 if (euid != 0 && getuid() != euid) { 2579 debug_msg("peer euid %u != uid %u", 2580 (uint_t)euid, (uint_t)getuid()); 2581 (void) close(fd); 2582 return (-1); 2583 } 2584 2585 for (retries = 0; retries < UDS_RECV_RETRIES; retries++) { 2586 if (((ret = safe_read(fd, &req, sizeof (req))) != 1) || 2587 (errno != EAGAIN)) 2588 break; 2589 2590 (void) poll(NULL, 0, 100); /* 100ms pause */ 2591 } 2592 2593 if (ret != 0) { 2594 error_msg(gettext("Failed read: %s"), strerror(errno)); 2595 (void) close(fd); 2596 return (-1); 2597 } 2598 2599 switch (req) { 2600 case UR_REFRESH_INETD: 2601 /* flag the request for event_loop() to process */ 2602 refresh_inetd_requested = B_TRUE; 2603 (void) close(fd); 2604 break; 2605 case UR_STOP_INETD: 2606 inetd_stop(); 2607 break; 2608 default: 2609 error_msg("unexpected UDS request"); 2610 (void) close(fd); 2611 return (-1); 2612 } 2613 2614 return (0); 2615 } 2616 2617 /* 2618 * Perform checks for common exec string errors. We limit the checks to 2619 * whether the file exists, is a regular file, and has at least one execute 2620 * bit set. We leave the core security checks to exec() so as not to duplicate 2621 * and thus incur the associated drawbacks, but hope to catch the common 2622 * errors here. 2623 */ 2624 static boolean_t 2625 passes_basic_exec_checks(const char *instance, const char *method, 2626 const char *path) 2627 { 2628 struct stat sbuf; 2629 2630 debug_msg("Entering passes_basic_exec_checks"); 2631 2632 /* check the file exists */ 2633 while (stat(path, &sbuf) == -1) { 2634 if (errno != EINTR) { 2635 error_msg(gettext( 2636 "Can't stat the %s method of instance %s: %s"), 2637 method, instance, strerror(errno)); 2638 return (B_FALSE); 2639 } 2640 } 2641 2642 /* 2643 * Check if the file is a regular file and has at least one execute 2644 * bit set. 2645 */ 2646 if ((sbuf.st_mode & S_IFMT) != S_IFREG) { 2647 error_msg(gettext( 2648 "The %s method of instance %s isn't a regular file"), 2649 method, instance); 2650 return (B_FALSE); 2651 } else if ((sbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 2652 error_msg(gettext("The %s method instance %s doesn't have " 2653 "any execute permissions set"), method, instance); 2654 return (B_FALSE); 2655 } 2656 2657 return (B_TRUE); 2658 } 2659 2660 static void 2661 exec_method(instance_t *instance, instance_method_t method, method_info_t *mi, 2662 struct method_context *mthd_ctxt, const proto_info_t *pi) 2663 { 2664 char **args; 2665 char **env; 2666 const char *errf; 2667 int serrno; 2668 basic_cfg_t *cfg = instance->config->basic; 2669 2670 if (method == IM_START) { 2671 /* 2672 * If wrappers checks fail, pretend the method was exec'd and 2673 * failed. 2674 */ 2675 if (!tcp_wrappers_ok(instance)) 2676 exit(IMRET_FAILURE); 2677 } 2678 2679 /* 2680 * Revert the disposition of handled signals and ignored signals to 2681 * their defaults, unblocking any blocked ones as a side effect. 2682 */ 2683 (void) sigset(SIGHUP, SIG_DFL); 2684 (void) sigset(SIGTERM, SIG_DFL); 2685 (void) sigset(SIGINT, SIG_DFL); 2686 2687 /* 2688 * Setup exec arguments. Do this before the fd setup below, so our 2689 * logging related file fd doesn't get taken over before we call 2690 * expand_address(). 2691 */ 2692 if ((method == IM_START) && 2693 (strcmp(mi->exec_args_we.we_wordv[0], "%A") == 0)) { 2694 args = expand_address(instance, pi); 2695 } else { 2696 args = mi->exec_args_we.we_wordv; 2697 } 2698 2699 /* Generate audit trail for start operations */ 2700 if (method == IM_START) { 2701 adt_event_data_t *ae; 2702 struct sockaddr_storage ss; 2703 priv_set_t *privset; 2704 socklen_t sslen = sizeof (ss); 2705 2706 if ((ae = adt_alloc_event(audit_handle, ADT_inetd_connect)) 2707 == NULL) { 2708 error_msg(gettext("Unable to allocate audit event for " 2709 "the %s method of instance %s"), 2710 methods[method].name, instance->fmri); 2711 exit(IMRET_FAILURE); 2712 } 2713 2714 /* 2715 * The inetd_connect audit record consists of: 2716 * Service name 2717 * Execution path 2718 * Remote address and port 2719 * Local port 2720 * Process privileges 2721 */ 2722 ae->adt_inetd_connect.service_name = cfg->svc_name; 2723 ae->adt_inetd_connect.cmd = mi->exec_path; 2724 2725 if (instance->remote_addr.ss_family == AF_INET) { 2726 struct in_addr *in = SS_SINADDR(instance->remote_addr); 2727 ae->adt_inetd_connect.ip_adr[0] = in->s_addr; 2728 ae->adt_inetd_connect.ip_type = ADT_IPv4; 2729 } else { 2730 uint32_t *addr6; 2731 int i; 2732 2733 ae->adt_inetd_connect.ip_type = ADT_IPv6; 2734 addr6 = (uint32_t *)SS_SINADDR(instance->remote_addr); 2735 for (i = 0; i < 4; ++i) 2736 ae->adt_inetd_connect.ip_adr[i] = addr6[i]; 2737 } 2738 2739 ae->adt_inetd_connect.ip_remote_port = 2740 ntohs(SS_PORT(instance->remote_addr)); 2741 2742 if (getsockname(instance->conn_fd, (struct sockaddr *)&ss, 2743 &sslen) == 0) 2744 ae->adt_inetd_connect.ip_local_port = 2745 ntohs(SS_PORT(ss)); 2746 2747 privset = mthd_ctxt->priv_set; 2748 if (privset == NULL) { 2749 privset = priv_allocset(); 2750 if (privset != NULL && 2751 getppriv(PRIV_EFFECTIVE, privset) != 0) { 2752 priv_freeset(privset); 2753 privset = NULL; 2754 } 2755 } 2756 2757 ae->adt_inetd_connect.privileges = privset; 2758 2759 (void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS); 2760 adt_free_event(ae); 2761 2762 if (privset != NULL && mthd_ctxt->priv_set == NULL) 2763 priv_freeset(privset); 2764 } 2765 2766 /* 2767 * Set method context before the fd setup below so we can output an 2768 * error message if it fails. 2769 */ 2770 if ((errno = restarter_set_method_context(mthd_ctxt, &errf)) != 0) { 2771 const char *msg; 2772 2773 if (errno == -1) { 2774 if (strcmp(errf, "core_set_process_path") == 0) { 2775 msg = gettext("Failed to set the corefile path " 2776 "for the %s method of instance %s"); 2777 } else if (strcmp(errf, "setproject") == 0) { 2778 msg = gettext("Failed to assign a resource " 2779 "control for the %s method of instance %s"); 2780 } else if (strcmp(errf, "pool_set_binding") == 0) { 2781 msg = gettext("Failed to bind the %s method of " 2782 "instance %s to a pool due to a system " 2783 "error"); 2784 } else { 2785 assert(0); 2786 abort(); 2787 } 2788 2789 error_msg(msg, methods[method].name, instance->fmri); 2790 2791 exit(IMRET_FAILURE); 2792 } 2793 2794 if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) { 2795 switch (errno) { 2796 case ENOENT: 2797 msg = gettext("Failed to find resource pool " 2798 "for the %s method of instance %s"); 2799 break; 2800 2801 case EBADF: 2802 msg = gettext("Failed to bind the %s method of " 2803 "instance %s to a pool due to invalid " 2804 "configuration"); 2805 break; 2806 2807 case EINVAL: 2808 msg = gettext("Failed to bind the %s method of " 2809 "instance %s to a pool due to invalid " 2810 "pool name"); 2811 break; 2812 2813 default: 2814 assert(0); 2815 abort(); 2816 } 2817 2818 exit(IMRET_FAILURE); 2819 } 2820 2821 if (errf != NULL) { 2822 error_msg(gettext("Failed to set credentials for the " 2823 "%s method of instance %s (%s: %s)"), 2824 methods[method].name, instance->fmri, errf, 2825 strerror(errno)); 2826 exit(IMRET_FAILURE); 2827 } 2828 2829 switch (errno) { 2830 case ENOMEM: 2831 msg = gettext("Failed to set credentials for the %s " 2832 "method of instance %s (out of memory)"); 2833 break; 2834 2835 case ENOENT: 2836 msg = gettext("Failed to set credentials for the %s " 2837 "method of instance %s (no passwd or shadow " 2838 "entry for user)"); 2839 break; 2840 2841 default: 2842 assert(0); 2843 abort(); 2844 } 2845 2846 error_msg(msg, methods[method].name, instance->fmri); 2847 exit(IMRET_FAILURE); 2848 } 2849 2850 /* let exec() free mthd_ctxt */ 2851 2852 /* setup standard fds */ 2853 if (method == IM_START) { 2854 (void) dup2(instance->conn_fd, STDIN_FILENO); 2855 } else { 2856 (void) close(STDIN_FILENO); 2857 (void) open("/dev/null", O_RDONLY); 2858 } 2859 (void) dup2(STDIN_FILENO, STDOUT_FILENO); 2860 (void) dup2(STDIN_FILENO, STDERR_FILENO); 2861 2862 closefrom(STDERR_FILENO + 1); 2863 2864 method_preexec(); 2865 2866 env = set_smf_env(mthd_ctxt, instance, methods[method].name); 2867 2868 if (env != NULL) { 2869 do { 2870 (void) execve(mi->exec_path, args, env); 2871 } while (errno == EINTR); 2872 } 2873 2874 serrno = errno; 2875 /* start up logging again to report the error */ 2876 msg_init(); 2877 errno = serrno; 2878 2879 error_msg( 2880 gettext("Failed to exec %s method of instance %s: %s"), 2881 methods[method].name, instance->fmri, strerror(errno)); 2882 2883 if ((method == IM_START) && (instance->config->basic->iswait)) { 2884 /* 2885 * We couldn't exec the start method for a wait type service. 2886 * Eat up data from the endpoint, so that hopefully the 2887 * service's fd won't wake poll up on the next time round 2888 * event_loop(). This behavior is carried over from the old 2889 * inetd, and it seems somewhat arbitrary that it isn't 2890 * also done in the case of fork failures; but I guess 2891 * it assumes an exec failure is less likely to be the result 2892 * of a resource shortage, and is thus not worth retrying. 2893 */ 2894 consume_wait_data(instance, 0); 2895 } 2896 2897 exit(IMRET_FAILURE); 2898 } 2899 2900 static restarter_error_t 2901 get_method_error_success(instance_method_t method) 2902 { 2903 switch (method) { 2904 case IM_OFFLINE: 2905 return (RERR_RESTART); 2906 case IM_ONLINE: 2907 return (RERR_RESTART); 2908 case IM_DISABLE: 2909 return (RERR_RESTART); 2910 case IM_REFRESH: 2911 return (RERR_REFRESH); 2912 case IM_START: 2913 return (RERR_RESTART); 2914 } 2915 (void) fprintf(stderr, gettext("Internal fatal error in inetd.\n")); 2916 2917 abort(); 2918 /* NOTREACHED */ 2919 } 2920 2921 static int 2922 smf_kill_process(instance_t *instance, int sig) 2923 { 2924 rep_val_t *rv; 2925 int ret = IMRET_SUCCESS; 2926 2927 /* Carry out process assassination */ 2928 for (rv = uu_list_first(instance->start_pids); 2929 rv != NULL; 2930 rv = uu_list_next(instance->start_pids, rv)) { 2931 if ((kill((pid_t)rv->val, sig) != 0) && 2932 (errno != ESRCH)) { 2933 ret = IMRET_FAILURE; 2934 error_msg(gettext("Unable to kill " 2935 "start process (%ld) of instance %s: %s"), 2936 rv->val, instance->fmri, strerror(errno)); 2937 } 2938 } 2939 return (ret); 2940 } 2941 2942 /* 2943 * Runs the specified method of the specified service instance. 2944 * If the method was never specified, we handle it the same as if the 2945 * method was called and returned success, carrying on any transition the 2946 * instance may be in the midst of. 2947 * If the method isn't executable in its specified profile or an error occurs 2948 * forking a process to run the method in the function returns -1. 2949 * If a method binary is successfully executed, the function switches the 2950 * instance's cur state to the method's associated 'run' state and the next 2951 * state to the methods associated next state. 2952 * Returns -1 if there's an error before forking, else 0. 2953 */ 2954 int 2955 run_method(instance_t *instance, instance_method_t method, 2956 const proto_info_t *start_info) 2957 { 2958 pid_t child_pid; 2959 method_info_t *mi; 2960 struct method_context *mthd_ctxt = NULL; 2961 const char *errstr; 2962 int sig = 0; 2963 int ret; 2964 instance_cfg_t *cfg = instance->config; 2965 ctid_t cid; 2966 boolean_t trans_failure = B_TRUE; 2967 int serrno; 2968 2969 debug_msg("Entering run_method, instance: %s, method: %s", 2970 instance->fmri, methods[method].name); 2971 2972 /* 2973 * Don't bother updating the instance's state for the start method 2974 * as there isn't a separate start method state. 2975 */ 2976 if (method != IM_START) 2977 update_instance_states(instance, get_method_state(method), 2978 methods[method].dst_state, 2979 get_method_error_success(method)); 2980 2981 if ((mi = cfg->methods[method]) == NULL) { 2982 /* 2983 * If the absent method is IM_OFFLINE, default action needs 2984 * to be taken to avoid lingering processes which can prevent 2985 * the upcoming rebinding from happening. 2986 */ 2987 if ((method == IM_OFFLINE) && instance->config->basic->iswait) { 2988 warn_msg(gettext("inetd_offline method for instance %s " 2989 "is unspecified. Taking default action: kill."), 2990 instance->fmri); 2991 (void) str2sig("TERM", &sig); 2992 ret = smf_kill_process(instance, sig); 2993 process_non_start_term(instance, ret); 2994 return (0); 2995 } else { 2996 process_non_start_term(instance, IMRET_SUCCESS); 2997 return (0); 2998 } 2999 } 3000 3001 /* Handle special method tokens, not allowed on start */ 3002 if (method != IM_START) { 3003 if (restarter_is_null_method(mi->exec_path)) { 3004 /* :true means nothing should be done */ 3005 process_non_start_term(instance, IMRET_SUCCESS); 3006 return (0); 3007 } 3008 3009 if ((sig = restarter_is_kill_method(mi->exec_path)) >= 0) { 3010 /* Carry out contract assassination */ 3011 ret = iterate_repository_contracts(instance, sig); 3012 /* ENOENT means we didn't find any contracts */ 3013 if (ret != 0 && ret != ENOENT) { 3014 error_msg(gettext("Failed to send signal %d " 3015 "to contracts of instance %s: %s"), sig, 3016 instance->fmri, strerror(ret)); 3017 goto prefork_failure; 3018 } else { 3019 process_non_start_term(instance, IMRET_SUCCESS); 3020 return (0); 3021 } 3022 } 3023 3024 if ((sig = restarter_is_kill_proc_method(mi->exec_path)) >= 0) { 3025 ret = smf_kill_process(instance, sig); 3026 process_non_start_term(instance, ret); 3027 return (0); 3028 } 3029 } 3030 3031 /* 3032 * Get the associated method context before the fork so we can 3033 * modify the instances state if things go wrong. 3034 */ 3035 if ((mthd_ctxt = read_method_context(instance->fmri, 3036 methods[method].name, mi->exec_path, &errstr)) == NULL) { 3037 error_msg(gettext("Failed to retrieve method context for the " 3038 "%s method of instance %s: %s"), methods[method].name, 3039 instance->fmri, errstr); 3040 goto prefork_failure; 3041 } 3042 3043 /* 3044 * Perform some basic checks before we fork to limit the possibility 3045 * of exec failures, so we can modify the instance state if necessary. 3046 */ 3047 if (!passes_basic_exec_checks(instance->fmri, methods[method].name, 3048 mi->exec_path)) { 3049 trans_failure = B_FALSE; 3050 goto prefork_failure; 3051 } 3052 3053 if (contract_prefork() == -1) 3054 goto prefork_failure; 3055 child_pid = fork(); 3056 serrno = errno; 3057 contract_postfork(); 3058 3059 switch (child_pid) { 3060 case -1: 3061 error_msg(gettext( 3062 "Unable to fork %s method of instance %s: %s"), 3063 methods[method].name, instance->fmri, strerror(serrno)); 3064 if ((serrno != EAGAIN) && (serrno != ENOMEM)) 3065 trans_failure = B_FALSE; 3066 goto prefork_failure; 3067 case 0: /* child */ 3068 exec_method(instance, method, mi, mthd_ctxt, start_info); 3069 /* NOTREACHED */ 3070 default: /* parent */ 3071 restarter_free_method_context(mthd_ctxt); 3072 mthd_ctxt = NULL; 3073 3074 if (get_latest_contract(&cid) < 0) 3075 cid = -1; 3076 3077 /* 3078 * Register this method so its termination is noticed and 3079 * the state transition this method participates in is 3080 * continued. 3081 */ 3082 if (register_method(instance, child_pid, cid, method) != 0) { 3083 /* 3084 * Since we will never find out about the termination 3085 * of this method, if it's a non-start method treat 3086 * is as a failure so we don't block restarter event 3087 * processing on it whilst it languishes in a method 3088 * running state. 3089 */ 3090 error_msg(gettext("Failed to monitor status of " 3091 "%s method of instance %s"), methods[method].name, 3092 instance->fmri); 3093 if (method != IM_START) 3094 process_non_start_term(instance, IMRET_FAILURE); 3095 } 3096 3097 add_method_ids(instance, child_pid, cid, method); 3098 3099 /* do tcp tracing for those nowait instances that request it */ 3100 if ((method == IM_START) && cfg->basic->do_tcp_trace && 3101 !cfg->basic->iswait) { 3102 char buf[INET6_ADDRSTRLEN]; 3103 3104 syslog(LOG_NOTICE, "%s[%d] from %s %d", 3105 cfg->basic->svc_name, child_pid, 3106 inet_ntop_native(instance->remote_addr.ss_family, 3107 SS_SINADDR(instance->remote_addr), buf, 3108 sizeof (buf)), 3109 ntohs(SS_PORT(instance->remote_addr))); 3110 } 3111 } 3112 3113 return (0); 3114 3115 prefork_failure: 3116 if (mthd_ctxt != NULL) { 3117 restarter_free_method_context(mthd_ctxt); 3118 mthd_ctxt = NULL; 3119 } 3120 3121 if (method == IM_START) { 3122 /* 3123 * Only place a start method in maintenance if we're sure 3124 * that the failure was non-transient. 3125 */ 3126 if (!trans_failure) { 3127 destroy_bound_fds(instance); 3128 update_state(instance, IIS_MAINTENANCE, RERR_FAULT); 3129 } 3130 } else { 3131 /* treat the failure as if the method ran and failed */ 3132 process_non_start_term(instance, IMRET_FAILURE); 3133 } 3134 3135 return (-1); 3136 } 3137 3138 static int 3139 accept_connection(instance_t *instance, proto_info_t *pi) 3140 { 3141 int fd; 3142 socklen_t size; 3143 3144 debug_msg("Entering accept_connection"); 3145 3146 if (instance->config->basic->istlx) { 3147 fd = tlx_accept(instance->fmri, (tlx_info_t *)pi, 3148 &(instance->remote_addr)); 3149 } else { 3150 size = sizeof (instance->remote_addr); 3151 fd = accept(pi->listen_fd, 3152 (struct sockaddr *)&(instance->remote_addr), &size); 3153 if (fd < 0) 3154 error_msg("accept: %s", strerror(errno)); 3155 } 3156 3157 return (fd); 3158 } 3159 3160 /* 3161 * Handle an incoming connection request for a nowait service. 3162 * This involves accepting the incoming connection on a new fd. Connection 3163 * rate checks are then performed, transitioning the service to the 3164 * conrate offline state if these fail. Otherwise, the service's start method 3165 * is run (performing TCP wrappers checks if applicable as we do), and on 3166 * success concurrent copies checking is done, transitioning the service to the 3167 * copies offline state if this fails. 3168 */ 3169 static void 3170 process_nowait_request(instance_t *instance, proto_info_t *pi) 3171 { 3172 basic_cfg_t *cfg = instance->config->basic; 3173 int ret; 3174 adt_event_data_t *ae; 3175 char buf[BUFSIZ]; 3176 3177 debug_msg("Entering process_nowait_req"); 3178 3179 /* accept nowait service connections on a new fd */ 3180 if ((instance->conn_fd = accept_connection(instance, pi)) == -1) { 3181 /* 3182 * Failed accept. Return and allow the event loop to initiate 3183 * another attempt later if the request is still present. 3184 */ 3185 return; 3186 } 3187 3188 /* 3189 * Limit connection rate of nowait services. If either conn_rate_max 3190 * or conn_rate_offline are <= 0, no connection rate limit checking 3191 * is done. If the configured rate is exceeded, the instance is taken 3192 * to the connrate_offline state and a timer scheduled to try and 3193 * bring the instance back online after the configured offline time. 3194 */ 3195 if ((cfg->conn_rate_max > 0) && (cfg->conn_rate_offline > 0)) { 3196 if (instance->conn_rate_count++ == 0) { 3197 instance->conn_rate_start = time(NULL); 3198 } else if (instance->conn_rate_count > 3199 cfg->conn_rate_max) { 3200 time_t now = time(NULL); 3201 3202 if ((now - instance->conn_rate_start) > 1) { 3203 instance->conn_rate_start = now; 3204 instance->conn_rate_count = 1; 3205 } else { 3206 /* Generate audit record */ 3207 if ((ae = adt_alloc_event(audit_handle, 3208 ADT_inetd_ratelimit)) == NULL) { 3209 error_msg(gettext("Unable to allocate " 3210 "rate limit audit event")); 3211 } else { 3212 adt_inetd_ratelimit_t *rl = 3213 &ae->adt_inetd_ratelimit; 3214 /* 3215 * The inetd_ratelimit audit 3216 * record consists of: 3217 * Service name 3218 * Connection rate limit 3219 */ 3220 rl->service_name = cfg->svc_name; 3221 (void) snprintf(buf, sizeof (buf), 3222 "limit=%lld", cfg->conn_rate_max); 3223 rl->limit = buf; 3224 (void) adt_put_event(ae, ADT_SUCCESS, 3225 ADT_SUCCESS); 3226 adt_free_event(ae); 3227 } 3228 3229 error_msg(gettext( 3230 "Instance %s has exceeded its configured " 3231 "connection rate, additional connections " 3232 "will not be accepted for %d seconds"), 3233 instance->fmri, cfg->conn_rate_offline); 3234 3235 close_net_fd(instance, instance->conn_fd); 3236 instance->conn_fd = -1; 3237 3238 destroy_bound_fds(instance); 3239 3240 instance->conn_rate_count = 0; 3241 3242 instance->conn_rate_exceeded = B_TRUE; 3243 (void) run_method(instance, IM_OFFLINE, NULL); 3244 3245 return; 3246 } 3247 } 3248 } 3249 3250 ret = run_method(instance, IM_START, pi); 3251 3252 close_net_fd(instance, instance->conn_fd); 3253 instance->conn_fd = -1; 3254 3255 if (ret == -1) /* the method wasn't forked */ 3256 return; 3257 3258 instance->copies++; 3259 3260 /* 3261 * Limit concurrent connections of nowait services. 3262 */ 3263 if (copies_limit_exceeded(instance)) { 3264 /* Generate audit record */ 3265 if ((ae = adt_alloc_event(audit_handle, ADT_inetd_copylimit)) 3266 == NULL) { 3267 error_msg(gettext("Unable to allocate copy limit " 3268 "audit event")); 3269 } else { 3270 /* 3271 * The inetd_copylimit audit record consists of: 3272 * Service name 3273 * Copy limit 3274 */ 3275 ae->adt_inetd_copylimit.service_name = cfg->svc_name; 3276 (void) snprintf(buf, sizeof (buf), "limit=%lld", 3277 cfg->max_copies); 3278 ae->adt_inetd_copylimit.limit = buf; 3279 (void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS); 3280 adt_free_event(ae); 3281 } 3282 3283 warn_msg(gettext("Instance %s has reached its maximum " 3284 "configured copies, no new connections will be accepted"), 3285 instance->fmri); 3286 destroy_bound_fds(instance); 3287 (void) run_method(instance, IM_OFFLINE, NULL); 3288 } 3289 } 3290 3291 /* 3292 * Handle an incoming request for a wait type service. 3293 * Failure rate checking is done first, taking the service to the maintenance 3294 * state if the checks fail. Following this, the service's start method is run, 3295 * and on success, we stop listening for new requests for this service. 3296 */ 3297 static void 3298 process_wait_request(instance_t *instance, const proto_info_t *pi) 3299 { 3300 basic_cfg_t *cfg = instance->config->basic; 3301 int ret; 3302 adt_event_data_t *ae; 3303 char buf[BUFSIZ]; 3304 3305 debug_msg("Entering process_wait_request"); 3306 3307 instance->conn_fd = pi->listen_fd; 3308 3309 /* 3310 * Detect broken servers and transition them to maintenance. If a 3311 * wait type service exits without accepting the connection or 3312 * consuming (reading) the datagram, that service's descriptor will 3313 * select readable again, and inetd will fork another instance of 3314 * the server. If either wait_fail_cnt or wait_fail_interval are <= 0, 3315 * no failure rate detection is done. 3316 */ 3317 if ((cfg->wait_fail_cnt > 0) && (cfg->wait_fail_interval > 0)) { 3318 if (instance->fail_rate_count++ == 0) { 3319 instance->fail_rate_start = time(NULL); 3320 } else if (instance->fail_rate_count > cfg->wait_fail_cnt) { 3321 time_t now = time(NULL); 3322 3323 if ((now - instance->fail_rate_start) > 3324 cfg->wait_fail_interval) { 3325 instance->fail_rate_start = now; 3326 instance->fail_rate_count = 1; 3327 } else { 3328 /* Generate audit record */ 3329 if ((ae = adt_alloc_event(audit_handle, 3330 ADT_inetd_failrate)) == NULL) { 3331 error_msg(gettext("Unable to allocate " 3332 "failure rate audit event")); 3333 } else { 3334 adt_inetd_failrate_t *fr = 3335 &ae->adt_inetd_failrate; 3336 /* 3337 * The inetd_failrate audit record 3338 * consists of: 3339 * Service name 3340 * Failure rate 3341 * Interval 3342 * Last two are expressed as k=v pairs 3343 * in the values field. 3344 */ 3345 fr->service_name = cfg->svc_name; 3346 (void) snprintf(buf, sizeof (buf), 3347 "limit=%lld,interval=%d", 3348 cfg->wait_fail_cnt, 3349 cfg->wait_fail_interval); 3350 fr->values = buf; 3351 (void) adt_put_event(ae, ADT_SUCCESS, 3352 ADT_SUCCESS); 3353 adt_free_event(ae); 3354 } 3355 3356 error_msg(gettext( 3357 "Instance %s has exceeded its configured " 3358 "failure rate, transitioning to " 3359 "maintenance"), instance->fmri); 3360 instance->fail_rate_count = 0; 3361 3362 destroy_bound_fds(instance); 3363 3364 instance->maintenance_req = B_TRUE; 3365 (void) run_method(instance, IM_OFFLINE, NULL); 3366 return; 3367 } 3368 } 3369 } 3370 3371 ret = run_method(instance, IM_START, pi); 3372 3373 instance->conn_fd = -1; 3374 3375 if (ret == 0) { 3376 /* 3377 * Stop listening for connections now we've fired off the 3378 * server for a wait type instance. 3379 */ 3380 (void) poll_bound_fds(instance, B_FALSE); 3381 } 3382 } 3383 3384 /* 3385 * Process any networks requests for each proto for each instance. 3386 */ 3387 void 3388 process_network_events(void) 3389 { 3390 instance_t *instance; 3391 3392 debug_msg("Entering process_network_events"); 3393 3394 for (instance = uu_list_first(instance_list); instance != NULL; 3395 instance = uu_list_next(instance_list, instance)) { 3396 basic_cfg_t *cfg; 3397 proto_info_t *pi; 3398 3399 /* 3400 * Ignore instances in states that definitely don't have any 3401 * listening fds. 3402 */ 3403 switch (instance->cur_istate) { 3404 case IIS_ONLINE: 3405 case IIS_DEGRADED: 3406 case IIS_IN_REFRESH_METHOD: 3407 break; 3408 default: 3409 continue; 3410 } 3411 3412 cfg = instance->config->basic; 3413 3414 for (pi = uu_list_first(cfg->proto_list); pi != NULL; 3415 pi = uu_list_next(cfg->proto_list, pi)) { 3416 if ((pi->listen_fd != -1) && 3417 isset_pollfd(pi->listen_fd)) { 3418 if (cfg->iswait) { 3419 process_wait_request(instance, pi); 3420 } else { 3421 process_nowait_request(instance, pi); 3422 } 3423 } 3424 } 3425 } 3426 } 3427 3428 /* ARGSUSED0 */ 3429 static void 3430 sigterm_handler(int sig) 3431 { 3432 debug_msg("Entering sigterm_handler"); 3433 3434 got_sigterm = B_TRUE; 3435 } 3436 3437 /* ARGSUSED0 */ 3438 static void 3439 sighup_handler(int sig) 3440 { 3441 debug_msg("Entering sighup_handler"); 3442 3443 refresh_inetd_requested = B_TRUE; 3444 } 3445 3446 /* 3447 * inetd's major work loop. This function sits in poll waiting for events 3448 * to occur, processing them when they do. The possible events are 3449 * master restarter requests, expired timer queue timers, stop/refresh signal 3450 * requests, contract events indicating process termination, stop/refresh 3451 * requests originating from one of the stop/refresh inetd processes and 3452 * network events. 3453 * The loop is exited when a stop request is received and processed, and 3454 * all the instances have reached a suitable 'stopping' state. 3455 */ 3456 static void 3457 event_loop(void) 3458 { 3459 instance_t *instance; 3460 int timeout; 3461 3462 debug_msg("Entering event_loop"); 3463 3464 for (;;) { 3465 int pret = -1; 3466 3467 timeout = iu_earliest_timer(timer_queue); 3468 3469 debug_msg("Doing signal check/poll"); 3470 if (!got_sigterm && !refresh_inetd_requested) { 3471 pret = poll(poll_fds, num_pollfds, timeout); 3472 if ((pret == -1) && (errno != EINTR)) { 3473 error_msg(gettext("poll failure: %s"), 3474 strerror(errno)); 3475 continue; 3476 } 3477 debug_msg("Exiting poll, returned: %d", pret); 3478 } 3479 3480 if (got_sigterm) { 3481 msg_fini(); 3482 inetd_stop(); 3483 got_sigterm = B_FALSE; 3484 goto check_if_stopped; 3485 } 3486 3487 /* 3488 * Process any stop/refresh requests from the Unix Domain 3489 * Socket. 3490 */ 3491 if ((pret != -1) && isset_pollfd(uds_fd)) { 3492 while (process_uds_event() == 0) 3493 ; 3494 } 3495 3496 /* 3497 * Process refresh request. We do this check after the UDS 3498 * event check above, as it would be wasted processing if we 3499 * started refreshing inetd based on a SIGHUP, and then were 3500 * told to shut-down via a UDS event. 3501 */ 3502 if (refresh_inetd_requested) { 3503 refresh_inetd_requested = B_FALSE; 3504 if (!inetd_stopping) 3505 inetd_refresh(); 3506 } 3507 3508 /* 3509 * We were interrupted by a signal. Don't waste any more 3510 * time processing a potentially inaccurate poll return. 3511 */ 3512 if (pret == -1) 3513 continue; 3514 3515 /* 3516 * Process any instance restarter events. 3517 */ 3518 if (isset_pollfd(rst_event_pipe[PE_CONSUMER])) { 3519 while (process_restarter_event() == 0) 3520 ; 3521 } 3522 3523 /* 3524 * Process any expired timers (bind retry, con-rate offline, 3525 * method timeouts). 3526 */ 3527 (void) iu_expire_timers(timer_queue); 3528 3529 process_terminated_methods(); 3530 3531 /* 3532 * If inetd is stopping, check whether all our managed 3533 * instances have been stopped and we can return. 3534 */ 3535 if (inetd_stopping) { 3536 check_if_stopped: 3537 for (instance = uu_list_first(instance_list); 3538 instance != NULL; 3539 instance = uu_list_next(instance_list, instance)) { 3540 if (!instance_stopped(instance)) { 3541 debug_msg("%s not yet stopped", 3542 instance->fmri); 3543 break; 3544 } 3545 } 3546 /* if all instances are stopped, return */ 3547 if (instance == NULL) 3548 return; 3549 } 3550 3551 process_network_events(); 3552 } 3553 } 3554 3555 static void 3556 fini(void) 3557 { 3558 debug_msg("Entering fini"); 3559 3560 method_fini(); 3561 uds_fini(); 3562 if (timer_queue != NULL) 3563 iu_tq_destroy(timer_queue); 3564 3565 3566 /* 3567 * We don't bother to undo the restarter interface at all. 3568 * Because of quirks in the interface, there is no way to 3569 * disconnect from the channel and cause any new events to be 3570 * queued. However, any events which are received and not 3571 * acknowledged will be re-sent when inetd restarts as long as inetd 3572 * uses the same subscriber ID, which it does. 3573 * 3574 * By keeping the event pipe open but ignoring it, any events which 3575 * occur will cause restarter_event_proxy to hang without breaking 3576 * anything. 3577 */ 3578 3579 if (instance_list != NULL) { 3580 void *cookie = NULL; 3581 instance_t *inst; 3582 3583 while ((inst = uu_list_teardown(instance_list, &cookie)) != 3584 NULL) 3585 destroy_instance(inst); 3586 uu_list_destroy(instance_list); 3587 } 3588 if (instance_pool != NULL) 3589 uu_list_pool_destroy(instance_pool); 3590 tlx_fini(); 3591 config_fini(); 3592 repval_fini(); 3593 poll_fini(); 3594 3595 /* Close audit session */ 3596 (void) adt_end_session(audit_handle); 3597 } 3598 3599 static int 3600 init(void) 3601 { 3602 int err; 3603 3604 debug_msg("Entering init"); 3605 3606 if (repval_init() < 0) 3607 goto failed; 3608 3609 if (config_init() < 0) 3610 goto failed; 3611 3612 if (tlx_init() < 0) 3613 goto failed; 3614 3615 /* Setup instance list. */ 3616 if ((instance_pool = uu_list_pool_create("instance_pool", 3617 sizeof (instance_t), offsetof(instance_t, link), NULL, 3618 UU_LIST_POOL_DEBUG)) == NULL) { 3619 error_msg("%s: %s", 3620 gettext("Failed to create instance pool"), 3621 uu_strerror(uu_error())); 3622 goto failed; 3623 } 3624 if ((instance_list = uu_list_create(instance_pool, NULL, 0)) == NULL) { 3625 error_msg("%s: %s", 3626 gettext("Failed to create instance list"), 3627 uu_strerror(uu_error())); 3628 goto failed; 3629 } 3630 3631 /* 3632 * Create event pipe to communicate events with the main event 3633 * loop and add it to the event loop's fdset. 3634 */ 3635 if (pipe(rst_event_pipe) < 0) { 3636 error_msg("pipe: %s", strerror(errno)); 3637 goto failed; 3638 } 3639 /* 3640 * We only leave the producer end to block on reads/writes as we 3641 * can't afford to block in the main thread, yet need to in 3642 * the restarter event thread, so it can sit and wait for an 3643 * acknowledgement to be written to the pipe. 3644 */ 3645 disable_blocking(rst_event_pipe[PE_CONSUMER]); 3646 if ((set_pollfd(rst_event_pipe[PE_CONSUMER], POLLIN)) == -1) 3647 goto failed; 3648 3649 /* 3650 * Register with master restarter for managed service events. This 3651 * will fail, amongst other reasons, if inetd is already running. 3652 */ 3653 if ((err = restarter_bind_handle(RESTARTER_EVENT_VERSION, 3654 INETD_INSTANCE_FMRI, restarter_event_proxy, 0, 3655 &rst_event_handle)) != 0) { 3656 error_msg(gettext( 3657 "Failed to register for restarter events: %s"), 3658 strerror(err)); 3659 goto failed; 3660 } 3661 3662 if (contract_init() < 0) 3663 goto failed; 3664 3665 if ((timer_queue = iu_tq_create()) == NULL) { 3666 error_msg(gettext("Failed to create timer queue.")); 3667 goto failed; 3668 } 3669 3670 if (uds_init() < 0) 3671 goto failed; 3672 3673 if (method_init() < 0) 3674 goto failed; 3675 3676 /* Initialize auditing session */ 3677 if (adt_start_session(&audit_handle, NULL, ADT_USE_PROC_DATA) != 0) { 3678 error_msg(gettext("Unable to start audit session")); 3679 } 3680 3681 /* 3682 * Initialize signal dispositions/masks 3683 */ 3684 (void) sigset(SIGHUP, sighup_handler); 3685 (void) sigset(SIGTERM, sigterm_handler); 3686 (void) sigignore(SIGINT); 3687 3688 return (0); 3689 3690 failed: 3691 fini(); 3692 return (-1); 3693 } 3694 3695 static int 3696 start_method(void) 3697 { 3698 int i; 3699 int pipe_fds[2]; 3700 int child; 3701 3702 debug_msg("ENTERING START_METHOD:"); 3703 3704 /* Create pipe for child to notify parent of initialization success. */ 3705 if (pipe(pipe_fds) < 0) { 3706 debug_msg("pipe: %s", strerror(errno)); 3707 return (SMF_EXIT_ERR_OTHER); 3708 } 3709 3710 if ((child = fork()) == -1) { 3711 debug_msg("fork: %s", strerror(errno)); 3712 (void) close(pipe_fds[PE_CONSUMER]); 3713 (void) close(pipe_fds[PE_PRODUCER]); 3714 return (SMF_EXIT_ERR_OTHER); 3715 } else if (child > 0) { /* parent */ 3716 3717 /* Wait on child to return success of initialization. */ 3718 (void) close(pipe_fds[PE_PRODUCER]); 3719 if ((safe_read(pipe_fds[PE_CONSUMER], &i, sizeof (i)) != 0) || 3720 (i < 0)) { 3721 error_msg(gettext( 3722 "Initialization failed, unable to start")); 3723 (void) close(pipe_fds[PE_CONSUMER]); 3724 /* 3725 * Batch all initialization errors as 'other' errors, 3726 * resulting in retries being attempted. 3727 */ 3728 return (SMF_EXIT_ERR_OTHER); 3729 } else { 3730 (void) close(pipe_fds[PE_CONSUMER]); 3731 return (SMF_EXIT_OK); 3732 } 3733 } else { /* child */ 3734 /* 3735 * Perform initialization and return success code down 3736 * the pipe. 3737 */ 3738 (void) close(pipe_fds[PE_CONSUMER]); 3739 i = init(); 3740 if ((safe_write(pipe_fds[PE_PRODUCER], &i, sizeof (i)) < 0) || 3741 (i < 0)) { 3742 error_msg(gettext("pipe write failure: %s"), 3743 strerror(errno)); 3744 exit(1); 3745 } 3746 (void) close(pipe_fds[PE_PRODUCER]); 3747 3748 (void) setsid(); 3749 3750 /* 3751 * Log a message if the configuration file has changed since 3752 * inetconv was last run. 3753 */ 3754 check_conf_file(); 3755 3756 event_loop(); 3757 3758 fini(); 3759 debug_msg("inetd stopped"); 3760 msg_fini(); 3761 exit(0); 3762 } 3763 /* NOTREACHED */ 3764 } 3765 3766 /* 3767 * When inetd is run from outside the SMF, this message is output to provide 3768 * the person invoking inetd with further information that will help them 3769 * understand how to start and stop inetd, and to achieve the other 3770 * behaviors achievable with the legacy inetd command line interface, if 3771 * it is possible. 3772 */ 3773 static void 3774 legacy_usage(void) 3775 { 3776 (void) fprintf(stderr, 3777 "inetd is now an smf(5) managed service and can no longer be run " 3778 "from the\n" 3779 "command line. To enable or disable inetd refer to svcadm(1M) on\n" 3780 "how to enable \"%s\", the inetd instance.\n" 3781 "\n" 3782 "The traditional inetd command line option mappings are:\n" 3783 "\t-d : there is no supported debug output\n" 3784 "\t-s : inetd is only runnable from within the SMF\n" 3785 "\t-t : See inetadm(1M) on how to enable TCP tracing\n" 3786 "\t-r : See inetadm(1M) on how to set a failure rate\n" 3787 "\n" 3788 "To specify an alternative configuration file see svccfg(1M)\n" 3789 "for how to modify the \"%s/%s\" string type property of\n" 3790 "the inetd instance, and modify it according to the syntax:\n" 3791 "\"%s [alt_config_file] %%m\".\n" 3792 "\n" 3793 "For further information on inetd see inetd(1M).\n", 3794 INETD_INSTANCE_FMRI, START_METHOD_ARG, SCF_PROPERTY_EXEC, 3795 INETD_PATH); 3796 } 3797 3798 /* 3799 * Usage message printed out for usage errors when running under the SMF. 3800 */ 3801 static void 3802 smf_usage(const char *arg0) 3803 { 3804 error_msg("Usage: %s [alt_conf_file] %s|%s|%s", arg0, START_METHOD_ARG, 3805 STOP_METHOD_ARG, REFRESH_METHOD_ARG); 3806 } 3807 3808 /* 3809 * Returns B_TRUE if we're being run from within the SMF, else B_FALSE. 3810 */ 3811 static boolean_t 3812 run_through_smf(void) 3813 { 3814 char *fmri; 3815 3816 /* 3817 * check if the instance fmri environment variable has been set by 3818 * our restarter. 3819 */ 3820 return (((fmri = getenv("SMF_FMRI")) != NULL) && 3821 (strcmp(fmri, INETD_INSTANCE_FMRI) == 0)); 3822 } 3823 3824 int 3825 main(int argc, char *argv[]) 3826 { 3827 char *method; 3828 int ret; 3829 3830 #if !defined(TEXT_DOMAIN) 3831 #define TEXT_DOMAIN "SYS_TEST" 3832 #endif 3833 (void) textdomain(TEXT_DOMAIN); 3834 (void) setlocale(LC_ALL, ""); 3835 3836 if (!run_through_smf()) { 3837 legacy_usage(); 3838 return (SMF_EXIT_ERR_NOSMF); 3839 } 3840 3841 msg_init(); /* setup logging */ 3842 3843 (void) enable_extended_FILE_stdio(-1, -1); 3844 3845 /* inetd invocation syntax is inetd [alt_conf_file] method_name */ 3846 3847 switch (argc) { 3848 case 2: 3849 method = argv[1]; 3850 break; 3851 case 3: 3852 conf_file = argv[1]; 3853 method = argv[2]; 3854 break; 3855 default: 3856 smf_usage(argv[0]); 3857 return (SMF_EXIT_ERR_CONFIG); 3858 3859 } 3860 3861 if (strcmp(method, START_METHOD_ARG) == 0) { 3862 ret = start_method(); 3863 } else if (strcmp(method, STOP_METHOD_ARG) == 0) { 3864 ret = stop_method(); 3865 } else if (strcmp(method, REFRESH_METHOD_ARG) == 0) { 3866 ret = refresh_method(); 3867 } else { 3868 smf_usage(argv[0]); 3869 return (SMF_EXIT_ERR_CONFIG); 3870 } 3871 3872 return (ret); 3873 } 3874