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