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