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