17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 576cf44abSjeanm * Common Development and Distribution License (the "License"). 676cf44abSjeanm * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2276cf44abSjeanm * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * fork.c - safe forking for svc.startd 307c478bd9Sstevel@tonic-gate * 317c478bd9Sstevel@tonic-gate * fork_configd() and fork_sulogin() are related, special cases that handle the 327c478bd9Sstevel@tonic-gate * spawning of specific client processes for svc.startd. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <sys/contract/process.h> 367c478bd9Sstevel@tonic-gate #include <sys/corectl.h> 377c478bd9Sstevel@tonic-gate #include <sys/ctfs.h> 387c478bd9Sstevel@tonic-gate #include <sys/stat.h> 397c478bd9Sstevel@tonic-gate #include <sys/types.h> 407c478bd9Sstevel@tonic-gate #include <sys/uio.h> 417c478bd9Sstevel@tonic-gate #include <sys/wait.h> 427c478bd9Sstevel@tonic-gate #include <assert.h> 437c478bd9Sstevel@tonic-gate #include <errno.h> 447c478bd9Sstevel@tonic-gate #include <fcntl.h> 457c478bd9Sstevel@tonic-gate #include <libcontract.h> 467c478bd9Sstevel@tonic-gate #include <libcontract_priv.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate #include <port.h> 497c478bd9Sstevel@tonic-gate #include <signal.h> 507c478bd9Sstevel@tonic-gate #include <stdarg.h> 517c478bd9Sstevel@tonic-gate #include <stdio.h> 527c478bd9Sstevel@tonic-gate #include <stdlib.h> 537c478bd9Sstevel@tonic-gate #include <string.h> 547c478bd9Sstevel@tonic-gate #include <unistd.h> 5576cf44abSjeanm #include <utmpx.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include "configd_exit.h" 587c478bd9Sstevel@tonic-gate #include "protocol.h" 597c478bd9Sstevel@tonic-gate #include "startd.h" 607c478bd9Sstevel@tonic-gate 6176cf44abSjeanm static struct utmpx *utmpp; /* pointer for getutxent() */ 6276cf44abSjeanm 637c478bd9Sstevel@tonic-gate pid_t 647c478bd9Sstevel@tonic-gate startd_fork1(int *forkerr) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate pid_t p; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* 697c478bd9Sstevel@tonic-gate * prefork stack 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate wait_prefork(); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate p = fork1(); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate if (p == -1 && forkerr != NULL) 767c478bd9Sstevel@tonic-gate *forkerr = errno; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * postfork stack 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate wait_postfork(p); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate return (p); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * void fork_mount(char *, char *) 887c478bd9Sstevel@tonic-gate * Run mount(1M) with the given options and mount point. (mount(1M) has much 897c478bd9Sstevel@tonic-gate * hidden knowledge; it's much less correct to reimplement that logic here to 907c478bd9Sstevel@tonic-gate * save a fork(2)/exec(2) invocation.) 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate int 937c478bd9Sstevel@tonic-gate fork_mount(char *path, char *opts) 947c478bd9Sstevel@tonic-gate { 957c478bd9Sstevel@tonic-gate pid_t pid; 967c478bd9Sstevel@tonic-gate uint_t tries = 0; 977c478bd9Sstevel@tonic-gate int status; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate for (pid = fork1(); pid == -1; pid = fork1()) { 1007c478bd9Sstevel@tonic-gate if (++tries > MAX_MOUNT_RETRIES) 1017c478bd9Sstevel@tonic-gate return (-1); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate (void) sleep(tries); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate if (pid != 0) { 1077c478bd9Sstevel@tonic-gate (void) waitpid(pid, &status, 0); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * If our mount(1M) invocation exited by peculiar means, or with 1117c478bd9Sstevel@tonic-gate * a non-zero status, our mount likelihood is low. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate if (!WIFEXITED(status) || 1147c478bd9Sstevel@tonic-gate WEXITSTATUS(status) != 0) 1157c478bd9Sstevel@tonic-gate return (-1); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate return (0); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate (void) execl("/sbin/mount", "mount", "-o", opts, path, NULL); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate return (-1); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * pid_t fork_common(...) 1277c478bd9Sstevel@tonic-gate * Common routine used by fork_sulogin and fork_configd to fork a 1287c478bd9Sstevel@tonic-gate * process in a contract with the provided terms. Invokes 1297c478bd9Sstevel@tonic-gate * fork_sulogin (with its no-fork argument set) on errors. 1307c478bd9Sstevel@tonic-gate */ 1317c478bd9Sstevel@tonic-gate static pid_t 1327c478bd9Sstevel@tonic-gate fork_common(const char *name, int retries, ctid_t *ctidp, 1337c478bd9Sstevel@tonic-gate uint_t inf, uint_t crit, uint_t fatal, uint_t param, uint64_t cookie) 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate uint_t tries = 0; 1367c478bd9Sstevel@tonic-gate int ctfd, err; 1377c478bd9Sstevel@tonic-gate pid_t pid; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate /* 1407c478bd9Sstevel@tonic-gate * Establish process contract terms. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate if ((ctfd = open64(CTFS_ROOT "/process/template", O_RDWR)) == -1) { 1437c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not open process contract template " 1447c478bd9Sstevel@tonic-gate "for %s: %s\n", name, strerror(errno)); 1457c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate err = ct_tmpl_set_critical(ctfd, crit); 1497c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(ctfd, fatal); 1507c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_informative(ctfd, inf); 1517c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(ctfd, param); 1527c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_cookie(ctfd, cookie); 1537c478bd9Sstevel@tonic-gate if (err) { 1547c478bd9Sstevel@tonic-gate (void) close(ctfd); 1557c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not set %s process contract " 1567c478bd9Sstevel@tonic-gate "terms\n", name); 1577c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate if (err = ct_tmpl_activate(ctfd)) { 1617c478bd9Sstevel@tonic-gate (void) close(ctfd); 1627c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not activate %s process contract " 1637c478bd9Sstevel@tonic-gate "template: %s\n", name, strerror(err)); 1647c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * Attempt to fork "retries" times. 1697c478bd9Sstevel@tonic-gate */ 1707c478bd9Sstevel@tonic-gate for (pid = fork1(); pid == -1; pid = fork1()) { 1717c478bd9Sstevel@tonic-gate if (++tries > retries) { 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * When we exit the sulogin session, init(1M) 1747c478bd9Sstevel@tonic-gate * will restart svc.startd(1M). 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate err = errno; 1777c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(ctfd); 1787c478bd9Sstevel@tonic-gate (void) close(ctfd); 1797c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not fork to start %s: %s\n", 1807c478bd9Sstevel@tonic-gate name, strerror(err)); 1817c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate (void) sleep(tries); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * Clean up, return pid and ctid. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate if (pid != 0 && (errno = contract_latest(ctidp)) != 0) 1907c478bd9Sstevel@tonic-gate uu_die("Could not get new contract id for %s\n", name); 1917c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(ctfd); 1927c478bd9Sstevel@tonic-gate (void) close(ctfd); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate return (pid); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* 1987c478bd9Sstevel@tonic-gate * void fork_sulogin(boolean_t, const char *, ...) 1997c478bd9Sstevel@tonic-gate * When we are invoked with the -s flag from boot (or run into an unfixable 2007c478bd9Sstevel@tonic-gate * situation), we run a private copy of sulogin. When the sulogin session 2017c478bd9Sstevel@tonic-gate * is ended, we continue. This is the last fallback action for system 2027c478bd9Sstevel@tonic-gate * maintenance. 2037c478bd9Sstevel@tonic-gate * 2047c478bd9Sstevel@tonic-gate * If immediate is true, fork_sulogin() executes sulogin(1M) directly, without 2057c478bd9Sstevel@tonic-gate * forking. 2067c478bd9Sstevel@tonic-gate * 2077c478bd9Sstevel@tonic-gate * Because fork_sulogin() is needed potentially before we daemonize, we leave 2087c478bd9Sstevel@tonic-gate * it outside the wait_register() framework. 2097c478bd9Sstevel@tonic-gate */ 2107c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 2117c478bd9Sstevel@tonic-gate void 2127c478bd9Sstevel@tonic-gate fork_sulogin(boolean_t immediate, const char *format, ...) 2137c478bd9Sstevel@tonic-gate { 2147c478bd9Sstevel@tonic-gate va_list args; 215*2f1b831aSacruz int fd_console; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate (void) printf("Requesting System Maintenance Mode\n"); 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate if (!booting_to_single_user) 2207c478bd9Sstevel@tonic-gate (void) printf("(See /lib/svc/share/README for more " 2217c478bd9Sstevel@tonic-gate "information.)\n"); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate va_start(args, format); 2247c478bd9Sstevel@tonic-gate (void) vprintf(format, args); 2257c478bd9Sstevel@tonic-gate va_end(args); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate if (!immediate) { 2287c478bd9Sstevel@tonic-gate ctid_t ctid; 2297c478bd9Sstevel@tonic-gate pid_t pid; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate pid = fork_common("sulogin", MAX_SULOGIN_RETRIES, &ctid, 2327c478bd9Sstevel@tonic-gate CT_PR_EV_HWERR, 0, CT_PR_EV_HWERR, CT_PR_PGRPONLY, 2337c478bd9Sstevel@tonic-gate SULOGIN_COOKIE); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if (pid != 0) { 2367c478bd9Sstevel@tonic-gate (void) waitpid(pid, NULL, 0); 2377c478bd9Sstevel@tonic-gate contract_abandon(ctid); 2387c478bd9Sstevel@tonic-gate return; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate /* close all inherited fds */ 2417c478bd9Sstevel@tonic-gate closefrom(0); 2427c478bd9Sstevel@tonic-gate } else { 2437c478bd9Sstevel@tonic-gate (void) printf("Directly executing sulogin.\n"); 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * Can't call closefrom() in this MT section 2467c478bd9Sstevel@tonic-gate * so safely close a minimum set of fds. 2477c478bd9Sstevel@tonic-gate */ 248*2f1b831aSacruz (void) close(STDIN_FILENO); 249*2f1b831aSacruz (void) close(STDOUT_FILENO); 250*2f1b831aSacruz (void) close(STDERR_FILENO); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate (void) setpgrp(); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /* open the console for sulogin */ 2567c478bd9Sstevel@tonic-gate if ((fd_console = open("/dev/console", O_RDWR)) >= 0) { 2577c478bd9Sstevel@tonic-gate if (fd_console != STDIN_FILENO) 2587c478bd9Sstevel@tonic-gate while (dup2(fd_console, STDIN_FILENO) < 0 && 2597c478bd9Sstevel@tonic-gate errno == EINTR) 2607c478bd9Sstevel@tonic-gate ; 2617c478bd9Sstevel@tonic-gate if (fd_console != STDOUT_FILENO) 2627c478bd9Sstevel@tonic-gate while (dup2(fd_console, STDOUT_FILENO) < 0 && 2637c478bd9Sstevel@tonic-gate errno == EINTR) 2647c478bd9Sstevel@tonic-gate ; 2657c478bd9Sstevel@tonic-gate if (fd_console != STDERR_FILENO) 2667c478bd9Sstevel@tonic-gate while (dup2(fd_console, STDERR_FILENO) < 0 && 2677c478bd9Sstevel@tonic-gate errno == EINTR) 2687c478bd9Sstevel@tonic-gate ; 269*2f1b831aSacruz if (fd_console > STDERR_FILENO) 2707c478bd9Sstevel@tonic-gate (void) close(fd_console); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 27376cf44abSjeanm setutxent(); 27476cf44abSjeanm while ((utmpp = getutxent()) != NULL) { 27576cf44abSjeanm if (strcmp(utmpp->ut_user, "LOGIN") != 0) { 27676cf44abSjeanm if (strcmp(utmpp->ut_line, "console") == 0) { 27776cf44abSjeanm (void) kill(utmpp->ut_pid, 9); 27876cf44abSjeanm break; 27976cf44abSjeanm } 28076cf44abSjeanm } 28176cf44abSjeanm } 28276cf44abSjeanm 2837c478bd9Sstevel@tonic-gate (void) execl("/sbin/sulogin", "sulogin", NULL); 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate uu_warn("Could not exec() sulogin"); 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate exit(1); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate #define CONFIGD_PATH "/lib/svc/bin/svc.configd" 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* 2937c478bd9Sstevel@tonic-gate * void fork_configd(int status) 2947c478bd9Sstevel@tonic-gate * We are interested in exit events (since the parent's exiting means configd 2957c478bd9Sstevel@tonic-gate * is ready to run and since the child's exiting indicates an error case) and 2967c478bd9Sstevel@tonic-gate * in empty events. This means we have a unique template for initiating 2977c478bd9Sstevel@tonic-gate * configd. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3007c478bd9Sstevel@tonic-gate void 3017c478bd9Sstevel@tonic-gate fork_configd(int exitstatus) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate pid_t pid; 3047c478bd9Sstevel@tonic-gate ctid_t ctid = -1; 3057c478bd9Sstevel@tonic-gate int err; 3067c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate retry: 3097c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "fork_configd trying to start svc.configd\n"); 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate /* 3127c478bd9Sstevel@tonic-gate * If we're retrying, we will have an old contract lying around 3137c478bd9Sstevel@tonic-gate * from the failure. Since we're going to be creating a new 3147c478bd9Sstevel@tonic-gate * contract shortly, we abandon the old one now. 3157c478bd9Sstevel@tonic-gate */ 3167c478bd9Sstevel@tonic-gate if (ctid != -1) 3177c478bd9Sstevel@tonic-gate contract_abandon(ctid); 3187c478bd9Sstevel@tonic-gate ctid = -1; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate pid = fork_common("svc.configd", MAX_CONFIGD_RETRIES, &ctid, 3217c478bd9Sstevel@tonic-gate 0, CT_PR_EV_EXIT, 0, CT_PR_INHERIT | CT_PR_REGENT, CONFIGD_COOKIE); 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate if (pid != 0) { 3247c478bd9Sstevel@tonic-gate int exitstatus; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate st->st_configd_pid = pid; 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate if (waitpid(pid, &exitstatus, 0) == -1) { 3297c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "waitpid on svc.configd " 3307c478bd9Sstevel@tonic-gate "failed: %s\n", strerror(errno)); 3317c478bd9Sstevel@tonic-gate } else if (WIFEXITED(exitstatus)) { 3327c478bd9Sstevel@tonic-gate char *errstr; 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate /* 3357c478bd9Sstevel@tonic-gate * Examine exitstatus. This will eventually get more 3367c478bd9Sstevel@tonic-gate * complicated, as we will want to teach startd how to 3377c478bd9Sstevel@tonic-gate * invoke configd with alternate repositories, etc. 3387c478bd9Sstevel@tonic-gate * 3397c478bd9Sstevel@tonic-gate * Note that exec(2) failure results in an exit status 3407c478bd9Sstevel@tonic-gate * of 1, resulting in the default clause below. 3417c478bd9Sstevel@tonic-gate */ 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate /* 3447c478bd9Sstevel@tonic-gate * Assign readable strings to cases we don't handle, or 3457c478bd9Sstevel@tonic-gate * have error outcomes that cannot be eliminated. 3467c478bd9Sstevel@tonic-gate */ 3477c478bd9Sstevel@tonic-gate switch (WEXITSTATUS(exitstatus)) { 3487c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_BAD_ARGS: 3497c478bd9Sstevel@tonic-gate errstr = "bad arguments"; 3507c478bd9Sstevel@tonic-gate break; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_BAD: 3537c478bd9Sstevel@tonic-gate errstr = "database corrupt"; 3547c478bd9Sstevel@tonic-gate break; 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_LOCKED: 3577c478bd9Sstevel@tonic-gate errstr = "database locked"; 3587c478bd9Sstevel@tonic-gate break; 3597c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_INIT_FAILED: 3607c478bd9Sstevel@tonic-gate errstr = "initialization failure"; 3617c478bd9Sstevel@tonic-gate break; 3627c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DOOR_INIT_FAILED: 3637c478bd9Sstevel@tonic-gate errstr = "door initialization failure"; 3647c478bd9Sstevel@tonic-gate break; 3657c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_INIT_FAILED: 3667c478bd9Sstevel@tonic-gate errstr = "database initialization failure"; 3677c478bd9Sstevel@tonic-gate break; 3687c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_NO_THREADS: 3697c478bd9Sstevel@tonic-gate errstr = "no threads available"; 3707c478bd9Sstevel@tonic-gate break; 3717c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_LOST_MAIN_DOOR: 3727c478bd9Sstevel@tonic-gate errstr = "lost door server attachment"; 3737c478bd9Sstevel@tonic-gate break; 3747c478bd9Sstevel@tonic-gate case 1: 3757c478bd9Sstevel@tonic-gate errstr = "execution failure"; 3767c478bd9Sstevel@tonic-gate break; 3777c478bd9Sstevel@tonic-gate default: 3787c478bd9Sstevel@tonic-gate errstr = "unknown error"; 3797c478bd9Sstevel@tonic-gate break; 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate /* 3837c478bd9Sstevel@tonic-gate * Remedial actions for various configd failures. 3847c478bd9Sstevel@tonic-gate */ 3857c478bd9Sstevel@tonic-gate switch (WEXITSTATUS(exitstatus)) { 3867c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_OKAY: 3877c478bd9Sstevel@tonic-gate break; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_LOCKED: 3907c478bd9Sstevel@tonic-gate /* attempt remount of / read-write */ 3917c478bd9Sstevel@tonic-gate if (fs_is_read_only("/", NULL) == 1) { 3927c478bd9Sstevel@tonic-gate if (fs_remount("/") == -1) 3937c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, 3947c478bd9Sstevel@tonic-gate "remount of root " 3957c478bd9Sstevel@tonic-gate "filesystem failed\n"); 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate goto retry; 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate break; 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate default: 4027c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "svc.configd exited " 4037c478bd9Sstevel@tonic-gate "with status %d (%s)\n", 4047c478bd9Sstevel@tonic-gate WEXITSTATUS(exitstatus), errstr); 4057c478bd9Sstevel@tonic-gate goto retry; 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(exitstatus)) { 4087c478bd9Sstevel@tonic-gate char signame[SIG2STR_MAX]; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate if (sig2str(WTERMSIG(exitstatus), signame)) 4117c478bd9Sstevel@tonic-gate (void) snprintf(signame, SIG2STR_MAX, 4127c478bd9Sstevel@tonic-gate "signum %d", WTERMSIG(exitstatus)); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "svc.configd signalled:" 4157c478bd9Sstevel@tonic-gate " %s\n", signame); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate goto retry; 4187c478bd9Sstevel@tonic-gate } else { 4197c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "svc.configd non-exit " 4207c478bd9Sstevel@tonic-gate "condition: 0x%x\n", exitstatus); 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate goto retry; 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate /* 4267c478bd9Sstevel@tonic-gate * Announce that we have a valid svc.configd status. 4277c478bd9Sstevel@tonic-gate */ 4287c478bd9Sstevel@tonic-gate MUTEX_LOCK(&st->st_configd_live_lock); 4297c478bd9Sstevel@tonic-gate st->st_configd_lives = 1; 4307c478bd9Sstevel@tonic-gate err = pthread_cond_broadcast(&st->st_configd_live_cv); 4317c478bd9Sstevel@tonic-gate assert(err == 0); 4327c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_configd_live_lock); 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "fork_configd broadcasts configd is " 4357c478bd9Sstevel@tonic-gate "live\n"); 4367c478bd9Sstevel@tonic-gate return; 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * Set our per-process core file path to leave core files in 4417c478bd9Sstevel@tonic-gate * /etc/svc/volatile directory, named after the PID to aid in debugging. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), 4447c478bd9Sstevel@tonic-gate "/etc/svc/volatile/core.configd.%%p"); 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate (void) core_set_process_path(path, strlen(path) + 1, getpid()); 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "executing svc.configd\n"); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate (void) execl(CONFIGD_PATH, CONFIGD_PATH, NULL); 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate /* 4537c478bd9Sstevel@tonic-gate * Status code is used above to identify configd exec failure. 4547c478bd9Sstevel@tonic-gate */ 4557c478bd9Sstevel@tonic-gate exit(1); 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate void * 4597c478bd9Sstevel@tonic-gate fork_configd_thread(void *vctid) 4607c478bd9Sstevel@tonic-gate { 4617c478bd9Sstevel@tonic-gate int fd, err; 4627c478bd9Sstevel@tonic-gate ctid_t configd_ctid = (ctid_t)vctid; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate if (configd_ctid == -1) { 4657c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 4667c478bd9Sstevel@tonic-gate "fork_configd_thread starting svc.configd\n"); 4677c478bd9Sstevel@tonic-gate fork_configd(0); 4687c478bd9Sstevel@tonic-gate } else { 4697c478bd9Sstevel@tonic-gate /* 4707c478bd9Sstevel@tonic-gate * configd_ctid is known: we broadcast and continue. 4717c478bd9Sstevel@tonic-gate * test contract for appropriate state by verifying that 4727c478bd9Sstevel@tonic-gate * there is one or more processes within it? 4737c478bd9Sstevel@tonic-gate */ 4747c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 4757c478bd9Sstevel@tonic-gate "fork_configd_thread accepting svc.configd with CTID %ld\n", 4767c478bd9Sstevel@tonic-gate configd_ctid); 4777c478bd9Sstevel@tonic-gate MUTEX_LOCK(&st->st_configd_live_lock); 4787c478bd9Sstevel@tonic-gate st->st_configd_lives = 1; 4797c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&st->st_configd_live_cv); 4807c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_configd_live_lock); 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY); 4847c478bd9Sstevel@tonic-gate if (fd == -1) 4857c478bd9Sstevel@tonic-gate uu_die("process bundle open failed"); 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate * Make sure we get all events (including those generated by configd 4897c478bd9Sstevel@tonic-gate * before this thread was started). 4907c478bd9Sstevel@tonic-gate */ 4917c478bd9Sstevel@tonic-gate err = ct_event_reset(fd); 4927c478bd9Sstevel@tonic-gate assert(err == 0); 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate for (;;) { 4957c478bd9Sstevel@tonic-gate int efd, sfd; 4967c478bd9Sstevel@tonic-gate ct_evthdl_t ev; 4977c478bd9Sstevel@tonic-gate uint32_t type; 4987c478bd9Sstevel@tonic-gate ctevid_t evid; 4997c478bd9Sstevel@tonic-gate ct_stathdl_t status; 5007c478bd9Sstevel@tonic-gate ctid_t ctid; 5017c478bd9Sstevel@tonic-gate uint64_t cookie; 5027c478bd9Sstevel@tonic-gate pid_t pid; 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate if (err = ct_event_read_critical(fd, &ev)) { 5057c478bd9Sstevel@tonic-gate assert(err != EINVAL && err != EAGAIN); 5067c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, 5077c478bd9Sstevel@tonic-gate "Error reading next contract event: %s", 5087c478bd9Sstevel@tonic-gate strerror(err)); 5097c478bd9Sstevel@tonic-gate continue; 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate evid = ct_event_get_evid(ev); 5137c478bd9Sstevel@tonic-gate ctid = ct_event_get_ctid(ev); 5147c478bd9Sstevel@tonic-gate type = ct_event_get_type(ev); 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate /* Fetch cookie. */ 5177c478bd9Sstevel@tonic-gate sfd = contract_open(ctid, "process", "status", O_RDONLY); 5187c478bd9Sstevel@tonic-gate if (sfd < 0) { 5197c478bd9Sstevel@tonic-gate ct_event_free(ev); 5207c478bd9Sstevel@tonic-gate continue; 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate if (err = ct_status_read(sfd, CTD_COMMON, &status)) { 5247c478bd9Sstevel@tonic-gate log_framework(LOG_WARNING, "Could not get status for " 5257c478bd9Sstevel@tonic-gate "contract %ld: %s\n", ctid, strerror(err)); 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate ct_event_free(ev); 5287c478bd9Sstevel@tonic-gate startd_close(sfd); 5297c478bd9Sstevel@tonic-gate continue; 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate cookie = ct_status_get_cookie(status); 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate ct_status_free(status); 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate startd_close(sfd); 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate /* 5397c478bd9Sstevel@tonic-gate * Don't process events from contracts we aren't interested in. 5407c478bd9Sstevel@tonic-gate */ 5417c478bd9Sstevel@tonic-gate if (cookie != CONFIGD_COOKIE) { 5427c478bd9Sstevel@tonic-gate ct_event_free(ev); 5437c478bd9Sstevel@tonic-gate continue; 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate if (type == CT_PR_EV_EXIT) { 5477c478bd9Sstevel@tonic-gate int exitstatus; 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate (void) ct_pr_event_get_pid(ev, &pid); 5507c478bd9Sstevel@tonic-gate (void) ct_pr_event_get_exitstatus(ev, 5517c478bd9Sstevel@tonic-gate &exitstatus); 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate if (st->st_configd_pid != pid) { 5547c478bd9Sstevel@tonic-gate /* 5557c478bd9Sstevel@tonic-gate * This is the child exiting, so we 5567c478bd9Sstevel@tonic-gate * abandon the contract and restart 5577c478bd9Sstevel@tonic-gate * configd. 5587c478bd9Sstevel@tonic-gate */ 5597c478bd9Sstevel@tonic-gate contract_abandon(ctid); 5607c478bd9Sstevel@tonic-gate fork_configd(exitstatus); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate efd = contract_open(ctid, "process", "ctl", O_WRONLY); 5657c478bd9Sstevel@tonic-gate if (efd != -1) { 5667c478bd9Sstevel@tonic-gate (void) ct_ctl_ack(efd, evid); 5677c478bd9Sstevel@tonic-gate startd_close(efd); 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate ct_event_free(ev); 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 5757c478bd9Sstevel@tonic-gate return (NULL); 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate void 5797c478bd9Sstevel@tonic-gate fork_rc_script(char rl, const char *arg, boolean_t wait) 5807c478bd9Sstevel@tonic-gate { 5817c478bd9Sstevel@tonic-gate pid_t pid; 5827c478bd9Sstevel@tonic-gate int tmpl, err, stat; 5837c478bd9Sstevel@tonic-gate char path[20] = "/sbin/rc.", log[20] = "rc..log", timebuf[20]; 5847c478bd9Sstevel@tonic-gate time_t now; 5857c478bd9Sstevel@tonic-gate struct tm ltime; 5867c478bd9Sstevel@tonic-gate size_t sz; 5877c478bd9Sstevel@tonic-gate char *pathenv; 5887c478bd9Sstevel@tonic-gate char **nenv; 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate path[8] = rl; 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate tmpl = open64(CTFS_ROOT "/process/template", O_RDWR); 5937c478bd9Sstevel@tonic-gate if (tmpl >= 0) { 5947c478bd9Sstevel@tonic-gate err = ct_tmpl_set_critical(tmpl, 0); 5957c478bd9Sstevel@tonic-gate assert(err == 0); 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate err = ct_tmpl_set_informative(tmpl, 0); 5987c478bd9Sstevel@tonic-gate assert(err == 0); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate err = ct_pr_tmpl_set_fatal(tmpl, 0); 6017c478bd9Sstevel@tonic-gate assert(err == 0); 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate err = ct_tmpl_activate(tmpl); 6047c478bd9Sstevel@tonic-gate assert(err == 0); 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate err = close(tmpl); 6077c478bd9Sstevel@tonic-gate assert(err == 0); 6087c478bd9Sstevel@tonic-gate } else { 6097c478bd9Sstevel@tonic-gate uu_warn("Could not create contract template for %s.\n", path); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate pid = startd_fork1(NULL); 6137c478bd9Sstevel@tonic-gate if (pid < 0) { 6147c478bd9Sstevel@tonic-gate return; 6157c478bd9Sstevel@tonic-gate } else if (pid != 0) { 6167c478bd9Sstevel@tonic-gate /* parent */ 6177c478bd9Sstevel@tonic-gate if (wait) { 6187c478bd9Sstevel@tonic-gate do 6197c478bd9Sstevel@tonic-gate err = waitpid(pid, &stat, 0); 62076cf44abSjeanm while (err != 0 && errno == EINTR) 62176cf44abSjeanm ; 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate if (!WIFEXITED(stat)) { 6247c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, 6257c478bd9Sstevel@tonic-gate "%s terminated with waitpid() status %d.\n", 6267c478bd9Sstevel@tonic-gate path, stat); 6277c478bd9Sstevel@tonic-gate } else if (WEXITSTATUS(stat) != 0) { 6287c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, 6297c478bd9Sstevel@tonic-gate "%s failed with status %d.\n", path, 6307c478bd9Sstevel@tonic-gate WEXITSTATUS(stat)); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate return; 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate /* child */ 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate log[2] = rl; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate setlog(log); 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate now = time(NULL); 6447c478bd9Sstevel@tonic-gate sz = strftime(timebuf, sizeof (timebuf), "%b %e %T", 6457c478bd9Sstevel@tonic-gate localtime_r(&now, <ime)); 6467c478bd9Sstevel@tonic-gate assert(sz != 0); 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s Executing %s %s\n", timebuf, path, arg); 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate if (rl == 'S') 6517c478bd9Sstevel@tonic-gate pathenv = "PATH=/sbin:/usr/sbin:/usr/bin"; 6527c478bd9Sstevel@tonic-gate else 6537c478bd9Sstevel@tonic-gate pathenv = "PATH=/usr/sbin:/usr/bin"; 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate nenv = set_smf_env(NULL, 0, pathenv, NULL, NULL); 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate (void) execle(path, path, arg, 0, nenv); 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate perror("exec"); 6607c478bd9Sstevel@tonic-gate exit(0); 6617c478bd9Sstevel@tonic-gate } 662