xref: /illumos-gate/usr/src/lib/libscf/common/midlevel.c (revision 33efde4275d24731ef87927237b0ffb0630b6b2d)
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
scf_simple_handle_destroy(scf_simple_handle_t * simple_h)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 *
assemble_fmri(scf_handle_t * h,const char * base,const char * pg,const char * prop)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 *
fill_prop(scf_property_t * prop,const char * pgname,const char * propname,scf_handle_t * h)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
insert_app_props(scf_iter_t * propiter,char * pgname,char * propname,struct scf_simple_pg * thispg,scf_property_t * prop,size_t namelen,scf_handle_t * h)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
transaction_property_set(scf_transaction_t * tx,scf_transaction_entry_t * e,const char * pname,scf_type_t ty)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
get_inst_enabled(const scf_instance_t * inst,const char * pgname)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
set_inst_enabled(const scf_instance_t * inst,uint8_t desired,const char * pgname,uint32_t pgflags,const char * comment)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
delete_inst_enabled(const scf_instance_t * inst,const char * pgname)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
set_inst_action_inst(scf_instance_t * inst,const char * action)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
set_inst_action(const char * fmri,const char * action)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
get_inst_state(scf_instance_t * inst,scf_handle_t * h)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
set_inst_enabled_atboot(scf_instance_t * inst,uint8_t desired,const char * comment)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
set_inst_enabled_flags(const char * fmri,int flags,uint8_t desired,const char * comment)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 *
get_instance_pg(scf_simple_handle_t * simple_h)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
smf_enable_instance(const char * fmri,int flags)1204 smf_enable_instance(const char *fmri, int flags)
1205 {
1206 	return (set_inst_enabled_flags(fmri, flags, B_TRUE, ""));
1207 }
1208 
1209 int
smf_disable_instance_with_comment(const char * fmri,int flags,const char * comment)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
smf_disable_instance(const char * fmri,int flags)1217 smf_disable_instance(const char *fmri, int flags)
1218 {
1219 	return (set_inst_enabled_flags(fmri, flags, B_FALSE, ""));
1220 }
1221 int
_smf_refresh_instance_i(scf_instance_t * inst)1222 _smf_refresh_instance_i(scf_instance_t *inst)
1223 {
1224 	return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
1225 }
1226 
1227 int
_smf_refresh_all_instances(scf_service_t * s)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
smf_refresh_instance(const char * instance)1257 smf_refresh_instance(const char *instance)
1258 {
1259 	return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
1260 }
1261 
1262 int
smf_restart_instance(const char * instance)1263 smf_restart_instance(const char *instance)
1264 {
1265 	return (set_inst_action(instance, SCF_PROPERTY_RESTART));
1266 }
1267 
1268 int
smf_maintain_instance(const char * instance,int flags)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
smf_degrade_instance(const char * instance,int flags)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
smf_restore_instance(const char * instance)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 *
smf_get_state(const char * instance)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 *
scf_general_pg_setup(const char * fmri,const char * pg_name)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 *
scf_transaction_setup(scf_simple_handle_t * simple_h)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
scf_transaction_restart(scf_simple_handle_t * simple_h,scf_transaction_t * tx)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
scf_read_count_property(scf_simple_handle_t * simple_h,char * prop_name,uint64_t * ret_count)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
scf_set_count_property(scf_transaction_t * trans,char * propname,uint64_t count,boolean_t create_flag)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
scf_simple_walk_instances(uint_t state_flags,void * private,int (* inst_callback)(scf_handle_t *,scf_instance_t *,void *))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 *
scf_simple_prop_get(scf_handle_t * hin,const char * instance,const char * pgname,const char * propname)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 	free(fmri_buf);
1798 	if (local_h)
1799 		scf_handle_destroy(h);
1800 	return (NULL);
1801 }
1802 
1803 
1804 void
scf_simple_prop_free(scf_simple_prop_t * prop)1805 scf_simple_prop_free(scf_simple_prop_t *prop)
1806 {
1807 	int i;
1808 
1809 	if (prop == NULL)
1810 		return;
1811 
1812 	free(prop->pr_propname);
1813 	free(prop->pr_pgname);
1814 	switch (prop->pr_type) {
1815 	case SCF_TYPE_OPAQUE: {
1816 		for (i = 0; i < prop->pr_numvalues; i++) {
1817 			free(prop->pr_vallist[i].pv_opaque.o_value);
1818 		}
1819 		break;
1820 	}
1821 	case SCF_TYPE_ASTRING:
1822 	case SCF_TYPE_USTRING:
1823 	case SCF_TYPE_HOST:
1824 	case SCF_TYPE_HOSTNAME:
1825 	case SCF_TYPE_NET_ADDR:
1826 	case SCF_TYPE_NET_ADDR_V4:
1827 	case SCF_TYPE_NET_ADDR_V6:
1828 	case SCF_TYPE_URI:
1829 	case SCF_TYPE_FMRI: {
1830 		for (i = 0; i < prop->pr_numvalues; i++) {
1831 			free(prop->pr_vallist[i].pv_str);
1832 		}
1833 		break;
1834 	}
1835 	default:
1836 		break;
1837 	}
1838 	free(prop->pr_vallist);
1839 	free(prop);
1840 }
1841 
1842 
1843 scf_simple_app_props_t *
scf_simple_app_props_get(scf_handle_t * hin,const char * inst_fmri)1844 scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
1845 {
1846 	scf_instance_t		*inst = NULL;
1847 	scf_service_t		*svc = NULL;
1848 	scf_propertygroup_t	*pg = NULL;
1849 	scf_property_t		*prop = NULL;
1850 	scf_simple_app_props_t	*ret = NULL;
1851 	scf_iter_t		*pgiter = NULL, *propiter = NULL;
1852 	struct scf_simple_pg	*thispg = NULL, *nextpg;
1853 	scf_simple_prop_t	*thisprop, *nextprop;
1854 	scf_handle_t		*h = NULL;
1855 	int			pgiter_ret, propiter_ret;
1856 	ssize_t			namelen;
1857 	char			*propname = NULL, *pgname = NULL, *sys_fmri;
1858 	uint8_t			found;
1859 	boolean_t		local_h = B_TRUE;
1860 
1861 	/* If the user passed in a handle, use it. */
1862 	if (hin != NULL) {
1863 		h = hin;
1864 		local_h = B_FALSE;
1865 	}
1866 
1867 	if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
1868 		return (NULL);
1869 
1870 	if (inst_fmri == NULL) {
1871 		if ((namelen = scf_myname(h, NULL, 0)) == -1) {
1872 			if (local_h)
1873 				scf_handle_destroy(h);
1874 			return (NULL);
1875 		}
1876 		if ((sys_fmri = malloc(namelen + 1)) == NULL) {
1877 			if (local_h)
1878 				scf_handle_destroy(h);
1879 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1880 			return (NULL);
1881 		}
1882 		if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
1883 			if (local_h)
1884 				scf_handle_destroy(h);
1885 			free(sys_fmri);
1886 			return (NULL);
1887 		}
1888 	} else {
1889 		if ((sys_fmri = strdup(inst_fmri)) == NULL) {
1890 			if (local_h)
1891 				scf_handle_destroy(h);
1892 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1893 			return (NULL);
1894 		}
1895 	}
1896 
1897 	namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
1898 	assert(namelen > 0);
1899 
1900 	if ((inst = scf_instance_create(h)) == NULL ||
1901 	    (svc = scf_service_create(h)) == NULL ||
1902 	    (pgiter = scf_iter_create(h)) == NULL ||
1903 	    (propiter = scf_iter_create(h)) == NULL ||
1904 	    (pg = scf_pg_create(h)) == NULL ||
1905 	    (prop = scf_property_create(h)) == NULL) {
1906 		free(sys_fmri);
1907 		goto error2;
1908 	}
1909 
1910 	if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
1911 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
1912 		free(sys_fmri);
1913 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
1914 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1915 		goto error2;
1916 	}
1917 
1918 	if ((ret = malloc(sizeof (*ret))) == NULL ||
1919 	    (thispg = malloc(sizeof (*thispg))) == NULL ||
1920 	    (propname = malloc(namelen)) == NULL ||
1921 	    (pgname = malloc(namelen)) == NULL) {
1922 		free(thispg);
1923 		free(ret);
1924 		free(sys_fmri);
1925 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1926 		goto error2;
1927 	}
1928 
1929 	ret->ap_fmri = sys_fmri;
1930 	thispg->pg_name = NULL;
1931 	thispg->pg_proplist = NULL;
1932 	thispg->pg_next = NULL;
1933 	ret->ap_pglist = thispg;
1934 
1935 	if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
1936 	    0) {
1937 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
1938 			(void) scf_set_error(SCF_ERROR_INTERNAL);
1939 		goto error1;
1940 	}
1941 
1942 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
1943 		if (thispg->pg_name != NULL) {
1944 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
1945 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1946 				goto error1;
1947 			}
1948 			nextpg->pg_name = NULL;
1949 			nextpg->pg_next = NULL;
1950 			nextpg->pg_proplist = NULL;
1951 			thispg->pg_next = nextpg;
1952 			thispg = nextpg;
1953 		} else {
1954 			/* This is the first iteration */
1955 			nextpg = thispg;
1956 		}
1957 
1958 		if ((nextpg->pg_name = malloc(namelen)) == NULL) {
1959 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1960 			goto error1;
1961 		}
1962 
1963 		if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
1964 			if (scf_error() == SCF_ERROR_NOT_SET)
1965 				(void) scf_set_error(SCF_ERROR_INTERNAL);
1966 			goto error1;
1967 		}
1968 
1969 		thisprop = NULL;
1970 
1971 		scf_iter_reset(propiter);
1972 
1973 		if (scf_iter_pg_properties(propiter, pg) != 0) {
1974 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
1975 				(void) scf_set_error(SCF_ERROR_INTERNAL);
1976 			goto error1;
1977 		}
1978 
1979 		while ((propiter_ret = scf_iter_next_property(propiter, prop))
1980 		    == 1) {
1981 			if (scf_property_get_name(prop, propname, namelen) <
1982 			    0) {
1983 				if (scf_error() == SCF_ERROR_NOT_SET)
1984 					(void) scf_set_error(
1985 					    SCF_ERROR_INTERNAL);
1986 				goto error1;
1987 			}
1988 			if (thisprop != NULL) {
1989 				if ((nextprop = fill_prop(prop,
1990 				    nextpg->pg_name, propname, h)) == NULL)
1991 					goto error1;
1992 				thisprop->pr_next = nextprop;
1993 				thisprop = nextprop;
1994 			} else {
1995 				/* This is the first iteration */
1996 				if ((thisprop = fill_prop(prop,
1997 				    nextpg->pg_name, propname, h)) == NULL)
1998 					goto error1;
1999 				nextpg->pg_proplist = thisprop;
2000 				nextprop = thisprop;
2001 			}
2002 			nextprop->pr_pg = nextpg;
2003 			nextprop->pr_next = NULL;
2004 		}
2005 
2006 		if (propiter_ret == -1) {
2007 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2008 				(void) scf_set_error(SCF_ERROR_INTERNAL);
2009 			goto error1;
2010 		}
2011 	}
2012 
2013 	if (pgiter_ret == -1) {
2014 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2015 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2016 		goto error1;
2017 	}
2018 
2019 	/*
2020 	 * At this point, we've filled the scf_simple_app_props_t with all the
2021 	 * properties at the service level.  Now we iterate over all the
2022 	 * properties at the instance level, overwriting any duplicate
2023 	 * properties, in order to provide service/instance composition.
2024 	 */
2025 
2026 	scf_iter_reset(pgiter);
2027 	scf_iter_reset(propiter);
2028 
2029 	if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
2030 	    != 0) {
2031 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2032 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2033 		goto error1;
2034 	}
2035 
2036 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
2037 
2038 		thispg = ret->ap_pglist;
2039 		found = 0;
2040 
2041 		/*
2042 		 * Find either the end of the list, so we can append the
2043 		 * property group, or an existing property group that matches
2044 		 * it, so we can insert/overwrite its properties.
2045 		 */
2046 
2047 		if (scf_pg_get_name(pg, pgname, namelen) < 0) {
2048 			if (scf_error() == SCF_ERROR_NOT_SET)
2049 				(void) scf_set_error(SCF_ERROR_INTERNAL);
2050 			goto error1;
2051 		}
2052 
2053 		while ((thispg != NULL) && (thispg->pg_name != NULL)) {
2054 			if (strcmp(thispg->pg_name, pgname) == 0) {
2055 				found = 1;
2056 				break;
2057 			}
2058 			if (thispg->pg_next == NULL)
2059 				break;
2060 
2061 			thispg = thispg->pg_next;
2062 		}
2063 
2064 		scf_iter_reset(propiter);
2065 
2066 		if (scf_iter_pg_properties(propiter, pg) != 0) {
2067 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2068 				(void) scf_set_error(SCF_ERROR_INTERNAL);
2069 			goto error1;
2070 		}
2071 
2072 		if (found) {
2073 			/*
2074 			 * insert_app_props inserts or overwrites the
2075 			 * properties in thispg.
2076 			 */
2077 
2078 			if (insert_app_props(propiter, pgname, propname,
2079 			    thispg, prop, namelen, h) == -1)
2080 				goto error1;
2081 
2082 		} else {
2083 			/*
2084 			 * If the property group wasn't found, we're adding
2085 			 * a newly allocated property group to the end of the
2086 			 * list.
2087 			 */
2088 
2089 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
2090 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2091 				goto error1;
2092 			}
2093 			nextpg->pg_next = NULL;
2094 			nextpg->pg_proplist = NULL;
2095 			thisprop = NULL;
2096 
2097 			if ((nextpg->pg_name = strdup(pgname)) == NULL) {
2098 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2099 				free(nextpg);
2100 				goto error1;
2101 			}
2102 
2103 			if (thispg->pg_name == NULL) {
2104 				free(thispg);
2105 				ret->ap_pglist = nextpg;
2106 			} else {
2107 				thispg->pg_next = nextpg;
2108 			}
2109 
2110 			while ((propiter_ret =
2111 			    scf_iter_next_property(propiter, prop)) == 1) {
2112 				if (scf_property_get_name(prop, propname,
2113 				    namelen) < 0) {
2114 					if (scf_error() == SCF_ERROR_NOT_SET)
2115 						(void) scf_set_error(
2116 						    SCF_ERROR_INTERNAL);
2117 					goto error1;
2118 				}
2119 				if (thisprop != NULL) {
2120 					if ((nextprop = fill_prop(prop,
2121 					    pgname, propname, h)) ==
2122 					    NULL)
2123 						goto error1;
2124 					thisprop->pr_next = nextprop;
2125 					thisprop = nextprop;
2126 				} else {
2127 					/* This is the first iteration */
2128 					if ((thisprop = fill_prop(prop,
2129 					    pgname, propname, h)) ==
2130 					    NULL)
2131 						goto error1;
2132 					nextpg->pg_proplist = thisprop;
2133 					nextprop = thisprop;
2134 				}
2135 				nextprop->pr_pg = nextpg;
2136 				nextprop->pr_next = NULL;
2137 			}
2138 
2139 			if (propiter_ret == -1) {
2140 				if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2141 					(void) scf_set_error(
2142 					    SCF_ERROR_INTERNAL);
2143 				goto error1;
2144 			}
2145 		}
2146 
2147 	}
2148 
2149 	if (pgiter_ret == -1) {
2150 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2151 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2152 		goto error1;
2153 	}
2154 
2155 	if (ret->ap_pglist->pg_name == NULL)
2156 		goto error1;
2157 
2158 	scf_iter_destroy(pgiter);
2159 	scf_iter_destroy(propiter);
2160 	scf_pg_destroy(pg);
2161 	scf_property_destroy(prop);
2162 	scf_instance_destroy(inst);
2163 	scf_service_destroy(svc);
2164 	free(propname);
2165 	free(pgname);
2166 	if (local_h)
2167 		scf_handle_destroy(h);
2168 
2169 	return (ret);
2170 
2171 	/*
2172 	 * Exit point for a successful call.  Below this line are exit points
2173 	 * for failures at various stages during the function.
2174 	 */
2175 
2176 error1:
2177 	scf_simple_app_props_free(ret);
2178 
2179 error2:
2180 	scf_iter_destroy(pgiter);
2181 	scf_iter_destroy(propiter);
2182 	scf_pg_destroy(pg);
2183 	scf_property_destroy(prop);
2184 	scf_instance_destroy(inst);
2185 	scf_service_destroy(svc);
2186 	free(propname);
2187 	free(pgname);
2188 	if (local_h)
2189 		scf_handle_destroy(h);
2190 	return (NULL);
2191 }
2192 
2193 
2194 void
scf_simple_app_props_free(scf_simple_app_props_t * propblock)2195 scf_simple_app_props_free(scf_simple_app_props_t *propblock)
2196 {
2197 	struct scf_simple_pg	*pgthis, *pgnext;
2198 	scf_simple_prop_t	*propthis, *propnext;
2199 
2200 	if ((propblock == NULL) || (propblock->ap_pglist == NULL))
2201 		return;
2202 
2203 	for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
2204 		pgnext = pgthis->pg_next;
2205 
2206 		propthis = pgthis->pg_proplist;
2207 
2208 		while (propthis != NULL) {
2209 			propnext = propthis->pr_next;
2210 			scf_simple_prop_free(propthis);
2211 			propthis = propnext;
2212 		}
2213 
2214 		free(pgthis->pg_name);
2215 		free(pgthis);
2216 	}
2217 
2218 	free(propblock->ap_fmri);
2219 	free(propblock);
2220 }
2221 
2222 const scf_simple_prop_t *
scf_simple_app_props_next(const scf_simple_app_props_t * propblock,scf_simple_prop_t * last)2223 scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
2224     scf_simple_prop_t *last)
2225 {
2226 	struct scf_simple_pg	*this;
2227 
2228 	if (propblock == NULL) {
2229 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2230 		return (NULL);
2231 	}
2232 
2233 	this = propblock->ap_pglist;
2234 
2235 	/*
2236 	 * We're looking for the first property in this block if last is
2237 	 * NULL
2238 	 */
2239 
2240 	if (last == NULL) {
2241 		/* An empty pglist is legal, it just means no properties */
2242 		if (this == NULL) {
2243 			(void) scf_set_error(SCF_ERROR_NONE);
2244 			return (NULL);
2245 		}
2246 		/*
2247 		 * Walk until we find a pg with a property in it, or we run
2248 		 * out of property groups.
2249 		 */
2250 		while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2251 			this = this->pg_next;
2252 
2253 		if (this->pg_proplist == NULL) {
2254 			(void) scf_set_error(SCF_ERROR_NONE);
2255 			return (NULL);
2256 		}
2257 
2258 		return (this->pg_proplist);
2259 
2260 	}
2261 	/*
2262 	 * If last isn't NULL, then return the next prop in the property group,
2263 	 * or walk the property groups until we find another property, or
2264 	 * run out of property groups.
2265 	 */
2266 	if (last->pr_next != NULL)
2267 		return (last->pr_next);
2268 
2269 	if (last->pr_pg->pg_next == NULL) {
2270 		(void) scf_set_error(SCF_ERROR_NONE);
2271 		return (NULL);
2272 	}
2273 
2274 	this = last->pr_pg->pg_next;
2275 
2276 	while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2277 		this = this->pg_next;
2278 
2279 	if (this->pg_proplist == NULL) {
2280 		(void) scf_set_error(SCF_ERROR_NONE);
2281 		return (NULL);
2282 	}
2283 
2284 	return (this->pg_proplist);
2285 }
2286 
2287 const scf_simple_prop_t *
scf_simple_app_props_search(const scf_simple_app_props_t * propblock,const char * pgname,const char * propname)2288 scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
2289     const char *pgname, const char *propname)
2290 {
2291 	struct scf_simple_pg	*pg;
2292 	scf_simple_prop_t	*prop;
2293 
2294 	if ((propblock == NULL) || (propname == NULL)) {
2295 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2296 		return (NULL);
2297 	}
2298 
2299 	pg = propblock->ap_pglist;
2300 
2301 	/*
2302 	 * If pgname is NULL, we're searching the default application
2303 	 * property group, otherwise we look for the specified group.
2304 	 */
2305 	if (pgname == NULL) {
2306 		while ((pg != NULL) &&
2307 		    (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
2308 			pg = pg->pg_next;
2309 	} else {
2310 		while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
2311 			pg = pg->pg_next;
2312 	}
2313 
2314 	if (pg == NULL) {
2315 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
2316 		return (NULL);
2317 	}
2318 
2319 	prop = pg->pg_proplist;
2320 
2321 	while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
2322 		prop = prop->pr_next;
2323 
2324 	if (prop == NULL) {
2325 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
2326 		return (NULL);
2327 	}
2328 
2329 	return (prop);
2330 }
2331 
2332 void
scf_simple_prop_next_reset(scf_simple_prop_t * prop)2333 scf_simple_prop_next_reset(scf_simple_prop_t *prop)
2334 {
2335 	if (prop == NULL)
2336 		return;
2337 	prop->pr_iter = 0;
2338 }
2339 
2340 ssize_t
scf_simple_prop_numvalues(const scf_simple_prop_t * prop)2341 scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
2342 {
2343 	if (prop == NULL)
2344 		return (scf_set_error(SCF_ERROR_NOT_SET));
2345 
2346 	return (prop->pr_numvalues);
2347 }
2348 
2349 
2350 scf_type_t
scf_simple_prop_type(const scf_simple_prop_t * prop)2351 scf_simple_prop_type(const scf_simple_prop_t *prop)
2352 {
2353 	if (prop == NULL)
2354 		return (scf_set_error(SCF_ERROR_NOT_SET));
2355 
2356 	return (prop->pr_type);
2357 }
2358 
2359 
2360 char *
scf_simple_prop_name(const scf_simple_prop_t * prop)2361 scf_simple_prop_name(const scf_simple_prop_t *prop)
2362 {
2363 	if ((prop == NULL) || (prop->pr_propname == NULL)) {
2364 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2365 		return (NULL);
2366 	}
2367 
2368 	return (prop->pr_propname);
2369 }
2370 
2371 
2372 char *
scf_simple_prop_pgname(const scf_simple_prop_t * prop)2373 scf_simple_prop_pgname(const scf_simple_prop_t *prop)
2374 {
2375 	if ((prop == NULL) || (prop->pr_pgname == NULL)) {
2376 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2377 		return (NULL);
2378 	}
2379 
2380 	return (prop->pr_pgname);
2381 }
2382 
2383 
2384 static union scf_simple_prop_val *
scf_next_val(scf_simple_prop_t * prop,scf_type_t type)2385 scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
2386 {
2387 	if (prop == NULL) {
2388 		(void) scf_set_error(SCF_ERROR_NOT_SET);
2389 		return (NULL);
2390 	}
2391 
2392 	switch (prop->pr_type) {
2393 	case SCF_TYPE_USTRING:
2394 	case SCF_TYPE_HOST:
2395 	case SCF_TYPE_HOSTNAME:
2396 	case SCF_TYPE_NET_ADDR:
2397 	case SCF_TYPE_NET_ADDR_V4:
2398 	case SCF_TYPE_NET_ADDR_V6:
2399 	case SCF_TYPE_URI:
2400 	case SCF_TYPE_FMRI: {
2401 		if (type != SCF_TYPE_USTRING) {
2402 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2403 			return (NULL);
2404 		}
2405 		break;
2406 		}
2407 	default: {
2408 		if (type != prop->pr_type) {
2409 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2410 			return (NULL);
2411 		}
2412 		break;
2413 		}
2414 	}
2415 
2416 	if (prop->pr_iter >= prop->pr_numvalues) {
2417 		(void) scf_set_error(SCF_ERROR_NONE);
2418 		return (NULL);
2419 	}
2420 
2421 	return (&prop->pr_vallist[prop->pr_iter++]);
2422 }
2423 
2424 
2425 uint8_t *
scf_simple_prop_next_boolean(scf_simple_prop_t * prop)2426 scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
2427 {
2428 	union scf_simple_prop_val *ret;
2429 
2430 	ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
2431 
2432 	if (ret == NULL)
2433 		return (NULL);
2434 
2435 	return (&ret->pv_bool);
2436 }
2437 
2438 
2439 uint64_t *
scf_simple_prop_next_count(scf_simple_prop_t * prop)2440 scf_simple_prop_next_count(scf_simple_prop_t *prop)
2441 {
2442 	union scf_simple_prop_val *ret;
2443 
2444 	ret = scf_next_val(prop, SCF_TYPE_COUNT);
2445 
2446 	if (ret == NULL)
2447 		return (NULL);
2448 
2449 	return (&ret->pv_uint);
2450 }
2451 
2452 
2453 int64_t *
scf_simple_prop_next_integer(scf_simple_prop_t * prop)2454 scf_simple_prop_next_integer(scf_simple_prop_t *prop)
2455 {
2456 	union scf_simple_prop_val *ret;
2457 
2458 	ret = scf_next_val(prop, SCF_TYPE_INTEGER);
2459 
2460 	if (ret == NULL)
2461 		return (NULL);
2462 
2463 	return (&ret->pv_int);
2464 }
2465 
2466 int64_t *
scf_simple_prop_next_time(scf_simple_prop_t * prop,int32_t * nsec)2467 scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
2468 {
2469 	union scf_simple_prop_val *ret;
2470 
2471 	ret = scf_next_val(prop, SCF_TYPE_TIME);
2472 
2473 	if (ret == NULL)
2474 		return (NULL);
2475 
2476 	if (nsec != NULL)
2477 		*nsec = ret->pv_time.t_nsec;
2478 
2479 	return (&ret->pv_time.t_sec);
2480 }
2481 
2482 char *
scf_simple_prop_next_astring(scf_simple_prop_t * prop)2483 scf_simple_prop_next_astring(scf_simple_prop_t *prop)
2484 {
2485 	union scf_simple_prop_val *ret;
2486 
2487 	ret = scf_next_val(prop, SCF_TYPE_ASTRING);
2488 
2489 	if (ret == NULL)
2490 		return (NULL);
2491 
2492 	return (ret->pv_str);
2493 }
2494 
2495 char *
scf_simple_prop_next_ustring(scf_simple_prop_t * prop)2496 scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
2497 {
2498 	union scf_simple_prop_val *ret;
2499 
2500 	ret = scf_next_val(prop, SCF_TYPE_USTRING);
2501 
2502 	if (ret == NULL)
2503 		return (NULL);
2504 
2505 	return (ret->pv_str);
2506 }
2507 
2508 void *
scf_simple_prop_next_opaque(scf_simple_prop_t * prop,size_t * length)2509 scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
2510 {
2511 	union scf_simple_prop_val *ret;
2512 
2513 	ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
2514 
2515 	if (ret == NULL) {
2516 		*length = 0;
2517 		return (NULL);
2518 	}
2519 
2520 	*length = ret->pv_opaque.o_size;
2521 	return (ret->pv_opaque.o_value);
2522 }
2523 
2524 /*
2525  * Generate a filename based on the fmri and the given name and return
2526  * it in the buffer of MAXPATHLEN provided by the caller.
2527  * If temp_filename is non-zero, also generate a temporary, unique filename
2528  * and return it in the temp buffer of MAXPATHLEN provided by the caller.
2529  * The path to the generated pathname is also created.
2530  * Given fmri should begin with a scheme such as "svc:".
2531  * Returns
2532  *      0 on success
2533  *      -1 if filename would exceed MAXPATHLEN or
2534  *	-2 if unable to create directory to filename path
2535  */
2536 int
gen_filenms_from_fmri(const char * fmri,const char * name,char * filename,char * temp_filename)2537 gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
2538     char *temp_filename)
2539 {
2540 	int		len;
2541 
2542 	len = strlen(SMF_SPEEDY_FILES_PATH);
2543 	len += strlen(fmri);
2544 	len += 2;			/* for slash and null */
2545 	len += strlen(name);
2546 	len += 6;			/* For X's needed for mkstemp */
2547 
2548 	if (len > MAXPATHLEN)
2549 		return (-1);
2550 
2551 	/* Construct directory name first - speedy path ends in slash */
2552 	(void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
2553 	(void) strcat(filename, fmri);
2554 	if (mkdirp(filename, 0755) == -1) {
2555 		/* errno is set */
2556 		if (errno != EEXIST)
2557 			return (-2);
2558 	}
2559 
2560 	(void) strcat(filename, "/");
2561 	(void) strcat(filename, name);
2562 
2563 	if (temp_filename) {
2564 		(void) strcpy(temp_filename, filename);
2565 		(void) strcat(temp_filename, "XXXXXX");
2566 	}
2567 
2568 	return (0);
2569 }
2570 
2571 scf_type_t
scf_true_base_type(scf_type_t type)2572 scf_true_base_type(scf_type_t type)
2573 {
2574 	scf_type_t base = type;
2575 
2576 	do {
2577 		type = base;
2578 		(void) scf_type_base_type(type, &base);
2579 	} while (base != type);
2580 
2581 	return (base);
2582 }
2583 
2584 /*
2585  * Convenience routine which frees all strings and opaque data
2586  * allocated by scf_read_propvec.
2587  *
2588  * Like free(3C), this function preserves the value of errno.
2589  */
2590 void
scf_clean_propvec(scf_propvec_t * propvec)2591 scf_clean_propvec(scf_propvec_t *propvec)
2592 {
2593 	int saved_errno = errno;
2594 	scf_propvec_t *prop;
2595 
2596 	for (prop = propvec; prop->pv_prop != NULL; prop++) {
2597 		assert(prop->pv_type != SCF_TYPE_INVALID);
2598 		if (prop->pv_type == SCF_TYPE_OPAQUE) {
2599 			scf_opaque_t *o = prop->pv_ptr;
2600 
2601 			if (o->so_addr != NULL)
2602 				free(o->so_addr);
2603 		} else if (scf_true_base_type(prop->pv_type) ==
2604 		    SCF_TYPE_ASTRING) {
2605 			if (*(char **)prop->pv_ptr != NULL)
2606 				free(*(char **)prop->pv_ptr);
2607 		}
2608 	}
2609 
2610 	errno = saved_errno;
2611 }
2612 
2613 static int
count_props(scf_propvec_t * props)2614 count_props(scf_propvec_t *props)
2615 {
2616 	int count = 0;
2617 
2618 	for (; props->pv_prop != NULL; props++)
2619 		count++;
2620 	return (count);
2621 }
2622 
2623 /*
2624  * Reads a vector of properties from the specified fmri/property group.
2625  * If 'running' is true, reads from the running snapshot instead of the
2626  * editing snapshot.
2627  *
2628  * For string types, a buffer is allocated using malloc(3C) to hold the
2629  * zero-terminated string, a pointer to which is stored in the
2630  * caller-provided char **.  It is the caller's responsbility to free
2631  * this string.  To simplify error handling, unread strings are
2632  * initialized to NULL.
2633  *
2634  * For opaque types, a buffer is allocated using malloc(3C) to hold the
2635  * opaque data.  A pointer to this buffer and its size are stored in
2636  * the caller-provided scf_opaque_t.  It is the caller's responsibility
2637  * to free this buffer.  To simplify error handling, the address fields
2638  * for unread opaque data are initialized to NULL.
2639  *
2640  * All other data is stored directly in caller-provided variables or
2641  * structures.
2642  *
2643  * If this function fails to read a specific property, *badprop is set
2644  * to point at that property's entry in the properties array.
2645  *
2646  * On all failures, all memory allocated by this function is freed.
2647  */
2648 int
scf_read_propvec(const char * fmri,const char * pgname,boolean_t running,scf_propvec_t * properties,scf_propvec_t ** badprop)2649 scf_read_propvec(const char *fmri, const char *pgname, boolean_t running,
2650     scf_propvec_t *properties, scf_propvec_t **badprop)
2651 {
2652 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2653 	scf_service_t *s = scf_service_create(h);
2654 	scf_instance_t *i = scf_instance_create(h);
2655 	scf_snapshot_t *snap = running ? scf_snapshot_create(h) : NULL;
2656 	scf_propertygroup_t *pg = scf_pg_create(h);
2657 	scf_property_t *p = scf_property_create(h);
2658 	scf_value_t *v = scf_value_create(h);
2659 	boolean_t instance = B_TRUE;
2660 	scf_propvec_t *prop;
2661 	int error = 0;
2662 
2663 	for (prop = properties; prop->pv_prop != NULL; prop++) {
2664 		if (prop->pv_type == SCF_TYPE_OPAQUE)
2665 			((scf_opaque_t *)prop->pv_ptr)->so_addr = NULL;
2666 		else if (scf_true_base_type(prop->pv_type) == SCF_TYPE_ASTRING)
2667 			*((char **)prop->pv_ptr) = NULL;
2668 	}
2669 
2670 	if (h == NULL || s == NULL || i == NULL || (running && snap == NULL) ||
2671 	    pg == NULL || p == NULL || v == NULL)
2672 		goto scferror;
2673 
2674 	if (scf_handle_decode_fmri(h, fmri, NULL, s, i, NULL, NULL, 0) == -1)
2675 		goto scferror;
2676 
2677 	if (scf_instance_to_fmri(i, NULL, 0) == -1) {
2678 		if (scf_error() != SCF_ERROR_NOT_SET)
2679 			goto scferror;
2680 		instance = B_FALSE;
2681 	}
2682 
2683 	if (running) {
2684 		if (!instance) {
2685 			error = SCF_ERROR_TYPE_MISMATCH;
2686 			goto out;
2687 		}
2688 
2689 		if (scf_instance_get_snapshot(i, "running", snap) !=
2690 		    SCF_SUCCESS)
2691 			goto scferror;
2692 	}
2693 
2694 	if ((instance ? scf_instance_get_pg_composed(i, snap, pgname, pg) :
2695 	    scf_service_get_pg(s, pgname, pg)) == -1)
2696 		goto scferror;
2697 
2698 	for (prop = properties; prop->pv_prop != NULL; prop++) {
2699 		int ret = 0;
2700 
2701 		if (scf_pg_get_property(pg, prop->pv_prop, p) == -1 ||
2702 		    scf_property_get_value(p, v) == -1) {
2703 			*badprop = prop;
2704 			goto scferror;
2705 		}
2706 		switch (prop->pv_type) {
2707 		case SCF_TYPE_BOOLEAN: {
2708 			uint8_t b;
2709 
2710 			ret = scf_value_get_boolean(v, &b);
2711 			if (ret == -1)
2712 				break;
2713 			if (prop->pv_aux != 0) {
2714 				uint64_t *bits = prop->pv_ptr;
2715 				*bits = b ? (*bits | prop->pv_aux) :
2716 				    (*bits & ~prop->pv_aux);
2717 			} else {
2718 				boolean_t *bool = prop->pv_ptr;
2719 				*bool = b ? B_TRUE : B_FALSE;
2720 			}
2721 			break;
2722 		}
2723 		case SCF_TYPE_COUNT:
2724 			ret = scf_value_get_count(v, prop->pv_ptr);
2725 			break;
2726 		case SCF_TYPE_INTEGER:
2727 			ret = scf_value_get_integer(v, prop->pv_ptr);
2728 			break;
2729 		case SCF_TYPE_TIME: {
2730 			scf_time_t *time = prop->pv_ptr;
2731 
2732 			ret = scf_value_get_time(v, &time->t_seconds,
2733 			    &time->t_ns);
2734 			break;
2735 		}
2736 		case SCF_TYPE_OPAQUE: {
2737 			scf_opaque_t *opaque = prop->pv_ptr;
2738 			ssize_t size = scf_value_get_opaque(v, NULL, 0);
2739 
2740 			if (size == -1) {
2741 				*badprop = prop;
2742 				goto scferror;
2743 			}
2744 			if ((opaque->so_addr = malloc(size)) == NULL) {
2745 				error = SCF_ERROR_NO_MEMORY;
2746 				goto out;
2747 			}
2748 			opaque->so_size = size;
2749 			ret = scf_value_get_opaque(v, opaque->so_addr, size);
2750 			break;
2751 		}
2752 		default: {
2753 			char *s;
2754 			ssize_t size;
2755 
2756 			assert(scf_true_base_type(prop->pv_type) ==
2757 			    SCF_TYPE_ASTRING);
2758 
2759 			size = scf_value_get_astring(v, NULL, 0);
2760 			if (size == -1) {
2761 				*badprop = prop;
2762 				goto scferror;
2763 			}
2764 			if ((s = malloc(++size)) == NULL) {
2765 				error = SCF_ERROR_NO_MEMORY;
2766 				goto out;
2767 			}
2768 			ret = scf_value_get_astring(v, s, size);
2769 			*(char **)prop->pv_ptr = s;
2770 		}
2771 
2772 		if (ret == -1) {
2773 			*badprop = prop;
2774 			goto scferror;
2775 		}
2776 
2777 		}
2778 	}
2779 
2780 	goto out;
2781 
2782 scferror:
2783 	error = scf_error();
2784 	scf_clean_propvec(properties);
2785 
2786 out:
2787 	scf_value_destroy(v);
2788 	scf_property_destroy(p);
2789 	scf_pg_destroy(pg);
2790 	scf_snapshot_destroy(snap);
2791 	scf_instance_destroy(i);
2792 	scf_service_destroy(s);
2793 	scf_handle_destroy(h);
2794 
2795 	if (error != 0) {
2796 		(void) scf_set_error(error);
2797 		return (SCF_FAILED);
2798 	}
2799 
2800 	return (SCF_SUCCESS);
2801 }
2802 
2803 /*
2804  * Writes a vector of properties to the specified fmri/property group.
2805  *
2806  * If this function fails to write a specific property, *badprop is set
2807  * to point at that property's entry in the properties array.
2808  *
2809  * One significant difference between this function and the
2810  * scf_read_propvec function is that for string types, pv_ptr is a
2811  * char *, not a char **.  This means that you can't write a propvec
2812  * you just read, but makes other uses (hopefully the majority) simpler.
2813  */
2814 int
scf_write_propvec(const char * fmri,const char * pgname,scf_propvec_t * properties,scf_propvec_t ** badprop)2815 scf_write_propvec(const char *fmri, const char *pgname,
2816     scf_propvec_t *properties, scf_propvec_t **badprop)
2817 {
2818 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2819 	scf_service_t *s = scf_service_create(h);
2820 	scf_instance_t *inst = scf_instance_create(h);
2821 	scf_snapshot_t *snap = scf_snapshot_create(h);
2822 	scf_propertygroup_t *pg = scf_pg_create(h);
2823 	scf_property_t *p = scf_property_create(h);
2824 	scf_transaction_t *tx = scf_transaction_create(h);
2825 	scf_value_t **v = NULL;
2826 	scf_transaction_entry_t **e = NULL;
2827 	boolean_t instance = B_TRUE;
2828 	int i, n;
2829 	scf_propvec_t *prop;
2830 	int error = 0, ret;
2831 
2832 	n = count_props(properties);
2833 	v = calloc(n, sizeof (scf_value_t *));
2834 	e = calloc(n, sizeof (scf_transaction_entry_t *));
2835 
2836 	if (v == NULL || e == NULL) {
2837 		error = SCF_ERROR_NO_MEMORY;
2838 		goto out;
2839 	}
2840 
2841 	if (h == NULL || s == NULL || inst == NULL || pg == NULL || p == NULL ||
2842 	    tx == NULL)
2843 		goto scferror;
2844 
2845 	for (i = 0; i < n; i++) {
2846 		v[i] = scf_value_create(h);
2847 		e[i] = scf_entry_create(h);
2848 		if (v[i] == NULL || e[i] == NULL)
2849 			goto scferror;
2850 	}
2851 
2852 	if (scf_handle_decode_fmri(h, fmri, NULL, s, inst, NULL, NULL, 0)
2853 	    != SCF_SUCCESS)
2854 		goto scferror;
2855 
2856 	if (scf_instance_to_fmri(inst, NULL, 0) == -1) {
2857 		if (scf_error() != SCF_ERROR_NOT_SET)
2858 			goto scferror;
2859 		instance = B_FALSE;
2860 	}
2861 
2862 	if ((instance ? scf_instance_get_pg(inst, pgname, pg) :
2863 	    scf_service_get_pg(s, pgname, pg)) == -1)
2864 		goto scferror;
2865 
2866 top:
2867 	if (scf_transaction_start(tx, pg) == -1)
2868 		goto scferror;
2869 
2870 	for (prop = properties, i = 0; prop->pv_prop != NULL; prop++, i++) {
2871 		ret = scf_transaction_property_change(tx, e[i], prop->pv_prop,
2872 		    prop->pv_type);
2873 		if (ret == -1 && scf_error() == SCF_ERROR_NOT_FOUND)
2874 			ret = scf_transaction_property_new(tx, e[i],
2875 			    prop->pv_prop, prop->pv_type);
2876 
2877 		if (ret == -1) {
2878 			*badprop = prop;
2879 			goto scferror;
2880 		}
2881 
2882 		switch (prop->pv_type) {
2883 		case SCF_TYPE_BOOLEAN: {
2884 			boolean_t b = (prop->pv_aux != 0) ?
2885 			    (*(uint64_t *)prop->pv_ptr & prop->pv_aux) != 0 :
2886 			    *(boolean_t *)prop->pv_ptr;
2887 
2888 			scf_value_set_boolean(v[i], b ? 1 : 0);
2889 			break;
2890 		}
2891 		case SCF_TYPE_COUNT:
2892 			scf_value_set_count(v[i], *(uint64_t *)prop->pv_ptr);
2893 			break;
2894 		case SCF_TYPE_INTEGER:
2895 			scf_value_set_integer(v[i], *(int64_t *)prop->pv_ptr);
2896 			break;
2897 		case SCF_TYPE_TIME: {
2898 			scf_time_t *time = prop->pv_ptr;
2899 
2900 			ret = scf_value_set_time(v[i], time->t_seconds,
2901 			    time->t_ns);
2902 			break;
2903 		}
2904 		case SCF_TYPE_OPAQUE: {
2905 			scf_opaque_t *opaque = prop->pv_ptr;
2906 
2907 			ret = scf_value_set_opaque(v[i], opaque->so_addr,
2908 			    opaque->so_size);
2909 			break;
2910 		}
2911 		case SCF_TYPE_ASTRING:
2912 			ret = scf_value_set_astring(v[i],
2913 			    (const char *)prop->pv_ptr);
2914 			break;
2915 		default:
2916 			ret = scf_value_set_from_string(v[i], prop->pv_type,
2917 			    (const char *)prop->pv_ptr);
2918 		}
2919 
2920 		if (ret == -1 || scf_entry_add_value(e[i], v[i]) == -1) {
2921 			*badprop = prop;
2922 			goto scferror;
2923 		}
2924 	}
2925 
2926 	ret = scf_transaction_commit(tx);
2927 	if (ret == 1)
2928 		goto out;
2929 
2930 	if (ret == 0 && scf_pg_update(pg) != -1) {
2931 		scf_transaction_reset(tx);
2932 		goto top;
2933 	}
2934 
2935 scferror:
2936 	error = scf_error();
2937 
2938 out:
2939 	if (v != NULL) {
2940 		for (i = 0; i < n; i++)
2941 			scf_value_destroy(v[i]);
2942 		free(v);
2943 	}
2944 
2945 	if (e != NULL) {
2946 		for (i = 0; i < n; i++)
2947 			scf_entry_destroy(e[i]);
2948 		free(e);
2949 	}
2950 
2951 	scf_transaction_destroy(tx);
2952 	scf_property_destroy(p);
2953 	scf_pg_destroy(pg);
2954 	scf_snapshot_destroy(snap);
2955 	scf_instance_destroy(inst);
2956 	scf_service_destroy(s);
2957 	scf_handle_destroy(h);
2958 
2959 	if (error != 0) {
2960 		(void) scf_set_error(error);
2961 		return (SCF_FAILED);
2962 	}
2963 
2964 	return (SCF_SUCCESS);
2965 }
2966 
2967 /*
2968  * Returns
2969  *   0 - success
2970  *   ECONNABORTED - repository connection broken
2971  *   ECANCELED - inst was deleted
2972  *   EPERM
2973  *   EACCES
2974  *   EROFS
2975  *   ENOMEM
2976  */
2977 int
scf_instance_delete_prop(scf_instance_t * inst,const char * pgname,const char * pname)2978 scf_instance_delete_prop(scf_instance_t *inst, const char *pgname,
2979     const char *pname)
2980 {
2981 	scf_handle_t *h;
2982 	scf_propertygroup_t *pg;
2983 	scf_transaction_t *tx;
2984 	scf_transaction_entry_t *e;
2985 	int error = 0, ret = 1, r;
2986 
2987 	h = scf_instance_handle(inst);
2988 
2989 	if ((pg = scf_pg_create(h)) == NULL) {
2990 		return (ENOMEM);
2991 	}
2992 
2993 	if (scf_instance_get_pg(inst, pgname, pg) != 0) {
2994 		error = scf_error();
2995 		scf_pg_destroy(pg);
2996 		switch (error) {
2997 		case SCF_ERROR_NOT_FOUND:
2998 			return (SCF_SUCCESS);
2999 
3000 		case SCF_ERROR_DELETED:
3001 			return (ECANCELED);
3002 
3003 		case SCF_ERROR_CONNECTION_BROKEN:
3004 		default:
3005 			return (ECONNABORTED);
3006 
3007 		case SCF_ERROR_NOT_SET:
3008 			bad_error("scf_instance_get_pg", scf_error());
3009 		}
3010 	}
3011 
3012 	tx = scf_transaction_create(h);
3013 	e = scf_entry_create(h);
3014 	if (tx == NULL || e == NULL) {
3015 		ret = ENOMEM;
3016 		goto out;
3017 	}
3018 
3019 	for (;;) {
3020 		if (scf_transaction_start(tx, pg) != 0) {
3021 			goto scferror;
3022 		}
3023 
3024 		if (scf_transaction_property_delete(tx, e, pname) != 0) {
3025 			goto scferror;
3026 		}
3027 
3028 		if ((r = scf_transaction_commit(tx)) == 1) {
3029 			ret = 0;
3030 			goto out;
3031 		}
3032 
3033 		if (r == -1) {
3034 			goto scferror;
3035 		}
3036 
3037 		scf_transaction_reset(tx);
3038 		if (scf_pg_update(pg) == -1) {
3039 			goto scferror;
3040 		}
3041 	}
3042 
3043 scferror:
3044 	switch (scf_error()) {
3045 	case SCF_ERROR_DELETED:
3046 	case SCF_ERROR_NOT_FOUND:
3047 		ret = 0;
3048 		break;
3049 
3050 	case SCF_ERROR_PERMISSION_DENIED:
3051 		ret = EPERM;
3052 		break;
3053 
3054 	case SCF_ERROR_BACKEND_ACCESS:
3055 		ret = EACCES;
3056 		break;
3057 
3058 	case SCF_ERROR_BACKEND_READONLY:
3059 		ret = EROFS;
3060 		break;
3061 
3062 	case SCF_ERROR_CONNECTION_BROKEN:
3063 	default:
3064 		ret = ECONNABORTED;
3065 		break;
3066 
3067 	case SCF_ERROR_HANDLE_MISMATCH:
3068 	case SCF_ERROR_INVALID_ARGUMENT:
3069 	case SCF_ERROR_NOT_BOUND:
3070 	case SCF_ERROR_NOT_SET:
3071 		bad_error("scf_instance_delete_prop", scf_error());
3072 	}
3073 
3074 out:
3075 	scf_transaction_destroy(tx);
3076 	scf_entry_destroy(e);
3077 	scf_pg_destroy(pg);
3078 
3079 	return (ret);
3080 }
3081 
3082 /*
3083  * Check the "application/auto_enable" property for the passed FMRI.
3084  * scf_simple_prop_get() should find the property on an instance
3085  * or on the service FMRI.  The routine returns:
3086  * -1: inconclusive (likely no such property or FMRI)
3087  *  0: auto_enable is false
3088  *  1: auto_enable is true
3089  */
3090 static int
is_auto_enabled(char * fmri)3091 is_auto_enabled(char *fmri)
3092 {
3093 	scf_simple_prop_t *prop;
3094 	int retval = -1;
3095 	uint8_t *ret;
3096 
3097 	prop = scf_simple_prop_get(NULL, fmri, SCF_GROUP_APPLICATION,
3098 	    "auto_enable");
3099 	if (!prop)
3100 		return (retval);
3101 	ret = scf_simple_prop_next_boolean(prop);
3102 	retval = (*ret != 0);
3103 	scf_simple_prop_free(prop);
3104 	return (retval);
3105 }
3106 
3107 /*
3108  * Check an array of services and enable any that don't have the
3109  * "application/auto_enable" property set to "false", which is
3110  * the interface to turn off this behaviour (see PSARC 2004/739).
3111  */
3112 void
_check_services(char ** svcs)3113 _check_services(char **svcs)
3114 {
3115 	char *s;
3116 
3117 	for (; *svcs; svcs++) {
3118 		if (is_auto_enabled(*svcs) == 0)
3119 			continue;
3120 		if ((s = smf_get_state(*svcs)) != NULL) {
3121 			if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
3122 				(void) smf_enable_instance(*svcs,
3123 				    SMF_TEMPORARY);
3124 			free(s);
3125 		}
3126 	}
3127 }
3128 
3129 /*ARGSUSED*/
3130 static int
str_compare(const char * s1,const char * s2,size_t n)3131 str_compare(const char *s1, const char *s2, size_t n)
3132 {
3133 	return (strcmp(s1, s2));
3134 }
3135 
3136 static int
str_n_compare(const char * s1,const char * s2,size_t n)3137 str_n_compare(const char *s1, const char *s2, size_t n)
3138 {
3139 	return (strncmp(s1, s2, n));
3140 }
3141 
3142 int32_t
state_from_string(const char * state,size_t l)3143 state_from_string(const char *state, size_t l)
3144 {
3145 	int (*str_cmp)(const char *, const char *, size_t);
3146 
3147 	if (l == 0)
3148 		str_cmp = str_compare;
3149 	else
3150 		str_cmp = str_n_compare;
3151 
3152 	if (str_cmp(SCF_STATE_STRING_UNINIT, state, l) == 0)
3153 		return (SCF_STATE_UNINIT);
3154 	else if (str_cmp(SCF_STATE_STRING_MAINT, state, l) == 0)
3155 		return (SCF_STATE_MAINT);
3156 	else if (str_cmp(SCF_STATE_STRING_OFFLINE, state, l) == 0)
3157 		return (SCF_STATE_OFFLINE);
3158 	else if (str_cmp(SCF_STATE_STRING_DISABLED, state, l) == 0)
3159 		return (SCF_STATE_DISABLED);
3160 	else if (str_cmp(SCF_STATE_STRING_ONLINE, state, l) == 0)
3161 		return (SCF_STATE_ONLINE);
3162 	else if (str_cmp(SCF_STATE_STRING_DEGRADED, state, l) == 0)
3163 		return (SCF_STATE_DEGRADED);
3164 	else if (str_cmp("all", state, l) == 0)
3165 		return (SCF_STATE_ALL);
3166 	else
3167 		return (-1);
3168 }
3169 
3170 /*
3171  * int32_t smf_state_from_string()
3172  * return the value of the macro SCF_STATE_* for the corresponding state
3173  * it returns SCF_STATE_ALL if "all" is passed. -1 if the string passed doesn't
3174  * correspond to any valid state.
3175  */
3176 int32_t
smf_state_from_string(const char * state)3177 smf_state_from_string(const char *state)
3178 {
3179 	return (state_from_string(state, 0));
3180 }
3181 
3182 /*
3183  * smf_state_to_string()
3184  * Takes an int32_t representing an SMF state and returns
3185  * the corresponding string. The string is read only and need not to be
3186  * freed.
3187  * returns NULL on invalid input.
3188  */
3189 const char *
smf_state_to_string(int32_t s)3190 smf_state_to_string(int32_t s)
3191 {
3192 	switch (s) {
3193 	case SCF_STATE_UNINIT:
3194 		return (SCF_STATE_STRING_UNINIT);
3195 	case SCF_STATE_MAINT:
3196 		return (SCF_STATE_STRING_MAINT);
3197 	case SCF_STATE_OFFLINE:
3198 		return (SCF_STATE_STRING_OFFLINE);
3199 	case SCF_STATE_DISABLED:
3200 		return (SCF_STATE_STRING_DISABLED);
3201 	case SCF_STATE_ONLINE:
3202 		return (SCF_STATE_STRING_ONLINE);
3203 	case SCF_STATE_DEGRADED:
3204 		return (SCF_STATE_STRING_DEGRADED);
3205 	case SCF_STATE_ALL:
3206 		return ("all");
3207 	default:
3208 		return (NULL);
3209 	}
3210 }
3211