xref: /titanic_52/usr/src/cmd/ctrun/ctrun.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/contract.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <signal.h>
41*7c478bd9Sstevel@tonic-gate #include <limits.h>
42*7c478bd9Sstevel@tonic-gate #include <libuutil.h>
43*7c478bd9Sstevel@tonic-gate #include <libcontract.h>
44*7c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #include <locale.h>
47*7c478bd9Sstevel@tonic-gate #include <langinfo.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate static int opt_verbose;
50*7c478bd9Sstevel@tonic-gate static int opt_Verbose;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #define	OPT_NORMAL	0x1
53*7c478bd9Sstevel@tonic-gate #define	OPT_FATAL	0x2
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate typedef struct optvect {
56*7c478bd9Sstevel@tonic-gate 	const char	*opt_name;
57*7c478bd9Sstevel@tonic-gate 	uint_t		opt_value;
58*7c478bd9Sstevel@tonic-gate 	uint_t		opt_flags;
59*7c478bd9Sstevel@tonic-gate } optvect_t;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static optvect_t option_params[] = {
62*7c478bd9Sstevel@tonic-gate 	{ "noorphan", CT_PR_NOORPHAN },
63*7c478bd9Sstevel@tonic-gate 	{ "pgrponly", CT_PR_PGRPONLY },
64*7c478bd9Sstevel@tonic-gate 	{ "regent", CT_PR_REGENT },
65*7c478bd9Sstevel@tonic-gate 	{ "inherit", CT_PR_INHERIT },
66*7c478bd9Sstevel@tonic-gate 	{ NULL }
67*7c478bd9Sstevel@tonic-gate };
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static optvect_t option_events[] = {
70*7c478bd9Sstevel@tonic-gate 	{ "core", CT_PR_EV_CORE, OPT_NORMAL | OPT_FATAL },
71*7c478bd9Sstevel@tonic-gate 	{ "signal", CT_PR_EV_SIGNAL, OPT_NORMAL | OPT_FATAL },
72*7c478bd9Sstevel@tonic-gate 	{ "hwerr", CT_PR_EV_HWERR, OPT_NORMAL | OPT_FATAL },
73*7c478bd9Sstevel@tonic-gate 	{ "empty", CT_PR_EV_EMPTY, OPT_NORMAL },
74*7c478bd9Sstevel@tonic-gate 	{ "fork", CT_PR_EV_FORK, OPT_NORMAL },
75*7c478bd9Sstevel@tonic-gate 	{ "exit", CT_PR_EV_EXIT, OPT_NORMAL },
76*7c478bd9Sstevel@tonic-gate 	{ NULL }
77*7c478bd9Sstevel@tonic-gate };
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate typedef enum lifetime {
80*7c478bd9Sstevel@tonic-gate 	LT_NONE,
81*7c478bd9Sstevel@tonic-gate 	LT_CHILD,
82*7c478bd9Sstevel@tonic-gate 	LT_CONTRACT
83*7c478bd9Sstevel@tonic-gate } lifetime_t;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate  * Exit code to use when the child exited abnormally (i.e. exited with
87*7c478bd9Sstevel@tonic-gate  * a status we are unable to emulate).
88*7c478bd9Sstevel@tonic-gate  */
89*7c478bd9Sstevel@tonic-gate #define	EXIT_BADCHILD	123
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #define	USAGESTR	\
92*7c478bd9Sstevel@tonic-gate 	"Usage: %s [-i eventlist] [-f eventlist] [-l lifetime] \n" \
93*7c478bd9Sstevel@tonic-gate 	"\t[-o optionlist] [-r count [-t]] [-v] command\n"
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /*
96*7c478bd9Sstevel@tonic-gate  * usage
97*7c478bd9Sstevel@tonic-gate  *
98*7c478bd9Sstevel@tonic-gate  * Educate the user.
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate static void
101*7c478bd9Sstevel@tonic-gate usage(void)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(USAGESTR), uu_getpname());
104*7c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate /*
108*7c478bd9Sstevel@tonic-gate  * bit2str
109*7c478bd9Sstevel@tonic-gate  *
110*7c478bd9Sstevel@tonic-gate  * Convert a bit into its string representation.
111*7c478bd9Sstevel@tonic-gate  */
112*7c478bd9Sstevel@tonic-gate static const char *
113*7c478bd9Sstevel@tonic-gate bit2str(optvect_t *options, uint_t bit)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	for (; options->opt_name; options++)
116*7c478bd9Sstevel@tonic-gate 		if (options->opt_value == bit)
117*7c478bd9Sstevel@tonic-gate 			return (options->opt_name);
118*7c478bd9Sstevel@tonic-gate 	return (NULL);
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * str2bit
123*7c478bd9Sstevel@tonic-gate  *
124*7c478bd9Sstevel@tonic-gate  * Convert a string into its bit representation.  If match is set, only
125*7c478bd9Sstevel@tonic-gate  * look at those options with the match bit set in its opt_flags
126*7c478bd9Sstevel@tonic-gate  * field.
127*7c478bd9Sstevel@tonic-gate  */
128*7c478bd9Sstevel@tonic-gate static uint_t
129*7c478bd9Sstevel@tonic-gate str2bit(optvect_t *options, int match, const char *str, int len)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	for (; options->opt_name; options++) {
132*7c478bd9Sstevel@tonic-gate 		if (match && (options->opt_flags & match) == 0)
133*7c478bd9Sstevel@tonic-gate 			continue;
134*7c478bd9Sstevel@tonic-gate 		if (strncmp(str, options->opt_name, len) == 0)
135*7c478bd9Sstevel@tonic-gate 			return (options->opt_value);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	return (0);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate  * opt2bits
142*7c478bd9Sstevel@tonic-gate  *
143*7c478bd9Sstevel@tonic-gate  * Given a set of textual options separated by commas or spaces,
144*7c478bd9Sstevel@tonic-gate  * convert them to a set of bits.  Errors are fatal, except for empty
145*7c478bd9Sstevel@tonic-gate  * options (which are ignored) and duplicate options (which are
146*7c478bd9Sstevel@tonic-gate  * idempotent).
147*7c478bd9Sstevel@tonic-gate  */
148*7c478bd9Sstevel@tonic-gate static void
149*7c478bd9Sstevel@tonic-gate opt2bits(optvect_t *options, int match, const char *str, uint_t *bits, char c)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	const char *ptr, *next = str;
152*7c478bd9Sstevel@tonic-gate 	uint_t result = 0;
153*7c478bd9Sstevel@tonic-gate 	uint_t bit;
154*7c478bd9Sstevel@tonic-gate 	int none = 0;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	while (*str) {
157*7c478bd9Sstevel@tonic-gate 		int len;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 		ptr = strpbrk(str, ", ");
160*7c478bd9Sstevel@tonic-gate 		if (ptr != NULL) {
161*7c478bd9Sstevel@tonic-gate 			len = ptr - str;
162*7c478bd9Sstevel@tonic-gate 			next = ptr + 1;
163*7c478bd9Sstevel@tonic-gate 		} else {
164*7c478bd9Sstevel@tonic-gate 			len = strlen(str);
165*7c478bd9Sstevel@tonic-gate 			next = str + len;
166*7c478bd9Sstevel@tonic-gate 		}
167*7c478bd9Sstevel@tonic-gate 		if (len == 0) {
168*7c478bd9Sstevel@tonic-gate 			uu_warn(gettext("empty option\n"));
169*7c478bd9Sstevel@tonic-gate 			bit = 0;
170*7c478bd9Sstevel@tonic-gate 		} else {
171*7c478bd9Sstevel@tonic-gate 			bit = str2bit(options, match, str, len);
172*7c478bd9Sstevel@tonic-gate 			if (bit == 0 && strncmp(str, "none", len) == 0) {
173*7c478bd9Sstevel@tonic-gate 				none = 1;
174*7c478bd9Sstevel@tonic-gate 				if (result)
175*7c478bd9Sstevel@tonic-gate 					goto noneerr;
176*7c478bd9Sstevel@tonic-gate 			} else if (bit == 0) {
177*7c478bd9Sstevel@tonic-gate 				uu_warn(gettext("unrecognized option '%.*s'\n"),
178*7c478bd9Sstevel@tonic-gate 				    len, str);
179*7c478bd9Sstevel@tonic-gate 				uu_warn(gettext("error parsing '-%c' option\n"),
180*7c478bd9Sstevel@tonic-gate 				    c);
181*7c478bd9Sstevel@tonic-gate 				usage();
182*7c478bd9Sstevel@tonic-gate 			} else if (none) {
183*7c478bd9Sstevel@tonic-gate 				goto noneerr;
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 			if (result & bit)
186*7c478bd9Sstevel@tonic-gate 				uu_warn(gettext("option '%.*s' "
187*7c478bd9Sstevel@tonic-gate 				    "specified twice\n"), len, str);
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 		result |= bit;
190*7c478bd9Sstevel@tonic-gate 		str = next;
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	*bits = result;
194*7c478bd9Sstevel@tonic-gate 	return;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate noneerr:
197*7c478bd9Sstevel@tonic-gate 	uu_warn(gettext("option is incompatible with others: '%s'\n"), "none");
198*7c478bd9Sstevel@tonic-gate 	usage();
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate /*
202*7c478bd9Sstevel@tonic-gate  * close_on_exec
203*7c478bd9Sstevel@tonic-gate  *
204*7c478bd9Sstevel@tonic-gate  * Given a fd, marks it close-on-exec.
205*7c478bd9Sstevel@tonic-gate  */
206*7c478bd9Sstevel@tonic-gate static int
207*7c478bd9Sstevel@tonic-gate close_on_exec(int fd)
208*7c478bd9Sstevel@tonic-gate {
209*7c478bd9Sstevel@tonic-gate 	int flags = fcntl(fd, F_GETFD, 0);
210*7c478bd9Sstevel@tonic-gate 	if ((flags != -1) && (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1))
211*7c478bd9Sstevel@tonic-gate 		return (0);
212*7c478bd9Sstevel@tonic-gate 	return (-1);
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate /*
216*7c478bd9Sstevel@tonic-gate  * v_printf
217*7c478bd9Sstevel@tonic-gate  *
218*7c478bd9Sstevel@tonic-gate  * Output routine for messages printed only when -v is specified.
219*7c478bd9Sstevel@tonic-gate  */
220*7c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
221*7c478bd9Sstevel@tonic-gate static void
222*7c478bd9Sstevel@tonic-gate v_printf(const char *format, ...)
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	va_list va;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	if (opt_verbose) {
227*7c478bd9Sstevel@tonic-gate 		(void) printf("%s(%ld): ", uu_getpname(), getpid());
228*7c478bd9Sstevel@tonic-gate 		va_start(va, format);
229*7c478bd9Sstevel@tonic-gate 		(void) vprintf(format, va);
230*7c478bd9Sstevel@tonic-gate 		va_end(va);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate  * get_event
236*7c478bd9Sstevel@tonic-gate  *
237*7c478bd9Sstevel@tonic-gate  * Reads and acknowledges an event.  Returns the event type.
238*7c478bd9Sstevel@tonic-gate  */
239*7c478bd9Sstevel@tonic-gate static uint_t
240*7c478bd9Sstevel@tonic-gate get_event(int fd, int ctfd, ctid_t ctid)
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	ct_evthdl_t ev;
243*7c478bd9Sstevel@tonic-gate 	uint_t result;
244*7c478bd9Sstevel@tonic-gate 	ctevid_t evid;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	for (;;) {
247*7c478bd9Sstevel@tonic-gate 		int efd;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		/*
250*7c478bd9Sstevel@tonic-gate 		 * Normally we only need to look at critical messages.
251*7c478bd9Sstevel@tonic-gate 		 * If we are displaying contract events, however, we
252*7c478bd9Sstevel@tonic-gate 		 * have to read them all.
253*7c478bd9Sstevel@tonic-gate 		 */
254*7c478bd9Sstevel@tonic-gate 		errno = opt_verbose ? ct_event_read(fd, &ev) :
255*7c478bd9Sstevel@tonic-gate 		    ct_event_read_critical(fd, &ev);
256*7c478bd9Sstevel@tonic-gate 		if (errno != 0)
257*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("failed to listen to contract events"));
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 		/*
260*7c478bd9Sstevel@tonic-gate 		 * If requested, display the event.
261*7c478bd9Sstevel@tonic-gate 		 */
262*7c478bd9Sstevel@tonic-gate 		if (opt_verbose) {
263*7c478bd9Sstevel@tonic-gate 			v_printf(gettext("event from contract %ld: "),
264*7c478bd9Sstevel@tonic-gate 			    ct_event_get_ctid(ev));
265*7c478bd9Sstevel@tonic-gate 			contract_event_dump(stdout, ev, opt_Verbose);
266*7c478bd9Sstevel@tonic-gate 			if ((ct_event_get_flags(ev) & CTE_INFO) != 0) {
267*7c478bd9Sstevel@tonic-gate 				ct_event_free(ev);
268*7c478bd9Sstevel@tonic-gate 				continue;
269*7c478bd9Sstevel@tonic-gate 			}
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 		/*
273*7c478bd9Sstevel@tonic-gate 		 * We're done if this event is one of ours.
274*7c478bd9Sstevel@tonic-gate 		 */
275*7c478bd9Sstevel@tonic-gate 		evid = ct_event_get_evid(ev);
276*7c478bd9Sstevel@tonic-gate 		if (ct_event_get_ctid(ev) == ctid)
277*7c478bd9Sstevel@tonic-gate 			break;
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 		/*
280*7c478bd9Sstevel@tonic-gate 		 * ACK events from other contracts.
281*7c478bd9Sstevel@tonic-gate 		 * This shouldn't happen, but it could.
282*7c478bd9Sstevel@tonic-gate 		 */
283*7c478bd9Sstevel@tonic-gate 		efd = contract_open(ct_event_get_ctid(ev), "process", "ctl",
284*7c478bd9Sstevel@tonic-gate 		    O_WRONLY);
285*7c478bd9Sstevel@tonic-gate 		if (efd != -1) {
286*7c478bd9Sstevel@tonic-gate 			(void) ct_ctl_ack(efd, evid);
287*7c478bd9Sstevel@tonic-gate 			(void) close(efd);
288*7c478bd9Sstevel@tonic-gate 		}
289*7c478bd9Sstevel@tonic-gate 		ct_event_free(ev);
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	/*
293*7c478bd9Sstevel@tonic-gate 	 * Note that if we want to use ctrun as a simple restarter, we
294*7c478bd9Sstevel@tonic-gate 	 * need persistently keep track of fatal events so we can
295*7c478bd9Sstevel@tonic-gate 	 * properly handle the death of the contract.  Rather than keep
296*7c478bd9Sstevel@tonic-gate 	 * a file or somesuch lying around, it might make more sense to
297*7c478bd9Sstevel@tonic-gate 	 * leave the significant fatal event sitting in the queue so
298*7c478bd9Sstevel@tonic-gate 	 * that a restarted instance of ctrun can pick it up.  For now
299*7c478bd9Sstevel@tonic-gate 	 * we'll just ACK all events.
300*7c478bd9Sstevel@tonic-gate 	 */
301*7c478bd9Sstevel@tonic-gate 	(void) ct_ctl_ack(ctfd, evid);
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	result = ct_event_get_type(ev);
304*7c478bd9Sstevel@tonic-gate 	ct_event_free(ev);
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	return (result);
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate /*
310*7c478bd9Sstevel@tonic-gate  * abandon
311*7c478bd9Sstevel@tonic-gate  *
312*7c478bd9Sstevel@tonic-gate  * Given an fd for a contract's ctl file, abandon the contract and
313*7c478bd9Sstevel@tonic-gate  * close the file.
314*7c478bd9Sstevel@tonic-gate  */
315*7c478bd9Sstevel@tonic-gate static void
316*7c478bd9Sstevel@tonic-gate abandon(int ctfd)
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate 	if (ct_ctl_abandon(ctfd) == -1)
319*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("failed to abandon contract %d"), ctfd);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	(void) close(ctfd);
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate static int chldstat;
325*7c478bd9Sstevel@tonic-gate static int chldexited;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate /*
328*7c478bd9Sstevel@tonic-gate  * sigchld
329*7c478bd9Sstevel@tonic-gate  *
330*7c478bd9Sstevel@tonic-gate  * Our SIGCHLD handler.  Sets chldstat and chldexited so the
331*7c478bd9Sstevel@tonic-gate  * interrupted code knows what happened.
332*7c478bd9Sstevel@tonic-gate  */
333*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
334*7c478bd9Sstevel@tonic-gate static void
335*7c478bd9Sstevel@tonic-gate sigchld(int sig, struct siginfo *si, void *ucp)
336*7c478bd9Sstevel@tonic-gate {
337*7c478bd9Sstevel@tonic-gate 	int err = errno;
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	if (si->si_code == CLD_EXITED)
340*7c478bd9Sstevel@tonic-gate 		chldstat = si->si_status;
341*7c478bd9Sstevel@tonic-gate 	else
342*7c478bd9Sstevel@tonic-gate 		chldstat = EXIT_BADCHILD;
343*7c478bd9Sstevel@tonic-gate 	chldexited = 1;
344*7c478bd9Sstevel@tonic-gate 	while (waitpid(si->si_pid, NULL, 0) == -1 && errno == EINTR)
345*7c478bd9Sstevel@tonic-gate 		;
346*7c478bd9Sstevel@tonic-gate 	errno = err;
347*7c478bd9Sstevel@tonic-gate }
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate /*
350*7c478bd9Sstevel@tonic-gate  * dowait
351*7c478bd9Sstevel@tonic-gate  *
352*7c478bd9Sstevel@tonic-gate  * Waits for the specified child to exit.  Returns the exit code ctrun
353*7c478bd9Sstevel@tonic-gate  * should return.
354*7c478bd9Sstevel@tonic-gate  */
355*7c478bd9Sstevel@tonic-gate static int
356*7c478bd9Sstevel@tonic-gate dowait(int pid)
357*7c478bd9Sstevel@tonic-gate {
358*7c478bd9Sstevel@tonic-gate 	pid_t wpid;
359*7c478bd9Sstevel@tonic-gate 	int wstatus;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	do
362*7c478bd9Sstevel@tonic-gate 		wpid = waitpid(pid, &wstatus, 0);
363*7c478bd9Sstevel@tonic-gate 	while (wpid == -1 && errno == EINTR);
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	if (wpid == -1)
366*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("wait failed"));
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	if (WIFEXITED(wstatus))
369*7c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(wstatus));
370*7c478bd9Sstevel@tonic-gate 	else
371*7c478bd9Sstevel@tonic-gate 		return (EXIT_BADCHILD);
372*7c478bd9Sstevel@tonic-gate }
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate int
375*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
376*7c478bd9Sstevel@tonic-gate {
377*7c478bd9Sstevel@tonic-gate 	int	fd, efd;
378*7c478bd9Sstevel@tonic-gate 	pid_t	pid;
379*7c478bd9Sstevel@tonic-gate 	ctid_t	ctid = 0;
380*7c478bd9Sstevel@tonic-gate 	int	ctfd;
381*7c478bd9Sstevel@tonic-gate 	int	pipefds[2];
382*7c478bd9Sstevel@tonic-gate 	struct sigaction osact;
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	int	s;
385*7c478bd9Sstevel@tonic-gate 	ctid_t	opt_adopt = 0;
386*7c478bd9Sstevel@tonic-gate 	int	opt_transfer = 0;
387*7c478bd9Sstevel@tonic-gate 	int	opt_count = -1;
388*7c478bd9Sstevel@tonic-gate 	uint_t	opt_info = CT_PR_EV_CORE;
389*7c478bd9Sstevel@tonic-gate 	uint_t	opt_crit = 0;
390*7c478bd9Sstevel@tonic-gate 	uint_t	eff_fatal, opt_fatal = CT_PR_EV_HWERR;
391*7c478bd9Sstevel@tonic-gate 	uint_t	eff_param, opt_param = 0;
392*7c478bd9Sstevel@tonic-gate 	lifetime_t opt_life = LT_CONTRACT;
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
395*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
396*7c478bd9Sstevel@tonic-gate 	uu_alt_exit(UU_PROFILE_LAUNCHER);
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	while ((s = getopt(argc, argv, "a:l:o:i:c:f:r:tvV")) != EOF) {
401*7c478bd9Sstevel@tonic-gate 		switch (s) {
402*7c478bd9Sstevel@tonic-gate 		case 'a':
403*7c478bd9Sstevel@tonic-gate 			if (uu_strtoint(optarg, &opt_adopt, sizeof (opt_adopt),
404*7c478bd9Sstevel@tonic-gate 			    0, 0, INT32_MAX) == -1) {
405*7c478bd9Sstevel@tonic-gate 				uu_warn(gettext("invalid contract ID '%s'\n"),
406*7c478bd9Sstevel@tonic-gate 				    optarg);
407*7c478bd9Sstevel@tonic-gate 				usage();
408*7c478bd9Sstevel@tonic-gate 			}
409*7c478bd9Sstevel@tonic-gate 			break;
410*7c478bd9Sstevel@tonic-gate 		case 'v':
411*7c478bd9Sstevel@tonic-gate 			opt_verbose = 1;
412*7c478bd9Sstevel@tonic-gate 			break;
413*7c478bd9Sstevel@tonic-gate 		case 'V':
414*7c478bd9Sstevel@tonic-gate 			opt_Verbose = 1;
415*7c478bd9Sstevel@tonic-gate 			opt_verbose = 1;
416*7c478bd9Sstevel@tonic-gate 			break;
417*7c478bd9Sstevel@tonic-gate 		case 't':
418*7c478bd9Sstevel@tonic-gate 			opt_transfer = 1;
419*7c478bd9Sstevel@tonic-gate 			break;
420*7c478bd9Sstevel@tonic-gate 		case 'r':
421*7c478bd9Sstevel@tonic-gate 			if (uu_strtoint(optarg, &opt_count, sizeof (opt_adopt),
422*7c478bd9Sstevel@tonic-gate 			    0, 0, INT32_MAX) == -1) {
423*7c478bd9Sstevel@tonic-gate 				uu_warn(gettext("invalid count '%s'\n"),
424*7c478bd9Sstevel@tonic-gate 				    optarg);
425*7c478bd9Sstevel@tonic-gate 				usage();
426*7c478bd9Sstevel@tonic-gate 			}
427*7c478bd9Sstevel@tonic-gate 			break;
428*7c478bd9Sstevel@tonic-gate 		case 'l':
429*7c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "none") == 0) {
430*7c478bd9Sstevel@tonic-gate 				opt_life = LT_NONE;
431*7c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "child") == 0) {
432*7c478bd9Sstevel@tonic-gate 				opt_life = LT_CHILD;
433*7c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "contract") == 0) {
434*7c478bd9Sstevel@tonic-gate 				opt_life = LT_CONTRACT;
435*7c478bd9Sstevel@tonic-gate 			} else {
436*7c478bd9Sstevel@tonic-gate 				uu_warn(gettext("invalid lifetime '%s'\n"),
437*7c478bd9Sstevel@tonic-gate 				    optarg);
438*7c478bd9Sstevel@tonic-gate 				usage();
439*7c478bd9Sstevel@tonic-gate 			}
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 			break;
442*7c478bd9Sstevel@tonic-gate 		case 'o':
443*7c478bd9Sstevel@tonic-gate 			opt2bits(option_params, 0, optarg, &opt_param,
444*7c478bd9Sstevel@tonic-gate 			    optopt);
445*7c478bd9Sstevel@tonic-gate 			break;
446*7c478bd9Sstevel@tonic-gate 		case 'i':
447*7c478bd9Sstevel@tonic-gate 			opt2bits(option_events, OPT_NORMAL, optarg, &opt_info,
448*7c478bd9Sstevel@tonic-gate 			    optopt);
449*7c478bd9Sstevel@tonic-gate 			break;
450*7c478bd9Sstevel@tonic-gate 		case 'c':
451*7c478bd9Sstevel@tonic-gate 			opt2bits(option_events, OPT_NORMAL, optarg, &opt_crit,
452*7c478bd9Sstevel@tonic-gate 			    optopt);
453*7c478bd9Sstevel@tonic-gate 			break;
454*7c478bd9Sstevel@tonic-gate 		case 'f':
455*7c478bd9Sstevel@tonic-gate 			opt2bits(option_events, OPT_FATAL, optarg, &opt_fatal,
456*7c478bd9Sstevel@tonic-gate 			    optopt);
457*7c478bd9Sstevel@tonic-gate 			break;
458*7c478bd9Sstevel@tonic-gate 		default:
459*7c478bd9Sstevel@tonic-gate 			usage();
460*7c478bd9Sstevel@tonic-gate 		}
461*7c478bd9Sstevel@tonic-gate 	}
462*7c478bd9Sstevel@tonic-gate 	argc -= optind;
463*7c478bd9Sstevel@tonic-gate 	argv += optind;
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	/*
466*7c478bd9Sstevel@tonic-gate 	 * Basic argument sanity checks.
467*7c478bd9Sstevel@tonic-gate 	 */
468*7c478bd9Sstevel@tonic-gate 	if ((opt_life == LT_NONE) && (opt_param & CT_PR_NOORPHAN)) {
469*7c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot use option '%s' with lifetime '%s'\n"),
470*7c478bd9Sstevel@tonic-gate 		    bit2str(option_params, CT_PR_NOORPHAN), "none");
471*7c478bd9Sstevel@tonic-gate 		usage();
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	if ((opt_life != LT_CONTRACT) && (opt_count >= 0)) {
475*7c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot restart with lifetime '%s'\n"),
476*7c478bd9Sstevel@tonic-gate 		    opt_life == LT_NONE ? "none" : "child");
477*7c478bd9Sstevel@tonic-gate 		usage();
478*7c478bd9Sstevel@tonic-gate 	}
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	if ((opt_param & CT_PR_PGRPONLY) && (opt_count >= 0)) {
481*7c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot restart with option '%s'\n"),
482*7c478bd9Sstevel@tonic-gate 		    bit2str(option_params, CT_PR_PGRPONLY));
483*7c478bd9Sstevel@tonic-gate 		usage();
484*7c478bd9Sstevel@tonic-gate 	}
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	if (opt_transfer && (opt_count == -1)) {
487*7c478bd9Sstevel@tonic-gate 		uu_warn(gettext("cannot transfer when not restarting\n"));
488*7c478bd9Sstevel@tonic-gate 		usage();
489*7c478bd9Sstevel@tonic-gate 	}
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	if (argc <= 0)
492*7c478bd9Sstevel@tonic-gate 		usage();
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	/*
495*7c478bd9Sstevel@tonic-gate 	 * Create a process contract template and our process's process
496*7c478bd9Sstevel@tonic-gate 	 * contract bundle endpoint.  Mark them close-on-exec so we
497*7c478bd9Sstevel@tonic-gate 	 * don't have to worry about closing them in our child.
498*7c478bd9Sstevel@tonic-gate 	 */
499*7c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
500*7c478bd9Sstevel@tonic-gate 	if (fd == -1)
501*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("template open failed"));
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	efd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
504*7c478bd9Sstevel@tonic-gate 	if (efd == -1)
505*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("process bundle open failed"));
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	if (close_on_exec(fd) || close_on_exec(efd))
508*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("could not set FD_CLOEXEC"));
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	/*
511*7c478bd9Sstevel@tonic-gate 	 * Set the process contract's terms based on our arguments.
512*7c478bd9Sstevel@tonic-gate 	 */
513*7c478bd9Sstevel@tonic-gate 	if (errno = ct_pr_tmpl_set_param(fd, opt_param))
514*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("set param failed"));
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	if (errno = ct_tmpl_set_informative(fd, opt_info))
517*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("set notify failed"));
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	if (errno = ct_pr_tmpl_set_fatal(fd, opt_fatal))
520*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("set fatal failed"));
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	if (opt_param & CT_PR_PGRPONLY)
523*7c478bd9Sstevel@tonic-gate 		opt_crit = CT_PR_EV_EMPTY;
524*7c478bd9Sstevel@tonic-gate 	else
525*7c478bd9Sstevel@tonic-gate 		opt_crit |= opt_fatal | CT_PR_EV_EMPTY;
526*7c478bd9Sstevel@tonic-gate 	if (errno = ct_tmpl_set_critical(fd, opt_crit))
527*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("set critical failed"));
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	/*
530*7c478bd9Sstevel@tonic-gate 	 * Activate the template.
531*7c478bd9Sstevel@tonic-gate 	 */
532*7c478bd9Sstevel@tonic-gate 	if (errno = ct_tmpl_activate(fd))
533*7c478bd9Sstevel@tonic-gate 		uu_die(gettext("template activate failed"));
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate restart:
536*7c478bd9Sstevel@tonic-gate 	if (opt_adopt) {
537*7c478bd9Sstevel@tonic-gate 		/*
538*7c478bd9Sstevel@tonic-gate 		 * Adopt a specific contract.
539*7c478bd9Sstevel@tonic-gate 		 */
540*7c478bd9Sstevel@tonic-gate 		ct_stathdl_t st;
541*7c478bd9Sstevel@tonic-gate 		int stfd;
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 		if ((ctfd = contract_open(opt_adopt, "process", "ctl",
544*7c478bd9Sstevel@tonic-gate 		    O_WRONLY)) == -1)
545*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not open contract %ld"),
546*7c478bd9Sstevel@tonic-gate 			    opt_adopt);
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 		/*
549*7c478bd9Sstevel@tonic-gate 		 * Read the contract's terms so that we interpret its
550*7c478bd9Sstevel@tonic-gate 		 * events properly.
551*7c478bd9Sstevel@tonic-gate 		 */
552*7c478bd9Sstevel@tonic-gate 		if (((stfd = contract_open(opt_adopt, "process", "status",
553*7c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == -1) ||
554*7c478bd9Sstevel@tonic-gate 		    (errno = ct_status_read(stfd, CTD_FIXED, &st)) ||
555*7c478bd9Sstevel@tonic-gate 		    (errno = ct_pr_status_get_fatal(st, &eff_fatal)) ||
556*7c478bd9Sstevel@tonic-gate 		    (errno = ct_pr_status_get_param(st, &eff_param)))
557*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not stat contract %ld"),
558*7c478bd9Sstevel@tonic-gate 			    opt_adopt);
559*7c478bd9Sstevel@tonic-gate 		ct_status_free(st);
560*7c478bd9Sstevel@tonic-gate 		(void) close(stfd);
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 		if (errno = ct_ctl_adopt(ctfd))
563*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not adopt contract %ld"),
564*7c478bd9Sstevel@tonic-gate 			    opt_adopt);
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 		ctid = opt_adopt;
567*7c478bd9Sstevel@tonic-gate 		opt_adopt = 0;
568*7c478bd9Sstevel@tonic-gate 		v_printf(gettext("adopted contract id %ld\n"), ctid);
569*7c478bd9Sstevel@tonic-gate 	} else {
570*7c478bd9Sstevel@tonic-gate 		/*
571*7c478bd9Sstevel@tonic-gate 		 * Create a new process.
572*7c478bd9Sstevel@tonic-gate 		 */
573*7c478bd9Sstevel@tonic-gate 		if (opt_life == LT_CONTRACT) {
574*7c478bd9Sstevel@tonic-gate 			struct sigaction sact;
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate 			/*
577*7c478bd9Sstevel@tonic-gate 			 * Since we are going to be waiting for and
578*7c478bd9Sstevel@tonic-gate 			 * reacting to contract events, install a
579*7c478bd9Sstevel@tonic-gate 			 * signal handler so we capture the exit status
580*7c478bd9Sstevel@tonic-gate 			 * of our child.
581*7c478bd9Sstevel@tonic-gate 			 */
582*7c478bd9Sstevel@tonic-gate 			chldstat = UU_EXIT_OK;
583*7c478bd9Sstevel@tonic-gate 			chldexited = 0;
584*7c478bd9Sstevel@tonic-gate 			sact.sa_sigaction = sigchld;
585*7c478bd9Sstevel@tonic-gate 			sact.sa_flags = SA_SIGINFO | SA_RESTART |
586*7c478bd9Sstevel@tonic-gate 			    SA_NOCLDSTOP;
587*7c478bd9Sstevel@tonic-gate 			(void) sigemptyset(&sact.sa_mask);
588*7c478bd9Sstevel@tonic-gate 			if (sigaction(SIGCHLD, &sact, &osact) == -1)
589*7c478bd9Sstevel@tonic-gate 				uu_die(gettext("failed to install "
590*7c478bd9Sstevel@tonic-gate 				    "sigchld handler"));
591*7c478bd9Sstevel@tonic-gate 		} else if (opt_life == LT_NONE) {
592*7c478bd9Sstevel@tonic-gate 			/*
593*7c478bd9Sstevel@tonic-gate 			 * Though we aren't waiting for our child to
594*7c478bd9Sstevel@tonic-gate 			 * exit, as a well-behaved command launcher we
595*7c478bd9Sstevel@tonic-gate 			 * must wait for it to exec.  On success the
596*7c478bd9Sstevel@tonic-gate 			 * pipe will simply close, and on failure the
597*7c478bd9Sstevel@tonic-gate 			 * proper exit status will be sent.
598*7c478bd9Sstevel@tonic-gate 			 */
599*7c478bd9Sstevel@tonic-gate 			if (pipe(pipefds) == -1 ||
600*7c478bd9Sstevel@tonic-gate 			    close_on_exec(pipefds[0]) == -1 ||
601*7c478bd9Sstevel@tonic-gate 			    close_on_exec(pipefds[1]) == -1)
602*7c478bd9Sstevel@tonic-gate 				uu_die(gettext("failed to create pipe"));
603*7c478bd9Sstevel@tonic-gate 		}
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == -1) {
606*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("fork failed"));
607*7c478bd9Sstevel@tonic-gate 		} else if (pid == 0) {
608*7c478bd9Sstevel@tonic-gate 			int result = execvp(argv[0], argv);
609*7c478bd9Sstevel@tonic-gate 			if (opt_life == LT_NONE) {
610*7c478bd9Sstevel@tonic-gate 				char a = 1;
611*7c478bd9Sstevel@tonic-gate 				int err = errno;
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate 				(void) write(pipefds[1], &a, sizeof (a));
614*7c478bd9Sstevel@tonic-gate 				errno = err;
615*7c478bd9Sstevel@tonic-gate 			}
616*7c478bd9Sstevel@tonic-gate 			if (result == -1)
617*7c478bd9Sstevel@tonic-gate 				uu_xdie(errno == ENOENT ? 127 : 126,
618*7c478bd9Sstevel@tonic-gate 				    gettext("exec failed"));
619*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("exec returned!\n"));
620*7c478bd9Sstevel@tonic-gate 		}
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 		/*
623*7c478bd9Sstevel@tonic-gate 		 * Get the newly-created contract's id and ctl fd.
624*7c478bd9Sstevel@tonic-gate 		 */
625*7c478bd9Sstevel@tonic-gate 		if (errno = contract_latest(&ctid))
626*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not get new contract's id"));
627*7c478bd9Sstevel@tonic-gate 		if ((ctfd = contract_open(ctid, "process", "ctl",
628*7c478bd9Sstevel@tonic-gate 		    O_WRONLY)) == -1)
629*7c478bd9Sstevel@tonic-gate 			uu_die(gettext("could not open contract"));
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 		/*
632*7c478bd9Sstevel@tonic-gate 		 * Clear the transfer parameter so that the contract
633*7c478bd9Sstevel@tonic-gate 		 * will be freed sooner and admins won't get nervous.
634*7c478bd9Sstevel@tonic-gate 		 */
635*7c478bd9Sstevel@tonic-gate 		if (opt_transfer) {
636*7c478bd9Sstevel@tonic-gate 			(void) ct_pr_tmpl_set_transfer(fd, 0);
637*7c478bd9Sstevel@tonic-gate 			(void) ct_tmpl_activate(fd);
638*7c478bd9Sstevel@tonic-gate 		}
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 		v_printf(gettext("created contract id %ld\n"), ctid);
641*7c478bd9Sstevel@tonic-gate 		eff_param = opt_param;
642*7c478bd9Sstevel@tonic-gate 		eff_fatal = opt_fatal;
643*7c478bd9Sstevel@tonic-gate 	}
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 	if (opt_life == LT_CONTRACT) {
646*7c478bd9Sstevel@tonic-gate 		uint_t event, errevent = 0;
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 		/*
649*7c478bd9Sstevel@tonic-gate 		 * Wait until the contract empties out.
650*7c478bd9Sstevel@tonic-gate 		 */
651*7c478bd9Sstevel@tonic-gate 		do {
652*7c478bd9Sstevel@tonic-gate 			event = get_event(efd, ctfd, ctid);
653*7c478bd9Sstevel@tonic-gate 			if (event & eff_fatal) {
654*7c478bd9Sstevel@tonic-gate 				if ((eff_param & CT_PR_PGRPONLY) == 0)
655*7c478bd9Sstevel@tonic-gate 					errevent = event;
656*7c478bd9Sstevel@tonic-gate 				v_printf(gettext(
657*7c478bd9Sstevel@tonic-gate 				    "fatal \"%s\" event from contract %ld\n"),
658*7c478bd9Sstevel@tonic-gate 				    bit2str(option_events, event), ctid);
659*7c478bd9Sstevel@tonic-gate 			}
660*7c478bd9Sstevel@tonic-gate 		} while ((event & CT_PR_EV_EMPTY) == 0);
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 		/*
663*7c478bd9Sstevel@tonic-gate 		 * If we encountered a fatal error event, and we
664*7c478bd9Sstevel@tonic-gate 		 * haven't expended our maximum loop count, restart.
665*7c478bd9Sstevel@tonic-gate 		 */
666*7c478bd9Sstevel@tonic-gate 		if ((errevent != 0) &&
667*7c478bd9Sstevel@tonic-gate 		    ((opt_count == 0) || (opt_count-- > 1))) {
668*7c478bd9Sstevel@tonic-gate 			v_printf(gettext("failure in contract %ld, "
669*7c478bd9Sstevel@tonic-gate 			    "restarting command\n"), ctid);
670*7c478bd9Sstevel@tonic-gate 			if (opt_transfer) {
671*7c478bd9Sstevel@tonic-gate 				/*
672*7c478bd9Sstevel@tonic-gate 				 * Add the failed contract to the new
673*7c478bd9Sstevel@tonic-gate 				 * contract's terms so that its
674*7c478bd9Sstevel@tonic-gate 				 * inherited subcontracts can be
675*7c478bd9Sstevel@tonic-gate 				 * adopted by the new process.
676*7c478bd9Sstevel@tonic-gate 				 */
677*7c478bd9Sstevel@tonic-gate 				if (errno = ct_pr_tmpl_set_transfer(fd, ctid))
678*7c478bd9Sstevel@tonic-gate 					uu_die(gettext("set transfer failed"));
679*7c478bd9Sstevel@tonic-gate 				if (errno = ct_tmpl_activate(fd))
680*7c478bd9Sstevel@tonic-gate 					uu_die(gettext(
681*7c478bd9Sstevel@tonic-gate 					    "template activate failed"));
682*7c478bd9Sstevel@tonic-gate 				(void) close(ctfd);
683*7c478bd9Sstevel@tonic-gate 			} else {
684*7c478bd9Sstevel@tonic-gate 				abandon(ctfd);
685*7c478bd9Sstevel@tonic-gate 			}
686*7c478bd9Sstevel@tonic-gate 			goto restart;
687*7c478bd9Sstevel@tonic-gate 		}
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 		/*
690*7c478bd9Sstevel@tonic-gate 		 * At this point we are done with the contract; we
691*7c478bd9Sstevel@tonic-gate 		 * don't want it to be inherited when we exit.
692*7c478bd9Sstevel@tonic-gate 		 */
693*7c478bd9Sstevel@tonic-gate 		abandon(ctfd);
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 		/*
696*7c478bd9Sstevel@tonic-gate 		 * In case there was a race between SIGCHLD delivery
697*7c478bd9Sstevel@tonic-gate 		 * and contract event delivery, disable the signal
698*7c478bd9Sstevel@tonic-gate 		 * handler and look for the child.
699*7c478bd9Sstevel@tonic-gate 		 */
700*7c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGCHLD, &osact, NULL);
701*7c478bd9Sstevel@tonic-gate 		if (chldexited == 0)
702*7c478bd9Sstevel@tonic-gate 			chldstat = dowait(pid);
703*7c478bd9Sstevel@tonic-gate 	} else if (opt_life == LT_NONE) {
704*7c478bd9Sstevel@tonic-gate 		char a;
705*7c478bd9Sstevel@tonic-gate 		int result;
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 		chldstat = UU_EXIT_OK;
708*7c478bd9Sstevel@tonic-gate 		(void) close(pipefds[1]);
709*7c478bd9Sstevel@tonic-gate 		do {
710*7c478bd9Sstevel@tonic-gate 			result = read(pipefds[0], &a, sizeof (a));
711*7c478bd9Sstevel@tonic-gate 			if (result == -1 && errno != EINTR)
712*7c478bd9Sstevel@tonic-gate 				uu_die(gettext("read failed"));
713*7c478bd9Sstevel@tonic-gate 			if (result == 1)
714*7c478bd9Sstevel@tonic-gate 				chldstat = dowait(pid);
715*7c478bd9Sstevel@tonic-gate 		} while (result == -1);
716*7c478bd9Sstevel@tonic-gate 	} else {
717*7c478bd9Sstevel@tonic-gate 		chldstat = dowait(pid);
718*7c478bd9Sstevel@tonic-gate 	}
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	return (chldstat);
721*7c478bd9Sstevel@tonic-gate }
722