xref: /titanic_52/usr/src/cmd/svc/startd/wait.c (revision 2a17138d7a5102bc6e0bf0444224cd0c416d98f0)
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
563c7f71bSrm88369  * Common Development and Distribution License (the "License").
663c7f71bSrm88369  * 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 /*
2263c7f71bSrm88369  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*2a17138dSJerry Jelinek  * Copyright 2012, Joyent, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * wait.c - asynchronous monitoring of "wait registered" start methods
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Use event ports to poll on the set of fds representing the /proc/[pid]/psinfo
317c478bd9Sstevel@tonic-gate  * files.  If one of these fds returns an event, then we inform the restarter
327c478bd9Sstevel@tonic-gate  * that it has stopped.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * The wait_info_list holds the series of processes currently being monitored
357c478bd9Sstevel@tonic-gate  * for exit.  The wi_fd member, which contains the file descriptor of the psinfo
367c478bd9Sstevel@tonic-gate  * file being polled upon ("event ported upon"), will be set to -1 if the file
377c478bd9Sstevel@tonic-gate  * descriptor is inactive (already closed or not yet opened).
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifdef _FILE_OFFSET_BITS
417c478bd9Sstevel@tonic-gate #undef _FILE_OFFSET_BITS
427c478bd9Sstevel@tonic-gate #endif /* _FILE_OFFSET_BITS */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/resource.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/uio.h>
487c478bd9Sstevel@tonic-gate #include <sys/wait.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <assert.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
537c478bd9Sstevel@tonic-gate #include <libuutil.h>
547c478bd9Sstevel@tonic-gate #include <poll.h>
557c478bd9Sstevel@tonic-gate #include <port.h>
567c478bd9Sstevel@tonic-gate #include <pthread.h>
577c478bd9Sstevel@tonic-gate #include <procfs.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <stropts.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include "startd.h"
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	WAIT_FILES	262144		/* reasonably high maximum */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int port_fd;
677c478bd9Sstevel@tonic-gate static scf_handle_t *wait_hndl;
687c478bd9Sstevel@tonic-gate static struct rlimit init_fd_rlimit;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static uu_list_pool_t *wait_info_pool;
717c478bd9Sstevel@tonic-gate static uu_list_t *wait_info_list;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static pthread_mutex_t wait_info_lock;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * void wait_remove(wait_info_t *, int)
777c478bd9Sstevel@tonic-gate  *   Remove the given wait_info structure from our list, performing various
787c478bd9Sstevel@tonic-gate  *   cleanup operations along the way.  If the direct flag is false (meaning
7963c7f71bSrm88369  *   that we are being called with from restarter instance list context) and
8063c7f71bSrm88369  *   the instance should not be ignored, then notify the restarter that the
8163c7f71bSrm88369  *   associated instance has exited. If the wi_ignore flag is true then it
8263c7f71bSrm88369  *   means that the stop was initiated from within svc.startd, rather than
8363c7f71bSrm88369  *   from outside it.
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *   Since we may no longer be the startd that started this process, we only are
867c478bd9Sstevel@tonic-gate  *   concerned with a waitpid(3C) failure if the wi_parent field is non-zero.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate static void
897c478bd9Sstevel@tonic-gate wait_remove(wait_info_t *wi, int direct)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	int status;
92*2a17138dSJerry Jelinek 	stop_cause_t cause = RSTOP_EXIT;
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));
104*2a17138dSJerry Jelinek 			if (WEXITSTATUS(status) == SMF_EXIT_ERR_CONFIG)
105*2a17138dSJerry Jelinek 				cause = RSTOP_ERR_CFG;
106*2a17138dSJerry Jelinek 			else
107*2a17138dSJerry Jelinek 				cause = RSTOP_ERR_EXIT;
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&wait_info_lock);
112e39bb43dSrm88369 	if (wi->wi_fd != -1) {
113e39bb43dSrm88369 		startd_close(wi->wi_fd);
114e39bb43dSrm88369 		wi->wi_fd = -1;
115e39bb43dSrm88369 	}
1167c478bd9Sstevel@tonic-gate 	uu_list_remove(wait_info_list, wi);
1177c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&wait_info_lock);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/*
1207c478bd9Sstevel@tonic-gate 	 * Make an attempt to clear out any utmpx record associated with this
1217c478bd9Sstevel@tonic-gate 	 * PID.
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	utmpx_mark_dead(wi->wi_pid, status, B_FALSE);
1247c478bd9Sstevel@tonic-gate 
12563c7f71bSrm88369 	if (!direct && !wi->wi_ignore) {
1267c478bd9Sstevel@tonic-gate 		/*
1277c478bd9Sstevel@tonic-gate 		 * Bind wait_hndl lazily.
1287c478bd9Sstevel@tonic-gate 		 */
1297c478bd9Sstevel@tonic-gate 		if (wait_hndl == NULL) {
1307c478bd9Sstevel@tonic-gate 			for (wait_hndl =
1317c478bd9Sstevel@tonic-gate 			    libscf_handle_create_bound(SCF_VERSION);
1327c478bd9Sstevel@tonic-gate 			    wait_hndl == NULL;
1337c478bd9Sstevel@tonic-gate 			    wait_hndl =
1347c478bd9Sstevel@tonic-gate 			    libscf_handle_create_bound(SCF_VERSION)) {
1357c478bd9Sstevel@tonic-gate 				log_error(LOG_INFO, "[wait_remove] Unable to "
1367c478bd9Sstevel@tonic-gate 				    "bind a new repository handle: %s\n",
1377c478bd9Sstevel@tonic-gate 				    scf_strerror(scf_error()));
1387c478bd9Sstevel@tonic-gate 				(void) sleep(2);
1397c478bd9Sstevel@tonic-gate 			}
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
1437c478bd9Sstevel@tonic-gate 		    "wait_remove requesting stop of %s\n", wi->wi_fmri);
144*2a17138dSJerry Jelinek 		(void) stop_instance_fmri(wait_hndl, wi->wi_fmri, cause);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	uu_list_node_fini(wi, &wi->wi_link, wait_info_pool);
1487c478bd9Sstevel@tonic-gate 	startd_free(wi, sizeof (wait_info_t));
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
15263c7f71bSrm88369  * void wait_ignore_by_fmri(const char *)
15363c7f71bSrm88369  *   wait_ignore_by_fmri is called when svc.startd is going to stop the
15463c7f71bSrm88369  *   instance. Since we need to wait on the process and close the utmpx record,
15563c7f71bSrm88369  *   we're going to set the wi_ignore flag, so that when the process exits we
15663c7f71bSrm88369  *   clean up, but don't tell the restarter to stop it.
15763c7f71bSrm88369  */
15863c7f71bSrm88369 void
15963c7f71bSrm88369 wait_ignore_by_fmri(const char *fmri)
16063c7f71bSrm88369 {
16163c7f71bSrm88369 	wait_info_t *wi;
16263c7f71bSrm88369 
16363c7f71bSrm88369 	MUTEX_LOCK(&wait_info_lock);
16463c7f71bSrm88369 
16563c7f71bSrm88369 	for (wi = uu_list_first(wait_info_list); wi != NULL;
16663c7f71bSrm88369 	    wi = uu_list_next(wait_info_list, wi)) {
16763c7f71bSrm88369 		if (strcmp(wi->wi_fmri, fmri) == 0)
16863c7f71bSrm88369 			break;
16963c7f71bSrm88369 	}
17063c7f71bSrm88369 
17163c7f71bSrm88369 	if (wi != NULL) {
17263c7f71bSrm88369 		wi->wi_ignore = 1;
17363c7f71bSrm88369 	}
17463c7f71bSrm88369 
17563c7f71bSrm88369 	MUTEX_UNLOCK(&wait_info_lock);
17663c7f71bSrm88369 }
17763c7f71bSrm88369 
17863c7f71bSrm88369 /*
1797c478bd9Sstevel@tonic-gate  * int wait_register(pid_t, char *, int, int)
1807c478bd9Sstevel@tonic-gate  *   wait_register is called after we have called fork(2), and know which pid we
1817c478bd9Sstevel@tonic-gate  *   wish to monitor.  However, since the child may have already exited by the
1827c478bd9Sstevel@tonic-gate  *   time we are called, we must handle the error cases from open(2)
1837c478bd9Sstevel@tonic-gate  *   appropriately.  The am_parent flag is recorded to handle waitpid(2)
1847c478bd9Sstevel@tonic-gate  *   behaviour on removal; similarly, the direct flag is passed through to a
1857c478bd9Sstevel@tonic-gate  *   potential call to wait_remove() to govern its behaviour in different
1867c478bd9Sstevel@tonic-gate  *   contexts.
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  *   Returns 0 if registration successful, 1 if child pid did not exist, and -1
1897c478bd9Sstevel@tonic-gate  *   if a different error occurred.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate int
1927c478bd9Sstevel@tonic-gate wait_register(pid_t pid, const char *inst_fmri, int am_parent, int direct)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	char *fname = uu_msprintf("/proc/%ld/psinfo", pid);
1957c478bd9Sstevel@tonic-gate 	int fd;
1967c478bd9Sstevel@tonic-gate 	wait_info_t *wi;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	assert(pid != 0);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (fname == NULL)
2017c478bd9Sstevel@tonic-gate 		return (-1);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	wi = startd_alloc(sizeof (wait_info_t));
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	uu_list_node_init(wi, &wi->wi_link, wait_info_pool);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	wi->wi_fd = -1;
2087c478bd9Sstevel@tonic-gate 	wi->wi_pid = pid;
2097c478bd9Sstevel@tonic-gate 	wi->wi_fmri = inst_fmri;
2107c478bd9Sstevel@tonic-gate 	wi->wi_parent = am_parent;
21163c7f71bSrm88369 	wi->wi_ignore = 0;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&wait_info_lock);
2147c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_before(wait_info_list, NULL, wi);
2157c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&wait_info_lock);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1) {
2187c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
2197c478bd9Sstevel@tonic-gate 			/*
2207c478bd9Sstevel@tonic-gate 			 * Child has already exited.
2217c478bd9Sstevel@tonic-gate 			 */
2227c478bd9Sstevel@tonic-gate 			wait_remove(wi, direct);
2237c478bd9Sstevel@tonic-gate 			uu_free(fname);
2247c478bd9Sstevel@tonic-gate 			return (1);
2257c478bd9Sstevel@tonic-gate 		} else {
2267c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
2277c478bd9Sstevel@tonic-gate 			    "open %s failed; not monitoring %s: %s\n", fname,
2287c478bd9Sstevel@tonic-gate 			    inst_fmri, strerror(errno));
2297c478bd9Sstevel@tonic-gate 			uu_free(fname);
2307c478bd9Sstevel@tonic-gate 			return (-1);
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	uu_free(fname);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	wi->wi_fd = fd;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi)) {
2397c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
2407c478bd9Sstevel@tonic-gate 		    "initial port_association of %d / %s failed: %s\n", fd,
2417c478bd9Sstevel@tonic-gate 		    inst_fmri, strerror(errno));
2427c478bd9Sstevel@tonic-gate 		return (-1);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "monitoring PID %ld on fd %d (%s)\n", pid, fd,
2467c478bd9Sstevel@tonic-gate 	    inst_fmri);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (0);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2527c478bd9Sstevel@tonic-gate void *
2537c478bd9Sstevel@tonic-gate wait_thread(void *args)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	for (;;) {
2567c478bd9Sstevel@tonic-gate 		port_event_t pe;
2577c478bd9Sstevel@tonic-gate 		int fd;
2587c478bd9Sstevel@tonic-gate 		wait_info_t *wi;
2597c478bd9Sstevel@tonic-gate 
260e39bb43dSrm88369 		if (port_get(port_fd, &pe, NULL) != 0) {
261e39bb43dSrm88369 			if (errno == EINTR)
2627c478bd9Sstevel@tonic-gate 				continue;
263e39bb43dSrm88369 			else {
2647c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
265e39bb43dSrm88369 				    "port_get() failed with %s\n",
266e39bb43dSrm88369 				    strerror(errno));
267e39bb43dSrm88369 				bad_error("port_get", errno);
268e39bb43dSrm88369 			}
269e39bb43dSrm88369 		}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		fd = pe.portev_object;
2727c478bd9Sstevel@tonic-gate 		wi = pe.portev_user;
273e39bb43dSrm88369 		assert(wi != NULL);
274e39bb43dSrm88369 		assert(fd == wi->wi_fd);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		if ((pe.portev_events & POLLHUP) == POLLHUP) {
2777c478bd9Sstevel@tonic-gate 			psinfo_t psi;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			if (lseek(fd, 0, SEEK_SET) != 0 ||
2807c478bd9Sstevel@tonic-gate 			    read(fd, &psi, sizeof (psinfo_t)) !=
2817c478bd9Sstevel@tonic-gate 			    sizeof (psinfo_t)) {
2827c478bd9Sstevel@tonic-gate 				log_framework(LOG_WARNING,
2837c478bd9Sstevel@tonic-gate 				    "couldn't get psinfo data for %s (%s); "
2847c478bd9Sstevel@tonic-gate 				    "assuming failed\n", wi->wi_fmri,
2857c478bd9Sstevel@tonic-gate 				    strerror(errno));
2867c478bd9Sstevel@tonic-gate 				goto err_remove;
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			if (psi.pr_nlwp != 0 ||
2907c478bd9Sstevel@tonic-gate 			    psi.pr_nzomb != 0 ||
2917c478bd9Sstevel@tonic-gate 			    psi.pr_lwp.pr_lwpid != 0) {
2927c478bd9Sstevel@tonic-gate 				/*
2937c478bd9Sstevel@tonic-gate 				 * We have determined, in accordance with the
2947c478bd9Sstevel@tonic-gate 				 * definition in proc(4), this process is not a
2957c478bd9Sstevel@tonic-gate 				 * zombie.  Reassociate.
2967c478bd9Sstevel@tonic-gate 				 */
2977c478bd9Sstevel@tonic-gate 				if (port_associate(port_fd, PORT_SOURCE_FD, fd,
2987c478bd9Sstevel@tonic-gate 				    0, wi))
2997c478bd9Sstevel@tonic-gate 					log_error(LOG_WARNING,
3007c478bd9Sstevel@tonic-gate 					    "port_association of %d / %s "
3017c478bd9Sstevel@tonic-gate 					    "failed\n", fd, wi->wi_fmri);
3027c478bd9Sstevel@tonic-gate 				continue;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 		} else if (
3057c478bd9Sstevel@tonic-gate 		    (pe.portev_events & POLLERR) == 0) {
3067c478bd9Sstevel@tonic-gate 			if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi))
3077c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
3087c478bd9Sstevel@tonic-gate 				    "port_association of %d / %s "
3097c478bd9Sstevel@tonic-gate 				    "failed\n", fd, wi->wi_fmri);
3107c478bd9Sstevel@tonic-gate 			continue;
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate err_remove:
3147c478bd9Sstevel@tonic-gate 		wait_remove(wi, 0);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate void
3217c478bd9Sstevel@tonic-gate wait_prefork()
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&wait_info_lock);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate void
3277c478bd9Sstevel@tonic-gate wait_postfork(pid_t pid)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	wait_info_t *wi;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&wait_info_lock);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (pid != 0)
3347c478bd9Sstevel@tonic-gate 		return;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * Close all of the child's wait-related fds.  The wait_thread() is
3387c478bd9Sstevel@tonic-gate 	 * gone, so no need to worry about returning events.  We always exec(2)
3397c478bd9Sstevel@tonic-gate 	 * after a fork request, so we needn't free the list elements
3407c478bd9Sstevel@tonic-gate 	 * themselves.
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	for (wi = uu_list_first(wait_info_list);
3447c478bd9Sstevel@tonic-gate 	    wi != NULL;
3457c478bd9Sstevel@tonic-gate 	    wi = uu_list_next(wait_info_list, wi)) {
3467c478bd9Sstevel@tonic-gate 		if (wi->wi_fd != -1)
3477c478bd9Sstevel@tonic-gate 			startd_close(wi->wi_fd);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	startd_close(port_fd);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate void
3567c478bd9Sstevel@tonic-gate wait_init()
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	struct rlimit fd_new;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
3617c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_NOFILE, &fd_new);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	fd_new.rlim_max = fd_new.rlim_cur = WAIT_FILES;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &fd_new);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if ((port_fd = port_create()) == -1)
3687c478bd9Sstevel@tonic-gate 		uu_die("wait_init couldn't port_create");
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	wait_info_pool = uu_list_pool_create("wait_info", sizeof (wait_info_t),
3717c478bd9Sstevel@tonic-gate 	    offsetof(wait_info_t, wi_link), NULL, UU_LIST_POOL_DEBUG);
3727c478bd9Sstevel@tonic-gate 	if (wait_info_pool == NULL)
3737c478bd9Sstevel@tonic-gate 		uu_die("wait_init couldn't create wait_info_pool");
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	wait_info_list = uu_list_create(wait_info_pool, wait_info_list, 0);
3767c478bd9Sstevel@tonic-gate 	if (wait_info_list == NULL)
3777c478bd9Sstevel@tonic-gate 		uu_die("wait_init couldn't create wait_info_list");
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&wait_info_lock, &mutex_attrs);
3807c478bd9Sstevel@tonic-gate }
381