xref: /onnv-gate/usr/src/cmd/svc/startd/fork.c (revision 8095:432e50464061)
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 /*
226073Sacruz  * Copyright 2008 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>
470Sstevel@tonic-gate #include <port.h>
480Sstevel@tonic-gate #include <signal.h>
490Sstevel@tonic-gate #include <stdarg.h>
500Sstevel@tonic-gate #include <stdio.h>
510Sstevel@tonic-gate #include <stdlib.h>
520Sstevel@tonic-gate #include <string.h>
530Sstevel@tonic-gate #include <unistd.h>
544740Sjeanm #include <utmpx.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include "configd_exit.h"
570Sstevel@tonic-gate #include "protocol.h"
580Sstevel@tonic-gate #include "startd.h"
590Sstevel@tonic-gate 
604740Sjeanm static	struct	utmpx	*utmpp;	/* pointer for getutxent() */
614740Sjeanm 
620Sstevel@tonic-gate pid_t
630Sstevel@tonic-gate startd_fork1(int *forkerr)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	pid_t p;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * prefork stack
690Sstevel@tonic-gate 	 */
700Sstevel@tonic-gate 	wait_prefork();
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	p = fork1();
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (p == -1 && forkerr != NULL)
750Sstevel@tonic-gate 		*forkerr = errno;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * postfork stack
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	wait_postfork(p);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	return (p);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * void fork_mount(char *, char *)
870Sstevel@tonic-gate  *   Run mount(1M) with the given options and mount point.  (mount(1M) has much
880Sstevel@tonic-gate  *   hidden knowledge; it's much less correct to reimplement that logic here to
890Sstevel@tonic-gate  *   save a fork(2)/exec(2) invocation.)
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate int
920Sstevel@tonic-gate fork_mount(char *path, char *opts)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	pid_t pid;
950Sstevel@tonic-gate 	uint_t tries = 0;
960Sstevel@tonic-gate 	int status;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	for (pid = fork1(); pid == -1; pid = fork1()) {
990Sstevel@tonic-gate 		if (++tries > MAX_MOUNT_RETRIES)
1000Sstevel@tonic-gate 			return (-1);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		(void) sleep(tries);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (pid != 0) {
1060Sstevel@tonic-gate 		(void) waitpid(pid, &status, 0);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		/*
1090Sstevel@tonic-gate 		 * If our mount(1M) invocation exited by peculiar means, or with
1100Sstevel@tonic-gate 		 * a non-zero status, our mount likelihood is low.
1110Sstevel@tonic-gate 		 */
1120Sstevel@tonic-gate 		if (!WIFEXITED(status) ||
1130Sstevel@tonic-gate 		    WEXITSTATUS(status) != 0)
1140Sstevel@tonic-gate 			return (-1);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		return (0);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	(void) execl("/sbin/mount", "mount", "-o", opts, path, NULL);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	return (-1);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * pid_t fork_common(...)
1260Sstevel@tonic-gate  *   Common routine used by fork_sulogin and fork_configd to fork a
1270Sstevel@tonic-gate  *   process in a contract with the provided terms.  Invokes
1280Sstevel@tonic-gate  *   fork_sulogin (with its no-fork argument set) on errors.
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate static pid_t
1316073Sacruz fork_common(const char *name, const char *svc_fmri, int retries, ctid_t *ctidp,
1320Sstevel@tonic-gate     uint_t inf, uint_t crit, uint_t fatal, uint_t param, uint64_t cookie)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	uint_t tries = 0;
1350Sstevel@tonic-gate 	int ctfd, err;
1360Sstevel@tonic-gate 	pid_t pid;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * Establish process contract terms.
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 	if ((ctfd = open64(CTFS_ROOT "/process/template", O_RDWR)) == -1) {
1420Sstevel@tonic-gate 		fork_sulogin(B_TRUE, "Could not open process contract template "
1430Sstevel@tonic-gate 		    "for %s: %s\n", name, strerror(errno));
1440Sstevel@tonic-gate 		/* NOTREACHED */
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	err = ct_tmpl_set_critical(ctfd, crit);
1480Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(ctfd, fatal);
1490Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(ctfd, inf);
1500Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(ctfd, param);
1510Sstevel@tonic-gate 	err |= ct_tmpl_set_cookie(ctfd, cookie);
1526073Sacruz 	err |= ct_pr_tmpl_set_svc_fmri(ctfd, svc_fmri);
1536073Sacruz 	err |= ct_pr_tmpl_set_svc_aux(ctfd, name);
1540Sstevel@tonic-gate 	if (err) {
1550Sstevel@tonic-gate 		(void) close(ctfd);
1560Sstevel@tonic-gate 		fork_sulogin(B_TRUE, "Could not set %s process contract "
1570Sstevel@tonic-gate 		    "terms\n", name);
1580Sstevel@tonic-gate 		/* NOTREACHED */
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (err = ct_tmpl_activate(ctfd)) {
1620Sstevel@tonic-gate 		(void) close(ctfd);
1630Sstevel@tonic-gate 		fork_sulogin(B_TRUE, "Could not activate %s process contract "
1640Sstevel@tonic-gate 		    "template: %s\n", name, strerror(err));
1650Sstevel@tonic-gate 		/* NOTREACHED */
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * Attempt to fork "retries" times.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	for (pid = fork1(); pid == -1; pid = fork1()) {
1720Sstevel@tonic-gate 		if (++tries > retries) {
1730Sstevel@tonic-gate 			/*
1740Sstevel@tonic-gate 			 * When we exit the sulogin session, init(1M)
1750Sstevel@tonic-gate 			 * will restart svc.startd(1M).
1760Sstevel@tonic-gate 			 */
1770Sstevel@tonic-gate 			err = errno;
1780Sstevel@tonic-gate 			(void) ct_tmpl_clear(ctfd);
1790Sstevel@tonic-gate 			(void) close(ctfd);
1800Sstevel@tonic-gate 			fork_sulogin(B_TRUE, "Could not fork to start %s: %s\n",
1810Sstevel@tonic-gate 			    name, strerror(err));
1820Sstevel@tonic-gate 			/* NOTREACHED */
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 		(void) sleep(tries);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/*
1880Sstevel@tonic-gate 	 * Clean up, return pid and ctid.
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 	if (pid != 0 && (errno = contract_latest(ctidp)) != 0)
1910Sstevel@tonic-gate 		uu_die("Could not get new contract id for %s\n", name);
1920Sstevel@tonic-gate 	(void) ct_tmpl_clear(ctfd);
1930Sstevel@tonic-gate 	(void) close(ctfd);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	return (pid);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * void fork_sulogin(boolean_t, const char *, ...)
2000Sstevel@tonic-gate  *   When we are invoked with the -s flag from boot (or run into an unfixable
2010Sstevel@tonic-gate  *   situation), we run a private copy of sulogin.  When the sulogin session
2020Sstevel@tonic-gate  *   is ended, we continue.  This is the last fallback action for system
2030Sstevel@tonic-gate  *   maintenance.
2040Sstevel@tonic-gate  *
2050Sstevel@tonic-gate  *   If immediate is true, fork_sulogin() executes sulogin(1M) directly, without
2060Sstevel@tonic-gate  *   forking.
2070Sstevel@tonic-gate  *
2080Sstevel@tonic-gate  *   Because fork_sulogin() is needed potentially before we daemonize, we leave
2090Sstevel@tonic-gate  *   it outside the wait_register() framework.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate /*PRINTFLIKE2*/
2120Sstevel@tonic-gate void
2130Sstevel@tonic-gate fork_sulogin(boolean_t immediate, const char *format, ...)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	va_list args;
2165617Sacruz 	int fd_console;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	(void) printf("Requesting System Maintenance Mode\n");
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (!booting_to_single_user)
2210Sstevel@tonic-gate 		(void) printf("(See /lib/svc/share/README for more "
2220Sstevel@tonic-gate 		    "information.)\n");
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	va_start(args, format);
2250Sstevel@tonic-gate 	(void) vprintf(format, args);
2260Sstevel@tonic-gate 	va_end(args);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (!immediate) {
2290Sstevel@tonic-gate 		ctid_t	ctid;
2300Sstevel@tonic-gate 		pid_t	pid;
2310Sstevel@tonic-gate 
2326073Sacruz 		pid = fork_common("sulogin", SVC_SULOGIN_FMRI,
2336073Sacruz 		    MAX_SULOGIN_RETRIES, &ctid, CT_PR_EV_HWERR, 0,
2346073Sacruz 		    CT_PR_EV_HWERR, CT_PR_PGRPONLY, SULOGIN_COOKIE);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		if (pid != 0) {
2370Sstevel@tonic-gate 			(void) waitpid(pid, NULL, 0);
2380Sstevel@tonic-gate 			contract_abandon(ctid);
2390Sstevel@tonic-gate 			return;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 		/* close all inherited fds */
2420Sstevel@tonic-gate 		closefrom(0);
2430Sstevel@tonic-gate 	} else {
2440Sstevel@tonic-gate 		(void) printf("Directly executing sulogin.\n");
2450Sstevel@tonic-gate 		/*
2460Sstevel@tonic-gate 		 * Can't call closefrom() in this MT section
2470Sstevel@tonic-gate 		 * so safely close a minimum set of fds.
2480Sstevel@tonic-gate 		 */
2495617Sacruz 		(void) close(STDIN_FILENO);
2505617Sacruz 		(void) close(STDOUT_FILENO);
2515617Sacruz 		(void) close(STDERR_FILENO);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	(void) setpgrp();
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/* open the console for sulogin */
2570Sstevel@tonic-gate 	if ((fd_console = open("/dev/console", O_RDWR)) >= 0) {
2580Sstevel@tonic-gate 		if (fd_console != STDIN_FILENO)
2590Sstevel@tonic-gate 			while (dup2(fd_console, STDIN_FILENO) < 0 &&
2600Sstevel@tonic-gate 			    errno == EINTR)
2610Sstevel@tonic-gate 				;
2620Sstevel@tonic-gate 		if (fd_console != STDOUT_FILENO)
2630Sstevel@tonic-gate 			while (dup2(fd_console, STDOUT_FILENO) < 0 &&
2640Sstevel@tonic-gate 			    errno == EINTR)
2650Sstevel@tonic-gate 				;
2660Sstevel@tonic-gate 		if (fd_console != STDERR_FILENO)
2670Sstevel@tonic-gate 			while (dup2(fd_console, STDERR_FILENO) < 0 &&
2680Sstevel@tonic-gate 			    errno == EINTR)
2690Sstevel@tonic-gate 				;
2705617Sacruz 		if (fd_console > STDERR_FILENO)
2710Sstevel@tonic-gate 			(void) close(fd_console);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2744740Sjeanm 	setutxent();
2754740Sjeanm 	while ((utmpp = getutxent()) != NULL) {
2764740Sjeanm 		if (strcmp(utmpp->ut_user, "LOGIN") != 0) {
2774740Sjeanm 			if (strcmp(utmpp->ut_line, "console") == 0) {
2784740Sjeanm 				(void) kill(utmpp->ut_pid, 9);
2794740Sjeanm 				break;
2804740Sjeanm 			}
2814740Sjeanm 		}
2824740Sjeanm 	}
2834740Sjeanm 
2840Sstevel@tonic-gate 	(void) execl("/sbin/sulogin", "sulogin", NULL);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	uu_warn("Could not exec() sulogin");
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	exit(1);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate #define	CONFIGD_PATH	"/lib/svc/bin/svc.configd"
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * void fork_configd(int status)
2950Sstevel@tonic-gate  *   We are interested in exit events (since the parent's exiting means configd
2960Sstevel@tonic-gate  *   is ready to run and since the child's exiting indicates an error case) and
2970Sstevel@tonic-gate  *   in empty events.  This means we have a unique template for initiating
2980Sstevel@tonic-gate  *   configd.
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate void
3010Sstevel@tonic-gate fork_configd(int exitstatus)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	pid_t pid;
3040Sstevel@tonic-gate 	ctid_t ctid = -1;
3050Sstevel@tonic-gate 	int err;
3060Sstevel@tonic-gate 	char path[PATH_MAX];
3070Sstevel@tonic-gate 
308*8095SSean.Wilcox@Sun.COM 	/*
309*8095SSean.Wilcox@Sun.COM 	 * Checking the existatus for the potential failure of the
310*8095SSean.Wilcox@Sun.COM 	 * daemonized svc.configd.  If this is not the first time
311*8095SSean.Wilcox@Sun.COM 	 * through, but a call from the svc.configd monitoring thread
312*8095SSean.Wilcox@Sun.COM 	 * after a failure this is the status that is expected.  Other
313*8095SSean.Wilcox@Sun.COM 	 * failures are exposed during initialization or are fixed
314*8095SSean.Wilcox@Sun.COM 	 * by a restart (e.g door closings).
315*8095SSean.Wilcox@Sun.COM 	 *
316*8095SSean.Wilcox@Sun.COM 	 * If this is on-disk database corruption it will also be
317*8095SSean.Wilcox@Sun.COM 	 * caught by a restart but could be cleared before the restart.
318*8095SSean.Wilcox@Sun.COM 	 *
319*8095SSean.Wilcox@Sun.COM 	 * Or this could be internal database corruption due to a
320*8095SSean.Wilcox@Sun.COM 	 * rogue service that needs to be cleared before restart.
321*8095SSean.Wilcox@Sun.COM 	 */
322*8095SSean.Wilcox@Sun.COM 	if (WEXITSTATUS(exitstatus) == CONFIGD_EXIT_DATABASE_BAD) {
323*8095SSean.Wilcox@Sun.COM 		fork_sulogin(B_FALSE, "svc.configd exited with database "
324*8095SSean.Wilcox@Sun.COM 		    "corrupt error after initialization of the repository\n");
325*8095SSean.Wilcox@Sun.COM 	}
326*8095SSean.Wilcox@Sun.COM 
3270Sstevel@tonic-gate retry:
3280Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "fork_configd trying to start svc.configd\n");
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * If we're retrying, we will have an old contract lying around
3320Sstevel@tonic-gate 	 * from the failure.  Since we're going to be creating a new
3330Sstevel@tonic-gate 	 * contract shortly, we abandon the old one now.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	if (ctid != -1)
3360Sstevel@tonic-gate 		contract_abandon(ctid);
3370Sstevel@tonic-gate 	ctid = -1;
3380Sstevel@tonic-gate 
3396073Sacruz 	pid = fork_common("svc.configd", SCF_SERVICE_CONFIGD,
3406073Sacruz 	    MAX_CONFIGD_RETRIES, &ctid, 0, CT_PR_EV_EXIT, 0,
3416073Sacruz 	    CT_PR_INHERIT | CT_PR_REGENT, CONFIGD_COOKIE);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (pid != 0) {
3440Sstevel@tonic-gate 		int exitstatus;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		st->st_configd_pid = pid;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		if (waitpid(pid, &exitstatus, 0) == -1) {
3490Sstevel@tonic-gate 			fork_sulogin(B_FALSE, "waitpid on svc.configd "
3500Sstevel@tonic-gate 			    "failed: %s\n", strerror(errno));
3510Sstevel@tonic-gate 		} else if (WIFEXITED(exitstatus)) {
3520Sstevel@tonic-gate 			char *errstr;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			/*
3550Sstevel@tonic-gate 			 * Examine exitstatus.  This will eventually get more
3560Sstevel@tonic-gate 			 * complicated, as we will want to teach startd how to
3570Sstevel@tonic-gate 			 * invoke configd with alternate repositories, etc.
3580Sstevel@tonic-gate 			 *
3590Sstevel@tonic-gate 			 * Note that exec(2) failure results in an exit status
3600Sstevel@tonic-gate 			 * of 1, resulting in the default clause below.
3610Sstevel@tonic-gate 			 */
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 			/*
3640Sstevel@tonic-gate 			 * Assign readable strings to cases we don't handle, or
3650Sstevel@tonic-gate 			 * have error outcomes that cannot be eliminated.
3660Sstevel@tonic-gate 			 */
3670Sstevel@tonic-gate 			switch (WEXITSTATUS(exitstatus)) {
3680Sstevel@tonic-gate 			case CONFIGD_EXIT_BAD_ARGS:
3690Sstevel@tonic-gate 				errstr = "bad arguments";
3700Sstevel@tonic-gate 				break;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_BAD:
3730Sstevel@tonic-gate 				errstr = "database corrupt";
3740Sstevel@tonic-gate 				break;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_LOCKED:
3770Sstevel@tonic-gate 				errstr = "database locked";
3780Sstevel@tonic-gate 				break;
3790Sstevel@tonic-gate 			case CONFIGD_EXIT_INIT_FAILED:
3800Sstevel@tonic-gate 				errstr = "initialization failure";
3810Sstevel@tonic-gate 				break;
3820Sstevel@tonic-gate 			case CONFIGD_EXIT_DOOR_INIT_FAILED:
3830Sstevel@tonic-gate 				errstr = "door initialization failure";
3840Sstevel@tonic-gate 				break;
3850Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_INIT_FAILED:
3860Sstevel@tonic-gate 				errstr = "database initialization failure";
3870Sstevel@tonic-gate 				break;
3880Sstevel@tonic-gate 			case CONFIGD_EXIT_NO_THREADS:
3890Sstevel@tonic-gate 				errstr = "no threads available";
3900Sstevel@tonic-gate 				break;
3910Sstevel@tonic-gate 			case CONFIGD_EXIT_LOST_MAIN_DOOR:
3920Sstevel@tonic-gate 				errstr = "lost door server attachment";
3930Sstevel@tonic-gate 				break;
3940Sstevel@tonic-gate 			case 1:
3950Sstevel@tonic-gate 				errstr = "execution failure";
3960Sstevel@tonic-gate 				break;
3970Sstevel@tonic-gate 			default:
3980Sstevel@tonic-gate 				errstr = "unknown error";
3990Sstevel@tonic-gate 				break;
4000Sstevel@tonic-gate 			}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 			/*
4030Sstevel@tonic-gate 			 * Remedial actions for various configd failures.
4040Sstevel@tonic-gate 			 */
4050Sstevel@tonic-gate 			switch (WEXITSTATUS(exitstatus)) {
4060Sstevel@tonic-gate 			case CONFIGD_EXIT_OKAY:
4070Sstevel@tonic-gate 				break;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 			case CONFIGD_EXIT_DATABASE_LOCKED:
4100Sstevel@tonic-gate 				/* attempt remount of / read-write */
4110Sstevel@tonic-gate 				if (fs_is_read_only("/", NULL) == 1) {
4120Sstevel@tonic-gate 					if (fs_remount("/") == -1)
4130Sstevel@tonic-gate 						fork_sulogin(B_FALSE,
4140Sstevel@tonic-gate 						    "remount of root "
4150Sstevel@tonic-gate 						    "filesystem failed\n");
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 					goto retry;
4180Sstevel@tonic-gate 				}
4190Sstevel@tonic-gate 				break;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 			default:
4220Sstevel@tonic-gate 				fork_sulogin(B_FALSE, "svc.configd exited "
4230Sstevel@tonic-gate 				    "with status %d (%s)\n",
4240Sstevel@tonic-gate 				    WEXITSTATUS(exitstatus), errstr);
4250Sstevel@tonic-gate 				goto retry;
4260Sstevel@tonic-gate 			}
4270Sstevel@tonic-gate 		} else if (WIFSIGNALED(exitstatus)) {
4280Sstevel@tonic-gate 			char signame[SIG2STR_MAX];
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 			if (sig2str(WTERMSIG(exitstatus), signame))
4310Sstevel@tonic-gate 				(void) snprintf(signame, SIG2STR_MAX,
4320Sstevel@tonic-gate 				    "signum %d", WTERMSIG(exitstatus));
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 			fork_sulogin(B_FALSE, "svc.configd signalled:"
4350Sstevel@tonic-gate 			    " %s\n", signame);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 			goto retry;
4380Sstevel@tonic-gate 		} else {
4390Sstevel@tonic-gate 			fork_sulogin(B_FALSE, "svc.configd non-exit "
4400Sstevel@tonic-gate 			    "condition: 0x%x\n", exitstatus);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 			goto retry;
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		/*
4460Sstevel@tonic-gate 		 * Announce that we have a valid svc.configd status.
4470Sstevel@tonic-gate 		 */
4480Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_configd_live_lock);
4490Sstevel@tonic-gate 		st->st_configd_lives = 1;
4500Sstevel@tonic-gate 		err = pthread_cond_broadcast(&st->st_configd_live_cv);
4510Sstevel@tonic-gate 		assert(err == 0);
4520Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_configd_live_lock);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "fork_configd broadcasts configd is "
4550Sstevel@tonic-gate 		    "live\n");
4560Sstevel@tonic-gate 		return;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * Set our per-process core file path to leave core files in
4610Sstevel@tonic-gate 	 * /etc/svc/volatile directory, named after the PID to aid in debugging.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path),
4640Sstevel@tonic-gate 	    "/etc/svc/volatile/core.configd.%%p");
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	(void) core_set_process_path(path, strlen(path) + 1, getpid());
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "executing svc.configd\n");
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	(void) execl(CONFIGD_PATH, CONFIGD_PATH, NULL);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/*
4730Sstevel@tonic-gate 	 * Status code is used above to identify configd exec failure.
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	exit(1);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate void *
4790Sstevel@tonic-gate fork_configd_thread(void *vctid)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	int fd, err;
4820Sstevel@tonic-gate 	ctid_t configd_ctid = (ctid_t)vctid;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (configd_ctid == -1) {
4850Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
4860Sstevel@tonic-gate 		    "fork_configd_thread starting svc.configd\n");
4870Sstevel@tonic-gate 		fork_configd(0);
4880Sstevel@tonic-gate 	} else {
4890Sstevel@tonic-gate 		/*
4900Sstevel@tonic-gate 		 * configd_ctid is known:  we broadcast and continue.
4910Sstevel@tonic-gate 		 * test contract for appropriate state by verifying that
4920Sstevel@tonic-gate 		 * there is one or more processes within it?
4930Sstevel@tonic-gate 		 */
4940Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
4950Sstevel@tonic-gate 		    "fork_configd_thread accepting svc.configd with CTID %ld\n",
4960Sstevel@tonic-gate 		    configd_ctid);
4970Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_configd_live_lock);
4980Sstevel@tonic-gate 		st->st_configd_lives = 1;
4990Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&st->st_configd_live_cv);
5000Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_configd_live_lock);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
5040Sstevel@tonic-gate 	if (fd == -1)
5050Sstevel@tonic-gate 		uu_die("process bundle open failed");
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Make sure we get all events (including those generated by configd
5090Sstevel@tonic-gate 	 * before this thread was started).
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	err = ct_event_reset(fd);
5120Sstevel@tonic-gate 	assert(err == 0);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	for (;;) {
5150Sstevel@tonic-gate 		int efd, sfd;
5160Sstevel@tonic-gate 		ct_evthdl_t ev;
5170Sstevel@tonic-gate 		uint32_t type;
5180Sstevel@tonic-gate 		ctevid_t evid;
5190Sstevel@tonic-gate 		ct_stathdl_t status;
5200Sstevel@tonic-gate 		ctid_t ctid;
5210Sstevel@tonic-gate 		uint64_t cookie;
5220Sstevel@tonic-gate 		pid_t pid;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		if (err = ct_event_read_critical(fd, &ev)) {
5250Sstevel@tonic-gate 			assert(err != EINVAL && err != EAGAIN);
5260Sstevel@tonic-gate 			log_error(LOG_WARNING,
5270Sstevel@tonic-gate 			    "Error reading next contract event: %s",
5280Sstevel@tonic-gate 			    strerror(err));
5290Sstevel@tonic-gate 			continue;
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 		evid = ct_event_get_evid(ev);
5330Sstevel@tonic-gate 		ctid = ct_event_get_ctid(ev);
5340Sstevel@tonic-gate 		type = ct_event_get_type(ev);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		/* Fetch cookie. */
5370Sstevel@tonic-gate 		sfd = contract_open(ctid, "process", "status", O_RDONLY);
5380Sstevel@tonic-gate 		if (sfd < 0) {
5390Sstevel@tonic-gate 			ct_event_free(ev);
5400Sstevel@tonic-gate 			continue;
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 		if (err = ct_status_read(sfd, CTD_COMMON, &status)) {
5440Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Could not get status for "
5450Sstevel@tonic-gate 			    "contract %ld: %s\n", ctid, strerror(err));
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 			ct_event_free(ev);
5480Sstevel@tonic-gate 			startd_close(sfd);
5490Sstevel@tonic-gate 			continue;
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 		cookie = ct_status_get_cookie(status);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		ct_status_free(status);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		startd_close(sfd);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 		/*
5590Sstevel@tonic-gate 		 * Don't process events from contracts we aren't interested in.
5600Sstevel@tonic-gate 		 */
5610Sstevel@tonic-gate 		if (cookie != CONFIGD_COOKIE) {
5620Sstevel@tonic-gate 			ct_event_free(ev);
5630Sstevel@tonic-gate 			continue;
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 		if (type == CT_PR_EV_EXIT) {
5670Sstevel@tonic-gate 			int exitstatus;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 			(void) ct_pr_event_get_pid(ev, &pid);
5700Sstevel@tonic-gate 			(void) ct_pr_event_get_exitstatus(ev,
5710Sstevel@tonic-gate 			    &exitstatus);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 			if (st->st_configd_pid != pid) {
5740Sstevel@tonic-gate 				/*
5750Sstevel@tonic-gate 				 * This is the child exiting, so we
5760Sstevel@tonic-gate 				 * abandon the contract and restart
5770Sstevel@tonic-gate 				 * configd.
5780Sstevel@tonic-gate 				 */
5790Sstevel@tonic-gate 				contract_abandon(ctid);
5800Sstevel@tonic-gate 				fork_configd(exitstatus);
5810Sstevel@tonic-gate 			}
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 		efd = contract_open(ctid, "process", "ctl", O_WRONLY);
5850Sstevel@tonic-gate 		if (efd != -1) {
5860Sstevel@tonic-gate 			(void) ct_ctl_ack(efd, evid);
5870Sstevel@tonic-gate 			startd_close(efd);
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		ct_event_free(ev);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/*NOTREACHED*/
5950Sstevel@tonic-gate 	return (NULL);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate void
5990Sstevel@tonic-gate fork_rc_script(char rl, const char *arg, boolean_t wait)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	pid_t pid;
6020Sstevel@tonic-gate 	int tmpl, err, stat;
6030Sstevel@tonic-gate 	char path[20] = "/sbin/rc.", log[20] = "rc..log", timebuf[20];
6040Sstevel@tonic-gate 	time_t now;
6050Sstevel@tonic-gate 	struct tm ltime;
6060Sstevel@tonic-gate 	size_t sz;
6070Sstevel@tonic-gate 	char *pathenv;
6080Sstevel@tonic-gate 	char **nenv;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	path[8] = rl;
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	tmpl = open64(CTFS_ROOT "/process/template", O_RDWR);
6130Sstevel@tonic-gate 	if (tmpl >= 0) {
6140Sstevel@tonic-gate 		err = ct_tmpl_set_critical(tmpl, 0);
6150Sstevel@tonic-gate 		assert(err == 0);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 		err = ct_tmpl_set_informative(tmpl, 0);
6180Sstevel@tonic-gate 		assert(err == 0);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		err = ct_pr_tmpl_set_fatal(tmpl, 0);
6210Sstevel@tonic-gate 		assert(err == 0);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		err = ct_tmpl_activate(tmpl);
6240Sstevel@tonic-gate 		assert(err == 0);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		err = close(tmpl);
6270Sstevel@tonic-gate 		assert(err == 0);
6280Sstevel@tonic-gate 	} else {
6290Sstevel@tonic-gate 		uu_warn("Could not create contract template for %s.\n", path);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	pid = startd_fork1(NULL);
6330Sstevel@tonic-gate 	if (pid < 0) {
6340Sstevel@tonic-gate 		return;
6350Sstevel@tonic-gate 	} else if (pid != 0) {
6360Sstevel@tonic-gate 		/* parent */
6370Sstevel@tonic-gate 		if (wait) {
6380Sstevel@tonic-gate 			do
6390Sstevel@tonic-gate 				err = waitpid(pid, &stat, 0);
6404740Sjeanm 			while (err != 0 && errno == EINTR)
6414740Sjeanm 				;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 			if (!WIFEXITED(stat)) {
6440Sstevel@tonic-gate 				log_framework(LOG_INFO,
6450Sstevel@tonic-gate 				    "%s terminated with waitpid() status %d.\n",
6460Sstevel@tonic-gate 				    path, stat);
6470Sstevel@tonic-gate 			} else if (WEXITSTATUS(stat) != 0) {
6480Sstevel@tonic-gate 				log_framework(LOG_INFO,
6490Sstevel@tonic-gate 				    "%s failed with status %d.\n", path,
6500Sstevel@tonic-gate 				    WEXITSTATUS(stat));
6510Sstevel@tonic-gate 			}
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		return;
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/* child */
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	log[2] = rl;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	setlog(log);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	now = time(NULL);
6640Sstevel@tonic-gate 	sz = strftime(timebuf, sizeof (timebuf), "%b %e %T",
6650Sstevel@tonic-gate 	    localtime_r(&now, &ltime));
6660Sstevel@tonic-gate 	assert(sz != 0);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	(void) fprintf(stderr, "%s Executing %s %s\n", timebuf, path, arg);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (rl == 'S')
6710Sstevel@tonic-gate 		pathenv = "PATH=/sbin:/usr/sbin:/usr/bin";
6720Sstevel@tonic-gate 	else
6730Sstevel@tonic-gate 		pathenv = "PATH=/usr/sbin:/usr/bin";
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	nenv = set_smf_env(NULL, 0, pathenv, NULL, NULL);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	(void) execle(path, path, arg, 0, nenv);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	perror("exec");
6800Sstevel@tonic-gate 	exit(0);
6810Sstevel@tonic-gate }
682