17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53ad28c1eSrm  * Common Development and Distribution License (the "License").
63ad28c1eSrm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b8afd3a7SGary Mills  * Copyright (c) 2011 Gary Mills
23b8afd3a7SGary Mills  *
24f6e214c7SGavin Maltby  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * NOTES: To be expanded.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * The SMF inetd.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Below are some high level notes of the operation of the SMF inetd. The
337c478bd9Sstevel@tonic-gate  * notes don't go into any real detail, and the viewer of this file is
347c478bd9Sstevel@tonic-gate  * encouraged to look at the code and its associated comments to better
357c478bd9Sstevel@tonic-gate  * understand inetd's operation. This saves the potential for the code
367c478bd9Sstevel@tonic-gate  * and these notes diverging over time.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Inetd's major work is done from the context of event_loop(). Within this
397c478bd9Sstevel@tonic-gate  * loop, inetd polls for events arriving from a number of different file
407c478bd9Sstevel@tonic-gate  * descriptors, representing the following event types, and initiates
417c478bd9Sstevel@tonic-gate  * any necessary event processing:
427c478bd9Sstevel@tonic-gate  * - incoming network connections/datagrams.
437c478bd9Sstevel@tonic-gate  * - notification of terminated processes (discovered via contract events).
447c478bd9Sstevel@tonic-gate  * - instance specific events originating from the SMF master restarter.
457c478bd9Sstevel@tonic-gate  * - stop/refresh requests from the inetd method processes (coming in on a
467c478bd9Sstevel@tonic-gate  *   Unix Domain socket).
477c478bd9Sstevel@tonic-gate  * There's also a timeout set for the poll, which is set to the nearest
487c478bd9Sstevel@tonic-gate  * scheduled timer in a timer queue that inetd uses to perform delayed
497c478bd9Sstevel@tonic-gate  * processing, such as bind retries.
507c478bd9Sstevel@tonic-gate  * The SIGHUP and SIGINT signals can also interrupt the poll, and will
517c478bd9Sstevel@tonic-gate  * result in inetd being refreshed or stopped respectively, as was the
527c478bd9Sstevel@tonic-gate  * behavior with the old inetd.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Inetd implements a state machine for each instance. The states within the
557c478bd9Sstevel@tonic-gate  * machine are: offline, online, disabled, maintenance, uninitialized and
567c478bd9Sstevel@tonic-gate  * specializations of the offline state for when an instance exceeds one of
577c478bd9Sstevel@tonic-gate  * its DOS limits. The state of an instance can be changed as a
587c478bd9Sstevel@tonic-gate  * result/side-effect of one of the above events occurring, or inetd being
597c478bd9Sstevel@tonic-gate  * started up. The ongoing state of an instance is stored in the SMF
607c478bd9Sstevel@tonic-gate  * repository, as required of SMF restarters. This enables an administrator
617c478bd9Sstevel@tonic-gate  * to view the state of each instance, and, if inetd was to terminate
627c478bd9Sstevel@tonic-gate  * unexpectedly, it could use the stored state to re-commence where it left off.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * Within the state machine a number of methods are run (if provided) as part
657c478bd9Sstevel@tonic-gate  * of a state transition to aid/ effect a change in an instance's state. The
667c478bd9Sstevel@tonic-gate  * supported methods are: offline, online, disable, refresh and start. The
677c478bd9Sstevel@tonic-gate  * latter of these is the equivalent of the server program and its arguments
687c478bd9Sstevel@tonic-gate  * in the old inetd.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * Events from the SMF master restarter come in on a number of threads
717c478bd9Sstevel@tonic-gate  * created in the registration routine of librestart, the delegated restarter
727c478bd9Sstevel@tonic-gate  * library. These threads call into the restart_event_proxy() function
737c478bd9Sstevel@tonic-gate  * when an event arrives. To serialize the processing of instances, these events
747c478bd9Sstevel@tonic-gate  * are then written down a pipe to the process's main thread, which listens
757c478bd9Sstevel@tonic-gate  * for these events via a poll call, with the file descriptor of the other
767c478bd9Sstevel@tonic-gate  * end of the pipe in its read set, and processes the event appropriately.
777c478bd9Sstevel@tonic-gate  * When the event has been  processed (which may be delayed if the instance
787c478bd9Sstevel@tonic-gate  * for which the event is for is in the process of executing one of its methods
797c478bd9Sstevel@tonic-gate  * as part of a state transition) it writes an acknowledgement back down the
807c478bd9Sstevel@tonic-gate  * pipe the event was received on. The thread in restart_event_proxy() that
817c478bd9Sstevel@tonic-gate  * wrote the event will read the acknowledgement it was blocked upon, and will
827c478bd9Sstevel@tonic-gate  * then be able to return to its caller, thus implicitly acknowledging the
837c478bd9Sstevel@tonic-gate  * event, and allowing another event to be written down the pipe for the main
847c478bd9Sstevel@tonic-gate  * thread to process.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #include <netdb.h>
897c478bd9Sstevel@tonic-gate #include <stdio.h>
90004388ebScasper #include <stdio_ext.h>
917c478bd9Sstevel@tonic-gate #include <stdlib.h>
927c478bd9Sstevel@tonic-gate #include <strings.h>
937c478bd9Sstevel@tonic-gate #include <unistd.h>
947c478bd9Sstevel@tonic-gate #include <assert.h>
957c478bd9Sstevel@tonic-gate #include <sys/types.h>
967c478bd9Sstevel@tonic-gate #include <sys/socket.h>
977c478bd9Sstevel@tonic-gate #include <netinet/in.h>
987c478bd9Sstevel@tonic-gate #include <fcntl.h>
997c478bd9Sstevel@tonic-gate #include <signal.h>
1007c478bd9Sstevel@tonic-gate #include <errno.h>
1017c478bd9Sstevel@tonic-gate #include <locale.h>
1027c478bd9Sstevel@tonic-gate #include <syslog.h>
1037c478bd9Sstevel@tonic-gate #include <libintl.h>
1047c478bd9Sstevel@tonic-gate #include <librestart.h>
1057c478bd9Sstevel@tonic-gate #include <pthread.h>
1067c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1077c478bd9Sstevel@tonic-gate #include <time.h>
1087c478bd9Sstevel@tonic-gate #include <limits.h>
1097c478bd9Sstevel@tonic-gate #include <libgen.h>
1107c478bd9Sstevel@tonic-gate #include <tcpd.h>
1117c478bd9Sstevel@tonic-gate #include <libscf.h>
1127c478bd9Sstevel@tonic-gate #include <libuutil.h>
1137c478bd9Sstevel@tonic-gate #include <stddef.h>
1147c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
1157cd7ea0bSrs #include <ucred.h>
1167c478bd9Sstevel@tonic-gate #include "inetd_impl.h"
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* path to inetd's binary */
1197c478bd9Sstevel@tonic-gate #define	INETD_PATH	"/usr/lib/inet/inetd"
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * inetd's default configuration file paths. /etc/inetd/inetd.conf is set
1237c478bd9Sstevel@tonic-gate  * be be the primary file, so it is checked before /etc/inetd.conf.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate #define	PRIMARY_DEFAULT_CONF_FILE	"/etc/inet/inetd.conf"
1267c478bd9Sstevel@tonic-gate #define	SECONDARY_DEFAULT_CONF_FILE	"/etc/inetd.conf"
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* Arguments passed to this binary to request which method to execute. */
1297c478bd9Sstevel@tonic-gate #define	START_METHOD_ARG	"start"
1307c478bd9Sstevel@tonic-gate #define	STOP_METHOD_ARG		"stop"
1317c478bd9Sstevel@tonic-gate #define	REFRESH_METHOD_ARG	"refresh"
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* connection backlog for unix domain socket */
1347c478bd9Sstevel@tonic-gate #define	UDS_BACKLOG	2
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* number of retries to recv() a request on the UDS socket before giving up */
1377c478bd9Sstevel@tonic-gate #define	UDS_RECV_RETRIES	10
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* enumeration of the different ends of a pipe */
1407c478bd9Sstevel@tonic-gate enum pipe_end {
1417c478bd9Sstevel@tonic-gate 	PE_CONSUMER,
1427c478bd9Sstevel@tonic-gate 	PE_PRODUCER
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate typedef struct {
1467c478bd9Sstevel@tonic-gate 	internal_inst_state_t		istate;
1477c478bd9Sstevel@tonic-gate 	const char			*name;
1487c478bd9Sstevel@tonic-gate 	restarter_instance_state_t	smf_state;
1497c478bd9Sstevel@tonic-gate 	instance_method_t		method_running;
1507c478bd9Sstevel@tonic-gate } state_info_t;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Collection of information for each state.
1557c478bd9Sstevel@tonic-gate  * NOTE:  This table is indexed into using the internal_inst_state_t
1567c478bd9Sstevel@tonic-gate  * enumeration, so the ordering needs to be kept in synch.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate static state_info_t states[] = {
1597c478bd9Sstevel@tonic-gate 	{IIS_UNINITIALIZED, "uninitialized", RESTARTER_STATE_UNINIT,
1607c478bd9Sstevel@tonic-gate 	    IM_NONE},
1617c478bd9Sstevel@tonic-gate 	{IIS_ONLINE, "online", RESTARTER_STATE_ONLINE, IM_START},
1627c478bd9Sstevel@tonic-gate 	{IIS_IN_ONLINE_METHOD, "online_method", RESTARTER_STATE_OFFLINE,
1637c478bd9Sstevel@tonic-gate 	    IM_ONLINE},
1647c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE, "offline", RESTARTER_STATE_OFFLINE, IM_NONE},
1657c478bd9Sstevel@tonic-gate 	{IIS_IN_OFFLINE_METHOD, "offline_method", RESTARTER_STATE_OFFLINE,
1667c478bd9Sstevel@tonic-gate 	    IM_OFFLINE},
1677c478bd9Sstevel@tonic-gate 	{IIS_DISABLED, "disabled", RESTARTER_STATE_DISABLED, IM_NONE},
1687c478bd9Sstevel@tonic-gate 	{IIS_IN_DISABLE_METHOD, "disabled_method", RESTARTER_STATE_OFFLINE,
1697c478bd9Sstevel@tonic-gate 	    IM_DISABLE},
1707c478bd9Sstevel@tonic-gate 	{IIS_IN_REFRESH_METHOD, "refresh_method", RESTARTER_STATE_ONLINE,
1717c478bd9Sstevel@tonic-gate 	    IM_REFRESH},
1727c478bd9Sstevel@tonic-gate 	{IIS_MAINTENANCE, "maintenance", RESTARTER_STATE_MAINT, IM_NONE},
1737c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_CONRATE, "cr_offline", RESTARTER_STATE_OFFLINE, IM_NONE},
1747c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_BIND, "bind_offline", RESTARTER_STATE_OFFLINE, IM_NONE},
1757c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_COPIES, "copies_offline", RESTARTER_STATE_OFFLINE,
1767c478bd9Sstevel@tonic-gate 	    IM_NONE},
1777c478bd9Sstevel@tonic-gate 	{IIS_DEGRADED, "degraded", RESTARTER_STATE_DEGRADED, IM_NONE},
1787c478bd9Sstevel@tonic-gate 	{IIS_NONE, "none", RESTARTER_STATE_NONE, IM_NONE}
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Pipe used to send events from the threads created by restarter_bind_handle()
1837c478bd9Sstevel@tonic-gate  * to the main thread of control.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static int			rst_event_pipe[] = {-1, -1};
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Used to protect the critical section of code in restarter_event_proxy() that
1887c478bd9Sstevel@tonic-gate  * involves writing an event down the event pipe and reading an acknowledgement.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate static pthread_mutex_t		rst_event_pipe_mtx = PTHREAD_MUTEX_INITIALIZER;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* handle used in communication with the master restarter */
1937c478bd9Sstevel@tonic-gate static restarter_event_handle_t *rst_event_handle = NULL;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* set to indicate a refresh of inetd is requested */
1967c478bd9Sstevel@tonic-gate static boolean_t		refresh_inetd_requested = B_FALSE;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /* set by the SIGTERM handler to flag we got a SIGTERM */
1997c478bd9Sstevel@tonic-gate static boolean_t		got_sigterm = B_FALSE;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Timer queue used to store timers for delayed event processing, such as
2037c478bd9Sstevel@tonic-gate  * bind retries.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate iu_tq_t				*timer_queue = NULL;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * fd of Unix Domain socket used to communicate stop and refresh requests
2097c478bd9Sstevel@tonic-gate  * to the inetd start method process.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate static int			uds_fd = -1;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * List of inetd's currently managed instances; each containing its state,
2157c478bd9Sstevel@tonic-gate  * and in certain states its configuration.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate static uu_list_pool_t		*instance_pool = NULL;
2187c478bd9Sstevel@tonic-gate uu_list_t			*instance_list = NULL;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /* set to indicate we're being stopped */
2217c478bd9Sstevel@tonic-gate boolean_t			inetd_stopping = B_FALSE;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /* TCP wrappers syslog globals. Consumed by libwrap. */
2247c478bd9Sstevel@tonic-gate int				allow_severity = LOG_INFO;
2257c478bd9Sstevel@tonic-gate int				deny_severity = LOG_WARNING;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /* path of the configuration file being monitored by check_conf_file() */
2287c478bd9Sstevel@tonic-gate static char			*conf_file = NULL;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /* Auditing session handle */
2317c478bd9Sstevel@tonic-gate static adt_session_data_t	*audit_handle;
2327c478bd9Sstevel@tonic-gate 
233ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India /* Number of pending connections */
234ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India static size_t			tlx_pending_counter;
235ca35ed50SYateesh Kumar Vusirika - Sun Microsystems - Bangalore India 
2367c478bd9Sstevel@tonic-gate static void uds_fini(void);
2377c478bd9Sstevel@tonic-gate static int uds_init(void);
2387c478bd9Sstevel@tonic-gate static int run_method(instance_t *, instance_method_t, const proto_info_t *);
2397c478bd9Sstevel@tonic-gate static void create_bound_fds(instance_t *);
2407c478bd9Sstevel@tonic-gate static void destroy_bound_fds(instance_t *);
2417c478bd9Sstevel@tonic-gate static void destroy_instance(instance_t *);
2427c478bd9Sstevel@tonic-gate static void inetd_stop(void);
243b823d4c7Sdstaff static void
244b823d4c7Sdstaff exec_method(instance_t *instance, instance_method_t method, method_info_t *mi,
245b823d4c7Sdstaff     struct method_context *mthd_ctxt, const proto_info_t *pi) __NORETURN;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * The following two functions are callbacks that libumem uses to determine
2497c478bd9Sstevel@tonic-gate  * inetd's desired debugging/logging levels. The interface they consume is
2507c478bd9Sstevel@tonic-gate  * exported by FMA and is consolidation private. The comments in the two
2517c478bd9Sstevel@tonic-gate  * functions give the environment variable that will effectively be set to
2527c478bd9Sstevel@tonic-gate  * their returned value, and thus whose behavior for this value, described in
2537c478bd9Sstevel@tonic-gate  * umem_debug(3MALLOC), will be followed.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate const char *
_umem_debug_init(void)2577c478bd9Sstevel@tonic-gate _umem_debug_init(void)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	return ("default,verbose");	/* UMEM_DEBUG setting */
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate const char *
_umem_logging_init(void)2637c478bd9Sstevel@tonic-gate _umem_logging_init(void)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	return ("fail,contents");	/* UMEM_LOGGING setting */
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static void
log_invalid_cfg(const char * fmri)2697c478bd9Sstevel@tonic-gate log_invalid_cfg(const char *fmri)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	error_msg(gettext(
2727c478bd9Sstevel@tonic-gate 	    "Invalid configuration for instance %s, placing in maintenance"),
2737c478bd9Sstevel@tonic-gate 	    fmri);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * Returns B_TRUE if the instance is in a suitable state for inetd to stop.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate static boolean_t
instance_stopped(const instance_t * inst)2807c478bd9Sstevel@tonic-gate instance_stopped(const instance_t *inst)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	return ((inst->cur_istate == IIS_OFFLINE) ||
2837c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_MAINTENANCE) ||
2847c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_DISABLED) ||
2857c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_UNINITIALIZED));
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
288eb1a3463STruong Nguyen /*
289eb1a3463STruong Nguyen  * Given the instance fmri, obtain the corresonding scf_instance.
290eb1a3463STruong Nguyen  * Caller is responsible for freeing the returned scf_instance and
291eb1a3463STruong Nguyen  * its scf_handle.
292eb1a3463STruong Nguyen  */
293eb1a3463STruong Nguyen static int
fmri_to_instance(char * fmri,scf_instance_t ** scf_instp)294eb1a3463STruong Nguyen fmri_to_instance(char *fmri, scf_instance_t **scf_instp)
295eb1a3463STruong Nguyen {
296eb1a3463STruong Nguyen 	int retries, ret = 1;
297eb1a3463STruong Nguyen 	scf_handle_t	*h;
298eb1a3463STruong Nguyen 	scf_instance_t *scf_inst;
299eb1a3463STruong Nguyen 
300eb1a3463STruong Nguyen 	if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
301eb1a3463STruong Nguyen 		error_msg(gettext("Failed to get instance for %s"), fmri);
302eb1a3463STruong Nguyen 		return (1);
303eb1a3463STruong Nguyen 	}
304eb1a3463STruong Nguyen 
305eb1a3463STruong Nguyen 	if ((scf_inst = scf_instance_create(h)) == NULL)
306eb1a3463STruong Nguyen 		goto out;
307eb1a3463STruong Nguyen 
308eb1a3463STruong Nguyen 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
309eb1a3463STruong Nguyen 		if (make_handle_bound(h) == -1)
310eb1a3463STruong Nguyen 			break;
311eb1a3463STruong Nguyen 
312eb1a3463STruong Nguyen 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, scf_inst,
313eb1a3463STruong Nguyen 		    NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0) {
314eb1a3463STruong Nguyen 			ret = 0;
315eb1a3463STruong Nguyen 			*scf_instp = scf_inst;
316eb1a3463STruong Nguyen 			break;
317eb1a3463STruong Nguyen 		}
318eb1a3463STruong Nguyen 
319eb1a3463STruong Nguyen 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
320eb1a3463STruong Nguyen 			break;
321eb1a3463STruong Nguyen 	}
322eb1a3463STruong Nguyen 
323eb1a3463STruong Nguyen out:
324eb1a3463STruong Nguyen 	if (ret != 0) {
325eb1a3463STruong Nguyen 		error_msg(gettext("Failed to get instance for %s"), fmri);
326eb1a3463STruong Nguyen 		scf_instance_destroy(scf_inst);
327eb1a3463STruong Nguyen 		scf_handle_destroy(h);
328eb1a3463STruong Nguyen 	}
329eb1a3463STruong Nguyen 
330eb1a3463STruong Nguyen 	return (ret);
331eb1a3463STruong Nguyen }
332eb1a3463STruong Nguyen 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * Updates the current and next repository states of instance 'inst'. If
3357c478bd9Sstevel@tonic-gate  * any errors occur an error message is output.
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@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)3387c478bd9Sstevel@tonic-gate update_instance_states(instance_t *inst, internal_inst_state_t new_cur_state,
3397c478bd9Sstevel@tonic-gate     internal_inst_state_t new_next_state, restarter_error_t err)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	internal_inst_state_t	old_cur = inst->cur_istate;
3427c478bd9Sstevel@tonic-gate 	internal_inst_state_t	old_next = inst->next_istate;
343eb1a3463STruong Nguyen 	scf_instance_t		*scf_inst = NULL;
3447c478bd9Sstevel@tonic-gate 	scf_error_t		sret;
3457c478bd9Sstevel@tonic-gate 	int			ret;
346f6e214c7SGavin Maltby 	restarter_str_t		aux = restarter_str_none;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* update the repository/cached internal state */
3497c478bd9Sstevel@tonic-gate 	inst->cur_istate = new_cur_state;
3507c478bd9Sstevel@tonic-gate 	inst->next_istate = new_next_state;
3517c478bd9Sstevel@tonic-gate 	(void) set_single_rep_val(inst->cur_istate_rep,
3527c478bd9Sstevel@tonic-gate 	    (int64_t)new_cur_state);
3537c478bd9Sstevel@tonic-gate 	(void) set_single_rep_val(inst->next_istate_rep,
3547c478bd9Sstevel@tonic-gate 	    (int64_t)new_next_state);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (((sret = store_rep_vals(inst->cur_istate_rep, inst->fmri,
3577c478bd9Sstevel@tonic-gate 	    PR_NAME_CUR_INT_STATE)) != 0) ||
3587c478bd9Sstevel@tonic-gate 	    ((sret = store_rep_vals(inst->next_istate_rep, inst->fmri,
3597c478bd9Sstevel@tonic-gate 	    PR_NAME_NEXT_INT_STATE)) != 0))
3607c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to update state of instance %s in "
3617c478bd9Sstevel@tonic-gate 		    "repository: %s"), inst->fmri, scf_strerror(sret));
3627c478bd9Sstevel@tonic-gate 
363eb1a3463STruong Nguyen 	if (fmri_to_instance(inst->fmri, &scf_inst) == 0) {
364eb1a3463STruong Nguyen 		/*
365eb1a3463STruong Nguyen 		 * If transitioning to maintenance, check auxiliary_tty set
366eb1a3463STruong Nguyen 		 * by svcadm and assign appropriate value to auxiliary_state.
367eb1a3463STruong Nguyen 		 * If the maintenance event comes from a service request,
368eb1a3463STruong Nguyen 		 * validate auxiliary_fmri and copy it to
369eb1a3463STruong Nguyen 		 * restarter/auxiliary_fmri.
370eb1a3463STruong Nguyen 		 */
371eb1a3463STruong Nguyen 		if (new_cur_state == IIS_MAINTENANCE) {
372eb1a3463STruong Nguyen 			if (restarter_inst_ractions_from_tty(scf_inst) == 0)
373f6e214c7SGavin Maltby 				aux = restarter_str_service_request;
374eb1a3463STruong Nguyen 			else
375f6e214c7SGavin Maltby 				aux = restarter_str_administrative_request;
376eb1a3463STruong Nguyen 		}
377eb1a3463STruong Nguyen 
378f6e214c7SGavin Maltby 		if (aux == restarter_str_service_request) {
379eb1a3463STruong Nguyen 			if (restarter_inst_validate_ractions_aux_fmri(
380eb1a3463STruong Nguyen 			    scf_inst) == 0) {
381eb1a3463STruong Nguyen 				if (restarter_inst_set_aux_fmri(scf_inst))
382eb1a3463STruong Nguyen 					error_msg(gettext("Could not set "
383eb1a3463STruong Nguyen 					    "auxiliary_fmri property for %s"),
384eb1a3463STruong Nguyen 					    inst->fmri);
385eb1a3463STruong Nguyen 			} else {
386eb1a3463STruong Nguyen 				if (restarter_inst_reset_aux_fmri(scf_inst))
387eb1a3463STruong Nguyen 					error_msg(gettext("Could not reset "
388eb1a3463STruong Nguyen 					    "auxiliary_fmri property for %s"),
389eb1a3463STruong Nguyen 					    inst->fmri);
390eb1a3463STruong Nguyen 			}
391eb1a3463STruong Nguyen 		}
392eb1a3463STruong Nguyen 		scf_handle_destroy(scf_instance_handle(scf_inst));
393eb1a3463STruong Nguyen 		scf_instance_destroy(scf_inst);
394eb1a3463STruong Nguyen 	}
395eb1a3463STruong Nguyen 
3967c478bd9Sstevel@tonic-gate 	/* update the repository SMF state */
3977c478bd9Sstevel@tonic-gate 	if ((ret = restarter_set_states(rst_event_handle, inst->fmri,
3987c478bd9Sstevel@tonic-gate 	    states[old_cur].smf_state, states[new_cur_state].smf_state,
3997c478bd9Sstevel@tonic-gate 	    states[old_next].smf_state, states[new_next_state].smf_state,
400eb1a3463STruong Nguyen 	    err, aux)) != 0)
4017c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to update state of instance %s in "
4027c478bd9Sstevel@tonic-gate 		    "repository: %s"), inst->fmri, strerror(ret));
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate void
update_state(instance_t * inst,internal_inst_state_t new_cur,restarter_error_t err)4067c478bd9Sstevel@tonic-gate update_state(instance_t *inst, internal_inst_state_t new_cur,
4077c478bd9Sstevel@tonic-gate     restarter_error_t err)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	update_instance_states(inst, new_cur, IIS_NONE, err);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * Sends a refresh event to the inetd start method process and returns
4147c478bd9Sstevel@tonic-gate  * SMF_EXIT_OK if it managed to send it. If it fails to send the request for
4157c478bd9Sstevel@tonic-gate  * some reason it returns SMF_EXIT_ERR_OTHER.
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate static int
refresh_method(void)4187c478bd9Sstevel@tonic-gate refresh_method(void)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	uds_request_t   req = UR_REFRESH_INETD;
4217c478bd9Sstevel@tonic-gate 	int		fd;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if ((fd = connect_to_inetd()) < 0) {
4247c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to connect to inetd: %s"),
4257c478bd9Sstevel@tonic-gate 		    strerror(errno));
4267c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/* write the request and return success */
4307c478bd9Sstevel@tonic-gate 	if (safe_write(fd, &req, sizeof (req)) == -1) {
4317c478bd9Sstevel@tonic-gate 		error_msg(
4327c478bd9Sstevel@tonic-gate 		    gettext("Failed to send refresh request to inetd: %s"),
4337c478bd9Sstevel@tonic-gate 		    strerror(errno));
4347c478bd9Sstevel@tonic-gate 		(void) close(fd);
4357c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	(void) close(fd);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	return (SMF_EXIT_OK);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /*
4447c478bd9Sstevel@tonic-gate  * Sends a stop event to the inetd start method process and wait till it goes
4457c478bd9Sstevel@tonic-gate  * away. If inetd is determined to have stopped SMF_EXIT_OK is returned, else
4467c478bd9Sstevel@tonic-gate  * SMF_EXIT_ERR_OTHER is returned.
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate static int
stop_method(void)4497c478bd9Sstevel@tonic-gate stop_method(void)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	uds_request_t   req = UR_STOP_INETD;
4527c478bd9Sstevel@tonic-gate 	int		fd;
4537c478bd9Sstevel@tonic-gate 	char		c;
4547c478bd9Sstevel@tonic-gate 	ssize_t		ret;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if ((fd = connect_to_inetd()) == -1) {
4577c478bd9Sstevel@tonic-gate 		debug_msg(gettext("Failed to connect to inetd: %s"),
4587c478bd9Sstevel@tonic-gate 		    strerror(errno));
4597c478bd9Sstevel@tonic-gate 		/*
4607c478bd9Sstevel@tonic-gate 		 * Assume connect_to_inetd() failed because inetd was already
4617c478bd9Sstevel@tonic-gate 		 * stopped, and return success.
4627c478bd9Sstevel@tonic-gate 		 */
4637c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_OK);
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * This is safe to do since we're fired off in a separate process
4687c478bd9Sstevel@tonic-gate 	 * than inetd and in the case we get wedged, the stop method timeout
4697c478bd9Sstevel@tonic-gate 	 * will occur and we'd be killed by our restarter.
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 	enable_blocking(fd);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/* write the stop request to inetd and wait till it goes away */
4747c478bd9Sstevel@tonic-gate 	if (safe_write(fd, &req, sizeof (req)) != 0) {
4757c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to send stop request to inetd"));
4767c478bd9Sstevel@tonic-gate 		(void) close(fd);
4777c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/* wait until remote end of socket is closed */
4817c478bd9Sstevel@tonic-gate 	while (((ret = recv(fd, &c, sizeof (c), 0)) != 0) && (errno == EINTR))
4827c478bd9Sstevel@tonic-gate 		;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	(void) close(fd);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (ret != 0) {
4877c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to determine whether inetd stopped"));
4887c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	return (SMF_EXIT_OK);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * This function is called to handle restarter events coming in from the
4977c478bd9Sstevel@tonic-gate  * master restarter. It is registered with the master restarter via
4987c478bd9Sstevel@tonic-gate  * restarter_bind_handle() and simply passes a pointer to the event down
4997c478bd9Sstevel@tonic-gate  * the event pipe, which will be discovered by the poll in the event loop
5007c478bd9Sstevel@tonic-gate  * and processed there. It waits for an acknowledgement to be written back down
5017c478bd9Sstevel@tonic-gate  * the pipe before returning.
5027c478bd9Sstevel@tonic-gate  * Writing a pointer to the function's 'event' parameter down the pipe will
5037c478bd9Sstevel@tonic-gate  * be safe, as the thread in restarter_event_proxy() doesn't return until
5047c478bd9Sstevel@tonic-gate  * the main thread has finished its processing of the passed event, thus
5057c478bd9Sstevel@tonic-gate  * the referenced event will remain around until the function returns.
5067c478bd9Sstevel@tonic-gate  * To impose the limit of only one event being in the pipe and processed
5077c478bd9Sstevel@tonic-gate  * at once, a lock is taken on entry to this function and returned on exit.
5087c478bd9Sstevel@tonic-gate  * Always returns 0.
5097c478bd9Sstevel@tonic-gate  */
5107c478bd9Sstevel@tonic-gate static int
restarter_event_proxy(restarter_event_t * event)5117c478bd9Sstevel@tonic-gate restarter_event_proxy(restarter_event_t *event)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	boolean_t		processed;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&rst_event_pipe_mtx);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/* write the event to the main worker thread down the pipe */
5187c478bd9Sstevel@tonic-gate 	if (safe_write(rst_event_pipe[PE_PRODUCER], &event,
5197c478bd9Sstevel@tonic-gate 	    sizeof (event)) != 0)
5207c478bd9Sstevel@tonic-gate 		goto pipe_error;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * Wait for an acknowledgement that the event has been processed from
5247c478bd9Sstevel@tonic-gate 	 * the same pipe. In the case that inetd is stopping, any thread in
5257c478bd9Sstevel@tonic-gate 	 * this function will simply block on this read until inetd eventually
5267c478bd9Sstevel@tonic-gate 	 * exits. This will result in this function not returning success to
5277c478bd9Sstevel@tonic-gate 	 * its caller, and the event that was being processed when the
5287c478bd9Sstevel@tonic-gate 	 * function exited will be re-sent when inetd is next started.
5297c478bd9Sstevel@tonic-gate 	 */
5307c478bd9Sstevel@tonic-gate 	if (safe_read(rst_event_pipe[PE_PRODUCER], &processed,
5317c478bd9Sstevel@tonic-gate 	    sizeof (processed)) != 0)
5327c478bd9Sstevel@tonic-gate 		goto pipe_error;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&rst_event_pipe_mtx);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	return (processed ? 0 : EAGAIN);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate pipe_error:
5397c478bd9Sstevel@tonic-gate 	/*
5407c478bd9Sstevel@tonic-gate 	 * Something's seriously wrong with the event pipe. Notify the
5417c478bd9Sstevel@tonic-gate 	 * worker thread by closing this end of the event pipe and pause till
5427c478bd9Sstevel@tonic-gate 	 * inetd exits.
5437c478bd9Sstevel@tonic-gate 	 */
5447c478bd9Sstevel@tonic-gate 	error_msg(gettext("Can't process restarter events: %s"),
5457c478bd9Sstevel@tonic-gate 	    strerror(errno));
5467c478bd9Sstevel@tonic-gate 	(void) close(rst_event_pipe[PE_PRODUCER]);
5477c478bd9Sstevel@tonic-gate 	for (;;)
5487c478bd9Sstevel@tonic-gate 		(void) pause();
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * Let restarter_event_proxy() know we're finished with the event it's blocked
5557c478bd9Sstevel@tonic-gate  * upon. The 'processed' argument denotes whether we successfully processed the
5567c478bd9Sstevel@tonic-gate  * event.
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate static void
ack_restarter_event(boolean_t processed)5597c478bd9Sstevel@tonic-gate ack_restarter_event(boolean_t processed)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	/*
5627c478bd9Sstevel@tonic-gate 	 * If safe_write returns -1 something's seriously wrong with the event
5637c478bd9Sstevel@tonic-gate 	 * pipe, so start the shutdown proceedings.
5647c478bd9Sstevel@tonic-gate 	 */
5657c478bd9Sstevel@tonic-gate 	if (safe_write(rst_event_pipe[PE_CONSUMER], &processed,
5667c478bd9Sstevel@tonic-gate 	    sizeof (processed)) == -1)
5677c478bd9Sstevel@tonic-gate 		inetd_stop();
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Switch the syslog identification string to 'ident'.
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate static void
change_syslog_ident(const char * ident)5747c478bd9Sstevel@tonic-gate change_syslog_ident(const char *ident)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	closelog();
5777c478bd9Sstevel@tonic-gate 	openlog(ident, LOG_PID|LOG_CONS, LOG_DAEMON);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * Perform TCP wrappers checks on this instance. Due to the fact that the
5827c478bd9Sstevel@tonic-gate  * current wrappers code used in Solaris is taken untouched from the open
5837c478bd9Sstevel@tonic-gate  * source version, we're stuck with using the daemon name for the checks, as
5847c478bd9Sstevel@tonic-gate  * opposed to making use of instance FMRIs. Sigh.
5857c478bd9Sstevel@tonic-gate  * Returns B_TRUE if the check passed, else B_FALSE.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static boolean_t
tcp_wrappers_ok(instance_t * instance)5887c478bd9Sstevel@tonic-gate tcp_wrappers_ok(instance_t *instance)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	boolean_t		rval = B_TRUE;
5917c478bd9Sstevel@tonic-gate 	char			*daemon_name;
5927c478bd9Sstevel@tonic-gate 	basic_cfg_t		*cfg = instance->config->basic;
5937c478bd9Sstevel@tonic-gate 	struct request_info	req;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * Wrap the service using libwrap functions. The code below implements
5977c478bd9Sstevel@tonic-gate 	 * the functionality of tcpd. This is done only for stream,nowait
5987c478bd9Sstevel@tonic-gate 	 * services, following the convention of other vendors.  udp/dgram and
5997c478bd9Sstevel@tonic-gate 	 * stream/wait can NOT be wrapped with this libwrap, so be wary of
6007c478bd9Sstevel@tonic-gate 	 * changing the test below.
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 	if (cfg->do_tcp_wrappers && !cfg->iswait && !cfg->istlx) {
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		daemon_name = instance->config->methods[
6057c478bd9Sstevel@tonic-gate 		    IM_START]->exec_args_we.we_wordv[0];
6067c478bd9Sstevel@tonic-gate 		if (*daemon_name == '/')
6077c478bd9Sstevel@tonic-gate 			daemon_name = strrchr(daemon_name, '/') + 1;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		/*
6107c478bd9Sstevel@tonic-gate 		 * Change the syslog message identity to the name of the
6117c478bd9Sstevel@tonic-gate 		 * daemon being wrapped, as opposed to "inetd".
6127c478bd9Sstevel@tonic-gate 		 */
6137c478bd9Sstevel@tonic-gate 		change_syslog_ident(daemon_name);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		(void) request_init(&req, RQ_DAEMON, daemon_name, RQ_FILE,
6167c478bd9Sstevel@tonic-gate 		    instance->conn_fd, NULL);
6177c478bd9Sstevel@tonic-gate 		fromhost(&req);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		if (strcasecmp(eval_hostname(req.client), paranoid) == 0) {
6207c478bd9Sstevel@tonic-gate 			syslog(deny_severity,
6217c478bd9Sstevel@tonic-gate 			    "refused connect from %s (name/address mismatch)",
6227c478bd9Sstevel@tonic-gate 			    eval_client(&req));
6237c478bd9Sstevel@tonic-gate 			if (req.sink != NULL)
6247c478bd9Sstevel@tonic-gate 				req.sink(instance->conn_fd);
6257c478bd9Sstevel@tonic-gate 			rval = B_FALSE;
6267c478bd9Sstevel@tonic-gate 		} else if (!hosts_access(&req)) {
6277c478bd9Sstevel@tonic-gate 			syslog(deny_severity,
6287c478bd9Sstevel@tonic-gate 			    "refused connect from %s (access denied)",
6297c478bd9Sstevel@tonic-gate 			    eval_client(&req));
6307c478bd9Sstevel@tonic-gate 			if (req.sink != NULL)
6317c478bd9Sstevel@tonic-gate 				req.sink(instance->conn_fd);
6327c478bd9Sstevel@tonic-gate 			rval = B_FALSE;
6337c478bd9Sstevel@tonic-gate 		} else {
6347c478bd9Sstevel@tonic-gate 			syslog(allow_severity, "connect from %s",
6357c478bd9Sstevel@tonic-gate 			    eval_client(&req));
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		/* Revert syslog identity back to "inetd". */
6397c478bd9Sstevel@tonic-gate 		change_syslog_ident(SYSLOG_IDENT);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	return (rval);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * Handler registered with the timer queue code to remove an instance from
6467c478bd9Sstevel@tonic-gate  * the connection rate offline state when it has been there for its allotted
6477c478bd9Sstevel@tonic-gate  * time.
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate /* ARGSUSED */
6507c478bd9Sstevel@tonic-gate static void
conn_rate_online(iu_tq_t * tq,void * arg)6517c478bd9Sstevel@tonic-gate conn_rate_online(iu_tq_t *tq, void *arg)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	instance_t *instance = arg;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	assert(instance->cur_istate == IIS_OFFLINE_CONRATE);
6567c478bd9Sstevel@tonic-gate 	instance->timer_id = -1;
6577c478bd9Sstevel@tonic-gate 	update_state(instance, IIS_OFFLINE, RERR_RESTART);
6587c478bd9Sstevel@tonic-gate 	process_offline_inst(instance);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * Check whether this instance in the offline state is in transition to
6637c478bd9Sstevel@tonic-gate  * another state and do the work to continue this transition.
6647c478bd9Sstevel@tonic-gate  */
6657c478bd9Sstevel@tonic-gate void
process_offline_inst(instance_t * inst)6667c478bd9Sstevel@tonic-gate process_offline_inst(instance_t *inst)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	if (inst->disable_req) {
6697c478bd9Sstevel@tonic-gate 		inst->disable_req = B_FALSE;
6707c478bd9Sstevel@tonic-gate 		(void) run_method(inst, IM_DISABLE, NULL);
6717c478bd9Sstevel@tonic-gate 	} else if (inst->maintenance_req) {
6727c478bd9Sstevel@tonic-gate 		inst->maintenance_req = B_FALSE;
6737c478bd9Sstevel@tonic-gate 		update_state(inst, IIS_MAINTENANCE, RERR_RESTART);
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * If inetd is in the process of stopping, we don't want to enter
6767c478bd9Sstevel@tonic-gate 	 * any states but offline, disabled and maintenance.
6777c478bd9Sstevel@tonic-gate 	 */
6787c478bd9Sstevel@tonic-gate 	} else if (!inetd_stopping) {
6797c478bd9Sstevel@tonic-gate 		if (inst->conn_rate_exceeded) {
6807c478bd9Sstevel@tonic-gate 			basic_cfg_t *cfg = inst->config->basic;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			inst->conn_rate_exceeded = B_FALSE;
6837c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE_CONRATE, RERR_RESTART);
6847c478bd9Sstevel@tonic-gate 			/*
6857c478bd9Sstevel@tonic-gate 			 * Schedule a timer to bring the instance out of the
6867c478bd9Sstevel@tonic-gate 			 * connection rate offline state.
6877c478bd9Sstevel@tonic-gate 			 */
6887c478bd9Sstevel@tonic-gate 			inst->timer_id = iu_schedule_timer(timer_queue,
6897c478bd9Sstevel@tonic-gate 			    cfg->conn_rate_offline, conn_rate_online,
6907c478bd9Sstevel@tonic-gate 			    inst);
6917c478bd9Sstevel@tonic-gate 			if (inst->timer_id == -1) {
6927c478bd9Sstevel@tonic-gate 				error_msg(gettext("%s unable to set timer, "
6937c478bd9Sstevel@tonic-gate 				    "won't be brought on line after %d "
6947c478bd9Sstevel@tonic-gate 				    "seconds."), inst->fmri,
6957c478bd9Sstevel@tonic-gate 				    cfg->conn_rate_offline);
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		} else if (copies_limit_exceeded(inst)) {
6997c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE_COPIES, RERR_RESTART);
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * Create a socket bound to the instance's configured address. If the
7067c478bd9Sstevel@tonic-gate  * bind fails, returns -1, else the fd of the bound socket.
7077c478bd9Sstevel@tonic-gate  */
7087c478bd9Sstevel@tonic-gate static int
create_bound_socket(const instance_t * inst,socket_info_t * sock_info)709fff9db26Svp create_bound_socket(const instance_t *inst, socket_info_t *sock_info)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	int		fd;
7127c478bd9Sstevel@tonic-gate 	int		on = 1;
713fff9db26Svp 	const char	*fmri = inst->fmri;
7147c478bd9Sstevel@tonic-gate 	rpc_info_t	*rpc = sock_info->pr_info.ri;
7157c478bd9Sstevel@tonic-gate 	const char	*proto = sock_info->pr_info.proto;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	fd = socket(sock_info->local_addr.ss_family, sock_info->type,
7187c478bd9Sstevel@tonic-gate 	    sock_info->protocol);
7197c478bd9Sstevel@tonic-gate 	if (fd < 0) {
7207c478bd9Sstevel@tonic-gate 		error_msg(gettext(
7217c478bd9Sstevel@tonic-gate 		    "Socket creation failure for instance %s, proto %s: %s"),
7227c478bd9Sstevel@tonic-gate 		    fmri, proto, strerror(errno));
7237c478bd9Sstevel@tonic-gate 		return (-1);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) == -1) {
7277c478bd9Sstevel@tonic-gate 		error_msg(gettext("setsockopt SO_REUSEADDR failed for service "
7287c478bd9Sstevel@tonic-gate 		    "instance %s, proto %s: %s"), fmri, proto, strerror(errno));
7297c478bd9Sstevel@tonic-gate 		(void) close(fd);
7307c478bd9Sstevel@tonic-gate 		return (-1);
7317c478bd9Sstevel@tonic-gate 	}
732e23de8e2SGary Mills 	if (inst->config->basic->do_tcp_keepalive &&
733e23de8e2SGary Mills 	    !inst->config->basic->iswait && !inst->config->basic->istlx) {
734e23de8e2SGary Mills 		/* set the keepalive option */
735e23de8e2SGary Mills 		if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on,
736e23de8e2SGary Mills 		    sizeof (on)) == -1) {
737e23de8e2SGary Mills 			error_msg(gettext("setsockopt SO_KEEPALIVE failed for "
738e23de8e2SGary Mills 			    "service instance %s, proto %s: %s"), fmri,
739e23de8e2SGary Mills 			    proto, strerror(errno));
740e23de8e2SGary Mills 			(void) close(fd);
741e23de8e2SGary Mills 			return (-1);
742e23de8e2SGary Mills 		}
743e23de8e2SGary Mills 	}
7447c478bd9Sstevel@tonic-gate 	if (sock_info->pr_info.v6only) {
7457c478bd9Sstevel@tonic-gate 		/* restrict socket to IPv6 communications only */
7467c478bd9Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
7477c478bd9Sstevel@tonic-gate 		    sizeof (on)) == -1) {
7487c478bd9Sstevel@tonic-gate 			error_msg(gettext("setsockopt IPV6_V6ONLY failed for "
7497c478bd9Sstevel@tonic-gate 			    "service instance %s, proto %s: %s"), fmri, proto,
7507c478bd9Sstevel@tonic-gate 			    strerror(errno));
7517c478bd9Sstevel@tonic-gate 			(void) close(fd);
7527c478bd9Sstevel@tonic-gate 			return (-1);
7537c478bd9Sstevel@tonic-gate 		}
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (rpc != NULL)
7577c478bd9Sstevel@tonic-gate 		SS_SETPORT(sock_info->local_addr, 0);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (bind(fd, (struct sockaddr *)&(sock_info->local_addr),
7607c478bd9Sstevel@tonic-gate 	    SS_ADDRLEN(sock_info->local_addr)) < 0) {
7617c478bd9Sstevel@tonic-gate 		error_msg(gettext(
7627c478bd9Sstevel@tonic-gate 		    "Failed to bind to the port of service instance %s, "
7637c478bd9Sstevel@tonic-gate 		    "proto %s: %s"), fmri, proto, strerror(errno));
7647c478bd9Sstevel@tonic-gate 		(void) close(fd);
7657c478bd9Sstevel@tonic-gate 		return (-1);
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * Retrieve and store the address bound to for RPC services.
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 	if (rpc != NULL) {
7727c478bd9Sstevel@tonic-gate 		struct sockaddr_storage	ss;
7737c478bd9Sstevel@tonic-gate 		int			ss_size = sizeof (ss);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		if (getsockname(fd, (struct sockaddr *)&ss, &ss_size) < 0) {
7767c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed getsockname for instance %s, "
7777c478bd9Sstevel@tonic-gate 			    "proto %s: %s"), fmri, proto, strerror(errno));
7787c478bd9Sstevel@tonic-gate 			(void) close(fd);
7797c478bd9Sstevel@tonic-gate 			return (-1);
7807c478bd9Sstevel@tonic-gate 		}
7817c478bd9Sstevel@tonic-gate 		(void) memcpy(rpc->netbuf.buf, &ss,
7827c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_storage));
7837c478bd9Sstevel@tonic-gate 		rpc->netbuf.len = SS_ADDRLEN(ss);
7847c478bd9Sstevel@tonic-gate 		rpc->netbuf.maxlen = SS_ADDRLEN(ss);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
787fff9db26Svp 	if (sock_info->type == SOCK_STREAM) {
788fff9db26Svp 		int qlen = inst->config->basic->conn_backlog;
789fff9db26Svp 
790fff9db26Svp 		debug_msg("Listening for service %s with backlog queue"
791fff9db26Svp 		    " size %d", fmri, qlen);
792fff9db26Svp 		(void) listen(fd, qlen);
793fff9db26Svp 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	return (fd);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate /*
7997c478bd9Sstevel@tonic-gate  * Handler registered with the timer queue code to retry the creation
8007c478bd9Sstevel@tonic-gate  * of a bound fd.
8017c478bd9Sstevel@tonic-gate  */
8027c478bd9Sstevel@tonic-gate /* ARGSUSED */
8037c478bd9Sstevel@tonic-gate static void
retry_bind(iu_tq_t * tq,void * arg)8047c478bd9Sstevel@tonic-gate retry_bind(iu_tq_t *tq, void *arg)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	instance_t *instance = arg;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
8097c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
8107c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
8117c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
8127c478bd9Sstevel@tonic-gate 	case IIS_IN_ONLINE_METHOD:
8137c478bd9Sstevel@tonic-gate 	case IIS_IN_REFRESH_METHOD:
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 	default:
8167c478bd9Sstevel@tonic-gate #ifndef NDEBUG
8177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n",
8187c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, instance->cur_istate);
8197c478bd9Sstevel@tonic-gate #endif
8207c478bd9Sstevel@tonic-gate 		abort();
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	instance->bind_timer_id = -1;
8247c478bd9Sstevel@tonic-gate 	create_bound_fds(instance);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * For each of the fds for the given instance that are bound, if 'listen' is
8295e2844d4SRenaud Manus  * set add them to the poll set, else remove them from it. If proto_name is
8305e2844d4SRenaud Manus  * not NULL then apply the change only to this specific protocol endpoint.
8315e2844d4SRenaud Manus  * If any additions fail, returns -1, else 0 on success.
8327c478bd9Sstevel@tonic-gate  */
8337c478bd9Sstevel@tonic-gate int
poll_bound_fds(instance_t * instance,boolean_t listen,char * proto_name)8345e2844d4SRenaud Manus poll_bound_fds(instance_t *instance, boolean_t listen, char *proto_name)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
8377c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
8387c478bd9Sstevel@tonic-gate 	int		ret = 0;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
8417c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
8427c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1) {	/* fd bound */
8435e2844d4SRenaud Manus 			if (proto_name == NULL ||
8445e2844d4SRenaud Manus 			    strcmp(pi->proto, proto_name) == 0) {
8455e2844d4SRenaud Manus 				if (listen == B_FALSE) {
8465e2844d4SRenaud Manus 					clear_pollfd(pi->listen_fd);
8475e2844d4SRenaud Manus 				} else if (set_pollfd(pi->listen_fd,
8485e2844d4SRenaud Manus 				    POLLIN) == -1) {
8495e2844d4SRenaud Manus 					ret = -1;
8505e2844d4SRenaud Manus 				}
8517c478bd9Sstevel@tonic-gate 			}
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	return (ret);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * Handle the case were we either fail to create a bound fd or we fail
8607c478bd9Sstevel@tonic-gate  * to add a bound fd to the poll set for the given instance.
8617c478bd9Sstevel@tonic-gate  */
8627c478bd9Sstevel@tonic-gate static void
handle_bind_failure(instance_t * instance)8637c478bd9Sstevel@tonic-gate handle_bind_failure(instance_t *instance)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	basic_cfg_t *cfg = instance->config->basic;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * We must be being called as a result of a failed poll_bound_fds()
8697c478bd9Sstevel@tonic-gate 	 * as a bind retry is already scheduled. Just return and let it do
8707c478bd9Sstevel@tonic-gate 	 * the work.
8717c478bd9Sstevel@tonic-gate 	 */
8727c478bd9Sstevel@tonic-gate 	if (instance->bind_timer_id != -1)
8737c478bd9Sstevel@tonic-gate 		return;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/*
8767c478bd9Sstevel@tonic-gate 	 * Check if the rebind retries limit is operative and if so,
8777c478bd9Sstevel@tonic-gate 	 * if it has been reached.
8787c478bd9Sstevel@tonic-gate 	 */
8797c478bd9Sstevel@tonic-gate 	if (((cfg->bind_fail_interval <= 0) ||		/* no retries */
8807c478bd9Sstevel@tonic-gate 	    ((cfg->bind_fail_max >= 0) &&		/* limit reached */
8817c478bd9Sstevel@tonic-gate 	    (++instance->bind_fail_count > cfg->bind_fail_max))) ||
8827c478bd9Sstevel@tonic-gate 	    ((instance->bind_timer_id = iu_schedule_timer(timer_queue,
8837c478bd9Sstevel@tonic-gate 	    cfg->bind_fail_interval, retry_bind, instance)) == -1)) {
8847c478bd9Sstevel@tonic-gate 		proto_info_t *pi;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		instance->bind_fail_count = 0;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
8897c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
8907c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
8917c478bd9Sstevel@tonic-gate 			/* check if any of the fds are being poll'd upon */
8927c478bd9Sstevel@tonic-gate 			for (pi = uu_list_first(cfg->proto_list); pi != NULL;
8937c478bd9Sstevel@tonic-gate 			    pi = uu_list_next(cfg->proto_list, pi)) {
8947c478bd9Sstevel@tonic-gate 				if ((pi->listen_fd != -1) &&
8957c478bd9Sstevel@tonic-gate 				    (find_pollfd(pi->listen_fd) != NULL))
8967c478bd9Sstevel@tonic-gate 					break;
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 			if (pi != NULL)	{	/* polling on > 0 fds */
8997c478bd9Sstevel@tonic-gate 				warn_msg(gettext("Failed to bind on "
9007c478bd9Sstevel@tonic-gate 				    "all protocols for instance %s, "
9017c478bd9Sstevel@tonic-gate 				    "transitioning to degraded"),
9027c478bd9Sstevel@tonic-gate 				    instance->fmri);
9037c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_DEGRADED, RERR_NONE);
9047c478bd9Sstevel@tonic-gate 				instance->bind_retries_exceeded = B_TRUE;
9057c478bd9Sstevel@tonic-gate 				break;
9067c478bd9Sstevel@tonic-gate 			}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
9097c478bd9Sstevel@tonic-gate 			/*
9107c478bd9Sstevel@tonic-gate 			 * In the case we failed the 'bind' because set_pollfd()
9117c478bd9Sstevel@tonic-gate 			 * failed on all bound fds, use the offline handling.
9127c478bd9Sstevel@tonic-gate 			 */
9137c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
9147c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE:
9157c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_BIND:
9167c478bd9Sstevel@tonic-gate 			error_msg(gettext("Too many bind failures for instance "
9177c478bd9Sstevel@tonic-gate 			"%s, transitioning to maintenance"), instance->fmri);
9187c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE,
9197c478bd9Sstevel@tonic-gate 			    RERR_FAULT);
9207c478bd9Sstevel@tonic-gate 			break;
9217c478bd9Sstevel@tonic-gate 		case IIS_IN_ONLINE_METHOD:
9227c478bd9Sstevel@tonic-gate 		case IIS_IN_REFRESH_METHOD:
9237c478bd9Sstevel@tonic-gate 			warn_msg(gettext("Failed to bind on all "
9247c478bd9Sstevel@tonic-gate 			    "protocols for instance %s, instance will go to "
9257c478bd9Sstevel@tonic-gate 			    "degraded"), instance->fmri);
9267c478bd9Sstevel@tonic-gate 			/*
9277c478bd9Sstevel@tonic-gate 			 * Set the retries exceeded flag so when the method
9287c478bd9Sstevel@tonic-gate 			 * completes the instance goes to the degraded state.
9297c478bd9Sstevel@tonic-gate 			 */
9307c478bd9Sstevel@tonic-gate 			instance->bind_retries_exceeded = B_TRUE;
9317c478bd9Sstevel@tonic-gate 			break;
9327c478bd9Sstevel@tonic-gate 		default:
9337c478bd9Sstevel@tonic-gate #ifndef NDEBUG
9347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9357c478bd9Sstevel@tonic-gate 			    "%s:%d: Unknown instance state %d.\n",
9367c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, instance->cur_istate);
9377c478bd9Sstevel@tonic-gate #endif
9387c478bd9Sstevel@tonic-gate 			abort();
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 	} else if (instance->cur_istate == IIS_OFFLINE) {
9417c478bd9Sstevel@tonic-gate 		/*
9427c478bd9Sstevel@tonic-gate 		 * bind re-scheduled, so if we're offline reflect this in the
9437c478bd9Sstevel@tonic-gate 		 * state.
9447c478bd9Sstevel@tonic-gate 		 */
9457c478bd9Sstevel@tonic-gate 		update_state(instance, IIS_OFFLINE_BIND, RERR_NONE);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
949b823d4c7Sdstaff 
950b823d4c7Sdstaff /*
951513ad6b4Sdstaff  * Check if two transport protocols for RPC conflict.
952b823d4c7Sdstaff  */
953b823d4c7Sdstaff 
954b823d4c7Sdstaff boolean_t
is_rpc_proto_conflict(const char * proto0,const char * proto1)955b823d4c7Sdstaff is_rpc_proto_conflict(const char *proto0, const char *proto1) {
956b823d4c7Sdstaff 	if (strcmp(proto0, "tcp") == 0) {
957b823d4c7Sdstaff 		if (strcmp(proto1, "tcp") == 0)
958b823d4c7Sdstaff 			return (B_TRUE);
959b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6") == 0)
960b823d4c7Sdstaff 			return (B_TRUE);
961b823d4c7Sdstaff 		return (B_FALSE);
962b823d4c7Sdstaff 	}
963b823d4c7Sdstaff 
964b823d4c7Sdstaff 	if (strcmp(proto0, "tcp6") == 0) {
965b823d4c7Sdstaff 		if (strcmp(proto1, "tcp") == 0)
966b823d4c7Sdstaff 			return (B_TRUE);
967b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6only") == 0)
968b823d4c7Sdstaff 			return (B_TRUE);
969b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6") == 0)
970b823d4c7Sdstaff 			return (B_TRUE);
971b823d4c7Sdstaff 		return (B_FALSE);
972b823d4c7Sdstaff 	}
973b823d4c7Sdstaff 
974b823d4c7Sdstaff 	if (strcmp(proto0, "tcp6only") == 0) {
975b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6only") == 0)
976b823d4c7Sdstaff 			return (B_TRUE);
977b823d4c7Sdstaff 		if (strcmp(proto1, "tcp6") == 0)
978b823d4c7Sdstaff 			return (B_TRUE);
979b823d4c7Sdstaff 		return (B_FALSE);
980b823d4c7Sdstaff 	}
981b823d4c7Sdstaff 
982b823d4c7Sdstaff 	if (strcmp(proto0, "udp") == 0) {
983b823d4c7Sdstaff 		if (strcmp(proto1, "udp") == 0)
984b823d4c7Sdstaff 			return (B_TRUE);
985b823d4c7Sdstaff 		if (strcmp(proto1, "udp6") == 0)
986b823d4c7Sdstaff 			return (B_TRUE);
987b823d4c7Sdstaff 		return (B_FALSE);
988b823d4c7Sdstaff 	}
989b823d4c7Sdstaff 
990b823d4c7Sdstaff 	if (strcmp(proto0, "udp6") == 0) {
991b823d4c7Sdstaff 
992b823d4c7Sdstaff 		if (strcmp(proto1, "udp") == 0)
993b823d4c7Sdstaff 			return (B_TRUE);
994b823d4c7Sdstaff 		if (strcmp(proto1, "udp6only") == 0)
995b823d4c7Sdstaff 			return (B_TRUE);
996b823d4c7Sdstaff 		if (strcmp(proto1, "udp6") == 0)
997b823d4c7Sdstaff 			return (B_TRUE);
998b823d4c7Sdstaff 		return (B_FALSE);
999b823d4c7Sdstaff 	}
1000b823d4c7Sdstaff 
1001b823d4c7Sdstaff 	if (strcmp(proto0, "udp6only") == 0) {
1002b823d4c7Sdstaff 
1003b823d4c7Sdstaff 		if (strcmp(proto1, "udp6only") == 0)
1004b823d4c7Sdstaff 			return (B_TRUE);
1005b823d4c7Sdstaff 		if (strcmp(proto1, "udp6") == 0)
1006b823d4c7Sdstaff 			return (B_TRUE);
1007b823d4c7Sdstaff 		return (0);
1008b823d4c7Sdstaff 	}
1009b823d4c7Sdstaff 
1010b823d4c7Sdstaff 	/*
1011b823d4c7Sdstaff 	 * If the protocol isn't TCP/IP or UDP/IP assume that it has its own
1012b823d4c7Sdstaff 	 * port namepsace and that conflicts can be detected by literal string
1013b823d4c7Sdstaff 	 * comparison.
1014b823d4c7Sdstaff 	 */
1015b823d4c7Sdstaff 
1016b823d4c7Sdstaff 	if (strcmp(proto0, proto1))
1017b823d4c7Sdstaff 		return (FALSE);
1018b823d4c7Sdstaff 
1019b823d4c7Sdstaff 	return (B_TRUE);
1020b823d4c7Sdstaff }
1021b823d4c7Sdstaff 
1022b823d4c7Sdstaff 
1023b823d4c7Sdstaff /*
1024b823d4c7Sdstaff  * Check if inetd thinks this RPC program number is already registered.
1025b823d4c7Sdstaff  *
1026b823d4c7Sdstaff  * An RPC protocol conflict occurs if
1027b823d4c7Sdstaff  * 	a) the program numbers are the same and,
1028b823d4c7Sdstaff  * 	b) the version numbers overlap,
1029b823d4c7Sdstaff  * 	c) the protocols (TCP vs UDP vs tic*) are the same.
1030b823d4c7Sdstaff  */
1031b823d4c7Sdstaff 
1032b823d4c7Sdstaff boolean_t
is_rpc_num_in_use(int rpc_n,char * proto,int lowver,int highver)1033b823d4c7Sdstaff is_rpc_num_in_use(int rpc_n, char *proto, int lowver, int highver) {
1034b823d4c7Sdstaff 	instance_t *i;
1035b823d4c7Sdstaff 	basic_cfg_t *cfg;
1036b823d4c7Sdstaff 	proto_info_t *pi;
1037b823d4c7Sdstaff 
1038b823d4c7Sdstaff 	for (i = uu_list_first(instance_list); i != NULL;
1039b823d4c7Sdstaff 	    i = uu_list_next(instance_list, i)) {
1040b823d4c7Sdstaff 
1041b823d4c7Sdstaff 		if (i->cur_istate != IIS_ONLINE)
1042b823d4c7Sdstaff 			continue;
10437778f43aSdstaff 		cfg = i->config->basic;
1044b823d4c7Sdstaff 
1045b823d4c7Sdstaff 		for (pi = uu_list_first(cfg->proto_list); pi != NULL;
1046b823d4c7Sdstaff 		    pi = uu_list_next(cfg->proto_list, pi)) {
1047b823d4c7Sdstaff 
1048b823d4c7Sdstaff 			if (pi->ri == NULL)
1049b823d4c7Sdstaff 				continue;
1050b823d4c7Sdstaff 			if (pi->ri->prognum != rpc_n)
1051b823d4c7Sdstaff 				continue;
1052513ad6b4Sdstaff 			if (!is_rpc_proto_conflict(pi->proto, proto))
1053b823d4c7Sdstaff 				continue;
1054b823d4c7Sdstaff 			if ((lowver < pi->ri->lowver &&
1055b823d4c7Sdstaff 			    highver < pi->ri->lowver) ||
1056b823d4c7Sdstaff 			    (lowver > pi->ri->highver &&
1057b823d4c7Sdstaff 			    highver > pi->ri->highver))
1058b823d4c7Sdstaff 				continue;
1059b823d4c7Sdstaff 			return (B_TRUE);
1060b823d4c7Sdstaff 		}
1061b823d4c7Sdstaff 	}
1062b823d4c7Sdstaff 	return (B_FALSE);
1063b823d4c7Sdstaff }
1064b823d4c7Sdstaff 
1065b823d4c7Sdstaff 
10667c478bd9Sstevel@tonic-gate /*
10677c478bd9Sstevel@tonic-gate  * Independent of the transport, for each of the entries in the instance's
10687c478bd9Sstevel@tonic-gate  * proto list this function first attempts to create an associated network fd;
10697c478bd9Sstevel@tonic-gate  * for RPC services these are then bound to a kernel chosen port and the
10707c478bd9Sstevel@tonic-gate  * fd is registered with rpcbind; for non-RPC services the fds are bound
10717c478bd9Sstevel@tonic-gate  * to the port associated with the instance's service name. On any successful
10727c478bd9Sstevel@tonic-gate  * binds the instance is taken online. Failed binds are handled by
10737c478bd9Sstevel@tonic-gate  * handle_bind_failure().
10747c478bd9Sstevel@tonic-gate  */
10757c478bd9Sstevel@tonic-gate void
create_bound_fds(instance_t * instance)10767c478bd9Sstevel@tonic-gate create_bound_fds(instance_t *instance)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
10797c478bd9Sstevel@tonic-gate 	boolean_t	failure = B_FALSE;
10807c478bd9Sstevel@tonic-gate 	boolean_t	success = B_FALSE;
10817c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/*
10847c478bd9Sstevel@tonic-gate 	 * Loop through and try and bind any unbound protos.
10857c478bd9Sstevel@tonic-gate 	 */
10867c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
10877c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
10887c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1)
10897c478bd9Sstevel@tonic-gate 			continue;
10907c478bd9Sstevel@tonic-gate 		if (cfg->istlx) {
1091fff9db26Svp 			pi->listen_fd = create_bound_endpoint(instance,
10927c478bd9Sstevel@tonic-gate 			    (tlx_info_t *)pi);
10937c478bd9Sstevel@tonic-gate 		} else {
10947c478bd9Sstevel@tonic-gate 			/*
10957c478bd9Sstevel@tonic-gate 			 * We cast pi to a void so we can then go on to cast
10967c478bd9Sstevel@tonic-gate 			 * it to a socket_info_t without lint complaining
10977c478bd9Sstevel@tonic-gate 			 * about alignment. This is done because the x86
10987c478bd9Sstevel@tonic-gate 			 * version of lint thinks a lint suppression directive
10997c478bd9Sstevel@tonic-gate 			 * is unnecessary and flags it as such, yet the sparc
11007c478bd9Sstevel@tonic-gate 			 * version complains if it's absent.
11017c478bd9Sstevel@tonic-gate 			 */
11027c478bd9Sstevel@tonic-gate 			void *p = pi;
1103fff9db26Svp 			pi->listen_fd = create_bound_socket(instance,
11047c478bd9Sstevel@tonic-gate 			    (socket_info_t *)p);
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 		if (pi->listen_fd == -1) {
11077c478bd9S