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