1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #ifdef _FILE_OFFSET_BITS 30*0Sstevel@tonic-gate #undef _FILE_OFFSET_BITS 31*0Sstevel@tonic-gate #endif /* _FILE_OFFSET_BITS */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/contract/process.h> 34*0Sstevel@tonic-gate #include <sys/ctfs.h> 35*0Sstevel@tonic-gate #include <sys/types.h> 36*0Sstevel@tonic-gate #include <assert.h> 37*0Sstevel@tonic-gate #include <errno.h> 38*0Sstevel@tonic-gate #include <fcntl.h> 39*0Sstevel@tonic-gate #include <libcontract.h> 40*0Sstevel@tonic-gate #include <libcontract_priv.h> 41*0Sstevel@tonic-gate #include <libuutil.h> 42*0Sstevel@tonic-gate #include <limits.h> 43*0Sstevel@tonic-gate #include <procfs.h> 44*0Sstevel@tonic-gate #include <signal.h> 45*0Sstevel@tonic-gate #include <string.h> 46*0Sstevel@tonic-gate #include <unistd.h> 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #include "startd.h" 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate void 51*0Sstevel@tonic-gate contract_abandon(ctid_t ctid) 52*0Sstevel@tonic-gate { 53*0Sstevel@tonic-gate int err; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate assert(ctid != 0); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate err = contract_abandon_id(ctid); 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate if (err) 60*0Sstevel@tonic-gate log_framework(LOG_NOTICE, 61*0Sstevel@tonic-gate "failed to abandon contract %ld: %s\n", ctid, 62*0Sstevel@tonic-gate strerror(err)); 63*0Sstevel@tonic-gate } 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate int 66*0Sstevel@tonic-gate contract_kill(ctid_t ctid, int sig, const char *fmri) 67*0Sstevel@tonic-gate { 68*0Sstevel@tonic-gate if (sigsend(P_CTID, ctid, sig) == -1 && errno != ESRCH) { 69*0Sstevel@tonic-gate log_error(LOG_WARNING, 70*0Sstevel@tonic-gate "%s: Could not signal all contract members: %s\n", fmri, 71*0Sstevel@tonic-gate strerror(errno)); 72*0Sstevel@tonic-gate return (-1); 73*0Sstevel@tonic-gate } 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate return (0); 76*0Sstevel@tonic-gate } 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate ctid_t 79*0Sstevel@tonic-gate contract_init() 80*0Sstevel@tonic-gate { 81*0Sstevel@tonic-gate int psfd, csfd; 82*0Sstevel@tonic-gate ctid_t ctid, configd_ctid = -1; 83*0Sstevel@tonic-gate psinfo_t psi; 84*0Sstevel@tonic-gate ct_stathdl_t s; 85*0Sstevel@tonic-gate ctid_t *ctids; 86*0Sstevel@tonic-gate uint_t nctids; 87*0Sstevel@tonic-gate uint_t n; 88*0Sstevel@tonic-gate int err; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate /* 91*0Sstevel@tonic-gate * 2. Acquire any contracts we should have inherited. First, find the 92*0Sstevel@tonic-gate * contract we belong to, then get its status. 93*0Sstevel@tonic-gate */ 94*0Sstevel@tonic-gate if ((psfd = open("/proc/self/psinfo", O_RDONLY)) < 0) { 95*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open /proc/self/psinfo; unable " 96*0Sstevel@tonic-gate "to check to adopt contracts: %s\n", strerror(errno)); 97*0Sstevel@tonic-gate return (-1); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate if (read(psfd, &psi, sizeof (psinfo_t)) != sizeof (psinfo_t)) { 101*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not read from /proc/self/psinfo; " 102*0Sstevel@tonic-gate "unable to adopt contracts: %s\n", 103*0Sstevel@tonic-gate strerror(errno)); 104*0Sstevel@tonic-gate startd_close(psfd); 105*0Sstevel@tonic-gate return (-1); 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate ctid = psi.pr_contract; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate startd_close(psfd); 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate if ((csfd = contract_open(ctid, "process", "status", O_RDONLY)) < 0) { 113*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open containing contract " 114*0Sstevel@tonic-gate "status; unable to adopt contracts: %s\n", strerror(errno)); 115*0Sstevel@tonic-gate return (-1); 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* 3. Go about adopting our member list. */ 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate err = ct_status_read(csfd, CTD_ALL, &s); 121*0Sstevel@tonic-gate startd_close(csfd); 122*0Sstevel@tonic-gate if (err) { 123*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not read containing contract " 124*0Sstevel@tonic-gate "status; unable to adopt: %s\n", strerror(err)); 125*0Sstevel@tonic-gate return (-1); 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate if (err = ct_pr_status_get_contracts(s, &ctids, &nctids)) { 129*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not get my inherited contracts; " 130*0Sstevel@tonic-gate "unable to adopt: %s\n", strerror(err)); 131*0Sstevel@tonic-gate ct_status_free(s); 132*0Sstevel@tonic-gate return (-1); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate if (nctids == 0) { 136*0Sstevel@tonic-gate /* 137*0Sstevel@tonic-gate * We're booting, as a svc.startd which managed to fork a 138*0Sstevel@tonic-gate * child will always have a svc.configd contract to adopt. 139*0Sstevel@tonic-gate */ 140*0Sstevel@tonic-gate st->st_initial = 1; 141*0Sstevel@tonic-gate ct_status_free(s); 142*0Sstevel@tonic-gate return (-1); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* 146*0Sstevel@tonic-gate * We're restarting after an interruption of some kind. 147*0Sstevel@tonic-gate */ 148*0Sstevel@tonic-gate log_framework(LOG_NOTICE, "restarting after interruption\n"); 149*0Sstevel@tonic-gate st->st_initial = 0; 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate /* 152*0Sstevel@tonic-gate * 3'. Loop through the array, adopting them all where possible, and 153*0Sstevel@tonic-gate * noting which one contains svc.configd (via a cookie vlaue of 154*0Sstevel@tonic-gate * CONFIGD_COOKIE). 155*0Sstevel@tonic-gate */ 156*0Sstevel@tonic-gate for (n = 0; n < nctids; n++) { 157*0Sstevel@tonic-gate int ccfd; 158*0Sstevel@tonic-gate ct_stathdl_t cs; 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate if ((ccfd = contract_open(ctids[n], "process", "ctl", 161*0Sstevel@tonic-gate O_WRONLY)) < 0) { 162*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open contract %ld ctl " 163*0Sstevel@tonic-gate "for adoption: %s\n", ctids[n], strerror(err)); 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate continue; 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate if ((csfd = contract_open(ctids[n], "process", "status", 169*0Sstevel@tonic-gate O_RDONLY)) < 0) { 170*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open contract %ld " 171*0Sstevel@tonic-gate "status for cookie: %s\n", ctids[n], strerror(err)); 172*0Sstevel@tonic-gate startd_close(ccfd); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate continue; 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate if (err = ct_ctl_adopt(ccfd)) { 178*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not adopt contract %ld: " 179*0Sstevel@tonic-gate "%s\n", ctids[n], strerror(err)); 180*0Sstevel@tonic-gate startd_close(ccfd); 181*0Sstevel@tonic-gate startd_close(csfd); 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate continue; 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate startd_close(ccfd); 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate if (err = ct_status_read(csfd, CTD_COMMON, &cs)) { 189*0Sstevel@tonic-gate log_error(LOG_WARNING, "Can not read contract %ld" 190*0Sstevel@tonic-gate "status; unable to fetch cookie: %s\n", ctids[n], 191*0Sstevel@tonic-gate strerror(err)); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate ct_status_free(cs); 194*0Sstevel@tonic-gate startd_close(csfd); 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate continue; 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate if (ct_status_get_cookie(cs) == CONFIGD_COOKIE) 200*0Sstevel@tonic-gate configd_ctid = ctids[n]; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate ct_status_free(cs); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate startd_close(csfd); 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate ct_status_free(s); 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate return (configd_ctid); 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate int 213*0Sstevel@tonic-gate contract_is_empty(ctid_t ctid) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate int fd; 216*0Sstevel@tonic-gate ct_stathdl_t ctstat; 217*0Sstevel@tonic-gate pid_t *members; 218*0Sstevel@tonic-gate uint_t num; 219*0Sstevel@tonic-gate int ret; 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate fd = contract_open(ctid, "process", "status", O_RDONLY); 222*0Sstevel@tonic-gate if (fd < 0) 223*0Sstevel@tonic-gate return (1); 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate ret = ct_status_read(fd, CTD_ALL, &ctstat); 226*0Sstevel@tonic-gate (void) close(fd); 227*0Sstevel@tonic-gate if (ret != 0) 228*0Sstevel@tonic-gate return (1); 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate ret = ct_pr_status_get_members(ctstat, &members, &num); 231*0Sstevel@tonic-gate ct_status_free(ctstat); 232*0Sstevel@tonic-gate if (ret != 0) 233*0Sstevel@tonic-gate return (1); 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate if (num == 0) 236*0Sstevel@tonic-gate return (1); 237*0Sstevel@tonic-gate else 238*0Sstevel@tonic-gate return (0); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate typedef struct contract_bucket { 242*0Sstevel@tonic-gate pthread_mutex_t cb_lock; 243*0Sstevel@tonic-gate uu_list_t *cb_list; 244*0Sstevel@tonic-gate } contract_bucket_t; 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate #define CI_HASH_SIZE 64 247*0Sstevel@tonic-gate #define CI_HASH_MASK (CI_HASH_SIZE - 1); 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate /* 250*0Sstevel@tonic-gate * contract_hash is a hash table of contract ids to restarter instance 251*0Sstevel@tonic-gate * IDs. It can be used for quick lookups when processing contract events, 252*0Sstevel@tonic-gate * because the restarter instance lock doesn't need to be held to access 253*0Sstevel@tonic-gate * its entries. 254*0Sstevel@tonic-gate */ 255*0Sstevel@tonic-gate static contract_bucket_t contract_hash[CI_HASH_SIZE]; 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate static contract_bucket_t * 258*0Sstevel@tonic-gate contract_hold_bucket(ctid_t ctid) 259*0Sstevel@tonic-gate { 260*0Sstevel@tonic-gate contract_bucket_t *bp; 261*0Sstevel@tonic-gate int hash; 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate hash = ctid & CI_HASH_MASK; 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate bp = &contract_hash[hash]; 266*0Sstevel@tonic-gate MUTEX_LOCK(&bp->cb_lock); 267*0Sstevel@tonic-gate return (bp); 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate static void 271*0Sstevel@tonic-gate contract_release_bucket(contract_bucket_t *bp) 272*0Sstevel@tonic-gate { 273*0Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&bp->cb_lock)); 274*0Sstevel@tonic-gate MUTEX_UNLOCK(&bp->cb_lock); 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate static contract_entry_t * 278*0Sstevel@tonic-gate contract_lookup(contract_bucket_t *bp, ctid_t ctid) 279*0Sstevel@tonic-gate { 280*0Sstevel@tonic-gate contract_entry_t *ce; 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&bp->cb_lock)); 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate if (bp->cb_list == NULL) 285*0Sstevel@tonic-gate return (NULL); 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate for (ce = uu_list_first(bp->cb_list); ce != NULL; 288*0Sstevel@tonic-gate ce = uu_list_next(bp->cb_list, ce)) { 289*0Sstevel@tonic-gate if (ce->ce_ctid == ctid) 290*0Sstevel@tonic-gate return (ce); 291*0Sstevel@tonic-gate } 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate return (NULL); 294*0Sstevel@tonic-gate } 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate static void 297*0Sstevel@tonic-gate contract_insert(contract_bucket_t *bp, contract_entry_t *ce) 298*0Sstevel@tonic-gate { 299*0Sstevel@tonic-gate int r; 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate if (bp->cb_list == NULL) 302*0Sstevel@tonic-gate bp->cb_list = startd_list_create(contract_list_pool, bp, 0); 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate uu_list_node_init(ce, &ce->ce_link, contract_list_pool); 305*0Sstevel@tonic-gate r = uu_list_insert_before(bp->cb_list, NULL, ce); 306*0Sstevel@tonic-gate assert(r == 0); 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate void 310*0Sstevel@tonic-gate contract_hash_init() 311*0Sstevel@tonic-gate { 312*0Sstevel@tonic-gate int i; 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate for (i = 0; i < CI_HASH_SIZE; i++) 315*0Sstevel@tonic-gate (void) pthread_mutex_init(&contract_hash[i].cb_lock, 316*0Sstevel@tonic-gate &mutex_attrs); 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate void 320*0Sstevel@tonic-gate contract_hash_store(ctid_t ctid, int instid) 321*0Sstevel@tonic-gate { 322*0Sstevel@tonic-gate contract_bucket_t *bp; 323*0Sstevel@tonic-gate contract_entry_t *ce; 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate bp = contract_hold_bucket(ctid); 326*0Sstevel@tonic-gate assert(contract_lookup(bp, ctid) == NULL); 327*0Sstevel@tonic-gate ce = startd_alloc(sizeof (contract_entry_t)); 328*0Sstevel@tonic-gate ce->ce_ctid = ctid; 329*0Sstevel@tonic-gate ce->ce_instid = instid; 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate contract_insert(bp, ce); 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate contract_release_bucket(bp); 334*0Sstevel@tonic-gate } 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate void 337*0Sstevel@tonic-gate contract_hash_remove(ctid_t ctid) 338*0Sstevel@tonic-gate { 339*0Sstevel@tonic-gate contract_bucket_t *bp; 340*0Sstevel@tonic-gate contract_entry_t *ce; 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate bp = contract_hold_bucket(ctid); 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gate ce = contract_lookup(bp, ctid); 345*0Sstevel@tonic-gate if (ce != NULL) { 346*0Sstevel@tonic-gate uu_list_remove(bp->cb_list, ce); 347*0Sstevel@tonic-gate startd_free(ce, sizeof (contract_entry_t)); 348*0Sstevel@tonic-gate } 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate contract_release_bucket(bp); 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate /* 354*0Sstevel@tonic-gate * int lookup_inst_by_contract() 355*0Sstevel@tonic-gate * Lookup the instance id in the hash table by the contract id. 356*0Sstevel@tonic-gate * Returns instid if found, -1 if not. Doesn't do a hold on the 357*0Sstevel@tonic-gate * instance, so a check for continued existence is required. 358*0Sstevel@tonic-gate */ 359*0Sstevel@tonic-gate int 360*0Sstevel@tonic-gate lookup_inst_by_contract(ctid_t ctid) 361*0Sstevel@tonic-gate { 362*0Sstevel@tonic-gate contract_bucket_t *bp; 363*0Sstevel@tonic-gate contract_entry_t *ce; 364*0Sstevel@tonic-gate int id = -1; 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate bp = contract_hold_bucket(ctid); 367*0Sstevel@tonic-gate ce = contract_lookup(bp, ctid); 368*0Sstevel@tonic-gate if (ce != NULL) 369*0Sstevel@tonic-gate id = ce->ce_instid; 370*0Sstevel@tonic-gate contract_release_bucket(bp); 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate return (id); 373*0Sstevel@tonic-gate } 374