xref: /onnv-gate/usr/src/cmd/svc/startd/fork.c (revision 11996:91b62f7b8186)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54740Sjeanm  * Common Development and Distribution License (the "License").
64740Sjeanm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11996SThomas.Whitten@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * fork.c - safe forking for svc.startd
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * fork_configd() and fork_sulogin() are related, special cases that handle the
300Sstevel@tonic-gate  * spawning of specific client processes for svc.startd.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/contract/process.h>
340Sstevel@tonic-gate #include <sys/corectl.h>
350Sstevel@tonic-gate #include <sys/ctfs.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/uio.h>
390Sstevel@tonic-gate #include <sys/wait.h>
400Sstevel@tonic-gate #include <assert.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <fcntl.h>
430Sstevel@tonic-gate #include <libcontract.h>
440Sstevel@tonic-gate #include <libcontract_priv.h>
456073Sacruz #include <libscf_priv.h>
460Sstevel@tonic-gate #include <limits.h>
478944Sdp@eng.sun.com #include <poll.h>
480Sstevel@tonic-gate #include <port.h>
490Sstevel@tonic-gate #include <signal.h>
500Sstevel@tonic-gate #include <stdarg.h>
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <string.h>
540Sstevel@tonic-gate #include <unistd.h>
554740Sjeanm #include <utmpx.h>
568944Sdp@eng.sun.com #include <spawn.h>
570Sstevel@tonic-gate 
58*11996SThomas.Whitten@Sun.COM #include "manifest_hash.h"
590Sstevel@tonic-gate #include "configd_exit.h"
600Sstevel@tonic-gate #include "protocol.h"
610Sstevel@tonic-gate #include "startd.h"
620Sstevel@tonic-gate 
634740Sjeanm static	struct	utmpx	*utmpp;	/* pointer for getutxent() */
644740Sjeanm 
650Sstevel@tonic-gate pid_t
startd_fork1(int * forkerr)660Sstevel@tonic-gate startd_fork1(int *forkerr)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	pid_t p;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/*
710Sstevel@tonic-gate 	 * prefork stack
720Sstevel@tonic-gate 	 */
730Sstevel@tonic-gate 	wait_prefork();
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	p = fork1();
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (p == -1 && forkerr != NULL)
780Sstevel@tonic-gate 		*forkerr = errno;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * postfork stack
820Sstevel@tonic-gate 	 */
830Sstevel@tonic-gate 	wait_postfork(p);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	return (p);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * void fork_mount(char *, char *)
900Sstevel@tonic-gate  *   Run mount(1M) with the given options and mount point.  (mount(1M) has much
910Sstevel@tonic-gate  *   hidden knowledge; it's much less correct to reimplement that logic here to
920Sstevel@tonic-gate  *   save a fork(2)/exec(2) invocation.)
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate int
fork_mount(char * path,char * opts)950Sstevel@tonic-gate fork_mount(char *path, char *opts)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	pid_t pid;
980Sstevel@tonic-gate 	uint_t tries = 0;
990Sstevel@tonic-gate 	int status;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	for (pid = fork1(); pid == -1; pid = fork1()) {
1020Sstevel@tonic-gate 		if (++tries > MAX_MOUNT_RETRIES)
1030Sstevel@tonic-gate 			return (-1);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		(void) sleep(tries);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (pid != 0) {
1090Sstevel@tonic-gate 		(void) waitpid(pid, &status, 0);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		/*
1120Sstevel@tonic-gate 		 * If our mount(1M) invocation exited by peculiar means, or with
1130Sstevel@tonic-gate 		 * a non-zero status, our mount likelihood is low.
1140Sstevel@tonic-gate 		 */
1150Sstevel@tonic-gate 		if (!WIFEXITED(status) ||
1160Sstevel@tonic-gate 		    WEXITSTATUS(status) != 0)
1170Sstevel@tonic-gate 			return (-1);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		return (0);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	(void) execl("/sbin/mount", "mount", "-o", opts, path, NULL);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	return (-1);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * pid_t fork_common(...)
129*11996SThomas.Whitten@Sun.COM  *   Common routine used by fork_sulogin, fork_emi, and fork_configd to
130*11996SThomas.Whitten@Sun.COM  *   fork a process in a contract with the provided terms.  Invokes
1310Sstevel@tonic-gate  *   fork_sulogin (with its no-fork argument set) on errors.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate static pid_t
fork_common(const char * name,const char * svc_fmri,int retries,ctid_t * ctidp,uint_t inf,uint_t crit,uint_t fatal,uint_t param,uint64_t cookie)1346073Sacruz fork_common(const char *name, const char *svc_fmri, int retries, ctid_t *ctidp,
1350Sstevel@tonic-gate     uint_t inf, uint_t crit, uint_t fatal, uint_t param, uint64_t cookie)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	uint_t tries = 0;
1380Sstevel@tonic-gate 	int ctfd, err;
1390Sstevel@tonic-gate 	pid_t pid;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	/*
1420Sstevel@tonic-gate 	 * Establish process contract terms.
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	if ((ctfd = open64(CTFS_ROOT "/process/template", O_RDWR)) == -1) {
1450Sstevel@tonic-gate 		fork_sulogin(B_TRUE, "Could not open process contract template "
1460Sstevel@tonic-gate 		    "for %s: %s\n", name, strerror(errno));
1470Sstevel@tonic-gate 		/* NOTREACHED */
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	err = ct_tmpl_set_critical(ctfd, crit);
1510Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(ctfd, fatal);
1520Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(ctfd, inf);
1530Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(ctfd, param);
1540Sstevel@tonic-gate 	err |= ct_tmpl_set_cookie(ctfd, cookie);
1556073Sacruz 	err |= ct_pr_tmpl_set_svc_fmri(ctfd, svc_fmri);
1566073Sacruz 	err |= ct_pr_tmpl_set_svc_aux(ctfd, name);
1570Sstevel@tonic-gate 	if (err) {
1580Sstevel@tonic-gate 		(void) close(ctfd);
1590Sstevel@tonic-gate 		fork_sulogin(B_TRUE, "Could not set %s process contract "
1600Sstevel@tonic-gate 		    "terms\n", name);
1610Sstevel@tonic-gate 		/* NOTREACHED */
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (err = ct_tmpl_activate(ctfd)) {
1650Sstevel@tonic-gate 		(void) close(ctfd);
1660Sstevel@tonic-gate 		fork_sulogin(B_TRUE, "Could not activate %s process contract "
1670Sstevel@tonic-gate 		    "template: %s\n", name, strerror(err));
1680Sstevel@tonic-gate 		/* NOTREACHED */
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/*
1720Sstevel@tonic-gate 	 * Attempt to fork "retries" times.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	for (pid = fork1(); pid == -1; pid = fork1()) {
1750Sstevel@tonic-gate 		if (++tries > retries) {
1760Sstevel@tonic-gate 			/*
1770Sstevel@tonic-gate 			 * When we exit the sulogin session, init(1M)
1780Sstevel@tonic-gate 			 * will restart svc.startd(1M).
1790Sstevel@tonic-gate 			 */
1800Sstevel@tonic-gate 			err = errno;
1810Sstevel@tonic-gate 			(void) ct_tmpl_clear(ctfd);
1820Sstevel@tonic-gate 			(void) close(ctfd);
1830Sstevel@tonic-gate 			fork_sulogin(B_TRUE, "Could not fork to start %s: %s\n",
1840Sstevel@tonic-gate 			    name, strerror(err));
1850Sstevel@tonic-gate 			/* NOTREACHED */
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		(void) sleep(tries);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * Clean up, return pid and ctid.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	if (pid != 0 && (errno = contract_latest(ctidp)) != 0)
1940Sstevel@tonic-gate 		uu_die("Could not get new contract id for %s\n", name);
1950Sstevel@tonic-gate 	(void) ct_tmpl_clear(ctfd);
1960Sstevel@tonic-gate 	(void) close(ctfd);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	return (pid);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * void fork_sulogin(boolean_t, const char *, ...)
2030Sstevel@tonic-gate  *   When we are invoked with the -s flag from boot (or run into an unfixable
2040Sstevel@tonic-gate  *   situation), we run a private copy of sulogin.  When the sulogin session
2050Sstevel@tonic-gate  *   is ended, we continue.  This is the last fallback action for system
2060Sstevel@tonic-gate  *   maintenance.
2070Sstevel@tonic-gate  *
2080Sstevel@tonic-gate  *   If immediate is true, fork_sulogin() executes sulogin(1M) directly, without
2090Sstevel@tonic-gate  *   forking.
2100Sstevel@tonic-gate  *
2110Sstevel@tonic-gate  *   Because fork_sulogin() is needed potentially before we daemonize, we leave
2120Sstevel@tonic-gate  *   it outside the wait_register() framework.
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate /*PRINTFLIKE2*/
2150Sstevel@tonic-gate void
fork_sulogin(boolean_t immediate,const char * format,...)2160Sstevel@tonic-gate fork_sulogin(boolean_t immediate, const char *format, ...)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	va_list args;
2195617Sacruz 	int fd_console;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	(void) printf("Requesting System Maintenance Mode\n");
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (!booting_to_single_user)
2240Sstevel@tonic-gate 		(void) printf("(See /lib/svc/share/README for more "
2250Sstevel@tonic-gate 		    "information.)\n");
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	va_start(args, format);
2280Sstevel@tonic-gate 	(void) vprintf(format, args);
2290Sstevel@tonic-gate 	va_end(args);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (!immediate) {
2320Sstevel@tonic-gate 		ctid_t	ctid;
2330Sstevel@tonic-gate 		pid_t	pid;
2340Sstevel@tonic-gate 
2356073Sacruz 		pid = fork_common("sulogin", SVC_SULOGIN_FMRI,
2366073Sacruz 		    MAX_SULOGIN_RETRIES, &ctid, CT_PR_EV_HWERR, 0,
2376073Sacruz 		    CT_PR_EV_HWERR, CT_PR_PGRPONLY, SULOGIN_COOKIE);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		if (pid != 0) {
2400Sstevel@tonic-gate 			(void) waitpid(pid, NULL, 0);
2410Sstevel@tonic-gate 			contract_abandon(ctid);
2420Sstevel@tonic-gate 			return;
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		/* close all inherited fds */
2450Sstevel@tonic-gate 		closefrom(0);
2460Sstevel@tonic-gate 	} else {
2470Sstevel@tonic-gate 		(void) printf("Directly executing sulogin.\n");
2480Sstevel@tonic-gate 		/*
2490Sstevel@tonic-gate 		 * Can't call closefrom() in this MT section
2500Sstevel@tonic-gate 		 * so safely close a minimum set of fds.
2510Sstevel@tonic-gate 		 */
2525617Sacruz 		(void) close(STDIN_FILENO);
2535617Sacruz 		(void) close(STDOUT_FILENO);
2545617Sacruz 		(void) close(STDERR_FILENO);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	(void) setpgrp();
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/* open the console for sulogin */
2600Sstevel@tonic-gate 	if ((fd_console = open("/dev/console", O_RDWR)) >= 0) {
2610Sstevel@tonic-gate 		if (fd_console != STDIN_FILENO)
2620Sstevel@tonic-gate 			while (dup2(fd_console, STDIN_FILENO) < 0 &&
2630Sstevel@tonic-gate 			    errno == EINTR)
2640Sstevel@tonic-gate 				;
2650Sstevel@tonic-gate 		if (fd_console != STDOUT_FILENO)
2660Sstevel@tonic-gate 			while (dup2(fd_console, STDOUT_FILENO) < 0 &&
2670Sstevel@tonic-gate 			    errno == EINTR)
2680Sstevel@tonic-gate 				;
2690Sstevel@tonic-gate 		if (fd_console != STDERR_FILENO)
2700Sstevel@tonic-gate 			while (dup2(fd_console, STDERR_FILENO) < 0 &&
2710Sstevel@tonic-gate 			    errno == EINTR)
2720Sstevel@tonic-gate 				;
2735617Sacruz 		if (fd_console > STDERR_FILENO)
2740Sstevel@tonic-gate 			(void) close(fd_console);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2774740Sjeanm 	setutxent();
2784740Sjeanm 	while ((utmpp = getutxent()) != NULL) {
2794740Sjeanm 		if (strcmp(utmpp->ut_user, "LOGIN") != 0) {
2804740Sjeanm 			if (strcmp(utmpp->ut_line, "console") == 0) {
2814740Sjeanm 				(void) kill(utmpp->ut_pid, 9);
2824740Sjeanm 				break;
2834740Sjeanm 			}
2844740Sjeanm 		}
2854740Sjeanm 	}
2864740Sjeanm 
2870Sstevel@tonic-gate 	(void) execl("/sbin/sulogin", "sulogin", NULL);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	uu_warn("Could not exec() sulogin");
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	exit(1);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate #define	CONFIGD_PATH	"/lib/svc/bin/svc.configd"
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * void fork_configd(int status)
2980Sstevel@tonic-gate  *   We are interested in exit events (since the parent's exiting means configd
2990Sstevel@tonic-gate  *   is ready to run and since the child's exiting indicates an error case) and
3000Sstevel@tonic-gate  *   in empty events.  This means we have a unique template for initiating
3010Sstevel@tonic-gate  *   configd.
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate void
fork_configd(int exitstatus)3040Sstevel@tonic-gate fork_configd(int exitstatus)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	pid_t pid;
3070Sstevel@tonic-gate 	ctid_t ctid = -1;
3080Sstevel@tonic-gate 	int err;
3090Sstevel@tonic-gate 	char path[PATH_MAX];
3100Sstevel@tonic-gate 
3118095SSean.Wilcox@Sun.COM 	/*
3128095SSean.Wilcox@Sun.COM 	 * Checking the existatus for the potential failure of the
3138095SSean.Wilcox@Sun.COM 	 * daemonized svc.configd.  If this is not the first time
3148095SSean.Wilcox@Sun.COM 	 * through, but a call from the svc.configd monitoring thread
3158095SSean.Wilcox@Sun.COM 	 * after a failure this is the status that is expected.  Other
3168095SSean.Wilcox@Sun.COM 	 * failures are exposed during initialization or are fixed
3178095SSean.Wilcox@Sun.COM 	 * by a restart (e.g door closings).
3188095SSean.Wilcox@Sun.COM 	 *
3198095SSean.Wilcox@Sun.COM 	 * If this is on-disk database corruption it will also be
3208095SSean.Wilcox@Sun.COM 	 * caught by a restart but could be cleared before the restart.
3218095SSean.Wilcox@Sun.COM 	 *
3228095SSean.Wilcox@Sun.COM 	 * Or this could be internal database corruption due to a
3238095SSean.Wilcox@Sun.COM 	 * rogue service that needs to be cleared before restart.
3248095SSean.Wilcox@Sun.COM 	 */
3258095SSean.Wilcox@Sun.COM 	if (WEXITSTATUS(exitstatus) == CONFIGD_EXIT_DATABASE_BAD) {
3268095SSean.Wilcox@Sun.COM 		fork_sulogin(B_FALSE, "svc.configd exited with database "
3278095SSean.Wilcox@Sun.COM 		    "corrupt error after initialization of the repository\n");
3288095SSean.Wilcox@Sun.COM 	}
3298095SSean.Wilcox@Sun.COM 
3300Sstevel@tonic-gate retry:
3310Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "fork_configd trying to start svc.configd\n");
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * If we're retrying, we will have an old contract lying around
3350Sstevel@tonic-gate 	 * from the failure.  Since we're going to be creating a new
3360Sstevel@tonic-gate 	 * contract shortly, we abandon the old one now.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	if (ctid != -1)
3390Sstevel@tonic-gate 		contract_abandon(ctid);
3400Sstevel@tonic-gate 	ctid = -1;
3410Sstevel@tonic-gate 
3426073Sacruz 	pid = fork_common("svc.configd", SCF_SERVICE_CONFIGD,
3436073Sacruz 	    MAX_CONFIGD_RETRIES, &ctid, 0, CT_PR_EV_EXIT, 0,
3446073Sacruz 	    CT_PR_INHERIT | CT_PR_REGENT, CONFIGD_COOKIE);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (pid != 0) {
3470Sstevel@tonic-gate 		int exitstatus;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		st->st_configd_pid = pid;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		if (waitpid(pid, &exitstatus, 0) == -1) {
3520Sstevel@tonic-gate 			fork_sulogin(B_FALSE, "waitpid on svc.configd "
3530Sstevel@tonic-gate 			    "failed: %s\n", strerror(errno));
3540Sstevel@tonic-gate 		} else if (WIFEXITED(exitstatus)) {
3550Sstevel@tonic-gate 			char *errstr;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			/*
3580Sstevel@tonic-gate 			 * Examine exitstatus.  This will eventually get more
3590Sstevel@tonic-gate 			 * complicated, as we will want to teach startd how to
3600Sstevel@tonic-gate 			 * invoke configd with alternate repositories, etc.
3610Sstevel@tonic-gate 			 *
3620Sstevel@tonic-gate 			 * Note that exec(2) failure results in an exit status
3630Sstevel@tonic-gate 			 * of 1, resulting in the default clause below.
3640Sstevel@tonic-gate 			 */
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 			/*
3670Sstevel@tonic-gate 			 * Assign readable strings to cases we don't handle, or
3680Sstevel@tonic-gate 			 * have error outcomes that cannot be eliminated.
3690Sstevel@tonic-gate 			 */
3700Sstevel@tonic-gate 			switch (WEXITSTATUS(exitstatus)) {
3710Sstevel@tonic-gate 			case CONFIGD_EXIT_BAD_ARGS:
3720Sstevel@tonic-gate 				errstr = "bad arguments";
3730Sstevel@tonic-gate 				break;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_BAD:
3760Sstevel@tonic-gate 				errstr = "database corrupt";
3770Sstevel@tonic-gate 				break;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_LOCKED:
3800Sstevel@tonic-gate 				errstr = "database locked";
3810Sstevel@tonic-gate 				break;
3820Sstevel@tonic-gate 			case CONFIGD_EXIT_INIT_FAILED:
3830Sstevel@tonic-gate 				errstr = "initialization failure";
3840Sstevel@tonic-gate 				break;
3850Sstevel@tonic-gate 			case CONFIGD_EXIT_DOOR_INIT_FAILED:
3860Sstevel@tonic-gate 				errstr = "door initialization failure";
3870Sstevel@tonic-gate 				break;
3880Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_INIT_FAILED:
3890Sstevel@tonic-gate 				errstr = "database initialization failure";
3900Sstevel@tonic-gate 				break;
3910Sstevel@tonic-gate 			case CONFIGD_EXIT_NO_THREADS:
3920Sstevel@tonic-gate 				errstr = "no threads available";
3930Sstevel@tonic-gate 				break;
3940Sstevel@tonic-gate 			case CONFIGD_EXIT_LOST_MAIN_DOOR:
3950Sstevel@tonic-gate 				errstr = "lost door server attachment";
3960Sstevel@tonic-gate 				break;
3970Sstevel@tonic-gate 			case 1:
3980Sstevel@tonic-gate 				errstr = "execution failure";
3990Sstevel@tonic-gate 				break;
4000Sstevel@tonic-gate 			default:
4010Sstevel@tonic-gate 				errstr = "unknown error";
4020Sstevel@tonic-gate 				break;
4030Sstevel@tonic-gate 			}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 			/*
4060Sstevel@tonic-gate 			 * Remedial actions for various configd failures.
4070Sstevel@tonic-gate 			 */
4080Sstevel@tonic-gate 			switch (WEXITSTATUS(exitstatus)) {
4090Sstevel@tonic-gate 			case CONFIGD_EXIT_OKAY:
4100Sstevel@tonic-gate 				break;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_LOCKED:
4130Sstevel@tonic-gate 				/* attempt remount of / read-write */
4140Sstevel@tonic-gate 				if (fs_is_read_only("/", NULL) == 1) {
4150Sstevel@tonic-gate 					if (fs_remount("/") == -1)
4160Sstevel@tonic-gate 						fork_sulogin(B_FALSE,
4170Sstevel@tonic-gate 						    "remount of root "
4180Sstevel@tonic-gate 						    "filesystem failed\n");
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 					goto retry;
4210Sstevel@tonic-gate 				}
4220Sstevel@tonic-gate 				break;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 			default:
4250Sstevel@tonic-gate 				fork_sulogin(B_FALSE, "svc.configd exited "
4260Sstevel@tonic-gate 				    "with status %d (%s)\n",
4270Sstevel@tonic-gate 				    WEXITSTATUS(exitstatus), errstr);
4280Sstevel@tonic-gate 				goto retry;
4290Sstevel@tonic-gate 			}
4300Sstevel@tonic-gate 		} else if (WIFSIGNALED(exitstatus)) {
4310Sstevel@tonic-gate 			char signame[SIG2STR_MAX];
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 			if (sig2str(WTERMSIG(exitstatus), signame))
4340Sstevel@tonic-gate 				(void) snprintf(signame, SIG2STR_MAX,
4350Sstevel@tonic-gate 				    "signum %d", WTERMSIG(exitstatus));
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 			fork_sulogin(B_FALSE, "svc.configd signalled:"
4380Sstevel@tonic-gate 			    " %s\n", signame);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 			goto retry;
4410Sstevel@tonic-gate 		} else {
4420Sstevel@tonic-gate 			fork_sulogin(B_FALSE, "svc.configd non-exit "
4430Sstevel@tonic-gate 			    "condition: 0x%x\n", exitstatus);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 			goto retry;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 		/*
4490Sstevel@tonic-gate 		 * Announce that we have a valid svc.configd status.
4500Sstevel@tonic-gate 		 */
4510Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_configd_live_lock);
4520Sstevel@tonic-gate 		st->st_configd_lives = 1;
4530Sstevel@tonic-gate 		err = pthread_cond_broadcast(&st->st_configd_live_cv);
4540Sstevel@tonic-gate 		assert(err == 0);
4550Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_configd_live_lock);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "fork_configd broadcasts configd is "
4580Sstevel@tonic-gate 		    "live\n");
4590Sstevel@tonic-gate 		return;
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	/*
4630Sstevel@tonic-gate 	 * Set our per-process core file path to leave core files in
4640Sstevel@tonic-gate 	 * /etc/svc/volatile directory, named after the PID to aid in debugging.
4650Sstevel@tonic-gate 	 */
4660Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path),
4670Sstevel@tonic-gate 	    "/etc/svc/volatile/core.configd.%%p");
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	(void) core_set_process_path(path, strlen(path) + 1, getpid());
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "executing svc.configd\n");
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	(void) execl(CONFIGD_PATH, CONFIGD_PATH, NULL);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	/*
4760Sstevel@tonic-gate 	 * Status code is used above to identify configd exec failure.
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 	exit(1);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate void *
fork_configd_thread(void * vctid)4820Sstevel@tonic-gate fork_configd_thread(void *vctid)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	int fd, err;
4850Sstevel@tonic-gate 	ctid_t configd_ctid = (ctid_t)vctid;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (configd_ctid == -1) {
4880Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
4890Sstevel@tonic-gate 		    "fork_configd_thread starting svc.configd\n");
4900Sstevel@tonic-gate 		fork_configd(0);
4910Sstevel@tonic-gate 	} else {
4920Sstevel@tonic-gate 		/*
4930Sstevel@tonic-gate 		 * configd_ctid is known:  we broadcast and continue.
4940Sstevel@tonic-gate 		 * test contract for appropriate state by verifying that
4950Sstevel@tonic-gate 		 * there is one or more processes within it?
4960Sstevel@tonic-gate 		 */
4970Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
4980Sstevel@tonic-gate 		    "fork_configd_thread accepting svc.configd with CTID %ld\n",
4990Sstevel@tonic-gate 		    configd_ctid);
5000Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_configd_live_lock);
5010Sstevel@tonic-gate 		st->st_configd_lives = 1;
5020Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&st->st_configd_live_cv);
5030Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_configd_live_lock);
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
5070Sstevel@tonic-gate 	if (fd == -1)
5080Sstevel@tonic-gate 		uu_die("process bundle open failed");
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	/*
5110Sstevel@tonic-gate 	 * Make sure we get all events (including those generated by configd
5120Sstevel@tonic-gate 	 * before this thread was started).
5130Sstevel@tonic-gate 	 */
5140Sstevel@tonic-gate 	err = ct_event_reset(fd);
5150Sstevel@tonic-gate 	assert(err == 0);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	for (;;) {
5180Sstevel@tonic-gate 		int efd, sfd;
5190Sstevel@tonic-gate 		ct_evthdl_t ev;
5200Sstevel@tonic-gate 		uint32_t type;
5210Sstevel@tonic-gate 		ctevid_t evid;
5220Sstevel@tonic-gate 		ct_stathdl_t status;
5230Sstevel@tonic-gate 		ctid_t ctid;
5240Sstevel@tonic-gate 		uint64_t cookie;
5250Sstevel@tonic-gate 		pid_t pid;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 		if (err = ct_event_read_critical(fd, &ev)) {
5280Sstevel@tonic-gate 			assert(err != EINVAL && err != EAGAIN);
5290Sstevel@tonic-gate 			log_error(LOG_WARNING,
5300Sstevel@tonic-gate 			    "Error reading next contract event: %s",
5310Sstevel@tonic-gate 			    strerror(err));
5320Sstevel@tonic-gate 			continue;
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		evid = ct_event_get_evid(ev);
5360Sstevel@tonic-gate 		ctid = ct_event_get_ctid(ev);
5370Sstevel@tonic-gate 		type = ct_event_get_type(ev);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		/* Fetch cookie. */
5400Sstevel@tonic-gate 		sfd = contract_open(ctid, "process", "status", O_RDONLY);
5410Sstevel@tonic-gate 		if (sfd < 0) {
5420Sstevel@tonic-gate 			ct_event_free(ev);
5430Sstevel@tonic-gate 			continue;
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 		if (err = ct_status_read(sfd, CTD_COMMON, &status)) {
5470Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Could not get status for "
5480Sstevel@tonic-gate 			    "contract %ld: %s\n", ctid, strerror(err));
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 			ct_event_free(ev);
5510Sstevel@tonic-gate 			startd_close(sfd);
5520Sstevel@tonic-gate 			continue;
5530Sstevel@tonic-gate 		}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 		cookie = ct_status_get_cookie(status);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 		ct_status_free(status);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		startd_close(sfd);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		/*
5620Sstevel@tonic-gate 		 * Don't process events from contracts we aren't interested in.
5630Sstevel@tonic-gate 		 */
5640Sstevel@tonic-gate 		if (cookie != CONFIGD_COOKIE) {
5650Sstevel@tonic-gate 			ct_event_free(ev);
5660Sstevel@tonic-gate 			continue;
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (type == CT_PR_EV_EXIT) {
5700Sstevel@tonic-gate 			int exitstatus;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 			(void) ct_pr_event_get_pid(ev, &pid);
5730Sstevel@tonic-gate 			(void) ct_pr_event_get_exitstatus(ev,
5740Sstevel@tonic-gate 			    &exitstatus);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 			if (st->st_configd_pid != pid) {
5770Sstevel@tonic-gate 				/*
5780Sstevel@tonic-gate 				 * This is the child exiting, so we
5790Sstevel@tonic-gate 				 * abandon the contract and restart
5800Sstevel@tonic-gate 				 * configd.
5810Sstevel@tonic-gate 				 */
5820Sstevel@tonic-gate 				contract_abandon(ctid);
5830Sstevel@tonic-gate 				fork_configd(exitstatus);
5840Sstevel@tonic-gate 			}
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		efd = contract_open(ctid, "process", "ctl", O_WRONLY);
5880Sstevel@tonic-gate 		if (efd != -1) {
5890Sstevel@tonic-gate 			(void) ct_ctl_ack(efd, evid);
5900Sstevel@tonic-gate 			startd_close(efd);
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		ct_event_free(ev);
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*NOTREACHED*/
5980Sstevel@tonic-gate 	return (NULL);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate void
fork_rc_script(char rl,const char * arg,boolean_t wait)6020Sstevel@tonic-gate fork_rc_script(char rl, const char *arg, boolean_t wait)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	pid_t pid;
6050Sstevel@tonic-gate 	int tmpl, err, stat;
6060Sstevel@tonic-gate 	char path[20] = "/sbin/rc.", log[20] = "rc..log", timebuf[20];
6070Sstevel@tonic-gate 	time_t now;
6080Sstevel@tonic-gate 	struct tm ltime;
6090Sstevel@tonic-gate 	size_t sz;
6100Sstevel@tonic-gate 	char *pathenv;
6110Sstevel@tonic-gate 	char **nenv;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	path[8] = rl;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	tmpl = open64(CTFS_ROOT "/process/template", O_RDWR);
6160Sstevel@tonic-gate 	if (tmpl >= 0) {
6170Sstevel@tonic-gate 		err = ct_tmpl_set_critical(tmpl, 0);
6180Sstevel@tonic-gate 		assert(err == 0);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		err = ct_tmpl_set_informative(tmpl, 0);
6210Sstevel@tonic-gate 		assert(err == 0);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		err = ct_pr_tmpl_set_fatal(tmpl, 0);
6240Sstevel@tonic-gate 		assert(err == 0);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		err = ct_tmpl_activate(tmpl);
6270Sstevel@tonic-gate 		assert(err == 0);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		err = close(tmpl);
6300Sstevel@tonic-gate 		assert(err == 0);
6310Sstevel@tonic-gate 	} else {
6320Sstevel@tonic-gate 		uu_warn("Could not create contract template for %s.\n", path);
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	pid = startd_fork1(NULL);
6360Sstevel@tonic-gate 	if (pid < 0) {
6370Sstevel@tonic-gate 		return;
6380Sstevel@tonic-gate 	} else if (pid != 0) {
6390Sstevel@tonic-gate 		/* parent */
6400Sstevel@tonic-gate 		if (wait) {
6410Sstevel@tonic-gate 			do
6420Sstevel@tonic-gate 				err = waitpid(pid, &stat, 0);
6434740Sjeanm 			while (err != 0 && errno == EINTR)
6444740Sjeanm 				;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 			if (!WIFEXITED(stat)) {
6470Sstevel@tonic-gate 				log_framework(LOG_INFO,
6480Sstevel@tonic-gate 				    "%s terminated with waitpid() status %d.\n",
6490Sstevel@tonic-gate 				    path, stat);
6500Sstevel@tonic-gate 			} else if (WEXITSTATUS(stat) != 0) {
6510Sstevel@tonic-gate 				log_framework(LOG_INFO,
6520Sstevel@tonic-gate 				    "%s failed with status %d.\n", path,
6530Sstevel@tonic-gate 				    WEXITSTATUS(stat));
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		return;
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/* child */
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	log[2] = rl;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	setlog(log);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	now = time(NULL);
6670Sstevel@tonic-gate 	sz = strftime(timebuf, sizeof (timebuf), "%b %e %T",
6680Sstevel@tonic-gate 	    localtime_r(&now, &ltime));
6690Sstevel@tonic-gate 	assert(sz != 0);
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	(void) fprintf(stderr, "%s Executing %s %s\n", timebuf, path, arg);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (rl == 'S')
6740Sstevel@tonic-gate 		pathenv = "PATH=/sbin:/usr/sbin:/usr/bin";
6750Sstevel@tonic-gate 	else
6760Sstevel@tonic-gate 		pathenv = "PATH=/usr/sbin:/usr/bin";
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	nenv = set_smf_env(NULL, 0, pathenv, NULL, NULL);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	(void) execle(path, path, arg, 0, nenv);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	perror("exec");
6830Sstevel@tonic-gate 	exit(0);
6840Sstevel@tonic-gate }
6858944Sdp@eng.sun.com 
686*11996SThomas.Whitten@Sun.COM #define	SVCCFG_PATH	"/usr/sbin/svccfg"
687*11996SThomas.Whitten@Sun.COM #define	EMI_MFST	"/lib/svc/manifest/system/early-manifest-import.xml"
688*11996SThomas.Whitten@Sun.COM #define	EMI_PATH	"/lib/svc/method/manifest-import"
689*11996SThomas.Whitten@Sun.COM 
690*11996SThomas.Whitten@Sun.COM /*
691*11996SThomas.Whitten@Sun.COM  * Set Early Manifest Import service's state and log file.
692*11996SThomas.Whitten@Sun.COM  */
693*11996SThomas.Whitten@Sun.COM static int
emi_set_state(restarter_instance_state_t state,boolean_t setlog)694*11996SThomas.Whitten@Sun.COM emi_set_state(restarter_instance_state_t state, boolean_t setlog)
695*11996SThomas.Whitten@Sun.COM {
696*11996SThomas.Whitten@Sun.COM 	int r, ret = 1;
697*11996SThomas.Whitten@Sun.COM 	instance_data_t idata;
698*11996SThomas.Whitten@Sun.COM 	scf_handle_t *hndl = NULL;
699*11996SThomas.Whitten@Sun.COM 	scf_instance_t *inst = NULL;
700*11996SThomas.Whitten@Sun.COM 
701*11996SThomas.Whitten@Sun.COM retry:
702*11996SThomas.Whitten@Sun.COM 	if (hndl == NULL)
703*11996SThomas.Whitten@Sun.COM 		hndl = libscf_handle_create_bound(SCF_VERSION);
704*11996SThomas.Whitten@Sun.COM 
705*11996SThomas.Whitten@Sun.COM 	if (hndl == NULL) {
706*11996SThomas.Whitten@Sun.COM 		/*
707*11996SThomas.Whitten@Sun.COM 		 * In the case that we can't bind to the repository
708*11996SThomas.Whitten@Sun.COM 		 * (which should have been started), we need to allow
709*11996SThomas.Whitten@Sun.COM 		 * the user into maintenance mode to determine what's
710*11996SThomas.Whitten@Sun.COM 		 * failed.
711*11996SThomas.Whitten@Sun.COM 		 */
712*11996SThomas.Whitten@Sun.COM 		fork_sulogin(B_FALSE, "Unable to bind a new repository"
713*11996SThomas.Whitten@Sun.COM 		    " handle: %s\n", scf_strerror(scf_error()));
714*11996SThomas.Whitten@Sun.COM 		goto retry;
715*11996SThomas.Whitten@Sun.COM 	}
716*11996SThomas.Whitten@Sun.COM 
717*11996SThomas.Whitten@Sun.COM 	if (inst == NULL)
718*11996SThomas.Whitten@Sun.COM 		inst = safe_scf_instance_create(hndl);
719*11996SThomas.Whitten@Sun.COM 
720*11996SThomas.Whitten@Sun.COM 	if (scf_handle_decode_fmri(hndl, SCF_INSTANCE_EMI, NULL, NULL,
721*11996SThomas.Whitten@Sun.COM 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
722*11996SThomas.Whitten@Sun.COM 		switch (scf_error()) {
723*11996SThomas.Whitten@Sun.COM 		case SCF_ERROR_NOT_FOUND:
724*11996SThomas.Whitten@Sun.COM 			goto out;
725*11996SThomas.Whitten@Sun.COM 
726*11996SThomas.Whitten@Sun.COM 		case SCF_ERROR_CONNECTION_BROKEN:
727*11996SThomas.Whitten@Sun.COM 		case SCF_ERROR_NOT_BOUND:
728*11996SThomas.Whitten@Sun.COM 			libscf_handle_rebind(hndl);
729*11996SThomas.Whitten@Sun.COM 			goto retry;
730*11996SThomas.Whitten@Sun.COM 
731*11996SThomas.Whitten@Sun.COM 		default:
732*11996SThomas.Whitten@Sun.COM 			fork_sulogin(B_FALSE, "Couldn't fetch %s service: "
733*11996SThomas.Whitten@Sun.COM 			    "%s\n", SCF_INSTANCE_EMI,
734*11996SThomas.Whitten@Sun.COM 			    scf_strerror(scf_error()));
735*11996SThomas.Whitten@Sun.COM 			goto retry;
736*11996SThomas.Whitten@Sun.COM 		}
737*11996SThomas.Whitten@Sun.COM 	}
738*11996SThomas.Whitten@Sun.COM 
739*11996SThomas.Whitten@Sun.COM 	if (setlog) {
740*11996SThomas.Whitten@Sun.COM 		(void) libscf_note_method_log(inst, st->st_log_prefix, EMI_LOG);
741*11996SThomas.Whitten@Sun.COM 		log_framework(LOG_DEBUG,
742*11996SThomas.Whitten@Sun.COM 		    "Set logfile property for %s\n", SCF_INSTANCE_EMI);
743*11996SThomas.Whitten@Sun.COM 	}
744*11996SThomas.Whitten@Sun.COM 
745*11996SThomas.Whitten@Sun.COM 	idata.i_fmri = SCF_INSTANCE_EMI;
746*11996SThomas.Whitten@Sun.COM 	idata.i_state =  RESTARTER_STATE_NONE;
747*11996SThomas.Whitten@Sun.COM 	idata.i_next_state = RESTARTER_STATE_NONE;
748*11996SThomas.Whitten@Sun.COM 	switch (r = _restarter_commit_states(hndl, &idata, state,
749*11996SThomas.Whitten@Sun.COM 	    RESTARTER_STATE_NONE, NULL)) {
750*11996SThomas.Whitten@Sun.COM 	case 0:
751*11996SThomas.Whitten@Sun.COM 		break;
752*11996SThomas.Whitten@Sun.COM 
753*11996SThomas.Whitten@Sun.COM 	case ECONNABORTED:
754*11996SThomas.Whitten@Sun.COM 		libscf_handle_rebind(hndl);
755*11996SThomas.Whitten@Sun.COM 		goto retry;
756*11996SThomas.Whitten@Sun.COM 
757*11996SThomas.Whitten@Sun.COM 	case ENOMEM:
758*11996SThomas.Whitten@Sun.COM 	case ENOENT:
759*11996SThomas.Whitten@Sun.COM 	case EPERM:
760*11996SThomas.Whitten@Sun.COM 	case EACCES:
761*11996SThomas.Whitten@Sun.COM 	case EROFS:
762*11996SThomas.Whitten@Sun.COM 		fork_sulogin(B_FALSE, "Could not set state of "
763*11996SThomas.Whitten@Sun.COM 		    "%s: %s\n", SCF_INSTANCE_EMI, strerror(r));
764*11996SThomas.Whitten@Sun.COM 		goto retry;
765*11996SThomas.Whitten@Sun.COM 		break;
766*11996SThomas.Whitten@Sun.COM 
767*11996SThomas.Whitten@Sun.COM 	case EINVAL:
768*11996SThomas.Whitten@Sun.COM 	default:
769*11996SThomas.Whitten@Sun.COM 		bad_error("_restarter_commit_states", r);
770*11996SThomas.Whitten@Sun.COM 	}
771*11996SThomas.Whitten@Sun.COM 	ret = 0;
772*11996SThomas.Whitten@Sun.COM 
773*11996SThomas.Whitten@Sun.COM out:
774*11996SThomas.Whitten@Sun.COM 	scf_instance_destroy(inst);
775*11996SThomas.Whitten@Sun.COM 	scf_handle_destroy(hndl);
776*11996SThomas.Whitten@Sun.COM 	return (ret);
777*11996SThomas.Whitten@Sun.COM }
778*11996SThomas.Whitten@Sun.COM 
779*11996SThomas.Whitten@Sun.COM /*
780*11996SThomas.Whitten@Sun.COM  * It is possible that the early-manifest-import service is disabled.  This
781*11996SThomas.Whitten@Sun.COM  * would not be the normal case for Solaris, but it may happen on dedicated
782*11996SThomas.Whitten@Sun.COM  * systems.  So this function checks the state of the general/enabled
783*11996SThomas.Whitten@Sun.COM  * property for Early Manifest Import.
784*11996SThomas.Whitten@Sun.COM  *
785*11996SThomas.Whitten@Sun.COM  * It is also possible that the early-manifest-import service does not yet
786*11996SThomas.Whitten@Sun.COM  * have a repository representation when this function runs.  This happens
787*11996SThomas.Whitten@Sun.COM  * if non-Early Manifest Import system is upgraded to an Early Manifest
788*11996SThomas.Whitten@Sun.COM  * Import based system.  Thus, the non-existence of general/enabled is not
789*11996SThomas.Whitten@Sun.COM  * an error.
790*11996SThomas.Whitten@Sun.COM  *
791*11996SThomas.Whitten@Sun.COM  * Returns 1 if Early Manifest Import is disabled and 0 otherwise.
792*11996SThomas.Whitten@Sun.COM  */
793*11996SThomas.Whitten@Sun.COM static int
emi_is_disabled()794*11996SThomas.Whitten@Sun.COM emi_is_disabled()
795*11996SThomas.Whitten@Sun.COM {
796*11996SThomas.Whitten@Sun.COM 	int disabled = 0;
797*11996SThomas.Whitten@Sun.COM 	int disconnected = 1;
798*11996SThomas.Whitten@Sun.COM 	int enabled;
799*11996SThomas.Whitten@Sun.COM 	scf_handle_t *hndl = NULL;
800*11996SThomas.Whitten@Sun.COM 	scf_instance_t *inst = NULL;
801*11996SThomas.Whitten@Sun.COM 	uchar_t stored_hash[MHASH_SIZE];
802*11996SThomas.Whitten@Sun.COM 	char *pname;
803*11996SThomas.Whitten@Sun.COM 	int hashash, r;
804*11996SThomas.Whitten@Sun.COM 
805*11996SThomas.Whitten@Sun.COM 	while (hndl == NULL) {
806*11996SThomas.Whitten@Sun.COM 		hndl = libscf_handle_create_bound(SCF_VERSION);
807*11996SThomas.Whitten@Sun.COM 
808*11996SThomas.Whitten@Sun.COM 		if (hndl == NULL) {
809*11996SThomas.Whitten@Sun.COM 			/*
810*11996SThomas.Whitten@Sun.COM 			 * In the case that we can't bind to the repository
811*11996SThomas.Whitten@Sun.COM 			 * (which should have been started), we need to
812*11996SThomas.Whitten@Sun.COM 			 * allow the user into maintenance mode to
813*11996SThomas.Whitten@Sun.COM 			 * determine what's failed.
814*11996SThomas.Whitten@Sun.COM 			 */
815*11996SThomas.Whitten@Sun.COM 			fork_sulogin(B_FALSE, "Unable to bind a new repository "
816*11996SThomas.Whitten@Sun.COM 			    "handle: %s\n", scf_strerror(scf_error()));
817*11996SThomas.Whitten@Sun.COM 		}
818*11996SThomas.Whitten@Sun.COM 	}
819*11996SThomas.Whitten@Sun.COM 
820*11996SThomas.Whitten@Sun.COM 	while (disconnected) {
821*11996SThomas.Whitten@Sun.COM 		r = libscf_fmri_get_instance(hndl, SCF_INSTANCE_EMI, &inst);
822*11996SThomas.Whitten@Sun.COM 		if (r != 0) {
823*11996SThomas.Whitten@Sun.COM 			switch (r) {
824*11996SThomas.Whitten@Sun.COM 			case ECONNABORTED:
825*11996SThomas.Whitten@Sun.COM 				libscf_handle_rebind(hndl);
826*11996SThomas.Whitten@Sun.COM 				continue;
827*11996SThomas.Whitten@Sun.COM 
828*11996SThomas.Whitten@Sun.COM 			case ENOENT:
829*11996SThomas.Whitten@Sun.COM 				/*
830*11996SThomas.Whitten@Sun.COM 				 * Early Manifest Import service is not in
831*11996SThomas.Whitten@Sun.COM 				 * the repository. Check the manifest file
832*11996SThomas.Whitten@Sun.COM 				 * and service's hash in smf/manifest to
833*11996SThomas.Whitten@Sun.COM 				 * figure out whether Early Manifest Import
834*11996SThomas.Whitten@Sun.COM 				 * service was deleted. If Early Manifest Import
835*11996SThomas.Whitten@Sun.COM 				 * service was deleted, treat that as a disable
836*11996SThomas.Whitten@Sun.COM 				 * and don't run early import.
837*11996SThomas.Whitten@Sun.COM 				 */
838*11996SThomas.Whitten@Sun.COM 
839*11996SThomas.Whitten@Sun.COM 				if (access(EMI_MFST, F_OK)) {
840*11996SThomas.Whitten@Sun.COM 					/*
841*11996SThomas.Whitten@Sun.COM 					 * Manifest isn't found, so service is
842*11996SThomas.Whitten@Sun.COM 					 * properly removed.
843*11996SThomas.Whitten@Sun.COM 					 */
844*11996SThomas.Whitten@Sun.COM 					disabled = 1;
845*11996SThomas.Whitten@Sun.COM 				} else {
846*11996SThomas.Whitten@Sun.COM 					/*
847*11996SThomas.Whitten@Sun.COM 					 * If manifest exists and we have the
848*11996SThomas.Whitten@Sun.COM 					 * hash, the service was improperly
849*11996SThomas.Whitten@Sun.COM 					 * deleted, generate a warning and treat
850*11996SThomas.Whitten@Sun.COM 					 * this as a disable.
851*11996SThomas.Whitten@Sun.COM 					 */
852*11996SThomas.Whitten@Sun.COM 
853*11996SThomas.Whitten@Sun.COM 					if ((pname = mhash_filename_to_propname(
854*11996SThomas.Whitten@Sun.COM 					    EMI_MFST, B_TRUE)) == NULL) {
855*11996SThomas.Whitten@Sun.COM 						/*
856*11996SThomas.Whitten@Sun.COM 						 * Treat failure to get propname
857*11996SThomas.Whitten@Sun.COM 						 * as a disable.
858*11996SThomas.Whitten@Sun.COM 						 */
859*11996SThomas.Whitten@Sun.COM 						disabled = 1;
860*11996SThomas.Whitten@Sun.COM 						uu_warn("Failed to get propname"
861*11996SThomas.Whitten@Sun.COM 						    " for %s.\n",
862*11996SThomas.Whitten@Sun.COM 						    SCF_INSTANCE_EMI);
863*11996SThomas.Whitten@Sun.COM 					} else {
864*11996SThomas.Whitten@Sun.COM 						hashash = mhash_retrieve_entry(
865*11996SThomas.Whitten@Sun.COM 						    hndl, pname,
866*11996SThomas.Whitten@Sun.COM 						    stored_hash,
867*11996SThomas.Whitten@Sun.COM 						    NULL) == 0;
868*11996SThomas.Whitten@Sun.COM 						uu_free(pname);
869*11996SThomas.Whitten@Sun.COM 
870*11996SThomas.Whitten@Sun.COM 						if (hashash) {
871*11996SThomas.Whitten@Sun.COM 							disabled = 1;
872*11996SThomas.Whitten@Sun.COM 							uu_warn("%s service is "
873*11996SThomas.Whitten@Sun.COM 							    "deleted \n",
874*11996SThomas.Whitten@Sun.COM 							    SCF_INSTANCE_EMI);
875*11996SThomas.Whitten@Sun.COM 						}
876*11996SThomas.Whitten@Sun.COM 					}
877*11996SThomas.Whitten@Sun.COM 
878*11996SThomas.Whitten@Sun.COM 				}
879*11996SThomas.Whitten@Sun.COM 
880*11996SThomas.Whitten@Sun.COM 				disconnected = 0;
881*11996SThomas.Whitten@Sun.COM 				continue;
882*11996SThomas.Whitten@Sun.COM 
883*11996SThomas.Whitten@Sun.COM 			default:
884*11996SThomas.Whitten@Sun.COM 				bad_error("libscf_fmri_get_instance",
885*11996SThomas.Whitten@Sun.COM 				    scf_error());
886*11996SThomas.Whitten@Sun.COM 			}
887*11996SThomas.Whitten@Sun.COM 		}
888*11996SThomas.Whitten@Sun.COM 		r = libscf_get_basic_instance_data(hndl, inst, SCF_INSTANCE_EMI,
889*11996SThomas.Whitten@Sun.COM 		    &enabled, NULL, NULL);
890*11996SThomas.Whitten@Sun.COM 		if (r == 0) {
891*11996SThomas.Whitten@Sun.COM 			/*
892*11996SThomas.Whitten@Sun.COM 			 * enabled can be returned as -1, which indicates
893*11996SThomas.Whitten@Sun.COM 			 * that the enabled property was not found.  To us
894*11996SThomas.Whitten@Sun.COM 			 * that means that the service was not disabled.
895*11996SThomas.Whitten@Sun.COM 			 */
896*11996SThomas.Whitten@Sun.COM 			if (enabled == 0)
897*11996SThomas.Whitten@Sun.COM 				disabled = 1;
898*11996SThomas.Whitten@Sun.COM 		} else {
899*11996SThomas.Whitten@Sun.COM 			switch (r) {
900*11996SThomas.Whitten@Sun.COM 			case ECONNABORTED:
901*11996SThomas.Whitten@Sun.COM 				libscf_handle_rebind(hndl);
902*11996SThomas.Whitten@Sun.COM 				continue;
903*11996SThomas.Whitten@Sun.COM 
904*11996SThomas.Whitten@Sun.COM 			case ECANCELED:
905*11996SThomas.Whitten@Sun.COM 			case ENOENT:
906*11996SThomas.Whitten@Sun.COM 				break;
907*11996SThomas.Whitten@Sun.COM 			default:
908*11996SThomas.Whitten@Sun.COM 				bad_error("libscf_get_basic_instance_data", r);
909*11996SThomas.Whitten@Sun.COM 			}
910*11996SThomas.Whitten@Sun.COM 		}
911*11996SThomas.Whitten@Sun.COM 		disconnected = 0;
912*11996SThomas.Whitten@Sun.COM 	}
913*11996SThomas.Whitten@Sun.COM 
914*11996SThomas.Whitten@Sun.COM out:
915*11996SThomas.Whitten@Sun.COM 	if (inst != NULL)
916*11996SThomas.Whitten@Sun.COM 		scf_instance_destroy(inst);
917*11996SThomas.Whitten@Sun.COM 	scf_handle_destroy(hndl);
918*11996SThomas.Whitten@Sun.COM 	return (disabled);
919*11996SThomas.Whitten@Sun.COM }
920*11996SThomas.Whitten@Sun.COM 
921*11996SThomas.Whitten@Sun.COM void
fork_emi()922*11996SThomas.Whitten@Sun.COM fork_emi()
923*11996SThomas.Whitten@Sun.COM {
924*11996SThomas.Whitten@Sun.COM 	pid_t pid;
925*11996SThomas.Whitten@Sun.COM 	ctid_t ctid = -1;
926*11996SThomas.Whitten@Sun.COM 	char **envp, **np;
927*11996SThomas.Whitten@Sun.COM 	char *emipath;
928*11996SThomas.Whitten@Sun.COM 	char corepath[PATH_MAX];
929*11996SThomas.Whitten@Sun.COM 	char *svc_state;
930*11996SThomas.Whitten@Sun.COM 	int setemilog;
931*11996SThomas.Whitten@Sun.COM 	int sz;
932*11996SThomas.Whitten@Sun.COM 
933*11996SThomas.Whitten@Sun.COM 	if (emi_is_disabled()) {
934*11996SThomas.Whitten@Sun.COM 		log_framework(LOG_NOTICE, "%s is  disabled and will "
935*11996SThomas.Whitten@Sun.COM 		    "not be run.\n", SCF_INSTANCE_EMI);
936*11996SThomas.Whitten@Sun.COM 		return;
937*11996SThomas.Whitten@Sun.COM 	}
938*11996SThomas.Whitten@Sun.COM 
939*11996SThomas.Whitten@Sun.COM 	/*
940*11996SThomas.Whitten@Sun.COM 	 * Early Manifest Import should run only once, at boot. If svc.startd
941*11996SThomas.Whitten@Sun.COM 	 * is some how restarted, Early Manifest Import  should not run again.
942*11996SThomas.Whitten@Sun.COM 	 * Use the Early Manifest Import service's state to figure out whether
943*11996SThomas.Whitten@Sun.COM 	 * Early Manifest Import has successfully completed earlier and bail
944*11996SThomas.Whitten@Sun.COM 	 * out if it did.
945*11996SThomas.Whitten@Sun.COM 	 */
946*11996SThomas.Whitten@Sun.COM 	if (svc_state = smf_get_state(SCF_INSTANCE_EMI)) {
947*11996SThomas.Whitten@Sun.COM 		if (strcmp(svc_state, SCF_STATE_STRING_ONLINE) == 0) {
948*11996SThomas.Whitten@Sun.COM 			free(svc_state);
949*11996SThomas.Whitten@Sun.COM 			return;
950*11996SThomas.Whitten@Sun.COM 		}
951*11996SThomas.Whitten@Sun.COM 		free(svc_state);
952*11996SThomas.Whitten@Sun.COM 	}
953*11996SThomas.Whitten@Sun.COM 
954*11996SThomas.Whitten@Sun.COM 	/*
955*11996SThomas.Whitten@Sun.COM 	 * Attempt to set Early Manifest Import service's state and log file.
956*11996SThomas.Whitten@Sun.COM 	 * If emi_set_state fails, set log file again in the next call to
957*11996SThomas.Whitten@Sun.COM 	 * emi_set_state.
958*11996SThomas.Whitten@Sun.COM 	 */
959*11996SThomas.Whitten@Sun.COM 	setemilog = emi_set_state(RESTARTER_STATE_OFFLINE, B_TRUE);
960*11996SThomas.Whitten@Sun.COM 
961*11996SThomas.Whitten@Sun.COM 	/* Don't go further if /usr isn't available */
962*11996SThomas.Whitten@Sun.COM 	if (access(SVCCFG_PATH, F_OK)) {
963*11996SThomas.Whitten@Sun.COM 		log_framework(LOG_NOTICE, "Early Manifest Import is not "
964*11996SThomas.Whitten@Sun.COM 		    "supported on systems with a separate /usr filesystem.\n");
965*11996SThomas.Whitten@Sun.COM 		return;
966*11996SThomas.Whitten@Sun.COM 	}
967*11996SThomas.Whitten@Sun.COM 
968*11996SThomas.Whitten@Sun.COM fork_retry:
969*11996SThomas.Whitten@Sun.COM 	log_framework(LOG_DEBUG, "Starting Early Manifest Import\n");
970*11996SThomas.Whitten@Sun.COM 
971*11996SThomas.Whitten@Sun.COM 	/*
972*11996SThomas.Whitten@Sun.COM 	 * If we're retrying, we will have an old contract lying around
973*11996SThomas.Whitten@Sun.COM 	 * from the failure.  Since we're going to be creating a new
974*11996SThomas.Whitten@Sun.COM 	 * contract shortly, we abandon the old one now.
975*11996SThomas.Whitten@Sun.COM 	 */
976*11996SThomas.Whitten@Sun.COM 	if (ctid != -1)
977*11996SThomas.Whitten@Sun.COM 		contract_abandon(ctid);
978*11996SThomas.Whitten@Sun.COM 	ctid = -1;
979*11996SThomas.Whitten@Sun.COM 
980*11996SThomas.Whitten@Sun.COM 	pid = fork_common(SCF_INSTANCE_EMI, SCF_INSTANCE_EMI,
981*11996SThomas.Whitten@Sun.COM 	    MAX_EMI_RETRIES, &ctid, 0, 0, 0, 0, EMI_COOKIE);
982*11996SThomas.Whitten@Sun.COM 
983*11996SThomas.Whitten@Sun.COM 	if (pid != 0) {
984*11996SThomas.Whitten@Sun.COM 		int exitstatus;
985*11996SThomas.Whitten@Sun.COM 
986*11996SThomas.Whitten@Sun.COM 		if (waitpid(pid, &exitstatus, 0) == -1) {
987*11996SThomas.Whitten@Sun.COM 			fork_sulogin(B_FALSE, "waitpid on %s failed: "
988*11996SThomas.Whitten@Sun.COM 			    "%s\n", SCF_INSTANCE_EMI, strerror(errno));
989*11996SThomas.Whitten@Sun.COM 		} else if (WIFEXITED(exitstatus)) {
990*11996SThomas.Whitten@Sun.COM 			if (WEXITSTATUS(exitstatus)) {
991*11996SThomas.Whitten@Sun.COM 				fork_sulogin(B_FALSE, "%s exited with status "
992*11996SThomas.Whitten@Sun.COM 				    "%d \n", SCF_INSTANCE_EMI,
993*11996SThomas.Whitten@Sun.COM 				    WEXITSTATUS(exitstatus));
994*11996SThomas.Whitten@Sun.COM 				goto fork_retry;
995*11996SThomas.Whitten@Sun.COM 			}
996*11996SThomas.Whitten@Sun.COM 		} else if (WIFSIGNALED(exitstatus)) {
997*11996SThomas.Whitten@Sun.COM 			char signame[SIG2STR_MAX];
998*11996SThomas.Whitten@Sun.COM 
999*11996SThomas.Whitten@Sun.COM 			if (sig2str(WTERMSIG(exitstatus), signame))
1000*11996SThomas.Whitten@Sun.COM 				(void) snprintf(signame, SIG2STR_MAX,
1001*11996SThomas.Whitten@Sun.COM 				    "signum %d", WTERMSIG(exitstatus));
1002*11996SThomas.Whitten@Sun.COM 
1003*11996SThomas.Whitten@Sun.COM 			fork_sulogin(B_FALSE, "%s signalled: %s\n",
1004*11996SThomas.Whitten@Sun.COM 			    SCF_INSTANCE_EMI, signame);
1005*11996SThomas.Whitten@Sun.COM 			goto fork_retry;
1006*11996SThomas.Whitten@Sun.COM 		} else {
1007*11996SThomas.Whitten@Sun.COM 			fork_sulogin(B_FALSE, "%s non-exit condition: 0x%x\n",
1008*11996SThomas.Whitten@Sun.COM 			    SCF_INSTANCE_EMI, exitstatus);
1009*11996SThomas.Whitten@Sun.COM 			goto fork_retry;
1010*11996SThomas.Whitten@Sun.COM 		}
1011*11996SThomas.Whitten@Sun.COM 
1012*11996SThomas.Whitten@Sun.COM 		log_framework(LOG_DEBUG, "%s completed successfully\n",
1013*11996SThomas.Whitten@Sun.COM 		    SCF_INSTANCE_EMI);
1014*11996SThomas.Whitten@Sun.COM 
1015*11996SThomas.Whitten@Sun.COM 		/*
1016*11996SThomas.Whitten@Sun.COM 		 * Once Early Manifest Import completed, the Early Manifest
1017*11996SThomas.Whitten@Sun.COM 		 * Import service must have been imported so set log file and
1018*11996SThomas.Whitten@Sun.COM 		 * state properties. Since this information is required for
1019*11996SThomas.Whitten@Sun.COM 		 * late manifest import and common admin operations, failing to
1020*11996SThomas.Whitten@Sun.COM 		 * set these properties should result in su login so admin can
1021*11996SThomas.Whitten@Sun.COM 		 * correct the problem.
1022*11996SThomas.Whitten@Sun.COM 		 */
1023*11996SThomas.Whitten@Sun.COM 		(void) emi_set_state(RESTARTER_STATE_ONLINE,
1024*11996SThomas.Whitten@Sun.COM 		    setemilog ? B_TRUE : B_FALSE);
1025*11996SThomas.Whitten@Sun.COM 
1026*11996SThomas.Whitten@Sun.COM 		return;
1027*11996SThomas.Whitten@Sun.COM 	}
1028*11996SThomas.Whitten@Sun.COM 
1029*11996SThomas.Whitten@Sun.COM 	/* child */
1030*11996SThomas.Whitten@Sun.COM 
1031*11996SThomas.Whitten@Sun.COM 	/*
1032*11996SThomas.Whitten@Sun.COM 	 * Set our per-process core file path to leave core files in
1033*11996SThomas.Whitten@Sun.COM 	 * /etc/svc/volatile directory, named after the PID to aid in debugging.
1034*11996SThomas.Whitten@Sun.COM 	 */
1035*11996SThomas.Whitten@Sun.COM 	(void) snprintf(corepath, sizeof (corepath),
1036*11996SThomas.Whitten@Sun.COM 	    "/etc/svc/volatile/core.emi.%%p");
1037*11996SThomas.Whitten@Sun.COM 	(void) core_set_process_path(corepath, strlen(corepath) + 1, getpid());
1038*11996SThomas.Whitten@Sun.COM 
1039*11996SThomas.Whitten@Sun.COM 	/*
1040*11996SThomas.Whitten@Sun.COM 	 * Similar to running legacy services, we need to manually set
1041*11996SThomas.Whitten@Sun.COM 	 * log files here and environment variables.
1042*11996SThomas.Whitten@Sun.COM 	 */
1043*11996SThomas.Whitten@Sun.COM 	setlog(EMI_LOG);
1044*11996SThomas.Whitten@Sun.COM 
1045*11996SThomas.Whitten@Sun.COM 	envp = startd_zalloc(sizeof (char *) * 3);
1046*11996SThomas.Whitten@Sun.COM 	np = envp;
1047*11996SThomas.Whitten@Sun.COM 
1048*11996SThomas.Whitten@Sun.COM 	sz = sizeof ("SMF_FMRI=") + strlen(SCF_INSTANCE_EMI);
1049*11996SThomas.Whitten@Sun.COM 	*np = startd_zalloc(sz);
1050*11996SThomas.Whitten@Sun.COM 	(void) strlcpy(*np, "SMF_FMRI=", sz);
1051*11996SThomas.Whitten@Sun.COM 	(void) strncat(*np, SCF_INSTANCE_EMI, sz);
1052*11996SThomas.Whitten@Sun.COM 	np++;
1053*11996SThomas.Whitten@Sun.COM 
1054*11996SThomas.Whitten@Sun.COM 	emipath = getenv("PATH");
1055*11996SThomas.Whitten@Sun.COM 	if (emipath == NULL)
1056*11996SThomas.Whitten@Sun.COM 		emipath = strdup("/usr/sbin:/usr/bin");
1057*11996SThomas.Whitten@Sun.COM 
1058*11996SThomas.Whitten@Sun.COM 	sz = sizeof ("PATH=") + strlen(emipath);
1059*11996SThomas.Whitten@Sun.COM 	*np = startd_zalloc(sz);
1060*11996SThomas.Whitten@Sun.COM 	(void) strlcpy(*np, "PATH=", sz);
1061*11996SThomas.Whitten@Sun.COM 	(void) strncat(*np, emipath, sz);
1062*11996SThomas.Whitten@Sun.COM 
1063*11996SThomas.Whitten@Sun.COM 	log_framework(LOG_DEBUG, "executing Early Manifest Import\n");
1064*11996SThomas.Whitten@Sun.COM 	(void) execle(EMI_PATH, EMI_PATH, NULL, envp);
1065*11996SThomas.Whitten@Sun.COM 
1066*11996SThomas.Whitten@Sun.COM 	/*
1067*11996SThomas.Whitten@Sun.COM 	 * Status code is used above to identify Early Manifest Import
1068*11996SThomas.Whitten@Sun.COM 	 * exec failure.
1069*11996SThomas.Whitten@Sun.COM 	 */
1070*11996SThomas.Whitten@Sun.COM 	exit(1);
1071*11996SThomas.Whitten@Sun.COM }
1072*11996SThomas.Whitten@Sun.COM 
10738944Sdp@eng.sun.com extern char **environ;
10748944Sdp@eng.sun.com 
10758944Sdp@eng.sun.com /*
10768944Sdp@eng.sun.com  * A local variation on system(3c) which accepts a timeout argument.  This
10778944Sdp@eng.sun.com  * allows us to better ensure that the system will actually shut down.
10788944Sdp@eng.sun.com  *
10798944Sdp@eng.sun.com  * gracetime specifies an amount of time in seconds which the routine must wait
10808944Sdp@eng.sun.com  * after the command exits, to allow for asynchronous effects (like sent
10818944Sdp@eng.sun.com  * signals) to take effect.  This can be zero.
10828944Sdp@eng.sun.com  */
10838944Sdp@eng.sun.com void
fork_with_timeout(const char * cmd,uint_t gracetime,uint_t timeout)10848944Sdp@eng.sun.com fork_with_timeout(const char *cmd, uint_t gracetime, uint_t timeout)
10858944Sdp@eng.sun.com {
10868944Sdp@eng.sun.com 	int err = 0;
10878944Sdp@eng.sun.com 	pid_t pid;
10888944Sdp@eng.sun.com 	char *argv[4];
10898944Sdp@eng.sun.com 	posix_spawnattr_t attr;
10908944Sdp@eng.sun.com 	posix_spawn_file_actions_t factions;
10918944Sdp@eng.sun.com 
10928944Sdp@eng.sun.com 	sigset_t mask, savemask;
10938944Sdp@eng.sun.com 	uint_t msec_timeout;
10948944Sdp@eng.sun.com 	uint_t msec_spent = 0;
10958944Sdp@eng.sun.com 	uint_t msec_gracetime;
10968944Sdp@eng.sun.com 	int status;
10978944Sdp@eng.sun.com 
10988944Sdp@eng.sun.com 	msec_timeout = timeout * 1000;
10998944Sdp@eng.sun.com 	msec_gracetime = gracetime * 1000;
11008944Sdp@eng.sun.com 
11018944Sdp@eng.sun.com 	/*
11028944Sdp@eng.sun.com 	 * See also system(3c) in libc.  This is very similar, except
11038944Sdp@eng.sun.com 	 * that we avoid some unneeded complexity.
11048944Sdp@eng.sun.com 	 */
11058944Sdp@eng.sun.com 	err = posix_spawnattr_init(&attr);
11068944Sdp@eng.sun.com 	if (err == 0)
11078944Sdp@eng.sun.com 		err = posix_spawnattr_setflags(&attr,
11088944Sdp@eng.sun.com 		    POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
11098944Sdp@eng.sun.com 		    POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP |
11108944Sdp@eng.sun.com 		    POSIX_SPAWN_NOEXECERR_NP);
11118944Sdp@eng.sun.com 
11128944Sdp@eng.sun.com 	/*
11138944Sdp@eng.sun.com 	 * We choose to close fd's above 2, a deviation from system.
11148944Sdp@eng.sun.com 	 */
11158944Sdp@eng.sun.com 	if (err == 0)
11168944Sdp@eng.sun.com 		err = posix_spawn_file_actions_init(&factions);
11178944Sdp@eng.sun.com 	if (err == 0)
11188944Sdp@eng.sun.com 		err = posix_spawn_file_actions_addclosefrom_np(&factions,
11198944Sdp@eng.sun.com 		    STDERR_FILENO + 1);
11208944Sdp@eng.sun.com 
11218944Sdp@eng.sun.com 	(void) sigemptyset(&mask);
11228944Sdp@eng.sun.com 	(void) sigaddset(&mask, SIGCHLD);
11238944Sdp@eng.sun.com 	(void) thr_sigsetmask(SIG_BLOCK, &mask, &savemask);
11248944Sdp@eng.sun.com 
11258944Sdp@eng.sun.com 	argv[0] = "/bin/sh";
11268944Sdp@eng.sun.com 	argv[1] = "-c";
11278944Sdp@eng.sun.com 	argv[2] = (char *)cmd;
11288944Sdp@eng.sun.com 	argv[3] = NULL;
11298944Sdp@eng.sun.com 
11308944Sdp@eng.sun.com 	if (err == 0)
11318944Sdp@eng.sun.com 		err = posix_spawn(&pid, "/bin/sh", &factions, &attr,
11328944Sdp@eng.sun.com 		    (char *const *)argv, (char *const *)environ);
11338944Sdp@eng.sun.com 
11348944Sdp@eng.sun.com 	(void) posix_spawnattr_destroy(&attr);
11358944Sdp@eng.sun.com 	(void) posix_spawn_file_actions_destroy(&factions);
11368944Sdp@eng.sun.com 
11378944Sdp@eng.sun.com 	if (err) {
11388944Sdp@eng.sun.com 		uu_warn("Failed to spawn %s: %s\n", cmd, strerror(err));
11398944Sdp@eng.sun.com 	} else {
11408944Sdp@eng.sun.com 		for (;;) {
11418944Sdp@eng.sun.com 			int w;
11428944Sdp@eng.sun.com 			w = waitpid(pid, &status, WNOHANG);
11438944Sdp@eng.sun.com 			if (w == -1 && errno != EINTR)
11448944Sdp@eng.sun.com 				break;
11458944Sdp@eng.sun.com 			if (w > 0) {
11468944Sdp@eng.sun.com 				/*
11478944Sdp@eng.sun.com 				 * Command succeeded, so give it gracetime
11488944Sdp@eng.sun.com 				 * seconds for it to have an effect.
11498944Sdp@eng.sun.com 				 */
11508944Sdp@eng.sun.com 				if (status == 0 && msec_gracetime != 0)
11518944Sdp@eng.sun.com 					(void) poll(NULL, 0, msec_gracetime);
11528944Sdp@eng.sun.com 				break;
11538944Sdp@eng.sun.com 			}
11548944Sdp@eng.sun.com 
11558944Sdp@eng.sun.com 			(void) poll(NULL, 0, 100);
11568944Sdp@eng.sun.com 			msec_spent += 100;
11578944Sdp@eng.sun.com 			/*
11588944Sdp@eng.sun.com 			 * If we timed out, kill off the process, then try to
11598944Sdp@eng.sun.com 			 * wait for it-- it's possible that we could accumulate
11608944Sdp@eng.sun.com 			 * a zombie here since we don't allow waitpid to hang,
11618944Sdp@eng.sun.com 			 * but it's better to let that happen and continue to
11628944Sdp@eng.sun.com 			 * make progress.
11638944Sdp@eng.sun.com 			 */
11648944Sdp@eng.sun.com 			if (msec_spent >= msec_timeout) {
11658944Sdp@eng.sun.com 				uu_warn("'%s' timed out after %d "
11668944Sdp@eng.sun.com 				    "seconds.  Killing.\n", cmd,
11678944Sdp@eng.sun.com 				    timeout);
11688944Sdp@eng.sun.com 				(void) kill(pid, SIGTERM);
11698944Sdp@eng.sun.com 				(void) poll(NULL, 0, 100);
11708944Sdp@eng.sun.com 				(void) kill(pid, SIGKILL);
11718944Sdp@eng.sun.com 				(void) poll(NULL, 0, 100);
11728944Sdp@eng.sun.com 				(void) waitpid(pid, &status, WNOHANG);
11738944Sdp@eng.sun.com 				break;
11748944Sdp@eng.sun.com 			}
11758944Sdp@eng.sun.com 		}
11768944Sdp@eng.sun.com 	}
11778944Sdp@eng.sun.com 	(void) thr_sigsetmask(SIG_BLOCK, &savemask, NULL);
11788944Sdp@eng.sun.com }
1179