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