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