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