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