xref: /titanic_51/usr/src/cmd/cmd-inet/usr.lib/inetd/inetd.c (revision b72c368a02e0464faeef362bc5a1cf0fc69981da)
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 2009 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:
1730 		switch (instance->cur_istate) {
1731 		case IIS_OFFLINE_CONRATE:
1732 		case IIS_OFFLINE_BIND:
1733 		case IIS_OFFLINE_COPIES:
1734 			/*
1735 			 * inetd must be closing down as we wouldn't get this
1736 			 * event in one of these states from the master
1737 			 * restarter. Take the instance to the offline resting
1738 			 * state.
1739 			 */
1740 			if (instance->cur_istate == IIS_OFFLINE_BIND) {
1741 				cancel_bind_timer(instance);
1742 			} else if (instance->cur_istate ==
1743 			    IIS_OFFLINE_CONRATE) {
1744 				cancel_inst_timer(instance);
1745 			}
1746 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
1747 			goto done;
1748 		}
1749 		break;
1750 	}
1751 
1752 	switch (instance->cur_istate) {
1753 	case IIS_OFFLINE:
1754 		switch (event) {
1755 		case RESTARTER_EVENT_TYPE_START:
1756 			/*
1757 			 * Dependencies are met, let's take the service online.
1758 			 * Only try and bind for a wait type service if
1759 			 * no process is running on its behalf. Otherwise, just
1760 			 * mark the service online and binding will be attempted
1761 			 * when the process exits.
1762 			 */
1763 			if (!(instance->config->basic->iswait &&
1764 			    (uu_list_first(instance->start_pids) != NULL))) {
1765 				create_bound_fds(instance);
1766 			} else {
1767 				update_state(instance, IIS_ONLINE, RERR_NONE);
1768 			}
1769 			break;
1770 		case RESTARTER_EVENT_TYPE_DISABLE:
1771 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1772 			/*
1773 			 * The instance should be disabled, so run the
1774 			 * instance's disabled method that will do the work
1775 			 * to take it there.
1776 			 */
1777 			(void) run_method(instance, IM_DISABLE, NULL);
1778 			break;
1779 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1780 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1781 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1782 			/*
1783 			 * The master restarter has requested the instance
1784 			 * go to maintenance; since we're already offline
1785 			 * just update the state to the maintenance state.
1786 			 */
1787 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1788 			break;
1789 		}
1790 		break;
1791 
1792 	case IIS_OFFLINE_BIND:
1793 		switch (event) {
1794 		case RESTARTER_EVENT_TYPE_DISABLE:
1795 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1796 			/*
1797 			 * The instance should be disabled. Firstly, as for
1798 			 * the above dependencies unmet comment, cancel
1799 			 * the bind retry timer and update the state to
1800 			 * offline. Then, run the disable method to do the
1801 			 * work to take the instance from offline to
1802 			 * disabled.
1803 			 */
1804 			cancel_bind_timer(instance);
1805 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
1806 			(void) run_method(instance, IM_DISABLE, NULL);
1807 			break;
1808 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1809 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1810 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1811 			/*
1812 			 * The master restarter has requested the instance
1813 			 * be placed in the maintenance state. Cancel the
1814 			 * outstanding retry timer, and since we're already
1815 			 * offline, update the state to maintenance.
1816 			 */
1817 			cancel_bind_timer(instance);
1818 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1819 			break;
1820 		}
1821 		break;
1822 
1823 	case IIS_DEGRADED:
1824 	case IIS_ONLINE:
1825 		switch (event) {
1826 		case RESTARTER_EVENT_TYPE_DISABLE:
1827 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1828 			/*
1829 			 * The instance needs to be disabled. Do the same work
1830 			 * as for the dependencies unmet event below to
1831 			 * take the instance offline.
1832 			 */
1833 			destroy_bound_fds(instance);
1834 			/*
1835 			 * Indicate that the offline method is being run
1836 			 * as part of going to the disabled state, and to
1837 			 * carry on this transition.
1838 			 */
1839 			instance->disable_req = B_TRUE;
1840 			(void) run_method(instance, IM_OFFLINE, NULL);
1841 			break;
1842 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1843 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1844 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1845 			/*
1846 			 * The master restarter has requested the instance be
1847 			 * placed in the maintenance state. This involves
1848 			 * firstly taking the service offline, so do the
1849 			 * same work as for the dependencies unmet event
1850 			 * below. We set the maintenance_req flag to
1851 			 * indicate that when we get to the offline state
1852 			 * we should be placed directly into the maintenance
1853 			 * state.
1854 			 */
1855 			instance->maintenance_req = B_TRUE;
1856 			/* FALLTHROUGH */
1857 		case RESTARTER_EVENT_TYPE_STOP:
1858 			/*
1859 			 * Dependencies have become unmet. Close and
1860 			 * stop listening on the instance's network file
1861 			 * descriptor, and run the offline method to do
1862 			 * any work required to take us to the offline state.
1863 			 */
1864 			destroy_bound_fds(instance);
1865 			(void) run_method(instance, IM_OFFLINE, NULL);
1866 		}
1867 		break;
1868 
1869 	case IIS_UNINITIALIZED:
1870 		if (event == RESTARTER_EVENT_TYPE_DISABLE ||
1871 		    event == RESTARTER_EVENT_TYPE_ADMIN_DISABLE) {
1872 			update_state(instance, IIS_DISABLED, RERR_NONE);
1873 			break;
1874 		} else if (event != RESTARTER_EVENT_TYPE_ENABLE) {
1875 			/*
1876 			 * Ignore other events until we know whether we're
1877 			 * enabled or not.
1878 			 */
1879 			break;
1880 		}
1881 
1882 		/*
1883 		 * We've got an enabled event; make use of the handling in the
1884 		 * disable case.
1885 		 */
1886 		/* FALLTHROUGH */
1887 
1888 	case IIS_DISABLED:
1889 		switch (event) {
1890 		case RESTARTER_EVENT_TYPE_ENABLE:
1891 			/*
1892 			 * The instance needs enabling. Commence reading its
1893 			 * configuration and if successful place the instance
1894 			 * in the offline state and let process_offline_inst()
1895 			 * take it from there.
1896 			 */
1897 			destroy_instance_cfg(instance->config);
1898 			instance->config = read_instance_cfg(instance->fmri);
1899 			if (instance->config != NULL) {
1900 				update_state(instance, IIS_OFFLINE,
1901 				    RERR_RESTART);
1902 				process_offline_inst(instance);
1903 			} else {
1904 				log_invalid_cfg(instance->fmri);
1905 				update_state(instance, IIS_MAINTENANCE,
1906 				    RERR_RESTART);
1907 			}
1908 
1909 			break;
1910 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1911 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1912 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1913 			/*
1914 			 * The master restarter has requested the instance be
1915 			 * placed in the maintenance state, so just update its
1916 			 * state to maintenance.
1917 			 */
1918 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1919 			break;
1920 		}
1921 		break;
1922 
1923 	case IIS_MAINTENANCE:
1924 		switch (event) {
1925 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
1926 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
1927 			/*
1928 			 * The master restarter has requested that the instance
1929 			 * be taken out of maintenance. Read its configuration,
1930 			 * and if successful place the instance in the offline
1931 			 * state and call process_offline_inst() to take it
1932 			 * from there.
1933 			 */
1934 			destroy_instance_cfg(instance->config);
1935 			instance->config = read_instance_cfg(instance->fmri);
1936 			if (instance->config != NULL) {
1937 				update_state(instance, IIS_OFFLINE,
1938 				    RERR_RESTART);
1939 				process_offline_inst(instance);
1940 			} else {
1941 				boolean_t enabled;
1942 
1943 				/*
1944 				 * The configuration was invalid. If the
1945 				 * service has disabled requested, let's
1946 				 * just place the instance in disabled even
1947 				 * though we haven't been able to run its
1948 				 * disable method, as the slightly incorrect
1949 				 * state is likely to be less of an issue to
1950 				 * an administrator than refusing to move an
1951 				 * instance to disabled. If disable isn't
1952 				 * requested, re-mark the service's state
1953 				 * as maintenance, so the administrator can
1954 				 * see the request was processed.
1955 				 */
1956 				if ((read_enable_merged(instance->fmri,
1957 				    &enabled) == 0) && !enabled) {
1958 					update_state(instance, IIS_DISABLED,
1959 					    RERR_RESTART);
1960 				} else {
1961 					log_invalid_cfg(instance->fmri);
1962 					update_state(instance, IIS_MAINTENANCE,
1963 					    RERR_FAULT);
1964 				}
1965 			}
1966 			break;
1967 		}
1968 		break;
1969 
1970 	case IIS_OFFLINE_CONRATE:
1971 		switch (event) {
1972 		case RESTARTER_EVENT_TYPE_DISABLE:
1973 			/*
1974 			 * The instance wants disabling. Take the instance
1975 			 * offline as for the dependencies unmet event above,
1976 			 * and then from there run the disable method to do
1977 			 * the work to take the instance to the disabled state.
1978 			 */
1979 			cancel_inst_timer(instance);
1980 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
1981 			(void) run_method(instance, IM_DISABLE, NULL);
1982 			break;
1983 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
1984 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
1985 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
1986 			/*
1987 			 * The master restarter has requested the instance
1988 			 * be taken to maintenance. Cancel the timer setup
1989 			 * when we entered this state, and go directly to
1990 			 * maintenance.
1991 			 */
1992 			cancel_inst_timer(instance);
1993 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
1994 			break;
1995 		}
1996 		break;
1997 
1998 	case IIS_OFFLINE_COPIES:
1999 		switch (event) {
2000 		case RESTARTER_EVENT_TYPE_DISABLE:
2001 			/*
2002 			 * The instance wants disabling. Update the state
2003 			 * to offline, and run the disable method to do the
2004 			 * work to take it to the disabled state.
2005 			 */
2006 			update_state(instance, IIS_OFFLINE, RERR_RESTART);
2007 			(void) run_method(instance, IM_DISABLE, NULL);
2008 			break;
2009 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
2010 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
2011 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
2012 			/*
2013 			 * The master restarter has requested the instance be
2014 			 * placed in maintenance. Since it's already offline
2015 			 * simply update the state.
2016 			 */
2017 			update_state(instance, IIS_MAINTENANCE, RERR_RESTART);
2018 			break;
2019 		}
2020 		break;
2021 
2022 	default:
2023 		debug_msg("handle_restarter_event: instance in an "
2024 		    "unexpected state");
2025 		assert(0);
2026 	}
2027 
2028 done:
2029 	if (send_ack)
2030 		ack_restarter_event(B_TRUE);
2031 }
2032 
2033 /*
2034  * Tries to read and process an event from the event pipe. If there isn't one
2035  * or an error occurred processing the event it returns -1. Else, if the event
2036  * is for an instance we're not already managing we read its state, add it to
2037  * our list to manage, and if appropriate read its configuration. Whether it's
2038  * new to us or not, we then handle the specific event.
2039  * Returns 0 if an event was read and processed successfully, else -1.
2040  */
2041 static int
2042 process_restarter_event(void)
2043 {
2044 	char			*fmri;
2045 	size_t			fmri_size;
2046 	restarter_event_type_t  event_type;
2047 	instance_t		*instance;
2048 	restarter_event_t	*event;
2049 	ssize_t			sz;
2050 
2051 	/*
2052 	 * Try to read an event pointer from the event pipe.
2053 	 */
2054 	errno = 0;
2055 	switch (safe_read(rst_event_pipe[PE_CONSUMER], &event,
2056 	    sizeof (event))) {
2057 	case 0:
2058 		break;
2059 	case  1:
2060 		if (errno == EAGAIN)	/* no event to read */
2061 			return (-1);
2062 
2063 		/* other end of pipe closed */
2064 
2065 		/* FALLTHROUGH */
2066 	default:			/* unexpected read error */
2067 		/*
2068 		 * There's something wrong with the event pipe. Let's
2069 		 * shutdown and be restarted.
2070 		 */
2071 		inetd_stop();
2072 		return (-1);
2073 	}
2074 
2075 	/*
2076 	 * Check if we're currently managing the instance which the event
2077 	 * pertains to. If not, read its complete state and add it to our
2078 	 * list to manage.
2079 	 */
2080 
2081 	fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2082 	if ((fmri = malloc(fmri_size)) == NULL) {
2083 		error_msg(strerror(errno));
2084 		goto fail;
2085 	}
2086 	sz = restarter_event_get_instance(event, fmri, fmri_size);
2087 	if (sz >= fmri_size)
2088 		assert(0);
2089 
2090 	for (instance = uu_list_first(instance_list); instance != NULL;
2091 	    instance = uu_list_next(instance_list, instance)) {
2092 		if (strcmp(instance->fmri, fmri) == 0)
2093 			break;
2094 	}
2095 
2096 	if (instance == NULL) {
2097 		int err;
2098 
2099 		debug_msg("New instance to manage: %s", fmri);
2100 
2101 		if (((instance = create_instance(fmri)) == NULL) ||
2102 		    (retrieve_instance_state(instance) != 0) ||
2103 		    (retrieve_method_pids(instance) != 0)) {
2104 			destroy_instance(instance);
2105 			free(fmri);
2106 			goto fail;
2107 		}
2108 
2109 		if (((err = iterate_repository_contracts(instance, 0))
2110 		    != 0) && (err != ENOENT)) {
2111 			error_msg(gettext(
2112 			    "Failed to adopt contracts of instance %s: %s"),
2113 			    instance->fmri, strerror(err));
2114 			destroy_instance(instance);
2115 			free(fmri);
2116 			goto fail;
2117 		}
2118 
2119 		uu_list_node_init(instance, &instance->link, instance_pool);
2120 		(void) uu_list_insert_after(instance_list, NULL, instance);
2121 
2122 		/*
2123 		 * Only read configuration for instances that aren't in any of
2124 		 * the disabled, maintenance or uninitialized states, since
2125 		 * they'll read it on state exit.
2126 		 */
2127 		if ((instance->cur_istate != IIS_DISABLED) &&
2128 		    (instance->cur_istate != IIS_MAINTENANCE) &&
2129 		    (instance->cur_istate != IIS_UNINITIALIZED)) {
2130 			instance->config = read_instance_cfg(instance->fmri);
2131 			if (instance->config == NULL) {
2132 				log_invalid_cfg(instance->fmri);
2133 				update_state(instance, IIS_MAINTENANCE,
2134 				    RERR_FAULT);
2135 			}
2136 		}
2137 	}
2138 
2139 	free(fmri);
2140 
2141 	event_type = restarter_event_get_type(event);
2142 	debug_msg("Event type: %d for instance: %s", event_type,
2143 	    instance->fmri);
2144 
2145 	/*
2146 	 * If the instance is currently running a method, don't process the
2147 	 * event now, but attach it to the instance for processing when
2148 	 * the instance finishes its transition.
2149 	 */
2150 	if (INST_IN_TRANSITION(instance)) {
2151 		debug_msg("storing event %d for instance %s", event_type,
2152 		    instance->fmri);
2153 		instance->pending_rst_event = event_type;
2154 	} else {
2155 		handle_restarter_event(instance, event_type, B_TRUE);
2156 	}
2157 
2158 	return (0);
2159 
2160 fail:
2161 	ack_restarter_event(B_FALSE);
2162 	return (-1);
2163 }
2164 
2165 /*
2166  * Do the state machine processing associated with the termination of instance
2167  * 'inst''s start method for the 'proto_name' protocol if this parameter is not
2168  * NULL.
2169  */
2170 void
2171 process_start_term(instance_t *inst, char *proto_name)
2172 {
2173 	basic_cfg_t	*cfg;
2174 
2175 	inst->copies--;
2176 
2177 	if ((inst->cur_istate == IIS_MAINTENANCE) ||
2178 	    (inst->cur_istate == IIS_DISABLED)) {
2179 		/* do any further processing/checks when we exit these states */
2180 		return;
2181 	}
2182 
2183 	cfg = inst->config->basic;
2184 
2185 	if (cfg->iswait) {
2186 		proto_info_t	*pi;
2187 		boolean_t	listen;
2188 
2189 		switch (inst->cur_istate) {
2190 		case IIS_ONLINE:
2191 		case IIS_DEGRADED:
2192 		case IIS_IN_REFRESH_METHOD:
2193 			/*
2194 			 * A wait type service's start method has exited.
2195 			 * Check if the method was fired off in this inetd's
2196 			 * lifetime, or a previous one; if the former,
2197 			 * re-commence listening on the service's behalf; if
2198 			 * the latter, mark the service offline and let bind
2199 			 * attempts commence.
2200 			 */
2201 			listen = B_FALSE;
2202 			for (pi = uu_list_first(cfg->proto_list); pi != NULL;
2203 			    pi = uu_list_next(cfg->proto_list, pi)) {
2204 				/*
2205 				 * If a bound fd exists, the method was fired
2206 				 * off during this inetd's lifetime.
2207 				 */
2208 				if (pi->listen_fd != -1) {
2209 					listen = B_TRUE;
2210 					if (proto_name == NULL ||
2211 					    strcmp(pi->proto, proto_name) == 0)
2212 						break;
2213 				}
2214 			}
2215 			if (pi != NULL) {
2216 				if (poll_bound_fds(inst, B_TRUE, proto_name) !=
2217 				    0)
2218 					handle_bind_failure(inst);
2219 			} else if (listen == B_FALSE) {
2220 				update_state(inst, IIS_OFFLINE, RERR_RESTART);
2221 				create_bound_fds(inst);
2222 			}
2223 		}
2224 	} else {
2225 		/*
2226 		 * Check if a nowait service should be brought back online
2227 		 * after exceeding its copies limit.
2228 		 */
2229 		if ((inst->cur_istate == IIS_OFFLINE_COPIES) &&
2230 		    !copies_limit_exceeded(inst)) {
2231 			update_state(inst, IIS_OFFLINE, RERR_NONE);
2232 			process_offline_inst(inst);
2233 		}
2234 	}
2235 }
2236 
2237 /*
2238  * If the instance has a pending event process it and initiate the
2239  * acknowledgement.
2240  */
2241 static void
2242 process_pending_rst_event(instance_t *inst)
2243 {
2244 	if (inst->pending_rst_event != RESTARTER_EVENT_TYPE_INVALID) {
2245 		restarter_event_type_t re;
2246 
2247 		debug_msg("Injecting pending event %d for instance %s",
2248 		    inst->pending_rst_event, inst->fmri);
2249 		re = inst->pending_rst_event;
2250 		inst->pending_rst_event = RESTARTER_EVENT_TYPE_INVALID;
2251 		handle_restarter_event(inst, re, B_TRUE);
2252 	}
2253 }
2254 
2255 /*
2256  * Do the state machine processing associated with the termination
2257  * of the specified instance's non-start method with the specified status.
2258  * Once the processing of the termination is done, the function also picks up
2259  * any processing that was blocked on the method running.
2260  */
2261 void
2262 process_non_start_term(instance_t *inst, int status)
2263 {
2264 	boolean_t ran_online_method = B_FALSE;
2265 
2266 	if (status == IMRET_FAILURE) {
2267 		error_msg(gettext("The %s method of instance %s failed, "
2268 		    "transitioning to maintenance"),
2269 		    methods[states[inst->cur_istate].method_running].name,
2270 		    inst->fmri);
2271 
2272 		if ((inst->cur_istate == IIS_IN_ONLINE_METHOD) ||
2273 		    (inst->cur_istate == IIS_IN_REFRESH_METHOD))
2274 			destroy_bound_fds(inst);
2275 
2276 		update_state(inst, IIS_MAINTENANCE, RERR_FAULT);
2277 
2278 		inst->maintenance_req = B_FALSE;
2279 		inst->conn_rate_exceeded = B_FALSE;
2280 
2281 		if (inst->new_config != NULL) {
2282 			destroy_instance_cfg(inst->new_config);
2283 			inst->new_config = NULL;
2284 		}
2285 
2286 		if (!inetd_stopping)
2287 			process_pending_rst_event(inst);
2288 
2289 		return;
2290 	}
2291 
2292 	/* non-failure method return */
2293 
2294 	if (status != IMRET_SUCCESS) {
2295 		/*
2296 		 * An instance method never returned a supported return code.
2297 		 * We'll assume this means the method succeeded for now whilst
2298 		 * non-GL-cognizant methods are used - eg. pkill.
2299 		 */
2300 		debug_msg("The %s method of instance %s returned "
2301 		    "non-compliant exit code: %d, assuming success",
2302 		    methods[states[inst->cur_istate].method_running].name,
2303 		    inst->fmri, status);
2304 	}
2305 
2306 	/*
2307 	 * Update the state from the in-transition state.
2308 	 */
2309 	switch (inst->cur_istate) {
2310 	case IIS_IN_ONLINE_METHOD:
2311 		ran_online_method = B_TRUE;
2312 		/* FALLTHROUGH */
2313 	case IIS_IN_REFRESH_METHOD:
2314 		/*
2315 		 * If we've exhausted the bind retries, flag that by setting
2316 		 * the instance's state to degraded.
2317 		 */
2318 		if (inst->bind_retries_exceeded) {
2319 			update_state(inst, IIS_DEGRADED, RERR_NONE);
2320 			break;
2321 		}
2322 		/* FALLTHROUGH */
2323 	default:
2324 		update_state(inst,
2325 		    methods[states[inst->cur_istate].method_running].dst_state,
2326 		    RERR_NONE);
2327 	}
2328 
2329 	if (inst->cur_istate == IIS_OFFLINE) {
2330 		if (inst->new_config != NULL) {
2331 			/*
2332 			 * This instance was found during refresh to need
2333 			 * taking offline because its newly read configuration
2334 			 * was sufficiently different. Now we're offline,
2335 			 * activate this new configuration.
2336 			 */
2337 			destroy_instance_cfg(inst->config);
2338 			inst->config = inst->new_config;
2339 			inst->new_config = NULL;
2340 		}
2341 
2342 		/* continue/complete any transitions that are in progress */
2343 		process_offline_inst(inst);
2344 
2345 	} else if (ran_online_method) {
2346 		/*
2347 		 * We've just successfully executed the online method. We have
2348 		 * a set of bound network fds that were created before running
2349 		 * this method, so now we're online start listening for
2350 		 * connections on them.
2351 		 */
2352 		if (poll_bound_fds(inst, B_TRUE, NULL) != 0)
2353 			handle_bind_failure(inst);
2354 	}
2355 
2356 	/*
2357 	 * If we're now out of transition (process_offline_inst() could have
2358 	 * fired off another method), carry out any jobs that were blocked by
2359 	 * us being in transition.
2360 	 */
2361 	if (!INST_IN_TRANSITION(inst)) {
2362 		if (inetd_stopping) {
2363 			if (!instance_stopped(inst)) {
2364 				/*
2365 				 * inetd is stopping, and this instance hasn't
2366 				 * been stopped. Inject a stop event.
2367 				 */
2368 				handle_restarter_event(inst,
2369 				    RESTARTER_EVENT_TYPE_STOP, B_FALSE);
2370 			}
2371 		} else {
2372 			process_pending_rst_event(inst);
2373 		}
2374 	}
2375 }
2376 
2377 /*
2378  * Check if configuration file specified is readable. If not return B_FALSE,
2379  * else return B_TRUE.
2380  */
2381 static boolean_t
2382 can_read_file(const char *path)
2383 {
2384 	int	ret;
2385 	int	serrno;
2386 
2387 	do {
2388 		ret = access(path, R_OK);
2389 	} while ((ret < 0) && (errno == EINTR));
2390 	if (ret < 0) {
2391 		if (errno != ENOENT) {
2392 			serrno = errno;
2393 			error_msg(gettext("Failed to access configuration "
2394 			    "file %s for performing modification checks: %s"),
2395 			    path, strerror(errno));
2396 			errno = serrno;
2397 		}
2398 		return (B_FALSE);
2399 	}
2400 	return (B_TRUE);
2401 }
2402 
2403 /*
2404  * Check whether the configuration file has changed contents since inetd
2405  * was last started/refreshed, and if so, log a message indicating that
2406  * inetconv needs to be run.
2407  */
2408 static void
2409 check_conf_file(void)
2410 {
2411 	char		*new_hash;
2412 	char		*old_hash = NULL;
2413 	scf_error_t	ret;
2414 	const char	*file;
2415 
2416 	if (conf_file == NULL) {
2417 		/*
2418 		 * No explicit config file specified, so see if one of the
2419 		 * default two are readable, checking the primary one first
2420 		 * followed by the secondary.
2421 		 */
2422 		if (can_read_file(PRIMARY_DEFAULT_CONF_FILE)) {
2423 			file = PRIMARY_DEFAULT_CONF_FILE;
2424 		} else if ((errno == ENOENT) &&
2425 		    can_read_file(SECONDARY_DEFAULT_CONF_FILE)) {
2426 			file = SECONDARY_DEFAULT_CONF_FILE;
2427 		} else {
2428 			return;
2429 		}
2430 	} else {
2431 		file = conf_file;
2432 		if (!can_read_file(file))
2433 			return;
2434 	}
2435 
2436 	if (calculate_hash(file, &new_hash) == 0) {
2437 		ret = retrieve_inetd_hash(&old_hash);
2438 		if (((ret == SCF_ERROR_NONE) &&
2439 		    (strcmp(old_hash, new_hash) != 0))) {
2440 			/* modified config file */
2441 			warn_msg(gettext(
2442 			    "Configuration file %s has been modified since "
2443 			    "inetconv was last run. \"inetconv -i %s\" must be "
2444 			    "run to apply any changes to the SMF"), file, file);
2445 		} else if ((ret != SCF_ERROR_NOT_FOUND) &&
2446 		    (ret != SCF_ERROR_NONE)) {
2447 			/* No message if hash not yet computed */
2448 			error_msg(gettext("Failed to check whether "
2449 			    "configuration file %s has been modified: %s"),
2450 			    file, scf_strerror(ret));
2451 		}
2452 		free(old_hash);
2453 		free(new_hash);
2454 	} else {
2455 		error_msg(gettext("Failed to check whether configuration file "
2456 		    "%s has been modified: %s"), file, strerror(errno));
2457 	}
2458 }
2459 
2460 /*
2461  * Refresh all inetd's managed instances and check the configuration file
2462  * for any updates since inetconv was last run, logging a message if there
2463  * are. We call the SMF refresh function to refresh each instance so that
2464  * the refresh request goes through the framework, and thus results in the
2465  * running snapshot of each instance being updated from the configuration
2466  * snapshot.
2467  */
2468 static void
2469 inetd_refresh(void)
2470 {
2471 	instance_t	*inst;
2472 
2473 	refresh_debug_flag();
2474 
2475 	/* call libscf to send refresh requests for all managed instances */
2476 	for (inst = uu_list_first(instance_list); inst != NULL;
2477 	    inst = uu_list_next(instance_list, inst)) {
2478 		if (smf_refresh_instance(inst->fmri) < 0) {
2479 			error_msg(gettext("Failed to refresh instance %s: %s"),
2480 			    inst->fmri, scf_strerror(scf_error()));
2481 		}
2482 	}
2483 
2484 	/*
2485 	 * Log a message if the configuration file has changed since inetconv
2486 	 * was last run.
2487 	 */
2488 	check_conf_file();
2489 }
2490 
2491 /*
2492  * Initiate inetd's shutdown.
2493  */
2494 static void
2495 inetd_stop(void)
2496 {
2497 	instance_t *inst;
2498 
2499 	/* Block handling signals for stop and refresh */
2500 	(void) sighold(SIGHUP);
2501 	(void) sighold(SIGTERM);
2502 
2503 	/* Indicate inetd is coming down */
2504 	inetd_stopping = B_TRUE;
2505 
2506 	/* Stop polling on restarter events. */
2507 	clear_pollfd(rst_event_pipe[PE_CONSUMER]);
2508 
2509 	/* Stop polling for any more stop/refresh requests. */
2510 	clear_pollfd(uds_fd);
2511 
2512 	/*
2513 	 * Send a stop event to all currently unstopped instances that
2514 	 * aren't in transition. For those that are in transition, the
2515 	 * event will get sent when the transition completes.
2516 	 */
2517 	for (inst = uu_list_first(instance_list); inst != NULL;
2518 	    inst = uu_list_next(instance_list, inst)) {
2519 		if (!instance_stopped(inst) && !INST_IN_TRANSITION(inst))
2520 			handle_restarter_event(inst,
2521 			    RESTARTER_EVENT_TYPE_STOP, B_FALSE);
2522 	}
2523 }
2524 
2525 /*
2526  * Sets up the intra-inetd-process Unix Domain Socket.
2527  * Returns -1 on error, else 0.
2528  */
2529 static int
2530 uds_init(void)
2531 {
2532 	struct sockaddr_un addr;
2533 
2534 	if ((uds_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
2535 		error_msg("socket: %s", strerror(errno));
2536 		return (-1);
2537 	}
2538 
2539 	disable_blocking(uds_fd);
2540 
2541 	(void) unlink(INETD_UDS_PATH);  /* clean-up any stale files */
2542 
2543 	(void) memset(&addr, 0, sizeof (addr));
2544 	addr.sun_family = AF_UNIX;
2545 	/* CONSTCOND */
2546 	assert(sizeof (INETD_UDS_PATH) <= sizeof (addr.sun_path));
2547 	(void) strlcpy(addr.sun_path, INETD_UDS_PATH, sizeof (addr.sun_path));
2548 
2549 	if (bind(uds_fd, (struct sockaddr *)(&addr), sizeof (addr)) < 0) {
2550 		error_msg(gettext("Failed to bind socket to %s: %s"),
2551 		    INETD_UDS_PATH, strerror(errno));
2552 		(void) close(uds_fd);
2553 		return (-1);
2554 	}
2555 
2556 	(void) listen(uds_fd, UDS_BACKLOG);
2557 
2558 	if ((set_pollfd(uds_fd, POLLIN)) == -1) {
2559 		(void) close(uds_fd);
2560 		(void) unlink(INETD_UDS_PATH);
2561 		return (-1);
2562 	}
2563 
2564 	return (0);
2565 }
2566 
2567 static void
2568 uds_fini(void)
2569 {
2570 	if (uds_fd != -1)
2571 		(void) close(uds_fd);
2572 	(void) unlink(INETD_UDS_PATH);
2573 }
2574 
2575 /*
2576  * Handle an incoming request on the Unix Domain Socket. Returns -1 if there
2577  * was an error handling the event, else 0.
2578  */
2579 static int
2580 process_uds_event(void)
2581 {
2582 	uds_request_t		req;
2583 	int			fd;
2584 	struct sockaddr_un	addr;
2585 	socklen_t		len = sizeof (addr);
2586 	int			ret;
2587 	uint_t			retries = 0;
2588 	ucred_t			*ucred = NULL;
2589 	uid_t			euid;
2590 
2591 	do {
2592 		fd = accept(uds_fd, (struct sockaddr *)&addr, &len);
2593 	} while ((fd < 0) && (errno == EINTR));
2594 	if (fd < 0) {
2595 		if (errno != EWOULDBLOCK)
2596 			error_msg("accept failed: %s", strerror(errno));
2597 		return (-1);
2598 	}
2599 
2600 	if (getpeerucred(fd, &ucred) == -1) {
2601 		error_msg("getpeerucred failed: %s", strerror(errno));
2602 		(void) close(fd);
2603 		return (-1);
2604 	}
2605 
2606 	/* Check peer credentials before acting on the request */
2607 	euid = ucred_geteuid(ucred);
2608 	ucred_free(ucred);
2609 	if (euid != 0 && getuid() != euid) {
2610 		debug_msg("peer euid %u != uid %u",
2611 		    (uint_t)euid, (uint_t)getuid());
2612 		(void) close(fd);
2613 		return (-1);
2614 	}
2615 
2616 	for (retries = 0; retries < UDS_RECV_RETRIES; retries++) {
2617 		if (((ret = safe_read(fd, &req, sizeof (req))) != 1) ||
2618 		    (errno != EAGAIN))
2619 			break;
2620 
2621 		(void) poll(NULL, 0, 100);	/* 100ms pause */
2622 	}
2623 
2624 	if (ret != 0) {
2625 		error_msg(gettext("Failed read: %s"), strerror(errno));
2626 		(void) close(fd);
2627 		return (-1);
2628 	}
2629 
2630 	switch (req) {
2631 	case UR_REFRESH_INETD:
2632 		/* flag the request for event_loop() to process */
2633 		refresh_inetd_requested = B_TRUE;
2634 		(void) close(fd);
2635 		break;
2636 	case UR_STOP_INETD:
2637 		inetd_stop();
2638 		break;
2639 	default:
2640 		error_msg("unexpected UDS request");
2641 		(void) close(fd);
2642 		return (-1);
2643 	}
2644 
2645 	return (0);
2646 }
2647 
2648 /*
2649  * Perform checks for common exec string errors. We limit the checks to
2650  * whether the file exists, is a regular file, and has at least one execute
2651  * bit set. We leave the core security checks to exec() so as not to duplicate
2652  * and thus incur the associated drawbacks, but hope to catch the common
2653  * errors here.
2654  */
2655 static boolean_t
2656 passes_basic_exec_checks(const char *instance, const char *method,
2657     const char *path)
2658 {
2659 	struct stat	sbuf;
2660 
2661 	/* check the file exists */
2662 	while (stat(path, &sbuf) == -1) {
2663 		if (errno != EINTR) {
2664 			error_msg(gettext(
2665 			    "Can't stat the %s method of instance %s: %s"),
2666 			    method, instance, strerror(errno));
2667 			return (B_FALSE);
2668 		}
2669 	}
2670 
2671 	/*
2672 	 * Check if the file is a regular file and has at least one execute
2673 	 * bit set.
2674 	 */
2675 	if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
2676 		error_msg(gettext(
2677 		    "The %s method of instance %s isn't a regular file"),
2678 		    method, instance);
2679 		return (B_FALSE);
2680 	} else if ((sbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
2681 		error_msg(gettext("The %s method instance %s doesn't have "
2682 		    "any execute permissions set"), method, instance);
2683 		return (B_FALSE);
2684 	}
2685 
2686 	return (B_TRUE);
2687 }
2688 
2689 static void
2690 exec_method(instance_t *instance, instance_method_t method, method_info_t *mi,
2691     struct method_context *mthd_ctxt, const proto_info_t *pi)
2692 {
2693 	char		**args;
2694 	char 		**env;
2695 	const char	*errf;
2696 	int		serrno;
2697 	basic_cfg_t	*cfg = instance->config->basic;
2698 
2699 	if (method == IM_START) {
2700 		/*
2701 		 * If wrappers checks fail, pretend the method was exec'd and
2702 		 * failed.
2703 		 */
2704 		if (!tcp_wrappers_ok(instance))
2705 			exit(IMRET_FAILURE);
2706 	}
2707 
2708 	/*
2709 	 * Revert the disposition of handled signals and ignored signals to
2710 	 * their defaults, unblocking any blocked ones as a side effect.
2711 	 */
2712 	(void) sigset(SIGHUP, SIG_DFL);
2713 	(void) sigset(SIGTERM, SIG_DFL);
2714 	(void) sigset(SIGINT, SIG_DFL);
2715 
2716 	/*
2717 	 * Setup exec arguments. Do this before the fd setup below, so our
2718 	 * logging related file fd doesn't get taken over before we call
2719 	 * expand_address().
2720 	 */
2721 	if ((method == IM_START) &&
2722 	    (strcmp(mi->exec_args_we.we_wordv[0], "%A") == 0)) {
2723 		args = expand_address(instance, pi);
2724 	} else {
2725 		args = mi->exec_args_we.we_wordv;
2726 	}
2727 
2728 	/* Generate audit trail for start operations */
2729 	if (method == IM_START) {
2730 		adt_event_data_t *ae;
2731 		struct sockaddr_storage ss;
2732 		priv_set_t *privset;
2733 		socklen_t sslen = sizeof (ss);
2734 
2735 		if ((ae = adt_alloc_event(audit_handle, ADT_inetd_connect))
2736 		    == NULL) {
2737 			error_msg(gettext("Unable to allocate audit event for "
2738 			    "the %s method of instance %s"),
2739 			    methods[method].name, instance->fmri);
2740 			exit(IMRET_FAILURE);
2741 		}
2742 
2743 		/*
2744 		 * The inetd_connect audit record consists of:
2745 		 *	Service name
2746 		 *	Execution path
2747 		 *	Remote address and port
2748 		 *	Local port
2749 		 *	Process privileges
2750 		 */
2751 		ae->adt_inetd_connect.service_name = cfg->svc_name;
2752 		ae->adt_inetd_connect.cmd = mi->exec_path;
2753 
2754 		if (instance->remote_addr.ss_family == AF_INET) {
2755 			struct in_addr *in = SS_SINADDR(instance->remote_addr);
2756 			ae->adt_inetd_connect.ip_adr[0] = in->s_addr;
2757 			ae->adt_inetd_connect.ip_type = ADT_IPv4;
2758 		} else {
2759 			uint32_t *addr6;
2760 			int i;
2761 
2762 			ae->adt_inetd_connect.ip_type = ADT_IPv6;
2763 			addr6 = (uint32_t *)SS_SINADDR(instance->remote_addr);
2764 			for (i = 0; i < 4; ++i)
2765 				ae->adt_inetd_connect.ip_adr[i] = addr6[i];
2766 		}
2767 
2768 		ae->adt_inetd_connect.ip_remote_port =
2769 		    ntohs(SS_PORT(instance->remote_addr));
2770 
2771 		if (getsockname(instance->conn_fd, (struct sockaddr *)&ss,
2772 		    &sslen) == 0)
2773 			ae->adt_inetd_connect.ip_local_port =
2774 			    ntohs(SS_PORT(ss));
2775 
2776 		privset = mthd_ctxt->priv_set;
2777 		if (privset == NULL) {
2778 			privset = priv_allocset();
2779 			if (privset != NULL &&
2780 			    getppriv(PRIV_EFFECTIVE, privset) != 0) {
2781 				priv_freeset(privset);
2782 				privset = NULL;
2783 			}
2784 		}
2785 
2786 		ae->adt_inetd_connect.privileges = privset;
2787 
2788 		(void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS);
2789 		adt_free_event(ae);
2790 
2791 		if (privset != NULL && mthd_ctxt->priv_set == NULL)
2792 			priv_freeset(privset);
2793 	}
2794 
2795 	/*
2796 	 * Set method context before the fd setup below so we can output an
2797 	 * error message if it fails.
2798 	 */
2799 	if ((errno = restarter_set_method_context(mthd_ctxt, &errf)) != 0) {
2800 		const char *msg;
2801 
2802 		if (errno == -1) {
2803 			if (strcmp(errf, "core_set_process_path") == 0) {
2804 				msg = gettext("Failed to set the corefile path "
2805 				    "for the %s method of instance %s");
2806 			} else if (strcmp(errf, "setproject") == 0) {
2807 				msg = gettext("Failed to assign a resource "
2808 				    "control for the %s method of instance %s");
2809 			} else if (strcmp(errf, "pool_set_binding") == 0) {
2810 				msg = gettext("Failed to bind the %s method of "
2811 				    "instance %s to a pool due to a system "
2812 				    "error");
2813 			} else {
2814 				assert(0);
2815 				abort();
2816 			}
2817 
2818 			error_msg(msg, methods[method].name, instance->fmri);
2819 
2820 			exit(IMRET_FAILURE);
2821 		}
2822 
2823 		if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) {
2824 			switch (errno) {
2825 			case ENOENT:
2826 				msg = gettext("Failed to find resource pool "
2827 				    "for the %s method of instance %s");
2828 				break;
2829 
2830 			case EBADF:
2831 				msg = gettext("Failed to bind the %s method of "
2832 				    "instance %s to a pool due to invalid "
2833 				    "configuration");
2834 				break;
2835 
2836 			case EINVAL:
2837 				msg = gettext("Failed to bind the %s method of "
2838 				    "instance %s to a pool due to invalid "
2839 				    "pool name");
2840 				break;
2841 
2842 			default:
2843 				assert(0);
2844 				abort();
2845 			}
2846 
2847 			exit(IMRET_FAILURE);
2848 		}
2849 
2850 		if (errf != NULL) {
2851 			error_msg(gettext("Failed to set credentials for the "
2852 			    "%s method of instance %s (%s: %s)"),
2853 			    methods[method].name, instance->fmri, errf,
2854 			    strerror(errno));
2855 			exit(IMRET_FAILURE);
2856 		}
2857 
2858 		switch (errno) {
2859 		case ENOMEM:
2860 			msg = gettext("Failed to set credentials for the %s "
2861 			    "method of instance %s (out of memory)");
2862 			break;
2863 
2864 		case ENOENT:
2865 			msg = gettext("Failed to set credentials for the %s "
2866 			    "method of instance %s (no passwd or shadow "
2867 			    "entry for user)");
2868 			break;
2869 
2870 		default:
2871 			assert(0);
2872 			abort();
2873 		}
2874 
2875 		error_msg(msg, methods[method].name, instance->fmri);
2876 		exit(IMRET_FAILURE);
2877 	}
2878 
2879 	/* let exec() free mthd_ctxt */
2880 
2881 	/* setup standard fds */
2882 	if (method == IM_START) {
2883 		(void) dup2(instance->conn_fd, STDIN_FILENO);
2884 	} else {
2885 		(void) close(STDIN_FILENO);
2886 		(void) open("/dev/null", O_RDONLY);
2887 	}
2888 	(void) dup2(STDIN_FILENO, STDOUT_FILENO);
2889 	(void) dup2(STDIN_FILENO, STDERR_FILENO);
2890 
2891 	closefrom(STDERR_FILENO + 1);
2892 
2893 	method_preexec();
2894 
2895 	env = set_smf_env(mthd_ctxt, instance, methods[method].name);
2896 
2897 	if (env != NULL) {
2898 		do {
2899 			(void) execve(mi->exec_path, args, env);
2900 		} while (errno == EINTR);
2901 	}
2902 
2903 	serrno = errno;
2904 	/* start up logging again to report the error */
2905 	msg_init();
2906 	errno = serrno;
2907 
2908 	error_msg(
2909 	    gettext("Failed to exec %s method of instance %s: %s"),
2910 	    methods[method].name, instance->fmri, strerror(errno));
2911 
2912 	if ((method == IM_START) && (instance->config->basic->iswait)) {
2913 		/*
2914 		 * We couldn't exec the start method for a wait type service.
2915 		 * Eat up data from the endpoint, so that hopefully the
2916 		 * service's fd won't wake poll up on the next time round
2917 		 * event_loop(). This behavior is carried over from the old
2918 		 * inetd, and it seems somewhat arbitrary that it isn't
2919 		 * also done in the case of fork failures; but I guess
2920 		 * it assumes an exec failure is less likely to be the result
2921 		 * of a resource shortage, and is thus not worth retrying.
2922 		 */
2923 		consume_wait_data(instance, 0);
2924 	}
2925 
2926 	exit(IMRET_FAILURE);
2927 }
2928 
2929 static restarter_error_t
2930 get_method_error_success(instance_method_t method)
2931 {
2932 	switch (method) {
2933 	case IM_OFFLINE:
2934 		return (RERR_RESTART);
2935 	case IM_ONLINE:
2936 		return (RERR_RESTART);
2937 	case IM_DISABLE:
2938 		return (RERR_RESTART);
2939 	case IM_REFRESH:
2940 		return (RERR_REFRESH);
2941 	case IM_START:
2942 		return (RERR_RESTART);
2943 	}
2944 	(void) fprintf(stderr, gettext("Internal fatal error in inetd.\n"));
2945 
2946 	abort();
2947 	/* NOTREACHED */
2948 }
2949 
2950 static int
2951 smf_kill_process(instance_t *instance, int sig)
2952 {
2953 	rep_val_t	*rv;
2954 	int		ret = IMRET_SUCCESS;
2955 
2956 	/* Carry out process assassination */
2957 	for (rv = uu_list_first(instance->start_pids);
2958 	    rv != NULL;
2959 	    rv = uu_list_next(instance->start_pids, rv)) {
2960 		if ((kill((pid_t)rv->val, sig) != 0) &&
2961 		    (errno != ESRCH)) {
2962 			ret = IMRET_FAILURE;
2963 			error_msg(gettext("Unable to kill "
2964 			    "start process (%ld) of instance %s: %s"),
2965 			    rv->val, instance->fmri, strerror(errno));
2966 		}
2967 	}
2968 	return (ret);
2969 }
2970 
2971 /*
2972  * Runs the specified method of the specified service instance.
2973  * If the method was never specified, we handle it the same as if the
2974  * method was called and returned success, carrying on any transition the
2975  * instance may be in the midst of.
2976  * If the method isn't executable in its specified profile or an error occurs
2977  * forking a process to run the method in the function returns -1.
2978  * If a method binary is successfully executed, the function switches the
2979  * instance's cur state to the method's associated 'run' state and the next
2980  * state to the methods associated next state.
2981  * Returns -1 if there's an error before forking, else 0.
2982  */
2983 int
2984 run_method(instance_t *instance, instance_method_t method,
2985     const proto_info_t *start_info)
2986 {
2987 	pid_t			child_pid;
2988 	method_info_t		*mi;
2989 	struct method_context	*mthd_ctxt = NULL;
2990 	int			sig = 0;
2991 	int			ret;
2992 	instance_cfg_t		*cfg = instance->config;
2993 	ctid_t			cid;
2994 	boolean_t		trans_failure = B_TRUE;
2995 	int			serrno;
2996 
2997 	/*
2998 	 * Don't bother updating the instance's state for the start method
2999 	 * as there isn't a separate start method state.
3000 	 */
3001 	if (method != IM_START)
3002 		update_instance_states(instance, get_method_state(method),
3003 		    methods[method].dst_state,
3004 		    get_method_error_success(method));
3005 
3006 	if ((mi = cfg->methods[method]) == NULL) {
3007 		/*
3008 		 * If the absent method is IM_OFFLINE, default action needs
3009 		 * to be taken to avoid lingering processes which can prevent
3010 		 * the upcoming rebinding from happening.
3011 		 */
3012 		if ((method == IM_OFFLINE) && instance->config->basic->iswait) {
3013 			warn_msg(gettext("inetd_offline method for instance %s "
3014 			    "is unspecified.  Taking default action: kill."),
3015 			    instance->fmri);
3016 			(void) str2sig("TERM", &sig);
3017 			ret = smf_kill_process(instance, sig);
3018 			process_non_start_term(instance, ret);
3019 			return (0);
3020 		} else {
3021 			process_non_start_term(instance, IMRET_SUCCESS);
3022 			return (0);
3023 		}
3024 	}
3025 
3026 	/* Handle special method tokens, not allowed on start */
3027 	if (method != IM_START) {
3028 		if (restarter_is_null_method(mi->exec_path)) {
3029 			/* :true means nothing should be done */
3030 			process_non_start_term(instance, IMRET_SUCCESS);
3031 			return (0);
3032 		}
3033 
3034 		if ((sig = restarter_is_kill_method(mi->exec_path)) >= 0) {
3035 			/* Carry out contract assassination */
3036 			ret = iterate_repository_contracts(instance, sig);
3037 			/* ENOENT means we didn't find any contracts */
3038 			if (ret != 0 && ret != ENOENT) {
3039 				error_msg(gettext("Failed to send signal %d "
3040 				    "to contracts of instance %s: %s"), sig,
3041 				    instance->fmri, strerror(ret));
3042 				goto prefork_failure;
3043 			} else {
3044 				process_non_start_term(instance, IMRET_SUCCESS);
3045 				return (0);
3046 			}
3047 		}
3048 
3049 		if ((sig = restarter_is_kill_proc_method(mi->exec_path)) >= 0) {
3050 			ret = smf_kill_process(instance, sig);
3051 			process_non_start_term(instance, ret);
3052 			return (0);
3053 		}
3054 	}
3055 
3056 	/*
3057 	 * Get the associated method context before the fork so we can
3058 	 * modify the instances state if things go wrong.
3059 	 */
3060 	if ((mthd_ctxt = read_method_context(instance->fmri,
3061 	    methods[method].name, mi->exec_path)) == NULL)
3062 		goto prefork_failure;
3063 
3064 	/*
3065 	 * Perform some basic checks before we fork to limit the possibility
3066 	 * of exec failures, so we can modify the instance state if necessary.
3067 	 */
3068 	if (!passes_basic_exec_checks(instance->fmri, methods[method].name,
3069 	    mi->exec_path)) {
3070 		trans_failure = B_FALSE;
3071 		goto prefork_failure;
3072 	}
3073 
3074 	if (contract_prefork(instance->fmri, method) == -1)
3075 		goto prefork_failure;
3076 	child_pid = fork();
3077 	serrno = errno;
3078 	contract_postfork();
3079 
3080 	switch (child_pid) {
3081 	case -1:
3082 		error_msg(gettext(
3083 		    "Unable to fork %s method of instance %s: %s"),
3084 		    methods[method].name, instance->fmri, strerror(serrno));
3085 		if ((serrno != EAGAIN) && (serrno != ENOMEM))
3086 			trans_failure = B_FALSE;
3087 		goto prefork_failure;
3088 	case 0:				/* child */
3089 		exec_method(instance, method, mi, mthd_ctxt, start_info);
3090 		/* NOTREACHED */
3091 	default:			/* parent */
3092 		restarter_free_method_context(mthd_ctxt);
3093 		mthd_ctxt = NULL;
3094 
3095 		if (get_latest_contract(&cid) < 0)
3096 			cid = -1;
3097 
3098 		/*
3099 		 * Register this method so its termination is noticed and
3100 		 * the state transition this method participates in is
3101 		 * continued.
3102 		 */
3103 		if (register_method(instance, child_pid, cid, method,
3104 		    start_info->proto) != 0) {
3105 			/*
3106 			 * Since we will never find out about the termination
3107 			 * of this method, if it's a non-start method treat
3108 			 * is as a failure so we don't block restarter event
3109 			 * processing on it whilst it languishes in a method
3110 			 * running state.
3111 			 */
3112 			error_msg(gettext("Failed to monitor status of "
3113 			    "%s method of instance %s"), methods[method].name,
3114 			    instance->fmri);
3115 			if (method != IM_START)
3116 				process_non_start_term(instance, IMRET_FAILURE);
3117 		}
3118 
3119 		add_method_ids(instance, child_pid, cid, method);
3120 
3121 		/* do tcp tracing for those nowait instances that request it */
3122 		if ((method == IM_START) && cfg->basic->do_tcp_trace &&
3123 		    !cfg->basic->iswait) {
3124 			char buf[INET6_ADDRSTRLEN];
3125 
3126 			syslog(LOG_NOTICE, "%s[%d] from %s %d",
3127 			    cfg->basic->svc_name, child_pid,
3128 			    inet_ntop_native(instance->remote_addr.ss_family,
3129 			    SS_SINADDR(instance->remote_addr), buf,
3130 			    sizeof (buf)),
3131 			    ntohs(SS_PORT(instance->remote_addr)));
3132 		}
3133 	}
3134 
3135 	return (0);
3136 
3137 prefork_failure:
3138 	if (mthd_ctxt != NULL) {
3139 		restarter_free_method_context(mthd_ctxt);
3140 		mthd_ctxt = NULL;
3141 	}
3142 
3143 	if (method == IM_START) {
3144 		/*
3145 		 * Only place a start method in maintenance if we're sure
3146 		 * that the failure was non-transient.
3147 		 */
3148 		if (!trans_failure) {
3149 			destroy_bound_fds(instance);
3150 			update_state(instance, IIS_MAINTENANCE, RERR_FAULT);
3151 		}
3152 	} else {
3153 		/* treat the failure as if the method ran and failed */
3154 		process_non_start_term(instance, IMRET_FAILURE);
3155 	}
3156 
3157 	return (-1);
3158 }
3159 
3160 static int
3161 pending_connections(instance_t *instance, proto_info_t *pi)
3162 {
3163 	if (instance->config->basic->istlx) {
3164 		tlx_info_t *tl = (tlx_info_t *)pi;
3165 
3166 		return (uu_list_numnodes(tl->conn_ind_queue) != 0);
3167 	} else {
3168 		return (0);
3169 	}
3170 }
3171 
3172 static int
3173 accept_connection(instance_t *instance, proto_info_t *pi)
3174 {
3175 	int		fd;
3176 	socklen_t	size;
3177 
3178 	if (instance->config->basic->istlx) {
3179 		tlx_info_t *tl = (tlx_info_t *)pi;
3180 		tlx_pending_counter = \
3181 		    tlx_pending_counter - uu_list_numnodes(tl->conn_ind_queue);
3182 
3183 		fd = tlx_accept(instance->fmri, (tlx_info_t *)pi,
3184 		    &(instance->remote_addr));
3185 
3186 		tlx_pending_counter = \
3187 		    tlx_pending_counter + uu_list_numnodes(tl->conn_ind_queue);
3188 	} else {
3189 		size = sizeof (instance->remote_addr);
3190 		fd = accept(pi->listen_fd,
3191 		    (struct sockaddr *)&(instance->remote_addr), &size);
3192 		if (fd < 0)
3193 			error_msg("accept: %s", strerror(errno));
3194 	}
3195 
3196 	return (fd);
3197 }
3198 
3199 /*
3200  * Handle an incoming connection request for a nowait service.
3201  * This involves accepting the incoming connection on a new fd. Connection
3202  * rate checks are then performed, transitioning the service to the
3203  * conrate offline state if these fail. Otherwise, the service's start method
3204  * is run (performing TCP wrappers checks if applicable as we do), and on
3205  * success concurrent copies checking is done, transitioning the service to the
3206  * copies offline state if this fails.
3207  */
3208 static void
3209 process_nowait_request(instance_t *instance, proto_info_t *pi)
3210 {
3211 	basic_cfg_t		*cfg = instance->config->basic;
3212 	int			ret;
3213 	adt_event_data_t	*ae;
3214 	char			buf[BUFSIZ];
3215 
3216 	/* accept nowait service connections on a new fd */
3217 	if ((instance->conn_fd = accept_connection(instance, pi)) == -1) {
3218 		/*
3219 		 * Failed accept. Return and allow the event loop to initiate
3220 		 * another attempt later if the request is still present.
3221 		 */
3222 		return;
3223 	}
3224 
3225 	/*
3226 	 * Limit connection rate of nowait services. If either conn_rate_max
3227 	 * or conn_rate_offline are <= 0, no connection rate limit checking
3228 	 * is done. If the configured rate is exceeded, the instance is taken
3229 	 * to the connrate_offline state and a timer scheduled to try and
3230 	 * bring the instance back online after the configured offline time.
3231 	 */
3232 	if ((cfg->conn_rate_max > 0) && (cfg->conn_rate_offline > 0)) {
3233 		if (instance->conn_rate_count++ == 0) {
3234 			instance->conn_rate_start = time(NULL);
3235 		} else if (instance->conn_rate_count >
3236 		    cfg->conn_rate_max) {
3237 			time_t now = time(NULL);
3238 
3239 			if ((now - instance->conn_rate_start) > 1) {
3240 				instance->conn_rate_start = now;
3241 				instance->conn_rate_count = 1;
3242 			} else {
3243 				/* Generate audit record */
3244 				if ((ae = adt_alloc_event(audit_handle,
3245 				    ADT_inetd_ratelimit)) == NULL) {
3246 					error_msg(gettext("Unable to allocate "
3247 					    "rate limit audit event"));
3248 				} else {
3249 					adt_inetd_ratelimit_t *rl =
3250 					    &ae->adt_inetd_ratelimit;
3251 					/*
3252 					 * The inetd_ratelimit audit
3253 					 * record consists of:
3254 					 * 	Service name
3255 					 *	Connection rate limit
3256 					 */
3257 					rl->service_name = cfg->svc_name;
3258 					(void) snprintf(buf, sizeof (buf),
3259 					    "limit=%lld", cfg->conn_rate_max);
3260 					rl->limit = buf;
3261 					(void) adt_put_event(ae, ADT_SUCCESS,
3262 					    ADT_SUCCESS);
3263 					adt_free_event(ae);
3264 				}
3265 
3266 				error_msg(gettext(
3267 				    "Instance %s has exceeded its configured "
3268 				    "connection rate, additional connections "
3269 				    "will not be accepted for %d seconds"),
3270 				    instance->fmri, cfg->conn_rate_offline);
3271 
3272 				close_net_fd(instance, instance->conn_fd);
3273 				instance->conn_fd = -1;
3274 
3275 				destroy_bound_fds(instance);
3276 
3277 				instance->conn_rate_count = 0;
3278 
3279 				instance->conn_rate_exceeded = B_TRUE;
3280 				(void) run_method(instance, IM_OFFLINE, NULL);
3281 
3282 				return;
3283 			}
3284 		}
3285 	}
3286 
3287 	ret = run_method(instance, IM_START, pi);
3288 
3289 	close_net_fd(instance, instance->conn_fd);
3290 	instance->conn_fd = -1;
3291 
3292 	if (ret == -1) /* the method wasn't forked  */
3293 		return;
3294 
3295 	instance->copies++;
3296 
3297 	/*
3298 	 * Limit concurrent connections of nowait services.
3299 	 */
3300 	if (copies_limit_exceeded(instance)) {
3301 		/* Generate audit record */
3302 		if ((ae = adt_alloc_event(audit_handle, ADT_inetd_copylimit))
3303 		    == NULL) {
3304 			error_msg(gettext("Unable to allocate copy limit "
3305 			    "audit event"));
3306 		} else {
3307 			/*
3308 			 * The inetd_copylimit audit record consists of:
3309 			 *	Service name
3310 			 * 	Copy limit
3311 			 */
3312 			ae->adt_inetd_copylimit.service_name = cfg->svc_name;
3313 			(void) snprintf(buf, sizeof (buf), "limit=%lld",
3314 			    cfg->max_copies);
3315 			ae->adt_inetd_copylimit.limit = buf;
3316 			(void) adt_put_event(ae, ADT_SUCCESS, ADT_SUCCESS);
3317 			adt_free_event(ae);
3318 		}
3319 
3320 		warn_msg(gettext("Instance %s has reached its maximum "
3321 		    "configured copies, no new connections will be accepted"),
3322 		    instance->fmri);
3323 		destroy_bound_fds(instance);
3324 		(void) run_method(instance, IM_OFFLINE, NULL);
3325 	}
3326 }
3327 
3328 /*
3329  * Handle an incoming request for a wait type service.
3330  * Failure rate checking is done first, taking the service to the maintenance
3331  * state if the checks fail. Following this, the service's start method is run,
3332  * and on success, we stop listening for new requests for this service.
3333  */
3334 static void
3335 process_wait_request(instance_t *instance, const proto_info_t *pi)
3336 {
3337 	basic_cfg_t		*cfg = instance->config->basic;
3338 	int			ret;
3339 	adt_event_data_t	*ae;
3340 	char			buf[BUFSIZ];
3341 
3342 	instance->conn_fd = pi->listen_fd;
3343 
3344 	/*
3345 	 * Detect broken servers and transition them to maintenance. If a
3346 	 * wait type service exits without accepting the connection or
3347 	 * consuming (reading) the datagram, that service's descriptor will
3348 	 * select readable again, and inetd will fork another instance of
3349 	 * the server. If either wait_fail_cnt or wait_fail_interval are <= 0,
3350 	 * no failure rate detection is done.
3351 	 */
3352 	if ((cfg->wait_fail_cnt > 0) && (cfg->wait_fail_interval > 0)) {
3353 		if (instance->fail_rate_count++ == 0) {
3354 			instance->fail_rate_start = time(NULL);
3355 		} else if (instance->fail_rate_count > cfg->wait_fail_cnt) {
3356 			time_t now = time(NULL);
3357 
3358 			if ((now - instance->fail_rate_start) >
3359 			    cfg->wait_fail_interval) {
3360 				instance->fail_rate_start = now;
3361 				instance->fail_rate_count = 1;
3362 			} else {
3363 				/* Generate audit record */
3364 				if ((ae = adt_alloc_event(audit_handle,
3365 				    ADT_inetd_failrate)) == NULL) {
3366 					error_msg(gettext("Unable to allocate "
3367 					    "failure rate audit event"));
3368 				} else {
3369 					adt_inetd_failrate_t *fr =
3370 					    &ae->adt_inetd_failrate;
3371 					/*
3372 					 * The inetd_failrate audit record
3373 					 * consists of:
3374 					 * 	Service name
3375 					 * 	Failure rate
3376 					 *	Interval
3377 					 * Last two are expressed as k=v pairs
3378 					 * in the values field.
3379 					 */
3380 					fr->service_name = cfg->svc_name;
3381 					(void) snprintf(buf, sizeof (buf),
3382 					    "limit=%lld,interval=%d",
3383 					    cfg->wait_fail_cnt,
3384 					    cfg->wait_fail_interval);
3385 					fr->values = buf;
3386 					(void) adt_put_event(ae, ADT_SUCCESS,
3387 					    ADT_SUCCESS);
3388 					adt_free_event(ae);
3389 				}
3390 
3391 				error_msg(gettext(
3392 				    "Instance %s has exceeded its configured "
3393 				    "failure rate, transitioning to "
3394 				    "maintenance"), instance->fmri);
3395 				instance->fail_rate_count = 0;
3396 
3397 				destroy_bound_fds(instance);
3398 
3399 				instance->maintenance_req = B_TRUE;
3400 				(void) run_method(instance, IM_OFFLINE, NULL);
3401 				return;
3402 			}
3403 		}
3404 	}
3405 
3406 	ret = run_method(instance, IM_START, pi);
3407 
3408 	instance->conn_fd = -1;
3409 
3410 	if (ret == 0) {
3411 		/*
3412 		 * Stop listening for connections now we've fired off the
3413 		 * server for a wait type instance.
3414 		 */
3415 		(void) poll_bound_fds(instance, B_FALSE, pi->proto);
3416 	}
3417 }
3418 
3419 /*
3420  * Process any networks requests for each proto for each instance.
3421  */
3422 void
3423 process_network_events(void)
3424 {
3425 	instance_t	*instance;
3426 
3427 	for (instance = uu_list_first(instance_list); instance != NULL;
3428 	    instance = uu_list_next(instance_list, instance)) {
3429 		basic_cfg_t	*cfg;
3430 		proto_info_t	*pi;
3431 
3432 		/*
3433 		 * Ignore instances in states that definitely don't have any
3434 		 * listening fds.
3435 		 */
3436 		switch (instance->cur_istate) {
3437 		case IIS_ONLINE:
3438 		case IIS_DEGRADED:
3439 		case IIS_IN_REFRESH_METHOD:
3440 			break;
3441 		default:
3442 			continue;
3443 		}
3444 
3445 		cfg = instance->config->basic;
3446 
3447 		for (pi = uu_list_first(cfg->proto_list); pi != NULL;
3448 		    pi = uu_list_next(cfg->proto_list, pi)) {
3449 			if (((pi->listen_fd != -1) &&
3450 			    isset_pollfd(pi->listen_fd)) ||
3451 			    pending_connections(instance, pi)) {
3452 				if (cfg->iswait) {
3453 					process_wait_request(instance, pi);
3454 				} else {
3455 					process_nowait_request(instance, pi);
3456 				}
3457 			}
3458 		}
3459 	}
3460 }
3461 
3462 /* ARGSUSED0 */
3463 static void
3464 sigterm_handler(int sig)
3465 {
3466 	got_sigterm = B_TRUE;
3467 }
3468 
3469 /* ARGSUSED0 */
3470 static void
3471 sighup_handler(int sig)
3472 {
3473 	refresh_inetd_requested = B_TRUE;
3474 }
3475 
3476 /*
3477  * inetd's major work loop. This function sits in poll waiting for events
3478  * to occur, processing them when they do. The possible events are
3479  * master restarter requests, expired timer queue timers, stop/refresh signal
3480  * requests, contract events indicating process termination, stop/refresh
3481  * requests originating from one of the stop/refresh inetd processes and
3482  * network events.
3483  * The loop is exited when a stop request is received and processed, and
3484  * all the instances have reached a suitable 'stopping' state.
3485  */
3486 static void
3487 event_loop(void)
3488 {
3489 	instance_t		*instance;
3490 	int			timeout;
3491 
3492 	for (;;) {
3493 		int	pret = -1;
3494 
3495 		if (tlx_pending_counter != 0)
3496 			timeout = 0;
3497 		else
3498 			timeout = iu_earliest_timer(timer_queue);
3499 
3500 		if (!got_sigterm && !refresh_inetd_requested) {
3501 			pret = poll(poll_fds, num_pollfds, timeout);
3502 			if ((pret == -1) && (errno != EINTR)) {
3503 				error_msg(gettext("poll failure: %s"),
3504 				    strerror(errno));
3505 				continue;
3506 			}
3507 		}
3508 
3509 		if (got_sigterm) {
3510 			msg_fini();
3511 			inetd_stop();
3512 			got_sigterm = B_FALSE;
3513 			goto check_if_stopped;
3514 		}
3515 
3516 		/*
3517 		 * Process any stop/refresh requests from the Unix Domain
3518 		 * Socket.
3519 		 */
3520 		if ((pret != -1) && isset_pollfd(uds_fd)) {
3521 			while (process_uds_event() == 0)
3522 				;
3523 		}
3524 
3525 		/*
3526 		 * Process refresh request. We do this check after the UDS
3527 		 * event check above, as it would be wasted processing if we
3528 		 * started refreshing inetd based on a SIGHUP, and then were
3529 		 * told to shut-down via a UDS event.
3530 		 */
3531 		if (refresh_inetd_requested) {
3532 			refresh_inetd_requested = B_FALSE;
3533 			if (!inetd_stopping)
3534 				inetd_refresh();
3535 		}
3536 
3537 		/*
3538 		 * We were interrupted by a signal. Don't waste any more
3539 		 * time processing a potentially inaccurate poll return.
3540 		 */
3541 		if (pret == -1)
3542 			continue;
3543 
3544 		/*
3545 		 * Process any instance restarter events.
3546 		 */
3547 		if (isset_pollfd(rst_event_pipe[PE_CONSUMER])) {
3548 			while (process_restarter_event() == 0)
3549 				;
3550 		}
3551 
3552 		/*
3553 		 * Process any expired timers (bind retry, con-rate offline,
3554 		 * method timeouts).
3555 		 */
3556 		(void) iu_expire_timers(timer_queue);
3557 
3558 		process_terminated_methods();
3559 
3560 		/*
3561 		 * If inetd is stopping, check whether all our managed
3562 		 * instances have been stopped and we can return.
3563 		 */
3564 		if (inetd_stopping) {
3565 check_if_stopped:
3566 			for (instance = uu_list_first(instance_list);
3567 			    instance != NULL;
3568 			    instance = uu_list_next(instance_list, instance)) {
3569 				if (!instance_stopped(instance)) {
3570 					debug_msg("%s not yet stopped",
3571 					    instance->fmri);
3572 					break;
3573 				}
3574 			}
3575 			/* if all instances are stopped, return */
3576 			if (instance == NULL)
3577 				return;
3578 		}
3579 
3580 		process_network_events();
3581 	}
3582 }
3583 
3584 static void
3585 fini(void)
3586 {
3587 	method_fini();
3588 	uds_fini();
3589 	if (timer_queue != NULL)
3590 		iu_tq_destroy(timer_queue);
3591 
3592 
3593 	/*
3594 	 * We don't bother to undo the restarter interface at all.
3595 	 * Because of quirks in the interface, there is no way to
3596 	 * disconnect from the channel and cause any new events to be
3597 	 * queued.  However, any events which are received and not
3598 	 * acknowledged will be re-sent when inetd restarts as long as inetd
3599 	 * uses the same subscriber ID, which it does.
3600 	 *
3601 	 * By keeping the event pipe open but ignoring it, any events which
3602 	 * occur will cause restarter_event_proxy to hang without breaking
3603 	 * anything.
3604 	 */
3605 
3606 	if (instance_list != NULL) {
3607 		void		*cookie = NULL;
3608 		instance_t	*inst;
3609 
3610 		while ((inst = uu_list_teardown(instance_list, &cookie)) !=
3611 		    NULL)
3612 			destroy_instance(inst);
3613 		uu_list_destroy(instance_list);
3614 	}
3615 	if (instance_pool != NULL)
3616 		uu_list_pool_destroy(instance_pool);
3617 	tlx_fini();
3618 	config_fini();
3619 	repval_fini();
3620 	poll_fini();
3621 
3622 	/* Close audit session */
3623 	(void) adt_end_session(audit_handle);
3624 }
3625 
3626 static int
3627 init(void)
3628 {
3629 	int err;
3630 
3631 	if (repval_init() < 0)
3632 		goto failed;
3633 
3634 	if (config_init() < 0)
3635 		goto failed;
3636 
3637 	refresh_debug_flag();
3638 
3639 	if (tlx_init() < 0)
3640 		goto failed;
3641 
3642 	/* Setup instance list. */
3643 	if ((instance_pool = uu_list_pool_create("instance_pool",
3644 	    sizeof (instance_t), offsetof(instance_t, link), NULL,
3645 	    UU_LIST_POOL_DEBUG)) == NULL) {
3646 		error_msg("%s: %s",
3647 		    gettext("Failed to create instance pool"),
3648 		    uu_strerror(uu_error()));
3649 		goto failed;
3650 	}
3651 	if ((instance_list = uu_list_create(instance_pool, NULL, 0)) == NULL) {
3652 		error_msg("%s: %s",
3653 		    gettext("Failed to create instance list"),
3654 		    uu_strerror(uu_error()));
3655 		goto failed;
3656 	}
3657 
3658 	/*
3659 	 * Create event pipe to communicate events with the main event
3660 	 * loop and add it to the event loop's fdset.
3661 	 */
3662 	if (pipe(rst_event_pipe) < 0) {
3663 		error_msg("pipe: %s", strerror(errno));
3664 		goto failed;
3665 	}
3666 	/*
3667 	 * We only leave the producer end to block on reads/writes as we
3668 	 * can't afford to block in the main thread, yet need to in
3669 	 * the restarter event thread, so it can sit and wait for an
3670 	 * acknowledgement to be written to the pipe.
3671 	 */
3672 	disable_blocking(rst_event_pipe[PE_CONSUMER]);
3673 	if ((set_pollfd(rst_event_pipe[PE_CONSUMER], POLLIN)) == -1)
3674 		goto failed;
3675 
3676 	/*
3677 	 * Register with master restarter for managed service events. This
3678 	 * will fail, amongst other reasons, if inetd is already running.
3679 	 */
3680 	if ((err = restarter_bind_handle(RESTARTER_EVENT_VERSION,
3681 	    INETD_INSTANCE_FMRI, restarter_event_proxy, 0,
3682 	    &rst_event_handle)) != 0) {
3683 		error_msg(gettext(
3684 		    "Failed to register for restarter events: %s"),
3685 		    strerror(err));
3686 		goto failed;
3687 	}
3688 
3689 	if (contract_init() < 0)
3690 		goto failed;
3691 
3692 	if ((timer_queue = iu_tq_create()) == NULL) {
3693 		error_msg(gettext("Failed to create timer queue."));
3694 		goto failed;
3695 	}
3696 
3697 	if (uds_init() < 0)
3698 		goto failed;
3699 
3700 	if (method_init() < 0)
3701 		goto failed;
3702 
3703 	/* Initialize auditing session */
3704 	if (adt_start_session(&audit_handle, NULL, ADT_USE_PROC_DATA) != 0) {
3705 		error_msg(gettext("Unable to start audit session"));
3706 	}
3707 
3708 	/*
3709 	 * Initialize signal dispositions/masks
3710 	 */
3711 	(void) sigset(SIGHUP, sighup_handler);
3712 	(void) sigset(SIGTERM, sigterm_handler);
3713 	(void) sigignore(SIGINT);
3714 
3715 	return (0);
3716 
3717 failed:
3718 	fini();
3719 	return (-1);
3720 }
3721 
3722 static int
3723 start_method(void)
3724 {
3725 	int	i;
3726 	int	pipe_fds[2];
3727 	int	child;
3728 
3729 	/* Create pipe for child to notify parent of initialization success. */
3730 	if (pipe(pipe_fds) < 0) {
3731 		error_msg("pipe: %s", strerror(errno));
3732 		return (SMF_EXIT_ERR_OTHER);
3733 	}
3734 
3735 	if ((child = fork()) == -1) {
3736 		error_msg("fork: %s", strerror(errno));
3737 		(void) close(pipe_fds[PE_CONSUMER]);
3738 		(void) close(pipe_fds[PE_PRODUCER]);
3739 		return (SMF_EXIT_ERR_OTHER);
3740 	} else if (child > 0) {			/* parent */
3741 
3742 		/* Wait on child to return success of initialization. */
3743 		(void) close(pipe_fds[PE_PRODUCER]);
3744 		if ((safe_read(pipe_fds[PE_CONSUMER], &i, sizeof (i)) != 0) ||
3745 		    (i < 0)) {
3746 			error_msg(gettext(
3747 			    "Initialization failed, unable to start"));
3748 			(void) close(pipe_fds[PE_CONSUMER]);
3749 			/*
3750 			 * Batch all initialization errors as 'other' errors,
3751 			 * resulting in retries being attempted.
3752 			 */
3753 			return (SMF_EXIT_ERR_OTHER);
3754 		} else {
3755 			(void) close(pipe_fds[PE_CONSUMER]);
3756 			return (SMF_EXIT_OK);
3757 		}
3758 	} else {				/* child */
3759 		/*
3760 		 * Perform initialization and return success code down
3761 		 * the pipe.
3762 		 */
3763 		(void) close(pipe_fds[PE_CONSUMER]);
3764 		i = init();
3765 		if ((safe_write(pipe_fds[PE_PRODUCER], &i, sizeof (i)) < 0) ||
3766 		    (i < 0)) {
3767 			error_msg(gettext("pipe write failure: %s"),
3768 			    strerror(errno));
3769 			exit(1);
3770 		}
3771 		(void) close(pipe_fds[PE_PRODUCER]);
3772 
3773 		(void) setsid();
3774 
3775 		/*
3776 		 * Log a message if the configuration file has changed since
3777 		 * inetconv was last run.
3778 		 */
3779 		check_conf_file();
3780 
3781 		event_loop();
3782 
3783 		fini();
3784 		debug_msg("inetd stopped");
3785 		msg_fini();
3786 		exit(0);
3787 	}
3788 	/* NOTREACHED */
3789 }
3790 
3791 /*
3792  * When inetd is run from outside the SMF, this message is output to provide
3793  * the person invoking inetd with further information that will help them
3794  * understand how to start and stop inetd, and to achieve the other
3795  * behaviors achievable with the legacy inetd command line interface, if
3796  * it is possible.
3797  */
3798 static void
3799 legacy_usage(void)
3800 {
3801 	(void) fprintf(stderr,
3802 	    "inetd is now an smf(5) managed service and can no longer be run "
3803 	    "from the\n"
3804 	    "command line. To enable or disable inetd refer to svcadm(1M) on\n"
3805 	    "how to enable \"%s\", the inetd instance.\n"
3806 	    "\n"
3807 	    "The traditional inetd command line option mappings are:\n"
3808 	    "\t-d : there is no supported debug output\n"
3809 	    "\t-s : inetd is only runnable from within the SMF\n"
3810 	    "\t-t : See inetadm(1M) on how to enable TCP tracing\n"
3811 	    "\t-r : See inetadm(1M) on how to set a failure rate\n"
3812 	    "\n"
3813 	    "To specify an alternative configuration file see svccfg(1M)\n"
3814 	    "for how to modify the \"%s/%s\" string type property of\n"
3815 	    "the inetd instance, and modify it according to the syntax:\n"
3816 	    "\"%s [alt_config_file] %%m\".\n"
3817 	    "\n"
3818 	    "For further information on inetd see inetd(1M).\n",
3819 	    INETD_INSTANCE_FMRI, START_METHOD_ARG, SCF_PROPERTY_EXEC,
3820 	    INETD_PATH);
3821 }
3822 
3823 /*
3824  * Usage message printed out for usage errors when running under the SMF.
3825  */
3826 static void
3827 smf_usage(const char *arg0)
3828 {
3829 	error_msg("Usage: %s [alt_conf_file] %s|%s|%s", arg0, START_METHOD_ARG,
3830 	    STOP_METHOD_ARG, REFRESH_METHOD_ARG);
3831 }
3832 
3833 /*
3834  * Returns B_TRUE if we're being run from within the SMF, else B_FALSE.
3835  */
3836 static boolean_t
3837 run_through_smf(void)
3838 {
3839 	char *fmri;
3840 
3841 	/*
3842 	 * check if the instance fmri environment variable has been set by
3843 	 * our restarter.
3844 	 */
3845 	return (((fmri = getenv("SMF_FMRI")) != NULL) &&
3846 	    (strcmp(fmri, INETD_INSTANCE_FMRI) == 0));
3847 }
3848 
3849 int
3850 main(int argc, char *argv[])
3851 {
3852 	char		*method;
3853 	int		ret;
3854 
3855 #if	!defined(TEXT_DOMAIN)
3856 #define	TEXT_DOMAIN "SYS_TEST"
3857 #endif
3858 	(void) textdomain(TEXT_DOMAIN);
3859 	(void) setlocale(LC_ALL, "");
3860 
3861 	if (!run_through_smf()) {
3862 		legacy_usage();
3863 		return (SMF_EXIT_ERR_NOSMF);
3864 	}
3865 
3866 	msg_init();	/* setup logging */
3867 
3868 	(void) enable_extended_FILE_stdio(-1, -1);
3869 
3870 	/* inetd invocation syntax is inetd [alt_conf_file] method_name */
3871 
3872 	switch (argc) {
3873 	case 2:
3874 		method = argv[1];
3875 		break;
3876 	case 3:
3877 		conf_file = argv[1];
3878 		method = argv[2];
3879 		break;
3880 	default:
3881 		smf_usage(argv[0]);
3882 		return (SMF_EXIT_ERR_CONFIG);
3883 
3884 	}
3885 
3886 	if (strcmp(method, START_METHOD_ARG) == 0) {
3887 		ret = start_method();
3888 	} else if (strcmp(method, STOP_METHOD_ARG) == 0) {
3889 		ret = stop_method();
3890 	} else if (strcmp(method, REFRESH_METHOD_ARG) == 0) {
3891 		ret = refresh_method();
3892 	} else {
3893 		smf_usage(argv[0]);
3894 		return (SMF_EXIT_ERR_CONFIG);
3895 	}
3896 
3897 	return (ret);
3898 }
3899