xref: /illumos-gate/usr/src/cmd/svc/startd/contract.c (revision 53f3aea0)
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
5*53f3aea0SRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*53f3aea0SRoger A. Faulkner  * 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  */
21*53f3aea0SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*53f3aea0SRoger A. Faulkner  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifdef _FILE_OFFSET_BITS
287c478bd9Sstevel@tonic-gate #undef _FILE_OFFSET_BITS
297c478bd9Sstevel@tonic-gate #endif /* _FILE_OFFSET_BITS */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
327c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <assert.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <libcontract.h>
387c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
397c478bd9Sstevel@tonic-gate #include <libuutil.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <procfs.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include "startd.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate void
contract_abandon(ctid_t ctid)497c478bd9Sstevel@tonic-gate contract_abandon(ctid_t ctid)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	int err;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	assert(ctid != 0);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	err = contract_abandon_id(ctid);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (err)
587c478bd9Sstevel@tonic-gate 		log_framework(LOG_NOTICE,
597c478bd9Sstevel@tonic-gate 		    "failed to abandon contract %ld: %s\n", ctid,
607c478bd9Sstevel@tonic-gate 		    strerror(err));
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate int
contract_kill(ctid_t ctid,int sig,const char * fmri)647c478bd9Sstevel@tonic-gate contract_kill(ctid_t ctid, int sig, const char *fmri)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	if (sigsend(P_CTID, ctid, sig) == -1 && errno != ESRCH) {
677c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
687c478bd9Sstevel@tonic-gate 		    "%s: Could not signal all contract members: %s\n", fmri,
697c478bd9Sstevel@tonic-gate 		    strerror(errno));
707c478bd9Sstevel@tonic-gate 		return (-1);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	return (0);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate ctid_t
contract_init()777c478bd9Sstevel@tonic-gate contract_init()
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	int psfd, csfd;
807c478bd9Sstevel@tonic-gate 	ctid_t ctid, configd_ctid = -1;
817c478bd9Sstevel@tonic-gate 	psinfo_t psi;
827c478bd9Sstevel@tonic-gate 	ct_stathdl_t s;
837c478bd9Sstevel@tonic-gate 	ctid_t *ctids;
847c478bd9Sstevel@tonic-gate 	uint_t nctids;
857c478bd9Sstevel@tonic-gate 	uint_t n;
867c478bd9Sstevel@tonic-gate 	int err;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * 2.  Acquire any contracts we should have inherited.  First, find the
907c478bd9Sstevel@tonic-gate 	 * contract we belong to, then get its status.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if ((psfd = open("/proc/self/psinfo", O_RDONLY)) < 0) {
937c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Can not open /proc/self/psinfo; unable "
947c478bd9Sstevel@tonic-gate 		    "to check to adopt contracts: %s\n", strerror(errno));
957c478bd9Sstevel@tonic-gate 		return (-1);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (read(psfd, &psi, sizeof (psinfo_t)) != sizeof (psinfo_t)) {
997c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Can not read from /proc/self/psinfo; "
1007c478bd9Sstevel@tonic-gate 		    "unable to adopt contracts: %s\n",
1017c478bd9Sstevel@tonic-gate 		    strerror(errno));
1027c478bd9Sstevel@tonic-gate 		startd_close(psfd);
1037c478bd9Sstevel@tonic-gate 		return (-1);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	ctid = psi.pr_contract;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	startd_close(psfd);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if ((csfd = contract_open(ctid, "process", "status", O_RDONLY)) < 0) {
1117c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Can not open containing contract "
1127c478bd9Sstevel@tonic-gate 		    "status; unable to adopt contracts: %s\n", strerror(errno));
1137c478bd9Sstevel@tonic-gate 		return (-1);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* 3.  Go about adopting our member list. */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	err = ct_status_read(csfd, CTD_ALL, &s);
1197c478bd9Sstevel@tonic-gate 	startd_close(csfd);
1207c478bd9Sstevel@tonic-gate 	if (err) {
1217c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Can not read containing contract "
1227c478bd9Sstevel@tonic-gate 		    "status; unable to adopt: %s\n", strerror(err));
1237c478bd9Sstevel@tonic-gate 		return (-1);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (err = ct_pr_status_get_contracts(s, &ctids, &nctids)) {
1277c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Can not get my inherited contracts; "
1287c478bd9Sstevel@tonic-gate 		    "unable to adopt: %s\n", strerror(err));
1297c478bd9Sstevel@tonic-gate 		ct_status_free(s);
1307c478bd9Sstevel@tonic-gate 		return (-1);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (nctids == 0) {
1347c478bd9Sstevel@tonic-gate 		/*
1357c478bd9Sstevel@tonic-gate 		 * We're booting, as a svc.startd which managed to fork a
1367c478bd9Sstevel@tonic-gate 		 * child will always have a svc.configd contract to adopt.
1377c478bd9Sstevel@tonic-gate 		 */
1387c478bd9Sstevel@tonic-gate 		st->st_initial = 1;
1397c478bd9Sstevel@tonic-gate 		ct_status_free(s);
1407c478bd9Sstevel@tonic-gate 		return (-1);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * We're restarting after an interruption of some kind.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	log_framework(LOG_NOTICE, "restarting after interruption\n");
1477c478bd9Sstevel@tonic-gate 	st->st_initial = 0;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * 3'.  Loop through the array, adopting them all where possible, and
1517c478bd9Sstevel@tonic-gate 	 * noting which one contains svc.configd (via a cookie vlaue of
1527c478bd9Sstevel@tonic-gate 	 * CONFIGD_COOKIE).
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	for (n = 0; n < nctids; n++) {
1557c478bd9Sstevel@tonic-gate 		int ccfd;
1567c478bd9Sstevel@tonic-gate 		ct_stathdl_t cs;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		if ((ccfd = contract_open(ctids[n], "process", "ctl",
1597c478bd9Sstevel@tonic-gate 		    O_WRONLY)) < 0) {
1607c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING, "Can not open contract %ld ctl "
1617c478bd9Sstevel@tonic-gate 			    "for adoption: %s\n", ctids[n], strerror(err));
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 			continue;
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		if ((csfd = contract_open(ctids[n], "process", "status",
1677c478bd9Sstevel@tonic-gate 		    O_RDONLY)) < 0) {
1687c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING, "Can not open contract %ld "
1697c478bd9Sstevel@tonic-gate 			    "status for cookie: %s\n", ctids[n], strerror(err));
1707c478bd9Sstevel@tonic-gate 			startd_close(ccfd);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 			continue;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (err = ct_ctl_adopt(ccfd)) {
1767c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING, "Can not adopt contract %ld: "
1777c478bd9Sstevel@tonic-gate 			    "%s\n", ctids[n], strerror(err));
1787c478bd9Sstevel@tonic-gate 			startd_close(ccfd);
1797c478bd9Sstevel@tonic-gate 			startd_close(csfd);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 			continue;
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		startd_close(ccfd);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		if (err = ct_status_read(csfd, CTD_COMMON, &cs)) {
1877c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING, "Can not read contract %ld"
1887c478bd9Sstevel@tonic-gate 			    "status; unable to fetch cookie: %s\n", ctids[n],
1897c478bd9Sstevel@tonic-gate 			    strerror(err));
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 			ct_status_free(cs);
1927c478bd9Sstevel@tonic-gate 			startd_close(csfd);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 			continue;
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if (ct_status_get_cookie(cs) == CONFIGD_COOKIE)
1987c478bd9Sstevel@tonic-gate 			configd_ctid = ctids[n];
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		ct_status_free(cs);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		startd_close(csfd);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	ct_status_free(s);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (configd_ctid);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate int
contract_is_empty(ctid_t ctid)2117c478bd9Sstevel@tonic-gate contract_is_empty(ctid_t ctid)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	int fd;
2147c478bd9Sstevel@tonic-gate 	ct_stathdl_t ctstat;
2157c478bd9Sstevel@tonic-gate 	pid_t *members;
2167c478bd9Sstevel@tonic-gate 	uint_t num;
2177c478bd9Sstevel@tonic-gate 	int ret;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	fd = contract_open(ctid, "process", "status", O_RDONLY);
2207c478bd9Sstevel@tonic-gate 	if (fd < 0)
2217c478bd9Sstevel@tonic-gate 		return (1);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	ret = ct_status_read(fd, CTD_ALL, &ctstat);
2247c478bd9Sstevel@tonic-gate 	(void) close(fd);
2257c478bd9Sstevel@tonic-gate 	if (ret != 0)
2267c478bd9Sstevel@tonic-gate 		return (1);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	ret = ct_pr_status_get_members(ctstat, &members, &num);
2297c478bd9Sstevel@tonic-gate 	ct_status_free(ctstat);
2307c478bd9Sstevel@tonic-gate 	if (ret != 0)
2317c478bd9Sstevel@tonic-gate 		return (1);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	if (num == 0)
2347c478bd9Sstevel@tonic-gate 		return (1);
2357c478bd9Sstevel@tonic-gate 	else
2367c478bd9Sstevel@tonic-gate 		return (0);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate typedef struct contract_bucket {
2407c478bd9Sstevel@tonic-gate 	pthread_mutex_t cb_lock;
2417c478bd9Sstevel@tonic-gate 	uu_list_t	*cb_list;
2427c478bd9Sstevel@tonic-gate } contract_bucket_t;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #define	CI_HASH_SIZE	64
2457c478bd9Sstevel@tonic-gate #define	CI_HASH_MASK	(CI_HASH_SIZE - 1);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * contract_hash is a hash table of contract ids to restarter instance
2497c478bd9Sstevel@tonic-gate  * IDs.  It can be used for quick lookups when processing contract events,
2507c478bd9Sstevel@tonic-gate  * because the restarter instance lock doesn't need to be held to access
2517c478bd9Sstevel@tonic-gate  * its entries.
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate static contract_bucket_t contract_hash[CI_HASH_SIZE];
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate static contract_bucket_t *
contract_hold_bucket(ctid_t ctid)2567c478bd9Sstevel@tonic-gate contract_hold_bucket(ctid_t ctid)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	contract_bucket_t *bp;
2597c478bd9Sstevel@tonic-gate 	int hash;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	hash = ctid & CI_HASH_MASK;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	bp = &contract_hash[hash];
2647c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&bp->cb_lock);
2657c478bd9Sstevel@tonic-gate 	return (bp);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static void
contract_release_bucket(contract_bucket_t * bp)2697c478bd9Sstevel@tonic-gate contract_release_bucket(contract_bucket_t *bp)
2707c478bd9Sstevel@tonic-gate {
271*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&bp->cb_lock));
2727c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&bp->cb_lock);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static contract_entry_t *
contract_lookup(contract_bucket_t * bp,ctid_t ctid)2767c478bd9Sstevel@tonic-gate contract_lookup(contract_bucket_t *bp, ctid_t ctid)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	contract_entry_t *ce;
2797c478bd9Sstevel@tonic-gate 
280*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&bp->cb_lock));
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (bp->cb_list == NULL)
2837c478bd9Sstevel@tonic-gate 		return (NULL);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	for (ce = uu_list_first(bp->cb_list); ce != NULL;
2867c478bd9Sstevel@tonic-gate 	    ce = uu_list_next(bp->cb_list, ce)) {
2877c478bd9Sstevel@tonic-gate 		if (ce->ce_ctid == ctid)
2887c478bd9Sstevel@tonic-gate 			return (ce);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	return (NULL);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate static void
contract_insert(contract_bucket_t * bp,contract_entry_t * ce)2957c478bd9Sstevel@tonic-gate contract_insert(contract_bucket_t *bp, contract_entry_t *ce)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	int r;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (bp->cb_list == NULL)
3007c478bd9Sstevel@tonic-gate 		bp->cb_list = startd_list_create(contract_list_pool, bp, 0);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	uu_list_node_init(ce, &ce->ce_link, contract_list_pool);
3037c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(bp->cb_list, NULL, ce);
3047c478bd9Sstevel@tonic-gate 	assert(r == 0);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate void
contract_hash_init()3087c478bd9Sstevel@tonic-gate contract_hash_init()
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	int i;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	for (i = 0; i < CI_HASH_SIZE; i++)
3137c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_init(&contract_hash[i].cb_lock,
3147c478bd9Sstevel@tonic-gate 		    &mutex_attrs);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate void
contract_hash_store(ctid_t ctid,int instid)3187c478bd9Sstevel@tonic-gate contract_hash_store(ctid_t ctid, int instid)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	contract_bucket_t *bp;
3217c478bd9Sstevel@tonic-gate 	contract_entry_t *ce;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	bp = contract_hold_bucket(ctid);
3247c478bd9Sstevel@tonic-gate 	assert(contract_lookup(bp, ctid) == NULL);
3257c478bd9Sstevel@tonic-gate 	ce = startd_alloc(sizeof (contract_entry_t));
3267c478bd9Sstevel@tonic-gate 	ce->ce_ctid = ctid;
3277c478bd9Sstevel@tonic-gate 	ce->ce_instid = instid;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	contract_insert(bp, ce);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	contract_release_bucket(bp);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate void
contract_hash_remove(ctid_t ctid)3357c478bd9Sstevel@tonic-gate contract_hash_remove(ctid_t ctid)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	contract_bucket_t *bp;
3387c478bd9Sstevel@tonic-gate 	contract_entry_t *ce;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	bp = contract_hold_bucket(ctid);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	ce = contract_lookup(bp, ctid);
3437c478bd9Sstevel@tonic-gate 	if (ce != NULL) {
3447c478bd9Sstevel@tonic-gate 		uu_list_remove(bp->cb_list, ce);
3457c478bd9Sstevel@tonic-gate 		startd_free(ce, sizeof (contract_entry_t));
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	contract_release_bucket(bp);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * int lookup_inst_by_contract()
3537c478bd9Sstevel@tonic-gate  *   Lookup the instance id in the hash table by the contract id.
3547c478bd9Sstevel@tonic-gate  *   Returns instid if found, -1 if not.  Doesn't do a hold on the
3557c478bd9Sstevel@tonic-gate  *   instance, so a check for continued existence is required.
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate int
lookup_inst_by_contract(ctid_t ctid)3587c478bd9Sstevel@tonic-gate lookup_inst_by_contract(ctid_t ctid)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	contract_bucket_t *bp;
3617c478bd9Sstevel@tonic-gate 	contract_entry_t *ce;
3627c478bd9Sstevel@tonic-gate 	int id = -1;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	bp = contract_hold_bucket(ctid);
3657c478bd9Sstevel@tonic-gate 	ce = contract_lookup(bp, ctid);
3667c478bd9Sstevel@tonic-gate 	if (ce != NULL)
3677c478bd9Sstevel@tonic-gate 		id = ce->ce_instid;
3687c478bd9Sstevel@tonic-gate 	contract_release_bucket(bp);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	return (id);
3717c478bd9Sstevel@tonic-gate }
372