17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53ad28c1eSrm  * Common Development and Distribution License (the "License").
63ad28c1eSrm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227b209c2cSacruz  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * NOTES: To be expanded.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * The SMF inetd.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Below are some high level notes of the operation of the SMF inetd. The
327c478bd9Sstevel@tonic-gate  * notes don't go into any real detail, and the viewer of this file is
337c478bd9Sstevel@tonic-gate  * encouraged to look at the code and its associated comments to better
347c478bd9Sstevel@tonic-gate  * understand inetd's operation. This saves the potential for the code
357c478bd9Sstevel@tonic-gate  * and these notes diverging over time.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Inetd's major work is done from the context of event_loop(). Within this
387c478bd9Sstevel@tonic-gate  * loop, inetd polls for events arriving from a number of different file
397c478bd9Sstevel@tonic-gate  * descriptors, representing the following event types, and initiates
407c478bd9Sstevel@tonic-gate  * any necessary event processing:
417c478bd9Sstevel@tonic-gate  * - incoming network connections/datagrams.
427c478bd9Sstevel@tonic-gate  * - notification of terminated processes (discovered via contract events).
437c478bd9Sstevel@tonic-gate  * - instance specific events originating from the SMF master restarter.
447c478bd9Sstevel@tonic-gate  * - stop/refresh requests from the inetd method processes (coming in on a
457c478bd9Sstevel@tonic-gate  *   Unix Domain socket).
467c478bd9Sstevel@tonic-gate  * There's also a timeout set for the poll, which is set to the nearest
477c478bd9Sstevel@tonic-gate  * scheduled timer in a timer queue that inetd uses to perform delayed
487c478bd9Sstevel@tonic-gate  * processing, such as bind retries.
497c478bd9Sstevel@tonic-gate  * The SIGHUP and SIGINT signals can also interrupt the poll, and will
507c478bd9Sstevel@tonic-gate  * result in inetd being refreshed or stopped respectively, as was the
517c478bd9Sstevel@tonic-gate  * behavior with the old inetd.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Inetd implements a state machine for each instance. The states within the
547c478bd9Sstevel@tonic-gate  * machine are: offline, online, disabled, maintenance, uninitialized and
557c478bd9Sstevel@tonic-gate  * specializations of the offline state for when an instance exceeds one of
567c478bd9Sstevel@tonic-gate  * its DOS limits. The state of an instance can be changed as a
577c478bd9Sstevel@tonic-gate  * result/side-effect of one of the above events occurring, or inetd being
587c478bd9Sstevel@tonic-gate  * started up. The ongoing state of an instance is stored in the SMF
597c478bd9Sstevel@tonic-gate  * repository, as required of SMF restarters. This enables an administrator
607c478bd9Sstevel@tonic-gate  * to view the state of each instance, and, if inetd was to terminate
617c478bd9Sstevel@tonic-gate  * unexpectedly, it could use the stored state to re-commence where it left off.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * Within the state machine a number of methods are run (if provided) as part
647c478bd9Sstevel@tonic-gate  * of a state transition to aid/ effect a change in an instance's state. The
657c478bd9Sstevel@tonic-gate  * supported methods are: offline, online, disable, refresh and start. The
667c478bd9Sstevel@tonic-gate  * latter of these is the equivalent of the server program and its arguments
677c478bd9Sstevel@tonic-gate  * in the old inetd.
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * Events from the SMF master restarter come in on a number of threads
707c478bd9Sstevel@tonic-gate  * created in the registration routine of librestart, the delegated restarter
717c478bd9Sstevel@tonic-gate  * library. These threads call into the restart_event_proxy() function
727c478bd9Sstevel@tonic-gate  * when an event arrives. To serialize the processing of instances, these events
737c478bd9Sstevel@tonic-gate  * are then written down a pipe to the process's main thread, which listens
747c478bd9Sstevel@tonic-gate  * for these events via a poll call, with the file descriptor of the other
757c478bd9Sstevel@tonic-gate  * end of the pipe in its read set, and processes the event appropriately.
767c478bd9Sstevel@tonic-gate  * When the event has been  processed (which may be delayed if the instance
777c478bd9Sstevel@tonic-gate  * for which the event is for is in the process of executing one of its methods
787c478bd9Sstevel@tonic-gate  * as part of a state transition) it writes an acknowledgement back down the
797c478bd9Sstevel@tonic-gate  * pipe the event was received on. The thread in restart_event_proxy() that
807c478bd9Sstevel@tonic-gate  * wrote the event will read the acknowledgement it was blocked upon, and will
817c478bd9Sstevel@tonic-gate  * then be able to return to its caller, thus implicitly acknowledging the
827c478bd9Sstevel@tonic-gate  * event, and allowing another event to be written down the pipe for the main
837c478bd9Sstevel@tonic-gate  * thread to process.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #include <netdb.h>
887c478bd9Sstevel@tonic-gate #include <stdio.h>
89004388ebScasper #include <stdio_ext.h>
907c478bd9Sstevel@tonic-gate #include <stdlib.h>
917c478bd9Sstevel@tonic-gate #include <strings.h>
927c478bd9Sstevel@tonic-gate #include <unistd.h>
937c478bd9Sstevel@tonic-gate #include <assert.h>
947c478bd9Sstevel@tonic-gate #include <sys/types.h>
957c478bd9Sstevel@tonic-gate #include <sys/socket.h>
967c478bd9Sstevel@tonic-gate #include <netinet/in.h>
977c478bd9Sstevel@tonic-gate #include <fcntl.h>
987c478bd9Sstevel@tonic-gate #include <signal.h>
997c478bd9Sstevel@tonic-gate #include <errno.h>
1007c478bd9Sstevel@tonic-gate #include <locale.h>
1017c478bd9Sstevel@tonic-gate #include <syslog.h>
1027c478bd9Sstevel@tonic-gate #include <libintl.h>
1037c478bd9Sstevel@tonic-gate #include <librestart.h>
1047c478bd9Sstevel@tonic-gate #include <pthread.h>
1057c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1067c478bd9Sstevel@tonic-gate #include <time.h>
1077c478bd9Sstevel@tonic-gate #include <limits.h>
1087c478bd9Sstevel@tonic-gate #include <libgen.h>
1097c478bd9Sstevel@tonic-gate #include <tcpd.h>
1107c478bd9Sstevel@tonic-gate #include <libscf.h>
1117c478bd9Sstevel@tonic-gate #include <libuutil.h>
1127c478bd9Sstevel@tonic-gate #include <stddef.h>
1137c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
1147cd7ea0bSrs #include <ucred.h>
1157c478bd9Sstevel@tonic-gate #include "inetd_impl.h"
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* path to inetd's binary */
1187c478bd9Sstevel@tonic-gate #define	INETD_PATH	"/usr/lib/inet/inetd"
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * inetd's default configuration file paths. /etc/inetd/inetd.conf is set
1227c478bd9Sstevel@tonic-gate  * be be the primary file, so it is checked before /etc/inetd.conf.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate #define	PRIMARY_DEFAULT_CONF_FILE	"/etc/inet/inetd.conf"
1257c478bd9Sstevel@tonic-gate #define	SECONDARY_DEFAULT_CONF_FILE	"/etc/inetd.conf"
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* Arguments passed to this binary to request which method to execute. */
1287c478bd9Sstevel@tonic-gate #define	START_METHOD_ARG	"start"
1297c478bd9Sstevel@tonic-gate #define	STOP_METHOD_ARG		"stop"
1307c478bd9Sstevel@tonic-gate #define	REFRESH_METHOD_ARG	"refresh"
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* connection backlog for unix domain socket */
1337c478bd9Sstevel@tonic-gate #define	UDS_BACKLOG	2
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /* number of retries to recv() a request on the UDS socket before giving up */
1367c478bd9Sstevel@tonic-gate #define	UDS_RECV_RETRIES	10
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* enumeration of the different ends of a pipe */
1397c478bd9Sstevel@tonic-gate enum pipe_end {
1407c478bd9Sstevel@tonic-gate 	PE_CONSUMER,
1417c478bd9Sstevel@tonic-gate 	PE_PRODUCER
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate typedef struct {
1457c478bd9Sstevel@tonic-gate 	internal_inst_state_t		istate;
1467c478bd9Sstevel@tonic-gate 	const char			*name;
1477c478bd9Sstevel@tonic-gate 	restarter_instance_state_t	smf_state;
1487c478bd9Sstevel@tonic-gate 	instance_method_t		method_running;
1497c478bd9Sstevel@tonic-gate } state_info_t;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Collection of information for each state.
1547c478bd9Sstevel@tonic-gate  * NOTE:  This table is indexed into using the internal_inst_state_t
1557c478bd9Sstevel@tonic-gate  * enumeration, so the ordering needs to be kept in synch.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate static state_info_t states[] = {
1587c478bd9Sstevel@tonic-gate 	{IIS_UNINITIALIZED, "uninitialized", RESTARTER_STATE_UNINIT,
1597c478bd9Sstevel@tonic-gate 	    IM_NONE},
1607c478bd9Sstevel@tonic-gate 	{IIS_ONLINE, "online", RESTARTER_STATE_ONLINE, IM_START},
1617c478bd9Sstevel@tonic-gate 	{IIS_IN_ONLINE_METHOD, "online_method", RESTARTER_STATE_OFFLINE,
1627c478bd9Sstevel@tonic-gate 	    IM_ONLINE},
1637c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE, "offline", RESTARTER_STATE_OFFLINE, IM_NONE},
1647c478bd9Sstevel@tonic-gate 	{IIS_IN_OFFLINE_METHOD, "offline_method", RESTARTER_STATE_OFFLINE,
1657c478bd9Sstevel@tonic-gate 	    IM_OFFLINE},
1667c478bd9Sstevel@tonic-gate 	{IIS_DISABLED, "disabled", RESTARTER_STATE_DISABLED, IM_NONE},
1677c478bd9Sstevel@tonic-gate 	{IIS_IN_DISABLE_METHOD, "disabled_method", RESTARTER_STATE_OFFLINE,
1687c478bd9Sstevel@tonic-gate 	    IM_DISABLE},
1697c478bd9Sstevel@tonic-gate 	{IIS_IN_REFRESH_METHOD, "refresh_method", RESTARTER_STATE_ONLINE,
1707c478bd9Sstevel@tonic-gate 	    IM_REFRESH},
1717c478bd9Sstevel@tonic-gate 	{IIS_MAINTENANCE, "maintenance", RESTARTER_STATE_MAINT, IM_NONE},
1727c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_CONRATE, "cr_offline", RESTARTER_STATE_OFFLINE, IM_NONE},
1737c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_BIND, "bind_offline", RESTARTER_STATE_OFFLINE, IM_NONE},
1747c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_COPIES, "copies_offline", RESTARTER_STATE_OFFLINE,
1757c478bd9Sstevel@tonic-gate 	    IM_NONE},
1767c478bd9Sstevel@tonic-gate 	{IIS_DEGRADED, "degraded", RESTARTER_STATE_DEGRADED, IM_NONE},
1777c478bd9Sstevel@tonic-gate 	{IIS_NONE, "none", RESTARTER_STATE_NONE, IM_NONE}
1787c478bd9Sstevel@tonic-gate };
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * Pipe used to send events from the threads created by restarter_bind_handle()
1827c478bd9Sstevel@tonic-gate  * to the main thread of control.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate static int			rst_event_pipe[] = {-1, -1};
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Used to protect the critical section of code in restarter_event_proxy() that
1877c478bd9Sstevel@tonic-gate  * involves writing an event down the event pipe and reading an acknowledgement.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate static pthread_mutex_t		rst_event_pipe_mtx = PTHREAD_MUTEX_INITIALIZER;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /* handle used in communication with the master restarter */
1927c478bd9Sstevel@tonic-gate static restarter_event_handle_t *rst_event_handle = NULL;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /* set to indicate a refresh of inetd is requested */
1957c478bd9Sstevel@tonic-gate static boolean_t		refresh_inetd_requested = B_FALSE;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /* set by the SIGTERM handler to flag we got a SIGTERM */
1987c478bd9Sstevel@tonic-gate static boolean_t		got_sigterm = B_FALSE;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * Timer queue used to store timers for delayed event processing, such as
2027c478bd9Sstevel@tonic-gate  * bind retries.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate iu_tq_t				*timer_queue = NULL;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * fd of Unix Domain socket used to communicate stop and refresh requests
2087c478bd9Sstevel@tonic-gate  * to the inetd start method process.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate static int			uds_fd = -1;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * List of inetd's currently managed instances; each containing its state,
2147c478bd9Sstevel@tonic-gate  * and in certain states its configuration.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate static uu_list_pool_t		*instance_pool = NULL;
2177c478bd9Sstevel@tonic-gate uu_list_t			*instance_list = NULL;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /* set to indicate we're being stopped */
2207c478bd9Sstevel@tonic-gate boolean_t			inetd_stopping = B_FALSE;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /* TCP wrappers syslog globals. Consumed by libwrap. */
2237c478bd9Sstevel@tonic-gate int				allow_severity = LOG_INFO;
2247c478bd9Sstevel@tonic-gate int				deny_severity = LOG_WARNING;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /* path of the configuration file being monitored by check_conf_file() */
2277c478bd9Sstevel@tonic-gate static char			*conf_file = NULL;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /* Auditing session handle */
2307c478bd9Sstevel@tonic-gate static adt_session_data_t	*audit_handle;
2317c478bd9Sstevel@tonic-gate 
232*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India /* Number of pending connections */
233*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India static size_t			tlx_pending_counter;
234*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 
2357c478bd9Sstevel@tonic-gate static void uds_fini(void);
2367c478bd9Sstevel@tonic-gate static int uds_init(void);
2377c478bd9Sstevel@tonic-gate static int run_method(instance_t *, instance_method_t, const proto_info_t *);
2387c478bd9Sstevel@tonic-gate static void create_bound_fds(instance_t *);
2397c478bd9Sstevel@tonic-gate static void destroy_bound_fds(instance_t *);
2407c478bd9Sstevel@tonic-gate static void destroy_instance(instance_t *);
2417c478bd9Sstevel@tonic-gate static void inetd_stop(void);
242b823d4c7Sdstaff static void
243b823d4c7Sdstaff exec_method(instance_t *instance, instance_method_t method, method_info_t *mi,
244b823d4c7Sdstaff     struct method_context *mthd_ctxt, const proto_info_t *pi) __NORETURN;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * The following two functions are callbacks that libumem uses to determine
2487c478bd9Sstevel@tonic-gate  * inetd's desired debugging/logging levels. The interface they consume is
2497c478bd9Sstevel@tonic-gate  * exported by FMA and is consolidation private. The comments in the two
2507c478bd9Sstevel@tonic-gate  * functions give the environment variable that will effectively be set to
2517c478bd9Sstevel@tonic-gate  * their returned value, and thus whose behavior for this value, described in
2527c478bd9Sstevel@tonic-gate  * umem_debug(3MALLOC), will be followed.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate const char *
2567c478bd9Sstevel@tonic-gate _umem_debug_init(void)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	return ("default,verbose");	/* UMEM_DEBUG setting */
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate const char *
2627c478bd9Sstevel@tonic-gate _umem_logging_init(void)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	return ("fail,contents");	/* UMEM_LOGGING setting */
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static void
2687c478bd9Sstevel@tonic-gate log_invalid_cfg(const char *fmri)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	error_msg(gettext(
2717c478bd9Sstevel@tonic-gate 	    "Invalid configuration for instance %s, placing in maintenance"),
2727c478bd9Sstevel@tonic-gate 	    fmri);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Returns B_TRUE if the instance is in a suitable state for inetd to stop.
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate static boolean_t
2797c478bd9Sstevel@tonic-gate instance_stopped(const instance_t *inst)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	return ((inst->cur_istate == IIS_OFFLINE) ||
2827c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_MAINTENANCE) ||
2837c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_DISABLED) ||
2847c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_UNINITIALIZED));
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate  * Updates the current and next repository states of instance 'inst'. If
2897c478bd9Sstevel@tonic-gate  * any errors occur an error message is output.
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate static void
2927c478bd9Sstevel@tonic-gate update_instance_states(instance_t *inst, internal_inst_state_t new_cur_state,
2937c478bd9Sstevel@tonic-gate     internal_inst_state_t new_next_state, restarter_error_t err)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	internal_inst_state_t	old_cur = inst->cur_istate;
2967c478bd9Sstevel@tonic-gate 	internal_inst_state_t	old_next = inst->next_istate;
2977c478bd9Sstevel@tonic-gate 	scf_error_t		sret;
2987c478bd9Sstevel@tonic-gate 	int			ret;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/* update the repository/cached internal state */
3017c478bd9Sstevel@tonic-gate 	inst->cur_istate = new_cur_state;
3027c478bd9Sstevel@tonic-gate 	inst->next_istate = new_next_state;
3037c478bd9Sstevel@tonic-gate 	(void) set_single_rep_val(inst->cur_istate_rep,
3047c478bd9Sstevel@tonic-gate 	    (int64_t)new_cur_state);
3057c478bd9Sstevel@tonic-gate 	(void) set_single_rep_val(inst->next_istate_rep,
3067c478bd9Sstevel@tonic-gate 	    (int64_t)new_next_state);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (((sret = store_rep_vals(inst->cur_istate_rep, inst->fmri,
3097c478bd9Sstevel@tonic-gate 	    PR_NAME_CUR_INT_STATE)) != 0) ||
3107c478bd9Sstevel@tonic-gate 	    ((sret = store_rep_vals(inst->next_istate_rep, inst->fmri,
3117c478bd9Sstevel@tonic-gate 	    PR_NAME_NEXT_INT_STATE)) != 0))
3127c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to update state of instance %s in "
3137c478bd9Sstevel@tonic-gate 		    "repository: %s"), inst->fmri, scf_strerror(sret));
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/* update the repository SMF state */
3167c478bd9Sstevel@tonic-gate 	if ((ret = restarter_set_states(rst_event_handle, inst->fmri,
3177c478bd9Sstevel@tonic-gate 	    states[old_cur].smf_state, states[new_cur_state].smf_state,
3187c478bd9Sstevel@tonic-gate 	    states[old_next].smf_state, states[new_next_state].smf_state,
3197c478bd9Sstevel@tonic-gate 	    err, 0)) != 0)
3207c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to update state of instance %s in "
3217c478bd9Sstevel@tonic-gate 		    "repository: %s"), inst->fmri, strerror(ret));
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate void
3267c478bd9Sstevel@tonic-gate update_state(instance_t *inst, internal_inst_state_t new_cur,
3277c478bd9Sstevel@tonic-gate     restarter_error_t err)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	update_instance_states(inst, new_cur, IIS_NONE, err);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * Sends a refresh event to the inetd start method process and returns
3347c478bd9Sstevel@tonic-gate  * SMF_EXIT_OK if it managed to send it. If it fails to send the request for
3357c478bd9Sstevel@tonic-gate  * some reason it returns SMF_EXIT_ERR_OTHER.
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate static int
3387c478bd9Sstevel@tonic-gate refresh_method(void)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	uds_request_t   req = UR_REFRESH_INETD;
3417c478bd9Sstevel@tonic-gate 	int		fd;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if ((fd = connect_to_inetd()) < 0) {
3447c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to connect to inetd: %s"),
3457c478bd9Sstevel@tonic-gate 		    strerror(errno));
3467c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* write the request and return success */
3507c478bd9Sstevel@tonic-gate 	if (safe_write(fd, &req, sizeof (req)) == -1) {
3517c478bd9Sstevel@tonic-gate 		error_msg(
3527c478bd9Sstevel@tonic-gate 		    gettext("Failed to send refresh request to inetd: %s"),
3537c478bd9Sstevel@tonic-gate 		    strerror(errno));
3547c478bd9Sstevel@tonic-gate 		(void) close(fd);
3557c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	(void) close(fd);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	return (SMF_EXIT_OK);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * Sends a stop event to the inetd start method process and wait till it goes
3657c478bd9Sstevel@tonic-gate  * away. If inetd is determined to have stopped SMF_EXIT_OK is returned, else
3667c478bd9Sstevel@tonic-gate  * SMF_EXIT_ERR_OTHER is returned.
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate static int
3697c478bd9Sstevel@tonic-gate stop_method(void)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	uds_request_t   req = UR_STOP_INETD;
3727c478bd9Sstevel@tonic-gate 	int		fd;
3737c478bd9Sstevel@tonic-gate 	char		c;
3747c478bd9Sstevel@tonic-gate 	ssize_t		ret;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if ((fd = connect_to_inetd()) == -1) {
3777c478bd9Sstevel@tonic-gate 		debug_msg(gettext("Failed to connect to inetd: %s"),
3787c478bd9Sstevel@tonic-gate 		    strerror(errno));
3797c478bd9Sstevel@tonic-gate 		/*
3807c478bd9Sstevel@tonic-gate 		 * Assume connect_to_inetd() failed because inetd was already
3817c478bd9Sstevel@tonic-gate 		 * stopped, and return success.
3827c478bd9Sstevel@tonic-gate 		 */
3837c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_OK);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * This is safe to do since we're fired off in a separate process
3887c478bd9Sstevel@tonic-gate 	 * than inetd and in the case we get wedged, the stop method timeout
3897c478bd9Sstevel@tonic-gate 	 * will occur and we'd be killed by our restarter.
3907c478bd9Sstevel@tonic-gate 	 */
3917c478bd9Sstevel@tonic-gate 	enable_blocking(fd);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* write the stop request to inetd and wait till it goes away */
3947c478bd9Sstevel@tonic-gate 	if (safe_write(fd, &req, sizeof (req)) != 0) {
3957c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to send stop request to inetd"));
3967c478bd9Sstevel@tonic-gate 		(void) close(fd);
3977c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/* wait until remote end of socket is closed */
4017c478bd9Sstevel@tonic-gate 	while (((ret = recv(fd, &c, sizeof (c), 0)) != 0) && (errno == EINTR))
4027c478bd9Sstevel@tonic-gate 		;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	(void) close(fd);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (ret != 0) {
4077c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to determine whether inetd stopped"));
4087c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	return (SMF_EXIT_OK);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * This function is called to handle restarter events coming in from the
4177c478bd9Sstevel@tonic-gate  * master restarter. It is registered with the master restarter via
4187c478bd9Sstevel@tonic-gate  * restarter_bind_handle() and simply passes a pointer to the event down
4197c478bd9Sstevel@tonic-gate  * the event pipe, which will be discovered by the poll in the event loop
4207c478bd9Sstevel@tonic-gate  * and processed there. It waits for an acknowledgement to be written back down
4217c478bd9Sstevel@tonic-gate  * the pipe before returning.
4227c478bd9Sstevel@tonic-gate  * Writing a pointer to the function's 'event' parameter down the pipe will
4237c478bd9Sstevel@tonic-gate  * be safe, as the thread in restarter_event_proxy() doesn't return until
4247c478bd9Sstevel@tonic-gate  * the main thread has finished its processing of the passed event, thus
4257c478bd9Sstevel@tonic-gate  * the referenced event will remain around until the function returns.
4267c478bd9Sstevel@tonic-gate  * To impose the limit of only one event being in the pipe and processed
4277c478bd9Sstevel@tonic-gate  * at once, a lock is taken on entry to this function and returned on exit.
4287c478bd9Sstevel@tonic-gate  * Always returns 0.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate static int
4317c478bd9Sstevel@tonic-gate restarter_event_proxy(restarter_event_t *event)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	boolean_t		processed;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&rst_event_pipe_mtx);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/* write the event to the main worker thread down the pipe */
4387c478bd9Sstevel@tonic-gate 	if (safe_write(rst_event_pipe[PE_PRODUCER], &event,
4397c478bd9Sstevel@tonic-gate 	    sizeof (event)) != 0)
4407c478bd9Sstevel@tonic-gate 		goto pipe_error;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * Wait for an acknowledgement that the event has been processed from
4447c478bd9Sstevel@tonic-gate 	 * the same pipe. In the case that inetd is stopping, any thread in
4457c478bd9Sstevel@tonic-gate 	 * this function will simply block on this read until inetd eventually
4467c478bd9Sstevel@tonic-gate 	 * exits. This will result in this function not returning success to
4477c478bd9Sstevel@tonic-gate 	 * its caller, and the event that was being processed when the
4487c478bd9Sstevel@tonic-gate 	 * function exited will be re-sent when inetd is next started.
4497c478bd9Sstevel@tonic-gate 	 */
4507c478bd9Sstevel@tonic-gate 	if (safe_read(rst_event_pipe[PE_PRODUCER], &processed,
4517c478bd9Sstevel@tonic-gate 	    sizeof (processed)) != 0)
4527c478bd9Sstevel@tonic-gate 		goto pipe_error;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&rst_event_pipe_mtx);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (processed ? 0 : EAGAIN);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate pipe_error:
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Something's seriously wrong with the event pipe. Notify the
4617c478bd9Sstevel@tonic-gate 	 * worker thread by closing this end of the event pipe and pause till
4627c478bd9Sstevel@tonic-gate 	 * inetd exits.
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	error_msg(gettext("Can't process restarter events: %s"),
4657c478bd9Sstevel@tonic-gate 	    strerror(errno));
4667c478bd9Sstevel@tonic-gate 	(void) close(rst_event_pipe[PE_PRODUCER]);
4677c478bd9Sstevel@tonic-gate 	for (;;)
4687c478bd9Sstevel@tonic-gate 		(void) pause();
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Let restarter_event_proxy() know we're finished with the event it's blocked
4757c478bd9Sstevel@tonic-gate  * upon. The 'processed' argument denotes whether we successfully processed the
4767c478bd9Sstevel@tonic-gate  * event.
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate static void
4797c478bd9Sstevel@tonic-gate ack_restarter_event(boolean_t processed)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * If safe_write returns -1 something's seriously wrong with the event
4837c478bd9Sstevel@tonic-gate 	 * pipe, so start the shutdown proceedings.
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	if (safe_write(rst_event_pipe[PE_CONSUMER], &processed,
4867c478bd9Sstevel@tonic-gate 	    sizeof (processed)) == -1)
4877c478bd9Sstevel@tonic-gate 		inetd_stop();
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate  * Switch the syslog identification string to 'ident'.
4927c478bd9Sstevel@tonic-gate  */
4937c478bd9Sstevel@tonic-gate static void
4947c478bd9Sstevel@tonic-gate change_syslog_ident(const char *ident)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	closelog();
4977c478bd9Sstevel@tonic-gate 	openlog(ident, LOG_PID|LOG_CONS, LOG_DAEMON);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Perform TCP wrappers checks on this instance. Due to the fact that the
5027c478bd9Sstevel@tonic-gate  * current wrappers code used in Solaris is taken untouched from the open
5037c478bd9Sstevel@tonic-gate  * source version, we're stuck with using the daemon name for the checks, as
5047c478bd9Sstevel@tonic-gate  * opposed to making use of instance FMRIs. Sigh.
5057c478bd9Sstevel@tonic-gate  * Returns B_TRUE if the check passed, else B_FALSE.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate static boolean_t
5087c478bd9Sstevel@tonic-gate tcp_wrappers_ok(instance_t *instance)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	boolean_t		rval = B_TRUE;
5117c478bd9Sstevel@tonic-gate 	char			*daemon_name;
5127c478bd9Sstevel@tonic-gate 	basic_cfg_t		*cfg = instance->config->basic;
5137c478bd9Sstevel@tonic-gate 	struct request_info	req;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/*
5167c478bd9Sstevel@tonic-gate 	 * Wrap the service using libwrap functions. The code below implements
5177c478bd9Sstevel@tonic-gate 	 * the functionality of tcpd. This is done only for stream,nowait
5187c478bd9Sstevel@tonic-gate 	 * services, following the convention of other vendors.  udp/dgram and
5197c478bd9Sstevel@tonic-gate 	 * stream/wait can NOT be wrapped with this libwrap, so be wary of
5207c478bd9Sstevel@tonic-gate 	 * changing the test below.
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	if (cfg->do_tcp_wrappers && !cfg->iswait && !cfg->istlx) {
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		daemon_name = instance->config->methods[
5257c478bd9Sstevel@tonic-gate 		    IM_START]->exec_args_we.we_wordv[0];
5267c478bd9Sstevel@tonic-gate 		if (*daemon_name == '/')
5277c478bd9Sstevel@tonic-gate 			daemon_name = strrchr(daemon_name, '/') + 1;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		/*
5307c478bd9Sstevel@tonic-gate 		 * Change the syslog message identity to the name of the
5317c478bd9Sstevel@tonic-gate 		 * daemon being wrapped, as opposed to "inetd".
5327c478bd9Sstevel@tonic-gate 		 */
5337c478bd9Sstevel@tonic-gate 		change_syslog_ident(daemon_name);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		(void) request_init(&req, RQ_DAEMON, daemon_name, RQ_FILE,
5367c478bd9Sstevel@tonic-gate 		    instance->conn_fd, NULL);
5377c478bd9Sstevel@tonic-gate 		fromhost(&req);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		if (strcasecmp(eval_hostname(req.client), paranoid) == 0) {
5407c478bd9Sstevel@tonic-gate 			syslog(deny_severity,
5417c478bd9Sstevel@tonic-gate 			    "refused connect from %s (name/address mismatch)",
5427c478bd9Sstevel@tonic-gate 			    eval_client(&req));
5437c478bd9Sstevel@tonic-gate 			if (req.sink != NULL)
5447c478bd9Sstevel@tonic-gate 				req.sink(instance->conn_fd);
5457c478bd9Sstevel@tonic-gate 			rval = B_FALSE;
5467c478bd9Sstevel@tonic-gate 		} else if (!hosts_access(&req)) {
5477c478bd9Sstevel@tonic-gate 			syslog(deny_severity,
5487c478bd9Sstevel@tonic-gate 			    "refused connect from %s (access denied)",
5497c478bd9Sstevel@tonic-gate 			    eval_client(&req));
5507c478bd9Sstevel@tonic-gate 			if (req.sink != NULL)
5517c478bd9Sstevel@tonic-gate 				req.sink(instance->conn_fd);
5527c478bd9Sstevel@tonic-gate 			rval = B_FALSE;
5537c478bd9Sstevel@tonic-gate 		} else {
5547c478bd9Sstevel@tonic-gate 			syslog(allow_severity, "connect from %s",
5557c478bd9Sstevel@tonic-gate 			    eval_client(&req));
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		/* Revert syslog identity back to "inetd". */
5597c478bd9Sstevel@tonic-gate 		change_syslog_ident(SYSLOG_IDENT);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	return (rval);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Handler registered with the timer queue code to remove an instance from
5667c478bd9Sstevel@tonic-gate  * the connection rate offline state when it has been there for its allotted
5677c478bd9Sstevel@tonic-gate  * time.
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate /* ARGSUSED */
5707c478bd9Sstevel@tonic-gate static void
5717c478bd9Sstevel@tonic-gate conn_rate_online(iu_tq_t *tq, void *arg)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	instance_t *instance = arg;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	assert(instance->cur_istate == IIS_OFFLINE_CONRATE);
5767c478bd9Sstevel@tonic-gate 	instance->timer_id = -1;
5777c478bd9Sstevel@tonic-gate 	update_state(instance, IIS_OFFLINE, RERR_RESTART);
5787c478bd9Sstevel@tonic-gate 	process_offline_inst(instance);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * Check whether this instance in the offline state is in transition to
5837c478bd9Sstevel@tonic-gate  * another state and do the work to continue this transition.
5847c478bd9Sstevel@tonic-gate  */
5857c478bd9Sstevel@tonic-gate void
5867c478bd9Sstevel@tonic-gate process_offline_inst(instance_t *inst)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	if (inst->disable_req) {
5897c478bd9Sstevel@tonic-gate 		inst->disable_req = B_FALSE;
5907c478bd9Sstevel@tonic-gate 		(void) run_method(inst, IM_DISABLE, NULL);
5917c478bd9Sstevel@tonic-gate 	} else if (inst->maintenance_req) {
5927c478bd9Sstevel@tonic-gate 		inst->maintenance_req = B_FALSE;
5937c478bd9Sstevel@tonic-gate 		update_state(inst, IIS_MAINTENANCE, RERR_RESTART);
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * If inetd is in the process of stopping, we don't want to enter
5967c478bd9Sstevel@tonic-gate 	 * any states but offline, disabled and maintenance.
5977c478bd9Sstevel@tonic-gate 	 */
5987c478bd9Sstevel@tonic-gate 	} else if (!inetd_stopping) {
5997c478bd9Sstevel@tonic-gate 		if (inst->conn_rate_exceeded) {
6007c478bd9Sstevel@tonic-gate 			basic_cfg_t *cfg = inst->config->basic;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 			inst->conn_rate_exceeded = B_FALSE;
6037c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE_CONRATE, RERR_RESTART);
6047c478bd9Sstevel@tonic-gate 			/*
6057c478bd9Sstevel@tonic-gate 			 * Schedule a timer to bring the instance out of the
6067c478bd9Sstevel@tonic-gate 			 * connection rate offline state.
6077c478bd9Sstevel@tonic-gate 			 */
6087c478bd9Sstevel@tonic-gate 			inst->timer_id = iu_schedule_timer(timer_queue,
6097c478bd9Sstevel@tonic-gate 			    cfg->conn_rate_offline, conn_rate_online,
6107c478bd9Sstevel@tonic-gate 			    inst);
6117c478bd9Sstevel@tonic-gate 			if (inst->timer_id == -1) {
6127c478bd9Sstevel@tonic-gate 				error_msg(gettext("%s unable to set timer, "
6137c478bd9Sstevel@tonic-gate 				    "won't be brought on line after %d "
6147c478bd9Sstevel@tonic-gate 				    "seconds."), inst->fmri,
6157c478bd9Sstevel@tonic-gate 				    cfg->conn_rate_offline);
6167c478bd9Sstevel@tonic-gate 			}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		} else if (copies_limit_exceeded(inst)) {
6197c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE_COPIES, RERR_RESTART);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Create a socket bound to the instance's configured address. If the
6267c478bd9Sstevel@tonic-gate  * bind fails, returns -1, else the fd of the bound socket.
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate static int
629fff9db26Svp create_bound_socket(const instance_t *inst, socket_info_t *sock_info)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	int		fd;
6327c478bd9Sstevel@tonic-gate 	int		on = 1;
633fff9db26Svp 	const char	*fmri = inst->fmri;
6347c478bd9Sstevel@tonic-gate 	rpc_info_t	*rpc = sock_info->pr_info.ri;
6357c478bd9Sstevel@tonic-gate 	const char	*proto = sock_info->pr_info.proto;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	fd = socket(sock_info->local_addr.ss_family, sock_info->type,
6387c478bd9Sstevel@tonic-gate 	    sock_info->protocol);
6397c478bd9Sstevel@tonic-gate 	if (fd < 0) {
6407c478bd9Sstevel@tonic-gate 		error_msg(gettext(
6417c478bd9Sstevel@tonic-gate 		    "Socket creation failure for instance %s, proto %s: %s"),
6427c478bd9Sstevel@tonic-gate 		    fmri, proto, strerror(errno));
6437c478bd9Sstevel@tonic-gate 		return (-1);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) == -1) {
6477c478bd9Sstevel@tonic-gate 		error_msg(gettext("setsockopt SO_REUSEADDR failed for service "
6487c478bd9Sstevel@tonic-gate 		    "instance %s, proto %s: %s"), fmri, proto, strerror(errno));
6497c478bd9Sstevel@tonic-gate 		(void) close(fd);
6507c478bd9Sstevel@tonic-gate 		return (-1);
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 	if (sock_info->pr_info.v6only) {
6537c478bd9Sstevel@tonic-gate 		/* restrict socket to IPv6 communications only */
6547c478bd9Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
6557c478bd9Sstevel@tonic-gate 		    sizeof (on)) == -1) {
6567c478bd9Sstevel@tonic-gate 			error_msg(gettext("setsockopt IPV6_V6ONLY failed for "
6577c478bd9Sstevel@tonic-gate 			    "service instance %s, proto %s: %s"), fmri, proto,
6587c478bd9Sstevel@tonic-gate 			    strerror(errno));
6597c478bd9Sstevel@tonic-gate 			(void) close(fd);
6607c478bd9Sstevel@tonic-gate 			return (-1);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	if (rpc != NULL)
6657c478bd9Sstevel@tonic-gate 		SS_SETPORT(sock_info->local_addr, 0);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (bind(fd, (struct sockaddr *)&(sock_info->local_addr),
6687c478bd9Sstevel@tonic-gate 	    SS_ADDRLEN(sock_info->local_addr)) < 0) {
6697c478bd9Sstevel@tonic-gate 		error_msg(gettext(
6707c478bd9Sstevel@tonic-gate 		    "Failed to bind to the port of service instance %s, "
6717c478bd9Sstevel@tonic-gate 		    "proto %s: %s"), fmri, proto, strerror(errno));
6727c478bd9Sstevel@tonic-gate 		(void) close(fd);
6737c478bd9Sstevel@tonic-gate 		return (-1);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * Retrieve and store the address bound to for RPC services.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	if (rpc != NULL) {
6807c478bd9Sstevel@tonic-gate 		struct sockaddr_storage	ss;
6817c478bd9Sstevel@tonic-gate 		int			ss_size = sizeof (ss);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		if (getsockname(fd, (struct sockaddr *)&ss, &ss_size) < 0) {
6847c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed getsockname for instance %s, "
6857c478bd9Sstevel@tonic-gate 			    "proto %s: %s"), fmri, proto, strerror(errno));
6867c478bd9Sstevel@tonic-gate 			(void) close(fd);
6877c478bd9Sstevel@tonic-gate 			return (-1);
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 		(void) memcpy(rpc->netbuf.buf, &ss,
6907c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_storage));
6917c478bd9Sstevel@tonic-gate 		rpc->netbuf.len = SS_ADDRLEN(ss);
6927c478bd9Sstevel@tonic-gate 		rpc->netbuf.maxlen = SS_ADDRLEN(ss);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
695fff9db26Svp 	if (sock_info->type == SOCK_STREAM) {
696fff9db26Svp 		int qlen = inst->config->basic->conn_backlog;
697fff9db26Svp 
698fff9db26Svp 		debug_msg("Listening for service %s with backlog queue"
699fff9db26Svp 		    " size %d", fmri, qlen);
700fff9db26Svp 		(void) listen(fd, qlen);
701fff9db26Svp 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	return (fd);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate /*
7077c478bd9Sstevel@tonic-gate  * Handler registered with the timer queue code to retry the creation
7087c478bd9Sstevel@tonic-gate  * of a bound fd.
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate /* ARGSUSED */
7117c478bd9Sstevel@tonic-gate static void
7127c478bd9Sstevel@tonic-gate retry_bind(iu_tq_t *tq, void *arg)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	instance_t *instance = arg;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
7177c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
7187c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
7197c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
7207c478bd9Sstevel@tonic-gate 	case IIS_IN_ONLINE_METHOD:
7217c478bd9Sstevel@tonic-gate 	case IIS_IN_REFRESH_METHOD:
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 	default:
7247c478bd9Sstevel@tonic-gate #ifndef NDEBUG
7257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n",
7267c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, instance->cur_istate);
7277c478bd9Sstevel@tonic-gate #endif
7287c478bd9Sstevel@tonic-gate 		abort();
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	instance->bind_timer_id = -1;
7327c478bd9Sstevel@tonic-gate 	create_bound_fds(instance);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate  * For each of the fds for the given instance that are bound, if 'listen' is
7377c478bd9Sstevel@tonic-gate  * set add them to the poll set, else remove them from it. If any additions
7387c478bd9Sstevel@tonic-gate  * fail, returns -1, else 0 on success.
7397c478bd9Sstevel@tonic-gate  */
7407c478bd9Sstevel@tonic-gate int
7417c478bd9Sstevel@tonic-gate poll_bound_fds(instance_t *instance, boolean_t listen)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
7447c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
7457c478bd9Sstevel@tonic-gate 	int		ret = 0;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
7487c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
7497c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1) {	/* fd bound */
7507c478bd9Sstevel@tonic-gate 			if (!listen) {
7517c478bd9Sstevel@tonic-gate 				clear_pollfd(pi->listen_fd);
7527c478bd9Sstevel@tonic-gate 			} else if (set_pollfd(pi->listen_fd, POLLIN) == -1) {
7537c478bd9Sstevel@tonic-gate 				ret = -1;
7547c478bd9Sstevel@tonic-gate 			}
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	return (ret);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate /*
7627c478bd9Sstevel@tonic-gate  * Handle the case were we either fail to create a bound fd or we fail
7637c478bd9Sstevel@tonic-gate  * to add a bound fd to the poll set for the given instance.
7647c478bd9Sstevel@tonic-gate  */
7657c478bd9Sstevel@tonic-gate static void
7667c478bd9Sstevel@tonic-gate handle_bind_failure(instance_t *instance)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	basic_cfg_t *cfg = instance->config->basic;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 * We must be being called as a result of a failed poll_bound_fds()
7727c478bd9Sstevel@tonic-gate 	 * as a bind retry is already scheduled. Just return and let it do
7737c478bd9Sstevel@tonic-gate 	 * the work.
7747c478bd9Sstevel@tonic-gate 	 */
7757c478bd9Sstevel@tonic-gate 	if (instance->bind_timer_id != -1)
7767c478bd9Sstevel@tonic-gate 		return;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/*
7797c478bd9Sstevel@tonic-gate 	 * Check if the rebind retries limit is operative and if so,
7807c478bd9Sstevel@tonic-gate 	 * if it has been reached.
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	if (((cfg->bind_fail_interval <= 0) ||		/* no retries */
7837c478bd9Sstevel@tonic-gate 	    ((cfg->bind_fail_max >= 0) &&		/* limit reached */
7847c478bd9Sstevel@tonic-gate 	    (++instance->bind_fail_count > cfg->bind_fail_max))) ||
7857c478bd9Sstevel@tonic-gate 	    ((instance->bind_timer_id = iu_schedule_timer(timer_queue,
7867c478bd9Sstevel@tonic-gate 	    cfg->bind_fail_interval, retry_bind, instance)) == -1)) {
7877c478bd9Sstevel@tonic-gate 		proto_info_t *pi;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		instance->bind_fail_count = 0;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
7927c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
7937c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
7947c478bd9Sstevel@tonic-gate 			/* check if any of the fds are being poll'd upon */
7957c478bd9Sstevel@tonic-gate 			for (pi = uu_list_first(cfg->proto_list); pi != NULL;
7967c478bd9Sstevel@tonic-gate 			    pi = uu_list_next(cfg->proto_list, pi)) {
7977c478bd9Sstevel@tonic-gate 				if ((pi->listen_fd != -1) &&
7987c478bd9Sstevel@tonic-gate 				    (find_pollfd(pi->listen_fd) != NULL))
7997c478bd9Sstevel@tonic-gate 					break;
8007c478bd9Sstevel@tonic-gate 			}
8017c478bd9Sstevel@tonic-gate 			if (pi != NULL)	{	/* polling on > 0 fds */
8027c478bd9Sstevel@tonic-gate 				warn_msg(gettext("Failed to bind on "
8037c478bd9Sstevel@tonic-gate 				    "all protocols for instance %s, "
8047c478bd9Sstevel@tonic-gate 				    "transitioning to degraded"),
8057c478bd9Sstevel@tonic-gate 				    instance->fmri);
8067c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_DEGRADED, RERR_NONE);
8077c478bd9Sstevel@tonic-gate 				instance->bind_retries_exceeded = B_TRUE;
8087c478bd9Sstevel@tonic-gate 				break;
8097c478bd9Sstevel@tonic-gate 			}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
8127c478bd9Sstevel@tonic-gate 			/*
8137c478bd9Sstevel@tonic-gate 			 * In the case we failed the 'bind' because set_pollfd()
8147c478bd9Sstevel@tonic-gate 			 * failed on all bound fds, use the offline handling.
8157c478bd9Sstevel@tonic-gate 			 */
8167c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
8177c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE:
8187c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_BIND:
8197c478bd9Sstevel@tonic-gate 			error_msg(gettext("Too many bind failures for instance "
8207c478bd9Sstevel@tonic-gate 			"%s, transitioning to maintenance"), instance->fmri);
8217c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE,
8227c478bd9Sstevel@tonic-gate 			    RERR_FAULT);
8237c478bd9Sstevel@tonic-gate 			break;
8247c478bd9Sstevel@tonic-gate 		case IIS_IN_ONLINE_METHOD:
8257c478bd9Sstevel@tonic-gate 		case IIS_IN_REFRESH_METHOD:
8267c478bd9Sstevel@tonic-gate 			warn_msg(gettext("Failed to bind on all "
8277c478bd9Sstevel@tonic-gate 			    "protocols for instance %s, instance will go to "
8287c478bd9Sstevel@tonic-gate 			    "degraded"), instance->fmri);
8297c478bd9Sstevel@tonic-gate 			/*
8307c478bd9Sstevel@tonic-gate 			 * Set the retries exceeded flag so when the method
8317c478bd9Sstevel@tonic-gate 			 * completes the instance goes to the degraded state.
8327c478bd9Sstevel@tonic-gate 			 */
8337c478bd9Sstevel@tonic-gate 			instance->bind_retries_exceeded = B_TRUE;
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 		default:
8367c478bd9Sstevel@tonic-gate #ifndef NDEBUG
8377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8387c478bd9Sstevel@tonic-gate 			    "%s:%d: Unknown instance state %d.\n",
8397c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, instance->cur_istate);
8407c478bd9Sstevel@tonic-gate #endif
8417c478bd9Sstevel@tonic-gate 			abort();
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 	} else if (instance->cur_istate == IIS_OFFLINE) {
8447c478bd9Sstevel@tonic-gate 		/*
8457c478bd9Sstevel@tonic-gate 		 * bind re-scheduled, so if we're offline reflect this in the
8467c478bd9Sstevel@tonic-gate 		 * state.
8477c478bd9Sstevel@tonic-gate 		 */
8487c478bd9Sstevel@tonic-gate 		update_state(instance, IIS_OFFLINE_BIND, RERR_NONE);
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
852b823d4c7Sdstaff 
853b823d4c7Sdstaff /*
854513ad6b4Sdstaff  * Check if two transport protocols for RPC conflict.
855b823d4c7Sdstaff  */
856b823d4c7Sdstaff 
857b823d4c7Sdstaff boolean_t
858b823d4c7Sdstaff is_rpc_proto_conflict(const char *proto0, const char *proto1) {
859b823d4c7Sdstaff 	if (strcmp(proto0, "tcp") == 0) {
860b823d4c7Sdstaff 		if (strcmp(proto1, "tcp") == 0)
861b823d4c7Sdstaff 			return (B_TRUE);
862b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6") == 0)
863b823d4c7Sdstaff 			return (B_TRUE);
864b823d4c7Sdstaff 		return (B_FALSE);
865b823d4c7Sdstaff 	}
866b823d4c7Sdstaff 
867b823d4c7Sdstaff 	if (strcmp(proto0, "tcp6") == 0) {
868b823d4c7Sdstaff 		if (strcmp(proto1, "tcp") == 0)
869b823d4c7Sdstaff 			return (B_TRUE);
870b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6only") == 0)
871b823d4c7Sdstaff 			return (B_TRUE);
872b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6") == 0)
873b823d4c7Sdstaff 			return (B_TRUE);
874b823d4c7Sdstaff 		return (B_FALSE);
875b823d4c7Sdstaff 	}
876b823d4c7Sdstaff 
877b823d4c7Sdstaff 	if (strcmp(proto0, "tcp6only") == 0) {
878b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6only") == 0)
879b823d4c7Sdstaff 			return (B_TRUE);
880b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6") == 0)
881b823d4c7Sdstaff 			return (B_TRUE);
882b823d4c7Sdstaff 		return (B_FALSE);
883b823d4c7Sdstaff 	}
884b823d4c7Sdstaff 
885b823d4c7Sdstaff 	if (strcmp(proto0, "udp") == 0) {
886b823d4c7Sdstaff 		if (strcmp(proto1, "udp") == 0)
887b823d4c7Sdstaff 			return (B_TRUE);
888b823d4c7Sdstaff 		if (strcmp(proto1, "udp6") == 0)
889b823d4c7Sdstaff 			return (B_TRUE);
890b823d4c7Sdstaff 		return (B_FALSE);
891b823d4c7Sdstaff 	}
892b823d4c7Sdstaff 
893b823d4c7Sdstaff 	if (strcmp(proto0, "udp6") == 0) {
894b823d4c7Sdstaff 
895b823d4c7Sdstaff 		if (strcmp(proto1, "udp") == 0)
896b823d4c7Sdstaff 			return (B_TRUE);
897b823d4c7Sdstaff 		if (strcmp(proto1, "udp6only") == 0)
898b823d4c7Sdstaff 			return (B_TRUE);
899b823d4c7Sdstaff 		if (strcmp(proto1, "udp6") == 0)
900b823d4c7Sdstaff 			return (B_TRUE);
901b823d4c7Sdstaff 		return (B_FALSE);
902b823d4c7Sdstaff 	}
903b823d4c7Sdstaff 
904b823d4c7Sdstaff 	if (strcmp(proto0, "udp6only") == 0) {
905b823d4c7Sdstaff 
906b823d4c7Sdstaff 		if (strcmp(proto1, "udp6only") == 0)
907b823d4c7Sdstaff 			return (B_TRUE);
908b823d4c7Sdstaff 		if (strcmp(proto1, "udp6") == 0)
909b823d4c7Sdstaff 			return (B_TRUE);
910b823d4c7Sdstaff 		return (0);
911b823d4c7Sdstaff 	}
912b823d4c7Sdstaff 
913b823d4c7Sdstaff 	/*
914b823d4c7Sdstaff 	 * If the protocol isn't TCP/IP or UDP/IP assume that it has its own
915b823d4c7Sdstaff 	 * port namepsace and that conflicts can be detected by literal string
916b823d4c7Sdstaff 	 * comparison.
917b823d4c7Sdstaff 	 */
918b823d4c7Sdstaff 
919b823d4c7Sdstaff 	if (strcmp(proto0, proto1))
920b823d4c7Sdstaff 		return (FALSE);
921b823d4c7Sdstaff 
922b823d4c7Sdstaff 	return (B_TRUE);
923b823d4c7Sdstaff }
924b823d4c7Sdstaff 
925b823d4c7Sdstaff 
926b823d4c7Sdstaff /*
927b823d4c7Sdstaff  * Check if inetd thinks this RPC program number is already registered.
928b823d4c7Sdstaff  *
929b823d4c7Sdstaff  * An RPC protocol conflict occurs if
930b823d4c7Sdstaff  * 	a) the program numbers are the same and,
931b823d4c7Sdstaff  * 	b) the version numbers overlap,
932b823d4c7Sdstaff  * 	c) the protocols (TCP vs UDP vs tic*) are the same.
933b823d4c7Sdstaff  */
934b823d4c7Sdstaff 
935b823d4c7Sdstaff boolean_t
936b823d4c7Sdstaff is_rpc_num_in_use(int rpc_n, char *proto, int lowver, int highver) {
937b823d4c7Sdstaff 	instance_t *i;
938b823d4c7Sdstaff 	basic_cfg_t *cfg;
939b823d4c7Sdstaff 	proto_info_t *pi;
940b823d4c7Sdstaff 
941b823d4c7Sdstaff 	for (i = uu_list_first(instance_list); i != NULL;
942b823d4c7Sdstaff 	    i = uu_list_next(instance_list, i)) {
943b823d4c7Sdstaff 
944b823d4c7Sdstaff 		if (i->cur_istate != IIS_ONLINE)
945b823d4c7Sdstaff 			continue;
9467778f43aSdstaff 		cfg = i->config->basic;
947b823d4c7Sdstaff 
948b823d4c7Sdstaff 		for (pi = uu_list_first(cfg->proto_list); pi != NULL;
949b823d4c7Sdstaff 		    pi = uu_list_next(cfg->proto_list, pi)) {
950b823d4c7Sdstaff 
951b823d4c7Sdstaff 			if (pi->ri == NULL)
952b823d4c7Sdstaff 				continue;
953b823d4c7Sdstaff 			if (pi->ri->prognum != rpc_n)
954b823d4c7Sdstaff 				continue;
955513ad6b4Sdstaff 			if (!is_rpc_proto_conflict(pi->proto, proto))
956b823d4c7Sdstaff 				continue;
957b823d4c7Sdstaff 			if ((lowver < pi->ri->lowver &&
958b823d4c7Sdstaff 			    highver < pi->ri->lowver) ||
959b823d4c7Sdstaff 			    (lowver > pi->ri->highver &&
960b823d4c7Sdstaff 			    highver > pi->ri->highver))
961b823d4c7Sdstaff 				continue;
962b823d4c7Sdstaff 			return (B_TRUE);
963b823d4c7Sdstaff 		}
964b823d4c7Sdstaff 	}
965b823d4c7Sdstaff 	return (B_FALSE);
966b823d4c7Sdstaff }
967b823d4c7Sdstaff 
968b823d4c7Sdstaff 
9697c478bd9Sstevel@tonic-gate /*
9707c478bd9Sstevel@tonic-gate  * Independent of the transport, for each of the entries in the instance's
9717c478bd9Sstevel@tonic-gate  * proto list this function first attempts to create an associated network fd;
9727c478bd9Sstevel@tonic-gate  * for RPC services these are then bound to a kernel chosen port and the
9737c478bd9Sstevel@tonic-gate  * fd is registered with rpcbind; for non-RPC services the fds are bound
9747c478bd9Sstevel@tonic-gate  * to the port associated with the instance's service name. On any successful
9757c478bd9Sstevel@tonic-gate  * binds the instance is taken online. Failed binds are handled by
9767c478bd9Sstevel@tonic-gate  * handle_bind_failure().
9777c478bd9Sstevel@tonic-gate  */
9787c478bd9Sstevel@tonic-gate void
9797c478bd9Sstevel@tonic-gate create_bound_fds(instance_t *instance)
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
9827c478bd9Sstevel@tonic-gate 	boolean_t	failure = B_FALSE;
9837c478bd9Sstevel@tonic-gate 	boolean_t	success = B_FALSE;
9847c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/*
9877c478bd9Sstevel@tonic-gate 	 * Loop through and try and bind any unbound protos.
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
9907c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
9917c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1)
9927c478bd9Sstevel@tonic-gate 			continue;
9937c478bd9Sstevel@tonic-gate 		if (cfg->istlx) {
994fff9db26Svp 			pi->listen_fd = create_bound_endpoint(instance,
9957c478bd9Sstevel@tonic-gate 			    (tlx_info_t *)pi);
9967c478bd9Sstevel@tonic-gate 		} else {
9977c478bd9Sstevel@tonic-gate 			/*
9987c478bd9Sstevel@tonic-gate 			 * We cast pi to a void so we can then go on to cast
9997c478bd9Sstevel@tonic-gate 			 * it to a socket_info_t without lint complaining
10007c478bd9Sstevel@tonic-gate 			 * about alignment. This is done because the x86
10017c478bd9Sstevel@tonic-gate 			 * version of lint thinks a lint suppression directive
10027c478bd9Sstevel@tonic-gate 			 * is unnecessary and flags it as such, yet the sparc
10037c478bd9Sstevel@tonic-gate 			 * version complains if it's absent.
10047c478bd9Sstevel@tonic-gate 			 */
10057c478bd9Sstevel@tonic-gate 			void *p = pi;
1006fff9db26Svp 			pi->listen_fd = create_bound_socket(instance,
10077c478bd9Sstevel@tonic-gate 			    (socket_info_t *)p);
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 		if (pi->listen_fd == -1) {
10107c478bd9Sstevel@tonic-gate 			failure = B_TRUE;
10117c478bd9Sstevel@tonic-gate 			continue;
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 		if (pi->ri != NULL) {
1015b823d4c7Sdstaff 
1016b823d4c7Sdstaff 			/*
1017b823d4c7Sdstaff 			 * Don't register the same RPC program number twice.
1018b823d4c7Sdstaff 			 * Doing so silently discards the old service
1019b823d4c7Sdstaff 			 * without causing an error.
1020b823d4c7Sdstaff 			 */
1021b823d4c7Sdstaff 			if (is_rpc_num_in_use(pi->ri->prognum, pi->proto,
10227cd7ea0bSrs 			    pi->ri->lowver, pi->ri->highver)) {
1023b823d4c7Sdstaff 				failure = B_TRUE;
1024b823d4c7Sdstaff 				close_net_fd(instance, pi->listen_fd);
1025b823d4c7Sdstaff 				pi->listen_fd = -1;
1026b823d4c7Sdstaff 				continue;
1027b823d4c7Sdstaff 			}
1028b823d4c7Sdstaff 
10297c478bd9Sstevel@tonic-gate 			unregister_rpc_service(instance->fmri, pi->ri);
10307c478bd9Sstevel@tonic-gate 			if (register_rpc_service(instance->fmri, pi->ri) ==
10317c478bd9Sstevel@tonic-gate 			    -1) {
10327c478bd9Sstevel@tonic-gate 				close_net_fd(instance, pi->listen_fd);
10337c478bd9Sstevel@tonic-gate 				pi->listen_fd = -1;
10347c478bd9Sstevel@tonic-gate 				failure = B_TRUE;
10357c478bd9Sstevel@tonic-gate 				continue;
10367c478bd9Sstevel@tonic-gate 			}
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		success = B_TRUE;
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
10437c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE:
10447c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
10457c478bd9Sstevel@tonic-gate 		/*
10467c478bd9Sstevel@tonic-gate 		 * If we've managed to bind at least one proto lets run the
10477c478bd9Sstevel@tonic-gate 		 * online method, so we can start listening for it.
10487c478bd9Sstevel@tonic-gate 		 */
10497c478bd9Sstevel@tonic-gate 		if (success && run_method(instance, IM_ONLINE, NULL) == -1)
10507c478bd9Sstevel@tonic-gate 			return;	/* instance gone to maintenance */
10517c478bd9Sstevel@tonic-gate 		break;
10527c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
10537c478bd9Sstevel@tonic-gate 	case IIS_IN_REFRESH_METHOD:
10547c478bd9Sstevel@tonic-gate 		/*
10557c478bd9Sstevel@tonic-gate 		 * We're 'online', so start polling on any bound fds we're
10567c478bd9Sstevel@tonic-gate 		 * currently not.
10577c478bd9Sstevel@tonic-gate 		 */
10587c478bd9Sstevel@tonic-gate 		if (poll_bound_fds(instance, B_TRUE) != 0) {
10597c478bd9Sstevel@tonic-gate 			failure = B_TRUE;
10607c478bd9Sstevel@tonic-gate 		} else if (!failure) {
10617c478bd9Sstevel@tonic-gate 			/*
10627c478bd9Sstevel@tonic-gate 			 * We've successfully bound and poll'd upon all protos,
10637c478bd9Sstevel@tonic-gate 			 * so reset the failure count.
10647c478bd9Sstevel@tonic-gate 			 */
10657c478bd9Sstevel@tonic-gate 			instance->bind_fail_count = 0;
10667c478bd9Sstevel@tonic-gate 		}
10677c478bd9Sstevel@tonic-gate 		break;
10687c478bd9Sstevel@tonic-gate 	case IIS_IN_ONLINE_METHOD:
10697c478bd9Sstevel@tonic-gate 		/*
10707c478bd9Sstevel@tonic-gate 		 * Nothing to do here as the method completion code will start
10717c478bd9Sstevel@tonic-gate 		 * listening for any successfully bound fds.
10727c478bd9Sstevel@tonic-gate 		 */
10737c478bd9Sstevel@tonic-gate 		break;
10747c478bd9Sstevel@tonic-gate 	default:
10757c478bd9Sstevel@tonic-gate #ifndef NDEBUG
10767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n",
10777c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, instance->cur_istate);
10787c478bd9Sstevel@tonic-gate #endif
10797c478bd9Sstevel@tonic-gate 		abort();
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	if (failure)
10837c478bd9Sstevel@tonic-gate 		handle_bind_failure(instance);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate /*
10877c478bd9Sstevel@tonic-gate  * Counter to create_bound_fds(), for each of the bound network fds this
10887c478bd9Sstevel@tonic-gate  * function unregisters the instance from rpcbind if it's an RPC service,
10897c478bd9Sstevel@tonic-gate  * stops listening for new connections for it and then closes the listening fd.
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate static void
10927c478bd9Sstevel@tonic-gate destroy_bound_fds(instance_t *instance)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
10957c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
10987c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
10997c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1) {
11007c478bd9Sstevel@tonic-gate 			if (pi->ri != NULL)
11017c478bd9Sstevel@tonic-gate 				unregister_rpc_service(instance->fmri, pi->ri);
11027c478bd9Sstevel@tonic-gate 			clear_pollfd(pi->listen_fd);
11037c478bd9Sstevel@tonic-gate 			close_net_fd(instance, pi->listen_fd);
11047c478bd9Sstevel@tonic-gate 			pi->listen_fd = -1;
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/* cancel any bind retries */
11097c478bd9Sstevel@tonic-gate 	if (instance->bind_timer_id != -1)
11107c478bd9Sstevel@tonic-gate 		cancel_bind_timer(instance);
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	instance->bind_retries_exceeded = B_FALSE;
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate  * Perform %A address expansion and return a pointer to a static string
11177c478bd9Sstevel@tonic-gate  * array containing crafted arguments. This expansion is provided for
11187c478bd9Sstevel@tonic-gate  * compatibility with 4.2BSD daemons, and as such we've copied the logic of
11197c478bd9Sstevel@tonic-gate  * the legacy inetd to maintain this compatibility as much as possible. This
11207c478bd9Sstevel@tonic-gate  * logic is a bit scatty, but it dates back at least as far as SunOS 4.x.
11217c478bd9Sstevel@tonic-gate  */
11227c478bd9Sstevel@tonic-gate static char **
11237c478bd9Sstevel@tonic-gate expand_address(instance_t *inst, const proto_info_t *pi)
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	static char	addrbuf[sizeof ("ffffffff.65536")];
11267c478bd9Sstevel@tonic-gate 	static char	*ret[3];
11277c478bd9Sstevel@tonic-gate 	instance_cfg_t	*cfg = inst->config;
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * We cast pi to a void so we can then go on to cast it to a
11307c478bd9Sstevel@tonic-gate 	 * socket_info_t without lint complaining about alignment. This
11317c478bd9Sstevel@tonic-gate 	 * is done because the x86 version of lint thinks a lint suppression
11327c478bd9Sstevel@tonic-gate 	 * directive is unnecessary and flags it as such, yet the sparc
11337c478bd9Sstevel@tonic-gate 	 * version complains if it's absent.
11347c478bd9Sstevel@tonic-gate 	 */
11357c478bd9Sstevel@tonic-gate 	const void	*p = pi;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	/* set ret[0] to the basename of exec path */
11387c478bd9Sstevel@tonic-gate 	if ((ret[0] = strrchr(cfg->methods[IM_START]->exec_path, '/'))
11397c478bd9Sstevel@tonic-gate 	    != NULL) {
11407c478bd9Sstevel@tonic-gate 		ret[0]++;
11417c478bd9Sstevel@tonic-gate 	} else {
11427c478bd9Sstevel@tonic-gate 		ret[0] = cfg->methods[IM_START]->exec_path;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	if (!cfg->basic->istlx &&
11467c478bd9Sstevel@tonic-gate 	    (((socket_info_t *)p)->type == SOCK_DGRAM)) {
11477c478bd9Sstevel@tonic-gate 		ret[1] = NULL;
11487c478bd9Sstevel@tonic-gate 	} else {
11497c478bd9Sstevel@tonic-gate 		addrbuf[0] = '\0';
11507c478bd9Sstevel@tonic-gate 		if (!cfg->basic->iswait &&
11517c478bd9Sstevel@tonic-gate 		    (inst->remote_addr.ss_family == AF_INET)) {
11527c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sp;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 			sp = (struct sockaddr_in *)&(inst->remote_addr);
11557c478bd9Sstevel@tonic-gate 			(void) snprintf(addrbuf, sizeof (addrbuf), "%x.%hu",
11567c478bd9Sstevel@tonic-gate 			    ntohl(sp->sin_addr.s_addr), ntohs(sp->sin_port));
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 		ret[1] = addrbuf;
11597c478bd9Sstevel@tonic-gate 		ret[2] = NULL;
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	return (ret);
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate /*
11667c478bd9Sstevel@tonic-gate  * Returns the state associated with the supplied method being run for an
11677c478bd9Sstevel@tonic-gate  * instance.
11687c478bd9Sstevel@tonic-gate  */
11697c478bd9Sstevel@tonic-gate static internal_inst_state_t
11707c478bd9Sstevel@tonic-gate get_method_state(instance_method_t method)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	state_info_t *sip;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	for (sip = states; sip->istate != IIS_NONE; sip++) {
11757c478bd9Sstevel@tonic-gate 		if (sip->method_running == method)
11767c478bd9Sstevel@tonic-gate 			break;
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	assert(sip->istate != IIS_NONE);
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	return (sip->istate);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate /*
11847c478bd9Sstevel@tonic-gate  * Store the method's PID and CID in the repository. If the store fails
11857c478bd9Sstevel@tonic-gate  * we ignore it and just drive on.
11867c478bd9Sstevel@tonic-gate  */
11877c478bd9Sstevel@tonic-gate static void
11887c478bd9Sstevel@tonic-gate add_method_ids(instance_t *ins, pid_t pid, ctid_t cid, instance_method_t mthd)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	if (cid != -1)
119194501b61Sskamm 		(void) add_remove_contract(ins, B_TRUE, cid);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if (mthd == IM_START) {
11947c478bd9Sstevel@tonic-gate 		if (add_rep_val(ins->start_pids, (int64_t)pid) == 0) {
11957c478bd9Sstevel@tonic-gate 			(void) store_rep_vals(ins->start_pids, ins->fmri,
11967c478bd9Sstevel@tonic-gate 			    PR_NAME_START_PIDS);
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 	} else {
11997c478bd9Sstevel@tonic-gate 		if (add_rep_val(ins->non_start_pid, (int64_t)pid) == 0) {
12007c478bd9Sstevel@tonic-gate 			(void) store_rep_vals(ins->non_start_pid, ins->fmri,
12017c478bd9Sstevel@tonic-gate 			    PR_NAME_NON_START_PID);
12027c478bd9Sstevel@tonic-gate 		}
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * Remove the method's PID and CID from the repository. If the removal
12087c478bd9Sstevel@tonic-gate  * fails we ignore it and drive on.
12097c478bd9Sstevel@tonic-gate  */
12107c478bd9Sstevel@tonic-gate void
12117c478bd9Sstevel@tonic-gate remove_method_ids(instance_t *inst, pid_t pid, ctid_t cid,
12127c478bd9Sstevel@tonic-gate     instance_method_t mthd)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	if (cid != -1)
121594501b61Sskamm 		(void) add_remove_contract(inst, B_FALSE, cid);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if (mthd == IM_START) {
12187c478bd9Sstevel@tonic-gate 		remove_rep_val(inst->start_pids, (int64_t)pid);
12197c478bd9Sstevel@tonic-gate 		(void) store_rep_vals(inst->start_pids, inst->fmri,
12207c478bd9Sstevel@tonic-gate 		    PR_NAME_START_PIDS);
12217c478bd9Sstevel@tonic-gate 	} else {
12227c478bd9Sstevel@tonic-gate 		remove_rep_val(inst->non_start_pid, (int64_t)pid);
12237c478bd9Sstevel@tonic-gate 		(void) store_rep_vals(inst->non_start_pid, inst->fmri,
12247c478bd9Sstevel@tonic-gate 		    PR_NAME_NON_START_PID);
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate static instance_t *
12297c478bd9Sstevel@tonic-gate create_instance(const char *fmri)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	instance_t *ret;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (((ret = calloc(1, sizeof (instance_t))) == NULL) ||
12347c478bd9Sstevel@tonic-gate 	    ((ret->fmri = strdup(fmri)) == NULL))
12357c478bd9Sstevel@tonic-gate 		goto alloc_fail;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	ret->conn_fd = -1;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	ret->copies = 0;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	ret->conn_rate_count = 0;
12427c478bd9Sstevel@tonic-gate 	ret->fail_rate_count = 0;
12437c478bd9Sstevel@tonic-gate 	ret->bind_fail_count = 0;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	if (((ret->non_start_pid = create_rep_val_list()) == NULL) ||
124694501b61Sskamm 	    ((ret->start_pids = create_rep_val_list()) == NULL) ||
124794501b61Sskamm 	    ((ret->start_ctids = create_rep_val_list()) == NULL))
12487c478bd9Sstevel@tonic-gate 		goto alloc_fail;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	ret->cur_istate = IIS_NONE;
12517c478bd9Sstevel@tonic-gate 	ret->next_istate = IIS_NONE;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	if (((ret->cur_istate_rep = create_rep_val_list()) == NULL) ||
12547c478bd9Sstevel@tonic-gate 	    ((ret->next_istate_rep = create_rep_val_list()) == NULL))
12557c478bd9Sstevel@tonic-gate 		goto alloc_fail;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	ret->config = NULL;
12587c478bd9Sstevel@tonic-gate 	ret->new_config = NULL;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	ret->timer_id = -1;
12617c478bd9Sstevel@tonic-gate 	ret->bind_timer_id = -1;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	ret->disable_req = B_FALSE;
12647c478bd9Sstevel@tonic-gate 	ret->maintenance_req = B_FALSE;
12657c478bd9Sstevel@tonic-gate 	ret->conn_rate_exceeded = B_FALSE;
12667c478bd9Sstevel@tonic-gate 	ret->bind_retries_exceeded = B_FALSE;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	ret->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID;
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	return (ret);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate alloc_fail:
12737c478bd9Sstevel@tonic-gate 	error_msg(strerror(errno));
12747c478bd9Sstevel@tonic-gate 	destroy_instance(ret);
12757c478bd9Sstevel@tonic-gate 	return (NULL);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate static void
12797c478bd9Sstevel@tonic-gate destroy_instance(instance_t *inst)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	if (inst == NULL)
12827c478bd9Sstevel@tonic-gate 		return;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	destroy_instance_cfg(inst->config);
12857c478bd9Sstevel@tonic-gate 	destroy_instance_cfg(inst->new_config);
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->cur_istate_rep);
12887c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->next_istate_rep);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->start_pids);
12917c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->non_start_pid);
129294501b61Sskamm 	destroy_rep_val_list(inst->start_ctids);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	free(inst->fmri);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	free(inst);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * Retrieves the current and next states internal states. Returns 0 on success,
13017c478bd9Sstevel@tonic-gate  * else returns one of the following on error:
13027c478bd9Sstevel@tonic-gate  * SCF_ERROR_NO_MEMORY if memory allocation failed.
13037c478bd9Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
13047c478bd9Sstevel@tonic-gate  * SCF_ERROR_TYPE_MISMATCH if the property was of an unexpected type.
13057c478bd9Sstevel@tonic-gate  * SCF_ERROR_NO_RESOURCES if the server doesn't have adequate resources.
13067c478bd9Sstevel@tonic-gate  * SCF_ERROR_NO_SERVER if the server isn't running.
13077c478bd9Sstevel@tonic-gate  */
13087c478bd9Sstevel@tonic-gate static scf_error_t
13097c478bd9Sstevel@tonic-gate retrieve_instance_state(instance_t *inst)
13107c478bd9Sstevel@tonic-gate {
13117c478bd9Sstevel@tonic-gate 	scf_error_t	ret;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/* retrieve internal states */
13147c478bd9Sstevel@tonic-gate 	if (((ret = retrieve_rep_vals(inst->cur_istate_rep, inst->fmri,
13157c478bd9Sstevel@tonic-gate 	    PR_NAME_CUR_INT_STATE)) != 0) ||
13167c478bd9Sstevel@tonic-gate 	    ((ret = retrieve_rep_vals(inst->next_istate_rep, inst->fmri,
13177c478bd9Sstevel@tonic-gate 	    PR_NAME_NEXT_INT_STATE)) != 0)) {
13187c478bd9Sstevel@tonic-gate 		if (ret != SCF_ERROR_NOT_FOUND) {
13197c478bd9Sstevel@tonic-gate 			error_msg(gettext(
13207c478bd9Sstevel@tonic-gate 			    "Failed to read state of instance %s: %s"),
13217c478bd9Sstevel@tonic-gate 			    inst->fmri, scf_strerror(scf_error()));
13227c478bd9Sstevel@tonic-gate 			return (ret);
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 		debug_msg("instance with no previous int state - "
13267c478bd9Sstevel@tonic-gate 		    "setting state to uninitialized");
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 		if ((set_single_rep_val(inst->cur_istate_rep,
13297c478bd9Sstevel@tonic-gate 		    (int64_t)IIS_UNINITIALIZED) == -1) ||
13307c478bd9Sstevel@tonic-gate 		    (set_single_rep_val(inst->next_istate_rep,
13317c478bd9Sstevel@tonic-gate 		    (int64_t)IIS_NONE) == -1)) {
13327c478bd9Sstevel@tonic-gate 			return (SCF_ERROR_NO_MEMORY);
13337c478bd9Sstevel@tonic-gate 		}
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	/* update convenience states */
13377c478bd9Sstevel@tonic-gate 	inst->cur_istate = get_single_rep_val(inst->cur_istate_rep);
13387c478bd9Sstevel@tonic-gate 	inst->next_istate = get_single_rep_val(inst->next_istate_rep);
13397c478bd9Sstevel@tonic-gate 	return (0);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * Retrieve stored process ids and register each of them so we process their
13447c478bd9Sstevel@tonic-gate  * termination.
13457c478bd9Sstevel@tonic-gate  */
13467c478bd9Sstevel@tonic-gate static int
13477c478bd9Sstevel@tonic-gate retrieve_method_pids(instance_t *inst)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate 	rep_val_t	*rv;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	switch (retrieve_rep_vals(inst->start_pids, inst->fmri,
13527c478bd9Sstevel@tonic-gate 	    PR_NAME_START_PIDS)) {
13537c478bd9Sstevel@tonic-gate 	case 0:
13547c478bd9Sstevel@tonic-gate 		break;
13557c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
13567c478bd9Sstevel@tonic-gate 		return (0);
13577c478bd9Sstevel@tonic-gate 	default:
13587c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to retrieve the start pids of "
13597c478bd9Sstevel@tonic-gate 		    "instance %s from repository: %s"), inst->fmri,
13607c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
13617c478bd9Sstevel@tonic-gate 		return (-1);
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	rv = uu_list_first(inst->start_pids);
13657c478bd9Sstevel@tonic-gate 	while (rv != NULL) {
13667c478bd9Sstevel@tonic-gate 		if (register_method(inst, (pid_t)rv->val, (ctid_t)-1,
13677c478bd9Sstevel@tonic-gate 		    IM_START) == 0) {
13687c478bd9Sstevel@tonic-gate 			inst->copies++;
13697c478bd9Sstevel@tonic-gate 			rv = uu_list_next(inst->start_pids, rv);
13707c478bd9Sstevel@tonic-gate 		} else if (errno == ENOENT) {
13717c478bd9Sstevel@tonic-gate 			pid_t pid = (pid_t)rv->val;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 			/*
13747c478bd9Sstevel@tonic-gate 			 * The process must have already terminated. Remove
13757c478bd9Sstevel@tonic-gate 			 * it from the list.
13767c478bd9Sstevel@tonic-gate 			 */
13777c478bd9Sstevel@tonic-gate 			rv = uu_list_next(inst->start_pids, rv);
13787c478bd9Sstevel@tonic-gate 			remove_rep_val(inst->start_pids, pid);
13797c478bd9Sstevel@tonic-gate 		} else {
13807c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to listen for the completion "
13817c478bd9Sstevel@tonic-gate 			    "of %s method of instance %s"), START_METHOD_NAME,
13827c478bd9Sstevel@tonic-gate 			    inst->fmri);
13837c478bd9Sstevel@tonic-gate 			rv = uu_list_next(inst->start_pids, rv);
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	/* synch the repository pid list to remove any terminated pids */
13887c478bd9Sstevel@tonic-gate 	(void) store_rep_vals(inst->start_pids, inst->fmri, PR_NAME_START_PIDS);
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	return (0);
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate /*
13947c478bd9Sstevel@tonic-gate  * Remove the passed instance from inetd control.
13957c478bd9Sstevel@tonic-gate  */
13967c478bd9Sstevel@tonic-gate static void
13977c478bd9Sstevel@tonic-gate remove_instance(instance_t *instance)
13987c478bd9Sstevel@tonic-gate {
13997c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
14007c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
14017c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
14027c478bd9Sstevel@tonic-gate 		/* stop listening for network connections */
14037c478bd9Sstevel@tonic-gate 		destroy_bound_fds(instance);
14047c478bd9Sstevel@tonic-gate 		break;
14057c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
14067c478bd9Sstevel@tonic-gate 		cancel_bind_timer(instance);
14077c478bd9Sstevel@tonic-gate 		break;
14087c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_CONRATE:
14097c478bd9Sstevel@tonic-gate 		cancel_inst_timer(instance);
14107c478bd9Sstevel@tonic-gate 		break;
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	/* stop listening for terminated methods */
14147c478bd9Sstevel@tonic-gate 	unregister_instance_methods(instance);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	uu_list_remove(instance_list, instance);
14177c478bd9Sstevel@tonic-gate 	destroy_instance(instance);
14187c478bd9Sstevel@tonic-gate }
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate /*
14217c478bd9Sstevel@tonic-gate  * Refresh the configuration of instance 'inst'. This method gets called as
14227c478bd9Sstevel@tonic-gate  * a result of a refresh event for the instance from the master restarter, so
14237c478bd9Sstevel@tonic-gate  * we can rely upon the instance's running snapshot having been updated from
14247c478bd9Sstevel@tonic-gate  * its configuration snapshot.
14257c478bd9Sstevel@tonic-gate  */
14267c478bd9Sstevel@tonic-gate void
14277c478bd9Sstevel@tonic-gate refresh_instance(instance_t *inst)
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	instance_cfg_t	*cfg;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	switch (inst->cur_istate) {
14327c478bd9Sstevel@tonic-gate 	case IIS_MAINTENANCE:
14337c478bd9Sstevel@tonic-gate 	case IIS_DISABLED:
14347c478bd9Sstevel@tonic-gate 	case IIS_UNINITIALIZED:
14357c478bd9Sstevel@tonic-gate 		/*
14367c478bd9Sstevel@tonic-gate 		 * Ignore any possible changes, we'll re-read the configuration
14377c478bd9Sstevel@tonic-gate 		 * automatically when we exit these states.
14387c478bd9Sstevel@tonic-gate 		 */
14397c478bd9Sstevel@tonic-gate 		break;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_COPIES:
14427c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
14437c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE:
14447c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_CONRATE:
14457c478bd9Sstevel@tonic-gate 		destroy_instance_cfg(inst->config);
14467c478bd9Sstevel@tonic-gate 		if ((inst->config = read_instance_cfg(inst->fmri)) == NULL) {
14477c478bd9Sstevel@tonic-gate 			log_invalid_cfg(inst->fmri);
14487c478bd9Sstevel@tonic-gate 			if (inst->cur_istate == IIS_OFFLINE_BIND) {
14497c478bd9Sstevel@tonic-gate 				cancel_bind_timer(inst);
14507c478bd9Sstevel@tonic-gate 			} else if (inst->cur_istate == IIS_OFFLINE_CONRATE) {
14517c478bd9Sstevel@tonic-gate 				cancel_inst_timer(inst);
14527c478bd9Sstevel@tonic-gate 			}
14537c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_MAINTENANCE, RERR_FAULT);
14547c478bd9Sstevel@tonic-gate 		} else {
14557c478bd9Sstevel@tonic-gate 			switch (inst->cur_istate) {
14567c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE_BIND:
14577c478bd9Sstevel@tonic-gate 				if (copies_limit_exceeded(inst)) {
14587c478bd9Sstevel@tonic-gate 					/* Cancel scheduled bind retries. */
14597c478bd9Sstevel@tonic-gate 					cancel_bind_timer(inst);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 					/*
14627c478bd9Sstevel@tonic-gate 					 * Take the instance to the copies
14637c478bd9Sstevel@tonic-gate 					 * offline state, via the offline
14647c478bd9Sstevel@tonic-gate 					 * state.
14657c478bd9Sstevel@tonic-gate 					 */
14667c478bd9Sstevel@tonic-gate 					update_state(inst, IIS_OFFLINE,
14677c478bd9Sstevel@tonic-gate 					    RERR_RESTART);
14687c478bd9Sstevel@tonic-gate 					process_offline_inst(inst);
14697c478bd9Sstevel@tonic-gate 				}
14707c478bd9Sstevel@tonic-gate 				break;
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE:
14737c478bd9Sstevel@tonic-gate 				process_offline_inst(inst);
14747c478bd9Sstevel@tonic-gate 				break;
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE_CONRATE:
14777c478bd9Sstevel@tonic-gate 				/*
14787c478bd9Sstevel@tonic-gate 				 * Since we're already in a DOS state,
14797c478bd9Sstevel@tonic-gate 				 * don't bother evaluating the copies
14807c478bd9Sstevel@tonic-gate 				 * limit. This will be evaluated when
14817c478bd9Sstevel@tonic-gate 				 * we leave this state in
14827c478bd9Sstevel@tonic-gate 				 * process_offline_inst().
14837c478bd9Sstevel@tonic-gate 				 */
14847c478bd9Sstevel@tonic-gate 				break;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE_COPIES:
14877c478bd9Sstevel@tonic-gate 				/*
14887c478bd9Sstevel@tonic-gate 				 * Check if the copies limit has been increased
14897c478bd9Sstevel@tonic-gate 				 * above the current count.
14907c478bd9Sstevel@tonic-gate 				 */
14917c478bd9Sstevel@tonic-gate 				if (!copies_limit_exceeded(inst)) {
14927c478bd9Sstevel@tonic-gate 					update_state(inst, IIS_OFFLINE,
14937c478bd9Sstevel@tonic-gate 					    RERR_RESTART);
14947c478bd9Sstevel@tonic-gate 					process_offline_inst(inst);
14957c478bd9Sstevel@tonic-gate 				}
14967c478bd9Sstevel@tonic-gate 				break;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 			default:
14997c478bd9Sstevel@tonic-gate 				assert(0);
15007c478bd9Sstevel@tonic-gate 			}
15017c478bd9Sstevel@tonic-gate 		}
15027c478bd9Sstevel@tonic-gate 		break;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
15057c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
15067c478bd9Sstevel@tonic-gate 		if ((cfg = read_instance_cfg(inst->fmri)) != NULL) {
15077c478bd9Sstevel@tonic-gate 			instance_cfg_t *ocfg = inst->config;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 			/*
15107c478bd9Sstevel@tonic-gate 			 * Try to avoid the overhead of taking an instance
15117c478bd9Sstevel@tonic-gate 			 * offline and back on again. We do this by limiting
15127c478bd9Sstevel@tonic-gate 			 * this behavior to two eventualities:
15137c478bd9Sstevel@tonic-gate 			 * - there needs to be a re-bind to listen on behalf
15147c478bd9Sstevel@tonic-gate 			 *   of the instance with its new configuration. This
15157c478bd9Sstevel@tonic-gate 			 *   could be because for example its service has been
15167c478bd9Sstevel@tonic-gate 			 *   associated with a different port, or because the
15177c478bd9Sstevel@tonic-gate 			 *   v6only protocol option has been newly applied to
15187c478bd9Sstevel@tonic-gate 			 *   the instance.
15197c478bd9Sstevel@tonic-gate 			 * - one or both of the start or online methods of the
15207c478bd9Sstevel@tonic-gate 			 *   instance have changed in the new configuration.
15217c478bd9Sstevel@tonic-gate 			 *   Without taking the instance offline when the
15227c478bd9Sstevel@tonic-gate 			 *   start method changed the instance may be running
15237c478bd9Sstevel@tonic-gate 			 *   with unwanted parameters (or event an unwanted
15247c478bd9Sstevel@tonic-gate 			 *   binary); and without taking the instance offline
15257c478bd9Sstevel@tonic-gate 			 *   if its online method was to change, some part of
15267c478bd9Sstevel@tonic-gate 			 *   its running environment may have changed and would
15277c478bd9Sstevel@tonic-gate 			 *   not be picked up until the instance next goes
15287c478bd9Sstevel@tonic-gate 			 *   offline for another reason.
15297c478bd9Sstevel@tonic-gate 			 */
15307c478bd9Sstevel@tonic-gate 			if ((!bind_config_equal(ocfg->basic, cfg->basic)) ||
15317c478bd9Sstevel@tonic-gate 			    !method_info_equal(ocfg->methods[IM_ONLINE],
15327c478bd9Sstevel@tonic-gate 			    cfg->methods[IM_ONLINE]) ||
15337c478bd9Sstevel@tonic-gate 			    !method_info_equal(ocfg->methods[IM_START],
15347c478bd9Sstevel@tonic-gate 			    cfg->methods[IM_START])) {
15357c478bd9Sstevel@tonic-gate 				destroy_bound_fds(inst);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 				assert(inst->new_config == NULL);
15387c478bd9Sstevel@tonic-gate 				inst->new_config = cfg;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 				(void) run_method(inst, IM_OFFLINE, NULL);
15417c478bd9Sstevel@tonic-gate 			} else {	/* no bind config / method changes */
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 				/*
15447c478bd9Sstevel@tonic-gate 				 * swap the proto list over from the old
15457c478bd9Sstevel@tonic-gate 				 * configuration to the new, so we retain
15467c478bd9Sstevel@tonic-gate 				 * our set of network fds.
15477c478bd9Sstevel@tonic-gate 				 */
15487c478bd9Sstevel@tonic-gate 				destroy_proto_list(cfg->basic);
15497c478bd9Sstevel@tonic-gate 				cfg->basic->proto_list =
15507c478bd9Sstevel@tonic-gate 				    ocfg->basic->proto_list;
15517c478bd9Sstevel@tonic-gate 				ocfg->basic->proto_list = NULL;
15527c478bd9Sstevel@tonic-gate 				destroy_instance_cfg(ocfg);
15537c478bd9Sstevel@tonic-gate 				inst->config = cfg;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 				/* re-evaluate copies limits based on new cfg */
15567c478bd9Sstevel@tonic-gate 				if (copies_limit_exceeded(inst)) {
15577c478bd9Sstevel@tonic-gate 					destroy_bound_fds(inst);
15587c478bd9Sstevel@tonic-gate 					(void) run_method(inst, IM_OFFLINE,
15597c478bd9Sstevel@tonic-gate 					    NULL);
15607c478bd9Sstevel@tonic-gate 				} else {
15617c478bd9Sstevel@tonic-gate 					/*
15627c478bd9Sstevel@tonic-gate 					 * Since the instance isn't being
15637c478bd9Sstevel@tonic-gate 					 * taken offline, where we assume it
15647c478bd9Sstevel@tonic-gate 					 * would pick-up any configuration
15657c478bd9Sstevel@tonic-gate 					 * changes automatically when it goes
15667c478bd9Sstevel@tonic-gate 					 * back online, run its refresh method
15677c478bd9Sstevel@tonic-gate 					 * to allow it to pick-up any changes
15687c478bd9Sstevel@tonic-gate 					 * whilst still online.
15697c478bd9Sstevel@tonic-gate 					 */
15707c478bd9Sstevel@tonic-gate 					(void) run_method(inst, IM_REFRESH,
15717c478bd9Sstevel@tonic-gate 					    NULL);
15727c478bd9Sstevel@tonic-gate 				}
15737c478bd9Sstevel@tonic-gate 			}
15747c478bd9Sstevel@tonic-gate 		} else {
15757c478bd9Sstevel@tonic-gate 			log_invalid_cfg(inst->fmri);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 			destroy_bound_fds(inst);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 			inst->maintenance_req = B_TRUE;
15807c478bd9Sstevel@tonic-gate 			(void) run_method(inst, IM_OFFLINE, NULL);
15817c478bd9Sstevel@tonic-gate 		}
15827c478bd9Sstevel@tonic-gate 		break;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	default:
15857c478bd9Sstevel@tonic-gate 		debug_msg("Unhandled current state %d for instance in "
15867c478bd9Sstevel@tonic-gate 		    "refresh_instance", inst->cur_istate);
15877c478bd9Sstevel@tonic-gate 		assert(0);
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate /*
15927c478bd9Sstevel@tonic-gate  * Called by process_restarter_event() to handle a restarter event for an
15937c478bd9Sstevel@tonic-gate  * instance.
15947c478bd9Sstevel@tonic-gate  */
15957c478bd9Sstevel@tonic-gate static void
15967c478bd9Sstevel@tonic-gate handle_restarter_event(instance_t *instance, restarter_event_type_t event,
15977c478bd9Sstevel@tonic-gate     boolean_t send_ack)
15987c478bd9Sstevel@tonic-gate {
15997c478bd9Sstevel@tonic-gate 	switch (event) {
16008977779cSvp 	case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
16018977779cSvp 		/*
16028977779cSvp 		 * When startd restarts, it sends _ADD_INSTANCE to delegated
16038977779cSvp 		 * restarters for all those services managed by them. We should
16048977779cSvp 		 * acknowledge this event, as startd's graph needs to be updated
16058977779cSvp 		 * about the current state of the service, when startd is
16068977779cSvp 		 * restarting.
16078977779cSvp 		 * update_state() is ok to be called here, as commands for
16088977779cSvp 		 * instances in transition are deferred by
16098977779cSvp 		 * process_restarter_event().
16108977779cSvp 		 */
16118977779cSvp 		update_state(instance, instance->cur_istate, RERR_NONE);
16128977779cSvp 		goto done;
16137c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
16147c478bd9Sstevel@tonic-gate 		refresh_instance(instance);
16157c478bd9Sstevel@tonic-gate 		goto done;
16168977779cSvp 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
16178977779cSvp 		/*
16188977779cSvp 		 * We've got a restart event, so if the instance is online
16198977779cSvp 		 * in any way initiate taking it offline, and rely upon
16208977779cSvp 		 * our restarter to send us an online event to bring
16218977779cSvp 		 * it back online.
16228977779cSvp 		 */
16238977779cSvp 		switch (instance->cur_istate) {
16248977779cSvp 		case IIS_ONLINE:
16258977779cSvp 		case IIS_DEGRADED:
16268977779cSvp 			destroy_bound_fds(instance);
16278977779cSvp 			(void) run_method(instance, IM_OFFLINE, NULL);
16288977779cSvp 		}
16298977779cSvp 		goto done;
16307c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
16317c478bd9Sstevel@tonic-gate 		remove_instance(instance);
16327c478bd9Sstevel@tonic-gate 		goto done;
16337c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
16347c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
16357c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_CONRATE:
16367c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_BIND:
16377c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_COPIES:
16387c478bd9Sstevel@tonic-gate 			/*
16397c478bd9Sstevel@tonic-gate 			 * inetd must be closing down as we wouldn't get this
16407c478bd9Sstevel@tonic-gate 			 * event in one of these states from the master
16417c478bd9Sstevel@tonic-gate 			 * restarter. Take the instance to the offline resting
16427c478bd9Sstevel@tonic-gate 			 * state.
16437c478bd9Sstevel@tonic-gate 			 */
16447c478bd9Sstevel@tonic-gate 			if (instance->cur_istate == IIS_OFFLINE_BIND) {
16457c478bd9Sstevel@tonic-gate 				cancel_bind_timer(instance);
16467c478bd9Sstevel@tonic-gate 			} else if (instance->cur_istate ==
16477c478bd9Sstevel@tonic-gate 			    IIS_OFFLINE_CONRATE) {
16487c478bd9Sstevel@tonic-gate 				cancel_inst_timer(instance);
16497c478bd9Sstevel@tonic-gate 			}
16507c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
16517c478bd9Sstevel@tonic-gate 			goto done;
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 		break;
16547c478bd9Sstevel@tonic-gate 	}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
16577c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE:
16587c478bd9Sstevel@tonic-gate 		switch (event) {
16597c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_START:
16607c478bd9Sstevel@tonic-gate 			/*
16617c478bd9Sstevel@tonic-gate 			 * Dependencies are met, let's take the service online.
16627c478bd9Sstevel@tonic-gate 			 * Only try and bind for a wait type service if
16637c478bd9Sstevel@tonic-gate 			 * no process is running on its behalf. Otherwise, just
16647c478bd9Sstevel@tonic-gate 			 * mark the service online and binding will be attempted
16657c478bd9Sstevel@tonic-gate 			 * when the process exits.
16667c478bd9Sstevel@tonic-gate 			 */
16677c478bd9Sstevel@tonic-gate 			if (!(instance->config->basic->iswait &&
16687c478bd9Sstevel@tonic-gate 			    (uu_list_first(instance->start_pids) != NULL))) {
16697c478bd9Sstevel@tonic-gate 				create_bound_fds(instance);
16707c478bd9Sstevel@tonic-gate 			} else {
16717c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_ONLINE, RERR_NONE);
16727c478bd9Sstevel@tonic-gate 			}
16737c478bd9Sstevel@tonic-gate 			break;
16747c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
16757c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
16767c478bd9Sstevel@tonic-gate 			/*
16777c478bd9Sstevel@tonic-gate 			 * The instance should be disabled, so run the
16787c478bd9Sstevel@tonic-gate 			 * instance's disabled method that will do the work
16797c478bd9Sstevel@tonic-gate 			 * to take it there.
16807c478bd9Sstevel@tonic-gate 			 */
16817c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
16827c478bd9Sstevel@tonic-gate 			break;
16837c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
16847c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
16857c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
16867c478bd9Sstevel@tonic-gate 			/*
16877c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance
16887c478bd9Sstevel@tonic-gate 			 * go to maintenance; since we're already offline
16897c478bd9Sstevel@tonic-gate 			 * just update the state to the maintenance state.
16907c478bd9Sstevel@tonic-gate 			 */
16917c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
16927c478bd9Sstevel@tonic-gate 			break;
16937c478bd9Sstevel@tonic-gate 		}
16947c478bd9Sstevel@tonic-gate 		break;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
16977c478bd9Sstevel@tonic-gate 		switch (event) {
16987c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
16997c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
17007c478bd9Sstevel@tonic-gate 			/*
17017c478bd9Sstevel@tonic-gate 			 * The instance should be disabled. Firstly, as for
17027c478bd9Sstevel@tonic-gate 			 * the above dependencies unmet comment, cancel
17037c478bd9Sstevel@tonic-gate 			 * the bind retry timer and update the state to
17047c478bd9Sstevel@tonic-gate 			 * offline. Then, run the disable method to do the
17057c478bd9Sstevel@tonic-gate 			 * work to take the instance from offline to
17067c478bd9Sstevel@tonic-gate 			 * disabled.
17077c478bd9Sstevel@tonic-gate 			 */
17087c478bd9Sstevel@tonic-gate 			cancel_bind_timer(instance);
17097c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
17107c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
17117c478bd9Sstevel@tonic-gate 			break;
17127c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
17137c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
17147c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
17157c478bd9Sstevel@tonic-gate 			/*
17167c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance
17177c478bd9Sstevel@tonic-gate 			 * be placed in the maintenance state. Cancel the
17187c478bd9Sstevel@tonic-gate 			 * outstanding retry timer, and since we're already
17197c478bd9Sstevel@tonic-gate 			 * offline, update the state to maintenance.
17207c478bd9Sstevel@tonic-gate 			 */
17217c478bd9Sstevel@tonic-gate 			cancel_bind_timer(instance);
17227c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
17237c478bd9Sstevel@tonic-gate 			break;
17247c478bd9Sstevel@tonic-gate 		}
17257c478bd9Sstevel@tonic-gate 		break;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
17287c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
17297c478bd9Sstevel@tonic-gate 		switch (event) {
17307c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
17317c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
17327c478bd9Sstevel@tonic-gate 			/*
17337c478bd9Sstevel@tonic-gate 			 * The instance needs to be disabled. Do the same work
17347c478bd9Sstevel@tonic-gate 			 * as for the dependencies unmet event below to
17357c478bd9Sstevel@tonic-gate 			 * take the instance offline.
17367c478bd9Sstevel@tonic-gate 			 */
17377c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
17387c478bd9Sstevel@tonic-gate 			/*
17397c478bd9Sstevel@tonic-gate 			 * Indicate that the offline method is being run
17407c478bd9Sstevel@tonic-gate 			 * as part of going to the disabled state, and to
17417c478bd9Sstevel@tonic-gate 			 * carry on this transition.
17427c478bd9Sstevel@tonic-gate 			 */
17437c478bd9Sstevel@tonic-gate 			instance->disable_req = B_TRUE;
17447c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_OFFLINE, NULL);
17457c478bd9Sstevel@tonic-gate 			break;
17467c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
17477c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
17487c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
17497c478bd9Sstevel@tonic-gate 			/*
17507c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance be
17517c478bd9Sstevel@tonic-gate 			 * placed in the maintenance state. This involves
17527c478bd9Sstevel@tonic-gate 			 * firstly taking the service offline, so do the
17537c478bd9Sstevel@tonic-gate 			 * same work as for the dependencies unmet event
17547c478bd9Sstevel@tonic-gate 			 * below. We set the maintenance_req flag to
17557c478bd9Sstevel@tonic-gate 			 * indicate that when we get to the offline state
17567c478bd9Sstevel@tonic-gate 			 * we should be placed directly into the maintenance
17577c478bd9Sstevel@tonic-gate 			 * state.
17587c478bd9Sstevel@tonic-gate 			 */
17597c478bd9Sstevel@tonic-gate 			instance->maintenance_req = B_TRUE;
17607c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
17617c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_STOP:
17627c478bd9Sstevel@tonic-gate 			/*
17637c478bd9Sstevel@tonic-gate 			 * Dependencies have become unmet. Close and
17647c478bd9Sstevel@tonic-gate 			 * stop listening on the instance's network file
17657c478bd9Sstevel@tonic-gate 			 * descriptor, and run the offline method to do
17667c478bd9Sstevel@tonic-gate 			 * any work required to take us to the offline state.
17677c478bd9Sstevel@tonic-gate 			 */
17687c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
17697c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_OFFLINE, NULL);
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 		break;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	case IIS_UNINITIALIZED:
17747c478bd9Sstevel@tonic-gate 		if (event == RESTARTER_EVENT_TYPE_DISABLE ||
17757c478bd9Sstevel@tonic-gate 		    event == RESTARTER_EVENT_TYPE_ADMIN_DISABLE) {
17767c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_DISABLED, RERR_NONE);
17777c478bd9Sstevel@tonic-gate 			break;
17787c478bd9Sstevel@tonic-gate 		} else if (event != RESTARTER_EVENT_TYPE_ENABLE) {
17797c478bd9Sstevel@tonic-gate 			/*
17807c478bd9Sstevel@tonic-gate 			 * Ignore other events until we know whether we're
17817c478bd9Sstevel@tonic-gate 			 * enabled or not.
17827c478bd9Sstevel@tonic-gate 			 */
17837c478bd9Sstevel@tonic-gate 			break;
17847c478bd9Sstevel@tonic-gate 		}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 		/*
17877c478bd9Sstevel@tonic-gate 		 * We've got an enabled event; make use of the handling in the
17887c478bd9Sstevel@tonic-gate 		 * disable case.
17897c478bd9Sstevel@tonic-gate 		 */
17907c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	case IIS_DISABLED:
17937c478bd9Sstevel@tonic-gate 		switch (event) {
17947c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ENABLE:
17957c478bd9Sstevel@tonic-gate 			/*
17967c478bd9Sstevel@tonic-gate 			 * The instance needs enabling. Commence reading its
17977c478bd9Sstevel@tonic-gate 			 * configuration and if successful place the instance
17987c478bd9Sstevel@tonic-gate 			 * in the offline state and let process_offline_inst()
17997c478bd9Sstevel@tonic-gate 			 * take it from there.
18007c478bd9Sstevel@tonic-gate 			 */
18017c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(instance->config);
18027c478bd9Sstevel@tonic-gate 			instance->config = read_instance_cfg(instance->fmri);
18037c478bd9Sstevel@tonic-gate 			if (instance->config != NULL) {
18047c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_OFFLINE,
18057c478bd9Sstevel@tonic-gate 				    RERR_RESTART);
18067c478bd9Sstevel@tonic-gate 				process_offline_inst(instance);
18077c478bd9Sstevel@tonic-gate 			} else {
18087c478bd9Sstevel@tonic-gate 				log_invalid_cfg(instance->fmri);
18097c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_MAINTENANCE,
18107c478bd9Sstevel@tonic-gate 				    RERR_RESTART);
18117c478bd9Sstevel@tonic-gate 			}
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 			break;
18147c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
18157c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
18167c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
18177c478bd9Sstevel@tonic-gate 			/*
18187c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance be
18197c478bd9Sstevel@tonic-gate 			 * placed in the maintenance state, so just update its
18207c478bd9Sstevel@tonic-gate 			 * state to maintenance.
18217c478bd9Sstevel@tonic-gate 			 */
18227c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
18237c478bd9Sstevel@tonic-gate 			break;
18247c478bd9Sstevel@tonic-gate 		}
18257c478bd9Sstevel@tonic-gate 		break;
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	case IIS_MAINTENANCE:
18287c478bd9Sstevel@tonic-gate 		switch (event) {
18297c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
18307c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
18317c478bd9Sstevel@tonic-gate 			/*
18327c478bd9Sstevel@tonic-gate 			 * The master restarter has requested that the instance
18337c478bd9Sstevel@tonic-gate 			 * be taken out of maintenance. Read its configuration,
18347c478bd9Sstevel@tonic-gate 			 * and if successful place the instance in the offline
18357c478bd9Sstevel@tonic-gate 			 * state and call process_offline_inst() to take it
18367c478bd9Sstevel@tonic-gate 			 * from there.
18377c478bd9Sstevel@tonic-gate 			 */
18387c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(instance->config);
18397c478bd9Sstevel@tonic-gate 			instance->config = read_instance_cfg(instance->fmri);
18407c478bd9Sstevel@tonic-gate 			if (instance->config != NULL) {
18417c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_OFFLINE,
18427c478bd9Sstevel@tonic-gate 				    RERR_RESTART);
18437c478bd9Sstevel@tonic-gate 				process_offline_inst(instance);
18447c478bd9Sstevel@tonic-gate 			} else {
18457c478bd9Sstevel@tonic-gate 				boolean_t enabled;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 				/*
18487c478bd9Sstevel@tonic-gate 				 * The configuration was invalid. If the
18497c478bd9Sstevel@tonic-gate 				 * service has disabled requested, let's
18507c478bd9Sstevel@tonic-gate 				 * just place the instance in disabled even
18517c478bd9Sstevel@tonic-gate 				 * though we haven't been able to run its
18527c478bd9Sstevel@tonic-gate 				 * disable method, as the slightly incorrect
18537c478bd9Sstevel@tonic-gate 				 * state is likely to be less of an issue to
18547c478bd9Sstevel@tonic-gate 				 * an administrator than refusing to move an
18557c478bd9Sstevel@tonic-gate 				 * instance to disabled. If disable isn't
18567c478bd9Sstevel@tonic-gate 				 * requested, re-mark the service's state
18577c478bd9Sstevel@tonic-gate 				 * as maintenance, so the administrator can
18587c478bd9Sstevel@tonic-gate 				 * see the request was processed.
18597c478bd9Sstevel@tonic-gate 				 */
18607c478bd9Sstevel@tonic-gate 				if ((read_enable_merged(instance->fmri,
18617c478bd9Sstevel@tonic-gate 				    &enabled) == 0) && !enabled) {
18627c478bd9Sstevel@tonic-gate 					update_state(instance, IIS_DISABLED,
18637c478bd9Sstevel@tonic-gate 					    RERR_RESTART);
18647c478bd9Sstevel@tonic-gate 				} else {
18657c478bd9Sstevel@tonic-gate 					log_invalid_cfg(instance->fmri);
18667c478bd9Sstevel@tonic-gate 					update_state(instance, IIS_MAINTENANCE,
18677c478bd9Sstevel@tonic-gate 					    RERR_FAULT);
18687c478bd9Sstevel@tonic-gate 				}
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 			break;
18717c478bd9Sstevel@tonic-gate 		}
18727c478bd9Sstevel@tonic-gate 		break;
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_CONRATE:
18757c478bd9Sstevel@tonic-gate 		switch (event) {
18767c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
18777c478bd9Sstevel@tonic-gate 			/*
18787c478bd9Sstevel@tonic-gate 			 * The instance wants disabling. Take the instance
18797c478bd9Sstevel@tonic-gate 			 * offline as for the dependencies unmet event above,
18807c478bd9Sstevel@tonic-gate 			 * and then from there run the disable method to do
18817c478bd9Sstevel@tonic-gate 			 * the work to take the instance to the disabled state.
18827c478bd9Sstevel@tonic-gate 			 */
18837c478bd9Sstevel@tonic-gate 			cancel_inst_timer(instance);
18847c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
18857c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
18867c478bd9Sstevel@tonic-gate 			break;
18877c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
18887c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
18897c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
18907c478bd9Sstevel@tonic-gate 			/*
18917c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance
18927c478bd9Sstevel@tonic-gate 			 * be taken to maintenance. Cancel the timer setup
18937c478bd9Sstevel@tonic-gate 			 * when we entered this state, and go directly to
18947c478bd9Sstevel@tonic-gate 			 * maintenance.
18957c478bd9Sstevel@tonic-gate 			 */
18967c478bd9Sstevel@tonic-gate 			cancel_inst_timer(instance);
18977c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
18987c478bd9Sstevel@tonic-gate 			break;
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 		break;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_COPIES:
19037c478bd9Sstevel@tonic-gate 		switch (event) {
19047c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
19057c478bd9Sstevel@tonic-gate 			/*
19067c478bd9Sstevel@tonic-gate 			 * The instance wants disabling. Update the state
19077c478bd9Sstevel@tonic-gate 			 * to offline, and run the disable method to do the
19087c478bd9Sstevel@tonic-gate 			 * work to take it to the disabled state.
19097c478bd9Sstevel@tonic-gate 			 */
19107c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
19117c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
19127c478bd9Sstevel@tonic-gate 			break;
19137c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
19147c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
19157c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
19167c478bd9Sstevel@tonic-gate 			/*
19177c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance be
19187c478bd9Sstevel@tonic-gate 			 * placed in maintenance. Since it's already offline
19197c478bd9Sstevel@tonic-gate 			 * simply update the state.
19207c478bd9Sstevel@tonic-gate 			 */
19217c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
19227c478bd9Sstevel@tonic-gate 			break;
19237c478bd9Sstevel@tonic-gate 		}
19247c478bd9Sstevel@tonic-gate 		break;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	default:
19277c478bd9Sstevel@tonic-gate 		debug_msg("handle_restarter_event: instance in an "
19287c478bd9Sstevel@tonic-gate 		    "unexpected state");
19297c478bd9Sstevel@tonic-gate 		assert(0);
19307c478bd9Sstevel@tonic-gate 	}
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate done:
19337c478bd9Sstevel@tonic-gate 	if (send_ack)
19347c478bd9Sstevel@tonic-gate 		ack_restarter_event(B_TRUE);
19357c478bd9Sstevel@tonic-gate }
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate /*
19387c478bd9Sstevel@tonic-gate  * Tries to read and process an event from the event pipe. If there isn't one
19397c478bd9Sstevel@tonic-gate  * or an error occurred processing the event it returns -1. Else, if the event
19407c478bd9Sstevel@tonic-gate  * is for an instance we're not already managing we read its state, add it to
19417c478bd9Sstevel@tonic-gate  * our list to manage, and if appropriate read its configuration. Whether it's
19427c478bd9Sstevel@tonic-gate  * new to us or not, we then handle the specific event.
19437c478bd9Sstevel@tonic-gate  * Returns 0 if an event was read and processed successfully, else -1.
19447c478bd9Sstevel@tonic-gate  */
19457c478bd9Sstevel@tonic-gate static int
19467c478bd9Sstevel@tonic-gate process_restarter_event(void)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate 	char			*fmri;
19497c478bd9Sstevel@tonic-gate 	size_t			fmri_size;
19507c478bd9Sstevel@tonic-gate 	restarter_event_type_t  event_type;
19517c478bd9Sstevel@tonic-gate 	instance_t		*instance;
19527c478bd9Sstevel@tonic-gate 	restarter_event_t	*event;
19537c478bd9Sstevel@tonic-gate 	ssize_t			sz;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	/*
19567c478bd9Sstevel@tonic-gate 	 * Try to read an event pointer from the event pipe.
19577c478bd9Sstevel@tonic-gate 	 */
19587c478bd9Sstevel@tonic-gate 	errno = 0;
19597c478bd9Sstevel@tonic-gate 	switch (safe_read(rst_event_pipe[PE_CONSUMER], &event,
19607c478bd9Sstevel@tonic-gate 	    sizeof (event))) {
19617c478bd9Sstevel@tonic-gate 	case 0:
19627c478bd9Sstevel@tonic-gate 		break;
19637c478bd9Sstevel@tonic-gate 	case  1:
19647c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN)	/* no event to read */
19657c478bd9Sstevel@tonic-gate 			return (-1);
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 		/* other end of pipe closed */
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
19707c478bd9Sstevel@tonic-gate 	default:			/* unexpected read error */
19717c478bd9Sstevel@tonic-gate 		/*
19727c478bd9Sstevel@tonic-gate 		 * There's something wrong with the event pipe. Let's
19737c478bd9Sstevel@tonic-gate 		 * shutdown and be restarted.
19747c478bd9Sstevel@tonic-gate 		 */
19757c478bd9Sstevel@tonic-gate 		inetd_stop();
19767c478bd9Sstevel@tonic-gate 		return (-1);
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	/*
19807c478bd9Sstevel@tonic-gate 	 * Check if we're currently managing the instance which the event
19817c478bd9Sstevel@tonic-gate 	 * pertains to. If not, read its complete state and add it to our
19827c478bd9Sstevel@tonic-gate 	 * list to manage.
19837c478bd9Sstevel@tonic-gate 	 */
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
19867c478bd9Sstevel@tonic-gate 	if ((fmri = malloc(fmri_size)) == NULL) {
19877c478bd9Sstevel@tonic-gate 		error_msg(strerror(errno));
19887c478bd9Sstevel@tonic-gate 		goto fail;
19897c478bd9Sstevel@tonic-gate 	}
19907c478bd9Sstevel@tonic-gate 	sz = restarter_event_get_instance(event, fmri, fmri_size);
19917c478bd9Sstevel@tonic-gate 	if (sz >= fmri_size)
19927c478bd9Sstevel@tonic-gate 		assert(0);
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	for (instance = uu_list_first(instance_list); instance != NULL;
19957c478bd9Sstevel@tonic-gate 	    instance = uu_list_next(instance_list, instance)) {
19967c478bd9Sstevel@tonic-gate 		if (strcmp(instance->fmri, fmri) == 0)
19977c478bd9Sstevel@tonic-gate 			break;
19987c478bd9Sstevel@tonic-gate 	}
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	if (instance == NULL) {
20017c478bd9Sstevel@tonic-gate 		int err;
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 		debug_msg("New instance to manage: %s", fmri);
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 		if (((instance = create_instance(fmri)) == NULL) ||
20067c478bd9Sstevel@tonic-gate 		    (retrieve_instance_state(instance) != 0) ||
20077c478bd9Sstevel@tonic-gate 		    (retrieve_method_pids(instance) != 0)) {
20087c478bd9Sstevel@tonic-gate 			destroy_instance(instance);
20097c478bd9Sstevel@tonic-gate 			free(fmri);
20107c478bd9Sstevel@tonic-gate 			goto fail;
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 
201394501b61Sskamm 		if (((err = iterate_repository_contracts(instance, 0))
20147c478bd9Sstevel@tonic-gate 		    != 0) && (err != ENOENT)) {
20157c478bd9Sstevel@tonic-gate 			error_msg(gettext(
20167c478bd9Sstevel@tonic-gate 			    "Failed to adopt contracts of instance %s: %s"),
20177c478bd9Sstevel@tonic-gate 			    instance->fmri, strerror(err));
20187c478bd9Sstevel@tonic-gate 			destroy_instance(instance);
20197c478bd9Sstevel@tonic-gate 			free(fmri);
20207c478bd9Sstevel@tonic-gate 			goto fail;
20217c478bd9Sstevel@tonic-gate 		}
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 		uu_list_node_init(instance, &instance->link, instance_pool);
20247c478bd9Sstevel@tonic-gate 		(void) uu_list_insert_after(instance_list, NULL, instance);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 		/*
20277c478bd9Sstevel@tonic-gate 		 * Only read configuration for instances that aren't in any of
20287c478bd9Sstevel@tonic-gate 		 * the disabled, maintenance or uninitialized states, since
20297c478bd9Sstevel@tonic-gate 		 * they'll read it on state exit.
20307c478bd9Sstevel@tonic-gate 		 */
20317c478bd9Sstevel@tonic-gate 		if ((instance->cur_istate != IIS_DISABLED) &&
20327c478bd9Sstevel@tonic-gate 		    (instance->cur_istate != IIS_MAINTENANCE) &&
20337c478bd9Sstevel@tonic-gate 		    (instance->cur_istate != IIS_UNINITIALIZED)) {
20347c478bd9Sstevel@tonic-gate 			instance->config = read_instance_cfg(instance->fmri);
20357c478bd9Sstevel@tonic-gate 			if (instance->config == NULL) {
20367c478bd9Sstevel@tonic-gate 				log_invalid_cfg(instance->fmri);
20377c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_MAINTENANCE,
20387c478bd9Sstevel@tonic-gate 				    RERR_FAULT);
20397c478bd9Sstevel@tonic-gate 			}
20407c478bd9Sstevel@tonic-gate 		}
20417c478bd9Sstevel@tonic-gate 	}
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	free(fmri);
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	event_type = restarter_event_get_type(event);
20467c478bd9Sstevel@tonic-gate 	debug_msg("Event type: %d for instance: %s", event_type,
20477c478bd9Sstevel@tonic-gate 	    instance->fmri);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	/*
20507c478bd9Sstevel@tonic-gate 	 * If the instance is currently running a method, don't process the
20517c478bd9Sstevel@tonic-gate 	 * event now, but attach it to the instance for processing when
20527c478bd9Sstevel@tonic-gate 	 * the instance finishes its transition.
20537c478bd9Sstevel@tonic-gate 	 */
20547c478bd9Sstevel@tonic-gate 	if (INST_IN_TRANSITION(instance)) {
20557c478bd9Sstevel@tonic-gate 		debug_msg("storing event %d for instance %s", event_type,
20567c478bd9Sstevel@tonic-gate 		    instance->fmri);
20577c478bd9Sstevel@tonic-gate 		instance->pending_rst_event = event_type;
20587c478bd9Sstevel@tonic-gate 	} else {
20597c478bd9Sstevel@tonic-gate 		handle_restarter_event(instance, event_type, B_TRUE);
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	return (0);
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate fail:
20657c478bd9Sstevel@tonic-gate 	ack_restarter_event(B_FALSE);
20667c478bd9Sstevel@tonic-gate 	return (-1);
20677c478bd9Sstevel@tonic-gate }
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate /*
20707c478bd9Sstevel@tonic-gate  * Do the state machine processing associated with the termination of instance
20717c478bd9Sstevel@tonic-gate  * 'inst''s start method.
20727c478bd9Sstevel@tonic-gate  */
20737c478bd9Sstevel@tonic-gate void
20747c478bd9Sstevel@tonic-gate process_start_term(instance_t *inst)
20757c478bd9Sstevel@tonic-gate {
20767c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg;
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	inst->copies--;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	if ((inst->cur_istate == IIS_MAINTENANCE) ||
20817c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_DISABLED)) {
20827c478bd9Sstevel@tonic-gate 		/* do any further processing/checks when we exit these states */
20837c478bd9Sstevel@tonic-gate 		return;
20847c478bd9Sstevel@tonic-gate 	}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	cfg = inst->config->basic;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	if (cfg->iswait) {
20897c478bd9Sstevel@tonic-gate 		proto_info_t	*pi;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		switch (inst->cur_istate) {
20927c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
20937c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
20947c478bd9Sstevel@tonic-gate 		case IIS_IN_REFRESH_METHOD:
20957c478bd9Sstevel@tonic-gate 			/*
20967c478bd9Sstevel@tonic-gate 			 * A wait type service's start method has exited.
20977c478bd9Sstevel@tonic-gate 			 * Check if the method was fired off in this inetd's
20987c478bd9Sstevel@tonic-gate 			 * lifetime, or a previous one; if the former,
20997c478bd9Sstevel@tonic-gate 			 * re-commence listening on the service's behalf; if
21007c478bd9Sstevel@tonic-gate 			 * the latter, mark the service offline and let bind
21017c478bd9Sstevel@tonic-gate 			 * attempts commence.
21027c478bd9Sstevel@tonic-gate 			 */
21037c478bd9Sstevel@tonic-gate 			for (pi = uu_list_first(cfg->proto_list); pi != NULL;
21047c478bd9Sstevel@tonic-gate 			    pi = uu_list_next(cfg->proto_list, pi)) {
21057c478bd9Sstevel@tonic-gate 				/*
21067c478bd9Sstevel@tonic-gate 				 * If a bound fd exists, the method was fired
21077c478bd9Sstevel@tonic-gate 				 * off during this inetd's lifetime.
21087c478bd9Sstevel@tonic-gate 				 */
21097c478bd9Sstevel@tonic-gate 				if (pi->listen_fd != -1)
21107c478bd9Sstevel@tonic-gate 					break;
21117c478bd9Sstevel@tonic-gate 			}
21127c478bd9Sstevel@tonic-gate 			if (pi != NULL) {
21137c478bd9Sstevel@tonic-gate 				if (poll_bound_fds(inst, B_TRUE) != 0)
21147c478bd9Sstevel@tonic-gate 					handle_bind_failure(inst);
21157c478bd9Sstevel@tonic-gate 			} else {
21167c478bd9Sstevel@tonic-gate 				update_state(inst, IIS_OFFLINE, RERR_RESTART);
21177c478bd9Sstevel@tonic-gate 				create_bound_fds(inst);
21187c478bd9Sstevel@tonic-gate 			}
21197c478bd9Sstevel@tonic-gate 		}
21207c478bd9Sstevel@tonic-gate 	} else {
21217c478bd9Sstevel@tonic-gate 		/*
21227c478bd9Sstevel@tonic-gate 		 * Check if a nowait service should be brought back online
21237c478bd9Sstevel@tonic-gate 		 * after exceeding its copies limit.
21247c478bd9Sstevel@tonic-gate 		 */
21257c478bd9Sstevel@tonic-gate 		if ((inst->cur_istate == IIS_OFFLINE_COPIES) &&
21267c478bd9Sstevel@tonic-gate 		    !copies_limit_exceeded(inst)) {
21277c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE, RERR_NONE);
21287c478bd9Sstevel@tonic-gate 			process_offline_inst(inst);
21297c478bd9Sstevel@tonic-gate 		}
21307c478bd9Sstevel@tonic-gate 	}
21317c478bd9Sstevel@tonic-gate }
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate /*
21347c478bd9Sstevel@tonic-gate  * If the instance has a pending event process it and initiate the
21357c478bd9Sstevel@tonic-gate  * acknowledgement.
21367c478bd9Sstevel@tonic-gate  */
21377c478bd9Sstevel@tonic-gate static void
21387c478bd9Sstevel@tonic-gate process_pending_rst_event(instance_t *inst)
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate 	if (inst->pending_rst_event != RESTARTER_EVENT_TYPE_INVALID) {
21417c478bd9Sstevel@tonic-gate 		restarter_event_type_t re;
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 		debug_msg("Injecting pending event %d for instance %s",
21447c478bd9Sstevel@tonic-gate 		    inst->pending_rst_event, inst->fmri);
21457c478bd9Sstevel@tonic-gate 		re = inst->pending_rst_event;
21467c478bd9Sstevel@tonic-gate 		inst->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID;
21477c478bd9Sstevel@tonic-gate 		handle_restarter_event(inst, re, B_TRUE);
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate }
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate /*
21527c478bd9Sstevel@tonic-gate  * Do the state machine processing associated with the termination
21537c478bd9Sstevel@tonic-gate  * of the specified instance's non-start method with the specified status.
21547c478bd9Sstevel@tonic-gate  * Once the processing of the termination is done, the function also picks up
21557c478bd9Sstevel@tonic-gate  * any processing that was blocked on the method running.
21567c478bd9Sstevel@tonic-gate  */
21577c478bd9Sstevel@tonic-gate void
21587c478bd9Sstevel@tonic-gate process_non_start_term(instance_t *inst, int status)
21597c478bd9Sstevel@tonic-gate {
21607c478bd9Sstevel@tonic-gate 	boolean_t ran_online_method = B_FALSE;
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	if (status == IMRET_FAILURE) {
21637c478bd9Sstevel@tonic-gate 		error_msg(gettext("The %s method of instance %s failed, "
21647c478bd9Sstevel@tonic-gate 		    "transitioning to maintenance"),
21657c478bd9Sstevel@tonic-gate 		    methods[states[inst->cur_istate].method_running].name,
21667c478bd9Sstevel@tonic-gate 		    inst->fmri);
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 		if ((inst->cur_istate == IIS_IN_ONLINE_METHOD) ||
21697c478bd9Sstevel@tonic-gate 		    (inst->cur_istate == IIS_IN_REFRESH_METHOD))
21707c478bd9Sstevel@tonic-gate 			destroy_bound_fds(inst);
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 		update_state(inst, IIS_MAINTENANCE, RERR_FAULT);
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 		inst->maintenance_req = B_FALSE;
21757c478bd9Sstevel@tonic-gate 		inst->conn_rate_exceeded = B_FALSE;
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		if (inst->new_config != NULL) {
21787c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(inst->new_config);
21797c478bd9Sstevel@tonic-gate 			inst->new_config = NULL;
21807c478bd9Sstevel@tonic-gate 		}
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 		if (!inetd_stopping)
21837c478bd9Sstevel@tonic-gate 			process_pending_rst_event(inst);
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 		return;
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	/* non-failure method return */
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	if (status != IMRET_SUCCESS) {
21917c478bd9Sstevel@tonic-gate 		/*
21927c478bd9Sstevel@tonic-gate 		 * An instance method never returned a supported return code.
21937c478bd9Sstevel@tonic-gate 		 * We'll assume this means the method succeeded for now whilst
21947c478bd9Sstevel@tonic-gate 		 * non-GL-cognizant methods are used - eg. pkill.
21957c478bd9Sstevel@tonic-gate 		 */
21967c478bd9Sstevel@tonic-gate 		debug_msg("The %s method of instance %s returned "
21977c478bd9Sstevel@tonic-gate 		    "non-compliant exit code: %d, assuming success",
21987c478bd9Sstevel@tonic-gate 		    methods[states[inst->cur_istate].method_running].name,
21997c478bd9Sstevel@tonic-gate 		    inst->fmri, status);
22007c478bd9Sstevel@tonic-gate 	}
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	/*
22037c478bd9Sstevel@tonic-gate 	 * Update the state from the in-transition state.
22047c478bd9Sstevel@tonic-gate 	 */
22057c478bd9Sstevel@tonic-gate 	switch (inst->cur_istate) {
22067c478bd9Sstevel@tonic-gate 	case IIS_IN_ONLINE_METHOD:
22077c478bd9Sstevel@tonic-gate 		ran_online_method = B_TRUE;
22087c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
22097c478bd9Sstevel@tonic-gate 	case IIS_IN_REFRESH_METHOD:
22107c478bd9Sstevel@tonic-gate 		/*
22117c478bd9Sstevel@tonic-gate 		 * If we've exhausted the bind retries, flag that by setting
22127c478bd9Sstevel@tonic-gate 		 * the instance's state to degraded.
22137c478bd9Sstevel@tonic-gate 		 */
22147c478bd9Sstevel@tonic-gate 		if (inst->bind_retries_exceeded) {
22157c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_DEGRADED, RERR_NONE);
22167c478bd9Sstevel@tonic-gate 			break;
22177c478bd9Sstevel@tonic-gate 		}
22187c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
22197c478bd9Sstevel@tonic-gate 	default:
22207c478bd9Sstevel@tonic-gate 		update_state(inst,
22217c478bd9Sstevel@tonic-gate 		    methods[states[inst->cur_istate].method_running].dst_state,
22227c478bd9Sstevel@tonic-gate 		    RERR_NONE);
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	if (inst->cur_istate == IIS_OFFLINE) {
22267c478bd9Sstevel@tonic-gate 		if (inst->new_config != NULL) {
22277c478bd9Sstevel@tonic-gate 			/*
22287c478bd9Sstevel@tonic-gate 			 * This instance was found during refresh to need
22297c478bd9Sstevel@tonic-gate 			 * taking offline because its newly read configuration
22307c478bd9Sstevel@tonic-gate 			 * was sufficiently different. Now we're offline,
22317c478bd9Sstevel@tonic-gate 			 * activate this new configuration.
22327c478bd9Sstevel@tonic-gate 			 */
22337c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(inst->config);
22347c478bd9Sstevel@tonic-gate 			inst->config = inst->new_config;
22357c478bd9Sstevel@tonic-gate 			inst->new_config = NULL;
22367c478bd9Sstevel@tonic-gate 		}
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 		/* continue/complete any transitions that are in progress */
22397c478bd9Sstevel@tonic-gate 		process_offline_inst(inst);
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	} else if (ran_online_method) {
22427c478bd9Sstevel@tonic-gate 		/*
22437c478bd9Sstevel@tonic-gate 		 * We've just successfully executed the online method. We have
22447c478bd9Sstevel@tonic-gate 		 * a set of bound network fds that were created before running
22457c478bd9Sstevel@tonic-gate 		 * this method, so now we're online start listening for
22467c478bd9Sstevel@tonic-gate 		 * connections on them.
22477c478bd9Sstevel@tonic-gate 		 */
22487c478bd9Sstevel@tonic-gate 		if (poll_bound_fds(inst, B_TRUE) != 0)
22497c478bd9Sstevel@tonic-gate 			handle_bind_failure(inst);
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	/*
22537c478bd9Sstevel@tonic-gate 	 * If we're now out of transition (process_offline_inst() could have
22547c478bd9Sstevel@tonic-gate 	 * fired off another method), carry out any jobs that were blocked by
22557c478bd9Sstevel@tonic-gate 	 * us being in transition.
22567c478bd9Sstevel@tonic-gate 	 */
22577c478bd9Sstevel@tonic-gate 	if (!INST_IN_TRANSITION(inst)) {
22587c478bd9Sstevel@tonic-gate 		if (inetd_stopping) {
22597c478bd9Sstevel@tonic-gate 			if (!instance_stopped(inst)) {
22607c478bd9Sstevel@tonic-gate 				/*
22617c478bd9Sstevel@tonic-gate 				 * inetd is stopping, and this instance hasn't
22627c478bd9Sstevel@tonic-gate 				 * been stopped. Inject a stop event.
22637c478bd9Sstevel@tonic-gate 				 */
22647c478bd9Sstevel@tonic-gate 				handle_restarter_event(inst,
22657c478bd9Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_STOP, B_FALSE);
22667c478bd9Sstevel@tonic-gate 			}
22677c478bd9Sstevel@tonic-gate 		} else {
22687c478bd9Sstevel@tonic-gate 			process_pending_rst_event(inst);
22697c478bd9Sstevel@tonic-gate 		}
22707c478bd9Sstevel@tonic-gate 	}
22717c478bd9Sstevel@tonic-gate }
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate /*
22747c478bd9Sstevel@tonic-gate  * Check if configuration file specified is readable. If not return B_FALSE,
22757c478bd9Sstevel@tonic-gate  * else return B_TRUE.
22767c478bd9Sstevel@tonic-gate  */
22777c478bd9Sstevel@tonic-gate static boolean_t
22787c478bd9Sstevel@tonic-gate can_read_file(const char *path)
22797c478bd9Sstevel@tonic-gate {
22807c478bd9Sstevel@tonic-gate 	int	ret;
22817c478bd9Sstevel@tonic-gate 	int	serrno;
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	do {
22847c478bd9Sstevel@tonic-gate 		ret = access(path, R_OK);
22857c478bd9Sstevel@tonic-gate 	} while ((ret < 0) && (errno == EINTR));
22867c478bd9Sstevel@tonic-gate 	if (ret < 0) {
22877c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
22887c478bd9Sstevel@tonic-gate 			serrno = errno;
22897c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to access configuration "
22907c478bd9Sstevel@tonic-gate 			    "file %s for performing modification checks: %s"),
22917c478bd9Sstevel@tonic-gate 			    path, strerror(errno));
22927c478bd9Sstevel@tonic-gate 			errno = serrno;
22937c478bd9Sstevel@tonic-gate 		}
22947c478bd9Sstevel@tonic-gate 		return (B_FALSE);
22957c478bd9Sstevel@tonic-gate 	}
22967c478bd9Sstevel@tonic-gate 	return (B_TRUE);
22977c478bd9Sstevel@tonic-gate }
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate /*
23007c478bd9Sstevel@tonic-gate  * Check whether the configuration file has changed contents since inetd
23017c478bd9Sstevel@tonic-gate  * was last started/refreshed, and if so, log a message indicating that
23027c478bd9Sstevel@tonic-gate  * inetconv needs to be run.
23037c478bd9Sstevel@tonic-gate  */
23047c478bd9Sstevel@tonic-gate static void
23057c478bd9Sstevel@tonic-gate check_conf_file(void)
23067c478bd9Sstevel@tonic-gate {
23077c478bd9Sstevel@tonic-gate 	char		*new_hash;
23087c478bd9Sstevel@tonic-gate 	char		*old_hash = NULL;
23097c478bd9Sstevel@tonic-gate 	scf_error_t	ret;
23107c478bd9Sstevel@tonic-gate 	const char	*file;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	if (conf_file == NULL) {
23137c478bd9Sstevel@tonic-gate 		/*
23147c478bd9Sstevel@tonic-gate 		 * No explicit config file specified, so see if one of the
23157c478bd9Sstevel@tonic-gate 		 * default two are readable, checking the primary one first
23167c478bd9Sstevel@tonic-gate 		 * followed by the secondary.
23177c478bd9Sstevel@tonic-gate 		 */
23187c478bd9Sstevel@tonic-gate 		if (can_read_file(PRIMARY_DEFAULT_CONF_FILE)) {
23197c478bd9Sstevel@tonic-gate 			file = PRIMARY_DEFAULT_CONF_FILE;
23207c478bd9Sstevel@tonic-gate 		} else if ((errno == ENOENT) &&
23217c478bd9Sstevel@tonic-gate 		    can_read_file(SECONDARY_DEFAULT_CONF_FILE)) {
23227c478bd9Sstevel@tonic-gate 			file = SECONDARY_DEFAULT_CONF_FILE;
23237c478bd9Sstevel@tonic-gate 		} else {
23247c478bd9Sstevel@tonic-gate 			return;
23257c478bd9Sstevel@tonic-gate 		}
23267c478bd9Sstevel@tonic-gate 	} else {
23277c478bd9Sstevel@tonic-gate 		file = conf_file;
23287c478bd9Sstevel@tonic-gate 		if (!can_read_file(file))
23297c478bd9Sstevel@tonic-gate 			return;
23307c478bd9Sstevel@tonic-gate 	}
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 	if (calculate_hash(file, &new_hash) == 0) {
23337c478bd9Sstevel@tonic-gate 		ret = retrieve_inetd_hash(&old_hash);
23347c478bd9Sstevel@tonic-gate 		if (((ret == SCF_ERROR_NONE) &&
23357c478bd9Sstevel@tonic-gate 		    (strcmp(old_hash, new_hash) != 0))) {
23367c478bd9Sstevel@tonic-gate 			/* modified config file */
23377c478bd9Sstevel@tonic-gate 			warn_msg(gettext(
23387c478bd9Sstevel@tonic-gate 			    "Configuration file %s has been modified since "
23397c478bd9Sstevel@tonic-gate 			    "inetconv was last run. \"inetconv -i %s\" must be "
23407c478bd9Sstevel@tonic-gate 			    "run to apply any changes to the SMF"), file, file);
23417c478bd9Sstevel@tonic-gate 		} else if ((ret != SCF_ERROR_NOT_FOUND) &&
23427c478bd9Sstevel@tonic-gate 		    (ret != SCF_ERROR_NONE)) {
23437c478bd9Sstevel@tonic-gate 			/* No message if hash not yet computed */
23447c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to check whether "
23457c478bd9Sstevel@tonic-gate 			    "configuration file %s has been modified: %s"),
23467c478bd9Sstevel@tonic-gate 			    file, scf_strerror(ret));
23477c478bd9Sstevel@tonic-gate 		}
23487c478bd9Sstevel@tonic-gate 		free(old_hash);
23497c478bd9Sstevel@tonic-gate 		free(new_hash);
23507c478bd9Sstevel@tonic-gate 	} else {
23517c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to check whether configuration file "
23527c478bd9Sstevel@tonic-gate 		    "%s has been modified: %s"), file, strerror(errno));
23537c478bd9Sstevel@tonic-gate 	}
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate /*
23577c478bd9Sstevel@tonic-gate  * Refresh all inetd's managed instances and check the configuration file
23587c478bd9Sstevel@tonic-gate  * for any updates since inetconv was last run, logging a message if there
23597c478bd9Sstevel@tonic-gate  * are. We call the SMF refresh function to refresh each instance so that
23607c478bd9Sstevel@tonic-gate  * the refresh request goes through the framework, and thus results in the
23617c478bd9Sstevel@tonic-gate  * running snapshot of each instance being updated from the configuration
23627c478bd9Sstevel@tonic-gate  * snapshot.
23637c478bd9Sstevel@tonic-gate  */
23647c478bd9Sstevel@tonic-gate static void
23657c478bd9Sstevel@tonic-gate inetd_refresh(void)
23667c478bd9Sstevel@tonic-gate {
23677c478bd9Sstevel@tonic-gate 	instance_t	*inst;
23687c478bd9Sstevel@tonic-gate 
2369eed64e98Sgm 	refresh_debug_flag();
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	/* call libscf to send refresh requests for all managed instances */
23727c478bd9Sstevel@tonic-gate 	for (inst = uu_list_first(instance_list); inst != NULL;
23737c478bd9Sstevel@tonic-gate 	    inst = uu_list_next(instance_list, inst)) {
23747c478bd9Sstevel@tonic-gate 		if (smf_refresh_instance(inst->fmri) < 0) {
23757c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to refresh instance %s: %s"),
23767c478bd9Sstevel@tonic-gate 			    inst->fmri, scf_strerror(scf_error()));
23777c478bd9Sstevel@tonic-gate 		}
23787c478bd9Sstevel@tonic-gate 	}
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	/*
23817c478bd9Sstevel@tonic-gate 	 * Log a message if the configuration file has changed since inetconv
23827c478bd9Sstevel@tonic-gate 	 * was last run.
23837c478bd9Sstevel@tonic-gate 	 */
23847c478bd9Sstevel@tonic-gate 	check_conf_file();
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate /*
23887c478bd9Sstevel@tonic-gate  * Initiate inetd's shutdown.
23897c478bd9Sstevel@tonic-gate  */
23907c478bd9Sstevel@tonic-gate static void
23917c478bd9Sstevel@tonic-gate inetd_stop(void)
23927c478bd9Sstevel@tonic-gate {
23937c478bd9Sstevel@tonic-gate 	instance_t *inst;
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	/* Block handling signals for stop and refresh */
23967c478bd9Sstevel@tonic-gate 	(void) sighold(SIGHUP);
23977c478bd9Sstevel@tonic-gate 	(void) sighold(SIGTERM);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	/* Indicate inetd is coming down */
24007c478bd9Sstevel@tonic-gate 	inetd_stopping = B_TRUE;
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	/* Stop polling on restarter events. */
24037c478bd9Sstevel@tonic-gate 	clear_pollfd(rst_event_pipe[PE_CONSUMER]);
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	/* Stop polling for any more stop/refresh requests. */
24067c478bd9Sstevel@tonic-gate 	clear_pollfd(uds_fd);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	/*
24097c478bd9Sstevel@tonic-gate 	 * Send a stop event to all currently unstopped instances that
24107c478bd9Sstevel@tonic-gate 	 * aren't in transition. For those that are in transition, the
24117c478bd9Sstevel@tonic-gate 	 * event will get sent when the transition completes.
24127c478bd9Sstevel@tonic-gate 	 */
24137c478bd9Sstevel@tonic-gate 	for (inst = uu_list_first(instance_list); inst != NULL;
24147c478bd9Sstevel@tonic-gate 	    inst = uu_list_next(instance_list, inst)) {
24157c478bd9Sstevel@tonic-gate 		if (!instance_stopped(inst) && !INST_IN_TRANSITION(inst))
24167c478bd9Sstevel@tonic-gate 			handle_restarter_event(inst,
24177c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_STOP, B_FALSE);
24187c478bd9Sstevel@tonic-gate 	}
24197c478bd9Sstevel@tonic-gate }
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate /*
24227c478bd9Sstevel@tonic-gate  * Sets up the intra-inetd-process Unix Domain Socket.
24237c478bd9Sstevel@tonic-gate  * Returns -1 on error, else 0.
24247c478bd9Sstevel@tonic-gate  */
24257c478bd9Sstevel@tonic-gate static int
24267c478bd9Sstevel@tonic-gate uds_init(void)
24277c478bd9Sstevel@tonic-gate {
24287c478bd9Sstevel@tonic-gate 	struct sockaddr_un addr;
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	if ((uds_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
24317c478bd9Sstevel@tonic-gate 		error_msg("socket: %s", strerror(errno));
24327c478bd9Sstevel@tonic-gate 		return (-1);
24337c478bd9Sstevel@tonic-gate 	}
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	disable_blocking(uds_fd);
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	(void) unlink(INETD_UDS_PATH);  /* clean-up any stale files */
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 	(void) memset(&addr, 0, sizeof (addr));
24407c478bd9Sstevel@tonic-gate 	addr.sun_family = AF_UNIX;
24417c478bd9Sstevel@tonic-gate 	/* CONSTCOND */
24427c478bd9Sstevel@tonic-gate 	assert(sizeof (INETD_UDS_PATH) <= sizeof (addr.sun_path));
24437c478bd9Sstevel@tonic-gate 	(void) strlcpy(addr.sun_path, INETD_UDS_PATH, sizeof (addr.sun_path));
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 	if (bind(uds_fd, (struct sockaddr *)(&addr), sizeof (addr)) < 0) {
24467c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to bind socket to %s: %s"),
24477c478bd9Sstevel@tonic-gate 		    INETD_UDS_PATH, strerror(errno));
24487c478bd9Sstevel@tonic-gate 		(void) close(uds_fd);
24497c478bd9Sstevel@tonic-gate 		return (-1);
24507c478bd9Sstevel@tonic-gate 	}
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	(void) listen(uds_fd, UDS_BACKLOG);
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	if ((set_pollfd(uds_fd, POLLIN)) == -1) {
24557c478bd9Sstevel@tonic-gate 		(void) close(uds_fd);
24567c478bd9Sstevel@tonic-gate 		(void) unlink(INETD_UDS_PATH);
24577c478bd9Sstevel@tonic-gate 		return (-1);
24587c478bd9Sstevel@tonic-gate 	}
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	return (0);
24617c478bd9Sstevel@tonic-gate }
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate static void
24647c478bd9Sstevel@tonic-gate uds_fini(void)
24657c478bd9Sstevel@tonic-gate {
24667c478bd9Sstevel@tonic-gate 	if (uds_fd != -1)
24677c478bd9Sstevel@tonic-gate 		(void) close(uds_fd);
24687c478bd9Sstevel@tonic-gate 	(void) unlink(INETD_UDS_PATH);
24697c478bd9Sstevel@tonic-gate }
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate /*
24727c478bd9Sstevel@tonic-gate  * Handle an incoming request on the Unix Domain Socket. Returns -1 if there
24737c478bd9Sstevel@tonic-gate  * was an error handling the event, else 0.
24747c478bd9Sstevel@tonic-gate  */
24757c478bd9Sstevel@tonic-gate static int
24767c478bd9Sstevel@tonic-gate process_uds_event(void)
24777c478bd9Sstevel@tonic-gate {
24787c478bd9Sstevel@tonic-gate 	uds_request_t		req;
24797c478bd9Sstevel@tonic-gate 	int			fd;
24807c478bd9Sstevel@tonic-gate 	struct sockaddr_un	addr;
24817c478bd9Sstevel@tonic-gate 	socklen_t		len = sizeof (addr);
24827c478bd9Sstevel@tonic-gate 	int			ret;
24837c478bd9Sstevel@tonic-gate 	uint_t			retries = 0;
24847cd7ea0bSrs 	ucred_t			*ucred = NULL;
24857cd7ea0bSrs 	uid_t			euid;
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	do {
24887c478bd9Sstevel@tonic-gate 		fd = accept(uds_fd, (struct sockaddr *)&addr, &len);
24897c478bd9Sstevel@tonic-gate 	} while ((fd < 0) && (errno == EINTR));
24907c478bd9Sstevel@tonic-gate 	if (fd < 0) {
24917c478bd9Sstevel@tonic-gate 		if (errno != EWOULDBLOCK)
24927c478bd9Sstevel@tonic-gate 			error_msg("accept failed: %s", strerror(errno));
24937c478bd9Sstevel@tonic-gate 		return (-1);
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate 
24967cd7ea0bSrs 	if (getpeerucred(fd, &ucred) == -1) {
24977cd7ea0bSrs 		error_msg("getpeerucred failed: %s", strerror(errno));
24987cd7ea0bSrs 		(void) close(fd);
24997cd7ea0bSrs 		return (-1);
25007cd7ea0bSrs 	}
25017cd7ea0bSrs 
25027cd7ea0bSrs 	/* Check peer credentials before acting on the request */
25037cd7ea0bSrs 	euid = ucred_geteuid(ucred);
25047cd7ea0bSrs 	ucred_free(ucred);
25057cd7ea0bSrs 	if (euid != 0 && getuid() != euid) {
25067cd7ea0bSrs 		debug_msg("peer euid %u != uid %u",
25077cd7ea0bSrs 		    (uint_t)euid, (uint_t)getuid());
25087cd7ea0bSrs 		(void) close(fd);
25097cd7ea0bSrs 		return (-1);
25107cd7ea0bSrs 	}
25117cd7ea0bSrs 
25127c478bd9Sstevel@tonic-gate 	for (retries = 0; retries < UDS_RECV_RETRIES; retries++) {
25137c478bd9Sstevel@tonic-gate 		if (((ret = safe_read(fd, &req, sizeof (req))) != 1) ||
25147c478bd9Sstevel@tonic-gate 		    (errno != EAGAIN))
25157c478bd9Sstevel@tonic-gate 			break;
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, 100);	/* 100ms pause */
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	if (ret != 0) {
25217c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed read: %s"), strerror(errno));
25227c478bd9Sstevel@tonic-gate 		(void) close(fd);
25237c478bd9Sstevel@tonic-gate 		return (-1);
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	switch (req) {
25277c478bd9Sstevel@tonic-gate 	case UR_REFRESH_INETD:
25287c478bd9Sstevel@tonic-gate 		/* flag the request for event_loop() to process */
25297c478bd9Sstevel@tonic-gate 		refresh_inetd_requested = B_TRUE;
25307c478bd9Sstevel@tonic-gate 		(void) close(fd);
25317c478bd9Sstevel@tonic-gate 		break;
25327c478bd9Sstevel@tonic-gate 	case UR_STOP_INETD:
25337c478bd9Sstevel@tonic-gate 		inetd_stop();
25347c478bd9Sstevel@tonic-gate 		break;
25357c478bd9Sstevel@tonic-gate 	default:
25367c478bd9Sstevel@tonic-gate 		error_msg("unexpected UDS request");
25377c478bd9Sstevel@tonic-gate 		(void) close(fd);
25387c478bd9Sstevel@tonic-gate 		return (-1);
25397c478bd9Sstevel@tonic-gate 	}
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	return (0);
25427c478bd9Sstevel@tonic-gate }
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate /*
25457c478bd9Sstevel@tonic-gate  * Perform checks for common exec string errors. We limit the checks to
25467c478bd9Sstevel@tonic-gate  * whether the file exists, is a regular file, and has at least one execute
25477c478bd9Sstevel@tonic-gate  * bit set. We leave the core security checks to exec() so as not to duplicate
25487c478bd9Sstevel@tonic-gate  * and thus incur the associated drawbacks, but hope to catch the common
25497c478bd9Sstevel@tonic-gate  * errors here.
25507c478bd9Sstevel@tonic-gate  */
25517c478bd9Sstevel@tonic-gate static boolean_t
25527c478bd9Sstevel@tonic-gate passes_basic_exec_checks(const char *instance, const char *method,
25537c478bd9Sstevel@tonic-gate     const char *path)
25547c478bd9Sstevel@tonic-gate {
25557c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	/* check the file exists */
25587c478bd9Sstevel@tonic-gate 	while (stat(path, &sbuf) == -1) {
25597c478bd9Sstevel@tonic-gate 		if (errno != EINTR) {
25607c478bd9Sstevel@tonic-gate 			error_msg(gettext(
25617c478bd9Sstevel@tonic-gate 			    "Can't stat the %s method of instance %s: %s"),
25627c478bd9Sstevel@tonic-gate 			    method, instance, strerror(errno));
25637c478bd9Sstevel@tonic-gate 			return (B_FALSE);
25647c478bd9Sstevel@tonic-gate 		}
25657c478bd9Sstevel@tonic-gate 	}
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	/*
25687c478bd9Sstevel@tonic-gate 	 * Check if the file is a regular file and has at least one execute
25697c478bd9Sstevel@tonic-gate 	 * bit set.
25707c478bd9Sstevel@tonic-gate 	 */
25717c478bd9Sstevel@tonic-gate 	if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
25727c478bd9Sstevel@tonic-gate 		error_msg(gettext(
25737c478bd9Sstevel@tonic-gate 		    "The %s method of instance %s isn't a regular file"),
25747c478bd9Sstevel@tonic-gate 		    method, instance);
25757c478bd9Sstevel@tonic-gate 		return (B_FALSE);
25767c478bd9Sstevel@tonic-gate 	} else if ((sbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
25777c478bd9Sstevel@tonic-gate 		error_msg(gettext("The %s method instance %s doesn't have "
25787c478bd9Sstevel@tonic-gate 		    "any execute permissions set"), method, instance);
25797c478bd9Sstevel@tonic-gate 		return (B_FALSE);
25807c478bd9Sstevel@tonic-gate 	}
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	return (B_TRUE);
25837c478bd9Sstevel@tonic-gate }
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate static void
25867c478bd9Sstevel@tonic-gate exec_method(instance_t *instance, instance_method_t method, method_info_t *mi,
25877c478bd9Sstevel@tonic-gate     struct method_context *mthd_ctxt, const proto_info_t *pi)
25887c478bd9Sstevel@tonic-gate {
25897c478bd9Sstevel@tonic-gate 	char		**args;
25907c478bd9Sstevel@tonic-gate 	char 		**env;
25917c478bd9Sstevel@tonic-gate 	const char	*errf;
25927c478bd9Sstevel@tonic-gate 	int		serrno;
25937c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
25967c478bd9Sstevel@tonic-gate 		/*
25977c478bd9Sstevel@tonic-gate 		 * If wrappers checks fail, pretend the method was exec'd and
25987c478bd9Sstevel@tonic-gate 		 * failed.
25997c478bd9Sstevel@tonic-gate 		 */
26007c478bd9Sstevel@tonic-gate 		if (!tcp_wrappers_ok(instance))
26017c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
26027c478bd9Sstevel@tonic-gate 	}
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	/*
26057c478bd9Sstevel@tonic-gate 	 * Revert the disposition of handled signals and ignored signals to
26067c478bd9Sstevel@tonic-gate 	 * their defaults, unblocking any blocked ones as a side effect.
26077c478bd9Sstevel@tonic-gate 	 */
26087c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_DFL);
26097c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
26107c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	/*
26137c478bd9Sstevel@tonic-gate 	 * Setup exec arguments. Do this before the fd setup below, so our
26147c478bd9Sstevel@tonic-gate 	 * logging related file fd doesn't get taken over before we call
26157c478bd9Sstevel@tonic-gate 	 * expand_address().
26167c478bd9Sstevel@tonic-gate 	 */
26177c478bd9Sstevel@tonic-gate 	if ((method == IM_START) &&
26187c478bd9Sstevel@tonic-gate 	    (strcmp(mi->exec_args_we.we_wordv[0], "%A") == 0)) {
26197c478bd9Sstevel@tonic-gate 		args = expand_address(instance, pi);
26207c478bd9Sstevel@tonic-gate 	} else {
26217c478bd9Sstevel@tonic-gate 		args = mi->exec_args_we.we_wordv;
26227c478bd9Sstevel@tonic-gate 	}
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	/* Generate audit trail for start operations */
26257c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
26267c478bd9Sstevel@tonic-gate 		adt_event_data_t *ae;
26277c478bd9Sstevel@tonic-gate 		struct sockaddr_storage ss;
26287c478bd9Sstevel@tonic-gate 		priv_set_t *privset;
26297c478bd9Sstevel@tonic-gate 		socklen_t sslen = sizeof (ss);
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 		if ((ae = adt_alloc_event(audit_handle, ADT_inetd_connect))
26327c478bd9Sstevel@tonic-gate 		    == NULL) {
26337c478bd9Sstevel@tonic-gate 			error_msg(gettext("Unable to allocate audit event for "
26347c478bd9Sstevel@tonic-gate 			    "the %s method of instance %s"),
26357c478bd9Sstevel@tonic-gate 			    methods[method].name, instance->fmri);
26367c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
26377c478bd9Sstevel@tonic-gate 		}
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate 		/*
26407c478bd9Sstevel@tonic-gate 		 * The inetd_connect audit record consists of:
26417c478bd9Sstevel@tonic-gate 		 *	Service name
26427c478bd9Sstevel@tonic-gate 		 *	Execution path
26437c478bd9Sstevel@tonic-gate 		 *	Remote address and port
26447c478bd9Sstevel@tonic-gate 		 *	Local port
26457c478bd9Sstevel@tonic-gate 		 *	Process privileges
26467c478bd9Sstevel@tonic-gate 		 */
26477c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.service_name = cfg->svc_name;
26487c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.cmd = mi->exec_path;
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 		if (instance->remote_addr.ss_family == AF_INET) {
26517c478bd9Sstevel@tonic-gate 			struct in_addr *in = SS_SINADDR(instance->remote_addr);
26527c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_adr[0] = in->s_addr;
26537c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_type = ADT_IPv4;
26547c478bd9Sstevel@tonic-gate 		} else {
26557c478bd9Sstevel@tonic-gate 			uint32_t *addr6;
26567c478bd9Sstevel@tonic-gate 			int i;
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_type = ADT_IPv6;
26597c478bd9Sstevel@tonic-gate 			addr6 = (uint32_t *)SS_SINADDR(instance->remote_addr);
26607c478bd9Sstevel@tonic-gate 			for (i = 0; i < 4; ++i)
26617c478bd9Sstevel@tonic-gate 				ae->adt_inetd_connect.ip_adr[i] = addr6[i];
26627c478bd9Sstevel@tonic-gate 		}
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.ip_remote_port =
26657c478bd9Sstevel@tonic-gate 		    ntohs(SS_PORT(instance->remote_addr));
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 		if (getsockname(instance->conn_fd, (struct sockaddr *)&ss,
26687c478bd9Sstevel@tonic-gate 		    &sslen) == 0)
26697c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_local_port =
26707c478bd9Sstevel@tonic-gate 			    ntohs(SS_PORT(ss));
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 		privset = mthd_ctxt->priv_set;
26737c478bd9Sstevel@tonic-gate 		if (privset == NULL) {
26747c478bd9Sstevel@tonic-gate 			privset = priv_allocset();
26757c478bd9Sstevel@tonic-gate 			if (privset != NULL &&
26767c478bd9Sstevel@tonic-gate 			    getppriv(PRIV_EFFECTIVE, privset) != 0) {
26777c478bd9Sstevel@tonic-gate 				priv_freeset(privset);
26787c478bd9Sstevel@tonic-gate 				privset = NULL;
26797c478bd9Sstevel@tonic-gate 			}
26807c478bd9Sstevel@tonic-gate 		}
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.privileges = privset;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 		(void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS);
26857c478bd9Sstevel@tonic-gate 		adt_free_event(ae);
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 		if (privset != NULL && mthd_ctxt->priv_set == NULL)
26887c478bd9Sstevel@tonic-gate 			priv_freeset(privset);
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	/*
26927c478bd9Sstevel@tonic-gate 	 * Set method context before the fd setup below so we can output an
26937c478bd9Sstevel@tonic-gate 	 * error message if it fails.
26947c478bd9Sstevel@tonic-gate 	 */
26957c478bd9Sstevel@tonic-gate 	if ((errno = restarter_set_method_context(mthd_ctxt, &errf)) != 0) {
26967c478bd9Sstevel@tonic-gate 		const char *msg;
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 		if (errno == -1) {
26997c478bd9Sstevel@tonic-gate 			if (strcmp(errf, "core_set_process_path") == 0) {
27007c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to set the corefile path "
27017c478bd9Sstevel@tonic-gate 				    "for the %s method of instance %s");
27027c478bd9Sstevel@tonic-gate 			} else if (strcmp(errf, "setproject") == 0) {
27037c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to assign a resource "
27047c478bd9Sstevel@tonic-gate 				    "control for the %s method of instance %s");
27057c478bd9Sstevel@tonic-gate 			} else if (strcmp(errf, "pool_set_binding") == 0) {
27067c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to bind the %s method of "
27077c478bd9Sstevel@tonic-gate 				    "instance %s to a pool due to a system "
27087c478bd9Sstevel@tonic-gate 				    "error");
27097c478bd9Sstevel@tonic-gate 			} else {
27107c478bd9Sstevel@tonic-gate 				assert(0);
27117c478bd9Sstevel@tonic-gate 				abort();
27127c478bd9Sstevel@tonic-gate 			}
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 			error_msg(msg, methods[method].name, instance->fmri);
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
27177c478bd9Sstevel@tonic-gate 		}
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 		if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) {
27207c478bd9Sstevel@tonic-gate 			switch (errno) {
27217c478bd9Sstevel@tonic-gate 			case ENOENT:
27227c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to find resource pool "
27237c478bd9Sstevel@tonic-gate 				    "for the %s method of instance %s");
27247c478bd9Sstevel@tonic-gate 				break;
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 			case EBADF:
27277c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to bind the %s method of "
27287c478bd9Sstevel@tonic-gate 				    "instance %s to a pool due to invalid "
27297c478bd9Sstevel@tonic-gate 				    "configuration");
27307c478bd9Sstevel@tonic-gate 				break;
27317c478bd9Sstevel@tonic-gate 
27323ad28c1eSrm 			case EINVAL:
27333ad28c1eSrm 				msg = gettext("Failed to bind the %s method of "
27343ad28c1eSrm 				    "instance %s to a pool due to invalid "
27353ad28c1eSrm 				    "pool name");
27363ad28c1eSrm 				break;
27373ad28c1eSrm 
27387c478bd9Sstevel@tonic-gate 			default:
27397c478bd9Sstevel@tonic-gate 				assert(0);
27407c478bd9Sstevel@tonic-gate 				abort();
27417c478bd9Sstevel@tonic-gate 			}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
27447c478bd9Sstevel@tonic-gate 		}
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 		if (errf != NULL) {
27477c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to set credentials for the "
27487c478bd9Sstevel@tonic-gate 			    "%s method of instance %s (%s: %s)"),
27497c478bd9Sstevel@tonic-gate 			    methods[method].name, instance->fmri, errf,
27507c478bd9Sstevel@tonic-gate 			    strerror(errno));
27517c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
27527c478bd9Sstevel@tonic-gate 		}
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 		switch (errno) {
27557c478bd9Sstevel@tonic-gate 		case ENOMEM:
27567c478bd9Sstevel@tonic-gate 			msg = gettext("Failed to set credentials for the %s "
27577c478bd9Sstevel@tonic-gate 			    "method of instance %s (out of memory)");
27587c478bd9Sstevel@tonic-gate 			break;
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 		case ENOENT:
27617c478bd9Sstevel@tonic-gate 			msg = gettext("Failed to set credentials for the %s "
27627c478bd9Sstevel@tonic-gate 			    "method of instance %s (no passwd or shadow "
27637c478bd9Sstevel@tonic-gate 			    "entry for user)");
27647c478bd9Sstevel@tonic-gate 			break;
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 		default:
27677c478bd9Sstevel@tonic-gate 			assert(0);
27687c478bd9Sstevel@tonic-gate 			abort();
27697c478bd9Sstevel@tonic-gate 		}
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 		error_msg(msg, methods[method].name, instance->fmri);
27727c478bd9Sstevel@tonic-gate 		exit(IMRET_FAILURE);
27737c478bd9Sstevel@tonic-gate 	}
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	/* let exec() free mthd_ctxt */
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	/* setup standard fds */
27787c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
27797c478bd9Sstevel@tonic-gate 		(void) dup2(instance->conn_fd, STDIN_FILENO);
27807c478bd9Sstevel@tonic-gate 	} else {
27817c478bd9Sstevel@tonic-gate 		(void) close(STDIN_FILENO);
27827c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", O_RDONLY);
27837c478bd9Sstevel@tonic-gate 	}
27847c478bd9Sstevel@tonic-gate 	(void) dup2(STDIN_FILENO, STDOUT_FILENO);
27857c478bd9Sstevel@tonic-gate 	(void) dup2(STDIN_FILENO, STDERR_FILENO);
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	closefrom(STDERR_FILENO + 1);
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	method_preexec();
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	env = set_smf_env(mthd_ctxt, instance, methods[method].name);
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	if (env != NULL) {
27947c478bd9Sstevel@tonic-gate 		do {
27957c478bd9Sstevel@tonic-gate 			(void) execve(mi->exec_path, args, env);
27967c478bd9Sstevel@tonic-gate 		} while (errno == EINTR);
27977c478bd9Sstevel@tonic-gate 	}
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	serrno = errno;
28007c478bd9Sstevel@tonic-gate 	/* start up logging again to report the error */
28017c478bd9Sstevel@tonic-gate 	msg_init();
28027c478bd9Sstevel@tonic-gate 	errno = serrno;
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	error_msg(
28057c478bd9Sstevel@tonic-gate 	    gettext("Failed to exec %s method of instance %s: %s"),
28067c478bd9Sstevel@tonic-gate 	    methods[method].name, instance->fmri, strerror(errno));
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	if ((method == IM_START) && (instance->config->basic->iswait)) {
28097c478bd9Sstevel@tonic-gate 		/*
28107c478bd9Sstevel@tonic-gate 		 * We couldn't exec the start method for a wait type service.
28117c478bd9Sstevel@tonic-gate 		 * Eat up data from the endpoint, so that hopefully the
28127c478bd9Sstevel@tonic-gate 		 * service's fd won't wake poll up on the next time round
28137c478bd9Sstevel@tonic-gate 		 * event_loop(). This behavior is carried over from the old
28147c478bd9Sstevel@tonic-gate 		 * inetd, and it seems somewhat arbitrary that it isn't
28157c478bd9Sstevel@tonic-gate 		 * also done in the case of fork failures; but I guess
28167c478bd9Sstevel@tonic-gate 		 * it assumes an exec failure is less likely to be the result
28177c478bd9Sstevel@tonic-gate 		 * of a resource shortage, and is thus not worth retrying.
28187c478bd9Sstevel@tonic-gate 		 */
28197c478bd9Sstevel@tonic-gate 		consume_wait_data(instance, 0);
28207c478bd9Sstevel@tonic-gate 	}
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	exit(IMRET_FAILURE);
28237c478bd9Sstevel@tonic-gate }
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate static restarter_error_t
28267c478bd9Sstevel@tonic-gate get_method_error_success(instance_method_t method)
28277c478bd9Sstevel@tonic-gate {
28287c478bd9Sstevel@tonic-gate 	switch (method) {
28297c478bd9Sstevel@tonic-gate 	case IM_OFFLINE:
28307c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
28317c478bd9Sstevel@tonic-gate 	case IM_ONLINE:
28327c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
28337c478bd9Sstevel@tonic-gate 	case IM_DISABLE:
28347c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
28357c478bd9Sstevel@tonic-gate 	case IM_REFRESH:
28367c478bd9Sstevel@tonic-gate 		return (RERR_REFRESH);
28377c478bd9Sstevel@tonic-gate 	case IM_START:
28387c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
28397c478bd9Sstevel@tonic-gate 	}
2840b823d4c7Sdstaff 	(void) fprintf(stderr, gettext("Internal fatal error in inetd.\n"));
2841b823d4c7Sdstaff 
2842b823d4c7Sdstaff 	abort();
28437c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
28447c478bd9Sstevel@tonic-gate }
28457c478bd9Sstevel@tonic-gate 
2846324baee5Sstevep static int
2847324baee5Sstevep smf_kill_process(instance_t *instance, int sig)
2848324baee5Sstevep {
2849324baee5Sstevep 	rep_val_t	*rv;
2850324baee5Sstevep 	int		ret = IMRET_SUCCESS;
2851324baee5Sstevep 
2852324baee5Sstevep 	/* Carry out process assassination */
2853324baee5Sstevep 	for (rv = uu_list_first(instance->start_pids);
2854324baee5Sstevep 	    rv != NULL;
2855324baee5Sstevep 	    rv = uu_list_next(instance->start_pids, rv)) {
2856324baee5Sstevep 		if ((kill((pid_t)rv->val, sig) != 0) &&
2857324baee5Sstevep 		    (errno != ESRCH)) {
2858324baee5Sstevep 			ret = IMRET_FAILURE;
2859324baee5Sstevep 			error_msg(gettext("Unable to kill "
28607cd7ea0bSrs 			    "start process (%ld) of instance %s: %s"),
28617cd7ea0bSrs 			    rv->val, instance->fmri, strerror(errno));
2862324baee5Sstevep 		}
2863324baee5Sstevep 	}
2864324baee5Sstevep 	return (ret);
2865324baee5Sstevep }
2866324baee5Sstevep 
28677c478bd9Sstevel@tonic-gate /*
28687c478bd9Sstevel@tonic-gate  * Runs the specified method of the specified service instance.
28697c478bd9Sstevel@tonic-gate  * If the method was never specified, we handle it the same as if the
28707c478bd9Sstevel@tonic-gate  * method was called and returned success, carrying on any transition the
28717c478bd9Sstevel@tonic-gate  * instance may be in the midst of.
28727c478bd9Sstevel@tonic-gate  * If the method isn't executable in its specified profile or an error occurs
28737c478bd9Sstevel@tonic-gate  * forking a process to run the method in the function returns -1.
28747c478bd9Sstevel@tonic-gate  * If a method binary is successfully executed, the function switches the
28757c478bd9Sstevel@tonic-gate  * instance's cur state to the method's associated 'run' state and the next
28767c478bd9Sstevel@tonic-gate  * state to the methods associated next state.
28777c478bd9Sstevel@tonic-gate  * Returns -1 if there's an error before forking, else 0.
28787c478bd9Sstevel@tonic-gate  */
28797c478bd9Sstevel@tonic-gate int
28807c478bd9Sstevel@tonic-gate run_method(instance_t *instance, instance_method_t method,
28817c478bd9Sstevel@tonic-gate     const proto_info_t *start_info)
28827c478bd9Sstevel@tonic-gate {
28837c478bd9Sstevel@tonic-gate 	pid_t			child_pid;
28847c478bd9Sstevel@tonic-gate 	method_info_t		*mi;
28857c478bd9Sstevel@tonic-gate 	struct method_context	*mthd_ctxt = NULL;
28867c478bd9Sstevel@tonic-gate 	const char		*errstr;
2887324baee5Sstevep 	int			sig = 0;
28887c478bd9Sstevel@tonic-gate 	int			ret;
28897c478bd9Sstevel@tonic-gate 	instance_cfg_t		*cfg = instance->config;
28907c478bd9Sstevel@tonic-gate 	ctid_t			cid;
28917c478bd9Sstevel@tonic-gate 	boolean_t		trans_failure = B_TRUE;
28927c478bd9Sstevel@tonic-gate 	int			serrno;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	/*
28957c478bd9Sstevel@tonic-gate 	 * Don't bother updating the instance's state for the start method
28967c478bd9Sstevel@tonic-gate 	 * as there isn't a separate start method state.
28977c478bd9Sstevel@tonic-gate 	 */
28987c478bd9Sstevel@tonic-gate 	if (method != IM_START)
28997c478bd9Sstevel@tonic-gate 		update_instance_states(instance, get_method_state(method),
29007c478bd9Sstevel@tonic-gate 		    methods[method].dst_state,
29017c478bd9Sstevel@tonic-gate 		    get_method_error_success(method));
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	if ((mi = cfg->methods[method]) == NULL) {
29047c478bd9Sstevel@tonic-gate 		/*
2905324baee5Sstevep 		 * If the absent method is IM_OFFLINE, default action needs
2906324baee5Sstevep 		 * to be taken to avoid lingering processes which can prevent
2907324baee5Sstevep 		 * the upcoming rebinding from happening.
29087c478bd9Sstevel@tonic-gate 		 */
2909324baee5Sstevep 		if ((method == IM_OFFLINE) && instance->config->basic->iswait) {
2910324baee5Sstevep 			warn_msg(gettext("inetd_offline method for instance %s "
2911324baee5Sstevep 			    "is unspecified.  Taking default action: kill."),
2912324baee5Sstevep 			    instance->fmri);
2913324baee5Sstevep 			(void) str2sig("TERM", &sig);
2914324baee5Sstevep 			ret = smf_kill_process(instance, sig);
2915324baee5Sstevep 			process_non_start_term(instance, ret);
2916324baee5Sstevep 			return (0);
2917324baee5Sstevep 		} else {
2918324baee5Sstevep 			process_non_start_term(instance, IMRET_SUCCESS);
2919324baee5Sstevep 			return (0);
2920324baee5Sstevep 		}
29217c478bd9Sstevel@tonic-gate 	}
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	/* Handle special method tokens, not allowed on start */
29247c478bd9Sstevel@tonic-gate 	if (method != IM_START) {
29257c478bd9Sstevel@tonic-gate 		if (restarter_is_null_method(mi->exec_path)) {
29267c478bd9Sstevel@tonic-gate 			/* :true means nothing should be done */
29277c478bd9Sstevel@tonic-gate 			process_non_start_term(instance, IMRET_SUCCESS);
29287c478bd9Sstevel@tonic-gate 			return (0);
29297c478bd9Sstevel@tonic-gate 		}
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 		if ((sig = restarter_is_kill_method(mi->exec_path)) >= 0) {
29327c478bd9Sstevel@tonic-gate 			/* Carry out contract assassination */
293394501b61Sskamm 			ret = iterate_repository_contracts(instance, sig);
29347c478bd9Sstevel@tonic-gate 			/* ENOENT means we didn't find any contracts */
29357c478bd9Sstevel@tonic-gate 			if (ret != 0 && ret != ENOENT) {
29367c478bd9Sstevel@tonic-gate 				error_msg(gettext("Failed to send signal %d "
29377c478bd9Sstevel@tonic-gate 				    "to contracts of instance %s: %s"), sig,
29387c478bd9Sstevel@tonic-gate 				    instance->fmri, strerror(ret));
29397c478bd9Sstevel@tonic-gate 				goto prefork_failure;
29407c478bd9Sstevel@tonic-gate 			} else {
29417c478bd9Sstevel@tonic-gate 				process_non_start_term(instance, IMRET_SUCCESS);
29427c478bd9Sstevel@tonic-gate 				return (0);
29437c478bd9Sstevel@tonic-gate 			}
29447c478bd9Sstevel@tonic-gate 		}
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 		if ((sig = restarter_is_kill_proc_method(mi->exec_path)) >= 0) {
2947324baee5Sstevep 			ret = smf_kill_process(instance, sig);
29487c478bd9Sstevel@tonic-gate 			process_non_start_term(instance, ret);
29497c478bd9Sstevel@tonic-gate 			return (0);
29507c478bd9Sstevel@tonic-gate 		}
29517c478bd9Sstevel@tonic-gate 	}
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	/*
29547c478bd9Sstevel@tonic-gate 	 * Get the associated method context before the fork so we can
29557c478bd9Sstevel@tonic-gate 	 * modify the instances state if things go wrong.
29567c478bd9Sstevel@tonic-gate 	 */
29577c478bd9Sstevel@tonic-gate 	if ((mthd_ctxt = read_method_context(instance->fmri,
29587c478bd9Sstevel@tonic-gate 	    methods[method].name, mi->exec_path, &errstr)) == NULL) {
29597c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to retrieve method context for the "
29607c478bd9Sstevel@tonic-gate 		    "%s method of instance %s: %s"), methods[method].name,
29617c478bd9Sstevel@tonic-gate 		    instance->fmri, errstr);
29627c478bd9Sstevel@tonic-gate 		goto prefork_failure;
29637c478bd9Sstevel@tonic-gate 	}
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	/*
29667c478bd9Sstevel@tonic-gate 	 * Perform some basic checks before we fork to limit the possibility
29677c478bd9Sstevel@tonic-gate 	 * of exec failures, so we can modify the instance state if necessary.
29687c478bd9Sstevel@tonic-gate 	 */
29697c478bd9Sstevel@tonic-gate 	if (!passes_basic_exec_checks(instance->fmri, methods[method].name,
29707c478bd9Sstevel@tonic-gate 	    mi->exec_path)) {
29717c478bd9Sstevel@tonic-gate 		trans_failure = B_FALSE;
29727c478bd9Sstevel@tonic-gate 		goto prefork_failure;
29737c478bd9Sstevel@tonic-gate 	}
29747c478bd9Sstevel@tonic-gate 
29757b209c2cSacruz 	if (contract_prefork(instance->fmri, method) == -1)
29767c478bd9Sstevel@tonic-gate 		goto prefork_failure;
29777c478bd9Sstevel@tonic-gate 	child_pid = fork();
29787c478bd9Sstevel@tonic-gate 	serrno = errno;
29797c478bd9Sstevel@tonic-gate 	contract_postfork();
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	switch (child_pid) {
29827c478bd9Sstevel@tonic-gate 	case -1:
29837c478bd9Sstevel@tonic-gate 		error_msg(gettext(
29847c478bd9Sstevel@tonic-gate 		    "Unable to fork %s method of instance %s: %s"),
29857c478bd9Sstevel@tonic-gate 		    methods[method].name, instance->fmri, strerror(serrno));
29867c478bd9Sstevel@tonic-gate 		if ((serrno != EAGAIN) && (serrno != ENOMEM))
29877c478bd9Sstevel@tonic-gate 			trans_failure = B_FALSE;
29887c478bd9Sstevel@tonic-gate 		goto prefork_failure;
29897c478bd9Sstevel@tonic-gate 	case 0:				/* child */
29907c478bd9Sstevel@tonic-gate 		exec_method(instance, method, mi, mthd_ctxt, start_info);
2991b823d4c7Sdstaff 		/* NOTREACHED */
29927c478bd9Sstevel@tonic-gate 	default:			/* parent */
29937c478bd9Sstevel@tonic-gate 		restarter_free_method_context(mthd_ctxt);
29947c478bd9Sstevel@tonic-gate 		mthd_ctxt = NULL;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 		if (get_latest_contract(&cid) < 0)
29977c478bd9Sstevel@tonic-gate 			cid = -1;
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 		/*
30007c478bd9Sstevel@tonic-gate 		 * Register this method so its termination is noticed and
30017c478bd9Sstevel@tonic-gate 		 * the state transition this method participates in is
30027c478bd9Sstevel@tonic-gate 		 * continued.
30037c478bd9Sstevel@tonic-gate 		 */
30047c478bd9Sstevel@tonic-gate 		if (register_method(instance, child_pid, cid, method) != 0) {
30057c478bd9Sstevel@tonic-gate 			/*
30067c478bd9Sstevel@tonic-gate 			 * Since we will never find out about the termination
30077c478bd9Sstevel@tonic-gate 			 * of this method, if it's a non-start method treat
30087c478bd9Sstevel@tonic-gate 			 * is as a failure so we don't block restarter event
30097c478bd9Sstevel@tonic-gate 			 * processing on it whilst it languishes in a method
30107c478bd9Sstevel@tonic-gate 			 * running state.
30117c478bd9Sstevel@tonic-gate 			 */
30127c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to monitor status of "
30137c478bd9Sstevel@tonic-gate 			    "%s method of instance %s"), methods[method].name,
30147c478bd9Sstevel@tonic-gate 			    instance->fmri);
30157c478bd9Sstevel@tonic-gate 			if (method != IM_START)
30167c478bd9Sstevel@tonic-gate 				process_non_start_term(instance, IMRET_FAILURE);
30177c478bd9Sstevel@tonic-gate 		}
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 		add_method_ids(instance, child_pid, cid, method);
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 		/* do tcp tracing for those nowait instances that request it */
30227c478bd9Sstevel@tonic-gate 		if ((method == IM_START) && cfg->basic->do_tcp_trace &&
30237c478bd9Sstevel@tonic-gate 		    !cfg->basic->iswait) {
30247c478bd9Sstevel@tonic-gate 			char buf[INET6_ADDRSTRLEN];
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 			syslog(LOG_NOTICE, "%s[%d] from %s %d",
30277c478bd9Sstevel@tonic-gate 			    cfg->basic->svc_name, child_pid,
30287c478bd9Sstevel@tonic-gate 			    inet_ntop_native(instance->remote_addr.ss_family,
30297c478bd9Sstevel@tonic-gate 			    SS_SINADDR(instance->remote_addr), buf,
30307c478bd9Sstevel@tonic-gate 			    sizeof (buf)),
30317c478bd9Sstevel@tonic-gate 			    ntohs(SS_PORT(instance->remote_addr)));
30327c478bd9Sstevel@tonic-gate 		}
30337c478bd9Sstevel@tonic-gate 	}
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	return (0);
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate prefork_failure:
30387c478bd9Sstevel@tonic-gate 	if (mthd_ctxt != NULL) {
30397c478bd9Sstevel@tonic-gate 		restarter_free_method_context(mthd_ctxt);
30407c478bd9Sstevel@tonic-gate 		mthd_ctxt = NULL;
30417c478bd9Sstevel@tonic-gate 	}
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
30447c478bd9Sstevel@tonic-gate 		/*
30457c478bd9Sstevel@tonic-gate 		 * Only place a start method in maintenance if we're sure
30467c478bd9Sstevel@tonic-gate 		 * that the failure was non-transient.
30477c478bd9Sstevel@tonic-gate 		 */
30487c478bd9Sstevel@tonic-gate 		if (!trans_failure) {
30497c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
30507c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_FAULT);
30517c478bd9Sstevel@tonic-gate 		}
30527c478bd9Sstevel@tonic-gate 	} else {
30537c478bd9Sstevel@tonic-gate 		/* treat the failure as if the method ran and failed */
30547c478bd9Sstevel@tonic-gate 		process_non_start_term(instance, IMRET_FAILURE);
30557c478bd9Sstevel@tonic-gate 	}
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	return (-1);
30587c478bd9Sstevel@tonic-gate }
30597c478bd9Sstevel@tonic-gate 
3060*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India static int
3061*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India pending_connections(instance_t *instance, proto_info_t *pi)
3062*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India {
3063*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 	if (instance->config->basic->istlx) {
3064*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		tlx_info_t *tl = (tlx_info_t *)pi;
3065*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 
3066*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		return (uu_list_numnodes(tl->conn_ind_queue) != 0);
3067*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 	} else {
3068*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		return (0);
3069*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 	}
3070*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India }
3071*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 
30727c478bd9Sstevel@tonic-gate static int
30737c478bd9Sstevel@tonic-gate accept_connection(instance_t *instance, proto_info_t *pi)
30747c478bd9Sstevel@tonic-gate {
30757c478bd9Sstevel@tonic-gate 	int		fd;
30767c478bd9Sstevel@tonic-gate 	socklen_t	size;
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 	if (instance->config->basic->istlx) {
3079*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		tlx_info_t *tl = (tlx_info_t *)pi;
3080*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		tlx_pending_counter = \
3081*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		    tlx_pending_counter - uu_list_numnodes(tl->conn_ind_queue);
3082*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 
30837c478bd9Sstevel@tonic-gate 		fd = tlx_accept(instance->fmri, (tlx_info_t *)pi,
30847c478bd9Sstevel@tonic-gate 		    &(instance->remote_addr));
3085*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 
3086*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		tlx_pending_counter = \
3087*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		    tlx_pending_counter + uu_list_numnodes(tl->conn_ind_queue);
30887c478bd9Sstevel@tonic-gate 	} else {
30897c478bd9Sstevel@tonic-gate 		size = sizeof (instance->remote_addr);
30907c478bd9Sstevel@tonic-gate 		fd = accept(pi->listen_fd,
30917c478bd9Sstevel@tonic-gate 		    (struct sockaddr *)&(instance->remote_addr), &size);
30927c478bd9Sstevel@tonic-gate 		if (fd < 0)
30937c478bd9Sstevel@tonic-gate 			error_msg("accept: %s", strerror(errno));
30947c478bd9Sstevel@tonic-gate 	}
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	return (fd);
30977c478bd9Sstevel@tonic-gate }
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate /*
31007c478bd9Sstevel@tonic-gate  * Handle an incoming connection request for a nowait service.
31017c478bd9Sstevel@tonic-gate  * This involves accepting the incoming connection on a new fd. Connection
31027c478bd9Sstevel@tonic-gate  * rate checks are then performed, transitioning the service to the
31037c478bd9Sstevel@tonic-gate  * conrate offline state if these fail. Otherwise, the service's start method
31047c478bd9Sstevel@tonic-gate  * is run (performing TCP wrappers checks if applicable as we do), and on
31057c478bd9Sstevel@tonic-gate  * success concurrent copies checking is done, transitioning the service to the
31067c478bd9Sstevel@tonic-gate  * copies offline state if this fails.
31077c478bd9Sstevel@tonic-gate  */
31087c478bd9Sstevel@tonic-gate static void
31097c478bd9Sstevel@tonic-gate process_nowait_request(instance_t *instance, proto_info_t *pi)
31107c478bd9Sstevel@tonic-gate {
31117c478bd9Sstevel@tonic-gate 	basic_cfg_t		*cfg = instance->config->basic;
31127c478bd9Sstevel@tonic-gate 	int			ret;
31137c478bd9Sstevel@tonic-gate 	adt_event_data_t	*ae;
31147c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	/* accept nowait service connections on a new fd */
31177c478bd9Sstevel@tonic-gate 	if ((instance->conn_fd = accept_connection(instance, pi)) == -1) {
31187c478bd9Sstevel@tonic-gate 		/*
31197c478bd9Sstevel@tonic-gate 		 * Failed accept. Return and allow the event loop to initiate
31207c478bd9Sstevel@tonic-gate 		 * another attempt later if the request is still present.
31217c478bd9Sstevel@tonic-gate 		 */
31227c478bd9Sstevel@tonic-gate 		return;
31237c478bd9Sstevel@tonic-gate 	}
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	/*
31267c478bd9Sstevel@tonic-gate 	 * Limit connection rate of nowait services. If either conn_rate_max
31277c478bd9Sstevel@tonic-gate 	 * or conn_rate_offline are <= 0, no connection rate limit checking
31287c478bd9Sstevel@tonic-gate 	 * is done. If the configured rate is exceeded, the instance is taken
31297c478bd9Sstevel@tonic-gate 	 * to the connrate_offline state and a timer scheduled to try and
31307c478bd9Sstevel@tonic-gate 	 * bring the instance back online after the configured offline time.
31317c478bd9Sstevel@tonic-gate 	 */
31327c478bd9Sstevel@tonic-gate 	if ((cfg->conn_rate_max > 0) && (cfg->conn_rate_offline > 0)) {
31337c478bd9Sstevel@tonic-gate 		if (instance->conn_rate_count++ == 0) {
31347c478bd9Sstevel@tonic-gate 			instance->conn_rate_start = time(NULL);
31357c478bd9Sstevel@tonic-gate 		} else if (instance->conn_rate_count >
31367c478bd9Sstevel@tonic-gate 		    cfg->conn_rate_max) {
31377c478bd9Sstevel@tonic-gate 			time_t now = time(NULL);
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 			if ((now - instance->conn_rate_start) > 1) {
31407c478bd9Sstevel@tonic-gate 				instance->conn_rate_start = now;
31417c478bd9Sstevel@tonic-gate 				instance->conn_rate_count = 1;
31427c478bd9Sstevel@tonic-gate 			} else {
31437c478bd9Sstevel@tonic-gate 				/* Generate audit record */
31447c478bd9Sstevel@tonic-gate 				if ((ae = adt_alloc_event(audit_handle,
31457c478bd9Sstevel@tonic-gate 				    ADT_inetd_ratelimit)) == NULL) {
31467c478bd9Sstevel@tonic-gate 					error_msg(gettext("Unable to allocate "
31477c478bd9Sstevel@tonic-gate 					    "rate limit audit event"));
31487c478bd9Sstevel@tonic-gate 				} else {
31497c478bd9Sstevel@tonic-gate 					adt_inetd_ratelimit_t *rl =
31507c478bd9Sstevel@tonic-gate 					    &ae->adt_inetd_ratelimit;
31517c478bd9Sstevel@tonic-gate 					/*
31527c478bd9Sstevel@tonic-gate 					 * The inetd_ratelimit audit
31537c478bd9Sstevel@tonic-gate 					 * record consists of:
31547c478bd9Sstevel@tonic-gate 					 * 	Service name
31557c478bd9Sstevel@tonic-gate 					 *	Connection rate limit
31567c478bd9Sstevel@tonic-gate 					 */
31577c478bd9Sstevel@tonic-gate 					rl->service_name = cfg->svc_name;
31587c478bd9Sstevel@tonic-gate 					(void) snprintf(buf, sizeof (buf),
31597c478bd9Sstevel@tonic-gate 					    "limit=%lld", cfg->conn_rate_max);
31607c478bd9Sstevel@tonic-gate 					rl->limit = buf;
31617c478bd9Sstevel@tonic-gate 					(void) adt_put_event(ae, ADT_SUCCESS,
31627c478bd9Sstevel@tonic-gate 					    ADT_SUCCESS);
31637c478bd9Sstevel@tonic-gate 					adt_free_event(ae);
31647c478bd9Sstevel@tonic-gate 				}
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 				error_msg(gettext(
31677c478bd9Sstevel@tonic-gate 				    "Instance %s has exceeded its configured "
31687c478bd9Sstevel@tonic-gate 				    "connection rate, additional connections "
31697c478bd9Sstevel@tonic-gate 				    "will not be accepted for %d seconds"),
31707c478bd9Sstevel@tonic-gate 				    instance->fmri, cfg->conn_rate_offline);
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate 				close_net_fd(instance, instance->conn_fd);
31737c478bd9Sstevel@tonic-gate 				instance->conn_fd = -1;
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 				destroy_bound_fds(instance);
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 				instance->conn_rate_count = 0;
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 				instance->conn_rate_exceeded = B_TRUE;
31807c478bd9Sstevel@tonic-gate 				(void) run_method(instance, IM_OFFLINE, NULL);
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 				return;
31837c478bd9Sstevel@tonic-gate 			}
31847c478bd9Sstevel@tonic-gate 		}
31857c478bd9Sstevel@tonic-gate 	}
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	ret = run_method(instance, IM_START, pi);
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	close_net_fd(instance, instance->conn_fd);
31907c478bd9Sstevel@tonic-gate 	instance->conn_fd = -1;
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	if (ret == -1) /* the method wasn't forked  */
31937c478bd9Sstevel@tonic-gate 		return;
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	instance->copies++;
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 	/*
31987c478bd9Sstevel@tonic-gate 	 * Limit concurrent connections of nowait services.
31997c478bd9Sstevel@tonic-gate 	 */
32007c478bd9Sstevel@tonic-gate 	if (copies_limit_exceeded(instance)) {
32017c478bd9Sstevel@tonic-gate 		/* Generate audit record */
32027c478bd9Sstevel@tonic-gate 		if ((ae = adt_alloc_event(audit_handle, ADT_inetd_copylimit))
32037c478bd9Sstevel@tonic-gate 		    == NULL) {
32047c478bd9Sstevel@tonic-gate 			error_msg(gettext("Unable to allocate copy limit "
32057c478bd9Sstevel@tonic-gate 			    "audit event"));
32067c478bd9Sstevel@tonic-gate 		} else {
32077c478bd9Sstevel@tonic-gate 			/*
32087c478bd9Sstevel@tonic-gate 			 * The inetd_copylimit audit record consists of:
32097c478bd9Sstevel@tonic-gate 			 *	Service name
32107c478bd9Sstevel@tonic-gate 			 * 	Copy limit
32117c478bd9Sstevel@tonic-gate 			 */
32127c478bd9Sstevel@tonic-gate 			ae->adt_inetd_copylimit.service_name = cfg->svc_name;
32137c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "limit=%lld",
32147c478bd9Sstevel@tonic-gate 			    cfg->max_copies);
32157c478bd9Sstevel@tonic-gate 			ae->adt_inetd_copylimit.limit = buf;
32167c478bd9Sstevel@tonic-gate 			(void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS);
32177c478bd9Sstevel@tonic-gate 			adt_free_event(ae);
32187c478bd9Sstevel@tonic-gate 		}
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 		warn_msg(gettext("Instance %s has reached its maximum "
32217c478bd9Sstevel@tonic-gate 		    "configured copies, no new connections will be accepted"),
32227c478bd9Sstevel@tonic-gate 		    instance->fmri);
32237c478bd9Sstevel@tonic-gate 		destroy_bound_fds(instance);
32247c478bd9Sstevel@tonic-gate 		(void) run_method(instance, IM_OFFLINE, NULL);
32257c478bd9Sstevel@tonic-gate 	}
32267c478bd9Sstevel@tonic-gate }
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate /*
32297c478bd9Sstevel@tonic-gate  * Handle an incoming request for a wait type service.
32307c478bd9Sstevel@tonic-gate  * Failure rate checking is done first, taking the service to the maintenance
32317c478bd9Sstevel@tonic-gate  * state if the checks fail. Following this, the service's start method is run,
32327c478bd9Sstevel@tonic-gate  * and on success, we stop listening for new requests for this service.
32337c478bd9Sstevel@tonic-gate  */
32347c478bd9Sstevel@tonic-gate static void
32357c478bd9Sstevel@tonic-gate process_wait_request(instance_t *instance, const proto_info_t *pi)
32367c478bd9Sstevel@tonic-gate {
32377c478bd9Sstevel@tonic-gate 	basic_cfg_t		*cfg = instance->config->basic;
32387c478bd9Sstevel@tonic-gate 	int			ret;
32397c478bd9Sstevel@tonic-gate 	adt_event_data_t	*ae;
32407c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	instance->conn_fd = pi->listen_fd;
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 	/*
32457c478bd9Sstevel@tonic-gate 	 * Detect broken servers and transition them to maintenance. If a
32467c478bd9Sstevel@tonic-gate 	 * wait type service exits without accepting the connection or
32477c478bd9Sstevel@tonic-gate 	 * consuming (reading) the datagram, that service's descriptor will
32487c478bd9Sstevel@tonic-gate 	 * select readable again, and inetd will fork another instance of
32497c478bd9Sstevel@tonic-gate 	 * the server. If either wait_fail_cnt or wait_fail_interval are <= 0,
32507c478bd9Sstevel@tonic-gate 	 * no failure rate detection is done.
32517c478bd9Sstevel@tonic-gate 	 */
32527c478bd9Sstevel@tonic-gate 	if ((cfg->wait_fail_cnt > 0) && (cfg->wait_fail_interval > 0)) {
32537c478bd9Sstevel@tonic-gate 		if (instance->fail_rate_count++ == 0) {
32547c478bd9Sstevel@tonic-gate 			instance->fail_rate_start = time(NULL);
32557c478bd9Sstevel@tonic-gate 		} else if (instance->fail_rate_count > cfg->wait_fail_cnt) {
32567c478bd9Sstevel@tonic-gate 			time_t now = time(NULL);
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 			if ((now - instance->fail_rate_start) >
32597c478bd9Sstevel@tonic-gate 			    cfg->wait_fail_interval) {
32607c478bd9Sstevel@tonic-gate 				instance->fail_rate_start = now;
32617c478bd9Sstevel@tonic-gate 				instance->fail_rate_count = 1;
32627c478bd9Sstevel@tonic-gate 			} else {
32637c478bd9Sstevel@tonic-gate 				/* Generate audit record */
32647c478bd9Sstevel@tonic-gate 				if ((ae = adt_alloc_event(audit_handle,
32657c478bd9Sstevel@tonic-gate 				    ADT_inetd_failrate)) == NULL) {
32667c478bd9Sstevel@tonic-gate 					error_msg(gettext("Unable to allocate "
32677c478bd9Sstevel@tonic-gate 					    "failure rate audit event"));
32687c478bd9Sstevel@tonic-gate 				} else {
32697c478bd9Sstevel@tonic-gate 					adt_inetd_failrate_t *fr =
32707c478bd9Sstevel@tonic-gate 					    &ae->adt_inetd_failrate;
32717c478bd9Sstevel@tonic-gate 					/*
32727c478bd9Sstevel@tonic-gate 					 * The inetd_failrate audit record
32737c478bd9Sstevel@tonic-gate 					 * consists of:
32747c478bd9Sstevel@tonic-gate 					 * 	Service name
32757c478bd9Sstevel@tonic-gate 					 * 	Failure rate
32767c478bd9Sstevel@tonic-gate 					 *	Interval
32777c478bd9Sstevel@tonic-gate 					 * Last two are expressed as k=v pairs
32787c478bd9Sstevel@tonic-gate 					 * in the values field.
32797c478bd9Sstevel@tonic-gate 					 */
32807c478bd9Sstevel@tonic-gate 					fr->service_name = cfg->svc_name;
32817c478bd9Sstevel@tonic-gate 					(void) snprintf(buf, sizeof (buf),
32827c478bd9Sstevel@tonic-gate 					    "limit=%lld,interval=%d",
32837c478bd9Sstevel@tonic-gate 					    cfg->wait_fail_cnt,
32847c478bd9Sstevel@tonic-gate 					    cfg->wait_fail_interval);
32857c478bd9Sstevel@tonic-gate 					fr->values = buf;
32867c478bd9Sstevel@tonic-gate 					(void) adt_put_event(ae, ADT_SUCCESS,
32877c478bd9Sstevel@tonic-gate 					    ADT_SUCCESS);
32887c478bd9Sstevel@tonic-gate 					adt_free_event(ae);
32897c478bd9Sstevel@tonic-gate 				}
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 				error_msg(gettext(
32927c478bd9Sstevel@tonic-gate 				    "Instance %s has exceeded its configured "
32937c478bd9Sstevel@tonic-gate 				    "failure rate, transitioning to "
32947c478bd9Sstevel@tonic-gate 				    "maintenance"), instance->fmri);
32957c478bd9Sstevel@tonic-gate 				instance->fail_rate_count = 0;
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 				destroy_bound_fds(instance);
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 				instance->maintenance_req = B_TRUE;
33007c478bd9Sstevel@tonic-gate 				(void) run_method(instance, IM_OFFLINE, NULL);
33017c478bd9Sstevel@tonic-gate 				return;
33027c478bd9Sstevel@tonic-gate 			}
33037c478bd9Sstevel@tonic-gate 		}
33047c478bd9Sstevel@tonic-gate 	}
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	ret = run_method(instance, IM_START, pi);
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate 	instance->conn_fd = -1;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	if (ret == 0) {
33117c478bd9Sstevel@tonic-gate 		/*
33127c478bd9Sstevel@tonic-gate 		 * Stop listening for connections now we've fired off the
33137c478bd9Sstevel@tonic-gate 		 * server for a wait type instance.
33147c478bd9Sstevel@tonic-gate 		 */
33157c478bd9Sstevel@tonic-gate 		(void) poll_bound_fds(instance, B_FALSE);
33167c478bd9Sstevel@tonic-gate 	}
33177c478bd9Sstevel@tonic-gate }
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate /*
33207c478bd9Sstevel@tonic-gate  * Process any networks requests for each proto for each instance.
33217c478bd9Sstevel@tonic-gate  */
33227c478bd9Sstevel@tonic-gate void
33237c478bd9Sstevel@tonic-gate process_network_events(void)
33247c478bd9Sstevel@tonic-gate {
33257c478bd9Sstevel@tonic-gate 	instance_t	*instance;
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	for (instance = uu_list_first(instance_list); instance != NULL;
33287c478bd9Sstevel@tonic-gate 	    instance = uu_list_next(instance_list, instance)) {
33297c478bd9Sstevel@tonic-gate 		basic_cfg_t	*cfg;
33307c478bd9Sstevel@tonic-gate 		proto_info_t	*pi;
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 		/*
33337c478bd9Sstevel@tonic-gate 		 * Ignore instances in states that definitely don't have any
33347c478bd9Sstevel@tonic-gate 		 * listening fds.
33357c478bd9Sstevel@tonic-gate 		 */
33367c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
33377c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
33387c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
33397c478bd9Sstevel@tonic-gate 		case IIS_IN_REFRESH_METHOD:
33407c478bd9Sstevel@tonic-gate 			break;
33417c478bd9Sstevel@tonic-gate 		default:
33427c478bd9Sstevel@tonic-gate 			continue;
33437c478bd9Sstevel@tonic-gate 		}
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 		cfg = instance->config->basic;
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 		for (pi = uu_list_first(cfg->proto_list); pi != NULL;
33487c478bd9Sstevel@tonic-gate 		    pi = uu_list_next(cfg->proto_list, pi)) {
3349*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 			if (((pi->listen_fd != -1) &&
3350*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 			    isset_pollfd(pi->listen_fd)) ||
3351*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 			    pending_connections(instance, pi)) {
33527c478bd9Sstevel@tonic-gate 				if (cfg->iswait) {
33537c478bd9Sstevel@tonic-gate 					process_wait_request(instance, pi);
33547c478bd9Sstevel@tonic-gate 				} else {
33557c478bd9Sstevel@tonic-gate 					process_nowait_request(instance, pi);
33567c478bd9Sstevel@tonic-gate 				}
33577c478bd9Sstevel@tonic-gate 			}
33587c478bd9Sstevel@tonic-gate 		}
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate }
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
33637c478bd9Sstevel@tonic-gate static void
33647c478bd9Sstevel@tonic-gate sigterm_handler(int sig)
33657c478bd9Sstevel@tonic-gate {
33667c478bd9Sstevel@tonic-gate 	got_sigterm = B_TRUE;
33677c478bd9Sstevel@tonic-gate }
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
33707c478bd9Sstevel@tonic-gate static void
33717c478bd9Sstevel@tonic-gate sighup_handler(int sig)
33727c478bd9Sstevel@tonic-gate {
33737c478bd9Sstevel@tonic-gate 	refresh_inetd_requested = B_TRUE;
33747c478bd9Sstevel@tonic-gate }
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate /*
33777c478bd9Sstevel@tonic-gate  * inetd's major work loop. This function sits in poll waiting for events
33787c478bd9Sstevel@tonic-gate  * to occur, processing them when they do. The possible events are
33797c478bd9Sstevel@tonic-gate  * master restarter requests, expired timer queue timers, stop/refresh signal
33807c478bd9Sstevel@tonic-gate  * requests, contract events indicating process termination, stop/refresh
33817c478bd9Sstevel@tonic-gate  * requests originating from one of the stop/refresh inetd processes and
33827c478bd9Sstevel@tonic-gate  * network events.
33837c478bd9Sstevel@tonic-gate  * The loop is exited when a stop request is received and processed, and
33847c478bd9Sstevel@tonic-gate  * all the instances have reached a suitable 'stopping' state.
33857c478bd9Sstevel@tonic-gate  */
33867c478bd9Sstevel@tonic-gate static void
33877c478bd9Sstevel@tonic-gate event_loop(void)
33887c478bd9Sstevel@tonic-gate {
33897c478bd9Sstevel@tonic-gate 	instance_t		*instance;
33907c478bd9Sstevel@tonic-gate 	int			timeout;
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	for (;;) {
33937c478bd9Sstevel@tonic-gate 		int	pret = -1;
33947c478bd9Sstevel@tonic-gate 
3395*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		if (tlx_pending_counter != 0)
3396*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 			timeout = 0;
3397*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 		else
3398*ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 			timeout = iu_earliest_timer(timer_queue);
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 		if (!got_sigterm && !refresh_inetd_requested) {
34017c478bd9Sstevel@tonic-gate 			pret = poll(poll_fds, num_pollfds, timeout);
34027c478bd9Sstevel@tonic-gate 			if ((pret == -1) && (errno != EINTR)) {
34037c478bd9Sstevel@tonic-gate 				error_msg(gettext("poll failure: %s"),
34047c478bd9Sstevel@tonic-gate 				    strerror(errno));
34057c478bd9Sstevel@tonic-gate 				continue;
34067c478bd9Sstevel@tonic-gate 			}
34077c478bd9Sstevel@tonic-gate 		}
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 		if (got_sigterm) {
34107c478bd9Sstevel@tonic-gate 			msg_fini();
34117c478bd9Sstevel@tonic-gate 			inetd_stop();
34127c478bd9Sstevel@tonic-gate 			got_sigterm = B_FALSE;
34137c478bd9Sstevel@tonic-gate 			goto check_if_stopped;
34147c478bd9Sstevel@tonic-gate 		}
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 		/*
34177c478bd9Sstevel@tonic-gate 		 * Process any stop/refresh requests from the Unix Domain
34187c478bd9Sstevel@tonic-gate 		 * Socket.
34197c478bd9Sstevel@tonic-gate 		 */
34207c478bd9Sstevel@tonic-gate 		if ((pret != -1) && isset_pollfd(uds_fd)) {
34217c478bd9Sstevel@tonic-gate 			while (process_uds_event() == 0)
34227c478bd9Sstevel@tonic-gate 				;
34237c478bd9Sstevel@tonic-gate 		}
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 		/*
34267c478bd9Sstevel@tonic-gate 		 * Process refresh request. We do this check after the UDS
34277c478bd9Sstevel@tonic-gate 		 * event check above, as it would be wasted processing if we
34287c478bd9Sstevel@tonic-gate 		 * started refreshing inetd based on a SIGHUP, and then were
34297c478bd9Sstevel@tonic-gate 		 * told to shut-down via a UDS event.
34307c478bd9Sstevel@tonic-gate 		 */
34317c478bd9Sstevel@tonic-gate 		if (refresh_inetd_requested) {
34327c478bd9Sstevel@tonic-gate 			refresh_inetd_requested = B_FALSE;
34337c478bd9Sstevel@tonic-gate 			if (!inetd_stopping)
34347c478bd9Sstevel@tonic-gate 				inetd_refresh();
34357c478bd9Sstevel@tonic-gate 		}
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 		/*
34387c478bd9Sstevel@tonic-gate 		 * We were interrupted by a signal. Don't waste any more
34397c478bd9Sstevel@tonic-gate 		 * time processing a potentially inaccurate poll return.
34407c478bd9Sstevel@tonic-gate 		 */
34417c478bd9Sstevel@tonic-gate 		if (pret == -1)
34427c478bd9Sstevel@tonic-gate 			continue;
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 		/*
34457c478bd9Sstevel@tonic-gate 		 * Process any instance restarter events.
34467c478bd9Sstevel@tonic-gate 		 */
34477c478bd9Sstevel@tonic-gate 		if (isset_pollfd(rst_event_pipe[PE_CONSUMER])) {
34487c478bd9Sstevel@tonic-gate 			while (process_restarter_event() == 0)
34497c478bd9Sstevel@tonic-gate 				;
34507c478bd9Sstevel@tonic-gate 		}
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 		/*
34537c478bd9Sstevel@tonic-gate 		 * Process any expired timers (bind retry, con-rate offline,
34547c478bd9Sstevel@tonic-gate 		 * method timeouts).
34557c478bd9Sstevel@tonic-gate 		 */
34567c478bd9Sstevel@tonic-gate 		(void) iu_expire_timers(timer_queue);
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 		process_terminated_methods();
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 		/*
34617c478bd9Sstevel@tonic-gate 		 * If inetd is stopping, check whether all our managed
34627c478bd9Sstevel@tonic-gate 		 * instances have been stopped and we can return.
34637c478bd9Sstevel@tonic-gate 		 */
34647c478bd9Sstevel@tonic-gate 		if (inetd_stopping) {
34657c478bd9Sstevel@tonic-gate check_if_stopped:
34667c478bd9Sstevel@tonic-gate 			for (instance = uu_list_first(instance_list);
34677c478bd9Sstevel@tonic-gate 			    instance != NULL;
34687c478bd9Sstevel@tonic-gate 			    instance = uu_list_next(instance_list, instance)) {
34697c478bd9Sstevel@tonic-gate 				if (!instance_stopped(instance)) {
34707c478bd9Sstevel@tonic-gate 					debug_msg("%s not yet stopped",
34717c478bd9Sstevel@tonic-gate 					    instance->fmri);
34727c478bd9Sstevel@tonic-gate 					break;
34737c478bd9Sstevel@tonic-gate 				}
34747c478bd9Sstevel@tonic-gate 			}
34757c478bd9Sstevel@tonic-gate 			/* if all instances are stopped, return */
34767c478bd9Sstevel@tonic-gate 			if (instance == NULL)
34777c478bd9Sstevel@tonic-gate 				return;
34787c478bd9Sstevel@tonic-gate 		}
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 		process_network_events();
34817c478bd9Sstevel@tonic-gate 	}
34827c478bd9Sstevel@tonic-gate }
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate static void
34857c478bd9Sstevel@tonic-gate fini(void)
34867c478bd9Sstevel@tonic-gate {
34877c478bd9Sstevel@tonic-gate 	method_fini();
34887c478bd9Sstevel@tonic-gate 	uds_fini();
34897c478bd9Sstevel@tonic-gate 	if (timer_queue != NULL)
34907c478bd9Sstevel@tonic-gate 		iu_tq_destroy(timer_queue);
3491b823d4c7Sdstaff 
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 	/*
3494b823d4c7Sdstaff 	 * We don't bother to undo the restarter interface at all.
3495b823d4c7Sdstaff 	 * Because of quirks in the interface, there is no way to
3496b823d4c7Sdstaff 	 * disconnect from the channel and cause any new events to be
3497b823d4c7Sdstaff 	 * queued.  However, any events which are received and not
3498b823d4c7Sdstaff 	 * acknowledged will be re-sent when inetd restarts as long as inetd
3499b823d4c7Sdstaff 	 * uses the same subscriber ID, which it does.
3500b823d4c7Sdstaff 	 *
3501b823d4c7Sdstaff 	 * By keeping the event pipe open but ignoring it, any events which
3502b823d4c7Sdstaff 	 * occur will cause restarter_event_proxy to hang without breaking
3503b823d4c7Sdstaff 	 * anything.
35047c478bd9Sstevel@tonic-gate 	 */
35057c478bd9Sstevel@tonic-gate 
35067c478bd9Sstevel@tonic-gate 	if (instance_list != NULL) {
35077c478bd9Sstevel@tonic-gate 		void		*cookie = NULL;
35087c478bd9Sstevel@tonic-gate 		instance_t	*inst;
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 		while ((inst = uu_list_teardown(instance_list, &cookie)) !=
35117c478bd9Sstevel@tonic-gate 		    NULL)
35127c478bd9Sstevel@tonic-gate 			destroy_instance(inst);
35137c478bd9Sstevel@tonic-gate 		uu_list_destroy(instance_list);
35147c478bd9Sstevel@tonic-gate 	}
35157c478bd9Sstevel@tonic-gate 	if (instance_pool != NULL)
35167c478bd9Sstevel@tonic-gate 		uu_list_pool_destroy(instance_pool);
35177c478bd9Sstevel@tonic-gate 	tlx_fini();
35187c478bd9Sstevel@tonic-gate 	config_fini();
35197c478bd9Sstevel@tonic-gate 	repval_fini();
35207c478bd9Sstevel@tonic-gate 	poll_fini();
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	/* Close audit session */
35237c478bd9Sstevel@tonic-gate 	(void) adt_end_session(audit_handle);
35247c478bd9Sstevel@tonic-gate }
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate static int
35277c478bd9Sstevel@tonic-gate init(void)
35287c478bd9Sstevel@tonic-gate {
35297c478bd9Sstevel@tonic-gate 	int err;
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate 	if (repval_init() < 0)
35327c478bd9Sstevel@tonic-gate 		goto failed;
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 	if (config_init() < 0)
35357c478bd9Sstevel@tonic-gate 		goto failed;
35367c478bd9Sstevel@tonic-gate 
3537eed64e98Sgm 	refresh_debug_flag();
3538eed64e98Sgm 
35397c478bd9Sstevel@tonic-gate 	if (tlx_init() < 0)
35407c478bd9Sstevel@tonic-gate 		goto failed;
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 	/* Setup instance list. */
35437c478bd9Sstevel@tonic-gate 	if ((instance_pool = uu_list_pool_create("instance_pool",
35447c478bd9Sstevel@tonic-gate 	    sizeof (instance_t), offsetof(instance_t, link), NULL,
35457c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG)) == NULL) {
35467c478bd9Sstevel@tonic-gate 		error_msg("%s: %s",
35477c478bd9Sstevel@tonic-gate 		    gettext("Failed to create instance pool"),
35487c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
35497c478bd9Sstevel@tonic-gate 		goto failed;
35507c478bd9Sstevel@tonic-gate 	}
35517c478bd9Sstevel@tonic-gate 	if ((instance_list = uu_list_create(instance_pool, NULL, 0)) == NULL) {
35527c478bd9Sstevel@tonic-gate 		error_msg("%s: %s",
35537c478bd9Sstevel@tonic-gate 		    gettext("Failed to create instance list"),
35547c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
35557c478bd9Sstevel@tonic-gate 		goto failed;
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	/*
35597c478bd9Sstevel@tonic-gate 	 * Create event pipe to communicate events with the main event
35607c478bd9Sstevel@tonic-gate 	 * loop and add it to the event loop's fdset.
35617c478bd9Sstevel@tonic-gate 	 */
35627c478bd9Sstevel@tonic-gate 	if (pipe(rst_event_pipe) < 0) {
35637c478bd9Sstevel@tonic-gate 		error_msg("pipe: %s", strerror(errno));
35647c478bd9Sstevel@tonic-gate 		goto failed;
35657c478bd9Sstevel@tonic-gate 	}
35667c478bd9Sstevel@tonic-gate 	/*
35677c478bd9Sstevel@tonic-gate 	 * We only leave the producer end to block on reads/writes as we
35687c478bd9Sstevel@tonic-gate 	 * can't afford to block in the main thread, yet need to in
35697c478bd9Sstevel@tonic-gate 	 * the restarter event thread, so it can sit and wait for an
35707c478bd9Sstevel@tonic-gate 	 * acknowledgement to be written to the pipe.
35717c478bd9Sstevel@tonic-gate 	 */
35727c478bd9Sstevel@tonic-gate 	disable_blocking(rst_event_pipe[PE_CONSUMER]);
35737c478bd9Sstevel@tonic-gate 	if ((set_pollfd(rst_event_pipe[PE_CONSUMER], POLLIN)) == -1)
35747c478bd9Sstevel@tonic-gate 		goto failed;
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate 	/*
35777c478bd9Sstevel@tonic-gate 	 * Register with master restarter for managed service events. This
35787c478bd9Sstevel@tonic-gate 	 * will fail, amongst other reasons, if inetd is already running.
35797c478bd9Sstevel@tonic-gate 	 */
35807c478bd9Sstevel@tonic-gate 	if ((err = restarter_bind_handle(RESTARTER_EVENT_VERSION,
35817c478bd9Sstevel@tonic-gate 	    INETD_INSTANCE_FMRI, restarter_event_proxy, 0,
35827c478bd9Sstevel@tonic-gate 	    &rst_event_handle)) != 0) {
35837c478bd9Sstevel@tonic-gate 		error_msg(gettext(
35847c478bd9Sstevel@tonic-gate 		    "Failed to register for restarter events: %s"),
35857c478bd9Sstevel@tonic-gate 		    strerror(err));
35867c478bd9Sstevel@tonic-gate 		goto failed;
35877c478bd9Sstevel@tonic-gate 	}
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	if (contract_init() < 0)
35907c478bd9Sstevel@tonic-gate 		goto failed;
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	if ((timer_queue = iu_tq_create()) == NULL) {
35937c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to create timer queue."));
35947c478bd9Sstevel@tonic-gate 		goto failed;
35957c478bd9Sstevel@tonic-gate 	}
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	if (uds_init() < 0)
35987c478bd9Sstevel@tonic-gate 		goto failed;
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate 	if (method_init() < 0)
36017c478bd9Sstevel@tonic-gate 		goto failed;
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	/* Initialize auditing session */
36047c478bd9Sstevel@tonic-gate 	if (adt_start_session(&audit_handle, NULL, ADT_USE_PROC_DATA) != 0) {
36057c478bd9Sstevel@tonic-gate 		error_msg(gettext("Unable to start audit session"));
36067c478bd9Sstevel@tonic-gate 	}
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 	/*
36097c478bd9Sstevel@tonic-gate 	 * Initialize signal dispositions/masks
36107c478bd9Sstevel@tonic-gate 	 */
36117c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, sighup_handler);
36127c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm_handler);
36137c478bd9Sstevel@tonic-gate 	(void) sigignore(SIGINT);
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 	return (0);
36167c478bd9Sstevel@tonic-gate 
36177c478bd9Sstevel@tonic-gate failed:
36187c478bd9Sstevel@tonic-gate 	fini();
36197c478bd9Sstevel@tonic-gate 	return (-1);
36207c478bd9Sstevel@tonic-gate }
36217c478bd9Sstevel@tonic-gate 
36227c478bd9Sstevel@tonic-gate static int
36237c478bd9Sstevel@tonic-gate start_method(void)
36247c478bd9Sstevel@tonic-gate {
36257c478bd9Sstevel@tonic-gate 	int	i;
36267c478bd9Sstevel@tonic-gate 	int	pipe_fds[2];
36277c478bd9Sstevel@tonic-gate 	int	child;
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate 	/* Create pipe for child to notify parent of initialization success. */
36307c478bd9Sstevel@tonic-gate 	if (pipe(pipe_fds) < 0) {
3631eed64e98Sgm 		error_msg("pipe: %s", strerror(errno));
36327c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
36337c478bd9Sstevel@tonic-gate 	}
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	if ((child = fork()) == -1) {
3636eed64e98Sgm 		error_msg("fork: %s", strerror(errno));
36377c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_CONSUMER]);
36387c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_PRODUCER]);
36397c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
36407c478bd9Sstevel@tonic-gate 	} else if (child > 0) {			/* parent */
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate 		/* Wait on child to return success of initialization. */
36437c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_PRODUCER]);
36447c478bd9Sstevel@tonic-gate 		if ((safe_read(pipe_fds[PE_CONSUMER], &i, sizeof (i)) != 0) ||
36457c478bd9Sstevel@tonic-gate 		    (i < 0)) {
36467c478bd9Sstevel@tonic-gate 			error_msg(gettext(
36477c478bd9Sstevel@tonic-gate 			    "Initialization failed, unable to start"));
36487c478bd9Sstevel@tonic-gate 			(void) close(pipe_fds[PE_CONSUMER]);
36497c478bd9Sstevel@tonic-gate 			/*
36507c478bd9Sstevel@tonic-gate 			 * Batch all initialization errors as 'other' errors,
36517c478bd9Sstevel@tonic-gate 			 * resulting in retries being attempted.
36527c478bd9Sstevel@tonic-gate 			 */
36537c478bd9Sstevel@tonic-gate 			return (SMF_EXIT_ERR_OTHER);
36547c478bd9Sstevel@tonic-gate 		} else {
36557c478bd9Sstevel@tonic-gate 			(void) close(pipe_fds[PE_CONSUMER]);
36567c478bd9Sstevel@tonic-gate 			return (SMF_EXIT_OK);
36577c478bd9Sstevel@tonic-gate 		}
36587c478bd9Sstevel@tonic-gate 	} else {				/* child */
36597c478bd9Sstevel@tonic-gate 		/*
36607c478bd9Sstevel@tonic-gate 		 * Perform initialization and return success code down
36617c478bd9Sstevel@tonic-gate 		 * the pipe.
36627c478bd9Sstevel@tonic-gate 		 */
36637c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_CONSUMER]);
36647c478bd9Sstevel@tonic-gate 		i = init();
36657c478bd9Sstevel@tonic-gate 		if ((safe_write(pipe_fds[PE_PRODUCER], &i, sizeof (i)) < 0) ||
36667c478bd9Sstevel@tonic-gate 		    (i < 0)) {
36677c478bd9Sstevel@tonic-gate 			error_msg(gettext("pipe write failure: %s"),
36687c478bd9Sstevel@tonic-gate 			    strerror(errno));
36697c478bd9Sstevel@tonic-gate 			exit(1);
36707c478bd9Sstevel@tonic-gate 		}
36717c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_PRODUCER]);
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 		(void) setsid();
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 		/*
36767c478bd9Sstevel@tonic-gate 		 * Log a message if the configuration file has changed since
36777c478bd9Sstevel@tonic-gate 		 * inetconv was last run.
36787c478bd9Sstevel@tonic-gate 		 */
36797c478bd9Sstevel@tonic-gate 		check_conf_file();
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 		event_loop();
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 		fini();
36847c478bd9Sstevel@tonic-gate 		debug_msg("inetd stopped");
36857c478bd9Sstevel@tonic-gate 		msg_fini();
36867c478bd9Sstevel@tonic-gate 		exit(0);
36877c478bd9Sstevel@tonic-gate 	}
36887c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
36897c478bd9Sstevel@tonic-gate }
36907c478bd9Sstevel@tonic-gate 
36917c478bd9Sstevel@tonic-gate /*
36927c478bd9Sstevel@tonic-gate  * When inetd is run from outside the SMF, this message is output to provide
36937c478bd9Sstevel@tonic-gate  * the person invoking inetd with further information that will help them
36947c478bd9Sstevel@tonic-gate  * understand how to start and stop inetd, and to achieve the other
36957c478bd9Sstevel@tonic-gate  * behaviors achievable with the legacy inetd command line interface, if
36967c478bd9Sstevel@tonic-gate  * it is possible.
36977c478bd9Sstevel@tonic-gate  */
36987c478bd9Sstevel@tonic-gate static void
36997c478bd9Sstevel@tonic-gate legacy_usage(void)
37007c478bd9Sstevel@tonic-gate {
37017c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
37027c478bd9Sstevel@tonic-gate 	    "inetd is now an smf(5) managed service and can no longer be run "
37037c478bd9Sstevel@tonic-gate 	    "from the\n"
37047c478bd9Sstevel@tonic-gate 	    "command line. To enable or disable inetd refer to svcadm(1M) on\n"
37057c478bd9Sstevel@tonic-gate 	    "how to enable \"%s\", the inetd instance.\n"
37067c478bd9Sstevel@tonic-gate 	    "\n"
37077c478bd9Sstevel@tonic-gate 	    "The traditional inetd command line option mappings are:\n"
37087c478bd9Sstevel@tonic-gate 	    "\t-d : there is no supported debug output\n"
37097c478bd9Sstevel@tonic-gate 	    "\t-s : inetd is only runnable from within the SMF\n"
37107c478bd9Sstevel@tonic-gate 	    "\t-t : See inetadm(1M) on how to enable TCP tracing\n"
37117c478bd9Sstevel@tonic-gate 	    "\t-r : See inetadm(1M) on how to set a failure rate\n"
37127c478bd9Sstevel@tonic-gate 	    "\n"
37137c478bd9Sstevel@tonic-gate 	    "To specify an alternative configuration file see svccfg(1M)\n"
37147c478bd9Sstevel@tonic-gate 	    "for how to modify the \"%s/%s\" string type property of\n"
37157c478bd9Sstevel@tonic-gate 	    "the inetd instance, and modify it according to the syntax:\n"
37167c478bd9Sstevel@tonic-gate 	    "\"%s [alt_config_file] %%m\".\n"
37177c478bd9Sstevel@tonic-gate 	    "\n"
37187c478bd9Sstevel@tonic-gate 	    "For further information on inetd see inetd(1M).\n",
37197c478bd9Sstevel@tonic-gate 	    INETD_INSTANCE_FMRI, START_METHOD_ARG, SCF_PROPERTY_EXEC,
37207c478bd9Sstevel@tonic-gate 	    INETD_PATH);
37217c478bd9Sstevel@tonic-gate }
37227c478bd9Sstevel@tonic-gate 
37237c478bd9Sstevel@tonic-gate /*
37247c478bd9Sstevel@tonic-gate  * Usage message printed out for usage errors when running under the SMF.
37257c478bd9Sstevel@tonic-gate  */
37267c478bd9Sstevel@tonic-gate static void
37277c478bd9Sstevel@tonic-gate smf_usage(const char *arg0)
37287c478bd9Sstevel@tonic-gate {
37297c478bd9Sstevel@tonic-gate 	error_msg("Usage: %s [alt_conf_file] %s|%s|%s", arg0, START_METHOD_ARG,
37307c478bd9Sstevel@tonic-gate 	    STOP_METHOD_ARG, REFRESH_METHOD_ARG);
37317c478bd9Sstevel@tonic-gate }
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate /*
37347c478bd9Sstevel@tonic-gate  * Returns B_TRUE if we're being run from within the SMF, else B_FALSE.
37357c478bd9Sstevel@tonic-gate  */
37367c478bd9Sstevel@tonic-gate static boolean_t
37377c478bd9Sstevel@tonic-gate run_through_smf(void)
37387c478bd9Sstevel@tonic-gate {
37397c478bd9Sstevel@tonic-gate 	char *fmri;
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 	/*
37427c478bd9Sstevel@tonic-gate 	 * check if the instance fmri environment variable has been set by
37437c478bd9Sstevel@tonic-gate 	 * our restarter.
37447c478bd9Sstevel@tonic-gate 	 */
37457c478bd9Sstevel@tonic-gate 	return (((fmri = getenv("SMF_FMRI")) != NULL) &&
37467c478bd9Sstevel@tonic-gate 	    (strcmp(fmri, INETD_INSTANCE_FMRI) == 0));
37477c478bd9Sstevel@tonic-gate }
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate int
37507c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
37517c478bd9Sstevel@tonic-gate {
37527c478bd9Sstevel@tonic-gate 	char		*method;
37537c478bd9Sstevel@tonic-gate 	int		ret;
37547c478bd9Sstevel@tonic-gate 
37557c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
37567c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
37577c478bd9Sstevel@tonic-gate #endif
37587c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
37597c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate 	if (!run_through_smf()) {
37627c478bd9Sstevel@tonic-gate 		legacy_usage();
37637c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_NOSMF);
37647c478bd9Sstevel@tonic-gate 	}
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	msg_init();	/* setup logging */
37677c478bd9Sstevel@tonic-gate 
3768004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3769004388ebScasper 
37707c478bd9Sstevel@tonic-gate 	/* inetd invocation syntax is inetd [alt_conf_file] method_name */
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	switch (argc) {
37737c478bd9Sstevel@tonic-gate 	case 2:
37747c478bd9Sstevel@tonic-gate 		method = argv[1];
37757c478bd9Sstevel@tonic-gate 		break;
37767c478bd9Sstevel@tonic-gate 	case 3:
37777c478bd9Sstevel@tonic-gate 		conf_file = argv[1];
37787c478bd9Sstevel@tonic-gate 		method = argv[2];
37797c478bd9Sstevel@tonic-gate 		break;
37807c478bd9Sstevel@tonic-gate 	default:
37817c478bd9Sstevel@tonic-gate 		smf_usage(argv[0]);
37827c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_CONFIG);
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	}
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate 	if (strcmp(method, START_METHOD_ARG) == 0) {
37877c478bd9Sstevel@tonic-gate 		ret = start_method();
37887c478bd9Sstevel@tonic-gate 	} else if (strcmp(method, STOP_METHOD_ARG) == 0) {
37897c478bd9Sstevel@tonic-gate 		ret = stop_method();
37907c478bd9Sstevel@tonic-gate 	} else if (strcmp(method, REFRESH_METHOD_ARG) == 0) {
37917c478bd9Sstevel@tonic-gate 		ret = refresh_method();
37927c478bd9Sstevel@tonic-gate 	} else {
37937c478bd9Sstevel@tonic-gate 		smf_usage(argv[0]);
37947c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_CONFIG);
37957c478bd9Sstevel@tonic-gate 	}
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate 	return (ret);
37987c478bd9Sstevel@tonic-gate }
3799