10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*11466SRoger.Faulkner@Sun.COM * Common Development and Distribution License (the "License").
6*11466SRoger.Faulkner@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*11466SRoger.Faulkner@Sun.COM
220Sstevel@tonic-gate /*
23*11466SRoger.Faulkner@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #ifdef _FILE_OFFSET_BITS
280Sstevel@tonic-gate #undef _FILE_OFFSET_BITS
290Sstevel@tonic-gate #endif /* _FILE_OFFSET_BITS */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <sys/contract/process.h>
320Sstevel@tonic-gate #include <sys/ctfs.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <assert.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <libcontract.h>
380Sstevel@tonic-gate #include <libcontract_priv.h>
390Sstevel@tonic-gate #include <libuutil.h>
400Sstevel@tonic-gate #include <limits.h>
410Sstevel@tonic-gate #include <procfs.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include "startd.h"
470Sstevel@tonic-gate
480Sstevel@tonic-gate void
contract_abandon(ctid_t ctid)490Sstevel@tonic-gate contract_abandon(ctid_t ctid)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate int err;
520Sstevel@tonic-gate
530Sstevel@tonic-gate assert(ctid != 0);
540Sstevel@tonic-gate
550Sstevel@tonic-gate err = contract_abandon_id(ctid);
560Sstevel@tonic-gate
570Sstevel@tonic-gate if (err)
580Sstevel@tonic-gate log_framework(LOG_NOTICE,
590Sstevel@tonic-gate "failed to abandon contract %ld: %s\n", ctid,
600Sstevel@tonic-gate strerror(err));
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate int
contract_kill(ctid_t ctid,int sig,const char * fmri)640Sstevel@tonic-gate contract_kill(ctid_t ctid, int sig, const char *fmri)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate if (sigsend(P_CTID, ctid, sig) == -1 && errno != ESRCH) {
670Sstevel@tonic-gate log_error(LOG_WARNING,
680Sstevel@tonic-gate "%s: Could not signal all contract members: %s\n", fmri,
690Sstevel@tonic-gate strerror(errno));
700Sstevel@tonic-gate return (-1);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate return (0);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate ctid_t
contract_init()770Sstevel@tonic-gate contract_init()
780Sstevel@tonic-gate {
790Sstevel@tonic-gate int psfd, csfd;
800Sstevel@tonic-gate ctid_t ctid, configd_ctid = -1;
810Sstevel@tonic-gate psinfo_t psi;
820Sstevel@tonic-gate ct_stathdl_t s;
830Sstevel@tonic-gate ctid_t *ctids;
840Sstevel@tonic-gate uint_t nctids;
850Sstevel@tonic-gate uint_t n;
860Sstevel@tonic-gate int err;
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * 2. Acquire any contracts we should have inherited. First, find the
900Sstevel@tonic-gate * contract we belong to, then get its status.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate if ((psfd = open("/proc/self/psinfo", O_RDONLY)) < 0) {
930Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open /proc/self/psinfo; unable "
940Sstevel@tonic-gate "to check to adopt contracts: %s\n", strerror(errno));
950Sstevel@tonic-gate return (-1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (read(psfd, &psi, sizeof (psinfo_t)) != sizeof (psinfo_t)) {
990Sstevel@tonic-gate log_error(LOG_WARNING, "Can not read from /proc/self/psinfo; "
1000Sstevel@tonic-gate "unable to adopt contracts: %s\n",
1010Sstevel@tonic-gate strerror(errno));
1020Sstevel@tonic-gate startd_close(psfd);
1030Sstevel@tonic-gate return (-1);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate ctid = psi.pr_contract;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate startd_close(psfd);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if ((csfd = contract_open(ctid, "process", "status", O_RDONLY)) < 0) {
1110Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open containing contract "
1120Sstevel@tonic-gate "status; unable to adopt contracts: %s\n", strerror(errno));
1130Sstevel@tonic-gate return (-1);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /* 3. Go about adopting our member list. */
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate err = ct_status_read(csfd, CTD_ALL, &s);
1190Sstevel@tonic-gate startd_close(csfd);
1200Sstevel@tonic-gate if (err) {
1210Sstevel@tonic-gate log_error(LOG_WARNING, "Can not read containing contract "
1220Sstevel@tonic-gate "status; unable to adopt: %s\n", strerror(err));
1230Sstevel@tonic-gate return (-1);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (err = ct_pr_status_get_contracts(s, &ctids, &nctids)) {
1270Sstevel@tonic-gate log_error(LOG_WARNING, "Can not get my inherited contracts; "
1280Sstevel@tonic-gate "unable to adopt: %s\n", strerror(err));
1290Sstevel@tonic-gate ct_status_free(s);
1300Sstevel@tonic-gate return (-1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate if (nctids == 0) {
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate * We're booting, as a svc.startd which managed to fork a
1360Sstevel@tonic-gate * child will always have a svc.configd contract to adopt.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate st->st_initial = 1;
1390Sstevel@tonic-gate ct_status_free(s);
1400Sstevel@tonic-gate return (-1);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate * We're restarting after an interruption of some kind.
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate log_framework(LOG_NOTICE, "restarting after interruption\n");
1470Sstevel@tonic-gate st->st_initial = 0;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * 3'. Loop through the array, adopting them all where possible, and
1510Sstevel@tonic-gate * noting which one contains svc.configd (via a cookie vlaue of
1520Sstevel@tonic-gate * CONFIGD_COOKIE).
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate for (n = 0; n < nctids; n++) {
1550Sstevel@tonic-gate int ccfd;
1560Sstevel@tonic-gate ct_stathdl_t cs;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if ((ccfd = contract_open(ctids[n], "process", "ctl",
1590Sstevel@tonic-gate O_WRONLY)) < 0) {
1600Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open contract %ld ctl "
1610Sstevel@tonic-gate "for adoption: %s\n", ctids[n], strerror(err));
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate continue;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if ((csfd = contract_open(ctids[n], "process", "status",
1670Sstevel@tonic-gate O_RDONLY)) < 0) {
1680Sstevel@tonic-gate log_error(LOG_WARNING, "Can not open contract %ld "
1690Sstevel@tonic-gate "status for cookie: %s\n", ctids[n], strerror(err));
1700Sstevel@tonic-gate startd_close(ccfd);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate continue;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate if (err = ct_ctl_adopt(ccfd)) {
1760Sstevel@tonic-gate log_error(LOG_WARNING, "Can not adopt contract %ld: "
1770Sstevel@tonic-gate "%s\n", ctids[n], strerror(err));
1780Sstevel@tonic-gate startd_close(ccfd);
1790Sstevel@tonic-gate startd_close(csfd);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate continue;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate startd_close(ccfd);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if (err = ct_status_read(csfd, CTD_COMMON, &cs)) {
1870Sstevel@tonic-gate log_error(LOG_WARNING, "Can not read contract %ld"
1880Sstevel@tonic-gate "status; unable to fetch cookie: %s\n", ctids[n],
1890Sstevel@tonic-gate strerror(err));
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate ct_status_free(cs);
1920Sstevel@tonic-gate startd_close(csfd);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate continue;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (ct_status_get_cookie(cs) == CONFIGD_COOKIE)
1980Sstevel@tonic-gate configd_ctid = ctids[n];
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate ct_status_free(cs);
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate startd_close(csfd);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate ct_status_free(s);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate return (configd_ctid);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate int
contract_is_empty(ctid_t ctid)2110Sstevel@tonic-gate contract_is_empty(ctid_t ctid)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate int fd;
2140Sstevel@tonic-gate ct_stathdl_t ctstat;
2150Sstevel@tonic-gate pid_t *members;
2160Sstevel@tonic-gate uint_t num;
2170Sstevel@tonic-gate int ret;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate fd = contract_open(ctid, "process", "status", O_RDONLY);
2200Sstevel@tonic-gate if (fd < 0)
2210Sstevel@tonic-gate return (1);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate ret = ct_status_read(fd, CTD_ALL, &ctstat);
2240Sstevel@tonic-gate (void) close(fd);
2250Sstevel@tonic-gate if (ret != 0)
2260Sstevel@tonic-gate return (1);
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate ret = ct_pr_status_get_members(ctstat, &members, &num);
2290Sstevel@tonic-gate ct_status_free(ctstat);
2300Sstevel@tonic-gate if (ret != 0)
2310Sstevel@tonic-gate return (1);
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (num == 0)
2340Sstevel@tonic-gate return (1);
2350Sstevel@tonic-gate else
2360Sstevel@tonic-gate return (0);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate typedef struct contract_bucket {
2400Sstevel@tonic-gate pthread_mutex_t cb_lock;
2410Sstevel@tonic-gate uu_list_t *cb_list;
2420Sstevel@tonic-gate } contract_bucket_t;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate #define CI_HASH_SIZE 64
2450Sstevel@tonic-gate #define CI_HASH_MASK (CI_HASH_SIZE - 1);
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate * contract_hash is a hash table of contract ids to restarter instance
2490Sstevel@tonic-gate * IDs. It can be used for quick lookups when processing contract events,
2500Sstevel@tonic-gate * because the restarter instance lock doesn't need to be held to access
2510Sstevel@tonic-gate * its entries.
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate static contract_bucket_t contract_hash[CI_HASH_SIZE];
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate static contract_bucket_t *
contract_hold_bucket(ctid_t ctid)2560Sstevel@tonic-gate contract_hold_bucket(ctid_t ctid)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate contract_bucket_t *bp;
2590Sstevel@tonic-gate int hash;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate hash = ctid & CI_HASH_MASK;
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate bp = &contract_hash[hash];
2640Sstevel@tonic-gate MUTEX_LOCK(&bp->cb_lock);
2650Sstevel@tonic-gate return (bp);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate static void
contract_release_bucket(contract_bucket_t * bp)2690Sstevel@tonic-gate contract_release_bucket(contract_bucket_t *bp)
2700Sstevel@tonic-gate {
271*11466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&bp->cb_lock));
2720Sstevel@tonic-gate MUTEX_UNLOCK(&bp->cb_lock);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate static contract_entry_t *
contract_lookup(contract_bucket_t * bp,ctid_t ctid)2760Sstevel@tonic-gate contract_lookup(contract_bucket_t *bp, ctid_t ctid)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate contract_entry_t *ce;
2790Sstevel@tonic-gate
280*11466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&bp->cb_lock));
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate if (bp->cb_list == NULL)
2830Sstevel@tonic-gate return (NULL);
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate for (ce = uu_list_first(bp->cb_list); ce != NULL;
2860Sstevel@tonic-gate ce = uu_list_next(bp->cb_list, ce)) {
2870Sstevel@tonic-gate if (ce->ce_ctid == ctid)
2880Sstevel@tonic-gate return (ce);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate return (NULL);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate static void
contract_insert(contract_bucket_t * bp,contract_entry_t * ce)2950Sstevel@tonic-gate contract_insert(contract_bucket_t *bp, contract_entry_t *ce)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate int r;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate if (bp->cb_list == NULL)
3000Sstevel@tonic-gate bp->cb_list = startd_list_create(contract_list_pool, bp, 0);
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate uu_list_node_init(ce, &ce->ce_link, contract_list_pool);
3030Sstevel@tonic-gate r = uu_list_insert_before(bp->cb_list, NULL, ce);
3040Sstevel@tonic-gate assert(r == 0);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate void
contract_hash_init()3080Sstevel@tonic-gate contract_hash_init()
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate int i;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate for (i = 0; i < CI_HASH_SIZE; i++)
3130Sstevel@tonic-gate (void) pthread_mutex_init(&contract_hash[i].cb_lock,
3140Sstevel@tonic-gate &mutex_attrs);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate void
contract_hash_store(ctid_t ctid,int instid)3180Sstevel@tonic-gate contract_hash_store(ctid_t ctid, int instid)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate contract_bucket_t *bp;
3210Sstevel@tonic-gate contract_entry_t *ce;
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate bp = contract_hold_bucket(ctid);
3240Sstevel@tonic-gate assert(contract_lookup(bp, ctid) == NULL);
3250Sstevel@tonic-gate ce = startd_alloc(sizeof (contract_entry_t));
3260Sstevel@tonic-gate ce->ce_ctid = ctid;
3270Sstevel@tonic-gate ce->ce_instid = instid;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate contract_insert(bp, ce);
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate contract_release_bucket(bp);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate void
contract_hash_remove(ctid_t ctid)3350Sstevel@tonic-gate contract_hash_remove(ctid_t ctid)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate contract_bucket_t *bp;
3380Sstevel@tonic-gate contract_entry_t *ce;
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate bp = contract_hold_bucket(ctid);
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate ce = contract_lookup(bp, ctid);
3430Sstevel@tonic-gate if (ce != NULL) {
3440Sstevel@tonic-gate uu_list_remove(bp->cb_list, ce);
3450Sstevel@tonic-gate startd_free(ce, sizeof (contract_entry_t));
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate contract_release_bucket(bp);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate * int lookup_inst_by_contract()
3530Sstevel@tonic-gate * Lookup the instance id in the hash table by the contract id.
3540Sstevel@tonic-gate * Returns instid if found, -1 if not. Doesn't do a hold on the
3550Sstevel@tonic-gate * instance, so a check for continued existence is required.
3560Sstevel@tonic-gate */
3570Sstevel@tonic-gate int
lookup_inst_by_contract(ctid_t ctid)3580Sstevel@tonic-gate lookup_inst_by_contract(ctid_t ctid)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate contract_bucket_t *bp;
3610Sstevel@tonic-gate contract_entry_t *ce;
3620Sstevel@tonic-gate int id = -1;
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate bp = contract_hold_bucket(ctid);
3650Sstevel@tonic-gate ce = contract_lookup(bp, ctid);
3660Sstevel@tonic-gate if (ce != NULL)
3670Sstevel@tonic-gate id = ce->ce_instid;
3680Sstevel@tonic-gate contract_release_bucket(bp);
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate return (id);
3710Sstevel@tonic-gate }
372