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