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