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  *
24*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25*0Sstevel@tonic-gate  * Use is subject to license terms.
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * This file contains routines to manipulate lists of repository values that
32*0Sstevel@tonic-gate  * are used to store process ids and the internal state. There are routines
33*0Sstevel@tonic-gate  * to read/write the lists from/to the repository and routines to modify or
34*0Sstevel@tonic-gate  * inspect the lists. It also contains routines that deal with the
35*0Sstevel@tonic-gate  * repository side of contract ids.
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <libintl.h>
41*0Sstevel@tonic-gate #include <unistd.h>
42*0Sstevel@tonic-gate #include <string.h>
43*0Sstevel@tonic-gate #include <signal.h>
44*0Sstevel@tonic-gate #include "inetd_impl.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * Number of consecutive repository bind retries performed by bind_to_rep()
49*0Sstevel@tonic-gate  * before failing.
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate #define	BIND_TO_REP_RETRIES	10
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /* Name of property group where inetd's state for a service is stored. */
54*0Sstevel@tonic-gate #define	PG_NAME_INSTANCE_STATE (const char *) "inetd_state"
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /* uu_list repval list pool */
57*0Sstevel@tonic-gate static uu_list_pool_t *rep_val_pool = NULL;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Repository object pointers that get set-up in repval_init() and closed down
61*0Sstevel@tonic-gate  * in repval_fini(). They're used in _retrieve_rep_vals(), _store_rep_vals(),
62*0Sstevel@tonic-gate  * add_remove_contract_norebind(), and adopt_repository_contracts().  They're
63*0Sstevel@tonic-gate  * global so they can be initialized once on inetd startup, and re-used
64*0Sstevel@tonic-gate  * there-after in the referenced functions.
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate static scf_handle_t		*rep_handle = NULL;
67*0Sstevel@tonic-gate static scf_propertygroup_t	*pg = NULL;
68*0Sstevel@tonic-gate static scf_instance_t		*inst = NULL;
69*0Sstevel@tonic-gate static scf_transaction_t	*trans = NULL;
70*0Sstevel@tonic-gate static scf_transaction_entry_t	*entry = NULL;
71*0Sstevel@tonic-gate static scf_property_t		*prop = NULL;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Try and make the given handle bind be bound to the repository. If
75*0Sstevel@tonic-gate  * it's already bound, or we succeed a new bind return 0; else return
76*0Sstevel@tonic-gate  * -1 on failure, with the SCF error set to one of the following:
77*0Sstevel@tonic-gate  * SCF_ERROR_NO_SERVER
78*0Sstevel@tonic-gate  * SCF_ERROR_NO_RESOURCES
79*0Sstevel@tonic-gate  */
80*0Sstevel@tonic-gate int
81*0Sstevel@tonic-gate make_handle_bound(scf_handle_t *hdl)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	uint_t retries;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	for (retries = 0; retries <= BIND_TO_REP_RETRIES; retries++) {
86*0Sstevel@tonic-gate 		if ((scf_handle_bind(hdl) == 0) ||
87*0Sstevel@tonic-gate 		    (scf_error() == SCF_ERROR_IN_USE))
88*0Sstevel@tonic-gate 			return (0);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 		assert(scf_error() != SCF_ERROR_INVALID_ARGUMENT);
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	return (-1);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate int
97*0Sstevel@tonic-gate repval_init(void)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	debug_msg("Entering repval_init");
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/*
102*0Sstevel@tonic-gate 	 * Create the repval list pool.
103*0Sstevel@tonic-gate 	 */
104*0Sstevel@tonic-gate 	rep_val_pool = uu_list_pool_create("rep_val_pool", sizeof (rep_val_t),
105*0Sstevel@tonic-gate 	    offsetof(rep_val_t, link), NULL, UU_LIST_POOL_DEBUG);
106*0Sstevel@tonic-gate 	if (rep_val_pool == NULL) {
107*0Sstevel@tonic-gate 		error_msg("%s: %s", gettext("Failed to create rep_val pool"),
108*0Sstevel@tonic-gate 		    uu_strerror(uu_error()));
109*0Sstevel@tonic-gate 		return (-1);
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	/*
113*0Sstevel@tonic-gate 	 * Create and bind a repository handle, and create all repository
114*0Sstevel@tonic-gate 	 * objects that we'll use later that are associated with it. On any
115*0Sstevel@tonic-gate 	 * errors we simply return -1 and let repval_fini() clean-up after
116*0Sstevel@tonic-gate 	 * us.
117*0Sstevel@tonic-gate 	 */
118*0Sstevel@tonic-gate 	if ((rep_handle = scf_handle_create(SCF_VERSION)) == NULL) {
119*0Sstevel@tonic-gate 		error_msg("%s: %s",
120*0Sstevel@tonic-gate 		    gettext("Failed to create repository handle"),
121*0Sstevel@tonic-gate 		    scf_strerror(scf_error()));
122*0Sstevel@tonic-gate 		goto cleanup;
123*0Sstevel@tonic-gate 	} else if (make_handle_bound(rep_handle) == -1) {
124*0Sstevel@tonic-gate 		goto cleanup;
125*0Sstevel@tonic-gate 	} else if (((pg = scf_pg_create(rep_handle)) == NULL) ||
126*0Sstevel@tonic-gate 	    ((inst = scf_instance_create(rep_handle)) == NULL) ||
127*0Sstevel@tonic-gate 	    ((trans = scf_transaction_create(rep_handle)) == NULL) ||
128*0Sstevel@tonic-gate 	    ((entry = scf_entry_create(rep_handle)) == NULL) ||
129*0Sstevel@tonic-gate 	    ((prop = scf_property_create(rep_handle)) == NULL)) {
130*0Sstevel@tonic-gate 		error_msg("%s: %s",
131*0Sstevel@tonic-gate 		    gettext("Failed to create repository object"),
132*0Sstevel@tonic-gate 		    scf_strerror(scf_error()));
133*0Sstevel@tonic-gate 		goto cleanup;
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	return (0);
137*0Sstevel@tonic-gate cleanup:
138*0Sstevel@tonic-gate 	repval_fini();
139*0Sstevel@tonic-gate 	return (-1);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate void
143*0Sstevel@tonic-gate repval_fini(void)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	debug_msg("Entering repval_fini");
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (rep_handle != NULL) {
148*0Sstevel@tonic-gate 		/*
149*0Sstevel@tonic-gate 		 * We unbind from the repository before we free the repository
150*0Sstevel@tonic-gate 		 * objects for efficiency reasons.
151*0Sstevel@tonic-gate 		 */
152*0Sstevel@tonic-gate 		(void) scf_handle_unbind(rep_handle);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 		scf_pg_destroy(pg);
155*0Sstevel@tonic-gate 		pg = NULL;
156*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
157*0Sstevel@tonic-gate 		inst = NULL;
158*0Sstevel@tonic-gate 		scf_transaction_destroy(trans);
159*0Sstevel@tonic-gate 		trans = NULL;
160*0Sstevel@tonic-gate 		scf_entry_destroy(entry);
161*0Sstevel@tonic-gate 		entry = NULL;
162*0Sstevel@tonic-gate 		scf_property_destroy(prop);
163*0Sstevel@tonic-gate 		prop = NULL;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 		scf_handle_destroy(rep_handle);
166*0Sstevel@tonic-gate 		rep_handle = NULL;
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	if (rep_val_pool != NULL) {
170*0Sstevel@tonic-gate 		uu_list_pool_destroy(rep_val_pool);
171*0Sstevel@tonic-gate 		rep_val_pool = NULL;
172*0Sstevel@tonic-gate 	}
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate uu_list_t *
176*0Sstevel@tonic-gate create_rep_val_list(void)
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate 	uu_list_t	*ret;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	debug_msg("Entering create_rep_val_list");
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	if ((ret = uu_list_create(rep_val_pool, NULL, 0)) == NULL)
183*0Sstevel@tonic-gate 		assert(uu_error() == UU_ERROR_NO_MEMORY);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	return (ret);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate void
189*0Sstevel@tonic-gate destroy_rep_val_list(uu_list_t *list)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	debug_msg("Entering destroy_rep_val_list");
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	if (list != NULL) {
194*0Sstevel@tonic-gate 		empty_rep_val_list(list);
195*0Sstevel@tonic-gate 		uu_list_destroy(list);
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate rep_val_t *
200*0Sstevel@tonic-gate find_rep_val(uu_list_t *list, int64_t val)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	rep_val_t *rv;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	debug_msg("Entering find_rep_val: val: %lld", val);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	for (rv = uu_list_first(list); rv != NULL;
207*0Sstevel@tonic-gate 	    rv = uu_list_next(list, rv)) {
208*0Sstevel@tonic-gate 		if (rv->val == val)
209*0Sstevel@tonic-gate 			break;
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 	return (rv);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate int
215*0Sstevel@tonic-gate add_rep_val(uu_list_t *list, int64_t val)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate 	rep_val_t *rv;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	debug_msg("Entering add_rep_val: val: %lld", val);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if ((rv = malloc(sizeof (rep_val_t))) == NULL)
222*0Sstevel@tonic-gate 		return (-1);
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	uu_list_node_init(rv, &rv->link, rep_val_pool);
225*0Sstevel@tonic-gate 	rv->val = val;
226*0Sstevel@tonic-gate 	rv->scf_val = NULL;
227*0Sstevel@tonic-gate 	(void) uu_list_insert_after(list, NULL, rv);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	return (0);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate void
233*0Sstevel@tonic-gate remove_rep_val(uu_list_t *list, int64_t val)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate 	rep_val_t *rv;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	debug_msg("Entering remove_rep_val: val: %lld", val);
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	if ((rv = find_rep_val(list, val)) != NULL) {
240*0Sstevel@tonic-gate 		uu_list_remove(list, rv);
241*0Sstevel@tonic-gate 		assert(rv->scf_val == NULL);
242*0Sstevel@tonic-gate 		free(rv);
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate void
247*0Sstevel@tonic-gate empty_rep_val_list(uu_list_t *list)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate 	void		*cookie = NULL;
250*0Sstevel@tonic-gate 	rep_val_t	*rv;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	debug_msg("Entering empty_rep_val_list");
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	while ((rv = uu_list_teardown(list, &cookie)) != NULL) {
255*0Sstevel@tonic-gate 		if (rv->scf_val != NULL)
256*0Sstevel@tonic-gate 			scf_value_destroy(rv->scf_val);
257*0Sstevel@tonic-gate 		free(rv);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate int64_t
262*0Sstevel@tonic-gate get_single_rep_val(uu_list_t *list)
263*0Sstevel@tonic-gate {
264*0Sstevel@tonic-gate 	rep_val_t *rv = uu_list_first(list);
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	debug_msg("Entering get_single_rep_val");
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	assert(rv != NULL);
269*0Sstevel@tonic-gate 	return (rv->val);
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate int
273*0Sstevel@tonic-gate set_single_rep_val(uu_list_t *list, int64_t val)
274*0Sstevel@tonic-gate {
275*0Sstevel@tonic-gate 	rep_val_t *rv = uu_list_first(list);
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	debug_msg("Entering set_single_rep_val");
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if (rv == NULL) {
280*0Sstevel@tonic-gate 		if (add_rep_val(list, val) == -1)
281*0Sstevel@tonic-gate 			return (-1);
282*0Sstevel@tonic-gate 	} else {
283*0Sstevel@tonic-gate 		rv->val = val;
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	return (0);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate /*
290*0Sstevel@tonic-gate  * Partner to add_tr_entry_values. This function frees the scf_values created
291*0Sstevel@tonic-gate  * in add_tr_entry_values() in the list 'vals'.
292*0Sstevel@tonic-gate  */
293*0Sstevel@tonic-gate static void
294*0Sstevel@tonic-gate remove_tr_entry_values(uu_list_t *vals)
295*0Sstevel@tonic-gate {
296*0Sstevel@tonic-gate 	rep_val_t	*rval;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	debug_msg("Entering remove_tr_entry_values");
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	for (rval = uu_list_first(vals); rval != NULL;
301*0Sstevel@tonic-gate 	    rval = uu_list_next(vals, rval)) {
302*0Sstevel@tonic-gate 		if (rval->scf_val != NULL) {
303*0Sstevel@tonic-gate 			scf_value_destroy(rval->scf_val);
304*0Sstevel@tonic-gate 			rval->scf_val = NULL;
305*0Sstevel@tonic-gate 		}
306*0Sstevel@tonic-gate 	}
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate /*
310*0Sstevel@tonic-gate  * This function creates and associates with transaction entry 'entry' an
311*0Sstevel@tonic-gate  * scf value for each value in 'vals'. The pointers to the scf values
312*0Sstevel@tonic-gate  * are stored in the list for later cleanup by remove_tr_entry_values.
313*0Sstevel@tonic-gate  * Returns 0 on success, else -1 on error with scf_error() set to:
314*0Sstevel@tonic-gate  * SCF_ERROR_NO_MEMORY if memory allocation failed.
315*0Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
316*0Sstevel@tonic-gate  */
317*0Sstevel@tonic-gate static int
318*0Sstevel@tonic-gate add_tr_entry_values(scf_handle_t *hdl, scf_transaction_entry_t *entry,
319*0Sstevel@tonic-gate     uu_list_t *vals)
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate 	rep_val_t *rval;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	debug_msg("Entering add_tr_entry_values");
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	for (rval = uu_list_first(vals); rval != NULL;
326*0Sstevel@tonic-gate 	    rval = uu_list_next(vals, rval)) {
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		assert(rval->scf_val == NULL);
329*0Sstevel@tonic-gate 		if ((rval->scf_val = scf_value_create(hdl)) == NULL) {
330*0Sstevel@tonic-gate 			remove_tr_entry_values(vals);
331*0Sstevel@tonic-gate 			return (-1);
332*0Sstevel@tonic-gate 		}
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 		scf_value_set_integer(rval->scf_val, rval->val);
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 		if (scf_entry_add_value(entry, rval->scf_val) < 0) {
337*0Sstevel@tonic-gate 			remove_tr_entry_values(vals);
338*0Sstevel@tonic-gate 			return (-1);
339*0Sstevel@tonic-gate 		}
340*0Sstevel@tonic-gate 	}
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	return (0);
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate /*
346*0Sstevel@tonic-gate  * Stores the values contained in the list 'vals' into the property 'prop_name'
347*0Sstevel@tonic-gate  * of the instance with fmri 'inst_fmri', within the instance's instance
348*0Sstevel@tonic-gate  * state property group.
349*0Sstevel@tonic-gate  *
350*0Sstevel@tonic-gate  * Returns 0 on success, else one of the following on failure:
351*0Sstevel@tonic-gate  * SCF_ERROR_NO_MEMORY if memory allocation failed.
352*0Sstevel@tonic-gate  * SCF_ERROR_NO_RESOURCES if the server doesn't have required resources.
353*0Sstevel@tonic-gate  * SCF_ERROR_VERSION_MISMATCH if program compiled against a newer libscf
354*0Sstevel@tonic-gate  * than on system.
355*0Sstevel@tonic-gate  * SCF_ERROR_PERMISSION_DENIED if insufficient privileges to modify pg.
356*0Sstevel@tonic-gate  * SCF_ERROR_BACKEND_ACCESS if the repository back-end refused the pg modify.
357*0Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
358*0Sstevel@tonic-gate  */
359*0Sstevel@tonic-gate static scf_error_t
360*0Sstevel@tonic-gate _store_rep_vals(uu_list_t *vals, const char *inst_fmri, const char *prop_name)
361*0Sstevel@tonic-gate {
362*0Sstevel@tonic-gate 	int			cret;
363*0Sstevel@tonic-gate 	int			ret;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	debug_msg("Entering _store_rep_vals: fmri: %s, prop: %s", inst_fmri,
366*0Sstevel@tonic-gate 	    prop_name);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(rep_handle, inst_fmri, NULL, NULL, inst,
369*0Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1)
370*0Sstevel@tonic-gate 		return (scf_error());
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	/*
373*0Sstevel@tonic-gate 	 * Fetch the instance state pg, and if it doesn't exist try and
374*0Sstevel@tonic-gate 	 * create it.
375*0Sstevel@tonic-gate 	 */
376*0Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, PG_NAME_INSTANCE_STATE, pg) < 0) {
377*0Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
378*0Sstevel@tonic-gate 			return (scf_error());
379*0Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, PG_NAME_INSTANCE_STATE,
380*0Sstevel@tonic-gate 		    SCF_GROUP_FRAMEWORK, SCF_PG_FLAG_NONPERSISTENT, pg) < 0)
381*0Sstevel@tonic-gate 			return (scf_error());
382*0Sstevel@tonic-gate 	}
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	/*
385*0Sstevel@tonic-gate 	 * Perform a transaction to write the values to the requested property.
386*0Sstevel@tonic-gate 	 * If someone got there before us, loop and retry.
387*0Sstevel@tonic-gate 	 */
388*0Sstevel@tonic-gate 	do {
389*0Sstevel@tonic-gate 		if (scf_transaction_start(trans, pg) < 0)
390*0Sstevel@tonic-gate 			return (scf_error());
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		if ((scf_transaction_property_new(trans, entry,
393*0Sstevel@tonic-gate 		    prop_name, SCF_TYPE_INTEGER) < 0) &&
394*0Sstevel@tonic-gate 		    (scf_transaction_property_change_type(trans, entry,
395*0Sstevel@tonic-gate 		    prop_name, SCF_TYPE_INTEGER) < 0)) {
396*0Sstevel@tonic-gate 			ret = scf_error();
397*0Sstevel@tonic-gate 			goto cleanup;
398*0Sstevel@tonic-gate 		}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 		if (add_tr_entry_values(rep_handle, entry, vals) < 0) {
401*0Sstevel@tonic-gate 			ret = scf_error();
402*0Sstevel@tonic-gate 			goto cleanup;
403*0Sstevel@tonic-gate 		}
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 		if ((cret = scf_transaction_commit(trans)) < 0) {
406*0Sstevel@tonic-gate 			ret = scf_error();
407*0Sstevel@tonic-gate 			goto cleanup;
408*0Sstevel@tonic-gate 		} else if (cret == 0) {
409*0Sstevel@tonic-gate 			scf_transaction_reset(trans);
410*0Sstevel@tonic-gate 			scf_entry_reset(entry);
411*0Sstevel@tonic-gate 			remove_tr_entry_values(vals);
412*0Sstevel@tonic-gate 			if (scf_pg_update(pg) < 0) {
413*0Sstevel@tonic-gate 				ret = scf_error();
414*0Sstevel@tonic-gate 				goto cleanup;
415*0Sstevel@tonic-gate 			}
416*0Sstevel@tonic-gate 		}
417*0Sstevel@tonic-gate 	} while (cret == 0);
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	ret = 0;
420*0Sstevel@tonic-gate cleanup:
421*0Sstevel@tonic-gate 	scf_transaction_reset(trans);
422*0Sstevel@tonic-gate 	scf_entry_reset(entry);
423*0Sstevel@tonic-gate 	remove_tr_entry_values(vals);
424*0Sstevel@tonic-gate 	return (ret);
425*0Sstevel@tonic-gate }
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate /*
428*0Sstevel@tonic-gate  * Retrieves the repository values of property 'prop_name', of the instance
429*0Sstevel@tonic-gate  * with fmri 'fmri', from within the instance's instance state property
430*0Sstevel@tonic-gate  * group and adds them to the value list 'list'.
431*0Sstevel@tonic-gate  *
432*0Sstevel@tonic-gate  * Returns 0 on success, else one of the following values on error:
433*0Sstevel@tonic-gate  * SCF_ERROR_NOT_FOUND if the property doesn't exist.
434*0Sstevel@tonic-gate  * SCF_ERROR_NO_MEMORY if memory allocation failed.
435*0Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
436*0Sstevel@tonic-gate  * SCF_ERROR_TYPE_MISMATCH if the property was of an unexpected type.
437*0Sstevel@tonic-gate  *
438*0Sstevel@tonic-gate  */
439*0Sstevel@tonic-gate static scf_error_t
440*0Sstevel@tonic-gate _retrieve_rep_vals(uu_list_t *list, const char *fmri, const char *prop_name)
441*0Sstevel@tonic-gate {
442*0Sstevel@tonic-gate 	scf_simple_prop_t	*sp;
443*0Sstevel@tonic-gate 	int64_t			*ip;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	debug_msg("Entering _retrieve_rep_vals: fmri: %s, prop: %s", fmri,
446*0Sstevel@tonic-gate 	    prop_name);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	if ((sp = scf_simple_prop_get(rep_handle, fmri, PG_NAME_INSTANCE_STATE,
449*0Sstevel@tonic-gate 	    prop_name)) == NULL)
450*0Sstevel@tonic-gate 		return (scf_error());
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	while ((ip = scf_simple_prop_next_integer(sp)) != NULL) {
453*0Sstevel@tonic-gate 		if (add_rep_val(list, *ip) == -1) {
454*0Sstevel@tonic-gate 			empty_rep_val_list(list);
455*0Sstevel@tonic-gate 			scf_simple_prop_free(sp);
456*0Sstevel@tonic-gate 			return (SCF_ERROR_NO_MEMORY);
457*0Sstevel@tonic-gate 		}
458*0Sstevel@tonic-gate 	}
459*0Sstevel@tonic-gate 	if (scf_error() != SCF_ERROR_NONE) {
460*0Sstevel@tonic-gate 		assert(scf_error() == SCF_ERROR_TYPE_MISMATCH);
461*0Sstevel@tonic-gate 		empty_rep_val_list(list);
462*0Sstevel@tonic-gate 		scf_simple_prop_free(sp);
463*0Sstevel@tonic-gate 		return (scf_error());
464*0Sstevel@tonic-gate 	}
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	scf_simple_prop_free(sp);
467*0Sstevel@tonic-gate 	return (0);
468*0Sstevel@tonic-gate }
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate /*
471*0Sstevel@tonic-gate  * A routine that loops trying to read/write repository values until
472*0Sstevel@tonic-gate  * either success, an error other that a broken repository connection or
473*0Sstevel@tonic-gate  * the number of retries reaches REP_OP_RETRIES.
474*0Sstevel@tonic-gate  * Returns 0 on success, else the error value from either _store_rep_vals or
475*0Sstevel@tonic-gate  * retrieve_rep_vals (based on whether 'store' was set or not), or one of the
476*0Sstevel@tonic-gate  * following if a rebind failed:
477*0Sstevel@tonic-gate  * SCF_ERROR_NO_RESOURCES if the server doesn't have adequate resources.
478*0Sstevel@tonic-gate  * SCF_ERROR_NO_SERVER if the server isn't running.
479*0Sstevel@tonic-gate  */
480*0Sstevel@tonic-gate static scf_error_t
481*0Sstevel@tonic-gate store_retrieve_rep_vals(uu_list_t *vals, const char *fmri,
482*0Sstevel@tonic-gate     const char *prop, boolean_t store)
483*0Sstevel@tonic-gate {
484*0Sstevel@tonic-gate 	scf_error_t	ret;
485*0Sstevel@tonic-gate 	uint_t		retries;
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	debug_msg("Entering store_retrieve_rep_vals, store: %d", store);
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
491*0Sstevel@tonic-gate 		if (make_handle_bound(rep_handle) == -1) {
492*0Sstevel@tonic-gate 			ret = scf_error();
493*0Sstevel@tonic-gate 			break;
494*0Sstevel@tonic-gate 		}
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		if ((ret = (store ? _store_rep_vals(vals, fmri, prop) :
497*0Sstevel@tonic-gate 		    _retrieve_rep_vals(vals, fmri, prop))) !=
498*0Sstevel@tonic-gate 		    SCF_ERROR_CONNECTION_BROKEN)
499*0Sstevel@tonic-gate 			break;
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 		(void) scf_handle_unbind(rep_handle);
502*0Sstevel@tonic-gate 	}
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	return (ret);
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate scf_error_t
508*0Sstevel@tonic-gate store_rep_vals(uu_list_t *vals, const char *fmri, const char *prop)
509*0Sstevel@tonic-gate {
510*0Sstevel@tonic-gate 	return (store_retrieve_rep_vals(vals, fmri, prop, B_TRUE));
511*0Sstevel@tonic-gate }
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate scf_error_t
514*0Sstevel@tonic-gate retrieve_rep_vals(uu_list_t *vals, const char *fmri, const char *prop)
515*0Sstevel@tonic-gate {
516*0Sstevel@tonic-gate 	return (store_retrieve_rep_vals(vals, fmri, prop, B_FALSE));
517*0Sstevel@tonic-gate }
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate /*
520*0Sstevel@tonic-gate  * Fails with ECONNABORTED, ENOENT, EACCES, EROFS, ENOMEM, or EPERM.
521*0Sstevel@tonic-gate  */
522*0Sstevel@tonic-gate static int
523*0Sstevel@tonic-gate add_remove_contract_norebind(const char *fmri, boolean_t add, ctid_t ctid)
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate 	int err;
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(rep_handle, fmri, NULL, NULL, inst, NULL,
528*0Sstevel@tonic-gate 	    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
529*0Sstevel@tonic-gate 		switch (scf_error()) {
530*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
531*0Sstevel@tonic-gate 			return (ECONNABORTED);
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
534*0Sstevel@tonic-gate 			return (ENOENT);
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
537*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
538*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
539*0Sstevel@tonic-gate 		default:
540*0Sstevel@tonic-gate 			assert(0);
541*0Sstevel@tonic-gate 			abort();
542*0Sstevel@tonic-gate 		}
543*0Sstevel@tonic-gate 	}
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate redo:
546*0Sstevel@tonic-gate 	if (add)
547*0Sstevel@tonic-gate 		err = restarter_store_contract(inst, ctid,
548*0Sstevel@tonic-gate 		    RESTARTER_CONTRACT_PRIMARY);
549*0Sstevel@tonic-gate 	else
550*0Sstevel@tonic-gate 		err = restarter_remove_contract(inst, ctid,
551*0Sstevel@tonic-gate 		    RESTARTER_CONTRACT_PRIMARY);
552*0Sstevel@tonic-gate 	switch (err) {
553*0Sstevel@tonic-gate 	case 0:
554*0Sstevel@tonic-gate 	case ENOMEM:
555*0Sstevel@tonic-gate 	case ECONNABORTED:
556*0Sstevel@tonic-gate 		return (err);
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	case ECANCELED:
559*0Sstevel@tonic-gate 		return (ENOENT);
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	case EPERM:
562*0Sstevel@tonic-gate 		assert(0);
563*0Sstevel@tonic-gate 		return (err);
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	case EACCES:
566*0Sstevel@tonic-gate 		error_msg(add ? gettext("Failed to write contract id %ld for "
567*0Sstevel@tonic-gate 		    "instance %s to repository: backend access denied.") :
568*0Sstevel@tonic-gate 		    gettext("Failed to remove contract id %ld for instance %s "
569*0Sstevel@tonic-gate 		    "from repository: backend access denied."), ctid, fmri);
570*0Sstevel@tonic-gate 		return (err);
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	case EROFS:
573*0Sstevel@tonic-gate 		error_msg(add ? gettext("Failed to write contract id %ld for "
574*0Sstevel@tonic-gate 		    "instance %s to repository: backend is read-only.") :
575*0Sstevel@tonic-gate 		    gettext("Failed to remove contract id %ld for instance %s "
576*0Sstevel@tonic-gate 		    "from repository: backend is read-only."), ctid, fmri);
577*0Sstevel@tonic-gate 		return (err);
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 	case EINVAL:
580*0Sstevel@tonic-gate 	case EBADF:
581*0Sstevel@tonic-gate 	default:
582*0Sstevel@tonic-gate 		assert(0);
583*0Sstevel@tonic-gate 		abort();
584*0Sstevel@tonic-gate 		/* NOTREACHED */
585*0Sstevel@tonic-gate 	}
586*0Sstevel@tonic-gate }
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate /*
589*0Sstevel@tonic-gate  * Tries to add/remove (dependent on the value of 'add') the specified
590*0Sstevel@tonic-gate  * contract id to the specified instance until either success, an error
591*0Sstevel@tonic-gate  * other that connection broken occurs, or the number of bind retries reaches
592*0Sstevel@tonic-gate  * REP_OP_RETRIES.
593*0Sstevel@tonic-gate  * Returns 0 on success else fails with one of ENOENT, EACCES, EROFS, EPERM,
594*0Sstevel@tonic-gate  * ECONNABORTED or ENOMEM.
595*0Sstevel@tonic-gate  */
596*0Sstevel@tonic-gate int
597*0Sstevel@tonic-gate add_remove_contract(const char *fmri, boolean_t add, ctid_t ctid)
598*0Sstevel@tonic-gate {
599*0Sstevel@tonic-gate 	uint_t	retries;
600*0Sstevel@tonic-gate 	int	err;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
603*0Sstevel@tonic-gate 		if (make_handle_bound(rep_handle) == -1) {
604*0Sstevel@tonic-gate 			err = ECONNABORTED;
605*0Sstevel@tonic-gate 			break;
606*0Sstevel@tonic-gate 		}
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 		if ((err = add_remove_contract_norebind(fmri, add, ctid)) !=
609*0Sstevel@tonic-gate 		    ECONNABORTED)
610*0Sstevel@tonic-gate 			break;
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 		(void) scf_handle_unbind(rep_handle);
613*0Sstevel@tonic-gate 	}
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	return (err);
616*0Sstevel@tonic-gate }
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate /*
619*0Sstevel@tonic-gate  * Iterate over all contracts associated with the instance specified by
620*0Sstevel@tonic-gate  * fmri; if sig !=0, we send each contract the specified signal, otherwise
621*0Sstevel@tonic-gate  * we call adopt_contract() to take ownership.  This really ought to be
622*0Sstevel@tonic-gate  * reworked to use a callback mechanism if more functionality is added.
623*0Sstevel@tonic-gate  *
624*0Sstevel@tonic-gate  * Returns 0 on success or ENOENT if the instance, its restarter property
625*0Sstevel@tonic-gate  * group, or its contract property don't exist, or EINVAL if the property
626*0Sstevel@tonic-gate  * is not of the correct type, ENOMEM if there was a memory allocation
627*0Sstevel@tonic-gate  * failure, EPERM if there were permission problems accessing the repository,
628*0Sstevel@tonic-gate  * or ECONNABORTED if the connection with the repository was broken.
629*0Sstevel@tonic-gate  */
630*0Sstevel@tonic-gate int
631*0Sstevel@tonic-gate iterate_repository_contracts(const char *fmri, int sig)
632*0Sstevel@tonic-gate {
633*0Sstevel@tonic-gate 	scf_iter_t	*iter;
634*0Sstevel@tonic-gate 	scf_value_t	*val = NULL;
635*0Sstevel@tonic-gate 	uint64_t	c;
636*0Sstevel@tonic-gate 	int		err;
637*0Sstevel@tonic-gate 	int		ret = 0;
638*0Sstevel@tonic-gate 	uint_t		retries = 0;
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	debug_msg("Entering iterate_repository_contracts");
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	if (make_handle_bound(rep_handle) == -1)
643*0Sstevel@tonic-gate 		return (ECONNABORTED);
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 	if (((iter = scf_iter_create(rep_handle)) == NULL) ||
646*0Sstevel@tonic-gate 	    ((val = scf_value_create(rep_handle)) == NULL)) {
647*0Sstevel@tonic-gate 		ret = ENOMEM;
648*0Sstevel@tonic-gate 		goto out;
649*0Sstevel@tonic-gate 	}
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate rep_retry:
652*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(rep_handle, fmri, NULL, NULL, inst, NULL,
653*0Sstevel@tonic-gate 	    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
654*0Sstevel@tonic-gate 		switch (scf_error()) {
655*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
656*0Sstevel@tonic-gate rebind:
657*0Sstevel@tonic-gate 			(void) scf_handle_unbind(rep_handle);
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 			if (retries++ == REP_OP_RETRIES) {
660*0Sstevel@tonic-gate 				ret = ECONNABORTED;
661*0Sstevel@tonic-gate 				goto out;
662*0Sstevel@tonic-gate 			}
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 			if (make_handle_bound(rep_handle) == -1) {
665*0Sstevel@tonic-gate 				ret = ECONNABORTED;
666*0Sstevel@tonic-gate 				goto out;
667*0Sstevel@tonic-gate 			}
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 			goto rep_retry;
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
672*0Sstevel@tonic-gate 			ret = ENOENT;
673*0Sstevel@tonic-gate 			goto out;
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
676*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
677*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
678*0Sstevel@tonic-gate 		default:
679*0Sstevel@tonic-gate 			assert(0);
680*0Sstevel@tonic-gate 			abort();
681*0Sstevel@tonic-gate 		}
682*0Sstevel@tonic-gate 	}
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
685*0Sstevel@tonic-gate 		switch (scf_error()) {
686*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
687*0Sstevel@tonic-gate 			goto rebind;
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
690*0Sstevel@tonic-gate 			ret = 0;
691*0Sstevel@tonic-gate 			goto out;
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
694*0Sstevel@tonic-gate 			ret = ENOENT;
695*0Sstevel@tonic-gate 			goto out;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
698*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
699*0Sstevel@tonic-gate 		default:
700*0Sstevel@tonic-gate 			assert(0);
701*0Sstevel@tonic-gate 			abort();
702*0Sstevel@tonic-gate 		}
703*0Sstevel@tonic-gate 	}
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_CONTRACT, prop) != 0) {
706*0Sstevel@tonic-gate 		switch (scf_error()) {
707*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
708*0Sstevel@tonic-gate 			goto rebind;
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
711*0Sstevel@tonic-gate 			ret = 0;
712*0Sstevel@tonic-gate 			goto out;
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
715*0Sstevel@tonic-gate 			ret = ENOENT;
716*0Sstevel@tonic-gate 			goto out;
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
719*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
720*0Sstevel@tonic-gate 		default:
721*0Sstevel@tonic-gate 			assert(0);
722*0Sstevel@tonic-gate 			abort();
723*0Sstevel@tonic-gate 		}
724*0Sstevel@tonic-gate 	}
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0) {
727*0Sstevel@tonic-gate 		switch (scf_error()) {
728*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
729*0Sstevel@tonic-gate 			goto rebind;
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
732*0Sstevel@tonic-gate 			ret = ENOENT;
733*0Sstevel@tonic-gate 			goto out;
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate 		case SCF_ERROR_TYPE_MISMATCH:
736*0Sstevel@tonic-gate 			ret = EINVAL;
737*0Sstevel@tonic-gate 			goto out;
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 		default:
740*0Sstevel@tonic-gate 			assert(0);
741*0Sstevel@tonic-gate 			abort();
742*0Sstevel@tonic-gate 		}
743*0Sstevel@tonic-gate 	}
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0) {
746*0Sstevel@tonic-gate 		switch (scf_error()) {
747*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
748*0Sstevel@tonic-gate 			goto rebind;
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
751*0Sstevel@tonic-gate 			ret = ENOENT;
752*0Sstevel@tonic-gate 			goto out;
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
755*0Sstevel@tonic-gate 		default:
756*0Sstevel@tonic-gate 			assert(0);
757*0Sstevel@tonic-gate 			abort();
758*0Sstevel@tonic-gate 		}
759*0Sstevel@tonic-gate 	}
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	for (;;) {
762*0Sstevel@tonic-gate 		err = scf_iter_next_value(iter, val);
763*0Sstevel@tonic-gate 		if (err == 0) {
764*0Sstevel@tonic-gate 			break;
765*0Sstevel@tonic-gate 		} else if (err != 1) {
766*0Sstevel@tonic-gate 			assert(scf_error() == SCF_ERROR_CONNECTION_BROKEN);
767*0Sstevel@tonic-gate 			goto rebind;
768*0Sstevel@tonic-gate 		}
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 		err = scf_value_get_count(val, &c);
771*0Sstevel@tonic-gate 		assert(err == 0);
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate 		if (sig == 0) {
774*0Sstevel@tonic-gate 			/* Try to adopt the contract */
775*0Sstevel@tonic-gate 			if (adopt_contract((ctid_t)c, fmri) != 0) {
776*0Sstevel@tonic-gate 				/*
777*0Sstevel@tonic-gate 				 * Adoption failed.  No reason to think it'll
778*0Sstevel@tonic-gate 				 * work later, so remove the id from our list
779*0Sstevel@tonic-gate 				 * in the repository.
780*0Sstevel@tonic-gate 				 *
781*0Sstevel@tonic-gate 				 * Beware: add_remove_contract_norebind() uses
782*0Sstevel@tonic-gate 				 * the global scf_ handles.  Fortunately we're
783*0Sstevel@tonic-gate 				 * done with them.  We need to be cognizant of
784*0Sstevel@tonic-gate 				 * repository disconnection, though.
785*0Sstevel@tonic-gate 				 */
786*0Sstevel@tonic-gate 				switch (add_remove_contract_norebind(fmri,
787*0Sstevel@tonic-gate 				    B_FALSE, (ctid_t)c)) {
788*0Sstevel@tonic-gate 				case 0:
789*0Sstevel@tonic-gate 				case ENOENT:
790*0Sstevel@tonic-gate 				case EACCES:
791*0Sstevel@tonic-gate 				case EROFS:
792*0Sstevel@tonic-gate 					break;
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate 				case ECONNABORTED:
795*0Sstevel@tonic-gate 					goto rebind;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 				default:
798*0Sstevel@tonic-gate 					assert(0);
799*0Sstevel@tonic-gate 					abort();
800*0Sstevel@tonic-gate 				}
801*0Sstevel@tonic-gate 			}
802*0Sstevel@tonic-gate 		} else {
803*0Sstevel@tonic-gate 			/*
804*0Sstevel@tonic-gate 			 * Send a signal to all in the contract; ESRCH just
805*0Sstevel@tonic-gate 			 * means they all exited before we could kill them
806*0Sstevel@tonic-gate 			 */
807*0Sstevel@tonic-gate 			if (sigsend(P_CTID, (ctid_t)c, sig) == -1 &&
808*0Sstevel@tonic-gate 			    errno != ESRCH) {
809*0Sstevel@tonic-gate 				warn_msg(gettext("Unable to signal all contract"
810*0Sstevel@tonic-gate 				    "members of instance %s: %s"), fmri,
811*0Sstevel@tonic-gate 				    strerror(errno));
812*0Sstevel@tonic-gate 			}
813*0Sstevel@tonic-gate 		}
814*0Sstevel@tonic-gate 	}
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate out:
817*0Sstevel@tonic-gate 	scf_value_destroy(val);
818*0Sstevel@tonic-gate 	scf_iter_destroy(iter);
819*0Sstevel@tonic-gate 	return (ret);
820*0Sstevel@tonic-gate }
821