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 53ad28c1eSrm88369 * Common Development and Distribution License (the "License"). 63ad28c1eSrm88369 * 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 */ 2113d8aaa1SSean Wilcox 227c478bd9Sstevel@tonic-gate /* 23f6e214c7SGavin Maltby * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 2422b8cfe6SJohn Sonnenschein * Copyright 2011 Joyent Inc. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * method.c - method execution functions 297c478bd9Sstevel@tonic-gate * 307c478bd9Sstevel@tonic-gate * This file contains the routines needed to run a method: a fork(2)-exec(2) 317c478bd9Sstevel@tonic-gate * invocation monitored using either the contract filesystem or waitpid(2). 327c478bd9Sstevel@tonic-gate * (Plain fork1(2) support is provided in fork.c.) 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * Contract Transfer 357c478bd9Sstevel@tonic-gate * When we restart a service, we want to transfer any contracts that the old 367c478bd9Sstevel@tonic-gate * service's contract inherited. This means that (a) we must not abandon the 377c478bd9Sstevel@tonic-gate * old contract when the service dies and (b) we must write the id of the old 387c478bd9Sstevel@tonic-gate * contract into the terms of the new contract. There should be limits to 397c478bd9Sstevel@tonic-gate * (a), though, since we don't want to keep the contract around forever. To 407c478bd9Sstevel@tonic-gate * this end we'll say that services in the offline state may have a contract 417c478bd9Sstevel@tonic-gate * to be transfered and services in the disabled or maintenance states cannot. 427c478bd9Sstevel@tonic-gate * This means that when a service transitions from online (or degraded) to 437c478bd9Sstevel@tonic-gate * offline, the contract should be preserved, and when the service transitions 447c478bd9Sstevel@tonic-gate * from offline to online (i.e., the start method), we'll transfer inherited 457c478bd9Sstevel@tonic-gate * contracts. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #include <sys/contract/process.h> 497c478bd9Sstevel@tonic-gate #include <sys/ctfs.h> 507c478bd9Sstevel@tonic-gate #include <sys/stat.h> 517c478bd9Sstevel@tonic-gate #include <sys/time.h> 527c478bd9Sstevel@tonic-gate #include <sys/types.h> 537c478bd9Sstevel@tonic-gate #include <sys/uio.h> 547c478bd9Sstevel@tonic-gate #include <sys/wait.h> 557c478bd9Sstevel@tonic-gate #include <alloca.h> 567c478bd9Sstevel@tonic-gate #include <assert.h> 577c478bd9Sstevel@tonic-gate #include <errno.h> 587c478bd9Sstevel@tonic-gate #include <fcntl.h> 597c478bd9Sstevel@tonic-gate #include <libcontract.h> 607c478bd9Sstevel@tonic-gate #include <libcontract_priv.h> 617c478bd9Sstevel@tonic-gate #include <libgen.h> 627c478bd9Sstevel@tonic-gate #include <librestart.h> 637c478bd9Sstevel@tonic-gate #include <libscf.h> 647c478bd9Sstevel@tonic-gate #include <limits.h> 657c478bd9Sstevel@tonic-gate #include <port.h> 667c478bd9Sstevel@tonic-gate #include <sac.h> 677c478bd9Sstevel@tonic-gate #include <signal.h> 687c478bd9Sstevel@tonic-gate #include <stdlib.h> 697c478bd9Sstevel@tonic-gate #include <string.h> 707c478bd9Sstevel@tonic-gate #include <strings.h> 717c478bd9Sstevel@tonic-gate #include <unistd.h> 72dfe57350Sjeanm #include <atomic.h> 73dfe57350Sjeanm #include <poll.h> 7422b8cfe6SJohn Sonnenschein #include <libscf_priv.h> 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #include "startd.h" 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #define SBIN_SH "/sbin/sh" 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* 81dfe57350Sjeanm * Used to tell if contracts are in the process of being 82dfe57350Sjeanm * stored into the svc.startd internal hash table. 83dfe57350Sjeanm */ 84dfe57350Sjeanm volatile uint16_t storing_contract = 0; 85dfe57350Sjeanm 86dfe57350Sjeanm /* 877c478bd9Sstevel@tonic-gate * Mapping from restart_on method-type to contract events. Must correspond to 887c478bd9Sstevel@tonic-gate * enum method_restart_t. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate static uint_t method_events[] = { 917c478bd9Sstevel@tonic-gate /* METHOD_RESTART_ALL */ 927c478bd9Sstevel@tonic-gate CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE | CT_PR_EV_EMPTY, 937c478bd9Sstevel@tonic-gate /* METHOD_RESTART_EXTERNAL_FAULT */ 947c478bd9Sstevel@tonic-gate CT_PR_EV_HWERR | CT_PR_EV_SIGNAL, 957c478bd9Sstevel@tonic-gate /* METHOD_RESTART_ANY_FAULT */ 967c478bd9Sstevel@tonic-gate CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE 977c478bd9Sstevel@tonic-gate }; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * method_record_start(restarter_inst_t *) 1017c478bd9Sstevel@tonic-gate * Record a service start for rate limiting. Place the current time 1027c478bd9Sstevel@tonic-gate * in the circular array of instance starts. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate static void 1057c478bd9Sstevel@tonic-gate method_record_start(restarter_inst_t *inst) 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate int index = inst->ri_start_index++ % RINST_START_TIMES; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate inst->ri_start_time[index] = gethrtime(); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * method_rate_critical(restarter_inst_t *) 1147c478bd9Sstevel@tonic-gate * Return true if the average start interval is less than the permitted 11522b8cfe6SJohn Sonnenschein * interval. The implicit interval defaults to RINST_FAILURE_RATE_NS and 11622b8cfe6SJohn Sonnenschein * RINST_START_TIMES but may be overridden with the svc properties 11722b8cfe6SJohn Sonnenschein * startd/critical_failure_count and startd/critical_failure_period 11822b8cfe6SJohn Sonnenschein * which represent the number of failures to consider and the amount of 11922b8cfe6SJohn Sonnenschein * time in seconds in which that number may occur, respectively. Note that 12022b8cfe6SJohn Sonnenschein * this time is measured as of the transition to 'enabled' rather than wall 12122b8cfe6SJohn Sonnenschein * clock time. 12222b8cfe6SJohn Sonnenschein * Implicit success if insufficient measurements for an average exist. 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate static int 1257c478bd9Sstevel@tonic-gate method_rate_critical(restarter_inst_t *inst) 1267c478bd9Sstevel@tonic-gate { 12722b8cfe6SJohn Sonnenschein hrtime_t critical_failure_period = RINST_FAILURE_RATE_NS; 12822b8cfe6SJohn Sonnenschein uint_t critical_failure_count = RINST_START_TIMES; 1297c478bd9Sstevel@tonic-gate uint_t n = inst->ri_start_index; 1307c478bd9Sstevel@tonic-gate hrtime_t avg_ns = 0; 13122b8cfe6SJohn Sonnenschein uint64_t scf_fr, scf_st; 13222b8cfe6SJohn Sonnenschein scf_propvec_t *prop = NULL; 13322b8cfe6SJohn Sonnenschein scf_propvec_t restart_critical[] = { 13422b8cfe6SJohn Sonnenschein { "critical_failure_period", NULL, SCF_TYPE_INTEGER, NULL, 0 }, 13522b8cfe6SJohn Sonnenschein { "critical_failure_count", NULL, SCF_TYPE_INTEGER, NULL, 0 }, 13622b8cfe6SJohn Sonnenschein { NULL } 13722b8cfe6SJohn Sonnenschein }; 1387c478bd9Sstevel@tonic-gate 13922b8cfe6SJohn Sonnenschein restart_critical[0].pv_ptr = &scf_fr; 14022b8cfe6SJohn Sonnenschein restart_critical[1].pv_ptr = &scf_st; 14122b8cfe6SJohn Sonnenschein 14222b8cfe6SJohn Sonnenschein if (scf_read_propvec(inst->ri_i.i_fmri, "startd", 14322b8cfe6SJohn Sonnenschein B_TRUE, restart_critical, &prop) != SCF_FAILED) { 14422b8cfe6SJohn Sonnenschein /* 14522b8cfe6SJohn Sonnenschein * critical_failure_period is expressed 14622b8cfe6SJohn Sonnenschein * in seconds but tracked in ns 14722b8cfe6SJohn Sonnenschein */ 14822b8cfe6SJohn Sonnenschein critical_failure_period = (hrtime_t)scf_fr * NANOSEC; 14922b8cfe6SJohn Sonnenschein critical_failure_count = (uint_t)scf_st; 15022b8cfe6SJohn Sonnenschein } 15122b8cfe6SJohn Sonnenschein if (inst->ri_start_index < critical_failure_count) 1527c478bd9Sstevel@tonic-gate return (0); 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate avg_ns = 15522b8cfe6SJohn Sonnenschein (inst->ri_start_time[(n - 1) % critical_failure_count] - 15622b8cfe6SJohn Sonnenschein inst->ri_start_time[n % critical_failure_count]) / 15722b8cfe6SJohn Sonnenschein (critical_failure_count - 1); 1587c478bd9Sstevel@tonic-gate 15922b8cfe6SJohn Sonnenschein return (avg_ns < critical_failure_period); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * int method_is_transient() 1647c478bd9Sstevel@tonic-gate * Determine if the method for the given instance is transient, 1657c478bd9Sstevel@tonic-gate * from a contract perspective. Return 1 if it is, and 0 if it isn't. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate static int 1687c478bd9Sstevel@tonic-gate method_is_transient(restarter_inst_t *inst, int type) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate if (instance_is_transient_style(inst) || type != METHOD_START) 1717c478bd9Sstevel@tonic-gate return (1); 1727c478bd9Sstevel@tonic-gate else 1737c478bd9Sstevel@tonic-gate return (0); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * void method_store_contract() 1787c478bd9Sstevel@tonic-gate * Store the newly created contract id into local structures and 1797c478bd9Sstevel@tonic-gate * the repository. If the repository connection is broken it is rebound. 1807c478bd9Sstevel@tonic-gate */ 1817c478bd9Sstevel@tonic-gate static void 1827c478bd9Sstevel@tonic-gate method_store_contract(restarter_inst_t *inst, int type, ctid_t *cid) 1837c478bd9Sstevel@tonic-gate { 1847c478bd9Sstevel@tonic-gate int r; 1857c478bd9Sstevel@tonic-gate boolean_t primary; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate if (errno = contract_latest(cid)) 1887c478bd9Sstevel@tonic-gate uu_die("%s: Couldn't get new contract's id", inst->ri_i.i_fmri); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate primary = !method_is_transient(inst, type); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (!primary) { 1937c478bd9Sstevel@tonic-gate if (inst->ri_i.i_transient_ctid != 0) { 1947c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, 1957c478bd9Sstevel@tonic-gate "%s: transient ctid expected to be 0 but " 1967c478bd9Sstevel@tonic-gate "was set to %ld\n", inst->ri_i.i_fmri, 1977c478bd9Sstevel@tonic-gate inst->ri_i.i_transient_ctid); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate inst->ri_i.i_transient_ctid = *cid; 2017c478bd9Sstevel@tonic-gate } else { 2027c478bd9Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid != 0) { 2037c478bd9Sstevel@tonic-gate /* 2047c478bd9Sstevel@tonic-gate * There was an old contract that we transferred. 2057c478bd9Sstevel@tonic-gate * Remove it. 2067c478bd9Sstevel@tonic-gate */ 2077c478bd9Sstevel@tonic-gate method_remove_contract(inst, B_TRUE, B_FALSE); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid != 0) { 2117c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, 2127c478bd9Sstevel@tonic-gate "%s: primary ctid expected to be 0 but " 2137c478bd9Sstevel@tonic-gate "was set to %ld\n", inst->ri_i.i_fmri, 2147c478bd9Sstevel@tonic-gate inst->ri_i.i_primary_ctid); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate inst->ri_i.i_primary_ctid = *cid; 2187c478bd9Sstevel@tonic-gate inst->ri_i.i_primary_ctid_stopped = 0; 2197c478bd9Sstevel@tonic-gate 220dfe57350Sjeanm log_framework(LOG_DEBUG, "Storing primary contract %ld for " 221dfe57350Sjeanm "%s.\n", *cid, inst->ri_i.i_fmri); 222dfe57350Sjeanm 2237c478bd9Sstevel@tonic-gate contract_hash_store(*cid, inst->ri_id); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate again: 2277c478bd9Sstevel@tonic-gate if (inst->ri_mi_deleted) 2287c478bd9Sstevel@tonic-gate return; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate r = restarter_store_contract(inst->ri_m_inst, *cid, primary ? 2317c478bd9Sstevel@tonic-gate RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT); 2327c478bd9Sstevel@tonic-gate switch (r) { 2337c478bd9Sstevel@tonic-gate case 0: 2347c478bd9Sstevel@tonic-gate break; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate case ECANCELED: 2377c478bd9Sstevel@tonic-gate inst->ri_mi_deleted = B_TRUE; 2387c478bd9Sstevel@tonic-gate break; 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate case ECONNABORTED: 2417c478bd9Sstevel@tonic-gate libscf_handle_rebind(scf_instance_handle(inst->ri_m_inst)); 2427c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate case EBADF: 2457c478bd9Sstevel@tonic-gate libscf_reget_instance(inst); 2467c478bd9Sstevel@tonic-gate goto again; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate case ENOMEM: 2497c478bd9Sstevel@tonic-gate case EPERM: 2507c478bd9Sstevel@tonic-gate case EACCES: 2517c478bd9Sstevel@tonic-gate case EROFS: 2527c478bd9Sstevel@tonic-gate uu_die("%s: Couldn't store contract id %ld", 2537c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, *cid); 2547c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate case EINVAL: 2577c478bd9Sstevel@tonic-gate default: 2587c478bd9Sstevel@tonic-gate bad_error("restarter_store_contract", r); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate /* 2637c478bd9Sstevel@tonic-gate * void method_remove_contract() 2647c478bd9Sstevel@tonic-gate * Remove any non-permanent contracts from internal structures and 2657c478bd9Sstevel@tonic-gate * the repository, then abandon them. 2667c478bd9Sstevel@tonic-gate * Returns 2677c478bd9Sstevel@tonic-gate * 0 - success 2687c478bd9Sstevel@tonic-gate * ECANCELED - inst was deleted from the repository 2697c478bd9Sstevel@tonic-gate * 2707c478bd9Sstevel@tonic-gate * If the repository connection was broken, it is rebound. 2717c478bd9Sstevel@tonic-gate */ 2727c478bd9Sstevel@tonic-gate void 2737c478bd9Sstevel@tonic-gate method_remove_contract(restarter_inst_t *inst, boolean_t primary, 2747c478bd9Sstevel@tonic-gate boolean_t abandon) 2757c478bd9Sstevel@tonic-gate { 2767c478bd9Sstevel@tonic-gate ctid_t * const ctidp = primary ? &inst->ri_i.i_primary_ctid : 2777c478bd9Sstevel@tonic-gate &inst->ri_i.i_transient_ctid; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate int r; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate assert(*ctidp != 0); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Removing %s contract %lu for %s.\n", 2847c478bd9Sstevel@tonic-gate primary ? "primary" : "transient", *ctidp, inst->ri_i.i_fmri); 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate if (abandon) 2877c478bd9Sstevel@tonic-gate contract_abandon(*ctidp); 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate again: 2907c478bd9Sstevel@tonic-gate if (inst->ri_mi_deleted) { 2917c478bd9Sstevel@tonic-gate r = ECANCELED; 2927c478bd9Sstevel@tonic-gate goto out; 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate r = restarter_remove_contract(inst->ri_m_inst, *ctidp, primary ? 2967c478bd9Sstevel@tonic-gate RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT); 2977c478bd9Sstevel@tonic-gate switch (r) { 2987c478bd9Sstevel@tonic-gate case 0: 2997c478bd9Sstevel@tonic-gate break; 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate case ECANCELED: 3027c478bd9Sstevel@tonic-gate inst->ri_mi_deleted = B_TRUE; 3037c478bd9Sstevel@tonic-gate break; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate case ECONNABORTED: 3067c478bd9Sstevel@tonic-gate libscf_handle_rebind(scf_instance_handle(inst->ri_m_inst)); 3077c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate case EBADF: 3107c478bd9Sstevel@tonic-gate libscf_reget_instance(inst); 3117c478bd9Sstevel@tonic-gate goto again; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate case ENOMEM: 3147c478bd9Sstevel@tonic-gate case EPERM: 3157c478bd9Sstevel@tonic-gate case EACCES: 3167c478bd9Sstevel@tonic-gate case EROFS: 3177c478bd9Sstevel@tonic-gate log_error(LOG_INFO, "%s: Couldn't remove contract id %ld: " 3187c478bd9Sstevel@tonic-gate "%s.\n", inst->ri_i.i_fmri, *ctidp, strerror(r)); 3197c478bd9Sstevel@tonic-gate break; 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate case EINVAL: 3227c478bd9Sstevel@tonic-gate default: 3237c478bd9Sstevel@tonic-gate bad_error("restarter_remove_contract", r); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate out: 3277c478bd9Sstevel@tonic-gate if (primary) 3287c478bd9Sstevel@tonic-gate contract_hash_remove(*ctidp); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate *ctidp = 0; 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337b209c2cSacruz static const char *method_names[] = { "start", "stop", "refresh" }; 3347b209c2cSacruz 3357c478bd9Sstevel@tonic-gate /* 3367c478bd9Sstevel@tonic-gate * int method_ready_contract(restarter_inst_t *, int, method_restart_t, int) 3377c478bd9Sstevel@tonic-gate * 3387c478bd9Sstevel@tonic-gate * Activate a contract template for the type method of inst. type, 3397c478bd9Sstevel@tonic-gate * restart_on, and cte_mask dictate the critical events term of the contract. 3407c478bd9Sstevel@tonic-gate * Returns 3417c478bd9Sstevel@tonic-gate * 0 - success 3427c478bd9Sstevel@tonic-gate * ECANCELED - inst has been deleted from the repository 3437c478bd9Sstevel@tonic-gate */ 3447c478bd9Sstevel@tonic-gate static int 3457c478bd9Sstevel@tonic-gate method_ready_contract(restarter_inst_t *inst, int type, 3467c478bd9Sstevel@tonic-gate method_restart_t restart_on, uint_t cte_mask) 3477c478bd9Sstevel@tonic-gate { 3487c478bd9Sstevel@tonic-gate int tmpl, err, istrans, iswait, ret; 3497c478bd9Sstevel@tonic-gate uint_t cevents, fevents; 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate /* 3527c478bd9Sstevel@tonic-gate * Correctly supporting wait-style services is tricky without 3537c478bd9Sstevel@tonic-gate * rearchitecting startd to cope with multiple event sources 3547c478bd9Sstevel@tonic-gate * simultaneously trying to stop an instance. Until a better 3557c478bd9Sstevel@tonic-gate * solution is implemented, we avoid this problem for 3567c478bd9Sstevel@tonic-gate * wait-style services by making contract events fatal and 3577c478bd9Sstevel@tonic-gate * letting the wait code alone handle stopping the service. 3587c478bd9Sstevel@tonic-gate */ 3597c478bd9Sstevel@tonic-gate iswait = instance_is_wait_style(inst); 3607c478bd9Sstevel@tonic-gate istrans = method_is_transient(inst, type); 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate tmpl = open64(CTFS_ROOT "/process/template", O_RDWR); 3637c478bd9Sstevel@tonic-gate if (tmpl == -1) 3647c478bd9Sstevel@tonic-gate uu_die("Could not create contract template"); 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * We assume non-login processes are unlikely to create 3687c478bd9Sstevel@tonic-gate * multiple process groups, and set CT_PR_PGRPONLY for all 3697c478bd9Sstevel@tonic-gate * wait-style services' contracts. 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate err = ct_pr_tmpl_set_param(tmpl, CT_PR_INHERIT | CT_PR_REGENT | 3727c478bd9Sstevel@tonic-gate (iswait ? CT_PR_PGRPONLY : 0)); 3737c478bd9Sstevel@tonic-gate assert(err == 0); 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate if (istrans) { 3767c478bd9Sstevel@tonic-gate cevents = 0; 3777c478bd9Sstevel@tonic-gate fevents = 0; 3787c478bd9Sstevel@tonic-gate } else { 3797c478bd9Sstevel@tonic-gate assert(restart_on >= 0); 3807c478bd9Sstevel@tonic-gate assert(restart_on <= METHOD_RESTART_ANY_FAULT); 3817c478bd9Sstevel@tonic-gate cevents = method_events[restart_on] & ~cte_mask; 3827c478bd9Sstevel@tonic-gate fevents = iswait ? 3837c478bd9Sstevel@tonic-gate (method_events[restart_on] & ~cte_mask & CT_PR_ALLFATAL) : 3847c478bd9Sstevel@tonic-gate 0; 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate err = ct_tmpl_set_critical(tmpl, cevents); 3887c478bd9Sstevel@tonic-gate assert(err == 0); 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate err = ct_tmpl_set_informative(tmpl, 0); 3917c478bd9Sstevel@tonic-gate assert(err == 0); 3927c478bd9Sstevel@tonic-gate err = ct_pr_tmpl_set_fatal(tmpl, fevents); 3937c478bd9Sstevel@tonic-gate assert(err == 0); 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate err = ct_tmpl_set_cookie(tmpl, istrans ? METHOD_OTHER_COOKIE : 3967c478bd9Sstevel@tonic-gate METHOD_START_COOKIE); 3977c478bd9Sstevel@tonic-gate assert(err == 0); 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate if (type == METHOD_START && inst->ri_i.i_primary_ctid != 0) { 4007c478bd9Sstevel@tonic-gate ret = ct_pr_tmpl_set_transfer(tmpl, inst->ri_i.i_primary_ctid); 4017c478bd9Sstevel@tonic-gate switch (ret) { 4027c478bd9Sstevel@tonic-gate case 0: 4037c478bd9Sstevel@tonic-gate break; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate case ENOTEMPTY: 4067c478bd9Sstevel@tonic-gate /* No contracts for you! */ 4077c478bd9Sstevel@tonic-gate method_remove_contract(inst, B_TRUE, B_TRUE); 4087c478bd9Sstevel@tonic-gate if (inst->ri_mi_deleted) { 4097c478bd9Sstevel@tonic-gate ret = ECANCELED; 4107c478bd9Sstevel@tonic-gate goto out; 4117c478bd9Sstevel@tonic-gate } 4127c478bd9Sstevel@tonic-gate break; 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate case EINVAL: 4157c478bd9Sstevel@tonic-gate case ESRCH: 4167c478bd9Sstevel@tonic-gate case EACCES: 4177c478bd9Sstevel@tonic-gate default: 4187c478bd9Sstevel@tonic-gate bad_error("ct_pr_tmpl_set_transfer", ret); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate 4227b209c2cSacruz err = ct_pr_tmpl_set_svc_fmri(tmpl, inst->ri_i.i_fmri); 4237b209c2cSacruz assert(err == 0); 4247b209c2cSacruz err = ct_pr_tmpl_set_svc_aux(tmpl, method_names[type]); 4257b209c2cSacruz assert(err == 0); 4267b209c2cSacruz 4277c478bd9Sstevel@tonic-gate err = ct_tmpl_activate(tmpl); 4287c478bd9Sstevel@tonic-gate assert(err == 0); 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate ret = 0; 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate out: 4337c478bd9Sstevel@tonic-gate err = close(tmpl); 4347c478bd9Sstevel@tonic-gate assert(err == 0); 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate return (ret); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate static void 4407c478bd9Sstevel@tonic-gate exec_method(const restarter_inst_t *inst, int type, const char *method, 4417c478bd9Sstevel@tonic-gate struct method_context *mcp, uint8_t need_session) 4427c478bd9Sstevel@tonic-gate { 4437c478bd9Sstevel@tonic-gate char *cmd; 4447c478bd9Sstevel@tonic-gate const char *errf; 4457c478bd9Sstevel@tonic-gate char **nenv; 44677c20ef8Sacruz int rsmc_errno = 0; 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate cmd = uu_msprintf("exec %s", method); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate if (inst->ri_utmpx_prefix[0] != '\0' && inst->ri_utmpx_prefix != NULL) 4517c478bd9Sstevel@tonic-gate (void) utmpx_mark_init(getpid(), inst->ri_utmpx_prefix); 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate setlog(inst->ri_logstem); 4545ca87c7fSlianep log_instance(inst, B_FALSE, "Executing %s method (\"%s\").", 4557c478bd9Sstevel@tonic-gate method_names[type], method); 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate if (need_session) 4587c478bd9Sstevel@tonic-gate (void) setpgrp(); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate /* Set credentials. */ 46177c20ef8Sacruz rsmc_errno = restarter_set_method_context(mcp, &errf); 46277c20ef8Sacruz if (rsmc_errno != 0) { 46313d8aaa1SSean Wilcox log_instance(inst, B_FALSE, 46413d8aaa1SSean Wilcox "svc.startd could not set context for method: "); 4657c478bd9Sstevel@tonic-gate 46677c20ef8Sacruz if (rsmc_errno == -1) { 4677c478bd9Sstevel@tonic-gate if (strcmp(errf, "core_set_process_path") == 0) { 46813d8aaa1SSean Wilcox log_instance(inst, B_FALSE, 46913d8aaa1SSean Wilcox "Could not set corefile path."); 4707c478bd9Sstevel@tonic-gate } else if (strcmp(errf, "setproject") == 0) { 47113d8aaa1SSean Wilcox log_instance(inst, B_FALSE, "%s: a resource " 47213d8aaa1SSean Wilcox "control assignment failed", errf); 4737c478bd9Sstevel@tonic-gate } else if (strcmp(errf, "pool_set_binding") == 0) { 47413d8aaa1SSean Wilcox log_instance(inst, B_FALSE, "%s: a system " 47513d8aaa1SSean Wilcox "error occurred", errf); 4767c478bd9Sstevel@tonic-gate } else { 4777c478bd9Sstevel@tonic-gate #ifndef NDEBUG 4787c478bd9Sstevel@tonic-gate uu_warn("%s:%d: Bad function name \"%s\" for " 4797c478bd9Sstevel@tonic-gate "error %d from " 4807c478bd9Sstevel@tonic-gate "restarter_set_method_context().\n", 48177c20ef8Sacruz __FILE__, __LINE__, errf, rsmc_errno); 4827c478bd9Sstevel@tonic-gate #endif 4837c478bd9Sstevel@tonic-gate abort(); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate exit(1); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) { 49077c20ef8Sacruz switch (rsmc_errno) { 4917c478bd9Sstevel@tonic-gate case ENOENT: 49213d8aaa1SSean Wilcox log_instance(inst, B_FALSE, "%s: the pool " 49313d8aaa1SSean Wilcox "could not be found", errf); 4947c478bd9Sstevel@tonic-gate break; 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate case EBADF: 49713d8aaa1SSean Wilcox log_instance(inst, B_FALSE, "%s: the " 49813d8aaa1SSean Wilcox "configuration is invalid", errf); 4997c478bd9Sstevel@tonic-gate break; 5007c478bd9Sstevel@tonic-gate 5013ad28c1eSrm88369 case EINVAL: 50213d8aaa1SSean Wilcox log_instance(inst, B_FALSE, "%s: pool name " 50313d8aaa1SSean Wilcox "\"%s\" is invalid", errf, 50413d8aaa1SSean Wilcox mcp->resource_pool); 5053ad28c1eSrm88369 break; 5063ad28c1eSrm88369 5077c478bd9Sstevel@tonic-gate default: 5087c478bd9Sstevel@tonic-gate #ifndef NDEBUG 5097c478bd9Sstevel@tonic-gate uu_warn("%s:%d: Bad error %d for function %s " 5107c478bd9Sstevel@tonic-gate "in restarter_set_method_context().\n", 51177c20ef8Sacruz __FILE__, __LINE__, rsmc_errno, errf); 5127c478bd9Sstevel@tonic-gate #endif 5137c478bd9Sstevel@tonic-gate abort(); 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate exit(SMF_EXIT_ERR_CONFIG); 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate 519*33875201SHengqing Hu if (errf != NULL && strcmp(errf, "chdir") == 0) { 520*33875201SHengqing Hu switch (rsmc_errno) { 521*33875201SHengqing Hu case EACCES: 522*33875201SHengqing Hu case EFAULT: 523*33875201SHengqing Hu case EIO: 524*33875201SHengqing Hu case ELOOP: 525*33875201SHengqing Hu case ENAMETOOLONG: 526*33875201SHengqing Hu case ENOENT: 527*33875201SHengqing Hu case ENOLINK: 528*33875201SHengqing Hu case ENOTDIR: 529*33875201SHengqing Hu log_instance(inst, B_FALSE, "%s: %s (\"%s\")", 530*33875201SHengqing Hu errf, 531*33875201SHengqing Hu strerror(rsmc_errno), mcp->working_dir); 532*33875201SHengqing Hu break; 533*33875201SHengqing Hu 534*33875201SHengqing Hu default: 535*33875201SHengqing Hu #ifndef NDEBUG 536*33875201SHengqing Hu uu_warn("%s:%d: Bad error %d for function %s " 537*33875201SHengqing Hu "in restarter_set_method_context().\n", 538*33875201SHengqing Hu __FILE__, __LINE__, rsmc_errno, errf); 539*33875201SHengqing Hu #endif 540*33875201SHengqing Hu abort(); 541*33875201SHengqing Hu } 542*33875201SHengqing Hu 543*33875201SHengqing Hu exit(SMF_EXIT_ERR_CONFIG); 544*33875201SHengqing Hu } 545*33875201SHengqing Hu 5467c478bd9Sstevel@tonic-gate if (errf != NULL) { 54777c20ef8Sacruz errno = rsmc_errno; 5487c478bd9Sstevel@tonic-gate perror(errf); 5497c478bd9Sstevel@tonic-gate 55077c20ef8Sacruz switch (rsmc_errno) { 5517c478bd9Sstevel@tonic-gate case EINVAL: 5527c478bd9Sstevel@tonic-gate case EPERM: 5537c478bd9Sstevel@tonic-gate case ENOENT: 5547c478bd9Sstevel@tonic-gate case ENAMETOOLONG: 5557c478bd9Sstevel@tonic-gate case ERANGE: 5567c478bd9Sstevel@tonic-gate case ESRCH: 5577c478bd9Sstevel@tonic-gate exit(SMF_EXIT_ERR_CONFIG); 5587c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate default: 5617c478bd9Sstevel@tonic-gate exit(1); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate 56577c20ef8Sacruz switch (rsmc_errno) { 5667c478bd9Sstevel@tonic-gate case ENOMEM: 56713d8aaa1SSean Wilcox log_instance(inst, B_FALSE, "Out of memory."); 5687c478bd9Sstevel@tonic-gate exit(1); 5697c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate case ENOENT: 57213d8aaa1SSean Wilcox log_instance(inst, B_FALSE, "Missing passwd entry for " 57313d8aaa1SSean Wilcox "user."); 5747c478bd9Sstevel@tonic-gate exit(SMF_EXIT_ERR_CONFIG); 5757c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate default: 5787c478bd9Sstevel@tonic-gate #ifndef NDEBUG 5797c478bd9Sstevel@tonic-gate uu_warn("%s:%d: Bad miscellaneous error %d from " 5807c478bd9Sstevel@tonic-gate "restarter_set_method_context().\n", __FILE__, 58177c20ef8Sacruz __LINE__, rsmc_errno); 5827c478bd9Sstevel@tonic-gate #endif 5837c478bd9Sstevel@tonic-gate abort(); 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5873eae19d9Swesolows nenv = set_smf_env(mcp->env, mcp->env_sz, NULL, inst, 5883eae19d9Swesolows method_names[type]); 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate log_preexec(); 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate (void) execle(SBIN_SH, SBIN_SH, "-c", cmd, NULL, nenv); 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate exit(10); 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate static void 5987c478bd9Sstevel@tonic-gate write_status(restarter_inst_t *inst, const char *mname, int stat) 5997c478bd9Sstevel@tonic-gate { 6007c478bd9Sstevel@tonic-gate int r; 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate again: 6037c478bd9Sstevel@tonic-gate if (inst->ri_mi_deleted) 6047c478bd9Sstevel@tonic-gate return; 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate r = libscf_write_method_status(inst->ri_m_inst, mname, stat); 6077c478bd9Sstevel@tonic-gate switch (r) { 6087c478bd9Sstevel@tonic-gate case 0: 6097c478bd9Sstevel@tonic-gate break; 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate case ECONNABORTED: 6127c478bd9Sstevel@tonic-gate libscf_reget_instance(inst); 6137c478bd9Sstevel@tonic-gate goto again; 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate case ECANCELED: 6167c478bd9Sstevel@tonic-gate inst->ri_mi_deleted = 1; 6177c478bd9Sstevel@tonic-gate break; 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate case EPERM: 6207c478bd9Sstevel@tonic-gate case EACCES: 6217c478bd9Sstevel@tonic-gate case EROFS: 6227c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, "Could not write exit status " 6237c478bd9Sstevel@tonic-gate "for %s method of %s: %s.\n", mname, 6247c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, strerror(r)); 6257c478bd9Sstevel@tonic-gate break; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate case ENAMETOOLONG: 6287c478bd9Sstevel@tonic-gate default: 6297c478bd9Sstevel@tonic-gate bad_error("libscf_write_method_status", r); 6307c478bd9Sstevel@tonic-gate } 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate /* 6347c478bd9Sstevel@tonic-gate * int method_run() 6357c478bd9Sstevel@tonic-gate * Execute the type method of instp. If it requires a fork(), wait for it 6367c478bd9Sstevel@tonic-gate * to return and return its exit code in *exit_code. Otherwise set 6377c478bd9Sstevel@tonic-gate * *exit_code to 0 if the method succeeds & -1 if it fails. If the 6387c478bd9Sstevel@tonic-gate * repository connection is broken, it is rebound, but inst may not be 6397c478bd9Sstevel@tonic-gate * reset. 6407c478bd9Sstevel@tonic-gate * Returns 6417c478bd9Sstevel@tonic-gate * 0 - success 6427c478bd9Sstevel@tonic-gate * EINVAL - A correct method or method context couldn't be retrieved. 6437c478bd9Sstevel@tonic-gate * EIO - Contract kill failed. 6447c478bd9Sstevel@tonic-gate * EFAULT - Method couldn't be executed successfully. 6457c478bd9Sstevel@tonic-gate * ELOOP - Retry threshold exceeded. 6467c478bd9Sstevel@tonic-gate * ECANCELED - inst was deleted from the repository before method was run 6477c478bd9Sstevel@tonic-gate * ERANGE - Timeout retry threshold exceeded. 6487c478bd9Sstevel@tonic-gate * EAGAIN - Failed due to external cause, retry. 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate int 6517c478bd9Sstevel@tonic-gate method_run(restarter_inst_t **instp, int type, int *exit_code) 6527c478bd9Sstevel@tonic-gate { 6537c478bd9Sstevel@tonic-gate char *method; 6547c478bd9Sstevel@tonic-gate int ret_status; 6557c478bd9Sstevel@tonic-gate pid_t pid; 6567c478bd9Sstevel@tonic-gate method_restart_t restart_on; 6577c478bd9Sstevel@tonic-gate uint_t cte_mask; 6587c478bd9Sstevel@tonic-gate uint8_t need_session; 6597c478bd9Sstevel@tonic-gate scf_handle_t *h; 6607c478bd9Sstevel@tonic-gate scf_snapshot_t *snap; 6617c478bd9Sstevel@tonic-gate const char *mname; 662870ad75aSSean Wilcox mc_error_t *m_error; 6637c478bd9Sstevel@tonic-gate struct method_context *mcp; 6647c478bd9Sstevel@tonic-gate int result = 0, timeout_fired = 0; 6657c478bd9Sstevel@tonic-gate int sig, r; 6667c478bd9Sstevel@tonic-gate boolean_t transient; 6677c478bd9Sstevel@tonic-gate uint64_t timeout; 6687c478bd9Sstevel@tonic-gate uint8_t timeout_retry; 6697c478bd9Sstevel@tonic-gate ctid_t ctid; 6707c478bd9Sstevel@tonic-gate int ctfd = -1; 6717c478bd9Sstevel@tonic-gate restarter_inst_t *inst = *instp; 6727c478bd9Sstevel@tonic-gate int id = inst->ri_id; 673ca948064Ssl108498 int forkerr; 6747c478bd9Sstevel@tonic-gate 67553f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&inst->ri_lock)); 6767c478bd9Sstevel@tonic-gate assert(instance_in_transition(inst)); 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate if (inst->ri_mi_deleted) 6797c478bd9Sstevel@tonic-gate return (ECANCELED); 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate *exit_code = 0; 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate assert(0 <= type && type <= 2); 6847c478bd9Sstevel@tonic-gate mname = method_names[type]; 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate if (type == METHOD_START) 6877c478bd9Sstevel@tonic-gate inst->ri_pre_online_hook(); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate h = scf_instance_handle(inst->ri_m_inst); 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate snap = scf_snapshot_create(h); 6927c478bd9Sstevel@tonic-gate if (snap == NULL || 6937c478bd9Sstevel@tonic-gate scf_instance_get_snapshot(inst->ri_m_inst, "running", snap) != 0) { 6947c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 6957c478bd9Sstevel@tonic-gate "Could not get running snapshot for %s. " 6967c478bd9Sstevel@tonic-gate "Using editing version to run method %s.\n", 6977c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, mname); 6987c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap); 6997c478bd9Sstevel@tonic-gate snap = NULL; 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate /* 7037c478bd9Sstevel@tonic-gate * After this point, we may be logging to the instance log. 7047c478bd9Sstevel@tonic-gate * Make sure we've noted where that log is as a property of 7057c478bd9Sstevel@tonic-gate * the instance. 7067c478bd9Sstevel@tonic-gate */ 7077c478bd9Sstevel@tonic-gate r = libscf_note_method_log(inst->ri_m_inst, st->st_log_prefix, 7087c478bd9Sstevel@tonic-gate inst->ri_logstem); 7097c478bd9Sstevel@tonic-gate if (r != 0) { 7107c478bd9Sstevel@tonic-gate log_framework(LOG_WARNING, 7117c478bd9Sstevel@tonic-gate "%s: couldn't note log location: %s\n", 7127c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, strerror(r)); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate if ((method = libscf_get_method(h, type, inst, snap, &restart_on, 7167c478bd9Sstevel@tonic-gate &cte_mask, &need_session, &timeout, &timeout_retry)) == NULL) { 7177c478bd9Sstevel@tonic-gate if (errno == LIBSCF_PGROUP_ABSENT) { 7187c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 7197c478bd9Sstevel@tonic-gate "%s: instance has no method property group '%s'.\n", 7207c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, mname); 7217c478bd9Sstevel@tonic-gate if (type == METHOD_REFRESH) 7227c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "No '%s' method " 7237c478bd9Sstevel@tonic-gate "defined. Treating as :true.", mname); 7247c478bd9Sstevel@tonic-gate else 7257c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "Method property " 7267c478bd9Sstevel@tonic-gate "group '%s' is not present.", mname); 7277c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap); 7287c478bd9Sstevel@tonic-gate return (0); 7297c478bd9Sstevel@tonic-gate } else if (errno == LIBSCF_PROPERTY_ABSENT) { 7307c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 7317c478bd9Sstevel@tonic-gate "%s: instance has no '%s/exec' method property.\n", 7327c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, mname); 7337c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "Method property '%s/exec " 7347c478bd9Sstevel@tonic-gate "is not present.", mname); 7357c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap); 7367c478bd9Sstevel@tonic-gate return (0); 7377c478bd9Sstevel@tonic-gate } else { 7387c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, 7397c478bd9Sstevel@tonic-gate "%s: instance libscf_get_method failed\n", 7407c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri); 7417c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap); 7427c478bd9Sstevel@tonic-gate return (EINVAL); 7437c478bd9Sstevel@tonic-gate } 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate /* open service contract if stopping a non-transient service */ 7477c478bd9Sstevel@tonic-gate if (type == METHOD_STOP && (!instance_is_transient_style(inst))) { 7487c478bd9Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid == 0) { 7497c478bd9Sstevel@tonic-gate /* service is not running, nothing to stop */ 7507c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: instance has no primary " 7517c478bd9Sstevel@tonic-gate "contract, no service to stop.\n", 7527c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri); 7537c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap); 7547c478bd9Sstevel@tonic-gate return (0); 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate if ((ctfd = contract_open(inst->ri_i.i_primary_ctid, "process", 7577c478bd9Sstevel@tonic-gate "events", O_RDONLY)) < 0) { 7587c478bd9Sstevel@tonic-gate result = EFAULT; 7597c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "Could not open service " 7605ca87c7fSlianep "contract %ld. Stop method not run.", 7617c478bd9Sstevel@tonic-gate inst->ri_i.i_primary_ctid); 7627c478bd9Sstevel@tonic-gate goto out; 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate if (restarter_is_null_method(method)) { 7677c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: null method succeeds\n", 7687c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri); 7697c478bd9Sstevel@tonic-gate 7705ca87c7fSlianep log_instance(inst, B_TRUE, "Executing %s method (null).", 7715ca87c7fSlianep mname); 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate if (type == METHOD_START) 7747c478bd9Sstevel@tonic-gate write_status(inst, mname, 0); 7757c478bd9Sstevel@tonic-gate goto out; 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate sig = restarter_is_kill_method(method); 7797c478bd9Sstevel@tonic-gate if (sig >= 0) { 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid == 0) { 7827c478bd9Sstevel@tonic-gate log_error(LOG_ERR, "%s: :kill with no contract\n", 7837c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri); 7845ca87c7fSlianep log_instance(inst, B_TRUE, "Invalid use of \":kill\" " 7855ca87c7fSlianep "as stop method for transient service."); 7867c478bd9Sstevel@tonic-gate result = EINVAL; 7877c478bd9Sstevel@tonic-gate goto out; 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 7917c478bd9Sstevel@tonic-gate "%s: :killing contract with signal %d\n", 7927c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, sig); 7937c478bd9Sstevel@tonic-gate 7945ca87c7fSlianep log_instance(inst, B_TRUE, "Executing %s method (:kill).", 7957c478bd9Sstevel@tonic-gate mname); 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate if (contract_kill(inst->ri_i.i_primary_ctid, sig, 7987c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri) != 0) { 7997c478bd9Sstevel@tonic-gate result = EIO; 8007c478bd9Sstevel@tonic-gate goto out; 8017c478bd9Sstevel@tonic-gate } else 8027c478bd9Sstevel@tonic-gate goto assured_kill; 8037c478bd9Sstevel@tonic-gate } 8047c478bd9Sstevel@tonic-gate 8057c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: forking to run method %s\n", 8067c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, method); 8077c478bd9Sstevel@tonic-gate 808870ad75aSSean Wilcox m_error = restarter_get_method_context(RESTARTER_METHOD_CONTEXT_VERSION, 8097c478bd9Sstevel@tonic-gate inst->ri_m_inst, snap, mname, method, &mcp); 8107c478bd9Sstevel@tonic-gate 811870ad75aSSean Wilcox if (m_error != NULL) { 812870ad75aSSean Wilcox log_instance(inst, B_TRUE, "%s", m_error->msg); 813870ad75aSSean Wilcox restarter_mc_error_destroy(m_error); 8147c478bd9Sstevel@tonic-gate result = EINVAL; 8157c478bd9Sstevel@tonic-gate goto out; 8167c478bd9Sstevel@tonic-gate } 8177c478bd9Sstevel@tonic-gate 8187c478bd9Sstevel@tonic-gate r = method_ready_contract(inst, type, restart_on, cte_mask); 8197c478bd9Sstevel@tonic-gate if (r != 0) { 8207c478bd9Sstevel@tonic-gate assert(r == ECANCELED); 8217c478bd9Sstevel@tonic-gate assert(inst->ri_mi_deleted); 8227c478bd9Sstevel@tonic-gate restarter_free_method_context(mcp); 8237c478bd9Sstevel@tonic-gate result = ECANCELED; 8247c478bd9Sstevel@tonic-gate goto out; 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate /* 8287c478bd9Sstevel@tonic-gate * Validate safety of method contexts, to save children work. 8297c478bd9Sstevel@tonic-gate */ 8307c478bd9Sstevel@tonic-gate if (!restarter_rm_libs_loadable()) 8317c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: method contexts limited " 8327c478bd9Sstevel@tonic-gate "to root-accessible libraries\n", inst->ri_i.i_fmri); 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate /* 8357c478bd9Sstevel@tonic-gate * If the service is restarting too quickly, send it to 8367c478bd9Sstevel@tonic-gate * maintenance. 8377c478bd9Sstevel@tonic-gate */ 8387c478bd9Sstevel@tonic-gate if (type == METHOD_START) { 8397c478bd9Sstevel@tonic-gate method_record_start(inst); 8407c478bd9Sstevel@tonic-gate if (method_rate_critical(inst)) { 8417c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "Restarting too quickly, " 8425ca87c7fSlianep "changing state to maintenance."); 8437c478bd9Sstevel@tonic-gate result = ELOOP; 844d3186a0eSjeanm restarter_free_method_context(mcp); 8457c478bd9Sstevel@tonic-gate goto out; 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 849dfe57350Sjeanm atomic_add_16(&storing_contract, 1); 850ca948064Ssl108498 pid = startd_fork1(&forkerr); 8517c478bd9Sstevel@tonic-gate if (pid == 0) 8527c478bd9Sstevel@tonic-gate exec_method(inst, type, method, mcp, need_session); 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate if (pid == -1) { 855dfe57350Sjeanm atomic_add_16(&storing_contract, -1); 856ca948064Ssl108498 if (forkerr == EAGAIN) 857ca948064Ssl108498 result = EAGAIN; 858ca948064Ssl108498 else 8597c478bd9Sstevel@tonic-gate result = EFAULT; 860ca948064Ssl108498 861ca948064Ssl108498 log_error(LOG_WARNING, 862ca948064Ssl108498 "%s: Couldn't fork to execute method %s: %s\n", 863ca948064Ssl108498 inst->ri_i.i_fmri, method, strerror(forkerr)); 864ca948064Ssl108498 865dfe57350Sjeanm restarter_free_method_context(mcp); 8667c478bd9Sstevel@tonic-gate goto out; 8677c478bd9Sstevel@tonic-gate } 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* 8717c478bd9Sstevel@tonic-gate * Get the contract id, decide whether it is primary or transient, and 8727c478bd9Sstevel@tonic-gate * stash it in inst & the repository. 8737c478bd9Sstevel@tonic-gate */ 8747c478bd9Sstevel@tonic-gate method_store_contract(inst, type, &ctid); 875dfe57350Sjeanm atomic_add_16(&storing_contract, -1); 876dfe57350Sjeanm 877dfe57350Sjeanm restarter_free_method_context(mcp); 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate /* 8807c478bd9Sstevel@tonic-gate * Similarly for the start method PID. 8817c478bd9Sstevel@tonic-gate */ 8827c478bd9Sstevel@tonic-gate if (type == METHOD_START && !inst->ri_mi_deleted) 8837c478bd9Sstevel@tonic-gate (void) libscf_write_start_pid(inst->ri_m_inst, pid); 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate if (instance_is_wait_style(inst) && type == METHOD_START) { 8867c478bd9Sstevel@tonic-gate /* Wait style instances don't get timeouts on start methods. */ 8877c478bd9Sstevel@tonic-gate if (wait_register(pid, inst->ri_i.i_fmri, 1, 0)) { 8887c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, 8897c478bd9Sstevel@tonic-gate "%s: couldn't register %ld for wait\n", 8907c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, pid); 8917c478bd9Sstevel@tonic-gate result = EFAULT; 8927c478bd9Sstevel@tonic-gate goto contract_out; 8937c478bd9Sstevel@tonic-gate } 8947c478bd9Sstevel@tonic-gate write_status(inst, mname, 0); 8957c478bd9Sstevel@tonic-gate 8967c478bd9Sstevel@tonic-gate } else { 8977c478bd9Sstevel@tonic-gate int r, err; 8987c478bd9Sstevel@tonic-gate time_t start_time; 8997c478bd9Sstevel@tonic-gate time_t end_time; 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate /* 9027c478bd9Sstevel@tonic-gate * Because on upgrade/live-upgrade we may have no chance 9037c478bd9Sstevel@tonic-gate * to override faulty timeout values on the way to 9047c478bd9Sstevel@tonic-gate * manifest import, all services on the path to manifest 9057c478bd9Sstevel@tonic-gate * import are treated the same as INFINITE timeout services. 9067c478bd9Sstevel@tonic-gate */ 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate start_time = time(NULL); 9097c478bd9Sstevel@tonic-gate if (timeout != METHOD_TIMEOUT_INFINITE && !is_timeout_ovr(inst)) 9107c478bd9Sstevel@tonic-gate timeout_insert(inst, ctid, timeout); 9117c478bd9Sstevel@tonic-gate else 9127c478bd9Sstevel@tonic-gate timeout = METHOD_TIMEOUT_INFINITE; 9137c478bd9Sstevel@tonic-gate 9147c478bd9Sstevel@tonic-gate /* Unlock the instance while waiting for the method. */ 9157c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_lock); 9167c478bd9Sstevel@tonic-gate 91777c20ef8Sacruz do { 9187c478bd9Sstevel@tonic-gate r = waitpid(pid, &ret_status, NULL); 91977c20ef8Sacruz } while (r == -1 && errno == EINTR); 9207c478bd9Sstevel@tonic-gate if (r == -1) 9217c478bd9Sstevel@tonic-gate err = errno; 9227c478bd9Sstevel@tonic-gate 9237c478bd9Sstevel@tonic-gate /* Re-grab the lock. */ 9247c478bd9Sstevel@tonic-gate inst = inst_lookup_by_id(id); 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate /* 9277c478bd9Sstevel@tonic-gate * inst can't be removed, as the removal thread waits 9287c478bd9Sstevel@tonic-gate * for completion of this one. 9297c478bd9Sstevel@tonic-gate */ 9307c478bd9Sstevel@tonic-gate assert(inst != NULL); 9317c478bd9Sstevel@tonic-gate *instp = inst; 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate if (inst->ri_timeout != NULL && inst->ri_timeout->te_fired) 9347c478bd9Sstevel@tonic-gate timeout_fired = 1; 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate timeout_remove(inst, ctid); 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 9397c478bd9Sstevel@tonic-gate "%s method for %s exited with status %d.\n", mname, 9407c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, WEXITSTATUS(ret_status)); 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate if (r == -1) { 9437c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, 9447c478bd9Sstevel@tonic-gate "Couldn't waitpid() for %s method of %s (%s).\n", 9457c478bd9Sstevel@tonic-gate mname, inst->ri_i.i_fmri, strerror(err)); 9467c478bd9Sstevel@tonic-gate result = EFAULT; 9477c478bd9Sstevel@tonic-gate goto contract_out; 9487c478bd9Sstevel@tonic-gate } 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate if (type == METHOD_START) 9517c478bd9Sstevel@tonic-gate write_status(inst, mname, ret_status); 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate /* return ERANGE if this service doesn't retry on timeout */ 9547c478bd9Sstevel@tonic-gate if (timeout_fired == 1 && timeout_retry == 0) { 9557c478bd9Sstevel@tonic-gate result = ERANGE; 9567c478bd9Sstevel@tonic-gate goto contract_out; 9577c478bd9Sstevel@tonic-gate } 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate if (!WIFEXITED(ret_status)) { 9607c478bd9Sstevel@tonic-gate /* 9617c478bd9Sstevel@tonic-gate * If method didn't exit itself (it was killed by an 9627c478bd9Sstevel@tonic-gate * external entity, etc.), consider the entire 9637c478bd9Sstevel@tonic-gate * method_run as failed. 9647c478bd9Sstevel@tonic-gate */ 9657c478bd9Sstevel@tonic-gate if (WIFSIGNALED(ret_status)) { 9667c478bd9Sstevel@tonic-gate char buf[SIG2STR_MAX]; 9677c478bd9Sstevel@tonic-gate (void) sig2str(WTERMSIG(ret_status), buf); 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, "%s: Method \"%s\" " 9707c478bd9Sstevel@tonic-gate "failed due to signal %s.\n", 9717c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, method, buf); 9727c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "Method \"%s\" " 9735ca87c7fSlianep "failed due to signal %s.", mname, buf); 9747c478bd9Sstevel@tonic-gate } else { 9757c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, "%s: Method \"%s\" " 9767c478bd9Sstevel@tonic-gate "failed with exit status %d.\n", 9777c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, method, 9787c478bd9Sstevel@tonic-gate WEXITSTATUS(ret_status)); 9797c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "Method \"%s\" " 9805ca87c7fSlianep "failed with exit status %d.", mname, 9817c478bd9Sstevel@tonic-gate WEXITSTATUS(ret_status)); 9827c478bd9Sstevel@tonic-gate } 9837c478bd9Sstevel@tonic-gate result = EAGAIN; 9847c478bd9Sstevel@tonic-gate goto contract_out; 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate *exit_code = WEXITSTATUS(ret_status); 9887c478bd9Sstevel@tonic-gate if (*exit_code != 0) { 9897c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, 9907c478bd9Sstevel@tonic-gate "%s: Method \"%s\" failed with exit status %d.\n", 9917c478bd9Sstevel@tonic-gate inst->ri_i.i_fmri, method, WEXITSTATUS(ret_status)); 9927c478bd9Sstevel@tonic-gate } 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate log_instance(inst, B_TRUE, "Method \"%s\" exited with status " 9955ca87c7fSlianep "%d.", mname, *exit_code); 9967c478bd9Sstevel@tonic-gate 9977c478bd9Sstevel@tonic-gate if (*exit_code != 0) 9987c478bd9Sstevel@tonic-gate goto contract_out; 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate end_time = time(NULL); 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate /* Give service contract remaining seconds to empty */ 10037c478bd9Sstevel@tonic-gate if (timeout != METHOD_TIMEOUT_INFINITE) 10047c478bd9Sstevel@tonic-gate timeout -= (end_time - start_time); 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate assured_kill: 10087c478bd9Sstevel@tonic-gate /* 10097c478bd9Sstevel@tonic-gate * For stop methods, assure that the service contract has emptied 10107c478bd9Sstevel@tonic-gate * before returning. 10117c478bd9Sstevel@tonic-gate */ 10127c478bd9Sstevel@tonic-gate if (type == METHOD_STOP && (!instance_is_transient_style(inst)) && 10137c478bd9Sstevel@tonic-gate !(contract_is_empty(inst->ri_i.i_primary_ctid))) { 10144d53c7adSDan Price int times = 0; 10157c478bd9Sstevel@tonic-gate 10167c478bd9Sstevel@tonic-gate if (timeout != METHOD_TIMEOUT_INFINITE) 10177c478bd9Sstevel@tonic-gate timeout_insert(inst, inst->ri_i.i_primary_ctid, 10187c478bd9Sstevel@tonic-gate timeout); 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate for (;;) { 10214d53c7adSDan Price /* 10224d53c7adSDan Price * Check frequently at first, then back off. This 10234d53c7adSDan Price * keeps startd from idling while shutting down. 10244d53c7adSDan Price */ 10254d53c7adSDan Price if (times < 20) { 10264d53c7adSDan Price (void) poll(NULL, 0, 5); 10274d53c7adSDan Price times++; 10284d53c7adSDan Price } else { 1029dfe57350Sjeanm (void) poll(NULL, 0, 100); 10304d53c7adSDan Price } 1031dfe57350Sjeanm if (contract_is_empty(inst->ri_i.i_primary_ctid)) 10327c478bd9Sstevel@tonic-gate break; 10337c478bd9Sstevel@tonic-gate } 10347c478bd9Sstevel@tonic-gate 10357c478bd9Sstevel@tonic-gate if (timeout != METHOD_TIMEOUT_INFINITE) 10367c478bd9Sstevel@tonic-gate if (inst->ri_timeout->te_fired) 10377c478bd9Sstevel@tonic-gate result = EFAULT; 10387c478bd9Sstevel@tonic-gate 10397c478bd9Sstevel@tonic-gate timeout_remove(inst, inst->ri_i.i_primary_ctid); 10407c478bd9Sstevel@tonic-gate } 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate contract_out: 10437c478bd9Sstevel@tonic-gate /* Abandon contracts for transient methods & methods that fail. */ 10447c478bd9Sstevel@tonic-gate transient = method_is_transient(inst, type); 10457c478bd9Sstevel@tonic-gate if ((transient || *exit_code != 0 || result != 0) && 10467c478bd9Sstevel@tonic-gate (restarter_is_kill_method(method) < 0)) 10477c478bd9Sstevel@tonic-gate method_remove_contract(inst, !transient, B_TRUE); 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate out: 10507c478bd9Sstevel@tonic-gate if (ctfd >= 0) 10517c478bd9Sstevel@tonic-gate (void) close(ctfd); 10527c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap); 10537c478bd9Sstevel@tonic-gate free(method); 10547c478bd9Sstevel@tonic-gate return (result); 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate 10577c478bd9Sstevel@tonic-gate /* 10587c478bd9Sstevel@tonic-gate * The method thread executes a service method to effect a state transition. 10597c478bd9Sstevel@tonic-gate * The next_state of info->sf_id should be non-_NONE on entrance, and it will 10607c478bd9Sstevel@tonic-gate * be _NONE on exit (state will either be what next_state was (on success), or 10617c478bd9Sstevel@tonic-gate * it will be _MAINT (on error)). 10627c478bd9Sstevel@tonic-gate * 10637c478bd9Sstevel@tonic-gate * There are six classes of methods to consider: start & other (stop, refresh) 10647c478bd9Sstevel@tonic-gate * for each of "normal" services, wait services, and transient services. For 10657c478bd9Sstevel@tonic-gate * each, the method must be fetched from the repository & executed. fork()ed 10667c478bd9Sstevel@tonic-gate * methods must be waited on, except for the start method of wait services 10677c478bd9Sstevel@tonic-gate * (which must be registered with the wait subsystem via wait_register()). If 10687c478bd9Sstevel@tonic-gate * the method succeeded (returned 0), then for start methods its contract 10697c478bd9Sstevel@tonic-gate * should be recorded as the primary contract for the service. For other 10707c478bd9Sstevel@tonic-gate * methods, it should be abandoned. If the method fails, then depending on 10717c478bd9Sstevel@tonic-gate * the failure, either the method should be reexecuted or the service should 10727c478bd9Sstevel@tonic-gate * be put into maintenance. Either way the contract should be abandoned. 10737c478bd9Sstevel@tonic-gate */ 10747c478bd9Sstevel@tonic-gate void * 10757c478bd9Sstevel@tonic-gate method_thread(void *arg) 10767c478bd9Sstevel@tonic-gate { 10777c478bd9Sstevel@tonic-gate fork_info_t *info = arg; 10787c478bd9Sstevel@tonic-gate restarter_inst_t *inst; 10797c478bd9Sstevel@tonic-gate scf_handle_t *local_handle; 10807c478bd9Sstevel@tonic-gate scf_instance_t *s_inst = NULL; 10817c478bd9Sstevel@tonic-gate int r, exit_code; 10827c478bd9Sstevel@tonic-gate boolean_t retryable; 1083f6e214c7SGavin Maltby restarter_str_t reason; 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate assert(0 <= info->sf_method_type && info->sf_method_type <= 2); 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate /* Get (and lock) the restarter_inst_t. */ 10887c478bd9Sstevel@tonic-gate inst = inst_lookup_by_id(info->sf_id); 10897c478bd9Sstevel@tonic-gate 10907c478bd9Sstevel@tonic-gate assert(inst->ri_method_thread != 0); 10917c478bd9Sstevel@tonic-gate assert(instance_in_transition(inst) == 1); 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate /* 10947c478bd9Sstevel@tonic-gate * We cannot leave this function with inst in transition, because 10957c478bd9Sstevel@tonic-gate * protocol.c withholds messages for inst otherwise. 10967c478bd9Sstevel@tonic-gate */ 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "method_thread() running %s method for %s.\n", 10997c478bd9Sstevel@tonic-gate method_names[info->sf_method_type], inst->ri_i.i_fmri); 11007c478bd9Sstevel@tonic-gate 11017c478bd9Sstevel@tonic-gate local_handle = libscf_handle_create_bound_loop(); 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate rebind_retry: 11047c478bd9Sstevel@tonic-gate /* get scf_instance_t */ 11057c478bd9Sstevel@tonic-gate switch (r = libscf_fmri_get_instance(local_handle, inst->ri_i.i_fmri, 11067c478bd9Sstevel@tonic-gate &s_inst)) { 11077c478bd9Sstevel@tonic-gate case 0: 11087c478bd9Sstevel@tonic-gate break; 11097c478bd9Sstevel@tonic-gate 11107c478bd9Sstevel@tonic-gate case ECONNABORTED: 11117c478bd9Sstevel@tonic-gate libscf_handle_rebind(local_handle); 11127c478bd9Sstevel@tonic-gate goto rebind_retry; 11137c478bd9Sstevel@tonic-gate 11147c478bd9Sstevel@tonic-gate case ENOENT: 11157c478bd9Sstevel@tonic-gate /* 11167c478bd9Sstevel@tonic-gate * It's not there, but we need to call this so protocol.c 11177c478bd9Sstevel@tonic-gate * doesn't think it's in transition anymore. 11187c478bd9Sstevel@tonic-gate */ 11197c478bd9Sstevel@tonic-gate (void) restarter_instance_update_states(local_handle, inst, 11207c478bd9Sstevel@tonic-gate inst->ri_i.i_state, RESTARTER_STATE_NONE, RERR_NONE, 1121f6e214c7SGavin Maltby restarter_str_none); 11227c478bd9Sstevel@tonic-gate goto out; 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate case EINVAL: 11257c478bd9Sstevel@tonic-gate case ENOTSUP: 11267c478bd9Sstevel@tonic-gate default: 11277c478bd9Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", r); 11287c478bd9Sstevel@tonic-gate } 11297c478bd9Sstevel@tonic-gate 11307c478bd9Sstevel@tonic-gate inst->ri_m_inst = s_inst; 11317c478bd9Sstevel@tonic-gate inst->ri_mi_deleted = B_FALSE; 11327c478bd9Sstevel@tonic-gate 11337c478bd9Sstevel@tonic-gate retry: 11347c478bd9Sstevel@tonic-gate if (info->sf_method_type == METHOD_START) 11357c478bd9Sstevel@tonic-gate log_transition(inst, START_REQUESTED); 11367c478bd9Sstevel@tonic-gate 11377c478bd9Sstevel@tonic-gate r = method_run(&inst, info->sf_method_type, &exit_code); 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate if (r == 0 && exit_code == 0) { 11407c478bd9Sstevel@tonic-gate /* Success! */ 11417c478bd9Sstevel@tonic-gate assert(inst->ri_i.i_next_state != RESTARTER_STATE_NONE); 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate /* 11447c478bd9Sstevel@tonic-gate * When a stop method succeeds, remove the primary contract of 11457c478bd9Sstevel@tonic-gate * the service, unless we're going to offline, in which case 11467c478bd9Sstevel@tonic-gate * retain the contract so we can transfer inherited contracts to 11477c478bd9Sstevel@tonic-gate * the replacement service. 11487c478bd9Sstevel@tonic-gate */ 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate if (info->sf_method_type == METHOD_STOP && 11517c478bd9Sstevel@tonic-gate inst->ri_i.i_primary_ctid != 0) { 11527c478bd9Sstevel@tonic-gate if (inst->ri_i.i_next_state == RESTARTER_STATE_OFFLINE) 11537c478bd9Sstevel@tonic-gate inst->ri_i.i_primary_ctid_stopped = 1; 11547c478bd9Sstevel@tonic-gate else 11557c478bd9Sstevel@tonic-gate method_remove_contract(inst, B_TRUE, B_TRUE); 11567c478bd9Sstevel@tonic-gate } 11577c478bd9Sstevel@tonic-gate /* 11587c478bd9Sstevel@tonic-gate * We don't care whether the handle was rebound because this is 11597c478bd9Sstevel@tonic-gate * the last thing we do with it. 11607c478bd9Sstevel@tonic-gate */ 11617c478bd9Sstevel@tonic-gate (void) restarter_instance_update_states(local_handle, inst, 11627c478bd9Sstevel@tonic-gate inst->ri_i.i_next_state, RESTARTER_STATE_NONE, 1163f6e214c7SGavin Maltby info->sf_event_type, info->sf_reason); 11647c478bd9Sstevel@tonic-gate 11657c478bd9Sstevel@tonic-gate (void) update_fault_count(inst, FAULT_COUNT_RESET); 11667c478bd9Sstevel@tonic-gate 11677c478bd9Sstevel@tonic-gate goto out; 11687c478bd9Sstevel@tonic-gate } 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate /* Failure. Retry or go to maintenance. */ 11717c478bd9Sstevel@tonic-gate 11727c478bd9Sstevel@tonic-gate if (r != 0 && r != EAGAIN) { 11737c478bd9Sstevel@tonic-gate retryable = B_FALSE; 11747c478bd9Sstevel@tonic-gate } else { 11757c478bd9Sstevel@tonic-gate switch (exit_code) { 11767c478bd9Sstevel@tonic-gate case SMF_EXIT_ERR_CONFIG: 11777c478bd9Sstevel@tonic-gate case SMF_EXIT_ERR_NOSMF: 11787c478bd9Sstevel@tonic-gate case SMF_EXIT_ERR_PERM: 11797c478bd9Sstevel@tonic-gate case SMF_EXIT_ERR_FATAL: 11807c478bd9Sstevel@tonic-gate retryable = B_FALSE; 11817c478bd9Sstevel@tonic-gate break; 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate default: 11847c478bd9Sstevel@tonic-gate retryable = B_TRUE; 11857c478bd9Sstevel@tonic-gate } 11867c478bd9Sstevel@tonic-gate } 11877c478bd9Sstevel@tonic-gate 11887c478bd9Sstevel@tonic-gate if (retryable && update_fault_count(inst, FAULT_COUNT_INCR) != 1) 11897c478bd9Sstevel@tonic-gate goto retry; 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate /* maintenance */ 11927c478bd9Sstevel@tonic-gate if (r == ELOOP) 11937c478bd9Sstevel@tonic-gate log_transition(inst, START_FAILED_REPEATEDLY); 11947c478bd9Sstevel@tonic-gate else if (r == ERANGE) 11957c478bd9Sstevel@tonic-gate log_transition(inst, START_FAILED_TIMEOUT_FATAL); 11967c478bd9Sstevel@tonic-gate else if (exit_code == SMF_EXIT_ERR_CONFIG) 11977c478bd9Sstevel@tonic-gate log_transition(inst, START_FAILED_CONFIGURATION); 11987c478bd9Sstevel@tonic-gate else if (exit_code == SMF_EXIT_ERR_FATAL) 11997c478bd9Sstevel@tonic-gate log_transition(inst, START_FAILED_FATAL); 12007c478bd9Sstevel@tonic-gate else 12017c478bd9Sstevel@tonic-gate log_transition(inst, START_FAILED_OTHER); 12027c478bd9Sstevel@tonic-gate 1203f6e214c7SGavin Maltby if (r == ELOOP) { 1204f6e214c7SGavin Maltby reason = restarter_str_restarting_too_quickly; 1205f6e214c7SGavin Maltby } else if (retryable) { 1206f6e214c7SGavin Maltby reason = restarter_str_fault_threshold_reached; 1207f6e214c7SGavin Maltby } else { 1208f6e214c7SGavin Maltby reason = restarter_str_method_failed; 1209f6e214c7SGavin Maltby } 12107c478bd9Sstevel@tonic-gate 12117c478bd9Sstevel@tonic-gate (void) restarter_instance_update_states(local_handle, inst, 12127c478bd9Sstevel@tonic-gate RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, RERR_FAULT, 1213f6e214c7SGavin Maltby reason); 12147c478bd9Sstevel@tonic-gate 12157c478bd9Sstevel@tonic-gate if (!method_is_transient(inst, info->sf_method_type) && 12167c478bd9Sstevel@tonic-gate inst->ri_i.i_primary_ctid != 0) 12177c478bd9Sstevel@tonic-gate method_remove_contract(inst, B_TRUE, B_TRUE); 12187c478bd9Sstevel@tonic-gate 12197c478bd9Sstevel@tonic-gate out: 12207c478bd9Sstevel@tonic-gate inst->ri_method_thread = 0; 1221addbbe95Srm88369 1222addbbe95Srm88369 /* 1223addbbe95Srm88369 * Unlock the mutex after broadcasting to avoid a race condition 1224addbbe95Srm88369 * with restarter_delete_inst() when the 'inst' structure is freed. 1225addbbe95Srm88369 */ 12267c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&inst->ri_method_cv); 1227addbbe95Srm88369 MUTEX_UNLOCK(&inst->ri_lock); 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate scf_instance_destroy(s_inst); 12307c478bd9Sstevel@tonic-gate scf_handle_destroy(local_handle); 12317c478bd9Sstevel@tonic-gate startd_free(info, sizeof (fork_info_t)); 12327c478bd9Sstevel@tonic-gate return (NULL); 12337c478bd9Sstevel@tonic-gate } 1234