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