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