1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2013, Joyent, Inc. All rights reserved.
25  * Copyright 2016 RackTop Systems.
26  * Copyright (c) 2016 by Delphix. All rights reserved.
27  * Copyright 2017 OmniOS Community Edition (OmniOSce) Association.
28  */
29 
30 /*
31  * This is the main implementation file for the low-level repository
32  * interface.
33  */
34 
35 #include "lowlevel_impl.h"
36 
37 #include "repcache_protocol.h"
38 #include "scf_type.h"
39 
40 #include <assert.h>
41 #include <alloca.h>
42 #include <door.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <fnmatch.h>
46 #include <libuutil.h>
47 #include <poll.h>
48 #include <pthread.h>
49 #include <synch.h>
50 #include <stddef.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sys/mman.h>
55 #include <sys/sysmacros.h>
56 #include <libzonecfg.h>
57 #include <unistd.h>
58 #include <dlfcn.h>
59 
60 #define	ENV_SCF_DEBUG		"LIBSCF_DEBUG"
61 #define	ENV_SCF_DOORPATH	"LIBSCF_DOORPATH"
62 
63 static uint32_t default_debug = 0;
64 static const char *default_door_path = REPOSITORY_DOOR_NAME;
65 
66 #define	CALL_FAILED		-1
67 #define	RESULT_TOO_BIG		-2
68 #define	NOT_BOUND		-3
69 
70 static pthread_mutex_t	lowlevel_init_lock;
71 static int32_t		lowlevel_inited;
72 
73 static uu_list_pool_t	*tran_entry_pool;
74 static uu_list_pool_t	*datael_pool;
75 static uu_list_pool_t	*iter_pool;
76 
77 /*
78  * base32[] index32[] are used in base32 encoding and decoding.
79  */
80 static char base32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
81 static char index32[128] = {
82 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 0-7 */
83 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 8-15 */
84 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 16-23 */
85 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 24-31 */
86 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 32-39 */
87 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 40-47 */
88 	-1, -1, 26, 27, 28, 29, 30, 31,	/* 48-55 */
89 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 56-63 */
90 	-1, 0, 1, 2, 3, 4, 5, 6,	/* 64-71 */
91 	7, 8, 9, 10, 11, 12, 13, 14,	/* 72-79 */
92 	15, 16, 17, 18, 19, 20, 21, 22,	/* 80-87 */
93 	23, 24, 25, -1, -1, -1, -1, -1,	/* 88-95 */
94 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 96-103 */
95 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 104-111 */
96 	-1, -1, -1, -1, -1, -1, -1, -1,	/* 112-119 */
97 	-1, -1, -1, -1, -1, -1, -1, -1	/* 120-127 */
98 };
99 
100 #define	DECODE32_GS	(8)	/* scf_decode32 group size */
101 
102 #ifdef lint
103 #define	assert_nolint(x) (void)0
104 #else
105 #define	assert_nolint(x) assert(x)
106 #endif
107 
108 static void scf_iter_reset_locked(scf_iter_t *iter);
109 static void scf_value_reset_locked(scf_value_t *val, int and_destroy);
110 
111 #define	TYPE_VALUE	(-100)
112 
113 /*
114  * Hold and release subhandles.  We only allow one thread access to the
115  * subhandles at a time, and it can use any subset, grabbing and releasing
116  * them in any order.  The only restrictions are that you cannot hold an
117  * already-held subhandle, and all subhandles must be released before
118  * returning to the original caller.
119  */
120 static void
handle_hold_subhandles(scf_handle_t * h,int mask)121 handle_hold_subhandles(scf_handle_t *h, int mask)
122 {
123 	assert(mask != 0 && (mask & ~RH_HOLD_ALL) == 0);
124 
125 	(void) pthread_mutex_lock(&h->rh_lock);
126 	while (h->rh_hold_flags != 0 && h->rh_holder != pthread_self()) {
127 		int cancel_state;
128 
129 		(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
130 		    &cancel_state);
131 		(void) pthread_cond_wait(&h->rh_cv, &h->rh_lock);
132 		(void) pthread_setcancelstate(cancel_state, NULL);
133 	}
134 	if (h->rh_hold_flags == 0)
135 		h->rh_holder = pthread_self();
136 	assert(!(h->rh_hold_flags & mask));
137 	h->rh_hold_flags |= mask;
138 	(void) pthread_mutex_unlock(&h->rh_lock);
139 }
140 
141 static void
handle_rele_subhandles(scf_handle_t * h,int mask)142 handle_rele_subhandles(scf_handle_t *h, int mask)
143 {
144 	assert(mask != 0 && (mask & ~RH_HOLD_ALL) == 0);
145 
146 	(void) pthread_mutex_lock(&h->rh_lock);
147 	assert(h->rh_holder == pthread_self());
148 	assert((h->rh_hold_flags & mask));
149 
150 	h->rh_hold_flags &= ~mask;
151 	if (h->rh_hold_flags == 0)
152 		(void) pthread_cond_signal(&h->rh_cv);
153 	(void) pthread_mutex_unlock(&h->rh_lock);
154 }
155 
156 #define	HOLD_HANDLE(h, flag, field) \
157 	(handle_hold_subhandles((h), (flag)), (h)->field)
158 
159 #define	RELE_HANDLE(h, flag) \
160 	(handle_rele_subhandles((h), (flag)))
161 
162 /*
163  * convenience macros, for functions that only need a one or two handles at
164  * any given time
165  */
166 #define	HANDLE_HOLD_ITER(h)	HOLD_HANDLE((h), RH_HOLD_ITER, rh_iter)
167 #define	HANDLE_HOLD_SCOPE(h)	HOLD_HANDLE((h), RH_HOLD_SCOPE, rh_scope)
168 #define	HANDLE_HOLD_SERVICE(h)	HOLD_HANDLE((h), RH_HOLD_SERVICE, rh_service)
169 #define	HANDLE_HOLD_INSTANCE(h)	HOLD_HANDLE((h), RH_HOLD_INSTANCE, rh_instance)
170 #define	HANDLE_HOLD_SNAPSHOT(h)	HOLD_HANDLE((h), RH_HOLD_SNAPSHOT, rh_snapshot)
171 #define	HANDLE_HOLD_SNAPLVL(h)	HOLD_HANDLE((h), RH_HOLD_SNAPLVL, rh_snaplvl)
172 #define	HANDLE_HOLD_PG(h)	HOLD_HANDLE((h), RH_HOLD_PG, rh_pg)
173 #define	HANDLE_HOLD_PROPERTY(h)	HOLD_HANDLE((h), RH_HOLD_PROPERTY, rh_property)
174 #define	HANDLE_HOLD_VALUE(h)	HOLD_HANDLE((h), RH_HOLD_VALUE, rh_value)
175 
176 #define	HANDLE_RELE_ITER(h)	RELE_HANDLE((h), RH_HOLD_ITER)
177 #define	HANDLE_RELE_SCOPE(h)	RELE_HANDLE((h), RH_HOLD_SCOPE)
178 #define	HANDLE_RELE_SERVICE(h)	RELE_HANDLE((h), RH_HOLD_SERVICE)
179 #define	HANDLE_RELE_INSTANCE(h)	RELE_HANDLE((h), RH_HOLD_INSTANCE)
180 #define	HANDLE_RELE_SNAPSHOT(h)	RELE_HANDLE((h), RH_HOLD_SNAPSHOT)
181 #define	HANDLE_RELE_SNAPLVL(h)	RELE_HANDLE((h), RH_HOLD_SNAPLVL)
182 #define	HANDLE_RELE_PG(h)	RELE_HANDLE((h), RH_HOLD_PG)
183 #define	HANDLE_RELE_PROPERTY(h)	RELE_HANDLE((h), RH_HOLD_PROPERTY)
184 #define	HANDLE_RELE_VALUE(h)	RELE_HANDLE((h), RH_HOLD_VALUE)
185 
186 /*ARGSUSED*/
187 static int
transaction_entry_compare(const void * l_arg,const void * r_arg,void * private)188 transaction_entry_compare(const void *l_arg, const void *r_arg, void *private)
189 {
190 	const char *l_prop =
191 	    ((scf_transaction_entry_t *)l_arg)->entry_property;
192 	const char *r_prop =
193 	    ((scf_transaction_entry_t *)r_arg)->entry_property;
194 
195 	int ret;
196 
197 	ret = strcmp(l_prop, r_prop);
198 	if (ret > 0)
199 		return (1);
200 	if (ret < 0)
201 		return (-1);
202 	return (0);
203 }
204 
205 static int
datael_compare(const void * l_arg,const void * r_arg,void * private)206 datael_compare(const void *l_arg, const void *r_arg, void *private)
207 {
208 	uint32_t l_id = ((scf_datael_t *)l_arg)->rd_entity;
209 	uint32_t r_id = (r_arg != NULL) ? ((scf_datael_t *)r_arg)->rd_entity :
210 	    *(uint32_t *)private;
211 
212 	if (l_id > r_id)
213 		return (1);
214 	if (l_id < r_id)
215 		return (-1);
216 	return (0);
217 }
218 
219 static int
iter_compare(const void * l_arg,const void * r_arg,void * private)220 iter_compare(const void *l_arg, const void *r_arg, void *private)
221 {
222 	uint32_t l_id = ((scf_iter_t *)l_arg)->iter_id;
223 	uint32_t r_id = (r_arg != NULL) ? ((scf_iter_t *)r_arg)->iter_id :
224 	    *(uint32_t *)private;
225 
226 	if (l_id > r_id)
227 		return (1);
228 	if (l_id < r_id)
229 		return (-1);
230 	return (0);
231 }
232 
233 static int
lowlevel_init(void)234 lowlevel_init(void)
235 {
236 	const char *debug;
237 	const char *door_path;
238 
239 	(void) pthread_mutex_lock(&lowlevel_init_lock);
240 	if (lowlevel_inited == 0) {
241 		if (!issetugid() &&
242 		    (debug = getenv(ENV_SCF_DEBUG)) != NULL && debug[0] != 0 &&
243 		    uu_strtoint(debug, &default_debug, sizeof (default_debug),
244 		    0, 0, 0) == -1) {
245 			(void) fprintf(stderr, "LIBSCF: $%s (%s): %s",
246 			    ENV_SCF_DEBUG, debug,
247 			    uu_strerror(uu_error()));
248 		}
249 
250 		if (!issetugid() &&
251 		    (door_path = getenv(ENV_SCF_DOORPATH)) != NULL &&
252 		    door_path[0] != 0) {
253 			default_door_path = strdup(door_path);
254 			if (default_door_path == NULL)
255 				default_door_path = door_path;
256 		}
257 
258 		datael_pool = uu_list_pool_create("SUNW,libscf_datael",
259 		    sizeof (scf_datael_t), offsetof(scf_datael_t, rd_node),
260 		    datael_compare, UU_LIST_POOL_DEBUG);
261 
262 		iter_pool = uu_list_pool_create("SUNW,libscf_iter",
263 		    sizeof (scf_iter_t), offsetof(scf_iter_t, iter_node),
264 		    iter_compare, UU_LIST_POOL_DEBUG);
265 
266 		assert_nolint(offsetof(scf_transaction_entry_t,
267 		    entry_property) == 0);
268 		tran_entry_pool = uu_list_pool_create(
269 		    "SUNW,libscf_transaction_entity",
270 		    sizeof (scf_transaction_entry_t),
271 		    offsetof(scf_transaction_entry_t, entry_link),
272 		    transaction_entry_compare, UU_LIST_POOL_DEBUG);
273 
274 		if (datael_pool == NULL || iter_pool == NULL ||
275 		    tran_entry_pool == NULL) {
276 			lowlevel_inited = -1;
277 			goto end;
278 		}
279 
280 		if (!scf_setup_error()) {
281 			lowlevel_inited = -1;
282 			goto end;
283 		}
284 		lowlevel_inited = 1;
285 	}
286 end:
287 	(void) pthread_mutex_unlock(&lowlevel_init_lock);
288 	if (lowlevel_inited > 0)
289 		return (1);
290 	return (0);
291 }
292 
293 static const struct {
294 	scf_type_t ti_type;
295 	rep_protocol_value_type_t ti_proto_type;
296 	const char *ti_name;
297 } scf_type_info[] = {
298 	{SCF_TYPE_BOOLEAN,	REP_PROTOCOL_TYPE_BOOLEAN,
299 	    SCF_TYPE_STRING_BOOLEAN},
300 	{SCF_TYPE_COUNT,	REP_PROTOCOL_TYPE_COUNT,
301 	    SCF_TYPE_STRING_COUNT},
302 	{SCF_TYPE_INTEGER,	REP_PROTOCOL_TYPE_INTEGER,
303 	    SCF_TYPE_STRING_INTEGER},
304 	{SCF_TYPE_TIME,		REP_PROTOCOL_TYPE_TIME,
305 	    SCF_TYPE_STRING_TIME},
306 	{SCF_TYPE_ASTRING,	REP_PROTOCOL_TYPE_STRING,
307 	    SCF_TYPE_STRING_ASTRING},
308 	{SCF_TYPE_OPAQUE,	REP_PROTOCOL_TYPE_OPAQUE,
309 	    SCF_TYPE_STRING_OPAQUE},
310 	{SCF_TYPE_USTRING,	REP_PROTOCOL_SUBTYPE_USTRING,
311 	    SCF_TYPE_STRING_USTRING},
312 	{SCF_TYPE_URI,		REP_PROTOCOL_SUBTYPE_URI,
313 	    SCF_TYPE_STRING_URI},
314 	{SCF_TYPE_FMRI,		REP_PROTOCOL_SUBTYPE_FMRI,
315 	    SCF_TYPE_STRING_FMRI},
316 	{SCF_TYPE_HOST,		REP_PROTOCOL_SUBTYPE_HOST,
317 	    SCF_TYPE_STRING_HOST},
318 	{SCF_TYPE_HOSTNAME,	REP_PROTOCOL_SUBTYPE_HOSTNAME,
319 	    SCF_TYPE_STRING_HOSTNAME},
320 	{SCF_TYPE_NET_ADDR,	REP_PROTOCOL_SUBTYPE_NETADDR,
321 	    SCF_TYPE_STRING_NET_ADDR},
322 	{SCF_TYPE_NET_ADDR_V4,	REP_PROTOCOL_SUBTYPE_NETADDR_V4,
323 	    SCF_TYPE_STRING_NET_ADDR_V4},
324 	{SCF_TYPE_NET_ADDR_V6,	REP_PROTOCOL_SUBTYPE_NETADDR_V6,
325 	    SCF_TYPE_STRING_NET_ADDR_V6}
326 };
327 
328 #define	SCF_TYPE_INFO_COUNT (sizeof (scf_type_info) / sizeof (*scf_type_info))
329 static rep_protocol_value_type_t
scf_type_to_protocol_type(scf_type_t t)330 scf_type_to_protocol_type(scf_type_t t)
331 {
332 	int i;
333 
334 	for (i = 0; i < SCF_TYPE_INFO_COUNT; i++)
335 		if (scf_type_info[i].ti_type == t)
336 			return (scf_type_info[i].ti_proto_type);
337 
338 	return (REP_PROTOCOL_TYPE_INVALID);
339 }
340 
341 static scf_type_t
scf_protocol_type_to_type(rep_protocol_value_type_t t)342 scf_protocol_type_to_type(rep_protocol_value_type_t t)
343 {
344 	int i;
345 
346 	for (i = 0; i < SCF_TYPE_INFO_COUNT; i++)
347 		if (scf_type_info[i].ti_proto_type == t)
348 			return (scf_type_info[i].ti_type);
349 
350 	return (SCF_TYPE_INVALID);
351 }
352 
353 const char *
scf_type_to_string(scf_type_t ty)354 scf_type_to_string(scf_type_t ty)
355 {
356 	int i;
357 
358 	for (i = 0; i < SCF_TYPE_INFO_COUNT; i++)
359 		if (scf_type_info[i].ti_type == ty)
360 			return (scf_type_info[i].ti_name);
361 
362 	return ("unknown");
363 }
364 
365 scf_type_t
scf_string_to_type(const char * name)366 scf_string_to_type(const char *name)
367 {
368 	int i;
369 
370 	for (i = 0; i < sizeof (scf_type_info) / sizeof (*scf_type_info); i++)
371 		if (strcmp(scf_type_info[i].ti_name, name) == 0)
372 			return (scf_type_info[i].ti_type);
373 
374 	return (SCF_TYPE_INVALID);
375 }
376 
377 int
scf_type_base_type(scf_type_t type,scf_type_t * out)378 scf_type_base_type(scf_type_t type, scf_type_t *out)
379 {
380 	rep_protocol_value_type_t t = scf_type_to_protocol_type(type);
381 	if (t == REP_PROTOCOL_TYPE_INVALID)
382 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
383 
384 	*out = scf_protocol_type_to_type(scf_proto_underlying_type(t));
385 	return (SCF_SUCCESS);
386 }
387 
388 /*
389  * Convert a protocol error code into an SCF_ERROR_* code.
390  */
391 static scf_error_t
proto_error(rep_protocol_responseid_t e)392 proto_error(rep_protocol_responseid_t e)
393 {
394 	switch (e) {
395 	case REP_PROTOCOL_FAIL_MISORDERED:
396 	case REP_PROTOCOL_FAIL_UNKNOWN_ID:
397 	case REP_PROTOCOL_FAIL_INVALID_TYPE:
398 	case REP_PROTOCOL_FAIL_TRUNCATED:
399 	case REP_PROTOCOL_FAIL_TYPE_MISMATCH:
400 	case REP_PROTOCOL_FAIL_NOT_APPLICABLE:
401 	case REP_PROTOCOL_FAIL_UNKNOWN:
402 		return (SCF_ERROR_INTERNAL);
403 
404 	case REP_PROTOCOL_FAIL_BAD_TX:
405 		return (SCF_ERROR_INVALID_ARGUMENT);
406 	case REP_PROTOCOL_FAIL_BAD_REQUEST:
407 		return (SCF_ERROR_INVALID_ARGUMENT);
408 	case REP_PROTOCOL_FAIL_NO_RESOURCES:
409 		return (SCF_ERROR_NO_RESOURCES);
410 	case REP_PROTOCOL_FAIL_NOT_FOUND:
411 		return (SCF_ERROR_NOT_FOUND);
412 	case REP_PROTOCOL_FAIL_DELETED:
413 		return (SCF_ERROR_DELETED);
414 	case REP_PROTOCOL_FAIL_NOT_SET:
415 		return (SCF_ERROR_NOT_SET);
416 	case REP_PROTOCOL_FAIL_EXISTS:
417 		return (SCF_ERROR_EXISTS);
418 	case REP_PROTOCOL_FAIL_DUPLICATE_ID:
419 		return (SCF_ERROR_EXISTS);
420 	case REP_PROTOCOL_FAIL_PERMISSION_DENIED:
421 		return (SCF_ERROR_PERMISSION_DENIED);
422 	case REP_PROTOCOL_FAIL_BACKEND_ACCESS:
423 		return (SCF_ERROR_BACKEND_ACCESS);
424 	case REP_PROTOCOL_FAIL_BACKEND_READONLY:
425 		return (SCF_ERROR_BACKEND_READONLY);
426 
427 	case REP_PROTOCOL_SUCCESS:
428 	case REP_PROTOCOL_DONE:
429 	case REP_PROTOCOL_FAIL_NOT_LATEST:	/* TX code should handle this */
430 	default:
431 #ifndef NDEBUG
432 		uu_warn("%s:%d: Bad error code %d passed to proto_error().\n",
433 		    __FILE__, __LINE__, e);
434 #endif
435 		abort();
436 		/*NOTREACHED*/
437 	}
438 }
439 
440 ssize_t
scf_limit(uint32_t limit)441 scf_limit(uint32_t limit)
442 {
443 	switch (limit) {
444 	case SCF_LIMIT_MAX_NAME_LENGTH:
445 	case SCF_LIMIT_MAX_PG_TYPE_LENGTH:
446 		return (REP_PROTOCOL_NAME_LEN - 1);
447 	case SCF_LIMIT_MAX_VALUE_LENGTH:
448 		return (REP_PROTOCOL_VALUE_LEN - 1);
449 	case SCF_LIMIT_MAX_FMRI_LENGTH:
450 		return (SCF_FMRI_PREFIX_MAX_LEN +
451 		    sizeof (SCF_FMRI_SCOPE_PREFIX) - 1 +
452 		    sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1 +
453 		    sizeof (SCF_FMRI_SERVICE_PREFIX) - 1 +
454 		    sizeof (SCF_FMRI_INSTANCE_PREFIX) - 1 +
455 		    sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
456 		    sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
457 		    5 * (REP_PROTOCOL_NAME_LEN - 1));
458 	default:
459 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
460 	}
461 }
462 
463 static size_t
scf_opaque_decode(char * out_arg,const char * in,size_t max_out)464 scf_opaque_decode(char *out_arg, const char *in, size_t max_out)
465 {
466 	char a, b;
467 	char *out = out_arg;
468 
469 	while (max_out > 0 && (a = in[0]) != 0 && (b = in[1]) != 0) {
470 		in += 2;
471 
472 		if (a >= '0' && a <= '9')
473 			a -= '0';
474 		else if (a >= 'a' && a <= 'f')
475 			a = a - 'a' + 10;
476 		else if (a >= 'A' && a <= 'F')
477 			a = a - 'A' + 10;
478 		else
479 			break;
480 
481 		if (b >= '0' && b <= '9')
482 			b -= '0';
483 		else if (b >= 'a' && b <= 'f')
484 			b = b - 'a' + 10;
485 		else if (b >= 'A' && b <= 'F')
486 			b = b - 'A' + 10;
487 		else
488 			break;
489 
490 		*out++ = (a << 4) | b;
491 		max_out--;
492 	}
493 
494 	return (out - out_arg);
495 }
496 
497 static size_t
scf_opaque_encode(char * out_arg,const char * in_arg,size_t in_sz)498 scf_opaque_encode(char *out_arg, const char *in_arg, size_t in_sz)
499 {
500 	uint8_t *in = (uint8_t *)in_arg;
501 	uint8_t *end = in + in_sz;
502 	char *out = out_arg;
503 
504 	if (out == NULL)
505 		return (2 * in_sz);
506 
507 	while (in < end) {
508 		uint8_t c = *in++;
509 
510 		uint8_t a = (c & 0xf0) >> 4;
511 		uint8_t b = (c & 0x0f);
512 
513 		if (a <= 9)
514 			*out++ = a + '0';
515 		else
516 			*out++ = a + 'a' - 10;
517 
518 		if (b <= 9)
519 			*out++ = b + '0';
520 		else
521 			*out++ = b + 'a' - 10;
522 	}
523 
524 	*out = 0;
525 
526 	return (out - out_arg);
527 }
528 
529 static void
handle_do_close(scf_handle_t * h)530 handle_do_close(scf_handle_t *h)
531 {
532 	assert(MUTEX_HELD(&h->rh_lock));
533 	assert(h->rh_doorfd != -1);
534 
535 	/*
536 	 * if there are any active FD users, we just move the FD over
537 	 * to rh_doorfd_old -- they'll close it when they finish.
538 	 */
539 	if (h->rh_fd_users > 0) {
540 		h->rh_doorfd_old = h->rh_doorfd;
541 		h->rh_doorfd = -1;
542 	} else {
543 		assert(h->rh_doorfd_old == -1);
544 		(void) close(h->rh_doorfd);
545 		h->rh_doorfd = -1;
546 	}
547 }
548 
549 /*
550  * Check if a handle is currently bound.  fork()ing implicitly unbinds
551  * the handle in the child.
552  */
553 static int
handle_is_bound(scf_handle_t * h)554 handle_is_bound(scf_handle_t *h)
555 {
556 	assert(MUTEX_HELD(&h->rh_lock));
557 
558 	if (h->rh_doorfd == -1)
559 		return (0);
560 
561 	if (getpid() == h->rh_doorpid)
562 		return (1);
563 
564 	/* forked since our last bind -- initiate handle close */
565 	handle_do_close(h);
566 	return (0);
567 }
568 
569 static int
handle_has_server_locked(scf_handle_t * h)570 handle_has_server_locked(scf_handle_t *h)
571 {
572 	door_info_t i;
573 	assert(MUTEX_HELD(&h->rh_lock));
574 
575 	return (handle_is_bound(h) && door_info(h->rh_doorfd, &i) != -1 &&
576 	    i.di_target != -1);
577 }
578 
579 static int
handle_has_server(scf_handle_t * h)580 handle_has_server(scf_handle_t *h)
581 {
582 	int ret;
583 
584 	(void) pthread_mutex_lock(&h->rh_lock);
585 	ret = handle_has_server_locked(h);
586 	(void) pthread_mutex_unlock(&h->rh_lock);
587 
588 	return (ret);
589 }
590 
591 /*
592  * This makes a door request on the client door associated with handle h.
593  * It will automatically retry calls which fail on EINTR.  If h is not bound,
594  * returns NOT_BOUND.  If the door call fails or the server response is too
595  * small, returns CALL_FAILED.  If the server response is too big, truncates the
596  * response and returns RESULT_TOO_BIG.  Otherwise, the size of the result is
597  * returned.
598  */
599 static ssize_t
make_door_call(scf_handle_t * h,const void * req,size_t req_sz,void * res,size_t res_sz)600 make_door_call(scf_handle_t *h, const void *req, size_t req_sz,
601     void *res, size_t res_sz)
602 {
603 	door_arg_t arg;
604 	int r;
605 
606 	assert(MUTEX_HELD(&h->rh_lock));
607 
608 	if (!handle_is_bound(h)) {
609 		return (NOT_BOUND);
610 	}
611 
612 	arg.data_ptr = (void *)req;
613 	arg.data_size = req_sz;
614 	arg.desc_ptr = NULL;
615 	arg.desc_num = 0;
616 	arg.rbuf = res;
617 	arg.rsize = res_sz;
618 
619 	while ((r = door_call(h->rh_doorfd, &arg)) < 0) {
620 		if (errno != EINTR)
621 			break;
622 	}
623 
624 	if (r < 0) {
625 		return (CALL_FAILED);
626 	}
627 
628 	if (arg.desc_num > 0) {
629 		while (arg.desc_num > 0) {
630 			if (arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR) {
631 				int cfd = arg.desc_ptr->d_data.d_desc.d_id;
632 				(void) close(cfd);
633 			}
634 			arg.desc_ptr++;
635 			arg.desc_num--;
636 		}
637 	}
638 	if (arg.data_ptr != res && arg.data_size > 0)
639 		(void) memmove(res, arg.data_ptr, MIN(arg.data_size, res_sz));
640 
641 	if (arg.rbuf != res)
642 		(void) munmap(arg.rbuf, arg.rsize);
643 
644 	if (arg.data_size > res_sz)
645 		return (RESULT_TOO_BIG);
646 
647 	if (arg.data_size < sizeof (uint32_t))
648 		return (CALL_FAILED);
649 
650 	return (arg.data_size);
651 }
652 
653 /*
654  * Should only be used when r < 0.
655  */
656 #define	DOOR_ERRORS_BLOCK(r)	{					\
657 	switch (r) {							\
658 	case NOT_BOUND:							\
659 		return (scf_set_error(SCF_ERROR_NOT_BOUND));		\
660 									\
661 	case CALL_FAILED:						\
662 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));	\
663 									\
664 	case RESULT_TOO_BIG:						\
665 		return (scf_set_error(SCF_ERROR_INTERNAL));		\
666 									\
667 	default:							\
668 		assert(r == NOT_BOUND || r == CALL_FAILED ||		\
669 		    r == RESULT_TOO_BIG);				\
670 		abort();						\
671 	}								\
672 }
673 
674 /*
675  * Like make_door_call(), but takes an fd instead of a handle, and expects
676  * a single file descriptor, returned via res_fd.
677  *
678  * If no file descriptor is returned, *res_fd == -1.
679  */
680 static int
make_door_call_retfd(int fd,const void * req,size_t req_sz,void * res,size_t res_sz,int * res_fd)681 make_door_call_retfd(int fd, const void *req, size_t req_sz, void *res,
682     size_t res_sz, int *res_fd)
683 {
684 	door_arg_t arg;
685 	int r;
686 	char rbuf[256];
687 
688 	*res_fd = -1;
689 
690 	if (fd == -1)
691 		return (NOT_BOUND);
692 
693 	arg.data_ptr = (void *)req;
694 	arg.data_size = req_sz;
695 	arg.desc_ptr = NULL;
696 	arg.desc_num = 0;
697 	arg.rbuf = rbuf;
698 	arg.rsize = sizeof (rbuf);
699 
700 	while ((r = door_call(fd, &arg)) < 0) {
701 		if (errno != EINTR)
702 			break;
703 	}
704 
705 	if (r < 0)
706 		return (CALL_FAILED);
707 
708 	if (arg.desc_num > 1) {
709 		while (arg.desc_num > 0) {
710 			if (arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR) {
711 				int cfd =
712 				    arg.desc_ptr->d_data.d_desc.d_descriptor;
713 				(void) close(cfd);
714 			}
715 			arg.desc_ptr++;
716 			arg.desc_num--;
717 		}
718 	}
719 	if (arg.desc_num == 1 && arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR)
720 		*res_fd = arg.desc_ptr->d_data.d_desc.d_descriptor;
721 
722 	if (arg.data_size > 0)
723 		(void) memmove(res, arg.data_ptr, MIN(arg.data_size, res_sz));
724 
725 	if (arg.rbuf != rbuf)
726 		(void) munmap(arg.rbuf, arg.rsize);
727 
728 	if (arg.data_size > res_sz)
729 		return (RESULT_TOO_BIG);
730 
731 	if (arg.data_size < sizeof (uint32_t))
732 		return (CALL_FAILED);
733 
734 	return (arg.data_size);
735 }
736 
737 /*
738  * Fails with
739  *   _VERSION_MISMATCH
740  *   _NO_MEMORY
741  */
742 scf_handle_t *
scf_handle_create(scf_version_t v)743 scf_handle_create(scf_version_t v)
744 {
745 	scf_handle_t *ret;
746 	int failed;
747 
748 	/*
749 	 * This will need to be revisited when we bump SCF_VERSION
750 	 */
751 	if (v != SCF_VERSION) {
752 		(void) scf_set_error(SCF_ERROR_VERSION_MISMATCH);
753 		return (NULL);
754 	}
755 
756 	if (!lowlevel_init()) {
757 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
758 		return (NULL);
759 	}
760 
761 	ret = uu_zalloc(sizeof (*ret));
762 	if (ret == NULL) {
763 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
764 		return (NULL);
765 	}
766 
767 	ret->rh_dataels = uu_list_create(datael_pool, ret, 0);
768 	ret->rh_iters = uu_list_create(iter_pool, ret, 0);
769 	if (ret->rh_dataels == NULL || ret->rh_iters == NULL) {
770 		if (ret->rh_dataels != NULL)
771 			uu_list_destroy(ret->rh_dataels);
772 		if (ret->rh_iters != NULL)
773 			uu_list_destroy(ret->rh_iters);
774 		uu_free(ret);
775 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
776 		return (NULL);
777 	}
778 
779 	ret->rh_doorfd = -1;
780 	ret->rh_doorfd_old = -1;
781 	(void) pthread_mutex_init(&ret->rh_lock, NULL);
782 
783 	handle_hold_subhandles(ret, RH_HOLD_ALL);
784 
785 	failed = ((ret->rh_iter = scf_iter_create(ret)) == NULL ||
786 	    (ret->rh_scope = scf_scope_create(ret)) == NULL ||
787 	    (ret->rh_service = scf_service_create(ret)) == NULL ||
788 	    (ret->rh_instance = scf_instance_create(ret)) == NULL ||
789 	    (ret->rh_snapshot = scf_snapshot_create(ret)) == NULL ||
790 	    (ret->rh_snaplvl = scf_snaplevel_create(ret)) == NULL ||
791 	    (ret->rh_pg = scf_pg_create(ret)) == NULL ||
792 	    (ret->rh_property = scf_property_create(ret)) == NULL ||
793 	    (ret->rh_value = scf_value_create(ret)) == NULL);
794 
795 	/*
796 	 * these subhandles count as internal references, not external ones.
797 	 */
798 	ret->rh_intrefs = ret->rh_extrefs;
799 	ret->rh_extrefs = 0;
800 	handle_rele_subhandles(ret, RH_HOLD_ALL);
801 
802 	if (failed) {
803 		scf_handle_destroy(ret);
804 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
805 		return (NULL);
806 	}
807 
808 	scf_value_set_count(ret->rh_value, default_debug);
809 	(void) scf_handle_decorate(ret, "debug", ret->rh_value);
810 
811 	return (ret);
812 }
813 
814 /*
815  * Fails with
816  *   _NO_MEMORY
817  *   _NO_SERVER - server door could not be open()ed
818  *		  door call failed
819  *		  door_info() failed
820  *   _VERSION_MISMATCH - server returned bad file descriptor
821  *			 server claimed bad request
822  *			 server reported version mismatch
823  *			 server refused with unknown reason
824  *   _INVALID_ARGUMENT
825  *   _NO_RESOURCES - server is out of memory
826  *   _PERMISSION_DENIED
827  *   _INTERNAL - could not set up entities or iters
828  *		 server response too big
829  */
830 scf_handle_t *
_scf_handle_create_and_bind(scf_version_t ver)831 _scf_handle_create_and_bind(scf_version_t ver)
832 {
833 	scf_handle_t *h;
834 
835 	h = scf_handle_create(ver);
836 	if (h == NULL)
837 		return (NULL);
838 
839 	if (scf_handle_bind(h) == -1) {
840 		scf_handle_destroy(h);
841 		return (NULL);
842 	}
843 	return (h);
844 }
845 
846 int
scf_handle_decorate(scf_handle_t * handle,const char * name,scf_value_t * v)847 scf_handle_decorate(scf_handle_t *handle, const char *name, scf_value_t *v)
848 {
849 	if (v != SCF_DECORATE_CLEAR && handle != v->value_handle)
850 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
851 
852 	(void) pthread_mutex_lock(&handle->rh_lock);
853 	if (handle_is_bound(handle)) {
854 		(void) pthread_mutex_unlock(&handle->rh_lock);
855 		return (scf_set_error(SCF_ERROR_IN_USE));
856 	}
857 	(void) pthread_mutex_unlock(&handle->rh_lock);
858 
859 	if (strcmp(name, "debug") == 0) {
860 		if (v == SCF_DECORATE_CLEAR) {
861 			(void) pthread_mutex_lock(&handle->rh_lock);
862 			handle->rh_debug = 0;
863 			(void) pthread_mutex_unlock(&handle->rh_lock);
864 		} else {
865 			uint64_t val;
866 			if (scf_value_get_count(v, &val) < 0)
867 				return (-1);		/* error already set */
868 
869 			(void) pthread_mutex_lock(&handle->rh_lock);
870 			handle->rh_debug = (uid_t)val;
871 			(void) pthread_mutex_unlock(&handle->rh_lock);
872 		}
873 		return (0);
874 	}
875 	if (strcmp(name, "door_path") == 0) {
876 		char name[sizeof (handle->rh_doorpath)];
877 
878 		if (v == SCF_DECORATE_CLEAR) {
879 			(void) pthread_mutex_lock(&handle->rh_lock);
880 			handle->rh_doorpath[0] = 0;
881 			(void) pthread_mutex_unlock(&handle->rh_lock);
882 		} else {
883 			ssize_t len;
884 
885 			if ((len = scf_value_get_astring(v, name,
886 			    sizeof (name))) < 0) {
887 				return (-1);		/* error already set */
888 			}
889 			if (len == 0 || len >= sizeof (name)) {
890 				return (scf_set_error(
891 				    SCF_ERROR_INVALID_ARGUMENT));
892 			}
893 			(void) pthread_mutex_lock(&handle->rh_lock);
894 			(void) strlcpy(handle->rh_doorpath, name,
895 			    sizeof (handle->rh_doorpath));
896 			(void) pthread_mutex_unlock(&handle->rh_lock);
897 		}
898 		return (0);
899 	}
900 
901 	if (strcmp(name, "zone") == 0) {
902 		char zone[MAXPATHLEN], root[MAXPATHLEN], door[MAXPATHLEN];
903 		static int (*zone_get_rootpath)(char *, char *, size_t);
904 		ssize_t len;
905 
906 		/*
907 		 * In order to be able to set the zone on a handle, we want
908 		 * to determine the zone's path, which requires us to call into
909 		 * libzonecfg -- but libzonecfg.so links against libscf.so so
910 		 * we must not explicitly link to it.  To circumvent the
911 		 * circular dependency, we will pull it in here via dlopen().
912 		 */
913 		if (zone_get_rootpath == NULL) {
914 			void *dl = dlopen("libzonecfg.so.1", RTLD_LAZY), *sym;
915 
916 			if (dl == NULL)
917 				return (scf_set_error(SCF_ERROR_NOT_FOUND));
918 
919 			if ((sym = dlsym(dl, "zone_get_rootpath")) == NULL) {
920 				(void) dlclose(dl);
921 				return (scf_set_error(SCF_ERROR_INTERNAL));
922 			}
923 
924 			zone_get_rootpath = (int(*)(char *, char *, size_t))sym;
925 		}
926 
927 		if (v == SCF_DECORATE_CLEAR) {
928 			(void) pthread_mutex_lock(&handle->rh_lock);
929 			handle->rh_doorpath[0] = 0;
930 			(void) pthread_mutex_unlock(&handle->rh_lock);
931 
932 			return (0);
933 		}
934 
935 		if ((len = scf_value_get_astring(v, zone, sizeof (zone))) < 0)
936 			return (-1);
937 
938 		if (len == 0 || len >= sizeof (zone))
939 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
940 
941 		if (zone_get_rootpath(zone, root, sizeof (root)) != Z_OK) {
942 			if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
943 				root[0] = '\0';
944 			} else {
945 				return (scf_set_error(SCF_ERROR_NOT_FOUND));
946 			}
947 		}
948 
949 		if (snprintf(door, sizeof (door), "%s/%s", root,
950 		    default_door_path) >= sizeof (door))
951 			return (scf_set_error(SCF_ERROR_INTERNAL));
952 
953 		(void) pthread_mutex_lock(&handle->rh_lock);
954 		(void) strlcpy(handle->rh_doorpath, door,
955 		    sizeof (handle->rh_doorpath));
956 		(void) pthread_mutex_unlock(&handle->rh_lock);
957 
958 		return (0);
959 	}
960 
961 	return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
962 }
963 
964 /*
965  * fails with INVALID_ARGUMENT and HANDLE_MISMATCH.
966  */
967 int
_scf_handle_decorations(scf_handle_t * handle,scf_decoration_func * f,scf_value_t * v,void * data)968 _scf_handle_decorations(scf_handle_t *handle, scf_decoration_func *f,
969     scf_value_t *v, void *data)
970 {
971 	scf_decoration_info_t i;
972 	char name[sizeof (handle->rh_doorpath)];
973 	uint64_t debug;
974 
975 	if (f == NULL || v == NULL)
976 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
977 
978 	if (v->value_handle != handle)
979 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
980 
981 	i.sdi_name = (const char *)"debug";
982 	i.sdi_type = SCF_TYPE_COUNT;
983 	(void) pthread_mutex_lock(&handle->rh_lock);
984 	debug = handle->rh_debug;
985 	(void) pthread_mutex_unlock(&handle->rh_lock);
986 	if (debug != 0) {
987 		scf_value_set_count(v, debug);
988 		i.sdi_value = v;
989 	} else {
990 		i.sdi_value = SCF_DECORATE_CLEAR;
991 	}
992 
993 	if ((*f)(&i, data) == 0)
994 		return (0);
995 
996 	i.sdi_name = (const char *)"door_path";
997 	i.sdi_type = SCF_TYPE_ASTRING;
998 	(void) pthread_mutex_lock(&handle->rh_lock);
999 	(void) strlcpy(name, handle->rh_doorpath, sizeof (name));
1000 	(void) pthread_mutex_unlock(&handle->rh_lock);
1001 	if (name[0] != 0) {
1002 		(void) scf_value_set_astring(v, name);
1003 		i.sdi_value = v;
1004 	} else {
1005 		i.sdi_value = SCF_DECORATE_CLEAR;
1006 	}
1007 
1008 	if ((*f)(&i, data) == 0)
1009 		return (0);
1010 
1011 	return (1);
1012 }
1013 
1014 /*
1015  * Fails if handle is not bound.
1016  */
1017 static int
handle_unbind_unlocked(scf_handle_t * handle)1018 handle_unbind_unlocked(scf_handle_t *handle)
1019 {
1020 	rep_protocol_request_t request;
1021 	rep_protocol_response_t response;
1022 
1023 	if (!handle_is_bound(handle))
1024 		return (-1);
1025 
1026 	request.rpr_request = REP_PROTOCOL_CLOSE;
1027 
1028 	(void) make_door_call(handle, &request, sizeof (request),
1029 	    &response, sizeof (response));
1030 
1031 	handle_do_close(handle);
1032 
1033 	return (SCF_SUCCESS);
1034 }
1035 
1036 /*
1037  * Fails with
1038  *   _HANDLE_DESTROYED - dp's handle has been destroyed
1039  *   _INTERNAL - server response too big
1040  *		 entity already set up with different type
1041  *   _NO_RESOURCES - server out of memory
1042  */
1043 static int
datael_attach(scf_datael_t * dp)1044 datael_attach(scf_datael_t *dp)
1045 {
1046 	scf_handle_t *h = dp->rd_handle;
1047 
1048 	struct rep_protocol_entity_setup request;
1049 	rep_protocol_response_t response;
1050 	ssize_t r;
1051 
1052 	assert(MUTEX_HELD(&h->rh_lock));
1053 
1054 	dp->rd_reset = 0;		/* setup implicitly resets */
1055 
1056 	if (h->rh_flags & HANDLE_DEAD)
1057 		return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED));
1058 
1059 	if (!handle_is_bound(h))
1060 		return (SCF_SUCCESS);		/* nothing to do */
1061 
1062 	request.rpr_request = REP_PROTOCOL_ENTITY_SETUP;
1063 	request.rpr_entityid = dp->rd_entity;
1064 	request.rpr_entitytype = dp->rd_type;
1065 
1066 	r = make_door_call(h, &request, sizeof (request),
1067 	    &response, sizeof (response));
1068 
1069 	if (r == NOT_BOUND || r == CALL_FAILED)
1070 		return (SCF_SUCCESS);
1071 	if (r == RESULT_TOO_BIG)
1072 		return (scf_set_error(SCF_ERROR_INTERNAL));
1073 
1074 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
1075 		return (scf_set_error(proto_error(response.rpr_response)));
1076 
1077 	return (SCF_SUCCESS);
1078 }
1079 
1080 /*
1081  * Fails with
1082  *   _HANDLE_DESTROYED - iter's handle has been destroyed
1083  *   _INTERNAL - server response too big
1084  *		 iter already existed
1085  *   _NO_RESOURCES
1086  */
1087 static int
iter_attach(scf_iter_t * iter)1088 iter_attach(scf_iter_t *iter)
1089 {
1090 	scf_handle_t *h = iter->iter_handle;
1091 	struct rep_protocol_iter_request request;
1092 	struct rep_protocol_response response;
1093 	int r;
1094 
1095 	assert(MUTEX_HELD(&h->rh_lock));
1096 
1097 	if (h->rh_flags & HANDLE_DEAD)
1098 		return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED));
1099 
1100 	if (!handle_is_bound(h))
1101 		return (SCF_SUCCESS);		/* nothing to do */
1102 
1103 	request.rpr_request = REP_PROTOCOL_ITER_SETUP;
1104 	request.rpr_iterid = iter->iter_id;
1105 
1106 	r = make_door_call(h, &request, sizeof (request),
1107 	    &response, sizeof (response));
1108 
1109 	if (r == NOT_BOUND || r == CALL_FAILED)
1110 		return (SCF_SUCCESS);
1111 	if (r == RESULT_TOO_BIG)
1112 		return (scf_set_error(SCF_ERROR_INTERNAL));
1113 
1114 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
1115 		return (scf_set_error(proto_error(response.rpr_response)));
1116 
1117 	return (SCF_SUCCESS);
1118 }
1119 
1120 /*
1121  * Fails with
1122  *   _IN_USE - handle already bound
1123  *   _NO_SERVER - server door could not be open()ed
1124  *		  door call failed
1125  *		  door_info() failed
1126  *   _VERSION_MISMATCH - server returned bad file descriptor
1127  *			 server claimed bad request
1128  *			 server reported version mismatch
1129  *			 server refused with unknown reason
1130  *   _INVALID_ARGUMENT
1131  *   _NO_RESOURCES - server is out of memory
1132  *   _PERMISSION_DENIED
1133  *   _INTERNAL - could not set up entities or iters
1134  *		 server response too big
1135  *
1136  * perhaps this should try multiple times.
1137  */
1138 int
scf_handle_bind(scf_handle_t * handle)1139 scf_handle_bind(scf_handle_t *handle)
1140 {
1141 	scf_datael_t *el;
1142 	scf_iter_t *iter;
1143 
1144 	pid_t pid;
1145 	int fd;
1146 	int res;
1147 	door_info_t info;
1148 	repository_door_request_t request;
1149 	repository_door_response_t response;
1150 	const char *door_name = default_door_path;
1151 
1152 	(void) pthread_mutex_lock(&handle->rh_lock);
1153 	if (handle_is_bound(handle)) {
1154 		(void) pthread_mutex_unlock(&handle->rh_lock);
1155 		return (scf_set_error(SCF_ERROR_IN_USE));
1156 	}
1157 
1158 	/* wait until any active fd users have cleared out */
1159 	while (handle->rh_fd_users > 0) {
1160 		int cancel_state;
1161 
1162 		(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
1163 		    &cancel_state);
1164 		(void) pthread_cond_wait(&handle->rh_cv, &handle->rh_lock);
1165 		(void) pthread_setcancelstate(cancel_state, NULL);
1166 	}
1167 
1168 	/* check again, since we had to drop the lock */
1169 	if (handle_is_bound(handle)) {
1170 		(void) pthread_mutex_unlock(&handle->rh_lock);
1171 		return (scf_set_error(SCF_ERROR_IN_USE));
1172 	}
1173 
1174 	assert(handle->rh_doorfd == -1 && handle->rh_doorfd_old == -1);
1175 
1176 	if (handle->rh_doorpath[0] != 0)
1177 		door_name = handle->rh_doorpath;
1178 
1179 	fd = open(door_name, O_RDONLY, 0);
1180 	if (fd == -1) {
1181 		(void) pthread_mutex_unlock(&handle->rh_lock);
1182 		return (scf_set_error(SCF_ERROR_NO_SERVER));
1183 	}
1184 
1185 	request.rdr_version = REPOSITORY_DOOR_VERSION;
1186 	request.rdr_request = REPOSITORY_DOOR_REQUEST_CONNECT;
1187 	request.rdr_flags = handle->rh_flags;
1188 	request.rdr_debug = handle->rh_debug;
1189 
1190 	pid = getpid();
1191 
1192 	res = make_door_call_retfd(fd, &request, sizeof (request),
1193 	    &response, sizeof (response), &handle->rh_doorfd);
1194 
1195 	(void) close(fd);
1196 
1197 	if (res < 0) {
1198 		(void) pthread_mutex_unlock(&handle->rh_lock);
1199 
1200 		assert(res != NOT_BOUND);
1201 		if (res == CALL_FAILED)
1202 			return (scf_set_error(SCF_ERROR_NO_SERVER));
1203 		assert(res == RESULT_TOO_BIG);
1204 		return (scf_set_error(SCF_ERROR_INTERNAL));
1205 	}
1206 
1207 	if (handle->rh_doorfd < 0) {
1208 		(void) pthread_mutex_unlock(&handle->rh_lock);
1209 
1210 		switch (response.rdr_status) {
1211 		case REPOSITORY_DOOR_SUCCESS:
1212 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
1213 
1214 		case REPOSITORY_DOOR_FAIL_BAD_REQUEST:
1215 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
1216 
1217 		case REPOSITORY_DOOR_FAIL_VERSION_MISMATCH:
1218 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
1219 
1220 		case REPOSITORY_DOOR_FAIL_BAD_FLAG:
1221 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1222 
1223 		case REPOSITORY_DOOR_FAIL_NO_RESOURCES:
1224 			return (scf_set_error(SCF_ERROR_NO_RESOURCES));
1225 
1226 		case REPOSITORY_DOOR_FAIL_PERMISSION_DENIED:
1227 			return (scf_set_error(SCF_ERROR_PERMISSION_DENIED));
1228 
1229 		default:
1230 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
1231 		}
1232 	}
1233 
1234 	(void) fcntl(handle->rh_doorfd, F_SETFD, FD_CLOEXEC);
1235 
1236 	if (door_info(handle->rh_doorfd, &info) < 0) {
1237 		(void) close(handle->rh_doorfd);
1238 		handle->rh_doorfd = -1;
1239 
1240 		(void) pthread_mutex_unlock(&handle->rh_lock);
1241 		return (scf_set_error(SCF_ERROR_NO_SERVER));
1242 	}
1243 
1244 	handle->rh_doorpid = pid;
1245 	handle->rh_doorid = info.di_uniquifier;
1246 
1247 	/*
1248 	 * Now, re-attach everything
1249 	 */
1250 	for (el = uu_list_first(handle->rh_dataels); el != NULL;
1251 	    el = uu_list_next(handle->rh_dataels, el)) {
1252 		if (datael_attach(el) == -1) {
1253 			assert(scf_error() != SCF_ERROR_HANDLE_DESTROYED);
1254 			(void) handle_unbind_unlocked(handle);
1255 			(void) pthread_mutex_unlock(&handle->rh_lock);
1256 			return (-1);
1257 		}
1258 	}
1259 
1260 	for (iter = uu_list_first(handle->rh_iters); iter != NULL;
1261 	    iter = uu_list_next(handle->rh_iters, iter)) {
1262 		if (iter_attach(iter) == -1) {
1263 			assert(scf_error() != SCF_ERROR_HANDLE_DESTROYED);
1264 			(void) handle_unbind_unlocked(handle);
1265 			(void) pthread_mutex_unlock(&handle->rh_lock);
1266 			return (-1);
1267 		}
1268 	}
1269 	(void) pthread_mutex_unlock(&handle->rh_lock);
1270 	return (SCF_SUCCESS);
1271 }
1272 
1273 int
scf_handle_unbind(scf_handle_t * handle)1274 scf_handle_unbind(scf_handle_t *handle)
1275 {
1276 	int ret;
1277 	(void) pthread_mutex_lock(&handle->rh_lock);
1278 	ret = handle_unbind_unlocked(handle);
1279 	(void) pthread_mutex_unlock(&handle->rh_lock);
1280 	return (ret == SCF_SUCCESS ? ret : scf_set_error(SCF_ERROR_NOT_BOUND));
1281 }
1282 
1283 static scf_handle_t *
handle_get(scf_handle_t * h)1284 handle_get(scf_handle_t *h)
1285 {
1286 	(void) pthread_mutex_lock(&h->rh_lock);
1287 	if (h->rh_flags & HANDLE_DEAD) {
1288 		(void) pthread_mutex_unlock(&h->rh_lock);
1289 		(void) scf_set_error(SCF_ERROR_HANDLE_DESTROYED);
1290 		return (NULL);
1291 	}
1292 	(void) pthread_mutex_unlock(&h->rh_lock);
1293 	return (h);
1294 }
1295 
1296 /*
1297  * Called when an object is removed from the handle.  On the last remove,
1298  * cleans up and frees the handle.
1299  */
1300 static void
handle_unrefed(scf_handle_t * handle)1301 handle_unrefed(scf_handle_t *handle)
1302 {
1303 	scf_iter_t *iter;
1304 	scf_value_t *v;
1305 	scf_scope_t *sc;
1306 	scf_service_t *svc;
1307 	scf_instance_t *inst;
1308 	scf_snapshot_t *snap;
1309 	scf_snaplevel_t *snaplvl;
1310 	scf_propertygroup_t *pg;
1311 	scf_property_t *prop;
1312 
1313 	assert(MUTEX_HELD(&handle->rh_lock));
1314 
1315 	/*
1316 	 * Don't do anything if the handle has not yet been destroyed, there
1317 	 * are still external references, or we're already doing unrefed
1318 	 * handling.
1319 	 */
1320 	if (!(handle->rh_flags & HANDLE_DEAD) ||
1321 	    handle->rh_extrefs > 0 ||
1322 	    handle->rh_fd_users > 0 ||
1323 	    (handle->rh_flags & HANDLE_UNREFED)) {
1324 		(void) pthread_mutex_unlock(&handle->rh_lock);
1325 		return;
1326 	}
1327 
1328 	handle->rh_flags |= HANDLE_UNREFED;
1329 
1330 	/*
1331 	 * Now that we know that there are no external references, and the
1332 	 * HANDLE_DEAD flag keeps new ones from appearing, we can clean up
1333 	 * our subhandles and destroy the handle completely.
1334 	 */
1335 	assert(handle->rh_intrefs >= 0);
1336 	handle->rh_extrefs = handle->rh_intrefs;
1337 	handle->rh_intrefs = 0;
1338 	(void) pthread_mutex_unlock(&handle->rh_lock);
1339 
1340 	handle_hold_subhandles(handle, RH_HOLD_ALL);
1341 
1342 	iter = handle->rh_iter;
1343 	sc = handle->rh_scope;
1344 	svc = handle->rh_service;
1345 	inst = handle->rh_instance;
1346 	snap = handle->rh_snapshot;
1347 	snaplvl = handle->rh_snaplvl;
1348 	pg = handle->rh_pg;
1349 	prop = handle->rh_property;
1350 	v = handle->rh_value;
1351 
1352 	handle->rh_iter = NULL;
1353 	handle->rh_scope = NULL;
1354 	handle->rh_service = NULL;
1355 	handle->rh_instance = NULL;
1356 	handle->rh_snapshot = NULL;
1357 	handle->rh_snaplvl = NULL;
1358 	handle->rh_pg = NULL;
1359 	handle->rh_property = NULL;
1360 	handle->rh_value = NULL;
1361 
1362 	if (iter != NULL)
1363 		scf_iter_destroy(iter);
1364 	if (sc != NULL)
1365 		scf_scope_destroy(sc);
1366 	if (svc != NULL)
1367 		scf_service_destroy(svc);
1368 	if (inst != NULL)
1369 		scf_instance_destroy(inst);
1370 	if (snap != NULL)
1371 		scf_snapshot_destroy(snap);
1372 	if (snaplvl != NULL)
1373 		scf_snaplevel_destroy(snaplvl);
1374 	if (pg != NULL)
1375 		scf_pg_destroy(pg);
1376 	if (prop != NULL)
1377 		scf_property_destroy(prop);
1378 	if (v != NULL)
1379 		scf_value_destroy(v);
1380 
1381 	(void) pthread_mutex_lock(&handle->rh_lock);
1382 
1383 	/* there should be no outstanding children at this point */
1384 	assert(handle->rh_extrefs == 0);
1385 	assert(handle->rh_intrefs == 0);
1386 	assert(handle->rh_values == 0);
1387 	assert(handle->rh_entries == 0);
1388 	assert(uu_list_numnodes(handle->rh_dataels) == 0);
1389 	assert(uu_list_numnodes(handle->rh_iters) == 0);
1390 
1391 	uu_list_destroy(handle->rh_dataels);
1392 	uu_list_destroy(handle->rh_iters);
1393 	handle->rh_dataels = NULL;
1394 	handle->rh_iters = NULL;
1395 	(void) pthread_mutex_unlock(&handle->rh_lock);
1396 
1397 	(void) pthread_mutex_destroy(&handle->rh_lock);
1398 
1399 	uu_free(handle);
1400 }
1401 
1402 void
scf_handle_destroy(scf_handle_t * handle)1403 scf_handle_destroy(scf_handle_t *handle)
1404 {
1405 	if (handle == NULL)
1406 		return;
1407 
1408 	(void) pthread_mutex_lock(&handle->rh_lock);
1409 	if (handle->rh_flags & HANDLE_DEAD) {
1410 		/*
1411 		 * This is an error (you are not allowed to reference the
1412 		 * handle after it is destroyed), but we can't report it.
1413 		 */
1414 		(void) pthread_mutex_unlock(&handle->rh_lock);
1415 		return;
1416 	}
1417 	handle->rh_flags |= HANDLE_DEAD;
1418 	(void) handle_unbind_unlocked(handle);
1419 	handle_unrefed(handle);
1420 }
1421 
1422 ssize_t
scf_myname(scf_handle_t * h,char * out,size_t len)1423 scf_myname(scf_handle_t *h, char *out, size_t len)
1424 {
1425 	char *cp;
1426 
1427 	if (!handle_has_server(h))
1428 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));
1429 
1430 	cp = getenv("SMF_FMRI");
1431 	if (cp == NULL)
1432 		return (scf_set_error(SCF_ERROR_NOT_SET));
1433 
1434 	return (strlcpy(out, cp, len));
1435 }
1436 
1437 static uint32_t
handle_alloc_entityid(scf_handle_t * h)1438 handle_alloc_entityid(scf_handle_t *h)
1439 {
1440 	uint32_t nextid;
1441 
1442 	assert(MUTEX_HELD(&h->rh_lock));
1443 
1444 	if (uu_list_numnodes(h->rh_dataels) == UINT32_MAX)
1445 		return (0);		/* no ids available */
1446 
1447 	/*
1448 	 * The following loop assumes that there are not a huge number of
1449 	 * outstanding entities when we've wrapped.  If that ends up not
1450 	 * being the case, the O(N^2) nature of this search will hurt a lot,
1451 	 * and the data structure should be switched to an AVL tree.
1452 	 */
1453 	nextid = h->rh_nextentity + 1;
1454 	for (;;) {
1455 		scf_datael_t *cur;
1456 
1457 		if (nextid == 0) {
1458 			nextid++;
1459 			h->rh_flags |= HANDLE_WRAPPED_ENTITY;
1460 		}
1461 		if (!(h->rh_flags & HANDLE_WRAPPED_ENTITY))
1462 			break;
1463 
1464 		cur = uu_list_find(h->rh_dataels, NULL, &nextid, NULL);
1465 		if (cur == NULL)
1466 			break;		/* not in use */
1467 
1468 		if (nextid == h->rh_nextentity)
1469 			return (0);	/* wrapped around; no ids available */
1470 		nextid++;
1471 	}
1472 
1473 	h->rh_nextentity = nextid;
1474 	return (nextid);
1475 }
1476 
1477 static uint32_t
handle_alloc_iterid(scf_handle_t * h)1478 handle_alloc_iterid(scf_handle_t *h)
1479 {
1480 	uint32_t nextid;
1481 
1482 	assert(MUTEX_HELD(&h->rh_lock));
1483 
1484 	if (uu_list_numnodes(h->rh_iters) == UINT32_MAX)
1485 		return (0);		/* no ids available */
1486 
1487 	/* see the comment in handle_alloc_entityid */
1488 	nextid = h->rh_nextiter + 1;
1489 	for (;;) {
1490 		scf_iter_t *cur;
1491 
1492 		if (nextid == 0) {
1493 			nextid++;
1494 			h->rh_flags |= HANDLE_WRAPPED_ITER;
1495 		}
1496 		if (!(h->rh_flags & HANDLE_WRAPPED_ITER))
1497 			break;			/* not yet wrapped */
1498 
1499 		cur = uu_list_find(h->rh_iters, NULL, &nextid, NULL);
1500 		if (cur == NULL)
1501 			break;		/* not in use */
1502 
1503 		if (nextid == h->rh_nextiter)
1504 			return (0);	/* wrapped around; no ids available */
1505 		nextid++;
1506 	}
1507 
1508 	h->rh_nextiter = nextid;
1509 	return (nextid);
1510 }
1511 
1512 static uint32_t
handle_next_changeid(scf_handle_t * handle)1513 handle_next_changeid(scf_handle_t *handle)
1514 {
1515 	uint32_t nextid;
1516 
1517 	assert(MUTEX_HELD(&handle->rh_lock));
1518 
1519 	nextid = ++handle->rh_nextchangeid;
1520 	if (nextid == 0)
1521 		nextid = ++handle->rh_nextchangeid;
1522 	return (nextid);
1523 }
1524 
1525 /*
1526  * Fails with
1527  *   _INVALID_ARGUMENT - h is NULL
1528  *   _HANDLE_DESTROYED
1529  *   _INTERNAL - server response too big
1530  *		 entity already set up with different type
1531  *   _NO_RESOURCES
1532  */
1533 static int
datael_init(scf_datael_t * dp,scf_handle_t * h,uint32_t type)1534 datael_init(scf_datael_t *dp, scf_handle_t *h, uint32_t type)
1535 {
1536 	int ret;
1537 
1538 	if (h == NULL)
1539 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1540 
1541 	uu_list_node_init(dp, &dp->rd_node, datael_pool);
1542 
1543 	dp->rd_handle = h;
1544 	dp->rd_type = type;
1545 	dp->rd_reset = 0;
1546 
1547 	(void) pthread_mutex_lock(&h->rh_lock);
1548 	if (h->rh_flags & HANDLE_DEAD) {
1549 		/*
1550 		 * we're in undefined territory (the user cannot use a handle
1551 		 * directly after it has been destroyed), but we don't want
1552 		 * to allow any new references to happen, so we fail here.
1553 		 */
1554 		(void) pthread_mutex_unlock(&h->rh_lock);
1555 		return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED));
1556 	}
1557 	dp->rd_entity = handle_alloc_entityid(h);
1558 	if (dp->rd_entity == 0) {
1559 		(void) pthread_mutex_unlock(&h->rh_lock);
1560 		uu_list_node_fini(dp, &dp->rd_node, datael_pool);
1561 		return (scf_set_error(SCF_ERROR_NO_MEMORY));
1562 	}
1563 
1564 	ret = datael_attach(dp);
1565 	if (ret == 0) {
1566 		(void) uu_list_insert_before(h->rh_dataels, NULL, dp);
1567 		h->rh_extrefs++;
1568 	} else {
1569 		uu_list_node_fini(dp, &dp->rd_node, datael_pool);
1570 	}
1571 	(void) pthread_mutex_unlock(&h->rh_lock);
1572 
1573 	return (ret);
1574 }
1575 
1576 static void
datael_destroy(scf_datael_t * dp)1577 datael_destroy(scf_datael_t *dp)
1578 {
1579 	scf_handle_t *h = dp->rd_handle;
1580 
1581 	struct rep_protocol_entity_teardown request;
1582 	rep_protocol_response_t response;
1583 
1584 	(void) pthread_mutex_lock(&h->rh_lock);
1585 	uu_list_remove(h->rh_dataels, dp);
1586 	--h->rh_extrefs;
1587 
1588 	if (handle_is_bound(h)) {
1589 		request.rpr_request = REP_PROTOCOL_ENTITY_TEARDOWN;
1590 		request.rpr_entityid = dp->rd_entity;
1591 
1592 		(void) make_door_call(h, &request, sizeof (request),
1593 		    &response, sizeof (response));
1594 	}
1595 	handle_unrefed(h);			/* drops h->rh_lock */
1596 
1597 	dp->rd_handle = NULL;
1598 }
1599 
1600 static scf_handle_t *
datael_handle(const scf_datael_t * dp)1601 datael_handle(const scf_datael_t *dp)
1602 {
1603 	return (handle_get(dp->rd_handle));
1604 }
1605 
1606 /*
1607  * We delay ENTITY_RESETs until right before the entity is used.  By doing
1608  * them lazily, we remove quite a few unnecessary calls.
1609  */
1610 static void
datael_do_reset_locked(scf_datael_t * dp)1611 datael_do_reset_locked(scf_datael_t *dp)
1612 {
1613 	scf_handle_t *h = dp->rd_handle;
1614 
1615 	struct rep_protocol_entity_reset request;
1616 	rep_protocol_response_t response;
1617 
1618 	assert(MUTEX_HELD(&h->rh_lock));
1619 
1620 	request.rpr_request = REP_PROTOCOL_ENTITY_RESET;
1621 	request.rpr_entityid = dp->rd_entity;
1622 
1623 	(void) make_door_call(h, &request, sizeof (request),
1624 	    &response, sizeof (response));
1625 
1626 	dp->rd_reset = 0;
1627 }
1628 
1629 static void
datael_reset_locked(scf_datael_t * dp)1630 datael_reset_locked(scf_datael_t *dp)
1631 {
1632 	assert(MUTEX_HELD(&dp->rd_handle->rh_lock));
1633 	dp->rd_reset = 1;
1634 }
1635 
1636 static void
datael_reset(scf_datael_t * dp)1637 datael_reset(scf_datael_t *dp)
1638 {
1639 	scf_handle_t *h = dp->rd_handle;
1640 
1641 	(void) pthread_mutex_lock(&h->rh_lock);
1642 	dp->rd_reset = 1;
1643 	(void) pthread_mutex_unlock(&h->rh_lock);
1644 }
1645 
1646 static void
datael_finish_reset(const scf_datael_t * dp_arg)1647 datael_finish_reset(const scf_datael_t *dp_arg)
1648 {
1649 	scf_datael_t *dp = (scf_datael_t *)dp_arg;
1650 
1651 	if (dp->rd_reset)
1652 		datael_do_reset_locked(dp);
1653 }
1654 
1655 /*
1656  * Fails with _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response too
1657  * big, bad entity id, request not applicable to entity, name too long for
1658  * buffer), _NOT_SET, _DELETED, or _CONSTRAINT_VIOLATED (snaplevel is not of an
1659  * instance).
1660  */
1661 static ssize_t
datael_get_name(const scf_datael_t * dp,char * buf,size_t size,uint32_t type)1662 datael_get_name(const scf_datael_t *dp, char *buf, size_t size, uint32_t type)
1663 {
1664 	scf_handle_t *h = dp->rd_handle;
1665 
1666 	struct rep_protocol_entity_name request;
1667 	struct rep_protocol_name_response response;
1668 	ssize_t r;
1669 
1670 	(void) pthread_mutex_lock(&h->rh_lock);
1671 	request.rpr_request = REP_PROTOCOL_ENTITY_NAME;
1672 	request.rpr_entityid = dp->rd_entity;
1673 	request.rpr_answertype = type;
1674 
1675 	datael_finish_reset(dp);
1676 	r = make_door_call(h, &request, sizeof (request),
1677 	    &response, sizeof (response));
1678 	(void) pthread_mutex_unlock(&h->rh_lock);
1679 
1680 	if (r < 0)
1681 		DOOR_ERRORS_BLOCK(r);
1682 
1683 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
1684 		assert(response.rpr_response != REP_PROTOCOL_FAIL_BAD_REQUEST);
1685 		if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_FOUND)
1686 			return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
1687 		return (scf_set_error(proto_error(response.rpr_response)));
1688 	}
1689 	return (strlcpy(buf, response.rpr_name, size));
1690 }
1691 
1692 /*
1693  * Fails with _HANDLE_MISMATCH, _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL
1694  * (server response too big, bad element id), _EXISTS (elements have same id),
1695  * _NOT_SET, _DELETED, _CONSTRAINT_VIOLATED, _NOT_FOUND (scope has no parent),
1696  * or _SUCCESS.
1697  */
1698 static int
datael_get_parent(const scf_datael_t * dp,scf_datael_t * pp)1699 datael_get_parent(const scf_datael_t *dp, scf_datael_t *pp)
1700 {
1701 	scf_handle_t *h = dp->rd_handle;
1702 
1703 	struct rep_protocol_entity_parent request;
1704 	struct rep_protocol_response response;
1705 
1706 	ssize_t r;
1707 
1708 	if (h != pp->rd_handle)
1709 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
1710 
1711 	(void) pthread_mutex_lock(&h->rh_lock);
1712 	request.rpr_request = REP_PROTOCOL_ENTITY_GET_PARENT;
1713 	request.rpr_entityid = dp->rd_entity;
1714 	request.rpr_outid = pp->rd_entity;
1715 
1716 	datael_finish_reset(dp);
1717 	datael_finish_reset(pp);
1718 	r = make_door_call(h, &request, sizeof (request),
1719 	    &response, sizeof (response));
1720 	(void) pthread_mutex_unlock(&h->rh_lock);
1721 
1722 	if (r < 0)
1723 		DOOR_ERRORS_BLOCK(r);
1724 
1725 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
1726 		if (response.rpr_response == REP_PROTOCOL_FAIL_TYPE_MISMATCH)
1727 			return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
1728 		return (scf_set_error(proto_error(response.rpr_response)));
1729 	}
1730 
1731 	return (SCF_SUCCESS);
1732 }
1733 
1734 /*
1735  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type,
1736  * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response
1737  * too big, bad id, iter already exists, element cannot have children of type,
1738  * type is invalid, iter was reset, sequence was bad, iter walks values, iter
1739  * does not walk type entities), _NOT_SET, _DELETED, _NO_RESOURCES,
1740  * _BACKEND_ACCESS, _NOT_FOUND.
1741  */
1742 static int
datael_get_child_composed_locked(const scf_datael_t * dp,const char * name,uint32_t type,scf_datael_t * out,scf_iter_t * iter)1743 datael_get_child_composed_locked(const scf_datael_t *dp, const char *name,
1744     uint32_t type, scf_datael_t *out, scf_iter_t *iter)
1745 {
1746 	struct rep_protocol_iter_start request;
1747 	struct rep_protocol_iter_read read_request;
1748 	struct rep_protocol_response response;
1749 
1750 	scf_handle_t *h = dp->rd_handle;
1751 	ssize_t r;
1752 
1753 	if (h != out->rd_handle)
1754 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
1755 
1756 	if (out->rd_type != type)
1757 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1758 
1759 	assert(MUTEX_HELD(&h->rh_lock));
1760 	assert(iter != NULL);
1761 
1762 	scf_iter_reset_locked(iter);
1763 	iter->iter_type = type;
1764 
1765 	request.rpr_request = REP_PROTOCOL_ITER_START;
1766 	request.rpr_iterid = iter->iter_id;
1767 	request.rpr_entity = dp->rd_entity;
1768 	request.rpr_itertype = type;
1769 	request.rpr_flags = RP_ITER_START_EXACT | RP_ITER_START_COMPOSED;
1770 
1771 	if (name == NULL || strlcpy(request.rpr_pattern, name,
1772 	    sizeof (request.rpr_pattern)) >= sizeof (request.rpr_pattern)) {
1773 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1774 	}
1775 
1776 	datael_finish_reset(dp);
1777 	datael_finish_reset(out);
1778 
1779 	/*
1780 	 * We hold the handle lock across both door calls, so that they
1781 	 * appear atomic.
1782 	 */
1783 	r = make_door_call(h, &request, sizeof (request),
1784 	    &response, sizeof (response));
1785 
1786 	if (r < 0)
1787 		DOOR_ERRORS_BLOCK(r);
1788 
1789 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
1790 		return (scf_set_error(proto_error(response.rpr_response)));
1791 
1792 	iter->iter_sequence++;
1793 
1794 	read_request.rpr_request = REP_PROTOCOL_ITER_READ;
1795 	read_request.rpr_iterid = iter->iter_id;
1796 	read_request.rpr_sequence = iter->iter_sequence;
1797 	read_request.rpr_entityid = out->rd_entity;
1798 
1799 	r = make_door_call(h, &read_request, sizeof (read_request),
1800 	    &response, sizeof (response));
1801 
1802 	scf_iter_reset_locked(iter);
1803 
1804 	if (r < 0)
1805 		DOOR_ERRORS_BLOCK(r);
1806 
1807 	if (response.rpr_response == REP_PROTOCOL_DONE) {
1808 		return (scf_set_error(SCF_ERROR_NOT_FOUND));
1809 	}
1810 
1811 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
1812 		if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_SET ||
1813 		    response.rpr_response == REP_PROTOCOL_FAIL_BAD_REQUEST)
1814 			return (scf_set_error(SCF_ERROR_INTERNAL));
1815 		return (scf_set_error(proto_error(response.rpr_response)));
1816 	}
1817 
1818 	return (0);
1819 }
1820 
1821 /*
1822  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type,
1823  * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response
1824  * too big, bad id, element cannot have children of type, type is invalid),
1825  * _NOT_SET, _DELETED, _NO_RESOURCES, _BACKEND_ACCESS.
1826  */
1827 static int
datael_get_child_locked(const scf_datael_t * dp,const char * name,uint32_t type,scf_datael_t * out)1828 datael_get_child_locked(const scf_datael_t *dp, const char *name,
1829     uint32_t type, scf_datael_t *out)
1830 {
1831 	struct rep_protocol_entity_get_child request;
1832 	struct rep_protocol_response response;
1833 
1834 	scf_handle_t *h = dp->rd_handle;
1835 	ssize_t r;
1836 
1837 	if (h != out->rd_handle)
1838 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
1839 
1840 	if (out->rd_type != type)
1841 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1842 
1843 	assert(MUTEX_HELD(&h->rh_lock));
1844 
1845 	request.rpr_request = REP_PROTOCOL_ENTITY_GET_CHILD;
1846 	request.rpr_entityid = dp->rd_entity;
1847 	request.rpr_childid = out->rd_entity;
1848 
1849 	if (name == NULL || strlcpy(request.rpr_name, name,
1850 	    sizeof (request.rpr_name)) >= sizeof (request.rpr_name)) {
1851 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1852 	}
1853 
1854 	datael_finish_reset(dp);
1855 	datael_finish_reset(out);
1856 
1857 	r = make_door_call(h, &request, sizeof (request),
1858 	    &response, sizeof (response));
1859 
1860 	if (r < 0)
1861 		DOOR_ERRORS_BLOCK(r);
1862 
1863 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
1864 		return (scf_set_error(proto_error(response.rpr_response)));
1865 	return (0);
1866 }
1867 
1868 /*
1869  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type,
1870  * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response
1871  * too big, bad id, iter already exists, element cannot have children of type,
1872  * type is invalid, iter was reset, sequence was bad, iter walks values, iter
1873  * does not walk type entities), _NOT_SET, _DELETED, _NO_RESOURCES,
1874  * _BACKEND_ACCESS, _NOT_FOUND.
1875  */
1876 static int
datael_get_child(const scf_datael_t * dp,const char * name,uint32_t type,scf_datael_t * out,boolean_t composed)1877 datael_get_child(const scf_datael_t *dp, const char *name, uint32_t type,
1878     scf_datael_t *out, boolean_t composed)
1879 {
1880 	scf_handle_t *h = dp->rd_handle;
1881 	uint32_t held = 0;
1882 	int ret;
1883 
1884 	scf_iter_t *iter = NULL;
1885 
1886 	if (composed)
1887 		iter = HANDLE_HOLD_ITER(h);
1888 
1889 	if (out == NULL) {
1890 		switch (type) {
1891 		case REP_PROTOCOL_ENTITY_SERVICE:
1892 			out = &HANDLE_HOLD_SERVICE(h)->rd_d;
1893 			held = RH_HOLD_SERVICE;
1894 			break;
1895 
1896 		case REP_PROTOCOL_ENTITY_INSTANCE:
1897 			out = &HANDLE_HOLD_INSTANCE(h)->rd_d;
1898 			held = RH_HOLD_INSTANCE;
1899 			break;
1900 
1901 		case REP_PROTOCOL_ENTITY_SNAPSHOT:
1902 			out = &HANDLE_HOLD_SNAPSHOT(h)->rd_d;
1903 			held = RH_HOLD_SNAPSHOT;
1904 			break;
1905 
1906 		case REP_PROTOCOL_ENTITY_SNAPLEVEL:
1907 			out = &HANDLE_HOLD_SNAPLVL(h)->rd_d;
1908 			held = RH_HOLD_SNAPLVL;
1909 			break;
1910 
1911 		case REP_PROTOCOL_ENTITY_PROPERTYGRP:
1912 			out = &HANDLE_HOLD_PG(h)->rd_d;
1913 			held = RH_HOLD_PG;
1914 			break;
1915 
1916 		case REP_PROTOCOL_ENTITY_PROPERTY:
1917 			out = &HANDLE_HOLD_PROPERTY(h)->rd_d;
1918 			held = RH_HOLD_PROPERTY;
1919 			break;
1920 
1921 		default:
1922 			assert(0);
1923 			abort();
1924 		}
1925 	}
1926 
1927 	(void) pthread_mutex_lock(&h->rh_lock);
1928 	if (composed)
1929 		ret = datael_get_child_composed_locked(dp, name, type, out,
1930 		    iter);
1931 	else
1932 		ret = datael_get_child_locked(dp, name, type, out);
1933 	(void) pthread_mutex_unlock(&h->rh_lock);
1934 
1935 	if (composed)
1936 		HANDLE_RELE_ITER(h);
1937 
1938 	if (held)
1939 		handle_rele_subhandles(h, held);
1940 
1941 	return (ret);
1942 }
1943 
1944 /*
1945  * Fails with
1946  *   _HANDLE_MISMATCH
1947  *   _INVALID_ARGUMENT - name is too long
1948  *			 invalid changeid
1949  *			 name is invalid
1950  *			 cannot create children for dp's type of node
1951  *   _NOT_BOUND - handle is not bound
1952  *   _CONNECTION_BROKEN - server is not reachable
1953  *   _INTERNAL - server response too big
1954  *		 dp or cp has unknown id
1955  *		 type is _PROPERTYGRP
1956  *		 type is invalid
1957  *		 dp cannot have children of type type
1958  *		 database is corrupt
1959  *   _EXISTS - dp & cp have the same id
1960  *   _EXISTS - child already exists
1961  *   _DELETED - dp has been deleted
1962  *   _NOT_SET - dp is reset
1963  *   _NO_RESOURCES
1964  *   _PERMISSION_DENIED
1965  *   _BACKEND_ACCESS
1966  *   _BACKEND_READONLY
1967  */
1968 static int
datael_add_child(const scf_datael_t * dp,const char * name,uint32_t type,scf_datael_t * cp)1969 datael_add_child(const scf_datael_t *dp, const char *name, uint32_t type,
1970     scf_datael_t *cp)
1971 {
1972 	scf_handle_t *h = dp->rd_handle;
1973 
1974 	struct rep_protocol_entity_create_child request;
1975 	struct rep_protocol_response response;
1976 	ssize_t r;
1977 	uint32_t held = 0;
1978 
1979 	if (cp == NULL) {
1980 		switch (type) {
1981 		case REP_PROTOCOL_ENTITY_SCOPE:
1982 			cp = &HANDLE_HOLD_SCOPE(h)->rd_d;
1983 			held = RH_HOLD_SCOPE;
1984 			break;
1985 		case REP_PROTOCOL_ENTITY_SERVICE:
1986 			cp = &HANDLE_HOLD_SERVICE(h)->rd_d;
1987 			held = RH_HOLD_SERVICE;
1988 			break;
1989 		case REP_PROTOCOL_ENTITY_INSTANCE:
1990 			cp = &HANDLE_HOLD_INSTANCE(h)->rd_d;
1991 			held = RH_HOLD_INSTANCE;
1992 			break;
1993 		case REP_PROTOCOL_ENTITY_SNAPSHOT:
1994 		default:
1995 			assert(0);
1996 			abort();
1997 		}
1998 		assert(h == cp->rd_handle);
1999 
2000 	} else if (h != cp->rd_handle) {
2001 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2002 	}
2003 
2004 	if (strlcpy(request.rpr_name, name, sizeof (request.rpr_name)) >=
2005 	    sizeof (request.rpr_name)) {
2006 		r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2007 		goto err;
2008 	}
2009 
2010 	(void) pthread_mutex_lock(&h->rh_lock);
2011 	request.rpr_request = REP_PROTOCOL_ENTITY_CREATE_CHILD;
2012 	request.rpr_entityid = dp->rd_entity;
2013 	request.rpr_childtype = type;
2014 	request.rpr_childid = cp->rd_entity;
2015 
2016 	datael_finish_reset(dp);
2017 	request.rpr_changeid = handle_next_changeid(h);
2018 	r = make_door_call(h, &request, sizeof (request),
2019 	    &response, sizeof (response));
2020 	(void) pthread_mutex_unlock(&h->rh_lock);
2021 
2022 	if (held)
2023 		handle_rele_subhandles(h, held);
2024 
2025 	if (r < 0)
2026 		DOOR_ERRORS_BLOCK(r);
2027 
2028 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
2029 		return (scf_set_error(proto_error(response.rpr_response)));
2030 
2031 	return (SCF_SUCCESS);
2032 
2033 err:
2034 	if (held)
2035 		handle_rele_subhandles(h, held);
2036 	return (r);
2037 }
2038 
2039 static int
datael_add_pg(const scf_datael_t * dp,const char * name,const char * type,uint32_t flags,scf_datael_t * cp)2040 datael_add_pg(const scf_datael_t *dp, const char *name, const char *type,
2041     uint32_t flags, scf_datael_t *cp)
2042 {
2043 	scf_handle_t *h = dp->rd_handle;
2044 
2045 	struct rep_protocol_entity_create_pg request;
2046 	struct rep_protocol_response response;
2047 	ssize_t r;
2048 
2049 	int holding_els = 0;
2050 
2051 	if (cp == NULL) {
2052 		holding_els = 1;
2053 		cp = &HANDLE_HOLD_PG(h)->rd_d;
2054 		assert(h == cp->rd_handle);
2055 
2056 	} else if (h != cp->rd_handle) {
2057 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2058 	}
2059 
2060 	request.rpr_request = REP_PROTOCOL_ENTITY_CREATE_PG;
2061 
2062 	if (name == NULL || strlcpy(request.rpr_name, name,
2063 	    sizeof (request.rpr_name)) > sizeof (request.rpr_name)) {
2064 		r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2065 		goto err;
2066 	}
2067 
2068 	if (type == NULL || strlcpy(request.rpr_type, type,
2069 	    sizeof (request.rpr_type)) > sizeof (request.rpr_type)) {
2070 		r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2071 		goto err;
2072 	}
2073 
2074 	(void) pthread_mutex_lock(&h->rh_lock);
2075 	request.rpr_entityid = dp->rd_entity;
2076 	request.rpr_childid = cp->rd_entity;
2077 	request.rpr_flags = flags;
2078 
2079 	datael_finish_reset(dp);
2080 	datael_finish_reset(cp);
2081 	request.rpr_changeid = handle_next_changeid(h);
2082 	r = make_door_call(h, &request, sizeof (request),
2083 	    &response, sizeof (response));
2084 	(void) pthread_mutex_unlock(&h->rh_lock);
2085 
2086 	if (holding_els)
2087 		HANDLE_RELE_PG(h);
2088 
2089 	if (r < 0)
2090 		DOOR_ERRORS_BLOCK(r);
2091 
2092 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
2093 		return (scf_set_error(proto_error(response.rpr_response)));
2094 
2095 	return (SCF_SUCCESS);
2096 
2097 err:
2098 	if (holding_els)
2099 		HANDLE_RELE_PG(h);
2100 	return (r);
2101 }
2102 
2103 static int
datael_delete(const scf_datael_t * dp)2104 datael_delete(const scf_datael_t *dp)
2105 {
2106 	scf_handle_t *h = dp->rd_handle;
2107 
2108 	struct rep_protocol_entity_delete request;
2109 	struct rep_protocol_response response;
2110 	ssize_t r;
2111 
2112 	(void) pthread_mutex_lock(&h->rh_lock);
2113 	request.rpr_request = REP_PROTOCOL_ENTITY_DELETE;
2114 	request.rpr_entityid = dp->rd_entity;
2115 
2116 	datael_finish_reset(dp);
2117 	request.rpr_changeid = handle_next_changeid(h);
2118 	r = make_door_call(h, &request, sizeof (request),
2119 	    &response, sizeof (response));
2120 	(void) pthread_mutex_unlock(&h->rh_lock);
2121 
2122 	if (r < 0)
2123 		DOOR_ERRORS_BLOCK(r);
2124 
2125 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
2126 		return (scf_set_error(proto_error(response.rpr_response)));
2127 
2128 	return (SCF_SUCCESS);
2129 }
2130 
2131 /*
2132  * Fails with
2133  *   _INVALID_ARGUMENT - h is NULL
2134  *   _NO_MEMORY
2135  *   _HANDLE_DESTROYED - h has been destroyed
2136  *   _INTERNAL - server response too big
2137  *		 iter already exists
2138  *   _NO_RESOURCES
2139  */
2140 scf_iter_t *
scf_iter_create(scf_handle_t * h)2141 scf_iter_create(scf_handle_t *h)
2142 {
2143 	scf_iter_t *iter;
2144 
2145 	if (h == NULL) {
2146 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2147 		return (NULL);
2148 	}
2149 
2150 	iter = uu_zalloc(sizeof (*iter));
2151 	if (iter == NULL) {
2152 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2153 		return (NULL);
2154 	}
2155 
2156 	uu_list_node_init(iter, &iter->iter_node, iter_pool);
2157 	iter->iter_handle = h;
2158 	iter->iter_sequence = 1;
2159 	iter->iter_type = REP_PROTOCOL_ENTITY_NONE;
2160 
2161 	(void) pthread_mutex_lock(&h->rh_lock);
2162 	iter->iter_id = handle_alloc_iterid(h);
2163 	if (iter->iter_id == 0) {
2164 		(void) pthread_mutex_unlock(&h->rh_lock);
2165 		uu_list_node_fini(iter, &iter->iter_node, iter_pool);
2166 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2167 		uu_free(iter);
2168 		return (NULL);
2169 	}
2170 	if (iter_attach(iter) == -1) {
2171 		uu_list_node_fini(iter, &iter->iter_node, iter_pool);
2172 		(void) pthread_mutex_unlock(&h->rh_lock);
2173 		uu_free(iter);
2174 		return (NULL);
2175 	}
2176 	(void) uu_list_insert_before(h->rh_iters, NULL, iter);
2177 	h->rh_extrefs++;
2178 	(void) pthread_mutex_unlock(&h->rh_lock);
2179 	return (iter);
2180 }
2181 
2182 scf_handle_t *
scf_iter_handle(const scf_iter_t * iter)2183 scf_iter_handle(const scf_iter_t *iter)
2184 {
2185 	return (handle_get(iter->iter_handle));
2186 }
2187 
2188 static void
scf_iter_reset_locked(scf_iter_t * iter)2189 scf_iter_reset_locked(scf_iter_t *iter)
2190 {
2191 	struct rep_protocol_iter_request request;
2192 	struct rep_protocol_response response;
2193 
2194 	request.rpr_request = REP_PROTOCOL_ITER_RESET;
2195 	request.rpr_iterid = iter->iter_id;
2196 
2197 	assert(MUTEX_HELD(&iter->iter_handle->rh_lock));
2198 
2199 	(void) make_door_call(iter->iter_handle,
2200 	    &request, sizeof (request), &response, sizeof (response));
2201 
2202 	iter->iter_type = REP_PROTOCOL_ENTITY_NONE;
2203 	iter->iter_sequence = 1;
2204 }
2205 
2206 void
scf_iter_reset(scf_iter_t * iter)2207 scf_iter_reset(scf_iter_t *iter)
2208 {
2209 	(void) pthread_mutex_lock(&iter->iter_handle->rh_lock);
2210 	scf_iter_reset_locked(iter);
2211 	(void) pthread_mutex_unlock(&iter->iter_handle->rh_lock);
2212 }
2213 
2214 void
scf_iter_destroy(scf_iter_t * iter)2215 scf_iter_destroy(scf_iter_t *iter)
2216 {
2217 	scf_handle_t *handle;
2218 
2219 	struct rep_protocol_iter_request request;
2220 	struct rep_protocol_response response;
2221 
2222 	if (iter == NULL)
2223 		return;
2224 
2225 	handle = iter->iter_handle;
2226 
2227 	(void) pthread_mutex_lock(&handle->rh_lock);
2228 	request.rpr_request = REP_PROTOCOL_ITER_TEARDOWN;
2229 	request.rpr_iterid = iter->iter_id;
2230 
2231 	(void) make_door_call(handle, &request, sizeof (request),
2232 	    &response, sizeof (response));
2233 
2234 	uu_list_remove(handle->rh_iters, iter);
2235 	--handle->rh_extrefs;
2236 	handle_unrefed(handle);			/* drops h->rh_lock */
2237 	iter->iter_handle = NULL;
2238 
2239 	uu_list_node_fini(iter, &iter->iter_node, iter_pool);
2240 	uu_free(iter);
2241 }
2242 
2243 static int
handle_get_local_scope_locked(scf_handle_t * handle,scf_scope_t * out)2244 handle_get_local_scope_locked(scf_handle_t *handle, scf_scope_t *out)
2245 {
2246 	struct rep_protocol_entity_get request;
2247 	struct rep_protocol_name_response response;
2248 	ssize_t r;
2249 
2250 	assert(MUTEX_HELD(&handle->rh_lock));
2251 
2252 	if (handle != out->rd_d.rd_handle)
2253 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2254 
2255 	request.rpr_request = REP_PROTOCOL_ENTITY_GET;
2256 	request.rpr_entityid = out->rd_d.rd_entity;
2257 	request.rpr_object = RP_ENTITY_GET_MOST_LOCAL_SCOPE;
2258 
2259 	datael_finish_reset(&out->rd_d);
2260 	r = make_door_call(handle, &request, sizeof (request),
2261 	    &response, sizeof (response));
2262 
2263 	if (r < 0)
2264 		DOOR_ERRORS_BLOCK(r);
2265 
2266 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
2267 		return (scf_set_error(proto_error(response.rpr_response)));
2268 
2269 	return (SCF_SUCCESS);
2270 }
2271 
2272 int
scf_iter_handle_scopes(scf_iter_t * iter,const scf_handle_t * handle)2273 scf_iter_handle_scopes(scf_iter_t *iter, const scf_handle_t *handle)
2274 {
2275 	scf_handle_t *h = iter->iter_handle;
2276 	if (h != handle)
2277 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2278 
2279 	(void) pthread_mutex_lock(&h->rh_lock);
2280 	scf_iter_reset_locked(iter);
2281 
2282 	if (!handle_is_bound(h)) {
2283 		(void) pthread_mutex_unlock(&h->rh_lock);
2284 		return (scf_set_error(SCF_ERROR_NOT_BOUND));
2285 	}
2286 
2287 	if (!handle_has_server_locked(h)) {
2288 		(void) pthread_mutex_unlock(&h->rh_lock);
2289 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));
2290 	}
2291 
2292 	iter->iter_type = REP_PROTOCOL_ENTITY_SCOPE;
2293 	iter->iter_sequence = 1;
2294 	(void) pthread_mutex_unlock(&h->rh_lock);
2295 	return (0);
2296 }
2297 
2298 int
scf_iter_next_scope(scf_iter_t * iter,scf_scope_t * out)2299 scf_iter_next_scope(scf_iter_t *iter, scf_scope_t *out)
2300 {
2301 	int ret;
2302 	scf_handle_t *h = iter->iter_handle;
2303 
2304 	if (h != out->rd_d.rd_handle)
2305 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2306 
2307 	(void) pthread_mutex_lock(&h->rh_lock);
2308 	if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE) {
2309 		(void) pthread_mutex_unlock(&h->rh_lock);
2310 		return (scf_set_error(SCF_ERROR_NOT_SET));
2311 	}
2312 	if (iter->iter_type != REP_PROTOCOL_ENTITY_SCOPE) {
2313 		(void) pthread_mutex_unlock(&h->rh_lock);
2314 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
2315 	}
2316 	if (iter->iter_sequence == 1) {
2317 		if ((ret = handle_get_local_scope_locked(h, out)) ==
2318 		    SCF_SUCCESS) {
2319 			iter->iter_sequence++;
2320 			ret = 1;
2321 		}
2322 	} else {
2323 		datael_reset_locked(&out->rd_d);
2324 		ret = 0;
2325 	}
2326 	(void) pthread_mutex_unlock(&h->rh_lock);
2327 	return (ret);
2328 }
2329 
2330 int
scf_handle_get_scope(scf_handle_t * h,const char * name,scf_scope_t * out)2331 scf_handle_get_scope(scf_handle_t *h, const char *name, scf_scope_t *out)
2332 {
2333 	int ret;
2334 
2335 	if (h != out->rd_d.rd_handle)
2336 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2337 
2338 	(void) pthread_mutex_lock(&h->rh_lock);
2339 	if (strcmp(name, SCF_SCOPE_LOCAL) == 0) {
2340 		ret = handle_get_local_scope_locked(h, out);
2341 	} else {
2342 		datael_reset_locked(&out->rd_d);
2343 		if (uu_check_name(name, 0) == -1)
2344 			ret = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2345 		else
2346 			ret = scf_set_error(SCF_ERROR_NOT_FOUND);
2347 	}
2348 	(void) pthread_mutex_unlock(&h->rh_lock);
2349 	return (ret);
2350 }
2351 
2352 static int
datael_setup_iter(scf_iter_t * iter,const scf_datael_t * dp,uint32_t res_type,boolean_t composed)2353 datael_setup_iter(scf_iter_t *iter, const scf_datael_t *dp, uint32_t res_type,
2354     boolean_t composed)
2355 {
2356 	scf_handle_t *h = dp->rd_handle;
2357 
2358 	struct rep_protocol_iter_start request;
2359 	struct rep_protocol_response response;
2360 
2361 	ssize_t r;
2362 
2363 	if (h != iter->iter_handle)
2364 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2365 
2366 	(void) pthread_mutex_lock(&h->rh_lock);
2367 	scf_iter_reset_locked(iter);
2368 	iter->iter_type = res_type;
2369 
2370 	request.rpr_request = REP_PROTOCOL_ITER_START;
2371 	request.rpr_iterid = iter->iter_id;
2372 	request.rpr_entity = dp->rd_entity;
2373 	request.rpr_itertype = res_type;
2374 	request.rpr_flags = RP_ITER_START_ALL |
2375 	    (composed ? RP_ITER_START_COMPOSED : 0);
2376 	request.rpr_pattern[0] = 0;
2377 
2378 	datael_finish_reset(dp);
2379 	r = make_door_call(h, &request, sizeof (request),
2380 	    &response, sizeof (response));
2381 
2382 	if (r < 0) {
2383 		(void) pthread_mutex_unlock(&h->rh_lock);
2384 		DOOR_ERRORS_BLOCK(r);
2385 	}
2386 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
2387 		(void) pthread_mutex_unlock(&h->rh_lock);
2388 		return (scf_set_error(proto_error(response.rpr_response)));
2389 	}
2390 	iter->iter_sequence++;
2391 	(void) pthread_mutex_unlock(&h->rh_lock);
2392 	return (SCF_SUCCESS);
2393 }
2394 
2395 static int
datael_setup_iter_pgtyped(scf_iter_t * iter,const scf_datael_t * dp,const char * pgtype,boolean_t composed)2396 datael_setup_iter_pgtyped(scf_iter_t *iter, const scf_datael_t *dp,
2397     const char *pgtype, boolean_t composed)
2398 {
2399 	scf_handle_t *h = dp->rd_handle;
2400 
2401 	struct rep_protocol_iter_start request;
2402 	struct rep_protocol_response response;
2403 
2404 	ssize_t r;
2405 
2406 	if (h != iter->iter_handle)
2407 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2408 
2409 	if (pgtype == NULL || strlcpy(request.rpr_pattern, pgtype,
2410 	    sizeof (request.rpr_pattern)) >= sizeof (request.rpr_pattern)) {
2411 		scf_iter_reset(iter);
2412 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
2413 	}
2414 
2415 	(void) pthread_mutex_lock(&h->rh_lock);
2416 	request.rpr_request = REP_PROTOCOL_ITER_START;
2417 	request.rpr_iterid = iter->iter_id;
2418 	request.rpr_entity = dp->rd_entity;
2419 	request.rpr_itertype = REP_PROTOCOL_ENTITY_PROPERTYGRP;
2420 	request.rpr_flags = RP_ITER_START_PGTYPE |
2421 	    (composed ? RP_ITER_START_COMPOSED : 0);
2422 
2423 	datael_finish_reset(dp);
2424 	scf_iter_reset_locked(iter);
2425 	iter->iter_type = REP_PROTOCOL_ENTITY_PROPERTYGRP;
2426 
2427 	r = make_door_call(h, &request, sizeof (request),
2428 	    &response, sizeof (response));
2429 
2430 	if (r < 0) {
2431 		(void) pthread_mutex_unlock(&h->rh_lock);
2432 
2433 		DOOR_ERRORS_BLOCK(r);
2434 	}
2435 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
2436 		(void) pthread_mutex_unlock(&h->rh_lock);
2437 		return (scf_set_error(proto_error(response.rpr_response)));
2438 	}
2439 	iter->iter_sequence++;
2440 	(void) pthread_mutex_unlock(&h->rh_lock);
2441 	return (SCF_SUCCESS);
2442 }
2443 
2444 static int
datael_iter_next(scf_iter_t * iter,scf_datael_t * out)2445 datael_iter_next(scf_iter_t *iter, scf_datael_t *out)
2446 {
2447 	scf_handle_t *h = iter->iter_handle;
2448 
2449 	struct rep_protocol_iter_read request;
2450 	struct rep_protocol_response response;
2451 	ssize_t r;
2452 
2453 	if (h != out->rd_handle)
2454 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2455 
2456 	(void) pthread_mutex_lock(&h->rh_lock);
2457 	if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE ||
2458 	    iter->iter_sequence == 1) {
2459 		(void) pthread_mutex_unlock(&h->rh_lock);
2460 		return (scf_set_error(SCF_ERROR_NOT_SET));
2461 	}
2462 
2463 	if (out->rd_type != iter->iter_type) {
2464 		(void) pthread_mutex_unlock(&h->rh_lock);
2465 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
2466 	}
2467 
2468 	request.rpr_request = REP_PROTOCOL_ITER_READ;
2469 	request.rpr_iterid = iter->iter_id;
2470 	request.rpr_sequence = iter->iter_sequence;
2471 	request.rpr_entityid = out->rd_entity;
2472 
2473 	datael_finish_reset(out);
2474 	r = make_door_call(h, &request, sizeof (request),
2475 	    &response, sizeof (response));
2476 
2477 	if (r < 0) {
2478 		(void) pthread_mutex_unlock(&h->rh_lock);
2479 		DOOR_ERRORS_BLOCK(r);
2480 	}
2481 
2482 	if (response.rpr_response == REP_PROTOCOL_DONE) {
2483 		(void) pthread_mutex_unlock(&h->rh_lock);
2484 		return (0);
2485 	}
2486 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
2487 		(void) pthread_mutex_unlock(&h->rh_lock);
2488 		return (scf_set_error(proto_error(response.rpr_response)));
2489 	}
2490 	iter->iter_sequence++;
2491 	(void) pthread_mutex_unlock(&h->rh_lock);
2492 
2493 	return (1);
2494 }
2495 
2496 int
scf_iter_scope_services(scf_iter_t * iter,const scf_scope_t * s)2497 scf_iter_scope_services(scf_iter_t *iter, const scf_scope_t *s)
2498 {
2499 	return (datael_setup_iter(iter, &s->rd_d,
2500 	    REP_PROTOCOL_ENTITY_SERVICE, 0));
2501 }
2502 
2503 int
scf_iter_next_service(scf_iter_t * iter,scf_service_t * out)2504 scf_iter_next_service(scf_iter_t *iter, scf_service_t *out)
2505 {
2506 	return (datael_iter_next(iter, &out->rd_d));
2507 }
2508 
2509 int
scf_iter_service_instances(scf_iter_t * iter,const scf_service_t * svc)2510 scf_iter_service_instances(scf_iter_t *iter, const scf_service_t *svc)
2511 {
2512 	return (datael_setup_iter(iter, &svc->rd_d,
2513 	    REP_PROTOCOL_ENTITY_INSTANCE, 0));
2514 }
2515 
2516 int
scf_iter_next_instance(scf_iter_t * iter,scf_instance_t * out)2517 scf_iter_next_instance(scf_iter_t *iter, scf_instance_t *out)
2518 {
2519 	return (datael_iter_next(iter, &out->rd_d));
2520 }
2521 
2522 int
scf_iter_service_pgs(scf_iter_t * iter,const scf_service_t * svc)2523 scf_iter_service_pgs(scf_iter_t *iter, const scf_service_t *svc)
2524 {
2525 	return (datael_setup_iter(iter, &svc->rd_d,
2526 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 0));
2527 }
2528 
2529 int
scf_iter_service_pgs_typed(scf_iter_t * iter,const scf_service_t * svc,const char * type)2530 scf_iter_service_pgs_typed(scf_iter_t *iter, const scf_service_t *svc,
2531     const char *type)
2532 {
2533 	return (datael_setup_iter_pgtyped(iter, &svc->rd_d, type, 0));
2534 }
2535 
2536 int
scf_iter_instance_snapshots(scf_iter_t * iter,const scf_instance_t * inst)2537 scf_iter_instance_snapshots(scf_iter_t *iter, const scf_instance_t *inst)
2538 {
2539 	return (datael_setup_iter(iter, &inst->rd_d,
2540 	    REP_PROTOCOL_ENTITY_SNAPSHOT, 0));
2541 }
2542 
2543 int
scf_iter_next_snapshot(scf_iter_t * iter,scf_snapshot_t * out)2544 scf_iter_next_snapshot(scf_iter_t *iter, scf_snapshot_t *out)
2545 {
2546 	return (datael_iter_next(iter, &out->rd_d));
2547 }
2548 
2549 int
scf_iter_instance_pgs(scf_iter_t * iter,const scf_instance_t * inst)2550 scf_iter_instance_pgs(scf_iter_t *iter, const scf_instance_t *inst)
2551 {
2552 	return (datael_setup_iter(iter, &inst->rd_d,
2553 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 0));
2554 }
2555 
2556 int
scf_iter_instance_pgs_typed(scf_iter_t * iter,const scf_instance_t * inst,const char * type)2557 scf_iter_instance_pgs_typed(scf_iter_t *iter, const scf_instance_t *inst,
2558     const char *type)
2559 {
2560 	return (datael_setup_iter_pgtyped(iter, &inst->rd_d, type, 0));
2561 }
2562 
2563 int
scf_iter_instance_pgs_composed(scf_iter_t * iter,const scf_instance_t * inst,const scf_snapshot_t * snap)2564 scf_iter_instance_pgs_composed(scf_iter_t *iter, const scf_instance_t *inst,
2565     const scf_snapshot_t *snap)
2566 {
2567 	if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle)
2568 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2569 
2570 	return (datael_setup_iter(iter, snap ? &snap->rd_d : &inst->rd_d,
2571 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 1));
2572 }
2573 
2574 int
scf_iter_instance_pgs_typed_composed(scf_iter_t * iter,const scf_instance_t * inst,const scf_snapshot_t * snap,const char * type)2575 scf_iter_instance_pgs_typed_composed(scf_iter_t *iter,
2576     const scf_instance_t *inst, const scf_snapshot_t *snap, const char *type)
2577 {
2578 	if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle)
2579 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2580 
2581 	return (datael_setup_iter_pgtyped(iter,
2582 	    snap ? &snap->rd_d : &inst->rd_d, type, 1));
2583 }
2584 
2585 int
scf_iter_snaplevel_pgs(scf_iter_t * iter,const scf_snaplevel_t * inst)2586 scf_iter_snaplevel_pgs(scf_iter_t *iter, const scf_snaplevel_t *inst)
2587 {
2588 	return (datael_setup_iter(iter, &inst->rd_d,
2589 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 0));
2590 }
2591 
2592 int
scf_iter_snaplevel_pgs_typed(scf_iter_t * iter,const scf_snaplevel_t * inst,const char * type)2593 scf_iter_snaplevel_pgs_typed(scf_iter_t *iter, const scf_snaplevel_t *inst,
2594     const char *type)
2595 {
2596 	return (datael_setup_iter_pgtyped(iter, &inst->rd_d, type, 0));
2597 }
2598 
2599 int
scf_iter_next_pg(scf_iter_t * iter,scf_propertygroup_t * out)2600 scf_iter_next_pg(scf_iter_t *iter, scf_propertygroup_t *out)
2601 {
2602 	return (datael_iter_next(iter, &out->rd_d));
2603 }
2604 
2605 int
scf_iter_pg_properties(scf_iter_t * iter,const scf_propertygroup_t * pg)2606 scf_iter_pg_properties(scf_iter_t *iter, const scf_propertygroup_t *pg)
2607 {
2608 	return (datael_setup_iter(iter, &pg->rd_d,
2609 	    REP_PROTOCOL_ENTITY_PROPERTY, 0));
2610 }
2611 
2612 int
scf_iter_next_property(scf_iter_t * iter,scf_property_t * out)2613 scf_iter_next_property(scf_iter_t *iter, scf_property_t *out)
2614 {
2615 	return (datael_iter_next(iter, &out->rd_d));
2616 }
2617 
2618 /*
2619  * Fails with
2620  *   _INVALID_ARGUMENT - handle is NULL
2621  *   _INTERNAL - server response too big
2622  *		 entity already set up with different type
2623  *   _NO_RESOURCES
2624  *   _NO_MEMORY
2625  */
2626 scf_scope_t *
scf_scope_create(scf_handle_t * handle)2627 scf_scope_create(scf_handle_t *handle)
2628 {
2629 	scf_scope_t *ret;
2630 
2631 	ret = uu_zalloc(sizeof (*ret));
2632 	if (ret != NULL) {
2633 		if (datael_init(&ret->rd_d, handle,
2634 		    REP_PROTOCOL_ENTITY_SCOPE) == -1) {
2635 			uu_free(ret);
2636 			return (NULL);
2637 		}
2638 	} else {
2639 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2640 	}
2641 
2642 	return (ret);
2643 }
2644 
2645 scf_handle_t *
scf_scope_handle(const scf_scope_t * val)2646 scf_scope_handle(const scf_scope_t *val)
2647 {
2648 	return (datael_handle(&val->rd_d));
2649 }
2650 
2651 void
scf_scope_destroy(scf_scope_t * val)2652 scf_scope_destroy(scf_scope_t *val)
2653 {
2654 	if (val == NULL)
2655 		return;
2656 
2657 	datael_destroy(&val->rd_d);
2658 	uu_free(val);
2659 }
2660 
2661 ssize_t
scf_scope_get_name(const scf_scope_t * rep,char * out,size_t len)2662 scf_scope_get_name(const scf_scope_t *rep, char *out, size_t len)
2663 {
2664 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
2665 }
2666 
2667 /*ARGSUSED*/
2668 int
scf_scope_get_parent(const scf_scope_t * child,scf_scope_t * parent)2669 scf_scope_get_parent(const scf_scope_t *child, scf_scope_t *parent)
2670 {
2671 	char name[1];
2672 
2673 	/* fake up the side-effects */
2674 	datael_reset(&parent->rd_d);
2675 	if (scf_scope_get_name(child, name, sizeof (name)) < 0)
2676 		return (-1);
2677 	return (scf_set_error(SCF_ERROR_NOT_FOUND));
2678 }
2679 
2680 /*
2681  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
2682  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
2683  */
2684 scf_service_t *
scf_service_create(scf_handle_t * handle)2685 scf_service_create(scf_handle_t *handle)
2686 {
2687 	scf_service_t *ret;
2688 	ret = uu_zalloc(sizeof (*ret));
2689 	if (ret != NULL) {
2690 		if (datael_init(&ret->rd_d, handle,
2691 		    REP_PROTOCOL_ENTITY_SERVICE) == -1) {
2692 			uu_free(ret);
2693 			return (NULL);
2694 		}
2695 	} else {
2696 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2697 	}
2698 
2699 	return (ret);
2700 }
2701 
2702 
2703 /*
2704  * Fails with
2705  *   _HANDLE_MISMATCH
2706  *   _INVALID_ARGUMENT
2707  *   _NOT_BOUND
2708  *   _CONNECTION_BROKEN
2709  *   _INTERNAL
2710  *   _EXISTS
2711  *   _DELETED
2712  *   _NOT_SET
2713  *   _NO_RESOURCES
2714  *   _PERMISSION_DENIED
2715  *   _BACKEND_ACCESS
2716  *   _BACKEND_READONLY
2717  */
2718 int
scf_scope_add_service(const scf_scope_t * scope,const char * name,scf_service_t * svc)2719 scf_scope_add_service(const scf_scope_t *scope, const char *name,
2720     scf_service_t *svc)
2721 {
2722 	return (datael_add_child(&scope->rd_d, name,
2723 	    REP_PROTOCOL_ENTITY_SERVICE, (svc != NULL)? &svc->rd_d : NULL));
2724 }
2725 
2726 /*
2727  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
2728  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
2729  * _BACKEND_ACCESS, _NOT_FOUND.
2730  */
2731 int
scf_scope_get_service(const scf_scope_t * s,const char * name,scf_service_t * svc)2732 scf_scope_get_service(const scf_scope_t *s, const char *name,
2733     scf_service_t *svc)
2734 {
2735 	return (datael_get_child(&s->rd_d, name, REP_PROTOCOL_ENTITY_SERVICE,
2736 	    svc ? &svc->rd_d : NULL, 0));
2737 }
2738 
2739 scf_handle_t *
scf_service_handle(const scf_service_t * val)2740 scf_service_handle(const scf_service_t *val)
2741 {
2742 	return (datael_handle(&val->rd_d));
2743 }
2744 
2745 int
scf_service_delete(scf_service_t * svc)2746 scf_service_delete(scf_service_t *svc)
2747 {
2748 	return (datael_delete(&svc->rd_d));
2749 }
2750 
2751 int
scf_instance_delete(scf_instance_t * inst)2752 scf_instance_delete(scf_instance_t *inst)
2753 {
2754 	return (datael_delete(&inst->rd_d));
2755 }
2756 
2757 int
scf_pg_delete(scf_propertygroup_t * pg)2758 scf_pg_delete(scf_propertygroup_t *pg)
2759 {
2760 	return (datael_delete(&pg->rd_d));
2761 }
2762 
2763 int
_scf_snapshot_delete(scf_snapshot_t * snap)2764 _scf_snapshot_delete(scf_snapshot_t *snap)
2765 {
2766 	return (datael_delete(&snap->rd_d));
2767 }
2768 
2769 /*
2770  * Fails with
2771  *   _HANDLE_MISMATCH
2772  *   _INVALID_ARGUMENT
2773  *   _NOT_BOUND
2774  *   _CONNECTION_BROKEN
2775  *   _INTERNAL
2776  *   _EXISTS
2777  *   _DELETED
2778  *   _NOT_SET
2779  *   _NO_RESOURCES
2780  *   _PERMISSION_DENIED
2781  *   _BACKEND_ACCESS
2782  *   _BACKEND_READONLY
2783  */
2784 int
scf_service_add_instance(const scf_service_t * svc,const char * name,scf_instance_t * instance)2785 scf_service_add_instance(const scf_service_t *svc, const char *name,
2786     scf_instance_t *instance)
2787 {
2788 	return (datael_add_child(&svc->rd_d, name,
2789 	    REP_PROTOCOL_ENTITY_INSTANCE,
2790 	    (instance != NULL)? &instance->rd_d : NULL));
2791 }
2792 
2793 
2794 /*
2795  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
2796  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
2797  * _BACKEND_ACCESS, _NOT_FOUND.
2798  */
2799 int
scf_service_get_instance(const scf_service_t * svc,const char * name,scf_instance_t * inst)2800 scf_service_get_instance(const scf_service_t *svc, const char *name,
2801     scf_instance_t *inst)
2802 {
2803 	return (datael_get_child(&svc->rd_d, name, REP_PROTOCOL_ENTITY_INSTANCE,
2804 	    inst ? &inst->rd_d : NULL, 0));
2805 }
2806 
2807 int
scf_service_add_pg(const scf_service_t * svc,const char * name,const char * type,uint32_t flags,scf_propertygroup_t * pg)2808 scf_service_add_pg(const scf_service_t *svc, const char *name,
2809     const char *type, uint32_t flags, scf_propertygroup_t *pg)
2810 {
2811 	return (datael_add_pg(&svc->rd_d, name, type, flags,
2812 	    (pg != NULL)?&pg->rd_d : NULL));
2813 }
2814 
2815 /*
2816  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
2817  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
2818  * _BACKEND_ACCESS, _NOT_FOUND.
2819  */
2820 int
scf_service_get_pg(const scf_service_t * svc,const char * name,scf_propertygroup_t * pg)2821 scf_service_get_pg(const scf_service_t *svc, const char *name,
2822     scf_propertygroup_t *pg)
2823 {
2824 	return (datael_get_child(&svc->rd_d, name,
2825 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0));
2826 }
2827 
2828 int
scf_instance_add_pg(const scf_instance_t * inst,const char * name,const char * type,uint32_t flags,scf_propertygroup_t * pg)2829 scf_instance_add_pg(const scf_instance_t *inst, const char *name,
2830     const char *type, uint32_t flags, scf_propertygroup_t *pg)
2831 {
2832 	return (datael_add_pg(&inst->rd_d, name, type, flags,
2833 	    (pg != NULL)?&pg->rd_d : NULL));
2834 }
2835 
2836 /*
2837  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
2838  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
2839  * _BACKEND_ACCESS, _NOT_FOUND.
2840  */
2841 int
scf_instance_get_snapshot(const scf_instance_t * inst,const char * name,scf_snapshot_t * pg)2842 scf_instance_get_snapshot(const scf_instance_t *inst, const char *name,
2843     scf_snapshot_t *pg)
2844 {
2845 	return (datael_get_child(&inst->rd_d, name,
2846 	    REP_PROTOCOL_ENTITY_SNAPSHOT, pg ? &pg->rd_d : NULL, 0));
2847 }
2848 
2849 /*
2850  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
2851  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
2852  * _BACKEND_ACCESS, _NOT_FOUND.
2853  */
2854 int
scf_instance_get_pg(const scf_instance_t * inst,const char * name,scf_propertygroup_t * pg)2855 scf_instance_get_pg(const scf_instance_t *inst, const char *name,
2856     scf_propertygroup_t *pg)
2857 {
2858 	return (datael_get_child(&inst->rd_d, name,
2859 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0));
2860 }
2861 
2862 /*
2863  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
2864  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
2865  * _BACKEND_ACCESS, _NOT_FOUND.
2866  */
2867 int
scf_instance_get_pg_composed(const scf_instance_t * inst,const scf_snapshot_t * snap,const char * name,scf_propertygroup_t * pg)2868 scf_instance_get_pg_composed(const scf_instance_t *inst,
2869     const scf_snapshot_t *snap, const char *name, scf_propertygroup_t *pg)
2870 {
2871 	if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle)
2872 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
2873 
2874 	return (datael_get_child(snap ? &snap->rd_d : &inst->rd_d, name,
2875 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 1));
2876 }
2877 
2878 /*
2879  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
2880  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
2881  * _BACKEND_ACCESS, _NOT_FOUND.
2882  */
2883 int
scf_pg_get_property(const scf_propertygroup_t * pg,const char * name,scf_property_t * prop)2884 scf_pg_get_property(const scf_propertygroup_t *pg, const char *name,
2885     scf_property_t *prop)
2886 {
2887 	return (datael_get_child(&pg->rd_d, name, REP_PROTOCOL_ENTITY_PROPERTY,
2888 	    prop ? &prop->rd_d : NULL, 0));
2889 }
2890 
2891 void
scf_service_destroy(scf_service_t * val)2892 scf_service_destroy(scf_service_t *val)
2893 {
2894 	if (val == NULL)
2895 		return;
2896 
2897 	datael_destroy(&val->rd_d);
2898 	uu_free(val);
2899 }
2900 
2901 ssize_t
scf_service_get_name(const scf_service_t * rep,char * out,size_t len)2902 scf_service_get_name(const scf_service_t *rep, char *out, size_t len)
2903 {
2904 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
2905 }
2906 
2907 /*
2908  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
2909  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
2910  */
2911 scf_instance_t *
scf_instance_create(scf_handle_t * handle)2912 scf_instance_create(scf_handle_t *handle)
2913 {
2914 	scf_instance_t *ret;
2915 
2916 	ret = uu_zalloc(sizeof (*ret));
2917 	if (ret != NULL) {
2918 		if (datael_init(&ret->rd_d, handle,
2919 		    REP_PROTOCOL_ENTITY_INSTANCE) == -1) {
2920 			uu_free(ret);
2921 			return (NULL);
2922 		}
2923 	} else {
2924 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2925 	}
2926 
2927 	return (ret);
2928 }
2929 
2930 scf_handle_t *
scf_instance_handle(const scf_instance_t * val)2931 scf_instance_handle(const scf_instance_t *val)
2932 {
2933 	return (datael_handle(&val->rd_d));
2934 }
2935 
2936 void
scf_instance_destroy(scf_instance_t * val)2937 scf_instance_destroy(scf_instance_t *val)
2938 {
2939 	if (val == NULL)
2940 		return;
2941 
2942 	datael_destroy(&val->rd_d);
2943 	uu_free(val);
2944 }
2945 
2946 ssize_t
scf_instance_get_name(const scf_instance_t * rep,char * out,size_t len)2947 scf_instance_get_name(const scf_instance_t *rep, char *out, size_t len)
2948 {
2949 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
2950 }
2951 
2952 /*
2953  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
2954  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
2955  */
2956 scf_snapshot_t *
scf_snapshot_create(scf_handle_t * handle)2957 scf_snapshot_create(scf_handle_t *handle)
2958 {
2959 	scf_snapshot_t *ret;
2960 
2961 	ret = uu_zalloc(sizeof (*ret));
2962 	if (ret != NULL) {
2963 		if (datael_init(&ret->rd_d, handle,
2964 		    REP_PROTOCOL_ENTITY_SNAPSHOT) == -1) {
2965 			uu_free(ret);
2966 			return (NULL);
2967 		}
2968 	} else {
2969 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2970 	}
2971 
2972 	return (ret);
2973 }
2974 
2975 scf_handle_t *
scf_snapshot_handle(const scf_snapshot_t * val)2976 scf_snapshot_handle(const scf_snapshot_t *val)
2977 {
2978 	return (datael_handle(&val->rd_d));
2979 }
2980 
2981 void
scf_snapshot_destroy(scf_snapshot_t * val)2982 scf_snapshot_destroy(scf_snapshot_t *val)
2983 {
2984 	if (val == NULL)
2985 		return;
2986 
2987 	datael_destroy(&val->rd_d);
2988 	uu_free(val);
2989 }
2990 
2991 ssize_t
scf_snapshot_get_name(const scf_snapshot_t * rep,char * out,size_t len)2992 scf_snapshot_get_name(const scf_snapshot_t *rep, char *out, size_t len)
2993 {
2994 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
2995 }
2996 
2997 /*
2998  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
2999  * (bad server response or id in use), _NO_RESOURCES, _NO_MEMORY.
3000  */
3001 scf_snaplevel_t *
scf_snaplevel_create(scf_handle_t * handle)3002 scf_snaplevel_create(scf_handle_t *handle)
3003 {
3004 	scf_snaplevel_t *ret;
3005 
3006 	ret = uu_zalloc(sizeof (*ret));
3007 	if (ret != NULL) {
3008 		if (datael_init(&ret->rd_d, handle,
3009 		    REP_PROTOCOL_ENTITY_SNAPLEVEL) == -1) {
3010 			uu_free(ret);
3011 			return (NULL);
3012 		}
3013 	} else {
3014 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
3015 	}
3016 
3017 	return (ret);
3018 }
3019 
3020 scf_handle_t *
scf_snaplevel_handle(const scf_snaplevel_t * val)3021 scf_snaplevel_handle(const scf_snaplevel_t *val)
3022 {
3023 	return (datael_handle(&val->rd_d));
3024 }
3025 
3026 void
scf_snaplevel_destroy(scf_snaplevel_t * val)3027 scf_snaplevel_destroy(scf_snaplevel_t *val)
3028 {
3029 	if (val == NULL)
3030 		return;
3031 
3032 	datael_destroy(&val->rd_d);
3033 	uu_free(val);
3034 }
3035 
3036 ssize_t
scf_snaplevel_get_scope_name(const scf_snaplevel_t * rep,char * out,size_t len)3037 scf_snaplevel_get_scope_name(const scf_snaplevel_t *rep, char *out, size_t len)
3038 {
3039 	return (datael_get_name(&rep->rd_d, out, len,
3040 	    RP_ENTITY_NAME_SNAPLEVEL_SCOPE));
3041 }
3042 
3043 ssize_t
scf_snaplevel_get_service_name(const scf_snaplevel_t * rep,char * out,size_t len)3044 scf_snaplevel_get_service_name(const scf_snaplevel_t *rep, char *out,
3045     size_t len)
3046 {
3047 	return (datael_get_name(&rep->rd_d, out, len,
3048 	    RP_ENTITY_NAME_SNAPLEVEL_SERVICE));
3049 }
3050 
3051 ssize_t
scf_snaplevel_get_instance_name(const scf_snaplevel_t * rep,char * out,size_t len)3052 scf_snaplevel_get_instance_name(const scf_snaplevel_t *rep, char *out,
3053     size_t len)
3054 {
3055 	return (datael_get_name(&rep->rd_d, out, len,
3056 	    RP_ENTITY_NAME_SNAPLEVEL_INSTANCE));
3057 }
3058 
3059 /*
3060  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
3061  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
3062  * _BACKEND_ACCESS, _NOT_FOUND.
3063  */
3064 int
scf_snaplevel_get_pg(const scf_snaplevel_t * snap,const char * name,scf_propertygroup_t * pg)3065 scf_snaplevel_get_pg(const scf_snaplevel_t *snap, const char *name,
3066     scf_propertygroup_t *pg)
3067 {
3068 	return (datael_get_child(&snap->rd_d, name,
3069 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0));
3070 }
3071 
3072 static int
snaplevel_next(const scf_datael_t * src,scf_snaplevel_t * dst_arg)3073 snaplevel_next(const scf_datael_t *src, scf_snaplevel_t *dst_arg)
3074 {
3075 	scf_handle_t *h = src->rd_handle;
3076 	scf_snaplevel_t *dst = dst_arg;
3077 	struct rep_protocol_entity_pair request;
3078 	struct rep_protocol_response response;
3079 	int r;
3080 	int dups = 0;
3081 
3082 	if (h != dst->rd_d.rd_handle)
3083 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
3084 
3085 	if (src == &dst->rd_d) {
3086 		dups = 1;
3087 		dst = HANDLE_HOLD_SNAPLVL(h);
3088 	}
3089 	(void) pthread_mutex_lock(&h->rh_lock);
3090 	request.rpr_request = REP_PROTOCOL_NEXT_SNAPLEVEL;
3091 	request.rpr_entity_src = src->rd_entity;
3092 	request.rpr_entity_dst = dst->rd_d.rd_entity;
3093 
3094 	datael_finish_reset(src);
3095 	datael_finish_reset(&dst->rd_d);
3096 	r = make_door_call(h, &request, sizeof (request),
3097 	    &response, sizeof (response));
3098 	/*
3099 	 * if we succeeded, we need to swap dst and dst_arg's identity.  We
3100 	 * take advantage of the fact that the only in-library knowledge is
3101 	 * their entity ids.
3102 	 */
3103 	if (dups && r >= 0 &&
3104 	    (response.rpr_response == REP_PROTOCOL_SUCCESS ||
3105 	    response.rpr_response == REP_PROTOCOL_DONE)) {
3106 		int entity = dst->rd_d.rd_entity;
3107 
3108 		dst->rd_d.rd_entity = dst_arg->rd_d.rd_entity;
3109 		dst_arg->rd_d.rd_entity = entity;
3110 	}
3111 	(void) pthread_mutex_unlock(&h->rh_lock);
3112 
3113 	if (dups)
3114 		HANDLE_RELE_SNAPLVL(h);
3115 
3116 	if (r < 0)
3117 		DOOR_ERRORS_BLOCK(r);
3118 
3119 	if (response.rpr_response != REP_PROTOCOL_SUCCESS &&
3120 	    response.rpr_response != REP_PROTOCOL_DONE) {
3121 		return (scf_set_error(proto_error(response.rpr_response)));
3122 	}
3123 
3124 	return (response.rpr_response == REP_PROTOCOL_SUCCESS) ?
3125 	    SCF_SUCCESS : SCF_COMPLETE;
3126 }
3127 
scf_snapshot_get_base_snaplevel(const scf_snapshot_t * base,scf_snaplevel_t * out)3128 int scf_snapshot_get_base_snaplevel(const scf_snapshot_t *base,
3129     scf_snaplevel_t *out)
3130 {
3131 	return (snaplevel_next(&base->rd_d, out));
3132 }
3133 
scf_snaplevel_get_next_snaplevel(const scf_snaplevel_t * base,scf_snaplevel_t * out)3134 int scf_snaplevel_get_next_snaplevel(const scf_snaplevel_t *base,
3135     scf_snaplevel_t *out)
3136 {
3137 	return (snaplevel_next(&base->rd_d, out));
3138 }
3139 
3140 /*
3141  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
3142  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
3143  */
3144 scf_propertygroup_t *
scf_pg_create(scf_handle_t * handle)3145 scf_pg_create(scf_handle_t *handle)
3146 {
3147 	scf_propertygroup_t *ret;
3148 	ret = uu_zalloc(sizeof (*ret));
3149 	if (ret != NULL) {
3150 		if (datael_init(&ret->rd_d, handle,
3151 		    REP_PROTOCOL_ENTITY_PROPERTYGRP) == -1) {
3152 			uu_free(ret);
3153 			return (NULL);
3154 		}
3155 	} else {
3156 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
3157 	}
3158 
3159 	return (ret);
3160 }
3161 
3162 scf_handle_t *
scf_pg_handle(const scf_propertygroup_t * val)3163 scf_pg_handle(const scf_propertygroup_t *val)
3164 {
3165 	return (datael_handle(&val->rd_d));
3166 }
3167 
3168 void
scf_pg_destroy(scf_propertygroup_t * val)3169 scf_pg_destroy(scf_propertygroup_t *val)
3170 {
3171 	if (val == NULL)
3172 		return;
3173 
3174 	datael_destroy(&val->rd_d);
3175 	uu_free(val);
3176 }
3177 
3178 ssize_t
scf_pg_get_name(const scf_propertygroup_t * pg,char * out,size_t len)3179 scf_pg_get_name(const scf_propertygroup_t *pg,  char *out, size_t len)
3180 {
3181 	return (datael_get_name(&pg->rd_d, out, len, RP_ENTITY_NAME_NAME));
3182 }
3183 
3184 ssize_t
scf_pg_get_type(const scf_propertygroup_t * pg,char * out,size_t len)3185 scf_pg_get_type(const scf_propertygroup_t *pg,  char *out, size_t len)
3186 {
3187 	return (datael_get_name(&pg->rd_d, out, len, RP_ENTITY_NAME_PGTYPE));
3188 }
3189 
3190 int
scf_pg_get_flags(const scf_propertygroup_t * pg,uint32_t * out)3191 scf_pg_get_flags(const scf_propertygroup_t *pg, uint32_t *out)
3192 {
3193 	char buf[REP_PROTOCOL_NAME_LEN];
3194 	ssize_t res;
3195 
3196 	res = datael_get_name(&pg->rd_d, buf, sizeof (buf),
3197 	    RP_ENTITY_NAME_PGFLAGS);
3198 
3199 	if (res == -1)
3200 		return (-1);
3201 
3202 	if (uu_strtouint(buf, out, sizeof (*out), 0, 0, UINT32_MAX) == -1)
3203 		return (scf_set_error(SCF_ERROR_INTERNAL));
3204 
3205 	return (0);
3206 }
3207 
3208 static int
datael_update(scf_datael_t * dp)3209 datael_update(scf_datael_t *dp)
3210 {
3211 	scf_handle_t *h = dp->rd_handle;
3212 
3213 	struct rep_protocol_entity_update request;
3214 	struct rep_protocol_response response;
3215 
3216 	int