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 /* 22*9444c26fSTom Whitten * Copyright 2010 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> 474d53c7adSDan Price #include <poll.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> 564d53c7adSDan Price #include <spawn.h> 577c478bd9Sstevel@tonic-gate 58*9444c26fSTom Whitten #include "manifest_hash.h" 597c478bd9Sstevel@tonic-gate #include "configd_exit.h" 607c478bd9Sstevel@tonic-gate #include "protocol.h" 617c478bd9Sstevel@tonic-gate #include "startd.h" 627c478bd9Sstevel@tonic-gate 6376cf44abSjeanm static struct utmpx *utmpp; /* pointer for getutxent() */ 6476cf44abSjeanm 657c478bd9Sstevel@tonic-gate pid_t 667c478bd9Sstevel@tonic-gate startd_fork1(int *forkerr) 677c478bd9Sstevel@tonic-gate { 687c478bd9Sstevel@tonic-gate pid_t p; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate /* 717c478bd9Sstevel@tonic-gate * prefork stack 727c478bd9Sstevel@tonic-gate */ 737c478bd9Sstevel@tonic-gate wait_prefork(); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate p = fork1(); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate if (p == -1 && forkerr != NULL) 787c478bd9Sstevel@tonic-gate *forkerr = errno; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * postfork stack 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate wait_postfork(p); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate return (p); 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * void fork_mount(char *, char *) 907c478bd9Sstevel@tonic-gate * Run mount(1M) with the given options and mount point. (mount(1M) has much 917c478bd9Sstevel@tonic-gate * hidden knowledge; it's much less correct to reimplement that logic here to 927c478bd9Sstevel@tonic-gate * save a fork(2)/exec(2) invocation.) 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate int 957c478bd9Sstevel@tonic-gate fork_mount(char *path, char *opts) 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate pid_t pid; 987c478bd9Sstevel@tonic-gate uint_t tries = 0; 997c478bd9Sstevel@tonic-gate int status; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate for (pid = fork1(); pid == -1; pid = fork1()) { 1027c478bd9Sstevel@tonic-gate if (++tries > MAX_MOUNT_RETRIES) 1037c478bd9Sstevel@tonic-gate return (-1); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate (void) sleep(tries); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate if (pid != 0) { 1097c478bd9Sstevel@tonic-gate (void) waitpid(pid, &status, 0); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * If our mount(1M) invocation exited by peculiar means, or with 1137c478bd9Sstevel@tonic-gate * a non-zero status, our mount likelihood is low. 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate if (!WIFEXITED(status) || 1167c478bd9Sstevel@tonic-gate WEXITSTATUS(status) != 0) 1177c478bd9Sstevel@tonic-gate return (-1); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate return (0); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate (void) execl("/sbin/mount", "mount", "-o", opts, path, NULL); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate return (-1); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * pid_t fork_common(...) 129*9444c26fSTom Whitten * Common routine used by fork_sulogin, fork_emi, and fork_configd to 130*9444c26fSTom Whitten * fork a process in a contract with the provided terms. Invokes 1317c478bd9Sstevel@tonic-gate * fork_sulogin (with its no-fork argument set) on errors. 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate static pid_t 1347b209c2cSacruz fork_common(const char *name, const char *svc_fmri, int retries, ctid_t *ctidp, 1357c478bd9Sstevel@tonic-gate uint_t inf, uint_t crit, uint_t fatal, uint_t param, uint64_t cookie) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate uint_t tries = 0; 1387c478bd9Sstevel@tonic-gate int ctfd, err; 1397c478bd9Sstevel@tonic-gate pid_t pid; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Establish process contract terms. 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate if ((ctfd = open64(CTFS_ROOT "/process/template", O_RDWR)) == -1) { 1457c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not open process contract template " 1467c478bd9Sstevel@tonic-gate "for %s: %s\n", name, strerror(errno)); 1477c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate err = ct_tmpl_set_critical(ctfd, crit); 1517c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(ctfd, fatal); 1527c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_informative(ctfd, inf); 1537c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(ctfd, param); 1547c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_cookie(ctfd, cookie); 1557b209c2cSacruz err |= ct_pr_tmpl_set_svc_fmri(ctfd, svc_fmri); 1567b209c2cSacruz err |= ct_pr_tmpl_set_svc_aux(ctfd, name); 1577c478bd9Sstevel@tonic-gate if (err) { 1587c478bd9Sstevel@tonic-gate (void) close(ctfd); 1597c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not set %s process contract " 1607c478bd9Sstevel@tonic-gate "terms\n", name); 1617c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate if (err = ct_tmpl_activate(ctfd)) { 1657c478bd9Sstevel@tonic-gate (void) close(ctfd); 1667c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not activate %s process contract " 1677c478bd9Sstevel@tonic-gate "template: %s\n", name, strerror(err)); 1687c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * Attempt to fork "retries" times. 1737c478bd9Sstevel@tonic-gate */ 1747c478bd9Sstevel@tonic-gate for (pid = fork1(); pid == -1; pid = fork1()) { 1757c478bd9Sstevel@tonic-gate if (++tries > retries) { 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * When we exit the sulogin session, init(1M) 1787c478bd9Sstevel@tonic-gate * will restart svc.startd(1M). 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate err = errno; 1817c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(ctfd); 1827c478bd9Sstevel@tonic-gate (void) close(ctfd); 1837c478bd9Sstevel@tonic-gate fork_sulogin(B_TRUE, "Could not fork to start %s: %s\n", 1847c478bd9Sstevel@tonic-gate name, strerror(err)); 1857c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate (void) sleep(tries); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* 1917c478bd9Sstevel@tonic-gate * Clean up, return pid and ctid. 1927c478bd9Sstevel@tonic-gate */ 1937c478bd9Sstevel@tonic-gate if (pid != 0 && (errno = contract_latest(ctidp)) != 0) 1947c478bd9Sstevel@tonic-gate uu_die("Could not get new contract id for %s\n", name); 1957c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(ctfd); 1967c478bd9Sstevel@tonic-gate (void) close(ctfd); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate return (pid); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* 2027c478bd9Sstevel@tonic-gate * void fork_sulogin(boolean_t, const char *, ...) 2037c478bd9Sstevel@tonic-gate * When we are invoked with the -s flag from boot (or run into an unfixable 2047c478bd9Sstevel@tonic-gate * situation), we run a private copy of sulogin. When the sulogin session 2057c478bd9Sstevel@tonic-gate * is ended, we continue. This is the last fallback action for system 2067c478bd9Sstevel@tonic-gate * maintenance. 2077c478bd9Sstevel@tonic-gate * 2087c478bd9Sstevel@tonic-gate * If immediate is true, fork_sulogin() executes sulogin(1M) directly, without 2097c478bd9Sstevel@tonic-gate * forking. 2107c478bd9Sstevel@tonic-gate * 2117c478bd9Sstevel@tonic-gate * Because fork_sulogin() is needed potentially before we daemonize, we leave 2127c478bd9Sstevel@tonic-gate * it outside the wait_register() framework. 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 2157c478bd9Sstevel@tonic-gate void 2167c478bd9Sstevel@tonic-gate fork_sulogin(boolean_t immediate, const char *format, ...) 2177c478bd9Sstevel@tonic-gate { 2187c478bd9Sstevel@tonic-gate va_list args; 2192f1b831aSacruz int fd_console; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate (void) printf("Requesting System Maintenance Mode\n"); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (!booting_to_single_user) 2247c478bd9Sstevel@tonic-gate (void) printf("(See /lib/svc/share/README for more " 2257c478bd9Sstevel@tonic-gate "information.)\n"); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate va_start(args, format); 2287c478bd9Sstevel@tonic-gate (void) vprintf(format, args); 2297c478bd9Sstevel@tonic-gate va_end(args); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate if (!immediate) { 2327c478bd9Sstevel@tonic-gate ctid_t ctid; 2337c478bd9Sstevel@tonic-gate pid_t pid; 2347c478bd9Sstevel@tonic-gate 2357b209c2cSacruz pid = fork_common("sulogin", SVC_SULOGIN_FMRI, 2367b209c2cSacruz MAX_SULOGIN_RETRIES, &ctid, CT_PR_EV_HWERR, 0, 2377b209c2cSacruz CT_PR_EV_HWERR, CT_PR_PGRPONLY, SULOGIN_COOKIE); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (pid != 0) { 2407c478bd9Sstevel@tonic-gate (void) waitpid(pid, NULL, 0); 2417c478bd9Sstevel@tonic-gate contract_abandon(ctid); 2427c478bd9Sstevel@tonic-gate return; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate /* close all inherited fds */ 2457c478bd9Sstevel@tonic-gate closefrom(0); 2467c478bd9Sstevel@tonic-gate } else { 2477c478bd9Sstevel@tonic-gate (void) printf("Directly executing sulogin.\n"); 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * Can't call closefrom() in this MT section 2507c478bd9Sstevel@tonic-gate * so safely close a minimum set of fds. 2517c478bd9Sstevel@tonic-gate */ 2522f1b831aSacruz (void) close(STDIN_FILENO); 2532f1b831aSacruz (void) close(STDOUT_FILENO); 2542f1b831aSacruz (void) close(STDERR_FILENO); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate (void) setpgrp(); 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate /* open the console for sulogin */ 2607c478bd9Sstevel@tonic-gate if ((fd_console = open("/dev/console", O_RDWR)) >= 0) { 2617c478bd9Sstevel@tonic-gate if (fd_console != STDIN_FILENO) 2627c478bd9Sstevel@tonic-gate while (dup2(fd_console, STDIN_FILENO) < 0 && 2637c478bd9Sstevel@tonic-gate errno == EINTR) 2647c478bd9Sstevel@tonic-gate ; 2657c478bd9Sstevel@tonic-gate if (fd_console != STDOUT_FILENO) 2667c478bd9Sstevel@tonic-gate while (dup2(fd_console, STDOUT_FILENO) < 0 && 2677c478bd9Sstevel@tonic-gate errno == EINTR) 2687c478bd9Sstevel@tonic-gate ; 2697c478bd9Sstevel@tonic-gate if (fd_console != STDERR_FILENO) 2707c478bd9Sstevel@tonic-gate while (dup2(fd_console, STDERR_FILENO) < 0 && 2717c478bd9Sstevel@tonic-gate errno == EINTR) 2727c478bd9Sstevel@tonic-gate ; 2732f1b831aSacruz if (fd_console > STDERR_FILENO) 2747c478bd9Sstevel@tonic-gate (void) close(fd_console); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 27776cf44abSjeanm setutxent(); 27876cf44abSjeanm while ((utmpp = getutxent()) != NULL) { 27976cf44abSjeanm if (strcmp(utmpp->ut_user, "LOGIN") != 0) { 28076cf44abSjeanm if (strcmp(utmpp->ut_line, "console") == 0) { 28176cf44abSjeanm (void) kill(utmpp->ut_pid, 9); 28276cf44abSjeanm break; 28376cf44abSjeanm } 28476cf44abSjeanm } 28576cf44abSjeanm } 28676cf44abSjeanm 2877c478bd9Sstevel@tonic-gate (void) execl("/sbin/sulogin", "sulogin", NULL); 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate uu_warn("Could not exec() sulogin"); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate exit(1); 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate #define CONFIGD_PATH "/lib/svc/bin/svc.configd" 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * void fork_configd(int status) 2987c478bd9Sstevel@tonic-gate * We are interested in exit events (since the parent's exiting means configd 2997c478bd9Sstevel@tonic-gate * is ready to run and since the child's exiting indicates an error case) and 3007c478bd9Sstevel@tonic-gate * in empty events. This means we have a unique template for initiating 3017c478bd9Sstevel@tonic-gate * configd. 3027c478bd9Sstevel@tonic-gate */ 3037c478bd9Sstevel@tonic-gate void 3047c478bd9Sstevel@tonic-gate fork_configd(int exitstatus) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate pid_t pid; 3077c478bd9Sstevel@tonic-gate ctid_t ctid = -1; 3087c478bd9Sstevel@tonic-gate int err; 3097c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 3107c478bd9Sstevel@tonic-gate 3114362b902SSean Wilcox /* 3124362b902SSean Wilcox * Checking the existatus for the potential failure of the 3134362b902SSean Wilcox * daemonized svc.configd. If this is not the first time 3144362b902SSean Wilcox * through, but a call from the svc.configd monitoring thread 3154362b902SSean Wilcox * after a failure this is the status that is expected. Other 3164362b902SSean Wilcox * failures are exposed during initialization or are fixed 3174362b902SSean Wilcox * by a restart (e.g door closings). 3184362b902SSean Wilcox * 3194362b902SSean Wilcox * If this is on-disk database corruption it will also be 3204362b902SSean Wilcox * caught by a restart but could be cleared before the restart. 3214362b902SSean Wilcox * 3224362b902SSean Wilcox * Or this could be internal database corruption due to a 3234362b902SSean Wilcox * rogue service that needs to be cleared before restart. 3244362b902SSean Wilcox */ 3254362b902SSean Wilcox if (WEXITSTATUS(exitstatus) == CONFIGD_EXIT_DATABASE_BAD) { 3264362b902SSean Wilcox fork_sulogin(B_FALSE, "svc.configd exited with database " 3274362b902SSean Wilcox "corrupt error after initialization of the repository\n"); 3284362b902SSean Wilcox } 3294362b902SSean Wilcox 3307c478bd9Sstevel@tonic-gate retry: 3317c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "fork_configd trying to start svc.configd\n"); 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * If we're retrying, we will have an old contract lying around 3357c478bd9Sstevel@tonic-gate * from the failure. Since we're going to be creating a new 3367c478bd9Sstevel@tonic-gate * contract shortly, we abandon the old one now. 3377c478bd9Sstevel@tonic-gate */ 3387c478bd9Sstevel@tonic-gate if (ctid != -1) 3397c478bd9Sstevel@tonic-gate contract_abandon(ctid); 3407c478bd9Sstevel@tonic-gate ctid = -1; 3417c478bd9Sstevel@tonic-gate 3427b209c2cSacruz pid = fork_common("svc.configd", SCF_SERVICE_CONFIGD, 3437b209c2cSacruz MAX_CONFIGD_RETRIES, &ctid, 0, CT_PR_EV_EXIT, 0, 3447b209c2cSacruz CT_PR_INHERIT | CT_PR_REGENT, CONFIGD_COOKIE); 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate if (pid != 0) { 3477c478bd9Sstevel@tonic-gate int exitstatus; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate st->st_configd_pid = pid; 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate if (waitpid(pid, &exitstatus, 0) == -1) { 3527c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "waitpid on svc.configd " 3537c478bd9Sstevel@tonic-gate "failed: %s\n", strerror(errno)); 3547c478bd9Sstevel@tonic-gate } else if (WIFEXITED(exitstatus)) { 3557c478bd9Sstevel@tonic-gate char *errstr; 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* 3587c478bd9Sstevel@tonic-gate * Examine exitstatus. This will eventually get more 3597c478bd9Sstevel@tonic-gate * complicated, as we will want to teach startd how to 3607c478bd9Sstevel@tonic-gate * invoke configd with alternate repositories, etc. 3617c478bd9Sstevel@tonic-gate * 3627c478bd9Sstevel@tonic-gate * Note that exec(2) failure results in an exit status 3637c478bd9Sstevel@tonic-gate * of 1, resulting in the default clause below. 3647c478bd9Sstevel@tonic-gate */ 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * Assign readable strings to cases we don't handle, or 3687c478bd9Sstevel@tonic-gate * have error outcomes that cannot be eliminated. 3697c478bd9Sstevel@tonic-gate */ 3707c478bd9Sstevel@tonic-gate switch (WEXITSTATUS(exitstatus)) { 3717c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_BAD_ARGS: 3727c478bd9Sstevel@tonic-gate errstr = "bad arguments"; 3737c478bd9Sstevel@tonic-gate break; 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_BAD: 3767c478bd9Sstevel@tonic-gate errstr = "database corrupt"; 3777c478bd9Sstevel@tonic-gate break; 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_LOCKED: 3807c478bd9Sstevel@tonic-gate errstr = "database locked"; 3817c478bd9Sstevel@tonic-gate break; 3827c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_INIT_FAILED: 3837c478bd9Sstevel@tonic-gate errstr = "initialization failure"; 3847c478bd9Sstevel@tonic-gate break; 3857c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DOOR_INIT_FAILED: 3867c478bd9Sstevel@tonic-gate errstr = "door initialization failure"; 3877c478bd9Sstevel@tonic-gate break; 3887c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_INIT_FAILED: 3897c478bd9Sstevel@tonic-gate errstr = "database initialization failure"; 3907c478bd9Sstevel@tonic-gate break; 3917c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_NO_THREADS: 3927c478bd9Sstevel@tonic-gate errstr = "no threads available"; 3937c478bd9Sstevel@tonic-gate break; 3947c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_LOST_MAIN_DOOR: 3957c478bd9Sstevel@tonic-gate errstr = "lost door server attachment"; 3967c478bd9Sstevel@tonic-gate break; 3977c478bd9Sstevel@tonic-gate case 1: 3987c478bd9Sstevel@tonic-gate errstr = "execution failure"; 3997c478bd9Sstevel@tonic-gate break; 4007c478bd9Sstevel@tonic-gate default: 4017c478bd9Sstevel@tonic-gate errstr = "unknown error"; 4027c478bd9Sstevel@tonic-gate break; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate /* 4067c478bd9Sstevel@tonic-gate * Remedial actions for various configd failures. 4077c478bd9Sstevel@tonic-gate */ 4087c478bd9Sstevel@tonic-gate switch (WEXITSTATUS(exitstatus)) { 4097c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_OKAY: 4107c478bd9Sstevel@tonic-gate break; 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate case CONFIGD_EXIT_DATABASE_LOCKED: 4137c478bd9Sstevel@tonic-gate /* attempt remount of / read-write */ 4147c478bd9Sstevel@tonic-gate if (fs_is_read_only("/", NULL) == 1) { 4157c478bd9Sstevel@tonic-gate if (fs_remount("/") == -1) 4167c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, 4177c478bd9Sstevel@tonic-gate "remount of root " 4187c478bd9Sstevel@tonic-gate "filesystem failed\n"); 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate goto retry; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate break; 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate default: 4257c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "svc.configd exited " 4267c478bd9Sstevel@tonic-gate "with status %d (%s)\n", 4277c478bd9Sstevel@tonic-gate WEXITSTATUS(exitstatus), errstr); 4287c478bd9Sstevel@tonic-gate goto retry; 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(exitstatus)) { 4317c478bd9Sstevel@tonic-gate char signame[SIG2STR_MAX]; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate if (sig2str(WTERMSIG(exitstatus), signame)) 4347c478bd9Sstevel@tonic-gate (void) snprintf(signame, SIG2STR_MAX, 4357c478bd9Sstevel@tonic-gate "signum %d", WTERMSIG(exitstatus)); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "svc.configd signalled:" 4387c478bd9Sstevel@tonic-gate " %s\n", signame); 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate goto retry; 4417c478bd9Sstevel@tonic-gate } else { 4427c478bd9Sstevel@tonic-gate fork_sulogin(B_FALSE, "svc.configd non-exit " 4437c478bd9Sstevel@tonic-gate "condition: 0x%x\n", exitstatus); 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate goto retry; 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate /* 4497c478bd9Sstevel@tonic-gate * Announce that we have a valid svc.configd status. 4507c478bd9Sstevel@tonic-gate */ 4517c478bd9Sstevel@tonic-gate MUTEX_LOCK(&st->st_configd_live_lock); 4527c478bd9Sstevel@tonic-gate st->st_configd_lives = 1; 4537c478bd9Sstevel@tonic-gate err = pthread_cond_broadcast(&st->st_configd_live_cv); 4547c478bd9Sstevel@tonic-gate assert(err == 0); 4557c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_configd_live_lock); 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "fork_configd broadcasts configd is " 4587c478bd9Sstevel@tonic-gate "live\n"); 4597c478bd9Sstevel@tonic-gate return; 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate /* 4637c478bd9Sstevel@tonic-gate * Set our per-process core file path to leave core files in 4647c478bd9Sstevel@tonic-gate * /etc/svc/volatile directory, named after the PID to aid in debugging. 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), 4677c478bd9Sstevel@tonic-gate "/etc/svc/volatile/core.configd.%%p"); 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate (void) core_set_process_path(path, strlen(path) + 1, getpid()); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "executing svc.configd\n"); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate (void) execl(CONFIGD_PATH, CONFIGD_PATH, NULL); 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * Status code is used above to identify configd exec failure. 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate exit(1); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate void * 4827c478bd9Sstevel@tonic-gate fork_configd_thread(void *vctid) 4837c478bd9Sstevel@tonic-gate { 4847c478bd9Sstevel@tonic-gate int fd, err; 4857c478bd9Sstevel@tonic-gate ctid_t configd_ctid = (ctid_t)vctid; 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate if (configd_ctid == -1) { 4887c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 4897c478bd9Sstevel@tonic-gate "fork_configd_thread starting svc.configd\n"); 4907c478bd9Sstevel@tonic-gate fork_configd(0); 4917c478bd9Sstevel@tonic-gate } else { 4927c478bd9Sstevel@tonic-gate /* 4937c478bd9Sstevel@tonic-gate * configd_ctid is known: we broadcast and continue. 4947c478bd9Sstevel@tonic-gate * test contract for appropriate state by verifying that 4957c478bd9Sstevel@tonic-gate * there is one or more processes within it? 4967c478bd9Sstevel@tonic-gate */ 4977c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 4987c478bd9Sstevel@tonic-gate "fork_configd_thread accepting svc.configd with CTID %ld\n", 4997c478bd9Sstevel@tonic-gate configd_ctid); 5007c478bd9Sstevel@tonic-gate MUTEX_LOCK(&st->st_configd_live_lock); 5017c478bd9Sstevel@tonic-gate st->st_configd_lives = 1; 5027c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&st->st_configd_live_cv); 5037c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_configd_live_lock); 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY); 5077c478bd9Sstevel@tonic-gate if (fd == -1) 5087c478bd9Sstevel@tonic-gate uu_die("process bundle open failed"); 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate /* 5117c478bd9Sstevel@tonic-gate * Make sure we get all events (including those generated by configd 5127c478bd9Sstevel@tonic-gate * before this thread was started). 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate err = ct_event_reset(fd); 5157c478bd9Sstevel@tonic-gate assert(err == 0); 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate for (;;) { 5187c478bd9Sstevel@tonic-gate int efd, sfd; 5197c478bd9Sstevel@tonic-gate ct_evthdl_t ev; 5207c478bd9Sstevel@tonic-gate uint32_t type; 5217c478bd9Sstevel@tonic-gate ctevid_t evid; 5227c478bd9Sstevel@tonic-gate ct_stathdl_t status; 5237c478bd9Sstevel@tonic-gate ctid_t ctid; 5247c478bd9Sstevel@tonic-gate uint64_t cookie; 5257c478bd9Sstevel@tonic-gate pid_t pid; 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate if (err = ct_event_read_critical(fd, &ev)) { 5287c478bd9Sstevel@tonic-gate assert(err != EINVAL && err != EAGAIN); 5297c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, 5307c478bd9Sstevel@tonic-gate "Error reading next contract event: %s", 5317c478bd9Sstevel@tonic-gate strerror(err)); 5327c478bd9Sstevel@tonic-gate continue; 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate evid = ct_event_get_evid(ev); 5367c478bd9Sstevel@tonic-gate ctid = ct_event_get_ctid(ev); 5377c478bd9Sstevel@tonic-gate type = ct_event_get_type(ev); 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate /* Fetch cookie. */ 5407c478bd9Sstevel@tonic-gate sfd = contract_open(ctid, "process", "status", O_RDONLY); 5417c478bd9Sstevel@tonic-gate if (sfd < 0) { 5427c478bd9Sstevel@tonic-gate ct_event_free(ev); 5437c478bd9Sstevel@tonic-gate continue; 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate if (err = ct_status_read(sfd, CTD_COMMON, &status)) { 5477c478bd9Sstevel@tonic-gate log_framework(LOG_WARNING, "Could not get status for " 5487c478bd9Sstevel@tonic-gate "contract %ld: %s\n", ctid, strerror(err)); 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate ct_event_free(ev); 5517c478bd9Sstevel@tonic-gate startd_close(sfd); 5527c478bd9Sstevel@tonic-gate continue; 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate cookie = ct_status_get_cookie(status); 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate ct_status_free(status); 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate startd_close(sfd); 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate /* 5627c478bd9Sstevel@tonic-gate * Don't process events from contracts we aren't interested in. 5637c478bd9Sstevel@tonic-gate */ 5647c478bd9Sstevel@tonic-gate if (cookie != CONFIGD_COOKIE) { 5657c478bd9Sstevel@tonic-gate ct_event_free(ev); 5667c478bd9Sstevel@tonic-gate continue; 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate if (type == CT_PR_EV_EXIT) { 5707c478bd9Sstevel@tonic-gate int exitstatus; 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate (void) ct_pr_event_get_pid(ev, &pid); 5737c478bd9Sstevel@tonic-gate (void) ct_pr_event_get_exitstatus(ev, 5747c478bd9Sstevel@tonic-gate &exitstatus); 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate if (st->st_configd_pid != pid) { 5777c478bd9Sstevel@tonic-gate /* 5787c478bd9Sstevel@tonic-gate * This is the child exiting, so we 5797c478bd9Sstevel@tonic-gate * abandon the contract and restart 5807c478bd9Sstevel@tonic-gate * configd. 5817c478bd9Sstevel@tonic-gate */ 5827c478bd9Sstevel@tonic-gate contract_abandon(ctid); 5837c478bd9Sstevel@tonic-gate fork_configd(exitstatus); 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate efd = contract_open(ctid, "process", "ctl", O_WRONLY); 5887c478bd9Sstevel@tonic-gate if (efd != -1) { 5897c478bd9Sstevel@tonic-gate (void) ct_ctl_ack(efd, evid); 5907c478bd9Sstevel@tonic-gate startd_close(efd); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate ct_event_free(ev); 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 5987c478bd9Sstevel@tonic-gate return (NULL); 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate void 6027c478bd9Sstevel@tonic-gate fork_rc_script(char rl, const char *arg, boolean_t wait) 6037c478bd9Sstevel@tonic-gate { 6047c478bd9Sstevel@tonic-gate pid_t pid; 6057c478bd9Sstevel@tonic-gate int tmpl, err, stat; 6067c478bd9Sstevel@tonic-gate char path[20] = "/sbin/rc.", log[20] = "rc..log", timebuf[20]; 6077c478bd9Sstevel@tonic-gate time_t now; 6087c478bd9Sstevel@tonic-gate struct tm ltime; 6097c478bd9Sstevel@tonic-gate size_t sz; 6107c478bd9Sstevel@tonic-gate char *pathenv; 6117c478bd9Sstevel@tonic-gate char **nenv; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate path[8] = rl; 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate tmpl = open64(CTFS_ROOT "/process/template", O_RDWR); 6167c478bd9Sstevel@tonic-gate if (tmpl >= 0) { 6177c478bd9Sstevel@tonic-gate err = ct_tmpl_set_critical(tmpl, 0); 6187c478bd9Sstevel@tonic-gate assert(err == 0); 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate err = ct_tmpl_set_informative(tmpl, 0); 6217c478bd9Sstevel@tonic-gate assert(err == 0); 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate err = ct_pr_tmpl_set_fatal(tmpl, 0); 6247c478bd9Sstevel@tonic-gate assert(err == 0); 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate err = ct_tmpl_activate(tmpl); 6277c478bd9Sstevel@tonic-gate assert(err == 0); 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate err = close(tmpl); 6307c478bd9Sstevel@tonic-gate assert(err == 0); 6317c478bd9Sstevel@tonic-gate } else { 6327c478bd9Sstevel@tonic-gate uu_warn("Could not create contract template for %s.\n", path); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate pid = startd_fork1(NULL); 6367c478bd9Sstevel@tonic-gate if (pid < 0) { 6377c478bd9Sstevel@tonic-gate return; 6387c478bd9Sstevel@tonic-gate } else if (pid != 0) { 6397c478bd9Sstevel@tonic-gate /* parent */ 6407c478bd9Sstevel@tonic-gate if (wait) { 6417c478bd9Sstevel@tonic-gate do 6427c478bd9Sstevel@tonic-gate err = waitpid(pid, &stat, 0); 64376cf44abSjeanm while (err != 0 && errno == EINTR) 64476cf44abSjeanm ; 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate if (!WIFEXITED(stat)) { 6477c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, 6487c478bd9Sstevel@tonic-gate "%s terminated with waitpid() status %d.\n", 6497c478bd9Sstevel@tonic-gate path, stat); 6507c478bd9Sstevel@tonic-gate } else if (WEXITSTATUS(stat) != 0) { 6517c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, 6527c478bd9Sstevel@tonic-gate "%s failed with status %d.\n", path, 6537c478bd9Sstevel@tonic-gate WEXITSTATUS(stat)); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate } 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate return; 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate /* child */ 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate log[2] = rl; 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate setlog(log); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate now = time(NULL); 6677c478bd9Sstevel@tonic-gate sz = strftime(timebuf, sizeof (timebuf), "%b %e %T", 6687c478bd9Sstevel@tonic-gate localtime_r(&now, <ime)); 6697c478bd9Sstevel@tonic-gate assert(sz != 0); 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s Executing %s %s\n", timebuf, path, arg); 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate if (rl == 'S') 6747c478bd9Sstevel@tonic-gate pathenv = "PATH=/sbin:/usr/sbin:/usr/bin"; 6757c478bd9Sstevel@tonic-gate else 6767c478bd9Sstevel@tonic-gate pathenv = "PATH=/usr/sbin:/usr/bin"; 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate nenv = set_smf_env(NULL, 0, pathenv, NULL, NULL); 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate (void) execle(path, path, arg, 0, nenv); 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate perror("exec"); 6837c478bd9Sstevel@tonic-gate exit(0); 6847c478bd9Sstevel@tonic-gate } 6854d53c7adSDan Price 686*9444c26fSTom Whitten #define SVCCFG_PATH "/usr/sbin/svccfg" 687*9444c26fSTom Whitten #define EMI_MFST "/lib/svc/manifest/system/early-manifest-import.xml" 688*9444c26fSTom Whitten #define EMI_PATH "/lib/svc/method/manifest-import" 689*9444c26fSTom Whitten 690*9444c26fSTom Whitten /* 691*9444c26fSTom Whitten * Set Early Manifest Import service's state and log file. 692*9444c26fSTom Whitten */ 693*9444c26fSTom Whitten static int 694*9444c26fSTom Whitten emi_set_state(restarter_instance_state_t state, boolean_t setlog) 695*9444c26fSTom Whitten { 696*9444c26fSTom Whitten int r, ret = 1; 697*9444c26fSTom Whitten instance_data_t idata; 698*9444c26fSTom Whitten scf_handle_t *hndl = NULL; 699*9444c26fSTom Whitten scf_instance_t *inst = NULL; 700*9444c26fSTom Whitten 701*9444c26fSTom Whitten retry: 702*9444c26fSTom Whitten if (hndl == NULL) 703*9444c26fSTom Whitten hndl = libscf_handle_create_bound(SCF_VERSION); 704*9444c26fSTom Whitten 705*9444c26fSTom Whitten if (hndl == NULL) { 706*9444c26fSTom Whitten /* 707*9444c26fSTom Whitten * In the case that we can't bind to the repository 708*9444c26fSTom Whitten * (which should have been started), we need to allow 709*9444c26fSTom Whitten * the user into maintenance mode to determine what's 710*9444c26fSTom Whitten * failed. 711*9444c26fSTom Whitten */ 712*9444c26fSTom Whitten fork_sulogin(B_FALSE, "Unable to bind a new repository" 713*9444c26fSTom Whitten " handle: %s\n", scf_strerror(scf_error())); 714*9444c26fSTom Whitten goto retry; 715*9444c26fSTom Whitten } 716*9444c26fSTom Whitten 717*9444c26fSTom Whitten if (inst == NULL) 718*9444c26fSTom Whitten inst = safe_scf_instance_create(hndl); 719*9444c26fSTom Whitten 720*9444c26fSTom Whitten if (scf_handle_decode_fmri(hndl, SCF_INSTANCE_EMI, NULL, NULL, 721*9444c26fSTom Whitten inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) { 722*9444c26fSTom Whitten switch (scf_error()) { 723*9444c26fSTom Whitten case SCF_ERROR_NOT_FOUND: 724*9444c26fSTom Whitten goto out; 725*9444c26fSTom Whitten 726*9444c26fSTom Whitten case SCF_ERROR_CONNECTION_BROKEN: 727*9444c26fSTom Whitten case SCF_ERROR_NOT_BOUND: 728*9444c26fSTom Whitten libscf_handle_rebind(hndl); 729*9444c26fSTom Whitten goto retry; 730*9444c26fSTom Whitten 731*9444c26fSTom Whitten default: 732*9444c26fSTom Whitten fork_sulogin(B_FALSE, "Couldn't fetch %s service: " 733*9444c26fSTom Whitten "%s\n", SCF_INSTANCE_EMI, 734*9444c26fSTom Whitten scf_strerror(scf_error())); 735*9444c26fSTom Whitten goto retry; 736*9444c26fSTom Whitten } 737*9444c26fSTom Whitten } 738*9444c26fSTom Whitten 739*9444c26fSTom Whitten if (setlog) { 740*9444c26fSTom Whitten (void) libscf_note_method_log(inst, st->st_log_prefix, EMI_LOG); 741*9444c26fSTom Whitten log_framework(LOG_DEBUG, 742*9444c26fSTom Whitten "Set logfile property for %s\n", SCF_INSTANCE_EMI); 743*9444c26fSTom Whitten } 744*9444c26fSTom Whitten 745*9444c26fSTom Whitten idata.i_fmri = SCF_INSTANCE_EMI; 746*9444c26fSTom Whitten idata.i_state = RESTARTER_STATE_NONE; 747*9444c26fSTom Whitten idata.i_next_state = RESTARTER_STATE_NONE; 748*9444c26fSTom Whitten switch (r = _restarter_commit_states(hndl, &idata, state, 749*9444c26fSTom Whitten RESTARTER_STATE_NONE, NULL)) { 750*9444c26fSTom Whitten case 0: 751*9444c26fSTom Whitten break; 752*9444c26fSTom Whitten 753*9444c26fSTom Whitten case ECONNABORTED: 754*9444c26fSTom Whitten libscf_handle_rebind(hndl); 755*9444c26fSTom Whitten goto retry; 756*9444c26fSTom Whitten 757*9444c26fSTom Whitten case ENOMEM: 758*9444c26fSTom Whitten case ENOENT: 759*9444c26fSTom Whitten case EPERM: 760*9444c26fSTom Whitten case EACCES: 761*9444c26fSTom Whitten case EROFS: 762*9444c26fSTom Whitten fork_sulogin(B_FALSE, "Could not set state of " 763*9444c26fSTom Whitten "%s: %s\n", SCF_INSTANCE_EMI, strerror(r)); 764*9444c26fSTom Whitten goto retry; 765*9444c26fSTom Whitten break; 766*9444c26fSTom Whitten 767*9444c26fSTom Whitten case EINVAL: 768*9444c26fSTom Whitten default: 769*9444c26fSTom Whitten bad_error("_restarter_commit_states", r); 770*9444c26fSTom Whitten } 771*9444c26fSTom Whitten ret = 0; 772*9444c26fSTom Whitten 773*9444c26fSTom Whitten out: 774*9444c26fSTom Whitten scf_instance_destroy(inst); 775*9444c26fSTom Whitten scf_handle_destroy(hndl); 776*9444c26fSTom Whitten return (ret); 777*9444c26fSTom Whitten } 778*9444c26fSTom Whitten 779*9444c26fSTom Whitten /* 780*9444c26fSTom Whitten * It is possible that the early-manifest-import service is disabled. This 781*9444c26fSTom Whitten * would not be the normal case for Solaris, but it may happen on dedicated 782*9444c26fSTom Whitten * systems. So this function checks the state of the general/enabled 783*9444c26fSTom Whitten * property for Early Manifest Import. 784*9444c26fSTom Whitten * 785*9444c26fSTom Whitten * It is also possible that the early-manifest-import service does not yet 786*9444c26fSTom Whitten * have a repository representation when this function runs. This happens 787*9444c26fSTom Whitten * if non-Early Manifest Import system is upgraded to an Early Manifest 788*9444c26fSTom Whitten * Import based system. Thus, the non-existence of general/enabled is not 789*9444c26fSTom Whitten * an error. 790*9444c26fSTom Whitten * 791*9444c26fSTom Whitten * Returns 1 if Early Manifest Import is disabled and 0 otherwise. 792*9444c26fSTom Whitten */ 793*9444c26fSTom Whitten static int 794*9444c26fSTom Whitten emi_is_disabled() 795*9444c26fSTom Whitten { 796*9444c26fSTom Whitten int disabled = 0; 797*9444c26fSTom Whitten int disconnected = 1; 798*9444c26fSTom Whitten int enabled; 799*9444c26fSTom Whitten scf_handle_t *hndl = NULL; 800*9444c26fSTom Whitten scf_instance_t *inst = NULL; 801*9444c26fSTom Whitten uchar_t stored_hash[MHASH_SIZE]; 802*9444c26fSTom Whitten char *pname; 803*9444c26fSTom Whitten int hashash, r; 804*9444c26fSTom Whitten 805*9444c26fSTom Whitten while (hndl == NULL) { 806*9444c26fSTom Whitten hndl = libscf_handle_create_bound(SCF_VERSION); 807*9444c26fSTom Whitten 808*9444c26fSTom Whitten if (hndl == NULL) { 809*9444c26fSTom Whitten /* 810*9444c26fSTom Whitten * In the case that we can't bind to the repository 811*9444c26fSTom Whitten * (which should have been started), we need to 812*9444c26fSTom Whitten * allow the user into maintenance mode to 813*9444c26fSTom Whitten * determine what's failed. 814*9444c26fSTom Whitten */ 815*9444c26fSTom Whitten fork_sulogin(B_FALSE, "Unable to bind a new repository " 816*9444c26fSTom Whitten "handle: %s\n", scf_strerror(scf_error())); 817*9444c26fSTom Whitten } 818*9444c26fSTom Whitten } 819*9444c26fSTom Whitten 820*9444c26fSTom Whitten while (disconnected) { 821*9444c26fSTom Whitten r = libscf_fmri_get_instance(hndl, SCF_INSTANCE_EMI, &inst); 822*9444c26fSTom Whitten if (r != 0) { 823*9444c26fSTom Whitten switch (r) { 824*9444c26fSTom Whitten case ECONNABORTED: 825*9444c26fSTom Whitten libscf_handle_rebind(hndl); 826*9444c26fSTom Whitten continue; 827*9444c26fSTom Whitten 828*9444c26fSTom Whitten case ENOENT: 829*9444c26fSTom Whitten /* 830*9444c26fSTom Whitten * Early Manifest Import service is not in 831*9444c26fSTom Whitten * the repository. Check the manifest file 832*9444c26fSTom Whitten * and service's hash in smf/manifest to 833*9444c26fSTom Whitten * figure out whether Early Manifest Import 834*9444c26fSTom Whitten * service was deleted. If Early Manifest Import 835*9444c26fSTom Whitten * service was deleted, treat that as a disable 836*9444c26fSTom Whitten * and don't run early import. 837*9444c26fSTom Whitten */ 838*9444c26fSTom Whitten 839*9444c26fSTom Whitten if (access(EMI_MFST, F_OK)) { 840*9444c26fSTom Whitten /* 841*9444c26fSTom Whitten * Manifest isn't found, so service is 842*9444c26fSTom Whitten * properly removed. 843*9444c26fSTom Whitten */ 844*9444c26fSTom Whitten disabled = 1; 845*9444c26fSTom Whitten } else { 846*9444c26fSTom Whitten /* 847*9444c26fSTom Whitten * If manifest exists and we have the 848*9444c26fSTom Whitten * hash, the service was improperly 849*9444c26fSTom Whitten * deleted, generate a warning and treat 850*9444c26fSTom Whitten * this as a disable. 851*9444c26fSTom Whitten */ 852*9444c26fSTom Whitten 853*9444c26fSTom Whitten if ((pname = mhash_filename_to_propname( 854*9444c26fSTom Whitten EMI_MFST, B_TRUE)) == NULL) { 855*9444c26fSTom Whitten /* 856*9444c26fSTom Whitten * Treat failure to get propname 857*9444c26fSTom Whitten * as a disable. 858*9444c26fSTom Whitten */ 859*9444c26fSTom Whitten disabled = 1; 860*9444c26fSTom Whitten uu_warn("Failed to get propname" 861*9444c26fSTom Whitten " for %s.\n", 862*9444c26fSTom Whitten SCF_INSTANCE_EMI); 863*9444c26fSTom Whitten } else { 864*9444c26fSTom Whitten hashash = mhash_retrieve_entry( 865*9444c26fSTom Whitten hndl, pname, 866*9444c26fSTom Whitten stored_hash, 867*9444c26fSTom Whitten NULL) == 0; 868*9444c26fSTom Whitten uu_free(pname); 869*9444c26fSTom Whitten 870*9444c26fSTom Whitten if (hashash) { 871*9444c26fSTom Whitten disabled = 1; 872*9444c26fSTom Whitten uu_warn("%s service is " 873*9444c26fSTom Whitten "deleted \n", 874*9444c26fSTom Whitten SCF_INSTANCE_EMI); 875*9444c26fSTom Whitten } 876*9444c26fSTom Whitten } 877*9444c26fSTom Whitten 878*9444c26fSTom Whitten } 879*9444c26fSTom Whitten 880*9444c26fSTom Whitten disconnected = 0; 881*9444c26fSTom Whitten continue; 882*9444c26fSTom Whitten 883*9444c26fSTom Whitten default: 884*9444c26fSTom Whitten bad_error("libscf_fmri_get_instance", 885*9444c26fSTom Whitten scf_error()); 886*9444c26fSTom Whitten } 887*9444c26fSTom Whitten } 888*9444c26fSTom Whitten r = libscf_get_basic_instance_data(hndl, inst, SCF_INSTANCE_EMI, 889*9444c26fSTom Whitten &enabled, NULL, NULL); 890*9444c26fSTom Whitten if (r == 0) { 891*9444c26fSTom Whitten /* 892*9444c26fSTom Whitten * enabled can be returned as -1, which indicates 893*9444c26fSTom Whitten * that the enabled property was not found. To us 894*9444c26fSTom Whitten * that means that the service was not disabled. 895*9444c26fSTom Whitten */ 896*9444c26fSTom Whitten if (enabled == 0) 897*9444c26fSTom Whitten disabled = 1; 898*9444c26fSTom Whitten } else { 899*9444c26fSTom Whitten switch (r) { 900*9444c26fSTom Whitten case ECONNABORTED: 901*9444c26fSTom Whitten libscf_handle_rebind(hndl); 902*9444c26fSTom Whitten continue; 903*9444c26fSTom Whitten 904*9444c26fSTom Whitten case ECANCELED: 905*9444c26fSTom Whitten case ENOENT: 906*9444c26fSTom Whitten break; 907*9444c26fSTom Whitten default: 908*9444c26fSTom Whitten bad_error("libscf_get_basic_instance_data", r); 909*9444c26fSTom Whitten } 910*9444c26fSTom Whitten } 911*9444c26fSTom Whitten disconnected = 0; 912*9444c26fSTom Whitten } 913*9444c26fSTom Whitten 914*9444c26fSTom Whitten out: 915*9444c26fSTom Whitten if (inst != NULL) 916*9444c26fSTom Whitten scf_instance_destroy(inst); 917*9444c26fSTom Whitten scf_handle_destroy(hndl); 918*9444c26fSTom Whitten return (disabled); 919*9444c26fSTom Whitten } 920*9444c26fSTom Whitten 921*9444c26fSTom Whitten void 922*9444c26fSTom Whitten fork_emi() 923*9444c26fSTom Whitten { 924*9444c26fSTom Whitten pid_t pid; 925*9444c26fSTom Whitten ctid_t ctid = -1; 926*9444c26fSTom Whitten char **envp, **np; 927*9444c26fSTom Whitten char *emipath; 928*9444c26fSTom Whitten char corepath[PATH_MAX]; 929*9444c26fSTom Whitten char *svc_state; 930*9444c26fSTom Whitten int setemilog; 931*9444c26fSTom Whitten int sz; 932*9444c26fSTom Whitten 933*9444c26fSTom Whitten if (emi_is_disabled()) { 934*9444c26fSTom Whitten log_framework(LOG_NOTICE, "%s is disabled and will " 935*9444c26fSTom Whitten "not be run.\n", SCF_INSTANCE_EMI); 936*9444c26fSTom Whitten return; 937*9444c26fSTom Whitten } 938*9444c26fSTom Whitten 939*9444c26fSTom Whitten /* 940*9444c26fSTom Whitten * Early Manifest Import should run only once, at boot. If svc.startd 941*9444c26fSTom Whitten * is some how restarted, Early Manifest Import should not run again. 942*9444c26fSTom Whitten * Use the Early Manifest Import service's state to figure out whether 943*9444c26fSTom Whitten * Early Manifest Import has successfully completed earlier and bail 944*9444c26fSTom Whitten * out if it did. 945*9444c26fSTom Whitten */ 946*9444c26fSTom Whitten if (svc_state = smf_get_state(SCF_INSTANCE_EMI)) { 947*9444c26fSTom Whitten if (strcmp(svc_state, SCF_STATE_STRING_ONLINE) == 0) { 948*9444c26fSTom Whitten free(svc_state); 949*9444c26fSTom Whitten return; 950*9444c26fSTom Whitten } 951*9444c26fSTom Whitten free(svc_state); 952*9444c26fSTom Whitten } 953*9444c26fSTom Whitten 954*9444c26fSTom Whitten /* 955*9444c26fSTom Whitten * Attempt to set Early Manifest Import service's state and log file. 956*9444c26fSTom Whitten * If emi_set_state fails, set log file again in the next call to 957*9444c26fSTom Whitten * emi_set_state. 958*9444c26fSTom Whitten */ 959*9444c26fSTom Whitten setemilog = emi_set_state(RESTARTER_STATE_OFFLINE, B_TRUE); 960*9444c26fSTom Whitten 961*9444c26fSTom Whitten /* Don't go further if /usr isn't available */ 962*9444c26fSTom Whitten if (access(SVCCFG_PATH, F_OK)) { 963*9444c26fSTom Whitten log_framework(LOG_NOTICE, "Early Manifest Import is not " 964*9444c26fSTom Whitten "supported on systems with a separate /usr filesystem.\n"); 965*9444c26fSTom Whitten return; 966*9444c26fSTom Whitten } 967*9444c26fSTom Whitten 968*9444c26fSTom Whitten fork_retry: 969*9444c26fSTom Whitten log_framework(LOG_DEBUG, "Starting Early Manifest Import\n"); 970*9444c26fSTom Whitten 971*9444c26fSTom Whitten /* 972*9444c26fSTom Whitten * If we're retrying, we will have an old contract lying around 973*9444c26fSTom Whitten * from the failure. Since we're going to be creating a new 974*9444c26fSTom Whitten * contract shortly, we abandon the old one now. 975*9444c26fSTom Whitten */ 976*9444c26fSTom Whitten if (ctid != -1) 977*9444c26fSTom Whitten contract_abandon(ctid); 978*9444c26fSTom Whitten ctid = -1; 979*9444c26fSTom Whitten 980*9444c26fSTom Whitten pid = fork_common(SCF_INSTANCE_EMI, SCF_INSTANCE_EMI, 981*9444c26fSTom Whitten MAX_EMI_RETRIES, &ctid, 0, 0, 0, 0, EMI_COOKIE); 982*9444c26fSTom Whitten 983*9444c26fSTom Whitten if (pid != 0) { 984*9444c26fSTom Whitten int exitstatus; 985*9444c26fSTom Whitten 986*9444c26fSTom Whitten if (waitpid(pid, &exitstatus, 0) == -1) { 987*9444c26fSTom Whitten fork_sulogin(B_FALSE, "waitpid on %s failed: " 988*9444c26fSTom Whitten "%s\n", SCF_INSTANCE_EMI, strerror(errno)); 989*9444c26fSTom Whitten } else if (WIFEXITED(exitstatus)) { 990*9444c26fSTom Whitten if (WEXITSTATUS(exitstatus)) { 991*9444c26fSTom Whitten fork_sulogin(B_FALSE, "%s exited with status " 992*9444c26fSTom Whitten "%d \n", SCF_INSTANCE_EMI, 993*9444c26fSTom Whitten WEXITSTATUS(exitstatus)); 994*9444c26fSTom Whitten goto fork_retry; 995*9444c26fSTom Whitten } 996*9444c26fSTom Whitten } else if (WIFSIGNALED(exitstatus)) { 997*9444c26fSTom Whitten char signame[SIG2STR_MAX]; 998*9444c26fSTom Whitten 999*9444c26fSTom Whitten if (sig2str(WTERMSIG(exitstatus), signame)) 1000*9444c26fSTom Whitten (void) snprintf(signame, SIG2STR_MAX, 1001*9444c26fSTom Whitten "signum %d", WTERMSIG(exitstatus)); 1002*9444c26fSTom Whitten 1003*9444c26fSTom Whitten fork_sulogin(B_FALSE, "%s signalled: %s\n", 1004*9444c26fSTom Whitten SCF_INSTANCE_EMI, signame); 1005*9444c26fSTom Whitten goto fork_retry; 1006*9444c26fSTom Whitten } else { 1007*9444c26fSTom Whitten fork_sulogin(B_FALSE, "%s non-exit condition: 0x%x\n", 1008*9444c26fSTom Whitten SCF_INSTANCE_EMI, exitstatus); 1009*9444c26fSTom Whitten goto fork_retry; 1010*9444c26fSTom Whitten } 1011*9444c26fSTom Whitten 1012*9444c26fSTom Whitten log_framework(LOG_DEBUG, "%s completed successfully\n", 1013*9444c26fSTom Whitten SCF_INSTANCE_EMI); 1014*9444c26fSTom Whitten 1015*9444c26fSTom Whitten /* 1016*9444c26fSTom Whitten * Once Early Manifest Import completed, the Early Manifest 1017*9444c26fSTom Whitten * Import service must have been imported so set log file and 1018*9444c26fSTom Whitten * state properties. Since this information is required for 1019*9444c26fSTom Whitten * late manifest import and common admin operations, failing to 1020*9444c26fSTom Whitten * set these properties should result in su login so admin can 1021*9444c26fSTom Whitten * correct the problem. 1022*9444c26fSTom Whitten */ 1023*9444c26fSTom Whitten (void) emi_set_state(RESTARTER_STATE_ONLINE, 1024*9444c26fSTom Whitten setemilog ? B_TRUE : B_FALSE); 1025*9444c26fSTom Whitten 1026*9444c26fSTom Whitten return; 1027*9444c26fSTom Whitten } 1028*9444c26fSTom Whitten 1029*9444c26fSTom Whitten /* child */ 1030*9444c26fSTom Whitten 1031*9444c26fSTom Whitten /* 1032*9444c26fSTom Whitten * Set our per-process core file path to leave core files in 1033*9444c26fSTom Whitten * /etc/svc/volatile directory, named after the PID to aid in debugging. 1034*9444c26fSTom Whitten */ 1035*9444c26fSTom Whitten (void) snprintf(corepath, sizeof (corepath), 1036*9444c26fSTom Whitten "/etc/svc/volatile/core.emi.%%p"); 1037*9444c26fSTom Whitten (void) core_set_process_path(corepath, strlen(corepath) + 1, getpid()); 1038*9444c26fSTom Whitten 1039*9444c26fSTom Whitten /* 1040*9444c26fSTom Whitten * Similar to running legacy services, we need to manually set 1041*9444c26fSTom Whitten * log files here and environment variables. 1042*9444c26fSTom Whitten */ 1043*9444c26fSTom Whitten setlog(EMI_LOG); 1044*9444c26fSTom Whitten 1045*9444c26fSTom Whitten envp = startd_zalloc(sizeof (char *) * 3); 1046*9444c26fSTom Whitten np = envp; 1047*9444c26fSTom Whitten 1048*9444c26fSTom Whitten sz = sizeof ("SMF_FMRI=") + strlen(SCF_INSTANCE_EMI); 1049*9444c26fSTom Whitten *np = startd_zalloc(sz); 1050*9444c26fSTom Whitten (void) strlcpy(*np, "SMF_FMRI=", sz); 1051*9444c26fSTom Whitten (void) strncat(*np, SCF_INSTANCE_EMI, sz); 1052*9444c26fSTom Whitten np++; 1053*9444c26fSTom Whitten 1054*9444c26fSTom Whitten emipath = getenv("PATH"); 1055*9444c26fSTom Whitten if (emipath == NULL) 1056*9444c26fSTom Whitten emipath = strdup("/usr/sbin:/usr/bin"); 1057*9444c26fSTom Whitten 1058*9444c26fSTom Whitten sz = sizeof ("PATH=") + strlen(emipath); 1059*9444c26fSTom Whitten *np = startd_zalloc(sz); 1060*9444c26fSTom Whitten (void) strlcpy(*np, "PATH=", sz); 1061*9444c26fSTom Whitten (void) strncat(*np, emipath, sz); 1062*9444c26fSTom Whitten 1063*9444c26fSTom Whitten log_framework(LOG_DEBUG, "executing Early Manifest Import\n"); 1064*9444c26fSTom Whitten (void) execle(EMI_PATH, EMI_PATH, NULL, envp); 1065*9444c26fSTom Whitten 1066*9444c26fSTom Whitten /* 1067*9444c26fSTom Whitten * Status code is used above to identify Early Manifest Import 1068*9444c26fSTom Whitten * exec failure. 1069*9444c26fSTom Whitten */ 1070*9444c26fSTom Whitten exit(1); 1071*9444c26fSTom Whitten } 1072*9444c26fSTom Whitten 10734d53c7adSDan Price extern char **environ; 10744d53c7adSDan Price 10754d53c7adSDan Price /* 10764d53c7adSDan Price * A local variation on system(3c) which accepts a timeout argument. This 10774d53c7adSDan Price * allows us to better ensure that the system will actually shut down. 10784d53c7adSDan Price * 10794d53c7adSDan Price * gracetime specifies an amount of time in seconds which the routine must wait 10804d53c7adSDan Price * after the command exits, to allow for asynchronous effects (like sent 10814d53c7adSDan Price * signals) to take effect. This can be zero. 10824d53c7adSDan Price */ 10834d53c7adSDan Price void 10844d53c7adSDan Price fork_with_timeout(const char *cmd, uint_t gracetime, uint_t timeout) 10854d53c7adSDan Price { 10864d53c7adSDan Price int err = 0; 10874d53c7adSDan Price pid_t pid; 10884d53c7adSDan Price char *argv[4]; 10894d53c7adSDan Price posix_spawnattr_t attr; 10904d53c7adSDan Price posix_spawn_file_actions_t factions; 10914d53c7adSDan Price 10924d53c7adSDan Price sigset_t mask, savemask; 10934d53c7adSDan Price uint_t msec_timeout; 10944d53c7adSDan Price uint_t msec_spent = 0; 10954d53c7adSDan Price uint_t msec_gracetime; 10964d53c7adSDan Price int status; 10974d53c7adSDan Price 10984d53c7adSDan Price msec_timeout = timeout * 1000; 10994d53c7adSDan Price msec_gracetime = gracetime * 1000; 11004d53c7adSDan Price 11014d53c7adSDan Price /* 11024d53c7adSDan Price * See also system(3c) in libc. This is very similar, except 11034d53c7adSDan Price * that we avoid some unneeded complexity. 11044d53c7adSDan Price */ 11054d53c7adSDan Price err = posix_spawnattr_init(&attr); 11064d53c7adSDan Price if (err == 0) 11074d53c7adSDan Price err = posix_spawnattr_setflags(&attr, 11084d53c7adSDan Price POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF | 11094d53c7adSDan Price POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP | 11104d53c7adSDan Price POSIX_SPAWN_NOEXECERR_NP); 11114d53c7adSDan Price 11124d53c7adSDan Price /* 11134d53c7adSDan Price * We choose to close fd's above 2, a deviation from system. 11144d53c7adSDan Price */ 11154d53c7adSDan Price if (err == 0) 11164d53c7adSDan Price err = posix_spawn_file_actions_init(&factions); 11174d53c7adSDan Price if (err == 0) 11184d53c7adSDan Price err = posix_spawn_file_actions_addclosefrom_np(&factions, 11194d53c7adSDan Price STDERR_FILENO + 1); 11204d53c7adSDan Price 11214d53c7adSDan Price (void) sigemptyset(&mask); 11224d53c7adSDan Price (void) sigaddset(&mask, SIGCHLD); 11234d53c7adSDan Price (void) thr_sigsetmask(SIG_BLOCK, &mask, &savemask); 11244d53c7adSDan Price 11254d53c7adSDan Price argv[0] = "/bin/sh"; 11264d53c7adSDan Price argv[1] = "-c"; 11274d53c7adSDan Price argv[2] = (char *)cmd; 11284d53c7adSDan Price argv[3] = NULL; 11294d53c7adSDan Price 11304d53c7adSDan Price if (err == 0) 11314d53c7adSDan Price err = posix_spawn(&pid, "/bin/sh", &factions, &attr, 11324d53c7adSDan Price (char *const *)argv, (char *const *)environ); 11334d53c7adSDan Price 11344d53c7adSDan Price (void) posix_spawnattr_destroy(&attr); 11354d53c7adSDan Price (void) posix_spawn_file_actions_destroy(&factions); 11364d53c7adSDan Price 11374d53c7adSDan Price if (err) { 11384d53c7adSDan Price uu_warn("Failed to spawn %s: %s\n", cmd, strerror(err)); 11394d53c7adSDan Price } else { 11404d53c7adSDan Price for (;;) { 11414d53c7adSDan Price int w; 11424d53c7adSDan Price w = waitpid(pid, &status, WNOHANG); 11434d53c7adSDan Price if (w == -1 && errno != EINTR) 11444d53c7adSDan Price break; 11454d53c7adSDan Price if (w > 0) { 11464d53c7adSDan Price /* 11474d53c7adSDan Price * Command succeeded, so give it gracetime 11484d53c7adSDan Price * seconds for it to have an effect. 11494d53c7adSDan Price */ 11504d53c7adSDan Price if (status == 0 && msec_gracetime != 0) 11514d53c7adSDan Price (void) poll(NULL, 0, msec_gracetime); 11524d53c7adSDan Price break; 11534d53c7adSDan Price } 11544d53c7adSDan Price 11554d53c7adSDan Price (void) poll(NULL, 0, 100); 11564d53c7adSDan Price msec_spent += 100; 11574d53c7adSDan Price /* 11584d53c7adSDan Price * If we timed out, kill off the process, then try to 11594d53c7adSDan Price * wait for it-- it's possible that we could accumulate 11604d53c7adSDan Price * a zombie here since we don't allow waitpid to hang, 11614d53c7adSDan Price * but it's better to let that happen and continue to 11624d53c7adSDan Price * make progress. 11634d53c7adSDan Price */ 11644d53c7adSDan Price if (msec_spent >= msec_timeout) { 11654d53c7adSDan Price uu_warn("'%s' timed out after %d " 11664d53c7adSDan Price "seconds. Killing.\n", cmd, 11674d53c7adSDan Price timeout); 11684d53c7adSDan Price (void) kill(pid, SIGTERM); 11694d53c7adSDan Price (void) poll(NULL, 0, 100); 11704d53c7adSDan Price (void) kill(pid, SIGKILL); 11714d53c7adSDan Price (void) poll(NULL, 0, 100); 11724d53c7adSDan Price (void) waitpid(pid, &status, WNOHANG); 11734d53c7adSDan Price break; 11744d53c7adSDan Price } 11754d53c7adSDan Price } 11764d53c7adSDan Price } 11774d53c7adSDan Price (void) thr_sigsetmask(SIG_BLOCK, &savemask, NULL); 11784d53c7adSDan Price } 1179