xref: /titanic_52/usr/src/cmd/ctrun/ctrun.c (revision 7b209c2cc5ea45251aba06dcc6181d3f23da807a)
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*7b209c2cSacruz  * Common Development and Distribution License (the "License").
6*7b209c2cSacruz  * 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*7b209c2cSacruz  * 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 #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/wait.h>
307c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
317c478bd9Sstevel@tonic-gate #include <sys/contract.h>
327c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <libuutil.h>
427c478bd9Sstevel@tonic-gate #include <libcontract.h>
437c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <langinfo.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static int opt_verbose;
497c478bd9Sstevel@tonic-gate static int opt_Verbose;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	OPT_NORMAL	0x1
527c478bd9Sstevel@tonic-gate #define	OPT_FATAL	0x2
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef struct optvect {
557c478bd9Sstevel@tonic-gate 	const char	*opt_name;
567c478bd9Sstevel@tonic-gate 	uint_t		opt_value;
577c478bd9Sstevel@tonic-gate 	uint_t		opt_flags;
587c478bd9Sstevel@tonic-gate } optvect_t;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static optvect_t option_params[] = {
617c478bd9Sstevel@tonic-gate 	{ "noorphan", CT_PR_NOORPHAN },
627c478bd9Sstevel@tonic-gate 	{ "pgrponly", CT_PR_PGRPONLY },
637c478bd9Sstevel@tonic-gate 	{ "regent", CT_PR_REGENT },
647c478bd9Sstevel@tonic-gate 	{ "inherit", CT_PR_INHERIT },
657c478bd9Sstevel@tonic-gate 	{ NULL }
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static optvect_t option_events[] = {
697c478bd9Sstevel@tonic-gate 	{ "core", CT_PR_EV_CORE, OPT_NORMAL | OPT_FATAL },
707c478bd9Sstevel@tonic-gate 	{ "signal", CT_PR_EV_SIGNAL, OPT_NORMAL | OPT_FATAL },
717c478bd9Sstevel@tonic-gate 	{ "hwerr", CT_PR_EV_HWERR, OPT_NORMAL | OPT_FATAL },
727c478bd9Sstevel@tonic-gate 	{ "empty", CT_PR_EV_EMPTY, OPT_NORMAL },
737c478bd9Sstevel@tonic-gate 	{ "fork", CT_PR_EV_FORK, OPT_NORMAL },
747c478bd9Sstevel@tonic-gate 	{ "exit", CT_PR_EV_EXIT, OPT_NORMAL },
757c478bd9Sstevel@tonic-gate 	{ NULL }
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate typedef enum lifetime {
797c478bd9Sstevel@tonic-gate 	LT_NONE,
807c478bd9Sstevel@tonic-gate 	LT_CHILD,
817c478bd9Sstevel@tonic-gate 	LT_CONTRACT
827c478bd9Sstevel@tonic-gate } lifetime_t;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Exit code to use when the child exited abnormally (i.e. exited with
867c478bd9Sstevel@tonic-gate  * a status we are unable to emulate).
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #define	EXIT_BADCHILD	123
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	USAGESTR	\
917c478bd9Sstevel@tonic-gate 	"Usage: %s [-i eventlist] [-f eventlist] [-l lifetime] \n" \
92*7b209c2cSacruz 	"\t[-o optionlist] [-r count [-t]] [-v]\n" \
93*7b209c2cSacruz 	"\t[-F fmri] [-A aux] command\n"
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * usage
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * Educate the user.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate static void
1017c478bd9Sstevel@tonic-gate usage(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(USAGESTR), uu_getpname());
1047c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * bit2str
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * Convert a bit into its string representation.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static const char *
1137c478bd9Sstevel@tonic-gate bit2str(optvect_t *options, uint_t bit)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	for (; options->opt_name; options++)
1167c478bd9Sstevel@tonic-gate 		if (options->opt_value == bit)
1177c478bd9Sstevel@tonic-gate 			return (options->opt_name);
1187c478bd9Sstevel@tonic-gate 	return (NULL);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * str2bit
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * Convert a string into its bit representation.  If match is set, only
1257c478bd9Sstevel@tonic-gate  * look at those options with the match bit set in its opt_flags
1267c478bd9Sstevel@tonic-gate  * field.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate static uint_t
1297c478bd9Sstevel@tonic-gate str2bit(optvect_t *options, int match, const char *str, int len)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	for (; options->opt_name; options++) {
1327c478bd9Sstevel@tonic-gate 		if (match && (options->opt_flags & match) == 0)
1337c478bd9Sstevel@tonic-gate 			continue;
1347c478bd9Sstevel@tonic-gate 		if (strncmp(str, options->opt_name, len) == 0)
1357c478bd9Sstevel@tonic-gate 			return (options->opt_value);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * opt2bits
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * Given a set of textual options separated by commas or spaces,
1447c478bd9Sstevel@tonic-gate  * convert them to a set of bits.  Errors are fatal, except for empty
1457c478bd9Sstevel@tonic-gate  * options (which are ignored) and duplicate options (which are
1467c478bd9Sstevel@tonic-gate  * idempotent).
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate static void
1497c478bd9Sstevel@tonic-gate opt2bits(optvect_t *options, int match, const char *str, uint_t *bits, char c)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	const char *ptr, *next = str;
1527c478bd9Sstevel@tonic-gate 	uint_t result = 0;
1537c478bd9Sstevel@tonic-gate 	uint_t bit;
1547c478bd9Sstevel@tonic-gate 	int none = 0;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	while (*str) {
1577c478bd9Sstevel@tonic-gate 		int len;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		ptr = strpbrk(str, ", ");
1607c478bd9Sstevel@tonic-gate 		if (ptr != NULL) {
1617c478bd9Sstevel@tonic-gate 			len = ptr - str;
1627c478bd9Sstevel@tonic-gate 			next = ptr + 1;
1637c478bd9Sstevel@tonic-gate 		} else {
1647c478bd9Sstevel@tonic-gate 			len = strlen(str);
1657c478bd9Sstevel@tonic-gate 			next = str + len;
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		if (len == 0) {
1687c478bd9Sstevel@tonic-gate 			uu_warn(gettext("empty option\n"));
1697c478bd9Sstevel@tonic-gate 			bit = 0;
1707c478bd9Sstevel@tonic-gate 		} else {
1717c478bd9Sstevel@tonic-gate 			bit = str2bit(options, match, str, len);
1727c478bd9Sstevel@tonic-gate 			if (bit == 0 && strncmp(str, "none", len) == 0) {
1737c478bd9Sstevel@tonic-gate 				none = 1;
1747c478bd9Sstevel@tonic-gate 				if (result)
1757c478bd9Sstevel@tonic-gate 					goto noneerr;
1767c478bd9Sstevel@tonic-gate 			} else if (bit == 0) {
1777c478bd9Sstevel@tonic-gate 				uu_warn(gettext("unrecognized option '%.*s'\n"),
1787c478bd9Sstevel@tonic-gate 				    len, str);
1797c478bd9Sstevel@tonic-gate 				uu_warn(gettext("error parsing '-%c' option\n"),
1807c478bd9Sstevel@tonic-gate 				    c);
1817c478bd9Sstevel@tonic-gate 				usage();
1827c478bd9Sstevel@tonic-gate 			} else if (none) {
1837c478bd9Sstevel@tonic-gate 				goto noneerr;
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 			if (result & bit)
1867c478bd9Sstevel@tonic-gate 				uu_warn(gettext("option '%.*s' "
1877c478bd9Sstevel@tonic-gate 				    "specified twice\n"), len, str);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		result |= bit;
1907c478bd9Sstevel@tonic-gate 		str = next;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	*bits = result;
1947c478bd9Sstevel@tonic-gate 	return;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate noneerr:
1977c478bd9Sstevel@tonic-gate 	uu_warn(gettext("option is incompatible with others: '%s'\n"), "none");
1987c478bd9Sstevel@tonic-gate 	usage();
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * close_on_exec
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * Given a fd, marks it close-on-exec.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate static int
2077c478bd9Sstevel@tonic-gate close_on_exec(int fd)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	int flags = fcntl(fd, F_GETFD, 0);
2107c478bd9Sstevel@tonic-gate 	if ((flags != -1) && (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1))
2117c478bd9Sstevel@tonic-gate 		return (0);
2127c478bd9Sstevel@tonic-gate 	return (-1);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * v_printf
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  * Output routine for messages printed only when -v is specified.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
2217c478bd9Sstevel@tonic-gate static void
2227c478bd9Sstevel@tonic-gate v_printf(const char *format, ...)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	va_list va;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (opt_verbose) {
2277c478bd9Sstevel@tonic-gate 		(void) printf("%s(%ld): ", uu_getpname(), getpid());
2287c478bd9Sstevel@tonic-gate 		va_start(va, format);
2297c478bd9Sstevel@tonic-gate 		(void) vprintf(format, va);
2307c478bd9Sstevel@tonic-gate 		va_end(va);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * get_event
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  * Reads and acknowledges an event.  Returns the event type.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate static uint_t
2407c478bd9Sstevel@tonic-gate get_event(int fd, int ctfd, ctid_t ctid)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	ct_evthdl_t ev;
2437c478bd9Sstevel@tonic-gate 	uint_t result;
2447c478bd9Sstevel@tonic-gate 	ctevid_t evid;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	for (;;) {
2477c478bd9Sstevel@tonic-gate 		int efd;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		/*
2507c478bd9Sstevel@tonic-gate 		 * Normally we only need to look at critical messages.
2517c478bd9Sstevel@tonic-gate 		 * If we are displaying contract events, however, we
2527c478bd9Sstevel@tonic-gate 		 * have to read them all.
2537c478bd9Sstevel@tonic-gate 		 */
2547c478bd9Sstevel@tonic-gate 		errno = opt_verbose ? ct_event_read(fd, &ev) :
2557c478bd9Sstevel@tonic-gate 		    ct_event_read_critical(fd, &ev);
2567c478bd9Sstevel@tonic-gate 		if (errno != 0)
2577c478bd9Sstevel@tonic-gate 			uu_die(gettext("failed to listen to contract events"));
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		/*
2607c478bd9Sstevel@tonic-gate 		 * If requested, display the event.
2617c478bd9Sstevel@tonic-gate 		 */
2627c478bd9Sstevel@tonic-gate 		if (opt_verbose) {
2637c478bd9Sstevel@tonic-gate 			v_printf(gettext("event from contract %ld: "),
2647c478bd9Sstevel@tonic-gate 			    ct_event_get_ctid(ev));
2657c478bd9Sstevel@tonic-gate 			contract_event_dump(stdout, ev, opt_Verbose);
2667c478bd9Sstevel@tonic-gate 			if ((ct_event_get_flags(ev) & CTE_INFO) != 0) {
2677c478bd9Sstevel@tonic-gate 				ct_event_free(ev);
2687c478bd9Sstevel@tonic-gate 				continue;
2697c478bd9Sstevel@tonic-gate 			}
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		/*
2737c478bd9Sstevel@tonic-gate 		 * We're done if this event is one of ours.
2747c478bd9Sstevel@tonic-gate 		 */
2757c478bd9Sstevel@tonic-gate 		evid = ct_event_get_evid(ev);
2767c478bd9Sstevel@tonic-gate 		if (ct_event_get_ctid(ev) == ctid)
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		/*
2807c478bd9Sstevel@tonic-gate 		 * ACK events from other contracts.
2817c478bd9Sstevel@tonic-gate 		 * This shouldn't happen, but it could.
2827c478bd9Sstevel@tonic-gate 		 */
2837c478bd9Sstevel@tonic-gate 		efd = contract_open(ct_event_get_ctid(ev), "process", "ctl",
2847c478bd9Sstevel@tonic-gate 		    O_WRONLY);
2857c478bd9Sstevel@tonic-gate 		if (efd != -1) {
2867c478bd9Sstevel@tonic-gate 			(void) ct_ctl_ack(efd, evid);
2877c478bd9Sstevel@tonic-gate 			(void) close(efd);
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		ct_event_free(ev);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Note that if we want to use ctrun as a simple restarter, we
2947c478bd9Sstevel@tonic-gate 	 * need persistently keep track of fatal events so we can
2957c478bd9Sstevel@tonic-gate 	 * properly handle the death of the contract.  Rather than keep
2967c478bd9Sstevel@tonic-gate 	 * a file or somesuch lying around, it might make more sense to
2977c478bd9Sstevel@tonic-gate 	 * leave the significant fatal event sitting in the queue so
2987c478bd9Sstevel@tonic-gate 	 * that a restarted instance of ctrun can pick it up.  For now
2997c478bd9Sstevel@tonic-gate 	 * we'll just ACK all events.
3007c478bd9Sstevel@tonic-gate 	 */
3017c478bd9Sstevel@tonic-gate 	(void) ct_ctl_ack(ctfd, evid);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	result = ct_event_get_type(ev);
3047c478bd9Sstevel@tonic-gate 	ct_event_free(ev);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	return (result);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * abandon
3117c478bd9Sstevel@tonic-gate  *
3127c478bd9Sstevel@tonic-gate  * Given an fd for a contract's ctl file, abandon the contract and
3137c478bd9Sstevel@tonic-gate  * close the file.
3147c478bd9Sstevel@tonic-gate  */
3157c478bd9Sstevel@tonic-gate static void
3167c478bd9Sstevel@tonic-gate abandon(int ctfd)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	if (ct_ctl_abandon(ctfd) == -1)
3197c478bd9Sstevel@tonic-gate 		uu_die(gettext("failed to abandon contract %d"), ctfd);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	(void) close(ctfd);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate static int chldstat;
3257c478bd9Sstevel@tonic-gate static int chldexited;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * sigchld
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  * Our SIGCHLD handler.  Sets chldstat and chldexited so the
3317c478bd9Sstevel@tonic-gate  * interrupted code knows what happened.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3347c478bd9Sstevel@tonic-gate static void
3357c478bd9Sstevel@tonic-gate sigchld(int sig, struct siginfo *si, void *ucp)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	int err = errno;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (si->si_code == CLD_EXITED)
3407c478bd9Sstevel@tonic-gate 		chldstat = si->si_status;
3417c478bd9Sstevel@tonic-gate 	else
3427c478bd9Sstevel@tonic-gate 		chldstat = EXIT_BADCHILD;
3437c478bd9Sstevel@tonic-gate 	chldexited = 1;
3447c478bd9Sstevel@tonic-gate 	while (waitpid(si->si_pid, NULL, 0) == -1 && errno == EINTR)
3457c478bd9Sstevel@tonic-gate 		;
3467c478bd9Sstevel@tonic-gate 	errno = err;
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * dowait
3517c478bd9Sstevel@tonic-gate  *
3527c478bd9Sstevel@tonic-gate  * Waits for the specified child to exit.  Returns the exit code ctrun
3537c478bd9Sstevel@tonic-gate  * should return.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static int
3567c478bd9Sstevel@tonic-gate dowait(int pid)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	pid_t wpid;
3597c478bd9Sstevel@tonic-gate 	int wstatus;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	do
3627c478bd9Sstevel@tonic-gate 		wpid = waitpid(pid, &wstatus, 0);
3637c478bd9Sstevel@tonic-gate 	while (wpid == -1 && errno == EINTR);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (wpid == -1)
3667c478bd9Sstevel@tonic-gate 		uu_die(gettext("wait failed"));
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (WIFEXITED(wstatus))
3697c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(wstatus));
3707c478bd9Sstevel@tonic-gate 	else
3717c478bd9Sstevel@tonic-gate 		return (EXIT_BADCHILD);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate int
3757c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	int	fd, efd;
3787c478bd9Sstevel@tonic-gate 	pid_t	pid;
3797c478bd9Sstevel@tonic-gate 	ctid_t	ctid = 0;
3807c478bd9Sstevel@tonic-gate 	int	ctfd;
3817c478bd9Sstevel@tonic-gate 	int	pipefds[2];
3827c478bd9Sstevel@tonic-gate 	struct sigaction osact;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	int	s;
3857c478bd9Sstevel@tonic-gate 	ctid_t	opt_adopt = 0;
3867c478bd9Sstevel@tonic-gate 	int	opt_transfer = 0;
3877c478bd9Sstevel@tonic-gate 	int	opt_count = -1;
3887c478bd9Sstevel@tonic-gate 	uint_t	opt_info = CT_PR_EV_CORE;
3897c478bd9Sstevel@tonic-gate 	uint_t	opt_crit = 0;
3907c478bd9Sstevel@tonic-gate 	uint_t	eff_fatal, opt_fatal = CT_PR_EV_HWERR;
3917c478bd9Sstevel@tonic-gate 	uint_t	eff_param, opt_param = 0;
3927c478bd9Sstevel@tonic-gate 	lifetime_t opt_life = LT_CONTRACT;
3937c478bd9Sstevel@tonic-gate 
394*7b209c2cSacruz 	char *svc_fmri = NULL;
395*7b209c2cSacruz 	char *svc_aux = NULL;
396*7b209c2cSacruz 
3977c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3987c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3997c478bd9Sstevel@tonic-gate 	uu_alt_exit(UU_PROFILE_LAUNCHER);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
4027c478bd9Sstevel@tonic-gate 
403*7b209c2cSacruz 	while ((s = getopt(argc, argv, "a:A:l:o:i:c:f:F:r:tvV")) != EOF) {
4047c478bd9Sstevel@tonic-gate 		switch (s) {
4057c478bd9Sstevel@tonic-gate 		case 'a':
4067c478bd9Sstevel@tonic-gate 			if (uu_strtoint(optarg, &opt_adopt, sizeof (opt_adopt),
4077c478bd9Sstevel@tonic-gate 			    0, 0, INT32_MAX) == -1) {
4087c478bd9Sstevel@tonic-gate 				uu_warn(gettext("invalid contract ID '%s'\n"),
4097c478bd9Sstevel@tonic-gate 				    optarg);
4107c478bd9Sstevel@tonic-gate 				usage();
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 			break;
4137c478bd9Sstevel@tonic-gate 		case 'v':
4147c478bd9Sstevel@tonic-gate 			opt_verbose = 1;
4157c478bd9Sstevel@tonic-gate 			break;
4167c478bd9Sstevel@tonic-gate 		case 'V':
4177c478bd9Sstevel@tonic-gate 			opt_Verbose = 1;
4187c478bd9Sstevel@tonic-gate 			opt_verbose = 1;
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate 		case 't':
4217c478bd9Sstevel@tonic-gate 			opt_transfer = 1;
4227c478bd9Sstevel@tonic-gate 			break;
4237c478bd9Sstevel@tonic-gate 		case 'r':
4247c478bd9Sstevel@tonic-gate 			if (uu_strtoint(optarg, &opt_count, sizeof (opt_adopt),
4257c478bd9Sstevel@tonic-gate 			    0, 0, INT32_MAX) == -1) {
4267c478bd9Sstevel@tonic-gate 				uu_warn(gettext("invalid count '%s'\n"),
4277c478bd9Sstevel@tonic-gate 				    optarg);
4287c478bd9Sstevel@tonic-gate 				usage();
4297c478bd9Sstevel@tonic-gate 			}
4307c478bd9Sstevel@tonic-gate 			break;
4317c478bd9Sstevel@tonic-gate 		case 'l':
4327c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "none") == 0) {
4337c478bd9Sstevel@tonic-gate 				opt_life = LT_NONE;
4347c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "child") == 0) {
4357c478bd9Sstevel@tonic-gate 				opt_life = LT_CHILD;
4367c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "contract") == 0) {
4377c478bd9Sstevel@tonic-gate 				opt_life = LT_CONTRACT;
4387c478bd9Sstevel@tonic-gate 			} else {
4397c478bd9Sstevel@tonic-gate 				uu_warn(gettext("invalid lifetime '%s'\n"),
4407c478bd9Sstevel@tonic-gate 				    optarg);
4417c478bd9Sstevel@tonic-gate 				usage();
4427c478bd9Sstevel@tonic-gate 			}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 			break;
4457c478bd9Sstevel@tonic-gate 		case 'o':
4467c478bd9Sstevel@tonic-gate 			opt2bits(option_params, 0, optarg, &opt_param,
4477c478bd9Sstevel@tonic-gate 			    optopt);
4487c478bd9Sstevel@tonic-gate 			break;
4497c478bd9Sstevel@tonic-gate 		case 'i':
4507c478bd9Sstevel@tonic-gate 			opt2bits(option_events, OPT_NORMAL, optarg, &opt_info,
4517c478bd9Sstevel@tonic-gate 			    optopt);
4527c478bd9Sstevel@tonic-gate 			break;
4537c478bd9Sstevel@tonic-gate 		case 'c':
4547c478bd9Sstevel@tonic-gate 			opt2bits(option_events, OPT_NORMAL, optarg, &opt_crit,
4557c478bd9Sstevel@tonic-gate 			    optopt);
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 		case 'f':
4587c478bd9Sstevel@tonic-gate 			opt2bits(option_events, OPT_FATAL, optarg, &opt_fatal,
4597c478bd9Sstevel@tonic-gate 			    optopt);
4607c478bd9Sstevel@tonic-gate 			break;
461*7b209c2cSacruz 		case 'F':
462*7b209c2cSacruz 			svc_fmri = optarg;
463*7b209c2cSacruz 			break;
464*7b209c2cSacruz 		case 'A':
465*7b209c2cSacruz 			svc_aux = optarg;
466*7b209c2cSacruz 			break;
4677c478bd9Sstevel@tonic-gate 		default:
4687c478bd9Sstevel@tonic-gate 			usage();
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	argc -= optind;
4727c478bd9Sstevel@tonic-gate 	argv += optind;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/*
4757c478bd9Sstevel@tonic-gate 	 * Basic argument sanity checks.
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 	if ((opt_life == LT_NONE) && (opt_param & CT_PR_NOORPHAN)) {
4787c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot use option '%s' with lifetime '%s'\n"),
4797c478bd9Sstevel@tonic-gate 		    bit2str(option_params, CT_PR_NOORPHAN), "none");
4807c478bd9Sstevel@tonic-gate 		usage();
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if ((opt_life != LT_CONTRACT) && (opt_count >= 0)) {
4847c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot restart with lifetime '%s'\n"),
4857c478bd9Sstevel@tonic-gate 		    opt_life == LT_NONE ? "none" : "child");
4867c478bd9Sstevel@tonic-gate 		usage();
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if ((opt_param & CT_PR_PGRPONLY) && (opt_count >= 0)) {
4907c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot restart with option '%s'\n"),
4917c478bd9Sstevel@tonic-gate 		    bit2str(option_params, CT_PR_PGRPONLY));
4927c478bd9Sstevel@tonic-gate 		usage();
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (opt_transfer && (opt_count == -1)) {
4967c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot transfer when not restarting\n"));
4977c478bd9Sstevel@tonic-gate 		usage();
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (argc <= 0)
5017c478bd9Sstevel@tonic-gate 		usage();
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * Create a process contract template and our process's process
5057c478bd9Sstevel@tonic-gate 	 * contract bundle endpoint.  Mark them close-on-exec so we
5067c478bd9Sstevel@tonic-gate 	 * don't have to worry about closing them in our child.
5077c478bd9Sstevel@tonic-gate 	 */
5087c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
5097c478bd9Sstevel@tonic-gate 	if (fd == -1)
5107c478bd9Sstevel@tonic-gate 		uu_die(gettext("template open failed"));
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	efd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
5137c478bd9Sstevel@tonic-gate 	if (efd == -1)
5147c478bd9Sstevel@tonic-gate 		uu_die(gettext("process bundle open failed"));
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (close_on_exec(fd) || close_on_exec(efd))
5177c478bd9Sstevel@tonic-gate 		uu_die(gettext("could not set FD_CLOEXEC"));
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Set the process contract's terms based on our arguments.
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	if (errno = ct_pr_tmpl_set_param(fd, opt_param))
5237c478bd9Sstevel@tonic-gate 		uu_die(gettext("set param failed"));
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (errno = ct_tmpl_set_informative(fd, opt_info))
5267c478bd9Sstevel@tonic-gate 		uu_die(gettext("set notify failed"));
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if (errno = ct_pr_tmpl_set_fatal(fd, opt_fatal))
5297c478bd9Sstevel@tonic-gate 		uu_die(gettext("set fatal failed"));
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (opt_param & CT_PR_PGRPONLY)
5327c478bd9Sstevel@tonic-gate 		opt_crit = CT_PR_EV_EMPTY;
5337c478bd9Sstevel@tonic-gate 	else
5347c478bd9Sstevel@tonic-gate 		opt_crit |= opt_fatal | CT_PR_EV_EMPTY;
5357c478bd9Sstevel@tonic-gate 	if (errno = ct_tmpl_set_critical(fd, opt_crit))
5367c478bd9Sstevel@tonic-gate 		uu_die(gettext("set critical failed"));
537*7b209c2cSacruz 	if (svc_fmri && (errno = ct_pr_tmpl_set_svc_fmri(fd, svc_fmri)))
538*7b209c2cSacruz 		uu_die(gettext("set fmri failed: "
539*7b209c2cSacruz 		    "insufficient privileges\n"));
540*7b209c2cSacruz 	if (svc_aux && (errno = ct_pr_tmpl_set_svc_aux(fd, svc_aux)))
541*7b209c2cSacruz 		uu_die(gettext("set aux failed"));
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	/*
5447c478bd9Sstevel@tonic-gate 	 * Activate the template.
5457c478bd9Sstevel@tonic-gate 	 */
5467c478bd9Sstevel@tonic-gate 	if (errno = ct_tmpl_activate(fd))
5477c478bd9Sstevel@tonic-gate 		uu_die(gettext("template activate failed"));
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate restart:
5507c478bd9Sstevel@tonic-gate 	if (opt_adopt) {
5517c478bd9Sstevel@tonic-gate 		/*
5527c478bd9Sstevel@tonic-gate 		 * Adopt a specific contract.
5537c478bd9Sstevel@tonic-gate 		 */
5547c478bd9Sstevel@tonic-gate 		ct_stathdl_t st;
5557c478bd9Sstevel@tonic-gate 		int stfd;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		if ((ctfd = contract_open(opt_adopt, "process", "ctl",
5587c478bd9Sstevel@tonic-gate 		    O_WRONLY)) == -1)
5597c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not open contract %ld"),
5607c478bd9Sstevel@tonic-gate 			    opt_adopt);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * Read the contract's terms so that we interpret its
5647c478bd9Sstevel@tonic-gate 		 * events properly.
5657c478bd9Sstevel@tonic-gate 		 */
5667c478bd9Sstevel@tonic-gate 		if (((stfd = contract_open(opt_adopt, "process", "status",
5677c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == -1) ||
5687c478bd9Sstevel@tonic-gate 		    (errno = ct_status_read(stfd, CTD_FIXED, &st)) ||
5697c478bd9Sstevel@tonic-gate 		    (errno = ct_pr_status_get_fatal(st, &eff_fatal)) ||
5707c478bd9Sstevel@tonic-gate 		    (errno = ct_pr_status_get_param(st, &eff_param)))
5717c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not stat contract %ld"),
5727c478bd9Sstevel@tonic-gate 			    opt_adopt);
5737c478bd9Sstevel@tonic-gate 		ct_status_free(st);
5747c478bd9Sstevel@tonic-gate 		(void) close(stfd);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		if (errno = ct_ctl_adopt(ctfd))
5777c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not adopt contract %ld"),
5787c478bd9Sstevel@tonic-gate 			    opt_adopt);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		ctid = opt_adopt;
5817c478bd9Sstevel@tonic-gate 		opt_adopt = 0;
5827c478bd9Sstevel@tonic-gate 		v_printf(gettext("adopted contract id %ld\n"), ctid);
5837c478bd9Sstevel@tonic-gate 	} else {
5847c478bd9Sstevel@tonic-gate 		/*
5857c478bd9Sstevel@tonic-gate 		 * Create a new process.
5867c478bd9Sstevel@tonic-gate 		 */
5877c478bd9Sstevel@tonic-gate 		if (opt_life == LT_CONTRACT) {
5887c478bd9Sstevel@tonic-gate 			struct sigaction sact;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			/*
5917c478bd9Sstevel@tonic-gate 			 * Since we are going to be waiting for and
5927c478bd9Sstevel@tonic-gate 			 * reacting to contract events, install a
5937c478bd9Sstevel@tonic-gate 			 * signal handler so we capture the exit status
5947c478bd9Sstevel@tonic-gate 			 * of our child.
5957c478bd9Sstevel@tonic-gate 			 */
5967c478bd9Sstevel@tonic-gate 			chldstat = UU_EXIT_OK;
5977c478bd9Sstevel@tonic-gate 			chldexited = 0;
5987c478bd9Sstevel@tonic-gate 			sact.sa_sigaction = sigchld;
5997c478bd9Sstevel@tonic-gate 			sact.sa_flags = SA_SIGINFO | SA_RESTART |
6007c478bd9Sstevel@tonic-gate 			    SA_NOCLDSTOP;
6017c478bd9Sstevel@tonic-gate 			(void) sigemptyset(&sact.sa_mask);
6027c478bd9Sstevel@tonic-gate 			if (sigaction(SIGCHLD, &sact, &osact) == -1)
6037c478bd9Sstevel@tonic-gate 				uu_die(gettext("failed to install "
6047c478bd9Sstevel@tonic-gate 				    "sigchld handler"));
6057c478bd9Sstevel@tonic-gate 		} else if (opt_life == LT_NONE) {
6067c478bd9Sstevel@tonic-gate 			/*
6077c478bd9Sstevel@tonic-gate 			 * Though we aren't waiting for our child to
6087c478bd9Sstevel@tonic-gate 			 * exit, as a well-behaved command launcher we
6097c478bd9Sstevel@tonic-gate 			 * must wait for it to exec.  On success the
6107c478bd9Sstevel@tonic-gate 			 * pipe will simply close, and on failure the
6117c478bd9Sstevel@tonic-gate 			 * proper exit status will be sent.
6127c478bd9Sstevel@tonic-gate 			 */
6137c478bd9Sstevel@tonic-gate 			if (pipe(pipefds) == -1 ||
6147c478bd9Sstevel@tonic-gate 			    close_on_exec(pipefds[0]) == -1 ||
6157c478bd9Sstevel@tonic-gate 			    close_on_exec(pipefds[1]) == -1)
6167c478bd9Sstevel@tonic-gate 				uu_die(gettext("failed to create pipe"));
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == -1) {
6207c478bd9Sstevel@tonic-gate 			uu_die(gettext("fork failed"));
6217c478bd9Sstevel@tonic-gate 		} else if (pid == 0) {
6227c478bd9Sstevel@tonic-gate 			int result = execvp(argv[0], argv);
6237c478bd9Sstevel@tonic-gate 			if (opt_life == LT_NONE) {
6247c478bd9Sstevel@tonic-gate 				char a = 1;
6257c478bd9Sstevel@tonic-gate 				int err = errno;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 				(void) write(pipefds[1], &a, sizeof (a));
6287c478bd9Sstevel@tonic-gate 				errno = err;
6297c478bd9Sstevel@tonic-gate 			}
6307c478bd9Sstevel@tonic-gate 			if (result == -1)
6317c478bd9Sstevel@tonic-gate 				uu_xdie(errno == ENOENT ? 127 : 126,
6327c478bd9Sstevel@tonic-gate 				    gettext("exec failed"));
6337c478bd9Sstevel@tonic-gate 			uu_die(gettext("exec returned!\n"));
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		/*
6377c478bd9Sstevel@tonic-gate 		 * Get the newly-created contract's id and ctl fd.
6387c478bd9Sstevel@tonic-gate 		 */
6397c478bd9Sstevel@tonic-gate 		if (errno = contract_latest(&ctid))
6407c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not get new contract's id"));
6417c478bd9Sstevel@tonic-gate 		if ((ctfd = contract_open(ctid, "process", "ctl",
6427c478bd9Sstevel@tonic-gate 		    O_WRONLY)) == -1)
6437c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not open contract"));
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		/*
6467c478bd9Sstevel@tonic-gate 		 * Clear the transfer parameter so that the contract
6477c478bd9Sstevel@tonic-gate 		 * will be freed sooner and admins won't get nervous.
6487c478bd9Sstevel@tonic-gate 		 */
6497c478bd9Sstevel@tonic-gate 		if (opt_transfer) {
6507c478bd9Sstevel@tonic-gate 			(void) ct_pr_tmpl_set_transfer(fd, 0);
6517c478bd9Sstevel@tonic-gate 			(void) ct_tmpl_activate(fd);
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		v_printf(gettext("created contract id %ld\n"), ctid);
6557c478bd9Sstevel@tonic-gate 		eff_param = opt_param;
6567c478bd9Sstevel@tonic-gate 		eff_fatal = opt_fatal;
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (opt_life == LT_CONTRACT) {
6607c478bd9Sstevel@tonic-gate 		uint_t event, errevent = 0;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * Wait until the contract empties out.
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		do {
6667c478bd9Sstevel@tonic-gate 			event = get_event(efd, ctfd, ctid);
6677c478bd9Sstevel@tonic-gate 			if (event & eff_fatal) {
6687c478bd9Sstevel@tonic-gate 				if ((eff_param & CT_PR_PGRPONLY) == 0)
6697c478bd9Sstevel@tonic-gate 					errevent = event;
6707c478bd9Sstevel@tonic-gate 				v_printf(gettext(
6717c478bd9Sstevel@tonic-gate 				    "fatal \"%s\" event from contract %ld\n"),
6727c478bd9Sstevel@tonic-gate 				    bit2str(option_events, event), ctid);
6737c478bd9Sstevel@tonic-gate 			}
6747c478bd9Sstevel@tonic-gate 		} while ((event & CT_PR_EV_EMPTY) == 0);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		/*
6777c478bd9Sstevel@tonic-gate 		 * If we encountered a fatal error event, and we
6787c478bd9Sstevel@tonic-gate 		 * haven't expended our maximum loop count, restart.
6797c478bd9Sstevel@tonic-gate 		 */
6807c478bd9Sstevel@tonic-gate 		if ((errevent != 0) &&
6817c478bd9Sstevel@tonic-gate 		    ((opt_count == 0) || (opt_count-- > 1))) {
6827c478bd9Sstevel@tonic-gate 			v_printf(gettext("failure in contract %ld, "
6837c478bd9Sstevel@tonic-gate 			    "restarting command\n"), ctid);
6847c478bd9Sstevel@tonic-gate 			if (opt_transfer) {
6857c478bd9Sstevel@tonic-gate 				/*
6867c478bd9Sstevel@tonic-gate 				 * Add the failed contract to the new
6877c478bd9Sstevel@tonic-gate 				 * contract's terms so that its
6887c478bd9Sstevel@tonic-gate 				 * inherited subcontracts can be
6897c478bd9Sstevel@tonic-gate 				 * adopted by the new process.
6907c478bd9Sstevel@tonic-gate 				 */
6917c478bd9Sstevel@tonic-gate 				if (errno = ct_pr_tmpl_set_transfer(fd, ctid))
6927c478bd9Sstevel@tonic-gate 					uu_die(gettext("set transfer failed"));
6937c478bd9Sstevel@tonic-gate 				if (errno = ct_tmpl_activate(fd))
6947c478bd9Sstevel@tonic-gate 					uu_die(gettext(
6957c478bd9Sstevel@tonic-gate 					    "template activate failed"));
6967c478bd9Sstevel@tonic-gate 				(void) close(ctfd);
6977c478bd9Sstevel@tonic-gate 			} else {
6987c478bd9Sstevel@tonic-gate 				abandon(ctfd);
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 			goto restart;
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * At this point we are done with the contract; we
7057c478bd9Sstevel@tonic-gate 		 * don't want it to be inherited when we exit.
7067c478bd9Sstevel@tonic-gate 		 */
7077c478bd9Sstevel@tonic-gate 		abandon(ctfd);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		/*
7107c478bd9Sstevel@tonic-gate 		 * In case there was a race between SIGCHLD delivery
7117c478bd9Sstevel@tonic-gate 		 * and contract event delivery, disable the signal
7127c478bd9Sstevel@tonic-gate 		 * handler and look for the child.
7137c478bd9Sstevel@tonic-gate 		 */
7147c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGCHLD, &osact, NULL);
7157c478bd9Sstevel@tonic-gate 		if (chldexited == 0)
7167c478bd9Sstevel@tonic-gate 			chldstat = dowait(pid);
7177c478bd9Sstevel@tonic-gate 	} else if (opt_life == LT_NONE) {
7187c478bd9Sstevel@tonic-gate 		char a;
7197c478bd9Sstevel@tonic-gate 		int result;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		chldstat = UU_EXIT_OK;
7227c478bd9Sstevel@tonic-gate 		(void) close(pipefds[1]);
7237c478bd9Sstevel@tonic-gate 		do {
7247c478bd9Sstevel@tonic-gate 			result = read(pipefds[0], &a, sizeof (a));
7257c478bd9Sstevel@tonic-gate 			if (result == -1 && errno != EINTR)
7267c478bd9Sstevel@tonic-gate 				uu_die(gettext("read failed"));
7277c478bd9Sstevel@tonic-gate 			if (result == 1)
7287c478bd9Sstevel@tonic-gate 				chldstat = dowait(pid);
7297c478bd9Sstevel@tonic-gate 		} while (result == -1);
7307c478bd9Sstevel@tonic-gate 	} else {
7317c478bd9Sstevel@tonic-gate 		chldstat = dowait(pid);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	return (chldstat);
7357c478bd9Sstevel@tonic-gate }
736