xref: /illumos-gate/usr/src/lib/libscf/common/midlevel.c (revision a92282e44f968185a6bba094d1e5fece2da819cf)
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 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright 2018 RackTop Systems.
26  * Copyright 2020 Joyent, Inc.
27  */
28 
29 #include "libscf_impl.h"
30 
31 #include <assert.h>
32 #include <libuutil.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <sys/param.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <assert.h>
40 #include "midlevel_impl.h"
41 #include "lowlevel_impl.h"
42 
43 #ifndef NDEBUG
44 #define	bad_error(func, err)	{					\
45 	uu_warn("%s:%d: %s failed with unexpected error %d.  Aborting.\n", \
46 	    __FILE__, __LINE__, func, err);				\
47 	abort();							\
48 }
49 #else
50 #define	bad_error(func, err)	abort()
51 #endif
52 
53 /* Path to speedy files area must end with a slash */
54 #define	SMF_SPEEDY_FILES_PATH		"/etc/svc/volatile/"
55 
56 void
57 scf_simple_handle_destroy(scf_simple_handle_t *simple_h)
58 {
59 	if (simple_h == NULL)
60 		return;
61 
62 	scf_pg_destroy(simple_h->running_pg);
63 	scf_pg_destroy(simple_h->editing_pg);
64 	scf_snapshot_destroy(simple_h->snap);
65 	scf_instance_destroy(simple_h->inst);
66 	scf_handle_destroy(simple_h->h);
67 	uu_free(simple_h);
68 }
69 
70 /*
71  * Given a base service FMRI and the names of a property group and property,
72  * assemble_fmri() merges them into a property FMRI.  Note that if the base
73  * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
74  */
75 
76 static char *
77 assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
78     const char *prop)
79 {
80 	size_t	fmri_sz, pglen;
81 	ssize_t baselen;
82 	char	*fmri_buf;
83 
84 	if (prop == NULL) {
85 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
86 		return (NULL);
87 	}
88 
89 	if (pg == NULL)
90 		pglen = strlen(SCF_PG_APP_DEFAULT);
91 	else
92 		pglen = strlen(pg);
93 
94 	if (base == NULL) {
95 		if ((baselen = scf_myname(h, NULL, 0)) == -1)
96 			return (NULL);
97 	} else {
98 		baselen = strlen(base);
99 	}
100 
101 	fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
102 	    pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
103 	    strlen(prop) + 1;
104 
105 	if ((fmri_buf = malloc(fmri_sz)) == NULL) {
106 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
107 		return (NULL);
108 	}
109 
110 	if (base == NULL) {
111 		if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
112 			free(fmri_buf);
113 			return (NULL);
114 		}
115 	} else {
116 		(void) strcpy(fmri_buf, base);
117 	}
118 
119 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
120 
121 	if (pg == NULL)
122 		(void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
123 	else
124 		(void) strcat(fmri_buf, pg);
125 
126 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
127 	(void) strcat(fmri_buf, prop);
128 	return (fmri_buf);
129 }
130 
131 /*
132  * Given a property, this function allocates and fills an scf_simple_prop_t
133  * with the data it contains.
134  */
135 
136 static scf_simple_prop_t *
137 fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
138     scf_handle_t *h)
139 {
140 	scf_simple_prop_t		*ret;
141 	scf_iter_t			*iter;
142 	scf_value_t			*val;
143 	int				iterret, i;
144 	ssize_t				valsize, numvals;
145 	union scf_simple_prop_val	*vallist = NULL, *vallist_backup = NULL;
146 
147 	if ((ret = malloc(sizeof (*ret))) == NULL) {
148 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
149 		return (NULL);
150 	}
151 
152 	ret->pr_next = NULL;
153 	ret->pr_pg = NULL;
154 	ret->pr_iter = 0;
155 
156 	if (pgname == NULL)
157 		ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
158 	else
159 		ret->pr_pgname = strdup(pgname);
160 
161 	if (ret->pr_pgname == NULL) {
162 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
163 		free(ret);
164 		return (NULL);
165 	}
166 
167 	if ((ret->pr_propname = strdup(propname)) == NULL) {
168 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
169 		free(ret->pr_pgname);
170 		free(ret);
171 		return (NULL);
172 	}
173 
174 	if (scf_property_type(prop, &ret->pr_type) == -1)
175 		goto error3;
176 
177 	if ((iter = scf_iter_create(h)) == NULL)
178 		goto error3;
179 	if ((val = scf_value_create(h)) == NULL) {
180 		scf_iter_destroy(iter);
181 		goto error3;
182 	}
183 
184 	if (scf_iter_property_values(iter, prop) == -1)
185 		goto error1;
186 
187 	for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
188 	    numvals++) {
189 		vallist_backup = vallist;
190 		if ((vallist = realloc(vallist, (numvals + 1) *
191 		    sizeof (*vallist))) == NULL) {
192 			vallist = vallist_backup;
193 			goto error1;
194 		}
195 
196 		switch (ret->pr_type) {
197 		case SCF_TYPE_BOOLEAN:
198 			if (scf_value_get_boolean(val,
199 			    &vallist[numvals].pv_bool) == -1)
200 				goto error1;
201 			break;
202 
203 		case SCF_TYPE_COUNT:
204 			if (scf_value_get_count(val,
205 			    &vallist[numvals].pv_uint) == -1)
206 				goto error1;
207 			break;
208 
209 		case SCF_TYPE_INTEGER:
210 			if (scf_value_get_integer(val,
211 			    &vallist[numvals].pv_int) == -1)
212 				goto error1;
213 			break;
214 
215 		case SCF_TYPE_TIME:
216 			if (scf_value_get_time(val,
217 			    &vallist[numvals].pv_time.t_sec,
218 			    &vallist[numvals].pv_time.t_nsec) == -1)
219 				goto error1;
220 			break;
221 
222 		case SCF_TYPE_ASTRING:
223 			vallist[numvals].pv_str = NULL;
224 			if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
225 			    -1)
226 				goto error1;
227 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
228 			    NULL) {
229 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
230 				goto error1;
231 			}
232 			if (scf_value_get_astring(val,
233 			    vallist[numvals].pv_str, valsize+1) == -1) {
234 				free(vallist[numvals].pv_str);
235 				goto error1;
236 			}
237 			break;
238 
239 		case SCF_TYPE_USTRING:
240 		case SCF_TYPE_HOST:
241 		case SCF_TYPE_HOSTNAME:
242 		case SCF_TYPE_NET_ADDR:
243 		case SCF_TYPE_NET_ADDR_V4:
244 		case SCF_TYPE_NET_ADDR_V6:
245 		case SCF_TYPE_URI:
246 		case SCF_TYPE_FMRI:
247 			vallist[numvals].pv_str = NULL;
248 			if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
249 			    -1)
250 				goto error1;
251 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
252 			    NULL) {
253 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
254 				goto error1;
255 			}
256 			if (scf_value_get_ustring(val,
257 			    vallist[numvals].pv_str, valsize+1) == -1) {
258 				free(vallist[numvals].pv_str);
259 				goto error1;
260 			}
261 			break;
262 
263 		case SCF_TYPE_OPAQUE:
264 			vallist[numvals].pv_opaque.o_value = NULL;
265 			if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
266 			    -1)
267 				goto error1;
268 			if ((vallist[numvals].pv_opaque.o_value =
269 			    malloc(valsize)) == NULL) {
270 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
271 				goto error1;
272 			}
273 			vallist[numvals].pv_opaque.o_size = valsize;
274 			if (scf_value_get_opaque(val,
275 			    vallist[numvals].pv_opaque.o_value,
276 			    valsize) == -1) {
277 				free(vallist[numvals].pv_opaque.o_value);
278 				goto error1;
279 			}
280 			break;
281 
282 		default:
283 			(void) scf_set_error(SCF_ERROR_INTERNAL);
284 			goto error1;
285 
286 		}
287 	}
288 
289 	if (iterret == -1) {
290 		int err = scf_error();
291 		if (err != SCF_ERROR_CONNECTION_BROKEN &&
292 		    err != SCF_ERROR_PERMISSION_DENIED)
293 			(void) scf_set_error(SCF_ERROR_INTERNAL);
294 		goto error1;
295 	}
296 
297 	ret->pr_vallist = vallist;
298 	ret->pr_numvalues = numvals;
299 
300 	scf_iter_destroy(iter);
301 	(void) scf_value_destroy(val);
302 
303 	return (ret);
304 
305 	/*
306 	 * Exit point for a successful call.  Below this line are exit points
307 	 * for failures at various stages during the function.
308 	 */
309 
310 error1:
311 	if (vallist == NULL)
312 		goto error2;
313 
314 	switch (ret->pr_type) {
315 	case SCF_TYPE_ASTRING:
316 	case SCF_TYPE_USTRING:
317 	case SCF_TYPE_HOST:
318 	case SCF_TYPE_HOSTNAME:
319 	case SCF_TYPE_NET_ADDR:
320 	case SCF_TYPE_NET_ADDR_V4:
321 	case SCF_TYPE_NET_ADDR_V6:
322 	case SCF_TYPE_URI:
323 	case SCF_TYPE_FMRI: {
324 		for (i = 0; i < numvals; i++) {
325 			free(vallist[i].pv_str);
326 		}
327 		break;
328 	}
329 	case SCF_TYPE_OPAQUE: {
330 		for (i = 0; i < numvals; i++) {
331 			free(vallist[i].pv_opaque.o_value);
332 		}
333 		break;
334 	}
335 	default:
336 		break;
337 	}
338 
339 	free(vallist);
340 
341 error2:
342 	scf_iter_destroy(iter);
343 	(void) scf_value_destroy(val);
344 
345 error3:
346 	free(ret->pr_pgname);
347 	free(ret->pr_propname);
348 	free(ret);
349 	return (NULL);
350 }
351 
352 /*
353  * insert_app_props iterates over a property iterator, getting all the
354  * properties from a property group, and adding or overwriting them into
355  * a simple_app_props_t.  This is used by scf_simple_app_props_get to provide
356  * service/instance composition while filling the app_props_t.
357  * insert_app_props iterates over a single property group.
358  */
359 
360 static int
361 insert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
362     scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
363     scf_handle_t *h)
364 {
365 	scf_simple_prop_t	*thisprop, *prevprop, *newprop;
366 	uint8_t			found;
367 	int			propiter_ret;
368 
369 	while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
370 
371 		if (scf_property_get_name(prop, propname, namelen) < 0) {
372 			if (scf_error() == SCF_ERROR_NOT_SET)
373 				(void) scf_set_error(SCF_ERROR_INTERNAL);
374 			return (-1);
375 		}
376 
377 		thisprop = thispg->pg_proplist;
378 		prevprop = thispg->pg_proplist;
379 		found = 0;
380 
381 		while ((thisprop != NULL) && (!found)) {
382 			if (strcmp(thisprop->pr_propname, propname) == 0) {
383 				found = 1;
384 				if ((newprop = fill_prop(prop, pgname,
385 				    propname, h)) == NULL)
386 					return (-1);
387 
388 				if (thisprop == thispg->pg_proplist)
389 					thispg->pg_proplist = newprop;
390 				else
391 					prevprop->pr_next = newprop;
392 
393 				newprop->pr_pg = thispg;
394 				newprop->pr_next = thisprop->pr_next;
395 				scf_simple_prop_free(thisprop);
396 				thisprop = NULL;
397 			} else {
398 				if (thisprop != thispg->pg_proplist)
399 					prevprop = prevprop->pr_next;
400 				thisprop = thisprop->pr_next;
401 			}
402 		}
403 
404 		if (!found) {
405 			if ((newprop = fill_prop(prop, pgname, propname, h)) ==
406 			    NULL)
407 				return (-1);
408 
409 			if (thispg->pg_proplist == NULL)
410 				thispg->pg_proplist = newprop;
411 			else
412 				prevprop->pr_next = newprop;
413 
414 			newprop->pr_pg = thispg;
415 		}
416 	}
417 
418 	if (propiter_ret == -1) {
419 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
420 			(void) scf_set_error(SCF_ERROR_INTERNAL);
421 		return (-1);
422 	}
423 
424 	return (0);
425 }
426 
427 
428 /*
429  * Sets up e in tx to set pname's values.  Returns 0 on success or -1 on
430  * failure, with scf_error() set to
431  *   SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
432  *   SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
433  *   SCF_ERROR_NOT_BOUND - handle is not bound
434  *   SCF_ERROR_CONNECTION_BROKEN - connection was broken
435  *   SCF_ERROR_NOT_SET - tx has not been started
436  *   SCF_ERROR_DELETED - the pg tx was started on was deleted
437  */
438 static int
439 transaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
440     const char *pname, scf_type_t ty)
441 {
442 	for (;;) {
443 		if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
444 			return (0);
445 
446 		switch (scf_error()) {
447 		case SCF_ERROR_HANDLE_MISMATCH:
448 		case SCF_ERROR_INVALID_ARGUMENT:
449 		case SCF_ERROR_NOT_BOUND:
450 		case SCF_ERROR_CONNECTION_BROKEN:
451 		case SCF_ERROR_NOT_SET:
452 		case SCF_ERROR_DELETED:
453 		default:
454 			return (-1);
455 
456 		case SCF_ERROR_NOT_FOUND:
457 			break;
458 		}
459 
460 		if (scf_transaction_property_new(tx, e, pname, ty) == 0)
461 			return (0);
462 
463 		switch (scf_error()) {
464 		case SCF_ERROR_HANDLE_MISMATCH:
465 		case SCF_ERROR_INVALID_ARGUMENT:
466 		case SCF_ERROR_NOT_BOUND:
467 		case SCF_ERROR_CONNECTION_BROKEN:
468 		case SCF_ERROR_NOT_SET:
469 		case SCF_ERROR_DELETED:
470 		default:
471 			return (-1);
472 
473 		case SCF_ERROR_EXISTS:
474 			break;
475 		}
476 	}
477 }
478 
479 static int
480 get_inst_enabled(const scf_instance_t *inst, const char *pgname)
481 {
482 	scf_propertygroup_t	*gpg = NULL;
483 	scf_property_t		*eprop = NULL;
484 	scf_value_t		*v = NULL;
485 	scf_handle_t		*h = NULL;
486 	uint8_t			enabled;
487 	int			ret = -1;
488 
489 	if ((h = scf_instance_handle(inst)) == NULL)
490 		return (-1);
491 
492 	if ((gpg = scf_pg_create(h)) == NULL ||
493 	    (eprop = scf_property_create(h)) == NULL ||
494 	    (v = scf_value_create(h)) == NULL)
495 		goto out;
496 
497 	if (scf_instance_get_pg(inst, pgname, gpg) ||
498 	    scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
499 	    scf_property_get_value(eprop, v) ||
500 	    scf_value_get_boolean(v, &enabled))
501 		goto out;
502 	ret = enabled;
503 
504 out:
505 	scf_pg_destroy(gpg);
506 	scf_property_destroy(eprop);
507 	scf_value_destroy(v);
508 	return (ret);
509 }
510 
511 /*
512  * set_inst_enabled() is a "master" enable/disable call that takes the
513  * instance and the desired state for the enabled bit in the instance's
514  * named property group.  If the group doesn't exist, it's created with the
515  * given flags.  Called by smf_{dis,en}able_instance().
516  *
517  * Note that if we're enabling, comment will be "", and we use that to clear out
518  * any old disabled comment.
519  */
520 static int
521 set_inst_enabled(const scf_instance_t *inst, uint8_t desired,
522     const char *pgname, uint32_t pgflags, const char *comment)
523 {
524 	scf_transaction_t	*tx = NULL;
525 	scf_transaction_entry_t *ent1 = NULL;
526 	scf_transaction_entry_t *ent2 = NULL;
527 	scf_propertygroup_t	*gpg = NULL;
528 	scf_property_t		*eprop = NULL;
529 	scf_value_t		*v1 = NULL;
530 	scf_value_t		*v2 = NULL;
531 	scf_handle_t		*h = NULL;
532 	int			ret = -1;
533 	int			committed;
534 	uint8_t			b;
535 
536 	if ((h = scf_instance_handle(inst)) == NULL)
537 		return (-1);
538 
539 	if ((gpg = scf_pg_create(h)) == NULL ||
540 	    (eprop = scf_property_create(h)) == NULL ||
541 	    (v1 = scf_value_create(h)) == NULL ||
542 	    (v2 = scf_value_create(h)) == NULL ||
543 	    (tx = scf_transaction_create(h)) == NULL ||
544 	    (ent1 = scf_entry_create(h)) == NULL ||
545 	    (ent2 = scf_entry_create(h)) == NULL)
546 		goto out;
547 
548 general_pg_get:
549 	if (scf_instance_get_pg(inst, SCF_PG_GENERAL, gpg) == -1) {
550 		if (scf_error() != SCF_ERROR_NOT_FOUND)
551 			goto out;
552 
553 		if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
554 		    SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, gpg) == -1) {
555 			if (scf_error() != SCF_ERROR_EXISTS)
556 				goto out;
557 			goto general_pg_get;
558 		}
559 	}
560 
561 	if (strcmp(pgname, SCF_PG_GENERAL) != 0) {
562 get:
563 		if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
564 			if (scf_error() != SCF_ERROR_NOT_FOUND)
565 				goto out;
566 
567 			if (scf_instance_add_pg(inst, pgname,
568 			    SCF_GROUP_FRAMEWORK, pgflags, gpg) == -1) {
569 				if (scf_error() != SCF_ERROR_EXISTS)
570 					goto out;
571 				goto get;
572 			}
573 		}
574 	}
575 
576 	if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
577 		if (scf_error() != SCF_ERROR_NOT_FOUND)
578 			goto out;
579 		else
580 			goto set;
581 	}
582 
583 	/*
584 	 * If it's already set the way we want, forgo the transaction.
585 	 */
586 	if (scf_property_get_value(eprop, v1) == -1) {
587 		switch (scf_error()) {
588 		case SCF_ERROR_CONSTRAINT_VIOLATED:
589 		case SCF_ERROR_NOT_FOUND:
590 			/* Misconfigured, so set anyway. */
591 			goto set;
592 
593 		default:
594 			goto out;
595 		}
596 	}
597 	if (scf_value_get_boolean(v1, &b) == -1) {
598 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
599 			goto out;
600 		goto set;
601 	}
602 	if (b == desired) {
603 		ret = 0;
604 		goto out;
605 	}
606 
607 set:
608 	do {
609 		if (scf_transaction_start(tx, gpg) == -1)
610 			goto out;
611 
612 		if (transaction_property_set(tx, ent1, SCF_PROPERTY_ENABLED,
613 		    SCF_TYPE_BOOLEAN) != 0) {
614 			switch (scf_error()) {
615 			case SCF_ERROR_CONNECTION_BROKEN:
616 			case SCF_ERROR_DELETED:
617 			default:
618 				goto out;
619 
620 			case SCF_ERROR_HANDLE_MISMATCH:
621 			case SCF_ERROR_INVALID_ARGUMENT:
622 			case SCF_ERROR_NOT_BOUND:
623 			case SCF_ERROR_NOT_SET:
624 				bad_error("transaction_property_set",
625 				    scf_error());
626 			}
627 		}
628 
629 		scf_value_set_boolean(v1, desired);
630 		if (scf_entry_add_value(ent1, v1) == -1)
631 			goto out;
632 
633 		if (transaction_property_set(tx, ent2,
634 		    SCF_PROPERTY_COMMENT, SCF_TYPE_ASTRING) != 0) {
635 			switch (scf_error()) {
636 			case SCF_ERROR_CONNECTION_BROKEN:
637 			case SCF_ERROR_DELETED:
638 			default:
639 				goto out;
640 
641 			case SCF_ERROR_HANDLE_MISMATCH:
642 			case SCF_ERROR_INVALID_ARGUMENT:
643 			case SCF_ERROR_NOT_BOUND:
644 			case SCF_ERROR_NOT_SET:
645 				bad_error("transaction_property_set",
646 				    scf_error());
647 			}
648 		}
649 
650 		if (scf_value_set_astring(v2, comment) == -1)
651 			goto out;
652 
653 		if (scf_entry_add_value(ent2, v2) == -1)
654 			goto out;
655 
656 		committed = scf_transaction_commit(tx);
657 		if (committed == -1)
658 			goto out;
659 
660 		scf_transaction_reset(tx);
661 
662 		if (committed == 0) { /* out-of-sync */
663 			if (scf_pg_update(gpg) == -1)
664 				goto out;
665 		}
666 	} while (committed == 0);
667 
668 	ret = 0;
669 
670 out:
671 	scf_value_destroy(v1);
672 	scf_value_destroy(v2);
673 	scf_entry_destroy(ent1);
674 	scf_entry_destroy(ent2);
675 	scf_transaction_destroy(tx);
676 	scf_property_destroy(eprop);
677 	scf_pg_destroy(gpg);
678 
679 	return (ret);
680 }
681 
682 static int
683 delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
684 {
685 	scf_transaction_t	*tx = NULL;
686 	scf_transaction_entry_t *ent1 = NULL;
687 	scf_transaction_entry_t *ent2 = NULL;
688 	scf_propertygroup_t	*gpg = NULL;
689 	scf_handle_t		*h = NULL;
690 	int			ret = -1;
691 	int			committed;
692 
693 	if ((h = scf_instance_handle(inst)) == NULL)
694 		return (-1);
695 
696 	if ((gpg = scf_pg_create(h)) == NULL ||
697 	    (tx = scf_transaction_create(h)) == NULL ||
698 	    (ent1 = scf_entry_create(h)) == NULL ||
699 	    (ent2 = scf_entry_create(h)) == NULL)
700 		goto out;
701 
702 	if (scf_instance_get_pg(inst, pgname, gpg) != 0)
703 		goto error;
704 	do {
705 		if (scf_transaction_start(tx, gpg) == -1)
706 			goto error;
707 
708 		ret = scf_transaction_property_delete(tx, ent1,
709 		    SCF_PROPERTY_ENABLED);
710 
711 		if (ret == -1 && scf_error() != SCF_ERROR_DELETED &&
712 		    scf_error() != SCF_ERROR_NOT_FOUND)
713 			goto error;
714 
715 		ret = scf_transaction_property_delete(tx, ent2,
716 		    SCF_PROPERTY_COMMENT);
717 
718 		if (ret == -1 && scf_error() != SCF_ERROR_DELETED &&
719 		    scf_error() != SCF_ERROR_NOT_FOUND)
720 			goto error;
721 
722 		if ((committed = scf_transaction_commit(tx)) == -1)
723 			goto error;
724 
725 		scf_transaction_reset(tx);
726 
727 		if (committed == 0 && scf_pg_update(gpg) == -1)
728 			goto error;
729 	} while (committed == 0);
730 
731 	ret = 0;
732 	goto out;
733 
734 error:
735 	switch (scf_error()) {
736 	case SCF_ERROR_DELETED:
737 	case SCF_ERROR_NOT_FOUND:
738 		/* success */
739 		ret = 0;
740 	}
741 
742 out:
743 	scf_entry_destroy(ent1);
744 	scf_entry_destroy(ent2);
745 	scf_transaction_destroy(tx);
746 	scf_pg_destroy(gpg);
747 
748 	return (ret);
749 }
750 
751 /*
752  * Returns 0 on success or -1 on failure.  On failure leaves scf_error() set to
753  *   SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
754  *   SCF_ERROR_NOT_BOUND - inst's handle is not bound
755  *   SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
756  *   SCF_ERROR_NOT_SET - inst is not set
757  *   SCF_ERROR_DELETED - inst was deleted
758  *   SCF_ERROR_PERMISSION_DENIED
759  *   SCF_ERROR_BACKEND_ACCESS
760  *   SCF_ERROR_BACKEND_READONLY
761  */
762 static int
763 set_inst_action_inst(scf_instance_t *inst, const char *action)
764 {
765 	scf_handle_t			*h;
766 	scf_transaction_t		*tx = NULL;
767 	scf_transaction_entry_t		*ent = NULL;
768 	scf_propertygroup_t		*pg = NULL;
769 	scf_property_t			*prop = NULL;
770 	scf_value_t			*v = NULL;
771 	int				trans, ret = -1;
772 	int64_t				t;
773 	hrtime_t			timestamp;
774 
775 	if ((h = scf_instance_handle(inst)) == NULL ||
776 	    (pg = scf_pg_create(h)) == NULL ||
777 	    (prop = scf_property_create(h)) == NULL ||
778 	    (v = scf_value_create(h)) == NULL ||
779 	    (tx = scf_transaction_create(h)) == NULL ||
780 	    (ent = scf_entry_create(h)) == NULL)
781 		goto out;
782 
783 get:
784 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
785 		switch (scf_error()) {
786 		case SCF_ERROR_NOT_BOUND:
787 		case SCF_ERROR_CONNECTION_BROKEN:
788 		case SCF_ERROR_NOT_SET:
789 		case SCF_ERROR_DELETED:
790 		default:
791 			goto out;
792 
793 		case SCF_ERROR_NOT_FOUND:
794 			break;
795 
796 		case SCF_ERROR_HANDLE_MISMATCH:
797 		case SCF_ERROR_INVALID_ARGUMENT:
798 			bad_error("scf_instance_get_pg", scf_error());
799 		}
800 
801 		/* Try creating the restarter_actions property group. */
802 add:
803 		if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
804 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
805 		    SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
806 			switch (scf_error()) {
807 			case SCF_ERROR_NOT_BOUND:
808 			case SCF_ERROR_CONNECTION_BROKEN:
809 			case SCF_ERROR_NOT_SET:
810 			case SCF_ERROR_DELETED:
811 			case SCF_ERROR_PERMISSION_DENIED:
812 			case SCF_ERROR_BACKEND_ACCESS:
813 			case SCF_ERROR_BACKEND_READONLY:
814 			default:
815 				goto out;
816 
817 			case SCF_ERROR_EXISTS:
818 				goto get;
819 
820 			case SCF_ERROR_HANDLE_MISMATCH:
821 			case SCF_ERROR_INVALID_ARGUMENT:
822 				bad_error("scf_instance_add_pg", scf_error());
823 			}
824 		}
825 	}
826 
827 	for (;;) {
828 		timestamp = gethrtime();
829 
830 		if (scf_pg_get_property(pg, action, prop) != 0) {
831 			switch (scf_error()) {
832 			case SCF_ERROR_CONNECTION_BROKEN:
833 			default:
834 				goto out;
835 
836 			case SCF_ERROR_DELETED:
837 				goto add;
838 
839 			case SCF_ERROR_NOT_FOUND:
840 				break;
841 
842 			case SCF_ERROR_HANDLE_MISMATCH:
843 			case SCF_ERROR_INVALID_ARGUMENT:
844 			case SCF_ERROR_NOT_BOUND:
845 			case SCF_ERROR_NOT_SET:
846 				bad_error("scf_pg_get_property", scf_error());
847 			}
848 		} else if (scf_property_get_value(prop, v) != 0) {
849 			switch (scf_error()) {
850 			case SCF_ERROR_CONNECTION_BROKEN:
851 			default:
852 				goto out;
853 
854 			case SCF_ERROR_DELETED:
855 				goto add;
856 
857 			case SCF_ERROR_CONSTRAINT_VIOLATED:
858 			case SCF_ERROR_NOT_FOUND:
859 				break;
860 
861 			case SCF_ERROR_HANDLE_MISMATCH:
862 			case SCF_ERROR_NOT_BOUND:
863 			case SCF_ERROR_NOT_SET:
864 				bad_error("scf_property_get_value",
865 				    scf_error());
866 			}
867 		} else if (scf_value_get_integer(v, &t) != 0) {
868 			bad_error("scf_value_get_integer", scf_error());
869 		} else if (t > timestamp) {
870 			break;
871 		}
872 
873 		if (scf_transaction_start(tx, pg) == -1) {
874 			switch (scf_error()) {
875 			case SCF_ERROR_NOT_BOUND:
876 			case SCF_ERROR_CONNECTION_BROKEN:
877 			case SCF_ERROR_PERMISSION_DENIED:
878 			case SCF_ERROR_BACKEND_ACCESS:
879 			case SCF_ERROR_BACKEND_READONLY:
880 			default:
881 				goto out;
882 
883 			case SCF_ERROR_DELETED:
884 				goto add;
885 
886 			case SCF_ERROR_HANDLE_MISMATCH:
887 			case SCF_ERROR_NOT_SET:
888 			case SCF_ERROR_IN_USE:
889 				bad_error("scf_transaction_start", scf_error());
890 			}
891 		}
892 
893 		if (transaction_property_set(tx, ent, action,
894 		    SCF_TYPE_INTEGER) != 0) {
895 			switch (scf_error()) {
896 			case SCF_ERROR_NOT_BOUND:
897 			case SCF_ERROR_CONNECTION_BROKEN:
898 			case SCF_ERROR_DELETED:
899 			default:
900 				goto out;
901 
902 			case SCF_ERROR_HANDLE_MISMATCH:
903 			case SCF_ERROR_INVALID_ARGUMENT:
904 			case SCF_ERROR_NOT_SET:
905 				bad_error("transaction_property_set",
906 				    scf_error());
907 			}
908 		}
909 
910 		scf_value_set_integer(v, timestamp);
911 		if (scf_entry_add_value(ent, v) == -1)
912 			bad_error("scf_entry_add_value", scf_error());
913 
914 		trans = scf_transaction_commit(tx);
915 		if (trans == 1)
916 			break;
917 
918 		if (trans != 0) {
919 			switch (scf_error()) {
920 			case SCF_ERROR_CONNECTION_BROKEN:
921 			case SCF_ERROR_PERMISSION_DENIED:
922 			case SCF_ERROR_BACKEND_ACCESS:
923 			case SCF_ERROR_BACKEND_READONLY:
924 			default:
925 				goto out;
926 
927 			case SCF_ERROR_DELETED:
928 				scf_transaction_reset(tx);
929 				goto add;
930 
931 			case SCF_ERROR_INVALID_ARGUMENT:
932 			case SCF_ERROR_NOT_BOUND:
933 			case SCF_ERROR_NOT_SET:
934 				bad_error("scf_transaction_commit",
935 				    scf_error());
936 			}
937 		}
938 
939 		scf_transaction_reset(tx);
940 		if (scf_pg_update(pg) == -1) {
941 			switch (scf_error()) {
942 			case SCF_ERROR_CONNECTION_BROKEN:
943 			default:
944 				goto out;
945 
946 			case SCF_ERROR_DELETED:
947 				goto add;
948 
949 			case SCF_ERROR_NOT_SET:
950 			case SCF_ERROR_NOT_BOUND:
951 				bad_error("scf_pg_update", scf_error());
952 			}
953 		}
954 	}
955 
956 	ret = 0;
957 
958 out:
959 	scf_value_destroy(v);
960 	scf_entry_destroy(ent);
961 	scf_transaction_destroy(tx);
962 	scf_property_destroy(prop);
963 	scf_pg_destroy(pg);
964 	return (ret);
965 }
966 
967 static int
968 set_inst_action(const char *fmri, const char *action)
969 {
970 	scf_handle_t *h;
971 	scf_instance_t *inst;
972 	int ret = -1;
973 
974 	h = _scf_handle_create_and_bind(SCF_VERSION);
975 	if (h == NULL)
976 		return (-1);
977 
978 	inst = scf_instance_create(h);
979 
980 	if (inst != NULL) {
981 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
982 		    NULL, SCF_DECODE_FMRI_EXACT) == 0) {
983 			ret = set_inst_action_inst(inst, action);
984 			if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
985 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
986 		} else {
987 			switch (scf_error()) {
988 			case SCF_ERROR_CONSTRAINT_VIOLATED:
989 				(void) scf_set_error(
990 				    SCF_ERROR_INVALID_ARGUMENT);
991 				break;
992 			case SCF_ERROR_DELETED:
993 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
994 				break;
995 			}
996 		}
997 
998 		scf_instance_destroy(inst);
999 	}
1000 
1001 	scf_handle_destroy(h);
1002 
1003 	return (ret);
1004 }
1005 
1006 
1007 /*
1008  * get_inst_state() gets the state string from an instance, and returns
1009  * the SCF_STATE_* constant that coincides with the instance's current state.
1010  */
1011 
1012 static int
1013 get_inst_state(scf_instance_t *inst, scf_handle_t *h)
1014 {
1015 	scf_propertygroup_t	*pg = NULL;
1016 	scf_property_t		*prop = NULL;
1017 	scf_value_t		*val = NULL;
1018 	char			state[MAX_SCF_STATE_STRING_SZ];
1019 	int			ret = -1;
1020 
1021 	if (((pg = scf_pg_create(h)) == NULL) ||
1022 	    ((prop = scf_property_create(h)) == NULL) ||
1023 	    ((val = scf_value_create(h)) == NULL))
1024 		goto out;
1025 
1026 	/* Pull the state property from the instance */
1027 
1028 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
1029 	    scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
1030 	    scf_property_get_value(prop, val) == -1) {
1031 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
1032 			(void) scf_set_error(SCF_ERROR_INTERNAL);
1033 		goto out;
1034 	}
1035 
1036 	if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
1037 		(void) scf_set_error(SCF_ERROR_INTERNAL);
1038 		goto out;
1039 	}
1040 
1041 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
1042 		ret = SCF_STATE_UNINIT;
1043 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
1044 		ret = SCF_STATE_MAINT;
1045 	} else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
1046 		ret = SCF_STATE_OFFLINE;
1047 	} else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
1048 		ret = SCF_STATE_DISABLED;
1049 	} else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
1050 		ret = SCF_STATE_ONLINE;
1051 	} else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
1052 		ret = SCF_STATE_DEGRADED;
1053 	}
1054 
1055 out:
1056 	scf_pg_destroy(pg);
1057 	scf_property_destroy(prop);
1058 	(void) scf_value_destroy(val);
1059 
1060 	return (ret);
1061 }
1062 
1063 /*
1064  * Sets an instance to be enabled or disabled after reboot, using the
1065  * temporary (overriding) general_ovr property group to reflect the
1066  * present state, if it is different.
1067  */
1068 static int
1069 set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired,
1070     const char *comment)
1071 {
1072 	int enabled;
1073 	int persistent;
1074 	int ret = -1;
1075 
1076 	if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
1077 		if (scf_error() != SCF_ERROR_NOT_FOUND)
1078 			goto out;
1079 		persistent = B_FALSE;
1080 	}
1081 	if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
1082 		enabled = persistent;
1083 		if (persistent != desired) {
1084 			/*
1085 			 * Temporarily store the present enabled state.
1086 			 */
1087 			if (set_inst_enabled(inst, persistent,
1088 			    SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS,
1089 			    comment))
1090 				goto out;
1091 		}
1092 	}
1093 	if (persistent != desired)
1094 		if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
1095 		    SCF_PG_GENERAL_FLAGS, comment))
1096 			goto out;
1097 	if (enabled == desired)
1098 		ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
1099 	else
1100 		ret = 0;
1101 
1102 out:
1103 	return (ret);
1104 }
1105 
1106 static int
1107 set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired,
1108     const char *comment)
1109 {
1110 	int ret = -1;
1111 	scf_handle_t *h;
1112 	scf_instance_t *inst;
1113 
1114 	if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
1115 	    flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
1116 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1117 		return (ret);
1118 	}
1119 
1120 	if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
1121 		return (ret);
1122 
1123 	if ((inst = scf_instance_create(h)) == NULL) {
1124 		scf_handle_destroy(h);
1125 		return (ret);
1126 	}
1127 
1128 	if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
1129 	    SCF_DECODE_FMRI_EXACT) == -1) {
1130 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
1131 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1132 		goto out;
1133 	}
1134 
1135 	if (flags & SMF_AT_NEXT_BOOT) {
1136 		ret = set_inst_enabled_atboot(inst, desired, comment);
1137 	} else {
1138 		if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
1139 		    SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
1140 		    SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS, comment))
1141 			goto out;
1142 
1143 		/*
1144 		 * Make the persistent value effective by deleting the
1145 		 * temporary one.
1146 		 */
1147 		if (flags & SMF_TEMPORARY)
1148 			ret = 0;
1149 		else
1150 			ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
1151 	}
1152 
1153 out:
1154 	scf_instance_destroy(inst);
1155 	scf_handle_destroy(h);
1156 	if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
1157 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
1158 	return (ret);
1159 }
1160 
1161 /*
1162  * Create and return a pg from the instance associated with the given handle.
1163  * This function is only called in scf_transaction_setup and
1164  * scf_transaction_restart where the h->rh_instance pointer is properly filled
1165  * in by scf_general_setup_pg().
1166  */
1167 static scf_propertygroup_t *
1168 get_instance_pg(scf_simple_handle_t *simple_h)
1169 {
1170 	scf_propertygroup_t	*ret_pg = scf_pg_create(simple_h->h);
1171 	char			*pg_name;
1172 	ssize_t			namelen;
1173 
1174 	if (ret_pg == NULL) {
1175 		return (NULL);
1176 	}
1177 
1178 	namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
1179 	assert(namelen > 0);
1180 
1181 	if ((pg_name = malloc(namelen)) == NULL) {
1182 		if (scf_error() == SCF_ERROR_NOT_SET) {
1183 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1184 		}
1185 		return (NULL);
1186 	}
1187 
1188 	if (scf_pg_get_name(simple_h->running_pg, pg_name, namelen) < 0) {
1189 		if (scf_error() == SCF_ERROR_NOT_SET) {
1190 			(void) scf_set_error(SCF_ERROR_INTERNAL);
1191 		}
1192 		return (NULL);
1193 	}
1194 
1195 	/* Get pg from instance */
1196 	if (scf_instance_get_pg(simple_h->inst, pg_name, ret_pg) == -1) {
1197 		return (NULL);
1198 	}
1199 
1200 	return (ret_pg);
1201 }
1202 
1203 int
1204 smf_enable_instance(const char *fmri, int flags)
1205 {
1206 	return (set_inst_enabled_flags(fmri, flags, B_TRUE, ""));
1207 }
1208 
1209 int
1210 smf_disable_instance_with_comment(const char *fmri, int flags,
1211     const char *comment)
1212 {
1213 	return (set_inst_enabled_flags(fmri, flags, B_FALSE, comment));
1214 }
1215 
1216 int
1217 smf_disable_instance(const char *fmri, int flags)
1218 {
1219 	return (set_inst_enabled_flags(fmri, flags, B_FALSE, ""));
1220 }
1221 int
1222 _smf_refresh_instance_i(scf_instance_t *inst)
1223 {
1224 	return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
1225 }
1226 
1227 int
1228 _smf_refresh_all_instances(scf_service_t *s)
1229 {
1230 	scf_handle_t	*h = scf_service_handle(s);
1231 	scf_instance_t	*i = scf_instance_create(h);
1232 	scf_iter_t	*it = scf_iter_create(h);
1233 	int err, r = -1;
1234 
1235 	if (h == NULL || i == NULL || it == NULL)
1236 		goto error;
1237 
1238 	if (scf_iter_service_instances(it, s) != 0)
1239 		goto error;
1240 
1241 	while ((err = scf_iter_next_instance(it, i)) == 1)
1242 		if (_smf_refresh_instance_i(i) != 0)
1243 			goto error;
1244 
1245 	if (err == -1)
1246 		goto error;
1247 
1248 	r = 0;
1249 error:
1250 	scf_instance_destroy(i);
1251 	scf_iter_destroy(it);
1252 
1253 	return (r);
1254 }
1255 
1256 int
1257 smf_refresh_instance(const char *instance)
1258 {
1259 	return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
1260 }
1261 
1262 int
1263 smf_restart_instance(const char *instance)
1264 {
1265 	return (set_inst_action(instance, SCF_PROPERTY_RESTART));
1266 }
1267 
1268 int
1269 smf_maintain_instance(const char *instance, int flags)
1270 {
1271 	if (flags & SMF_TEMPORARY)
1272 		return (set_inst_action(instance,
1273 		    (flags & SMF_IMMEDIATE) ?
1274 		    SCF_PROPERTY_MAINT_ON_IMMTEMP :
1275 		    SCF_PROPERTY_MAINT_ON_TEMPORARY));
1276 	else
1277 		return (set_inst_action(instance,
1278 		    (flags & SMF_IMMEDIATE) ?
1279 		    SCF_PROPERTY_MAINT_ON_IMMEDIATE :
1280 		    SCF_PROPERTY_MAINT_ON));
1281 }
1282 
1283 int
1284 smf_degrade_instance(const char *instance, int flags)
1285 {
1286 	scf_simple_prop_t		*prop;
1287 	const char			*state_str;
1288 
1289 	if (flags & SMF_TEMPORARY)
1290 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1291 
1292 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
1293 	    SCF_PROPERTY_STATE)) == NULL)
1294 		return (SCF_FAILED);
1295 
1296 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1297 		scf_simple_prop_free(prop);
1298 		return (SCF_FAILED);
1299 	}
1300 
1301 	if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
1302 		scf_simple_prop_free(prop);
1303 		return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
1304 	}
1305 	scf_simple_prop_free(prop);
1306 
1307 	return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
1308 	    SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
1309 }
1310 
1311 int
1312 smf_restore_instance(const char *instance)
1313 {
1314 	scf_simple_prop_t		*prop;
1315 	const char			*state_str;
1316 	int				ret;
1317 
1318 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
1319 	    SCF_PROPERTY_STATE)) == NULL)
1320 		return (SCF_FAILED);
1321 
1322 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1323 		scf_simple_prop_free(prop);
1324 		return (SCF_FAILED);
1325 	}
1326 
1327 	if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
1328 		ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
1329 	} else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
1330 		ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
1331 	} else {
1332 		ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
1333 	}
1334 
1335 	scf_simple_prop_free(prop);
1336 	return (ret);
1337 }
1338 
1339 char *
1340 smf_get_state(const char *instance)
1341 {
1342 	scf_simple_prop_t		*prop;
1343 	const char			*state_str;
1344 	char				*ret;
1345 
1346 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
1347 	    SCF_PROPERTY_STATE)) == NULL)
1348 		return (NULL);
1349 
1350 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1351 		scf_simple_prop_free(prop);
1352 		return (NULL);
1353 	}
1354 
1355 	if ((ret = strdup(state_str)) == NULL)
1356 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1357 
1358 	scf_simple_prop_free(prop);
1359 	return (ret);
1360 }
1361 
1362 /*
1363  * scf_general_pg_setup(fmri, pg_name)
1364  * Create a scf_simple_handle_t and fill in the instance, snapshot, and
1365  * property group fields associated with the given fmri and property group
1366  * name.
1367  * Returns:
1368  *      Handle  on success
1369  *      Null  on error with scf_error set to:
1370  *              SCF_ERROR_HANDLE_MISMATCH,
1371  *              SCF_ERROR_INVALID_ARGUMENT,
1372  *              SCF_ERROR_CONSTRAINT_VIOLATED,
1373  *              SCF_ERROR_NOT_FOUND,
1374  *              SCF_ERROR_NOT_SET,
1375  *              SCF_ERROR_DELETED,
1376  *              SCF_ERROR_NOT_BOUND,
1377  *              SCF_ERROR_CONNECTION_BROKEN,
1378  *              SCF_ERROR_INTERNAL,
1379  *              SCF_ERROR_NO_RESOURCES,
1380  *              SCF_ERROR_BACKEND_ACCESS
1381  */
1382 scf_simple_handle_t *
1383 scf_general_pg_setup(const char *fmri, const char *pg_name)
1384 {
1385 	scf_simple_handle_t	*ret;
1386 
1387 	ret = uu_zalloc(sizeof (*ret));
1388 	if (ret == NULL) {
1389 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1390 		return (NULL);
1391 	} else {
1392 
1393 		ret->h = _scf_handle_create_and_bind(SCF_VERSION);
1394 		ret->inst = scf_instance_create(ret->h);
1395 		ret->snap = scf_snapshot_create(ret->h);
1396 		ret->running_pg = scf_pg_create(ret->h);
1397 	}
1398 
1399 	if ((ret->h == NULL) || (ret->inst == NULL) ||
1400 	    (ret->snap == NULL) || (ret->running_pg == NULL)) {
1401 		goto out;
1402 	}
1403 
1404 	if (scf_handle_decode_fmri(ret->h, fmri, NULL, NULL, ret->inst,
1405 	    NULL, NULL, 0) == -1) {
1406 		goto out;
1407 	}
1408 
1409 	if ((scf_instance_get_snapshot(ret->inst, "running", ret->snap))
1410 	    != 0) {
1411 		goto out;
1412 	}
1413 
1414 	if (scf_instance_get_pg_composed(ret->inst, ret->snap, pg_name,
1415 	    ret->running_pg) != 0) {
1416 		goto out;
1417 	}
1418 
1419 	return (ret);
1420 
1421 out:
1422 	scf_simple_handle_destroy(ret);
1423 	return (NULL);
1424 }
1425 
1426 /*
1427  * scf_transaction_setup(h)
1428  * creates and starts the transaction
1429  * Returns:
1430  *      transaction  on success
1431  *      NULL on failure with scf_error set to:
1432  *      SCF_ERROR_NO_MEMORY,
1433  *	SCF_ERROR_INVALID_ARGUMENT,
1434  *      SCF_ERROR_HANDLE_DESTROYED,
1435  *	SCF_ERROR_INTERNAL,
1436  *	SCF_ERROR_NO_RESOURCES,
1437  *      SCF_ERROR_NOT_BOUND,
1438  *	SCF_ERROR_CONNECTION_BROKEN,
1439  *      SCF_ERROR_NOT_SET,
1440  *	SCF_ERROR_DELETED,
1441  *	SCF_ERROR_CONSTRAINT_VIOLATED,
1442  *      SCF_ERROR_HANDLE_MISMATCH,
1443  *	SCF_ERROR_BACKEND_ACCESS,
1444  *	SCF_ERROR_IN_USE
1445  */
1446 scf_transaction_t *
1447 scf_transaction_setup(scf_simple_handle_t *simple_h)
1448 {
1449 	scf_transaction_t	*tx = NULL;
1450 
1451 	if ((tx = scf_transaction_create(simple_h->h)) == NULL) {
1452 		return (NULL);
1453 	}
1454 
1455 	if ((simple_h->editing_pg = get_instance_pg(simple_h)) == NULL) {
1456 		return (NULL);
1457 	}
1458 
1459 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1460 		scf_pg_destroy(simple_h->editing_pg);
1461 		simple_h->editing_pg = NULL;
1462 		return (NULL);
1463 	}
1464 
1465 	return (tx);
1466 }
1467 
1468 int
1469 scf_transaction_restart(scf_simple_handle_t *simple_h, scf_transaction_t *tx)
1470 {
1471 	scf_transaction_reset(tx);
1472 
1473 	if (scf_pg_update(simple_h->editing_pg) == -1) {
1474 		return (SCF_FAILED);
1475 	}
1476 
1477 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1478 		return (SCF_FAILED);
1479 	}
1480 
1481 	return (SCF_SUCCESS);
1482 }
1483 
1484 /*
1485  * scf_read_count_property(scf_simple_handle_t *simple_h, char *prop_name,
1486  * uint64_t *ret_count)
1487  *
1488  * For the given property name, return the count value.
1489  * RETURNS:
1490  *	SCF_SUCCESS
1491  *	SCF_FAILED on failure with scf_error() set to:
1492  *		SCF_ERROR_HANDLE_DESTROYED
1493  *		SCF_ERROR_INTERNAL
1494  *		SCF_ERROR_NO_RESOURCES
1495  *		SCF_ERROR_NO_MEMORY
1496  *		SCF_ERROR_HANDLE_MISMATCH
1497  *		SCF_ERROR_INVALID_ARGUMENT
1498  *		SCF_ERROR_NOT_BOUND
1499  *		SCF_ERROR_CONNECTION_BROKEN
1500  *		SCF_ERROR_NOT_SET
1501  *		SCF_ERROR_DELETED
1502  *		SCF_ERROR_BACKEND_ACCESS
1503  *		SCF_ERROR_CONSTRAINT_VIOLATED
1504  *		SCF_ERROR_TYPE_MISMATCH
1505  */
1506 int
1507 scf_read_count_property(
1508 	scf_simple_handle_t	*simple_h,
1509 	char			*prop_name,
1510 	uint64_t		*ret_count)
1511 {
1512 	scf_property_t		*prop = scf_property_create(simple_h->h);
1513 	scf_value_t		*val = scf_value_create(simple_h->h);
1514 	int			ret = SCF_FAILED;
1515 
1516 	if ((val == NULL) || (prop == NULL)) {
1517 		goto out;
1518 	}
1519 
1520 	/*
1521 	 * Get the property struct that goes with this property group and
1522 	 * property name.
1523 	 */
1524 	if (scf_pg_get_property(simple_h->running_pg, prop_name, prop) != 0) {
1525 		goto out;
1526 	}
1527 
1528 	/* Get the value structure */
1529 	if (scf_property_get_value(prop, val) == -1) {
1530 		goto out;
1531 	}
1532 
1533 	/*
1534 	 * Now get the count value.
1535 	 */
1536 	if (scf_value_get_count(val, ret_count) == -1) {
1537 		goto out;
1538 	}
1539 
1540 	ret = SCF_SUCCESS;
1541 
1542 out:
1543 	scf_property_destroy(prop);
1544 	scf_value_destroy(val);
1545 	return (ret);
1546 }
1547 
1548 /*
1549  * scf_trans_add_count_property(trans, propname, count, create_flag)
1550  *
1551  * Set a count property transaction entry into the pending SMF transaction.
1552  * The transaction is created and committed outside of this function.
1553  * Returns:
1554  *	SCF_SUCCESS
1555  *	SCF_FAILED on failure with scf_error() set to:
1556  *			SCF_ERROR_HANDLE_DESTROYED,
1557  *			SCF_ERROR_INVALID_ARGUMENT,
1558  *			SCF_ERROR_NO_MEMORY,
1559  *			SCF_ERROR_HANDLE_MISMATCH,
1560  *			SCF_ERROR_NOT_SET,
1561  *			SCF_ERROR_IN_USE,
1562  *			SCF_ERROR_NOT_FOUND,
1563  *			SCF_ERROR_EXISTS,
1564  *			SCF_ERROR_TYPE_MISMATCH,
1565  *			SCF_ERROR_NOT_BOUND,
1566  *			SCF_ERROR_CONNECTION_BROKEN,
1567  *			SCF_ERROR_INTERNAL,
1568  *			SCF_ERROR_DELETED,
1569  *			SCF_ERROR_NO_RESOURCES,
1570  *			SCF_ERROR_BACKEND_ACCESS
1571  */
1572 int
1573 scf_set_count_property(
1574 	scf_transaction_t	*trans,
1575 	char			*propname,
1576 	uint64_t		count,
1577 	boolean_t		create_flag)
1578 {
1579 	scf_handle_t		*handle = scf_transaction_handle(trans);
1580 	scf_value_t		*value = scf_value_create(handle);
1581 	scf_transaction_entry_t	*entry = scf_entry_create(handle);
1582 
1583 	if ((value == NULL) || (entry == NULL)) {
1584 		return (SCF_FAILED);
1585 	}
1586 
1587 	/*
1588 	 * Property must be set in transaction and won't take
1589 	 * effect until the transaction is committed.
1590 	 *
1591 	 * Attempt to change the current value. However, create new property
1592 	 * if it doesn't exist and the create flag is set.
1593 	 */
1594 	if (scf_transaction_property_change(trans, entry, propname,
1595 	    SCF_TYPE_COUNT) == 0) {
1596 		scf_value_set_count(value, count);
1597 		if (scf_entry_add_value(entry, value) == 0) {
1598 			return (SCF_SUCCESS);
1599 		}
1600 	} else {
1601 		if ((create_flag == B_TRUE) &&
1602 		    (scf_error() == SCF_ERROR_NOT_FOUND)) {
1603 			if (scf_transaction_property_new(trans, entry, propname,
1604 			    SCF_TYPE_COUNT) == 0) {
1605 				scf_value_set_count(value, count);
1606 				if (scf_entry_add_value(entry, value) == 0) {
1607 					return (SCF_SUCCESS);
1608 				}
1609 			}
1610 		}
1611 	}
1612 
1613 	/*
1614 	 * cleanup if there were any errors that didn't leave these
1615 	 * values where they would be cleaned up later.
1616 	 */
1617 	if (value != NULL)
1618 		scf_value_destroy(value);
1619 	if (entry != NULL)
1620 		scf_entry_destroy(entry);
1621 	return (SCF_FAILED);
1622 }
1623 
1624 int
1625 scf_simple_walk_instances(uint_t state_flags, void *private,
1626     int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
1627 {
1628 	scf_scope_t		*scope = NULL;
1629 	scf_service_t		*svc = NULL;
1630 	scf_instance_t		*inst = NULL;
1631 	scf_iter_t		*svc_iter = NULL, *inst_iter = NULL;
1632 	scf_handle_t		*h = NULL;
1633 	int			ret = SCF_FAILED;
1634 	int			svc_iter_ret, inst_iter_ret;
1635 	int			inst_state;
1636 
1637 	if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
1638 		return (ret);
1639 
1640 	if (((scope = scf_scope_create(h)) == NULL) ||
1641 	    ((svc = scf_service_create(h)) == NULL) ||
1642 	    ((inst = scf_instance_create(h)) == NULL) ||
1643 	    ((svc_iter = scf_iter_create(h)) == NULL) ||
1644 	    ((inst_iter = scf_iter_create(h)) == NULL))
1645 		goto out;
1646 
1647 	/*
1648 	 * Get the local scope, and set up nested iteration through every
1649 	 * local service, and every instance of every service.
1650 	 */
1651 
1652 	if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
1653 	    (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
1654 		goto out;
1655 
1656 	while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
1657 
1658 		if ((scf_iter_service_instances(inst_iter, svc)) !=
1659 		    SCF_SUCCESS)
1660 			goto out;
1661 
1662 		while ((inst_iter_ret =
1663 		    scf_iter_next_instance(inst_iter, inst)) > 0) {
1664 			/*
1665 			 * If get_inst_state fails from an internal error,
1666 			 * IE, being unable to get the property group or
1667 			 * property containing the state of the instance,
1668 			 * we continue instead of failing, as this might just
1669 			 * be an improperly configured instance.
1670 			 */
1671 			if ((inst_state = get_inst_state(inst, h)) == -1) {
1672 				if (scf_error() == SCF_ERROR_INTERNAL) {
1673 					continue;
1674 				} else {
1675 					goto out;
1676 				}
1677 			}
1678 
1679 			if ((uint_t)inst_state & state_flags) {
1680 				if (inst_callback(h, inst, private) !=
1681 				    SCF_SUCCESS) {
1682 					(void) scf_set_error(
1683 					    SCF_ERROR_CALLBACK_FAILED);
1684 					goto out;
1685 				}
1686 			}
1687 		}
1688 
1689 		if (inst_iter_ret == -1)
1690 			goto out;
1691 		scf_iter_reset(inst_iter);
1692 	}
1693 
1694 	if (svc_iter_ret != -1)
1695 		ret = SCF_SUCCESS;
1696 
1697 out:
1698 	scf_scope_destroy(scope);
1699 	scf_service_destroy(svc);
1700 	scf_instance_destroy(inst);
1701 	scf_iter_destroy(svc_iter);
1702 	scf_iter_destroy(inst_iter);
1703 	scf_handle_destroy(h);
1704 
1705 	return (ret);
1706 }
1707 
1708 
1709 scf_simple_prop_t *
1710 scf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
1711     const char *propname)
1712 {
1713 	char			*fmri_buf, *svcfmri = NULL;
1714 	ssize_t			fmri_sz;
1715 	scf_property_t		*prop = NULL;
1716 	scf_service_t		*svc = NULL;
1717 	scf_simple_prop_t	*ret;
1718 	scf_handle_t		*h = NULL;
1719 	boolean_t		local_h = B_TRUE;
1720 
1721 	/* If the user passed in a handle, use it. */
1722 	if (hin != NULL) {
1723 		h = hin;
1724 		local_h = B_FALSE;
1725 	}
1726 
1727 	if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
1728 		return (NULL);
1729 
1730 	if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
1731 		if (local_h)
1732 			scf_handle_destroy(h);
1733 		return (NULL);
1734 	}
1735 
1736 	if ((svc = scf_service_create(h)) == NULL ||
1737 	    (prop = scf_property_create(h)) == NULL)
1738 		goto error1;
1739 	if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
1740 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
1741 		switch (scf_error()) {
1742 		/*
1743 		 * If the property isn't found in the instance, we grab the
1744 		 * underlying service, create an FMRI out of it, and then
1745 		 * query the datastore again at the service level for the
1746 		 * property.
1747 		 */
1748 		case SCF_ERROR_NOT_FOUND:
1749 			if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
1750 			    NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
1751 				goto error1;
1752 
1753 			fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
1754 			assert(fmri_sz > 0);
1755 
1756 			if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
1757 				goto error1;
1758 			if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
1759 			    propname)) == NULL)
1760 				goto error1;
1761 			if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
1762 			    NULL, NULL, prop, 0) == -1) {
1763 				free(svcfmri);
1764 				goto error1;
1765 			}
1766 			free(svcfmri);
1767 			break;
1768 		case SCF_ERROR_CONSTRAINT_VIOLATED:
1769 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1770 		default:
1771 			goto error1;
1772 		}
1773 	}
1774 	/*
1775 	 * At this point, we've successfully pulled the property from the
1776 	 * datastore, and simply need to copy its innards into an
1777 	 * scf_simple_prop_t.
1778 	 */
1779 	if ((ret = fill_prop(prop, pgname, propname, h)) == NULL)
1780 		goto error1;
1781 
1782 	scf_service_destroy(svc);
1783 	scf_property_destroy(prop);
1784 	free(fmri_buf);
1785 	if (local_h)
1786 		scf_handle_destroy(h);
1787 	return (ret);
1788 
1789 	/*
1790 	 * Exit point for a successful call.  Below this line are exit points
1791 	 * for failures at various stages during the function.
1792 	 */
1793 
1794 error1:
1795 	scf_service_destroy(svc);
1796 	scf_property_destroy(prop);
1797 error2:
1798 	free(fmri_buf);
1799 	if (local_h)
1800 		scf_handle_destroy(h);
1801 	return (NULL);
1802 }
1803 
1804 
1805 void
1806 scf_simple_prop_free(scf_simple_prop_t *prop)
1807 {
1808 	int i;
1809 
1810 	if (prop == NULL)
1811 		return;
1812 
1813 	free(prop->pr_propname);
1814 	free(prop->pr_pgname);
1815 	switch (prop->pr_type) {
1816 	case SCF_TYPE_OPAQUE: {
1817 		for (i = 0; i < prop->pr_numvalues; i++) {
1818 			free(prop->pr_vallist[i].pv_opaque.o_value);
1819 		}
1820 		break;
1821 	}
1822 	case SCF_TYPE_ASTRING:
1823 	case SCF_TYPE_USTRING:
1824 	case SCF_TYPE_HOST:
1825 	case SCF_TYPE_HOSTNAME:
1826 	case SCF_TYPE_NET_ADDR:
1827 	case SCF_TYPE_NET_ADDR_V4:
1828 	case SCF_TYPE_NET_ADDR_V6:
1829 	case SCF_TYPE_URI:
1830 	case SCF_TYPE_FMRI: {
1831 		for (i = 0; i < prop->pr_numvalues; i++) {
1832 			free(prop->pr_vallist[i].pv_str);
1833 		}
1834 		break;
1835 	}
1836 	default:
1837 		break;
1838 	}
1839 	free(prop->pr_vallist);
1840 	free(prop);
1841 }
1842 
1843 
1844 scf_simple_app_props_t *
1845 scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
1846 {
1847 	scf_instance_t		*inst = NULL;
1848 	scf_service_t		*svc = NULL;
1849 	scf_propertygroup_t	*pg = NULL;
1850 	scf_property_t		*prop = NULL;
1851 	scf_simple_app_props_t	*ret = NULL;
1852 	scf_iter_t		*pgiter = NULL, *propiter = NULL;
1853 	struct scf_simple_pg	*thispg = NULL, *nextpg;
1854 	scf_simple_prop_t	*thisprop, *nextprop;
1855 	scf_handle_t		*h = NULL;
1856 	int			pgiter_ret, propiter_ret;
1857 	ssize_t			namelen;
1858 	char			*propname = NULL, *pgname = NULL, *sys_fmri;
1859 	uint8_t			found;
1860 	boolean_t		local_h = B_TRUE;
1861 
1862 	/* If the user passed in a handle, use it. */
1863 	if (hin != NULL) {
1864 		h = hin;
1865 		local_h = B_FALSE;
1866 	}
1867 
1868 	if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
1869 		return (NULL);
1870 
1871 	if (inst_fmri == NULL) {
1872 		if ((namelen = scf_myname(h, NULL, 0)) == -1) {
1873 			if (local_h)
1874 				scf_handle_destroy(h);
1875 			return (NULL);
1876 		}
1877 		if ((sys_fmri = malloc(namelen + 1)) == NULL) {
1878 			if (local_h)
1879 				scf_handle_destroy(h);
1880 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1881 			return (NULL);
1882 		}
1883 		if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
1884 			if (local_h)
1885 				scf_handle_destroy(h);
1886 			free(sys_fmri);
1887 			return (NULL);
1888 		}
1889 	} else {
1890 		if ((sys_fmri = strdup(inst_fmri)) == NULL) {
1891 			if (local_h)
1892 				scf_handle_destroy(h);
1893 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1894 			return (NULL);
1895 		}
1896 	}
1897 
1898 	namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
1899 	assert(namelen > 0);
1900 
1901 	if ((inst = scf_instance_create(h)) == NULL ||
1902 	    (svc = scf_service_create(h)) == NULL ||
1903 	    (pgiter = scf_iter_create(h)) == NULL ||
1904 	    (propiter = scf_iter_create(h)) == NULL ||
1905 	    (pg = scf_pg_create(h)) == NULL ||
1906 	    (prop = scf_property_create(h)) == NULL) {
1907 		free(sys_fmri);
1908 		goto error2;
1909 	}
1910 
1911 	if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
1912 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
1913 		free(sys_fmri);
1914 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
1915 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1916 		goto error2;
1917 	}
1918 
1919 	if ((ret = malloc(sizeof (*ret))) == NULL ||
1920 	    (thispg = malloc(sizeof (*thispg))) == NULL ||
1921 	    (propname = malloc(namelen)) == NULL ||
1922 	    (pgname = malloc(namelen)) == NULL) {
1923 		free(thispg);
1924 		free(ret);
1925 		free(sys_fmri);
1926 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1927 		goto error2;
1928 	}
1929 
1930 	ret->ap_fmri = sys_fmri;
1931 	thispg->pg_name = NULL;
1932 	thispg->pg_proplist = NULL;
1933 	thispg->pg_next = NULL;
1934 	ret->ap_pglist = thispg;
1935 
1936 	if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
1937 	    0) {
1938 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
1939 			(void) scf_set_error(SCF_ERROR_INTERNAL);
1940 		goto error1;
1941 	}
1942 
1943 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
1944 		if (thispg->pg_name != NULL) {
1945 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
1946 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1947 				goto error1;
1948 			}
1949 			nextpg->pg_name = NULL;
1950 			nextpg->pg_next = NULL;
1951 			nextpg->pg_proplist = NULL;
1952 			thispg->pg_next = nextpg;
1953 			thispg = nextpg;
1954 		} else {
1955 			/* This is the first iteration */
1956 			nextpg = thispg;
1957 		}
1958 
1959 		if ((nextpg->pg_name = malloc(namelen)) == NULL) {
1960 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1961 			goto error1;
1962 		}
1963 
1964 		if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
1965 			if (scf_error() == SCF_ERROR_NOT_SET)
1966 				(void) scf_set_error(SCF_ERROR_INTERNAL);
1967 			goto error1;
1968 		}
1969 
1970 		thisprop = NULL;
1971 
1972 		scf_iter_reset(propiter);
1973 
1974 		if (scf_iter_pg_properties(propiter, pg) != 0) {
1975 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
1976 				(void) scf_set_error(SCF_ERROR_INTERNAL);
1977 			goto error1;
1978 		}
1979 
1980 		while ((propiter_ret = scf_iter_next_property(propiter, prop))
1981 		    == 1) {
1982 			if (scf_property_get_name(prop, propname, namelen) <
1983 			    0) {
1984 				if (scf_error() == SCF_ERROR_NOT_SET)
1985 					(void) scf_set_error(
1986 					    SCF_ERROR_INTERNAL);
1987 				goto error1;
1988 			}
1989 			if (thisprop != NULL) {
1990 				if ((nextprop = fill_prop(prop,
1991 				    nextpg->pg_name, propname, h)) == NULL)
1992 					goto error1;
1993 				thisprop->pr_next = nextprop;
1994 				thisprop = nextprop;
1995 			} else {
1996 				/* This is the first iteration */
1997 				if ((thisprop = fill_prop(prop,
1998 				    nextpg->pg_name, propname, h)) == NULL)
1999 					goto error1;
2000 				nextpg->pg_proplist = thisprop;
2001 				nextprop = thisprop;
2002 			}
2003 			nextprop->pr_pg = nextpg;
2004 			nextprop->pr_next = NULL;
2005 		}
2006 
2007 		if (propiter_ret == -1) {
2008 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2009 				(void) scf_set_error(SCF_ERROR_INTERNAL);
2010 			goto error1;
2011 		}
2012 	}
2013 
2014 	if (pgiter_ret == -1) {
2015 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2016 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2017 		goto error1;
2018 	}
2019 
2020 	/*
2021 	 * At this point, we've filled the scf_simple_app_props_t with all the
2022 	 * properties at the service level.  Now we iterate over all the
2023 	 * properties at the instance level, overwriting any duplicate
2024 	 * properties, in order to provide service/instance composition.
2025 	 */
2026 
2027 	scf_iter_reset(pgiter);
2028 	scf_iter_reset(propiter);
2029 
2030 	if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
2031 	    != 0) {
2032 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2033 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2034 		goto error1;
2035 	}
2036 
2037 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
2038 
2039 		thispg = ret->ap_pglist;
2040 		found = 0;
2041 
2042 		/*
2043 		 * Find either the end of the list, so we can append the
2044 		 * property group, or an existing property group that matches
2045 		 * it, so we can insert/overwrite its properties.
2046 		 */
2047 
2048 		if (scf_pg_get_name(pg, pgname, namelen) < 0) {
2049 			if (scf_error() == SCF_ERROR_NOT_SET)
2050 				(void) scf_set_error(SCF_ERROR_INTERNAL);
2051 			goto error1;
2052 		}
2053 
2054 		while ((thispg != NULL) && (thispg->pg_name != NULL)) {
2055 			if (strcmp(thispg->pg_name, pgname) == 0) {
2056 				found = 1;
2057 				break;
2058 			}
2059 			if (thispg->pg_next == NULL)
2060 				break;
2061 
2062 			thispg = thispg->pg_next;
2063 		}
2064 
2065 		scf_iter_reset(propiter);
2066 
2067 		if (scf_iter_pg_properties(propiter, pg) != 0) {
2068 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2069 				(void) scf_set_error(SCF_ERROR_INTERNAL);
2070 			goto error1;
2071 		}
2072 
2073 		if (found) {
2074 			/*
2075 			 * insert_app_props inserts or overwrites the
2076 			 * properties in thispg.
2077 			 */
2078 
2079 			if (insert_app_props(propiter, pgname, propname,
2080 			    thispg, prop, namelen, h) == -1)
2081 				goto error1;
2082 
2083 		} else {
2084 			/*
2085 			 * If the property group wasn't found, we're adding
2086 			 * a newly allocated property group to the end of the
2087 			 * list.
2088 			 */
2089 
2090 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
2091 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2092 				goto error1;
2093 			}
2094 			nextpg->pg_next = NULL;
2095 			nextpg->pg_proplist = NULL;
2096 			thisprop = NULL;
2097 
2098 			if ((nextpg->pg_name = strdup(pgname)) == NULL) {
2099 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2100 				free(nextpg);
2101 				goto error1;
2102 			}
2103 
2104 			if (thispg->pg_name == NULL) {
2105 				free(thispg);
2106 				ret->ap_pglist = nextpg;
2107 			} else {
2108 				thispg->pg_next = nextpg;
2109 			}
2110 
2111 			while ((propiter_ret =
2112 			    scf_iter_next_property(propiter, prop)) == 1) {
2113 				if (scf_property_get_name(prop, propname,
2114 				    namelen) < 0) {
2115 					if (scf_error() == SCF_ERROR_NOT_SET)
2116 						(void) scf_set_error(
2117 						    SCF_ERROR_INTERNAL);
2118 					goto error1;
2119 				}
2120 				if (thisprop != NULL) {
2121 					if ((nextprop = fill_prop(prop,
2122 					    pgname, propname, h)) ==
2123 					    NULL)
2124 						goto error1;
2125 					thisprop->pr_next = nextprop;
2126 					thisprop = nextprop;
2127 				} else {
2128 					/* This is the first iteration */
2129 					if ((thisprop = fill_prop(prop,
2130 					    pgname, propname, h)) ==
2131 					    NULL)
2132 						goto error1;
2133 					nextpg->pg_proplist = thisprop;
2134 					nextprop = thisprop;
2135 				}
2136 				nextprop->pr_pg = nextpg;
2137 				nextprop->pr_next = NULL;
2138 			}
2139 
2140 			if (propiter_ret == -1) {
2141 				if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2142 					(void) scf_set_error(
2143 					    SCF_ERROR_INTERNAL);
2144 				goto error1;
2145 			}
2146 		}
2147 
2148 	}
2149 
2150 	if (pgiter_ret == -1) {
2151 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2152 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2153 		goto error1;
2154 	}
2155 
2156 	if (ret->ap_pglist->pg_name == NULL)
2157 		goto error1;
2158 
2159 	scf_iter_destroy(pgiter);
2160 	scf_iter_destroy(propiter);
2161 	scf_pg_destroy(pg);
2162 	scf_property_destroy(prop);
2163 	scf_instance_destroy(inst);
2164 	scf_service_destroy(svc);
2165 	free(propname);
2166 	free(pgname);
2167 	if (local_h)
2168 		scf_handle_destroy(h);
2169 
2170 	return (ret);
2171 
2172 	/*
2173 	 * Exit point for a successful call.  Below this line are exit points
2174 	 * for failures at various stages during the function.
2175 	 */
2176 
2177 error1:
2178 	scf_simple_app_props_free(ret);
2179 
2180 error2:
2181 	scf_iter_destroy(pgiter);
2182 	scf_iter_destroy(propiter);
2183 	scf_pg_destroy(pg);
2184 	scf_property_destroy(prop);
2185 	scf_instance_destroy(inst);
2186 	scf_service_destroy(svc);
2187 	free(propname);
2188 	free(pgname);
2189 	if (local_h)
2190 		scf_handle_destroy(h);
2191 	return (NULL);
2192 }
2193 
2194 
2195 void
2196 scf_simple_app_props_free(scf_simple_app_props_t *propblock)
2197 {
2198 	struct scf_simple_pg	*pgthis, *pgnext;
2199 	scf_simple_prop_t	*propthis, *propnext;
2200 
2201 	if ((propblock == NULL) || (propblock->ap_pglist == NULL))
2202 		return;
2203 
2204 	for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
2205 		pgnext = pgthis->pg_next;
2206 
2207 		propthis = pgthis->pg_proplist;
2208 
2209 		while (propthis != NULL) {
2210 			propnext = propthis->pr_next;
2211 			scf_simple_prop_free(propthis);
2212 			propthis = propnext;
2213 		}
2214 
2215 		free(pgthis->pg_name);
2216 		free(pgthis);
2217 	}
2218 
2219 	free(propblock->ap_fmri);
2220 	free(propblock);
2221 }
2222 
2223 const scf_simple_prop_t *
2224 scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
2225     scf_simple_prop_t *last)
2226 {
2227 	struct scf_simple_pg	*this;
2228 
2229 	if (propblock == NULL) {
2230 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2231 		return (NULL);
2232 	}
2233 
2234 	this = propblock->ap_pglist;
2235 
2236 	/*
2237 	 * We're looking for the first property in this block if last is
2238 	 * NULL
2239 	 */
2240 
2241 	if (last == NULL) {
2242 		/* An empty pglist is legal, it just means no properties */
2243 		if (this == NULL) {
2244 			(void) scf_set_error(SCF_ERROR_NONE);
2245 			return (NULL);
2246 		}
2247 		/*
2248 		 * Walk until we find a pg with a property in it, or we run
2249 		 * out of property groups.
2250 		 */
2251 		while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2252 			this = this->pg_next;
2253 
2254 		if (this->pg_proplist == NULL) {
2255 			(void) scf_set_error(SCF_ERROR_NONE);
2256 			return (NULL);
2257 		}
2258 
2259 		return (this->pg_proplist);
2260 
2261 	}
2262 	/*
2263 	 * If last isn't NULL, then return the next prop in the property group,
2264 	 * or walk the property groups until we find another property, or
2265 	 * run out of property groups.
2266 	 */
2267 	if (last->pr_next != NULL)
2268 		return (last->pr_next);
2269 
2270 	if (last->pr_pg->pg_next == NULL) {
2271 		(void) scf_set_error(SCF_ERROR_NONE);
2272 		return (NULL);
2273 	}
2274 
2275 	this = last->pr_pg->pg_next;
2276 
2277 	while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2278 		this = this->pg_next;
2279 
2280 	if (this->pg_proplist == NULL) {
2281 		(void) scf_set_error(SCF_ERROR_NONE);
2282 		return (NULL);
2283 	}
2284 
2285 	return (this->pg_proplist);
2286 }
2287 
2288 const scf_simple_prop_t *
2289 scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
2290     const char *pgname, const char *propname)
2291 {
2292 	struct scf_simple_pg	*pg;
2293 	scf_simple_prop_t	*prop;
2294 
2295 	if ((propblock == NULL) || (propname == NULL)) {
2296 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2297 		return (NULL);
2298 	}
2299 
2300 	pg = propblock->ap_pglist;
2301 
2302 	/*
2303 	 * If pgname is NULL, we're searching the default application
2304 	 * property group, otherwise we look for the specified group.
2305 	 */
2306 	if (pgname == NULL) {
2307 		while ((pg != NULL) &&
2308 		    (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
2309 			pg = pg->pg_next;
2310 	} else {
2311 		while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
2312 			pg = pg->pg_next;
2313 	}
2314 
2315 	if (pg == NULL) {
2316 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
2317 		return (NULL);
2318 	}
2319 
2320 	prop = pg->pg_proplist;
2321 
2322 	while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
2323 		prop = prop->pr_next;
2324 
2325 	if (prop == NULL) {
2326 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
2327 		return (NULL);
2328 	}
2329 
2330 	return (prop);
2331 }
2332 
2333 void
2334 scf_simple_prop_next_reset(scf_simple_prop_t *prop)
2335 {
2336 	if (prop == NULL)
2337 		return;
2338 	prop->pr_iter = 0;
2339 }
2340 
2341 ssize_t
2342 scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
2343 {
2344 	if (prop == NULL)
2345 		return (scf_set_error(SCF_ERROR_NOT_SET));
2346 
2347 	return (prop->pr_numvalues);
2348 }
2349 
2350 
2351 scf_type_t
2352 scf_simple_prop_type(const scf_simple_prop_t *prop)
2353 {
2354 	if (prop == NULL)
2355 		return (scf_set_error(SCF_ERROR_NOT_SET));
2356 
2357 	return (prop->pr_type);
2358 }
2359 
2360 
2361 char *
2362 scf_simple_prop_name(const scf_simple_prop_t *prop)
2363 {
2364 	if ((prop == NULL) || (prop->pr_propname == NULL)) {
2365 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2366 		return (NULL);
2367 	}
2368 
2369 	return (prop->pr_propname);
2370 }
2371 
2372 
2373 char *
2374 scf_simple_prop_pgname(const scf_simple_prop_t *prop)
2375 {
2376 	if ((prop == NULL) || (prop->pr_pgname == NULL)) {
2377 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2378 		return (NULL);
2379 	}
2380 
2381 	return (prop->pr_pgname);
2382 }
2383 
2384 
2385 static union scf_simple_prop_val *
2386 scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
2387 {
2388 	if (prop == NULL) {
2389 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2390 		return (NULL);
2391 	}
2392 
2393 	switch (prop->pr_type) {
2394 	case SCF_TYPE_USTRING:
2395 	case SCF_TYPE_HOST:
2396 	case SCF_TYPE_HOSTNAME:
2397 	case SCF_TYPE_NET_ADDR:
2398 	case SCF_TYPE_NET_ADDR_V4:
2399 	case SCF_TYPE_NET_ADDR_V6:
2400 	case SCF_TYPE_URI:
2401 	case SCF_TYPE_FMRI: {
2402 		if (type != SCF_TYPE_USTRING) {
2403 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2404 			return (NULL);
2405 		}
2406 		break;
2407 		}
2408 	default: {
2409 		if (type != prop->pr_type) {
2410 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2411 			return (NULL);
2412 		}
2413 		break;
2414 		}
2415 	}
2416 
2417 	if (prop->pr_iter >= prop->pr_numvalues) {
2418 		(void) scf_set_error(SCF_ERROR_NONE);
2419 		return (NULL);
2420 	}
2421 
2422 	return (&prop->pr_vallist[prop->pr_iter++]);
2423 }
2424 
2425 
2426 uint8_t *
2427 scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
2428 {
2429 	union scf_simple_prop_val *ret;
2430 
2431 	ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
2432 
2433 	if (ret == NULL)
2434 		return (NULL);
2435 
2436 	return (&ret->pv_bool);
2437 }
2438 
2439 
2440 uint64_t *
2441 scf_simple_prop_next_count(scf_simple_prop_t *prop)
2442 {
2443 	union scf_simple_prop_val *ret;
2444 
2445 	ret = scf_next_val(prop, SCF_TYPE_COUNT);
2446 
2447 	if (ret == NULL)
2448 		return (NULL);
2449 
2450 	return (&ret->pv_uint);
2451 }
2452 
2453 
2454 int64_t *
2455 scf_simple_prop_next_integer(scf_simple_prop_t *prop)
2456 {
2457 	union scf_simple_prop_val *ret;
2458 
2459 	ret = scf_next_val(prop, SCF_TYPE_INTEGER);
2460 
2461 	if (ret == NULL)
2462 		return (NULL);
2463 
2464 	return (&ret->pv_int);
2465 }
2466 
2467 int64_t *
2468 scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
2469 {
2470 	union scf_simple_prop_val *ret;
2471 
2472 	ret = scf_next_val(prop, SCF_TYPE_TIME);
2473 
2474 	if (ret == NULL)
2475 		return (NULL);
2476 
2477 	if (nsec != NULL)
2478 		*nsec = ret->pv_time.t_nsec;
2479 
2480 	return (&ret->pv_time.t_sec);
2481 }
2482 
2483 char *
2484 scf_simple_prop_next_astring(scf_simple_prop_t *prop)
2485 {
2486 	union scf_simple_prop_val *ret;
2487 
2488 	ret = scf_next_val(prop, SCF_TYPE_ASTRING);
2489 
2490 	if (ret == NULL)
2491 		return (NULL);
2492 
2493 	return (ret->pv_str);
2494 }
2495 
2496 char *
2497 scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
2498 {
2499 	union scf_simple_prop_val *ret;
2500 
2501 	ret = scf_next_val(prop, SCF_TYPE_USTRING);
2502 
2503 	if (ret == NULL)
2504 		return (NULL);
2505 
2506 	return (ret->pv_str);
2507 }
2508 
2509 void *
2510 scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
2511 {
2512 	union scf_simple_prop_val *ret;
2513 
2514 	ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
2515 
2516 	if (ret == NULL) {
2517 		*length = 0;
2518 		return (NULL);
2519 	}
2520 
2521 	*length = ret->pv_opaque.o_size;
2522 	return (ret->pv_opaque.o_value);
2523 }
2524 
2525 /*
2526  * Generate a filename based on the fmri and the given name and return
2527  * it in the buffer of MAXPATHLEN provided by the caller.
2528  * If temp_filename is non-zero, also generate a temporary, unique filename
2529  * and return it in the temp buffer of MAXPATHLEN provided by the caller.
2530  * The path to the generated pathname is also created.
2531  * Given fmri should begin with a scheme such as "svc:".
2532  * Returns
2533  *      0 on success
2534  *      -1 if filename would exceed MAXPATHLEN or
2535  *	-2 if unable to create directory to filename path
2536  */
2537 int
2538 gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
2539     char *temp_filename)
2540 {
2541 	int		len;
2542 
2543 	len = strlen(SMF_SPEEDY_FILES_PATH);
2544 	len += strlen(fmri);
2545 	len += 2;			/* for slash and null */
2546 	len += strlen(name);
2547 	len += 6;			/* For X's needed for mkstemp */
2548 
2549 	if (len > MAXPATHLEN)
2550 		return (-1);
2551 
2552 	/* Construct directory name first - speedy path ends in slash */
2553 	(void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
2554 	(void) strcat(filename, fmri);
2555 	if (mkdirp(filename, 0755) == -1) {
2556 		/* errno is set */
2557 		if (errno != EEXIST)
2558 			return (-2);
2559 	}
2560 
2561 	(void) strcat(filename, "/");
2562 	(void) strcat(filename, name);
2563 
2564 	if (temp_filename) {
2565 		(void) strcpy(temp_filename, filename);
2566 		(void) strcat(temp_filename, "XXXXXX");
2567 	}
2568 
2569 	return (0);
2570 }
2571 
2572 scf_type_t
2573 scf_true_base_type(scf_type_t type)
2574 {
2575 	scf_type_t base = type;
2576 
2577 	do {
2578 		type = base;
2579 		(void) scf_type_base_type(type, &base);
2580 	} while (base != type);
2581 
2582 	return (base);
2583 }
2584 
2585 /*
2586  * Convenience routine which frees all strings and opaque data
2587  * allocated by scf_read_propvec.
2588  *
2589  * Like free(3C), this function preserves the value of errno.
2590  */
2591 void
2592 scf_clean_propvec(scf_propvec_t *propvec)
2593 {
2594 	int saved_errno = errno;
2595 	scf_propvec_t *prop;
2596 
2597 	for (prop = propvec; prop->pv_prop != NULL; prop++) {
2598 		assert(prop->pv_type != SCF_TYPE_INVALID);
2599 		if (prop->pv_type == SCF_TYPE_OPAQUE) {
2600 			scf_opaque_t *o = prop->pv_ptr;
2601 
2602 			if (o->so_addr != NULL)
2603 				free(o->so_addr);
2604 		} else if (scf_true_base_type(prop->pv_type) ==
2605 		    SCF_TYPE_ASTRING) {
2606 			if (*(char **)prop->pv_ptr != NULL)
2607 				free(*(char **)prop->pv_ptr);
2608 		}
2609 	}
2610 
2611 	errno = saved_errno;
2612 }
2613 
2614 static int
2615 count_props(scf_propvec_t *props)
2616 {
2617 	int count = 0;
2618 
2619 	for (; props->pv_prop != NULL; props++)
2620 		count++;
2621 	return (count);
2622 }
2623 
2624 /*
2625  * Reads a vector of properties from the specified fmri/property group.
2626  * If 'running' is true, reads from the running snapshot instead of the
2627  * editing snapshot.
2628  *
2629  * For string types, a buffer is allocated using malloc(3C) to hold the
2630  * zero-terminated string, a pointer to which is stored in the
2631  * caller-provided char **.  It is the caller's responsbility to free
2632  * this string.  To simplify error handling, unread strings are
2633  * initialized to NULL.
2634  *
2635  * For opaque types, a buffer is allocated using malloc(3C) to hold the
2636  * opaque data.  A pointer to this buffer and its size are stored in
2637  * the caller-provided scf_opaque_t.  It is the caller's responsibility
2638  * to free this buffer.  To simplify error handling, the address fields
2639  * for unread opaque data are initialized to NULL.
2640  *
2641  * All other data is stored directly in caller-provided variables or
2642  * structures.
2643  *
2644  * If this function fails to read a specific property, *badprop is set
2645  * to point at that property's entry in the properties array.
2646  *
2647  * On all failures, all memory allocated by this function is freed.
2648  */
2649 int
2650 scf_read_propvec(const char *fmri, const char *pgname, boolean_t running,
2651     scf_propvec_t *properties, scf_propvec_t **badprop)
2652 {
2653 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2654 	scf_service_t *s = scf_service_create(h);
2655 	scf_instance_t *i = scf_instance_create(h);
2656 	scf_snapshot_t *snap = running ? scf_snapshot_create(h) : NULL;
2657 	scf_propertygroup_t *pg = scf_pg_create(h);
2658 	scf_property_t *p = scf_property_create(h);
2659 	scf_value_t *v = scf_value_create(h);
2660 	boolean_t instance = B_TRUE;
2661 	scf_propvec_t *prop;
2662 	int error = 0;
2663 
2664 	for (prop = properties; prop->pv_prop != NULL; prop++) {
2665 		if (prop->pv_type == SCF_TYPE_OPAQUE)
2666 			((scf_opaque_t *)prop->pv_ptr)->so_addr = NULL;
2667 		else if (scf_true_base_type(prop->pv_type) == SCF_TYPE_ASTRING)
2668 			*((char **)prop->pv_ptr) = NULL;
2669 	}
2670 
2671 	if (h == NULL || s == NULL || i == NULL || (running && snap == NULL) ||
2672 	    pg == NULL || p == NULL || v == NULL)
2673 		goto scferror;
2674 
2675 	if (scf_handle_decode_fmri(h, fmri, NULL, s, i, NULL, NULL, 0) == -1)
2676 		goto scferror;
2677 
2678 	if (scf_instance_to_fmri(i, NULL, 0) == -1) {
2679 		if (scf_error() != SCF_ERROR_NOT_SET)
2680 			goto scferror;
2681 		instance = B_FALSE;
2682 	}
2683 
2684 	if (running) {
2685 		if (!instance) {
2686 			error = SCF_ERROR_TYPE_MISMATCH;
2687 			goto out;
2688 		}
2689 
2690 		if (scf_instance_get_snapshot(i, "running", snap) !=
2691 		    SCF_SUCCESS)
2692 			goto scferror;
2693 	}
2694 
2695 	if ((instance ? scf_instance_get_pg_composed(i, snap, pgname, pg) :
2696 	    scf_service_get_pg(s, pgname, pg)) == -1)
2697 		goto scferror;
2698 
2699 	for (prop = properties; prop->pv_prop != NULL; prop++) {
2700 		int ret = 0;
2701 
2702 		if (scf_pg_get_property(pg, prop->pv_prop, p) == -1 ||
2703 		    scf_property_get_value(p, v) == -1) {
2704 			*badprop = prop;
2705 			goto scferror;
2706 		}
2707 		switch (prop->pv_type) {
2708 		case SCF_TYPE_BOOLEAN: {
2709 			uint8_t b;
2710 
2711 			ret = scf_value_get_boolean(v, &b);
2712 			if (ret == -1)
2713 				break;
2714 			if (prop->pv_aux != 0) {
2715 				uint64_t *bits = prop->pv_ptr;
2716 				*bits = b ? (*bits | prop->pv_aux) :
2717 				    (*bits & ~prop->pv_aux);
2718 			} else {
2719 				boolean_t *bool = prop->pv_ptr;
2720 				*bool = b ? B_TRUE : B_FALSE;
2721 			}
2722 			break;
2723 		}
2724 		case SCF_TYPE_COUNT:
2725 			ret = scf_value_get_count(v, prop->pv_ptr);
2726 			break;
2727 		case SCF_TYPE_INTEGER:
2728 			ret = scf_value_get_integer(v, prop->pv_ptr);
2729 			break;
2730 		case SCF_TYPE_TIME: {
2731 			scf_time_t *time = prop->pv_ptr;
2732 
2733 			ret = scf_value_get_time(v, &time->t_seconds,
2734 			    &time->t_ns);
2735 			break;
2736 		}
2737 		case SCF_TYPE_OPAQUE: {
2738 			scf_opaque_t *opaque = prop->pv_ptr;
2739 			ssize_t size = scf_value_get_opaque(v, NULL, 0);
2740 
2741 			if (size == -1) {
2742 				*badprop = prop;
2743 				goto scferror;
2744 			}
2745 			if ((opaque->so_addr = malloc(size)) == NULL) {
2746 				error = SCF_ERROR_NO_MEMORY;
2747 				goto out;
2748 			}
2749 			opaque->so_size = size;
2750 			ret = scf_value_get_opaque(v, opaque->so_addr, size);
2751 			break;
2752 		}
2753 		default: {
2754 			char *s;
2755 			ssize_t size;
2756 
2757 			assert(scf_true_base_type(prop->pv_type) ==
2758 			    SCF_TYPE_ASTRING);
2759 
2760 			size = scf_value_get_astring(v, NULL, 0);
2761 			if (size == -1) {
2762 				*badprop = prop;
2763 				goto scferror;
2764 			}
2765 			if ((s = malloc(++size)) == NULL) {
2766 				error = SCF_ERROR_NO_MEMORY;
2767 				goto out;
2768 			}
2769 			ret = scf_value_get_astring(v, s, size);
2770 			*(char **)prop->pv_ptr = s;
2771 		}
2772 
2773 		if (ret == -1) {
2774 			*badprop = prop;
2775 			goto scferror;
2776 		}
2777 
2778 		}
2779 	}
2780 
2781 	goto out;
2782 
2783 scferror:
2784 	error = scf_error();
2785 	scf_clean_propvec(properties);
2786 
2787 out:
2788 	scf_value_destroy(v);
2789 	scf_property_destroy(p);
2790 	scf_pg_destroy(pg);
2791 	scf_snapshot_destroy(snap);
2792 	scf_instance_destroy(i);
2793 	scf_service_destroy(s);
2794 	scf_handle_destroy(h);
2795 
2796 	if (error != 0) {
2797 		(void) scf_set_error(error);
2798 		return (SCF_FAILED);
2799 	}
2800 
2801 	return (SCF_SUCCESS);
2802 }
2803 
2804 /*
2805  * Writes a vector of properties to the specified fmri/property group.
2806  *
2807  * If this function fails to write a specific property, *badprop is set
2808  * to point at that property's entry in the properties array.
2809  *
2810  * One significant difference between this function and the
2811  * scf_read_propvec function is that for string types, pv_ptr is a
2812  * char *, not a char **.  This means that you can't write a propvec
2813  * you just read, but makes other uses (hopefully the majority) simpler.
2814  */
2815 int
2816 scf_write_propvec(const char *fmri, const char *pgname,
2817     scf_propvec_t *properties, scf_propvec_t **badprop)
2818 {
2819 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2820 	scf_service_t *s = scf_service_create(h);
2821 	scf_instance_t *inst = scf_instance_create(h);
2822 	scf_snapshot_t *snap = scf_snapshot_create(h);
2823 	scf_propertygroup_t *pg = scf_pg_create(h);
2824 	scf_property_t *p = scf_property_create(h);
2825 	scf_transaction_t *tx = scf_transaction_create(h);
2826 	scf_value_t **v = NULL;
2827 	scf_transaction_entry_t **e = NULL;
2828 	boolean_t instance = B_TRUE;
2829 	int i, n;
2830 	scf_propvec_t *prop;
2831 	int error = 0, ret;
2832 
2833 	n = count_props(properties);
2834 	v = calloc(n, sizeof (scf_value_t *));
2835 	e = calloc(n, sizeof (scf_transaction_entry_t *));
2836 
2837 	if (v == NULL || e == NULL) {
2838 		error = SCF_ERROR_NO_MEMORY;
2839 		goto out;
2840 	}
2841 
2842 	if (h == NULL || s == NULL || inst == NULL || pg == NULL || p == NULL ||
2843 	    tx == NULL)
2844 		goto scferror;
2845 
2846 	for (i = 0; i < n; i++) {
2847 		v[i] = scf_value_create(h);
2848 		e[i] = scf_entry_create(h);
2849 		if (v[i] == NULL || e[i] == NULL)
2850 			goto scferror;
2851 	}
2852 
2853 	if (scf_handle_decode_fmri(h, fmri, NULL, s, inst, NULL, NULL, 0)
2854 	    != SCF_SUCCESS)
2855 		goto scferror;
2856 
2857 	if (scf_instance_to_fmri(inst, NULL, 0) == -1) {
2858 		if (scf_error() != SCF_ERROR_NOT_SET)
2859 			goto scferror;
2860 		instance = B_FALSE;
2861 	}
2862 
2863 	if ((instance ? scf_instance_get_pg(inst, pgname, pg) :
2864 	    scf_service_get_pg(s, pgname, pg)) == -1)
2865 		goto scferror;
2866 
2867 top:
2868 	if (scf_transaction_start(tx, pg) == -1)
2869 		goto scferror;
2870 
2871 	for (prop = properties, i = 0; prop->pv_prop != NULL; prop++, i++) {
2872 		ret = scf_transaction_property_change(tx, e[i], prop->pv_prop,
2873 		    prop->pv_type);
2874 		if (ret == -1 && scf_error() == SCF_ERROR_NOT_FOUND)
2875 			ret = scf_transaction_property_new(tx, e[i],
2876 			    prop->pv_prop, prop->pv_type);
2877 
2878 		if (ret == -1) {
2879 			*badprop = prop;
2880 			goto scferror;
2881 		}
2882 
2883 		switch (prop->pv_type) {
2884 		case SCF_TYPE_BOOLEAN: {
2885 			boolean_t b = (prop->pv_aux != 0) ?
2886 			    (*(uint64_t *)prop->pv_ptr & prop->pv_aux) != 0 :
2887 			    *(boolean_t *)prop->pv_ptr;
2888 
2889 			scf_value_set_boolean(v[i], b ? 1 : 0);
2890 			break;
2891 		}
2892 		case SCF_TYPE_COUNT:
2893 			scf_value_set_count(v[i], *(uint64_t *)prop->pv_ptr);
2894 			break;
2895 		case SCF_TYPE_INTEGER:
2896 			scf_value_set_integer(v[i], *(int64_t *)prop->pv_ptr);
2897 			break;
2898 		case SCF_TYPE_TIME: {
2899 			scf_time_t *time = prop->pv_ptr;
2900 
2901 			ret = scf_value_set_time(v[i], time->t_seconds,
2902 			    time->t_ns);
2903 			break;
2904 		}
2905 		case SCF_TYPE_OPAQUE: {
2906 			scf_opaque_t *opaque = prop->pv_ptr;
2907 
2908 			ret = scf_value_set_opaque(v[i], opaque->so_addr,
2909 			    opaque->so_size);
2910 			break;
2911 		}
2912 		case SCF_TYPE_ASTRING:
2913 			ret = scf_value_set_astring(v[i],
2914 			    (const char *)prop->pv_ptr);
2915 			break;
2916 		default:
2917 			ret = scf_value_set_from_string(v[i], prop->pv_type,
2918 			    (const char *)prop->pv_ptr);
2919 		}
2920 
2921 		if (ret == -1 || scf_entry_add_value(e[i], v[i]) == -1) {
2922 			*badprop = prop;
2923 			goto scferror;
2924 		}
2925 	}
2926 
2927 	ret = scf_transaction_commit(tx);
2928 	if (ret == 1)
2929 		goto out;
2930 
2931 	if (ret == 0 && scf_pg_update(pg) != -1) {
2932 		scf_transaction_reset(tx);
2933 		goto top;
2934 	}
2935 
2936 scferror:
2937 	error = scf_error();
2938 
2939 out:
2940 	if (v != NULL) {
2941 		for (i = 0; i < n; i++)
2942 			scf_value_destroy(v[i]);
2943 		free(v);
2944 	}
2945 
2946 	if (e != NULL) {
2947 		for (i = 0; i < n; i++)
2948 			scf_entry_destroy(e[i]);
2949 		free(e);
2950 	}
2951 
2952 	scf_transaction_destroy(tx);
2953 	scf_property_destroy(p);
2954 	scf_pg_destroy(pg);
2955 	scf_snapshot_destroy(snap);
2956 	scf_instance_destroy(inst);
2957 	scf_service_destroy(s);
2958 	scf_handle_destroy(h);
2959 
2960 	if (error != 0) {
2961 		(void) scf_set_error(error);
2962 		return (SCF_FAILED);
2963 	}
2964 
2965 	return (SCF_SUCCESS);
2966 }
2967 
2968 /*
2969  * Returns
2970  *   0 - success
2971  *   ECONNABORTED - repository connection broken
2972  *   ECANCELED - inst was deleted
2973  *   EPERM
2974  *   EACCES
2975  *   EROFS
2976  *   ENOMEM
2977  */
2978 int
2979 scf_instance_delete_prop(scf_instance_t *inst, const char *pgname,
2980     const char *pname)
2981 {
2982 	scf_handle_t *h;
2983 	scf_propertygroup_t *pg;
2984 	scf_transaction_t *tx;
2985 	scf_transaction_entry_t *e;
2986 	int error = 0, ret = 1, r;
2987 
2988 	h = scf_instance_handle(inst);
2989 
2990 	if ((pg = scf_pg_create(h)) == NULL) {
2991 		return (ENOMEM);
2992 	}
2993 
2994 	if (scf_instance_get_pg(inst, pgname, pg) != 0) {
2995 		error = scf_error();
2996 		scf_pg_destroy(pg);
2997 		switch (error) {
2998 		case SCF_ERROR_NOT_FOUND:
2999 			return (SCF_SUCCESS);
3000 
3001 		case SCF_ERROR_DELETED:
3002 			return (ECANCELED);
3003 
3004 		case SCF_ERROR_CONNECTION_BROKEN:
3005 		default:
3006 			return (ECONNABORTED);
3007 
3008 		case SCF_ERROR_NOT_SET:
3009 			bad_error("scf_instance_get_pg", scf_error());
3010 		}
3011 	}
3012 
3013 	tx = scf_transaction_create(h);
3014 	e = scf_entry_create(h);
3015 	if (tx == NULL || e == NULL) {
3016 		ret = ENOMEM;
3017 		goto out;
3018 	}
3019 
3020 	for (;;) {
3021 		if (scf_transaction_start(tx, pg) != 0) {
3022 			goto scferror;
3023 		}
3024 
3025 		if (scf_transaction_property_delete(tx, e, pname) != 0) {
3026 			goto scferror;
3027 		}
3028 
3029 		if ((r = scf_transaction_commit(tx)) == 1) {
3030 			ret = 0;
3031 			goto out;
3032 		}
3033 
3034 		if (r == -1) {
3035 			goto scferror;
3036 		}
3037 
3038 		scf_transaction_reset(tx);
3039 		if (scf_pg_update(pg) == -1) {
3040 			goto scferror;
3041 		}
3042 	}
3043 
3044 scferror:
3045 	switch (scf_error()) {
3046 	case SCF_ERROR_DELETED:
3047 	case SCF_ERROR_NOT_FOUND:
3048 		ret = 0;
3049 		break;
3050 
3051 	case SCF_ERROR_PERMISSION_DENIED:
3052 		ret = EPERM;
3053 		break;
3054 
3055 	case SCF_ERROR_BACKEND_ACCESS:
3056 		ret = EACCES;
3057 		break;
3058 
3059 	case SCF_ERROR_BACKEND_READONLY:
3060 		ret = EROFS;
3061 		break;
3062 
3063 	case SCF_ERROR_CONNECTION_BROKEN:
3064 	default:
3065 		ret = ECONNABORTED;
3066 		break;
3067 
3068 	case SCF_ERROR_HANDLE_MISMATCH:
3069 	case SCF_ERROR_INVALID_ARGUMENT:
3070 	case SCF_ERROR_NOT_BOUND:
3071 	case SCF_ERROR_NOT_SET:
3072 		bad_error("scf_instance_delete_prop", scf_error());
3073 	}
3074 
3075 out:
3076 	scf_transaction_destroy(tx);
3077 	scf_entry_destroy(e);
3078 	scf_pg_destroy(pg);
3079 
3080 	return (ret);
3081 }
3082 
3083 /*
3084  * Check the "application/auto_enable" property for the passed FMRI.
3085  * scf_simple_prop_get() should find the property on an instance
3086  * or on the service FMRI.  The routine returns:
3087  * -1: inconclusive (likely no such property or FMRI)
3088  *  0: auto_enable is false
3089  *  1: auto_enable is true
3090  */
3091 static int
3092 is_auto_enabled(char *fmri)
3093 {
3094 	scf_simple_prop_t *prop;
3095 	int retval = -1;
3096 	uint8_t *ret;
3097 
3098 	prop = scf_simple_prop_get(NULL, fmri, SCF_GROUP_APPLICATION,
3099 	    "auto_enable");
3100 	if (!prop)
3101 		return (retval);
3102 	ret = scf_simple_prop_next_boolean(prop);
3103 	retval = (*ret != 0);
3104 	scf_simple_prop_free(prop);
3105 	return (retval);
3106 }
3107 
3108 /*
3109  * Check an array of services and enable any that don't have the
3110  * "application/auto_enable" property set to "false", which is
3111  * the interface to turn off this behaviour (see PSARC 2004/739).
3112  */
3113 void
3114 _check_services(char **svcs)
3115 {
3116 	char *s;
3117 
3118 	for (; *svcs; svcs++) {
3119 		if (is_auto_enabled(*svcs) == 0)
3120 			continue;
3121 		if ((s = smf_get_state(*svcs)) != NULL) {
3122 			if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
3123 				(void) smf_enable_instance(*svcs,
3124 				    SMF_TEMPORARY);
3125 			free(s);
3126 		}
3127 	}
3128 }
3129 
3130 /*ARGSUSED*/
3131 static int
3132 str_compare(const char *s1, const char *s2, size_t n)
3133 {
3134 	return (strcmp(s1, s2));
3135 }
3136 
3137 static int
3138 str_n_compare(const char *s1, const char *s2, size_t n)
3139 {
3140 	return (strncmp(s1, s2, n));
3141 }
3142 
3143 int32_t
3144 state_from_string(const char *state, size_t l)
3145 {
3146 	int (*str_cmp)(const char *, const char *, size_t);
3147 
3148 	if (l == 0)
3149 		str_cmp = str_compare;
3150 	else
3151 		str_cmp = str_n_compare;
3152 
3153 	if (str_cmp(SCF_STATE_STRING_UNINIT, state, l) == 0)
3154 		return (SCF_STATE_UNINIT);
3155 	else if (str_cmp(SCF_STATE_STRING_MAINT, state, l) == 0)
3156 		return (SCF_STATE_MAINT);
3157 	else if (str_cmp(SCF_STATE_STRING_OFFLINE, state, l) == 0)
3158 		return (SCF_STATE_OFFLINE);
3159 	else if (str_cmp(SCF_STATE_STRING_DISABLED, state, l) == 0)
3160 		return (SCF_STATE_DISABLED);
3161 	else if (str_cmp(SCF_STATE_STRING_ONLINE, state, l) == 0)
3162 		return (SCF_STATE_ONLINE);
3163 	else if (str_cmp(SCF_STATE_STRING_DEGRADED, state, l) == 0)
3164 		return (SCF_STATE_DEGRADED);
3165 	else if (str_cmp("all", state, l) == 0)
3166 		return (SCF_STATE_ALL);
3167 	else
3168 		return (-1);
3169 }
3170 
3171 /*
3172  * int32_t smf_state_from_string()
3173  * return the value of the macro SCF_STATE_* for the corresponding state
3174  * it returns SCF_STATE_ALL if "all" is passed. -1 if the string passed doesn't
3175  * correspond to any valid state.
3176  */
3177 int32_t
3178 smf_state_from_string(const char *state)
3179 {
3180 	return (state_from_string(state, 0));
3181 }
3182 
3183 /*
3184  * smf_state_to_string()
3185  * Takes an int32_t representing an SMF state and returns
3186  * the corresponding string. The string is read only and need not to be
3187  * freed.
3188  * returns NULL on invalid input.
3189  */
3190 const char *
3191 smf_state_to_string(int32_t s)
3192 {
3193 	switch (s) {
3194 	case SCF_STATE_UNINIT:
3195 		return (SCF_STATE_STRING_UNINIT);
3196 	case SCF_STATE_MAINT:
3197 		return (SCF_STATE_STRING_MAINT);
3198 	case SCF_STATE_OFFLINE:
3199 		return (SCF_STATE_STRING_OFFLINE);
3200 	case SCF_STATE_DISABLED:
3201 		return (SCF_STATE_STRING_DISABLED);
3202 	case SCF_STATE_ONLINE:
3203 		return (SCF_STATE_STRING_ONLINE);
3204 	case SCF_STATE_DEGRADED:
3205 		return (SCF_STATE_STRING_DEGRADED);
3206 	case SCF_STATE_ALL:
3207 		return ("all");
3208 	default:
3209 		return (NULL);
3210 	}
3211 }
3212