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