xref: /titanic_53/usr/src/cmd/svc/startd/wait.c (revision 63c7f71bccc7117f1823a1fc784ada39d0cade44)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*63c7f71bSrm88369  * Common Development and Distribution License (the "License").
6*63c7f71bSrm88369  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*63c7f71bSrm88369  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * wait.c - asynchronous monitoring of "wait registered" start methods
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Use event ports to poll on the set of fds representing the /proc/[pid]/psinfo
327c478bd9Sstevel@tonic-gate  * files.  If one of these fds returns an event, then we inform the restarter
337c478bd9Sstevel@tonic-gate  * that it has stopped.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * The wait_info_list holds the series of processes currently being monitored
367c478bd9Sstevel@tonic-gate  * for exit.  The wi_fd member, which contains the file descriptor of the psinfo
377c478bd9Sstevel@tonic-gate  * file being polled upon ("event ported upon"), will be set to -1 if the file
387c478bd9Sstevel@tonic-gate  * descriptor is inactive (already closed or not yet opened).
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef _FILE_OFFSET_BITS
427c478bd9Sstevel@tonic-gate #undef _FILE_OFFSET_BITS
437c478bd9Sstevel@tonic-gate #endif /* _FILE_OFFSET_BITS */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/resource.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/uio.h>
497c478bd9Sstevel@tonic-gate #include <sys/wait.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <assert.h>
527c478bd9Sstevel@tonic-gate #include <errno.h>
537c478bd9Sstevel@tonic-gate #include <fcntl.h>
547c478bd9Sstevel@tonic-gate #include <libuutil.h>
557c478bd9Sstevel@tonic-gate #include <poll.h>
567c478bd9Sstevel@tonic-gate #include <port.h>
577c478bd9Sstevel@tonic-gate #include <pthread.h>
587c478bd9Sstevel@tonic-gate #include <procfs.h>
597c478bd9Sstevel@tonic-gate #include <string.h>
607c478bd9Sstevel@tonic-gate #include <stropts.h>
617c478bd9Sstevel@tonic-gate #include <unistd.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include "startd.h"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	WAIT_FILES	262144		/* reasonably high maximum */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static int port_fd;
687c478bd9Sstevel@tonic-gate static scf_handle_t *wait_hndl;
697c478bd9Sstevel@tonic-gate static struct rlimit init_fd_rlimit;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static uu_list_pool_t *wait_info_pool;
727c478bd9Sstevel@tonic-gate static uu_list_t *wait_info_list;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static pthread_mutex_t wait_info_lock;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * void wait_remove(wait_info_t *, int)
787c478bd9Sstevel@tonic-gate  *   Remove the given wait_info structure from our list, performing various
797c478bd9Sstevel@tonic-gate  *   cleanup operations along the way.  If the direct flag is false (meaning
80*63c7f71bSrm88369  *   that we are being called with from restarter instance list context) and
81*63c7f71bSrm88369  *   the instance should not be ignored, then notify the restarter that the
82*63c7f71bSrm88369  *   associated instance has exited. If the wi_ignore flag is true then it
83*63c7f71bSrm88369  *   means that the stop was initiated from within svc.startd, rather than
84*63c7f71bSrm88369  *   from outside it.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  *   Since we may no longer be the startd that started this process, we only are
877c478bd9Sstevel@tonic-gate  *   concerned with a waitpid(3C) failure if the wi_parent field is non-zero.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate static void
907c478bd9Sstevel@tonic-gate wait_remove(wait_info_t *wi, int direct)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	int status;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (waitpid(wi->wi_pid, &status, 0) == -1) {
957c478bd9Sstevel@tonic-gate 		if (wi->wi_parent)
967c478bd9Sstevel@tonic-gate 			log_framework(LOG_INFO,
977c478bd9Sstevel@tonic-gate 			    "instance %s waitpid failure: %s\n", wi->wi_fmri,
987c478bd9Sstevel@tonic-gate 			    strerror(errno));
997c478bd9Sstevel@tonic-gate 	} else {
1007c478bd9Sstevel@tonic-gate 		if (WEXITSTATUS(status) != 0) {
1017c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
1027c478bd9Sstevel@tonic-gate 			    "instance %s exited with status %d\n", wi->wi_fmri,
1037c478bd9Sstevel@tonic-gate 			    WEXITSTATUS(status));
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&wait_info_lock);
108e39bb43dSrm88369 	if (wi->wi_fd != -1) {
109e39bb43dSrm88369 		startd_close(wi->wi_fd);
110e39bb43dSrm88369 		wi->wi_fd = -1;
111e39bb43dSrm88369 	}
1127c478bd9Sstevel@tonic-gate 	uu_list_remove(wait_info_list, wi);
1137c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&wait_info_lock);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * Make an attempt to clear out any utmpx record associated with this
1177c478bd9Sstevel@tonic-gate 	 * PID.
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 	utmpx_mark_dead(wi->wi_pid, status, B_FALSE);
1207c478bd9Sstevel@tonic-gate 
121*63c7f71bSrm88369 	if (!direct && !wi->wi_ignore) {
1227c478bd9Sstevel@tonic-gate 		/*
1237c478bd9Sstevel@tonic-gate 		 * Bind wait_hndl lazily.
1247c478bd9Sstevel@tonic-gate 		 */
1257c478bd9Sstevel@tonic-gate 		if (wait_hndl == NULL) {
1267c478bd9Sstevel@tonic-gate 			for (wait_hndl =
1277c478bd9Sstevel@tonic-gate 			    libscf_handle_create_bound(SCF_VERSION);
1287c478bd9Sstevel@tonic-gate 			    wait_hndl == NULL;
1297c478bd9Sstevel@tonic-gate 			    wait_hndl =
1307c478bd9Sstevel@tonic-gate 			    libscf_handle_create_bound(SCF_VERSION)) {
1317c478bd9Sstevel@tonic-gate 				log_error(LOG_INFO, "[wait_remove] Unable to "
1327c478bd9Sstevel@tonic-gate 				    "bind a new repository handle: %s\n",
1337c478bd9Sstevel@tonic-gate 				    scf_strerror(scf_error()));
1347c478bd9Sstevel@tonic-gate 				(void) sleep(2);
1357c478bd9Sstevel@tonic-gate 			}
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
1397c478bd9Sstevel@tonic-gate 		    "wait_remove requesting stop of %s\n", wi->wi_fmri);
1407c478bd9Sstevel@tonic-gate 		(void) stop_instance_fmri(wait_hndl, wi->wi_fmri, RSTOP_EXIT);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	uu_list_node_fini(wi, &wi->wi_link, wait_info_pool);
1447c478bd9Sstevel@tonic-gate 	startd_free(wi, sizeof (wait_info_t));
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
148*63c7f71bSrm88369  * void wait_ignore_by_fmri(const char *)
149*63c7f71bSrm88369  *   wait_ignore_by_fmri is called when svc.startd is going to stop the
150*63c7f71bSrm88369  *   instance. Since we need to wait on the process and close the utmpx record,
151*63c7f71bSrm88369  *   we're going to set the wi_ignore flag, so that when the process exits we
152*63c7f71bSrm88369  *   clean up, but don't tell the restarter to stop it.
153*63c7f71bSrm88369  */
154*63c7f71bSrm88369 void
155*63c7f71bSrm88369 wait_ignore_by_fmri(const char *fmri)
156*63c7f71bSrm88369 {
157*63c7f71bSrm88369 	wait_info_t *wi;
158*63c7f71bSrm88369 
159*63c7f71bSrm88369 	MUTEX_LOCK(&wait_info_lock);
160*63c7f71bSrm88369 
161*63c7f71bSrm88369 	for (wi = uu_list_first(wait_info_list); wi != NULL;
162*63c7f71bSrm88369 	    wi = uu_list_next(wait_info_list, wi)) {
163*63c7f71bSrm88369 		if (strcmp(wi->wi_fmri, fmri) == 0)
164*63c7f71bSrm88369 			break;
165*63c7f71bSrm88369 	}
166*63c7f71bSrm88369 
167*63c7f71bSrm88369 	if (wi != NULL) {
168*63c7f71bSrm88369 		wi->wi_ignore = 1;
169*63c7f71bSrm88369 	}
170*63c7f71bSrm88369 
171*63c7f71bSrm88369 	MUTEX_UNLOCK(&wait_info_lock);
172*63c7f71bSrm88369 }
173*63c7f71bSrm88369 
174*63c7f71bSrm88369 /*
1757c478bd9Sstevel@tonic-gate  * int wait_register(pid_t, char *, int, int)
1767c478bd9Sstevel@tonic-gate  *   wait_register is called after we have called fork(2), and know which pid we
1777c478bd9Sstevel@tonic-gate  *   wish to monitor.  However, since the child may have already exited by the
1787c478bd9Sstevel@tonic-gate  *   time we are called, we must handle the error cases from open(2)
1797c478bd9Sstevel@tonic-gate  *   appropriately.  The am_parent flag is recorded to handle waitpid(2)
1807c478bd9Sstevel@tonic-gate  *   behaviour on removal; similarly, the direct flag is passed through to a
1817c478bd9Sstevel@tonic-gate  *   potential call to wait_remove() to govern its behaviour in different
1827c478bd9Sstevel@tonic-gate  *   contexts.
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  *   Returns 0 if registration successful, 1 if child pid did not exist, and -1
1857c478bd9Sstevel@tonic-gate  *   if a different error occurred.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate int
1887c478bd9Sstevel@tonic-gate wait_register(pid_t pid, const char *inst_fmri, int am_parent, int direct)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	char *fname = uu_msprintf("/proc/%ld/psinfo", pid);
1917c478bd9Sstevel@tonic-gate 	int fd;
1927c478bd9Sstevel@tonic-gate 	wait_info_t *wi;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	assert(pid != 0);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (fname == NULL)
1977c478bd9Sstevel@tonic-gate 		return (-1);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	wi = startd_alloc(sizeof (wait_info_t));
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	uu_list_node_init(wi, &wi->wi_link, wait_info_pool);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	wi->wi_fd = -1;
2047c478bd9Sstevel@tonic-gate 	wi->wi_pid = pid;
2057c478bd9Sstevel@tonic-gate 	wi->wi_fmri = inst_fmri;
2067c478bd9Sstevel@tonic-gate 	wi->wi_parent = am_parent;
207*63c7f71bSrm88369 	wi->wi_ignore = 0;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&wait_info_lock);
2107c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_before(wait_info_list, NULL, wi);
2117c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&wait_info_lock);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1) {
2147c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
2157c478bd9Sstevel@tonic-gate 			/*
2167c478bd9Sstevel@tonic-gate 			 * Child has already exited.
2177c478bd9Sstevel@tonic-gate 			 */
2187c478bd9Sstevel@tonic-gate 			wait_remove(wi, direct);
2197c478bd9Sstevel@tonic-gate 			uu_free(fname);
2207c478bd9Sstevel@tonic-gate 			return (1);
2217c478bd9Sstevel@tonic-gate 		} else {
2227c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
2237c478bd9Sstevel@tonic-gate 			    "open %s failed; not monitoring %s: %s\n", fname,
2247c478bd9Sstevel@tonic-gate 			    inst_fmri, strerror(errno));
2257c478bd9Sstevel@tonic-gate 			uu_free(fname);
2267c478bd9Sstevel@tonic-gate 			return (-1);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	uu_free(fname);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	wi->wi_fd = fd;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi)) {
2357c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
2367c478bd9Sstevel@tonic-gate 		    "initial port_association of %d / %s failed: %s\n", fd,
2377c478bd9Sstevel@tonic-gate 		    inst_fmri, strerror(errno));
2387c478bd9Sstevel@tonic-gate 		return (-1);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "monitoring PID %ld on fd %d (%s)\n", pid, fd,
2427c478bd9Sstevel@tonic-gate 	    inst_fmri);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	return (0);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2487c478bd9Sstevel@tonic-gate void *
2497c478bd9Sstevel@tonic-gate wait_thread(void *args)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	for (;;) {
2527c478bd9Sstevel@tonic-gate 		port_event_t pe;
2537c478bd9Sstevel@tonic-gate 		int fd;
2547c478bd9Sstevel@tonic-gate 		wait_info_t *wi;
2557c478bd9Sstevel@tonic-gate 
256e39bb43dSrm88369 		if (port_get(port_fd, &pe, NULL) != 0) {
257e39bb43dSrm88369 			if (errno == EINTR)
2587c478bd9Sstevel@tonic-gate 				continue;
259e39bb43dSrm88369 			else {
2607c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
261e39bb43dSrm88369 				    "port_get() failed with %s\n",
262e39bb43dSrm88369 				    strerror(errno));
263e39bb43dSrm88369 				bad_error("port_get", errno);
264e39bb43dSrm88369 			}
265e39bb43dSrm88369 		}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		fd = pe.portev_object;
2687c478bd9Sstevel@tonic-gate 		wi = pe.portev_user;
269e39bb43dSrm88369 		assert(wi != NULL);
270e39bb43dSrm88369 		assert(fd == wi->wi_fd);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		if ((pe.portev_events & POLLHUP) == POLLHUP) {
2737c478bd9Sstevel@tonic-gate 			psinfo_t psi;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			if (lseek(fd, 0, SEEK_SET) != 0 ||
2767c478bd9Sstevel@tonic-gate 			    read(fd, &psi, sizeof (psinfo_t)) !=
2777c478bd9Sstevel@tonic-gate 			    sizeof (psinfo_t)) {
2787c478bd9Sstevel@tonic-gate 				log_framework(LOG_WARNING,
2797c478bd9Sstevel@tonic-gate 				    "couldn't get psinfo data for %s (%s); "
2807c478bd9Sstevel@tonic-gate 				    "assuming failed\n", wi->wi_fmri,
2817c478bd9Sstevel@tonic-gate 				    strerror(errno));
2827c478bd9Sstevel@tonic-gate 				goto err_remove;
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 			if (psi.pr_nlwp != 0 ||
2867c478bd9Sstevel@tonic-gate 			    psi.pr_nzomb != 0 ||
2877c478bd9Sstevel@tonic-gate 			    psi.pr_lwp.pr_lwpid != 0) {
2887c478bd9Sstevel@tonic-gate 				/*
2897c478bd9Sstevel@tonic-gate 				 * We have determined, in accordance with the
2907c478bd9Sstevel@tonic-gate 				 * definition in proc(4), this process is not a
2917c478bd9Sstevel@tonic-gate 				 * zombie.  Reassociate.
2927c478bd9Sstevel@tonic-gate 				 */
2937c478bd9Sstevel@tonic-gate 				if (port_associate(port_fd, PORT_SOURCE_FD, fd,
2947c478bd9Sstevel@tonic-gate 				    0, wi))
2957c478bd9Sstevel@tonic-gate 					log_error(LOG_WARNING,
2967c478bd9Sstevel@tonic-gate 					    "port_association of %d / %s "
2977c478bd9Sstevel@tonic-gate 					    "failed\n", fd, wi->wi_fmri);
2987c478bd9Sstevel@tonic-gate 				continue;
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 		} else if (
3017c478bd9Sstevel@tonic-gate 		    (pe.portev_events & POLLERR) == 0) {
3027c478bd9Sstevel@tonic-gate 			if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi))
3037c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
3047c478bd9Sstevel@tonic-gate 				    "port_association of %d / %s "
3057c478bd9Sstevel@tonic-gate 				    "failed\n", fd, wi->wi_fmri);
3067c478bd9Sstevel@tonic-gate 			continue;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate err_remove:
3107c478bd9Sstevel@tonic-gate 		wait_remove(wi, 0);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate void
3177c478bd9Sstevel@tonic-gate wait_prefork()
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&wait_info_lock);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate void
3237c478bd9Sstevel@tonic-gate wait_postfork(pid_t pid)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	wait_info_t *wi;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&wait_info_lock);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (pid != 0)
3307c478bd9Sstevel@tonic-gate 		return;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Close all of the child's wait-related fds.  The wait_thread() is
3347c478bd9Sstevel@tonic-gate 	 * gone, so no need to worry about returning events.  We always exec(2)
3357c478bd9Sstevel@tonic-gate 	 * after a fork request, so we needn't free the list elements
3367c478bd9Sstevel@tonic-gate 	 * themselves.
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	for (wi = uu_list_first(wait_info_list);
3407c478bd9Sstevel@tonic-gate 	    wi != NULL;
3417c478bd9Sstevel@tonic-gate 	    wi = uu_list_next(wait_info_list, wi)) {
3427c478bd9Sstevel@tonic-gate 		if (wi->wi_fd != -1)
3437c478bd9Sstevel@tonic-gate 			startd_close(wi->wi_fd);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	startd_close(port_fd);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate void
3527c478bd9Sstevel@tonic-gate wait_init()
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	struct rlimit fd_new;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
3577c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_NOFILE, &fd_new);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	fd_new.rlim_max = fd_new.rlim_cur = WAIT_FILES;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &fd_new);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if ((port_fd = port_create()) == -1)
3647c478bd9Sstevel@tonic-gate 		uu_die("wait_init couldn't port_create");
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	wait_info_pool = uu_list_pool_create("wait_info", sizeof (wait_info_t),
3677c478bd9Sstevel@tonic-gate 	    offsetof(wait_info_t, wi_link), NULL, UU_LIST_POOL_DEBUG);
3687c478bd9Sstevel@tonic-gate 	if (wait_info_pool == NULL)
3697c478bd9Sstevel@tonic-gate 		uu_die("wait_init couldn't create wait_info_pool");
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	wait_info_list = uu_list_create(wait_info_pool, wait_info_list, 0);
3727c478bd9Sstevel@tonic-gate 	if (wait_info_list == NULL)
3737c478bd9Sstevel@tonic-gate 		uu_die("wait_init couldn't create wait_info_list");
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&wait_info_lock, &mutex_attrs);
3767c478bd9Sstevel@tonic-gate }
377