10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51712Srm88369 * Common Development and Distribution License (the "License"). 61712Srm88369 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*6073Sacruz * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * NOTES: To be expanded. 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * The SMF inetd. 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * Below are some high level notes of the operation of the SMF inetd. The 340Sstevel@tonic-gate * notes don't go into any real detail, and the viewer of this file is 350Sstevel@tonic-gate * encouraged to look at the code and its associated comments to better 360Sstevel@tonic-gate * understand inetd's operation. This saves the potential for the code 370Sstevel@tonic-gate * and these notes diverging over time. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * Inetd's major work is done from the context of event_loop(). Within this 400Sstevel@tonic-gate * loop, inetd polls for events arriving from a number of different file 410Sstevel@tonic-gate * descriptors, representing the following event types, and initiates 420Sstevel@tonic-gate * any necessary event processing: 430Sstevel@tonic-gate * - incoming network connections/datagrams. 440Sstevel@tonic-gate * - notification of terminated processes (discovered via contract events). 450Sstevel@tonic-gate * - instance specific events originating from the SMF master restarter. 460Sstevel@tonic-gate * - stop/refresh requests from the inetd method processes (coming in on a 470Sstevel@tonic-gate * Unix Domain socket). 480Sstevel@tonic-gate * There's also a timeout set for the poll, which is set to the nearest 490Sstevel@tonic-gate * scheduled timer in a timer queue that inetd uses to perform delayed 500Sstevel@tonic-gate * processing, such as bind retries. 510Sstevel@tonic-gate * The SIGHUP and SIGINT signals can also interrupt the poll, and will 520Sstevel@tonic-gate * result in inetd being refreshed or stopped respectively, as was the 530Sstevel@tonic-gate * behavior with the old inetd. 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * Inetd implements a state machine for each instance. The states within the 560Sstevel@tonic-gate * machine are: offline, online, disabled, maintenance, uninitialized and 570Sstevel@tonic-gate * specializations of the offline state for when an instance exceeds one of 580Sstevel@tonic-gate * its DOS limits. The state of an instance can be changed as a 590Sstevel@tonic-gate * result/side-effect of one of the above events occurring, or inetd being 600Sstevel@tonic-gate * started up. The ongoing state of an instance is stored in the SMF 610Sstevel@tonic-gate * repository, as required of SMF restarters. This enables an administrator 620Sstevel@tonic-gate * to view the state of each instance, and, if inetd was to terminate 630Sstevel@tonic-gate * unexpectedly, it could use the stored state to re-commence where it left off. 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * Within the state machine a number of methods are run (if provided) as part 660Sstevel@tonic-gate * of a state transition to aid/ effect a change in an instance's state. The 670Sstevel@tonic-gate * supported methods are: offline, online, disable, refresh and start. The 680Sstevel@tonic-gate * latter of these is the equivalent of the server program and its arguments 690Sstevel@tonic-gate * in the old inetd. 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * Events from the SMF master restarter come in on a number of threads 720Sstevel@tonic-gate * created in the registration routine of librestart, the delegated restarter 730Sstevel@tonic-gate * library. These threads call into the restart_event_proxy() function 740Sstevel@tonic-gate * when an event arrives. To serialize the processing of instances, these events 750Sstevel@tonic-gate * are then written down a pipe to the process's main thread, which listens 760Sstevel@tonic-gate * for these events via a poll call, with the file descriptor of the other 770Sstevel@tonic-gate * end of the pipe in its read set, and processes the event appropriately. 780Sstevel@tonic-gate * When the event has been processed (which may be delayed if the instance 790Sstevel@tonic-gate * for which the event is for is in the process of executing one of its methods 800Sstevel@tonic-gate * as part of a state transition) it writes an acknowledgement back down the 810Sstevel@tonic-gate * pipe the event was received on. The thread in restart_event_proxy() that 820Sstevel@tonic-gate * wrote the event will read the acknowledgement it was blocked upon, and will 830Sstevel@tonic-gate * then be able to return to its caller, thus implicitly acknowledging the 840Sstevel@tonic-gate * event, and allowing another event to be written down the pipe for the main 850Sstevel@tonic-gate * thread to process. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate 880Sstevel@tonic-gate 890Sstevel@tonic-gate #include <netdb.h> 900Sstevel@tonic-gate #include <stdio.h> 911914Scasper #include <stdio_ext.h> 920Sstevel@tonic-gate #include <stdlib.h> 930Sstevel@tonic-gate #include <strings.h> 940Sstevel@tonic-gate #include <unistd.h> 950Sstevel@tonic-gate #include <assert.h> 960Sstevel@tonic-gate #include <sys/types.h> 970Sstevel@tonic-gate #include <sys/socket.h> 980Sstevel@tonic-gate #include <netinet/in.h> 990Sstevel@tonic-gate #include <fcntl.h> 1000Sstevel@tonic-gate #include <signal.h> 1010Sstevel@tonic-gate #include <errno.h> 1020Sstevel@tonic-gate #include <locale.h> 1030Sstevel@tonic-gate #include <syslog.h> 1040Sstevel@tonic-gate #include <libintl.h> 1050Sstevel@tonic-gate #include <librestart.h> 1060Sstevel@tonic-gate #include <pthread.h> 1070Sstevel@tonic-gate #include <sys/stat.h> 1080Sstevel@tonic-gate #include <time.h> 1090Sstevel@tonic-gate #include <limits.h> 1100Sstevel@tonic-gate #include <libgen.h> 1110Sstevel@tonic-gate #include <tcpd.h> 1120Sstevel@tonic-gate #include <libscf.h> 1130Sstevel@tonic-gate #include <libuutil.h> 1140Sstevel@tonic-gate #include <stddef.h> 1150Sstevel@tonic-gate #include <bsm/adt_event.h> 1164357Srs200217 #include <ucred.h> 1170Sstevel@tonic-gate #include "inetd_impl.h" 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* path to inetd's binary */ 1200Sstevel@tonic-gate #define INETD_PATH "/usr/lib/inet/inetd" 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * inetd's default configuration file paths. /etc/inetd/inetd.conf is set 1240Sstevel@tonic-gate * be be the primary file, so it is checked before /etc/inetd.conf. 1250Sstevel@tonic-gate */ 1260Sstevel@tonic-gate #define PRIMARY_DEFAULT_CONF_FILE "/etc/inet/inetd.conf" 1270Sstevel@tonic-gate #define SECONDARY_DEFAULT_CONF_FILE "/etc/inetd.conf" 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* Arguments passed to this binary to request which method to execute. */ 1300Sstevel@tonic-gate #define START_METHOD_ARG "start" 1310Sstevel@tonic-gate #define STOP_METHOD_ARG "stop" 1320Sstevel@tonic-gate #define REFRESH_METHOD_ARG "refresh" 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* connection backlog for unix domain socket */ 1350Sstevel@tonic-gate #define UDS_BACKLOG 2 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate /* number of retries to recv() a request on the UDS socket before giving up */ 1380Sstevel@tonic-gate #define UDS_RECV_RETRIES 10 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /* enumeration of the different ends of a pipe */ 1410Sstevel@tonic-gate enum pipe_end { 1420Sstevel@tonic-gate PE_CONSUMER, 1430Sstevel@tonic-gate PE_PRODUCER 1440Sstevel@tonic-gate }; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate typedef struct { 1470Sstevel@tonic-gate internal_inst_state_t istate; 1480Sstevel@tonic-gate const char *name; 1490Sstevel@tonic-gate restarter_instance_state_t smf_state; 1500Sstevel@tonic-gate instance_method_t method_running; 1510Sstevel@tonic-gate } state_info_t; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * Collection of information for each state. 1560Sstevel@tonic-gate * NOTE: This table is indexed into using the internal_inst_state_t 1570Sstevel@tonic-gate * enumeration, so the ordering needs to be kept in synch. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate static state_info_t states[] = { 1600Sstevel@tonic-gate {IIS_UNINITIALIZED, "uninitialized", RESTARTER_STATE_UNINIT, 1610Sstevel@tonic-gate IM_NONE}, 1620Sstevel@tonic-gate {IIS_ONLINE, "online", RESTARTER_STATE_ONLINE, IM_START}, 1630Sstevel@tonic-gate {IIS_IN_ONLINE_METHOD, "online_method", RESTARTER_STATE_OFFLINE, 1640Sstevel@tonic-gate IM_ONLINE}, 1650Sstevel@tonic-gate {IIS_OFFLINE, "offline", RESTARTER_STATE_OFFLINE, IM_NONE}, 1660Sstevel@tonic-gate {IIS_IN_OFFLINE_METHOD, "offline_method", RESTARTER_STATE_OFFLINE, 1670Sstevel@tonic-gate IM_OFFLINE}, 1680Sstevel@tonic-gate {IIS_DISABLED, "disabled", RESTARTER_STATE_DISABLED, IM_NONE}, 1690Sstevel@tonic-gate {IIS_IN_DISABLE_METHOD, "disabled_method", RESTARTER_STATE_OFFLINE, 1700Sstevel@tonic-gate IM_DISABLE}, 1710Sstevel@tonic-gate {IIS_IN_REFRESH_METHOD, "refresh_method", RESTARTER_STATE_ONLINE, 1720Sstevel@tonic-gate IM_REFRESH}, 1730Sstevel@tonic-gate {IIS_MAINTENANCE, "maintenance", RESTARTER_STATE_MAINT, IM_NONE}, 1740Sstevel@tonic-gate {IIS_OFFLINE_CONRATE, "cr_offline", RESTARTER_STATE_OFFLINE, IM_NONE}, 1750Sstevel@tonic-gate {IIS_OFFLINE_BIND, "bind_offline", RESTARTER_STATE_OFFLINE, IM_NONE}, 1760Sstevel@tonic-gate {IIS_OFFLINE_COPIES, "copies_offline", RESTARTER_STATE_OFFLINE, 1770Sstevel@tonic-gate IM_NONE}, 1780Sstevel@tonic-gate {IIS_DEGRADED, "degraded", RESTARTER_STATE_DEGRADED, IM_NONE}, 1790Sstevel@tonic-gate {IIS_NONE, "none", RESTARTER_STATE_NONE, IM_NONE} 1800Sstevel@tonic-gate }; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * Pipe used to send events from the threads created by restarter_bind_handle() 1840Sstevel@tonic-gate * to the main thread of control. 1850Sstevel@tonic-gate */ 1860Sstevel@tonic-gate static int rst_event_pipe[] = {-1, -1}; 1870Sstevel@tonic-gate /* 1880Sstevel@tonic-gate * Used to protect the critical section of code in restarter_event_proxy() that 1890Sstevel@tonic-gate * involves writing an event down the event pipe and reading an acknowledgement. 1900Sstevel@tonic-gate */ 1910Sstevel@tonic-gate static pthread_mutex_t rst_event_pipe_mtx = PTHREAD_MUTEX_INITIALIZER; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate /* handle used in communication with the master restarter */ 1940Sstevel@tonic-gate static restarter_event_handle_t *rst_event_handle = NULL; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* set to indicate a refresh of inetd is requested */ 1970Sstevel@tonic-gate static boolean_t refresh_inetd_requested = B_FALSE; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* set by the SIGTERM handler to flag we got a SIGTERM */ 2000Sstevel@tonic-gate static boolean_t got_sigterm = B_FALSE; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* 2030Sstevel@tonic-gate * Timer queue used to store timers for delayed event processing, such as 2040Sstevel@tonic-gate * bind retries. 2050Sstevel@tonic-gate */ 2060Sstevel@tonic-gate iu_tq_t *timer_queue = NULL; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * fd of Unix Domain socket used to communicate stop and refresh requests 2100Sstevel@tonic-gate * to the inetd start method process. 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate static int uds_fd = -1; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * List of inetd's currently managed instances; each containing its state, 2160Sstevel@tonic-gate * and in certain states its configuration. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate static uu_list_pool_t *instance_pool = NULL; 2190Sstevel@tonic-gate uu_list_t *instance_list = NULL; 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate /* set to indicate we're being stopped */ 2220Sstevel@tonic-gate boolean_t inetd_stopping = B_FALSE; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* TCP wrappers syslog globals. Consumed by libwrap. */ 2250Sstevel@tonic-gate int allow_severity = LOG_INFO; 2260Sstevel@tonic-gate int deny_severity = LOG_WARNING; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* path of the configuration file being monitored by check_conf_file() */ 2290Sstevel@tonic-gate static char *conf_file = NULL; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* Auditing session handle */ 2320Sstevel@tonic-gate static adt_session_data_t *audit_handle; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate static void uds_fini(void); 2350Sstevel@tonic-gate static int uds_init(void); 2360Sstevel@tonic-gate static int run_method(instance_t *, instance_method_t, const proto_info_t *); 2370Sstevel@tonic-gate static void create_bound_fds(instance_t *); 2380Sstevel@tonic-gate static void destroy_bound_fds(instance_t *); 2390Sstevel@tonic-gate static void destroy_instance(instance_t *); 2400Sstevel@tonic-gate static void inetd_stop(void); 241759Sdstaff static void 242759Sdstaff exec_method(instance_t *instance, instance_method_t method, method_info_t *mi, 243759Sdstaff struct method_context *mthd_ctxt, const proto_info_t *pi) __NORETURN; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * The following two functions are callbacks that libumem uses to determine 2470Sstevel@tonic-gate * inetd's desired debugging/logging levels. The interface they consume is 2480Sstevel@tonic-gate * exported by FMA and is consolidation private. The comments in the two 2490Sstevel@tonic-gate * functions give the environment variable that will effectively be set to 2500Sstevel@tonic-gate * their returned value, and thus whose behavior for this value, described in 2510Sstevel@tonic-gate * umem_debug(3MALLOC), will be followed. 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate const char * 2550Sstevel@tonic-gate _umem_debug_init(void) 2560Sstevel@tonic-gate { 2570Sstevel@tonic-gate return ("default,verbose"); /* UMEM_DEBUG setting */ 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate const char * 2610Sstevel@tonic-gate _umem_logging_init(void) 2620Sstevel@tonic-gate { 2630Sstevel@tonic-gate return ("fail,contents"); /* UMEM_LOGGING setting */ 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate static void 2670Sstevel@tonic-gate log_invalid_cfg(const char *fmri) 2680Sstevel@tonic-gate { 2690Sstevel@tonic-gate error_msg(gettext( 2700Sstevel@tonic-gate "Invalid configuration for instance %s, placing in maintenance"), 2710Sstevel@tonic-gate fmri); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate /* 2750Sstevel@tonic-gate * Returns B_TRUE if the instance is in a suitable state for inetd to stop. 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate static boolean_t 2780Sstevel@tonic-gate instance_stopped(const instance_t *inst) 2790Sstevel@tonic-gate { 2800Sstevel@tonic-gate return ((inst->cur_istate == IIS_OFFLINE) || 2810Sstevel@tonic-gate (inst->cur_istate == IIS_MAINTENANCE) || 2820Sstevel@tonic-gate (inst->cur_istate == IIS_DISABLED) || 2830Sstevel@tonic-gate (inst->cur_istate == IIS_UNINITIALIZED)); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* 2870Sstevel@tonic-gate * Updates the current and next repository states of instance 'inst'. If 2880Sstevel@tonic-gate * any errors occur an error message is output. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate static void 2910Sstevel@tonic-gate update_instance_states(instance_t *inst, internal_inst_state_t new_cur_state, 2920Sstevel@tonic-gate internal_inst_state_t new_next_state, restarter_error_t err) 2930Sstevel@tonic-gate { 2940Sstevel@tonic-gate internal_inst_state_t old_cur = inst->cur_istate; 2950Sstevel@tonic-gate internal_inst_state_t old_next = inst->next_istate; 2960Sstevel@tonic-gate scf_error_t sret; 2970Sstevel@tonic-gate int ret; 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate debug_msg("Entering update_instance_states: oldcur: %s, newcur: %s " 3000Sstevel@tonic-gate "oldnext: %s, newnext: %s", states[old_cur].name, 3010Sstevel@tonic-gate states[new_cur_state].name, states[old_next].name, 3020Sstevel@tonic-gate states[new_next_state].name); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* update the repository/cached internal state */ 3060Sstevel@tonic-gate inst->cur_istate = new_cur_state; 3070Sstevel@tonic-gate inst->next_istate = new_next_state; 3080Sstevel@tonic-gate (void) set_single_rep_val(inst->cur_istate_rep, 3090Sstevel@tonic-gate (int64_t)new_cur_state); 3100Sstevel@tonic-gate (void) set_single_rep_val(inst->next_istate_rep, 3110Sstevel@tonic-gate (int64_t)new_next_state); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate if (((sret = store_rep_vals(inst->cur_istate_rep, inst->fmri, 3140Sstevel@tonic-gate PR_NAME_CUR_INT_STATE)) != 0) || 3150Sstevel@tonic-gate ((sret = store_rep_vals(inst->next_istate_rep, inst->fmri, 3160Sstevel@tonic-gate PR_NAME_NEXT_INT_STATE)) != 0)) 3170Sstevel@tonic-gate error_msg(gettext("Failed to update state of instance %s in " 3180Sstevel@tonic-gate "repository: %s"), inst->fmri, scf_strerror(sret)); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* update the repository SMF state */ 3210Sstevel@tonic-gate if ((ret = restarter_set_states(rst_event_handle, inst->fmri, 3220Sstevel@tonic-gate states[old_cur].smf_state, states[new_cur_state].smf_state, 3230Sstevel@tonic-gate states[old_next].smf_state, states[new_next_state].smf_state, 3240Sstevel@tonic-gate err, 0)) != 0) 3250Sstevel@tonic-gate error_msg(gettext("Failed to update state of instance %s in " 3260Sstevel@tonic-gate "repository: %s"), inst->fmri, strerror(ret)); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate void 3310Sstevel@tonic-gate update_state(instance_t *inst, internal_inst_state_t new_cur, 3320Sstevel@tonic-gate restarter_error_t err) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate update_instance_states(inst, new_cur, IIS_NONE, err); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Sends a refresh event to the inetd start method process and returns 3390Sstevel@tonic-gate * SMF_EXIT_OK if it managed to send it. If it fails to send the request for 3400Sstevel@tonic-gate * some reason it returns SMF_EXIT_ERR_OTHER. 3410Sstevel@tonic-gate */ 3420Sstevel@tonic-gate static int 3430Sstevel@tonic-gate refresh_method(void) 3440Sstevel@tonic-gate { 3450Sstevel@tonic-gate uds_request_t req = UR_REFRESH_INETD; 3460Sstevel@tonic-gate int fd; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate debug_msg("Entering refresh_method"); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if ((fd = connect_to_inetd()) < 0) { 3510Sstevel@tonic-gate error_msg(gettext("Failed to connect to inetd: %s"), 3520Sstevel@tonic-gate strerror(errno)); 3530Sstevel@tonic-gate return (SMF_EXIT_ERR_OTHER); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate /* write the request and return success */ 3570Sstevel@tonic-gate if (safe_write(fd, &req, sizeof (req)) == -1) { 3580Sstevel@tonic-gate error_msg( 3590Sstevel@tonic-gate gettext("Failed to send refresh request to inetd: %s"), 3600Sstevel@tonic-gate strerror(errno)); 3610Sstevel@tonic-gate (void) close(fd); 3620Sstevel@tonic-gate return (SMF_EXIT_ERR_OTHER); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate (void) close(fd); 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate return (SMF_EXIT_OK); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Sends a stop event to the inetd start method process and wait till it goes 3720Sstevel@tonic-gate * away. If inetd is determined to have stopped SMF_EXIT_OK is returned, else 3730Sstevel@tonic-gate * SMF_EXIT_ERR_OTHER is returned. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate static int 3760Sstevel@tonic-gate stop_method(void) 3770Sstevel@tonic-gate { 3780Sstevel@tonic-gate uds_request_t req = UR_STOP_INETD; 3790Sstevel@tonic-gate int fd; 3800Sstevel@tonic-gate char c; 3810Sstevel@tonic-gate ssize_t ret; 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate debug_msg("Entering stop_method"); 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate if ((fd = connect_to_inetd()) == -1) { 3860Sstevel@tonic-gate debug_msg(gettext("Failed to connect to inetd: %s"), 3870Sstevel@tonic-gate strerror(errno)); 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * Assume connect_to_inetd() failed because inetd was already 3900Sstevel@tonic-gate * stopped, and return success. 3910Sstevel@tonic-gate */ 3920Sstevel@tonic-gate return (SMF_EXIT_OK); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate /* 3960Sstevel@tonic-gate * This is safe to do since we're fired off in a separate process 3970Sstevel@tonic-gate * than inetd and in the case we get wedged, the stop method timeout 3980Sstevel@tonic-gate * will occur and we'd be killed by our restarter. 3990Sstevel@tonic-gate */ 4000Sstevel@tonic-gate enable_blocking(fd); 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate /* write the stop request to inetd and wait till it goes away */ 4030Sstevel@tonic-gate if (safe_write(fd, &req, sizeof (req)) != 0) { 4040Sstevel@tonic-gate error_msg(gettext("Failed to send stop request to inetd")); 4050Sstevel@tonic-gate (void) close(fd); 4060Sstevel@tonic-gate return (SMF_EXIT_ERR_OTHER); 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate /* wait until remote end of socket is closed */ 4100Sstevel@tonic-gate while (((ret = recv(fd, &c, sizeof (c), 0)) != 0) && (errno == EINTR)) 4110Sstevel@tonic-gate ; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate (void) close(fd); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate if (ret != 0) { 4160Sstevel@tonic-gate error_msg(gettext("Failed to determine whether inetd stopped")); 4170Sstevel@tonic-gate return (SMF_EXIT_ERR_OTHER); 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate return (SMF_EXIT_OK); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate /* 4250Sstevel@tonic-gate * This function is called to handle restarter events coming in from the 4260Sstevel@tonic-gate * master restarter. It is registered with the master restarter via 4270Sstevel@tonic-gate * restarter_bind_handle() and simply passes a pointer to the event down 4280Sstevel@tonic-gate * the event pipe, which will be discovered by the poll in the event loop 4290Sstevel@tonic-gate * and processed there. It waits for an acknowledgement to be written back down 4300Sstevel@tonic-gate * the pipe before returning. 4310Sstevel@tonic-gate * Writing a pointer to the function's 'event' parameter down the pipe will 4320Sstevel@tonic-gate * be safe, as the thread in restarter_event_proxy() doesn't return until 4330Sstevel@tonic-gate * the main thread has finished its processing of the passed event, thus 4340Sstevel@tonic-gate * the referenced event will remain around until the function returns. 4350Sstevel@tonic-gate * To impose the limit of only one event being in the pipe and processed 4360Sstevel@tonic-gate * at once, a lock is taken on entry to this function and returned on exit. 4370Sstevel@tonic-gate * Always returns 0. 4380Sstevel@tonic-gate */ 4390Sstevel@tonic-gate static int 4400Sstevel@tonic-gate restarter_event_proxy(restarter_event_t *event) 4410Sstevel@tonic-gate { 4420Sstevel@tonic-gate restarter_event_type_t ev_type; 4430Sstevel@tonic-gate boolean_t processed; 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate debug_msg("Entering restarter_event_proxy"); 4460Sstevel@tonic-gate ev_type = restarter_event_get_type(event); 4470Sstevel@tonic-gate debug_msg("event: %x, event type: %d", event, ev_type); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate (void) pthread_mutex_lock(&rst_event_pipe_mtx); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* write the event to the main worker thread down the pipe */ 4520Sstevel@tonic-gate if (safe_write(rst_event_pipe[PE_PRODUCER], &event, 4530Sstevel@tonic-gate sizeof (event)) != 0) 4540Sstevel@tonic-gate goto pipe_error; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate /* 4570Sstevel@tonic-gate * Wait for an acknowledgement that the event has been processed from 4580Sstevel@tonic-gate * the same pipe. In the case that inetd is stopping, any thread in 4590Sstevel@tonic-gate * this function will simply block on this read until inetd eventually 4600Sstevel@tonic-gate * exits. This will result in this function not returning success to 4610Sstevel@tonic-gate * its caller, and the event that was being processed when the 4620Sstevel@tonic-gate * function exited will be re-sent when inetd is next started. 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate if (safe_read(rst_event_pipe[PE_PRODUCER], &processed, 4650Sstevel@tonic-gate sizeof (processed)) != 0) 4660Sstevel@tonic-gate goto pipe_error; 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate (void) pthread_mutex_unlock(&rst_event_pipe_mtx); 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate return (processed ? 0 : EAGAIN); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate pipe_error: 4730Sstevel@tonic-gate /* 4740Sstevel@tonic-gate * Something's seriously wrong with the event pipe. Notify the 4750Sstevel@tonic-gate * worker thread by closing this end of the event pipe and pause till 4760Sstevel@tonic-gate * inetd exits. 4770Sstevel@tonic-gate */ 4780Sstevel@tonic-gate error_msg(gettext("Can't process restarter events: %s"), 4790Sstevel@tonic-gate strerror(errno)); 4800Sstevel@tonic-gate (void) close(rst_event_pipe[PE_PRODUCER]); 4810Sstevel@tonic-gate for (;;) 4820Sstevel@tonic-gate (void) pause(); 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate /* NOTREACHED */ 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate /* 4880Sstevel@tonic-gate * Let restarter_event_proxy() know we're finished with the event it's blocked 4890Sstevel@tonic-gate * upon. The 'processed' argument denotes whether we successfully processed the 4900Sstevel@tonic-gate * event. 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate static void 4930Sstevel@tonic-gate ack_restarter_event(boolean_t processed) 4940Sstevel@tonic-gate { 4950Sstevel@tonic-gate debug_msg("Entering ack_restarter_event"); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * If safe_write returns -1 something's seriously wrong with the event 4990Sstevel@tonic-gate * pipe, so start the shutdown proceedings. 5000Sstevel@tonic-gate */ 5010Sstevel@tonic-gate if (safe_write(rst_event_pipe[PE_CONSUMER], &processed, 5020Sstevel@tonic-gate sizeof (processed)) == -1) 5030Sstevel@tonic-gate inetd_stop(); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * Switch the syslog identification string to 'ident'. 5080Sstevel@tonic-gate */ 5090Sstevel@tonic-gate static void 5100Sstevel@tonic-gate change_syslog_ident(const char *ident) 5110Sstevel@tonic-gate { 5120Sstevel@tonic-gate debug_msg("Entering change_syslog_ident: ident: %s", ident); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate closelog(); 5150Sstevel@tonic-gate openlog(ident, LOG_PID|LOG_CONS, LOG_DAEMON); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * Perform TCP wrappers checks on this instance. Due to the fact that the 5200Sstevel@tonic-gate * current wrappers code used in Solaris is taken untouched from the open 5210Sstevel@tonic-gate * source version, we're stuck with using the daemon name for the checks, as 5220Sstevel@tonic-gate * opposed to making use of instance FMRIs. Sigh. 5230Sstevel@tonic-gate * Returns B_TRUE if the check passed, else B_FALSE. 5240Sstevel@tonic-gate */ 5250Sstevel@tonic-gate static boolean_t 5260Sstevel@tonic-gate tcp_wrappers_ok(instance_t *instance) 5270Sstevel@tonic-gate { 5280Sstevel@tonic-gate boolean_t rval = B_TRUE; 5290Sstevel@tonic-gate char *daemon_name; 5300Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 5310Sstevel@tonic-gate struct request_info req; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate debug_msg("Entering tcp_wrappers_ok, instance: %s", instance->fmri); 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * Wrap the service using libwrap functions. The code below implements 5370Sstevel@tonic-gate * the functionality of tcpd. This is done only for stream,nowait 5380Sstevel@tonic-gate * services, following the convention of other vendors. udp/dgram and 5390Sstevel@tonic-gate * stream/wait can NOT be wrapped with this libwrap, so be wary of 5400Sstevel@tonic-gate * changing the test below. 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate if (cfg->do_tcp_wrappers && !cfg->iswait && !cfg->istlx) { 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate daemon_name = instance->config->methods[ 5450Sstevel@tonic-gate IM_START]->exec_args_we.we_wordv[0]; 5460Sstevel@tonic-gate if (*daemon_name == '/') 5470Sstevel@tonic-gate daemon_name = strrchr(daemon_name, '/') + 1; 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate /* 5500Sstevel@tonic-gate * Change the syslog message identity to the name of the 5510Sstevel@tonic-gate * daemon being wrapped, as opposed to "inetd". 5520Sstevel@tonic-gate */ 5530Sstevel@tonic-gate change_syslog_ident(daemon_name); 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate (void) request_init(&req, RQ_DAEMON, daemon_name, RQ_FILE, 5560Sstevel@tonic-gate instance->conn_fd, NULL); 5570Sstevel@tonic-gate fromhost(&req); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate if (strcasecmp(eval_hostname(req.client), paranoid) == 0) { 5600Sstevel@tonic-gate syslog(deny_severity, 5610Sstevel@tonic-gate "refused connect from %s (name/address mismatch)", 5620Sstevel@tonic-gate eval_client(&req)); 5630Sstevel@tonic-gate if (req.sink != NULL) 5640Sstevel@tonic-gate req.sink(instance->conn_fd); 5650Sstevel@tonic-gate rval = B_FALSE; 5660Sstevel@tonic-gate } else if (!hosts_access(&req)) { 5670Sstevel@tonic-gate syslog(deny_severity, 5680Sstevel@tonic-gate "refused connect from %s (access denied)", 5690Sstevel@tonic-gate eval_client(&req)); 5700Sstevel@tonic-gate if (req.sink != NULL) 5710Sstevel@tonic-gate req.sink(instance->conn_fd); 5720Sstevel@tonic-gate rval = B_FALSE; 5730Sstevel@tonic-gate } else { 5740Sstevel@tonic-gate syslog(allow_severity, "connect from %s", 5750Sstevel@tonic-gate eval_client(&req)); 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* Revert syslog identity back to "inetd". */ 5790Sstevel@tonic-gate change_syslog_ident(SYSLOG_IDENT); 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate return (rval); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate /* 5850Sstevel@tonic-gate * Handler registered with the timer queue code to remove an instance from 5860Sstevel@tonic-gate * the connection rate offline state when it has been there for its allotted 5870Sstevel@tonic-gate * time. 5880Sstevel@tonic-gate */ 5890Sstevel@tonic-gate /* ARGSUSED */ 5900Sstevel@tonic-gate static void 5910Sstevel@tonic-gate conn_rate_online(iu_tq_t *tq, void *arg) 5920Sstevel@tonic-gate { 5930Sstevel@tonic-gate instance_t *instance = arg; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate debug_msg("Entering conn_rate_online, instance: %s", 5960Sstevel@tonic-gate instance->fmri); 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate assert(instance->cur_istate == IIS_OFFLINE_CONRATE); 5990Sstevel@tonic-gate instance->timer_id = -1; 6000Sstevel@tonic-gate update_state(instance, IIS_OFFLINE, RERR_RESTART); 6010Sstevel@tonic-gate process_offline_inst(instance); 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate /* 6050Sstevel@tonic-gate * Check whether this instance in the offline state is in transition to 6060Sstevel@tonic-gate * another state and do the work to continue this transition. 6070Sstevel@tonic-gate */ 6080Sstevel@tonic-gate void 6090Sstevel@tonic-gate process_offline_inst(instance_t *inst) 6100Sstevel@tonic-gate { 6110Sstevel@tonic-gate debug_msg("Entering process_offline_inst"); 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate if (inst->disable_req) { 6140Sstevel@tonic-gate inst->disable_req = B_FALSE; 6150Sstevel@tonic-gate (void) run_method(inst, IM_DISABLE, NULL); 6160Sstevel@tonic-gate } else if (inst->maintenance_req) { 6170Sstevel@tonic-gate inst->maintenance_req = B_FALSE; 6180Sstevel@tonic-gate update_state(inst, IIS_MAINTENANCE, RERR_RESTART); 6190Sstevel@tonic-gate /* 6200Sstevel@tonic-gate * If inetd is in the process of stopping, we don't want to enter 6210Sstevel@tonic-gate * any states but offline, disabled and maintenance. 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate } else if (!inetd_stopping) { 6240Sstevel@tonic-gate if (inst->conn_rate_exceeded) { 6250Sstevel@tonic-gate basic_cfg_t *cfg = inst->config->basic; 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate inst->conn_rate_exceeded = B_FALSE; 6280Sstevel@tonic-gate update_state(inst, IIS_OFFLINE_CONRATE, RERR_RESTART); 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Schedule a timer to bring the instance out of the 6310Sstevel@tonic-gate * connection rate offline state. 6320Sstevel@tonic-gate */ 6330Sstevel@tonic-gate inst->timer_id = iu_schedule_timer(timer_queue, 6340Sstevel@tonic-gate cfg->conn_rate_offline, conn_rate_online, 6350Sstevel@tonic-gate inst); 6360Sstevel@tonic-gate if (inst->timer_id == -1) { 6370Sstevel@tonic-gate error_msg(gettext("%s unable to set timer, " 6380Sstevel@tonic-gate "won't be brought on line after %d " 6390Sstevel@tonic-gate "seconds."), inst->fmri, 6400Sstevel@tonic-gate cfg->conn_rate_offline); 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate } else if (copies_limit_exceeded(inst)) { 6440Sstevel@tonic-gate update_state(inst, IIS_OFFLINE_COPIES, RERR_RESTART); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate } 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate /* 6500Sstevel@tonic-gate * Create a socket bound to the instance's configured address. If the 6510Sstevel@tonic-gate * bind fails, returns -1, else the fd of the bound socket. 6520Sstevel@tonic-gate */ 6530Sstevel@tonic-gate static int 6544754Svp157776 create_bound_socket(const instance_t *inst, socket_info_t *sock_info) 6550Sstevel@tonic-gate { 6560Sstevel@tonic-gate int fd; 6570Sstevel@tonic-gate int on = 1; 6584754Svp157776 const char *fmri = inst->fmri; 6590Sstevel@tonic-gate rpc_info_t *rpc = sock_info->pr_info.ri; 6600Sstevel@tonic-gate const char *proto = sock_info->pr_info.proto; 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate debug_msg("Entering create_bound_socket"); 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate fd = socket(sock_info->local_addr.ss_family, sock_info->type, 6650Sstevel@tonic-gate sock_info->protocol); 6660Sstevel@tonic-gate if (fd < 0) { 6670Sstevel@tonic-gate error_msg(gettext( 6680Sstevel@tonic-gate "Socket creation failure for instance %s, proto %s: %s"), 6690Sstevel@tonic-gate fmri, proto, strerror(errno)); 6700Sstevel@tonic-gate return (-1); 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) == -1) { 6740Sstevel@tonic-gate error_msg(gettext("setsockopt SO_REUSEADDR failed for service " 6750Sstevel@tonic-gate "instance %s, proto %s: %s"), fmri, proto, strerror(errno)); 6760Sstevel@tonic-gate (void) close(fd); 6770Sstevel@tonic-gate return (-1); 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate if (sock_info->pr_info.v6only) { 6800Sstevel@tonic-gate /* restrict socket to IPv6 communications only */ 6810Sstevel@tonic-gate if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, 6820Sstevel@tonic-gate sizeof (on)) == -1) { 6830Sstevel@tonic-gate error_msg(gettext("setsockopt IPV6_V6ONLY failed for " 6840Sstevel@tonic-gate "service instance %s, proto %s: %s"), fmri, proto, 6850Sstevel@tonic-gate strerror(errno)); 6860Sstevel@tonic-gate (void) close(fd); 6870Sstevel@tonic-gate return (-1); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate if (rpc != NULL) 6920Sstevel@tonic-gate SS_SETPORT(sock_info->local_addr, 0); 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate if (bind(fd, (struct sockaddr *)&(sock_info->local_addr), 6950Sstevel@tonic-gate SS_ADDRLEN(sock_info->local_addr)) < 0) { 6960Sstevel@tonic-gate error_msg(gettext( 6970Sstevel@tonic-gate "Failed to bind to the port of service instance %s, " 6980Sstevel@tonic-gate "proto %s: %s"), fmri, proto, strerror(errno)); 6990Sstevel@tonic-gate (void) close(fd); 7000Sstevel@tonic-gate return (-1); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate /* 7040Sstevel@tonic-gate * Retrieve and store the address bound to for RPC services. 7050Sstevel@tonic-gate */ 7060Sstevel@tonic-gate if (rpc != NULL) { 7070Sstevel@tonic-gate struct sockaddr_storage ss; 7080Sstevel@tonic-gate int ss_size = sizeof (ss); 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate if (getsockname(fd, (struct sockaddr *)&ss, &ss_size) < 0) { 7110Sstevel@tonic-gate error_msg(gettext("Failed getsockname for instance %s, " 7120Sstevel@tonic-gate "proto %s: %s"), fmri, proto, strerror(errno)); 7130Sstevel@tonic-gate (void) close(fd); 7140Sstevel@tonic-gate return (-1); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate (void) memcpy(rpc->netbuf.buf, &ss, 7170Sstevel@tonic-gate sizeof (struct sockaddr_storage)); 7180Sstevel@tonic-gate rpc->netbuf.len = SS_ADDRLEN(ss); 7190Sstevel@tonic-gate rpc->netbuf.maxlen = SS_ADDRLEN(ss); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7224754Svp157776 if (sock_info->type == SOCK_STREAM) { 7234754Svp157776 int qlen = inst->config->basic->conn_backlog; 7244754Svp157776 7254754Svp157776 debug_msg("Listening for service %s with backlog queue" 7264754Svp157776 " size %d", fmri, qlen); 7274754Svp157776 (void) listen(fd, qlen); 7284754Svp157776 } 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate return (fd); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * Handler registered with the timer queue code to retry the creation 7350Sstevel@tonic-gate * of a bound fd. 7360Sstevel@tonic-gate */ 7370Sstevel@tonic-gate /* ARGSUSED */ 7380Sstevel@tonic-gate static void 7390Sstevel@tonic-gate retry_bind(iu_tq_t *tq, void *arg) 7400Sstevel@tonic-gate { 7410Sstevel@tonic-gate instance_t *instance = arg; 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate debug_msg("Entering retry_bind, instance: %s", instance->fmri); 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate switch (instance->cur_istate) { 7460Sstevel@tonic-gate case IIS_OFFLINE_BIND: 7470Sstevel@tonic-gate case IIS_ONLINE: 7480Sstevel@tonic-gate case IIS_DEGRADED: 7490Sstevel@tonic-gate case IIS_IN_ONLINE_METHOD: 7500Sstevel@tonic-gate case IIS_IN_REFRESH_METHOD: 7510Sstevel@tonic-gate break; 7520Sstevel@tonic-gate default: 7530Sstevel@tonic-gate #ifndef NDEBUG 7540Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n", 7550Sstevel@tonic-gate __FILE__, __LINE__, instance->cur_istate); 7560Sstevel@tonic-gate #endif 7570Sstevel@tonic-gate abort(); 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate instance->bind_timer_id = -1; 7610Sstevel@tonic-gate create_bound_fds(instance); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * For each of the fds for the given instance that are bound, if 'listen' is 7660Sstevel@tonic-gate * set add them to the poll set, else remove them from it. If any additions 7670Sstevel@tonic-gate * fail, returns -1, else 0 on success. 7680Sstevel@tonic-gate */ 7690Sstevel@tonic-gate int 7700Sstevel@tonic-gate poll_bound_fds(instance_t *instance, boolean_t listen) 7710Sstevel@tonic-gate { 7720Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 7730Sstevel@tonic-gate proto_info_t *pi; 7740Sstevel@tonic-gate int ret = 0; 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate debug_msg("Entering poll_bound_fds: instance: %s, on: %d", 7770Sstevel@tonic-gate instance->fmri, listen); 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate for (pi = uu_list_first(cfg->proto_list); pi != NULL; 7800Sstevel@tonic-gate pi = uu_list_next(cfg->proto_list, pi)) { 7810Sstevel@tonic-gate if (pi->listen_fd != -1) { /* fd bound */ 7820Sstevel@tonic-gate if (!listen) { 7830Sstevel@tonic-gate clear_pollfd(pi->listen_fd); 7840Sstevel@tonic-gate } else if (set_pollfd(pi->listen_fd, POLLIN) == -1) { 7850Sstevel@tonic-gate ret = -1; 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate return (ret); 7910Sstevel@tonic-gate } 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate /* 7940Sstevel@tonic-gate * Handle the case were we either fail to create a bound fd or we fail 7950Sstevel@tonic-gate * to add a bound fd to the poll set for the given instance. 7960Sstevel@tonic-gate */ 7970Sstevel@tonic-gate static void 7980Sstevel@tonic-gate handle_bind_failure(instance_t *instance) 7990Sstevel@tonic-gate { 8000Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate debug_msg("Entering handle_bind_failure: instance: %s", instance); 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate /* 8050Sstevel@tonic-gate * We must be being called as a result of a failed poll_bound_fds() 8060Sstevel@tonic-gate * as a bind retry is already scheduled. Just return and let it do 8070Sstevel@tonic-gate * the work. 8080Sstevel@tonic-gate */ 8090Sstevel@tonic-gate if (instance->bind_timer_id != -1) 8100Sstevel@tonic-gate return; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Check if the rebind retries limit is operative and if so, 8140Sstevel@tonic-gate * if it has been reached. 8150Sstevel@tonic-gate */ 8160Sstevel@tonic-gate if (((cfg->bind_fail_interval <= 0) || /* no retries */ 8170Sstevel@tonic-gate ((cfg->bind_fail_max >= 0) && /* limit reached */ 8180Sstevel@tonic-gate (++instance->bind_fail_count > cfg->bind_fail_max))) || 8190Sstevel@tonic-gate ((instance->bind_timer_id = iu_schedule_timer(timer_queue, 8200Sstevel@tonic-gate cfg->bind_fail_interval, retry_bind, instance)) == -1)) { 8210Sstevel@tonic-gate proto_info_t *pi; 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate instance->bind_fail_count = 0; 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate switch (instance->cur_istate) { 8260Sstevel@tonic-gate case IIS_DEGRADED: 8270Sstevel@tonic-gate case IIS_ONLINE: 8280Sstevel@tonic-gate /* check if any of the fds are being poll'd upon */ 8290Sstevel@tonic-gate for (pi = uu_list_first(cfg->proto_list); pi != NULL; 8300Sstevel@tonic-gate pi = uu_list_next(cfg->proto_list, pi)) { 8310Sstevel@tonic-gate if ((pi->listen_fd != -1) && 8320Sstevel@tonic-gate (find_pollfd(pi->listen_fd) != NULL)) 8330Sstevel@tonic-gate break; 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate if (pi != NULL) { /* polling on > 0 fds */ 8360Sstevel@tonic-gate warn_msg(gettext("Failed to bind on " 8370Sstevel@tonic-gate "all protocols for instance %s, " 8380Sstevel@tonic-gate "transitioning to degraded"), 8390Sstevel@tonic-gate instance->fmri); 8400Sstevel@tonic-gate update_state(instance, IIS_DEGRADED, RERR_NONE); 8410Sstevel@tonic-gate instance->bind_retries_exceeded = B_TRUE; 8420Sstevel@tonic-gate break; 8430Sstevel@tonic-gate } 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate destroy_bound_fds(instance); 8460Sstevel@tonic-gate /* 8470Sstevel@tonic-gate * In the case we failed the 'bind' because set_pollfd() 8480Sstevel@tonic-gate * failed on all bound fds, use the offline handling. 8490Sstevel@tonic-gate */ 8500Sstevel@tonic-gate /* FALLTHROUGH */ 8510Sstevel@tonic-gate case IIS_OFFLINE: 8520Sstevel@tonic-gate case IIS_OFFLINE_BIND: 8530Sstevel@tonic-gate error_msg(gettext("Too many bind failures for instance " 8540Sstevel@tonic-gate "%s, transitioning to maintenance"), instance->fmri); 8550Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, 8560Sstevel@tonic-gate RERR_FAULT); 8570Sstevel@tonic-gate break; 8580Sstevel@tonic-gate case IIS_IN_ONLINE_METHOD: 8590Sstevel@tonic-gate case IIS_IN_REFRESH_METHOD: 8600Sstevel@tonic-gate warn_msg(gettext("Failed to bind on all " 8610Sstevel@tonic-gate "protocols for instance %s, instance will go to " 8620Sstevel@tonic-gate "degraded"), instance->fmri); 8630Sstevel@tonic-gate /* 8640Sstevel@tonic-gate * Set the retries exceeded flag so when the method 8650Sstevel@tonic-gate * completes the instance goes to the degraded state. 8660Sstevel@tonic-gate */ 8670Sstevel@tonic-gate instance->bind_retries_exceeded = B_TRUE; 8680Sstevel@tonic-gate break; 8690Sstevel@tonic-gate default: 8700Sstevel@tonic-gate #ifndef NDEBUG 8710Sstevel@tonic-gate (void) fprintf(stderr, 8720Sstevel@tonic-gate "%s:%d: Unknown instance state %d.\n", 8730Sstevel@tonic-gate __FILE__, __LINE__, instance->cur_istate); 8740Sstevel@tonic-gate #endif 8750Sstevel@tonic-gate abort(); 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate } else if (instance->cur_istate == IIS_OFFLINE) { 8780Sstevel@tonic-gate /* 8790Sstevel@tonic-gate * bind re-scheduled, so if we're offline reflect this in the 8800Sstevel@tonic-gate * state. 8810Sstevel@tonic-gate */ 8820Sstevel@tonic-gate update_state(instance, IIS_OFFLINE_BIND, RERR_NONE); 8830Sstevel@tonic-gate } 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate 886759Sdstaff 887759Sdstaff /* 888760Sdstaff * Check if two transport protocols for RPC conflict. 889759Sdstaff */ 890759Sdstaff 891759Sdstaff boolean_t 892759Sdstaff is_rpc_proto_conflict(const char *proto0, const char *proto1) { 893759Sdstaff if (strcmp(proto0, "tcp") == 0) { 894759Sdstaff if (strcmp(proto1, "tcp") == 0) 895759Sdstaff return (B_TRUE); 896759Sdstaff if (strcmp(proto1, "tcp6") == 0) 897759Sdstaff return (B_TRUE); 898759Sdstaff return (B_FALSE); 899759Sdstaff } 900759Sdstaff 901759Sdstaff if (strcmp(proto0, "tcp6") == 0) { 902759Sdstaff if (strcmp(proto1, "tcp") == 0) 903759Sdstaff return (B_TRUE); 904759Sdstaff if (strcmp(proto1, "tcp6only") == 0) 905759Sdstaff return (B_TRUE); 906759Sdstaff if (strcmp(proto1, "tcp6") == 0) 907759Sdstaff return (B_TRUE); 908759Sdstaff return (B_FALSE); 909759Sdstaff } 910759Sdstaff 911759Sdstaff if (strcmp(proto0, "tcp6only") == 0) { 912759Sdstaff if (strcmp(proto1, "tcp6only") == 0) 913759Sdstaff return (B_TRUE); 914759Sdstaff if (strcmp(proto1, "tcp6") == 0) 915759Sdstaff return (B_TRUE); 916759Sdstaff return (B_FALSE); 917759Sdstaff } 918759Sdstaff 919759Sdstaff if (strcmp(proto0, "udp") == 0) { 920759Sdstaff if (strcmp(proto1, "udp") == 0) 921759Sdstaff return (B_TRUE); 922759Sdstaff if (strcmp(proto1, "udp6") == 0) 923759Sdstaff return (B_TRUE); 924759Sdstaff return (B_FALSE); 925759Sdstaff } 926759Sdstaff 927759Sdstaff if (strcmp(proto0, "udp6") == 0) { 928759Sdstaff 929759Sdstaff if (strcmp(proto1, "udp") == 0) 930759Sdstaff return (B_TRUE); 931759Sdstaff if (strcmp(proto1, "udp6only") == 0) 932759Sdstaff return (B_TRUE); 933759Sdstaff if (strcmp(proto1, "udp6") == 0) 934759Sdstaff return (B_TRUE); 935759Sdstaff return (B_FALSE); 936759Sdstaff } 937759Sdstaff 938759Sdstaff if (strcmp(proto0, "udp6only") == 0) { 939759Sdstaff 940759Sdstaff if (strcmp(proto1, "udp6only") == 0) 941759Sdstaff return (B_TRUE); 942759Sdstaff if (strcmp(proto1, "udp6") == 0) 943759Sdstaff return (B_TRUE); 944759Sdstaff return (0); 945759Sdstaff } 946759Sdstaff 947759Sdstaff /* 948759Sdstaff * If the protocol isn't TCP/IP or UDP/IP assume that it has its own 949759Sdstaff * port namepsace and that conflicts can be detected by literal string 950759Sdstaff * comparison. 951759Sdstaff */ 952759Sdstaff 953759Sdstaff if (strcmp(proto0, proto1)) 954759Sdstaff return (FALSE); 955759Sdstaff 956759Sdstaff return (B_TRUE); 957759Sdstaff } 958759Sdstaff 959759Sdstaff 960759Sdstaff /* 961759Sdstaff * Check if inetd thinks this RPC program number is already registered. 962759Sdstaff * 963759Sdstaff * An RPC protocol conflict occurs if 964759Sdstaff * a) the program numbers are the same and, 965759Sdstaff * b) the version numbers overlap, 966759Sdstaff * c) the protocols (TCP vs UDP vs tic*) are the same. 967759Sdstaff */ 968759Sdstaff 969759Sdstaff boolean_t 970759Sdstaff is_rpc_num_in_use(int rpc_n, char *proto, int lowver, int highver) { 971759Sdstaff instance_t *i; 972759Sdstaff basic_cfg_t *cfg; 973759Sdstaff proto_info_t *pi; 974759Sdstaff 975759Sdstaff for (i = uu_list_first(instance_list); i != NULL; 976759Sdstaff i = uu_list_next(instance_list, i)) { 977759Sdstaff 978759Sdstaff if (i->cur_istate != IIS_ONLINE) 979759Sdstaff continue; 980784Sdstaff cfg = i->config->basic; 981759Sdstaff 982759Sdstaff for (pi = uu_list_first(cfg->proto_list); pi != NULL; 983759Sdstaff pi = uu_list_next(cfg->proto_list, pi)) { 984759Sdstaff 985759Sdstaff if (pi->ri == NULL) 986759Sdstaff continue; 987759Sdstaff if (pi->ri->prognum != rpc_n) 988759Sdstaff continue; 989760Sdstaff if (!is_rpc_proto_conflict(pi->proto, proto)) 990759Sdstaff continue; 991759Sdstaff if ((lowver < pi->ri->lowver && 992759Sdstaff highver < pi->ri->lowver) || 993759Sdstaff (lowver > pi->ri->highver && 994759Sdstaff highver > pi->ri->highver)) 995759Sdstaff continue; 996759Sdstaff return (B_TRUE); 997759Sdstaff } 998759Sdstaff } 999759Sdstaff return (B_FALSE); 1000759Sdstaff } 1001759Sdstaff 1002759Sdstaff 10030Sstevel@tonic-gate /* 10040Sstevel@tonic-gate * Independent of the transport, for each of the entries in the instance's 10050Sstevel@tonic-gate * proto list this function first attempts to create an associated network fd; 10060Sstevel@tonic-gate * for RPC services these are then bound to a kernel chosen port and the 10070Sstevel@tonic-gate * fd is registered with rpcbind; for non-RPC services the fds are bound 10080Sstevel@tonic-gate * to the port associated with the instance's service name. On any successful 10090Sstevel@tonic-gate * binds the instance is taken online. Failed binds are handled by 10100Sstevel@tonic-gate * handle_bind_failure(). 10110Sstevel@tonic-gate */ 10120Sstevel@tonic-gate void 10130Sstevel@tonic-gate create_bound_fds(instance_t *instance) 10140Sstevel@tonic-gate { 10150Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 10160Sstevel@tonic-gate boolean_t failure = B_FALSE; 10170Sstevel@tonic-gate boolean_t success = B_FALSE; 10180Sstevel@tonic-gate proto_info_t *pi; 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate debug_msg("Entering create_bound_fd: instance: %s", instance->fmri); 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate /* 10230Sstevel@tonic-gate * Loop through and try and bind any unbound protos. 10240Sstevel@tonic-gate */ 10250Sstevel@tonic-gate for (pi = uu_list_first(cfg->proto_list); pi != NULL; 10260Sstevel@tonic-gate pi = uu_list_next(cfg->proto_list, pi)) { 10270Sstevel@tonic-gate if (pi->listen_fd != -1) 10280Sstevel@tonic-gate continue; 10290Sstevel@tonic-gate if (cfg->istlx) { 10304754Svp157776 pi->listen_fd = create_bound_endpoint(instance, 10310Sstevel@tonic-gate (tlx_info_t *)pi); 10320Sstevel@tonic-gate } else { 10330Sstevel@tonic-gate /* 10340Sstevel@tonic-gate * We cast pi to a void so we can then go on to cast 10350Sstevel@tonic-gate * it to a socket_info_t without lint complaining 10360Sstevel@tonic-gate * about alignment. This is done because the x86 10370Sstevel@tonic-gate * version of lint thinks a lint suppression directive 10380Sstevel@tonic-gate * is unnecessary and flags it as such, yet the sparc 10390Sstevel@tonic-gate * version complains if it's absent. 10400Sstevel@tonic-gate */ 10410Sstevel@tonic-gate void *p = pi; 10424754Svp157776 pi->listen_fd = create_bound_socket(instance, 10430Sstevel@tonic-gate (socket_info_t *)p); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate if (pi->listen_fd == -1) { 10460Sstevel@tonic-gate failure = B_TRUE; 10470Sstevel@tonic-gate continue; 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate if (pi->ri != NULL) { 1051759Sdstaff 1052759Sdstaff /* 1053759Sdstaff * Don't register the same RPC program number twice. 1054759Sdstaff * Doing so silently discards the old service 1055759Sdstaff * without causing an error. 1056759Sdstaff */ 1057759Sdstaff if (is_rpc_num_in_use(pi->ri->prognum, pi->proto, 10584357Srs200217 pi->ri->lowver, pi->ri->highver)) { 1059759Sdstaff failure = B_TRUE; 1060759Sdstaff close_net_fd(instance, pi->listen_fd); 1061759Sdstaff pi->listen_fd = -1; 1062759Sdstaff continue; 1063759Sdstaff } 1064759Sdstaff 10650Sstevel@tonic-gate unregister_rpc_service(instance->fmri, pi->ri); 10660Sstevel@tonic-gate if (register_rpc_service(instance->fmri, pi->ri) == 10670Sstevel@tonic-gate -1) { 10680Sstevel@tonic-gate close_net_fd(instance, pi->listen_fd); 10690Sstevel@tonic-gate pi->listen_fd = -1; 10700Sstevel@tonic-gate failure = B_TRUE; 10710Sstevel@tonic-gate continue; 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate success = B_TRUE; 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate switch (instance->cur_istate) { 10790Sstevel@tonic-gate case IIS_OFFLINE: 10800Sstevel@tonic-gate case IIS_OFFLINE_BIND: 10810Sstevel@tonic-gate /* 10820Sstevel@tonic-gate * If we've managed to bind at least one proto lets run the 10830Sstevel@tonic-gate * online method, so we can start listening for it. 10840Sstevel@tonic-gate */ 10850Sstevel@tonic-gate if (success && run_method(instance, IM_ONLINE, NULL) == -1) 10860Sstevel@tonic-gate return; /* instance gone to maintenance */ 10870Sstevel@tonic-gate break; 10880Sstevel@tonic-gate case IIS_ONLINE: 10890Sstevel@tonic-gate case IIS_IN_REFRESH_METHOD: 10900Sstevel@tonic-gate /* 10910Sstevel@tonic-gate * We're 'online', so start polling on any bound fds we're 10920Sstevel@tonic-gate * currently not. 10930Sstevel@tonic-gate */ 10940Sstevel@tonic-gate if (poll_bound_fds(instance, B_TRUE) != 0) { 10950Sstevel@tonic-gate failure = B_TRUE; 10960Sstevel@tonic-gate } else if (!failure) { 10970Sstevel@tonic-gate /* 10980Sstevel@tonic-gate * We've successfully bound and poll'd upon all protos, 10990Sstevel@tonic-gate * so reset the failure count. 11000Sstevel@tonic-gate */ 11010Sstevel@tonic-gate instance->bind_fail_count = 0; 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate break; 11040Sstevel@tonic-gate case IIS_IN_ONLINE_METHOD: 11050Sstevel@tonic-gate /* 11060Sstevel@tonic-gate * Nothing to do here as the method completion code will start 11070Sstevel@tonic-gate * listening for any successfully bound fds. 11080Sstevel@tonic-gate */ 11090Sstevel@tonic-gate break; 11100Sstevel@tonic-gate default: 11110Sstevel@tonic-gate #ifndef NDEBUG 11120Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n", 11130Sstevel@tonic-gate __FILE__, __LINE__, instance->cur_istate); 11140Sstevel@tonic-gate #endif 11150Sstevel@tonic-gate abort(); 11160Sstevel@tonic-gate } 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate if (failure) 11190Sstevel@tonic-gate handle_bind_failure(instance); 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate /* 11230Sstevel@tonic-gate * Counter to create_bound_fds(), for each of the bound network fds this 11240Sstevel@tonic-gate * function unregisters the instance from rpcbind if it's an RPC service, 11250Sstevel@tonic-gate * stops listening for new connections for it and then closes the listening fd. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate static void 11280Sstevel@tonic-gate destroy_bound_fds(instance_t *instance) 11290Sstevel@tonic-gate { 11300Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 11310Sstevel@tonic-gate proto_info_t *pi; 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate debug_msg("Entering destroy_bound_fds: instance: %s", instance->fmri); 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate for (pi = uu_list_first(cfg->proto_list); pi != NULL; 11360Sstevel@tonic-gate pi = uu_list_next(cfg->proto_list, pi)) { 11370Sstevel@tonic-gate if (pi->listen_fd != -1) { 11380Sstevel@tonic-gate if (pi->ri != NULL) 11390Sstevel@tonic-gate unregister_rpc_service(instance->fmri, pi->ri); 11400Sstevel@tonic-gate clear_pollfd(pi->listen_fd); 11410Sstevel@tonic-gate close_net_fd(instance, pi->listen_fd); 11420Sstevel@tonic-gate pi->listen_fd = -1; 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate } 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate /* cancel any bind retries */ 11470Sstevel@tonic-gate if (instance->bind_timer_id != -1) 11480Sstevel@tonic-gate cancel_bind_timer(instance); 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate instance->bind_retries_exceeded = B_FALSE; 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate /* 11540Sstevel@tonic-gate * Perform %A address expansion and return a pointer to a static string 11550Sstevel@tonic-gate * array containing crafted arguments. This expansion is provided for 11560Sstevel@tonic-gate * compatibility with 4.2BSD daemons, and as such we've copied the logic of 11570Sstevel@tonic-gate * the legacy inetd to maintain this compatibility as much as possible. This 11580Sstevel@tonic-gate * logic is a bit scatty, but it dates back at least as far as SunOS 4.x. 11590Sstevel@tonic-gate */ 11600Sstevel@tonic-gate static char ** 11610Sstevel@tonic-gate expand_address(instance_t *inst, const proto_info_t *pi) 11620Sstevel@tonic-gate { 11630Sstevel@tonic-gate static char addrbuf[sizeof ("ffffffff.65536")]; 11640Sstevel@tonic-gate static char *ret[3]; 11650Sstevel@tonic-gate instance_cfg_t *cfg = inst->config; 11660Sstevel@tonic-gate /* 11670Sstevel@tonic-gate * We cast pi to a void so we can then go on to cast it to a 11680Sstevel@tonic-gate * socket_info_t without lint complaining about alignment. This 11690Sstevel@tonic-gate * is done because the x86 version of lint thinks a lint suppression 11700Sstevel@tonic-gate * directive is unnecessary and flags it as such, yet the sparc 11710Sstevel@tonic-gate * version complains if it's absent. 11720Sstevel@tonic-gate */ 11730Sstevel@tonic-gate const void *p = pi; 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate debug_msg("Entering expand_address"); 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate /* set ret[0] to the basename of exec path */ 11780Sstevel@tonic-gate if ((ret[0] = strrchr(cfg->methods[IM_START]->exec_path, '/')) 11790Sstevel@tonic-gate != NULL) { 11800Sstevel@tonic-gate ret[0]++; 11810Sstevel@tonic-gate } else { 11820Sstevel@tonic-gate ret[0] = cfg->methods[IM_START]->exec_path; 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate if (!cfg->basic->istlx && 11860Sstevel@tonic-gate (((socket_info_t *)p)->type == SOCK_DGRAM)) { 11870Sstevel@tonic-gate ret[1] = NULL; 11880Sstevel@tonic-gate } else { 11890Sstevel@tonic-gate addrbuf[0] = '\0'; 11900Sstevel@tonic-gate if (!cfg->basic->iswait && 11910Sstevel@tonic-gate (inst->remote_addr.ss_family == AF_INET)) { 11920Sstevel@tonic-gate struct sockaddr_in *sp; 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate sp = (struct sockaddr_in *)&(inst->remote_addr); 11950Sstevel@tonic-gate (void) snprintf(addrbuf, sizeof (addrbuf), "%x.%hu", 11960Sstevel@tonic-gate ntohl(sp->sin_addr.s_addr), ntohs(sp->sin_port)); 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate ret[1] = addrbuf; 11990Sstevel@tonic-gate ret[2] = NULL; 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate return (ret); 12030Sstevel@tonic-gate } 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate /* 12060Sstevel@tonic-gate * Returns the state associated with the supplied method being run for an 12070Sstevel@tonic-gate * instance. 12080Sstevel@tonic-gate */ 12090Sstevel@tonic-gate static internal_inst_state_t 12100Sstevel@tonic-gate get_method_state(instance_method_t method) 12110Sstevel@tonic-gate { 12120Sstevel@tonic-gate state_info_t *sip; 12130Sstevel@tonic-gate 12140Sstevel@tonic-gate for (sip = states; sip->istate != IIS_NONE; sip++) { 12150Sstevel@tonic-gate if (sip->method_running == method) 12160Sstevel@tonic-gate break; 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate assert(sip->istate != IIS_NONE); 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate return (sip->istate); 12210Sstevel@tonic-gate } 12220Sstevel@tonic-gate 12230Sstevel@tonic-gate /* 12240Sstevel@tonic-gate * Store the method's PID and CID in the repository. If the store fails 12250Sstevel@tonic-gate * we ignore it and just drive on. 12260Sstevel@tonic-gate */ 12270Sstevel@tonic-gate static void 12280Sstevel@tonic-gate add_method_ids(instance_t *ins, pid_t pid, ctid_t cid, instance_method_t mthd) 12290Sstevel@tonic-gate { 12300Sstevel@tonic-gate debug_msg("Entering add_method_ids"); 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate if (cid != -1) 12333175Sskamm (void) add_remove_contract(ins, B_TRUE, cid); 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate if (mthd == IM_START) { 12360Sstevel@tonic-gate if (add_rep_val(ins->start_pids, (int64_t)pid) == 0) { 12370Sstevel@tonic-gate (void) store_rep_vals(ins->start_pids, ins->fmri, 12380Sstevel@tonic-gate PR_NAME_START_PIDS); 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate } else { 12410Sstevel@tonic-gate if (add_rep_val(ins->non_start_pid, (int64_t)pid) == 0) { 12420Sstevel@tonic-gate (void) store_rep_vals(ins->non_start_pid, ins->fmri, 12430Sstevel@tonic-gate PR_NAME_NON_START_PID); 12440Sstevel@tonic-gate } 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate } 12470Sstevel@tonic-gate 12480Sstevel@tonic-gate /* 12490Sstevel@tonic-gate * Remove the method's PID and CID from the repository. If the removal 12500Sstevel@tonic-gate * fails we ignore it and drive on. 12510Sstevel@tonic-gate */ 12520Sstevel@tonic-gate void 12530Sstevel@tonic-gate remove_method_ids(instance_t *inst, pid_t pid, ctid_t cid, 12540Sstevel@tonic-gate instance_method_t mthd) 12550Sstevel@tonic-gate { 12560Sstevel@tonic-gate debug_msg("Entering remove_method_ids"); 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate if (cid != -1) 12593175Sskamm (void) add_remove_contract(inst, B_FALSE, cid); 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate if (mthd == IM_START) { 12620Sstevel@tonic-gate remove_rep_val(inst->start_pids, (int64_t)pid); 12630Sstevel@tonic-gate (void) store_rep_vals(inst->start_pids, inst->fmri, 12640Sstevel@tonic-gate PR_NAME_START_PIDS); 12650Sstevel@tonic-gate } else { 12660Sstevel@tonic-gate remove_rep_val(inst->non_start_pid, (int64_t)pid); 12670Sstevel@tonic-gate (void) store_rep_vals(inst->non_start_pid, inst->fmri, 12680Sstevel@tonic-gate PR_NAME_NON_START_PID); 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate static instance_t * 12730Sstevel@tonic-gate create_instance(const char *fmri) 12740Sstevel@tonic-gate { 12750Sstevel@tonic-gate instance_t *ret; 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate debug_msg("Entering create_instance, instance: %s", fmri); 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate if (((ret = calloc(1, sizeof (instance_t))) == NULL) || 12800Sstevel@tonic-gate ((ret->fmri = strdup(fmri)) == NULL)) 12810Sstevel@tonic-gate goto alloc_fail; 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate ret->conn_fd = -1; 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate ret->copies = 0; 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate ret->conn_rate_count = 0; 12880Sstevel@tonic-gate ret->fail_rate_count = 0; 12890Sstevel@tonic-gate ret->bind_fail_count = 0; 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate if (((ret->non_start_pid = create_rep_val_list()) == NULL) || 12923175Sskamm ((ret->start_pids = create_rep_val_list()) == NULL) || 12933175Sskamm ((ret->start_ctids = create_rep_val_list()) == NULL)) 12940Sstevel@tonic-gate goto alloc_fail; 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate ret->cur_istate = IIS_NONE; 12970Sstevel@tonic-gate ret->next_istate = IIS_NONE; 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate if (((ret->cur_istate_rep = create_rep_val_list()) == NULL) || 13000Sstevel@tonic-gate ((ret->next_istate_rep = create_rep_val_list()) == NULL)) 13010Sstevel@tonic-gate goto alloc_fail; 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate ret->config = NULL; 13040Sstevel@tonic-gate ret->new_config = NULL; 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate ret->timer_id = -1; 13070Sstevel@tonic-gate ret->bind_timer_id = -1; 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate ret->disable_req = B_FALSE; 13100Sstevel@tonic-gate ret->maintenance_req = B_FALSE; 13110Sstevel@tonic-gate ret->conn_rate_exceeded = B_FALSE; 13120Sstevel@tonic-gate ret->bind_retries_exceeded = B_FALSE; 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate ret->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID; 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate return (ret); 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate alloc_fail: 13190Sstevel@tonic-gate error_msg(strerror(errno)); 13200Sstevel@tonic-gate destroy_instance(ret); 13210Sstevel@tonic-gate return (NULL); 13220Sstevel@tonic-gate } 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate static void 13250Sstevel@tonic-gate destroy_instance(instance_t *inst) 13260Sstevel@tonic-gate { 13270Sstevel@tonic-gate debug_msg("Entering destroy_instance"); 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate if (inst == NULL) 13300Sstevel@tonic-gate return; 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate destroy_instance_cfg(inst->config); 13330Sstevel@tonic-gate destroy_instance_cfg(inst->new_config); 13340Sstevel@tonic-gate 13350Sstevel@tonic-gate destroy_rep_val_list(inst->cur_istate_rep); 13360Sstevel@tonic-gate destroy_rep_val_list(inst->next_istate_rep); 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate destroy_rep_val_list(inst->start_pids); 13390Sstevel@tonic-gate destroy_rep_val_list(inst->non_start_pid); 13403175Sskamm destroy_rep_val_list(inst->start_ctids); 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate free(inst->fmri); 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate free(inst); 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate /* 13480Sstevel@tonic-gate * Retrieves the current and next states internal states. Returns 0 on success, 13490Sstevel@tonic-gate * else returns one of the following on error: 13500Sstevel@tonic-gate * SCF_ERROR_NO_MEMORY if memory allocation failed. 13510Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken. 13520Sstevel@tonic-gate * SCF_ERROR_TYPE_MISMATCH if the property was of an unexpected type. 13530Sstevel@tonic-gate * SCF_ERROR_NO_RESOURCES if the server doesn't have adequate resources. 13540Sstevel@tonic-gate * SCF_ERROR_NO_SERVER if the server isn't running. 13550Sstevel@tonic-gate */ 13560Sstevel@tonic-gate static scf_error_t 13570Sstevel@tonic-gate retrieve_instance_state(instance_t *inst) 13580Sstevel@tonic-gate { 13590Sstevel@tonic-gate scf_error_t ret; 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate debug_msg("Entering retrieve_instance_state: instance: %s", 13620Sstevel@tonic-gate inst->fmri); 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate /* retrieve internal states */ 13650Sstevel@tonic-gate if (((ret = retrieve_rep_vals(inst->cur_istate_rep, inst->fmri, 13660Sstevel@tonic-gate PR_NAME_CUR_INT_STATE)) != 0) || 13670Sstevel@tonic-gate ((ret = retrieve_rep_vals(inst->next_istate_rep, inst->fmri, 13680Sstevel@tonic-gate PR_NAME_NEXT_INT_STATE)) != 0)) { 13690Sstevel@tonic-gate if (ret != SCF_ERROR_NOT_FOUND) { 13700Sstevel@tonic-gate error_msg(gettext( 13710Sstevel@tonic-gate "Failed to read state of instance %s: %s"), 13720Sstevel@tonic-gate inst->fmri, scf_strerror(scf_error())); 13730Sstevel@tonic-gate return (ret); 13740Sstevel@tonic-gate } 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate debug_msg("instance with no previous int state - " 13770Sstevel@tonic-gate "setting state to uninitialized"); 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate if ((set_single_rep_val(inst->cur_istate_rep, 13800Sstevel@tonic-gate (int64_t)IIS_UNINITIALIZED) == -1) || 13810Sstevel@tonic-gate (set_single_rep_val(inst->next_istate_rep, 13820Sstevel@tonic-gate (int64_t)IIS_NONE) == -1)) { 13830Sstevel@tonic-gate return (SCF_ERROR_NO_MEMORY); 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /* update convenience states */ 13880Sstevel@tonic-gate inst->cur_istate = get_single_rep_val(inst->cur_istate_rep); 13890Sstevel@tonic-gate inst->next_istate = get_single_rep_val(inst->next_istate_rep); 13900Sstevel@tonic-gate debug_msg("previous states: cur: %d, next: %d", inst->cur_istate, 13910Sstevel@tonic-gate inst->next_istate); 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate return (0); 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate /* 13970Sstevel@tonic-gate * Retrieve stored process ids and register each of them so we process their 13980Sstevel@tonic-gate * termination. 13990Sstevel@tonic-gate */ 14000Sstevel@tonic-gate static int 14010Sstevel@tonic-gate retrieve_method_pids(instance_t *inst) 14020Sstevel@tonic-gate { 14030Sstevel@tonic-gate rep_val_t *rv; 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate debug_msg("Entering remove_method_pids"); 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate switch (retrieve_rep_vals(inst->start_pids, inst->fmri, 14080Sstevel@tonic-gate PR_NAME_START_PIDS)) { 14090Sstevel@tonic-gate case 0: 14100Sstevel@tonic-gate break; 14110Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 14120Sstevel@tonic-gate return (0); 14130Sstevel@tonic-gate default: 14140Sstevel@tonic-gate error_msg(gettext("Failed to retrieve the start pids of " 14150Sstevel@tonic-gate "instance %s from repository: %s"), inst->fmri, 14160Sstevel@tonic-gate scf_strerror(scf_error())); 14170Sstevel@tonic-gate return (-1); 14180Sstevel@tonic-gate } 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate rv = uu_list_first(inst->start_pids); 14210Sstevel@tonic-gate while (rv != NULL) { 14220Sstevel@tonic-gate if (register_method(inst, (pid_t)rv->val, (ctid_t)-1, 14230Sstevel@tonic-gate IM_START) == 0) { 14240Sstevel@tonic-gate inst->copies++; 14250Sstevel@tonic-gate rv = uu_list_next(inst->start_pids, rv); 14260Sstevel@tonic-gate } else if (errno == ENOENT) { 14270Sstevel@tonic-gate pid_t pid = (pid_t)rv->val; 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate /* 14300Sstevel@tonic-gate * The process must have already terminated. Remove 14310Sstevel@tonic-gate * it from the list. 14320Sstevel@tonic-gate */ 14330Sstevel@tonic-gate rv = uu_list_next(inst->start_pids, rv); 14340Sstevel@tonic-gate remove_rep_val(inst->start_pids, pid); 14350Sstevel@tonic-gate } else { 14360Sstevel@tonic-gate error_msg(gettext("Failed to listen for the completion " 14370Sstevel@tonic-gate "of %s method of instance %s"), START_METHOD_NAME, 14380Sstevel@tonic-gate inst->fmri); 14390Sstevel@tonic-gate rv = uu_list_next(inst->start_pids, rv); 14400Sstevel@tonic-gate } 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate /* synch the repository pid list to remove any terminated pids */ 14440Sstevel@tonic-gate (void) store_rep_vals(inst->start_pids, inst->fmri, PR_NAME_START_PIDS); 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate return (0); 14470Sstevel@tonic-gate } 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate /* 14500Sstevel@tonic-gate * Remove the passed instance from inetd control. 14510Sstevel@tonic-gate */ 14520Sstevel@tonic-gate static void 14530Sstevel@tonic-gate remove_instance(instance_t *instance) 14540Sstevel@tonic-gate { 14550Sstevel@tonic-gate debug_msg("Entering remove_instance"); 14560Sstevel@tonic-gate 14570Sstevel@tonic-gate switch (instance->cur_istate) { 14580Sstevel@tonic-gate case IIS_ONLINE: 14590Sstevel@tonic-gate case IIS_DEGRADED: 14600Sstevel@tonic-gate /* stop listening for network connections */ 14610Sstevel@tonic-gate destroy_bound_fds(instance); 14620Sstevel@tonic-gate break; 14630Sstevel@tonic-gate case IIS_OFFLINE_BIND: 14640Sstevel@tonic-gate cancel_bind_timer(instance); 14650Sstevel@tonic-gate break; 14660Sstevel@tonic-gate case IIS_OFFLINE_CONRATE: 14670Sstevel@tonic-gate cancel_inst_timer(instance); 14680Sstevel@tonic-gate break; 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate /* stop listening for terminated methods */ 14720Sstevel@tonic-gate unregister_instance_methods(instance); 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate uu_list_remove(instance_list, instance); 14750Sstevel@tonic-gate destroy_instance(instance); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate /* 14790Sstevel@tonic-gate * Refresh the configuration of instance 'inst'. This method gets called as 14800Sstevel@tonic-gate * a result of a refresh event for the instance from the master restarter, so 14810Sstevel@tonic-gate * we can rely upon the instance's running snapshot having been updated from 14820Sstevel@tonic-gate * its configuration snapshot. 14830Sstevel@tonic-gate */ 14840Sstevel@tonic-gate void 14850Sstevel@tonic-gate refresh_instance(instance_t *inst) 14860Sstevel@tonic-gate { 14870Sstevel@tonic-gate instance_cfg_t *cfg; 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate debug_msg("Entering refresh_instance: inst: %s", inst->fmri); 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate switch (inst->cur_istate) { 14920Sstevel@tonic-gate case IIS_MAINTENANCE: 14930Sstevel@tonic-gate case IIS_DISABLED: 14940Sstevel@tonic-gate case IIS_UNINITIALIZED: 14950Sstevel@tonic-gate /* 14960Sstevel@tonic-gate * Ignore any possible changes, we'll re-read the configuration 14970Sstevel@tonic-gate * automatically when we exit these states. 14980Sstevel@tonic-gate */ 14990Sstevel@tonic-gate break; 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate case IIS_OFFLINE_COPIES: 15020Sstevel@tonic-gate case IIS_OFFLINE_BIND: 15030Sstevel@tonic-gate case IIS_OFFLINE: 15040Sstevel@tonic-gate case IIS_OFFLINE_CONRATE: 15050Sstevel@tonic-gate destroy_instance_cfg(inst->config); 15060Sstevel@tonic-gate if ((inst->config = read_instance_cfg(inst->fmri)) == NULL) { 15070Sstevel@tonic-gate log_invalid_cfg(inst->fmri); 15080Sstevel@tonic-gate if (inst->cur_istate == IIS_OFFLINE_BIND) { 15090Sstevel@tonic-gate cancel_bind_timer(inst); 15100Sstevel@tonic-gate } else if (inst->cur_istate == IIS_OFFLINE_CONRATE) { 15110Sstevel@tonic-gate cancel_inst_timer(inst); 15120Sstevel@tonic-gate } 15130Sstevel@tonic-gate update_state(inst, IIS_MAINTENANCE, RERR_FAULT); 15140Sstevel@tonic-gate } else { 15150Sstevel@tonic-gate switch (inst->cur_istate) { 15160Sstevel@tonic-gate case IIS_OFFLINE_BIND: 15170Sstevel@tonic-gate if (copies_limit_exceeded(inst)) { 15180Sstevel@tonic-gate /* Cancel scheduled bind retries. */ 15190Sstevel@tonic-gate cancel_bind_timer(inst); 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate /* 15220Sstevel@tonic-gate * Take the instance to the copies 15230Sstevel@tonic-gate * offline state, via the offline 15240Sstevel@tonic-gate * state. 15250Sstevel@tonic-gate */ 15260Sstevel@tonic-gate update_state(inst, IIS_OFFLINE, 15270Sstevel@tonic-gate RERR_RESTART); 15280Sstevel@tonic-gate process_offline_inst(inst); 15290Sstevel@tonic-gate } 15300Sstevel@tonic-gate break; 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate case IIS_OFFLINE: 15330Sstevel@tonic-gate process_offline_inst(inst); 15340Sstevel@tonic-gate break; 15350Sstevel@tonic-gate 15360Sstevel@tonic-gate case IIS_OFFLINE_CONRATE: 15370Sstevel@tonic-gate /* 15380Sstevel@tonic-gate * Since we're already in a DOS state, 15390Sstevel@tonic-gate * don't bother evaluating the copies 15400Sstevel@tonic-gate * limit. This will be evaluated when 15410Sstevel@tonic-gate * we leave this state in 15420Sstevel@tonic-gate * process_offline_inst(). 15430Sstevel@tonic-gate */ 15440Sstevel@tonic-gate break; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate case IIS_OFFLINE_COPIES: 15470Sstevel@tonic-gate /* 15480Sstevel@tonic-gate * Check if the copies limit has been increased 15490Sstevel@tonic-gate * above the current count. 15500Sstevel@tonic-gate */ 15510Sstevel@tonic-gate if (!copies_limit_exceeded(inst)) { 15520Sstevel@tonic-gate update_state(inst, IIS_OFFLINE, 15530Sstevel@tonic-gate RERR_RESTART); 15540Sstevel@tonic-gate process_offline_inst(inst); 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate break; 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate default: 15590Sstevel@tonic-gate assert(0); 15600Sstevel@tonic-gate } 15610Sstevel@tonic-gate } 15620Sstevel@tonic-gate break; 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate case IIS_DEGRADED: 15650Sstevel@tonic-gate case IIS_ONLINE: 15660Sstevel@tonic-gate if ((cfg = read_instance_cfg(inst->fmri)) != NULL) { 15670Sstevel@tonic-gate instance_cfg_t *ocfg = inst->config; 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate /* 15700Sstevel@tonic-gate * Try to avoid the overhead of taking an instance 15710Sstevel@tonic-gate * offline and back on again. We do this by limiting 15720Sstevel@tonic-gate * this behavior to two eventualities: 15730Sstevel@tonic-gate * - there needs to be a re-bind to listen on behalf 15740Sstevel@tonic-gate * of the instance with its new configuration. This 15750Sstevel@tonic-gate * could be because for example its service has been 15760Sstevel@tonic-gate * associated with a different port, or because the 15770Sstevel@tonic-gate * v6only protocol option has been newly applied to 15780Sstevel@tonic-gate * the instance. 15790Sstevel@tonic-gate * - one or both of the start or online methods of the 15800Sstevel@tonic-gate * instance have changed in the new configuration. 15810Sstevel@tonic-gate * Without taking the instance offline when the 15820Sstevel@tonic-gate * start method changed the instance may be running 15830Sstevel@tonic-gate * with unwanted parameters (or event an unwanted 15840Sstevel@tonic-gate * binary); and without taking the instance offline 15850Sstevel@tonic-gate * if its online method was to change, some part of 15860Sstevel@tonic-gate * its running environment may have changed and would 15870Sstevel@tonic-gate * not be picked up until the instance next goes 15880Sstevel@tonic-gate * offline for another reason. 15890Sstevel@tonic-gate */ 15900Sstevel@tonic-gate if ((!bind_config_equal(ocfg->basic, cfg->basic)) || 15910Sstevel@tonic-gate !method_info_equal(ocfg->methods[IM_ONLINE], 15920Sstevel@tonic-gate cfg->methods[IM_ONLINE]) || 15930Sstevel@tonic-gate !method_info_equal(ocfg->methods[IM_START], 15940Sstevel@tonic-gate cfg->methods[IM_START])) { 15950Sstevel@tonic-gate destroy_bound_fds(inst); 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate assert(inst->new_config == NULL); 15980Sstevel@tonic-gate inst->new_config = cfg; 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate (void) run_method(inst, IM_OFFLINE, NULL); 16010Sstevel@tonic-gate } else { /* no bind config / method changes */ 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate /* 16040Sstevel@tonic-gate * swap the proto list over from the old 16050Sstevel@tonic-gate * configuration to the new, so we retain 16060Sstevel@tonic-gate * our set of network fds. 16070Sstevel@tonic-gate */ 16080Sstevel@tonic-gate destroy_proto_list(cfg->basic); 16090Sstevel@tonic-gate cfg->basic->proto_list = 16100Sstevel@tonic-gate ocfg->basic->proto_list; 16110Sstevel@tonic-gate ocfg->basic->proto_list = NULL; 16120Sstevel@tonic-gate destroy_instance_cfg(ocfg); 16130Sstevel@tonic-gate inst->config = cfg; 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate /* re-evaluate copies limits based on new cfg */ 16160Sstevel@tonic-gate if (copies_limit_exceeded(inst)) { 16170Sstevel@tonic-gate destroy_bound_fds(inst); 16180Sstevel@tonic-gate (void) run_method(inst, IM_OFFLINE, 16190Sstevel@tonic-gate NULL); 16200Sstevel@tonic-gate } else { 16210Sstevel@tonic-gate /* 16220Sstevel@tonic-gate * Since the instance isn't being 16230Sstevel@tonic-gate * taken offline, where we assume it 16240Sstevel@tonic-gate * would pick-up any configuration 16250Sstevel@tonic-gate * changes automatically when it goes 16260Sstevel@tonic-gate * back online, run its refresh method 16270Sstevel@tonic-gate * to allow it to pick-up any changes 16280Sstevel@tonic-gate * whilst still online. 16290Sstevel@tonic-gate */ 16300Sstevel@tonic-gate (void) run_method(inst, IM_REFRESH, 16310Sstevel@tonic-gate NULL); 16320Sstevel@tonic-gate } 16330Sstevel@tonic-gate } 16340Sstevel@tonic-gate } else { 16350Sstevel@tonic-gate log_invalid_cfg(inst->fmri); 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate destroy_bound_fds(inst); 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate inst->maintenance_req = B_TRUE; 16400Sstevel@tonic-gate (void) run_method(inst, IM_OFFLINE, NULL); 16410Sstevel@tonic-gate } 16420Sstevel@tonic-gate break; 16430Sstevel@tonic-gate 16440Sstevel@tonic-gate default: 16450Sstevel@tonic-gate debug_msg("Unhandled current state %d for instance in " 16460Sstevel@tonic-gate "refresh_instance", inst->cur_istate); 16470Sstevel@tonic-gate assert(0); 16480Sstevel@tonic-gate } 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate 16510Sstevel@tonic-gate /* 16520Sstevel@tonic-gate * Called by process_restarter_event() to handle a restarter event for an 16530Sstevel@tonic-gate * instance. 16540Sstevel@tonic-gate */ 16550Sstevel@tonic-gate static void 16560Sstevel@tonic-gate handle_restarter_event(instance_t *instance, restarter_event_type_t event, 16570Sstevel@tonic-gate boolean_t send_ack) 16580Sstevel@tonic-gate { 16590Sstevel@tonic-gate debug_msg("Entering handle_restarter_event: inst: %s, event: %d, " 16600Sstevel@tonic-gate "curr state: %d", instance->fmri, event, instance->cur_istate); 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate switch (event) { 16634752Svp157776 case RESTARTER_EVENT_TYPE_ADD_INSTANCE: 16644752Svp157776 /* 16654752Svp157776 * When startd restarts, it sends _ADD_INSTANCE to delegated 16664752Svp157776 * restarters for all those services managed by them. We should 16674752Svp157776 * acknowledge this event, as startd's graph needs to be updated 16684752Svp157776 * about the current state of the service, when startd is 16694752Svp157776 * restarting. 16704752Svp157776 * update_state() is ok to be called here, as commands for 16714752Svp157776 * instances in transition are deferred by 16724752Svp157776 * process_restarter_event(). 16734752Svp157776 */ 16744752Svp157776 update_state(instance, instance->cur_istate, RERR_NONE); 16754752Svp157776 goto done; 16760Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_REFRESH: 16770Sstevel@tonic-gate refresh_instance(instance); 16780Sstevel@tonic-gate goto done; 16794752Svp157776 case RESTARTER_EVENT_TYPE_ADMIN_RESTART: 16804752Svp157776 /* 16814752Svp157776 * We've got a restart event, so if the instance is online 16824752Svp157776 * in any way initiate taking it offline, and rely upon 16834752Svp157776 * our restarter to send us an online event to bring 16844752Svp157776 * it back online. 16854752Svp157776 */ 16864752Svp157776 switch (instance->cur_istate) { 16874752Svp157776 case IIS_ONLINE: 16884752Svp157776 case IIS_DEGRADED: 16894752Svp157776 destroy_bound_fds(instance); 16904752Svp157776 (void) run_method(instance, IM_OFFLINE, NULL); 16914752Svp157776 } 16924752Svp157776 goto done; 16930Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE: 16940Sstevel@tonic-gate remove_instance(instance); 16950Sstevel@tonic-gate goto done; 16960Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_STOP: 16970Sstevel@tonic-gate switch (instance->cur_istate) { 16980Sstevel@tonic-gate case IIS_OFFLINE_CONRATE: 16990Sstevel@tonic-gate case IIS_OFFLINE_BIND: 17000Sstevel@tonic-gate case IIS_OFFLINE_COPIES: 17010Sstevel@tonic-gate /* 17020Sstevel@tonic-gate * inetd must be closing down as we wouldn't get this 17030Sstevel@tonic-gate * event in one of these states from the master 17040Sstevel@tonic-gate * restarter. Take the instance to the offline resting 17050Sstevel@tonic-gate * state. 17060Sstevel@tonic-gate */ 17070Sstevel@tonic-gate if (instance->cur_istate == IIS_OFFLINE_BIND) { 17080Sstevel@tonic-gate cancel_bind_timer(instance); 17090Sstevel@tonic-gate } else if (instance->cur_istate == 17100Sstevel@tonic-gate IIS_OFFLINE_CONRATE) { 17110Sstevel@tonic-gate cancel_inst_timer(instance); 17120Sstevel@tonic-gate } 17130Sstevel@tonic-gate update_state(instance, IIS_OFFLINE, RERR_RESTART); 17140Sstevel@tonic-gate goto done; 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate break; 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate switch (instance->cur_istate) { 17200Sstevel@tonic-gate case IIS_OFFLINE: 17210Sstevel@tonic-gate switch (event) { 17220Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_START: 17230Sstevel@tonic-gate /* 17240Sstevel@tonic-gate * Dependencies are met, let's take the service online. 17250Sstevel@tonic-gate * Only try and bind for a wait type service if 17260Sstevel@tonic-gate * no process is running on its behalf. Otherwise, just 17270Sstevel@tonic-gate * mark the service online and binding will be attempted 17280Sstevel@tonic-gate * when the process exits. 17290Sstevel@tonic-gate */ 17300Sstevel@tonic-gate if (!(instance->config->basic->iswait && 17310Sstevel@tonic-gate (uu_list_first(instance->start_pids) != NULL))) { 17320Sstevel@tonic-gate create_bound_fds(instance); 17330Sstevel@tonic-gate } else { 17340Sstevel@tonic-gate update_state(instance, IIS_ONLINE, RERR_NONE); 17350Sstevel@tonic-gate } 17360Sstevel@tonic-gate break; 17370Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE: 17380Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 17390Sstevel@tonic-gate /* 17400Sstevel@tonic-gate * The instance should be disabled, so run the 17410Sstevel@tonic-gate * instance's disabled method that will do the work 17420Sstevel@tonic-gate * to take it there. 17430Sstevel@tonic-gate */ 17440Sstevel@tonic-gate (void) run_method(instance, IM_DISABLE, NULL); 17450Sstevel@tonic-gate break; 17460Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 17470Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 17480Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 17490Sstevel@tonic-gate /* 17500Sstevel@tonic-gate * The master restarter has requested the instance 17510Sstevel@tonic-gate * go to maintenance; since we're already offline 17520Sstevel@tonic-gate * just update the state to the maintenance state. 17530Sstevel@tonic-gate */ 17540Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 17550Sstevel@tonic-gate break; 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate break; 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate case IIS_OFFLINE_BIND: 17600Sstevel@tonic-gate switch (event) { 17610Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE: 17620Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 17630Sstevel@tonic-gate /* 17640Sstevel@tonic-gate * The instance should be disabled. Firstly, as for 17650Sstevel@tonic-gate * the above dependencies unmet comment, cancel 17660Sstevel@tonic-gate * the bind retry timer and update the state to 17670Sstevel@tonic-gate * offline. Then, run the disable method to do the 17680Sstevel@tonic-gate * work to take the instance from offline to 17690Sstevel@tonic-gate * disabled. 17700Sstevel@tonic-gate */ 17710Sstevel@tonic-gate cancel_bind_timer(instance); 17720Sstevel@tonic-gate update_state(instance, IIS_OFFLINE, RERR_RESTART); 17730Sstevel@tonic-gate (void) run_method(instance, IM_DISABLE, NULL); 17740Sstevel@tonic-gate break; 17750Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 17760Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 17770Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 17780Sstevel@tonic-gate /* 17790Sstevel@tonic-gate * The master restarter has requested the instance 17800Sstevel@tonic-gate * be placed in the maintenance state. Cancel the 17810Sstevel@tonic-gate * outstanding retry timer, and since we're already 17820Sstevel@tonic-gate * offline, update the state to maintenance. 17830Sstevel@tonic-gate */ 17840Sstevel@tonic-gate cancel_bind_timer(instance); 17850Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 17860Sstevel@tonic-gate break; 17870Sstevel@tonic-gate } 17880Sstevel@tonic-gate break; 17890Sstevel@tonic-gate 17900Sstevel@tonic-gate case IIS_DEGRADED: 17910Sstevel@tonic-gate case IIS_ONLINE: 17920Sstevel@tonic-gate switch (event) { 17930Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE: 17940Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 17950Sstevel@tonic-gate /* 17960Sstevel@tonic-gate * The instance needs to be disabled. Do the same work 17970Sstevel@tonic-gate * as for the dependencies unmet event below to 17980Sstevel@tonic-gate * take the instance offline. 17990Sstevel@tonic-gate */ 18000Sstevel@tonic-gate destroy_bound_fds(instance); 18010Sstevel@tonic-gate /* 18020Sstevel@tonic-gate * Indicate that the offline method is being run 18030Sstevel@tonic-gate * as part of going to the disabled state, and to 18040Sstevel@tonic-gate * carry on this transition. 18050Sstevel@tonic-gate */ 18060Sstevel@tonic-gate instance->disable_req = B_TRUE; 18070Sstevel@tonic-gate (void) run_method(instance, IM_OFFLINE, NULL); 18080Sstevel@tonic-gate break; 18090Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 18100Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 18110Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 18120Sstevel@tonic-gate /* 18130Sstevel@tonic-gate * The master restarter has requested the instance be 18140Sstevel@tonic-gate * placed in the maintenance state. This involves 18150Sstevel@tonic-gate * firstly taking the service offline, so do the 18160Sstevel@tonic-gate * same work as for the dependencies unmet event 18170Sstevel@tonic-gate * below. We set the maintenance_req flag to 18180Sstevel@tonic-gate * indicate that when we get to the offline state 18190Sstevel@tonic-gate * we should be placed directly into the maintenance 18200Sstevel@tonic-gate * state. 18210Sstevel@tonic-gate */ 18220Sstevel@tonic-gate instance->maintenance_req = B_TRUE; 18230Sstevel@tonic-gate /* FALLTHROUGH */ 18240Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_STOP: 18250Sstevel@tonic-gate /* 18260Sstevel@tonic-gate * Dependencies have become unmet. Close and 18270Sstevel@tonic-gate * stop listening on the instance's network file 18280Sstevel@tonic-gate * descriptor, and run the offline method to do 18290Sstevel@tonic-gate * any work required to take us to the offline state. 18300Sstevel@tonic-gate */ 18310Sstevel@tonic-gate destroy_bound_fds(instance); 18320Sstevel@tonic-gate (void) run_method(instance, IM_OFFLINE, NULL); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate break; 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate case IIS_UNINITIALIZED: 18370Sstevel@tonic-gate if (event == RESTARTER_EVENT_TYPE_DISABLE || 18380Sstevel@tonic-gate event == RESTARTER_EVENT_TYPE_ADMIN_DISABLE) { 18390Sstevel@tonic-gate update_state(instance, IIS_DISABLED, RERR_NONE); 18400Sstevel@tonic-gate break; 18410Sstevel@tonic-gate } else if (event != RESTARTER_EVENT_TYPE_ENABLE) { 18420Sstevel@tonic-gate /* 18430Sstevel@tonic-gate * Ignore other events until we know whether we're 18440Sstevel@tonic-gate * enabled or not. 18450Sstevel@tonic-gate */ 18460Sstevel@tonic-gate break; 18470Sstevel@tonic-gate } 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate /* 18500Sstevel@tonic-gate * We've got an enabled event; make use of the handling in the 18510Sstevel@tonic-gate * disable case. 18520Sstevel@tonic-gate */ 18530Sstevel@tonic-gate /* FALLTHROUGH */ 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate case IIS_DISABLED: 18560Sstevel@tonic-gate switch (event) { 18570Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ENABLE: 18580Sstevel@tonic-gate /* 18590Sstevel@tonic-gate * The instance needs enabling. Commence reading its 18600Sstevel@tonic-gate * configuration and if successful place the instance 18610Sstevel@tonic-gate * in the offline state and let process_offline_inst() 18620Sstevel@tonic-gate * take it from there. 18630Sstevel@tonic-gate */ 18640Sstevel@tonic-gate destroy_instance_cfg(instance->config); 18650Sstevel@tonic-gate instance->config = read_instance_cfg(instance->fmri); 18660Sstevel@tonic-gate if (instance->config != NULL) { 18670Sstevel@tonic-gate update_state(instance, IIS_OFFLINE, 18680Sstevel@tonic-gate RERR_RESTART); 18690Sstevel@tonic-gate process_offline_inst(instance); 18700Sstevel@tonic-gate } else { 18710Sstevel@tonic-gate log_invalid_cfg(instance->fmri); 18720Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, 18730Sstevel@tonic-gate RERR_RESTART); 18740Sstevel@tonic-gate } 18750Sstevel@tonic-gate 18760Sstevel@tonic-gate break; 18770Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 18780Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 18790Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 18800Sstevel@tonic-gate /* 18810Sstevel@tonic-gate * The master restarter has requested the instance be 18820Sstevel@tonic-gate * placed in the maintenance state, so just update its 18830Sstevel@tonic-gate * state to maintenance. 18840Sstevel@tonic-gate */ 18850Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 18860Sstevel@tonic-gate break; 18870Sstevel@tonic-gate } 18880Sstevel@tonic-gate break; 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate case IIS_MAINTENANCE: 18910Sstevel@tonic-gate switch (event) { 18920Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF: 18930Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 18940Sstevel@tonic-gate /* 18950Sstevel@tonic-gate * The master restarter has requested that the instance 18960Sstevel@tonic-gate * be taken out of maintenance. Read its configuration, 18970Sstevel@tonic-gate * and if successful place the instance in the offline 18980Sstevel@tonic-gate * state and call process_offline_inst() to take it 18990Sstevel@tonic-gate * from there. 19000Sstevel@tonic-gate */ 19010Sstevel@tonic-gate destroy_instance_cfg(instance->config); 19020Sstevel@tonic-gate instance->config = read_instance_cfg(instance->fmri); 19030Sstevel@tonic-gate if (instance->config != NULL) { 19040Sstevel@tonic-gate update_state(instance, IIS_OFFLINE, 19050Sstevel@tonic-gate RERR_RESTART); 19060Sstevel@tonic-gate process_offline_inst(instance); 19070Sstevel@tonic-gate } else { 19080Sstevel@tonic-gate boolean_t enabled; 19090Sstevel@tonic-gate 19100Sstevel@tonic-gate /* 19110Sstevel@tonic-gate * The configuration was invalid. If the 19120Sstevel@tonic-gate * service has disabled requested, let's 19130Sstevel@tonic-gate * just place the instance in disabled even 19140Sstevel@tonic-gate * though we haven't been able to run its 19150Sstevel@tonic-gate * disable method, as the slightly incorrect 19160Sstevel@tonic-gate * state is likely to be less of an issue to 19170Sstevel@tonic-gate * an administrator than refusing to move an 19180Sstevel@tonic-gate * instance to disabled. If disable isn't 19190Sstevel@tonic-gate * requested, re-mark the service's state 19200Sstevel@tonic-gate * as maintenance, so the administrator can 19210Sstevel@tonic-gate * see the request was processed. 19220Sstevel@tonic-gate */ 19230Sstevel@tonic-gate if ((read_enable_merged(instance->fmri, 19240Sstevel@tonic-gate &enabled) == 0) && !enabled) { 19250Sstevel@tonic-gate update_state(instance, IIS_DISABLED, 19260Sstevel@tonic-gate RERR_RESTART); 19270Sstevel@tonic-gate } else { 19280Sstevel@tonic-gate log_invalid_cfg(instance->fmri); 19290Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, 19300Sstevel@tonic-gate RERR_FAULT); 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate } 19330Sstevel@tonic-gate break; 19340Sstevel@tonic-gate } 19350Sstevel@tonic-gate break; 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate case IIS_OFFLINE_CONRATE: 19380Sstevel@tonic-gate switch (event) { 19390Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE: 19400Sstevel@tonic-gate /* 19410Sstevel@tonic-gate * The instance wants disabling. Take the instance 19420Sstevel@tonic-gate * offline as for the dependencies unmet event above, 19430Sstevel@tonic-gate * and then from there run the disable method to do 19440Sstevel@tonic-gate * the work to take the instance to the disabled state. 19450Sstevel@tonic-gate */ 19460Sstevel@tonic-gate cancel_inst_timer(instance); 19470Sstevel@tonic-gate update_state(instance, IIS_OFFLINE, RERR_RESTART); 19480Sstevel@tonic-gate (void) run_method(instance, IM_DISABLE, NULL); 19490Sstevel@tonic-gate break; 19500Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 19510Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 19520Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 19530Sstevel@tonic-gate /* 19540Sstevel@tonic-gate * The master restarter has requested the instance 19550Sstevel@tonic-gate * be taken to maintenance. Cancel the timer setup 19560Sstevel@tonic-gate * when we entered this state, and go directly to 19570Sstevel@tonic-gate * maintenance. 19580Sstevel@tonic-gate */ 19590Sstevel@tonic-gate cancel_inst_timer(instance); 19600Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 19610Sstevel@tonic-gate break; 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate break; 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate case IIS_OFFLINE_COPIES: 19660Sstevel@tonic-gate switch (event) { 19670Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE: 19680Sstevel@tonic-gate /* 19690Sstevel@tonic-gate * The instance wants disabling. Update the state 19700Sstevel@tonic-gate * to offline, and run the disable method to do the 19710Sstevel@tonic-gate * work to take it to the disabled state. 19720Sstevel@tonic-gate */ 19730Sstevel@tonic-gate update_state(instance, IIS_OFFLINE, RERR_RESTART); 19740Sstevel@tonic-gate (void) run_method(instance, IM_DISABLE, NULL); 19750Sstevel@tonic-gate break; 19760Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 19770Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 19780Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 19790Sstevel@tonic-gate /* 19800Sstevel@tonic-gate * The master restarter has requested the instance be 19810Sstevel@tonic-gate * placed in maintenance. Since it's already offline 19820Sstevel@tonic-gate * simply update the state. 19830Sstevel@tonic-gate */ 19840Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, RERR_RESTART); 19850Sstevel@tonic-gate break; 19860Sstevel@tonic-gate } 19870Sstevel@tonic-gate break; 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate default: 19900Sstevel@tonic-gate debug_msg("handle_restarter_event: instance in an " 19910Sstevel@tonic-gate "unexpected state"); 19920Sstevel@tonic-gate assert(0); 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate done: 19960Sstevel@tonic-gate if (send_ack) 19970Sstevel@tonic-gate ack_restarter_event(B_TRUE); 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate /* 20010Sstevel@tonic-gate * Tries to read and process an event from the event pipe. If there isn't one 20020Sstevel@tonic-gate * or an error occurred processing the event it returns -1. Else, if the event 20030Sstevel@tonic-gate * is for an instance we're not already managing we read its state, add it to 20040Sstevel@tonic-gate * our list to manage, and if appropriate read its configuration. Whether it's 20050Sstevel@tonic-gate * new to us or not, we then handle the specific event. 20060Sstevel@tonic-gate * Returns 0 if an event was read and processed successfully, else -1. 20070Sstevel@tonic-gate */ 20080Sstevel@tonic-gate static int 20090Sstevel@tonic-gate process_restarter_event(void) 20100Sstevel@tonic-gate { 20110Sstevel@tonic-gate char *fmri; 20120Sstevel@tonic-gate size_t fmri_size; 20130Sstevel@tonic-gate restarter_event_type_t event_type; 20140Sstevel@tonic-gate instance_t *instance; 20150Sstevel@tonic-gate restarter_event_t *event; 20160Sstevel@tonic-gate ssize_t sz; 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate debug_msg("Entering process_restarter_event"); 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate /* 20210Sstevel@tonic-gate * Try to read an event pointer from the event pipe. 20220Sstevel@tonic-gate */ 20230Sstevel@tonic-gate errno = 0; 20240Sstevel@tonic-gate switch (safe_read(rst_event_pipe[PE_CONSUMER], &event, 20250Sstevel@tonic-gate sizeof (event))) { 20260Sstevel@tonic-gate case 0: 20270Sstevel@tonic-gate break; 20280Sstevel@tonic-gate case 1: 20290Sstevel@tonic-gate if (errno == EAGAIN) /* no event to read */ 20300Sstevel@tonic-gate return (-1); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* other end of pipe closed */ 20330Sstevel@tonic-gate 20340Sstevel@tonic-gate /* FALLTHROUGH */ 20350Sstevel@tonic-gate default: /* unexpected read error */ 20360Sstevel@tonic-gate /* 20370Sstevel@tonic-gate * There's something wrong with the event pipe. Let's 20380Sstevel@tonic-gate * shutdown and be restarted. 20390Sstevel@tonic-gate */ 20400Sstevel@tonic-gate inetd_stop(); 20410Sstevel@tonic-gate return (-1); 20420Sstevel@tonic-gate } 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate /* 20450Sstevel@tonic-gate * Check if we're currently managing the instance which the event 20460Sstevel@tonic-gate * pertains to. If not, read its complete state and add it to our 20470Sstevel@tonic-gate * list to manage. 20480Sstevel@tonic-gate */ 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); 20510Sstevel@tonic-gate if ((fmri = malloc(fmri_size)) == NULL) { 20520Sstevel@tonic-gate error_msg(strerror(errno)); 20530Sstevel@tonic-gate goto fail; 20540Sstevel@tonic-gate } 20550Sstevel@tonic-gate sz = restarter_event_get_instance(event, fmri, fmri_size); 20560Sstevel@tonic-gate if (sz >= fmri_size) 20570Sstevel@tonic-gate assert(0); 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate for (instance = uu_list_first(instance_list); instance != NULL; 20600Sstevel@tonic-gate instance = uu_list_next(instance_list, instance)) { 20610Sstevel@tonic-gate if (strcmp(instance->fmri, fmri) == 0) 20620Sstevel@tonic-gate break; 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate 20650Sstevel@tonic-gate if (instance == NULL) { 20660Sstevel@tonic-gate int err; 20670Sstevel@tonic-gate 20680Sstevel@tonic-gate debug_msg("New instance to manage: %s", fmri); 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate if (((instance = create_instance(fmri)) == NULL) || 20710Sstevel@tonic-gate (retrieve_instance_state(instance) != 0) || 20720Sstevel@tonic-gate (retrieve_method_pids(instance) != 0)) { 20730Sstevel@tonic-gate destroy_instance(instance); 20740Sstevel@tonic-gate free(fmri); 20750Sstevel@tonic-gate goto fail; 20760Sstevel@tonic-gate } 20770Sstevel@tonic-gate 20783175Sskamm if (((err = iterate_repository_contracts(instance, 0)) 20790Sstevel@tonic-gate != 0) && (err != ENOENT)) { 20800Sstevel@tonic-gate error_msg(gettext( 20810Sstevel@tonic-gate "Failed to adopt contracts of instance %s: %s"), 20820Sstevel@tonic-gate instance->fmri, strerror(err)); 20830Sstevel@tonic-gate destroy_instance(instance); 20840Sstevel@tonic-gate free(fmri); 20850Sstevel@tonic-gate goto fail; 20860Sstevel@tonic-gate } 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate uu_list_node_init(instance, &instance->link, instance_pool); 20890Sstevel@tonic-gate (void) uu_list_insert_after(instance_list, NULL, instance); 20900Sstevel@tonic-gate 20910Sstevel@tonic-gate /* 20920Sstevel@tonic-gate * Only read configuration for instances that aren't in any of 20930Sstevel@tonic-gate * the disabled, maintenance or uninitialized states, since 20940Sstevel@tonic-gate * they'll read it on state exit. 20950Sstevel@tonic-gate */ 20960Sstevel@tonic-gate if ((instance->cur_istate != IIS_DISABLED) && 20970Sstevel@tonic-gate (instance->cur_istate != IIS_MAINTENANCE) && 20980Sstevel@tonic-gate (instance->cur_istate != IIS_UNINITIALIZED)) { 20990Sstevel@tonic-gate instance->config = read_instance_cfg(instance->fmri); 21000Sstevel@tonic-gate if (instance->config == NULL) { 21010Sstevel@tonic-gate log_invalid_cfg(instance->fmri); 21020Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, 21030Sstevel@tonic-gate RERR_FAULT); 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate } 21060Sstevel@tonic-gate } 21070Sstevel@tonic-gate 21080Sstevel@tonic-gate free(fmri); 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate event_type = restarter_event_get_type(event); 21110Sstevel@tonic-gate debug_msg("Event type: %d for instance: %s", event_type, 21120Sstevel@tonic-gate instance->fmri); 21130Sstevel@tonic-gate 21140Sstevel@tonic-gate /* 21150Sstevel@tonic-gate * If the instance is currently running a method, don't process the 21160Sstevel@tonic-gate * event now, but attach it to the instance for processing when 21170Sstevel@tonic-gate * the instance finishes its transition. 21180Sstevel@tonic-gate */ 21190Sstevel@tonic-gate if (INST_IN_TRANSITION(instance)) { 21200Sstevel@tonic-gate debug_msg("storing event %d for instance %s", event_type, 21210Sstevel@tonic-gate instance->fmri); 21220Sstevel@tonic-gate instance->pending_rst_event = event_type; 21230Sstevel@tonic-gate } else { 21240Sstevel@tonic-gate handle_restarter_event(instance, event_type, B_TRUE); 21250Sstevel@tonic-gate } 21260Sstevel@tonic-gate 21270Sstevel@tonic-gate return (0); 21280Sstevel@tonic-gate 21290Sstevel@tonic-gate fail: 21300Sstevel@tonic-gate ack_restarter_event(B_FALSE); 21310Sstevel@tonic-gate return (-1); 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate 21340Sstevel@tonic-gate /* 21350Sstevel@tonic-gate * Do the state machine processing associated with the termination of instance 21360Sstevel@tonic-gate * 'inst''s start method. 21370Sstevel@tonic-gate */ 21380Sstevel@tonic-gate void 21390Sstevel@tonic-gate process_start_term(instance_t *inst) 21400Sstevel@tonic-gate { 21410Sstevel@tonic-gate basic_cfg_t *cfg; 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate debug_msg("Entering process_start_term: inst: %s", inst->fmri); 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate inst->copies--; 21460Sstevel@tonic-gate 21470Sstevel@tonic-gate if ((inst->cur_istate == IIS_MAINTENANCE) || 21480Sstevel@tonic-gate (inst->cur_istate == IIS_DISABLED)) { 21490Sstevel@tonic-gate /* do any further processing/checks when we exit these states */ 21500Sstevel@tonic-gate return; 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate 21530Sstevel@tonic-gate cfg = inst->config->basic; 21540Sstevel@tonic-gate 21550Sstevel@tonic-gate if (cfg->iswait) { 21560Sstevel@tonic-gate proto_info_t *pi; 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate switch (inst->cur_istate) { 21590Sstevel@tonic-gate case IIS_ONLINE: 21600Sstevel@tonic-gate case IIS_DEGRADED: 21610Sstevel@tonic-gate case IIS_IN_REFRESH_METHOD: 21620Sstevel@tonic-gate /* 21630Sstevel@tonic-gate * A wait type service's start method has exited. 21640Sstevel@tonic-gate * Check if the method was fired off in this inetd's 21650Sstevel@tonic-gate * lifetime, or a previous one; if the former, 21660Sstevel@tonic-gate * re-commence listening on the service's behalf; if 21670Sstevel@tonic-gate * the latter, mark the service offline and let bind 21680Sstevel@tonic-gate * attempts commence. 21690Sstevel@tonic-gate */ 21700Sstevel@tonic-gate for (pi = uu_list_first(cfg->proto_list); pi != NULL; 21710Sstevel@tonic-gate pi = uu_list_next(cfg->proto_list, pi)) { 21720Sstevel@tonic-gate /* 21730Sstevel@tonic-gate * If a bound fd exists, the method was fired 21740Sstevel@tonic-gate * off during this inetd's lifetime. 21750Sstevel@tonic-gate */ 21760Sstevel@tonic-gate if (pi->listen_fd != -1) 21770Sstevel@tonic-gate break; 21780Sstevel@tonic-gate } 21790Sstevel@tonic-gate if (pi != NULL) { 21800Sstevel@tonic-gate if (poll_bound_fds(inst, B_TRUE) != 0) 21810Sstevel@tonic-gate handle_bind_failure(inst); 21820Sstevel@tonic-gate } else { 21830Sstevel@tonic-gate update_state(inst, IIS_OFFLINE, RERR_RESTART); 21840Sstevel@tonic-gate create_bound_fds(inst); 21850Sstevel@tonic-gate } 21860Sstevel@tonic-gate } 21870Sstevel@tonic-gate } else { 21880Sstevel@tonic-gate /* 21890Sstevel@tonic-gate * Check if a nowait service should be brought back online 21900Sstevel@tonic-gate * after exceeding its copies limit. 21910Sstevel@tonic-gate */ 21920Sstevel@tonic-gate if ((inst->cur_istate == IIS_OFFLINE_COPIES) && 21930Sstevel@tonic-gate !copies_limit_exceeded(inst)) { 21940Sstevel@tonic-gate update_state(inst, IIS_OFFLINE, RERR_NONE); 21950Sstevel@tonic-gate process_offline_inst(inst); 21960Sstevel@tonic-gate } 21970Sstevel@tonic-gate } 21980Sstevel@tonic-gate } 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate /* 22010Sstevel@tonic-gate * If the instance has a pending event process it and initiate the 22020Sstevel@tonic-gate * acknowledgement. 22030Sstevel@tonic-gate */ 22040Sstevel@tonic-gate static void 22050Sstevel@tonic-gate process_pending_rst_event(instance_t *inst) 22060Sstevel@tonic-gate { 22070Sstevel@tonic-gate if (inst->pending_rst_event != RESTARTER_EVENT_TYPE_INVALID) { 22080Sstevel@tonic-gate restarter_event_type_t re; 22090Sstevel@tonic-gate 22100Sstevel@tonic-gate debug_msg("Injecting pending event %d for instance %s", 22110Sstevel@tonic-gate inst->pending_rst_event, inst->fmri); 22120Sstevel@tonic-gate re = inst->pending_rst_event; 22130Sstevel@tonic-gate inst->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID; 22140Sstevel@tonic-gate handle_restarter_event(inst, re, B_TRUE); 22150Sstevel@tonic-gate } 22160Sstevel@tonic-gate } 22170Sstevel@tonic-gate 22180Sstevel@tonic-gate /* 22190Sstevel@tonic-gate * Do the state machine processing associated with the termination 22200Sstevel@tonic-gate * of the specified instance's non-start method with the specified status. 22210Sstevel@tonic-gate * Once the processing of the termination is done, the function also picks up 22220Sstevel@tonic-gate * any processing that was blocked on the method running. 22230Sstevel@tonic-gate */ 22240Sstevel@tonic-gate void 22250Sstevel@tonic-gate process_non_start_term(instance_t *inst, int status) 22260Sstevel@tonic-gate { 22270Sstevel@tonic-gate boolean_t ran_online_method = B_FALSE; 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate debug_msg("Entering process_non_start_term: inst: %s, method: %s", 22300Sstevel@tonic-gate inst->fmri, methods[states[inst->cur_istate].method_running].name); 22310Sstevel@tonic-gate 22320Sstevel@tonic-gate if (status == IMRET_FAILURE) { 22330Sstevel@tonic-gate error_msg(gettext("The %s method of instance %s failed, " 22340Sstevel@tonic-gate "transitioning to maintenance"), 22350Sstevel@tonic-gate methods[states[inst->cur_istate].method_running].name, 22360Sstevel@tonic-gate inst->fmri); 22370Sstevel@tonic-gate 22380Sstevel@tonic-gate if ((inst->cur_istate == IIS_IN_ONLINE_METHOD) || 22390Sstevel@tonic-gate (inst->cur_istate == IIS_IN_REFRESH_METHOD)) 22400Sstevel@tonic-gate destroy_bound_fds(inst); 22410Sstevel@tonic-gate 22420Sstevel@tonic-gate update_state(inst, IIS_MAINTENANCE, RERR_FAULT); 22430Sstevel@tonic-gate 22440Sstevel@tonic-gate inst->maintenance_req = B_FALSE; 22450Sstevel@tonic-gate inst->conn_rate_exceeded = B_FALSE; 22460Sstevel@tonic-gate 22470Sstevel@tonic-gate if (inst->new_config != NULL) { 22480Sstevel@tonic-gate destroy_instance_cfg(inst->new_config); 22490Sstevel@tonic-gate inst->new_config = NULL; 22500Sstevel@tonic-gate } 22510Sstevel@tonic-gate 22520Sstevel@tonic-gate if (!inetd_stopping) 22530Sstevel@tonic-gate process_pending_rst_event(inst); 22540Sstevel@tonic-gate 22550Sstevel@tonic-gate return; 22560Sstevel@tonic-gate } 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate /* non-failure method return */ 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate if (status != IMRET_SUCCESS) { 22610Sstevel@tonic-gate /* 22620Sstevel@tonic-gate * An instance method never returned a supported return code. 22630Sstevel@tonic-gate * We'll assume this means the method succeeded for now whilst 22640Sstevel@tonic-gate * non-GL-cognizant methods are used - eg. pkill. 22650Sstevel@tonic-gate */ 22660Sstevel@tonic-gate debug_msg("The %s method of instance %s returned " 22670Sstevel@tonic-gate "non-compliant exit code: %d, assuming success", 22680Sstevel@tonic-gate methods[states[inst->cur_istate].method_running].name, 22690Sstevel@tonic-gate inst->fmri, status); 22700Sstevel@tonic-gate } 22710Sstevel@tonic-gate 22720Sstevel@tonic-gate /* 22730Sstevel@tonic-gate * Update the state from the in-transition state. 22740Sstevel@tonic-gate */ 22750Sstevel@tonic-gate switch (inst->cur_istate) { 22760Sstevel@tonic-gate case IIS_IN_ONLINE_METHOD: 22770Sstevel@tonic-gate ran_online_method = B_TRUE; 22780Sstevel@tonic-gate /* FALLTHROUGH */ 22790Sstevel@tonic-gate case IIS_IN_REFRESH_METHOD: 22800Sstevel@tonic-gate /* 22810Sstevel@tonic-gate * If we've exhausted the bind retries, flag that by setting 22820Sstevel@tonic-gate * the instance's state to degraded. 22830Sstevel@tonic-gate */ 22840Sstevel@tonic-gate if (inst->bind_retries_exceeded) { 22850Sstevel@tonic-gate update_state(inst, IIS_DEGRADED, RERR_NONE); 22860Sstevel@tonic-gate break; 22870Sstevel@tonic-gate } 22880Sstevel@tonic-gate /* FALLTHROUGH */ 22890Sstevel@tonic-gate default: 22900Sstevel@tonic-gate update_state(inst, 22910Sstevel@tonic-gate methods[states[inst->cur_istate].method_running].dst_state, 22920Sstevel@tonic-gate RERR_NONE); 22930Sstevel@tonic-gate } 22940Sstevel@tonic-gate 22950Sstevel@tonic-gate if (inst->cur_istate == IIS_OFFLINE) { 22960Sstevel@tonic-gate if (inst->new_config != NULL) { 22970Sstevel@tonic-gate /* 22980Sstevel@tonic-gate * This instance was found during refresh to need 22990Sstevel@tonic-gate * taking offline because its newly read configuration 23000Sstevel@tonic-gate * was sufficiently different. Now we're offline, 23010Sstevel@tonic-gate * activate this new configuration. 23020Sstevel@tonic-gate */ 23030Sstevel@tonic-gate destroy_instance_cfg(inst->config); 23040Sstevel@tonic-gate inst->config = inst->new_config; 23050Sstevel@tonic-gate inst->new_config = NULL; 23060Sstevel@tonic-gate } 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate /* continue/complete any transitions that are in progress */ 23090Sstevel@tonic-gate process_offline_inst(inst); 23100Sstevel@tonic-gate 23110Sstevel@tonic-gate } else if (ran_online_method) { 23120Sstevel@tonic-gate /* 23130Sstevel@tonic-gate * We've just successfully executed the online method. We have 23140Sstevel@tonic-gate * a set of bound network fds that were created before running 23150Sstevel@tonic-gate * this method, so now we're online start listening for 23160Sstevel@tonic-gate * connections on them. 23170Sstevel@tonic-gate */ 23180Sstevel@tonic-gate if (poll_bound_fds(inst, B_TRUE) != 0) 23190Sstevel@tonic-gate handle_bind_failure(inst); 23200Sstevel@tonic-gate } 23210Sstevel@tonic-gate 23220Sstevel@tonic-gate /* 23230Sstevel@tonic-gate * If we're now out of transition (process_offline_inst() could have 23240Sstevel@tonic-gate * fired off another method), carry out any jobs that were blocked by 23250Sstevel@tonic-gate * us being in transition. 23260Sstevel@tonic-gate */ 23270Sstevel@tonic-gate if (!INST_IN_TRANSITION(inst)) { 23280Sstevel@tonic-gate if (inetd_stopping) { 23290Sstevel@tonic-gate if (!instance_stopped(inst)) { 23300Sstevel@tonic-gate /* 23310Sstevel@tonic-gate * inetd is stopping, and this instance hasn't 23320Sstevel@tonic-gate * been stopped. Inject a stop event. 23330Sstevel@tonic-gate */ 23340Sstevel@tonic-gate handle_restarter_event(inst, 23350Sstevel@tonic-gate RESTARTER_EVENT_TYPE_STOP, B_FALSE); 23360Sstevel@tonic-gate } 23370Sstevel@tonic-gate } else { 23380Sstevel@tonic-gate process_pending_rst_event(inst); 23390Sstevel@tonic-gate } 23400Sstevel@tonic-gate } 23410Sstevel@tonic-gate } 23420Sstevel@tonic-gate 23430Sstevel@tonic-gate /* 23440Sstevel@tonic-gate * Check if configuration file specified is readable. If not return B_FALSE, 23450Sstevel@tonic-gate * else return B_TRUE. 23460Sstevel@tonic-gate */ 23470Sstevel@tonic-gate static boolean_t 23480Sstevel@tonic-gate can_read_file(const char *path) 23490Sstevel@tonic-gate { 23500Sstevel@tonic-gate int ret; 23510Sstevel@tonic-gate int serrno; 23520Sstevel@tonic-gate 23530Sstevel@tonic-gate debug_msg("Entering can_read_file"); 23540Sstevel@tonic-gate do { 23550Sstevel@tonic-gate ret = access(path, R_OK); 23560Sstevel@tonic-gate } while ((ret < 0) && (errno == EINTR)); 23570Sstevel@tonic-gate if (ret < 0) { 23580Sstevel@tonic-gate if (errno != ENOENT) { 23590Sstevel@tonic-gate serrno = errno; 23600Sstevel@tonic-gate error_msg(gettext("Failed to access configuration " 23610Sstevel@tonic-gate "file %s for performing modification checks: %s"), 23620Sstevel@tonic-gate path, strerror(errno)); 23630Sstevel@tonic-gate errno = serrno; 23640Sstevel@tonic-gate } 23650Sstevel@tonic-gate return (B_FALSE); 23660Sstevel@tonic-gate } 23670Sstevel@tonic-gate return (B_TRUE); 23680Sstevel@tonic-gate } 23690Sstevel@tonic-gate 23700Sstevel@tonic-gate /* 23710Sstevel@tonic-gate * Check whether the configuration file has changed contents since inetd 23720Sstevel@tonic-gate * was last started/refreshed, and if so, log a message indicating that 23730Sstevel@tonic-gate * inetconv needs to be run. 23740Sstevel@tonic-gate */ 23750Sstevel@tonic-gate static void 23760Sstevel@tonic-gate check_conf_file(void) 23770Sstevel@tonic-gate { 23780Sstevel@tonic-gate char *new_hash; 23790Sstevel@tonic-gate char *old_hash = NULL; 23800Sstevel@tonic-gate scf_error_t ret; 23810Sstevel@tonic-gate const char *file; 23820Sstevel@tonic-gate 23830Sstevel@tonic-gate debug_msg("Entering check_conf_file"); 23840Sstevel@tonic-gate 23850Sstevel@tonic-gate if (conf_file == NULL) { 23860Sstevel@tonic-gate /* 23870Sstevel@tonic-gate * No explicit config file specified, so see if one of the 23880Sstevel@tonic-gate * default two are readable, checking the primary one first 23890Sstevel@tonic-gate * followed by the secondary. 23900Sstevel@tonic-gate */ 23910Sstevel@tonic-gate if (can_read_file(PRIMARY_DEFAULT_CONF_FILE)) { 23920Sstevel@tonic-gate file = PRIMARY_DEFAULT_CONF_FILE; 23930Sstevel@tonic-gate } else if ((errno == ENOENT) && 23940Sstevel@tonic-gate can_read_file(SECONDARY_DEFAULT_CONF_FILE)) { 23950Sstevel@tonic-gate file = SECONDARY_DEFAULT_CONF_FILE; 23960Sstevel@tonic-gate } else { 23970Sstevel@tonic-gate return; 23980Sstevel@tonic-gate } 23990Sstevel@tonic-gate } else { 24000Sstevel@tonic-gate file = conf_file; 24010Sstevel@tonic-gate if (!can_read_file(file)) 24020Sstevel@tonic-gate return; 24030Sstevel@tonic-gate } 24040Sstevel@tonic-gate 24050Sstevel@tonic-gate if (calculate_hash(file, &new_hash) == 0) { 24060Sstevel@tonic-gate ret = retrieve_inetd_hash(&old_hash); 24070Sstevel@tonic-gate if (((ret == SCF_ERROR_NONE) && 24080Sstevel@tonic-gate (strcmp(old_hash, new_hash) != 0))) { 24090Sstevel@tonic-gate /* modified config file */ 24100Sstevel@tonic-gate warn_msg(gettext( 24110Sstevel@tonic-gate "Configuration file %s has been modified since " 24120Sstevel@tonic-gate "inetconv was last run. \"inetconv -i %s\" must be " 24130Sstevel@tonic-gate "run to apply any changes to the SMF"), file, file); 24140Sstevel@tonic-gate } else if ((ret != SCF_ERROR_NOT_FOUND) && 24150Sstevel@tonic-gate (ret != SCF_ERROR_NONE)) { 24160Sstevel@tonic-gate /* No message if hash not yet computed */ 24170Sstevel@tonic-gate error_msg(gettext("Failed to check whether " 24180Sstevel@tonic-gate "configuration file %s has been modified: %s"), 24190Sstevel@tonic-gate file, scf_strerror(ret)); 24200Sstevel@tonic-gate } 24210Sstevel@tonic-gate free(old_hash); 24220Sstevel@tonic-gate free(new_hash); 24230Sstevel@tonic-gate } else { 24240Sstevel@tonic-gate error_msg(gettext("Failed to check whether configuration file " 24250Sstevel@tonic-gate "%s has been modified: %s"), file, strerror(errno)); 24260Sstevel@tonic-gate } 24270Sstevel@tonic-gate } 24280Sstevel@tonic-gate 24290Sstevel@tonic-gate /* 24300Sstevel@tonic-gate * Refresh all inetd's managed instances and check the configuration file 24310Sstevel@tonic-gate * for any updates since inetconv was last run, logging a message if there 24320Sstevel@tonic-gate * are. We call the SMF refresh function to refresh each instance so that 24330Sstevel@tonic-gate * the refresh request goes through the framework, and thus results in the 24340Sstevel@tonic-gate * running snapshot of each instance being updated from the configuration 24350Sstevel@tonic-gate * snapshot. 24360Sstevel@tonic-gate */ 24370Sstevel@tonic-gate static void 24380Sstevel@tonic-gate inetd_refresh(void) 24390Sstevel@tonic-gate { 24400Sstevel@tonic-gate instance_t *inst; 24410Sstevel@tonic-gate 24420Sstevel@tonic-gate debug_msg("Entering inetd_refresh"); 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate /* call libscf to send refresh requests for all managed instances */ 24450Sstevel@tonic-gate for (inst = uu_list_first(instance_list); inst != NULL; 24460Sstevel@tonic-gate inst = uu_list_next(instance_list, inst)) { 24470Sstevel@tonic-gate if (smf_refresh_instance(inst->fmri) < 0) { 24480Sstevel@tonic-gate error_msg(gettext("Failed to refresh instance %s: %s"), 24490Sstevel@tonic-gate inst->fmri, scf_strerror(scf_error())); 24500Sstevel@tonic-gate } 24510Sstevel@tonic-gate } 24520Sstevel@tonic-gate 24530Sstevel@tonic-gate /* 24540Sstevel@tonic-gate * Log a message if the configuration file has changed since inetconv 24550Sstevel@tonic-gate * was last run. 24560Sstevel@tonic-gate */ 24570Sstevel@tonic-gate check_conf_file(); 24580Sstevel@tonic-gate } 24590Sstevel@tonic-gate 24600Sstevel@tonic-gate /* 24610Sstevel@tonic-gate * Initiate inetd's shutdown. 24620Sstevel@tonic-gate */ 24630Sstevel@tonic-gate static void 24640Sstevel@tonic-gate inetd_stop(void) 24650Sstevel@tonic-gate { 24660Sstevel@tonic-gate instance_t *inst; 24670Sstevel@tonic-gate 24680Sstevel@tonic-gate debug_msg("Entering inetd_stop"); 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate /* Block handling signals for stop and refresh */ 24710Sstevel@tonic-gate (void) sighold(SIGHUP); 24720Sstevel@tonic-gate (void) sighold(SIGTERM); 24730Sstevel@tonic-gate 24740Sstevel@tonic-gate /* Indicate inetd is coming down */ 24750Sstevel@tonic-gate inetd_stopping = B_TRUE; 24760Sstevel@tonic-gate 24770Sstevel@tonic-gate /* Stop polling on restarter events. */ 24780Sstevel@tonic-gate clear_pollfd(rst_event_pipe[PE_CONSUMER]); 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate /* Stop polling for any more stop/refresh requests. */ 24810Sstevel@tonic-gate clear_pollfd(uds_fd); 24820Sstevel@tonic-gate 24830Sstevel@tonic-gate /* 24840Sstevel@tonic-gate * Send a stop event to all currently unstopped instances that 24850Sstevel@tonic-gate * aren't in transition. For those that are in transition, the 24860Sstevel@tonic-gate * event will get sent when the transition completes. 24870Sstevel@tonic-gate */ 24880Sstevel@tonic-gate for (inst = uu_list_first(instance_list); inst != NULL; 24890Sstevel@tonic-gate inst = uu_list_next(instance_list, inst)) { 24900Sstevel@tonic-gate if (!instance_stopped(inst) && !INST_IN_TRANSITION(inst)) 24910Sstevel@tonic-gate handle_restarter_event(inst, 24920Sstevel@tonic-gate RESTARTER_EVENT_TYPE_STOP, B_FALSE); 24930Sstevel@tonic-gate } 24940Sstevel@tonic-gate } 24950Sstevel@tonic-gate 24960Sstevel@tonic-gate /* 24970Sstevel@tonic-gate * Sets up the intra-inetd-process Unix Domain Socket. 24980Sstevel@tonic-gate * Returns -1 on error, else 0. 24990Sstevel@tonic-gate */ 25000Sstevel@tonic-gate static int 25010Sstevel@tonic-gate uds_init(void) 25020Sstevel@tonic-gate { 25030Sstevel@tonic-gate struct sockaddr_un addr; 25040Sstevel@tonic-gate 25050Sstevel@tonic-gate debug_msg("Entering uds_init"); 25060Sstevel@tonic-gate 25070Sstevel@tonic-gate if ((uds_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { 25080Sstevel@tonic-gate error_msg("socket: %s", strerror(errno)); 25090Sstevel@tonic-gate return (-1); 25100Sstevel@tonic-gate } 25110Sstevel@tonic-gate 25120Sstevel@tonic-gate disable_blocking(uds_fd); 25130Sstevel@tonic-gate 25140Sstevel@tonic-gate (void) unlink(INETD_UDS_PATH); /* clean-up any stale files */ 25150Sstevel@tonic-gate 25160Sstevel@tonic-gate (void) memset(&addr, 0, sizeof (addr)); 25170Sstevel@tonic-gate addr.sun_family = AF_UNIX; 25180Sstevel@tonic-gate /* CONSTCOND */ 25190Sstevel@tonic-gate assert(sizeof (INETD_UDS_PATH) <= sizeof (addr.sun_path)); 25200Sstevel@tonic-gate (void) strlcpy(addr.sun_path, INETD_UDS_PATH, sizeof (addr.sun_path)); 25210Sstevel@tonic-gate 25220Sstevel@tonic-gate if (bind(uds_fd, (struct sockaddr *)(&addr), sizeof (addr)) < 0) { 25230Sstevel@tonic-gate error_msg(gettext("Failed to bind socket to %s: %s"), 25240Sstevel@tonic-gate INETD_UDS_PATH, strerror(errno)); 25250Sstevel@tonic-gate (void) close(uds_fd); 25260Sstevel@tonic-gate return (-1); 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate 25290Sstevel@tonic-gate (void) listen(uds_fd, UDS_BACKLOG); 25300Sstevel@tonic-gate 25310Sstevel@tonic-gate if ((set_pollfd(uds_fd, POLLIN)) == -1) { 25320Sstevel@tonic-gate (void) close(uds_fd); 25330Sstevel@tonic-gate (void) unlink(INETD_UDS_PATH); 25340Sstevel@tonic-gate return (-1); 25350Sstevel@tonic-gate } 25360Sstevel@tonic-gate 25370Sstevel@tonic-gate return (0); 25380Sstevel@tonic-gate } 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate static void 25410Sstevel@tonic-gate uds_fini(void) 25420Sstevel@tonic-gate { 25430Sstevel@tonic-gate if (uds_fd != -1) 25440Sstevel@tonic-gate (void) close(uds_fd); 25450Sstevel@tonic-gate (void) unlink(INETD_UDS_PATH); 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate /* 25490Sstevel@tonic-gate * Handle an incoming request on the Unix Domain Socket. Returns -1 if there 25500Sstevel@tonic-gate * was an error handling the event, else 0. 25510Sstevel@tonic-gate */ 25520Sstevel@tonic-gate static int 25530Sstevel@tonic-gate process_uds_event(void) 25540Sstevel@tonic-gate { 25550Sstevel@tonic-gate uds_request_t req; 25560Sstevel@tonic-gate int fd; 25570Sstevel@tonic-gate struct sockaddr_un addr; 25580Sstevel@tonic-gate socklen_t len = sizeof (addr); 25590Sstevel@tonic-gate int ret; 25600Sstevel@tonic-gate uint_t retries = 0; 25614357Srs200217 ucred_t *ucred = NULL; 25624357Srs200217 uid_t euid; 25630Sstevel@tonic-gate 25640Sstevel@tonic-gate debug_msg("Entering process_uds_event"); 25650Sstevel@tonic-gate 25660Sstevel@tonic-gate do { 25670Sstevel@tonic-gate fd = accept(uds_fd, (struct sockaddr *)&addr, &len); 25680Sstevel@tonic-gate } while ((fd < 0) && (errno == EINTR)); 25690Sstevel@tonic-gate if (fd < 0) { 25700Sstevel@tonic-gate if (errno != EWOULDBLOCK) 25710Sstevel@tonic-gate error_msg("accept failed: %s", strerror(errno)); 25720Sstevel@tonic-gate return (-1); 25730Sstevel@tonic-gate } 25740Sstevel@tonic-gate 25754357Srs200217 if (getpeerucred(fd, &ucred) == -1) { 25764357Srs200217 error_msg("getpeerucred failed: %s", strerror(errno)); 25774357Srs200217 (void) close(fd); 25784357Srs200217 return (-1); 25794357Srs200217 } 25804357Srs200217 25814357Srs200217 /* Check peer credentials before acting on the request */ 25824357Srs200217 euid = ucred_geteuid(ucred); 25834357Srs200217 ucred_free(ucred); 25844357Srs200217 if (euid != 0 && getuid() != euid) { 25854357Srs200217 debug_msg("peer euid %u != uid %u", 25864357Srs200217 (uint_t)euid, (uint_t)getuid()); 25874357Srs200217 (void) close(fd); 25884357Srs200217 return (-1); 25894357Srs200217 } 25904357Srs200217 25910Sstevel@tonic-gate for (retries = 0; retries < UDS_RECV_RETRIES; retries++) { 25920Sstevel@tonic-gate if (((ret = safe_read(fd, &req, sizeof (req))) != 1) || 25930Sstevel@tonic-gate (errno != EAGAIN)) 25940Sstevel@tonic-gate break; 25950Sstevel@tonic-gate 25960Sstevel@tonic-gate (void) poll(NULL, 0, 100); /* 100ms pause */ 25970Sstevel@tonic-gate } 25980Sstevel@tonic-gate 25990Sstevel@tonic-gate if (ret != 0) { 26000Sstevel@tonic-gate error_msg(gettext("Failed read: %s"), strerror(errno)); 26010Sstevel@tonic-gate (void) close(fd); 26020Sstevel@tonic-gate return (-1); 26030Sstevel@tonic-gate } 26040Sstevel@tonic-gate 26050Sstevel@tonic-gate switch (req) { 26060Sstevel@tonic-gate case UR_REFRESH_INETD: 26070Sstevel@tonic-gate /* flag the request for event_loop() to process */ 26080Sstevel@tonic-gate refresh_inetd_requested = B_TRUE; 26090Sstevel@tonic-gate (void) close(fd); 26100Sstevel@tonic-gate break; 26110Sstevel@tonic-gate case UR_STOP_INETD: 26120Sstevel@tonic-gate inetd_stop(); 26130Sstevel@tonic-gate break; 26140Sstevel@tonic-gate default: 26150Sstevel@tonic-gate error_msg("unexpected UDS request"); 26160Sstevel@tonic-gate (void) close(fd); 26170Sstevel@tonic-gate return (-1); 26180Sstevel@tonic-gate } 26190Sstevel@tonic-gate 26200Sstevel@tonic-gate return (0); 26210Sstevel@tonic-gate } 26220Sstevel@tonic-gate 26230Sstevel@tonic-gate /* 26240Sstevel@tonic-gate * Perform checks for common exec string errors. We limit the checks to 26250Sstevel@tonic-gate * whether the file exists, is a regular file, and has at least one execute 26260Sstevel@tonic-gate * bit set. We leave the core security checks to exec() so as not to duplicate 26270Sstevel@tonic-gate * and thus incur the associated drawbacks, but hope to catch the common 26280Sstevel@tonic-gate * errors here. 26290Sstevel@tonic-gate */ 26300Sstevel@tonic-gate static boolean_t 26310Sstevel@tonic-gate passes_basic_exec_checks(const char *instance, const char *method, 26320Sstevel@tonic-gate const char *path) 26330Sstevel@tonic-gate { 26340Sstevel@tonic-gate struct stat sbuf; 26350Sstevel@tonic-gate 26360Sstevel@tonic-gate debug_msg("Entering passes_basic_exec_checks"); 26370Sstevel@tonic-gate 26380Sstevel@tonic-gate /* check the file exists */ 26390Sstevel@tonic-gate while (stat(path, &sbuf) == -1) { 26400Sstevel@tonic-gate if (errno != EINTR) { 26410Sstevel@tonic-gate error_msg(gettext( 26420Sstevel@tonic-gate "Can't stat the %s method of instance %s: %s"), 26430Sstevel@tonic-gate method, instance, strerror(errno)); 26440Sstevel@tonic-gate return (B_FALSE); 26450Sstevel@tonic-gate } 26460Sstevel@tonic-gate } 26470Sstevel@tonic-gate 26480Sstevel@tonic-gate /* 26490Sstevel@tonic-gate * Check if the file is a regular file and has at least one execute 26500Sstevel@tonic-gate * bit set. 26510Sstevel@tonic-gate */ 26520Sstevel@tonic-gate if ((sbuf.st_mode & S_IFMT) != S_IFREG) { 26530Sstevel@tonic-gate error_msg(gettext( 26540Sstevel@tonic-gate "The %s method of instance %s isn't a regular file"), 26550Sstevel@tonic-gate method, instance); 26560Sstevel@tonic-gate return (B_FALSE); 26570Sstevel@tonic-gate } else if ((sbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 26580Sstevel@tonic-gate error_msg(gettext("The %s method instance %s doesn't have " 26590Sstevel@tonic-gate "any execute permissions set"), method, instance); 26600Sstevel@tonic-gate return (B_FALSE); 26610Sstevel@tonic-gate } 26620Sstevel@tonic-gate 26630Sstevel@tonic-gate return (B_TRUE); 26640Sstevel@tonic-gate } 26650Sstevel@tonic-gate 26660Sstevel@tonic-gate static void 26670Sstevel@tonic-gate exec_method(instance_t *instance, instance_method_t method, method_info_t *mi, 26680Sstevel@tonic-gate struct method_context *mthd_ctxt, const proto_info_t *pi) 26690Sstevel@tonic-gate { 26700Sstevel@tonic-gate char **args; 26710Sstevel@tonic-gate char **env; 26720Sstevel@tonic-gate const char *errf; 26730Sstevel@tonic-gate int serrno; 26740Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 26750Sstevel@tonic-gate 26760Sstevel@tonic-gate if (method == IM_START) { 26770Sstevel@tonic-gate /* 26780Sstevel@tonic-gate * If wrappers checks fail, pretend the method was exec'd and 26790Sstevel@tonic-gate * failed. 26800Sstevel@tonic-gate */ 26810Sstevel@tonic-gate if (!tcp_wrappers_ok(instance)) 26820Sstevel@tonic-gate exit(IMRET_FAILURE); 26830Sstevel@tonic-gate } 26840Sstevel@tonic-gate 26850Sstevel@tonic-gate /* 26860Sstevel@tonic-gate * Revert the disposition of handled signals and ignored signals to 26870Sstevel@tonic-gate * their defaults, unblocking any blocked ones as a side effect. 26880Sstevel@tonic-gate */ 26890Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_DFL); 26900Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 26910Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 26920Sstevel@tonic-gate 26930Sstevel@tonic-gate /* 26940Sstevel@tonic-gate * Setup exec arguments. Do this before the fd setup below, so our 26950Sstevel@tonic-gate * logging related file fd doesn't get taken over before we call 26960Sstevel@tonic-gate * expand_address(). 26970Sstevel@tonic-gate */ 26980Sstevel@tonic-gate if ((method == IM_START) && 26990Sstevel@tonic-gate (strcmp(mi->exec_args_we.we_wordv[0], "%A") == 0)) { 27000Sstevel@tonic-gate args = expand_address(instance, pi); 27010Sstevel@tonic-gate } else { 27020Sstevel@tonic-gate args = mi->exec_args_we.we_wordv; 27030Sstevel@tonic-gate } 27040Sstevel@tonic-gate 27050Sstevel@tonic-gate /* Generate audit trail for start operations */ 27060Sstevel@tonic-gate if (method == IM_START) { 27070Sstevel@tonic-gate adt_event_data_t *ae; 27080Sstevel@tonic-gate struct sockaddr_storage ss; 27090Sstevel@tonic-gate priv_set_t *privset; 27100Sstevel@tonic-gate socklen_t sslen = sizeof (ss); 27110Sstevel@tonic-gate 27120Sstevel@tonic-gate if ((ae = adt_alloc_event(audit_handle, ADT_inetd_connect)) 27130Sstevel@tonic-gate == NULL) { 27140Sstevel@tonic-gate error_msg(gettext("Unable to allocate audit event for " 27150Sstevel@tonic-gate "the %s method of instance %s"), 27160Sstevel@tonic-gate methods[method].name, instance->fmri); 27170Sstevel@tonic-gate exit(IMRET_FAILURE); 27180Sstevel@tonic-gate } 27190Sstevel@tonic-gate 27200Sstevel@tonic-gate /* 27210Sstevel@tonic-gate * The inetd_connect audit record consists of: 27220Sstevel@tonic-gate * Service name 27230Sstevel@tonic-gate * Execution path 27240Sstevel@tonic-gate * Remote address and port 27250Sstevel@tonic-gate * Local port 27260Sstevel@tonic-gate * Process privileges 27270Sstevel@tonic-gate */ 27280Sstevel@tonic-gate ae->adt_inetd_connect.service_name = cfg->svc_name; 27290Sstevel@tonic-gate ae->adt_inetd_connect.cmd = mi->exec_path; 27300Sstevel@tonic-gate 27310Sstevel@tonic-gate if (instance->remote_addr.ss_family == AF_INET) { 27320Sstevel@tonic-gate struct in_addr *in = SS_SINADDR(instance->remote_addr); 27330Sstevel@tonic-gate ae->adt_inetd_connect.ip_adr[0] = in->s_addr; 27340Sstevel@tonic-gate ae->adt_inetd_connect.ip_type = ADT_IPv4; 27350Sstevel@tonic-gate } else { 27360Sstevel@tonic-gate uint32_t *addr6; 27370Sstevel@tonic-gate int i; 27380Sstevel@tonic-gate 27390Sstevel@tonic-gate ae->adt_inetd_connect.ip_type = ADT_IPv6; 27400Sstevel@tonic-gate addr6 = (uint32_t *)SS_SINADDR(instance->remote_addr); 27410Sstevel@tonic-gate for (i = 0; i < 4; ++i) 27420Sstevel@tonic-gate ae->adt_inetd_connect.ip_adr[i] = addr6[i]; 27430Sstevel@tonic-gate } 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate ae->adt_inetd_connect.ip_remote_port = 27460Sstevel@tonic-gate ntohs(SS_PORT(instance->remote_addr)); 27470Sstevel@tonic-gate 27480Sstevel@tonic-gate if (getsockname(instance->conn_fd, (struct sockaddr *)&ss, 27490Sstevel@tonic-gate &sslen) == 0) 27500Sstevel@tonic-gate ae->adt_inetd_connect.ip_local_port = 27510Sstevel@tonic-gate ntohs(SS_PORT(ss)); 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate privset = mthd_ctxt->priv_set; 27540Sstevel@tonic-gate if (privset == NULL) { 27550Sstevel@tonic-gate privset = priv_allocset(); 27560Sstevel@tonic-gate if (privset != NULL && 27570Sstevel@tonic-gate getppriv(PRIV_EFFECTIVE, privset) != 0) { 27580Sstevel@tonic-gate priv_freeset(privset); 27590Sstevel@tonic-gate privset = NULL; 27600Sstevel@tonic-gate } 27610Sstevel@tonic-gate } 27620Sstevel@tonic-gate 27630Sstevel@tonic-gate ae->adt_inetd_connect.privileges = privset; 27640Sstevel@tonic-gate 27650Sstevel@tonic-gate (void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS); 27660Sstevel@tonic-gate adt_free_event(ae); 27670Sstevel@tonic-gate 27680Sstevel@tonic-gate if (privset != NULL && mthd_ctxt->priv_set == NULL) 27690Sstevel@tonic-gate priv_freeset(privset); 27700Sstevel@tonic-gate } 27710Sstevel@tonic-gate 27720Sstevel@tonic-gate /* 27730Sstevel@tonic-gate * Set method context before the fd setup below so we can output an 27740Sstevel@tonic-gate * error message if it fails. 27750Sstevel@tonic-gate */ 27760Sstevel@tonic-gate if ((errno = restarter_set_method_context(mthd_ctxt, &errf)) != 0) { 27770Sstevel@tonic-gate const char *msg; 27780Sstevel@tonic-gate 27790Sstevel@tonic-gate if (errno == -1) { 27800Sstevel@tonic-gate if (strcmp(errf, "core_set_process_path") == 0) { 27810Sstevel@tonic-gate msg = gettext("Failed to set the corefile path " 27820Sstevel@tonic-gate "for the %s method of instance %s"); 27830Sstevel@tonic-gate } else if (strcmp(errf, "setproject") == 0) { 27840Sstevel@tonic-gate msg = gettext("Failed to assign a resource " 27850Sstevel@tonic-gate "control for the %s method of instance %s"); 27860Sstevel@tonic-gate } else if (strcmp(errf, "pool_set_binding") == 0) { 27870Sstevel@tonic-gate msg = gettext("Failed to bind the %s method of " 27880Sstevel@tonic-gate "instance %s to a pool due to a system " 27890Sstevel@tonic-gate "error"); 27900Sstevel@tonic-gate } else { 27910Sstevel@tonic-gate assert(0); 27920Sstevel@tonic-gate abort(); 27930Sstevel@tonic-gate } 27940Sstevel@tonic-gate 27950Sstevel@tonic-gate error_msg(msg, methods[method].name, instance->fmri); 27960Sstevel@tonic-gate 27970Sstevel@tonic-gate exit(IMRET_FAILURE); 27980Sstevel@tonic-gate } 27990Sstevel@tonic-gate 28000Sstevel@tonic-gate if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) { 28010Sstevel@tonic-gate switch (errno) { 28020Sstevel@tonic-gate case ENOENT: 28030Sstevel@tonic-gate msg = gettext("Failed to find resource pool " 28040Sstevel@tonic-gate "for the %s method of instance %s"); 28050Sstevel@tonic-gate break; 28060Sstevel@tonic-gate 28070Sstevel@tonic-gate case EBADF: 28080Sstevel@tonic-gate msg = gettext("Failed to bind the %s method of " 28090Sstevel@tonic-gate "instance %s to a pool due to invalid " 28100Sstevel@tonic-gate "configuration"); 28110Sstevel@tonic-gate break; 28120Sstevel@tonic-gate 28131712Srm88369 case EINVAL: 28141712Srm88369 msg = gettext("Failed to bind the %s method of " 28151712Srm88369 "instance %s to a pool due to invalid " 28161712Srm88369 "pool name"); 28171712Srm88369 break; 28181712Srm88369 28190Sstevel@tonic-gate default: 28200Sstevel@tonic-gate assert(0); 28210Sstevel@tonic-gate abort(); 28220Sstevel@tonic-gate } 28230Sstevel@tonic-gate 28240Sstevel@tonic-gate exit(IMRET_FAILURE); 28250Sstevel@tonic-gate } 28260Sstevel@tonic-gate 28270Sstevel@tonic-gate if (errf != NULL) { 28280Sstevel@tonic-gate error_msg(gettext("Failed to set credentials for the " 28290Sstevel@tonic-gate "%s method of instance %s (%s: %s)"), 28300Sstevel@tonic-gate methods[method].name, instance->fmri, errf, 28310Sstevel@tonic-gate strerror(errno)); 28320Sstevel@tonic-gate exit(IMRET_FAILURE); 28330Sstevel@tonic-gate } 28340Sstevel@tonic-gate 28350Sstevel@tonic-gate switch (errno) { 28360Sstevel@tonic-gate case ENOMEM: 28370Sstevel@tonic-gate msg = gettext("Failed to set credentials for the %s " 28380Sstevel@tonic-gate "method of instance %s (out of memory)"); 28390Sstevel@tonic-gate break; 28400Sstevel@tonic-gate 28410Sstevel@tonic-gate case ENOENT: 28420Sstevel@tonic-gate msg = gettext("Failed to set credentials for the %s " 28430Sstevel@tonic-gate "method of instance %s (no passwd or shadow " 28440Sstevel@tonic-gate "entry for user)"); 28450Sstevel@tonic-gate break; 28460Sstevel@tonic-gate 28470Sstevel@tonic-gate default: 28480Sstevel@tonic-gate assert(0); 28490Sstevel@tonic-gate abort(); 28500Sstevel@tonic-gate } 28510Sstevel@tonic-gate 28520Sstevel@tonic-gate error_msg(msg, methods[method].name, instance->fmri); 28530Sstevel@tonic-gate exit(IMRET_FAILURE); 28540Sstevel@tonic-gate } 28550Sstevel@tonic-gate 28560Sstevel@tonic-gate /* let exec() free mthd_ctxt */ 28570Sstevel@tonic-gate 28580Sstevel@tonic-gate /* setup standard fds */ 28590Sstevel@tonic-gate if (method == IM_START) { 28600Sstevel@tonic-gate (void) dup2(instance->conn_fd, STDIN_FILENO); 28610Sstevel@tonic-gate } else { 28620Sstevel@tonic-gate (void) close(STDIN_FILENO); 28630Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 28640Sstevel@tonic-gate } 28650Sstevel@tonic-gate (void) dup2(STDIN_FILENO, STDOUT_FILENO); 28660Sstevel@tonic-gate (void) dup2(STDIN_FILENO, STDERR_FILENO); 28670Sstevel@tonic-gate 28680Sstevel@tonic-gate closefrom(STDERR_FILENO + 1); 28690Sstevel@tonic-gate 28700Sstevel@tonic-gate method_preexec(); 28710Sstevel@tonic-gate 28720Sstevel@tonic-gate env = set_smf_env(mthd_ctxt, instance, methods[method].name); 28730Sstevel@tonic-gate 28740Sstevel@tonic-gate if (env != NULL) { 28750Sstevel@tonic-gate do { 28760Sstevel@tonic-gate (void) execve(mi->exec_path, args, env); 28770Sstevel@tonic-gate } while (errno == EINTR); 28780Sstevel@tonic-gate } 28790Sstevel@tonic-gate 28800Sstevel@tonic-gate serrno = errno; 28810Sstevel@tonic-gate /* start up logging again to report the error */ 28820Sstevel@tonic-gate msg_init(); 28830Sstevel@tonic-gate errno = serrno; 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate error_msg( 28860Sstevel@tonic-gate gettext("Failed to exec %s method of instance %s: %s"), 28870Sstevel@tonic-gate methods[method].name, instance->fmri, strerror(errno)); 28880Sstevel@tonic-gate 28890Sstevel@tonic-gate if ((method == IM_START) && (instance->config->basic->iswait)) { 28900Sstevel@tonic-gate /* 28910Sstevel@tonic-gate * We couldn't exec the start method for a wait type service. 28920Sstevel@tonic-gate * Eat up data from the endpoint, so that hopefully the 28930Sstevel@tonic-gate * service's fd won't wake poll up on the next time round 28940Sstevel@tonic-gate * event_loop(). This behavior is carried over from the old 28950Sstevel@tonic-gate * inetd, and it seems somewhat arbitrary that it isn't 28960Sstevel@tonic-gate * also done in the case of fork failures; but I guess 28970Sstevel@tonic-gate * it assumes an exec failure is less likely to be the result 28980Sstevel@tonic-gate * of a resource shortage, and is thus not worth retrying. 28990Sstevel@tonic-gate */ 29000Sstevel@tonic-gate consume_wait_data(instance, 0); 29010Sstevel@tonic-gate } 29020Sstevel@tonic-gate 29030Sstevel@tonic-gate exit(IMRET_FAILURE); 29040Sstevel@tonic-gate } 29050Sstevel@tonic-gate 29060Sstevel@tonic-gate static restarter_error_t 29070Sstevel@tonic-gate get_method_error_success(instance_method_t method) 29080Sstevel@tonic-gate { 29090Sstevel@tonic-gate switch (method) { 29100Sstevel@tonic-gate case IM_OFFLINE: 29110Sstevel@tonic-gate return (RERR_RESTART); 29120Sstevel@tonic-gate case IM_ONLINE: 29130Sstevel@tonic-gate return (RERR_RESTART); 29140Sstevel@tonic-gate case IM_DISABLE: 29150Sstevel@tonic-gate return (RERR_RESTART); 29160Sstevel@tonic-gate case IM_REFRESH: 29170Sstevel@tonic-gate return (RERR_REFRESH); 29180Sstevel@tonic-gate case IM_START: 29190Sstevel@tonic-gate return (RERR_RESTART); 29200Sstevel@tonic-gate } 2921759Sdstaff (void) fprintf(stderr, gettext("Internal fatal error in inetd.\n")); 2922759Sdstaff 2923759Sdstaff abort(); 29240Sstevel@tonic-gate /* NOTREACHED */ 29250Sstevel@tonic-gate } 29260Sstevel@tonic-gate 29273837Sstevep static int 29283837Sstevep smf_kill_process(instance_t *instance, int sig) 29293837Sstevep { 29303837Sstevep rep_val_t *rv; 29313837Sstevep int ret = IMRET_SUCCESS; 29323837Sstevep 29333837Sstevep /* Carry out process assassination */ 29343837Sstevep for (rv = uu_list_first(instance->start_pids); 29353837Sstevep rv != NULL; 29363837Sstevep rv = uu_list_next(instance->start_pids, rv)) { 29373837Sstevep if ((kill((pid_t)rv->val, sig) != 0) && 29383837Sstevep (errno != ESRCH)) { 29393837Sstevep ret = IMRET_FAILURE; 29403837Sstevep error_msg(gettext("Unable to kill " 29414357Srs200217 "start process (%ld) of instance %s: %s"), 29424357Srs200217 rv->val, instance->fmri, strerror(errno)); 29433837Sstevep } 29443837Sstevep } 29453837Sstevep return (ret); 29463837Sstevep } 29473837Sstevep 29480Sstevel@tonic-gate /* 29490Sstevel@tonic-gate * Runs the specified method of the specified service instance. 29500Sstevel@tonic-gate * If the method was never specified, we handle it the same as if the 29510Sstevel@tonic-gate * method was called and returned success, carrying on any transition the 29520Sstevel@tonic-gate * instance may be in the midst of. 29530Sstevel@tonic-gate * If the method isn't executable in its specified profile or an error occurs 29540Sstevel@tonic-gate * forking a process to run the method in the function returns -1. 29550Sstevel@tonic-gate * If a method binary is successfully executed, the function switches the 29560Sstevel@tonic-gate * instance's cur state to the method's associated 'run' state and the next 29570Sstevel@tonic-gate * state to the methods associated next state. 29580Sstevel@tonic-gate * Returns -1 if there's an error before forking, else 0. 29590Sstevel@tonic-gate */ 29600Sstevel@tonic-gate int 29610Sstevel@tonic-gate run_method(instance_t *instance, instance_method_t method, 29620Sstevel@tonic-gate const proto_info_t *start_info) 29630Sstevel@tonic-gate { 29640Sstevel@tonic-gate pid_t child_pid; 29650Sstevel@tonic-gate method_info_t *mi; 29660Sstevel@tonic-gate struct method_context *mthd_ctxt = NULL; 29670Sstevel@tonic-gate const char *errstr; 29683837Sstevep int sig = 0; 29690Sstevel@tonic-gate int ret; 29700Sstevel@tonic-gate instance_cfg_t *cfg = instance->config; 29710Sstevel@tonic-gate ctid_t cid; 29720Sstevel@tonic-gate boolean_t trans_failure = B_TRUE; 29730Sstevel@tonic-gate int serrno; 29740Sstevel@tonic-gate 29750Sstevel@tonic-gate debug_msg("Entering run_method, instance: %s, method: %s", 29760Sstevel@tonic-gate instance->fmri, methods[method].name); 29770Sstevel@tonic-gate 29780Sstevel@tonic-gate /* 29790Sstevel@tonic-gate * Don't bother updating the instance's state for the start method 29800Sstevel@tonic-gate * as there isn't a separate start method state. 29810Sstevel@tonic-gate */ 29820Sstevel@tonic-gate if (method != IM_START) 29830Sstevel@tonic-gate update_instance_states(instance, get_method_state(method), 29840Sstevel@tonic-gate methods[method].dst_state, 29850Sstevel@tonic-gate get_method_error_success(method)); 29860Sstevel@tonic-gate 29870Sstevel@tonic-gate if ((mi = cfg->methods[method]) == NULL) { 29880Sstevel@tonic-gate /* 29893837Sstevep * If the absent method is IM_OFFLINE, default action needs 29903837Sstevep * to be taken to avoid lingering processes which can prevent 29913837Sstevep * the upcoming rebinding from happening. 29920Sstevel@tonic-gate */ 29933837Sstevep if ((method == IM_OFFLINE) && instance->config->basic->iswait) { 29943837Sstevep warn_msg(gettext("inetd_offline method for instance %s " 29953837Sstevep "is unspecified. Taking default action: kill."), 29963837Sstevep instance->fmri); 29973837Sstevep (void) str2sig("TERM", &sig); 29983837Sstevep ret = smf_kill_process(instance, sig); 29993837Sstevep process_non_start_term(instance, ret); 30003837Sstevep return (0); 30013837Sstevep } else { 30023837Sstevep process_non_start_term(instance, IMRET_SUCCESS); 30033837Sstevep return (0); 30043837Sstevep } 30050Sstevel@tonic-gate } 30060Sstevel@tonic-gate 30070Sstevel@tonic-gate /* Handle special method tokens, not allowed on start */ 30080Sstevel@tonic-gate if (method != IM_START) { 30090Sstevel@tonic-gate if (restarter_is_null_method(mi->exec_path)) { 30100Sstevel@tonic-gate /* :true means nothing should be done */ 30110Sstevel@tonic-gate process_non_start_term(instance, IMRET_SUCCESS); 30120Sstevel@tonic-gate return (0); 30130Sstevel@tonic-gate } 30140Sstevel@tonic-gate 30150Sstevel@tonic-gate if ((sig = restarter_is_kill_method(mi->exec_path)) >= 0) { 30160Sstevel@tonic-gate /* Carry out contract assassination */ 30173175Sskamm ret = iterate_repository_contracts(instance, sig); 30180Sstevel@tonic-gate /* ENOENT means we didn't find any contracts */ 30190Sstevel@tonic-gate if (ret != 0 && ret != ENOENT) { 30200Sstevel@tonic-gate error_msg(gettext("Failed to send signal %d " 30210Sstevel@tonic-gate "to contracts of instance %s: %s"), sig, 30220Sstevel@tonic-gate instance->fmri, strerror(ret)); 30230Sstevel@tonic-gate goto prefork_failure; 30240Sstevel@tonic-gate } else { 30250Sstevel@tonic-gate process_non_start_term(instance, IMRET_SUCCESS); 30260Sstevel@tonic-gate return (0); 30270Sstevel@tonic-gate } 30280Sstevel@tonic-gate } 30290Sstevel@tonic-gate 30300Sstevel@tonic-gate if ((sig = restarter_is_kill_proc_method(mi->exec_path)) >= 0) { 30313837Sstevep ret = smf_kill_process(instance, sig); 30320Sstevel@tonic-gate process_non_start_term(instance, ret); 30330Sstevel@tonic-gate return (0); 30340Sstevel@tonic-gate } 30350Sstevel@tonic-gate } 30360Sstevel@tonic-gate 30370Sstevel@tonic-gate /* 30380Sstevel@tonic-gate * Get the associated method context before the fork so we can 30390Sstevel@tonic-gate * modify the instances state if things go wrong. 30400Sstevel@tonic-gate */ 30410Sstevel@tonic-gate if ((mthd_ctxt = read_method_context(instance->fmri, 30420Sstevel@tonic-gate methods[method].name, mi->exec_path, &errstr)) == NULL) { 30430Sstevel@tonic-gate error_msg(gettext("Failed to retrieve method context for the " 30440Sstevel@tonic-gate "%s method of instance %s: %s"), methods[method].name, 30450Sstevel@tonic-gate instance->fmri, errstr); 30460Sstevel@tonic-gate goto prefork_failure; 30470Sstevel@tonic-gate } 30480Sstevel@tonic-gate 30490Sstevel@tonic-gate /* 30500Sstevel@tonic-gate * Perform some basic checks before we fork to limit the possibility 30510Sstevel@tonic-gate * of exec failures, so we can modify the instance state if necessary. 30520Sstevel@tonic-gate */ 30530Sstevel@tonic-gate if (!passes_basic_exec_checks(instance->fmri, methods[method].name, 30540Sstevel@tonic-gate mi->exec_path)) { 30550Sstevel@tonic-gate trans_failure = B_FALSE; 30560Sstevel@tonic-gate goto prefork_failure; 30570Sstevel@tonic-gate } 30580Sstevel@tonic-gate 3059*6073Sacruz if (contract_prefork(instance->fmri, method) == -1) 30600Sstevel@tonic-gate goto prefork_failure; 30610Sstevel@tonic-gate child_pid = fork(); 30620Sstevel@tonic-gate serrno = errno; 30630Sstevel@tonic-gate contract_postfork(); 30640Sstevel@tonic-gate 30650Sstevel@tonic-gate switch (child_pid) { 30660Sstevel@tonic-gate case -1: 30670Sstevel@tonic-gate error_msg(gettext( 30680Sstevel@tonic-gate "Unable to fork %s method of instance %s: %s"), 30690Sstevel@tonic-gate methods[method].name, instance->fmri, strerror(serrno)); 30700Sstevel@tonic-gate if ((serrno != EAGAIN) && (serrno != ENOMEM)) 30710Sstevel@tonic-gate trans_failure = B_FALSE; 30720Sstevel@tonic-gate goto prefork_failure; 30730Sstevel@tonic-gate case 0: /* child */ 30740Sstevel@tonic-gate exec_method(instance, method, mi, mthd_ctxt, start_info); 3075759Sdstaff /* NOTREACHED */ 30760Sstevel@tonic-gate default: /* parent */ 30770Sstevel@tonic-gate restarter_free_method_context(mthd_ctxt); 30780Sstevel@tonic-gate mthd_ctxt = NULL; 30790Sstevel@tonic-gate 30800Sstevel@tonic-gate if (get_latest_contract(&cid) < 0) 30810Sstevel@tonic-gate cid = -1; 30820Sstevel@tonic-gate 30830Sstevel@tonic-gate /* 30840Sstevel@tonic-gate * Register this method so its termination is noticed and 30850Sstevel@tonic-gate * the state transition this method participates in is 30860Sstevel@tonic-gate * continued. 30870Sstevel@tonic-gate */ 30880Sstevel@tonic-gate if (register_method(instance, child_pid, cid, method) != 0) { 30890Sstevel@tonic-gate /* 30900Sstevel@tonic-gate * Since we will never find out about the termination 30910Sstevel@tonic-gate * of this method, if it's a non-start method treat 30920Sstevel@tonic-gate * is as a failure so we don't block restarter event 30930Sstevel@tonic-gate * processing on it whilst it languishes in a method 30940Sstevel@tonic-gate * running state. 30950Sstevel@tonic-gate */ 30960Sstevel@tonic-gate error_msg(gettext("Failed to monitor status of " 30970Sstevel@tonic-gate "%s method of instance %s"), methods[method].name, 30980Sstevel@tonic-gate instance->fmri); 30990Sstevel@tonic-gate if (method != IM_START) 31000Sstevel@tonic-gate process_non_start_term(instance, IMRET_FAILURE); 31010Sstevel@tonic-gate } 31020Sstevel@tonic-gate 31030Sstevel@tonic-gate add_method_ids(instance, child_pid, cid, method); 31040Sstevel@tonic-gate 31050Sstevel@tonic-gate /* do tcp tracing for those nowait instances that request it */ 31060Sstevel@tonic-gate if ((method == IM_START) && cfg->basic->do_tcp_trace && 31070Sstevel@tonic-gate !cfg->basic->iswait) { 31080Sstevel@tonic-gate char buf[INET6_ADDRSTRLEN]; 31090Sstevel@tonic-gate 31100Sstevel@tonic-gate syslog(LOG_NOTICE, "%s[%d] from %s %d", 31110Sstevel@tonic-gate cfg->basic->svc_name, child_pid, 31120Sstevel@tonic-gate inet_ntop_native(instance->remote_addr.ss_family, 31130Sstevel@tonic-gate SS_SINADDR(instance->remote_addr), buf, 31140Sstevel@tonic-gate sizeof (buf)), 31150Sstevel@tonic-gate ntohs(SS_PORT(instance->remote_addr))); 31160Sstevel@tonic-gate } 31170Sstevel@tonic-gate } 31180Sstevel@tonic-gate 31190Sstevel@tonic-gate return (0); 31200Sstevel@tonic-gate 31210Sstevel@tonic-gate prefork_failure: 31220Sstevel@tonic-gate if (mthd_ctxt != NULL) { 31230Sstevel@tonic-gate restarter_free_method_context(mthd_ctxt); 31240Sstevel@tonic-gate mthd_ctxt = NULL; 31250Sstevel@tonic-gate } 31260Sstevel@tonic-gate 31270Sstevel@tonic-gate if (method == IM_START) { 31280Sstevel@tonic-gate /* 31290Sstevel@tonic-gate * Only place a start method in maintenance if we're sure 31300Sstevel@tonic-gate * that the failure was non-transient. 31310Sstevel@tonic-gate */ 31320Sstevel@tonic-gate if (!trans_failure) { 31330Sstevel@tonic-gate destroy_bound_fds(instance); 31340Sstevel@tonic-gate update_state(instance, IIS_MAINTENANCE, RERR_FAULT); 31350Sstevel@tonic-gate } 31360Sstevel@tonic-gate } else { 31370Sstevel@tonic-gate /* treat the failure as if the method ran and failed */ 31380Sstevel@tonic-gate process_non_start_term(instance, IMRET_FAILURE); 31390Sstevel@tonic-gate } 31400Sstevel@tonic-gate 31410Sstevel@tonic-gate return (-1); 31420Sstevel@tonic-gate } 31430Sstevel@tonic-gate 31440Sstevel@tonic-gate static int 31450Sstevel@tonic-gate accept_connection(instance_t *instance, proto_info_t *pi) 31460Sstevel@tonic-gate { 31470Sstevel@tonic-gate int fd; 31480Sstevel@tonic-gate socklen_t size; 31490Sstevel@tonic-gate 31500Sstevel@tonic-gate debug_msg("Entering accept_connection"); 31510Sstevel@tonic-gate 31520Sstevel@tonic-gate if (instance->config->basic->istlx) { 31530Sstevel@tonic-gate fd = tlx_accept(instance->fmri, (tlx_info_t *)pi, 31540Sstevel@tonic-gate &(instance->remote_addr)); 31550Sstevel@tonic-gate } else { 31560Sstevel@tonic-gate size = sizeof (instance->remote_addr); 31570Sstevel@tonic-gate fd = accept(pi->listen_fd, 31580Sstevel@tonic-gate (struct sockaddr *)&(instance->remote_addr), &size); 31590Sstevel@tonic-gate if (fd < 0) 31600Sstevel@tonic-gate error_msg("accept: %s", strerror(errno)); 31610Sstevel@tonic-gate } 31620Sstevel@tonic-gate 31630Sstevel@tonic-gate return (fd); 31640Sstevel@tonic-gate } 31650Sstevel@tonic-gate 31660Sstevel@tonic-gate /* 31670Sstevel@tonic-gate * Handle an incoming connection request for a nowait service. 31680Sstevel@tonic-gate * This involves accepting the incoming connection on a new fd. Connection 31690Sstevel@tonic-gate * rate checks are then performed, transitioning the service to the 31700Sstevel@tonic-gate * conrate offline state if these fail. Otherwise, the service's start method 31710Sstevel@tonic-gate * is run (performing TCP wrappers checks if applicable as we do), and on 31720Sstevel@tonic-gate * success concurrent copies checking is done, transitioning the service to the 31730Sstevel@tonic-gate * copies offline state if this fails. 31740Sstevel@tonic-gate */ 31750Sstevel@tonic-gate static void 31760Sstevel@tonic-gate process_nowait_request(instance_t *instance, proto_info_t *pi) 31770Sstevel@tonic-gate { 31780Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 31790Sstevel@tonic-gate int ret; 31800Sstevel@tonic-gate adt_event_data_t *ae; 31810Sstevel@tonic-gate char buf[BUFSIZ]; 31820Sstevel@tonic-gate 31830Sstevel@tonic-gate debug_msg("Entering process_nowait_req"); 31840Sstevel@tonic-gate 31850Sstevel@tonic-gate /* accept nowait service connections on a new fd */ 31860Sstevel@tonic-gate if ((instance->conn_fd = accept_connection(instance, pi)) == -1) { 31870Sstevel@tonic-gate /* 31880Sstevel@tonic-gate * Failed accept. Return and allow the event loop to initiate 31890Sstevel@tonic-gate * another attempt later if the request is still present. 31900Sstevel@tonic-gate */ 31910Sstevel@tonic-gate return; 31920Sstevel@tonic-gate } 31930Sstevel@tonic-gate 31940Sstevel@tonic-gate /* 31950Sstevel@tonic-gate * Limit connection rate of nowait services. If either conn_rate_max 31960Sstevel@tonic-gate * or conn_rate_offline are <= 0, no connection rate limit checking 31970Sstevel@tonic-gate * is done. If the configured rate is exceeded, the instance is taken 31980Sstevel@tonic-gate * to the connrate_offline state and a timer scheduled to try and 31990Sstevel@tonic-gate * bring the instance back online after the configured offline time. 32000Sstevel@tonic-gate */ 32010Sstevel@tonic-gate if ((cfg->conn_rate_max > 0) && (cfg->conn_rate_offline > 0)) { 32020Sstevel@tonic-gate if (instance->conn_rate_count++ == 0) { 32030Sstevel@tonic-gate instance->conn_rate_start = time(NULL); 32040Sstevel@tonic-gate } else if (instance->conn_rate_count > 32050Sstevel@tonic-gate cfg->conn_rate_max) { 32060Sstevel@tonic-gate time_t now = time(NULL); 32070Sstevel@tonic-gate 32080Sstevel@tonic-gate if ((now - instance->conn_rate_start) > 1) { 32090Sstevel@tonic-gate instance->conn_rate_start = now; 32100Sstevel@tonic-gate instance->conn_rate_count = 1; 32110Sstevel@tonic-gate } else { 32120Sstevel@tonic-gate /* Generate audit record */ 32130Sstevel@tonic-gate if ((ae = adt_alloc_event(audit_handle, 32140Sstevel@tonic-gate ADT_inetd_ratelimit)) == NULL) { 32150Sstevel@tonic-gate error_msg(gettext("Unable to allocate " 32160Sstevel@tonic-gate "rate limit audit event")); 32170Sstevel@tonic-gate } else { 32180Sstevel@tonic-gate adt_inetd_ratelimit_t *rl = 32190Sstevel@tonic-gate &ae->adt_inetd_ratelimit; 32200Sstevel@tonic-gate /* 32210Sstevel@tonic-gate * The inetd_ratelimit audit 32220Sstevel@tonic-gate * record consists of: 32230Sstevel@tonic-gate * Service name 32240Sstevel@tonic-gate * Connection rate limit 32250Sstevel@tonic-gate */ 32260Sstevel@tonic-gate rl->service_name = cfg->svc_name; 32270Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 32280Sstevel@tonic-gate "limit=%lld", cfg->conn_rate_max); 32290Sstevel@tonic-gate rl->limit = buf; 32300Sstevel@tonic-gate (void) adt_put_event(ae, ADT_SUCCESS, 32310Sstevel@tonic-gate ADT_SUCCESS); 32320Sstevel@tonic-gate adt_free_event(ae); 32330Sstevel@tonic-gate } 32340Sstevel@tonic-gate 32350Sstevel@tonic-gate error_msg(gettext( 32360Sstevel@tonic-gate "Instance %s has exceeded its configured " 32370Sstevel@tonic-gate "connection rate, additional connections " 32380Sstevel@tonic-gate "will not be accepted for %d seconds"), 32390Sstevel@tonic-gate instance->fmri, cfg->conn_rate_offline); 32400Sstevel@tonic-gate 32410Sstevel@tonic-gate close_net_fd(instance, instance->conn_fd); 32420Sstevel@tonic-gate instance->conn_fd = -1; 32430Sstevel@tonic-gate 32440Sstevel@tonic-gate destroy_bound_fds(instance); 32450Sstevel@tonic-gate 32460Sstevel@tonic-gate instance->conn_rate_count = 0; 32470Sstevel@tonic-gate 32480Sstevel@tonic-gate instance->conn_rate_exceeded = B_TRUE; 32490Sstevel@tonic-gate (void) run_method(instance, IM_OFFLINE, NULL); 32500Sstevel@tonic-gate 32510Sstevel@tonic-gate return; 32520Sstevel@tonic-gate } 32530Sstevel@tonic-gate } 32540Sstevel@tonic-gate } 32550Sstevel@tonic-gate 32560Sstevel@tonic-gate ret = run_method(instance, IM_START, pi); 32570Sstevel@tonic-gate 32580Sstevel@tonic-gate close_net_fd(instance, instance->conn_fd); 32590Sstevel@tonic-gate instance->conn_fd = -1; 32600Sstevel@tonic-gate 32610Sstevel@tonic-gate if (ret == -1) /* the method wasn't forked */ 32620Sstevel@tonic-gate return; 32630Sstevel@tonic-gate 32640Sstevel@tonic-gate instance->copies++; 32650Sstevel@tonic-gate 32660Sstevel@tonic-gate /* 32670Sstevel@tonic-gate * Limit concurrent connections of nowait services. 32680Sstevel@tonic-gate */ 32690Sstevel@tonic-gate if (copies_limit_exceeded(instance)) { 32700Sstevel@tonic-gate /* Generate audit record */ 32710Sstevel@tonic-gate if ((ae = adt_alloc_event(audit_handle, ADT_inetd_copylimit)) 32720Sstevel@tonic-gate == NULL) { 32730Sstevel@tonic-gate error_msg(gettext("Unable to allocate copy limit " 32740Sstevel@tonic-gate "audit event")); 32750Sstevel@tonic-gate } else { 32760Sstevel@tonic-gate /* 32770Sstevel@tonic-gate * The inetd_copylimit audit record consists of: 32780Sstevel@tonic-gate * Service name 32790Sstevel@tonic-gate * Copy limit 32800Sstevel@tonic-gate */ 32810Sstevel@tonic-gate ae->adt_inetd_copylimit.service_name = cfg->svc_name; 32820Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "limit=%lld", 32830Sstevel@tonic-gate cfg->max_copies); 32840Sstevel@tonic-gate ae->adt_inetd_copylimit.limit = buf; 32850Sstevel@tonic-gate (void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS); 32860Sstevel@tonic-gate adt_free_event(ae); 32870Sstevel@tonic-gate } 32880Sstevel@tonic-gate 32890Sstevel@tonic-gate warn_msg(gettext("Instance %s has reached its maximum " 32900Sstevel@tonic-gate "configured copies, no new connections will be accepted"), 32910Sstevel@tonic-gate instance->fmri); 32920Sstevel@tonic-gate destroy_bound_fds(instance); 32930Sstevel@tonic-gate (void) run_method(instance, IM_OFFLINE, NULL); 32940Sstevel@tonic-gate } 32950Sstevel@tonic-gate } 32960Sstevel@tonic-gate 32970Sstevel@tonic-gate /* 32980Sstevel@tonic-gate * Handle an incoming request for a wait type service. 32990Sstevel@tonic-gate * Failure rate checking is done first, taking the service to the maintenance 33000Sstevel@tonic-gate * state if the checks fail. Following this, the service's start method is run, 33010Sstevel@tonic-gate * and on success, we stop listening for new requests for this service. 33020Sstevel@tonic-gate */ 33030Sstevel@tonic-gate static void 33040Sstevel@tonic-gate process_wait_request(instance_t *instance, const proto_info_t *pi) 33050Sstevel@tonic-gate { 33060Sstevel@tonic-gate basic_cfg_t *cfg = instance->config->basic; 33070Sstevel@tonic-gate int ret; 33080Sstevel@tonic-gate adt_event_data_t *ae; 33090Sstevel@tonic-gate char buf[BUFSIZ]; 33100Sstevel@tonic-gate 33110Sstevel@tonic-gate debug_msg("Entering process_wait_request"); 33120Sstevel@tonic-gate 33130Sstevel@tonic-gate instance->conn_fd = pi->listen_fd; 33140Sstevel@tonic-gate 33150Sstevel@tonic-gate /* 33160Sstevel@tonic-gate * Detect broken servers and transition them to maintenance. If a 33170Sstevel@tonic-gate * wait type service exits without accepting the connection or 33180Sstevel@tonic-gate * consuming (reading) the datagram, that service's descriptor will 33190Sstevel@tonic-gate * select readable again, and inetd will fork another instance of 33200Sstevel@tonic-gate * the server. If either wait_fail_cnt or wait_fail_interval are <= 0, 33210Sstevel@tonic-gate * no failure rate detection is done. 33220Sstevel@tonic-gate */ 33230Sstevel@tonic-gate if ((cfg->wait_fail_cnt > 0) && (cfg->wait_fail_interval > 0)) { 33240Sstevel@tonic-gate if (instance->fail_rate_count++ == 0) { 33250Sstevel@tonic-gate instance->fail_rate_start = time(NULL); 33260Sstevel@tonic-gate } else if (instance->fail_rate_count > cfg->wait_fail_cnt) { 33270Sstevel@tonic-gate time_t now = time(NULL); 33280Sstevel@tonic-gate 33290Sstevel@tonic-gate if ((now - instance->fail_rate_start) > 33300Sstevel@tonic-gate cfg->wait_fail_interval) { 33310Sstevel@tonic-gate instance->fail_rate_start = now; 33320Sstevel@tonic-gate instance->fail_rate_count = 1; 33330Sstevel@tonic-gate } else { 33340Sstevel@tonic-gate /* Generate audit record */ 33350Sstevel@tonic-gate if ((ae = adt_alloc_event(audit_handle, 33360Sstevel@tonic-gate ADT_inetd_failrate)) == NULL) { 33370Sstevel@tonic-gate error_msg(gettext("Unable to allocate " 33380Sstevel@tonic-gate "failure rate audit event")); 33390Sstevel@tonic-gate } else { 33400Sstevel@tonic-gate adt_inetd_failrate_t *fr = 33410Sstevel@tonic-gate &ae->adt_inetd_failrate; 33420Sstevel@tonic-gate /* 33430Sstevel@tonic-gate * The inetd_failrate audit record 33440Sstevel@tonic-gate * consists of: 33450Sstevel@tonic-gate * Service name 33460Sstevel@tonic-gate * Failure rate 33470Sstevel@tonic-gate * Interval 33480Sstevel@tonic-gate * Last two are expressed as k=v pairs 33490Sstevel@tonic-gate * in the values field. 33500Sstevel@tonic-gate */ 33510Sstevel@tonic-gate fr->service_name = cfg->svc_name; 33520Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 33530Sstevel@tonic-gate "limit=%lld,interval=%d", 33540Sstevel@tonic-gate cfg->wait_fail_cnt, 33550Sstevel@tonic-gate cfg->wait_fail_interval); 33560Sstevel@tonic-gate fr->values = buf; 33570Sstevel@tonic-gate (void) adt_put_event(ae, ADT_SUCCESS, 33580Sstevel@tonic-gate ADT_SUCCESS); 33590Sstevel@tonic-gate adt_free_event(ae); 33600Sstevel@tonic-gate } 33610Sstevel@tonic-gate 33620Sstevel@tonic-gate error_msg(gettext( 33630Sstevel@tonic-gate "Instance %s has exceeded its configured " 33640Sstevel@tonic-gate "failure rate, transitioning to " 33650Sstevel@tonic-gate "maintenance"), instance->fmri); 33660Sstevel@tonic-gate instance->fail_rate_count = 0; 33670Sstevel@tonic-gate 33680Sstevel@tonic-gate destroy_bound_fds(instance); 33690Sstevel@tonic-gate 33700Sstevel@tonic-gate instance->maintenance_req = B_TRUE; 33710Sstevel@tonic-gate (void) run_method(instance, IM_OFFLINE, NULL); 33720Sstevel@tonic-gate return; 33730Sstevel@tonic-gate } 33740Sstevel@tonic-gate } 33750Sstevel@tonic-gate } 33760Sstevel@tonic-gate 33770Sstevel@tonic-gate ret = run_method(instance, IM_START, pi); 33780Sstevel@tonic-gate 33790Sstevel@tonic-gate instance->conn_fd = -1; 33800Sstevel@tonic-gate 33810Sstevel@tonic-gate if (ret == 0) { 33820Sstevel@tonic-gate /* 33830Sstevel@tonic-gate * Stop listening for connections now we've fired off the 33840Sstevel@tonic-gate * server for a wait type instance. 33850Sstevel@tonic-gate */ 33860Sstevel@tonic-gate (void) poll_bound_fds(instance, B_FALSE); 33870Sstevel@tonic-gate } 33880Sstevel@tonic-gate } 33890Sstevel@tonic-gate 33900Sstevel@tonic-gate /* 33910Sstevel@tonic-gate * Process any networks requests for each proto for each instance. 33920Sstevel@tonic-gate */ 33930Sstevel@tonic-gate void 33940Sstevel@tonic-gate process_network_events(void) 33950Sstevel@tonic-gate { 33960Sstevel@tonic-gate instance_t *instance; 33970Sstevel@tonic-gate 33980Sstevel@tonic-gate debug_msg("Entering process_network_events"); 33990Sstevel@tonic-gate 34000Sstevel@tonic-gate for (instance = uu_list_first(instance_list); instance != NULL; 34010Sstevel@tonic-gate instance = uu_list_next(instance_list, instance)) { 34020Sstevel@tonic-gate basic_cfg_t *cfg; 34030Sstevel@tonic-gate proto_info_t *pi; 34040Sstevel@tonic-gate 34050Sstevel@tonic-gate /* 34060Sstevel@tonic-gate * Ignore instances in states that definitely don't have any 34070Sstevel@tonic-gate * listening fds. 34080Sstevel@tonic-gate */ 34090Sstevel@tonic-gate switch (instance->cur_istate) { 34100Sstevel@tonic-gate case IIS_ONLINE: 34110Sstevel@tonic-gate case IIS_DEGRADED: 34120Sstevel@tonic-gate case IIS_IN_REFRESH_METHOD: 34130Sstevel@tonic-gate break; 34140Sstevel@tonic-gate default: 34150Sstevel@tonic-gate continue; 34160Sstevel@tonic-gate } 34170Sstevel@tonic-gate 34180Sstevel@tonic-gate cfg = instance->config->basic; 34190Sstevel@tonic-gate 34200Sstevel@tonic-gate for (pi = uu_list_first(cfg->proto_list); pi != NULL; 34210Sstevel@tonic-gate pi = uu_list_next(cfg->proto_list, pi)) { 34220Sstevel@tonic-gate if ((pi->listen_fd != -1) && 34230Sstevel@tonic-gate isset_pollfd(pi->listen_fd)) { 34240Sstevel@tonic-gate if (cfg->iswait) { 34250Sstevel@tonic-gate process_wait_request(instance, pi); 34260Sstevel@tonic-gate } else { 34270Sstevel@tonic-gate process_nowait_request(instance, pi); 34280Sstevel@tonic-gate } 34290Sstevel@tonic-gate } 34300Sstevel@tonic-gate } 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate } 34330Sstevel@tonic-gate 34340Sstevel@tonic-gate /* ARGSUSED0 */ 34350Sstevel@tonic-gate static void 34360Sstevel@tonic-gate sigterm_handler(int sig) 34370Sstevel@tonic-gate { 34380Sstevel@tonic-gate debug_msg("Entering sigterm_handler"); 34390Sstevel@tonic-gate 34400Sstevel@tonic-gate got_sigterm = B_TRUE; 34410Sstevel@tonic-gate } 34420Sstevel@tonic-gate 34430Sstevel@tonic-gate /* ARGSUSED0 */ 34440Sstevel@tonic-gate static void 34450Sstevel@tonic-gate sighup_handler(int sig) 34460Sstevel@tonic-gate { 34470Sstevel@tonic-gate debug_msg("Entering sighup_handler"); 34480Sstevel@tonic-gate 34490Sstevel@tonic-gate refresh_inetd_requested = B_TRUE; 34500Sstevel@tonic-gate } 34510Sstevel@tonic-gate 34520Sstevel@tonic-gate /* 34530Sstevel@tonic-gate * inetd's major work loop. This function sits in poll waiting for events 34540Sstevel@tonic-gate * to occur, processing them when they do. The possible events are 34550Sstevel@tonic-gate * master restarter requests, expired timer queue timers, stop/refresh signal 34560Sstevel@tonic-gate * requests, contract events indicating process termination, stop/refresh 34570Sstevel@tonic-gate * requests originating from one of the stop/refresh inetd processes and 34580Sstevel@tonic-gate * network events. 34590Sstevel@tonic-gate * The loop is exited when a stop request is received and processed, and 34600Sstevel@tonic-gate * all the instances have reached a suitable 'stopping' state. 34610Sstevel@tonic-gate */ 34620Sstevel@tonic-gate static void 34630Sstevel@tonic-gate event_loop(void) 34640Sstevel@tonic-gate { 34650Sstevel@tonic-gate instance_t *instance; 34660Sstevel@tonic-gate int timeout; 34670Sstevel@tonic-gate 34680Sstevel@tonic-gate debug_msg("Entering event_loop"); 34690Sstevel@tonic-gate 34700Sstevel@tonic-gate for (;;) { 34710Sstevel@tonic-gate int pret = -1; 34720Sstevel@tonic-gate 34730Sstevel@tonic-gate timeout = iu_earliest_timer(timer_queue); 34740Sstevel@tonic-gate 34750Sstevel@tonic-gate debug_msg("Doing signal check/poll"); 34760Sstevel@tonic-gate if (!got_sigterm && !refresh_inetd_requested) { 34770Sstevel@tonic-gate pret = poll(poll_fds, num_pollfds, timeout); 34780Sstevel@tonic-gate if ((pret == -1) && (errno != EINTR)) { 34790Sstevel@tonic-gate error_msg(gettext("poll failure: %s"), 34800Sstevel@tonic-gate strerror(errno)); 34810Sstevel@tonic-gate continue; 34820Sstevel@tonic-gate } 34830Sstevel@tonic-gate debug_msg("Exiting poll, returned: %d", pret); 34840Sstevel@tonic-gate } 34850Sstevel@tonic-gate 34860Sstevel@tonic-gate if (got_sigterm) { 34870Sstevel@tonic-gate msg_fini(); 34880Sstevel@tonic-gate inetd_stop(); 34890Sstevel@tonic-gate got_sigterm = B_FALSE; 34900Sstevel@tonic-gate goto check_if_stopped; 34910Sstevel@tonic-gate } 34920Sstevel@tonic-gate 34930Sstevel@tonic-gate /* 34940Sstevel@tonic-gate * Process any stop/refresh requests from the Unix Domain 34950Sstevel@tonic-gate * Socket. 34960Sstevel@tonic-gate */ 34970Sstevel@tonic-gate if ((pret != -1) && isset_pollfd(uds_fd)) { 34980Sstevel@tonic-gate while (process_uds_event() == 0) 34990Sstevel@tonic-gate ; 35000Sstevel@tonic-gate } 35010Sstevel@tonic-gate 35020Sstevel@tonic-gate /* 35030Sstevel@tonic-gate * Process refresh request. We do this check after the UDS 35040Sstevel@tonic-gate * event check above, as it would be wasted processing if we 35050Sstevel@tonic-gate * started refreshing inetd based on a SIGHUP, and then were 35060Sstevel@tonic-gate * told to shut-down via a UDS event. 35070Sstevel@tonic-gate */ 35080Sstevel@tonic-gate if (refresh_inetd_requested) { 35090Sstevel@tonic-gate refresh_inetd_requested = B_FALSE; 35100Sstevel@tonic-gate if (!inetd_stopping) 35110Sstevel@tonic-gate inetd_refresh(); 35120Sstevel@tonic-gate } 35130Sstevel@tonic-gate 35140Sstevel@tonic-gate /* 35150Sstevel@tonic-gate * We were interrupted by a signal. Don't waste any more 35160Sstevel@tonic-gate * time processing a potentially inaccurate poll return. 35170Sstevel@tonic-gate */ 35180Sstevel@tonic-gate if (pret == -1) 35190Sstevel@tonic-gate continue; 35200Sstevel@tonic-gate 35210Sstevel@tonic-gate /* 35220Sstevel@tonic-gate * Process any instance restarter events. 35230Sstevel@tonic-gate */ 35240Sstevel@tonic-gate if (isset_pollfd(rst_event_pipe[PE_CONSUMER])) { 35250Sstevel@tonic-gate while (process_restarter_event() == 0) 35260Sstevel@tonic-gate ; 35270Sstevel@tonic-gate } 35280Sstevel@tonic-gate 35290Sstevel@tonic-gate /* 35300Sstevel@tonic-gate * Process any expired timers (bind retry, con-rate offline, 35310Sstevel@tonic-gate * method timeouts). 35320Sstevel@tonic-gate */ 35330Sstevel@tonic-gate (void) iu_expire_timers(timer_queue); 35340Sstevel@tonic-gate 35350Sstevel@tonic-gate process_terminated_methods(); 35360Sstevel@tonic-gate 35370Sstevel@tonic-gate /* 35380Sstevel@tonic-gate * If inetd is stopping, check whether all our managed 35390Sstevel@tonic-gate * instances have been stopped and we can return. 35400Sstevel@tonic-gate */ 35410Sstevel@tonic-gate if (inetd_stopping) { 35420Sstevel@tonic-gate check_if_stopped: 35430Sstevel@tonic-gate for (instance = uu_list_first(instance_list); 35440Sstevel@tonic-gate instance != NULL; 35450Sstevel@tonic-gate instance = uu_list_next(instance_list, instance)) { 35460Sstevel@tonic-gate if (!instance_stopped(instance)) { 35470Sstevel@tonic-gate debug_msg("%s not yet stopped", 35480Sstevel@tonic-gate instance->fmri); 35490Sstevel@tonic-gate break; 35500Sstevel@tonic-gate } 35510Sstevel@tonic-gate } 35520Sstevel@tonic-gate /* if all instances are stopped, return */ 35530Sstevel@tonic-gate if (instance == NULL) 35540Sstevel@tonic-gate return; 35550Sstevel@tonic-gate } 35560Sstevel@tonic-gate 35570Sstevel@tonic-gate process_network_events(); 35580Sstevel@tonic-gate } 35590Sstevel@tonic-gate } 35600Sstevel@tonic-gate 35610Sstevel@tonic-gate static void 35620Sstevel@tonic-gate fini(void) 35630Sstevel@tonic-gate { 35640Sstevel@tonic-gate debug_msg("Entering fini"); 35650Sstevel@tonic-gate 35660Sstevel@tonic-gate method_fini(); 35670Sstevel@tonic-gate uds_fini(); 35680Sstevel@tonic-gate if (timer_queue != NULL) 35690Sstevel@tonic-gate iu_tq_destroy(timer_queue); 3570759Sdstaff 35710Sstevel@tonic-gate 35720Sstevel@tonic-gate /* 3573759Sdstaff * We don't bother to undo the restarter interface at all. 3574759Sdstaff * Because of quirks in the interface, there is no way to 3575759Sdstaff * disconnect from the channel and cause any new events to be 3576759Sdstaff * queued. However, any events which are received and not 3577759Sdstaff * acknowledged will be re-sent when inetd restarts as long as inetd 3578759Sdstaff * uses the same subscriber ID, which it does. 3579759Sdstaff * 3580759Sdstaff * By keeping the event pipe open but ignoring it, any events which 3581759Sdstaff * occur will cause restarter_event_proxy to hang without breaking 3582759Sdstaff * anything. 35830Sstevel@tonic-gate */ 35840Sstevel@tonic-gate 35850Sstevel@tonic-gate if (instance_list != NULL) { 35860Sstevel@tonic-gate void *cookie = NULL; 35870Sstevel@tonic-gate instance_t *inst; 35880Sstevel@tonic-gate 35890Sstevel@tonic-gate while ((inst = uu_list_teardown(instance_list, &cookie)) != 35900Sstevel@tonic-gate NULL) 35910Sstevel@tonic-gate destroy_instance(inst); 35920Sstevel@tonic-gate uu_list_destroy(instance_list); 35930Sstevel@tonic-gate } 35940Sstevel@tonic-gate if (instance_pool != NULL) 35950Sstevel@tonic-gate uu_list_pool_destroy(instance_pool); 35960Sstevel@tonic-gate tlx_fini(); 35970Sstevel@tonic-gate config_fini(); 35980Sstevel@tonic-gate repval_fini(); 35990Sstevel@tonic-gate poll_fini(); 36000Sstevel@tonic-gate 36010Sstevel@tonic-gate /* Close audit session */ 36020Sstevel@tonic-gate (void) adt_end_session(audit_handle); 36030Sstevel@tonic-gate } 36040Sstevel@tonic-gate 36050Sstevel@tonic-gate static int 36060Sstevel@tonic-gate init(void) 36070Sstevel@tonic-gate { 36080Sstevel@tonic-gate int err; 36090Sstevel@tonic-gate 36100Sstevel@tonic-gate debug_msg("Entering init"); 36110Sstevel@tonic-gate 36120Sstevel@tonic-gate if (repval_init() < 0) 36130Sstevel@tonic-gate goto failed; 36140Sstevel@tonic-gate 36150Sstevel@tonic-gate if (config_init() < 0) 36160Sstevel@tonic-gate goto failed; 36170Sstevel@tonic-gate 36180Sstevel@tonic-gate if (tlx_init() < 0) 36190Sstevel@tonic-gate goto failed; 36200Sstevel@tonic-gate 36210Sstevel@tonic-gate /* Setup instance list. */ 36220Sstevel@tonic-gate if ((instance_pool = uu_list_pool_create("instance_pool", 36230Sstevel@tonic-gate sizeof (instance_t), offsetof(instance_t, link), NULL, 36240Sstevel@tonic-gate UU_LIST_POOL_DEBUG)) == NULL) { 36250Sstevel@tonic-gate error_msg("%s: %s", 36260Sstevel@tonic-gate gettext("Failed to create instance pool"), 36270Sstevel@tonic-gate uu_strerror(uu_error())); 36280Sstevel@tonic-gate goto failed; 36290Sstevel@tonic-gate } 36300Sstevel@tonic-gate if ((instance_list = uu_list_create(instance_pool, NULL, 0)) == NULL) { 36310Sstevel@tonic-gate error_msg("%s: %s", 36320Sstevel@tonic-gate gettext("Failed to create instance list"), 36330Sstevel@tonic-gate uu_strerror(uu_error())); 36340Sstevel@tonic-gate goto failed; 36350Sstevel@tonic-gate } 36360Sstevel@tonic-gate 36370Sstevel@tonic-gate /* 36380Sstevel@tonic-gate * Create event pipe to communicate events with the main event 36390Sstevel@tonic-gate * loop and add it to the event loop's fdset. 36400Sstevel@tonic-gate */ 36410Sstevel@tonic-gate if (pipe(rst_event_pipe) < 0) { 36420Sstevel@tonic-gate error_msg("pipe: %s", strerror(errno)); 36430Sstevel@tonic-gate goto failed; 36440Sstevel@tonic-gate } 36450Sstevel@tonic-gate /* 36460Sstevel@tonic-gate * We only leave the producer end to block on reads/writes as we 36470Sstevel@tonic-gate * can't afford to block in the main thread, yet need to in 36480Sstevel@tonic-gate * the restarter event thread, so it can sit and wait for an 36490Sstevel@tonic-gate * acknowledgement to be written to the pipe. 36500Sstevel@tonic-gate */ 36510Sstevel@tonic-gate disable_blocking(rst_event_pipe[PE_CONSUMER]); 36520Sstevel@tonic-gate if ((set_pollfd(rst_event_pipe[PE_CONSUMER], POLLIN)) == -1) 36530Sstevel@tonic-gate goto failed; 36540Sstevel@tonic-gate 36550Sstevel@tonic-gate /* 36560Sstevel@tonic-gate * Register with master restarter for managed service events. This 36570Sstevel@tonic-gate * will fail, amongst other reasons, if inetd is already running. 36580Sstevel@tonic-gate */ 36590Sstevel@tonic-gate if ((err = restarter_bind_handle(RESTARTER_EVENT_VERSION, 36600Sstevel@tonic-gate INETD_INSTANCE_FMRI, restarter_event_proxy, 0, 36610Sstevel@tonic-gate &rst_event_handle)) != 0) { 36620Sstevel@tonic-gate error_msg(gettext( 36630Sstevel@tonic-gate "Failed to register for restarter events: %s"), 36640Sstevel@tonic-gate strerror(err)); 36650Sstevel@tonic-gate goto failed; 36660Sstevel@tonic-gate } 36670Sstevel@tonic-gate 36680Sstevel@tonic-gate if (contract_init() < 0) 36690Sstevel@tonic-gate goto failed; 36700Sstevel@tonic-gate 36710Sstevel@tonic-gate if ((timer_queue = iu_tq_create()) == NULL) { 36720Sstevel@tonic-gate error_msg(gettext("Failed to create timer queue.")); 36730Sstevel@tonic-gate goto failed; 36740Sstevel@tonic-gate } 36750Sstevel@tonic-gate 36760Sstevel@tonic-gate if (uds_init() < 0) 36770Sstevel@tonic-gate goto failed; 36780Sstevel@tonic-gate 36790Sstevel@tonic-gate if (method_init() < 0) 36800Sstevel@tonic-gate goto failed; 36810Sstevel@tonic-gate 36820Sstevel@tonic-gate /* Initialize auditing session */ 36830Sstevel@tonic-gate if (adt_start_session(&audit_handle, NULL, ADT_USE_PROC_DATA) != 0) { 36840Sstevel@tonic-gate error_msg(gettext("Unable to start audit session")); 36850Sstevel@tonic-gate } 36860Sstevel@tonic-gate 36870Sstevel@tonic-gate /* 36880Sstevel@tonic-gate * Initialize signal dispositions/masks 36890Sstevel@tonic-gate */ 36900Sstevel@tonic-gate (void) sigset(SIGHUP, sighup_handler); 36910Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm_handler); 36920Sstevel@tonic-gate (void) sigignore(SIGINT); 36930Sstevel@tonic-gate 36940Sstevel@tonic-gate return (0); 36950Sstevel@tonic-gate 36960Sstevel@tonic-gate failed: 36970Sstevel@tonic-gate fini(); 36980Sstevel@tonic-gate return (-1); 36990Sstevel@tonic-gate } 37000Sstevel@tonic-gate 37010Sstevel@tonic-gate static int 37020Sstevel@tonic-gate start_method(void) 37030Sstevel@tonic-gate { 37040Sstevel@tonic-gate int i; 37050Sstevel@tonic-gate int pipe_fds[2]; 37060Sstevel@tonic-gate int child; 37070Sstevel@tonic-gate 37080Sstevel@tonic-gate debug_msg("ENTERING START_METHOD:"); 37090Sstevel@tonic-gate 37100Sstevel@tonic-gate /* Create pipe for child to notify parent of initialization success. */ 37110Sstevel@tonic-gate if (pipe(pipe_fds) < 0) { 37120Sstevel@tonic-gate debug_msg("pipe: %s", strerror(errno)); 37130Sstevel@tonic-gate return (SMF_EXIT_ERR_OTHER); 37140Sstevel@tonic-gate } 37150Sstevel@tonic-gate 37160Sstevel@tonic-gate if ((child = fork()) == -1) { 37170Sstevel@tonic-gate debug_msg("fork: %s", strerror(errno)); 37180Sstevel@tonic-gate (void) close(pipe_fds[PE_CONSUMER]); 37190Sstevel@tonic-gate (void) close(pipe_fds[PE_PRODUCER]); 37200Sstevel@tonic-gate return (SMF_EXIT_ERR_OTHER); 37210Sstevel@tonic-gate } else if (child > 0) { /* parent */ 37220Sstevel@tonic-gate 37230Sstevel@tonic-gate /* Wait on child to return success of initialization. */ 37240Sstevel@tonic-gate (void) close(pipe_fds[PE_PRODUCER]); 37250Sstevel@tonic-gate if ((safe_read(pipe_fds[PE_CONSUMER], &i, sizeof (i)) != 0) || 37260Sstevel@tonic-gate (i < 0)) { 37270Sstevel@tonic-gate error_msg(gettext( 37280Sstevel@tonic-gate "Initialization failed, unable to start")); 37290Sstevel@tonic-gate (void) close(pipe_fds[PE_CONSUMER]); 37300Sstevel@tonic-gate /* 37310Sstevel@tonic-gate * Batch all initialization errors as 'other' errors, 37320Sstevel@tonic-gate * resulting in retries being attempted. 37330Sstevel@tonic-gate */ 37340Sstevel@tonic-gate return (SMF_EXIT_ERR_OTHER); 37350Sstevel@tonic-gate } else { 37360Sstevel@tonic-gate (void) close(pipe_fds[PE_CONSUMER]); 37370Sstevel@tonic-gate return (SMF_EXIT_OK); 37380Sstevel@tonic-gate } 37390Sstevel@tonic-gate } else { /* child */ 37400Sstevel@tonic-gate /* 37410Sstevel@tonic-gate * Perform initialization and return success code down 37420Sstevel@tonic-gate * the pipe. 37430Sstevel@tonic-gate */ 37440Sstevel@tonic-gate (void) close(pipe_fds[PE_CONSUMER]); 37450Sstevel@tonic-gate i = init(); 37460Sstevel@tonic-gate if ((safe_write(pipe_fds[PE_PRODUCER], &i, sizeof (i)) < 0) || 37470Sstevel@tonic-gate (i < 0)) { 37480Sstevel@tonic-gate error_msg(gettext("pipe write failure: %s"), 37490Sstevel@tonic-gate strerror(errno)); 37500Sstevel@tonic-gate exit(1); 37510Sstevel@tonic-gate } 37520Sstevel@tonic-gate (void) close(pipe_fds[PE_PRODUCER]); 37530Sstevel@tonic-gate 37540Sstevel@tonic-gate (void) setsid(); 37550Sstevel@tonic-gate 37560Sstevel@tonic-gate /* 37570Sstevel@tonic-gate * Log a message if the configuration file has changed since 37580Sstevel@tonic-gate * inetconv was last run. 37590Sstevel@tonic-gate */ 37600Sstevel@tonic-gate check_conf_file(); 37610Sstevel@tonic-gate 37620Sstevel@tonic-gate event_loop(); 37630Sstevel@tonic-gate 37640Sstevel@tonic-gate fini(); 37650Sstevel@tonic-gate debug_msg("inetd stopped"); 37660Sstevel@tonic-gate msg_fini(); 37670Sstevel@tonic-gate exit(0); 37680Sstevel@tonic-gate } 37690Sstevel@tonic-gate /* NOTREACHED */ 37700Sstevel@tonic-gate } 37710Sstevel@tonic-gate 37720Sstevel@tonic-gate /* 37730Sstevel@tonic-gate * When inetd is run from outside the SMF, this message is output to provide 37740Sstevel@tonic-gate * the person invoking inetd with further information that will help them 37750Sstevel@tonic-gate * understand how to start and stop inetd, and to achieve the other 37760Sstevel@tonic-gate * behaviors achievable with the legacy inetd command line interface, if 37770Sstevel@tonic-gate * it is possible. 37780Sstevel@tonic-gate */ 37790Sstevel@tonic-gate static void 37800Sstevel@tonic-gate legacy_usage(void) 37810Sstevel@tonic-gate { 37820Sstevel@tonic-gate (void) fprintf(stderr, 37830Sstevel@tonic-gate "inetd is now an smf(5) managed service and can no longer be run " 37840Sstevel@tonic-gate "from the\n" 37850Sstevel@tonic-gate "command line. To enable or disable inetd refer to svcadm(1M) on\n" 37860Sstevel@tonic-gate "how to enable \"%s\", the inetd instance.\n" 37870Sstevel@tonic-gate "\n" 37880Sstevel@tonic-gate "The traditional inetd command line option mappings are:\n" 37890Sstevel@tonic-gate "\t-d : there is no supported debug output\n" 37900Sstevel@tonic-gate "\t-s : inetd is only runnable from within the SMF\n" 37910Sstevel@tonic-gate "\t-t : See inetadm(1M) on how to enable TCP tracing\n" 37920Sstevel@tonic-gate "\t-r : See inetadm(1M) on how to set a failure rate\n" 37930Sstevel@tonic-gate "\n" 37940Sstevel@tonic-gate "To specify an alternative configuration file see svccfg(1M)\n" 37950Sstevel@tonic-gate "for how to modify the \"%s/%s\" string type property of\n" 37960Sstevel@tonic-gate "the inetd instance, and modify it according to the syntax:\n" 37970Sstevel@tonic-gate "\"%s [alt_config_file] %%m\".\n" 37980Sstevel@tonic-gate "\n" 37990Sstevel@tonic-gate "For further information on inetd see inetd(1M).\n", 38000Sstevel@tonic-gate INETD_INSTANCE_FMRI, START_METHOD_ARG, SCF_PROPERTY_EXEC, 38010Sstevel@tonic-gate INETD_PATH); 38020Sstevel@tonic-gate } 38030Sstevel@tonic-gate 38040Sstevel@tonic-gate /* 38050Sstevel@tonic-gate * Usage message printed out for usage errors when running under the SMF. 38060Sstevel@tonic-gate */ 38070Sstevel@tonic-gate static void 38080Sstevel@tonic-gate smf_usage(const char *arg0) 38090Sstevel@tonic-gate { 38100Sstevel@tonic-gate error_msg("Usage: %s [alt_conf_file] %s|%s|%s", arg0, START_METHOD_ARG, 38110Sstevel@tonic-gate STOP_METHOD_ARG, REFRESH_METHOD_ARG); 38120Sstevel@tonic-gate } 38130Sstevel@tonic-gate 38140Sstevel@tonic-gate /* 38150Sstevel@tonic-gate * Returns B_TRUE if we're being run from within the SMF, else B_FALSE. 38160Sstevel@tonic-gate */ 38170Sstevel@tonic-gate static boolean_t 38180Sstevel@tonic-gate run_through_smf(void) 38190Sstevel@tonic-gate { 38200Sstevel@tonic-gate char *fmri; 38210Sstevel@tonic-gate 38220Sstevel@tonic-gate /* 38230Sstevel@tonic-gate * check if the instance fmri environment variable has been set by 38240Sstevel@tonic-gate * our restarter. 38250Sstevel@tonic-gate */ 38260Sstevel@tonic-gate return (((fmri = getenv("SMF_FMRI")) != NULL) && 38270Sstevel@tonic-gate (strcmp(fmri, INETD_INSTANCE_FMRI) == 0)); 38280Sstevel@tonic-gate } 38290Sstevel@tonic-gate 38300Sstevel@tonic-gate int 38310Sstevel@tonic-gate main(int argc, char *argv[]) 38320Sstevel@tonic-gate { 38330Sstevel@tonic-gate char *method; 38340Sstevel@tonic-gate int ret; 38350Sstevel@tonic-gate 38360Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 38370Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 38380Sstevel@tonic-gate #endif 38390Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 38400Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 38410Sstevel@tonic-gate 38420Sstevel@tonic-gate if (!run_through_smf()) { 38430Sstevel@tonic-gate legacy_usage(); 38440Sstevel@tonic-gate return (SMF_EXIT_ERR_NOSMF); 38450Sstevel@tonic-gate } 38460Sstevel@tonic-gate 38470Sstevel@tonic-gate msg_init(); /* setup logging */ 38480Sstevel@tonic-gate 38491914Scasper (void) enable_extended_FILE_stdio(-1, -1); 38501914Scasper 38510Sstevel@tonic-gate /* inetd invocation syntax is inetd [alt_conf_file] method_name */ 38520Sstevel@tonic-gate 38530Sstevel@tonic-gate switch (argc) { 38540Sstevel@tonic-gate case 2: 38550Sstevel@tonic-gate method = argv[1]; 38560Sstevel@tonic-gate break; 38570Sstevel@tonic-gate case 3: 38580Sstevel@tonic-gate conf_file = argv[1]; 38590Sstevel@tonic-gate method = argv[2]; 38600Sstevel@tonic-gate break; 38610Sstevel@tonic-gate default: 38620Sstevel@tonic-gate smf_usage(argv[0]); 38630Sstevel@tonic-gate return (SMF_EXIT_ERR_CONFIG); 38640Sstevel@tonic-gate 38650Sstevel@tonic-gate } 38660Sstevel@tonic-gate 38670Sstevel@tonic-gate if (strcmp(method, START_METHOD_ARG) == 0) { 38680Sstevel@tonic-gate ret = start_method(); 38690Sstevel@tonic-gate } else if (strcmp(method, STOP_METHOD_ARG) == 0) { 38700Sstevel@tonic-gate ret = stop_method(); 38710Sstevel@tonic-gate } else if (strcmp(method, REFRESH_METHOD_ARG) == 0) { 38720Sstevel@tonic-gate ret = refresh_method(); 38730Sstevel@tonic-gate } else { 38740Sstevel@tonic-gate smf_usage(argv[0]); 38750Sstevel@tonic-gate return (SMF_EXIT_ERR_CONFIG); 38760Sstevel@tonic-gate } 38770Sstevel@tonic-gate 38780Sstevel@tonic-gate return (ret); 38790Sstevel@tonic-gate } 3880