1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * NOTES: To be expanded.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * The SMF inetd.
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Below are some high level notes of the operation of the SMF inetd. The
35*7c478bd9Sstevel@tonic-gate  * notes don't go into any real detail, and the viewer of this file is
36*7c478bd9Sstevel@tonic-gate  * encouraged to look at the code and its associated comments to better
37*7c478bd9Sstevel@tonic-gate  * understand inetd's operation. This saves the potential for the code
38*7c478bd9Sstevel@tonic-gate  * and these notes diverging over time.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * Inetd's major work is done from the context of event_loop(). Within this
41*7c478bd9Sstevel@tonic-gate  * loop, inetd polls for events arriving from a number of different file
42*7c478bd9Sstevel@tonic-gate  * descriptors, representing the following event types, and initiates
43*7c478bd9Sstevel@tonic-gate  * any necessary event processing:
44*7c478bd9Sstevel@tonic-gate  * - incoming network connections/datagrams.
45*7c478bd9Sstevel@tonic-gate  * - notification of terminated processes (discovered via contract events).
46*7c478bd9Sstevel@tonic-gate  * - instance specific events originating from the SMF master restarter.
47*7c478bd9Sstevel@tonic-gate  * - stop/refresh requests from the inetd method processes (coming in on a
48*7c478bd9Sstevel@tonic-gate  *   Unix Domain socket).
49*7c478bd9Sstevel@tonic-gate  * There's also a timeout set for the poll, which is set to the nearest
50*7c478bd9Sstevel@tonic-gate  * scheduled timer in a timer queue that inetd uses to perform delayed
51*7c478bd9Sstevel@tonic-gate  * processing, such as bind retries.
52*7c478bd9Sstevel@tonic-gate  * The SIGHUP and SIGINT signals can also interrupt the poll, and will
53*7c478bd9Sstevel@tonic-gate  * result in inetd being refreshed or stopped respectively, as was the
54*7c478bd9Sstevel@tonic-gate  * behavior with the old inetd.
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  * Inetd implements a state machine for each instance. The states within the
57*7c478bd9Sstevel@tonic-gate  * machine are: offline, online, disabled, maintenance, uninitialized and
58*7c478bd9Sstevel@tonic-gate  * specializations of the offline state for when an instance exceeds one of
59*7c478bd9Sstevel@tonic-gate  * its DOS limits. The state of an instance can be changed as a
60*7c478bd9Sstevel@tonic-gate  * result/side-effect of one of the above events occurring, or inetd being
61*7c478bd9Sstevel@tonic-gate  * started up. The ongoing state of an instance is stored in the SMF
62*7c478bd9Sstevel@tonic-gate  * repository, as required of SMF restarters. This enables an administrator
63*7c478bd9Sstevel@tonic-gate  * to view the state of each instance, and, if inetd was to terminate
64*7c478bd9Sstevel@tonic-gate  * unexpectedly, it could use the stored state to re-commence where it left off.
65*7c478bd9Sstevel@tonic-gate  *
66*7c478bd9Sstevel@tonic-gate  * Within the state machine a number of methods are run (if provided) as part
67*7c478bd9Sstevel@tonic-gate  * of a state transition to aid/ effect a change in an instance's state. The
68*7c478bd9Sstevel@tonic-gate  * supported methods are: offline, online, disable, refresh and start. The
69*7c478bd9Sstevel@tonic-gate  * latter of these is the equivalent of the server program and its arguments
70*7c478bd9Sstevel@tonic-gate  * in the old inetd.
71*7c478bd9Sstevel@tonic-gate  *
72*7c478bd9Sstevel@tonic-gate  * Events from the SMF master restarter come in on a number of threads
73*7c478bd9Sstevel@tonic-gate  * created in the registration routine of librestart, the delegated restarter
74*7c478bd9Sstevel@tonic-gate  * library. These threads call into the restart_event_proxy() function
75*7c478bd9Sstevel@tonic-gate  * when an event arrives. To serialize the processing of instances, these events
76*7c478bd9Sstevel@tonic-gate  * are then written down a pipe to the process's main thread, which listens
77*7c478bd9Sstevel@tonic-gate  * for these events via a poll call, with the file descriptor of the other
78*7c478bd9Sstevel@tonic-gate  * end of the pipe in its read set, and processes the event appropriately.
79*7c478bd9Sstevel@tonic-gate  * When the event has been  processed (which may be delayed if the instance
80*7c478bd9Sstevel@tonic-gate  * for which the event is for is in the process of executing one of its methods
81*7c478bd9Sstevel@tonic-gate  * as part of a state transition) it writes an acknowledgement back down the
82*7c478bd9Sstevel@tonic-gate  * pipe the event was received on. The thread in restart_event_proxy() that
83*7c478bd9Sstevel@tonic-gate  * wrote the event will read the acknowledgement it was blocked upon, and will
84*7c478bd9Sstevel@tonic-gate  * then be able to return to its caller, thus implicitly acknowledging the
85*7c478bd9Sstevel@tonic-gate  * event, and allowing another event to be written down the pipe for the main
86*7c478bd9Sstevel@tonic-gate  * thread to process.
87*7c478bd9Sstevel@tonic-gate  */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate #include <netdb.h>
91*7c478bd9Sstevel@tonic-gate #include <stdio.h>
92*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
93*7c478bd9Sstevel@tonic-gate #include <strings.h>
94*7c478bd9Sstevel@tonic-gate #include <unistd.h>
95*7c478bd9Sstevel@tonic-gate #include <assert.h>
96*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
97*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
98*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
99*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
100*7c478bd9Sstevel@tonic-gate #include <signal.h>
101*7c478bd9Sstevel@tonic-gate #include <errno.h>
102*7c478bd9Sstevel@tonic-gate #include <locale.h>
103*7c478bd9Sstevel@tonic-gate #include <syslog.h>
104*7c478bd9Sstevel@tonic-gate #include <libintl.h>
105*7c478bd9Sstevel@tonic-gate #include <librestart.h>
106*7c478bd9Sstevel@tonic-gate #include <pthread.h>
107*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
108*7c478bd9Sstevel@tonic-gate #include <time.h>
109*7c478bd9Sstevel@tonic-gate #include <limits.h>
110*7c478bd9Sstevel@tonic-gate #include <libgen.h>
111*7c478bd9Sstevel@tonic-gate #include <tcpd.h>
112*7c478bd9Sstevel@tonic-gate #include <libscf.h>
113*7c478bd9Sstevel@tonic-gate #include <libuutil.h>
114*7c478bd9Sstevel@tonic-gate #include <stddef.h>
115*7c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
116*7c478bd9Sstevel@tonic-gate #include "inetd_impl.h"
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /* path to inetd's binary */
119*7c478bd9Sstevel@tonic-gate #define	INETD_PATH	"/usr/lib/inet/inetd"
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * inetd's default configuration file paths. /etc/inetd/inetd.conf is set
123*7c478bd9Sstevel@tonic-gate  * be be the primary file, so it is checked before /etc/inetd.conf.
124*7c478bd9Sstevel@tonic-gate  */
125*7c478bd9Sstevel@tonic-gate #define	PRIMARY_DEFAULT_CONF_FILE	"/etc/inet/inetd.conf"
126*7c478bd9Sstevel@tonic-gate #define	SECONDARY_DEFAULT_CONF_FILE	"/etc/inetd.conf"
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate /* Arguments passed to this binary to request which method to execute. */
129*7c478bd9Sstevel@tonic-gate #define	START_METHOD_ARG	"start"
130*7c478bd9Sstevel@tonic-gate #define	STOP_METHOD_ARG		"stop"
131*7c478bd9Sstevel@tonic-gate #define	REFRESH_METHOD_ARG	"refresh"
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /* connection backlog for unix domain socket */
134*7c478bd9Sstevel@tonic-gate #define	UDS_BACKLOG	2
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate /* number of retries to recv() a request on the UDS socket before giving up */
137*7c478bd9Sstevel@tonic-gate #define	UDS_RECV_RETRIES	10
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /* enumeration of the different ends of a pipe */
140*7c478bd9Sstevel@tonic-gate enum pipe_end {
141*7c478bd9Sstevel@tonic-gate 	PE_CONSUMER,
142*7c478bd9Sstevel@tonic-gate 	PE_PRODUCER
143*7c478bd9Sstevel@tonic-gate };
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate typedef struct {
146*7c478bd9Sstevel@tonic-gate 	internal_inst_state_t		istate;
147*7c478bd9Sstevel@tonic-gate 	const char			*name;
148*7c478bd9Sstevel@tonic-gate 	restarter_instance_state_t	smf_state;
149*7c478bd9Sstevel@tonic-gate 	instance_method_t		method_running;
150*7c478bd9Sstevel@tonic-gate } state_info_t;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate  * Collection of information for each state.
155*7c478bd9Sstevel@tonic-gate  * NOTE:  This table is indexed into using the internal_inst_state_t
156*7c478bd9Sstevel@tonic-gate  * enumeration, so the ordering needs to be kept in synch.
157*7c478bd9Sstevel@tonic-gate  */
158*7c478bd9Sstevel@tonic-gate static state_info_t states[] = {
159*7c478bd9Sstevel@tonic-gate 	{IIS_UNINITIALIZED, "uninitialized", RESTARTER_STATE_UNINIT,
160*7c478bd9Sstevel@tonic-gate 	    IM_NONE},
161*7c478bd9Sstevel@tonic-gate 	{IIS_ONLINE, "online", RESTARTER_STATE_ONLINE, IM_START},
162*7c478bd9Sstevel@tonic-gate 	{IIS_IN_ONLINE_METHOD, "online_method", RESTARTER_STATE_OFFLINE,
163*7c478bd9Sstevel@tonic-gate 	    IM_ONLINE},
164*7c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE, "offline", RESTARTER_STATE_OFFLINE, IM_NONE},
165*7c478bd9Sstevel@tonic-gate 	{IIS_IN_OFFLINE_METHOD, "offline_method", RESTARTER_STATE_OFFLINE,
166*7c478bd9Sstevel@tonic-gate 	    IM_OFFLINE},
167*7c478bd9Sstevel@tonic-gate 	{IIS_DISABLED, "disabled", RESTARTER_STATE_DISABLED, IM_NONE},
168*7c478bd9Sstevel@tonic-gate 	{IIS_IN_DISABLE_METHOD, "disabled_method", RESTARTER_STATE_OFFLINE,
169*7c478bd9Sstevel@tonic-gate 	    IM_DISABLE},
170*7c478bd9Sstevel@tonic-gate 	{IIS_IN_REFRESH_METHOD, "refresh_method", RESTARTER_STATE_ONLINE,
171*7c478bd9Sstevel@tonic-gate 	    IM_REFRESH},
172*7c478bd9Sstevel@tonic-gate 	{IIS_MAINTENANCE, "maintenance", RESTARTER_STATE_MAINT, IM_NONE},
173*7c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_CONRATE, "cr_offline", RESTARTER_STATE_OFFLINE, IM_NONE},
174*7c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_BIND, "bind_offline", RESTARTER_STATE_OFFLINE, IM_NONE},
175*7c478bd9Sstevel@tonic-gate 	{IIS_OFFLINE_COPIES, "copies_offline", RESTARTER_STATE_OFFLINE,
176*7c478bd9Sstevel@tonic-gate 	    IM_NONE},
177*7c478bd9Sstevel@tonic-gate 	{IIS_DEGRADED, "degraded", RESTARTER_STATE_DEGRADED, IM_NONE},
178*7c478bd9Sstevel@tonic-gate 	{IIS_NONE, "none", RESTARTER_STATE_NONE, IM_NONE}
179*7c478bd9Sstevel@tonic-gate };
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate  * Pipe used to send events from the threads created by restarter_bind_handle()
183*7c478bd9Sstevel@tonic-gate  * to the main thread of control.
184*7c478bd9Sstevel@tonic-gate  */
185*7c478bd9Sstevel@tonic-gate static int			rst_event_pipe[] = {-1, -1};
186*7c478bd9Sstevel@tonic-gate /*
187*7c478bd9Sstevel@tonic-gate  * Used to protect the critical section of code in restarter_event_proxy() that
188*7c478bd9Sstevel@tonic-gate  * involves writing an event down the event pipe and reading an acknowledgement.
189*7c478bd9Sstevel@tonic-gate  */
190*7c478bd9Sstevel@tonic-gate static pthread_mutex_t		rst_event_pipe_mtx = PTHREAD_MUTEX_INITIALIZER;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate /* handle used in communication with the master restarter */
193*7c478bd9Sstevel@tonic-gate static restarter_event_handle_t *rst_event_handle = NULL;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate /* set to indicate a refresh of inetd is requested */
196*7c478bd9Sstevel@tonic-gate static boolean_t		refresh_inetd_requested = B_FALSE;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate /* set by the SIGTERM handler to flag we got a SIGTERM */
199*7c478bd9Sstevel@tonic-gate static boolean_t		got_sigterm = B_FALSE;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate /*
202*7c478bd9Sstevel@tonic-gate  * Timer queue used to store timers for delayed event processing, such as
203*7c478bd9Sstevel@tonic-gate  * bind retries.
204*7c478bd9Sstevel@tonic-gate  */
205*7c478bd9Sstevel@tonic-gate iu_tq_t				*timer_queue = NULL;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate /*
208*7c478bd9Sstevel@tonic-gate  * fd of Unix Domain socket used to communicate stop and refresh requests
209*7c478bd9Sstevel@tonic-gate  * to the inetd start method process.
210*7c478bd9Sstevel@tonic-gate  */
211*7c478bd9Sstevel@tonic-gate static int			uds_fd = -1;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate /*
214*7c478bd9Sstevel@tonic-gate  * List of inetd's currently managed instances; each containing its state,
215*7c478bd9Sstevel@tonic-gate  * and in certain states its configuration.
216*7c478bd9Sstevel@tonic-gate  */
217*7c478bd9Sstevel@tonic-gate static uu_list_pool_t		*instance_pool = NULL;
218*7c478bd9Sstevel@tonic-gate uu_list_t			*instance_list = NULL;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate /* set to indicate we're being stopped */
221*7c478bd9Sstevel@tonic-gate boolean_t			inetd_stopping = B_FALSE;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate /* TCP wrappers syslog globals. Consumed by libwrap. */
224*7c478bd9Sstevel@tonic-gate int				allow_severity = LOG_INFO;
225*7c478bd9Sstevel@tonic-gate int				deny_severity = LOG_WARNING;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate /* path of the configuration file being monitored by check_conf_file() */
228*7c478bd9Sstevel@tonic-gate static char			*conf_file = NULL;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate /* Auditing session handle */
231*7c478bd9Sstevel@tonic-gate static adt_session_data_t	*audit_handle;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate static void uds_fini(void);
234*7c478bd9Sstevel@tonic-gate static int uds_init(void);
235*7c478bd9Sstevel@tonic-gate static int run_method(instance_t *, instance_method_t, const proto_info_t *);
236*7c478bd9Sstevel@tonic-gate static void create_bound_fds(instance_t *);
237*7c478bd9Sstevel@tonic-gate static void destroy_bound_fds(instance_t *);
238*7c478bd9Sstevel@tonic-gate static void destroy_instance(instance_t *);
239*7c478bd9Sstevel@tonic-gate static void inetd_stop(void);
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * The following two functions are callbacks that libumem uses to determine
243*7c478bd9Sstevel@tonic-gate  * inetd's desired debugging/logging levels. The interface they consume is
244*7c478bd9Sstevel@tonic-gate  * exported by FMA and is consolidation private. The comments in the two
245*7c478bd9Sstevel@tonic-gate  * functions give the environment variable that will effectively be set to
246*7c478bd9Sstevel@tonic-gate  * their returned value, and thus whose behavior for this value, described in
247*7c478bd9Sstevel@tonic-gate  * umem_debug(3MALLOC), will be followed.
248*7c478bd9Sstevel@tonic-gate  */
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate const char *
251*7c478bd9Sstevel@tonic-gate _umem_debug_init(void)
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate 	return ("default,verbose");	/* UMEM_DEBUG setting */
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate const char *
257*7c478bd9Sstevel@tonic-gate _umem_logging_init(void)
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate 	return ("fail,contents");	/* UMEM_LOGGING setting */
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate static void
263*7c478bd9Sstevel@tonic-gate log_invalid_cfg(const char *fmri)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	error_msg(gettext(
266*7c478bd9Sstevel@tonic-gate 	    "Invalid configuration for instance %s, placing in maintenance"),
267*7c478bd9Sstevel@tonic-gate 	    fmri);
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate /*
271*7c478bd9Sstevel@tonic-gate  * Returns B_TRUE if the instance is in a suitable state for inetd to stop.
272*7c478bd9Sstevel@tonic-gate  */
273*7c478bd9Sstevel@tonic-gate static boolean_t
274*7c478bd9Sstevel@tonic-gate instance_stopped(const instance_t *inst)
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate 	return ((inst->cur_istate == IIS_OFFLINE) ||
277*7c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_MAINTENANCE) ||
278*7c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_DISABLED) ||
279*7c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_UNINITIALIZED));
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate /*
283*7c478bd9Sstevel@tonic-gate  * Updates the current and next repository states of instance 'inst'. If
284*7c478bd9Sstevel@tonic-gate  * any errors occur an error message is output.
285*7c478bd9Sstevel@tonic-gate  */
286*7c478bd9Sstevel@tonic-gate static void
287*7c478bd9Sstevel@tonic-gate update_instance_states(instance_t *inst, internal_inst_state_t new_cur_state,
288*7c478bd9Sstevel@tonic-gate     internal_inst_state_t new_next_state, restarter_error_t err)
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate 	internal_inst_state_t	old_cur = inst->cur_istate;
291*7c478bd9Sstevel@tonic-gate 	internal_inst_state_t	old_next = inst->next_istate;
292*7c478bd9Sstevel@tonic-gate 	scf_error_t		sret;
293*7c478bd9Sstevel@tonic-gate 	int			ret;
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering update_instance_states: oldcur: %s, newcur: %s "
296*7c478bd9Sstevel@tonic-gate 	    "oldnext: %s, newnext: %s", states[old_cur].name,
297*7c478bd9Sstevel@tonic-gate 	    states[new_cur_state].name, states[old_next].name,
298*7c478bd9Sstevel@tonic-gate 	    states[new_next_state].name);
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	/* update the repository/cached internal state */
302*7c478bd9Sstevel@tonic-gate 	inst->cur_istate = new_cur_state;
303*7c478bd9Sstevel@tonic-gate 	inst->next_istate = new_next_state;
304*7c478bd9Sstevel@tonic-gate 	(void) set_single_rep_val(inst->cur_istate_rep,
305*7c478bd9Sstevel@tonic-gate 	    (int64_t)new_cur_state);
306*7c478bd9Sstevel@tonic-gate 	(void) set_single_rep_val(inst->next_istate_rep,
307*7c478bd9Sstevel@tonic-gate 	    (int64_t)new_next_state);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	if (((sret = store_rep_vals(inst->cur_istate_rep, inst->fmri,
310*7c478bd9Sstevel@tonic-gate 	    PR_NAME_CUR_INT_STATE)) != 0) ||
311*7c478bd9Sstevel@tonic-gate 	    ((sret = store_rep_vals(inst->next_istate_rep, inst->fmri,
312*7c478bd9Sstevel@tonic-gate 	    PR_NAME_NEXT_INT_STATE)) != 0))
313*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to update state of instance %s in "
314*7c478bd9Sstevel@tonic-gate 		    "repository: %s"), inst->fmri, scf_strerror(sret));
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	/* update the repository SMF state */
317*7c478bd9Sstevel@tonic-gate 	if ((ret = restarter_set_states(rst_event_handle, inst->fmri,
318*7c478bd9Sstevel@tonic-gate 	    states[old_cur].smf_state, states[new_cur_state].smf_state,
319*7c478bd9Sstevel@tonic-gate 	    states[old_next].smf_state, states[new_next_state].smf_state,
320*7c478bd9Sstevel@tonic-gate 	    err, 0)) != 0)
321*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to update state of instance %s in "
322*7c478bd9Sstevel@tonic-gate 		    "repository: %s"), inst->fmri, strerror(ret));
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate void
327*7c478bd9Sstevel@tonic-gate update_state(instance_t *inst, internal_inst_state_t new_cur,
328*7c478bd9Sstevel@tonic-gate     restarter_error_t err)
329*7c478bd9Sstevel@tonic-gate {
330*7c478bd9Sstevel@tonic-gate 	update_instance_states(inst, new_cur, IIS_NONE, err);
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate /*
334*7c478bd9Sstevel@tonic-gate  * Sends a refresh event to the inetd start method process and returns
335*7c478bd9Sstevel@tonic-gate  * SMF_EXIT_OK if it managed to send it. If it fails to send the request for
336*7c478bd9Sstevel@tonic-gate  * some reason it returns SMF_EXIT_ERR_OTHER.
337*7c478bd9Sstevel@tonic-gate  */
338*7c478bd9Sstevel@tonic-gate static int
339*7c478bd9Sstevel@tonic-gate refresh_method(void)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate 	uds_request_t   req = UR_REFRESH_INETD;
342*7c478bd9Sstevel@tonic-gate 	int		fd;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering refresh_method");
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	if ((fd = connect_to_inetd()) < 0) {
347*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to connect to inetd: %s"),
348*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
349*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	/* write the request and return success */
353*7c478bd9Sstevel@tonic-gate 	if (safe_write(fd, &req, sizeof (req)) == -1) {
354*7c478bd9Sstevel@tonic-gate 		error_msg(
355*7c478bd9Sstevel@tonic-gate 		    gettext("Failed to send refresh request to inetd: %s"),
356*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
357*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
358*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
359*7c478bd9Sstevel@tonic-gate 	}
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	return (SMF_EXIT_OK);
364*7c478bd9Sstevel@tonic-gate }
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate /*
367*7c478bd9Sstevel@tonic-gate  * Sends a stop event to the inetd start method process and wait till it goes
368*7c478bd9Sstevel@tonic-gate  * away. If inetd is determined to have stopped SMF_EXIT_OK is returned, else
369*7c478bd9Sstevel@tonic-gate  * SMF_EXIT_ERR_OTHER is returned.
370*7c478bd9Sstevel@tonic-gate  */
371*7c478bd9Sstevel@tonic-gate static int
372*7c478bd9Sstevel@tonic-gate stop_method(void)
373*7c478bd9Sstevel@tonic-gate {
374*7c478bd9Sstevel@tonic-gate 	uds_request_t   req = UR_STOP_INETD;
375*7c478bd9Sstevel@tonic-gate 	int		fd;
376*7c478bd9Sstevel@tonic-gate 	char		c;
377*7c478bd9Sstevel@tonic-gate 	ssize_t		ret;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering stop_method");
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if ((fd = connect_to_inetd()) == -1) {
382*7c478bd9Sstevel@tonic-gate 		debug_msg(gettext("Failed to connect to inetd: %s"),
383*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
384*7c478bd9Sstevel@tonic-gate 		/*
385*7c478bd9Sstevel@tonic-gate 		 * Assume connect_to_inetd() failed because inetd was already
386*7c478bd9Sstevel@tonic-gate 		 * stopped, and return success.
387*7c478bd9Sstevel@tonic-gate 		 */
388*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_OK);
389*7c478bd9Sstevel@tonic-gate 	}
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	/*
392*7c478bd9Sstevel@tonic-gate 	 * This is safe to do since we're fired off in a separate process
393*7c478bd9Sstevel@tonic-gate 	 * than inetd and in the case we get wedged, the stop method timeout
394*7c478bd9Sstevel@tonic-gate 	 * will occur and we'd be killed by our restarter.
395*7c478bd9Sstevel@tonic-gate 	 */
396*7c478bd9Sstevel@tonic-gate 	enable_blocking(fd);
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	/* write the stop request to inetd and wait till it goes away */
399*7c478bd9Sstevel@tonic-gate 	if (safe_write(fd, &req, sizeof (req)) != 0) {
400*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to send stop request to inetd"));
401*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
402*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	/* wait until remote end of socket is closed */
406*7c478bd9Sstevel@tonic-gate 	while (((ret = recv(fd, &c, sizeof (c), 0)) != 0) && (errno == EINTR))
407*7c478bd9Sstevel@tonic-gate 		;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	if (ret != 0) {
412*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to determine whether inetd stopped"));
413*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
414*7c478bd9Sstevel@tonic-gate 	}
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	return (SMF_EXIT_OK);
417*7c478bd9Sstevel@tonic-gate }
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate /*
421*7c478bd9Sstevel@tonic-gate  * This function is called to handle restarter events coming in from the
422*7c478bd9Sstevel@tonic-gate  * master restarter. It is registered with the master restarter via
423*7c478bd9Sstevel@tonic-gate  * restarter_bind_handle() and simply passes a pointer to the event down
424*7c478bd9Sstevel@tonic-gate  * the event pipe, which will be discovered by the poll in the event loop
425*7c478bd9Sstevel@tonic-gate  * and processed there. It waits for an acknowledgement to be written back down
426*7c478bd9Sstevel@tonic-gate  * the pipe before returning.
427*7c478bd9Sstevel@tonic-gate  * Writing a pointer to the function's 'event' parameter down the pipe will
428*7c478bd9Sstevel@tonic-gate  * be safe, as the thread in restarter_event_proxy() doesn't return until
429*7c478bd9Sstevel@tonic-gate  * the main thread has finished its processing of the passed event, thus
430*7c478bd9Sstevel@tonic-gate  * the referenced event will remain around until the function returns.
431*7c478bd9Sstevel@tonic-gate  * To impose the limit of only one event being in the pipe and processed
432*7c478bd9Sstevel@tonic-gate  * at once, a lock is taken on entry to this function and returned on exit.
433*7c478bd9Sstevel@tonic-gate  * Always returns 0.
434*7c478bd9Sstevel@tonic-gate  */
435*7c478bd9Sstevel@tonic-gate static int
436*7c478bd9Sstevel@tonic-gate restarter_event_proxy(restarter_event_t *event)
437*7c478bd9Sstevel@tonic-gate {
438*7c478bd9Sstevel@tonic-gate 	restarter_event_type_t  ev_type;
439*7c478bd9Sstevel@tonic-gate 	boolean_t		processed;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering restarter_event_proxy");
442*7c478bd9Sstevel@tonic-gate 	ev_type = restarter_event_get_type(event);
443*7c478bd9Sstevel@tonic-gate 	debug_msg("event: %x, event type: %d", event, ev_type);
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&rst_event_pipe_mtx);
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	/* write the event to the main worker thread down the pipe */
448*7c478bd9Sstevel@tonic-gate 	if (safe_write(rst_event_pipe[PE_PRODUCER], &event,
449*7c478bd9Sstevel@tonic-gate 	    sizeof (event)) != 0)
450*7c478bd9Sstevel@tonic-gate 		goto pipe_error;
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	/*
453*7c478bd9Sstevel@tonic-gate 	 * Wait for an acknowledgement that the event has been processed from
454*7c478bd9Sstevel@tonic-gate 	 * the same pipe. In the case that inetd is stopping, any thread in
455*7c478bd9Sstevel@tonic-gate 	 * this function will simply block on this read until inetd eventually
456*7c478bd9Sstevel@tonic-gate 	 * exits. This will result in this function not returning success to
457*7c478bd9Sstevel@tonic-gate 	 * its caller, and the event that was being processed when the
458*7c478bd9Sstevel@tonic-gate 	 * function exited will be re-sent when inetd is next started.
459*7c478bd9Sstevel@tonic-gate 	 */
460*7c478bd9Sstevel@tonic-gate 	if (safe_read(rst_event_pipe[PE_PRODUCER], &processed,
461*7c478bd9Sstevel@tonic-gate 	    sizeof (processed)) != 0)
462*7c478bd9Sstevel@tonic-gate 		goto pipe_error;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&rst_event_pipe_mtx);
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	return (processed ? 0 : EAGAIN);
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate pipe_error:
469*7c478bd9Sstevel@tonic-gate 	/*
470*7c478bd9Sstevel@tonic-gate 	 * Something's seriously wrong with the event pipe. Notify the
471*7c478bd9Sstevel@tonic-gate 	 * worker thread by closing this end of the event pipe and pause till
472*7c478bd9Sstevel@tonic-gate 	 * inetd exits.
473*7c478bd9Sstevel@tonic-gate 	 */
474*7c478bd9Sstevel@tonic-gate 	error_msg(gettext("Can't process restarter events: %s"),
475*7c478bd9Sstevel@tonic-gate 	    strerror(errno));
476*7c478bd9Sstevel@tonic-gate 	(void) close(rst_event_pipe[PE_PRODUCER]);
477*7c478bd9Sstevel@tonic-gate 	for (;;)
478*7c478bd9Sstevel@tonic-gate 		(void) pause();
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
481*7c478bd9Sstevel@tonic-gate }
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate /*
484*7c478bd9Sstevel@tonic-gate  * Let restarter_event_proxy() know we're finished with the event it's blocked
485*7c478bd9Sstevel@tonic-gate  * upon. The 'processed' argument denotes whether we successfully processed the
486*7c478bd9Sstevel@tonic-gate  * event.
487*7c478bd9Sstevel@tonic-gate  */
488*7c478bd9Sstevel@tonic-gate static void
489*7c478bd9Sstevel@tonic-gate ack_restarter_event(boolean_t processed)
490*7c478bd9Sstevel@tonic-gate {
491*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering ack_restarter_event");
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	/*
494*7c478bd9Sstevel@tonic-gate 	 * If safe_write returns -1 something's seriously wrong with the event
495*7c478bd9Sstevel@tonic-gate 	 * pipe, so start the shutdown proceedings.
496*7c478bd9Sstevel@tonic-gate 	 */
497*7c478bd9Sstevel@tonic-gate 	if (safe_write(rst_event_pipe[PE_CONSUMER], &processed,
498*7c478bd9Sstevel@tonic-gate 	    sizeof (processed)) == -1)
499*7c478bd9Sstevel@tonic-gate 		inetd_stop();
500*7c478bd9Sstevel@tonic-gate }
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate /*
503*7c478bd9Sstevel@tonic-gate  * Switch the syslog identification string to 'ident'.
504*7c478bd9Sstevel@tonic-gate  */
505*7c478bd9Sstevel@tonic-gate static void
506*7c478bd9Sstevel@tonic-gate change_syslog_ident(const char *ident)
507*7c478bd9Sstevel@tonic-gate {
508*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering change_syslog_ident: ident: %s", ident);
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	closelog();
511*7c478bd9Sstevel@tonic-gate 	openlog(ident, LOG_PID|LOG_CONS, LOG_DAEMON);
512*7c478bd9Sstevel@tonic-gate }
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate /*
515*7c478bd9Sstevel@tonic-gate  * Perform TCP wrappers checks on this instance. Due to the fact that the
516*7c478bd9Sstevel@tonic-gate  * current wrappers code used in Solaris is taken untouched from the open
517*7c478bd9Sstevel@tonic-gate  * source version, we're stuck with using the daemon name for the checks, as
518*7c478bd9Sstevel@tonic-gate  * opposed to making use of instance FMRIs. Sigh.
519*7c478bd9Sstevel@tonic-gate  * Returns B_TRUE if the check passed, else B_FALSE.
520*7c478bd9Sstevel@tonic-gate  */
521*7c478bd9Sstevel@tonic-gate static boolean_t
522*7c478bd9Sstevel@tonic-gate tcp_wrappers_ok(instance_t *instance)
523*7c478bd9Sstevel@tonic-gate {
524*7c478bd9Sstevel@tonic-gate 	boolean_t		rval = B_TRUE;
525*7c478bd9Sstevel@tonic-gate 	char			*daemon_name;
526*7c478bd9Sstevel@tonic-gate 	basic_cfg_t		*cfg = instance->config->basic;
527*7c478bd9Sstevel@tonic-gate 	struct request_info	req;
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering tcp_wrappers_ok, instance: %s", instance->fmri);
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 	/*
532*7c478bd9Sstevel@tonic-gate 	 * Wrap the service using libwrap functions. The code below implements
533*7c478bd9Sstevel@tonic-gate 	 * the functionality of tcpd. This is done only for stream,nowait
534*7c478bd9Sstevel@tonic-gate 	 * services, following the convention of other vendors.  udp/dgram and
535*7c478bd9Sstevel@tonic-gate 	 * stream/wait can NOT be wrapped with this libwrap, so be wary of
536*7c478bd9Sstevel@tonic-gate 	 * changing the test below.
537*7c478bd9Sstevel@tonic-gate 	 */
538*7c478bd9Sstevel@tonic-gate 	if (cfg->do_tcp_wrappers && !cfg->iswait && !cfg->istlx) {
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 		daemon_name = instance->config->methods[
541*7c478bd9Sstevel@tonic-gate 		    IM_START]->exec_args_we.we_wordv[0];
542*7c478bd9Sstevel@tonic-gate 		if (*daemon_name == '/')
543*7c478bd9Sstevel@tonic-gate 			daemon_name = strrchr(daemon_name, '/') + 1;
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 		/*
546*7c478bd9Sstevel@tonic-gate 		 * Change the syslog message identity to the name of the
547*7c478bd9Sstevel@tonic-gate 		 * daemon being wrapped, as opposed to "inetd".
548*7c478bd9Sstevel@tonic-gate 		 */
549*7c478bd9Sstevel@tonic-gate 		change_syslog_ident(daemon_name);
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 		(void) request_init(&req, RQ_DAEMON, daemon_name, RQ_FILE,
552*7c478bd9Sstevel@tonic-gate 		    instance->conn_fd, NULL);
553*7c478bd9Sstevel@tonic-gate 		fromhost(&req);
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 		if (strcasecmp(eval_hostname(req.client), paranoid) == 0) {
556*7c478bd9Sstevel@tonic-gate 			syslog(deny_severity,
557*7c478bd9Sstevel@tonic-gate 			    "refused connect from %s (name/address mismatch)",
558*7c478bd9Sstevel@tonic-gate 			    eval_client(&req));
559*7c478bd9Sstevel@tonic-gate 			if (req.sink != NULL)
560*7c478bd9Sstevel@tonic-gate 				req.sink(instance->conn_fd);
561*7c478bd9Sstevel@tonic-gate 			rval = B_FALSE;
562*7c478bd9Sstevel@tonic-gate 		} else if (!hosts_access(&req)) {
563*7c478bd9Sstevel@tonic-gate 			syslog(deny_severity,
564*7c478bd9Sstevel@tonic-gate 			    "refused connect from %s (access denied)",
565*7c478bd9Sstevel@tonic-gate 			    eval_client(&req));
566*7c478bd9Sstevel@tonic-gate 			if (req.sink != NULL)
567*7c478bd9Sstevel@tonic-gate 				req.sink(instance->conn_fd);
568*7c478bd9Sstevel@tonic-gate 			rval = B_FALSE;
569*7c478bd9Sstevel@tonic-gate 		} else {
570*7c478bd9Sstevel@tonic-gate 			syslog(allow_severity, "connect from %s",
571*7c478bd9Sstevel@tonic-gate 			    eval_client(&req));
572*7c478bd9Sstevel@tonic-gate 		}
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 		/* Revert syslog identity back to "inetd". */
575*7c478bd9Sstevel@tonic-gate 		change_syslog_ident(SYSLOG_IDENT);
576*7c478bd9Sstevel@tonic-gate 	}
577*7c478bd9Sstevel@tonic-gate 	return (rval);
578*7c478bd9Sstevel@tonic-gate }
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate /*
581*7c478bd9Sstevel@tonic-gate  * Handler registered with the timer queue code to remove an instance from
582*7c478bd9Sstevel@tonic-gate  * the connection rate offline state when it has been there for its allotted
583*7c478bd9Sstevel@tonic-gate  * time.
584*7c478bd9Sstevel@tonic-gate  */
585*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
586*7c478bd9Sstevel@tonic-gate static void
587*7c478bd9Sstevel@tonic-gate conn_rate_online(iu_tq_t *tq, void *arg)
588*7c478bd9Sstevel@tonic-gate {
589*7c478bd9Sstevel@tonic-gate 	instance_t *instance = arg;
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering conn_rate_online, instance: %s",
592*7c478bd9Sstevel@tonic-gate 	    instance->fmri);
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 	assert(instance->cur_istate == IIS_OFFLINE_CONRATE);
595*7c478bd9Sstevel@tonic-gate 	instance->timer_id = -1;
596*7c478bd9Sstevel@tonic-gate 	update_state(instance, IIS_OFFLINE, RERR_RESTART);
597*7c478bd9Sstevel@tonic-gate 	process_offline_inst(instance);
598*7c478bd9Sstevel@tonic-gate }
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate /*
601*7c478bd9Sstevel@tonic-gate  * Check whether this instance in the offline state is in transition to
602*7c478bd9Sstevel@tonic-gate  * another state and do the work to continue this transition.
603*7c478bd9Sstevel@tonic-gate  */
604*7c478bd9Sstevel@tonic-gate void
605*7c478bd9Sstevel@tonic-gate process_offline_inst(instance_t *inst)
606*7c478bd9Sstevel@tonic-gate {
607*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_offline_inst");
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	if (inst->disable_req) {
610*7c478bd9Sstevel@tonic-gate 		inst->disable_req = B_FALSE;
611*7c478bd9Sstevel@tonic-gate 		(void) run_method(inst, IM_DISABLE, NULL);
612*7c478bd9Sstevel@tonic-gate 	} else if (inst->maintenance_req) {
613*7c478bd9Sstevel@tonic-gate 		inst->maintenance_req = B_FALSE;
614*7c478bd9Sstevel@tonic-gate 		update_state(inst, IIS_MAINTENANCE, RERR_RESTART);
615*7c478bd9Sstevel@tonic-gate 	/*
616*7c478bd9Sstevel@tonic-gate 	 * If inetd is in the process of stopping, we don't want to enter
617*7c478bd9Sstevel@tonic-gate 	 * any states but offline, disabled and maintenance.
618*7c478bd9Sstevel@tonic-gate 	 */
619*7c478bd9Sstevel@tonic-gate 	} else if (!inetd_stopping) {
620*7c478bd9Sstevel@tonic-gate 		if (inst->conn_rate_exceeded) {
621*7c478bd9Sstevel@tonic-gate 			basic_cfg_t *cfg = inst->config->basic;
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 			inst->conn_rate_exceeded = B_FALSE;
624*7c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE_CONRATE, RERR_RESTART);
625*7c478bd9Sstevel@tonic-gate 			/*
626*7c478bd9Sstevel@tonic-gate 			 * Schedule a timer to bring the instance out of the
627*7c478bd9Sstevel@tonic-gate 			 * connection rate offline state.
628*7c478bd9Sstevel@tonic-gate 			 */
629*7c478bd9Sstevel@tonic-gate 			inst->timer_id = iu_schedule_timer(timer_queue,
630*7c478bd9Sstevel@tonic-gate 			    cfg->conn_rate_offline, conn_rate_online,
631*7c478bd9Sstevel@tonic-gate 			    inst);
632*7c478bd9Sstevel@tonic-gate 			if (inst->timer_id == -1) {
633*7c478bd9Sstevel@tonic-gate 				error_msg(gettext("%s unable to set timer, "
634*7c478bd9Sstevel@tonic-gate 				    "won't be brought on line after %d "
635*7c478bd9Sstevel@tonic-gate 				    "seconds."), inst->fmri,
636*7c478bd9Sstevel@tonic-gate 				    cfg->conn_rate_offline);
637*7c478bd9Sstevel@tonic-gate 			}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 		} else if (copies_limit_exceeded(inst)) {
640*7c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE_COPIES, RERR_RESTART);
641*7c478bd9Sstevel@tonic-gate 		}
642*7c478bd9Sstevel@tonic-gate 	}
643*7c478bd9Sstevel@tonic-gate }
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate /*
646*7c478bd9Sstevel@tonic-gate  * Create a socket bound to the instance's configured address. If the
647*7c478bd9Sstevel@tonic-gate  * bind fails, returns -1, else the fd of the bound socket.
648*7c478bd9Sstevel@tonic-gate  */
649*7c478bd9Sstevel@tonic-gate static int
650*7c478bd9Sstevel@tonic-gate create_bound_socket(const char *fmri, socket_info_t *sock_info)
651*7c478bd9Sstevel@tonic-gate {
652*7c478bd9Sstevel@tonic-gate 	int		fd;
653*7c478bd9Sstevel@tonic-gate 	int		on = 1;
654*7c478bd9Sstevel@tonic-gate 	rpc_info_t	*rpc = sock_info->pr_info.ri;
655*7c478bd9Sstevel@tonic-gate 	const char	*proto = sock_info->pr_info.proto;
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering create_bound_socket");
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 	fd = socket(sock_info->local_addr.ss_family, sock_info->type,
660*7c478bd9Sstevel@tonic-gate 	    sock_info->protocol);
661*7c478bd9Sstevel@tonic-gate 	if (fd < 0) {
662*7c478bd9Sstevel@tonic-gate 		error_msg(gettext(
663*7c478bd9Sstevel@tonic-gate 		    "Socket creation failure for instance %s, proto %s: %s"),
664*7c478bd9Sstevel@tonic-gate 		    fmri, proto, strerror(errno));
665*7c478bd9Sstevel@tonic-gate 		return (-1);
666*7c478bd9Sstevel@tonic-gate 	}
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate 	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) == -1) {
669*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("setsockopt SO_REUSEADDR failed for service "
670*7c478bd9Sstevel@tonic-gate 		    "instance %s, proto %s: %s"), fmri, proto, strerror(errno));
671*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
672*7c478bd9Sstevel@tonic-gate 		return (-1);
673*7c478bd9Sstevel@tonic-gate 	}
674*7c478bd9Sstevel@tonic-gate 	if (sock_info->pr_info.v6only) {
675*7c478bd9Sstevel@tonic-gate 		/* restrict socket to IPv6 communications only */
676*7c478bd9Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
677*7c478bd9Sstevel@tonic-gate 		    sizeof (on)) == -1) {
678*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("setsockopt IPV6_V6ONLY failed for "
679*7c478bd9Sstevel@tonic-gate 			    "service instance %s, proto %s: %s"), fmri, proto,
680*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
681*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
682*7c478bd9Sstevel@tonic-gate 			return (-1);
683*7c478bd9Sstevel@tonic-gate 		}
684*7c478bd9Sstevel@tonic-gate 	}
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 	if (rpc != NULL)
687*7c478bd9Sstevel@tonic-gate 		SS_SETPORT(sock_info->local_addr, 0);
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 	if (bind(fd, (struct sockaddr *)&(sock_info->local_addr),
690*7c478bd9Sstevel@tonic-gate 	    SS_ADDRLEN(sock_info->local_addr)) < 0) {
691*7c478bd9Sstevel@tonic-gate 		error_msg(gettext(
692*7c478bd9Sstevel@tonic-gate 		    "Failed to bind to the port of service instance %s, "
693*7c478bd9Sstevel@tonic-gate 		    "proto %s: %s"), fmri, proto, strerror(errno));
694*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
695*7c478bd9Sstevel@tonic-gate 		return (-1);
696*7c478bd9Sstevel@tonic-gate 	}
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	/*
699*7c478bd9Sstevel@tonic-gate 	 * Retrieve and store the address bound to for RPC services.
700*7c478bd9Sstevel@tonic-gate 	 */
701*7c478bd9Sstevel@tonic-gate 	if (rpc != NULL) {
702*7c478bd9Sstevel@tonic-gate 		struct sockaddr_storage	ss;
703*7c478bd9Sstevel@tonic-gate 		int			ss_size = sizeof (ss);
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 		if (getsockname(fd, (struct sockaddr *)&ss, &ss_size) < 0) {
706*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed getsockname for instance %s, "
707*7c478bd9Sstevel@tonic-gate 			    "proto %s: %s"), fmri, proto, strerror(errno));
708*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
709*7c478bd9Sstevel@tonic-gate 			return (-1);
710*7c478bd9Sstevel@tonic-gate 		}
711*7c478bd9Sstevel@tonic-gate 		(void) memcpy(rpc->netbuf.buf, &ss,
712*7c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_storage));
713*7c478bd9Sstevel@tonic-gate 		rpc->netbuf.len = SS_ADDRLEN(ss);
714*7c478bd9Sstevel@tonic-gate 		rpc->netbuf.maxlen = SS_ADDRLEN(ss);
715*7c478bd9Sstevel@tonic-gate 	}
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 	if (sock_info->type == SOCK_STREAM)
718*7c478bd9Sstevel@tonic-gate 		(void) listen(fd, CONNECTION_BACKLOG);
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	return (fd);
721*7c478bd9Sstevel@tonic-gate }
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate /*
724*7c478bd9Sstevel@tonic-gate  * Handler registered with the timer queue code to retry the creation
725*7c478bd9Sstevel@tonic-gate  * of a bound fd.
726*7c478bd9Sstevel@tonic-gate  */
727*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
728*7c478bd9Sstevel@tonic-gate static void
729*7c478bd9Sstevel@tonic-gate retry_bind(iu_tq_t *tq, void *arg)
730*7c478bd9Sstevel@tonic-gate {
731*7c478bd9Sstevel@tonic-gate 	instance_t *instance = arg;
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering retry_bind, instance: %s", instance->fmri);
734*7c478bd9Sstevel@tonic-gate 
735*7c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
736*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
737*7c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
738*7c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
739*7c478bd9Sstevel@tonic-gate 	case IIS_IN_ONLINE_METHOD:
740*7c478bd9Sstevel@tonic-gate 	case IIS_IN_REFRESH_METHOD:
741*7c478bd9Sstevel@tonic-gate 		break;
742*7c478bd9Sstevel@tonic-gate 	default:
743*7c478bd9Sstevel@tonic-gate #ifndef NDEBUG
744*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n",
745*7c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, instance->cur_istate);
746*7c478bd9Sstevel@tonic-gate #endif
747*7c478bd9Sstevel@tonic-gate 		abort();
748*7c478bd9Sstevel@tonic-gate 	}
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 	instance->bind_timer_id = -1;
751*7c478bd9Sstevel@tonic-gate 	create_bound_fds(instance);
752*7c478bd9Sstevel@tonic-gate }
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate /*
755*7c478bd9Sstevel@tonic-gate  * For each of the fds for the given instance that are bound, if 'listen' is
756*7c478bd9Sstevel@tonic-gate  * set add them to the poll set, else remove them from it. If any additions
757*7c478bd9Sstevel@tonic-gate  * fail, returns -1, else 0 on success.
758*7c478bd9Sstevel@tonic-gate  */
759*7c478bd9Sstevel@tonic-gate int
760*7c478bd9Sstevel@tonic-gate poll_bound_fds(instance_t *instance, boolean_t listen)
761*7c478bd9Sstevel@tonic-gate {
762*7c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
763*7c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
764*7c478bd9Sstevel@tonic-gate 	int		ret = 0;
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering poll_bound_fds: instance: %s, on: %d",
767*7c478bd9Sstevel@tonic-gate 	    instance->fmri, listen);
768*7c478bd9Sstevel@tonic-gate 
769*7c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
770*7c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
771*7c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1) {	/* fd bound */
772*7c478bd9Sstevel@tonic-gate 			if (!listen) {
773*7c478bd9Sstevel@tonic-gate 				clear_pollfd(pi->listen_fd);
774*7c478bd9Sstevel@tonic-gate 			} else if (set_pollfd(pi->listen_fd, POLLIN) == -1) {
775*7c478bd9Sstevel@tonic-gate 				ret = -1;
776*7c478bd9Sstevel@tonic-gate 			}
777*7c478bd9Sstevel@tonic-gate 		}
778*7c478bd9Sstevel@tonic-gate 	}
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 	return (ret);
781*7c478bd9Sstevel@tonic-gate }
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate /*
784*7c478bd9Sstevel@tonic-gate  * Handle the case were we either fail to create a bound fd or we fail
785*7c478bd9Sstevel@tonic-gate  * to add a bound fd to the poll set for the given instance.
786*7c478bd9Sstevel@tonic-gate  */
787*7c478bd9Sstevel@tonic-gate static void
788*7c478bd9Sstevel@tonic-gate handle_bind_failure(instance_t *instance)
789*7c478bd9Sstevel@tonic-gate {
790*7c478bd9Sstevel@tonic-gate 	basic_cfg_t *cfg = instance->config->basic;
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering handle_bind_failure: instance: %s", instance);
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate 	/*
795*7c478bd9Sstevel@tonic-gate 	 * We must be being called as a result of a failed poll_bound_fds()
796*7c478bd9Sstevel@tonic-gate 	 * as a bind retry is already scheduled. Just return and let it do
797*7c478bd9Sstevel@tonic-gate 	 * the work.
798*7c478bd9Sstevel@tonic-gate 	 */
799*7c478bd9Sstevel@tonic-gate 	if (instance->bind_timer_id != -1)
800*7c478bd9Sstevel@tonic-gate 		return;
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate 	/*
803*7c478bd9Sstevel@tonic-gate 	 * Check if the rebind retries limit is operative and if so,
804*7c478bd9Sstevel@tonic-gate 	 * if it has been reached.
805*7c478bd9Sstevel@tonic-gate 	 */
806*7c478bd9Sstevel@tonic-gate 	if (((cfg->bind_fail_interval <= 0) ||		/* no retries */
807*7c478bd9Sstevel@tonic-gate 	    ((cfg->bind_fail_max >= 0) &&		/* limit reached */
808*7c478bd9Sstevel@tonic-gate 	    (++instance->bind_fail_count > cfg->bind_fail_max))) ||
809*7c478bd9Sstevel@tonic-gate 	    ((instance->bind_timer_id = iu_schedule_timer(timer_queue,
810*7c478bd9Sstevel@tonic-gate 	    cfg->bind_fail_interval, retry_bind, instance)) == -1)) {
811*7c478bd9Sstevel@tonic-gate 		proto_info_t *pi;
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate 		instance->bind_fail_count = 0;
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
816*7c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
817*7c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
818*7c478bd9Sstevel@tonic-gate 			/* check if any of the fds are being poll'd upon */
819*7c478bd9Sstevel@tonic-gate 			for (pi = uu_list_first(cfg->proto_list); pi != NULL;
820*7c478bd9Sstevel@tonic-gate 			    pi = uu_list_next(cfg->proto_list, pi)) {
821*7c478bd9Sstevel@tonic-gate 				if ((pi->listen_fd != -1) &&
822*7c478bd9Sstevel@tonic-gate 				    (find_pollfd(pi->listen_fd) != NULL))
823*7c478bd9Sstevel@tonic-gate 					break;
824*7c478bd9Sstevel@tonic-gate 			}
825*7c478bd9Sstevel@tonic-gate 			if (pi != NULL)	{	/* polling on > 0 fds */
826*7c478bd9Sstevel@tonic-gate 				warn_msg(gettext("Failed to bind on "
827*7c478bd9Sstevel@tonic-gate 				    "all protocols for instance %s, "
828*7c478bd9Sstevel@tonic-gate 				    "transitioning to degraded"),
829*7c478bd9Sstevel@tonic-gate 				    instance->fmri);
830*7c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_DEGRADED, RERR_NONE);
831*7c478bd9Sstevel@tonic-gate 				instance->bind_retries_exceeded = B_TRUE;
832*7c478bd9Sstevel@tonic-gate 				break;
833*7c478bd9Sstevel@tonic-gate 			}
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
836*7c478bd9Sstevel@tonic-gate 			/*
837*7c478bd9Sstevel@tonic-gate 			 * In the case we failed the 'bind' because set_pollfd()
838*7c478bd9Sstevel@tonic-gate 			 * failed on all bound fds, use the offline handling.
839*7c478bd9Sstevel@tonic-gate 			 */
840*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
841*7c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE:
842*7c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_BIND:
843*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Too many bind failures for instance "
844*7c478bd9Sstevel@tonic-gate 			"%s, transitioning to maintenance"), instance->fmri);
845*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE,
846*7c478bd9Sstevel@tonic-gate 			    RERR_FAULT);
847*7c478bd9Sstevel@tonic-gate 			break;
848*7c478bd9Sstevel@tonic-gate 		case IIS_IN_ONLINE_METHOD:
849*7c478bd9Sstevel@tonic-gate 		case IIS_IN_REFRESH_METHOD:
850*7c478bd9Sstevel@tonic-gate 			warn_msg(gettext("Failed to bind on all "
851*7c478bd9Sstevel@tonic-gate 			    "protocols for instance %s, instance will go to "
852*7c478bd9Sstevel@tonic-gate 			    "degraded"), instance->fmri);
853*7c478bd9Sstevel@tonic-gate 			/*
854*7c478bd9Sstevel@tonic-gate 			 * Set the retries exceeded flag so when the method
855*7c478bd9Sstevel@tonic-gate 			 * completes the instance goes to the degraded state.
856*7c478bd9Sstevel@tonic-gate 			 */
857*7c478bd9Sstevel@tonic-gate 			instance->bind_retries_exceeded = B_TRUE;
858*7c478bd9Sstevel@tonic-gate 			break;
859*7c478bd9Sstevel@tonic-gate 		default:
860*7c478bd9Sstevel@tonic-gate #ifndef NDEBUG
861*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
862*7c478bd9Sstevel@tonic-gate 			    "%s:%d: Unknown instance state %d.\n",
863*7c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, instance->cur_istate);
864*7c478bd9Sstevel@tonic-gate #endif
865*7c478bd9Sstevel@tonic-gate 			abort();
866*7c478bd9Sstevel@tonic-gate 		}
867*7c478bd9Sstevel@tonic-gate 	} else if (instance->cur_istate == IIS_OFFLINE) {
868*7c478bd9Sstevel@tonic-gate 		/*
869*7c478bd9Sstevel@tonic-gate 		 * bind re-scheduled, so if we're offline reflect this in the
870*7c478bd9Sstevel@tonic-gate 		 * state.
871*7c478bd9Sstevel@tonic-gate 		 */
872*7c478bd9Sstevel@tonic-gate 		update_state(instance, IIS_OFFLINE_BIND, RERR_NONE);
873*7c478bd9Sstevel@tonic-gate 	}
874*7c478bd9Sstevel@tonic-gate }
875*7c478bd9Sstevel@tonic-gate 
876*7c478bd9Sstevel@tonic-gate /*
877*7c478bd9Sstevel@tonic-gate  * Independent of the transport, for each of the entries in the instance's
878*7c478bd9Sstevel@tonic-gate  * proto list this function first attempts to create an associated network fd;
879*7c478bd9Sstevel@tonic-gate  * for RPC services these are then bound to a kernel chosen port and the
880*7c478bd9Sstevel@tonic-gate  * fd is registered with rpcbind; for non-RPC services the fds are bound
881*7c478bd9Sstevel@tonic-gate  * to the port associated with the instance's service name. On any successful
882*7c478bd9Sstevel@tonic-gate  * binds the instance is taken online. Failed binds are handled by
883*7c478bd9Sstevel@tonic-gate  * handle_bind_failure().
884*7c478bd9Sstevel@tonic-gate  */
885*7c478bd9Sstevel@tonic-gate void
886*7c478bd9Sstevel@tonic-gate create_bound_fds(instance_t *instance)
887*7c478bd9Sstevel@tonic-gate {
888*7c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
889*7c478bd9Sstevel@tonic-gate 	boolean_t	failure = B_FALSE;
890*7c478bd9Sstevel@tonic-gate 	boolean_t	success = B_FALSE;
891*7c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering create_bound_fd: instance: %s", instance->fmri);
894*7c478bd9Sstevel@tonic-gate 
895*7c478bd9Sstevel@tonic-gate 	/*
896*7c478bd9Sstevel@tonic-gate 	 * Loop through and try and bind any unbound protos.
897*7c478bd9Sstevel@tonic-gate 	 */
898*7c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
899*7c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
900*7c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1)
901*7c478bd9Sstevel@tonic-gate 			continue;
902*7c478bd9Sstevel@tonic-gate 		if (cfg->istlx) {
903*7c478bd9Sstevel@tonic-gate 			pi->listen_fd = create_bound_endpoint(instance->fmri,
904*7c478bd9Sstevel@tonic-gate 			    (tlx_info_t *)pi);
905*7c478bd9Sstevel@tonic-gate 		} else {
906*7c478bd9Sstevel@tonic-gate 			/*
907*7c478bd9Sstevel@tonic-gate 			 * We cast pi to a void so we can then go on to cast
908*7c478bd9Sstevel@tonic-gate 			 * it to a socket_info_t without lint complaining
909*7c478bd9Sstevel@tonic-gate 			 * about alignment. This is done because the x86
910*7c478bd9Sstevel@tonic-gate 			 * version of lint thinks a lint suppression directive
911*7c478bd9Sstevel@tonic-gate 			 * is unnecessary and flags it as such, yet the sparc
912*7c478bd9Sstevel@tonic-gate 			 * version complains if it's absent.
913*7c478bd9Sstevel@tonic-gate 			 */
914*7c478bd9Sstevel@tonic-gate 			void *p = pi;
915*7c478bd9Sstevel@tonic-gate 			pi->listen_fd = create_bound_socket(instance->fmri,
916*7c478bd9Sstevel@tonic-gate 			    (socket_info_t *)p);
917*7c478bd9Sstevel@tonic-gate 		}
918*7c478bd9Sstevel@tonic-gate 		if (pi->listen_fd == -1) {
919*7c478bd9Sstevel@tonic-gate 			failure = B_TRUE;
920*7c478bd9Sstevel@tonic-gate 			continue;
921*7c478bd9Sstevel@tonic-gate 		}
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 		if (pi->ri != NULL) {
924*7c478bd9Sstevel@tonic-gate 			unregister_rpc_service(instance->fmri, pi->ri);
925*7c478bd9Sstevel@tonic-gate 			if (register_rpc_service(instance->fmri, pi->ri) ==
926*7c478bd9Sstevel@tonic-gate 			    -1) {
927*7c478bd9Sstevel@tonic-gate 				close_net_fd(instance, pi->listen_fd);
928*7c478bd9Sstevel@tonic-gate 				pi->listen_fd = -1;
929*7c478bd9Sstevel@tonic-gate 				failure = B_TRUE;
930*7c478bd9Sstevel@tonic-gate 				continue;
931*7c478bd9Sstevel@tonic-gate 			}
932*7c478bd9Sstevel@tonic-gate 		}
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 		success = B_TRUE;
935*7c478bd9Sstevel@tonic-gate 	}
936*7c478bd9Sstevel@tonic-gate 
937*7c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
938*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE:
939*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
940*7c478bd9Sstevel@tonic-gate 		/*
941*7c478bd9Sstevel@tonic-gate 		 * If we've managed to bind at least one proto lets run the
942*7c478bd9Sstevel@tonic-gate 		 * online method, so we can start listening for it.
943*7c478bd9Sstevel@tonic-gate 		 */
944*7c478bd9Sstevel@tonic-gate 		if (success && run_method(instance, IM_ONLINE, NULL) == -1)
945*7c478bd9Sstevel@tonic-gate 			return;	/* instance gone to maintenance */
946*7c478bd9Sstevel@tonic-gate 		break;
947*7c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
948*7c478bd9Sstevel@tonic-gate 	case IIS_IN_REFRESH_METHOD:
949*7c478bd9Sstevel@tonic-gate 		/*
950*7c478bd9Sstevel@tonic-gate 		 * We're 'online', so start polling on any bound fds we're
951*7c478bd9Sstevel@tonic-gate 		 * currently not.
952*7c478bd9Sstevel@tonic-gate 		 */
953*7c478bd9Sstevel@tonic-gate 		if (poll_bound_fds(instance, B_TRUE) != 0) {
954*7c478bd9Sstevel@tonic-gate 			failure = B_TRUE;
955*7c478bd9Sstevel@tonic-gate 		} else if (!failure) {
956*7c478bd9Sstevel@tonic-gate 			/*
957*7c478bd9Sstevel@tonic-gate 			 * We've successfully bound and poll'd upon all protos,
958*7c478bd9Sstevel@tonic-gate 			 * so reset the failure count.
959*7c478bd9Sstevel@tonic-gate 			 */
960*7c478bd9Sstevel@tonic-gate 			instance->bind_fail_count = 0;
961*7c478bd9Sstevel@tonic-gate 		}
962*7c478bd9Sstevel@tonic-gate 		break;
963*7c478bd9Sstevel@tonic-gate 	case IIS_IN_ONLINE_METHOD:
964*7c478bd9Sstevel@tonic-gate 		/*
965*7c478bd9Sstevel@tonic-gate 		 * Nothing to do here as the method completion code will start
966*7c478bd9Sstevel@tonic-gate 		 * listening for any successfully bound fds.
967*7c478bd9Sstevel@tonic-gate 		 */
968*7c478bd9Sstevel@tonic-gate 		break;
969*7c478bd9Sstevel@tonic-gate 	default:
970*7c478bd9Sstevel@tonic-gate #ifndef NDEBUG
971*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Unknown instance state %d.\n",
972*7c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, instance->cur_istate);
973*7c478bd9Sstevel@tonic-gate #endif
974*7c478bd9Sstevel@tonic-gate 		abort();
975*7c478bd9Sstevel@tonic-gate 	}
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate 	if (failure)
978*7c478bd9Sstevel@tonic-gate 		handle_bind_failure(instance);
979*7c478bd9Sstevel@tonic-gate }
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate /*
982*7c478bd9Sstevel@tonic-gate  * Counter to create_bound_fds(), for each of the bound network fds this
983*7c478bd9Sstevel@tonic-gate  * function unregisters the instance from rpcbind if it's an RPC service,
984*7c478bd9Sstevel@tonic-gate  * stops listening for new connections for it and then closes the listening fd.
985*7c478bd9Sstevel@tonic-gate  */
986*7c478bd9Sstevel@tonic-gate static void
987*7c478bd9Sstevel@tonic-gate destroy_bound_fds(instance_t *instance)
988*7c478bd9Sstevel@tonic-gate {
989*7c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
990*7c478bd9Sstevel@tonic-gate 	proto_info_t	*pi;
991*7c478bd9Sstevel@tonic-gate 
992*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering destroy_bound_fds: instance: %s", instance->fmri);
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 	for (pi = uu_list_first(cfg->proto_list); pi != NULL;
995*7c478bd9Sstevel@tonic-gate 	    pi = uu_list_next(cfg->proto_list, pi)) {
996*7c478bd9Sstevel@tonic-gate 		if (pi->listen_fd != -1) {
997*7c478bd9Sstevel@tonic-gate 			if (pi->ri != NULL)
998*7c478bd9Sstevel@tonic-gate 				unregister_rpc_service(instance->fmri, pi->ri);
999*7c478bd9Sstevel@tonic-gate 			clear_pollfd(pi->listen_fd);
1000*7c478bd9Sstevel@tonic-gate 			close_net_fd(instance, pi->listen_fd);
1001*7c478bd9Sstevel@tonic-gate 			pi->listen_fd = -1;
1002*7c478bd9Sstevel@tonic-gate 		}
1003*7c478bd9Sstevel@tonic-gate 	}
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 	/* cancel any bind retries */
1006*7c478bd9Sstevel@tonic-gate 	if (instance->bind_timer_id != -1)
1007*7c478bd9Sstevel@tonic-gate 		cancel_bind_timer(instance);
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate 	instance->bind_retries_exceeded = B_FALSE;
1010*7c478bd9Sstevel@tonic-gate }
1011*7c478bd9Sstevel@tonic-gate 
1012*7c478bd9Sstevel@tonic-gate /*
1013*7c478bd9Sstevel@tonic-gate  * Perform %A address expansion and return a pointer to a static string
1014*7c478bd9Sstevel@tonic-gate  * array containing crafted arguments. This expansion is provided for
1015*7c478bd9Sstevel@tonic-gate  * compatibility with 4.2BSD daemons, and as such we've copied the logic of
1016*7c478bd9Sstevel@tonic-gate  * the legacy inetd to maintain this compatibility as much as possible. This
1017*7c478bd9Sstevel@tonic-gate  * logic is a bit scatty, but it dates back at least as far as SunOS 4.x.
1018*7c478bd9Sstevel@tonic-gate  */
1019*7c478bd9Sstevel@tonic-gate static char **
1020*7c478bd9Sstevel@tonic-gate expand_address(instance_t *inst, const proto_info_t *pi)
1021*7c478bd9Sstevel@tonic-gate {
1022*7c478bd9Sstevel@tonic-gate 	static char	addrbuf[sizeof ("ffffffff.65536")];
1023*7c478bd9Sstevel@tonic-gate 	static char	*ret[3];
1024*7c478bd9Sstevel@tonic-gate 	instance_cfg_t	*cfg = inst->config;
1025*7c478bd9Sstevel@tonic-gate 	/*
1026*7c478bd9Sstevel@tonic-gate 	 * We cast pi to a void so we can then go on to cast it to a
1027*7c478bd9Sstevel@tonic-gate 	 * socket_info_t without lint complaining about alignment. This
1028*7c478bd9Sstevel@tonic-gate 	 * is done because the x86 version of lint thinks a lint suppression
1029*7c478bd9Sstevel@tonic-gate 	 * directive is unnecessary and flags it as such, yet the sparc
1030*7c478bd9Sstevel@tonic-gate 	 * version complains if it's absent.
1031*7c478bd9Sstevel@tonic-gate 	 */
1032*7c478bd9Sstevel@tonic-gate 	const void	*p = pi;
1033*7c478bd9Sstevel@tonic-gate 
1034*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering expand_address");
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate 	/* set ret[0] to the basename of exec path */
1037*7c478bd9Sstevel@tonic-gate 	if ((ret[0] = strrchr(cfg->methods[IM_START]->exec_path, '/'))
1038*7c478bd9Sstevel@tonic-gate 	    != NULL) {
1039*7c478bd9Sstevel@tonic-gate 		ret[0]++;
1040*7c478bd9Sstevel@tonic-gate 	} else {
1041*7c478bd9Sstevel@tonic-gate 		ret[0] = cfg->methods[IM_START]->exec_path;
1042*7c478bd9Sstevel@tonic-gate 	}
1043*7c478bd9Sstevel@tonic-gate 
1044*7c478bd9Sstevel@tonic-gate 	if (!cfg->basic->istlx &&
1045*7c478bd9Sstevel@tonic-gate 	    (((socket_info_t *)p)->type == SOCK_DGRAM)) {
1046*7c478bd9Sstevel@tonic-gate 		ret[1] = NULL;
1047*7c478bd9Sstevel@tonic-gate 	} else {
1048*7c478bd9Sstevel@tonic-gate 		addrbuf[0] = '\0';
1049*7c478bd9Sstevel@tonic-gate 		if (!cfg->basic->iswait &&
1050*7c478bd9Sstevel@tonic-gate 		    (inst->remote_addr.ss_family == AF_INET)) {
1051*7c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sp;
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate 			sp = (struct sockaddr_in *)&(inst->remote_addr);
1054*7c478bd9Sstevel@tonic-gate 			(void) snprintf(addrbuf, sizeof (addrbuf), "%x.%hu",
1055*7c478bd9Sstevel@tonic-gate 			    ntohl(sp->sin_addr.s_addr), ntohs(sp->sin_port));
1056*7c478bd9Sstevel@tonic-gate 		}
1057*7c478bd9Sstevel@tonic-gate 		ret[1] = addrbuf;
1058*7c478bd9Sstevel@tonic-gate 		ret[2] = NULL;
1059*7c478bd9Sstevel@tonic-gate 	}
1060*7c478bd9Sstevel@tonic-gate 
1061*7c478bd9Sstevel@tonic-gate 	return (ret);
1062*7c478bd9Sstevel@tonic-gate }
1063*7c478bd9Sstevel@tonic-gate 
1064*7c478bd9Sstevel@tonic-gate /*
1065*7c478bd9Sstevel@tonic-gate  * Returns the state associated with the supplied method being run for an
1066*7c478bd9Sstevel@tonic-gate  * instance.
1067*7c478bd9Sstevel@tonic-gate  */
1068*7c478bd9Sstevel@tonic-gate static internal_inst_state_t
1069*7c478bd9Sstevel@tonic-gate get_method_state(instance_method_t method)
1070*7c478bd9Sstevel@tonic-gate {
1071*7c478bd9Sstevel@tonic-gate 	state_info_t *sip;
1072*7c478bd9Sstevel@tonic-gate 
1073*7c478bd9Sstevel@tonic-gate 	for (sip = states; sip->istate != IIS_NONE; sip++) {
1074*7c478bd9Sstevel@tonic-gate 		if (sip->method_running == method)
1075*7c478bd9Sstevel@tonic-gate 			break;
1076*7c478bd9Sstevel@tonic-gate 	}
1077*7c478bd9Sstevel@tonic-gate 	assert(sip->istate != IIS_NONE);
1078*7c478bd9Sstevel@tonic-gate 
1079*7c478bd9Sstevel@tonic-gate 	return (sip->istate);
1080*7c478bd9Sstevel@tonic-gate }
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate /*
1083*7c478bd9Sstevel@tonic-gate  * Store the method's PID and CID in the repository. If the store fails
1084*7c478bd9Sstevel@tonic-gate  * we ignore it and just drive on.
1085*7c478bd9Sstevel@tonic-gate  */
1086*7c478bd9Sstevel@tonic-gate static void
1087*7c478bd9Sstevel@tonic-gate add_method_ids(instance_t *ins, pid_t pid, ctid_t cid, instance_method_t mthd)
1088*7c478bd9Sstevel@tonic-gate {
1089*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering add_method_ids");
1090*7c478bd9Sstevel@tonic-gate 
1091*7c478bd9Sstevel@tonic-gate 	if (cid != -1)
1092*7c478bd9Sstevel@tonic-gate 		(void) add_remove_contract(ins->fmri, B_TRUE, cid);
1093*7c478bd9Sstevel@tonic-gate 
1094*7c478bd9Sstevel@tonic-gate 	if (mthd == IM_START) {
1095*7c478bd9Sstevel@tonic-gate 		if (add_rep_val(ins->start_pids, (int64_t)pid) == 0) {
1096*7c478bd9Sstevel@tonic-gate 			(void) store_rep_vals(ins->start_pids, ins->fmri,
1097*7c478bd9Sstevel@tonic-gate 			    PR_NAME_START_PIDS);
1098*7c478bd9Sstevel@tonic-gate 		}
1099*7c478bd9Sstevel@tonic-gate 	} else {
1100*7c478bd9Sstevel@tonic-gate 		if (add_rep_val(ins->non_start_pid, (int64_t)pid) == 0) {
1101*7c478bd9Sstevel@tonic-gate 			(void) store_rep_vals(ins->non_start_pid, ins->fmri,
1102*7c478bd9Sstevel@tonic-gate 			    PR_NAME_NON_START_PID);
1103*7c478bd9Sstevel@tonic-gate 		}
1104*7c478bd9Sstevel@tonic-gate 	}
1105*7c478bd9Sstevel@tonic-gate }
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate /*
1108*7c478bd9Sstevel@tonic-gate  * Remove the method's PID and CID from the repository. If the removal
1109*7c478bd9Sstevel@tonic-gate  * fails we ignore it and drive on.
1110*7c478bd9Sstevel@tonic-gate  */
1111*7c478bd9Sstevel@tonic-gate void
1112*7c478bd9Sstevel@tonic-gate remove_method_ids(instance_t *inst, pid_t pid, ctid_t cid,
1113*7c478bd9Sstevel@tonic-gate     instance_method_t mthd)
1114*7c478bd9Sstevel@tonic-gate {
1115*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering remove_method_ids");
1116*7c478bd9Sstevel@tonic-gate 
1117*7c478bd9Sstevel@tonic-gate 	if (cid != -1)
1118*7c478bd9Sstevel@tonic-gate 		(void) add_remove_contract(inst->fmri, B_FALSE, cid);
1119*7c478bd9Sstevel@tonic-gate 
1120*7c478bd9Sstevel@tonic-gate 	if (mthd == IM_START) {
1121*7c478bd9Sstevel@tonic-gate 		remove_rep_val(inst->start_pids, (int64_t)pid);
1122*7c478bd9Sstevel@tonic-gate 		(void) store_rep_vals(inst->start_pids, inst->fmri,
1123*7c478bd9Sstevel@tonic-gate 		    PR_NAME_START_PIDS);
1124*7c478bd9Sstevel@tonic-gate 	} else {
1125*7c478bd9Sstevel@tonic-gate 		remove_rep_val(inst->non_start_pid, (int64_t)pid);
1126*7c478bd9Sstevel@tonic-gate 		(void) store_rep_vals(inst->non_start_pid, inst->fmri,
1127*7c478bd9Sstevel@tonic-gate 		    PR_NAME_NON_START_PID);
1128*7c478bd9Sstevel@tonic-gate 	}
1129*7c478bd9Sstevel@tonic-gate }
1130*7c478bd9Sstevel@tonic-gate 
1131*7c478bd9Sstevel@tonic-gate static instance_t *
1132*7c478bd9Sstevel@tonic-gate create_instance(const char *fmri)
1133*7c478bd9Sstevel@tonic-gate {
1134*7c478bd9Sstevel@tonic-gate 	instance_t *ret;
1135*7c478bd9Sstevel@tonic-gate 
1136*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering create_instance, instance: %s", fmri);
1137*7c478bd9Sstevel@tonic-gate 
1138*7c478bd9Sstevel@tonic-gate 	if (((ret = calloc(1, sizeof (instance_t))) == NULL) ||
1139*7c478bd9Sstevel@tonic-gate 	    ((ret->fmri = strdup(fmri)) == NULL))
1140*7c478bd9Sstevel@tonic-gate 		goto alloc_fail;
1141*7c478bd9Sstevel@tonic-gate 
1142*7c478bd9Sstevel@tonic-gate 	ret->conn_fd = -1;
1143*7c478bd9Sstevel@tonic-gate 
1144*7c478bd9Sstevel@tonic-gate 	ret->copies = 0;
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate 	ret->conn_rate_count = 0;
1147*7c478bd9Sstevel@tonic-gate 	ret->fail_rate_count = 0;
1148*7c478bd9Sstevel@tonic-gate 	ret->bind_fail_count = 0;
1149*7c478bd9Sstevel@tonic-gate 
1150*7c478bd9Sstevel@tonic-gate 	if (((ret->non_start_pid = create_rep_val_list()) == NULL) ||
1151*7c478bd9Sstevel@tonic-gate 	    ((ret->start_pids = create_rep_val_list()) == NULL))
1152*7c478bd9Sstevel@tonic-gate 		goto alloc_fail;
1153*7c478bd9Sstevel@tonic-gate 
1154*7c478bd9Sstevel@tonic-gate 	ret->cur_istate = IIS_NONE;
1155*7c478bd9Sstevel@tonic-gate 	ret->next_istate = IIS_NONE;
1156*7c478bd9Sstevel@tonic-gate 
1157*7c478bd9Sstevel@tonic-gate 	if (((ret->cur_istate_rep = create_rep_val_list()) == NULL) ||
1158*7c478bd9Sstevel@tonic-gate 	    ((ret->next_istate_rep = create_rep_val_list()) == NULL))
1159*7c478bd9Sstevel@tonic-gate 		goto alloc_fail;
1160*7c478bd9Sstevel@tonic-gate 
1161*7c478bd9Sstevel@tonic-gate 	ret->config = NULL;
1162*7c478bd9Sstevel@tonic-gate 	ret->new_config = NULL;
1163*7c478bd9Sstevel@tonic-gate 
1164*7c478bd9Sstevel@tonic-gate 	ret->timer_id = -1;
1165*7c478bd9Sstevel@tonic-gate 	ret->bind_timer_id = -1;
1166*7c478bd9Sstevel@tonic-gate 
1167*7c478bd9Sstevel@tonic-gate 	ret->disable_req = B_FALSE;
1168*7c478bd9Sstevel@tonic-gate 	ret->maintenance_req = B_FALSE;
1169*7c478bd9Sstevel@tonic-gate 	ret->conn_rate_exceeded = B_FALSE;
1170*7c478bd9Sstevel@tonic-gate 	ret->bind_retries_exceeded = B_FALSE;
1171*7c478bd9Sstevel@tonic-gate 
1172*7c478bd9Sstevel@tonic-gate 	ret->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID;
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate 	return (ret);
1175*7c478bd9Sstevel@tonic-gate 
1176*7c478bd9Sstevel@tonic-gate alloc_fail:
1177*7c478bd9Sstevel@tonic-gate 	error_msg(strerror(errno));
1178*7c478bd9Sstevel@tonic-gate 	destroy_instance(ret);
1179*7c478bd9Sstevel@tonic-gate 	return (NULL);
1180*7c478bd9Sstevel@tonic-gate }
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate static void
1183*7c478bd9Sstevel@tonic-gate destroy_instance(instance_t *inst)
1184*7c478bd9Sstevel@tonic-gate {
1185*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering destroy_instance");
1186*7c478bd9Sstevel@tonic-gate 
1187*7c478bd9Sstevel@tonic-gate 	if (inst == NULL)
1188*7c478bd9Sstevel@tonic-gate 		return;
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 	destroy_instance_cfg(inst->config);
1191*7c478bd9Sstevel@tonic-gate 	destroy_instance_cfg(inst->new_config);
1192*7c478bd9Sstevel@tonic-gate 
1193*7c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->cur_istate_rep);
1194*7c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->next_istate_rep);
1195*7c478bd9Sstevel@tonic-gate 
1196*7c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->start_pids);
1197*7c478bd9Sstevel@tonic-gate 	destroy_rep_val_list(inst->non_start_pid);
1198*7c478bd9Sstevel@tonic-gate 
1199*7c478bd9Sstevel@tonic-gate 	free(inst->fmri);
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate 	free(inst);
1202*7c478bd9Sstevel@tonic-gate }
1203*7c478bd9Sstevel@tonic-gate 
1204*7c478bd9Sstevel@tonic-gate /*
1205*7c478bd9Sstevel@tonic-gate  * Retrieves the current and next states internal states. Returns 0 on success,
1206*7c478bd9Sstevel@tonic-gate  * else returns one of the following on error:
1207*7c478bd9Sstevel@tonic-gate  * SCF_ERROR_NO_MEMORY if memory allocation failed.
1208*7c478bd9Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
1209*7c478bd9Sstevel@tonic-gate  * SCF_ERROR_TYPE_MISMATCH if the property was of an unexpected type.
1210*7c478bd9Sstevel@tonic-gate  * SCF_ERROR_NO_RESOURCES if the server doesn't have adequate resources.
1211*7c478bd9Sstevel@tonic-gate  * SCF_ERROR_NO_SERVER if the server isn't running.
1212*7c478bd9Sstevel@tonic-gate  */
1213*7c478bd9Sstevel@tonic-gate static scf_error_t
1214*7c478bd9Sstevel@tonic-gate retrieve_instance_state(instance_t *inst)
1215*7c478bd9Sstevel@tonic-gate {
1216*7c478bd9Sstevel@tonic-gate 	scf_error_t	ret;
1217*7c478bd9Sstevel@tonic-gate 
1218*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering retrieve_instance_state: instance: %s",
1219*7c478bd9Sstevel@tonic-gate 	    inst->fmri);
1220*7c478bd9Sstevel@tonic-gate 
1221*7c478bd9Sstevel@tonic-gate 	/* retrieve internal states */
1222*7c478bd9Sstevel@tonic-gate 	if (((ret = retrieve_rep_vals(inst->cur_istate_rep, inst->fmri,
1223*7c478bd9Sstevel@tonic-gate 	    PR_NAME_CUR_INT_STATE)) != 0) ||
1224*7c478bd9Sstevel@tonic-gate 	    ((ret = retrieve_rep_vals(inst->next_istate_rep, inst->fmri,
1225*7c478bd9Sstevel@tonic-gate 	    PR_NAME_NEXT_INT_STATE)) != 0)) {
1226*7c478bd9Sstevel@tonic-gate 		if (ret != SCF_ERROR_NOT_FOUND) {
1227*7c478bd9Sstevel@tonic-gate 			error_msg(gettext(
1228*7c478bd9Sstevel@tonic-gate 			    "Failed to read state of instance %s: %s"),
1229*7c478bd9Sstevel@tonic-gate 			    inst->fmri, scf_strerror(scf_error()));
1230*7c478bd9Sstevel@tonic-gate 			return (ret);
1231*7c478bd9Sstevel@tonic-gate 		}
1232*7c478bd9Sstevel@tonic-gate 
1233*7c478bd9Sstevel@tonic-gate 		debug_msg("instance with no previous int state - "
1234*7c478bd9Sstevel@tonic-gate 		    "setting state to uninitialized");
1235*7c478bd9Sstevel@tonic-gate 
1236*7c478bd9Sstevel@tonic-gate 		if ((set_single_rep_val(inst->cur_istate_rep,
1237*7c478bd9Sstevel@tonic-gate 		    (int64_t)IIS_UNINITIALIZED) == -1) ||
1238*7c478bd9Sstevel@tonic-gate 		    (set_single_rep_val(inst->next_istate_rep,
1239*7c478bd9Sstevel@tonic-gate 		    (int64_t)IIS_NONE) == -1)) {
1240*7c478bd9Sstevel@tonic-gate 			return (SCF_ERROR_NO_MEMORY);
1241*7c478bd9Sstevel@tonic-gate 		}
1242*7c478bd9Sstevel@tonic-gate 	}
1243*7c478bd9Sstevel@tonic-gate 
1244*7c478bd9Sstevel@tonic-gate 	/* update convenience states */
1245*7c478bd9Sstevel@tonic-gate 	inst->cur_istate = get_single_rep_val(inst->cur_istate_rep);
1246*7c478bd9Sstevel@tonic-gate 	inst->next_istate = get_single_rep_val(inst->next_istate_rep);
1247*7c478bd9Sstevel@tonic-gate 	debug_msg("previous states: cur: %d, next: %d", inst->cur_istate,
1248*7c478bd9Sstevel@tonic-gate 	    inst->next_istate);
1249*7c478bd9Sstevel@tonic-gate 
1250*7c478bd9Sstevel@tonic-gate 	return (0);
1251*7c478bd9Sstevel@tonic-gate }
1252*7c478bd9Sstevel@tonic-gate 
1253*7c478bd9Sstevel@tonic-gate /*
1254*7c478bd9Sstevel@tonic-gate  * Retrieve stored process ids and register each of them so we process their
1255*7c478bd9Sstevel@tonic-gate  * termination.
1256*7c478bd9Sstevel@tonic-gate  */
1257*7c478bd9Sstevel@tonic-gate static int
1258*7c478bd9Sstevel@tonic-gate retrieve_method_pids(instance_t *inst)
1259*7c478bd9Sstevel@tonic-gate {
1260*7c478bd9Sstevel@tonic-gate 	rep_val_t	*rv;
1261*7c478bd9Sstevel@tonic-gate 
1262*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering remove_method_pids");
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate 	switch (retrieve_rep_vals(inst->start_pids, inst->fmri,
1265*7c478bd9Sstevel@tonic-gate 	    PR_NAME_START_PIDS)) {
1266*7c478bd9Sstevel@tonic-gate 	case 0:
1267*7c478bd9Sstevel@tonic-gate 		break;
1268*7c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
1269*7c478bd9Sstevel@tonic-gate 		return (0);
1270*7c478bd9Sstevel@tonic-gate 	default:
1271*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to retrieve the start pids of "
1272*7c478bd9Sstevel@tonic-gate 		    "instance %s from repository: %s"), inst->fmri,
1273*7c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
1274*7c478bd9Sstevel@tonic-gate 		return (-1);
1275*7c478bd9Sstevel@tonic-gate 	}
1276*7c478bd9Sstevel@tonic-gate 
1277*7c478bd9Sstevel@tonic-gate 	rv = uu_list_first(inst->start_pids);
1278*7c478bd9Sstevel@tonic-gate 	while (rv != NULL) {
1279*7c478bd9Sstevel@tonic-gate 		if (register_method(inst, (pid_t)rv->val, (ctid_t)-1,
1280*7c478bd9Sstevel@tonic-gate 		    IM_START) == 0) {
1281*7c478bd9Sstevel@tonic-gate 			inst->copies++;
1282*7c478bd9Sstevel@tonic-gate 			rv = uu_list_next(inst->start_pids, rv);
1283*7c478bd9Sstevel@tonic-gate 		} else if (errno == ENOENT) {
1284*7c478bd9Sstevel@tonic-gate 			pid_t pid = (pid_t)rv->val;
1285*7c478bd9Sstevel@tonic-gate 
1286*7c478bd9Sstevel@tonic-gate 			/*
1287*7c478bd9Sstevel@tonic-gate 			 * The process must have already terminated. Remove
1288*7c478bd9Sstevel@tonic-gate 			 * it from the list.
1289*7c478bd9Sstevel@tonic-gate 			 */
1290*7c478bd9Sstevel@tonic-gate 			rv = uu_list_next(inst->start_pids, rv);
1291*7c478bd9Sstevel@tonic-gate 			remove_rep_val(inst->start_pids, pid);
1292*7c478bd9Sstevel@tonic-gate 		} else {
1293*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to listen for the completion "
1294*7c478bd9Sstevel@tonic-gate 			    "of %s method of instance %s"), START_METHOD_NAME,
1295*7c478bd9Sstevel@tonic-gate 			    inst->fmri);
1296*7c478bd9Sstevel@tonic-gate 			rv = uu_list_next(inst->start_pids, rv);
1297*7c478bd9Sstevel@tonic-gate 		}
1298*7c478bd9Sstevel@tonic-gate 	}
1299*7c478bd9Sstevel@tonic-gate 
1300*7c478bd9Sstevel@tonic-gate 	/* synch the repository pid list to remove any terminated pids */
1301*7c478bd9Sstevel@tonic-gate 	(void) store_rep_vals(inst->start_pids, inst->fmri, PR_NAME_START_PIDS);
1302*7c478bd9Sstevel@tonic-gate 
1303*7c478bd9Sstevel@tonic-gate 	return (0);
1304*7c478bd9Sstevel@tonic-gate }
1305*7c478bd9Sstevel@tonic-gate 
1306*7c478bd9Sstevel@tonic-gate /*
1307*7c478bd9Sstevel@tonic-gate  * Remove the passed instance from inetd control.
1308*7c478bd9Sstevel@tonic-gate  */
1309*7c478bd9Sstevel@tonic-gate static void
1310*7c478bd9Sstevel@tonic-gate remove_instance(instance_t *instance)
1311*7c478bd9Sstevel@tonic-gate {
1312*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering remove_instance");
1313*7c478bd9Sstevel@tonic-gate 
1314*7c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
1315*7c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
1316*7c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
1317*7c478bd9Sstevel@tonic-gate 		/* stop listening for network connections */
1318*7c478bd9Sstevel@tonic-gate 		destroy_bound_fds(instance);
1319*7c478bd9Sstevel@tonic-gate 		break;
1320*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
1321*7c478bd9Sstevel@tonic-gate 		cancel_bind_timer(instance);
1322*7c478bd9Sstevel@tonic-gate 		break;
1323*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_CONRATE:
1324*7c478bd9Sstevel@tonic-gate 		cancel_inst_timer(instance);
1325*7c478bd9Sstevel@tonic-gate 		break;
1326*7c478bd9Sstevel@tonic-gate 	}
1327*7c478bd9Sstevel@tonic-gate 
1328*7c478bd9Sstevel@tonic-gate 	/* stop listening for terminated methods */
1329*7c478bd9Sstevel@tonic-gate 	unregister_instance_methods(instance);
1330*7c478bd9Sstevel@tonic-gate 
1331*7c478bd9Sstevel@tonic-gate 	uu_list_remove(instance_list, instance);
1332*7c478bd9Sstevel@tonic-gate 	destroy_instance(instance);
1333*7c478bd9Sstevel@tonic-gate }
1334*7c478bd9Sstevel@tonic-gate 
1335*7c478bd9Sstevel@tonic-gate /*
1336*7c478bd9Sstevel@tonic-gate  * Refresh the configuration of instance 'inst'. This method gets called as
1337*7c478bd9Sstevel@tonic-gate  * a result of a refresh event for the instance from the master restarter, so
1338*7c478bd9Sstevel@tonic-gate  * we can rely upon the instance's running snapshot having been updated from
1339*7c478bd9Sstevel@tonic-gate  * its configuration snapshot.
1340*7c478bd9Sstevel@tonic-gate  */
1341*7c478bd9Sstevel@tonic-gate void
1342*7c478bd9Sstevel@tonic-gate refresh_instance(instance_t *inst)
1343*7c478bd9Sstevel@tonic-gate {
1344*7c478bd9Sstevel@tonic-gate 	instance_cfg_t	*cfg;
1345*7c478bd9Sstevel@tonic-gate 
1346*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering refresh_instance: inst: %s", inst->fmri);
1347*7c478bd9Sstevel@tonic-gate 
1348*7c478bd9Sstevel@tonic-gate 	switch (inst->cur_istate) {
1349*7c478bd9Sstevel@tonic-gate 	case IIS_MAINTENANCE:
1350*7c478bd9Sstevel@tonic-gate 	case IIS_DISABLED:
1351*7c478bd9Sstevel@tonic-gate 	case IIS_UNINITIALIZED:
1352*7c478bd9Sstevel@tonic-gate 		/*
1353*7c478bd9Sstevel@tonic-gate 		 * Ignore any possible changes, we'll re-read the configuration
1354*7c478bd9Sstevel@tonic-gate 		 * automatically when we exit these states.
1355*7c478bd9Sstevel@tonic-gate 		 */
1356*7c478bd9Sstevel@tonic-gate 		break;
1357*7c478bd9Sstevel@tonic-gate 
1358*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_COPIES:
1359*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
1360*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE:
1361*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_CONRATE:
1362*7c478bd9Sstevel@tonic-gate 		destroy_instance_cfg(inst->config);
1363*7c478bd9Sstevel@tonic-gate 		if ((inst->config = read_instance_cfg(inst->fmri)) == NULL) {
1364*7c478bd9Sstevel@tonic-gate 			log_invalid_cfg(inst->fmri);
1365*7c478bd9Sstevel@tonic-gate 			if (inst->cur_istate == IIS_OFFLINE_BIND) {
1366*7c478bd9Sstevel@tonic-gate 				cancel_bind_timer(inst);
1367*7c478bd9Sstevel@tonic-gate 			} else if (inst->cur_istate == IIS_OFFLINE_CONRATE) {
1368*7c478bd9Sstevel@tonic-gate 				cancel_inst_timer(inst);
1369*7c478bd9Sstevel@tonic-gate 			}
1370*7c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_MAINTENANCE, RERR_FAULT);
1371*7c478bd9Sstevel@tonic-gate 		} else {
1372*7c478bd9Sstevel@tonic-gate 			switch (inst->cur_istate) {
1373*7c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE_BIND:
1374*7c478bd9Sstevel@tonic-gate 				if (copies_limit_exceeded(inst)) {
1375*7c478bd9Sstevel@tonic-gate 					/* Cancel scheduled bind retries. */
1376*7c478bd9Sstevel@tonic-gate 					cancel_bind_timer(inst);
1377*7c478bd9Sstevel@tonic-gate 
1378*7c478bd9Sstevel@tonic-gate 					/*
1379*7c478bd9Sstevel@tonic-gate 					 * Take the instance to the copies
1380*7c478bd9Sstevel@tonic-gate 					 * offline state, via the offline
1381*7c478bd9Sstevel@tonic-gate 					 * state.
1382*7c478bd9Sstevel@tonic-gate 					 */
1383*7c478bd9Sstevel@tonic-gate 					update_state(inst, IIS_OFFLINE,
1384*7c478bd9Sstevel@tonic-gate 					    RERR_RESTART);
1385*7c478bd9Sstevel@tonic-gate 					process_offline_inst(inst);
1386*7c478bd9Sstevel@tonic-gate 				}
1387*7c478bd9Sstevel@tonic-gate 				break;
1388*7c478bd9Sstevel@tonic-gate 
1389*7c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE:
1390*7c478bd9Sstevel@tonic-gate 				process_offline_inst(inst);
1391*7c478bd9Sstevel@tonic-gate 				break;
1392*7c478bd9Sstevel@tonic-gate 
1393*7c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE_CONRATE:
1394*7c478bd9Sstevel@tonic-gate 				/*
1395*7c478bd9Sstevel@tonic-gate 				 * Since we're already in a DOS state,
1396*7c478bd9Sstevel@tonic-gate 				 * don't bother evaluating the copies
1397*7c478bd9Sstevel@tonic-gate 				 * limit. This will be evaluated when
1398*7c478bd9Sstevel@tonic-gate 				 * we leave this state in
1399*7c478bd9Sstevel@tonic-gate 				 * process_offline_inst().
1400*7c478bd9Sstevel@tonic-gate 				 */
1401*7c478bd9Sstevel@tonic-gate 				break;
1402*7c478bd9Sstevel@tonic-gate 
1403*7c478bd9Sstevel@tonic-gate 			case IIS_OFFLINE_COPIES:
1404*7c478bd9Sstevel@tonic-gate 				/*
1405*7c478bd9Sstevel@tonic-gate 				 * Check if the copies limit has been increased
1406*7c478bd9Sstevel@tonic-gate 				 * above the current count.
1407*7c478bd9Sstevel@tonic-gate 				 */
1408*7c478bd9Sstevel@tonic-gate 				if (!copies_limit_exceeded(inst)) {
1409*7c478bd9Sstevel@tonic-gate 					update_state(inst, IIS_OFFLINE,
1410*7c478bd9Sstevel@tonic-gate 					    RERR_RESTART);
1411*7c478bd9Sstevel@tonic-gate 					process_offline_inst(inst);
1412*7c478bd9Sstevel@tonic-gate 				}
1413*7c478bd9Sstevel@tonic-gate 				break;
1414*7c478bd9Sstevel@tonic-gate 
1415*7c478bd9Sstevel@tonic-gate 			default:
1416*7c478bd9Sstevel@tonic-gate 				assert(0);
1417*7c478bd9Sstevel@tonic-gate 			}
1418*7c478bd9Sstevel@tonic-gate 		}
1419*7c478bd9Sstevel@tonic-gate 		break;
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
1422*7c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
1423*7c478bd9Sstevel@tonic-gate 		if ((cfg = read_instance_cfg(inst->fmri)) != NULL) {
1424*7c478bd9Sstevel@tonic-gate 			instance_cfg_t *ocfg = inst->config;
1425*7c478bd9Sstevel@tonic-gate 
1426*7c478bd9Sstevel@tonic-gate 			/*
1427*7c478bd9Sstevel@tonic-gate 			 * Try to avoid the overhead of taking an instance
1428*7c478bd9Sstevel@tonic-gate 			 * offline and back on again. We do this by limiting
1429*7c478bd9Sstevel@tonic-gate 			 * this behavior to two eventualities:
1430*7c478bd9Sstevel@tonic-gate 			 * - there needs to be a re-bind to listen on behalf
1431*7c478bd9Sstevel@tonic-gate 			 *   of the instance with its new configuration. This
1432*7c478bd9Sstevel@tonic-gate 			 *   could be because for example its service has been
1433*7c478bd9Sstevel@tonic-gate 			 *   associated with a different port, or because the
1434*7c478bd9Sstevel@tonic-gate 			 *   v6only protocol option has been newly applied to
1435*7c478bd9Sstevel@tonic-gate 			 *   the instance.
1436*7c478bd9Sstevel@tonic-gate 			 * - one or both of the start or online methods of the
1437*7c478bd9Sstevel@tonic-gate 			 *   instance have changed in the new configuration.
1438*7c478bd9Sstevel@tonic-gate 			 *   Without taking the instance offline when the
1439*7c478bd9Sstevel@tonic-gate 			 *   start method changed the instance may be running
1440*7c478bd9Sstevel@tonic-gate 			 *   with unwanted parameters (or event an unwanted
1441*7c478bd9Sstevel@tonic-gate 			 *   binary); and without taking the instance offline
1442*7c478bd9Sstevel@tonic-gate 			 *   if its online method was to change, some part of
1443*7c478bd9Sstevel@tonic-gate 			 *   its running environment may have changed and would
1444*7c478bd9Sstevel@tonic-gate 			 *   not be picked up until the instance next goes
1445*7c478bd9Sstevel@tonic-gate 			 *   offline for another reason.
1446*7c478bd9Sstevel@tonic-gate 			 */
1447*7c478bd9Sstevel@tonic-gate 			if ((!bind_config_equal(ocfg->basic, cfg->basic)) ||
1448*7c478bd9Sstevel@tonic-gate 			    !method_info_equal(ocfg->methods[IM_ONLINE],
1449*7c478bd9Sstevel@tonic-gate 			    cfg->methods[IM_ONLINE]) ||
1450*7c478bd9Sstevel@tonic-gate 			    !method_info_equal(ocfg->methods[IM_START],
1451*7c478bd9Sstevel@tonic-gate 			    cfg->methods[IM_START])) {
1452*7c478bd9Sstevel@tonic-gate 				destroy_bound_fds(inst);
1453*7c478bd9Sstevel@tonic-gate 
1454*7c478bd9Sstevel@tonic-gate 				assert(inst->new_config == NULL);
1455*7c478bd9Sstevel@tonic-gate 				inst->new_config = cfg;
1456*7c478bd9Sstevel@tonic-gate 
1457*7c478bd9Sstevel@tonic-gate 				(void) run_method(inst, IM_OFFLINE, NULL);
1458*7c478bd9Sstevel@tonic-gate 			} else {	/* no bind config / method changes */
1459*7c478bd9Sstevel@tonic-gate 
1460*7c478bd9Sstevel@tonic-gate 				/*
1461*7c478bd9Sstevel@tonic-gate 				 * swap the proto list over from the old
1462*7c478bd9Sstevel@tonic-gate 				 * configuration to the new, so we retain
1463*7c478bd9Sstevel@tonic-gate 				 * our set of network fds.
1464*7c478bd9Sstevel@tonic-gate 				 */
1465*7c478bd9Sstevel@tonic-gate 				destroy_proto_list(cfg->basic);
1466*7c478bd9Sstevel@tonic-gate 				cfg->basic->proto_list =
1467*7c478bd9Sstevel@tonic-gate 				    ocfg->basic->proto_list;
1468*7c478bd9Sstevel@tonic-gate 				ocfg->basic->proto_list = NULL;
1469*7c478bd9Sstevel@tonic-gate 				destroy_instance_cfg(ocfg);
1470*7c478bd9Sstevel@tonic-gate 				inst->config = cfg;
1471*7c478bd9Sstevel@tonic-gate 
1472*7c478bd9Sstevel@tonic-gate 				/* re-evaluate copies limits based on new cfg */
1473*7c478bd9Sstevel@tonic-gate 				if (copies_limit_exceeded(inst)) {
1474*7c478bd9Sstevel@tonic-gate 					destroy_bound_fds(inst);
1475*7c478bd9Sstevel@tonic-gate 					(void) run_method(inst, IM_OFFLINE,
1476*7c478bd9Sstevel@tonic-gate 					    NULL);
1477*7c478bd9Sstevel@tonic-gate 				} else {
1478*7c478bd9Sstevel@tonic-gate 					/*
1479*7c478bd9Sstevel@tonic-gate 					 * Since the instance isn't being
1480*7c478bd9Sstevel@tonic-gate 					 * taken offline, where we assume it
1481*7c478bd9Sstevel@tonic-gate 					 * would pick-up any configuration
1482*7c478bd9Sstevel@tonic-gate 					 * changes automatically when it goes
1483*7c478bd9Sstevel@tonic-gate 					 * back online, run its refresh method
1484*7c478bd9Sstevel@tonic-gate 					 * to allow it to pick-up any changes
1485*7c478bd9Sstevel@tonic-gate 					 * whilst still online.
1486*7c478bd9Sstevel@tonic-gate 					 */
1487*7c478bd9Sstevel@tonic-gate 					(void) run_method(inst, IM_REFRESH,
1488*7c478bd9Sstevel@tonic-gate 					    NULL);
1489*7c478bd9Sstevel@tonic-gate 				}
1490*7c478bd9Sstevel@tonic-gate 			}
1491*7c478bd9Sstevel@tonic-gate 		} else {
1492*7c478bd9Sstevel@tonic-gate 			log_invalid_cfg(inst->fmri);
1493*7c478bd9Sstevel@tonic-gate 
1494*7c478bd9Sstevel@tonic-gate 			destroy_bound_fds(inst);
1495*7c478bd9Sstevel@tonic-gate 
1496*7c478bd9Sstevel@tonic-gate 			inst->maintenance_req = B_TRUE;
1497*7c478bd9Sstevel@tonic-gate 			(void) run_method(inst, IM_OFFLINE, NULL);
1498*7c478bd9Sstevel@tonic-gate 		}
1499*7c478bd9Sstevel@tonic-gate 		break;
1500*7c478bd9Sstevel@tonic-gate 
1501*7c478bd9Sstevel@tonic-gate 	default:
1502*7c478bd9Sstevel@tonic-gate 		debug_msg("Unhandled current state %d for instance in "
1503*7c478bd9Sstevel@tonic-gate 		    "refresh_instance", inst->cur_istate);
1504*7c478bd9Sstevel@tonic-gate 		assert(0);
1505*7c478bd9Sstevel@tonic-gate 	}
1506*7c478bd9Sstevel@tonic-gate }
1507*7c478bd9Sstevel@tonic-gate 
1508*7c478bd9Sstevel@tonic-gate /*
1509*7c478bd9Sstevel@tonic-gate  * Called by process_restarter_event() to handle a restarter event for an
1510*7c478bd9Sstevel@tonic-gate  * instance.
1511*7c478bd9Sstevel@tonic-gate  */
1512*7c478bd9Sstevel@tonic-gate static void
1513*7c478bd9Sstevel@tonic-gate handle_restarter_event(instance_t *instance, restarter_event_type_t event,
1514*7c478bd9Sstevel@tonic-gate     boolean_t send_ack)
1515*7c478bd9Sstevel@tonic-gate {
1516*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering handle_restarter_event: inst: %s, event: %d, "
1517*7c478bd9Sstevel@tonic-gate 	    "curr state: %d", instance->fmri, event, instance->cur_istate);
1518*7c478bd9Sstevel@tonic-gate 
1519*7c478bd9Sstevel@tonic-gate 	switch (event) {
1520*7c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
1521*7c478bd9Sstevel@tonic-gate 		refresh_instance(instance);
1522*7c478bd9Sstevel@tonic-gate 		goto done;
1523*7c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
1524*7c478bd9Sstevel@tonic-gate 		remove_instance(instance);
1525*7c478bd9Sstevel@tonic-gate 		goto done;
1526*7c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
1527*7c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
1528*7c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_CONRATE:
1529*7c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_BIND:
1530*7c478bd9Sstevel@tonic-gate 		case IIS_OFFLINE_COPIES:
1531*7c478bd9Sstevel@tonic-gate 			/*
1532*7c478bd9Sstevel@tonic-gate 			 * inetd must be closing down as we wouldn't get this
1533*7c478bd9Sstevel@tonic-gate 			 * event in one of these states from the master
1534*7c478bd9Sstevel@tonic-gate 			 * restarter. Take the instance to the offline resting
1535*7c478bd9Sstevel@tonic-gate 			 * state.
1536*7c478bd9Sstevel@tonic-gate 			 */
1537*7c478bd9Sstevel@tonic-gate 			if (instance->cur_istate == IIS_OFFLINE_BIND) {
1538*7c478bd9Sstevel@tonic-gate 				cancel_bind_timer(instance);
1539*7c478bd9Sstevel@tonic-gate 			} else if (instance->cur_istate ==
1540*7c478bd9Sstevel@tonic-gate 			    IIS_OFFLINE_CONRATE) {
1541*7c478bd9Sstevel@tonic-gate 				cancel_inst_timer(instance);
1542*7c478bd9Sstevel@tonic-gate 			}
1543*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
1544*7c478bd9Sstevel@tonic-gate 			goto done;
1545*7c478bd9Sstevel@tonic-gate 		}
1546*7c478bd9Sstevel@tonic-gate 		break;
1547*7c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
1548*7c478bd9Sstevel@tonic-gate 		/*
1549*7c478bd9Sstevel@tonic-gate 		 * We've got a restart event, so if the instance is online
1550*7c478bd9Sstevel@tonic-gate 		 * in any way initiate taking it offline, and rely upon
1551*7c478bd9Sstevel@tonic-gate 		 * our restarter to send us an online event to bring
1552*7c478bd9Sstevel@tonic-gate 		 * it back online.
1553*7c478bd9Sstevel@tonic-gate 		 */
1554*7c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
1555*7c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
1556*7c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
1557*7c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
1558*7c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_OFFLINE, NULL);
1559*7c478bd9Sstevel@tonic-gate 		}
1560*7c478bd9Sstevel@tonic-gate 		goto done;
1561*7c478bd9Sstevel@tonic-gate 	}
1562*7c478bd9Sstevel@tonic-gate 
1563*7c478bd9Sstevel@tonic-gate 	switch (instance->cur_istate) {
1564*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE:
1565*7c478bd9Sstevel@tonic-gate 		switch (event) {
1566*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_START:
1567*7c478bd9Sstevel@tonic-gate 			/*
1568*7c478bd9Sstevel@tonic-gate 			 * Dependencies are met, let's take the service online.
1569*7c478bd9Sstevel@tonic-gate 			 * Only try and bind for a wait type service if
1570*7c478bd9Sstevel@tonic-gate 			 * no process is running on its behalf. Otherwise, just
1571*7c478bd9Sstevel@tonic-gate 			 * mark the service online and binding will be attempted
1572*7c478bd9Sstevel@tonic-gate 			 * when the process exits.
1573*7c478bd9Sstevel@tonic-gate 			 */
1574*7c478bd9Sstevel@tonic-gate 			if (!(instance->config->basic->iswait &&
1575*7c478bd9Sstevel@tonic-gate 			    (uu_list_first(instance->start_pids) != NULL))) {
1576*7c478bd9Sstevel@tonic-gate 				create_bound_fds(instance);
1577*7c478bd9Sstevel@tonic-gate 			} else {
1578*7c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_ONLINE, RERR_NONE);
1579*7c478bd9Sstevel@tonic-gate 			}
1580*7c478bd9Sstevel@tonic-gate 			break;
1581*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
1582*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1583*7c478bd9Sstevel@tonic-gate 			/*
1584*7c478bd9Sstevel@tonic-gate 			 * The instance should be disabled, so run the
1585*7c478bd9Sstevel@tonic-gate 			 * instance's disabled method that will do the work
1586*7c478bd9Sstevel@tonic-gate 			 * to take it there.
1587*7c478bd9Sstevel@tonic-gate 			 */
1588*7c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
1589*7c478bd9Sstevel@tonic-gate 			break;
1590*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1591*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1592*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1593*7c478bd9Sstevel@tonic-gate 			/*
1594*7c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance
1595*7c478bd9Sstevel@tonic-gate 			 * go to maintenance; since we're already offline
1596*7c478bd9Sstevel@tonic-gate 			 * just update the state to the maintenance state.
1597*7c478bd9Sstevel@tonic-gate 			 */
1598*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1599*7c478bd9Sstevel@tonic-gate 			break;
1600*7c478bd9Sstevel@tonic-gate 		}
1601*7c478bd9Sstevel@tonic-gate 		break;
1602*7c478bd9Sstevel@tonic-gate 
1603*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_BIND:
1604*7c478bd9Sstevel@tonic-gate 		switch (event) {
1605*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
1606*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1607*7c478bd9Sstevel@tonic-gate 			/*
1608*7c478bd9Sstevel@tonic-gate 			 * The instance should be disabled. Firstly, as for
1609*7c478bd9Sstevel@tonic-gate 			 * the above dependencies unmet comment, cancel
1610*7c478bd9Sstevel@tonic-gate 			 * the bind retry timer and update the state to
1611*7c478bd9Sstevel@tonic-gate 			 * offline. Then, run the disable method to do the
1612*7c478bd9Sstevel@tonic-gate 			 * work to take the instance from offline to
1613*7c478bd9Sstevel@tonic-gate 			 * disabled.
1614*7c478bd9Sstevel@tonic-gate 			 */
1615*7c478bd9Sstevel@tonic-gate 			cancel_bind_timer(instance);
1616*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
1617*7c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
1618*7c478bd9Sstevel@tonic-gate 			break;
1619*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1620*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1621*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1622*7c478bd9Sstevel@tonic-gate 			/*
1623*7c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance
1624*7c478bd9Sstevel@tonic-gate 			 * be placed in the maintenance state. Cancel the
1625*7c478bd9Sstevel@tonic-gate 			 * outstanding retry timer, and since we're already
1626*7c478bd9Sstevel@tonic-gate 			 * offline, update the state to maintenance.
1627*7c478bd9Sstevel@tonic-gate 			 */
1628*7c478bd9Sstevel@tonic-gate 			cancel_bind_timer(instance);
1629*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1630*7c478bd9Sstevel@tonic-gate 			break;
1631*7c478bd9Sstevel@tonic-gate 		}
1632*7c478bd9Sstevel@tonic-gate 		break;
1633*7c478bd9Sstevel@tonic-gate 
1634*7c478bd9Sstevel@tonic-gate 	case IIS_DEGRADED:
1635*7c478bd9Sstevel@tonic-gate 	case IIS_ONLINE:
1636*7c478bd9Sstevel@tonic-gate 		switch (event) {
1637*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
1638*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1639*7c478bd9Sstevel@tonic-gate 			/*
1640*7c478bd9Sstevel@tonic-gate 			 * The instance needs to be disabled. Do the same work
1641*7c478bd9Sstevel@tonic-gate 			 * as for the dependencies unmet event below to
1642*7c478bd9Sstevel@tonic-gate 			 * take the instance offline.
1643*7c478bd9Sstevel@tonic-gate 			 */
1644*7c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
1645*7c478bd9Sstevel@tonic-gate 			/*
1646*7c478bd9Sstevel@tonic-gate 			 * Indicate that the offline method is being run
1647*7c478bd9Sstevel@tonic-gate 			 * as part of going to the disabled state, and to
1648*7c478bd9Sstevel@tonic-gate 			 * carry on this transition.
1649*7c478bd9Sstevel@tonic-gate 			 */
1650*7c478bd9Sstevel@tonic-gate 			instance->disable_req = B_TRUE;
1651*7c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_OFFLINE, NULL);
1652*7c478bd9Sstevel@tonic-gate 			break;
1653*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1654*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1655*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1656*7c478bd9Sstevel@tonic-gate 			/*
1657*7c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance be
1658*7c478bd9Sstevel@tonic-gate 			 * placed in the maintenance state. This involves
1659*7c478bd9Sstevel@tonic-gate 			 * firstly taking the service offline, so do the
1660*7c478bd9Sstevel@tonic-gate 			 * same work as for the dependencies unmet event
1661*7c478bd9Sstevel@tonic-gate 			 * below. We set the maintenance_req flag to
1662*7c478bd9Sstevel@tonic-gate 			 * indicate that when we get to the offline state
1663*7c478bd9Sstevel@tonic-gate 			 * we should be placed directly into the maintenance
1664*7c478bd9Sstevel@tonic-gate 			 * state.
1665*7c478bd9Sstevel@tonic-gate 			 */
1666*7c478bd9Sstevel@tonic-gate 			instance->maintenance_req = B_TRUE;
1667*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
1668*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_STOP:
1669*7c478bd9Sstevel@tonic-gate 			/*
1670*7c478bd9Sstevel@tonic-gate 			 * Dependencies have become unmet. Close and
1671*7c478bd9Sstevel@tonic-gate 			 * stop listening on the instance's network file
1672*7c478bd9Sstevel@tonic-gate 			 * descriptor, and run the offline method to do
1673*7c478bd9Sstevel@tonic-gate 			 * any work required to take us to the offline state.
1674*7c478bd9Sstevel@tonic-gate 			 */
1675*7c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
1676*7c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_OFFLINE, NULL);
1677*7c478bd9Sstevel@tonic-gate 		}
1678*7c478bd9Sstevel@tonic-gate 		break;
1679*7c478bd9Sstevel@tonic-gate 
1680*7c478bd9Sstevel@tonic-gate 	case IIS_UNINITIALIZED:
1681*7c478bd9Sstevel@tonic-gate 		if (event == RESTARTER_EVENT_TYPE_DISABLE ||
1682*7c478bd9Sstevel@tonic-gate 		    event == RESTARTER_EVENT_TYPE_ADMIN_DISABLE) {
1683*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_DISABLED, RERR_NONE);
1684*7c478bd9Sstevel@tonic-gate 			break;
1685*7c478bd9Sstevel@tonic-gate 		} else if (event != RESTARTER_EVENT_TYPE_ENABLE) {
1686*7c478bd9Sstevel@tonic-gate 			/*
1687*7c478bd9Sstevel@tonic-gate 			 * Ignore other events until we know whether we're
1688*7c478bd9Sstevel@tonic-gate 			 * enabled or not.
1689*7c478bd9Sstevel@tonic-gate 			 */
1690*7c478bd9Sstevel@tonic-gate 			break;
1691*7c478bd9Sstevel@tonic-gate 		}
1692*7c478bd9Sstevel@tonic-gate 
1693*7c478bd9Sstevel@tonic-gate 		/*
1694*7c478bd9Sstevel@tonic-gate 		 * We've got an enabled event; make use of the handling in the
1695*7c478bd9Sstevel@tonic-gate 		 * disable case.
1696*7c478bd9Sstevel@tonic-gate 		 */
1697*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1698*7c478bd9Sstevel@tonic-gate 
1699*7c478bd9Sstevel@tonic-gate 	case IIS_DISABLED:
1700*7c478bd9Sstevel@tonic-gate 		switch (event) {
1701*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ENABLE:
1702*7c478bd9Sstevel@tonic-gate 			/*
1703*7c478bd9Sstevel@tonic-gate 			 * The instance needs enabling. Commence reading its
1704*7c478bd9Sstevel@tonic-gate 			 * configuration and if successful place the instance
1705*7c478bd9Sstevel@tonic-gate 			 * in the offline state and let process_offline_inst()
1706*7c478bd9Sstevel@tonic-gate 			 * take it from there.
1707*7c478bd9Sstevel@tonic-gate 			 */
1708*7c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(instance->config);
1709*7c478bd9Sstevel@tonic-gate 			instance->config = read_instance_cfg(instance->fmri);
1710*7c478bd9Sstevel@tonic-gate 			if (instance->config != NULL) {
1711*7c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_OFFLINE,
1712*7c478bd9Sstevel@tonic-gate 				    RERR_RESTART);
1713*7c478bd9Sstevel@tonic-gate 				process_offline_inst(instance);
1714*7c478bd9Sstevel@tonic-gate 			} else {
1715*7c478bd9Sstevel@tonic-gate 				log_invalid_cfg(instance->fmri);
1716*7c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_MAINTENANCE,
1717*7c478bd9Sstevel@tonic-gate 				    RERR_RESTART);
1718*7c478bd9Sstevel@tonic-gate 			}
1719*7c478bd9Sstevel@tonic-gate 
1720*7c478bd9Sstevel@tonic-gate 			break;
1721*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1722*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1723*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1724*7c478bd9Sstevel@tonic-gate 			/*
1725*7c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance be
1726*7c478bd9Sstevel@tonic-gate 			 * placed in the maintenance state, so just update its
1727*7c478bd9Sstevel@tonic-gate 			 * state to maintenance.
1728*7c478bd9Sstevel@tonic-gate 			 */
1729*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1730*7c478bd9Sstevel@tonic-gate 			break;
1731*7c478bd9Sstevel@tonic-gate 		}
1732*7c478bd9Sstevel@tonic-gate 		break;
1733*7c478bd9Sstevel@tonic-gate 
1734*7c478bd9Sstevel@tonic-gate 	case IIS_MAINTENANCE:
1735*7c478bd9Sstevel@tonic-gate 		switch (event) {
1736*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
1737*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1738*7c478bd9Sstevel@tonic-gate 			/*
1739*7c478bd9Sstevel@tonic-gate 			 * The master restarter has requested that the instance
1740*7c478bd9Sstevel@tonic-gate 			 * be taken out of maintenance. Read its configuration,
1741*7c478bd9Sstevel@tonic-gate 			 * and if successful place the instance in the offline
1742*7c478bd9Sstevel@tonic-gate 			 * state and call process_offline_inst() to take it
1743*7c478bd9Sstevel@tonic-gate 			 * from there.
1744*7c478bd9Sstevel@tonic-gate 			 */
1745*7c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(instance->config);
1746*7c478bd9Sstevel@tonic-gate 			instance->config = read_instance_cfg(instance->fmri);
1747*7c478bd9Sstevel@tonic-gate 			if (instance->config != NULL) {
1748*7c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_OFFLINE,
1749*7c478bd9Sstevel@tonic-gate 				    RERR_RESTART);
1750*7c478bd9Sstevel@tonic-gate 				process_offline_inst(instance);
1751*7c478bd9Sstevel@tonic-gate 			} else {
1752*7c478bd9Sstevel@tonic-gate 				boolean_t enabled;
1753*7c478bd9Sstevel@tonic-gate 
1754*7c478bd9Sstevel@tonic-gate 				/*
1755*7c478bd9Sstevel@tonic-gate 				 * The configuration was invalid. If the
1756*7c478bd9Sstevel@tonic-gate 				 * service has disabled requested, let's
1757*7c478bd9Sstevel@tonic-gate 				 * just place the instance in disabled even
1758*7c478bd9Sstevel@tonic-gate 				 * though we haven't been able to run its
1759*7c478bd9Sstevel@tonic-gate 				 * disable method, as the slightly incorrect
1760*7c478bd9Sstevel@tonic-gate 				 * state is likely to be less of an issue to
1761*7c478bd9Sstevel@tonic-gate 				 * an administrator than refusing to move an
1762*7c478bd9Sstevel@tonic-gate 				 * instance to disabled. If disable isn't
1763*7c478bd9Sstevel@tonic-gate 				 * requested, re-mark the service's state
1764*7c478bd9Sstevel@tonic-gate 				 * as maintenance, so the administrator can
1765*7c478bd9Sstevel@tonic-gate 				 * see the request was processed.
1766*7c478bd9Sstevel@tonic-gate 				 */
1767*7c478bd9Sstevel@tonic-gate 				if ((read_enable_merged(instance->fmri,
1768*7c478bd9Sstevel@tonic-gate 				    &enabled) == 0) && !enabled) {
1769*7c478bd9Sstevel@tonic-gate 					update_state(instance, IIS_DISABLED,
1770*7c478bd9Sstevel@tonic-gate 					    RERR_RESTART);
1771*7c478bd9Sstevel@tonic-gate 				} else {
1772*7c478bd9Sstevel@tonic-gate 					log_invalid_cfg(instance->fmri);
1773*7c478bd9Sstevel@tonic-gate 					update_state(instance, IIS_MAINTENANCE,
1774*7c478bd9Sstevel@tonic-gate 					    RERR_FAULT);
1775*7c478bd9Sstevel@tonic-gate 				}
1776*7c478bd9Sstevel@tonic-gate 			}
1777*7c478bd9Sstevel@tonic-gate 			break;
1778*7c478bd9Sstevel@tonic-gate 		}
1779*7c478bd9Sstevel@tonic-gate 		break;
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_CONRATE:
1782*7c478bd9Sstevel@tonic-gate 		switch (event) {
1783*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
1784*7c478bd9Sstevel@tonic-gate 			/*
1785*7c478bd9Sstevel@tonic-gate 			 * The instance wants disabling. Take the instance
1786*7c478bd9Sstevel@tonic-gate 			 * offline as for the dependencies unmet event above,
1787*7c478bd9Sstevel@tonic-gate 			 * and then from there run the disable method to do
1788*7c478bd9Sstevel@tonic-gate 			 * the work to take the instance to the disabled state.
1789*7c478bd9Sstevel@tonic-gate 			 */
1790*7c478bd9Sstevel@tonic-gate 			cancel_inst_timer(instance);
1791*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
1792*7c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
1793*7c478bd9Sstevel@tonic-gate 			break;
1794*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1795*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1796*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1797*7c478bd9Sstevel@tonic-gate 			/*
1798*7c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance
1799*7c478bd9Sstevel@tonic-gate 			 * be taken to maintenance. Cancel the timer setup
1800*7c478bd9Sstevel@tonic-gate 			 * when we entered this state, and go directly to
1801*7c478bd9Sstevel@tonic-gate 			 * maintenance.
1802*7c478bd9Sstevel@tonic-gate 			 */
1803*7c478bd9Sstevel@tonic-gate 			cancel_inst_timer(instance);
1804*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1805*7c478bd9Sstevel@tonic-gate 			break;
1806*7c478bd9Sstevel@tonic-gate 		}
1807*7c478bd9Sstevel@tonic-gate 		break;
1808*7c478bd9Sstevel@tonic-gate 
1809*7c478bd9Sstevel@tonic-gate 	case IIS_OFFLINE_COPIES:
1810*7c478bd9Sstevel@tonic-gate 		switch (event) {
1811*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
1812*7c478bd9Sstevel@tonic-gate 			/*
1813*7c478bd9Sstevel@tonic-gate 			 * The instance wants disabling. Update the state
1814*7c478bd9Sstevel@tonic-gate 			 * to offline, and run the disable method to do the
1815*7c478bd9Sstevel@tonic-gate 			 * work to take it to the disabled state.
1816*7c478bd9Sstevel@tonic-gate 			 */
1817*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
1818*7c478bd9Sstevel@tonic-gate 			(void) run_method(instance, IM_DISABLE, NULL);
1819*7c478bd9Sstevel@tonic-gate 			break;
1820*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1821*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1822*7c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1823*7c478bd9Sstevel@tonic-gate 			/*
1824*7c478bd9Sstevel@tonic-gate 			 * The master restarter has requested the instance be
1825*7c478bd9Sstevel@tonic-gate 			 * placed in maintenance. Since it's already offline
1826*7c478bd9Sstevel@tonic-gate 			 * simply update the state.
1827*7c478bd9Sstevel@tonic-gate 			 */
1828*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1829*7c478bd9Sstevel@tonic-gate 			break;
1830*7c478bd9Sstevel@tonic-gate 		}
1831*7c478bd9Sstevel@tonic-gate 		break;
1832*7c478bd9Sstevel@tonic-gate 
1833*7c478bd9Sstevel@tonic-gate 	default:
1834*7c478bd9Sstevel@tonic-gate 		debug_msg("handle_restarter_event: instance in an "
1835*7c478bd9Sstevel@tonic-gate 		    "unexpected state");
1836*7c478bd9Sstevel@tonic-gate 		assert(0);
1837*7c478bd9Sstevel@tonic-gate 	}
1838*7c478bd9Sstevel@tonic-gate 
1839*7c478bd9Sstevel@tonic-gate done:
1840*7c478bd9Sstevel@tonic-gate 	if (send_ack)
1841*7c478bd9Sstevel@tonic-gate 		ack_restarter_event(B_TRUE);
1842*7c478bd9Sstevel@tonic-gate }
1843*7c478bd9Sstevel@tonic-gate 
1844*7c478bd9Sstevel@tonic-gate /*
1845*7c478bd9Sstevel@tonic-gate  * Tries to read and process an event from the event pipe. If there isn't one
1846*7c478bd9Sstevel@tonic-gate  * or an error occurred processing the event it returns -1. Else, if the event
1847*7c478bd9Sstevel@tonic-gate  * is for an instance we're not already managing we read its state, add it to
1848*7c478bd9Sstevel@tonic-gate  * our list to manage, and if appropriate read its configuration. Whether it's
1849*7c478bd9Sstevel@tonic-gate  * new to us or not, we then handle the specific event.
1850*7c478bd9Sstevel@tonic-gate  * Returns 0 if an event was read and processed successfully, else -1.
1851*7c478bd9Sstevel@tonic-gate  */
1852*7c478bd9Sstevel@tonic-gate static int
1853*7c478bd9Sstevel@tonic-gate process_restarter_event(void)
1854*7c478bd9Sstevel@tonic-gate {
1855*7c478bd9Sstevel@tonic-gate 	char			*fmri;
1856*7c478bd9Sstevel@tonic-gate 	size_t			fmri_size;
1857*7c478bd9Sstevel@tonic-gate 	restarter_event_type_t  event_type;
1858*7c478bd9Sstevel@tonic-gate 	instance_t		*instance;
1859*7c478bd9Sstevel@tonic-gate 	restarter_event_t	*event;
1860*7c478bd9Sstevel@tonic-gate 	ssize_t			sz;
1861*7c478bd9Sstevel@tonic-gate 
1862*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_restarter_event");
1863*7c478bd9Sstevel@tonic-gate 
1864*7c478bd9Sstevel@tonic-gate 	/*
1865*7c478bd9Sstevel@tonic-gate 	 * Try to read an event pointer from the event pipe.
1866*7c478bd9Sstevel@tonic-gate 	 */
1867*7c478bd9Sstevel@tonic-gate 	errno = 0;
1868*7c478bd9Sstevel@tonic-gate 	switch (safe_read(rst_event_pipe[PE_CONSUMER], &event,
1869*7c478bd9Sstevel@tonic-gate 	    sizeof (event))) {
1870*7c478bd9Sstevel@tonic-gate 	case 0:
1871*7c478bd9Sstevel@tonic-gate 		break;
1872*7c478bd9Sstevel@tonic-gate 	case  1:
1873*7c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN)	/* no event to read */
1874*7c478bd9Sstevel@tonic-gate 			return (-1);
1875*7c478bd9Sstevel@tonic-gate 
1876*7c478bd9Sstevel@tonic-gate 		/* other end of pipe closed */
1877*7c478bd9Sstevel@tonic-gate 
1878*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1879*7c478bd9Sstevel@tonic-gate 	default:			/* unexpected read error */
1880*7c478bd9Sstevel@tonic-gate 		/*
1881*7c478bd9Sstevel@tonic-gate 		 * There's something wrong with the event pipe. Let's
1882*7c478bd9Sstevel@tonic-gate 		 * shutdown and be restarted.
1883*7c478bd9Sstevel@tonic-gate 		 */
1884*7c478bd9Sstevel@tonic-gate 		inetd_stop();
1885*7c478bd9Sstevel@tonic-gate 		return (-1);
1886*7c478bd9Sstevel@tonic-gate 	}
1887*7c478bd9Sstevel@tonic-gate 
1888*7c478bd9Sstevel@tonic-gate 	/*
1889*7c478bd9Sstevel@tonic-gate 	 * Check if we're currently managing the instance which the event
1890*7c478bd9Sstevel@tonic-gate 	 * pertains to. If not, read its complete state and add it to our
1891*7c478bd9Sstevel@tonic-gate 	 * list to manage.
1892*7c478bd9Sstevel@tonic-gate 	 */
1893*7c478bd9Sstevel@tonic-gate 
1894*7c478bd9Sstevel@tonic-gate 	fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
1895*7c478bd9Sstevel@tonic-gate 	if ((fmri = malloc(fmri_size)) == NULL) {
1896*7c478bd9Sstevel@tonic-gate 		error_msg(strerror(errno));
1897*7c478bd9Sstevel@tonic-gate 		goto fail;
1898*7c478bd9Sstevel@tonic-gate 	}
1899*7c478bd9Sstevel@tonic-gate 	sz = restarter_event_get_instance(event, fmri, fmri_size);
1900*7c478bd9Sstevel@tonic-gate 	if (sz >= fmri_size)
1901*7c478bd9Sstevel@tonic-gate 		assert(0);
1902*7c478bd9Sstevel@tonic-gate 
1903*7c478bd9Sstevel@tonic-gate 	for (instance = uu_list_first(instance_list); instance != NULL;
1904*7c478bd9Sstevel@tonic-gate 	    instance = uu_list_next(instance_list, instance)) {
1905*7c478bd9Sstevel@tonic-gate 		if (strcmp(instance->fmri, fmri) == 0)
1906*7c478bd9Sstevel@tonic-gate 			break;
1907*7c478bd9Sstevel@tonic-gate 	}
1908*7c478bd9Sstevel@tonic-gate 
1909*7c478bd9Sstevel@tonic-gate 	if (instance == NULL) {
1910*7c478bd9Sstevel@tonic-gate 		int err;
1911*7c478bd9Sstevel@tonic-gate 
1912*7c478bd9Sstevel@tonic-gate 		debug_msg("New instance to manage: %s", fmri);
1913*7c478bd9Sstevel@tonic-gate 
1914*7c478bd9Sstevel@tonic-gate 		if (((instance = create_instance(fmri)) == NULL) ||
1915*7c478bd9Sstevel@tonic-gate 		    (retrieve_instance_state(instance) != 0) ||
1916*7c478bd9Sstevel@tonic-gate 		    (retrieve_method_pids(instance) != 0)) {
1917*7c478bd9Sstevel@tonic-gate 			destroy_instance(instance);
1918*7c478bd9Sstevel@tonic-gate 			free(fmri);
1919*7c478bd9Sstevel@tonic-gate 			goto fail;
1920*7c478bd9Sstevel@tonic-gate 		}
1921*7c478bd9Sstevel@tonic-gate 
1922*7c478bd9Sstevel@tonic-gate 		if (((err = iterate_repository_contracts(instance->fmri, 0))
1923*7c478bd9Sstevel@tonic-gate 		    != 0) && (err != ENOENT)) {
1924*7c478bd9Sstevel@tonic-gate 			error_msg(gettext(
1925*7c478bd9Sstevel@tonic-gate 			    "Failed to adopt contracts of instance %s: %s"),
1926*7c478bd9Sstevel@tonic-gate 			    instance->fmri, strerror(err));
1927*7c478bd9Sstevel@tonic-gate 			destroy_instance(instance);
1928*7c478bd9Sstevel@tonic-gate 			free(fmri);
1929*7c478bd9Sstevel@tonic-gate 			goto fail;
1930*7c478bd9Sstevel@tonic-gate 		}
1931*7c478bd9Sstevel@tonic-gate 
1932*7c478bd9Sstevel@tonic-gate 		uu_list_node_init(instance, &instance->link, instance_pool);
1933*7c478bd9Sstevel@tonic-gate 		(void) uu_list_insert_after(instance_list, NULL, instance);
1934*7c478bd9Sstevel@tonic-gate 
1935*7c478bd9Sstevel@tonic-gate 		/*
1936*7c478bd9Sstevel@tonic-gate 		 * Only read configuration for instances that aren't in any of
1937*7c478bd9Sstevel@tonic-gate 		 * the disabled, maintenance or uninitialized states, since
1938*7c478bd9Sstevel@tonic-gate 		 * they'll read it on state exit.
1939*7c478bd9Sstevel@tonic-gate 		 */
1940*7c478bd9Sstevel@tonic-gate 		if ((instance->cur_istate != IIS_DISABLED) &&
1941*7c478bd9Sstevel@tonic-gate 		    (instance->cur_istate != IIS_MAINTENANCE) &&
1942*7c478bd9Sstevel@tonic-gate 		    (instance->cur_istate != IIS_UNINITIALIZED)) {
1943*7c478bd9Sstevel@tonic-gate 			instance->config = read_instance_cfg(instance->fmri);
1944*7c478bd9Sstevel@tonic-gate 			if (instance->config == NULL) {
1945*7c478bd9Sstevel@tonic-gate 				log_invalid_cfg(instance->fmri);
1946*7c478bd9Sstevel@tonic-gate 				update_state(instance, IIS_MAINTENANCE,
1947*7c478bd9Sstevel@tonic-gate 				    RERR_FAULT);
1948*7c478bd9Sstevel@tonic-gate 			}
1949*7c478bd9Sstevel@tonic-gate 		}
1950*7c478bd9Sstevel@tonic-gate 	}
1951*7c478bd9Sstevel@tonic-gate 
1952*7c478bd9Sstevel@tonic-gate 	free(fmri);
1953*7c478bd9Sstevel@tonic-gate 
1954*7c478bd9Sstevel@tonic-gate 	event_type = restarter_event_get_type(event);
1955*7c478bd9Sstevel@tonic-gate 	debug_msg("Event type: %d for instance: %s", event_type,
1956*7c478bd9Sstevel@tonic-gate 	    instance->fmri);
1957*7c478bd9Sstevel@tonic-gate 
1958*7c478bd9Sstevel@tonic-gate 	/*
1959*7c478bd9Sstevel@tonic-gate 	 * If the instance is currently running a method, don't process the
1960*7c478bd9Sstevel@tonic-gate 	 * event now, but attach it to the instance for processing when
1961*7c478bd9Sstevel@tonic-gate 	 * the instance finishes its transition.
1962*7c478bd9Sstevel@tonic-gate 	 */
1963*7c478bd9Sstevel@tonic-gate 	if (INST_IN_TRANSITION(instance)) {
1964*7c478bd9Sstevel@tonic-gate 		debug_msg("storing event %d for instance %s", event_type,
1965*7c478bd9Sstevel@tonic-gate 		    instance->fmri);
1966*7c478bd9Sstevel@tonic-gate 		instance->pending_rst_event = event_type;
1967*7c478bd9Sstevel@tonic-gate 	} else {
1968*7c478bd9Sstevel@tonic-gate 		handle_restarter_event(instance, event_type, B_TRUE);
1969*7c478bd9Sstevel@tonic-gate 	}
1970*7c478bd9Sstevel@tonic-gate 
1971*7c478bd9Sstevel@tonic-gate 	return (0);
1972*7c478bd9Sstevel@tonic-gate 
1973*7c478bd9Sstevel@tonic-gate fail:
1974*7c478bd9Sstevel@tonic-gate 	ack_restarter_event(B_FALSE);
1975*7c478bd9Sstevel@tonic-gate 	return (-1);
1976*7c478bd9Sstevel@tonic-gate }
1977*7c478bd9Sstevel@tonic-gate 
1978*7c478bd9Sstevel@tonic-gate /*
1979*7c478bd9Sstevel@tonic-gate  * Do the state machine processing associated with the termination of instance
1980*7c478bd9Sstevel@tonic-gate  * 'inst''s start method.
1981*7c478bd9Sstevel@tonic-gate  */
1982*7c478bd9Sstevel@tonic-gate void
1983*7c478bd9Sstevel@tonic-gate process_start_term(instance_t *inst)
1984*7c478bd9Sstevel@tonic-gate {
1985*7c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg;
1986*7c478bd9Sstevel@tonic-gate 
1987*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_start_term: inst: %s", inst->fmri);
1988*7c478bd9Sstevel@tonic-gate 
1989*7c478bd9Sstevel@tonic-gate 	inst->copies--;
1990*7c478bd9Sstevel@tonic-gate 
1991*7c478bd9Sstevel@tonic-gate 	if ((inst->cur_istate == IIS_MAINTENANCE) ||
1992*7c478bd9Sstevel@tonic-gate 	    (inst->cur_istate == IIS_DISABLED)) {
1993*7c478bd9Sstevel@tonic-gate 		/* do any further processing/checks when we exit these states */
1994*7c478bd9Sstevel@tonic-gate 		return;
1995*7c478bd9Sstevel@tonic-gate 	}
1996*7c478bd9Sstevel@tonic-gate 
1997*7c478bd9Sstevel@tonic-gate 	cfg = inst->config->basic;
1998*7c478bd9Sstevel@tonic-gate 
1999*7c478bd9Sstevel@tonic-gate 	if (cfg->iswait) {
2000*7c478bd9Sstevel@tonic-gate 		proto_info_t	*pi;
2001*7c478bd9Sstevel@tonic-gate 
2002*7c478bd9Sstevel@tonic-gate 		switch (inst->cur_istate) {
2003*7c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
2004*7c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
2005*7c478bd9Sstevel@tonic-gate 		case IIS_IN_REFRESH_METHOD:
2006*7c478bd9Sstevel@tonic-gate 			/*
2007*7c478bd9Sstevel@tonic-gate 			 * A wait type service's start method has exited.
2008*7c478bd9Sstevel@tonic-gate 			 * Check if the method was fired off in this inetd's
2009*7c478bd9Sstevel@tonic-gate 			 * lifetime, or a previous one; if the former,
2010*7c478bd9Sstevel@tonic-gate 			 * re-commence listening on the service's behalf; if
2011*7c478bd9Sstevel@tonic-gate 			 * the latter, mark the service offline and let bind
2012*7c478bd9Sstevel@tonic-gate 			 * attempts commence.
2013*7c478bd9Sstevel@tonic-gate 			 */
2014*7c478bd9Sstevel@tonic-gate 			for (pi = uu_list_first(cfg->proto_list); pi != NULL;
2015*7c478bd9Sstevel@tonic-gate 			    pi = uu_list_next(cfg->proto_list, pi)) {
2016*7c478bd9Sstevel@tonic-gate 				/*
2017*7c478bd9Sstevel@tonic-gate 				 * If a bound fd exists, the method was fired
2018*7c478bd9Sstevel@tonic-gate 				 * off during this inetd's lifetime.
2019*7c478bd9Sstevel@tonic-gate 				 */
2020*7c478bd9Sstevel@tonic-gate 				if (pi->listen_fd != -1)
2021*7c478bd9Sstevel@tonic-gate 					break;
2022*7c478bd9Sstevel@tonic-gate 			}
2023*7c478bd9Sstevel@tonic-gate 			if (pi != NULL) {
2024*7c478bd9Sstevel@tonic-gate 				if (poll_bound_fds(inst, B_TRUE) != 0)
2025*7c478bd9Sstevel@tonic-gate 					handle_bind_failure(inst);
2026*7c478bd9Sstevel@tonic-gate 			} else {
2027*7c478bd9Sstevel@tonic-gate 				update_state(inst, IIS_OFFLINE, RERR_RESTART);
2028*7c478bd9Sstevel@tonic-gate 				create_bound_fds(inst);
2029*7c478bd9Sstevel@tonic-gate 			}
2030*7c478bd9Sstevel@tonic-gate 		}
2031*7c478bd9Sstevel@tonic-gate 	} else {
2032*7c478bd9Sstevel@tonic-gate 		/*
2033*7c478bd9Sstevel@tonic-gate 		 * Check if a nowait service should be brought back online
2034*7c478bd9Sstevel@tonic-gate 		 * after exceeding its copies limit.
2035*7c478bd9Sstevel@tonic-gate 		 */
2036*7c478bd9Sstevel@tonic-gate 		if ((inst->cur_istate == IIS_OFFLINE_COPIES) &&
2037*7c478bd9Sstevel@tonic-gate 		    !copies_limit_exceeded(inst)) {
2038*7c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_OFFLINE, RERR_NONE);
2039*7c478bd9Sstevel@tonic-gate 			process_offline_inst(inst);
2040*7c478bd9Sstevel@tonic-gate 		}
2041*7c478bd9Sstevel@tonic-gate 	}
2042*7c478bd9Sstevel@tonic-gate }
2043*7c478bd9Sstevel@tonic-gate 
2044*7c478bd9Sstevel@tonic-gate /*
2045*7c478bd9Sstevel@tonic-gate  * If the instance has a pending event process it and initiate the
2046*7c478bd9Sstevel@tonic-gate  * acknowledgement.
2047*7c478bd9Sstevel@tonic-gate  */
2048*7c478bd9Sstevel@tonic-gate static void
2049*7c478bd9Sstevel@tonic-gate process_pending_rst_event(instance_t *inst)
2050*7c478bd9Sstevel@tonic-gate {
2051*7c478bd9Sstevel@tonic-gate 	if (inst->pending_rst_event != RESTARTER_EVENT_TYPE_INVALID) {
2052*7c478bd9Sstevel@tonic-gate 		restarter_event_type_t re;
2053*7c478bd9Sstevel@tonic-gate 
2054*7c478bd9Sstevel@tonic-gate 		debug_msg("Injecting pending event %d for instance %s",
2055*7c478bd9Sstevel@tonic-gate 		    inst->pending_rst_event, inst->fmri);
2056*7c478bd9Sstevel@tonic-gate 		re = inst->pending_rst_event;
2057*7c478bd9Sstevel@tonic-gate 		inst->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID;
2058*7c478bd9Sstevel@tonic-gate 		handle_restarter_event(inst, re, B_TRUE);
2059*7c478bd9Sstevel@tonic-gate 	}
2060*7c478bd9Sstevel@tonic-gate }
2061*7c478bd9Sstevel@tonic-gate 
2062*7c478bd9Sstevel@tonic-gate /*
2063*7c478bd9Sstevel@tonic-gate  * Do the state machine processing associated with the termination
2064*7c478bd9Sstevel@tonic-gate  * of the specified instance's non-start method with the specified status.
2065*7c478bd9Sstevel@tonic-gate  * Once the processing of the termination is done, the function also picks up
2066*7c478bd9Sstevel@tonic-gate  * any processing that was blocked on the method running.
2067*7c478bd9Sstevel@tonic-gate  */
2068*7c478bd9Sstevel@tonic-gate void
2069*7c478bd9Sstevel@tonic-gate process_non_start_term(instance_t *inst, int status)
2070*7c478bd9Sstevel@tonic-gate {
2071*7c478bd9Sstevel@tonic-gate 	boolean_t ran_online_method = B_FALSE;
2072*7c478bd9Sstevel@tonic-gate 
2073*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_non_start_term: inst: %s, method: %s",
2074*7c478bd9Sstevel@tonic-gate 	    inst->fmri, methods[states[inst->cur_istate].method_running].name);
2075*7c478bd9Sstevel@tonic-gate 
2076*7c478bd9Sstevel@tonic-gate 	if (status == IMRET_FAILURE) {
2077*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("The %s method of instance %s failed, "
2078*7c478bd9Sstevel@tonic-gate 		    "transitioning to maintenance"),
2079*7c478bd9Sstevel@tonic-gate 		    methods[states[inst->cur_istate].method_running].name,
2080*7c478bd9Sstevel@tonic-gate 		    inst->fmri);
2081*7c478bd9Sstevel@tonic-gate 
2082*7c478bd9Sstevel@tonic-gate 		if ((inst->cur_istate == IIS_IN_ONLINE_METHOD) ||
2083*7c478bd9Sstevel@tonic-gate 		    (inst->cur_istate == IIS_IN_REFRESH_METHOD))
2084*7c478bd9Sstevel@tonic-gate 			destroy_bound_fds(inst);
2085*7c478bd9Sstevel@tonic-gate 
2086*7c478bd9Sstevel@tonic-gate 		update_state(inst, IIS_MAINTENANCE, RERR_FAULT);
2087*7c478bd9Sstevel@tonic-gate 
2088*7c478bd9Sstevel@tonic-gate 		inst->maintenance_req = B_FALSE;
2089*7c478bd9Sstevel@tonic-gate 		inst->conn_rate_exceeded = B_FALSE;
2090*7c478bd9Sstevel@tonic-gate 
2091*7c478bd9Sstevel@tonic-gate 		if (inst->new_config != NULL) {
2092*7c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(inst->new_config);
2093*7c478bd9Sstevel@tonic-gate 			inst->new_config = NULL;
2094*7c478bd9Sstevel@tonic-gate 		}
2095*7c478bd9Sstevel@tonic-gate 
2096*7c478bd9Sstevel@tonic-gate 		if (!inetd_stopping)
2097*7c478bd9Sstevel@tonic-gate 			process_pending_rst_event(inst);
2098*7c478bd9Sstevel@tonic-gate 
2099*7c478bd9Sstevel@tonic-gate 		return;
2100*7c478bd9Sstevel@tonic-gate 	}
2101*7c478bd9Sstevel@tonic-gate 
2102*7c478bd9Sstevel@tonic-gate 	/* non-failure method return */
2103*7c478bd9Sstevel@tonic-gate 
2104*7c478bd9Sstevel@tonic-gate 	if (status != IMRET_SUCCESS) {
2105*7c478bd9Sstevel@tonic-gate 		/*
2106*7c478bd9Sstevel@tonic-gate 		 * An instance method never returned a supported return code.
2107*7c478bd9Sstevel@tonic-gate 		 * We'll assume this means the method succeeded for now whilst
2108*7c478bd9Sstevel@tonic-gate 		 * non-GL-cognizant methods are used - eg. pkill.
2109*7c478bd9Sstevel@tonic-gate 		 */
2110*7c478bd9Sstevel@tonic-gate 		debug_msg("The %s method of instance %s returned "
2111*7c478bd9Sstevel@tonic-gate 		    "non-compliant exit code: %d, assuming success",
2112*7c478bd9Sstevel@tonic-gate 		    methods[states[inst->cur_istate].method_running].name,
2113*7c478bd9Sstevel@tonic-gate 		    inst->fmri, status);
2114*7c478bd9Sstevel@tonic-gate 	}
2115*7c478bd9Sstevel@tonic-gate 
2116*7c478bd9Sstevel@tonic-gate 	/*
2117*7c478bd9Sstevel@tonic-gate 	 * Update the state from the in-transition state.
2118*7c478bd9Sstevel@tonic-gate 	 */
2119*7c478bd9Sstevel@tonic-gate 	switch (inst->cur_istate) {
2120*7c478bd9Sstevel@tonic-gate 	case IIS_IN_ONLINE_METHOD:
2121*7c478bd9Sstevel@tonic-gate 		ran_online_method = B_TRUE;
2122*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
2123*7c478bd9Sstevel@tonic-gate 	case IIS_IN_REFRESH_METHOD:
2124*7c478bd9Sstevel@tonic-gate 		/*
2125*7c478bd9Sstevel@tonic-gate 		 * If we've exhausted the bind retries, flag that by setting
2126*7c478bd9Sstevel@tonic-gate 		 * the instance's state to degraded.
2127*7c478bd9Sstevel@tonic-gate 		 */
2128*7c478bd9Sstevel@tonic-gate 		if (inst->bind_retries_exceeded) {
2129*7c478bd9Sstevel@tonic-gate 			update_state(inst, IIS_DEGRADED, RERR_NONE);
2130*7c478bd9Sstevel@tonic-gate 			break;
2131*7c478bd9Sstevel@tonic-gate 		}
2132*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
2133*7c478bd9Sstevel@tonic-gate 	default:
2134*7c478bd9Sstevel@tonic-gate 		update_state(inst,
2135*7c478bd9Sstevel@tonic-gate 		    methods[states[inst->cur_istate].method_running].dst_state,
2136*7c478bd9Sstevel@tonic-gate 		    RERR_NONE);
2137*7c478bd9Sstevel@tonic-gate 	}
2138*7c478bd9Sstevel@tonic-gate 
2139*7c478bd9Sstevel@tonic-gate 	if (inst->cur_istate == IIS_OFFLINE) {
2140*7c478bd9Sstevel@tonic-gate 		if (inst->new_config != NULL) {
2141*7c478bd9Sstevel@tonic-gate 			/*
2142*7c478bd9Sstevel@tonic-gate 			 * This instance was found during refresh to need
2143*7c478bd9Sstevel@tonic-gate 			 * taking offline because its newly read configuration
2144*7c478bd9Sstevel@tonic-gate 			 * was sufficiently different. Now we're offline,
2145*7c478bd9Sstevel@tonic-gate 			 * activate this new configuration.
2146*7c478bd9Sstevel@tonic-gate 			 */
2147*7c478bd9Sstevel@tonic-gate 			destroy_instance_cfg(inst->config);
2148*7c478bd9Sstevel@tonic-gate 			inst->config = inst->new_config;
2149*7c478bd9Sstevel@tonic-gate 			inst->new_config = NULL;
2150*7c478bd9Sstevel@tonic-gate 		}
2151*7c478bd9Sstevel@tonic-gate 
2152*7c478bd9Sstevel@tonic-gate 		/* continue/complete any transitions that are in progress */
2153*7c478bd9Sstevel@tonic-gate 		process_offline_inst(inst);
2154*7c478bd9Sstevel@tonic-gate 
2155*7c478bd9Sstevel@tonic-gate 	} else if (ran_online_method) {
2156*7c478bd9Sstevel@tonic-gate 		/*
2157*7c478bd9Sstevel@tonic-gate 		 * We've just successfully executed the online method. We have
2158*7c478bd9Sstevel@tonic-gate 		 * a set of bound network fds that were created before running
2159*7c478bd9Sstevel@tonic-gate 		 * this method, so now we're online start listening for
2160*7c478bd9Sstevel@tonic-gate 		 * connections on them.
2161*7c478bd9Sstevel@tonic-gate 		 */
2162*7c478bd9Sstevel@tonic-gate 		if (poll_bound_fds(inst, B_TRUE) != 0)
2163*7c478bd9Sstevel@tonic-gate 			handle_bind_failure(inst);
2164*7c478bd9Sstevel@tonic-gate 	}
2165*7c478bd9Sstevel@tonic-gate 
2166*7c478bd9Sstevel@tonic-gate 	/*
2167*7c478bd9Sstevel@tonic-gate 	 * If we're now out of transition (process_offline_inst() could have
2168*7c478bd9Sstevel@tonic-gate 	 * fired off another method), carry out any jobs that were blocked by
2169*7c478bd9Sstevel@tonic-gate 	 * us being in transition.
2170*7c478bd9Sstevel@tonic-gate 	 */
2171*7c478bd9Sstevel@tonic-gate 	if (!INST_IN_TRANSITION(inst)) {
2172*7c478bd9Sstevel@tonic-gate 		if (inetd_stopping) {
2173*7c478bd9Sstevel@tonic-gate 			if (!instance_stopped(inst)) {
2174*7c478bd9Sstevel@tonic-gate 				/*
2175*7c478bd9Sstevel@tonic-gate 				 * inetd is stopping, and this instance hasn't
2176*7c478bd9Sstevel@tonic-gate 				 * been stopped. Inject a stop event.
2177*7c478bd9Sstevel@tonic-gate 				 */
2178*7c478bd9Sstevel@tonic-gate 				handle_restarter_event(inst,
2179*7c478bd9Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_STOP, B_FALSE);
2180*7c478bd9Sstevel@tonic-gate 			}
2181*7c478bd9Sstevel@tonic-gate 		} else {
2182*7c478bd9Sstevel@tonic-gate 			process_pending_rst_event(inst);
2183*7c478bd9Sstevel@tonic-gate 		}
2184*7c478bd9Sstevel@tonic-gate 	}
2185*7c478bd9Sstevel@tonic-gate }
2186*7c478bd9Sstevel@tonic-gate 
2187*7c478bd9Sstevel@tonic-gate /*
2188*7c478bd9Sstevel@tonic-gate  * Check if configuration file specified is readable. If not return B_FALSE,
2189*7c478bd9Sstevel@tonic-gate  * else return B_TRUE.
2190*7c478bd9Sstevel@tonic-gate  */
2191*7c478bd9Sstevel@tonic-gate static boolean_t
2192*7c478bd9Sstevel@tonic-gate can_read_file(const char *path)
2193*7c478bd9Sstevel@tonic-gate {
2194*7c478bd9Sstevel@tonic-gate 	int	ret;
2195*7c478bd9Sstevel@tonic-gate 	int	serrno;
2196*7c478bd9Sstevel@tonic-gate 
2197*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering can_read_file");
2198*7c478bd9Sstevel@tonic-gate 	do {
2199*7c478bd9Sstevel@tonic-gate 		ret = access(path, R_OK);
2200*7c478bd9Sstevel@tonic-gate 	} while ((ret < 0) && (errno == EINTR));
2201*7c478bd9Sstevel@tonic-gate 	if (ret < 0) {
2202*7c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
2203*7c478bd9Sstevel@tonic-gate 			serrno = errno;
2204*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to access configuration "
2205*7c478bd9Sstevel@tonic-gate 			    "file %s for performing modification checks: %s"),
2206*7c478bd9Sstevel@tonic-gate 			    path, strerror(errno));
2207*7c478bd9Sstevel@tonic-gate 			errno = serrno;
2208*7c478bd9Sstevel@tonic-gate 		}
2209*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
2210*7c478bd9Sstevel@tonic-gate 	}
2211*7c478bd9Sstevel@tonic-gate 	return (B_TRUE);
2212*7c478bd9Sstevel@tonic-gate }
2213*7c478bd9Sstevel@tonic-gate 
2214*7c478bd9Sstevel@tonic-gate /*
2215*7c478bd9Sstevel@tonic-gate  * Check whether the configuration file has changed contents since inetd
2216*7c478bd9Sstevel@tonic-gate  * was last started/refreshed, and if so, log a message indicating that
2217*7c478bd9Sstevel@tonic-gate  * inetconv needs to be run.
2218*7c478bd9Sstevel@tonic-gate  */
2219*7c478bd9Sstevel@tonic-gate static void
2220*7c478bd9Sstevel@tonic-gate check_conf_file(void)
2221*7c478bd9Sstevel@tonic-gate {
2222*7c478bd9Sstevel@tonic-gate 	char		*new_hash;
2223*7c478bd9Sstevel@tonic-gate 	char		*old_hash = NULL;
2224*7c478bd9Sstevel@tonic-gate 	scf_error_t	ret;
2225*7c478bd9Sstevel@tonic-gate 	const char	*file;
2226*7c478bd9Sstevel@tonic-gate 
2227*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering check_conf_file");
2228*7c478bd9Sstevel@tonic-gate 
2229*7c478bd9Sstevel@tonic-gate 	if (conf_file == NULL) {
2230*7c478bd9Sstevel@tonic-gate 		/*
2231*7c478bd9Sstevel@tonic-gate 		 * No explicit config file specified, so see if one of the
2232*7c478bd9Sstevel@tonic-gate 		 * default two are readable, checking the primary one first
2233*7c478bd9Sstevel@tonic-gate 		 * followed by the secondary.
2234*7c478bd9Sstevel@tonic-gate 		 */
2235*7c478bd9Sstevel@tonic-gate 		if (can_read_file(PRIMARY_DEFAULT_CONF_FILE)) {
2236*7c478bd9Sstevel@tonic-gate 			file = PRIMARY_DEFAULT_CONF_FILE;
2237*7c478bd9Sstevel@tonic-gate 		} else if ((errno == ENOENT) &&
2238*7c478bd9Sstevel@tonic-gate 		    can_read_file(SECONDARY_DEFAULT_CONF_FILE)) {
2239*7c478bd9Sstevel@tonic-gate 			file = SECONDARY_DEFAULT_CONF_FILE;
2240*7c478bd9Sstevel@tonic-gate 		} else {
2241*7c478bd9Sstevel@tonic-gate 			return;
2242*7c478bd9Sstevel@tonic-gate 		}
2243*7c478bd9Sstevel@tonic-gate 	} else {
2244*7c478bd9Sstevel@tonic-gate 		file = conf_file;
2245*7c478bd9Sstevel@tonic-gate 		if (!can_read_file(file))
2246*7c478bd9Sstevel@tonic-gate 			return;
2247*7c478bd9Sstevel@tonic-gate 	}
2248*7c478bd9Sstevel@tonic-gate 
2249*7c478bd9Sstevel@tonic-gate 	if (calculate_hash(file, &new_hash) == 0) {
2250*7c478bd9Sstevel@tonic-gate 		ret = retrieve_inetd_hash(&old_hash);
2251*7c478bd9Sstevel@tonic-gate 		if (((ret == SCF_ERROR_NONE) &&
2252*7c478bd9Sstevel@tonic-gate 		    (strcmp(old_hash, new_hash) != 0))) {
2253*7c478bd9Sstevel@tonic-gate 			/* modified config file */
2254*7c478bd9Sstevel@tonic-gate 			warn_msg(gettext(
2255*7c478bd9Sstevel@tonic-gate 			    "Configuration file %s has been modified since "
2256*7c478bd9Sstevel@tonic-gate 			    "inetconv was last run. \"inetconv -i %s\" must be "
2257*7c478bd9Sstevel@tonic-gate 			    "run to apply any changes to the SMF"), file, file);
2258*7c478bd9Sstevel@tonic-gate 		} else if ((ret != SCF_ERROR_NOT_FOUND) &&
2259*7c478bd9Sstevel@tonic-gate 		    (ret != SCF_ERROR_NONE)) {
2260*7c478bd9Sstevel@tonic-gate 			/* No message if hash not yet computed */
2261*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to check whether "
2262*7c478bd9Sstevel@tonic-gate 			    "configuration file %s has been modified: %s"),
2263*7c478bd9Sstevel@tonic-gate 			    file, scf_strerror(ret));
2264*7c478bd9Sstevel@tonic-gate 		}
2265*7c478bd9Sstevel@tonic-gate 		free(old_hash);
2266*7c478bd9Sstevel@tonic-gate 		free(new_hash);
2267*7c478bd9Sstevel@tonic-gate 	} else {
2268*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to check whether configuration file "
2269*7c478bd9Sstevel@tonic-gate 		    "%s has been modified: %s"), file, strerror(errno));
2270*7c478bd9Sstevel@tonic-gate 	}
2271*7c478bd9Sstevel@tonic-gate }
2272*7c478bd9Sstevel@tonic-gate 
2273*7c478bd9Sstevel@tonic-gate /*
2274*7c478bd9Sstevel@tonic-gate  * Refresh all inetd's managed instances and check the configuration file
2275*7c478bd9Sstevel@tonic-gate  * for any updates since inetconv was last run, logging a message if there
2276*7c478bd9Sstevel@tonic-gate  * are. We call the SMF refresh function to refresh each instance so that
2277*7c478bd9Sstevel@tonic-gate  * the refresh request goes through the framework, and thus results in the
2278*7c478bd9Sstevel@tonic-gate  * running snapshot of each instance being updated from the configuration
2279*7c478bd9Sstevel@tonic-gate  * snapshot.
2280*7c478bd9Sstevel@tonic-gate  */
2281*7c478bd9Sstevel@tonic-gate static void
2282*7c478bd9Sstevel@tonic-gate inetd_refresh(void)
2283*7c478bd9Sstevel@tonic-gate {
2284*7c478bd9Sstevel@tonic-gate 	instance_t	*inst;
2285*7c478bd9Sstevel@tonic-gate 
2286*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering inetd_refresh");
2287*7c478bd9Sstevel@tonic-gate 
2288*7c478bd9Sstevel@tonic-gate 	/* call libscf to send refresh requests for all managed instances */
2289*7c478bd9Sstevel@tonic-gate 	for (inst = uu_list_first(instance_list); inst != NULL;
2290*7c478bd9Sstevel@tonic-gate 	    inst = uu_list_next(instance_list, inst)) {
2291*7c478bd9Sstevel@tonic-gate 		if (smf_refresh_instance(inst->fmri) < 0) {
2292*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to refresh instance %s: %s"),
2293*7c478bd9Sstevel@tonic-gate 			    inst->fmri, scf_strerror(scf_error()));
2294*7c478bd9Sstevel@tonic-gate 		}
2295*7c478bd9Sstevel@tonic-gate 	}
2296*7c478bd9Sstevel@tonic-gate 
2297*7c478bd9Sstevel@tonic-gate 	/*
2298*7c478bd9Sstevel@tonic-gate 	 * Log a message if the configuration file has changed since inetconv
2299*7c478bd9Sstevel@tonic-gate 	 * was last run.
2300*7c478bd9Sstevel@tonic-gate 	 */
2301*7c478bd9Sstevel@tonic-gate 	check_conf_file();
2302*7c478bd9Sstevel@tonic-gate }
2303*7c478bd9Sstevel@tonic-gate 
2304*7c478bd9Sstevel@tonic-gate /*
2305*7c478bd9Sstevel@tonic-gate  * Initiate inetd's shutdown.
2306*7c478bd9Sstevel@tonic-gate  */
2307*7c478bd9Sstevel@tonic-gate static void
2308*7c478bd9Sstevel@tonic-gate inetd_stop(void)
2309*7c478bd9Sstevel@tonic-gate {
2310*7c478bd9Sstevel@tonic-gate 	instance_t *inst;
2311*7c478bd9Sstevel@tonic-gate 
2312*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering inetd_stop");
2313*7c478bd9Sstevel@tonic-gate 
2314*7c478bd9Sstevel@tonic-gate 	/* Block handling signals for stop and refresh */
2315*7c478bd9Sstevel@tonic-gate 	(void) sighold(SIGHUP);
2316*7c478bd9Sstevel@tonic-gate 	(void) sighold(SIGTERM);
2317*7c478bd9Sstevel@tonic-gate 
2318*7c478bd9Sstevel@tonic-gate 	/* Indicate inetd is coming down */
2319*7c478bd9Sstevel@tonic-gate 	inetd_stopping = B_TRUE;
2320*7c478bd9Sstevel@tonic-gate 
2321*7c478bd9Sstevel@tonic-gate 	/* Stop polling on restarter events. */
2322*7c478bd9Sstevel@tonic-gate 	clear_pollfd(rst_event_pipe[PE_CONSUMER]);
2323*7c478bd9Sstevel@tonic-gate 
2324*7c478bd9Sstevel@tonic-gate 	/* Stop polling for any more stop/refresh requests. */
2325*7c478bd9Sstevel@tonic-gate 	clear_pollfd(uds_fd);
2326*7c478bd9Sstevel@tonic-gate 
2327*7c478bd9Sstevel@tonic-gate 	/*
2328*7c478bd9Sstevel@tonic-gate 	 * Send a stop event to all currently unstopped instances that
2329*7c478bd9Sstevel@tonic-gate 	 * aren't in transition. For those that are in transition, the
2330*7c478bd9Sstevel@tonic-gate 	 * event will get sent when the transition completes.
2331*7c478bd9Sstevel@tonic-gate 	 */
2332*7c478bd9Sstevel@tonic-gate 	for (inst = uu_list_first(instance_list); inst != NULL;
2333*7c478bd9Sstevel@tonic-gate 	    inst = uu_list_next(instance_list, inst)) {
2334*7c478bd9Sstevel@tonic-gate 		if (!instance_stopped(inst) && !INST_IN_TRANSITION(inst))
2335*7c478bd9Sstevel@tonic-gate 			handle_restarter_event(inst,
2336*7c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_STOP, B_FALSE);
2337*7c478bd9Sstevel@tonic-gate 	}
2338*7c478bd9Sstevel@tonic-gate }
2339*7c478bd9Sstevel@tonic-gate 
2340*7c478bd9Sstevel@tonic-gate /*
2341*7c478bd9Sstevel@tonic-gate  * Sets up the intra-inetd-process Unix Domain Socket.
2342*7c478bd9Sstevel@tonic-gate  * Returns -1 on error, else 0.
2343*7c478bd9Sstevel@tonic-gate  */
2344*7c478bd9Sstevel@tonic-gate static int
2345*7c478bd9Sstevel@tonic-gate uds_init(void)
2346*7c478bd9Sstevel@tonic-gate {
2347*7c478bd9Sstevel@tonic-gate 	struct sockaddr_un addr;
2348*7c478bd9Sstevel@tonic-gate 
2349*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering uds_init");
2350*7c478bd9Sstevel@tonic-gate 
2351*7c478bd9Sstevel@tonic-gate 	if ((uds_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
2352*7c478bd9Sstevel@tonic-gate 		error_msg("socket: %s", strerror(errno));
2353*7c478bd9Sstevel@tonic-gate 		return (-1);
2354*7c478bd9Sstevel@tonic-gate 	}
2355*7c478bd9Sstevel@tonic-gate 
2356*7c478bd9Sstevel@tonic-gate 	disable_blocking(uds_fd);
2357*7c478bd9Sstevel@tonic-gate 
2358*7c478bd9Sstevel@tonic-gate 	(void) unlink(INETD_UDS_PATH);  /* clean-up any stale files */
2359*7c478bd9Sstevel@tonic-gate 
2360*7c478bd9Sstevel@tonic-gate 	(void) memset(&addr, 0, sizeof (addr));
2361*7c478bd9Sstevel@tonic-gate 	addr.sun_family = AF_UNIX;
2362*7c478bd9Sstevel@tonic-gate 	/* CONSTCOND */
2363*7c478bd9Sstevel@tonic-gate 	assert(sizeof (INETD_UDS_PATH) <= sizeof (addr.sun_path));
2364*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(addr.sun_path, INETD_UDS_PATH, sizeof (addr.sun_path));
2365*7c478bd9Sstevel@tonic-gate 
2366*7c478bd9Sstevel@tonic-gate 	if (bind(uds_fd, (struct sockaddr *)(&addr), sizeof (addr)) < 0) {
2367*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to bind socket to %s: %s"),
2368*7c478bd9Sstevel@tonic-gate 		    INETD_UDS_PATH, strerror(errno));
2369*7c478bd9Sstevel@tonic-gate 		(void) close(uds_fd);
2370*7c478bd9Sstevel@tonic-gate 		return (-1);
2371*7c478bd9Sstevel@tonic-gate 	}
2372*7c478bd9Sstevel@tonic-gate 
2373*7c478bd9Sstevel@tonic-gate 	(void) listen(uds_fd, UDS_BACKLOG);
2374*7c478bd9Sstevel@tonic-gate 
2375*7c478bd9Sstevel@tonic-gate 	if ((set_pollfd(uds_fd, POLLIN)) == -1) {
2376*7c478bd9Sstevel@tonic-gate 		(void) close(uds_fd);
2377*7c478bd9Sstevel@tonic-gate 		(void) unlink(INETD_UDS_PATH);
2378*7c478bd9Sstevel@tonic-gate 		return (-1);
2379*7c478bd9Sstevel@tonic-gate 	}
2380*7c478bd9Sstevel@tonic-gate 
2381*7c478bd9Sstevel@tonic-gate 	return (0);
2382*7c478bd9Sstevel@tonic-gate }
2383*7c478bd9Sstevel@tonic-gate 
2384*7c478bd9Sstevel@tonic-gate static void
2385*7c478bd9Sstevel@tonic-gate uds_fini(void)
2386*7c478bd9Sstevel@tonic-gate {
2387*7c478bd9Sstevel@tonic-gate 	if (uds_fd != -1)
2388*7c478bd9Sstevel@tonic-gate 		(void) close(uds_fd);
2389*7c478bd9Sstevel@tonic-gate 	(void) unlink(INETD_UDS_PATH);
2390*7c478bd9Sstevel@tonic-gate }
2391*7c478bd9Sstevel@tonic-gate 
2392*7c478bd9Sstevel@tonic-gate /*
2393*7c478bd9Sstevel@tonic-gate  * Handle an incoming request on the Unix Domain Socket. Returns -1 if there
2394*7c478bd9Sstevel@tonic-gate  * was an error handling the event, else 0.
2395*7c478bd9Sstevel@tonic-gate  */
2396*7c478bd9Sstevel@tonic-gate static int
2397*7c478bd9Sstevel@tonic-gate process_uds_event(void)
2398*7c478bd9Sstevel@tonic-gate {
2399*7c478bd9Sstevel@tonic-gate 	uds_request_t		req;
2400*7c478bd9Sstevel@tonic-gate 	int			fd;
2401*7c478bd9Sstevel@tonic-gate 	struct sockaddr_un	addr;
2402*7c478bd9Sstevel@tonic-gate 	socklen_t		len = sizeof (addr);
2403*7c478bd9Sstevel@tonic-gate 	int			ret;
2404*7c478bd9Sstevel@tonic-gate 	uint_t			retries = 0;
2405*7c478bd9Sstevel@tonic-gate 
2406*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_uds_event");
2407*7c478bd9Sstevel@tonic-gate 
2408*7c478bd9Sstevel@tonic-gate 	do {
2409*7c478bd9Sstevel@tonic-gate 		fd = accept(uds_fd, (struct sockaddr *)&addr, &len);
2410*7c478bd9Sstevel@tonic-gate 	} while ((fd < 0) && (errno == EINTR));
2411*7c478bd9Sstevel@tonic-gate 	if (fd < 0) {
2412*7c478bd9Sstevel@tonic-gate 		if (errno != EWOULDBLOCK)
2413*7c478bd9Sstevel@tonic-gate 			error_msg("accept failed: %s", strerror(errno));
2414*7c478bd9Sstevel@tonic-gate 		return (-1);
2415*7c478bd9Sstevel@tonic-gate 	}
2416*7c478bd9Sstevel@tonic-gate 
2417*7c478bd9Sstevel@tonic-gate 	for (retries = 0; retries < UDS_RECV_RETRIES; retries++) {
2418*7c478bd9Sstevel@tonic-gate 		if (((ret = safe_read(fd, &req, sizeof (req))) != 1) ||
2419*7c478bd9Sstevel@tonic-gate 		    (errno != EAGAIN))
2420*7c478bd9Sstevel@tonic-gate 			break;
2421*7c478bd9Sstevel@tonic-gate 
2422*7c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, 100);	/* 100ms pause */
2423*7c478bd9Sstevel@tonic-gate 	}
2424*7c478bd9Sstevel@tonic-gate 
2425*7c478bd9Sstevel@tonic-gate 	if (ret != 0) {
2426*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed read: %s"), strerror(errno));
2427*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
2428*7c478bd9Sstevel@tonic-gate 		return (-1);
2429*7c478bd9Sstevel@tonic-gate 	}
2430*7c478bd9Sstevel@tonic-gate 
2431*7c478bd9Sstevel@tonic-gate 	switch (req) {
2432*7c478bd9Sstevel@tonic-gate 	case UR_REFRESH_INETD:
2433*7c478bd9Sstevel@tonic-gate 		/* flag the request for event_loop() to process */
2434*7c478bd9Sstevel@tonic-gate 		refresh_inetd_requested = B_TRUE;
2435*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
2436*7c478bd9Sstevel@tonic-gate 		break;
2437*7c478bd9Sstevel@tonic-gate 	case UR_STOP_INETD:
2438*7c478bd9Sstevel@tonic-gate 		inetd_stop();
2439*7c478bd9Sstevel@tonic-gate 		break;
2440*7c478bd9Sstevel@tonic-gate 	default:
2441*7c478bd9Sstevel@tonic-gate 		error_msg("unexpected UDS request");
2442*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
2443*7c478bd9Sstevel@tonic-gate 		return (-1);
2444*7c478bd9Sstevel@tonic-gate 	}
2445*7c478bd9Sstevel@tonic-gate 
2446*7c478bd9Sstevel@tonic-gate 	return (0);
2447*7c478bd9Sstevel@tonic-gate }
2448*7c478bd9Sstevel@tonic-gate 
2449*7c478bd9Sstevel@tonic-gate /*
2450*7c478bd9Sstevel@tonic-gate  * Perform checks for common exec string errors. We limit the checks to
2451*7c478bd9Sstevel@tonic-gate  * whether the file exists, is a regular file, and has at least one execute
2452*7c478bd9Sstevel@tonic-gate  * bit set. We leave the core security checks to exec() so as not to duplicate
2453*7c478bd9Sstevel@tonic-gate  * and thus incur the associated drawbacks, but hope to catch the common
2454*7c478bd9Sstevel@tonic-gate  * errors here.
2455*7c478bd9Sstevel@tonic-gate  */
2456*7c478bd9Sstevel@tonic-gate static boolean_t
2457*7c478bd9Sstevel@tonic-gate passes_basic_exec_checks(const char *instance, const char *method,
2458*7c478bd9Sstevel@tonic-gate     const char *path)
2459*7c478bd9Sstevel@tonic-gate {
2460*7c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
2461*7c478bd9Sstevel@tonic-gate 
2462*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering passes_basic_exec_checks");
2463*7c478bd9Sstevel@tonic-gate 
2464*7c478bd9Sstevel@tonic-gate 	/* check the file exists */
2465*7c478bd9Sstevel@tonic-gate 	while (stat(path, &sbuf) == -1) {
2466*7c478bd9Sstevel@tonic-gate 		if (errno != EINTR) {
2467*7c478bd9Sstevel@tonic-gate 			error_msg(gettext(
2468*7c478bd9Sstevel@tonic-gate 			    "Can't stat the %s method of instance %s: %s"),
2469*7c478bd9Sstevel@tonic-gate 			    method, instance, strerror(errno));
2470*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
2471*7c478bd9Sstevel@tonic-gate 		}
2472*7c478bd9Sstevel@tonic-gate 	}
2473*7c478bd9Sstevel@tonic-gate 
2474*7c478bd9Sstevel@tonic-gate 	/*
2475*7c478bd9Sstevel@tonic-gate 	 * Check if the file is a regular file and has at least one execute
2476*7c478bd9Sstevel@tonic-gate 	 * bit set.
2477*7c478bd9Sstevel@tonic-gate 	 */
2478*7c478bd9Sstevel@tonic-gate 	if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
2479*7c478bd9Sstevel@tonic-gate 		error_msg(gettext(
2480*7c478bd9Sstevel@tonic-gate 		    "The %s method of instance %s isn't a regular file"),
2481*7c478bd9Sstevel@tonic-gate 		    method, instance);
2482*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
2483*7c478bd9Sstevel@tonic-gate 	} else if ((sbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
2484*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("The %s method instance %s doesn't have "
2485*7c478bd9Sstevel@tonic-gate 		    "any execute permissions set"), method, instance);
2486*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
2487*7c478bd9Sstevel@tonic-gate 	}
2488*7c478bd9Sstevel@tonic-gate 
2489*7c478bd9Sstevel@tonic-gate 	return (B_TRUE);
2490*7c478bd9Sstevel@tonic-gate }
2491*7c478bd9Sstevel@tonic-gate 
2492*7c478bd9Sstevel@tonic-gate static void
2493*7c478bd9Sstevel@tonic-gate exec_method(instance_t *instance, instance_method_t method, method_info_t *mi,
2494*7c478bd9Sstevel@tonic-gate     struct method_context *mthd_ctxt, const proto_info_t *pi)
2495*7c478bd9Sstevel@tonic-gate {
2496*7c478bd9Sstevel@tonic-gate 	char		**args;
2497*7c478bd9Sstevel@tonic-gate 	char 		**env;
2498*7c478bd9Sstevel@tonic-gate 	const char	*errf;
2499*7c478bd9Sstevel@tonic-gate 	int		serrno;
2500*7c478bd9Sstevel@tonic-gate 	basic_cfg_t	*cfg = instance->config->basic;
2501*7c478bd9Sstevel@tonic-gate 
2502*7c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
2503*7c478bd9Sstevel@tonic-gate 		/*
2504*7c478bd9Sstevel@tonic-gate 		 * If wrappers checks fail, pretend the method was exec'd and
2505*7c478bd9Sstevel@tonic-gate 		 * failed.
2506*7c478bd9Sstevel@tonic-gate 		 */
2507*7c478bd9Sstevel@tonic-gate 		if (!tcp_wrappers_ok(instance))
2508*7c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
2509*7c478bd9Sstevel@tonic-gate 	}
2510*7c478bd9Sstevel@tonic-gate 
2511*7c478bd9Sstevel@tonic-gate 	/*
2512*7c478bd9Sstevel@tonic-gate 	 * Revert the disposition of handled signals and ignored signals to
2513*7c478bd9Sstevel@tonic-gate 	 * their defaults, unblocking any blocked ones as a side effect.
2514*7c478bd9Sstevel@tonic-gate 	 */
2515*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_DFL);
2516*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
2517*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
2518*7c478bd9Sstevel@tonic-gate 
2519*7c478bd9Sstevel@tonic-gate 	/*
2520*7c478bd9Sstevel@tonic-gate 	 * Setup exec arguments. Do this before the fd setup below, so our
2521*7c478bd9Sstevel@tonic-gate 	 * logging related file fd doesn't get taken over before we call
2522*7c478bd9Sstevel@tonic-gate 	 * expand_address().
2523*7c478bd9Sstevel@tonic-gate 	 */
2524*7c478bd9Sstevel@tonic-gate 	if ((method == IM_START) &&
2525*7c478bd9Sstevel@tonic-gate 	    (strcmp(mi->exec_args_we.we_wordv[0], "%A") == 0)) {
2526*7c478bd9Sstevel@tonic-gate 		args = expand_address(instance, pi);
2527*7c478bd9Sstevel@tonic-gate 	} else {
2528*7c478bd9Sstevel@tonic-gate 		args = mi->exec_args_we.we_wordv;
2529*7c478bd9Sstevel@tonic-gate 	}
2530*7c478bd9Sstevel@tonic-gate 
2531*7c478bd9Sstevel@tonic-gate 	/* Generate audit trail for start operations */
2532*7c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
2533*7c478bd9Sstevel@tonic-gate 		adt_event_data_t *ae;
2534*7c478bd9Sstevel@tonic-gate 		struct sockaddr_storage ss;
2535*7c478bd9Sstevel@tonic-gate 		priv_set_t *privset;
2536*7c478bd9Sstevel@tonic-gate 		socklen_t sslen = sizeof (ss);
2537*7c478bd9Sstevel@tonic-gate 
2538*7c478bd9Sstevel@tonic-gate 		if ((ae = adt_alloc_event(audit_handle, ADT_inetd_connect))
2539*7c478bd9Sstevel@tonic-gate 		    == NULL) {
2540*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Unable to allocate audit event for "
2541*7c478bd9Sstevel@tonic-gate 			    "the %s method of instance %s"),
2542*7c478bd9Sstevel@tonic-gate 			    methods[method].name, instance->fmri);
2543*7c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
2544*7c478bd9Sstevel@tonic-gate 		}
2545*7c478bd9Sstevel@tonic-gate 
2546*7c478bd9Sstevel@tonic-gate 		/*
2547*7c478bd9Sstevel@tonic-gate 		 * The inetd_connect audit record consists of:
2548*7c478bd9Sstevel@tonic-gate 		 *	Service name
2549*7c478bd9Sstevel@tonic-gate 		 *	Execution path
2550*7c478bd9Sstevel@tonic-gate 		 *	Remote address and port
2551*7c478bd9Sstevel@tonic-gate 		 *	Local port
2552*7c478bd9Sstevel@tonic-gate 		 *	Process privileges
2553*7c478bd9Sstevel@tonic-gate 		 */
2554*7c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.service_name = cfg->svc_name;
2555*7c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.cmd = mi->exec_path;
2556*7c478bd9Sstevel@tonic-gate 
2557*7c478bd9Sstevel@tonic-gate 		if (instance->remote_addr.ss_family == AF_INET) {
2558*7c478bd9Sstevel@tonic-gate 			struct in_addr *in = SS_SINADDR(instance->remote_addr);
2559*7c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_adr[0] = in->s_addr;
2560*7c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_type = ADT_IPv4;
2561*7c478bd9Sstevel@tonic-gate 		} else {
2562*7c478bd9Sstevel@tonic-gate 			uint32_t *addr6;
2563*7c478bd9Sstevel@tonic-gate 			int i;
2564*7c478bd9Sstevel@tonic-gate 
2565*7c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_type = ADT_IPv6;
2566*7c478bd9Sstevel@tonic-gate 			addr6 = (uint32_t *)SS_SINADDR(instance->remote_addr);
2567*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < 4; ++i)
2568*7c478bd9Sstevel@tonic-gate 				ae->adt_inetd_connect.ip_adr[i] = addr6[i];
2569*7c478bd9Sstevel@tonic-gate 		}
2570*7c478bd9Sstevel@tonic-gate 
2571*7c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.ip_remote_port =
2572*7c478bd9Sstevel@tonic-gate 		    ntohs(SS_PORT(instance->remote_addr));
2573*7c478bd9Sstevel@tonic-gate 
2574*7c478bd9Sstevel@tonic-gate 		if (getsockname(instance->conn_fd, (struct sockaddr *)&ss,
2575*7c478bd9Sstevel@tonic-gate 		    &sslen) == 0)
2576*7c478bd9Sstevel@tonic-gate 			ae->adt_inetd_connect.ip_local_port =
2577*7c478bd9Sstevel@tonic-gate 			    ntohs(SS_PORT(ss));
2578*7c478bd9Sstevel@tonic-gate 
2579*7c478bd9Sstevel@tonic-gate 		privset = mthd_ctxt->priv_set;
2580*7c478bd9Sstevel@tonic-gate 		if (privset == NULL) {
2581*7c478bd9Sstevel@tonic-gate 			privset = priv_allocset();
2582*7c478bd9Sstevel@tonic-gate 			if (privset != NULL &&
2583*7c478bd9Sstevel@tonic-gate 			    getppriv(PRIV_EFFECTIVE, privset) != 0) {
2584*7c478bd9Sstevel@tonic-gate 				priv_freeset(privset);
2585*7c478bd9Sstevel@tonic-gate 				privset = NULL;
2586*7c478bd9Sstevel@tonic-gate 			}
2587*7c478bd9Sstevel@tonic-gate 		}
2588*7c478bd9Sstevel@tonic-gate 
2589*7c478bd9Sstevel@tonic-gate 		ae->adt_inetd_connect.privileges = privset;
2590*7c478bd9Sstevel@tonic-gate 
2591*7c478bd9Sstevel@tonic-gate 		(void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS);
2592*7c478bd9Sstevel@tonic-gate 		adt_free_event(ae);
2593*7c478bd9Sstevel@tonic-gate 
2594*7c478bd9Sstevel@tonic-gate 		if (privset != NULL && mthd_ctxt->priv_set == NULL)
2595*7c478bd9Sstevel@tonic-gate 			priv_freeset(privset);
2596*7c478bd9Sstevel@tonic-gate 	}
2597*7c478bd9Sstevel@tonic-gate 
2598*7c478bd9Sstevel@tonic-gate 	/*
2599*7c478bd9Sstevel@tonic-gate 	 * Set method context before the fd setup below so we can output an
2600*7c478bd9Sstevel@tonic-gate 	 * error message if it fails.
2601*7c478bd9Sstevel@tonic-gate 	 */
2602*7c478bd9Sstevel@tonic-gate 	if ((errno = restarter_set_method_context(mthd_ctxt, &errf)) != 0) {
2603*7c478bd9Sstevel@tonic-gate 		const char *msg;
2604*7c478bd9Sstevel@tonic-gate 
2605*7c478bd9Sstevel@tonic-gate 		if (errno == -1) {
2606*7c478bd9Sstevel@tonic-gate 			if (strcmp(errf, "core_set_process_path") == 0) {
2607*7c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to set the corefile path "
2608*7c478bd9Sstevel@tonic-gate 				    "for the %s method of instance %s");
2609*7c478bd9Sstevel@tonic-gate 			} else if (strcmp(errf, "setproject") == 0) {
2610*7c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to assign a resource "
2611*7c478bd9Sstevel@tonic-gate 				    "control for the %s method of instance %s");
2612*7c478bd9Sstevel@tonic-gate 			} else if (strcmp(errf, "pool_set_binding") == 0) {
2613*7c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to bind the %s method of "
2614*7c478bd9Sstevel@tonic-gate 				    "instance %s to a pool due to a system "
2615*7c478bd9Sstevel@tonic-gate 				    "error");
2616*7c478bd9Sstevel@tonic-gate 			} else {
2617*7c478bd9Sstevel@tonic-gate 				assert(0);
2618*7c478bd9Sstevel@tonic-gate 				abort();
2619*7c478bd9Sstevel@tonic-gate 			}
2620*7c478bd9Sstevel@tonic-gate 
2621*7c478bd9Sstevel@tonic-gate 			error_msg(msg, methods[method].name, instance->fmri);
2622*7c478bd9Sstevel@tonic-gate 
2623*7c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
2624*7c478bd9Sstevel@tonic-gate 		}
2625*7c478bd9Sstevel@tonic-gate 
2626*7c478bd9Sstevel@tonic-gate 		if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) {
2627*7c478bd9Sstevel@tonic-gate 			switch (errno) {
2628*7c478bd9Sstevel@tonic-gate 			case ENOENT:
2629*7c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to find resource pool "
2630*7c478bd9Sstevel@tonic-gate 				    "for the %s method of instance %s");
2631*7c478bd9Sstevel@tonic-gate 				break;
2632*7c478bd9Sstevel@tonic-gate 
2633*7c478bd9Sstevel@tonic-gate 			case EBADF:
2634*7c478bd9Sstevel@tonic-gate 				msg = gettext("Failed to bind the %s method of "
2635*7c478bd9Sstevel@tonic-gate 				    "instance %s to a pool due to invalid "
2636*7c478bd9Sstevel@tonic-gate 				    "configuration");
2637*7c478bd9Sstevel@tonic-gate 				break;
2638*7c478bd9Sstevel@tonic-gate 
2639*7c478bd9Sstevel@tonic-gate 			default:
2640*7c478bd9Sstevel@tonic-gate 				assert(0);
2641*7c478bd9Sstevel@tonic-gate 				abort();
2642*7c478bd9Sstevel@tonic-gate 			}
2643*7c478bd9Sstevel@tonic-gate 
2644*7c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
2645*7c478bd9Sstevel@tonic-gate 		}
2646*7c478bd9Sstevel@tonic-gate 
2647*7c478bd9Sstevel@tonic-gate 		if (errf != NULL) {
2648*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to set credentials for the "
2649*7c478bd9Sstevel@tonic-gate 			    "%s method of instance %s (%s: %s)"),
2650*7c478bd9Sstevel@tonic-gate 			    methods[method].name, instance->fmri, errf,
2651*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
2652*7c478bd9Sstevel@tonic-gate 			exit(IMRET_FAILURE);
2653*7c478bd9Sstevel@tonic-gate 		}
2654*7c478bd9Sstevel@tonic-gate 
2655*7c478bd9Sstevel@tonic-gate 		switch (errno) {
2656*7c478bd9Sstevel@tonic-gate 		case ENOMEM:
2657*7c478bd9Sstevel@tonic-gate 			msg = gettext("Failed to set credentials for the %s "
2658*7c478bd9Sstevel@tonic-gate 			    "method of instance %s (out of memory)");
2659*7c478bd9Sstevel@tonic-gate 			break;
2660*7c478bd9Sstevel@tonic-gate 
2661*7c478bd9Sstevel@tonic-gate 		case ENOENT:
2662*7c478bd9Sstevel@tonic-gate 			msg = gettext("Failed to set credentials for the %s "
2663*7c478bd9Sstevel@tonic-gate 			    "method of instance %s (no passwd or shadow "
2664*7c478bd9Sstevel@tonic-gate 			    "entry for user)");
2665*7c478bd9Sstevel@tonic-gate 			break;
2666*7c478bd9Sstevel@tonic-gate 
2667*7c478bd9Sstevel@tonic-gate 		default:
2668*7c478bd9Sstevel@tonic-gate 			assert(0);
2669*7c478bd9Sstevel@tonic-gate 			abort();
2670*7c478bd9Sstevel@tonic-gate 		}
2671*7c478bd9Sstevel@tonic-gate 
2672*7c478bd9Sstevel@tonic-gate 		error_msg(msg, methods[method].name, instance->fmri);
2673*7c478bd9Sstevel@tonic-gate 		exit(IMRET_FAILURE);
2674*7c478bd9Sstevel@tonic-gate 	}
2675*7c478bd9Sstevel@tonic-gate 
2676*7c478bd9Sstevel@tonic-gate 	/* let exec() free mthd_ctxt */
2677*7c478bd9Sstevel@tonic-gate 
2678*7c478bd9Sstevel@tonic-gate 	/* setup standard fds */
2679*7c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
2680*7c478bd9Sstevel@tonic-gate 		(void) dup2(instance->conn_fd, STDIN_FILENO);
2681*7c478bd9Sstevel@tonic-gate 	} else {
2682*7c478bd9Sstevel@tonic-gate 		(void) close(STDIN_FILENO);
2683*7c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", O_RDONLY);
2684*7c478bd9Sstevel@tonic-gate 	}
2685*7c478bd9Sstevel@tonic-gate 	(void) dup2(STDIN_FILENO, STDOUT_FILENO);
2686*7c478bd9Sstevel@tonic-gate 	(void) dup2(STDIN_FILENO, STDERR_FILENO);
2687*7c478bd9Sstevel@tonic-gate 
2688*7c478bd9Sstevel@tonic-gate 	closefrom(STDERR_FILENO + 1);
2689*7c478bd9Sstevel@tonic-gate 
2690*7c478bd9Sstevel@tonic-gate 	method_preexec();
2691*7c478bd9Sstevel@tonic-gate 
2692*7c478bd9Sstevel@tonic-gate 	env = set_smf_env(mthd_ctxt, instance, methods[method].name);
2693*7c478bd9Sstevel@tonic-gate 
2694*7c478bd9Sstevel@tonic-gate 	if (env != NULL) {
2695*7c478bd9Sstevel@tonic-gate 		do {
2696*7c478bd9Sstevel@tonic-gate 			(void) execve(mi->exec_path, args, env);
2697*7c478bd9Sstevel@tonic-gate 		} while (errno == EINTR);
2698*7c478bd9Sstevel@tonic-gate 	}
2699*7c478bd9Sstevel@tonic-gate 
2700*7c478bd9Sstevel@tonic-gate 	serrno = errno;
2701*7c478bd9Sstevel@tonic-gate 	/* start up logging again to report the error */
2702*7c478bd9Sstevel@tonic-gate 	msg_init();
2703*7c478bd9Sstevel@tonic-gate 	errno = serrno;
2704*7c478bd9Sstevel@tonic-gate 
2705*7c478bd9Sstevel@tonic-gate 	error_msg(
2706*7c478bd9Sstevel@tonic-gate 	    gettext("Failed to exec %s method of instance %s: %s"),
2707*7c478bd9Sstevel@tonic-gate 	    methods[method].name, instance->fmri, strerror(errno));
2708*7c478bd9Sstevel@tonic-gate 
2709*7c478bd9Sstevel@tonic-gate 	if ((method == IM_START) && (instance->config->basic->iswait)) {
2710*7c478bd9Sstevel@tonic-gate 		/*
2711*7c478bd9Sstevel@tonic-gate 		 * We couldn't exec the start method for a wait type service.
2712*7c478bd9Sstevel@tonic-gate 		 * Eat up data from the endpoint, so that hopefully the
2713*7c478bd9Sstevel@tonic-gate 		 * service's fd won't wake poll up on the next time round
2714*7c478bd9Sstevel@tonic-gate 		 * event_loop(). This behavior is carried over from the old
2715*7c478bd9Sstevel@tonic-gate 		 * inetd, and it seems somewhat arbitrary that it isn't
2716*7c478bd9Sstevel@tonic-gate 		 * also done in the case of fork failures; but I guess
2717*7c478bd9Sstevel@tonic-gate 		 * it assumes an exec failure is less likely to be the result
2718*7c478bd9Sstevel@tonic-gate 		 * of a resource shortage, and is thus not worth retrying.
2719*7c478bd9Sstevel@tonic-gate 		 */
2720*7c478bd9Sstevel@tonic-gate 		consume_wait_data(instance, 0);
2721*7c478bd9Sstevel@tonic-gate 	}
2722*7c478bd9Sstevel@tonic-gate 
2723*7c478bd9Sstevel@tonic-gate 	exit(IMRET_FAILURE);
2724*7c478bd9Sstevel@tonic-gate }
2725*7c478bd9Sstevel@tonic-gate 
2726*7c478bd9Sstevel@tonic-gate static restarter_error_t
2727*7c478bd9Sstevel@tonic-gate get_method_error_success(instance_method_t method)
2728*7c478bd9Sstevel@tonic-gate {
2729*7c478bd9Sstevel@tonic-gate 	switch (method) {
2730*7c478bd9Sstevel@tonic-gate 	case IM_OFFLINE:
2731*7c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
2732*7c478bd9Sstevel@tonic-gate 	case IM_ONLINE:
2733*7c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
2734*7c478bd9Sstevel@tonic-gate 	case IM_DISABLE:
2735*7c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
2736*7c478bd9Sstevel@tonic-gate 	case IM_REFRESH:
2737*7c478bd9Sstevel@tonic-gate 		return (RERR_REFRESH);
2738*7c478bd9Sstevel@tonic-gate 	case IM_START:
2739*7c478bd9Sstevel@tonic-gate 		return (RERR_RESTART);
2740*7c478bd9Sstevel@tonic-gate 	}
2741*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2742*7c478bd9Sstevel@tonic-gate }
2743*7c478bd9Sstevel@tonic-gate 
2744*7c478bd9Sstevel@tonic-gate /*
2745*7c478bd9Sstevel@tonic-gate  * Runs the specified method of the specified service instance.
2746*7c478bd9Sstevel@tonic-gate  * If the method was never specified, we handle it the same as if the
2747*7c478bd9Sstevel@tonic-gate  * method was called and returned success, carrying on any transition the
2748*7c478bd9Sstevel@tonic-gate  * instance may be in the midst of.
2749*7c478bd9Sstevel@tonic-gate  * If the method isn't executable in its specified profile or an error occurs
2750*7c478bd9Sstevel@tonic-gate  * forking a process to run the method in the function returns -1.
2751*7c478bd9Sstevel@tonic-gate  * If a method binary is successfully executed, the function switches the
2752*7c478bd9Sstevel@tonic-gate  * instance's cur state to the method's associated 'run' state and the next
2753*7c478bd9Sstevel@tonic-gate  * state to the methods associated next state.
2754*7c478bd9Sstevel@tonic-gate  * Returns -1 if there's an error before forking, else 0.
2755*7c478bd9Sstevel@tonic-gate  */
2756*7c478bd9Sstevel@tonic-gate int
2757*7c478bd9Sstevel@tonic-gate run_method(instance_t *instance, instance_method_t method,
2758*7c478bd9Sstevel@tonic-gate     const proto_info_t *start_info)
2759*7c478bd9Sstevel@tonic-gate {
2760*7c478bd9Sstevel@tonic-gate 	pid_t			child_pid;
2761*7c478bd9Sstevel@tonic-gate 	method_info_t		*mi;
2762*7c478bd9Sstevel@tonic-gate 	struct method_context	*mthd_ctxt = NULL;
2763*7c478bd9Sstevel@tonic-gate 	const char		*errstr;
2764*7c478bd9Sstevel@tonic-gate 	int			sig;
2765*7c478bd9Sstevel@tonic-gate 	int			ret;
2766*7c478bd9Sstevel@tonic-gate 	instance_cfg_t		*cfg = instance->config;
2767*7c478bd9Sstevel@tonic-gate 	ctid_t			cid;
2768*7c478bd9Sstevel@tonic-gate 	boolean_t		trans_failure = B_TRUE;
2769*7c478bd9Sstevel@tonic-gate 	int			serrno;
2770*7c478bd9Sstevel@tonic-gate 
2771*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering run_method, instance: %s, method: %s",
2772*7c478bd9Sstevel@tonic-gate 	    instance->fmri, methods[method].name);
2773*7c478bd9Sstevel@tonic-gate 
2774*7c478bd9Sstevel@tonic-gate 	/*
2775*7c478bd9Sstevel@tonic-gate 	 * Don't bother updating the instance's state for the start method
2776*7c478bd9Sstevel@tonic-gate 	 * as there isn't a separate start method state.
2777*7c478bd9Sstevel@tonic-gate 	 */
2778*7c478bd9Sstevel@tonic-gate 	if (method != IM_START)
2779*7c478bd9Sstevel@tonic-gate 		update_instance_states(instance, get_method_state(method),
2780*7c478bd9Sstevel@tonic-gate 		    methods[method].dst_state,
2781*7c478bd9Sstevel@tonic-gate 		    get_method_error_success(method));
2782*7c478bd9Sstevel@tonic-gate 
2783*7c478bd9Sstevel@tonic-gate 	if ((mi = cfg->methods[method]) == NULL) {
2784*7c478bd9Sstevel@tonic-gate 		/*
2785*7c478bd9Sstevel@tonic-gate 		 * An unspecified method. Since the absence of this method
2786*7c478bd9Sstevel@tonic-gate 		 * must be valid (otherwise it would have been caught
2787*7c478bd9Sstevel@tonic-gate 		 * during configuration validation), simply pretend the method
2788*7c478bd9Sstevel@tonic-gate 		 * ran and returned success.
2789*7c478bd9Sstevel@tonic-gate 		 */
2790*7c478bd9Sstevel@tonic-gate 		process_non_start_term(instance, IMRET_SUCCESS);
2791*7c478bd9Sstevel@tonic-gate 		return (0);
2792*7c478bd9Sstevel@tonic-gate 	}
2793*7c478bd9Sstevel@tonic-gate 
2794*7c478bd9Sstevel@tonic-gate 	/* Handle special method tokens, not allowed on start */
2795*7c478bd9Sstevel@tonic-gate 	if (method != IM_START) {
2796*7c478bd9Sstevel@tonic-gate 		if (restarter_is_null_method(mi->exec_path)) {
2797*7c478bd9Sstevel@tonic-gate 			/* :true means nothing should be done */
2798*7c478bd9Sstevel@tonic-gate 			process_non_start_term(instance, IMRET_SUCCESS);
2799*7c478bd9Sstevel@tonic-gate 			return (0);
2800*7c478bd9Sstevel@tonic-gate 		}
2801*7c478bd9Sstevel@tonic-gate 
2802*7c478bd9Sstevel@tonic-gate 		if ((sig = restarter_is_kill_method(mi->exec_path)) >= 0) {
2803*7c478bd9Sstevel@tonic-gate 			/* Carry out contract assassination */
2804*7c478bd9Sstevel@tonic-gate 			ret = iterate_repository_contracts(instance->fmri, sig);
2805*7c478bd9Sstevel@tonic-gate 			/* ENOENT means we didn't find any contracts */
2806*7c478bd9Sstevel@tonic-gate 			if (ret != 0 && ret != ENOENT) {
2807*7c478bd9Sstevel@tonic-gate 				error_msg(gettext("Failed to send signal %d "
2808*7c478bd9Sstevel@tonic-gate 				    "to contracts of instance %s: %s"), sig,
2809*7c478bd9Sstevel@tonic-gate 				    instance->fmri, strerror(ret));
2810*7c478bd9Sstevel@tonic-gate 				goto prefork_failure;
2811*7c478bd9Sstevel@tonic-gate 			} else {
2812*7c478bd9Sstevel@tonic-gate 				process_non_start_term(instance, IMRET_SUCCESS);
2813*7c478bd9Sstevel@tonic-gate 				return (0);
2814*7c478bd9Sstevel@tonic-gate 			}
2815*7c478bd9Sstevel@tonic-gate 		}
2816*7c478bd9Sstevel@tonic-gate 
2817*7c478bd9Sstevel@tonic-gate 		if ((sig = restarter_is_kill_proc_method(mi->exec_path)) >= 0) {
2818*7c478bd9Sstevel@tonic-gate 			/* Carry out process assassination */
2819*7c478bd9Sstevel@tonic-gate 			rep_val_t	*rv;
2820*7c478bd9Sstevel@tonic-gate 
2821*7c478bd9Sstevel@tonic-gate 			ret = IMRET_SUCCESS;
2822*7c478bd9Sstevel@tonic-gate 			for (rv = uu_list_first(instance->start_pids);
2823*7c478bd9Sstevel@tonic-gate 			    rv != NULL;
2824*7c478bd9Sstevel@tonic-gate 			    rv = uu_list_next(instance->start_pids, rv)) {
2825*7c478bd9Sstevel@tonic-gate 				if ((kill((pid_t)rv->val, sig) != 0) &&
2826*7c478bd9Sstevel@tonic-gate 				    (errno != ESRCH)) {
2827*7c478bd9Sstevel@tonic-gate 					ret = IMRET_FAILURE;
2828*7c478bd9Sstevel@tonic-gate 					error_msg(gettext("Unable to signal "
2829*7c478bd9Sstevel@tonic-gate 					    "start process of instance %s: %s"),
2830*7c478bd9Sstevel@tonic-gate 					    instance->fmri, strerror(errno));
2831*7c478bd9Sstevel@tonic-gate 				}
2832*7c478bd9Sstevel@tonic-gate 			}
2833*7c478bd9Sstevel@tonic-gate 
2834*7c478bd9Sstevel@tonic-gate 			process_non_start_term(instance, ret);
2835*7c478bd9Sstevel@tonic-gate 			return (0);
2836*7c478bd9Sstevel@tonic-gate 		}
2837*7c478bd9Sstevel@tonic-gate 	}
2838*7c478bd9Sstevel@tonic-gate 
2839*7c478bd9Sstevel@tonic-gate 	/*
2840*7c478bd9Sstevel@tonic-gate 	 * Get the associated method context before the fork so we can
2841*7c478bd9Sstevel@tonic-gate 	 * modify the instances state if things go wrong.
2842*7c478bd9Sstevel@tonic-gate 	 */
2843*7c478bd9Sstevel@tonic-gate 	if ((mthd_ctxt = read_method_context(instance->fmri,
2844*7c478bd9Sstevel@tonic-gate 	    methods[method].name, mi->exec_path, &errstr)) == NULL) {
2845*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to retrieve method context for the "
2846*7c478bd9Sstevel@tonic-gate 		    "%s method of instance %s: %s"), methods[method].name,
2847*7c478bd9Sstevel@tonic-gate 		    instance->fmri, errstr);
2848*7c478bd9Sstevel@tonic-gate 		goto prefork_failure;
2849*7c478bd9Sstevel@tonic-gate 	}
2850*7c478bd9Sstevel@tonic-gate 
2851*7c478bd9Sstevel@tonic-gate 	/*
2852*7c478bd9Sstevel@tonic-gate 	 * Perform some basic checks before we fork to limit the possibility
2853*7c478bd9Sstevel@tonic-gate 	 * of exec failures, so we can modify the instance state if necessary.
2854*7c478bd9Sstevel@tonic-gate 	 */
2855*7c478bd9Sstevel@tonic-gate 	if (!passes_basic_exec_checks(instance->fmri, methods[method].name,
2856*7c478bd9Sstevel@tonic-gate 	    mi->exec_path)) {
2857*7c478bd9Sstevel@tonic-gate 		trans_failure = B_FALSE;
2858*7c478bd9Sstevel@tonic-gate 		goto prefork_failure;
2859*7c478bd9Sstevel@tonic-gate 	}
2860*7c478bd9Sstevel@tonic-gate 
2861*7c478bd9Sstevel@tonic-gate 	if (contract_prefork() == -1)
2862*7c478bd9Sstevel@tonic-gate 		goto prefork_failure;
2863*7c478bd9Sstevel@tonic-gate 	child_pid = fork();
2864*7c478bd9Sstevel@tonic-gate 	serrno = errno;
2865*7c478bd9Sstevel@tonic-gate 	contract_postfork();
2866*7c478bd9Sstevel@tonic-gate 
2867*7c478bd9Sstevel@tonic-gate 	switch (child_pid) {
2868*7c478bd9Sstevel@tonic-gate 	case -1:
2869*7c478bd9Sstevel@tonic-gate 		error_msg(gettext(
2870*7c478bd9Sstevel@tonic-gate 		    "Unable to fork %s method of instance %s: %s"),
2871*7c478bd9Sstevel@tonic-gate 		    methods[method].name, instance->fmri, strerror(serrno));
2872*7c478bd9Sstevel@tonic-gate 		if ((serrno != EAGAIN) && (serrno != ENOMEM))
2873*7c478bd9Sstevel@tonic-gate 			trans_failure = B_FALSE;
2874*7c478bd9Sstevel@tonic-gate 		goto prefork_failure;
2875*7c478bd9Sstevel@tonic-gate 	case 0:				/* child */
2876*7c478bd9Sstevel@tonic-gate 		exec_method(instance, method, mi, mthd_ctxt, start_info);
2877*7c478bd9Sstevel@tonic-gate 		break;
2878*7c478bd9Sstevel@tonic-gate 	default:			/* parent */
2879*7c478bd9Sstevel@tonic-gate 		restarter_free_method_context(mthd_ctxt);
2880*7c478bd9Sstevel@tonic-gate 		mthd_ctxt = NULL;
2881*7c478bd9Sstevel@tonic-gate 
2882*7c478bd9Sstevel@tonic-gate 		if (get_latest_contract(&cid) < 0)
2883*7c478bd9Sstevel@tonic-gate 			cid = -1;
2884*7c478bd9Sstevel@tonic-gate 
2885*7c478bd9Sstevel@tonic-gate 		/*
2886*7c478bd9Sstevel@tonic-gate 		 * Register this method so its termination is noticed and
2887*7c478bd9Sstevel@tonic-gate 		 * the state transition this method participates in is
2888*7c478bd9Sstevel@tonic-gate 		 * continued.
2889*7c478bd9Sstevel@tonic-gate 		 */
2890*7c478bd9Sstevel@tonic-gate 		if (register_method(instance, child_pid, cid, method) != 0) {
2891*7c478bd9Sstevel@tonic-gate 			/*
2892*7c478bd9Sstevel@tonic-gate 			 * Since we will never find out about the termination
2893*7c478bd9Sstevel@tonic-gate 			 * of this method, if it's a non-start method treat
2894*7c478bd9Sstevel@tonic-gate 			 * is as a failure so we don't block restarter event
2895*7c478bd9Sstevel@tonic-gate 			 * processing on it whilst it languishes in a method
2896*7c478bd9Sstevel@tonic-gate 			 * running state.
2897*7c478bd9Sstevel@tonic-gate 			 */
2898*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Failed to monitor status of "
2899*7c478bd9Sstevel@tonic-gate 			    "%s method of instance %s"), methods[method].name,
2900*7c478bd9Sstevel@tonic-gate 			    instance->fmri);
2901*7c478bd9Sstevel@tonic-gate 			if (method != IM_START)
2902*7c478bd9Sstevel@tonic-gate 				process_non_start_term(instance, IMRET_FAILURE);
2903*7c478bd9Sstevel@tonic-gate 		}
2904*7c478bd9Sstevel@tonic-gate 
2905*7c478bd9Sstevel@tonic-gate 		add_method_ids(instance, child_pid, cid, method);
2906*7c478bd9Sstevel@tonic-gate 
2907*7c478bd9Sstevel@tonic-gate 		/* do tcp tracing for those nowait instances that request it */
2908*7c478bd9Sstevel@tonic-gate 		if ((method == IM_START) && cfg->basic->do_tcp_trace &&
2909*7c478bd9Sstevel@tonic-gate 		    !cfg->basic->iswait) {
2910*7c478bd9Sstevel@tonic-gate 			char buf[INET6_ADDRSTRLEN];
2911*7c478bd9Sstevel@tonic-gate 
2912*7c478bd9Sstevel@tonic-gate 			syslog(LOG_NOTICE, "%s[%d] from %s %d",
2913*7c478bd9Sstevel@tonic-gate 			    cfg->basic->svc_name, child_pid,
2914*7c478bd9Sstevel@tonic-gate 			    inet_ntop_native(instance->remote_addr.ss_family,
2915*7c478bd9Sstevel@tonic-gate 			    SS_SINADDR(instance->remote_addr), buf,
2916*7c478bd9Sstevel@tonic-gate 			    sizeof (buf)),
2917*7c478bd9Sstevel@tonic-gate 			    ntohs(SS_PORT(instance->remote_addr)));
2918*7c478bd9Sstevel@tonic-gate 		}
2919*7c478bd9Sstevel@tonic-gate 	}
2920*7c478bd9Sstevel@tonic-gate 
2921*7c478bd9Sstevel@tonic-gate 	return (0);
2922*7c478bd9Sstevel@tonic-gate 
2923*7c478bd9Sstevel@tonic-gate prefork_failure:
2924*7c478bd9Sstevel@tonic-gate 	if (mthd_ctxt != NULL) {
2925*7c478bd9Sstevel@tonic-gate 		restarter_free_method_context(mthd_ctxt);
2926*7c478bd9Sstevel@tonic-gate 		mthd_ctxt = NULL;
2927*7c478bd9Sstevel@tonic-gate 	}
2928*7c478bd9Sstevel@tonic-gate 
2929*7c478bd9Sstevel@tonic-gate 	if (method == IM_START) {
2930*7c478bd9Sstevel@tonic-gate 		/*
2931*7c478bd9Sstevel@tonic-gate 		 * Only place a start method in maintenance if we're sure
2932*7c478bd9Sstevel@tonic-gate 		 * that the failure was non-transient.
2933*7c478bd9Sstevel@tonic-gate 		 */
2934*7c478bd9Sstevel@tonic-gate 		if (!trans_failure) {
2935*7c478bd9Sstevel@tonic-gate 			destroy_bound_fds(instance);
2936*7c478bd9Sstevel@tonic-gate 			update_state(instance, IIS_MAINTENANCE, RERR_FAULT);
2937*7c478bd9Sstevel@tonic-gate 		}
2938*7c478bd9Sstevel@tonic-gate 	} else {
2939*7c478bd9Sstevel@tonic-gate 		/* treat the failure as if the method ran and failed */
2940*7c478bd9Sstevel@tonic-gate 		process_non_start_term(instance, IMRET_FAILURE);
2941*7c478bd9Sstevel@tonic-gate 	}
2942*7c478bd9Sstevel@tonic-gate 
2943*7c478bd9Sstevel@tonic-gate 	return (-1);
2944*7c478bd9Sstevel@tonic-gate }
2945*7c478bd9Sstevel@tonic-gate 
2946*7c478bd9Sstevel@tonic-gate static int
2947*7c478bd9Sstevel@tonic-gate accept_connection(instance_t *instance, proto_info_t *pi)
2948*7c478bd9Sstevel@tonic-gate {
2949*7c478bd9Sstevel@tonic-gate 	int		fd;
2950*7c478bd9Sstevel@tonic-gate 	socklen_t	size;
2951*7c478bd9Sstevel@tonic-gate 
2952*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering accept_connection");
2953*7c478bd9Sstevel@tonic-gate 
2954*7c478bd9Sstevel@tonic-gate 	if (instance->config->basic->istlx) {
2955*7c478bd9Sstevel@tonic-gate 		fd = tlx_accept(instance->fmri, (tlx_info_t *)pi,
2956*7c478bd9Sstevel@tonic-gate 		    &(instance->remote_addr));
2957*7c478bd9Sstevel@tonic-gate 	} else {
2958*7c478bd9Sstevel@tonic-gate 		size = sizeof (instance->remote_addr);
2959*7c478bd9Sstevel@tonic-gate 		fd = accept(pi->listen_fd,
2960*7c478bd9Sstevel@tonic-gate 		    (struct sockaddr *)&(instance->remote_addr), &size);
2961*7c478bd9Sstevel@tonic-gate 		if (fd < 0)
2962*7c478bd9Sstevel@tonic-gate 			error_msg("accept: %s", strerror(errno));
2963*7c478bd9Sstevel@tonic-gate 	}
2964*7c478bd9Sstevel@tonic-gate 
2965*7c478bd9Sstevel@tonic-gate 	return (fd);
2966*7c478bd9Sstevel@tonic-gate }
2967*7c478bd9Sstevel@tonic-gate 
2968*7c478bd9Sstevel@tonic-gate /*
2969*7c478bd9Sstevel@tonic-gate  * Handle an incoming connection request for a nowait service.
2970*7c478bd9Sstevel@tonic-gate  * This involves accepting the incoming connection on a new fd. Connection
2971*7c478bd9Sstevel@tonic-gate  * rate checks are then performed, transitioning the service to the
2972*7c478bd9Sstevel@tonic-gate  * conrate offline state if these fail. Otherwise, the service's start method
2973*7c478bd9Sstevel@tonic-gate  * is run (performing TCP wrappers checks if applicable as we do), and on
2974*7c478bd9Sstevel@tonic-gate  * success concurrent copies checking is done, transitioning the service to the
2975*7c478bd9Sstevel@tonic-gate  * copies offline state if this fails.
2976*7c478bd9Sstevel@tonic-gate  */
2977*7c478bd9Sstevel@tonic-gate static void
2978*7c478bd9Sstevel@tonic-gate process_nowait_request(instance_t *instance, proto_info_t *pi)
2979*7c478bd9Sstevel@tonic-gate {
2980*7c478bd9Sstevel@tonic-gate 	basic_cfg_t		*cfg = instance->config->basic;
2981*7c478bd9Sstevel@tonic-gate 	int			ret;
2982*7c478bd9Sstevel@tonic-gate 	adt_event_data_t	*ae;
2983*7c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
2984*7c478bd9Sstevel@tonic-gate 
2985*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_nowait_req");
2986*7c478bd9Sstevel@tonic-gate 
2987*7c478bd9Sstevel@tonic-gate 	/* accept nowait service connections on a new fd */
2988*7c478bd9Sstevel@tonic-gate 	if ((instance->conn_fd = accept_connection(instance, pi)) == -1) {
2989*7c478bd9Sstevel@tonic-gate 		/*
2990*7c478bd9Sstevel@tonic-gate 		 * Failed accept. Return and allow the event loop to initiate
2991*7c478bd9Sstevel@tonic-gate 		 * another attempt later if the request is still present.
2992*7c478bd9Sstevel@tonic-gate 		 */
2993*7c478bd9Sstevel@tonic-gate 		return;
2994*7c478bd9Sstevel@tonic-gate 	}
2995*7c478bd9Sstevel@tonic-gate 
2996*7c478bd9Sstevel@tonic-gate 	/*
2997*7c478bd9Sstevel@tonic-gate 	 * Limit connection rate of nowait services. If either conn_rate_max
2998*7c478bd9Sstevel@tonic-gate 	 * or conn_rate_offline are <= 0, no connection rate limit checking
2999*7c478bd9Sstevel@tonic-gate 	 * is done. If the configured rate is exceeded, the instance is taken
3000*7c478bd9Sstevel@tonic-gate 	 * to the connrate_offline state and a timer scheduled to try and
3001*7c478bd9Sstevel@tonic-gate 	 * bring the instance back online after the configured offline time.
3002*7c478bd9Sstevel@tonic-gate 	 */
3003*7c478bd9Sstevel@tonic-gate 	if ((cfg->conn_rate_max > 0) && (cfg->conn_rate_offline > 0)) {
3004*7c478bd9Sstevel@tonic-gate 		if (instance->conn_rate_count++ == 0) {
3005*7c478bd9Sstevel@tonic-gate 			instance->conn_rate_start = time(NULL);
3006*7c478bd9Sstevel@tonic-gate 		} else if (instance->conn_rate_count >
3007*7c478bd9Sstevel@tonic-gate 		    cfg->conn_rate_max) {
3008*7c478bd9Sstevel@tonic-gate 			time_t now = time(NULL);
3009*7c478bd9Sstevel@tonic-gate 
3010*7c478bd9Sstevel@tonic-gate 			if ((now - instance->conn_rate_start) > 1) {
3011*7c478bd9Sstevel@tonic-gate 				instance->conn_rate_start = now;
3012*7c478bd9Sstevel@tonic-gate 				instance->conn_rate_count = 1;
3013*7c478bd9Sstevel@tonic-gate 			} else {
3014*7c478bd9Sstevel@tonic-gate 				/* Generate audit record */
3015*7c478bd9Sstevel@tonic-gate 				if ((ae = adt_alloc_event(audit_handle,
3016*7c478bd9Sstevel@tonic-gate 				    ADT_inetd_ratelimit)) == NULL) {
3017*7c478bd9Sstevel@tonic-gate 					error_msg(gettext("Unable to allocate "
3018*7c478bd9Sstevel@tonic-gate 					    "rate limit audit event"));
3019*7c478bd9Sstevel@tonic-gate 				} else {
3020*7c478bd9Sstevel@tonic-gate 					adt_inetd_ratelimit_t *rl =
3021*7c478bd9Sstevel@tonic-gate 					    &ae->adt_inetd_ratelimit;
3022*7c478bd9Sstevel@tonic-gate 					/*
3023*7c478bd9Sstevel@tonic-gate 					 * The inetd_ratelimit audit
3024*7c478bd9Sstevel@tonic-gate 					 * record consists of:
3025*7c478bd9Sstevel@tonic-gate 					 * 	Service name
3026*7c478bd9Sstevel@tonic-gate 					 *	Connection rate limit
3027*7c478bd9Sstevel@tonic-gate 					 */
3028*7c478bd9Sstevel@tonic-gate 					rl->service_name = cfg->svc_name;
3029*7c478bd9Sstevel@tonic-gate 					(void) snprintf(buf, sizeof (buf),
3030*7c478bd9Sstevel@tonic-gate 					    "limit=%lld", cfg->conn_rate_max);
3031*7c478bd9Sstevel@tonic-gate 					rl->limit = buf;
3032*7c478bd9Sstevel@tonic-gate 					(void) adt_put_event(ae, ADT_SUCCESS,
3033*7c478bd9Sstevel@tonic-gate 					    ADT_SUCCESS);
3034*7c478bd9Sstevel@tonic-gate 					adt_free_event(ae);
3035*7c478bd9Sstevel@tonic-gate 				}
3036*7c478bd9Sstevel@tonic-gate 
3037*7c478bd9Sstevel@tonic-gate 				error_msg(gettext(
3038*7c478bd9Sstevel@tonic-gate 				    "Instance %s has exceeded its configured "
3039*7c478bd9Sstevel@tonic-gate 				    "connection rate, additional connections "
3040*7c478bd9Sstevel@tonic-gate 				    "will not be accepted for %d seconds"),
3041*7c478bd9Sstevel@tonic-gate 				    instance->fmri, cfg->conn_rate_offline);
3042*7c478bd9Sstevel@tonic-gate 
3043*7c478bd9Sstevel@tonic-gate 				close_net_fd(instance, instance->conn_fd);
3044*7c478bd9Sstevel@tonic-gate 				instance->conn_fd = -1;
3045*7c478bd9Sstevel@tonic-gate 
3046*7c478bd9Sstevel@tonic-gate 				destroy_bound_fds(instance);
3047*7c478bd9Sstevel@tonic-gate 
3048*7c478bd9Sstevel@tonic-gate 				instance->conn_rate_count = 0;
3049*7c478bd9Sstevel@tonic-gate 
3050*7c478bd9Sstevel@tonic-gate 				instance->conn_rate_exceeded = B_TRUE;
3051*7c478bd9Sstevel@tonic-gate 				(void) run_method(instance, IM_OFFLINE, NULL);
3052*7c478bd9Sstevel@tonic-gate 
3053*7c478bd9Sstevel@tonic-gate 				return;
3054*7c478bd9Sstevel@tonic-gate 			}
3055*7c478bd9Sstevel@tonic-gate 		}
3056*7c478bd9Sstevel@tonic-gate 	}
3057*7c478bd9Sstevel@tonic-gate 
3058*7c478bd9Sstevel@tonic-gate 	ret = run_method(instance, IM_START, pi);
3059*7c478bd9Sstevel@tonic-gate 
3060*7c478bd9Sstevel@tonic-gate 	close_net_fd(instance, instance->conn_fd);
3061*7c478bd9Sstevel@tonic-gate 	instance->conn_fd = -1;
3062*7c478bd9Sstevel@tonic-gate 
3063*7c478bd9Sstevel@tonic-gate 	if (ret == -1) /* the method wasn't forked  */
3064*7c478bd9Sstevel@tonic-gate 		return;
3065*7c478bd9Sstevel@tonic-gate 
3066*7c478bd9Sstevel@tonic-gate 	instance->copies++;
3067*7c478bd9Sstevel@tonic-gate 
3068*7c478bd9Sstevel@tonic-gate 	/*
3069*7c478bd9Sstevel@tonic-gate 	 * Limit concurrent connections of nowait services.
3070*7c478bd9Sstevel@tonic-gate 	 */
3071*7c478bd9Sstevel@tonic-gate 	if (copies_limit_exceeded(instance)) {
3072*7c478bd9Sstevel@tonic-gate 		/* Generate audit record */
3073*7c478bd9Sstevel@tonic-gate 		if ((ae = adt_alloc_event(audit_handle, ADT_inetd_copylimit))
3074*7c478bd9Sstevel@tonic-gate 		    == NULL) {
3075*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("Unable to allocate copy limit "
3076*7c478bd9Sstevel@tonic-gate 			    "audit event"));
3077*7c478bd9Sstevel@tonic-gate 		} else {
3078*7c478bd9Sstevel@tonic-gate 			/*
3079*7c478bd9Sstevel@tonic-gate 			 * The inetd_copylimit audit record consists of:
3080*7c478bd9Sstevel@tonic-gate 			 *	Service name
3081*7c478bd9Sstevel@tonic-gate 			 * 	Copy limit
3082*7c478bd9Sstevel@tonic-gate 			 */
3083*7c478bd9Sstevel@tonic-gate 			ae->adt_inetd_copylimit.service_name = cfg->svc_name;
3084*7c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "limit=%lld",
3085*7c478bd9Sstevel@tonic-gate 			    cfg->max_copies);
3086*7c478bd9Sstevel@tonic-gate 			ae->adt_inetd_copylimit.limit = buf;
3087*7c478bd9Sstevel@tonic-gate 			(void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS);
3088*7c478bd9Sstevel@tonic-gate 			adt_free_event(ae);
3089*7c478bd9Sstevel@tonic-gate 		}
3090*7c478bd9Sstevel@tonic-gate 
3091*7c478bd9Sstevel@tonic-gate 		warn_msg(gettext("Instance %s has reached its maximum "
3092*7c478bd9Sstevel@tonic-gate 		    "configured copies, no new connections will be accepted"),
3093*7c478bd9Sstevel@tonic-gate 		    instance->fmri);
3094*7c478bd9Sstevel@tonic-gate 		destroy_bound_fds(instance);
3095*7c478bd9Sstevel@tonic-gate 		(void) run_method(instance, IM_OFFLINE, NULL);
3096*7c478bd9Sstevel@tonic-gate 	}
3097*7c478bd9Sstevel@tonic-gate }
3098*7c478bd9Sstevel@tonic-gate 
3099*7c478bd9Sstevel@tonic-gate /*
3100*7c478bd9Sstevel@tonic-gate  * Handle an incoming request for a wait type service.
3101*7c478bd9Sstevel@tonic-gate  * Failure rate checking is done first, taking the service to the maintenance
3102*7c478bd9Sstevel@tonic-gate  * state if the checks fail. Following this, the service's start method is run,
3103*7c478bd9Sstevel@tonic-gate  * and on success, we stop listening for new requests for this service.
3104*7c478bd9Sstevel@tonic-gate  */
3105*7c478bd9Sstevel@tonic-gate static void
3106*7c478bd9Sstevel@tonic-gate process_wait_request(instance_t *instance, const proto_info_t *pi)
3107*7c478bd9Sstevel@tonic-gate {
3108*7c478bd9Sstevel@tonic-gate 	basic_cfg_t		*cfg = instance->config->basic;
3109*7c478bd9Sstevel@tonic-gate 	int			ret;
3110*7c478bd9Sstevel@tonic-gate 	adt_event_data_t	*ae;
3111*7c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
3112*7c478bd9Sstevel@tonic-gate 
3113*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_wait_request");
3114*7c478bd9Sstevel@tonic-gate 
3115*7c478bd9Sstevel@tonic-gate 	instance->conn_fd = pi->listen_fd;
3116*7c478bd9Sstevel@tonic-gate 
3117*7c478bd9Sstevel@tonic-gate 	/*
3118*7c478bd9Sstevel@tonic-gate 	 * Detect broken servers and transition them to maintenance. If a
3119*7c478bd9Sstevel@tonic-gate 	 * wait type service exits without accepting the connection or
3120*7c478bd9Sstevel@tonic-gate 	 * consuming (reading) the datagram, that service's descriptor will
3121*7c478bd9Sstevel@tonic-gate 	 * select readable again, and inetd will fork another instance of
3122*7c478bd9Sstevel@tonic-gate 	 * the server. If either wait_fail_cnt or wait_fail_interval are <= 0,
3123*7c478bd9Sstevel@tonic-gate 	 * no failure rate detection is done.
3124*7c478bd9Sstevel@tonic-gate 	 */
3125*7c478bd9Sstevel@tonic-gate 	if ((cfg->wait_fail_cnt > 0) && (cfg->wait_fail_interval > 0)) {
3126*7c478bd9Sstevel@tonic-gate 		if (instance->fail_rate_count++ == 0) {
3127*7c478bd9Sstevel@tonic-gate 			instance->fail_rate_start = time(NULL);
3128*7c478bd9Sstevel@tonic-gate 		} else if (instance->fail_rate_count > cfg->wait_fail_cnt) {
3129*7c478bd9Sstevel@tonic-gate 			time_t now = time(NULL);
3130*7c478bd9Sstevel@tonic-gate 
3131*7c478bd9Sstevel@tonic-gate 			if ((now - instance->fail_rate_start) >
3132*7c478bd9Sstevel@tonic-gate 			    cfg->wait_fail_interval) {
3133*7c478bd9Sstevel@tonic-gate 				instance->fail_rate_start = now;
3134*7c478bd9Sstevel@tonic-gate 				instance->fail_rate_count = 1;
3135*7c478bd9Sstevel@tonic-gate 			} else {
3136*7c478bd9Sstevel@tonic-gate 				/* Generate audit record */
3137*7c478bd9Sstevel@tonic-gate 				if ((ae = adt_alloc_event(audit_handle,
3138*7c478bd9Sstevel@tonic-gate 				    ADT_inetd_failrate)) == NULL) {
3139*7c478bd9Sstevel@tonic-gate 					error_msg(gettext("Unable to allocate "
3140*7c478bd9Sstevel@tonic-gate 					    "failure rate audit event"));
3141*7c478bd9Sstevel@tonic-gate 				} else {
3142*7c478bd9Sstevel@tonic-gate 					adt_inetd_failrate_t *fr =
3143*7c478bd9Sstevel@tonic-gate 					    &ae->adt_inetd_failrate;
3144*7c478bd9Sstevel@tonic-gate 					/*
3145*7c478bd9Sstevel@tonic-gate 					 * The inetd_failrate audit record
3146*7c478bd9Sstevel@tonic-gate 					 * consists of:
3147*7c478bd9Sstevel@tonic-gate 					 * 	Service name
3148*7c478bd9Sstevel@tonic-gate 					 * 	Failure rate
3149*7c478bd9Sstevel@tonic-gate 					 *	Interval
3150*7c478bd9Sstevel@tonic-gate 					 * Last two are expressed as k=v pairs
3151*7c478bd9Sstevel@tonic-gate 					 * in the values field.
3152*7c478bd9Sstevel@tonic-gate 					 */
3153*7c478bd9Sstevel@tonic-gate 					fr->service_name = cfg->svc_name;
3154*7c478bd9Sstevel@tonic-gate 					(void) snprintf(buf, sizeof (buf),
3155*7c478bd9Sstevel@tonic-gate 					    "limit=%lld,interval=%d",
3156*7c478bd9Sstevel@tonic-gate 					    cfg->wait_fail_cnt,
3157*7c478bd9Sstevel@tonic-gate 					    cfg->wait_fail_interval);
3158*7c478bd9Sstevel@tonic-gate 					fr->values = buf;
3159*7c478bd9Sstevel@tonic-gate 					(void) adt_put_event(ae, ADT_SUCCESS,
3160*7c478bd9Sstevel@tonic-gate 					    ADT_SUCCESS);
3161*7c478bd9Sstevel@tonic-gate 					adt_free_event(ae);
3162*7c478bd9Sstevel@tonic-gate 				}
3163*7c478bd9Sstevel@tonic-gate 
3164*7c478bd9Sstevel@tonic-gate 				error_msg(gettext(
3165*7c478bd9Sstevel@tonic-gate 				    "Instance %s has exceeded its configured "
3166*7c478bd9Sstevel@tonic-gate 				    "failure rate, transitioning to "
3167*7c478bd9Sstevel@tonic-gate 				    "maintenance"), instance->fmri);
3168*7c478bd9Sstevel@tonic-gate 				instance->fail_rate_count = 0;
3169*7c478bd9Sstevel@tonic-gate 
3170*7c478bd9Sstevel@tonic-gate 				destroy_bound_fds(instance);
3171*7c478bd9Sstevel@tonic-gate 
3172*7c478bd9Sstevel@tonic-gate 				instance->maintenance_req = B_TRUE;
3173*7c478bd9Sstevel@tonic-gate 				(void) run_method(instance, IM_OFFLINE, NULL);
3174*7c478bd9Sstevel@tonic-gate 				return;
3175*7c478bd9Sstevel@tonic-gate 			}
3176*7c478bd9Sstevel@tonic-gate 		}
3177*7c478bd9Sstevel@tonic-gate 	}
3178*7c478bd9Sstevel@tonic-gate 
3179*7c478bd9Sstevel@tonic-gate 	ret = run_method(instance, IM_START, pi);
3180*7c478bd9Sstevel@tonic-gate 
3181*7c478bd9Sstevel@tonic-gate 	instance->conn_fd = -1;
3182*7c478bd9Sstevel@tonic-gate 
3183*7c478bd9Sstevel@tonic-gate 	if (ret == 0) {
3184*7c478bd9Sstevel@tonic-gate 		/*
3185*7c478bd9Sstevel@tonic-gate 		 * Stop listening for connections now we've fired off the
3186*7c478bd9Sstevel@tonic-gate 		 * server for a wait type instance.
3187*7c478bd9Sstevel@tonic-gate 		 */
3188*7c478bd9Sstevel@tonic-gate 		(void) poll_bound_fds(instance, B_FALSE);
3189*7c478bd9Sstevel@tonic-gate 	}
3190*7c478bd9Sstevel@tonic-gate }
3191*7c478bd9Sstevel@tonic-gate 
3192*7c478bd9Sstevel@tonic-gate /*
3193*7c478bd9Sstevel@tonic-gate  * Process any networks requests for each proto for each instance.
3194*7c478bd9Sstevel@tonic-gate  */
3195*7c478bd9Sstevel@tonic-gate void
3196*7c478bd9Sstevel@tonic-gate process_network_events(void)
3197*7c478bd9Sstevel@tonic-gate {
3198*7c478bd9Sstevel@tonic-gate 	instance_t	*instance;
3199*7c478bd9Sstevel@tonic-gate 
3200*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering process_network_events");
3201*7c478bd9Sstevel@tonic-gate 
3202*7c478bd9Sstevel@tonic-gate 	for (instance = uu_list_first(instance_list); instance != NULL;
3203*7c478bd9Sstevel@tonic-gate 	    instance = uu_list_next(instance_list, instance)) {
3204*7c478bd9Sstevel@tonic-gate 		basic_cfg_t	*cfg;
3205*7c478bd9Sstevel@tonic-gate 		proto_info_t	*pi;
3206*7c478bd9Sstevel@tonic-gate 
3207*7c478bd9Sstevel@tonic-gate 		/*
3208*7c478bd9Sstevel@tonic-gate 		 * Ignore instances in states that definitely don't have any
3209*7c478bd9Sstevel@tonic-gate 		 * listening fds.
3210*7c478bd9Sstevel@tonic-gate 		 */
3211*7c478bd9Sstevel@tonic-gate 		switch (instance->cur_istate) {
3212*7c478bd9Sstevel@tonic-gate 		case IIS_ONLINE:
3213*7c478bd9Sstevel@tonic-gate 		case IIS_DEGRADED:
3214*7c478bd9Sstevel@tonic-gate 		case IIS_IN_REFRESH_METHOD:
3215*7c478bd9Sstevel@tonic-gate 			break;
3216*7c478bd9Sstevel@tonic-gate 		default:
3217*7c478bd9Sstevel@tonic-gate 			continue;
3218*7c478bd9Sstevel@tonic-gate 		}
3219*7c478bd9Sstevel@tonic-gate 
3220*7c478bd9Sstevel@tonic-gate 		cfg = instance->config->basic;
3221*7c478bd9Sstevel@tonic-gate 
3222*7c478bd9Sstevel@tonic-gate 		for (pi = uu_list_first(cfg->proto_list); pi != NULL;
3223*7c478bd9Sstevel@tonic-gate 		    pi = uu_list_next(cfg->proto_list, pi)) {
3224*7c478bd9Sstevel@tonic-gate 			if ((pi->listen_fd != -1) &&
3225*7c478bd9Sstevel@tonic-gate 			    isset_pollfd(pi->listen_fd)) {
3226*7c478bd9Sstevel@tonic-gate 				if (cfg->iswait) {
3227*7c478bd9Sstevel@tonic-gate 					process_wait_request(instance, pi);
3228*7c478bd9Sstevel@tonic-gate 				} else {
3229*7c478bd9Sstevel@tonic-gate 					process_nowait_request(instance, pi);
3230*7c478bd9Sstevel@tonic-gate 				}
3231*7c478bd9Sstevel@tonic-gate 			}
3232*7c478bd9Sstevel@tonic-gate 		}
3233*7c478bd9Sstevel@tonic-gate 	}
3234*7c478bd9Sstevel@tonic-gate }
3235*7c478bd9Sstevel@tonic-gate 
3236*7c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
3237*7c478bd9Sstevel@tonic-gate static void
3238*7c478bd9Sstevel@tonic-gate sigterm_handler(int sig)
3239*7c478bd9Sstevel@tonic-gate {
3240*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering sigterm_handler");
3241*7c478bd9Sstevel@tonic-gate 
3242*7c478bd9Sstevel@tonic-gate 	got_sigterm = B_TRUE;
3243*7c478bd9Sstevel@tonic-gate }
3244*7c478bd9Sstevel@tonic-gate 
3245*7c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
3246*7c478bd9Sstevel@tonic-gate static void
3247*7c478bd9Sstevel@tonic-gate sighup_handler(int sig)
3248*7c478bd9Sstevel@tonic-gate {
3249*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering sighup_handler");
3250*7c478bd9Sstevel@tonic-gate 
3251*7c478bd9Sstevel@tonic-gate 	refresh_inetd_requested = B_TRUE;
3252*7c478bd9Sstevel@tonic-gate }
3253*7c478bd9Sstevel@tonic-gate 
3254*7c478bd9Sstevel@tonic-gate /*
3255*7c478bd9Sstevel@tonic-gate  * inetd's major work loop. This function sits in poll waiting for events
3256*7c478bd9Sstevel@tonic-gate  * to occur, processing them when they do. The possible events are
3257*7c478bd9Sstevel@tonic-gate  * master restarter requests, expired timer queue timers, stop/refresh signal
3258*7c478bd9Sstevel@tonic-gate  * requests, contract events indicating process termination, stop/refresh
3259*7c478bd9Sstevel@tonic-gate  * requests originating from one of the stop/refresh inetd processes and
3260*7c478bd9Sstevel@tonic-gate  * network events.
3261*7c478bd9Sstevel@tonic-gate  * The loop is exited when a stop request is received and processed, and
3262*7c478bd9Sstevel@tonic-gate  * all the instances have reached a suitable 'stopping' state.
3263*7c478bd9Sstevel@tonic-gate  */
3264*7c478bd9Sstevel@tonic-gate static void
3265*7c478bd9Sstevel@tonic-gate event_loop(void)
3266*7c478bd9Sstevel@tonic-gate {
3267*7c478bd9Sstevel@tonic-gate 	instance_t		*instance;
3268*7c478bd9Sstevel@tonic-gate 	int			timeout;
3269*7c478bd9Sstevel@tonic-gate 
3270*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering event_loop");
3271*7c478bd9Sstevel@tonic-gate 
3272*7c478bd9Sstevel@tonic-gate 	for (;;) {
3273*7c478bd9Sstevel@tonic-gate 		int	pret = -1;
3274*7c478bd9Sstevel@tonic-gate 
3275*7c478bd9Sstevel@tonic-gate 		timeout = iu_earliest_timer(timer_queue);
3276*7c478bd9Sstevel@tonic-gate 
3277*7c478bd9Sstevel@tonic-gate 		debug_msg("Doing signal check/poll");
3278*7c478bd9Sstevel@tonic-gate 		if (!got_sigterm && !refresh_inetd_requested) {
3279*7c478bd9Sstevel@tonic-gate 			pret = poll(poll_fds, num_pollfds, timeout);
3280*7c478bd9Sstevel@tonic-gate 			if ((pret == -1) && (errno != EINTR)) {
3281*7c478bd9Sstevel@tonic-gate 				error_msg(gettext("poll failure: %s"),
3282*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
3283*7c478bd9Sstevel@tonic-gate 				continue;
3284*7c478bd9Sstevel@tonic-gate 			}
3285*7c478bd9Sstevel@tonic-gate 			debug_msg("Exiting poll, returned: %d", pret);
3286*7c478bd9Sstevel@tonic-gate 		}
3287*7c478bd9Sstevel@tonic-gate 
3288*7c478bd9Sstevel@tonic-gate 		if (got_sigterm) {
3289*7c478bd9Sstevel@tonic-gate 			msg_fini();
3290*7c478bd9Sstevel@tonic-gate 			inetd_stop();
3291*7c478bd9Sstevel@tonic-gate 			got_sigterm = B_FALSE;
3292*7c478bd9Sstevel@tonic-gate 			goto check_if_stopped;
3293*7c478bd9Sstevel@tonic-gate 		}
3294*7c478bd9Sstevel@tonic-gate 
3295*7c478bd9Sstevel@tonic-gate 		/*
3296*7c478bd9Sstevel@tonic-gate 		 * Process any stop/refresh requests from the Unix Domain
3297*7c478bd9Sstevel@tonic-gate 		 * Socket.
3298*7c478bd9Sstevel@tonic-gate 		 */
3299*7c478bd9Sstevel@tonic-gate 		if ((pret != -1) && isset_pollfd(uds_fd)) {
3300*7c478bd9Sstevel@tonic-gate 			while (process_uds_event() == 0)
3301*7c478bd9Sstevel@tonic-gate 				;
3302*7c478bd9Sstevel@tonic-gate 		}
3303*7c478bd9Sstevel@tonic-gate 
3304*7c478bd9Sstevel@tonic-gate 		/*
3305*7c478bd9Sstevel@tonic-gate 		 * Process refresh request. We do this check after the UDS
3306*7c478bd9Sstevel@tonic-gate 		 * event check above, as it would be wasted processing if we
3307*7c478bd9Sstevel@tonic-gate 		 * started refreshing inetd based on a SIGHUP, and then were
3308*7c478bd9Sstevel@tonic-gate 		 * told to shut-down via a UDS event.
3309*7c478bd9Sstevel@tonic-gate 		 */
3310*7c478bd9Sstevel@tonic-gate 		if (refresh_inetd_requested) {
3311*7c478bd9Sstevel@tonic-gate 			refresh_inetd_requested = B_FALSE;
3312*7c478bd9Sstevel@tonic-gate 			if (!inetd_stopping)
3313*7c478bd9Sstevel@tonic-gate 				inetd_refresh();
3314*7c478bd9Sstevel@tonic-gate 		}
3315*7c478bd9Sstevel@tonic-gate 
3316*7c478bd9Sstevel@tonic-gate 		/*
3317*7c478bd9Sstevel@tonic-gate 		 * We were interrupted by a signal. Don't waste any more
3318*7c478bd9Sstevel@tonic-gate 		 * time processing a potentially inaccurate poll return.
3319*7c478bd9Sstevel@tonic-gate 		 */
3320*7c478bd9Sstevel@tonic-gate 		if (pret == -1)
3321*7c478bd9Sstevel@tonic-gate 			continue;
3322*7c478bd9Sstevel@tonic-gate 
3323*7c478bd9Sstevel@tonic-gate 		/*
3324*7c478bd9Sstevel@tonic-gate 		 * Process any instance restarter events.
3325*7c478bd9Sstevel@tonic-gate 		 */
3326*7c478bd9Sstevel@tonic-gate 		if (isset_pollfd(rst_event_pipe[PE_CONSUMER])) {
3327*7c478bd9Sstevel@tonic-gate 			while (process_restarter_event() == 0)
3328*7c478bd9Sstevel@tonic-gate 				;
3329*7c478bd9Sstevel@tonic-gate 		}
3330*7c478bd9Sstevel@tonic-gate 
3331*7c478bd9Sstevel@tonic-gate 		/*
3332*7c478bd9Sstevel@tonic-gate 		 * Process any expired timers (bind retry, con-rate offline,
3333*7c478bd9Sstevel@tonic-gate 		 * method timeouts).
3334*7c478bd9Sstevel@tonic-gate 		 */
3335*7c478bd9Sstevel@tonic-gate 		(void) iu_expire_timers(timer_queue);
3336*7c478bd9Sstevel@tonic-gate 
3337*7c478bd9Sstevel@tonic-gate 		process_terminated_methods();
3338*7c478bd9Sstevel@tonic-gate 
3339*7c478bd9Sstevel@tonic-gate 		/*
3340*7c478bd9Sstevel@tonic-gate 		 * If inetd is stopping, check whether all our managed
3341*7c478bd9Sstevel@tonic-gate 		 * instances have been stopped and we can return.
3342*7c478bd9Sstevel@tonic-gate 		 */
3343*7c478bd9Sstevel@tonic-gate 		if (inetd_stopping) {
3344*7c478bd9Sstevel@tonic-gate check_if_stopped:
3345*7c478bd9Sstevel@tonic-gate 			for (instance = uu_list_first(instance_list);
3346*7c478bd9Sstevel@tonic-gate 			    instance != NULL;
3347*7c478bd9Sstevel@tonic-gate 			    instance = uu_list_next(instance_list, instance)) {
3348*7c478bd9Sstevel@tonic-gate 				if (!instance_stopped(instance)) {
3349*7c478bd9Sstevel@tonic-gate 					debug_msg("%s not yet stopped",
3350*7c478bd9Sstevel@tonic-gate 					    instance->fmri);
3351*7c478bd9Sstevel@tonic-gate 					break;
3352*7c478bd9Sstevel@tonic-gate 				}
3353*7c478bd9Sstevel@tonic-gate 			}
3354*7c478bd9Sstevel@tonic-gate 			/* if all instances are stopped, return */
3355*7c478bd9Sstevel@tonic-gate 			if (instance == NULL)
3356*7c478bd9Sstevel@tonic-gate 				return;
3357*7c478bd9Sstevel@tonic-gate 		}
3358*7c478bd9Sstevel@tonic-gate 
3359*7c478bd9Sstevel@tonic-gate 		process_network_events();
3360*7c478bd9Sstevel@tonic-gate 	}
3361*7c478bd9Sstevel@tonic-gate }
3362*7c478bd9Sstevel@tonic-gate 
3363*7c478bd9Sstevel@tonic-gate static void
3364*7c478bd9Sstevel@tonic-gate fini(void)
3365*7c478bd9Sstevel@tonic-gate {
3366*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering fini");
3367*7c478bd9Sstevel@tonic-gate 
3368*7c478bd9Sstevel@tonic-gate 	method_fini();
3369*7c478bd9Sstevel@tonic-gate 	uds_fini();
3370*7c478bd9Sstevel@tonic-gate 	if (timer_queue != NULL)
3371*7c478bd9Sstevel@tonic-gate 		iu_tq_destroy(timer_queue);
3372*7c478bd9Sstevel@tonic-gate 	if (rst_event_handle != NULL)
3373*7c478bd9Sstevel@tonic-gate 		restarter_unbind_handle(rst_event_handle);
3374*7c478bd9Sstevel@tonic-gate 
3375*7c478bd9Sstevel@tonic-gate 	/*
3376*7c478bd9Sstevel@tonic-gate 	 * We don't explicitly close the event pipe as restarter_event_proxy()
3377*7c478bd9Sstevel@tonic-gate 	 * doesn't anticipate the pipe being closed; in the case it was trying
3378*7c478bd9Sstevel@tonic-gate 	 * to write to it it would get a SIGPIPE and result in an ungraceful
3379*7c478bd9Sstevel@tonic-gate 	 * shutdown, and in the case it was trying to read from it, safe_read()
3380*7c478bd9Sstevel@tonic-gate 	 * would return, and it would have to block itself in some way until the
3381*7c478bd9Sstevel@tonic-gate 	 * process exited. Not closing this end of the pipe prevents the first
3382*7c478bd9Sstevel@tonic-gate 	 * problem from occurring, and allows the thread in
3383*7c478bd9Sstevel@tonic-gate 	 * restarter_event_proxy() to block on the read till the process exits.
3384*7c478bd9Sstevel@tonic-gate 	 */
3385*7c478bd9Sstevel@tonic-gate 
3386*7c478bd9Sstevel@tonic-gate 	if (instance_list != NULL) {
3387*7c478bd9Sstevel@tonic-gate 		void		*cookie = NULL;
3388*7c478bd9Sstevel@tonic-gate 		instance_t	*inst;
3389*7c478bd9Sstevel@tonic-gate 
3390*7c478bd9Sstevel@tonic-gate 		while ((inst = uu_list_teardown(instance_list, &cookie)) !=
3391*7c478bd9Sstevel@tonic-gate 		    NULL)
3392*7c478bd9Sstevel@tonic-gate 			destroy_instance(inst);
3393*7c478bd9Sstevel@tonic-gate 		uu_list_destroy(instance_list);
3394*7c478bd9Sstevel@tonic-gate 	}
3395*7c478bd9Sstevel@tonic-gate 	if (instance_pool != NULL)
3396*7c478bd9Sstevel@tonic-gate 		uu_list_pool_destroy(instance_pool);
3397*7c478bd9Sstevel@tonic-gate 	tlx_fini();
3398*7c478bd9Sstevel@tonic-gate 	config_fini();
3399*7c478bd9Sstevel@tonic-gate 	repval_fini();
3400*7c478bd9Sstevel@tonic-gate 	poll_fini();
3401*7c478bd9Sstevel@tonic-gate 
3402*7c478bd9Sstevel@tonic-gate 	/* Close audit session */
3403*7c478bd9Sstevel@tonic-gate 	(void) adt_end_session(audit_handle);
3404*7c478bd9Sstevel@tonic-gate }
3405*7c478bd9Sstevel@tonic-gate 
3406*7c478bd9Sstevel@tonic-gate static int
3407*7c478bd9Sstevel@tonic-gate init(void)
3408*7c478bd9Sstevel@tonic-gate {
3409*7c478bd9Sstevel@tonic-gate 	int err;
3410*7c478bd9Sstevel@tonic-gate 
3411*7c478bd9Sstevel@tonic-gate 	debug_msg("Entering init");
3412*7c478bd9Sstevel@tonic-gate 
3413*7c478bd9Sstevel@tonic-gate 	if (repval_init() < 0)
3414*7c478bd9Sstevel@tonic-gate 		goto failed;
3415*7c478bd9Sstevel@tonic-gate 
3416*7c478bd9Sstevel@tonic-gate 	if (config_init() < 0)
3417*7c478bd9Sstevel@tonic-gate 		goto failed;
3418*7c478bd9Sstevel@tonic-gate 
3419*7c478bd9Sstevel@tonic-gate 	if (tlx_init() < 0)
3420*7c478bd9Sstevel@tonic-gate 		goto failed;
3421*7c478bd9Sstevel@tonic-gate 
3422*7c478bd9Sstevel@tonic-gate 	/* Setup instance list. */
3423*7c478bd9Sstevel@tonic-gate 	if ((instance_pool = uu_list_pool_create("instance_pool",
3424*7c478bd9Sstevel@tonic-gate 	    sizeof (instance_t), offsetof(instance_t, link), NULL,
3425*7c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG)) == NULL) {
3426*7c478bd9Sstevel@tonic-gate 		error_msg("%s: %s",
3427*7c478bd9Sstevel@tonic-gate 		    gettext("Failed to create instance pool"),
3428*7c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
3429*7c478bd9Sstevel@tonic-gate 		goto failed;
3430*7c478bd9Sstevel@tonic-gate 	}
3431*7c478bd9Sstevel@tonic-gate 	if ((instance_list = uu_list_create(instance_pool, NULL, 0)) == NULL) {
3432*7c478bd9Sstevel@tonic-gate 		error_msg("%s: %s",
3433*7c478bd9Sstevel@tonic-gate 		    gettext("Failed to create instance list"),
3434*7c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
3435*7c478bd9Sstevel@tonic-gate 		goto failed;
3436*7c478bd9Sstevel@tonic-gate 	}
3437*7c478bd9Sstevel@tonic-gate 
3438*7c478bd9Sstevel@tonic-gate 	/*
3439*7c478bd9Sstevel@tonic-gate 	 * Create event pipe to communicate events with the main event
3440*7c478bd9Sstevel@tonic-gate 	 * loop and add it to the event loop's fdset.
3441*7c478bd9Sstevel@tonic-gate 	 */
3442*7c478bd9Sstevel@tonic-gate 	if (pipe(rst_event_pipe) < 0) {
3443*7c478bd9Sstevel@tonic-gate 		error_msg("pipe: %s", strerror(errno));
3444*7c478bd9Sstevel@tonic-gate 		goto failed;
3445*7c478bd9Sstevel@tonic-gate 	}
3446*7c478bd9Sstevel@tonic-gate 	/*
3447*7c478bd9Sstevel@tonic-gate 	 * We only leave the producer end to block on reads/writes as we
3448*7c478bd9Sstevel@tonic-gate 	 * can't afford to block in the main thread, yet need to in
3449*7c478bd9Sstevel@tonic-gate 	 * the restarter event thread, so it can sit and wait for an
3450*7c478bd9Sstevel@tonic-gate 	 * acknowledgement to be written to the pipe.
3451*7c478bd9Sstevel@tonic-gate 	 */
3452*7c478bd9Sstevel@tonic-gate 	disable_blocking(rst_event_pipe[PE_CONSUMER]);
3453*7c478bd9Sstevel@tonic-gate 	if ((set_pollfd(rst_event_pipe[PE_CONSUMER], POLLIN)) == -1)
3454*7c478bd9Sstevel@tonic-gate 		goto failed;
3455*7c478bd9Sstevel@tonic-gate 
3456*7c478bd9Sstevel@tonic-gate 	/*
3457*7c478bd9Sstevel@tonic-gate 	 * Register with master restarter for managed service events. This
3458*7c478bd9Sstevel@tonic-gate 	 * will fail, amongst other reasons, if inetd is already running.
3459*7c478bd9Sstevel@tonic-gate 	 */
3460*7c478bd9Sstevel@tonic-gate 	if ((err = restarter_bind_handle(RESTARTER_EVENT_VERSION,
3461*7c478bd9Sstevel@tonic-gate 	    INETD_INSTANCE_FMRI, restarter_event_proxy, 0,
3462*7c478bd9Sstevel@tonic-gate 	    &rst_event_handle)) != 0) {
3463*7c478bd9Sstevel@tonic-gate 		error_msg(gettext(
3464*7c478bd9Sstevel@tonic-gate 		    "Failed to register for restarter events: %s"),
3465*7c478bd9Sstevel@tonic-gate 		    strerror(err));
3466*7c478bd9Sstevel@tonic-gate 		goto failed;
3467*7c478bd9Sstevel@tonic-gate 	}
3468*7c478bd9Sstevel@tonic-gate 
3469*7c478bd9Sstevel@tonic-gate 	if (contract_init() < 0)
3470*7c478bd9Sstevel@tonic-gate 		goto failed;
3471*7c478bd9Sstevel@tonic-gate 
3472*7c478bd9Sstevel@tonic-gate 	if ((timer_queue = iu_tq_create()) == NULL) {
3473*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to create timer queue."));
3474*7c478bd9Sstevel@tonic-gate 		goto failed;
3475*7c478bd9Sstevel@tonic-gate 	}
3476*7c478bd9Sstevel@tonic-gate 
3477*7c478bd9Sstevel@tonic-gate 	if (uds_init() < 0)
3478*7c478bd9Sstevel@tonic-gate 		goto failed;
3479*7c478bd9Sstevel@tonic-gate 
3480*7c478bd9Sstevel@tonic-gate 	if (method_init() < 0)
3481*7c478bd9Sstevel@tonic-gate 		goto failed;
3482*7c478bd9Sstevel@tonic-gate 
3483*7c478bd9Sstevel@tonic-gate 	/* Initialize auditing session */
3484*7c478bd9Sstevel@tonic-gate 	if (adt_start_session(&audit_handle, NULL, ADT_USE_PROC_DATA) != 0) {
3485*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Unable to start audit session"));
3486*7c478bd9Sstevel@tonic-gate 	}
3487*7c478bd9Sstevel@tonic-gate 
3488*7c478bd9Sstevel@tonic-gate 	/*
3489*7c478bd9Sstevel@tonic-gate 	 * Initialize signal dispositions/masks
3490*7c478bd9Sstevel@tonic-gate 	 */
3491*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, sighup_handler);
3492*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm_handler);
3493*7c478bd9Sstevel@tonic-gate 	(void) sigignore(SIGINT);
3494*7c478bd9Sstevel@tonic-gate 
3495*7c478bd9Sstevel@tonic-gate 	return (0);
3496*7c478bd9Sstevel@tonic-gate 
3497*7c478bd9Sstevel@tonic-gate failed:
3498*7c478bd9Sstevel@tonic-gate 	fini();
3499*7c478bd9Sstevel@tonic-gate 	return (-1);
3500*7c478bd9Sstevel@tonic-gate }
3501*7c478bd9Sstevel@tonic-gate 
3502*7c478bd9Sstevel@tonic-gate static int
3503*7c478bd9Sstevel@tonic-gate start_method(void)
3504*7c478bd9Sstevel@tonic-gate {
3505*7c478bd9Sstevel@tonic-gate 	int	i;
3506*7c478bd9Sstevel@tonic-gate 	int	pipe_fds[2];
3507*7c478bd9Sstevel@tonic-gate 	int	child;
3508*7c478bd9Sstevel@tonic-gate 
3509*7c478bd9Sstevel@tonic-gate 	debug_msg("ENTERING START_METHOD:");
3510*7c478bd9Sstevel@tonic-gate 
3511*7c478bd9Sstevel@tonic-gate 	/* Create pipe for child to notify parent of initialization success. */
3512*7c478bd9Sstevel@tonic-gate 	if (pipe(pipe_fds) < 0) {
3513*7c478bd9Sstevel@tonic-gate 		debug_msg("pipe: %s", strerror(errno));
3514*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
3515*7c478bd9Sstevel@tonic-gate 	}
3516*7c478bd9Sstevel@tonic-gate 
3517*7c478bd9Sstevel@tonic-gate 	if ((child = fork()) == -1) {
3518*7c478bd9Sstevel@tonic-gate 		debug_msg("fork: %s", strerror(errno));
3519*7c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_CONSUMER]);
3520*7c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_PRODUCER]);
3521*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_OTHER);
3522*7c478bd9Sstevel@tonic-gate 	} else if (child > 0) {			/* parent */
3523*7c478bd9Sstevel@tonic-gate 
3524*7c478bd9Sstevel@tonic-gate 		/* Wait on child to return success of initialization. */
3525*7c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_PRODUCER]);
3526*7c478bd9Sstevel@tonic-gate 		if ((safe_read(pipe_fds[PE_CONSUMER], &i, sizeof (i)) != 0) ||
3527*7c478bd9Sstevel@tonic-gate 		    (i < 0)) {
3528*7c478bd9Sstevel@tonic-gate 			error_msg(gettext(
3529*7c478bd9Sstevel@tonic-gate 			    "Initialization failed, unable to start"));
3530*7c478bd9Sstevel@tonic-gate 			(void) close(pipe_fds[PE_CONSUMER]);
3531*7c478bd9Sstevel@tonic-gate 			/*
3532*7c478bd9Sstevel@tonic-gate 			 * Batch all initialization errors as 'other' errors,
3533*7c478bd9Sstevel@tonic-gate 			 * resulting in retries being attempted.
3534*7c478bd9Sstevel@tonic-gate 			 */
3535*7c478bd9Sstevel@tonic-gate 			return (SMF_EXIT_ERR_OTHER);
3536*7c478bd9Sstevel@tonic-gate 		} else {
3537*7c478bd9Sstevel@tonic-gate 			(void) close(pipe_fds[PE_CONSUMER]);
3538*7c478bd9Sstevel@tonic-gate 			return (SMF_EXIT_OK);
3539*7c478bd9Sstevel@tonic-gate 		}
3540*7c478bd9Sstevel@tonic-gate 	} else {				/* child */
3541*7c478bd9Sstevel@tonic-gate 		/*
3542*7c478bd9Sstevel@tonic-gate 		 * Perform initialization and return success code down
3543*7c478bd9Sstevel@tonic-gate 		 * the pipe.
3544*7c478bd9Sstevel@tonic-gate 		 */
3545*7c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_CONSUMER]);
3546*7c478bd9Sstevel@tonic-gate 		i = init();
3547*7c478bd9Sstevel@tonic-gate 		if ((safe_write(pipe_fds[PE_PRODUCER], &i, sizeof (i)) < 0) ||
3548*7c478bd9Sstevel@tonic-gate 		    (i < 0)) {
3549*7c478bd9Sstevel@tonic-gate 			error_msg(gettext("pipe write failure: %s"),
3550*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
3551*7c478bd9Sstevel@tonic-gate 			exit(1);
3552*7c478bd9Sstevel@tonic-gate 		}
3553*7c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[PE_PRODUCER]);
3554*7c478bd9Sstevel@tonic-gate 
3555*7c478bd9Sstevel@tonic-gate 		(void) setsid();
3556*7c478bd9Sstevel@tonic-gate 
3557*7c478bd9Sstevel@tonic-gate 		/*
3558*7c478bd9Sstevel@tonic-gate 		 * Log a message if the configuration file has changed since
3559*7c478bd9Sstevel@tonic-gate 		 * inetconv was last run.
3560*7c478bd9Sstevel@tonic-gate 		 */
3561*7c478bd9Sstevel@tonic-gate 		check_conf_file();
3562*7c478bd9Sstevel@tonic-gate 
3563*7c478bd9Sstevel@tonic-gate 		event_loop();
3564*7c478bd9Sstevel@tonic-gate 
3565*7c478bd9Sstevel@tonic-gate 		fini();
3566*7c478bd9Sstevel@tonic-gate 		debug_msg("inetd stopped");
3567*7c478bd9Sstevel@tonic-gate 		msg_fini();
3568*7c478bd9Sstevel@tonic-gate 		exit(0);
3569*7c478bd9Sstevel@tonic-gate 	}
3570*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3571*7c478bd9Sstevel@tonic-gate }
3572*7c478bd9Sstevel@tonic-gate 
3573*7c478bd9Sstevel@tonic-gate /*
3574*7c478bd9Sstevel@tonic-gate  * When inetd is run from outside the SMF, this message is output to provide
3575*7c478bd9Sstevel@tonic-gate  * the person invoking inetd with further information that will help them
3576*7c478bd9Sstevel@tonic-gate  * understand how to start and stop inetd, and to achieve the other
3577*7c478bd9Sstevel@tonic-gate  * behaviors achievable with the legacy inetd command line interface, if
3578*7c478bd9Sstevel@tonic-gate  * it is possible.
3579*7c478bd9Sstevel@tonic-gate  */
3580*7c478bd9Sstevel@tonic-gate static void
3581*7c478bd9Sstevel@tonic-gate legacy_usage(void)
3582*7c478bd9Sstevel@tonic-gate {
3583*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3584*7c478bd9Sstevel@tonic-gate 	    "inetd is now an smf(5) managed service and can no longer be run "
3585*7c478bd9Sstevel@tonic-gate 	    "from the\n"
3586*7c478bd9Sstevel@tonic-gate 	    "command line. To enable or disable inetd refer to svcadm(1M) on\n"
3587*7c478bd9Sstevel@tonic-gate 	    "how to enable \"%s\", the inetd instance.\n"
3588*7c478bd9Sstevel@tonic-gate 	    "\n"
3589*7c478bd9Sstevel@tonic-gate 	    "The traditional inetd command line option mappings are:\n"
3590*7c478bd9Sstevel@tonic-gate 	    "\t-d : there is no supported debug output\n"
3591*7c478bd9Sstevel@tonic-gate 	    "\t-s : inetd is only runnable from within the SMF\n"
3592*7c478bd9Sstevel@tonic-gate 	    "\t-t : See inetadm(1M) on how to enable TCP tracing\n"
3593*7c478bd9Sstevel@tonic-gate 	    "\t-r : See inetadm(1M) on how to set a failure rate\n"
3594*7c478bd9Sstevel@tonic-gate 	    "\n"
3595*7c478bd9Sstevel@tonic-gate 	    "To specify an alternative configuration file see svccfg(1M)\n"
3596*7c478bd9Sstevel@tonic-gate 	    "for how to modify the \"%s/%s\" string type property of\n"
3597*7c478bd9Sstevel@tonic-gate 	    "the inetd instance, and modify it according to the syntax:\n"
3598*7c478bd9Sstevel@tonic-gate 	    "\"%s [alt_config_file] %%m\".\n"
3599*7c478bd9Sstevel@tonic-gate 	    "\n"
3600*7c478bd9Sstevel@tonic-gate 	    "For further information on inetd see inetd(1M).\n",
3601*7c478bd9Sstevel@tonic-gate 	    INETD_INSTANCE_FMRI, START_METHOD_ARG, SCF_PROPERTY_EXEC,
3602*7c478bd9Sstevel@tonic-gate 	    INETD_PATH);
3603*7c478bd9Sstevel@tonic-gate }
3604*7c478bd9Sstevel@tonic-gate 
3605*7c478bd9Sstevel@tonic-gate /*
3606*7c478bd9Sstevel@tonic-gate  * Usage message printed out for usage errors when running under the SMF.
3607*7c478bd9Sstevel@tonic-gate  */
3608*7c478bd9Sstevel@tonic-gate static void
3609*7c478bd9Sstevel@tonic-gate smf_usage(const char *arg0)
3610*7c478bd9Sstevel@tonic-gate {
3611*7c478bd9Sstevel@tonic-gate 	error_msg("Usage: %s [alt_conf_file] %s|%s|%s", arg0, START_METHOD_ARG,
3612*7c478bd9Sstevel@tonic-gate 	    STOP_METHOD_ARG, REFRESH_METHOD_ARG);
3613*7c478bd9Sstevel@tonic-gate }
3614*7c478bd9Sstevel@tonic-gate 
3615*7c478bd9Sstevel@tonic-gate /*
3616*7c478bd9Sstevel@tonic-gate  * Returns B_TRUE if we're being run from within the SMF, else B_FALSE.
3617*7c478bd9Sstevel@tonic-gate  */
3618*7c478bd9Sstevel@tonic-gate static boolean_t
3619*7c478bd9Sstevel@tonic-gate run_through_smf(void)
3620*7c478bd9Sstevel@tonic-gate {
3621*7c478bd9Sstevel@tonic-gate 	char *fmri;
3622*7c478bd9Sstevel@tonic-gate 
3623*7c478bd9Sstevel@tonic-gate 	/*
3624*7c478bd9Sstevel@tonic-gate 	 * check if the instance fmri environment variable has been set by
3625*7c478bd9Sstevel@tonic-gate 	 * our restarter.
3626*7c478bd9Sstevel@tonic-gate 	 */
3627*7c478bd9Sstevel@tonic-gate 	return (((fmri = getenv("SMF_FMRI")) != NULL) &&
3628*7c478bd9Sstevel@tonic-gate 	    (strcmp(fmri, INETD_INSTANCE_FMRI) == 0));
3629*7c478bd9Sstevel@tonic-gate }
3630*7c478bd9Sstevel@tonic-gate 
3631*7c478bd9Sstevel@tonic-gate int
3632*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3633*7c478bd9Sstevel@tonic-gate {
3634*7c478bd9Sstevel@tonic-gate 	char		*method;
3635*7c478bd9Sstevel@tonic-gate 	int		ret;
3636*7c478bd9Sstevel@tonic-gate 
3637*7c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
3638*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
3639*7c478bd9Sstevel@tonic-gate #endif
3640*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3641*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3642*7c478bd9Sstevel@tonic-gate 
3643*7c478bd9Sstevel@tonic-gate 	if (!run_through_smf()) {
3644*7c478bd9Sstevel@tonic-gate 		legacy_usage();
3645*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_NOSMF);
3646*7c478bd9Sstevel@tonic-gate 	}
3647*7c478bd9Sstevel@tonic-gate 
3648*7c478bd9Sstevel@tonic-gate 	msg_init();	/* setup logging */
3649*7c478bd9Sstevel@tonic-gate 
3650*7c478bd9Sstevel@tonic-gate 	/* inetd invocation syntax is inetd [alt_conf_file] method_name */
3651*7c478bd9Sstevel@tonic-gate 
3652*7c478bd9Sstevel@tonic-gate 	switch (argc) {
3653*7c478bd9Sstevel@tonic-gate 	case 2:
3654*7c478bd9Sstevel@tonic-gate 		method = argv[1];
3655*7c478bd9Sstevel@tonic-gate 		break;
3656*7c478bd9Sstevel@tonic-gate 	case 3:
3657*7c478bd9Sstevel@tonic-gate 		conf_file = argv[1];
3658*7c478bd9Sstevel@tonic-gate 		method = argv[2];
3659*7c478bd9Sstevel@tonic-gate 		break;
3660*7c478bd9Sstevel@tonic-gate 	default:
3661*7c478bd9Sstevel@tonic-gate 		smf_usage(argv[0]);
3662*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_CONFIG);
3663*7c478bd9Sstevel@tonic-gate 
3664*7c478bd9Sstevel@tonic-gate 	}
3665*7c478bd9Sstevel@tonic-gate 
3666*7c478bd9Sstevel@tonic-gate 	if (strcmp(method, START_METHOD_ARG) == 0) {
3667*7c478bd9Sstevel@tonic-gate 		ret = start_method();
3668*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(method, STOP_METHOD_ARG) == 0) {
3669*7c478bd9Sstevel@tonic-gate 		ret = stop_method();
3670*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(method, REFRESH_METHOD_ARG) == 0) {
3671*7c478bd9Sstevel@tonic-gate 		ret = refresh_method();
3672*7c478bd9Sstevel@tonic-gate 	} else {
3673*7c478bd9Sstevel@tonic-gate 		smf_usage(argv[0]);
3674*7c478bd9Sstevel@tonic-gate 		return (SMF_EXIT_ERR_CONFIG);
3675*7c478bd9Sstevel@tonic-gate 	}
3676*7c478bd9Sstevel@tonic-gate 
3677*7c478bd9Sstevel@tonic-gate 	return (ret);
3678*7c478bd9Sstevel@tonic-gate }
3679