Home | History | Annotate | Download | only in svccfg
      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 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 
     28 #include <alloca.h>
     29 #include <assert.h>
     30 #include <ctype.h>
     31 #include <door.h>
     32 #include <errno.h>
     33 #include <fcntl.h>
     34 #include <fnmatch.h>
     35 #include <inttypes.h>
     36 #include <libintl.h>
     37 #include <libscf.h>
     38 #include <libscf_priv.h>
     39 #include <libtecla.h>
     40 #include <libuutil.h>
     41 #include <limits.h>
     42 #include <locale.h>
     43 #include <stdarg.h>
     44 #include <string.h>
     45 #include <strings.h>
     46 #include <unistd.h>
     47 #include <wait.h>
     48 
     49 #include <libxml/tree.h>
     50 
     51 #include <sys/param.h>
     52 
     53 #include "svccfg.h"
     54 #include "manifest_hash.h"
     55 
     56 /* The colon namespaces in each entity (each followed by a newline). */
     57 #define	COLON_NAMESPACES	":properties\n"
     58 
     59 #define	TEMP_FILE_PATTERN	"/tmp/svccfg-XXXXXX"
     60 
     61 /* These are characters which the lexer requires to be in double-quotes. */
     62 #define	CHARS_TO_QUOTE		" \t\n\\>=\"()"
     63 
     64 #define	HASH_SIZE		16
     65 #define	HASH_SVC		"smf/manifest"
     66 #define	HASH_PG_TYPE		"framework"
     67 #define	HASH_PG_FLAGS		0
     68 #define	HASH_PROP		"md5sum"
     69 
     70 /*
     71  * Indentation used in the output of the describe subcommand.
     72  */
     73 #define	TMPL_VALUE_INDENT	"  "
     74 #define	TMPL_INDENT		"    "
     75 #define	TMPL_INDENT_2X		"        "
     76 #define	TMPL_CHOICE_INDENT	"      "
     77 
     78 /*
     79  * These are the classes of elements which may appear as children of service
     80  * or instance elements in XML manifests.
     81  */
     82 struct entity_elts {
     83 	xmlNodePtr	create_default_instance;
     84 	xmlNodePtr	single_instance;
     85 	xmlNodePtr	restarter;
     86 	xmlNodePtr	dependencies;
     87 	xmlNodePtr	dependents;
     88 	xmlNodePtr	method_context;
     89 	xmlNodePtr	exec_methods;
     90 	xmlNodePtr	property_groups;
     91 	xmlNodePtr	instances;
     92 	xmlNodePtr	stability;
     93 	xmlNodePtr	template;
     94 };
     95 
     96 /*
     97  * Likewise for property_group elements.
     98  */
     99 struct pg_elts {
    100 	xmlNodePtr	stability;
    101 	xmlNodePtr	propvals;
    102 	xmlNodePtr	properties;
    103 };
    104 
    105 /*
    106  * Likewise for template elements.
    107  */
    108 struct template_elts {
    109 	xmlNodePtr	common_name;
    110 	xmlNodePtr	description;
    111 	xmlNodePtr	documentation;
    112 };
    113 
    114 /*
    115  * This structure is for snaplevel lists.  They are convenient because libscf
    116  * only allows traversing snaplevels in one direction.
    117  */
    118 struct snaplevel {
    119 	uu_list_node_t	list_node;
    120 	scf_snaplevel_t	*sl;
    121 };
    122 
    123 /*
    124  * This is used for communication between lscf_service_export and
    125  * export_callback.
    126  */
    127 struct export_args {
    128 	const char	*filename;
    129 	int 		flags;
    130 };
    131 
    132 const char * const scf_pg_general = SCF_PG_GENERAL;
    133 const char * const scf_group_framework = SCF_GROUP_FRAMEWORK;
    134 const char * const scf_property_enabled = SCF_PROPERTY_ENABLED;
    135 const char * const scf_property_external = "external";
    136 
    137 const char * const snap_initial = "initial";
    138 const char * const snap_lastimport = "last-import";
    139 const char * const snap_previous = "previous";
    140 const char * const snap_running = "running";
    141 
    142 scf_handle_t *g_hndl = NULL;	/* only valid after lscf_prep_hndl() */
    143 
    144 ssize_t max_scf_fmri_len;
    145 ssize_t max_scf_name_len;
    146 ssize_t max_scf_pg_type_len;
    147 ssize_t max_scf_value_len;
    148 static size_t max_scf_len;
    149 
    150 static scf_scope_t *cur_scope;
    151 static scf_service_t *cur_svc = NULL;
    152 static scf_instance_t *cur_inst = NULL;
    153 static scf_snapshot_t *cur_snap = NULL;
    154 static scf_snaplevel_t *cur_level = NULL;
    155 
    156 static uu_list_pool_t *snaplevel_pool;
    157 /* cur_levels is the snaplevels of cur_snap, from least specific to most. */
    158 static uu_list_t *cur_levels;
    159 static struct snaplevel *cur_elt;		/* cur_elt->sl == cur_level */
    160 
    161 static FILE *tempfile = NULL;
    162 static char tempfilename[sizeof (TEMP_FILE_PATTERN)] = "";
    163 
    164 static const char *emsg_entity_not_selected;
    165 static const char *emsg_permission_denied;
    166 static const char *emsg_create_xml;
    167 static const char *emsg_cant_modify_snapshots;
    168 static const char *emsg_read_only;
    169 static const char *emsg_deleted;
    170 static const char *emsg_invalid_pg_name;
    171 static const char *emsg_invalid_prop_name;
    172 static const char *emsg_no_such_pg;
    173 static const char *emsg_fmri_invalid_pg_name;
    174 static const char *emsg_fmri_invalid_pg_name_type;
    175 static const char *emsg_pg_added;
    176 static const char *emsg_pg_changed;
    177 static const char *emsg_pg_deleted;
    178 static const char *emsg_pg_mod_perm;
    179 static const char *emsg_pg_add_perm;
    180 static const char *emsg_pg_del_perm;
    181 static const char *emsg_snap_perm;
    182 static const char *emsg_dpt_dangling;
    183 static const char *emsg_dpt_no_dep;
    184 
    185 static int li_only;
    186 static int no_refresh = 0;
    187 
    188 /* import globals, to minimize allocations */
    189 static scf_scope_t *imp_scope = NULL;
    190 static scf_service_t *imp_svc = NULL, *imp_tsvc = NULL;
    191 static scf_instance_t *imp_inst = NULL, *imp_tinst = NULL;
    192 static scf_snapshot_t *imp_snap = NULL, *imp_lisnap = NULL, *imp_tlisnap = NULL;
    193 static scf_snapshot_t *imp_rsnap = NULL;
    194 static scf_snaplevel_t *imp_snpl = NULL, *imp_rsnpl = NULL;
    195 static scf_propertygroup_t *imp_pg = NULL, *imp_pg2 = NULL;
    196 static scf_property_t *imp_prop = NULL;
    197 static scf_iter_t *imp_iter = NULL;
    198 static scf_iter_t *imp_rpg_iter = NULL;
    199 static scf_iter_t *imp_up_iter = NULL;
    200 static scf_transaction_t *imp_tx = NULL;	/* always reset this */
    201 static char *imp_str = NULL;
    202 static size_t imp_str_sz;
    203 static char *imp_tsname = NULL;
    204 static char *imp_fe1 = NULL;		/* for fmri_equal() */
    205 static char *imp_fe2 = NULL;
    206 static uu_list_t *imp_deleted_dpts = NULL;	/* pgroup_t's to refresh */
    207 
    208 /* upgrade_dependents() globals */
    209 static scf_instance_t *ud_inst = NULL;
    210 static scf_snaplevel_t *ud_snpl = NULL;
    211 static scf_propertygroup_t *ud_pg = NULL;
    212 static scf_propertygroup_t *ud_cur_depts_pg = NULL;
    213 static scf_propertygroup_t *ud_run_dpts_pg = NULL;
    214 static int ud_run_dpts_pg_set = 0;
    215 static scf_property_t *ud_prop = NULL;
    216 static scf_property_t *ud_dpt_prop = NULL;
    217 static scf_value_t *ud_val = NULL;
    218 static scf_iter_t *ud_iter = NULL, *ud_iter2 = NULL;
    219 static scf_transaction_t *ud_tx = NULL;
    220 static char *ud_ctarg = NULL;
    221 static char *ud_oldtarg = NULL;
    222 static char *ud_name = NULL;
    223 
    224 /* export globals */
    225 static scf_instance_t *exp_inst;
    226 static scf_propertygroup_t *exp_pg;
    227 static scf_property_t *exp_prop;
    228 static scf_value_t *exp_val;
    229 static scf_iter_t *exp_inst_iter, *exp_pg_iter, *exp_prop_iter, *exp_val_iter;
    230 static char *exp_str;
    231 static size_t exp_str_sz;
    232 
    233 static void scfdie_lineno(int lineno) __NORETURN;
    234 
    235 static char *start_method_names[] = {
    236 	"start",
    237 	"inetd_start",
    238 	NULL
    239 };
    240 
    241 static void
    242 safe_printf(const char *fmt, ...)
    243 {
    244 	va_list va;
    245 
    246 	va_start(va, fmt);
    247 	if (vprintf(fmt, va) < 0)
    248 		uu_die(gettext("Error writing to stdout"));
    249 	va_end(va);
    250 }
    251 
    252 /*
    253  * For unexpected libscf errors.
    254  */
    255 #ifdef NDEBUG
    256 
    257 static void scfdie(void) __NORETURN;
    258 
    259 static void
    260 scfdie(void)
    261 {
    262 	scf_error_t err = scf_error();
    263 
    264 	if (err == SCF_ERROR_CONNECTION_BROKEN)
    265 		uu_die(gettext("Repository connection broken.  Exiting.\n"));
    266 
    267 	uu_die(gettext("Unexpected fatal libscf error: %s.  Exiting.\n"),
    268 	    scf_strerror(err));
    269 }
    270 
    271 #else
    272 
    273 #define	scfdie()	scfdie_lineno(__LINE__)
    274 
    275 static void
    276 scfdie_lineno(int lineno)
    277 {
    278 	scf_error_t err = scf_error();
    279 
    280 	if (err == SCF_ERROR_CONNECTION_BROKEN)
    281 		uu_die(gettext("Repository connection broken.  Exiting.\n"));
    282 
    283 	uu_die(gettext("Unexpected libscf error on line %d of " __FILE__
    284 	    ": %s.\n"), lineno, scf_strerror(err));
    285 }
    286 
    287 #endif
    288 
    289 static void
    290 scfwarn(void)
    291 {
    292 	warn(gettext("Unexpected libscf error: %s.\n"),
    293 	    scf_strerror(scf_error()));
    294 }
    295 
    296 /*
    297  * Clear a field of a structure.
    298  */
    299 static int
    300 clear_int(void *a, void *b)
    301 {
    302 	/* LINTED */
    303 	*(int *)((char *)a + (size_t)b) = 0;
    304 
    305 	return (UU_WALK_NEXT);
    306 }
    307 
    308 static int
    309 scferror2errno(scf_error_t err)
    310 {
    311 	switch (err) {
    312 	case SCF_ERROR_BACKEND_ACCESS:
    313 		return (EACCES);
    314 
    315 	case SCF_ERROR_BACKEND_READONLY:
    316 		return (EROFS);
    317 
    318 	case SCF_ERROR_CONNECTION_BROKEN:
    319 		return (ECONNABORTED);
    320 
    321 	case SCF_ERROR_CONSTRAINT_VIOLATED:
    322 	case SCF_ERROR_INVALID_ARGUMENT:
    323 		return (EINVAL);
    324 
    325 	case SCF_ERROR_DELETED:
    326 		return (ECANCELED);
    327 
    328 	case SCF_ERROR_EXISTS:
    329 		return (EEXIST);
    330 
    331 	case SCF_ERROR_NO_MEMORY:
    332 		return (ENOMEM);
    333 
    334 	case SCF_ERROR_NO_RESOURCES:
    335 		return (ENOSPC);
    336 
    337 	case SCF_ERROR_NOT_FOUND:
    338 		return (ENOENT);
    339 
    340 	case SCF_ERROR_PERMISSION_DENIED:
    341 		return (EPERM);
    342 
    343 	default:
    344 #ifndef NDEBUG
    345 		(void) fprintf(stderr, "%s:%d: Unknown libscf error %d.\n",
    346 		    __FILE__, __LINE__, err);
    347 #else
    348 		(void) fprintf(stderr, "Unknown libscf error %d.\n", err);
    349 #endif
    350 		abort();
    351 		/* NOTREACHED */
    352 	}
    353 }
    354 
    355 static int
    356 entity_get_pg(void *ent, int issvc, const char *name,
    357     scf_propertygroup_t *pg)
    358 {
    359 	if (issvc)
    360 		return (scf_service_get_pg(ent, name, pg));
    361 	else
    362 		return (scf_instance_get_pg(ent, name, pg));
    363 }
    364 
    365 static void
    366 entity_destroy(void *ent, int issvc)
    367 {
    368 	if (issvc)
    369 		scf_service_destroy(ent);
    370 	else
    371 		scf_instance_destroy(ent);
    372 }
    373 
    374 static int
    375 get_pg(const char *pg_name, scf_propertygroup_t *pg)
    376 {
    377 	int ret;
    378 
    379 	if (cur_level != NULL)
    380 		ret = scf_snaplevel_get_pg(cur_level, pg_name, pg);
    381 	else if (cur_inst != NULL)
    382 		ret = scf_instance_get_pg(cur_inst, pg_name, pg);
    383 	else
    384 		ret = scf_service_get_pg(cur_svc, pg_name, pg);
    385 
    386 	return (ret);
    387 }
    388 
    389 /*
    390  * Find a snaplevel in a snapshot.  If get_svc is true, find the service
    391  * snaplevel.  Otherwise find the instance snaplevel.
    392  *
    393  * Returns
    394  *   0 - success
    395  *   ECONNABORTED - repository connection broken
    396  *   ECANCELED - instance containing snap was deleted
    397  *   ENOENT - snap has no snaplevels
    398  *	    - requested snaplevel not found
    399  */
    400 static int
    401 get_snaplevel(scf_snapshot_t *snap, int get_svc, scf_snaplevel_t *snpl)
    402 {
    403 	if (scf_snapshot_get_base_snaplevel(snap, snpl) != 0) {
    404 		switch (scf_error()) {
    405 		case SCF_ERROR_CONNECTION_BROKEN:
    406 		case SCF_ERROR_DELETED:
    407 		case SCF_ERROR_NOT_FOUND:
    408 			return (scferror2errno(scf_error()));
    409 
    410 		case SCF_ERROR_HANDLE_MISMATCH:
    411 		case SCF_ERROR_NOT_BOUND:
    412 		case SCF_ERROR_NOT_SET:
    413 		default:
    414 			bad_error("scf_snapshot_get_base_snaplevel",
    415 			    scf_error());
    416 		}
    417 	}
    418 
    419 	for (;;) {
    420 		ssize_t ssz;
    421 
    422 		ssz = scf_snaplevel_get_instance_name(snpl, NULL, 0);
    423 		if (ssz >= 0) {
    424 			if (!get_svc)
    425 				return (0);
    426 		} else {
    427 			switch (scf_error()) {
    428 			case SCF_ERROR_CONSTRAINT_VIOLATED:
    429 				if (get_svc)
    430 					return (0);
    431 				break;
    432 
    433 			case SCF_ERROR_DELETED:
    434 			case SCF_ERROR_CONNECTION_BROKEN:
    435 				return (scferror2errno(scf_error()));
    436 
    437 			case SCF_ERROR_NOT_SET:
    438 			case SCF_ERROR_NOT_BOUND:
    439 			default:
    440 				bad_error("scf_snaplevel_get_instance_name",
    441 				    scf_error());
    442 			}
    443 		}
    444 
    445 		if (scf_snaplevel_get_next_snaplevel(snpl, snpl) != 0) {
    446 			switch (scf_error()) {
    447 			case SCF_ERROR_NOT_FOUND:
    448 			case SCF_ERROR_CONNECTION_BROKEN:
    449 			case SCF_ERROR_DELETED:
    450 				return (scferror2errno(scf_error()));
    451 
    452 			case SCF_ERROR_HANDLE_MISMATCH:
    453 			case SCF_ERROR_NOT_BOUND:
    454 			case SCF_ERROR_NOT_SET:
    455 			case SCF_ERROR_INVALID_ARGUMENT:
    456 			default:
    457 				bad_error("scf_snaplevel_get_next_snaplevel",
    458 				    scf_error());
    459 			}
    460 		}
    461 	}
    462 }
    463 
    464 /*
    465  * If issvc is 0, take ent to be a pointer to an scf_instance_t.  If it has
    466  * a running snapshot, and that snapshot has an instance snaplevel, set pg to
    467  * the property group named name in it.  If it doesn't have a running
    468  * snapshot, set pg to the instance's current property group named name.
    469  *
    470  * If issvc is nonzero, take ent to be a pointer to an scf_service_t, and walk
    471  * its instances.  If one has a running snapshot with a service snaplevel, set
    472  * pg to the property group named name in it.  If no such snaplevel could be
    473  * found, set pg to the service's current property group named name.
    474  *
    475  * iter, inst, snap, and snpl are required scratch objects.
    476  *
    477  * Returns
    478  *   0 - success
    479  *   ECONNABORTED - repository connection broken
    480  *   ECANCELED - ent was deleted
    481  *   ENOENT - no such property group
    482  *   EINVAL - name is an invalid property group name
    483  *   EBADF - found running snapshot is missing a snaplevel
    484  */
    485 static int
    486 entity_get_running_pg(void *ent, int issvc, const char *name,
    487     scf_propertygroup_t *pg, scf_iter_t *iter, scf_instance_t *inst,
    488     scf_snapshot_t *snap, scf_snaplevel_t *snpl)
    489 {
    490 	int r;
    491 
    492 	if (issvc) {
    493 		/* Search for an instance with a running snapshot. */
    494 		if (scf_iter_service_instances(iter, ent) != 0) {
    495 			switch (scf_error()) {
    496 			case SCF_ERROR_DELETED:
    497 			case SCF_ERROR_CONNECTION_BROKEN:
    498 				return (scferror2errno(scf_error()));
    499 
    500 			case SCF_ERROR_NOT_SET:
    501 			case SCF_ERROR_NOT_BOUND:
    502 			case SCF_ERROR_HANDLE_MISMATCH:
    503 			default:
    504 				bad_error("scf_iter_service_instances",
    505 				    scf_error());
    506 			}
    507 		}
    508 
    509 		for (;;) {
    510 			r = scf_iter_next_instance(iter, inst);
    511 			if (r == 0) {
    512 				if (scf_service_get_pg(ent, name, pg) == 0)
    513 					return (0);
    514 
    515 				switch (scf_error()) {
    516 				case SCF_ERROR_DELETED:
    517 				case SCF_ERROR_NOT_FOUND:
    518 				case SCF_ERROR_INVALID_ARGUMENT:
    519 				case SCF_ERROR_CONNECTION_BROKEN:
    520 					return (scferror2errno(scf_error()));
    521 
    522 				case SCF_ERROR_NOT_BOUND:
    523 				case SCF_ERROR_HANDLE_MISMATCH:
    524 				case SCF_ERROR_NOT_SET:
    525 				default:
    526 					bad_error("scf_service_get_pg",
    527 					    scf_error());
    528 				}
    529 			}
    530 			if (r != 1) {
    531 				switch (scf_error()) {
    532 				case SCF_ERROR_DELETED:
    533 				case SCF_ERROR_CONNECTION_BROKEN:
    534 					return (scferror2errno(scf_error()));
    535 
    536 				case SCF_ERROR_INVALID_ARGUMENT:
    537 				case SCF_ERROR_NOT_SET:
    538 				case SCF_ERROR_NOT_BOUND:
    539 				case SCF_ERROR_HANDLE_MISMATCH:
    540 				default:
    541 					bad_error("scf_iter_next_instance",
    542 					    scf_error());
    543 				}
    544 			}
    545 
    546 			if (scf_instance_get_snapshot(inst, snap_running,
    547 			    snap) == 0)
    548 				break;
    549 
    550 			switch (scf_error()) {
    551 			case SCF_ERROR_NOT_FOUND:
    552 			case SCF_ERROR_DELETED:
    553 				continue;
    554 
    555 			case SCF_ERROR_CONNECTION_BROKEN:
    556 				return (ECONNABORTED);
    557 
    558 			case SCF_ERROR_HANDLE_MISMATCH:
    559 			case SCF_ERROR_INVALID_ARGUMENT:
    560 			case SCF_ERROR_NOT_SET:
    561 			case SCF_ERROR_NOT_BOUND:
    562 			default:
    563 				bad_error("scf_instance_get_snapshot",
    564 				    scf_error());
    565 			}
    566 		}
    567 	} else {
    568 		if (scf_instance_get_snapshot(ent, snap_running, snap) != 0) {
    569 			switch (scf_error()) {
    570 			case SCF_ERROR_NOT_FOUND:
    571 				break;
    572 
    573 			case SCF_ERROR_DELETED:
    574 			case SCF_ERROR_CONNECTION_BROKEN:
    575 				return (scferror2errno(scf_error()));
    576 
    577 			case SCF_ERROR_NOT_BOUND:
    578 			case SCF_ERROR_HANDLE_MISMATCH:
    579 			case SCF_ERROR_INVALID_ARGUMENT:
    580 			case SCF_ERROR_NOT_SET:
    581 			default:
    582 				bad_error("scf_instance_get_snapshot",
    583 				    scf_error());
    584 			}
    585 
    586 			if (scf_instance_get_pg(ent, name, pg) == 0)
    587 				return (0);
    588 
    589 			switch (scf_error()) {
    590 			case SCF_ERROR_DELETED:
    591 			case SCF_ERROR_NOT_FOUND:
    592 			case SCF_ERROR_INVALID_ARGUMENT:
    593 			case SCF_ERROR_CONNECTION_BROKEN:
    594 				return (scferror2errno(scf_error()));
    595 
    596 			case SCF_ERROR_NOT_BOUND:
    597 			case SCF_ERROR_HANDLE_MISMATCH:
    598 			case SCF_ERROR_NOT_SET:
    599 			default:
    600 				bad_error("scf_instance_get_pg", scf_error());
    601 			}
    602 		}
    603 	}
    604 
    605 	r = get_snaplevel(snap, issvc, snpl);
    606 	switch (r) {
    607 	case 0:
    608 		break;
    609 
    610 	case ECONNABORTED:
    611 	case ECANCELED:
    612 		return (r);
    613 
    614 	case ENOENT:
    615 		return (EBADF);
    616 
    617 	default:
    618 		bad_error("get_snaplevel", r);
    619 	}
    620 
    621 	if (scf_snaplevel_get_pg(snpl, name, pg) == 0)
    622 		return (0);
    623 
    624 	switch (scf_error()) {
    625 	case SCF_ERROR_DELETED:
    626 	case SCF_ERROR_INVALID_ARGUMENT:
    627 	case SCF_ERROR_CONNECTION_BROKEN:
    628 	case SCF_ERROR_NOT_FOUND:
    629 		return (scferror2errno(scf_error()));
    630 
    631 	case SCF_ERROR_NOT_BOUND:
    632 	case SCF_ERROR_HANDLE_MISMATCH:
    633 	case SCF_ERROR_NOT_SET:
    634 	default:
    635 		bad_error("scf_snaplevel_get_pg", scf_error());
    636 		/* NOTREACHED */
    637 	}
    638 }
    639 
    640 
    641 /*
    642  * To be registered with atexit().
    643  */
    644 static void
    645 remove_tempfile(void)
    646 {
    647 	int ret;
    648 
    649 	if (tempfile != NULL) {
    650 		if (fclose(tempfile) == EOF)
    651 			warn(gettext("Could not close temporary file"));
    652 		tempfile = NULL;
    653 	}
    654 
    655 	if (tempfilename[0] != '\0') {
    656 		do {
    657 			ret = remove(tempfilename);
    658 		} while (ret == -1 && errno == EINTR);
    659 		if (ret == -1)
    660 			warn(gettext("Could not remove temporary file"));
    661 		tempfilename[0] = '\0';
    662 	}
    663 }
    664 
    665 /*
    666  * Launch private svc.configd(1M) for manipulating alternate repositories.
    667  */
    668 static void
    669 start_private_repository(engine_state_t *est)
    670 {
    671 	int fd, stat;
    672 	struct door_info info;
    673 	pid_t pid;
    674 
    675 	/*
    676 	 * 1.  Create a temporary file for the door.
    677 	 */
    678 	if (est->sc_repo_doorname != NULL)
    679 		free((void *)est->sc_repo_doorname);
    680 
    681 	est->sc_repo_doorname = tempnam(est->sc_repo_doordir, "scfdr");
    682 	if (est->sc_repo_doorname == NULL)
    683 		uu_die(gettext("Could not acquire temporary filename"));
    684 
    685 	fd = open(est->sc_repo_doorname, O_CREAT | O_EXCL | O_RDWR, 0600);
    686 	if (fd < 0)
    687 		uu_die(gettext("Could not create temporary file for "
    688 		    "repository server"));
    689 
    690 	(void) close(fd);
    691 
    692 	/*
    693 	 * 2.  Launch a configd with that door, using the specified
    694 	 * repository.
    695 	 */
    696 	if ((est->sc_repo_pid = fork()) == 0) {
    697 		(void) execlp(est->sc_repo_server, est->sc_repo_server, "-p",
    698 		    "-d", est->sc_repo_doorname, "-r", est->sc_repo_filename,
    699 		    NULL);
    700 		uu_die(gettext("Could not execute %s"), est->sc_repo_server);
    701 	} else if (est->sc_repo_pid == -1)
    702 		uu_die(gettext("Attempt to fork failed"));
    703 
    704 	do {
    705 		pid = waitpid(est->sc_repo_pid, &stat, 0);
    706 	} while (pid == -1 && errno == EINTR);
    707 
    708 	if (pid == -1)
    709 		uu_die(gettext("Could not waitpid() for repository server"));
    710 
    711 	if (!WIFEXITED(stat)) {
    712 		uu_die(gettext("Repository server failed (status %d).\n"),
    713 		    stat);
    714 	} else if (WEXITSTATUS(stat) != 0) {
    715 		uu_die(gettext("Repository server failed (exit %d).\n"),
    716 		    WEXITSTATUS(stat));
    717 	}
    718 
    719 	/*
    720 	 * See if it was successful by checking if the door is a door.
    721 	 */
    722 
    723 	fd = open(est->sc_repo_doorname, O_RDWR);
    724 	if (fd < 0)
    725 		uu_die(gettext("Could not open door \"%s\""),
    726 		    est->sc_repo_doorname);
    727 
    728 	if (door_info(fd, &info) < 0)
    729 		uu_die(gettext("Unexpected door_info() error"));
    730 
    731 	if (close(fd) == -1)
    732 		warn(gettext("Could not close repository door"),
    733 		    strerror(errno));
    734 
    735 	est->sc_repo_pid = info.di_target;
    736 }
    737 
    738 void
    739 lscf_cleanup(void)
    740 {
    741 	/*
    742 	 * In the case where we've launched a private svc.configd(1M)
    743 	 * instance, we must terminate our child and remove the temporary
    744 	 * rendezvous point.
    745 	 */
    746 	if (est->sc_repo_pid > 0) {
    747 		(void) kill(est->sc_repo_pid, SIGTERM);
    748 		(void) waitpid(est->sc_repo_pid, NULL, 0);
    749 		(void) unlink(est->sc_repo_doorname);
    750 
    751 		est->sc_repo_pid = 0;
    752 	}
    753 }
    754 
    755 void
    756 unselect_cursnap(void)
    757 {
    758 	void *cookie;
    759 
    760 	cur_level = NULL;
    761 
    762 	cookie = NULL;
    763 	while ((cur_elt = uu_list_teardown(cur_levels, &cookie)) != NULL) {
    764 		scf_snaplevel_destroy(cur_elt->sl);
    765 		free(cur_elt);
    766 	}
    767 
    768 	scf_snapshot_destroy(cur_snap);
    769 	cur_snap = NULL;
    770 }
    771 
    772 void
    773 lscf_prep_hndl(void)
    774 {
    775 	if (g_hndl != NULL)
    776 		return;
    777 
    778 	g_hndl = scf_handle_create(SCF_VERSION);
    779 	if (g_hndl == NULL)
    780 		scfdie();
    781 
    782 	if (est->sc_repo_filename != NULL)
    783 		start_private_repository(est);
    784 
    785 	if (est->sc_repo_doorname != NULL) {
    786 		scf_value_t *repo_value;
    787 		int ret;
    788 
    789 		repo_value = scf_value_create(g_hndl);
    790 		if (repo_value == NULL)
    791 			scfdie();
    792 
    793 		ret = scf_value_set_astring(repo_value, est->sc_repo_doorname);
    794 		assert(ret == SCF_SUCCESS);
    795 
    796 		if (scf_handle_decorate(g_hndl, "door_path", repo_value) !=
    797 		    SCF_SUCCESS)
    798 			scfdie();
    799 
    800 		scf_value_destroy(repo_value);
    801 	}
    802 
    803 	if (scf_handle_bind(g_hndl) != 0)
    804 		uu_die(gettext("Could not connect to repository server: %s.\n"),
    805 		    scf_strerror(scf_error()));
    806 
    807 	cur_scope = scf_scope_create(g_hndl);
    808 	if (cur_scope == NULL)
    809 		scfdie();
    810 
    811 	if (scf_handle_get_local_scope(g_hndl, cur_scope) != 0)
    812 		scfdie();
    813 }
    814 
    815 static void
    816 repository_teardown(void)
    817 {
    818 	if (g_hndl != NULL) {
    819 		if (cur_snap != NULL)
    820 			unselect_cursnap();
    821 		scf_instance_destroy(cur_inst);
    822 		scf_service_destroy(cur_svc);
    823 		scf_scope_destroy(cur_scope);
    824 		scf_handle_destroy(g_hndl);
    825 		cur_inst = NULL;
    826 		cur_svc = NULL;
    827 		cur_scope = NULL;
    828 		g_hndl = NULL;
    829 		lscf_cleanup();
    830 	}
    831 }
    832 
    833 void
    834 lscf_set_repository(const char *repfile, int force)
    835 {
    836 	repository_teardown();
    837 
    838 	if (est->sc_repo_filename != NULL) {
    839 		free((void *)est->sc_repo_filename);
    840 		est->sc_repo_filename = NULL;
    841 	}
    842 
    843 	if ((force == 0) && (access(repfile, R_OK) != 0)) {
    844 		/*
    845 		 * Repository file does not exist
    846 		 * or has no read permission.
    847 		 */
    848 		warn(gettext("Cannot access \"%s\": %s\n"),
    849 		    repfile, strerror(errno));
    850 	} else {
    851 		est->sc_repo_filename = safe_strdup(repfile);
    852 	}
    853 
    854 	lscf_prep_hndl();
    855 }
    856 
    857 void
    858 lscf_init()
    859 {
    860 	if ((max_scf_fmri_len = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH)) < 0 ||
    861 	    (max_scf_name_len = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) < 0 ||
    862 	    (max_scf_pg_type_len = scf_limit(SCF_LIMIT_MAX_PG_TYPE_LENGTH)) <
    863 	    0 ||
    864 	    (max_scf_value_len = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH)) < 0)
    865 		scfdie();
    866 
    867 	max_scf_len = max_scf_fmri_len;
    868 	if (max_scf_name_len > max_scf_len)
    869 		max_scf_len = max_scf_name_len;
    870 	if (max_scf_pg_type_len > max_scf_len)
    871 		max_scf_len = max_scf_pg_type_len;
    872 	if (max_scf_value_len > max_scf_len)
    873 		max_scf_len = max_scf_value_len;
    874 
    875 	if (atexit(remove_tempfile) != 0)
    876 		uu_die(gettext("Could not register atexit() function"));
    877 
    878 	emsg_entity_not_selected = gettext("An entity is not selected.\n");
    879 	emsg_permission_denied = gettext("Permission denied.\n");
    880 	emsg_create_xml = gettext("Could not create XML node.\n");
    881 	emsg_cant_modify_snapshots = gettext("Cannot modify snapshots.\n");
    882 	emsg_read_only = gettext("Backend read-only.\n");
    883 	emsg_deleted = gettext("Current selection has been deleted.\n");
    884 	emsg_invalid_pg_name =
    885 	    gettext("Invalid property group name \"%s\".\n");
    886 	emsg_invalid_prop_name = gettext("Invalid property name \"%s\".\n");
    887 	emsg_no_such_pg = gettext("No such property group \"%s\".\n");
    888 	emsg_fmri_invalid_pg_name = gettext("Service %s has property group "
    889 	    "with invalid name \"%s\".\n");
    890 	emsg_fmri_invalid_pg_name_type = gettext("Service %s has property "
    891 	    "group with invalid name \"%s\" or type \"%s\".\n");
    892 	emsg_pg_added = gettext("%s changed unexpectedly "
    893 	    "(property group \"%s\" added).\n");
    894 	emsg_pg_changed = gettext("%s changed unexpectedly "
    895 	    "(property group \"%s\" changed).\n");
    896 	emsg_pg_deleted = gettext("%s changed unexpectedly "
    897 	    "(property group \"%s\" or an ancestor was deleted).\n");
    898 	emsg_pg_mod_perm = gettext("Could not modify property group \"%s\" "
    899 	    "in %s (permission denied).\n");
    900 	emsg_pg_add_perm = gettext("Could not create property group \"%s\" "
    901 	    "in %s (permission denied).\n");
    902 	emsg_pg_del_perm = gettext("Could not delete property group \"%s\" "
    903 	    "in %s (permission denied).\n");
    904 	emsg_snap_perm = gettext("Could not take \"%s\" snapshot of %s "
    905 	    "(permission denied).\n");
    906 	emsg_dpt_dangling = gettext("Conflict upgrading %s (not importing "
    907 	    "new dependent \"%s\" because it already exists).  Warning: The "
    908 	    "current dependent's target (%s) does not exist.\n");
    909 	emsg_dpt_no_dep = gettext("Conflict upgrading %s (not importing new "
    910 	    "dependent \"%s\" because it already exists).  Warning: The "
    911 	    "current dependent's target (%s) does not have a dependency named "
    912 	    "\"%s\" as expected.\n");
    913 
    914 	string_pool = uu_list_pool_create("strings", sizeof (string_list_t),
    915 	    offsetof(string_list_t, node), NULL, 0);
    916 	snaplevel_pool = uu_list_pool_create("snaplevels",
    917 	    sizeof (struct snaplevel), offsetof(struct snaplevel, list_node),
    918 	    NULL, 0);
    919 }
    920 
    921 
    922 static const char *
    923 prop_to_typestr(const scf_property_t *prop)
    924 {
    925 	scf_type_t ty;
    926 
    927 	if (scf_property_type(prop, &ty) != SCF_SUCCESS)
    928 		scfdie();
    929 
    930 	return (scf_type_to_string(ty));
    931 }
    932 
    933 static scf_type_t
    934 string_to_type(const char *type)
    935 {
    936 	size_t len = strlen(type);
    937 	char *buf;
    938 
    939 	if (len == 0 || type[len - 1] != ':')
    940 		return (SCF_TYPE_INVALID);
    941 
    942 	buf = (char *)alloca(len + 1);
    943 	(void) strlcpy(buf, type, len + 1);
    944 	buf[len - 1] = 0;
    945 
    946 	return (scf_string_to_type(buf));
    947 }
    948 
    949 static scf_value_t *
    950 string_to_value(const char *str, scf_type_t ty, boolean_t require_quotes)
    951 {
    952 	scf_value_t *v;
    953 	char *dup, *nstr;
    954 	size_t len;
    955 
    956 	v = scf_value_create(g_hndl);
    957 	if (v == NULL)
    958 		scfdie();
    959 
    960 	len = strlen(str);
    961 	if (require_quotes &&
    962 	    (len < 2 || str[0] != '\"' || str[len - 1] != '\"')) {
    963 		semerr(gettext("Multiple string values or string values "
    964 		    "with spaces must be quoted with '\"'.\n"));
    965 		scf_value_destroy(v);
    966 		return (NULL);
    967 	}
    968 
    969 	nstr = dup = safe_strdup(str);
    970 	if (dup[0] == '\"') {
    971 		/*
    972 		 * Strip out the first and the last quote.
    973 		 */
    974 		dup[len - 1] = '\0';
    975 		nstr = dup + 1;
    976 	}
    977 
    978 	if (scf_value_set_from_string(v, ty, (const char *)nstr) != 0) {
    979 		assert(scf_error() == SCF_ERROR_INVALID_ARGUMENT);
    980 		semerr(gettext("Invalid \"%s\" value \"%s\".\n"),
    981 		    scf_type_to_string(ty), nstr);
    982 		scf_value_destroy(v);
    983 		v = NULL;
    984 	}
    985 	free(dup);
    986 	return (v);
    987 }
    988 
    989 /*
    990  * Print str to strm, quoting double-quotes and backslashes with backslashes.
    991  * Optionally append a comment prefix ('#') to newlines ('\n').
    992  */
    993 static int
    994 quote_and_print(const char *str, FILE *strm, int commentnl)
    995 {
    996 	const char *cp;
    997 
    998 	for (cp = str; *cp != '\0'; ++cp) {
    999 		if (*cp == '"' || *cp == '\\')
   1000 			(void) putc('\\', strm);
   1001 
   1002 		(void) putc(*cp, strm);
   1003 
   1004 		if (commentnl && *cp == '\n') {
   1005 			(void) putc('#', strm);
   1006 		}
   1007 	}
   1008 
   1009 	return (ferror(strm));
   1010 }
   1011 
   1012 /*
   1013  * These wrappers around lowlevel functions provide consistent error checking
   1014  * and warnings.
   1015  */
   1016 static int
   1017 pg_get_prop(scf_propertygroup_t *pg, const char *propname, scf_property_t *prop)
   1018 {
   1019 	if (scf_pg_get_property(pg, propname, prop) == SCF_SUCCESS)
   1020 		return (0);
   1021 
   1022 	if (scf_error() != SCF_ERROR_NOT_FOUND)
   1023 		scfdie();
   1024 
   1025 	if (g_verbose) {
   1026 		ssize_t len;
   1027 		char *fmri;
   1028 
   1029 		len = scf_pg_to_fmri(pg, NULL, 0);
   1030 		if (len < 0)
   1031 			scfdie();
   1032 
   1033 		fmri = safe_malloc(len + 1);
   1034 
   1035 		if (scf_pg_to_fmri(pg, fmri, len + 1) < 0)
   1036 			scfdie();
   1037 
   1038 		warn(gettext("Expected property %s of property group %s is "
   1039 		    "missing.\n"), propname, fmri);
   1040 
   1041 		free(fmri);
   1042 	}
   1043 
   1044 	return (-1);
   1045 }
   1046 
   1047 static int
   1048 prop_check_type(scf_property_t *prop, scf_type_t ty)
   1049 {
   1050 	scf_type_t pty;
   1051 
   1052 	if (scf_property_type(prop, &pty) != SCF_SUCCESS)
   1053 		scfdie();
   1054 
   1055 	if (ty == pty)
   1056 		return (0);
   1057 
   1058 	if (g_verbose) {
   1059 		ssize_t len;
   1060 		char *fmri;
   1061 		const char *tystr;
   1062 
   1063 		len = scf_property_to_fmri(prop, NULL, 0);
   1064 		if (len < 0)
   1065 			scfdie();
   1066 
   1067 		fmri = safe_malloc(len + 1);
   1068 
   1069 		if (scf_property_to_fmri(prop, fmri, len + 1) < 0)
   1070 			scfdie();
   1071 
   1072 		tystr = scf_type_to_string(ty);
   1073 		if (tystr == NULL)
   1074 			tystr = "?";
   1075 
   1076 		warn(gettext("Property %s is not of expected type %s.\n"),
   1077 		    fmri, tystr);
   1078 
   1079 		free(fmri);
   1080 	}
   1081 
   1082 	return (-1);
   1083 }
   1084 
   1085 static int
   1086 prop_get_val(scf_property_t *prop, scf_value_t *val)
   1087 {
   1088 	scf_error_t err;
   1089 
   1090 	if (scf_property_get_value(prop, val) == SCF_SUCCESS)
   1091 		return (0);
   1092 
   1093 	err = scf_error();
   1094 
   1095 	if (err != SCF_ERROR_NOT_FOUND &&
   1096 	    err != SCF_ERROR_CONSTRAINT_VIOLATED &&
   1097 	    err != SCF_ERROR_PERMISSION_DENIED)
   1098 		scfdie();
   1099 
   1100 	if (g_verbose) {
   1101 		ssize_t len;
   1102 		char *fmri, *emsg;
   1103 
   1104 		len = scf_property_to_fmri(prop, NULL, 0);
   1105 		if (len < 0)
   1106 			scfdie();
   1107 
   1108 		fmri = safe_malloc(len + 1);
   1109 
   1110 		if (scf_property_to_fmri(prop, fmri, len + 1) < 0)
   1111 			scfdie();
   1112 
   1113 		if (err == SCF_ERROR_NOT_FOUND)
   1114 			emsg = gettext("Property %s has no values; expected "
   1115 			    "one.\n");
   1116 		else if (err == SCF_ERROR_CONSTRAINT_VIOLATED)
   1117 			emsg = gettext("Property %s has multiple values; "
   1118 			    "expected one.\n");
   1119 		else
   1120 			emsg = gettext("No permission to read property %s.\n");
   1121 
   1122 		warn(emsg, fmri);
   1123 
   1124 		free(fmri);
   1125 	}
   1126 
   1127 	return (-1);
   1128 }
   1129 
   1130 
   1131 static boolean_t
   1132 snaplevel_is_instance(const scf_snaplevel_t *level)
   1133 {
   1134 	if (scf_snaplevel_get_instance_name(level, NULL, 0) < 0) {
   1135 		if (scf_error() != SCF_ERROR_CONSTRAINT_VIOLATED)
   1136 			scfdie();
   1137 		return (0);
   1138 	} else {
   1139 		return (1);
   1140 	}
   1141 }
   1142 
   1143 /*
   1144  * Decode FMRI into a service or instance, and put the result in *ep.  If
   1145  * memory cannot be allocated, return SCF_ERROR_NO_MEMORY.  If the FMRI is
   1146  * invalid, return SCF_ERROR_INVALID_ARGUMENT.  If the FMRI does not specify
   1147  * an entity, return SCF_ERROR_CONSTRAINT_VIOLATED.  If the entity cannot be
   1148  * found, return SCF_ERROR_NOT_FOUND.  Otherwise return SCF_ERROR_NONE, point
   1149  * *ep to a valid scf_service_t or scf_instance_t, and set *isservice to
   1150  * whether *ep is a service.
   1151  */
   1152 static scf_error_t
   1153 fmri_to_entity(scf_handle_t *h, const char *fmri, void **ep, int *isservice)
   1154 {
   1155 	char *fmri_copy;
   1156 	const char *sstr, *istr, *pgstr;
   1157 	scf_service_t *svc;
   1158 	scf_instance_t *inst;
   1159 
   1160 	fmri_copy = strdup(fmri);
   1161 	if (fmri_copy == NULL)
   1162 		return (SCF_ERROR_NO_MEMORY);
   1163 
   1164 	if (scf_parse_svc_fmri(fmri_copy, NULL, &sstr, &istr, &pgstr, NULL) !=
   1165 	    SCF_SUCCESS) {
   1166 		free(fmri_copy);
   1167 		return (SCF_ERROR_INVALID_ARGUMENT);
   1168 	}
   1169 
   1170 	free(fmri_copy);
   1171 
   1172 	if (sstr == NULL || pgstr != NULL)
   1173 		return (SCF_ERROR_CONSTRAINT_VIOLATED);
   1174 
   1175 	if (istr == NULL) {
   1176 		svc = scf_service_create(h);
   1177 		if (svc == NULL)
   1178 			return (SCF_ERROR_NO_MEMORY);
   1179 
   1180 		if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
   1181 		    SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
   1182 			if (scf_error() != SCF_ERROR_NOT_FOUND)
   1183 				scfdie();
   1184 
   1185 			return (SCF_ERROR_NOT_FOUND);
   1186 		}
   1187 
   1188 		*ep = svc;
   1189 		*isservice = 1;
   1190 	} else {
   1191 		inst = scf_instance_create(h);
   1192 		if (inst == NULL)
   1193 			return (SCF_ERROR_NO_MEMORY);
   1194 
   1195 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
   1196 		    NULL, SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
   1197 			if (scf_error() != SCF_ERROR_NOT_FOUND)
   1198 				scfdie();
   1199 
   1200 			return (SCF_ERROR_NOT_FOUND);
   1201 		}
   1202 
   1203 		*ep = inst;
   1204 		*isservice = 0;
   1205 	}
   1206 
   1207 	return (SCF_ERROR_NONE);
   1208 }
   1209 
   1210 /*
   1211  * Create the entity named by fmri.  Place a pointer to its libscf handle in
   1212  * *ep, and set or clear *isservicep if it is a service or an instance.
   1213  * Returns
   1214  *   SCF_ERROR_NONE - success
   1215  *   SCF_ERROR_NO_MEMORY - scf_*_create() failed
   1216  *   SCF_ERROR_INVALID_ARGUMENT - fmri is invalid
   1217  *   SCF_ERROR_CONSTRAINT_VIOLATED - fmri is not a service or instance
   1218  *   SCF_ERROR_NOT_FOUND - no such scope
   1219  *   SCF_ERROR_PERMISSION_DENIED
   1220  *   SCF_ERROR_BACKEND_READONLY
   1221  *   SCF_ERROR_BACKEND_ACCESS
   1222  */
   1223 static scf_error_t
   1224 create_entity(scf_handle_t *h, const char *fmri, void **ep, int *isservicep)
   1225 {
   1226 	char *fmri_copy;
   1227 	const char *scstr, *sstr, *istr, *pgstr;
   1228 	scf_scope_t *scope = NULL;
   1229 	scf_service_t *svc = NULL;
   1230 	scf_instance_t *inst = NULL;
   1231 	scf_error_t scfe;
   1232 
   1233 	fmri_copy = safe_strdup(fmri);
   1234 
   1235 	if (scf_parse_svc_fmri(fmri_copy, &scstr, &sstr, &istr, &pgstr, NULL) !=
   1236 	    0) {
   1237 		free(fmri_copy);
   1238 		return (SCF_ERROR_INVALID_ARGUMENT);
   1239 	}
   1240 
   1241 	if (scstr == NULL || sstr == NULL || pgstr != NULL) {
   1242 		free(fmri_copy);
   1243 		return (SCF_ERROR_CONSTRAINT_VIOLATED);
   1244 	}
   1245 
   1246 	*ep = NULL;
   1247 
   1248 	if ((scope = scf_scope_create(h)) == NULL ||
   1249 	    (svc = scf_service_create(h)) == NULL ||
   1250 	    (inst = scf_instance_create(h)) == NULL) {
   1251 		scfe = SCF_ERROR_NO_MEMORY;
   1252 		goto out;
   1253 	}
   1254 
   1255 get_scope:
   1256 	if (scf_handle_get_scope(h, scstr, scope) != 0) {
   1257 		switch (scf_error()) {
   1258 		case SCF_ERROR_CONNECTION_BROKEN:
   1259 			scfdie();
   1260 			/* NOTREACHED */
   1261 
   1262 		case SCF_ERROR_NOT_FOUND:
   1263 			scfe = SCF_ERROR_NOT_FOUND;
   1264 			goto out;
   1265 
   1266 		case SCF_ERROR_HANDLE_MISMATCH:
   1267 		case SCF_ERROR_NOT_BOUND:
   1268 		case SCF_ERROR_INVALID_ARGUMENT:
   1269 		default:
   1270 			bad_error("scf_handle_get_scope", scf_error());
   1271 		}
   1272 	}
   1273 
   1274 get_svc:
   1275 	if (scf_scope_get_service(scope, sstr, svc) != 0) {
   1276 		switch (scf_error()) {
   1277 		case SCF_ERROR_CONNECTION_BROKEN:
   1278 			scfdie();
   1279 			/* NOTREACHED */
   1280 
   1281 		case SCF_ERROR_DELETED:
   1282 			goto get_scope;
   1283 
   1284 		case SCF_ERROR_NOT_FOUND:
   1285 			break;
   1286 
   1287 		case SCF_ERROR_HANDLE_MISMATCH:
   1288 		case SCF_ERROR_INVALID_ARGUMENT:
   1289 		case SCF_ERROR_NOT_BOUND:
   1290 		case SCF_ERROR_NOT_SET:
   1291 		default:
   1292 			bad_error("scf_scope_get_service", scf_error());
   1293 		}
   1294 
   1295 		if (scf_scope_add_service(scope, sstr, svc) != 0) {
   1296 			switch (scf_error()) {
   1297 			case SCF_ERROR_CONNECTION_BROKEN:
   1298 				scfdie();
   1299 				/* NOTREACHED */
   1300 
   1301 			case SCF_ERROR_DELETED:
   1302 				goto get_scope;
   1303 
   1304 			case SCF_ERROR_PERMISSION_DENIED:
   1305 			case SCF_ERROR_BACKEND_READONLY:
   1306 			case SCF_ERROR_BACKEND_ACCESS:
   1307 				scfe = scf_error();
   1308 				goto out;
   1309 
   1310 			case SCF_ERROR_HANDLE_MISMATCH:
   1311 			case SCF_ERROR_INVALID_ARGUMENT:
   1312 			case SCF_ERROR_NOT_BOUND:
   1313 			case SCF_ERROR_NOT_SET:
   1314 			default:
   1315 				bad_error("scf_scope_get_service", scf_error());
   1316 			}
   1317 		}
   1318 	}
   1319 
   1320 	if (istr == NULL) {
   1321 		scfe = SCF_ERROR_NONE;
   1322 		*ep = svc;
   1323 		*isservicep = 1;
   1324 		goto out;
   1325 	}
   1326 
   1327 get_inst:
   1328 	if (scf_service_get_instance(svc, istr, inst) != 0) {
   1329 		switch (scf_error()) {
   1330 		case SCF_ERROR_CONNECTION_BROKEN:
   1331 			scfdie();
   1332 			/* NOTREACHED */
   1333 
   1334 		case SCF_ERROR_DELETED:
   1335 			goto get_svc;
   1336 
   1337 		case SCF_ERROR_NOT_FOUND:
   1338 			break;
   1339 
   1340 		case SCF_ERROR_HANDLE_MISMATCH:
   1341 		case SCF_ERROR_INVALID_ARGUMENT:
   1342 		case SCF_ERROR_NOT_BOUND:
   1343 		case SCF_ERROR_NOT_SET:
   1344 		default:
   1345 			bad_error("scf_service_get_instance", scf_error());
   1346 		}
   1347 
   1348 		if (scf_service_add_instance(svc, istr, inst) != 0) {
   1349 			switch (scf_error()) {
   1350 			case SCF_ERROR_CONNECTION_BROKEN:
   1351 				scfdie();
   1352 				/* NOTREACHED */
   1353 
   1354 			case SCF_ERROR_DELETED:
   1355 				goto get_svc;
   1356 
   1357 			case SCF_ERROR_PERMISSION_DENIED:
   1358 			case SCF_ERROR_BACKEND_READONLY:
   1359 			case SCF_ERROR_BACKEND_ACCESS:
   1360 				scfe = scf_error();
   1361 				goto out;
   1362 
   1363 			case SCF_ERROR_HANDLE_MISMATCH:
   1364 			case SCF_ERROR_INVALID_ARGUMENT:
   1365 			case SCF_ERROR_NOT_BOUND:
   1366 			case SCF_ERROR_NOT_SET:
   1367 			default:
   1368 				bad_error("scf_service_add_instance",
   1369 				    scf_error());
   1370 			}
   1371 		}
   1372 	}
   1373 
   1374 	scfe = SCF_ERROR_NONE;
   1375 	*ep = inst;
   1376 	*isservicep = 0;
   1377 
   1378 out:
   1379 	if (*ep != inst)
   1380 		scf_instance_destroy(inst);
   1381 	if (*ep != svc)
   1382 		scf_service_destroy(svc);
   1383 	scf_scope_destroy(scope);
   1384 	free(fmri_copy);
   1385 	return (scfe);
   1386 }
   1387 
   1388 /*
   1389  * Create or update a snapshot of inst.  snap is a required scratch object.
   1390  *
   1391  * Returns
   1392  *   0 - success
   1393  *   ECONNABORTED - repository connection broken
   1394  *   EPERM - permission denied
   1395  *   ENOSPC - configd is out of resources
   1396  *   ECANCELED - inst was deleted
   1397  *   -1 - unknown libscf error (message printed)
   1398  */
   1399 static int
   1400 take_snap(scf_instance_t *inst, const char *name, scf_snapshot_t *snap)
   1401 {
   1402 again:
   1403 	if (scf_instance_get_snapshot(inst, name, snap) == 0) {
   1404 		if (_scf_snapshot_take_attach(inst, snap) != 0) {
   1405 			switch (scf_error()) {
   1406 			case SCF_ERROR_CONNECTION_BROKEN:
   1407 			case SCF_ERROR_PERMISSION_DENIED:
   1408 			case SCF_ERROR_NO_RESOURCES:
   1409 				return (scferror2errno(scf_error()));
   1410 
   1411 			case SCF_ERROR_NOT_SET:
   1412 			case SCF_ERROR_INVALID_ARGUMENT:
   1413 			default:
   1414 				bad_error("_scf_snapshot_take_attach",
   1415 				    scf_error());
   1416 			}
   1417 		}
   1418 	} else {
   1419 		switch (scf_error()) {
   1420 		case SCF_ERROR_NOT_FOUND:
   1421 			break;
   1422 
   1423 		case SCF_ERROR_DELETED:
   1424 		case SCF_ERROR_CONNECTION_BROKEN:
   1425 			return (scferror2errno(scf_error()));
   1426 
   1427 		case SCF_ERROR_HANDLE_MISMATCH:
   1428 		case SCF_ERROR_NOT_BOUND:
   1429 		case SCF_ERROR_INVALID_ARGUMENT:
   1430 		case SCF_ERROR_NOT_SET:
   1431 		default:
   1432 			bad_error("scf_instance_get_snapshot", scf_error());
   1433 		}
   1434 
   1435 		if (_scf_snapshot_take_new(inst, name, snap) != 0) {
   1436 			switch (scf_error()) {
   1437 			case SCF_ERROR_EXISTS:
   1438 				goto again;
   1439 
   1440 			case SCF_ERROR_CONNECTION_BROKEN:
   1441 			case SCF_ERROR_NO_RESOURCES:
   1442 			case SCF_ERROR_PERMISSION_DENIED:
   1443 				return (scferror2errno(scf_error()));
   1444 
   1445 			default:
   1446 				scfwarn();
   1447 				return (-1);
   1448 
   1449 			case SCF_ERROR_NOT_SET:
   1450 			case SCF_ERROR_INTERNAL:
   1451 			case SCF_ERROR_INVALID_ARGUMENT:
   1452 			case SCF_ERROR_HANDLE_MISMATCH:
   1453 				bad_error("_scf_snapshot_take_new",
   1454 				    scf_error());
   1455 			}
   1456 		}
   1457 	}
   1458 
   1459 	return (0);
   1460 }
   1461 
   1462 static int
   1463 refresh_running_snapshot(void *entity)
   1464 {
   1465 	scf_snapshot_t *snap;
   1466 	int r;
   1467 
   1468 	if ((snap = scf_snapshot_create(g_hndl)) == NULL)
   1469 		scfdie();
   1470 	r = take_snap(entity, snap_running, snap);
   1471 	scf_snapshot_destroy(snap);
   1472 
   1473 	return (r);
   1474 }
   1475 
   1476 /*
   1477  * Refresh entity.  If isservice is zero, take entity to be an scf_instance_t *.
   1478  * Otherwise take entity to be an scf_service_t * and refresh all of its child
   1479  * instances.  fmri is used for messages.  inst, iter, and name_buf are used
   1480  * for scratch space.  Returns
   1481  *   0 - success
   1482  *   ECONNABORTED - repository connection broken
   1483  *   ECANCELED - entity was deleted
   1484  *   EACCES - backend denied access
   1485  *   EPERM - permission denied
   1486  *   ENOSPC - repository server out of resources
   1487  *   -1 - _smf_refresh_instance_i() failed.  scf_error() should be set.
   1488  */
   1489 static int
   1490 refresh_entity(int isservice, void *entity, const char *fmri,
   1491     scf_instance_t *inst, scf_iter_t *iter, char *name_buf)
   1492 {
   1493 	scf_error_t scfe;
   1494 	int r;
   1495 
   1496 	if (!isservice) {
   1497 		if (est->sc_repo_filename == NULL &&
   1498 		    est->sc_repo_doorname == NULL) {
   1499 			if (_smf_refresh_instance_i(entity) == 0) {
   1500 				if (g_verbose)
   1501 					warn(gettext("Refreshed %s.\n"), fmri);
   1502 				return (0);
   1503 			}
   1504 
   1505 			switch (scf_error()) {
   1506 			case SCF_ERROR_BACKEND_ACCESS:
   1507 				return (EACCES);
   1508 
   1509 			case SCF_ERROR_PERMISSION_DENIED:
   1510 				return (EPERM);
   1511 
   1512 			default:
   1513 				return (-1);
   1514 			}
   1515 		} else {
   1516 			r = refresh_running_snapshot(entity);
   1517 			switch (r) {
   1518 			case 0:
   1519 				break;
   1520 
   1521 			case ECONNABORTED:
   1522 			case ECANCELED:
   1523 			case EPERM:
   1524 			case ENOSPC:
   1525 				break;
   1526 
   1527 			default:
   1528 				bad_error("refresh_running_snapshot",
   1529 				    scf_error());
   1530 			}
   1531 
   1532 			return (r);
   1533 		}
   1534 	}
   1535 
   1536 	if (scf_iter_service_instances(iter, entity) != 0) {
   1537 		switch (scf_error()) {
   1538 		case SCF_ERROR_CONNECTION_BROKEN:
   1539 			return (ECONNABORTED);
   1540 
   1541 		case SCF_ERROR_DELETED:
   1542 			return (ECANCELED);
   1543 
   1544 		case SCF_ERROR_HANDLE_MISMATCH:
   1545 		case SCF_ERROR_NOT_BOUND:
   1546 		case SCF_ERROR_NOT_SET:
   1547 		default:
   1548 			bad_error("scf_iter_service_instances", scf_error());
   1549 		}
   1550 	}
   1551 
   1552 	for (;;) {
   1553 		r = scf_iter_next_instance(iter, inst);
   1554 		if (r == 0)
   1555 			break;
   1556 		if (r != 1) {
   1557 			switch (scf_error()) {
   1558 			case SCF_ERROR_CONNECTION_BROKEN:
   1559 				return (ECONNABORTED);
   1560 
   1561 			case SCF_ERROR_DELETED:
   1562 				return (ECANCELED);
   1563 
   1564 			case SCF_ERROR_HANDLE_MISMATCH:
   1565 			case SCF_ERROR_NOT_BOUND:
   1566 			case SCF_ERROR_NOT_SET:
   1567 			case SCF_ERROR_INVALID_ARGUMENT:
   1568 			default:
   1569 				bad_error("scf_iter_next_instance",
   1570 				    scf_error());
   1571 			}
   1572 		}
   1573 
   1574 		if (est->sc_repo_filename != NULL ||
   1575 		    est->sc_repo_doorname != NULL) {
   1576 			r = refresh_running_snapshot(inst);
   1577 			switch (r) {
   1578 			case 0:
   1579 				continue;
   1580 
   1581 			case ECONNABORTED:
   1582 			case ECANCELED:
   1583 			case EPERM:
   1584 			case ENOSPC:
   1585 				break;
   1586 			default:
   1587 				bad_error("refresh_running_snapshot",
   1588 				    scf_error());
   1589 			}
   1590 
   1591 			return (r);
   1592 
   1593 		}
   1594 
   1595 		if (_smf_refresh_instance_i(inst) == 0) {
   1596 			if (g_verbose) {
   1597 				if (scf_instance_get_name(inst, name_buf,
   1598 				    max_scf_name_len + 1) < 0)
   1599 					(void) strcpy(name_buf, "?");
   1600 
   1601 				warn(gettext("Refreshed %s:%s.\n"),
   1602 				    fmri, name_buf);
   1603 			}
   1604 		} else {
   1605 			if (scf_error() != SCF_ERROR_BACKEND_ACCESS ||
   1606 			    g_verbose) {
   1607 				scfe = scf_error();
   1608 
   1609 				if (scf_instance_to_fmri(inst, name_buf,
   1610 				    max_scf_name_len + 1) < 0)
   1611 					(void) strcpy(name_buf, "?");
   1612 
   1613 				warn(gettext(
   1614 				    "Refresh of %s:%s failed: %s.\n"), fmri,
   1615 				    name_buf, scf_strerror(scfe));
   1616 			}
   1617 		}
   1618 	}
   1619 
   1620 	return (0);
   1621 }
   1622 
   1623 static void
   1624 private_refresh(void)
   1625 {
   1626 	scf_instance_t *pinst = NULL;
   1627 	scf_iter_t *piter = NULL;
   1628 	ssize_t fmrilen;
   1629 	size_t bufsz;
   1630 	char *fmribuf;
   1631 	void *ent;
   1632 	int issvc;
   1633 	int r;
   1634 
   1635 	if (est->sc_repo_filename == NULL && est->sc_repo_doorname == NULL)
   1636 		return;
   1637 
   1638 	assert(cur_svc != NULL);
   1639 
   1640 	bufsz = max_scf_fmri_len + 1;
   1641 	fmribuf = safe_malloc(bufsz);
   1642 	if (cur_inst) {
   1643 		issvc = 0;
   1644 		ent = cur_inst;
   1645 		fmrilen = scf_instance_to_fmri(ent, fmribuf, bufsz);
   1646 	} else {
   1647 		issvc = 1;
   1648 		ent = cur_svc;
   1649 		fmrilen = scf_service_to_fmri(ent, fmribuf, bufsz);
   1650 		if ((pinst = scf_instance_create(g_hndl)) == NULL)
   1651 			scfdie();
   1652 
   1653 		if ((piter = scf_iter_create(g_hndl)) == NULL)
   1654 			scfdie();
   1655 	}
   1656 	if (fmrilen < 0) {
   1657 		free(fmribuf);
   1658 		if (scf_error() != SCF_ERROR_DELETED)
   1659 			scfdie();
   1660 
   1661 		warn(emsg_deleted);
   1662 		return;
   1663 	}
   1664 	assert(fmrilen < bufsz);
   1665 
   1666 	r = refresh_entity(issvc, ent, fmribuf, pinst, piter, NULL);
   1667 	switch (r) {
   1668 	case 0:
   1669 		break;
   1670 
   1671 	case ECONNABORTED:
   1672 		warn(gettext("Could not refresh %s "
   1673 		    "(repository connection broken).\n"), fmribuf);
   1674 		break;
   1675 
   1676 	case ECANCELED:
   1677 		warn(emsg_deleted);
   1678 		break;
   1679 
   1680 	case EPERM:
   1681 		warn(gettext("Could not refresh %s "
   1682 		    "(permission denied).\n"), fmribuf);
   1683 		break;
   1684 
   1685 	case ENOSPC:
   1686 		warn(gettext("Could not refresh %s "
   1687 		    "(repository server out of resources).\n"),
   1688 		    fmribuf);
   1689 		break;
   1690 
   1691 	case EACCES:
   1692 	default:
   1693 		bad_error("refresh_entity", scf_error());
   1694 	}
   1695 
   1696 	if (issvc) {
   1697 		scf_instance_destroy(pinst);
   1698 		scf_iter_destroy(piter);
   1699 	}
   1700 
   1701 	free(fmribuf);
   1702 }
   1703 
   1704 
   1705 static int
   1706 stash_scferror_err(scf_callback_t *cbp, scf_error_t err)
   1707 {
   1708 	cbp->sc_err = scferror2errno(err);
   1709 	return (UU_WALK_ERROR);
   1710 }
   1711 
   1712 static int
   1713 stash_scferror(scf_callback_t *cbp)
   1714 {
   1715 	return (stash_scferror_err(cbp, scf_error()));
   1716 }
   1717 
   1718 /*
   1719  * Import.  These functions import a bundle into the repository.
   1720  */
   1721 
   1722 /*
   1723  * Add a transaction entry to lcbdata->sc_trans for this property_t.  Uses
   1724  * sc_handle, sc_trans, and sc_flags (SCI_NOENABLED) in lcbdata.  On success,
   1725  * returns UU_WALK_NEXT.  On error returns UU_WALK_ERROR and sets
   1726  * lcbdata->sc_err to
   1727  *   ENOMEM - out of memory
   1728  *   ECONNABORTED - repository connection broken
   1729  *   ECANCELED - sc_trans's property group was deleted
   1730  *   EINVAL - p's name is invalid (error printed)
   1731  *	    - p has an invalid value (error printed)
   1732  */
   1733 static int
   1734 lscf_property_import(void *v, void *pvt)
   1735 {
   1736 	property_t *p = v;
   1737 	scf_callback_t *lcbdata = pvt;
   1738 	value_t *vp;
   1739 	scf_transaction_t *trans = lcbdata->sc_trans;
   1740 	scf_transaction_entry_t *entr;
   1741 	scf_value_t *val;
   1742 	scf_type_t tp;
   1743 
   1744 	if ((lcbdata->sc_flags & SCI_NOENABLED ||
   1745 	    lcbdata->sc_flags & SCI_DELAYENABLE) &&
   1746 	    strcmp(p->sc_property_name, SCF_PROPERTY_ENABLED) == 0) {
   1747 		lcbdata->sc_enable = p;
   1748 		return (UU_WALK_NEXT);
   1749 	}
   1750 
   1751 	entr = scf_entry_create(lcbdata->sc_handle);
   1752 	if (entr == NULL) {
   1753 		switch (scf_error()) {
   1754 		case SCF_ERROR_NO_MEMORY:
   1755 			return (stash_scferror(lcbdata));
   1756 
   1757 		case SCF_ERROR_INVALID_ARGUMENT:
   1758 		default:
   1759 			bad_error("scf_entry_create", scf_error());
   1760 		}
   1761 	}
   1762 
   1763 	tp = p->sc_value_type;
   1764 
   1765 	if (scf_transaction_property_new(trans, entr,
   1766 	    p->sc_property_name, tp) != 0) {
   1767 		switch (scf_error()) {
   1768 		case SCF_ERROR_INVALID_ARGUMENT:
   1769 			semerr(emsg_invalid_prop_name, p->sc_property_name);
   1770 			scf_entry_destroy(entr);
   1771 			return (stash_scferror(lcbdata));
   1772 
   1773 		case SCF_ERROR_EXISTS:
   1774 			break;
   1775 
   1776 		case SCF_ERROR_DELETED:
   1777 		case SCF_ERROR_CONNECTION_BROKEN:
   1778 			scf_entry_destroy(entr);
   1779 			return (stash_scferror(lcbdata));
   1780 
   1781 		case SCF_ERROR_NOT_BOUND:
   1782 		case SCF_ERROR_HANDLE_MISMATCH:
   1783 		case SCF_ERROR_NOT_SET:
   1784 		default:
   1785 			bad_error("scf_transaction_property_new", scf_error());
   1786 		}
   1787 
   1788 		if (scf_transaction_property_change_type(trans, entr,
   1789 		    p->sc_property_name, tp) != 0) {
   1790 			switch (scf_error()) {
   1791 			case SCF_ERROR_DELETED:
   1792 			case SCF_ERROR_CONNECTION_BROKEN:
   1793 				scf_entry_destroy(entr);
   1794 				return (stash_scferror(lcbdata));
   1795 
   1796 			case SCF_ERROR_INVALID_ARGUMENT:
   1797 				semerr(emsg_invalid_prop_name,
   1798 				    p->sc_property_name);
   1799 				scf_entry_destroy(entr);
   1800 				return (stash_scferror(lcbdata));
   1801 
   1802 			case SCF_ERROR_NOT_FOUND:
   1803 			case SCF_ERROR_NOT_SET:
   1804 			case SCF_ERROR_HANDLE_MISMATCH:
   1805 			case SCF_ERROR_NOT_BOUND:
   1806 			default:
   1807 				bad_error(
   1808 				    "scf_transaction_property_change_type",
   1809 				    scf_error());
   1810 			}
   1811 		}
   1812 	}
   1813 
   1814 	for (vp = uu_list_first(p->sc_property_values);
   1815 	    vp != NULL;
   1816 	    vp = uu_list_next(p->sc_property_values, vp)) {
   1817 		val = scf_value_create(g_hndl);
   1818 		if (val == NULL) {
   1819 			switch (scf_error()) {
   1820 			case SCF_ERROR_NO_MEMORY:
   1821 				return (stash_scferror(lcbdata));
   1822 
   1823 			case SCF_ERROR_INVALID_ARGUMENT:
   1824 			default:
   1825 				bad_error("scf_value_create", scf_error());
   1826 			}
   1827 		}
   1828 
   1829 		switch (tp) {
   1830 		case SCF_TYPE_BOOLEAN:
   1831 			scf_value_set_boolean(val, vp->sc_u.sc_count);
   1832 			break;
   1833 		case SCF_TYPE_COUNT:
   1834 			scf_value_set_count(val, vp->sc_u.sc_count);
   1835 			break;
   1836 		case SCF_TYPE_INTEGER:
   1837 			scf_value_set_integer(val, vp->sc_u.sc_integer);
   1838 			break;
   1839 		default:
   1840 			assert(vp->sc_u.sc_string != NULL);
   1841 			if (scf_value_set_from_string(val, tp,
   1842 			    vp->sc_u.sc_string) != 0) {
   1843 				if (scf_error() != SCF_ERROR_INVALID_ARGUMENT)
   1844 					bad_error("scf_value_set_from_string",
   1845 					    scf_error());
   1846 
   1847 				warn(gettext("Value \"%s\" is not a valid "
   1848 				    "%s.\n"), vp->sc_u.sc_string,
   1849 				    scf_type_to_string(tp));
   1850 				scf_value_destroy(val);
   1851 				return (stash_scferror(lcbdata));
   1852 			}
   1853 			break;
   1854 		}
   1855 
   1856 		if (scf_entry_add_value(entr, val) != 0)
   1857 			bad_error("scf_entry_add_value", scf_error());
   1858 	}
   1859 
   1860 	return (UU_WALK_NEXT);
   1861 }
   1862 
   1863 /*
   1864  * Import a pgroup_t into the repository.  Uses sc_handle, sc_parent,
   1865  * sc_service, sc_flags (SCI_GENERALLAST, SCI_FORCE, & SCI_KEEP),
   1866  * sc_source_fmri, and sc_target_fmri in lcbdata, and uses imp_pg and imp_tx.
   1867  * On success, returns UU_WALK_NEXT.  On error returns UU_WALK_ERROR and sets
   1868  * lcbdata->sc_err to
   1869  *   ECONNABORTED - repository connection broken
   1870  *   ENOMEM - out of memory
   1871  *   ENOSPC - svc.configd is out of resources
   1872  *   ECANCELED - sc_parent was deleted
   1873  *   EPERM - could not create property group (permission denied) (error printed)
   1874  *	   - could not modify property group (permission denied) (error printed)
   1875  *	   - could not delete property group (permission denied) (error	printed)
   1876  *   EROFS - could not create property group (repository is read-only)
   1877  *	   - could not delete property group (repository is read-only)
   1878  *   EACCES - could not create property group (backend access denied)
   1879  *	    - could not delete property group (backend access denied)
   1880  *   EEXIST - could not create property group (already exists)
   1881  *   EINVAL - invalid property group name (error printed)
   1882  *	    - invalid property name (error printed)
   1883  *	    - invalid value (error printed)
   1884  *   EBUSY - new property group deleted (error printed)
   1885  *	   - new property group changed (error printed)
   1886  *	   - property group added (error printed)
   1887  *	   - property group deleted (error printed)
   1888  */
   1889 static int
   1890 entity_pgroup_import(void *v, void *pvt)
   1891 {
   1892 	pgroup_t *p = v;
   1893 	scf_callback_t cbdata;
   1894 	scf_callback_t *lcbdata = pvt;
   1895 	void *ent = lcbdata->sc_parent;
   1896 	int issvc = lcbdata->sc_service;
   1897 	int r;
   1898 
   1899 	const char * const pg_changed = gettext("%s changed unexpectedly "
   1900 	    "(new property group \"%s\" changed).\n");
   1901 
   1902 	/* Never import deleted property groups. */
   1903 	if (p->sc_pgroup_delete)
   1904 		return (UU_WALK_NEXT);
   1905 
   1906 	if (!issvc && (lcbdata->sc_flags & SCI_GENERALLAST) &&
   1907 	    strcmp(p->sc_pgroup_name, SCF_PG_GENERAL) == 0) {
   1908 		lcbdata->sc_general = p;
   1909 		return (UU_WALK_NEXT);
   1910 	}
   1911 
   1912 add_pg:
   1913 	if (issvc)
   1914 		r = scf_service_add_pg(ent, p->sc_pgroup_name,
   1915 		    p->sc_pgroup_type, p->sc_pgroup_flags, imp_pg);
   1916 	else
   1917 		r = scf_instance_add_pg(ent, p->sc_pgroup_name,
   1918 		    p->sc_pgroup_type, p->sc_pgroup_flags, imp_pg);
   1919 	if (r != 0) {
   1920 		switch (scf_error()) {
   1921 		case SCF_ERROR_DELETED:
   1922 		case SCF_ERROR_CONNECTION_BROKEN:
   1923 		case SCF_ERROR_BACKEND_READONLY:
   1924 		case SCF_ERROR_BACKEND_ACCESS:
   1925 		case SCF_ERROR_NO_RESOURCES:
   1926 			return (stash_scferror(lcbdata));
   1927 
   1928 		case SCF_ERROR_EXISTS:
   1929 			if (lcbdata->sc_flags & SCI_FORCE)
   1930 				break;
   1931 			return (stash_scferror(lcbdata));
   1932 
   1933 		case SCF_ERROR_INVALID_ARGUMENT:
   1934 			warn(emsg_fmri_invalid_pg_name_type,
   1935 			    lcbdata->sc_source_fmri,
   1936 			    p->sc_pgroup_name, p->sc_pgroup_type);
   1937 			return (stash_scferror(lcbdata));
   1938 
   1939 		case SCF_ERROR_PERMISSION_DENIED:
   1940 			warn(emsg_pg_add_perm, p->sc_pgroup_name,
   1941 			    lcbdata->sc_target_fmri);
   1942 			return (stash_scferror(lcbdata));
   1943 
   1944 		case SCF_ERROR_NOT_BOUND:
   1945 		case SCF_ERROR_HANDLE_MISMATCH:
   1946 		case SCF_ERROR_NOT_SET:
   1947 		default:
   1948 			bad_error("scf_service_add_pg", scf_error());
   1949 		}
   1950 
   1951 		if (entity_get_pg(ent, issvc, p->sc_pgroup_name, imp_pg) != 0) {
   1952 			switch (scf_error()) {
   1953 			case SCF_ERROR_CONNECTION_BROKEN:
   1954 			case SCF_ERROR_DELETED:
   1955 				return (stash_scferror(lcbdata));
   1956 
   1957 			case SCF_ERROR_INVALID_ARGUMENT:
   1958 				warn(emsg_fmri_invalid_pg_name,
   1959 				    lcbdata->sc_source_fmri,
   1960 				    p->sc_pgroup_name);
   1961 				return (stash_scferror(lcbdata));
   1962 
   1963 			case SCF_ERROR_NOT_FOUND:
   1964 				warn(emsg_pg_deleted, lcbdata->sc_target_fmri,
   1965 				    p->sc_pgroup_name);
   1966 				lcbdata->sc_err = EBUSY;
   1967 				return (UU_WALK_ERROR);
   1968 
   1969 			case SCF_ERROR_NOT_BOUND:
   1970 			case SCF_ERROR_HANDLE_MISMATCH:
   1971 			case SCF_ERROR_NOT_SET:
   1972 			default:
   1973 				bad_error("entity_get_pg", scf_error());
   1974 			}
   1975 		}
   1976 
   1977 		if (lcbdata->sc_flags & SCI_KEEP)
   1978 			goto props;
   1979 
   1980 		if (scf_pg_delete(imp_pg) != 0) {
   1981 			switch (scf_error()) {
   1982 			case SCF_ERROR_DELETED:
   1983 				warn(emsg_pg_deleted, lcbdata->sc_target_fmri,
   1984 				    p->sc_pgroup_name);
   1985 				lcbdata->sc_err = EBUSY;
   1986 				return (UU_WALK_ERROR);
   1987 
   1988 			case SCF_ERROR_PERMISSION_DENIED:
   1989 				warn(emsg_pg_del_perm, p->sc_pgroup_name,
   1990 				    lcbdata->sc_target_fmri);
   1991 				return (stash_scferror(lcbdata));
   1992 
   1993 			case SCF_ERROR_BACKEND_READONLY:
   1994 			case SCF_ERROR_BACKEND_ACCESS:
   1995 			case SCF_ERROR_CONNECTION_BROKEN:
   1996 				return (stash_scferror(lcbdata));
   1997 
   1998 			case SCF_ERROR_NOT_SET:
   1999 			default:
   2000 				bad_error("scf_pg_delete", scf_error());
   2001 			}
   2002 		}
   2003 
   2004 		goto add_pg;
   2005 	}
   2006 
   2007 props:
   2008 
   2009 	/*
   2010 	 * Add properties to property group, if any.
   2011 	 */
   2012 	cbdata.sc_handle = lcbdata->sc_handle;
   2013 	cbdata.sc_parent = imp_pg;
   2014 	cbdata.sc_flags = lcbdata->sc_flags;
   2015 	cbdata.sc_trans = imp_tx;
   2016 	cbdata.sc_enable = NULL;
   2017 
   2018 	if (scf_transaction_start(imp_tx, imp_pg) != 0) {
   2019 		switch (scf_error()) {
   2020 		case SCF_ERROR_BACKEND_ACCESS:
   2021 		case SCF_ERROR_BACKEND_READONLY:
   2022 		case SCF_ERROR_CONNECTION_BROKEN:
   2023 			return (stash_scferror(lcbdata));
   2024 
   2025 		case SCF_ERROR_DELETED:
   2026 			warn(pg_changed, lcbdata->sc_target_fmri,
   2027 			    p->sc_pgroup_name);
   2028 			lcbdata->sc_err = EBUSY;
   2029 			return (UU_WALK_ERROR);
   2030 
   2031 		case SCF_ERROR_PERMISSION_DENIED:
   2032 			warn(emsg_pg_mod_perm, p->sc_pgroup_name,
   2033 			    lcbdata->sc_target_fmri);
   2034 			return (stash_scferror(lcbdata));
   2035 
   2036 		case SCF_ERROR_NOT_BOUND:
   2037 		case SCF_ERROR_NOT_SET:
   2038 		case SCF_ERROR_IN_USE:
   2039 		case SCF_ERROR_HANDLE_MISMATCH:
   2040 		default:
   2041 			bad_error("scf_transaction_start", scf_error());
   2042 		}
   2043 	}
   2044 
   2045 	if (uu_list_walk(p->sc_pgroup_props, lscf_property_import, &cbdata,
   2046 	    UU_DEFAULT) != 0) {
   2047 		if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   2048 			bad_error("uu_list_walk", uu_error());
   2049 		scf_transaction_reset(imp_tx);
   2050 
   2051 		lcbdata->sc_err = cbdata.sc_err;
   2052 		if (cbdata.sc_err == ECANCELED) {
   2053 			warn(pg_changed, lcbdata->sc_target_fmri,
   2054 			    p->sc_pgroup_name);
   2055 			lcbdata->sc_err = EBUSY;
   2056 		}
   2057 		return (UU_WALK_ERROR);
   2058 	}
   2059 
   2060 	if ((lcbdata->sc_flags & SCI_DELAYENABLE) && cbdata.sc_enable) {
   2061 		cbdata.sc_flags = cbdata.sc_flags & (~SCI_DELAYENABLE);
   2062 
   2063 		/*
   2064 		 * take the snapshot running snapshot then
   2065 		 * import the stored general/enable property
   2066 		 */
   2067 		r = take_snap(ent, snap_running, imp_rsnap);
   2068 		switch (r) {
   2069 		case 0:
   2070 			break;
   2071 
   2072 		case ECONNABORTED:
   2073 			warn(gettext("Could not take %s snapshot on import "
   2074 			    "(repository connection broken).\n"),
   2075 			    snap_running);
   2076 			lcbdata->sc_err = r;
   2077 			return (UU_WALK_ERROR);
   2078 		case ECANCELED:
   2079 			warn(emsg_deleted);
   2080 			lcbdata->sc_err = r;
   2081 			return (UU_WALK_ERROR);
   2082 
   2083 		case EPERM:
   2084 			warn(gettext("Could not take %s snapshot "
   2085 			    "(permission denied).\n"), snap_running);
   2086 			lcbdata->sc_err = r;
   2087 			return (UU_WALK_ERROR);
   2088 
   2089 		case ENOSPC:
   2090 			warn(gettext("Could not take %s snapshot"
   2091 			    "(repository server out of resources).\n"),
   2092 			    snap_running);
   2093 			lcbdata->sc_err = r;
   2094 			return (UU_WALK_ERROR);
   2095 
   2096 		default:
   2097 			bad_error("take_snap", r);
   2098 		}
   2099 
   2100 		r = lscf_property_import(cbdata.sc_enable, &cbdata);
   2101 		if (r != UU_WALK_NEXT) {
   2102 			if (r != UU_WALK_ERROR)
   2103 				bad_error("lscf_property_import", r);
   2104 			return (EINVAL);
   2105 		}
   2106 	}
   2107 
   2108 	r = scf_transaction_commit(imp_tx);
   2109 	switch (r) {
   2110 	case 1:
   2111 		r = UU_WALK_NEXT;
   2112 		break;
   2113 
   2114 	case 0:
   2115 		warn(pg_changed, lcbdata->sc_target_fmri, p->sc_pgroup_name);
   2116 		lcbdata->sc_err = EBUSY;
   2117 		r = UU_WALK_ERROR;
   2118 		break;
   2119 
   2120 	case -1:
   2121 		switch (scf_error()) {
   2122 		case SCF_ERROR_BACKEND_READONLY:
   2123 		case SCF_ERROR_BACKEND_ACCESS:
   2124 		case SCF_ERROR_CONNECTION_BROKEN:
   2125 		case SCF_ERROR_NO_RESOURCES:
   2126 			r = stash_scferror(lcbdata);
   2127 			break;
   2128 
   2129 		case SCF_ERROR_DELETED:
   2130 			warn(emsg_pg_deleted, lcbdata->sc_target_fmri,
   2131 			    p->sc_pgroup_name);
   2132 			lcbdata->sc_err = EBUSY;
   2133 			r = UU_WALK_ERROR;
   2134 			break;
   2135 
   2136 		case SCF_ERROR_PERMISSION_DENIED:
   2137 			warn(emsg_pg_mod_perm, p->sc_pgroup_name,
   2138 			    lcbdata->sc_target_fmri);
   2139 			r = stash_scferror(lcbdata);
   2140 			break;
   2141 
   2142 		case SCF_ERROR_NOT_SET:
   2143 		case SCF_ERROR_INVALID_ARGUMENT:
   2144 		case SCF_ERROR_NOT_BOUND:
   2145 		default:
   2146 			bad_error("scf_transaction_commit", scf_error());
   2147 		}
   2148 		break;
   2149 
   2150 	default:
   2151 		bad_error("scf_transaction_commit", r);
   2152 	}
   2153 
   2154 	scf_transaction_destroy_children(imp_tx);
   2155 
   2156 	return (r);
   2157 }
   2158 
   2159 /*
   2160  * Returns
   2161  *   0 - success
   2162  *   ECONNABORTED - repository connection broken
   2163  *   ENOMEM - out of memory
   2164  *   ENOSPC - svc.configd is out of resources
   2165  *   ECANCELED - inst was deleted
   2166  *   EPERM - could not create property group (permission denied) (error printed)
   2167  *	   - could not modify property group (permission denied) (error printed)
   2168  *   EROFS - could not create property group (repository is read-only)
   2169  *   EACCES - could not create property group (backend access denied)
   2170  *   EEXIST - could not create property group (already exists)
   2171  *   EINVAL - invalid property group name (error printed)
   2172  *	    - invalid property name (error printed)
   2173  *	    - invalid value (error printed)
   2174  *   EBUSY - new property group changed (error printed)
   2175  */
   2176 static int
   2177 lscf_import_service_pgs(scf_service_t *svc, const char *target_fmri,
   2178     const entity_t *isvc, int flags)
   2179 {
   2180 	scf_callback_t cbdata;
   2181 
   2182 	cbdata.sc_handle = scf_service_handle(svc);
   2183 	cbdata.sc_parent = svc;
   2184 	cbdata.sc_service = 1;
   2185 	cbdata.sc_general = 0;
   2186 	cbdata.sc_enable = 0;
   2187 	cbdata.sc_flags = flags;
   2188 	cbdata.sc_source_fmri = isvc->sc_fmri;
   2189 	cbdata.sc_target_fmri = target_fmri;
   2190 
   2191 	if (uu_list_walk(isvc->sc_pgroups, entity_pgroup_import, &cbdata,
   2192 	    UU_DEFAULT) != 0) {
   2193 		if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   2194 			bad_error("uu_list_walk", uu_error());
   2195 
   2196 		return (cbdata.sc_err);
   2197 	}
   2198 
   2199 	return (0);
   2200 }
   2201 
   2202 /*
   2203  * Returns
   2204  *   0 - success
   2205  *   ECONNABORTED - repository connection broken
   2206  *   ENOMEM - out of memory
   2207  *   ENOSPC - svc.configd is out of resources
   2208  *   ECANCELED - inst was deleted
   2209  *   EPERM - could not create property group (permission denied) (error printed)
   2210  *	   - could not modify property group (permission denied) (error printed)
   2211  *   EROFS - could not create property group (repository is read-only)
   2212  *   EACCES - could not create property group (backend access denied)
   2213  *   EEXIST - could not create property group (already exists)
   2214  *   EINVAL - invalid property group name (error printed)
   2215  *	    - invalid property name (error printed)
   2216  *	    - invalid value (error printed)
   2217  *   EBUSY - new property group changed (error printed)
   2218  */
   2219 static int
   2220 lscf_import_instance_pgs(scf_instance_t *inst, const char *target_fmri,
   2221     const entity_t *iinst, int flags)
   2222 {
   2223 	scf_callback_t cbdata;
   2224 
   2225 	cbdata.sc_handle = scf_instance_handle(inst);
   2226 	cbdata.sc_parent = inst;
   2227 	cbdata.sc_service = 0;
   2228 	cbdata.sc_general = NULL;
   2229 	cbdata.sc_enable = NULL;
   2230 	cbdata.sc_flags = flags;
   2231 	cbdata.sc_source_fmri = iinst->sc_fmri;
   2232 	cbdata.sc_target_fmri = target_fmri;
   2233 
   2234 	if (uu_list_walk(iinst->sc_pgroups, entity_pgroup_import, &cbdata,
   2235 	    UU_DEFAULT) != 0) {
   2236 		if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   2237 			bad_error("uu_list_walk", uu_error());
   2238 
   2239 		return (cbdata.sc_err);
   2240 	}
   2241 
   2242 	if ((flags & SCI_GENERALLAST) && cbdata.sc_general) {
   2243 		cbdata.sc_flags = flags & (~SCI_GENERALLAST);
   2244 		/*
   2245 		 * If importing with the SCI_NOENABLED flag then
   2246 		 * skip the delay, but if not then add the delay
   2247 		 * of the enable property.
   2248 		 */
   2249 		if (!(cbdata.sc_flags & SCI_NOENABLED)) {
   2250 			cbdata.sc_flags |= SCI_DELAYENABLE;
   2251 		}
   2252 
   2253 		if (entity_pgroup_import(cbdata.sc_general, &cbdata)
   2254 		    != UU_WALK_NEXT)
   2255 			return (cbdata.sc_err);
   2256 	}
   2257 
   2258 	return (0);
   2259 }
   2260 
   2261 /*
   2262  * Report the reasons why we can't upgrade pg2 to pg1.
   2263  */
   2264 static void
   2265 report_pg_diffs(const pgroup_t *pg1, const pgroup_t *pg2, const char *fmri,
   2266     int new)
   2267 {
   2268 	property_t *p1, *p2;
   2269 
   2270 	assert(strcmp(pg1->sc_pgroup_name, pg2->sc_pgroup_name) == 0);
   2271 
   2272 	if (!pg_attrs_equal(pg1, pg2, fmri, new))
   2273 		return;
   2274 
   2275 	for (p1 = uu_list_first(pg1->sc_pgroup_props);
   2276 	    p1 != NULL;
   2277 	    p1 = uu_list_next(pg1->sc_pgroup_props, p1)) {
   2278 		p2 = uu_list_find(pg2->sc_pgroup_props, p1, NULL, NULL);
   2279 		if (p2 != NULL) {
   2280 			(void) prop_equal(p1, p2, fmri, pg1->sc_pgroup_name,
   2281 			    new);
   2282 			continue;
   2283 		}
   2284 
   2285 		if (new)
   2286 			warn(gettext("Conflict upgrading %s (new property "
   2287 			    "group \"%s\" is missing property \"%s\").\n"),
   2288 			    fmri, pg1->sc_pgroup_name, p1->sc_property_name);
   2289 		else
   2290 			warn(gettext("Conflict upgrading %s (property "
   2291 			    "\"%s/%s\" is missing).\n"), fmri,
   2292 			    pg1->sc_pgroup_name, p1->sc_property_name);
   2293 	}
   2294 
   2295 	/*
   2296 	 * Since pg1 should be from the manifest, any properties in pg2 which
   2297 	 * aren't in pg1 shouldn't be reported as conflicts.
   2298 	 */
   2299 }
   2300 
   2301 /*
   2302  * Add transaction entries to tx which will upgrade cur's pg according to old
   2303  * & new.
   2304  *
   2305  * Returns
   2306  *   0 - success
   2307  *   EINVAL - new has a property with an invalid name or value (message emitted)
   2308  *   ENOMEM - out of memory
   2309  */
   2310 static int
   2311 add_upgrade_entries(scf_transaction_t *tx, pgroup_t *old, pgroup_t *new,
   2312     pgroup_t *cur, int speak, const char *fmri)
   2313 {
   2314 	property_t *p, *new_p, *cur_p;
   2315 	scf_transaction_entry_t *e;
   2316 	int r;
   2317 	int is_general;
   2318 	int is_protected;
   2319 
   2320 	if (uu_list_walk(new->sc_pgroup_props, clear_int,
   2321 	    (void *)offsetof(property_t, sc_seen), UU_DEFAULT) != 0)
   2322 		bad_error("uu_list_walk", uu_error());
   2323 
   2324 	is_general = strcmp(old->sc_pgroup_name, SCF_PG_GENERAL) == 0;
   2325 
   2326 	for (p = uu_list_first(old->sc_pgroup_props);
   2327 	    p != NULL;
   2328 	    p = uu_list_next(old->sc_pgroup_props, p)) {
   2329 		/* p is a property in the old property group. */
   2330 
   2331 		/* Protect live properties. */
   2332 		is_protected = 0;
   2333 		if (is_general) {
   2334 			if (strcmp(p->sc_property_name, SCF_PROPERTY_ENABLED) ==
   2335 			    0 ||
   2336 			    strcmp(p->sc_property_name,
   2337 			    SCF_PROPERTY_RESTARTER) == 0)
   2338 				is_protected = 1;
   2339 		}
   2340 
   2341 		/* Look for the same property in the new properties. */
   2342 		new_p = uu_list_find(new->sc_pgroup_props, p, NULL, NULL);
   2343 		if (new_p != NULL) {
   2344 			new_p->sc_seen = 1;
   2345 
   2346 			/*
   2347 			 * If the new property is the same as the old, don't do
   2348 			 * anything (leave any user customizations).
   2349 			 */
   2350 			if (prop_equal(p, new_p, NULL, NULL, 0))
   2351 				continue;
   2352 
   2353 			if (new_p->sc_property_override)
   2354 				goto upgrade;
   2355 		}
   2356 
   2357 		cur_p = uu_list_find(cur->sc_pgroup_props, p, NULL, NULL);
   2358 		if (cur_p == NULL) {
   2359 			/*
   2360 			 * p has been deleted from the repository.  If we were
   2361 			 * going to delete it anyway, do nothing.  Otherwise
   2362 			 * report a conflict.
   2363 			 */
   2364 			if (new_p == NULL)
   2365 				continue;
   2366 
   2367 			if (is_protected)
   2368 				continue;
   2369 
   2370 			warn(gettext("Conflict upgrading %s "
   2371 			    "(property \"%s/%s\" is missing).\n"), fmri,
   2372 			    old->sc_pgroup_name, p->sc_property_name);
   2373 			continue;
   2374 		}
   2375 
   2376 		if (!prop_equal(p, cur_p, NULL, NULL, 0)) {
   2377 			/*
   2378 			 * Conflict.  Don't warn if the property is already the
   2379 			 * way we want it, though.
   2380 			 */
   2381 			if (is_protected)
   2382 				continue;
   2383 
   2384 			if (new_p == NULL)
   2385 				(void) prop_equal(p, cur_p, fmri,
   2386 				    old->sc_pgroup_name, 0);
   2387 			else
   2388 				(void) prop_equal(cur_p, new_p, fmri,
   2389 				    old->sc_pgroup_name, 0);
   2390 			continue;
   2391 		}
   2392 
   2393 		if (is_protected) {
   2394 			if (speak)
   2395 				warn(gettext("%s: Refusing to upgrade "
   2396 				    "\"%s/%s\" (live property).\n"), fmri,
   2397 				    old->sc_pgroup_name, p->sc_property_name);
   2398 			continue;
   2399 		}
   2400 
   2401 upgrade:
   2402 		/* p hasn't been customized in the repository.  Upgrade it. */
   2403 		if (new_p == NULL) {
   2404 			/* p was deleted.  Delete from cur if unchanged. */
   2405 			if (speak)
   2406 				warn(gettext(
   2407 				    "%s: Deleting property \"%s/%s\".\n"),
   2408 				    fmri, old->sc_pgroup_name,
   2409 				    p->sc_property_name);
   2410 
   2411 			e = scf_entry_create(g_hndl);
   2412 			if (e == NULL)
   2413 				return (ENOMEM);
   2414 
   2415 			if (scf_transaction_property_delete(tx, e,
   2416 			    p->sc_property_name) != 0) {
   2417 				switch (scf_error()) {
   2418 				case SCF_ERROR_DELETED:
   2419 					scf_entry_destroy(e);
   2420 					return (ECANCELED);
   2421 
   2422 				case SCF_ERROR_CONNECTION_BROKEN:
   2423 					scf_entry_destroy(e);
   2424 					return (ECONNABORTED);
   2425 
   2426 				case SCF_ERROR_NOT_FOUND:
   2427 					/*
   2428 					 * This can happen if cur is from the
   2429 					 * running snapshot (and it differs
   2430 					 * from the live properties).
   2431 					 */
   2432 					scf_entry_destroy(e);
   2433 					break;
   2434 
   2435 				case SCF_ERROR_HANDLE_MISMATCH:
   2436 				case SCF_ERROR_NOT_BOUND:
   2437 				case SCF_ERROR_NOT_SET:
   2438 				case SCF_ERROR_INVALID_ARGUMENT:
   2439 				default:
   2440 					bad_error(
   2441 					    "scf_transaction_property_delete",
   2442 					    scf_error());
   2443 				}
   2444 			}
   2445 		} else {
   2446 			scf_callback_t ctx;
   2447 
   2448 			if (speak)
   2449 				warn(gettext(
   2450 				    "%s: Upgrading property \"%s/%s\".\n"),
   2451 				    fmri, old->sc_pgroup_name,
   2452 				    p->sc_property_name);
   2453 
   2454 			ctx.sc_handle = g_hndl;
   2455 			ctx.sc_trans = tx;
   2456 			ctx.sc_flags = 0;
   2457 
   2458 			r = lscf_property_import(new_p, &ctx);
   2459 			if (r != UU_WALK_NEXT) {
   2460 				if (r != UU_WALK_ERROR)
   2461 					bad_error("lscf_property_import", r);
   2462 				return (EINVAL);
   2463 			}
   2464 		}
   2465 	}
   2466 
   2467 	/* Go over the properties which were added. */
   2468 	for (new_p = uu_list_first(new->sc_pgroup_props);
   2469 	    new_p != NULL;
   2470 	    new_p = uu_list_next(new->sc_pgroup_props, new_p)) {
   2471 		if (new_p->sc_seen)
   2472 			continue;
   2473 
   2474 		/* This is a new property. */
   2475 		cur_p = uu_list_find(cur->sc_pgroup_props, new_p, NULL, NULL);
   2476 		if (cur_p == NULL) {
   2477 			scf_callback_t ctx;
   2478 
   2479 			ctx.sc_handle = g_hndl;
   2480 			ctx.sc_trans = tx;
   2481 			ctx.sc_flags = 0;
   2482 
   2483 			r = lscf_property_import(new_p, &ctx);
   2484 			if (r != UU_WALK_NEXT) {
   2485 				if (r != UU_WALK_ERROR)
   2486 					bad_error("lscf_property_import", r);
   2487 				return (EINVAL);
   2488 			}
   2489 			continue;
   2490 		}
   2491 
   2492 		/*
   2493 		 * Report a conflict if the new property differs from the
   2494 		 * current one.  Unless it's general/enabled, since that's
   2495 		 * never in the last-import snapshot.
   2496 		 */
   2497 		if (strcmp(new_p->sc_property_name, SCF_PROPERTY_ENABLED) ==
   2498 		    0 &&
   2499 		    strcmp(cur->sc_pgroup_name, SCF_PG_GENERAL) == 0)
   2500 			continue;
   2501 
   2502 		(void) prop_equal(cur_p, new_p, fmri, old->sc_pgroup_name, 1);
   2503 	}
   2504 
   2505 	return (0);
   2506 }
   2507 
   2508 /*
   2509  * Upgrade pg according to old & new.
   2510  *
   2511  * Returns
   2512  *   0 - success
   2513  *   ECONNABORTED - repository connection broken
   2514  *   ENOMEM - out of memory
   2515  *   ENOSPC - svc.configd is out of resources
   2516  *   ECANCELED - pg was deleted
   2517  *   EPERM - couldn't modify pg (permission denied)
   2518  *   EROFS - couldn't modify pg (backend read-only)
   2519  *   EACCES - couldn't modify pg (backend access denied)
   2520  *   EINVAL - new has a property with invalid name or value (error printed)
   2521  *   EBUSY - pg changed unexpectedly
   2522  */
   2523 static int
   2524 upgrade_pg(scf_propertygroup_t *pg, pgroup_t *cur, pgroup_t *old,
   2525     pgroup_t *new, int speak, const char *fmri)
   2526 {
   2527 	int r;
   2528 
   2529 	if (scf_transaction_start(imp_tx, pg) != 0) {
   2530 		switch (scf_error()) {
   2531 		case SCF_ERROR_CONNECTION_BROKEN:
   2532 		case SCF_ERROR_DELETED:
   2533 		case SCF_ERROR_PERMISSION_DENIED:
   2534 		case SCF_ERROR_BACKEND_READONLY:
   2535 		case SCF_ERROR_BACKEND_ACCESS:
   2536 			return (scferror2errno(scf_error()));
   2537 
   2538 		case SCF_ERROR_HANDLE_MISMATCH:
   2539 		case SCF_ERROR_IN_USE:
   2540 		case SCF_ERROR_NOT_BOUND:
   2541 		case SCF_ERROR_NOT_SET:
   2542 		default:
   2543 			bad_error("scf_transaction_start", scf_error());
   2544 		}
   2545 	}
   2546 
   2547 	r = add_upgrade_entries(imp_tx, old, new, cur, speak, fmri);
   2548 	switch (r) {
   2549 	case 0:
   2550 		break;
   2551 
   2552 	case EINVAL:
   2553 	case ENOMEM:
   2554 		scf_transaction_destroy_children(imp_tx);
   2555 		return (r);
   2556 
   2557 	default:
   2558 		bad_error("add_upgrade_entries", r);
   2559 	}
   2560 
   2561 	r = scf_transaction_commit(imp_tx);
   2562 
   2563 	scf_transaction_destroy_children(imp_tx);
   2564 
   2565 	switch (r) {
   2566 	case 1:
   2567 		break;
   2568 
   2569 	case 0:
   2570 		return (EBUSY);
   2571 
   2572 	case -1:
   2573 		switch (scf_error()) {
   2574 		case SCF_ERROR_CONNECTION_BROKEN:
   2575 		case SCF_ERROR_NO_RESOURCES:
   2576 		case SCF_ERROR_PERMISSION_DENIED:
   2577 		case SCF_ERROR_BACKEND_READONLY:
   2578 		case SCF_ERROR_BACKEND_ACCESS:
   2579 		case SCF_ERROR_DELETED:
   2580 			return (scferror2errno(scf_error()));
   2581 
   2582 		case SCF_ERROR_NOT_BOUND:
   2583 		case SCF_ERROR_INVALID_ARGUMENT:
   2584 		case SCF_ERROR_NOT_SET:
   2585 		default:
   2586 			bad_error("scf_transaction_commit", scf_error());
   2587 		}
   2588 
   2589 	default:
   2590 		bad_error("scf_transaction_commit", r);
   2591 	}
   2592 
   2593 	return (0);
   2594 }
   2595 
   2596 /*
   2597  * Compares two entity FMRIs.  Returns
   2598  *
   2599  *   1 - equal
   2600  *   0 - not equal
   2601  *   -1 - f1 is invalid or not an entity
   2602  *   -2 - f2 is invalid or not an entity
   2603  */
   2604 static int
   2605 fmri_equal(const char *f1, const char *f2)
   2606 {
   2607 	int r;
   2608 	const char *s1, *i1, *pg1;
   2609 	const char *s2, *i2, *pg2;
   2610 
   2611 	if (strlcpy(imp_fe1, f1, max_scf_fmri_len + 1) >= max_scf_fmri_len + 1)
   2612 		return (-1);
   2613 	if (scf_parse_svc_fmri(imp_fe1, NULL, &s1, &i1, &pg1, NULL) != 0)
   2614 		return (-1);
   2615 
   2616 	if (s1 == NULL || pg1 != NULL)
   2617 		return (-1);
   2618 
   2619 	if (strlcpy(imp_fe2, f2, max_scf_fmri_len + 1) >= max_scf_fmri_len + 1)
   2620 		return (-2);
   2621 	if (scf_parse_svc_fmri(imp_fe2, NULL, &s2, &i2, &pg2, NULL) != 0)
   2622 		return (-2);
   2623 
   2624 	if (s2 == NULL || pg2 != NULL)
   2625 		return (-2);
   2626 
   2627 	r = strcmp(s1, s2);
   2628 	if (r != 0)
   2629 		return (0);
   2630 
   2631 	if (i1 == NULL && i2 == NULL)
   2632 		return (1);
   2633 
   2634 	if (i1 == NULL || i2 == NULL)
   2635 		return (0);
   2636 
   2637 	return (strcmp(i1, i2) == 0);
   2638 }
   2639 
   2640 /*
   2641  * Import a dependent by creating a dependency property group in the dependent
   2642  * entity.  If lcbdata->sc_trans is set, assume it's been started on the
   2643  * dependents pg, and add an entry to create a new property for this
   2644  * dependent.  Uses sc_handle, sc_trans, and sc_fmri in lcbdata.
   2645  *
   2646  * On success, returns UU_WALK_NEXT.  On error, returns UU_WALK_ERROR and sets
   2647  * lcbdata->sc_err to
   2648  *   ECONNABORTED - repository connection broken
   2649  *   ENOMEM - out of memory
   2650  *   ENOSPC - configd is out of resources
   2651  *   EINVAL - target is invalid (error printed)
   2652  *	    - target is not an entity (error printed)
   2653  *	    - dependent has invalid name (error printed)
   2654  *	    - invalid property name (error printed)
   2655  *	    - invalid value (error printed)
   2656  *	    - scope of target does not exist (error printed)
   2657  *   EPERM - couldn't create target (permission denied) (error printed)
   2658  *	   - couldn't create dependency pg (permission denied) (error printed)
   2659  *	   - couldn't modify dependency pg (permission denied) (error printed)
   2660  *   EROFS - couldn't create target (repository read-only)
   2661  *	   - couldn't create dependency pg (repository read-only)
   2662  *   EACCES - couldn't create target (backend access denied)
   2663  *	    - couldn't create dependency pg (backend access denied)
   2664  *   ECANCELED - sc_trans's pg was deleted
   2665  *   EALREADY - property for dependent already exists in sc_trans's pg
   2666  *   EEXIST - dependency pg already exists in target (error printed)
   2667  *   EBUSY - target deleted (error printed)
   2668  *         - property group changed during import (error printed)
   2669  */
   2670 static int
   2671 lscf_dependent_import(void *a1, void *pvt)
   2672 {
   2673 	pgroup_t *pgrp = a1;
   2674 	scf_callback_t *lcbdata = pvt;
   2675 
   2676 	int isservice;
   2677 	int ret;
   2678 	scf_transaction_entry_t *e;
   2679 	scf_value_t *val;
   2680 	scf_callback_t dependent_cbdata;
   2681 	scf_error_t scfe;
   2682 
   2683 	/*
   2684 	 * Decode the FMRI into dependent_cbdata->sc_parent.  Do it here so if
   2685 	 * it's invalid, we fail before modifying the repository.
   2686 	 */
   2687 	scfe = fmri_to_entity(lcbdata->sc_handle, pgrp->sc_pgroup_fmri,
   2688 	    &dependent_cbdata.sc_parent, &isservice);
   2689 	switch (scfe) {
   2690 	case SCF_ERROR_NONE:
   2691 		break;
   2692 
   2693 	case SCF_ERROR_NO_MEMORY:
   2694 		return (stash_scferror_err(lcbdata, scfe));
   2695 
   2696 	case SCF_ERROR_INVALID_ARGUMENT:
   2697 		semerr(gettext("The FMRI for the \"%s\" dependent is "
   2698 		    "invalid.\n"), pgrp->sc_pgroup_name);
   2699 		return (stash_scferror_err(lcbdata, scfe));
   2700 
   2701 	case SCF_ERROR_CONSTRAINT_VIOLATED:
   2702 		semerr(gettext("The FMRI \"%s\" for the \"%s\" dependent "
   2703 		    "specifies neither a service nor an instance.\n"),
   2704 		    pgrp->sc_pgroup_fmri, pgrp->sc_pgroup_name);
   2705 		return (stash_scferror_err(lcbdata, scfe));
   2706 
   2707 	case SCF_ERROR_NOT_FOUND:
   2708 		scfe = create_entity(lcbdata->sc_handle, pgrp->sc_pgroup_fmri,
   2709 		    &dependent_cbdata.sc_parent, &isservice);
   2710 		switch (scfe) {
   2711 		case SCF_ERROR_NONE:
   2712 			break;
   2713 
   2714 		case SCF_ERROR_NO_MEMORY:
   2715 		case SCF_ERROR_BACKEND_READONLY:
   2716 		case SCF_ERROR_BACKEND_ACCESS:
   2717 			return (stash_scferror_err(lcbdata, scfe));
   2718 
   2719 		case SCF_ERROR_NOT_FOUND:
   2720 			semerr(gettext("The scope in FMRI \"%s\" for the "
   2721 			    "\"%s\" dependent does not exist.\n"),
   2722 			    pgrp->sc_pgroup_fmri, pgrp->sc_pgroup_name);
   2723 			lcbdata->sc_err = EINVAL;
   2724 			return (UU_WALK_ERROR);
   2725 
   2726 		case SCF_ERROR_PERMISSION_DENIED:
   2727 			warn(gettext(
   2728 			    "Could not create %s (permission denied).\n"),
   2729 			    pgrp->sc_pgroup_fmri);
   2730 			return (stash_scferror_err(lcbdata, scfe));
   2731 
   2732 		case SCF_ERROR_INVALID_ARGUMENT:
   2733 		case SCF_ERROR_CONSTRAINT_VIOLATED:
   2734 		default:
   2735 			bad_error("create_entity", scfe);
   2736 		}
   2737 		break;
   2738 
   2739 	default:
   2740 		bad_error("fmri_to_entity", scfe);
   2741 	}
   2742 
   2743 	if (lcbdata->sc_trans != NULL) {
   2744 		e = scf_entry_create(lcbdata->sc_handle);
   2745 		if (e == NULL) {
   2746 			if (scf_error() != SCF_ERROR_NO_MEMORY)
   2747 				bad_error("scf_entry_create", scf_error());
   2748 
   2749 			entity_destroy(dependent_cbdata.sc_parent, isservice);
   2750 			return (stash_scferror(lcbdata));
   2751 		}
   2752 
   2753 		if (scf_transaction_property_new(lcbdata->sc_trans, e,
   2754 		    pgrp->sc_pgroup_name, SCF_TYPE_FMRI) != 0) {
   2755 			switch (scf_error()) {
   2756 			case SCF_ERROR_INVALID_ARGUMENT:
   2757 				warn(gettext("Dependent of %s has invalid name "
   2758 				    "\"%s\".\n"), pgrp->sc_parent->sc_fmri,
   2759 				    pgrp->sc_pgroup_name);
   2760 				/* FALLTHROUGH */
   2761 
   2762 			case SCF_ERROR_DELETED:
   2763 			case SCF_ERROR_CONNECTION_BROKEN:
   2764 				scf_entry_destroy(e);
   2765 				entity_destroy(dependent_cbdata.sc_parent,
   2766 				    isservice);
   2767 				return (stash_scferror(lcbdata));
   2768 
   2769 			case SCF_ERROR_EXISTS:
   2770 				scf_entry_destroy(e);
   2771 				entity_destroy(dependent_cbdata.sc_parent,
   2772 				    isservice);
   2773 				lcbdata->sc_err = EALREADY;
   2774 				return (UU_WALK_ERROR);
   2775 
   2776 			case SCF_ERROR_NOT_BOUND:
   2777 			case SCF_ERROR_HANDLE_MISMATCH:
   2778 			case SCF_ERROR_NOT_SET:
   2779 			default:
   2780 				bad_error("scf_transaction_property_new",
   2781 				    scf_error());
   2782 			}
   2783 		}
   2784 
   2785 		val = scf_value_create(lcbdata->sc_handle);
   2786 		if (val == NULL) {
   2787 			if (scf_error() != SCF_ERROR_NO_MEMORY)
   2788 				bad_error("scf_value_create", scf_error());
   2789 
   2790 			entity_destroy(dependent_cbdata.sc_parent, isservice);
   2791 			return (stash_scferror(lcbdata));
   2792 		}
   2793 
   2794 		if (scf_value_set_from_string(val, SCF_TYPE_FMRI,
   2795 		    pgrp->sc_pgroup_fmri) != 0)
   2796 			/* invalid should have been caught above */
   2797 			bad_error("scf_value_set_from_string", scf_error());
   2798 
   2799 		if (scf_entry_add_value(e, val) != 0)
   2800 			bad_error("scf_entry_add_value", scf_error());
   2801 	}
   2802 
   2803 	/* Add the property group to the target entity. */
   2804 
   2805 	dependent_cbdata.sc_handle = lcbdata->sc_handle;
   2806 	dependent_cbdata.sc_flags = 0;
   2807 	dependent_cbdata.sc_source_fmri = lcbdata->sc_source_fmri;
   2808 	dependent_cbdata.sc_target_fmri = pgrp->sc_pgroup_fmri;
   2809 
   2810 	ret = entity_pgroup_import(pgrp, &dependent_cbdata);
   2811 
   2812 	entity_destroy(dependent_cbdata.sc_parent, isservice);
   2813 
   2814 	if (ret == UU_WALK_NEXT)
   2815 		return (ret);
   2816 
   2817 	if (ret != UU_WALK_ERROR)
   2818 		bad_error("entity_pgroup_import", ret);
   2819 
   2820 	switch (dependent_cbdata.sc_err) {
   2821 	case ECANCELED:
   2822 		warn(gettext("%s deleted unexpectedly.\n"),
   2823 		    pgrp->sc_pgroup_fmri);
   2824 		lcbdata->sc_err = EBUSY;
   2825 		break;
   2826 
   2827 	case EEXIST:
   2828 		warn(gettext("Could not create \"%s\" dependency in %s "
   2829 		    "(already exists).\n"), pgrp->sc_pgroup_name,
   2830 		    pgrp->sc_pgroup_fmri);
   2831 		/* FALLTHROUGH */
   2832 
   2833 	default:
   2834 		lcbdata->sc_err = dependent_cbdata.sc_err;
   2835 	}
   2836 
   2837 	return (UU_WALK_ERROR);
   2838 }
   2839 
   2840 static int upgrade_dependent(const scf_property_t *, const entity_t *,
   2841     const scf_snaplevel_t *, scf_transaction_t *);
   2842 static int handle_dependent_conflict(const entity_t *, const scf_property_t *,
   2843     const pgroup_t *);
   2844 
   2845 /*
   2846  * Upgrade uncustomized dependents of ent to those specified in ient.  Read
   2847  * the current dependent targets from running (the snaplevel of a running
   2848  * snapshot which corresponds to ient) if not NULL (ent, an scf_service_t * or
   2849  * scf_instance_t * according to ient, otherwise).  Draw the ancestral
   2850  * dependent targets and dependency properties from li_dpts_pg (the
   2851  * "dependents" property group in snpl) and snpl (the snaplevel which
   2852  * corresponds to ent in a last-import snapshot).  If li_dpts_pg is NULL, then
   2853  * snpl doesn't have a "dependents" property group, and any dependents in ient
   2854  * are new.
   2855  *
   2856  * Returns
   2857  *   0 - success
   2858  *   ECONNABORTED - repository connection broken
   2859  *   ENOMEM - out of memory
   2860  *   ENOSPC - configd is out of resources
   2861  *   ECANCELED - ent was deleted
   2862  *   ENODEV - the entity containing li_dpts_pg was deleted
   2863  *   EPERM - could not modify dependents pg (permission denied) (error printed)
   2864  *	   - couldn't upgrade dependent (permission denied) (error printed)
   2865  *	   - couldn't create dependent (permission denied) (error printed)
   2866  *   EROFS - could not modify dependents pg (repository read-only)
   2867  *	   - couldn't upgrade dependent (repository read-only)
   2868  *	   - couldn't create dependent (repository read-only)
   2869  *   EACCES - could not modify dependents pg (backend access denied)
   2870  *	    - could not upgrade dependent (backend access denied)
   2871  *	    - could not create dependent (backend access denied)
   2872  *   EBUSY - "dependents" pg of ent added, changed, or deleted (error printed)
   2873  *	   - dependent target deleted (error printed)
   2874  *	   - dependent pg changed (error printed)
   2875  *   EINVAL - new dependent is invalid (error printed)
   2876  *   EBADF - snpl is corrupt (error printed)
   2877  *	   - snpl has corrupt pg (error printed)
   2878  *	   - dependency pg in target is corrupt (error printed)
   2879  *	   - target has corrupt snapshot (error printed)
   2880  *   EEXIST - dependency pg already existed in target service (error printed)
   2881  */
   2882 static int
   2883 upgrade_dependents(const scf_propertygroup_t *li_dpts_pg,
   2884     const scf_snaplevel_t *snpl, const entity_t *ient,
   2885     const scf_snaplevel_t *running, void *ent)
   2886 {
   2887 	pgroup_t *new_dpt_pgroup;
   2888 	scf_callback_t cbdata;
   2889 	int r, unseen, tx_started = 0;
   2890 	int have_cur_depts;
   2891 
   2892 	const char * const dependents = "dependents";
   2893 
   2894 	const int issvc = (ient->sc_etype == SVCCFG_SERVICE_OBJECT);
   2895 
   2896 	if (li_dpts_pg == NULL && uu_list_numnodes(ient->sc_dependents) == 0)
   2897 		/* Nothing to do. */
   2898 		return (0);
   2899 
   2900 	/* Fetch the current version of the "dependents" property group. */
   2901 	have_cur_depts = 1;
   2902 	if (entity_get_pg(ent, issvc, dependents, ud_cur_depts_pg) != 0) {
   2903 		switch (scf_error()) {
   2904 		case SCF_ERROR_NOT_FOUND:
   2905 			break;
   2906 
   2907 		case SCF_ERROR_DELETED:
   2908 		case SCF_ERROR_CONNECTION_BROKEN:
   2909 			return (scferror2errno(scf_error()));
   2910 
   2911 		case SCF_ERROR_NOT_SET:
   2912 		case SCF_ERROR_INVALID_ARGUMENT:
   2913 		case SCF_ERROR_HANDLE_MISMATCH:
   2914 		case SCF_ERROR_NOT_BOUND:
   2915 		default:
   2916 			bad_error("entity_get_pg", scf_error());
   2917 		}
   2918 
   2919 		have_cur_depts = 0;
   2920 	}
   2921 
   2922 	/* Fetch the running version of the "dependents" property group. */
   2923 	ud_run_dpts_pg_set = 0;
   2924 	if (running != NULL)
   2925 		r = scf_snaplevel_get_pg(running, dependents, ud_run_dpts_pg);
   2926 	else
   2927 		r = entity_get_pg(ent, issvc, dependents, ud_run_dpts_pg);
   2928 	if (r == 0) {
   2929 		ud_run_dpts_pg_set = 1;
   2930 	} else {
   2931 		switch (scf_error()) {
   2932 		case SCF_ERROR_NOT_FOUND:
   2933 			break;
   2934 
   2935 		case SCF_ERROR_DELETED:
   2936 		case SCF_ERROR_CONNECTION_BROKEN:
   2937 			return (scferror2errno(scf_error()));
   2938 
   2939 		case SCF_ERROR_NOT_SET:
   2940 		case SCF_ERROR_INVALID_ARGUMENT:
   2941 		case SCF_ERROR_HANDLE_MISMATCH:
   2942 		case SCF_ERROR_NOT_BOUND:
   2943 		default:
   2944 			bad_error(running ? "scf_snaplevel_get_pg" :
   2945 			    "entity_get_pg", scf_error());
   2946 		}
   2947 	}
   2948 
   2949 	/*
   2950 	 * Clear the seen fields of the dependents, so we can tell which ones
   2951 	 * are new.
   2952 	 */
   2953 	if (uu_list_walk(ient->sc_dependents, clear_int,
   2954 	    (void *)offsetof(pgroup_t, sc_pgroup_seen), UU_DEFAULT) != 0)
   2955 		bad_error("uu_list_walk", uu_error());
   2956 
   2957 	if (li_dpts_pg != NULL) {
   2958 		/*
   2959 		 * Each property in li_dpts_pg represents a dependent tag in
   2960 		 * the old manifest.  For each, call upgrade_dependent(),
   2961 		 * which will change ud_cur_depts_pg or dependencies in other
   2962 		 * services as appropriate.  Note (a) that changes to
   2963 		 * ud_cur_depts_pg are accumulated in ud_tx so they can all be
   2964 		 * made en masse, and (b) it's ok if the entity doesn't have
   2965 		 * a current version of the "dependents" property group,
   2966 		 * because we'll just consider all dependents as customized
   2967 		 * (by being deleted).
   2968 		 */
   2969 
   2970 		if (scf_iter_pg_properties(ud_iter, li_dpts_pg) != 0) {
   2971 			switch (scf_error()) {
   2972 			case SCF_ERROR_DELETED:
   2973 				return (ENODEV);
   2974 
   2975 			case SCF_ERROR_CONNECTION_BROKEN:
   2976 				return (ECONNABORTED);
   2977 
   2978 			case SCF_ERROR_HANDLE_MISMATCH:
   2979 			case SCF_ERROR_NOT_BOUND:
   2980 			case SCF_ERROR_NOT_SET:
   2981 			default:
   2982 				bad_error("scf_iter_pg_properties",
   2983 				    scf_error());
   2984 			}
   2985 		}
   2986 
   2987 		if (have_cur_depts &&
   2988 		    scf_transaction_start(ud_tx, ud_cur_depts_pg) != 0) {
   2989 			switch (scf_error()) {
   2990 			case SCF_ERROR_BACKEND_ACCESS:
   2991 			case SCF_ERROR_BACKEND_READONLY:
   2992 			case SCF_ERROR_CONNECTION_BROKEN:
   2993 				return (scferror2errno(scf_error()));
   2994 
   2995 			case SCF_ERROR_DELETED:
   2996 				warn(emsg_pg_deleted, ient->sc_fmri,
   2997 				    dependents);
   2998 				return (EBUSY);
   2999 
   3000 			case SCF_ERROR_PERMISSION_DENIED:
   3001 				warn(emsg_pg_mod_perm, dependents,
   3002 				    ient->sc_fmri);
   3003 				return (scferror2errno(scf_error()));
   3004 
   3005 			case SCF_ERROR_HANDLE_MISMATCH:
   3006 			case SCF_ERROR_IN_USE:
   3007 			case SCF_ERROR_NOT_BOUND:
   3008 			case SCF_ERROR_NOT_SET:
   3009 			default:
   3010 				bad_error("scf_transaction_start", scf_error());
   3011 			}
   3012 		}
   3013 		tx_started = have_cur_depts;
   3014 
   3015 		for (;;) {
   3016 			r = scf_iter_next_property(ud_iter, ud_dpt_prop);
   3017 			if (r == 0)
   3018 				break;
   3019 			if (r == 1) {
   3020 				r = upgrade_dependent(ud_dpt_prop, ient, snpl,
   3021 				    tx_started ? ud_tx : NULL);
   3022 				switch (r) {
   3023 				case 0:
   3024 					continue;
   3025 
   3026 				case ECONNABORTED:
   3027 				case ENOMEM:
   3028 				case ENOSPC:
   3029 				case EBADF:
   3030 				case EBUSY:
   3031 				case EINVAL:
   3032 				case EPERM:
   3033 				case EROFS:
   3034 				case EACCES:
   3035 				case EEXIST:
   3036 					break;
   3037 
   3038 				case ECANCELED:
   3039 					r = ENODEV;
   3040 					break;
   3041 
   3042 				default:
   3043 					bad_error("upgrade_dependent", r);
   3044 				}
   3045 
   3046 				if (tx_started)
   3047 					scf_transaction_destroy_children(ud_tx);
   3048 				return (r);
   3049 			}
   3050 			if (r != -1)
   3051 				bad_error("scf_iter_next_property", r);
   3052 
   3053 			switch (scf_error()) {
   3054 			case SCF_ERROR_DELETED:
   3055 				r = ENODEV;
   3056 				break;
   3057 
   3058 			case SCF_ERROR_CONNECTION_BROKEN:
   3059 				r = ECONNABORTED;
   3060 				break;
   3061 
   3062 			case SCF_ERROR_NOT_SET:
   3063 			case SCF_ERROR_INVALID_ARGUMENT:
   3064 			case SCF_ERROR_NOT_BOUND:
   3065 			case SCF_ERROR_HANDLE_MISMATCH:
   3066 			default:
   3067 				bad_error("scf_iter_next_property",
   3068 				    scf_error());
   3069 			}
   3070 
   3071 			if (tx_started)
   3072 				scf_transaction_destroy_children(ud_tx);
   3073 			return (r);
   3074 		}
   3075 	}
   3076 
   3077 	/* import unseen dependents */
   3078 	unseen = 0;
   3079 	for (new_dpt_pgroup = uu_list_first(ient->sc_dependents);
   3080 	    new_dpt_pgroup != NULL;
   3081 	    new_dpt_pgroup = uu_list_next(ient->sc_dependents,
   3082 	    new_dpt_pgroup)) {
   3083 		if (!new_dpt_pgroup->sc_pgroup_seen) {
   3084 			unseen = 1;
   3085 			break;
   3086 		}
   3087 	}
   3088 
   3089 	/* If there are none, exit early. */
   3090 	if (unseen == 0)
   3091 		goto commit;
   3092 
   3093 	/* Set up for lscf_dependent_import() */
   3094 	cbdata.sc_handle = g_hndl;
   3095 	cbdata.sc_parent = ent;
   3096 	cbdata.sc_service = issvc;
   3097 	cbdata.sc_flags = 0;
   3098 
   3099 	if (!have_cur_depts) {
   3100 		/*
   3101 		 * We have new dependents to import, so we need a "dependents"
   3102 		 * property group.
   3103 		 */
   3104 		if (issvc)
   3105 			r = scf_service_add_pg(ent, dependents,
   3106 			    SCF_GROUP_FRAMEWORK, 0, ud_cur_depts_pg);
   3107 		else
   3108 			r = scf_instance_add_pg(ent, dependents,
   3109 			    SCF_GROUP_FRAMEWORK, 0, ud_cur_depts_pg);
   3110 		if (r != 0) {
   3111 			switch (scf_error()) {
   3112 			case SCF_ERROR_DELETED:
   3113 			case SCF_ERROR_CONNECTION_BROKEN:
   3114 			case SCF_ERROR_BACKEND_READONLY:
   3115 			case SCF_ERROR_BACKEND_ACCESS:
   3116 			case SCF_ERROR_NO_RESOURCES:
   3117 				return (scferror2errno(scf_error()));
   3118 
   3119 			case SCF_ERROR_EXISTS:
   3120 				warn(emsg_pg_added, ient->sc_fmri, dependents);
   3121 				return (EBUSY);
   3122 
   3123 			case SCF_ERROR_PERMISSION_DENIED:
   3124 				warn(emsg_pg_add_perm, dependents,
   3125 				    ient->sc_fmri);
   3126 				return (scferror2errno(scf_error()));
   3127 
   3128 			case SCF_ERROR_NOT_BOUND:
   3129 			case SCF_ERROR_HANDLE_MISMATCH:
   3130 			case SCF_ERROR_INVALID_ARGUMENT:
   3131 			case SCF_ERROR_NOT_SET:
   3132 			default:
   3133 				bad_error("scf_service_add_pg", scf_error());
   3134 			}
   3135 		}
   3136 	}
   3137 
   3138 	cbdata.sc_trans = ud_tx;
   3139 
   3140 	if (!tx_started && scf_transaction_start(ud_tx, ud_cur_depts_pg) != 0) {
   3141 		switch (scf_error()) {
   3142 		case SCF_ERROR_CONNECTION_BROKEN:
   3143 		case SCF_ERROR_BACKEND_ACCESS:
   3144 		case SCF_ERROR_BACKEND_READONLY:
   3145 			return (scferror2errno(scf_error()));
   3146 
   3147 		case SCF_ERROR_DELETED:
   3148 			warn(emsg_pg_deleted, ient->sc_fmri, dependents);
   3149 			return (EBUSY);
   3150 
   3151 		case SCF_ERROR_PERMISSION_DENIED:
   3152 			warn(emsg_pg_mod_perm, dependents, ient->sc_fmri);
   3153 			return (scferror2errno(scf_error()));
   3154 
   3155 		case SCF_ERROR_HANDLE_MISMATCH:
   3156 		case SCF_ERROR_IN_USE:
   3157 		case SCF_ERROR_NOT_BOUND:
   3158 		case SCF_ERROR_NOT_SET:
   3159 		default:
   3160 			bad_error("scf_transaction_start", scf_error());
   3161 		}
   3162 	}
   3163 	tx_started = 1;
   3164 
   3165 	for (new_dpt_pgroup = uu_list_first(ient->sc_dependents);
   3166 	    new_dpt_pgroup != NULL;
   3167 	    new_dpt_pgroup = uu_list_next(ient->sc_dependents,
   3168 	    new_dpt_pgroup)) {
   3169 		if (new_dpt_pgroup->sc_pgroup_seen)
   3170 			continue;
   3171 
   3172 		if (ud_run_dpts_pg_set) {
   3173 			/*
   3174 			 * If the dependent is already there, then we have
   3175 			 * a conflict.
   3176 			 */
   3177 			if (scf_pg_get_property(ud_run_dpts_pg,
   3178 			    new_dpt_pgroup->sc_pgroup_name, ud_prop) == 0) {
   3179 				r = handle_dependent_conflict(ient, ud_prop,
   3180 				    new_dpt_pgroup);
   3181 				switch (r) {
   3182 				case 0:
   3183 					continue;
   3184 
   3185 				case ECONNABORTED:
   3186 				case ENOMEM:
   3187 				case EBUSY:
   3188 				case EBADF:
   3189 				case EINVAL:
   3190 					scf_transaction_destroy_children(ud_tx);
   3191 					return (r);
   3192 
   3193 				default:
   3194 					bad_error("handle_dependent_conflict",
   3195 					    r);
   3196 				}
   3197 			} else {
   3198 				switch (scf_error()) {
   3199 				case SCF_ERROR_NOT_FOUND:
   3200 					break;
   3201 
   3202 				case SCF_ERROR_INVALID_ARGUMENT:
   3203 					warn(emsg_fmri_invalid_pg_name,
   3204 					    ient->sc_fmri,
   3205 					    new_dpt_pgroup->sc_pgroup_name);
   3206 					scf_transaction_destroy_children(ud_tx);
   3207 					return (EINVAL);
   3208 
   3209 				case SCF_ERROR_DELETED:
   3210 					warn(emsg_pg_deleted, ient->sc_fmri,
   3211 					    new_dpt_pgroup->sc_pgroup_name);
   3212 					scf_transaction_destroy_children(ud_tx);
   3213 					return (EBUSY);
   3214 
   3215 				case SCF_ERROR_CONNECTION_BROKEN:
   3216 					scf_transaction_destroy_children(ud_tx);
   3217 					return (ECONNABORTED);
   3218 
   3219 				case SCF_ERROR_NOT_BOUND:
   3220 				case SCF_ERROR_HANDLE_MISMATCH:
   3221 				case SCF_ERROR_NOT_SET:
   3222 				default:
   3223 					bad_error("scf_pg_get_property",
   3224 					    scf_error());
   3225 				}
   3226 			}
   3227 		}
   3228 
   3229 		r = lscf_dependent_import(new_dpt_pgroup, &cbdata);
   3230 		if (r != UU_WALK_NEXT) {
   3231 			if (r != UU_WALK_ERROR)
   3232 				bad_error("lscf_dependent_import", r);
   3233 
   3234 			if (cbdata.sc_err == EALREADY) {
   3235 				/* Collisions were handled preemptively. */
   3236 				bad_error("lscf_dependent_import",
   3237 				    cbdata.sc_err);
   3238 			}
   3239 
   3240 			scf_transaction_destroy_children(ud_tx);
   3241 			return (cbdata.sc_err);
   3242 		}
   3243 	}
   3244 
   3245 commit:
   3246 	if (!tx_started)
   3247 		return (0);
   3248 
   3249 	r = scf_transaction_commit(ud_tx);
   3250 
   3251 	scf_transaction_destroy_children(ud_tx);
   3252 
   3253 	switch (r) {
   3254 	case 1:
   3255 		return (0);
   3256 
   3257 	case 0:
   3258 		warn(emsg_pg_changed, ient->sc_fmri, dependents);
   3259 		return (EBUSY);
   3260 
   3261 	case -1:
   3262 		break;
   3263 
   3264 	default:
   3265 		bad_error("scf_transaction_commit", r);
   3266 	}
   3267 
   3268 	switch (scf_error()) {
   3269 	case SCF_ERROR_CONNECTION_BROKEN:
   3270 	case SCF_ERROR_BACKEND_READONLY:
   3271 	case SCF_ERROR_BACKEND_ACCESS:
   3272 	case SCF_ERROR_NO_RESOURCES:
   3273 		return (scferror2errno(scf_error()));
   3274 
   3275 	case SCF_ERROR_DELETED:
   3276 		warn(emsg_pg_deleted, ient->sc_fmri, dependents);
   3277 		return (EBUSY);
   3278 
   3279 	case SCF_ERROR_PERMISSION_DENIED:
   3280 		warn(emsg_pg_mod_perm, dependents, ient->sc_fmri);
   3281 		return (scferror2errno(scf_error()));
   3282 
   3283 	case SCF_ERROR_NOT_BOUND:
   3284 	case SCF_ERROR_INVALID_ARGUMENT:
   3285 	case SCF_ERROR_NOT_SET:
   3286 	default:
   3287 		bad_error("scf_transaction_destroy", scf_error());
   3288 		/* NOTREACHED */
   3289 	}
   3290 }
   3291 
   3292 /*
   3293  * Used to add the manifests to the list of currently supported manifests.
   3294  * We could modify the existing manifest list removing entries if the files
   3295  * don't exist.
   3296  *
   3297  * Get the old list and the new file name
   3298  * If the new file name is in the list return
   3299  * If not then add the file to the list.
   3300  * As we process the list check to see if the files in the old list exist
   3301  * 	if not then remove the file from the list.
   3302  * Commit the list of manifest file names.
   3303  *
   3304  */
   3305 static int
   3306 upgrade_manifestfiles(pgroup_t *pg, const entity_t *ient,
   3307     const scf_snaplevel_t *running, void *ent)
   3308 {
   3309 	scf_propertygroup_t *ud_mfsts_pg = NULL;
   3310 	scf_property_t *ud_prop = NULL;
   3311 	scf_iter_t *ud_prop_iter;
   3312 	scf_value_t *fname_value;
   3313 	scf_callback_t cbdata;
   3314 	pgroup_t *mfst_pgroup;
   3315 	property_t *mfst_prop;
   3316 	property_t *old_prop;
   3317 	char *pname = malloc(MAXPATHLEN);
   3318 	char *fval;
   3319 	char *old_pname;
   3320 	char *old_fval;
   3321 	int no_upgrade_pg;
   3322 	int mfst_seen;
   3323 	int r;
   3324 
   3325 	const int issvc = (ient->sc_etype == SVCCFG_SERVICE_OBJECT);
   3326 
   3327 	/*
   3328 	 * This should always be the service base on the code
   3329 	 * path, and the fact that the manifests pg is a service
   3330 	 * level property group only.
   3331 	 */
   3332 	ud_mfsts_pg = scf_pg_create(g_hndl);
   3333 	ud_prop = scf_property_create(g_hndl);
   3334 	ud_prop_iter = scf_iter_create(g_hndl);
   3335 	fname_value = scf_value_create(g_hndl);
   3336 
   3337 	/* Fetch the "manifests" property group */
   3338 	no_upgrade_pg = 0;
   3339 	r = entity_get_pg(ent, issvc, SCF_PG_MANIFESTFILES,
   3340 	    ud_mfsts_pg);
   3341 	if (r != 0) {
   3342 		switch (scf_error()) {
   3343 		case SCF_ERROR_NOT_FOUND:
   3344 			no_upgrade_pg = 1;
   3345 			break;
   3346 
   3347 		case SCF_ERROR_DELETED:
   3348 		case SCF_ERROR_CONNECTION_BROKEN:
   3349 			return (scferror2errno(scf_error()));
   3350 
   3351 		case SCF_ERROR_NOT_SET:
   3352 		case SCF_ERROR_INVALID_ARGUMENT:
   3353 		case SCF_ERROR_HANDLE_MISMATCH:
   3354 		case SCF_ERROR_NOT_BOUND:
   3355 		default:
   3356 			bad_error(running ? "scf_snaplevel_get_pg" :
   3357 			    "entity_get_pg", scf_error());
   3358 		}
   3359 	}
   3360 
   3361 	if (no_upgrade_pg) {
   3362 		cbdata.sc_handle = g_hndl;
   3363 		cbdata.sc_parent = ent;
   3364 		cbdata.sc_service = issvc;
   3365 		cbdata.sc_flags = SCI_FORCE;
   3366 		cbdata.sc_source_fmri = ient->sc_fmri;
   3367 		cbdata.sc_target_fmri = ient->sc_fmri;
   3368 
   3369 		if (entity_pgroup_import(pg, &cbdata) != UU_WALK_NEXT)
   3370 			return (cbdata.sc_err);
   3371 
   3372 		return (0);
   3373 	}
   3374 
   3375 	/* Fetch the new manifests property group */
   3376 	for (mfst_pgroup = uu_list_first(ient->sc_pgroups);
   3377 	    mfst_pgroup != NULL;
   3378 	    mfst_pgroup = uu_list_next(ient->sc_pgroups, mfst_pgroup)) {
   3379 		if (strcmp(mfst_pgroup->sc_pgroup_name,
   3380 		    SCF_PG_MANIFESTFILES) == 0)
   3381 			break;
   3382 	}
   3383 
   3384 	if ((r = scf_iter_pg_properties(ud_prop_iter, ud_mfsts_pg)) !=
   3385 	    SCF_SUCCESS)
   3386 		return (-1);
   3387 
   3388 	while ((r = scf_iter_next_property(ud_prop_iter, ud_prop)) == 1) {
   3389 		mfst_seen = 0;
   3390 		if (scf_property_get_name(ud_prop, pname, MAXPATHLEN) < 0)
   3391 			continue;
   3392 
   3393 		for (mfst_prop = uu_list_first(mfst_pgroup->sc_pgroup_props);
   3394 		    mfst_prop != NULL;
   3395 		    mfst_prop = uu_list_next(mfst_pgroup->sc_pgroup_props,
   3396 		    mfst_prop)) {
   3397 			if (strcmp(mfst_prop->sc_property_name, pname) == 0) {
   3398 				mfst_seen = 1;
   3399 			}
   3400 		}
   3401 
   3402 		/*
   3403 		 * If the manifest is not seen then add it to the new mfst
   3404 		 * property list to get proccessed into the repo.
   3405 		 */
   3406 		if (mfst_seen == 0) {
   3407 			fval = malloc(MAXPATHLEN);
   3408 
   3409 			/*
   3410 			 * If we cannot get the value then there is no
   3411 			 * reason to attempt to attach the value to
   3412 			 * the property group
   3413 			 */
   3414 			if (fval != NULL &&
   3415 			    prop_get_val(ud_prop, fname_value) == 0 &&
   3416 			    scf_value_get_astring(fname_value, fval,
   3417 			    MAXPATHLEN) != -1)  {
   3418 				/*
   3419 				 * First check to see if the manifest is there
   3420 				 * if not then there is no need to add it.
   3421 				 */
   3422 				if (access(fval, F_OK) == -1) {
   3423 					free(fval);
   3424 					continue;
   3425 				}
   3426 
   3427 				old_pname = safe_strdup(pname);
   3428 				old_fval = safe_strdup(fval);
   3429 				old_prop = internal_property_create(old_pname,
   3430 				    SCF_TYPE_ASTRING, 1, old_fval);
   3431 
   3432 				/*
   3433 				 * Already checked to see if the property exists
   3434 				 * in the group, and it does not.
   3435 				 */
   3436 				(void) internal_attach_property(mfst_pgroup,
   3437 				    old_prop);
   3438 			}
   3439 		}
   3440 	}
   3441 
   3442 	cbdata.sc_handle = g_hndl;
   3443 	cbdata.sc_parent = ent;
   3444 	cbdata.sc_service = issvc;
   3445 	cbdata.sc_flags = SCI_FORCE;
   3446 	cbdata.sc_source_fmri = ient->sc_fmri;
   3447 	cbdata.sc_target_fmri = ient->sc_fmri;
   3448 
   3449 	if (entity_pgroup_import(mfst_pgroup, &cbdata) != UU_WALK_NEXT)
   3450 		return (cbdata.sc_err);
   3451 
   3452 	return (r);
   3453 }
   3454 
   3455 /*
   3456  * prop is taken to be a property in the "dependents" property group of snpl,
   3457  * which is taken to be the snaplevel of a last-import snapshot corresponding
   3458  * to ient.  If prop is a valid dependents property, upgrade the dependent it
   3459  * represents according to the repository & ient.  If ud_run_dpts_pg_set is
   3460  * true, then ud_run_dpts_pg is taken to be the "dependents" property group
   3461  * of the entity ient represents (possibly in the running snapshot).  If it
   3462  * needs to be changed, an entry will be added to tx, if not NULL.
   3463  *
   3464  * Returns
   3465  *   0 - success
   3466  *   ECONNABORTED - repository connection broken
   3467  *   ENOMEM - out of memory
   3468  *   ENOSPC - configd was out of resources
   3469  *   ECANCELED - snpl's entity was deleted
   3470  *   EINVAL - dependent target is invalid (error printed)
   3471  *	    - dependent is invalid (error printed)
   3472  *   EBADF - snpl is corrupt (error printed)
   3473  *	   - snpl has corrupt pg (error printed)
   3474  *	   - dependency pg in target is corrupt (error printed)
   3475  *	   - running snapshot in dependent is missing snaplevel (error printed)
   3476  *   EPERM - couldn't delete dependency pg (permission denied) (error printed)
   3477  *	   - couldn't create dependent (permission denied) (error printed)
   3478  *	   - couldn't modify dependent pg (permission denied) (error printed)
   3479  *   EROFS - couldn't delete dependency pg (repository read-only)
   3480  *	   - couldn't create dependent (repository read-only)
   3481  *   EACCES - couldn't delete dependency pg (backend access denied)
   3482  *	    - couldn't create dependent (backend access denied)
   3483  *   EBUSY - ud_run_dpts_pg was deleted (error printed)
   3484  *	   - tx's pg was deleted (error printed)
   3485  *	   - dependent pg was changed or deleted (error printed)
   3486  *   EEXIST - dependency pg already exists in new target (error printed)
   3487  */
   3488 static int
   3489 upgrade_dependent(const scf_property_t *prop, const entity_t *ient,
   3490     const scf_snaplevel_t *snpl, scf_transaction_t *tx)
   3491 {
   3492 	pgroup_t pgrp;
   3493 	scf_type_t ty;
   3494 	pgroup_t *new_dpt_pgroup;
   3495 	pgroup_t *old_dpt_pgroup = NULL;
   3496 	pgroup_t *current_pg;
   3497 	scf_callback_t cbdata;
   3498 	int tissvc;
   3499 	void *target_ent;
   3500 	scf_error_t serr;
   3501 	int r;
   3502 	scf_transaction_entry_t *ent;
   3503 
   3504 	const char * const cf_inval = gettext("Conflict upgrading %s "
   3505 	    "(dependent \"%s\" has invalid dependents property).\n");
   3506 	const char * const cf_missing = gettext("Conflict upgrading %s "
   3507 	    "(dependent \"%s\" is missing).\n");
   3508 	const char * const cf_newdpg = gettext("Conflict upgrading %s "
   3509 	    "(dependent \"%s\" has new dependency property group).\n");
   3510 	const char * const cf_newtarg = gettext("Conflict upgrading %s "
   3511 	    "(dependent \"%s\" has new target).\n");
   3512 	const char * const li_corrupt =
   3513 	    gettext("%s: \"last-import\" snapshot is corrupt.\n");
   3514 	const char * const upgrading =
   3515 	    gettext("%s: Upgrading dependent \"%s\".\n");
   3516 	const char * const r_no_lvl = gettext("%s: \"running\" snapshot is "
   3517 	    "corrupt (missing snaplevel).\n");
   3518 
   3519 	if (scf_property_type(prop, &ty) != 0) {
   3520 		switch (scf_error()) {
   3521 		case SCF_ERROR_DELETED:
   3522 		case SCF_ERROR_CONNECTION_BROKEN:
   3523 			return (scferror2errno(scf_error()));
   3524 
   3525 		case SCF_ERROR_NOT_BOUND:
   3526 		case SCF_ERROR_NOT_SET:
   3527 		default:
   3528 			bad_error("scf_property_type", scf_error());
   3529 		}
   3530 	}
   3531 
   3532 	if (!(ty == SCF_TYPE_FMRI || ty == SCF_TYPE_ASTRING)) {
   3533 		warn(li_corrupt, ient->sc_fmri);
   3534 		return (EBADF);
   3535 	}
   3536 
   3537 	/*
   3538 	 * prop represents a dependent in the old manifest.  It is named after
   3539 	 * the dependent.
   3540 	 */
   3541 	if (scf_property_get_name(prop, ud_name, max_scf_name_len + 1) < 0) {
   3542 		switch (scf_error()) {
   3543 		case SCF_ERROR_DELETED:
   3544 		case SCF_ERROR_CONNECTION_BROKEN:
   3545 			return (scferror2errno(scf_error()));
   3546 
   3547 		case SCF_ERROR_NOT_BOUND:
   3548 		case SCF_ERROR_NOT_SET:
   3549 		default:
   3550 			bad_error("scf_property_get_name", scf_error());
   3551 		}
   3552 	}
   3553 
   3554 	/* See if it's in the new manifest. */
   3555 	pgrp.sc_pgroup_name = ud_name;
   3556 	new_dpt_pgroup =
   3557 	    uu_list_find(ient->sc_dependents, &pgrp, NULL, UU_DEFAULT);
   3558 
   3559 	/* If it's not, delete it... if it hasn't been customized. */
   3560 	if (new_dpt_pgroup == NULL) {
   3561 		pgroup_t *dpt;
   3562 
   3563 		if (!ud_run_dpts_pg_set)
   3564 			return (0);
   3565 
   3566 		if (scf_property_get_value(prop, ud_val) != 0) {
   3567 			switch (scf_error()) {
   3568 			case SCF_ERROR_NOT_FOUND:
   3569 			case SCF_ERROR_CONSTRAINT_VIOLATED:
   3570 				warn(li_corrupt, ient->sc_fmri);
   3571 				return (EBADF);
   3572 
   3573 			case SCF_ERROR_DELETED:
   3574 			case SCF_ERROR_CONNECTION_BROKEN:
   3575 				return (scferror2errno(scf_error()));
   3576 
   3577 			case SCF_ERROR_HANDLE_MISMATCH:
   3578 			case SCF_ERROR_NOT_BOUND:
   3579 			case SCF_ERROR_NOT_SET:
   3580 			case SCF_ERROR_PERMISSION_DENIED:
   3581 			default:
   3582 				bad_error("scf_property_get_value",
   3583 				    scf_error());
   3584 			}
   3585 		}
   3586 
   3587 		if (scf_value_get_as_string(ud_val, ud_oldtarg,
   3588 		    max_scf_value_len + 1) < 0)
   3589 			bad_error("scf_value_get_as_string", scf_error());
   3590 
   3591 		if (scf_pg_get_property(ud_run_dpts_pg, ud_name, ud_prop) !=
   3592 		    0) {
   3593 			switch (scf_error()) {
   3594 			case SCF_ERROR_NOT_FOUND:
   3595 				return (0);
   3596 
   3597 			case SCF_ERROR_CONNECTION_BROKEN:
   3598 				return (scferror2errno(scf_error()));
   3599 
   3600 			case SCF_ERROR_DELETED:
   3601 				warn(emsg_pg_deleted, ient->sc_fmri,
   3602 				    "dependents");
   3603 				return (EBUSY);
   3604 
   3605 			case SCF_ERROR_INVALID_ARGUMENT:
   3606 			case SCF_ERROR_NOT_BOUND:
   3607 			case SCF_ERROR_HANDLE_MISMATCH:
   3608 			case SCF_ERROR_NOT_SET:
   3609 			default:
   3610 				bad_error("scf_pg_get_property", scf_error());
   3611 			}
   3612 		}
   3613 		if (scf_property_get_value(ud_prop, ud_val) != 0) {
   3614 			switch (scf_error()) {
   3615 			case SCF_ERROR_NOT_FOUND:
   3616 			case SCF_ERROR_CONSTRAINT_VIOLATED:
   3617 				warn(cf_inval, ient->sc_fmri, ud_name);
   3618 				return (0);
   3619 
   3620 			case SCF_ERROR_DELETED:
   3621 			case SCF_ERROR_CONNECTION_BROKEN:
   3622 				return (scferror2errno(scf_error()));
   3623 
   3624 			case SCF_ERROR_HANDLE_MISMATCH:
   3625 			case SCF_ERROR_NOT_BOUND:
   3626 			case SCF_ERROR_NOT_SET:
   3627 			case SCF_ERROR_PERMISSION_DENIED:
   3628 			default:
   3629 				bad_error("scf_property_get_value",
   3630 				    scf_error());
   3631 			}
   3632 		}
   3633 
   3634 		ty = scf_value_type(ud_val);
   3635 		assert(ty != SCF_TYPE_INVALID);
   3636 		if (!(ty == SCF_TYPE_FMRI || ty == SCF_TYPE_ASTRING)) {
   3637 			warn(cf_inval, ient->sc_fmri, ud_name);
   3638 			return (0);
   3639 		}
   3640 
   3641 		if (scf_value_get_as_string(ud_val, ud_ctarg,
   3642 		    max_scf_value_len + 1) < 0)
   3643 			bad_error("scf_value_get_as_string", scf_error());
   3644 
   3645 		r = fmri_equal(ud_ctarg, ud_oldtarg);
   3646 		switch (r) {
   3647 		case 1:
   3648 			break;
   3649 
   3650 		case 0:
   3651 		case -1:	/* warn? */
   3652 			warn(cf_newtarg, ient->sc_fmri, ud_name);
   3653 			return (0);
   3654 
   3655 		case -2:
   3656 			warn(li_corrupt, ient->sc_fmri);
   3657 			return (EBADF);
   3658 
   3659 		default:
   3660 			bad_error("fmri_equal", r);
   3661 		}
   3662 
   3663 		if (scf_snaplevel_get_pg(snpl, ud_name, ud_pg) != 0) {
   3664 			switch (scf_error()) {
   3665 			case SCF_ERROR_NOT_FOUND:
   3666 				warn(li_corrupt, ient->sc_fmri);
   3667 				return (EBADF);
   3668 
   3669 			case SCF_ERROR_DELETED:
   3670 			case SCF_ERROR_CONNECTION_BROKEN:
   3671 				return (scferror2errno(scf_error()));
   3672 
   3673 			case SCF_ERROR_NOT_BOUND:
   3674 			case SCF_ERROR_HANDLE_MISMATCH:
   3675 			case SCF_ERROR_INVALID_ARGUMENT:
   3676 			case SCF_ERROR_NOT_SET:
   3677 			default:
   3678 				bad_error("scf_snaplevel_get_pg", scf_error());
   3679 			}
   3680 		}
   3681 
   3682 		r = load_pg(ud_pg, &old_dpt_pgroup, ient->sc_fmri,
   3683 		    snap_lastimport);
   3684 		switch (r) {
   3685 		case 0:
   3686 			break;
   3687 
   3688 		case ECANCELED:
   3689 		case ECONNABORTED:
   3690 		case ENOMEM:
   3691 		case EBADF:
   3692 			return (r);
   3693 
   3694 		case EACCES:
   3695 		default:
   3696 			bad_error("load_pg", r);
   3697 		}
   3698 
   3699 		serr = fmri_to_entity(g_hndl, ud_ctarg, &target_ent, &tissvc);
   3700 		switch (serr) {
   3701 		case SCF_ERROR_NONE:
   3702 			break;
   3703 
   3704 		case SCF_ERROR_NO_MEMORY:
   3705 			internal_pgroup_free(old_dpt_pgroup);
   3706 			return (ENOMEM);
   3707 
   3708 		case SCF_ERROR_NOT_FOUND:
   3709 			internal_pgroup_free(old_dpt_pgroup);
   3710 			goto delprop;
   3711 
   3712 		case SCF_ERROR_CONSTRAINT_VIOLATED:	/* caught above */
   3713 		case SCF_ERROR_INVALID_ARGUMENT:	/* caught above */
   3714 		default:
   3715 			bad_error("fmri_to_entity", serr);
   3716 		}
   3717 
   3718 		r = entity_get_running_pg(target_ent, tissvc, ud_name,
   3719 		    ud_pg, ud_iter2, ud_inst, imp_snap, ud_snpl);
   3720 		switch (r) {
   3721 		case 0:
   3722 			break;
   3723 
   3724 		case ECONNABORTED:
   3725 			internal_pgroup_free(old_dpt_pgroup);
   3726 			return (r);
   3727 
   3728 		case ECANCELED:
   3729 		case ENOENT:
   3730 			internal_pgroup_free(old_dpt_pgroup);
   3731 			goto delprop;
   3732 
   3733 		case EBADF:
   3734 			warn(r_no_lvl, ud_ctarg);
   3735 			internal_pgroup_free(old_dpt_pgroup);
   3736 			return (r);
   3737 
   3738 		case EINVAL:
   3739 		default:
   3740 			bad_error("entity_get_running_pg", r);
   3741 		}
   3742 
   3743 		/* load it */
   3744 		r = load_pg(ud_pg, &current_pg, ud_ctarg, NULL);
   3745 		switch (r) {
   3746 		case 0:
   3747 			break;
   3748 
   3749 		case ECANCELED:
   3750 			internal_pgroup_free(old_dpt_pgroup);
   3751 			goto delprop;
   3752 
   3753 		case ECONNABORTED:
   3754 		case ENOMEM:
   3755 		case EBADF:
   3756 			internal_pgroup_free(old_dpt_pgroup);
   3757 			return (r);
   3758 
   3759 		case EACCES:
   3760 		default:
   3761 			bad_error("load_pg", r);
   3762 		}
   3763 
   3764 		/* compare property groups */
   3765 		if (!pg_equal(old_dpt_pgroup, current_pg)) {
   3766 			warn(cf_newdpg, ient->sc_fmri, ud_name);
   3767 			internal_pgroup_free(old_dpt_pgroup);
   3768 			internal_pgroup_free(current_pg);
   3769 			return (0);
   3770 		}
   3771 
   3772 		internal_pgroup_free(old_dpt_pgroup);
   3773 		internal_pgroup_free(current_pg);
   3774 
   3775 		if (g_verbose)
   3776 			warn(gettext("%s: Deleting dependent \"%s\".\n"),
   3777 			    ient->sc_fmri, ud_name);
   3778 
   3779 		if (entity_get_pg(target_ent, tissvc, ud_name, ud_pg) != 0) {
   3780 			switch (scf_error()) {
   3781 			case SCF_ERROR_NOT_FOUND:
   3782 			case SCF_ERROR_DELETED:
   3783 				internal_pgroup_free(old_dpt_pgroup);
   3784 				goto delprop;
   3785 
   3786 			case SCF_ERROR_CONNECTION_BROKEN:
   3787 				internal_pgroup_free(old_dpt_pgroup);
   3788 				return (ECONNABORTED);
   3789 
   3790 			case SCF_ERROR_NOT_SET:
   3791 			case SCF_ERROR_INVALID_ARGUMENT:
   3792 			case SCF_ERROR_HANDLE_MISMATCH:
   3793 			case SCF_ERROR_NOT_BOUND:
   3794 			default:
   3795 				bad_error("entity_get_pg", scf_error());
   3796 			}
   3797 		}
   3798 
   3799 		if (scf_pg_delete(ud_pg) != 0) {
   3800 			switch (scf_error()) {
   3801 			case SCF_ERROR_DELETED:
   3802 				break;
   3803 
   3804 			case SCF_ERROR_CONNECTION_BROKEN:
   3805 			case SCF_ERROR_BACKEND_READONLY:
   3806 			case SCF_ERROR_BACKEND_ACCESS:
   3807 				return (scferror2errno(scf_error()));
   3808 
   3809 			case SCF_ERROR_PERMISSION_DENIED:
   3810 				warn(emsg_pg_del_perm, ud_name, ient->sc_fmri);
   3811 				return (scferror2errno(scf_error()));
   3812 
   3813 			case SCF_ERROR_NOT_SET:
   3814 			default:
   3815 				bad_error("scf_pg_delete", scf_error());
   3816 			}
   3817 		}
   3818 
   3819 		/*
   3820 		 * This service was changed, so it must be refreshed.  But
   3821 		 * since it's not mentioned in the new manifest, we have to
   3822 		 * record its FMRI here for use later.  We record the name
   3823 		 * & the entity (via sc_parent) in case we need to print error
   3824 		 * messages during the refresh.
   3825 		 */
   3826 		dpt = internal_pgroup_new();
   3827 		if (dpt == NULL)
   3828 			return (ENOMEM);
   3829 		dpt->sc_pgroup_name = strdup(ud_name);
   3830 		dpt->sc_pgroup_fmri = strdup(ud_ctarg);
   3831 		if (dpt->sc_pgroup_name == NULL || dpt->sc_pgroup_fmri == NULL)
   3832 			return (ENOMEM);
   3833 		dpt->sc_parent = (entity_t *)ient;
   3834 		if (uu_list_insert_after(imp_deleted_dpts, NULL, dpt) != 0)
   3835 			uu_die(gettext("libuutil error: %s\n"),
   3836 			    uu_strerror(uu_error()));
   3837 
   3838 delprop:
   3839 		if (tx == NULL)
   3840 			return (0);
   3841 
   3842 		ent = scf_entry_create(g_hndl);
   3843 		if (ent == NULL)
   3844 			return (ENOMEM);
   3845 
   3846 		if (scf_transaction_property_delete(tx, ent, ud_name) != 0) {
   3847 			scf_entry_destroy(ent);
   3848 			switch (scf_error()) {
   3849 			case SCF_ERROR_DELETED:
   3850 				warn(emsg_pg_deleted, ient->sc_fmri,
   3851 				    "dependents");
   3852 				return (EBUSY);
   3853 
   3854 			case SCF_ERROR_CONNECTION_BROKEN:
   3855 				return (scferror2errno(scf_error()));
   3856 
   3857 			case SCF_ERROR_NOT_FOUND:
   3858 				break;
   3859 
   3860 			case SCF_ERROR_HANDLE_MISMATCH:
   3861 			case SCF_ERROR_NOT_BOUND:
   3862 			case SCF_ERROR_INVALID_ARGUMENT:
   3863 			case SCF_ERROR_NOT_SET:
   3864 			default:
   3865 				bad_error("scf_transaction_property_delete",
   3866 				    scf_error());
   3867 			}
   3868 		}
   3869 
   3870 		return (0);
   3871 	}
   3872 
   3873 	new_dpt_pgroup->sc_pgroup_seen = 1;
   3874 
   3875 	/*
   3876 	 * Decide whether the dependent has changed in the manifest.
   3877 	 */
   3878 	/* Compare the target. */
   3879 	if (scf_property_get_value(prop, ud_val) != 0) {
   3880 		switch (scf_error()) {
   3881 		case SCF_ERROR_NOT_FOUND:
   3882 		case SCF_ERROR_CONSTRAINT_VIOLATED:
   3883 			warn(li_corrupt, ient->sc_fmri);
   3884 			return (EBADF);
   3885 
   3886 		case SCF_ERROR_DELETED:
   3887 		case SCF_ERROR_CONNECTION_BROKEN:
   3888 			return (scferror2errno(scf_error()));
   3889 
   3890 		case SCF_ERROR_HANDLE_MISMATCH:
   3891 		case SCF_ERROR_NOT_BOUND:
   3892 		case SCF_ERROR_NOT_SET:
   3893 		case SCF_ERROR_PERMISSION_DENIED:
   3894 		default:
   3895 			bad_error("scf_property_get_value", scf_error());
   3896 		}
   3897 	}
   3898 
   3899 	if (scf_value_get_as_string(ud_val, ud_oldtarg, max_scf_value_len + 1) <
   3900 	    0)
   3901 		bad_error("scf_value_get_as_string", scf_error());
   3902 
   3903 	r = fmri_equal(ud_oldtarg, new_dpt_pgroup->sc_pgroup_fmri);
   3904 	switch (r) {
   3905 	case 0:
   3906 		break;
   3907 
   3908 	case 1:
   3909 		/* Compare the dependency pgs. */
   3910 		if (scf_snaplevel_get_pg(snpl, ud_name, ud_pg) != 0) {
   3911 			switch (scf_error()) {
   3912 			case SCF_ERROR_NOT_FOUND:
   3913 				warn(li_corrupt, ient->sc_fmri);
   3914 				return (EBADF);
   3915 
   3916 			case SCF_ERROR_DELETED:
   3917 			case SCF_ERROR_CONNECTION_BROKEN:
   3918 				return (scferror2errno(scf_error()));
   3919 
   3920 			case SCF_ERROR_NOT_BOUND:
   3921 			case SCF_ERROR_HANDLE_MISMATCH:
   3922 			case SCF_ERROR_INVALID_ARGUMENT:
   3923 			case SCF_ERROR_NOT_SET:
   3924 			default:
   3925 				bad_error("scf_snaplevel_get_pg", scf_error());
   3926 			}
   3927 		}
   3928 
   3929 		r = load_pg(ud_pg, &old_dpt_pgroup, ient->sc_fmri,
   3930 		    snap_lastimport);
   3931 		switch (r) {
   3932 		case 0:
   3933 			break;
   3934 
   3935 		case ECANCELED:
   3936 		case ECONNABORTED:
   3937 		case ENOMEM:
   3938 		case EBADF:
   3939 			return (r);
   3940 
   3941 		case EACCES:
   3942 		default:
   3943 			bad_error("load_pg", r);
   3944 		}
   3945 
   3946 		if (pg_equal(old_dpt_pgroup, new_dpt_pgroup)) {
   3947 			/* no change, leave customizations */
   3948 			internal_pgroup_free(old_dpt_pgroup);
   3949 			return (0);
   3950 		}
   3951 		break;
   3952 
   3953 	case -1:
   3954 		warn(li_corrupt, ient->sc_fmri);
   3955 		return (EBADF);
   3956 
   3957 	case -2:
   3958 		warn(gettext("Dependent \"%s\" has invalid target \"%s\".\n"),
   3959 		    ud_name, new_dpt_pgroup->sc_pgroup_fmri);
   3960 		return (EINVAL);
   3961 
   3962 	default:
   3963 		bad_error("fmri_equal", r);
   3964 	}
   3965 
   3966 	/*
   3967 	 * The dependent has changed in the manifest.  Upgrade the current
   3968 	 * properties if they haven't been customized.
   3969 	 */
   3970 
   3971 	/*
   3972 	 * If new_dpt_pgroup->sc_override, then act as though the property
   3973 	 * group hasn't been customized.
   3974 	 */
   3975 	if (new_dpt_pgroup->sc_pgroup_override)
   3976 		goto nocust;
   3977 
   3978 	if (!ud_run_dpts_pg_set) {
   3979 		warn(cf_missing, ient->sc_fmri, ud_name);
   3980 		r = 0;
   3981 		goto out;
   3982 	} else if (scf_pg_get_property(ud_run_dpts_pg, ud_name, ud_prop) != 0) {
   3983 		switch (scf_error()) {
   3984 		case SCF_ERROR_NOT_FOUND:
   3985 			warn(cf_missing, ient->sc_fmri, ud_name);
   3986 			r = 0;
   3987 			goto out;
   3988 
   3989 		case SCF_ERROR_CONNECTION_BROKEN:
   3990 			r = scferror2errno(scf_error());
   3991 			goto out;
   3992 
   3993 		case SCF_ERROR_DELETED:
   3994 			warn(emsg_pg_deleted, ient->sc_fmri, "dependents");
   3995 			r = EBUSY;
   3996 			goto out;
   3997 
   3998 		case SCF_ERROR_INVALID_ARGUMENT:
   3999 		case SCF_ERROR_NOT_BOUND:
   4000 		case SCF_ERROR_HANDLE_MISMATCH:
   4001 		case SCF_ERROR_NOT_SET:
   4002 		default:
   4003 			bad_error("scf_pg_get_property", scf_error());
   4004 		}
   4005 	}
   4006 
   4007 	if (scf_property_get_value(ud_prop, ud_val) != 0) {
   4008 		switch (scf_error()) {
   4009 		case SCF_ERROR_NOT_FOUND:
   4010 		case SCF_ERROR_CONSTRAINT_VIOLATED:
   4011 			warn(cf_inval, ient->sc_fmri, ud_name);
   4012 			r = 0;
   4013 			goto out;
   4014 
   4015 		case SCF_ERROR_DELETED:
   4016 		case SCF_ERROR_CONNECTION_BROKEN:
   4017 			r = scferror2errno(scf_error());
   4018 			goto out;
   4019 
   4020 		case SCF_ERROR_HANDLE_MISMATCH:
   4021 		case SCF_ERROR_NOT_BOUND:
   4022 		case SCF_ERROR_NOT_SET:
   4023 		case SCF_ERROR_PERMISSION_DENIED:
   4024 		default:
   4025 			bad_error("scf_property_get_value", scf_error());
   4026 		}
   4027 	}
   4028 
   4029 	ty = scf_value_type(ud_val);
   4030 	assert(ty != SCF_TYPE_INVALID);
   4031 	if (!(ty == SCF_TYPE_FMRI || ty == SCF_TYPE_ASTRING)) {
   4032 		warn(cf_inval, ient->sc_fmri, ud_name);
   4033 		r = 0;
   4034 		goto out;
   4035 	}
   4036 	if (scf_value_get_as_string(ud_val, ud_ctarg, max_scf_value_len + 1) <
   4037 	    0)
   4038 		bad_error("scf_value_get_as_string", scf_error());
   4039 
   4040 	r = fmri_equal(ud_ctarg, ud_oldtarg);
   4041 	if (r == -1) {
   4042 		warn(cf_inval, ient->sc_fmri, ud_name);
   4043 		r = 0;
   4044 		goto out;
   4045 	} else if (r == -2) {
   4046 		warn(li_corrupt, ient->sc_fmri);
   4047 		r = EBADF;
   4048 		goto out;
   4049 	} else if (r == 0) {
   4050 		/*
   4051 		 * Target has been changed.  Only abort now if it's been
   4052 		 * changed to something other than what's in the manifest.
   4053 		 */
   4054 		r = fmri_equal(ud_ctarg, new_dpt_pgroup->sc_pgroup_fmri);
   4055 		if (r == -1) {
   4056 			warn(cf_inval, ient->sc_fmri, ud_name);
   4057 			r = 0;
   4058 			goto out;
   4059 		} else if (r == 0) {
   4060 			warn(cf_newtarg, ient->sc_fmri, ud_name);
   4061 			r = 0;
   4062 			goto out;
   4063 		} else if (r != 1) {
   4064 			/* invalid sc_pgroup_fmri caught above */
   4065 			bad_error("fmri_equal", r);
   4066 		}
   4067 
   4068 		/*
   4069 		 * Fetch the current dependency pg.  If it's what the manifest
   4070 		 * says, then no problem.
   4071 		 */
   4072 		serr = fmri_to_entity(g_hndl, ud_ctarg, &target_ent, &tissvc);
   4073 		switch (serr) {
   4074 		case SCF_ERROR_NONE:
   4075 			break;
   4076 
   4077 		case SCF_ERROR_NOT_FOUND:
   4078 			warn(cf_missing, ient->sc_fmri, ud_name);
   4079 			r = 0;
   4080 			goto out;
   4081 
   4082 		case SCF_ERROR_NO_MEMORY:
   4083 			r = ENOMEM;
   4084 			goto out;
   4085 
   4086 		case SCF_ERROR_CONSTRAINT_VIOLATED:
   4087 		case SCF_ERROR_INVALID_ARGUMENT:
   4088 		default:
   4089 			bad_error("fmri_to_entity", serr);
   4090 		}
   4091 
   4092 		r = entity_get_running_pg(target_ent, tissvc, ud_name,
   4093 		    ud_pg, ud_iter2, ud_inst, imp_snap, ud_snpl);
   4094 		switch (r) {
   4095 		case 0:
   4096 			break;
   4097 
   4098 		case ECONNABORTED:
   4099 			goto out;
   4100 
   4101 		case ECANCELED:
   4102 		case ENOENT:
   4103 			warn(cf_missing, ient->sc_fmri, ud_name);
   4104 			r = 0;
   4105 			goto out;
   4106 
   4107 		case EBADF:
   4108 			warn(r_no_lvl, ud_ctarg);
   4109 			goto out;
   4110 
   4111 		case EINVAL:
   4112 		default:
   4113 			bad_error("entity_get_running_pg", r);
   4114 		}
   4115 
   4116 		r = load_pg(ud_pg, &current_pg, ud_ctarg, NULL);
   4117 		switch (r) {
   4118 		case 0:
   4119 			break;
   4120 
   4121 		case ECANCELED:
   4122 			warn(cf_missing, ient->sc_fmri, ud_name);
   4123 			r = 0;
   4124 			goto out;
   4125 
   4126 		case ECONNABORTED:
   4127 		case ENOMEM:
   4128 		case EBADF:
   4129 			goto out;
   4130 
   4131 		case EACCES:
   4132 		default:
   4133 			bad_error("load_pg", r);
   4134 		}
   4135 
   4136 		if (!pg_equal(current_pg, new_dpt_pgroup))
   4137 			warn(cf_newdpg, ient->sc_fmri, ud_name);
   4138 		internal_pgroup_free(current_pg);
   4139 		r = 0;
   4140 		goto out;
   4141 	} else if (r != 1) {
   4142 		bad_error("fmri_equal", r);
   4143 	}
   4144 
   4145 nocust:
   4146 	/*
   4147 	 * Target has not been customized.  Check the dependency property
   4148 	 * group.
   4149 	 */
   4150 
   4151 	if (old_dpt_pgroup == NULL) {
   4152 		if (scf_snaplevel_get_pg(snpl, new_dpt_pgroup->sc_pgroup_name,
   4153 		    ud_pg) != 0) {
   4154 			switch (scf_error()) {
   4155 			case SCF_ERROR_NOT_FOUND:
   4156 				warn(li_corrupt, ient->sc_fmri);
   4157 				return (EBADF);
   4158 
   4159 			case SCF_ERROR_DELETED:
   4160 			case SCF_ERROR_CONNECTION_BROKEN:
   4161 				return (scferror2errno(scf_error()));
   4162 
   4163 			case SCF_ERROR_NOT_BOUND:
   4164 			case SCF_ERROR_HANDLE_MISMATCH:
   4165 			case SCF_ERROR_INVALID_ARGUMENT:
   4166 			case SCF_ERROR_NOT_SET:
   4167 			default:
   4168 				bad_error("scf_snaplevel_get_pg", scf_error());
   4169 			}
   4170 		}
   4171 
   4172 		r = load_pg(ud_pg, &old_dpt_pgroup, ient->sc_fmri,
   4173 		    snap_lastimport);
   4174 		switch (r) {
   4175 		case 0:
   4176 			break;
   4177 
   4178 		case ECANCELED:
   4179 		case ECONNABORTED:
   4180 		case ENOMEM:
   4181 		case EBADF:
   4182 			return (r);
   4183 
   4184 		case EACCES:
   4185 		default:
   4186 			bad_error("load_pg", r);
   4187 		}
   4188 	}
   4189 
   4190 	serr = fmri_to_entity(g_hndl, ud_ctarg, &target_ent, &tissvc);
   4191 	switch (serr) {
   4192 	case SCF_ERROR_NONE:
   4193 		break;
   4194 
   4195 	case SCF_ERROR_NOT_FOUND:
   4196 		warn(cf_missing, ient->sc_fmri, ud_name);
   4197 		r = 0;
   4198 		goto out;
   4199 
   4200 	case SCF_ERROR_NO_MEMORY:
   4201 		r = ENOMEM;
   4202 		goto out;
   4203 
   4204 	case SCF_ERROR_CONSTRAINT_VIOLATED:
   4205 	case SCF_ERROR_INVALID_ARGUMENT:
   4206 	default:
   4207 		bad_error("fmri_to_entity", serr);
   4208 	}
   4209 
   4210 	r = entity_get_running_pg(target_ent, tissvc, ud_name, ud_pg,
   4211 	    ud_iter2, ud_inst, imp_snap, ud_snpl);
   4212 	switch (r) {
   4213 	case 0:
   4214 		break;
   4215 
   4216 	case ECONNABORTED:
   4217 		goto out;
   4218 
   4219 	case ECANCELED:
   4220 	case ENOENT:
   4221 		warn(cf_missing, ient->sc_fmri, ud_name);
   4222 		r = 0;
   4223 		goto out;
   4224 
   4225 	case EBADF:
   4226 		warn(r_no_lvl, ud_ctarg);
   4227 		goto out;
   4228 
   4229 	case EINVAL:
   4230 	default:
   4231 		bad_error("entity_get_running_pg", r);
   4232 	}
   4233 
   4234 	r = load_pg(ud_pg, &current_pg, ud_ctarg, NULL);
   4235 	switch (r) {
   4236 	case 0:
   4237 		break;
   4238 
   4239 	case ECANCELED:
   4240 		warn(cf_missing, ient->sc_fmri, ud_name);
   4241 		goto out;
   4242 
   4243 	case ECONNABORTED:
   4244 	case ENOMEM:
   4245 	case EBADF:
   4246 		goto out;
   4247 
   4248 	case EACCES:
   4249 	default:
   4250 		bad_error("load_pg", r);
   4251 	}
   4252 
   4253 	if (!pg_equal(current_pg, old_dpt_pgroup)) {
   4254 		if (!pg_equal(current_pg, new_dpt_pgroup))
   4255 			warn(cf_newdpg, ient->sc_fmri, ud_name);
   4256 		internal_pgroup_free(current_pg);
   4257 		r = 0;
   4258 		goto out;
   4259 	}
   4260 
   4261 	/* Uncustomized.  Upgrade. */
   4262 
   4263 	r = fmri_equal(new_dpt_pgroup->sc_pgroup_fmri, ud_oldtarg);
   4264 	switch (r) {
   4265 	case 1:
   4266 		if (pg_equal(current_pg, new_dpt_pgroup)) {
   4267 			/* Already upgraded. */
   4268 			internal_pgroup_free(current_pg);
   4269 			r = 0;
   4270 			goto out;
   4271 		}
   4272 
   4273 		internal_pgroup_free(current_pg);
   4274 
   4275 		/* upgrade current_pg */
   4276 		if (entity_get_pg(target_ent, tissvc, ud_name, ud_pg) != 0) {
   4277 			switch (scf_error()) {
   4278 			case SCF_ERROR_CONNECTION_BROKEN:
   4279 				r = scferror2errno(scf_error());
   4280 				goto out;
   4281 
   4282 			case SCF_ERROR_DELETED:
   4283 				warn(cf_missing, ient->sc_fmri, ud_name);
   4284 				r = 0;
   4285 				goto out;
   4286 
   4287 			case SCF_ERROR_NOT_FOUND:
   4288 				break;
   4289 
   4290 			case SCF_ERROR_INVALID_ARGUMENT:
   4291 			case SCF_ERROR_NOT_BOUND:
   4292 			case SCF_ERROR_NOT_SET:
   4293 			case SCF_ERROR_HANDLE_MISMATCH:
   4294 			default:
   4295 				bad_error("entity_get_pg", scf_error());
   4296 			}
   4297 
   4298 			if (tissvc)
   4299 				r = scf_service_add_pg(target_ent, ud_name,
   4300 				    SCF_GROUP_DEPENDENCY, 0, ud_pg);
   4301 			else
   4302 				r = scf_instance_add_pg(target_ent, ud_name,
   4303 				    SCF_GROUP_DEPENDENCY, 0, ud_pg);
   4304 			if (r != 0) {
   4305 				switch (scf_error()) {
   4306 				case SCF_ERROR_CONNECTION_BROKEN:
   4307 				case SCF_ERROR_NO_RESOURCES:
   4308 				case SCF_ERROR_BACKEND_READONLY:
   4309 				case SCF_ERROR_BACKEND_ACCESS:
   4310 					r = scferror2errno(scf_error());
   4311 					goto out;
   4312 
   4313 				case SCF_ERROR_DELETED:
   4314 					warn(cf_missing, ient->sc_fmri,
   4315 					    ud_name);
   4316 					r = 0;
   4317 					goto out;
   4318 
   4319 				case SCF_ERROR_PERMISSION_DENIED:
   4320 					warn(emsg_pg_deleted, ud_ctarg,
   4321 					    ud_name);
   4322 					r = EPERM;
   4323 					goto out;
   4324 
   4325 				case SCF_ERROR_EXISTS:
   4326 					warn(emsg_pg_added, ud_ctarg, ud_name);
   4327 					r = EBUSY;
   4328 					goto out;
   4329 
   4330 				case SCF_ERROR_NOT_BOUND:
   4331 				case SCF_ERROR_HANDLE_MISMATCH:
   4332 				case SCF_ERROR_INVALID_ARGUMENT:
   4333 				case SCF_ERROR_NOT_SET:
   4334 				default:
   4335 					bad_error("entity_add_pg", scf_error());
   4336 				}
   4337 			}
   4338 		}
   4339 
   4340 		r = load_pg(ud_pg, &current_pg, ud_ctarg, NULL);
   4341 		switch (r) {
   4342 		case 0:
   4343 			break;
   4344 
   4345 		case ECANCELED:
   4346 			warn(cf_missing, ient->sc_fmri, ud_name);
   4347 			goto out;
   4348 
   4349 		case ECONNABORTED:
   4350 		case ENOMEM:
   4351 		case EBADF:
   4352 			goto out;
   4353 
   4354 		case EACCES:
   4355 		default:
   4356 			bad_error("load_pg", r);
   4357 		}
   4358 
   4359 		if (g_verbose)
   4360 			warn(upgrading, ient->sc_fmri, ud_name);
   4361 
   4362 		r = upgrade_pg(ud_pg, current_pg, old_dpt_pgroup,
   4363 		    new_dpt_pgroup, 0, ient->sc_fmri);
   4364 		switch (r) {
   4365 		case 0:
   4366 			break;
   4367 
   4368 		case ECANCELED:
   4369 			warn(emsg_pg_deleted, ud_ctarg, ud_name);
   4370 			r = EBUSY;
   4371 			goto out;
   4372 
   4373 		case EPERM:
   4374 			warn(emsg_pg_mod_perm, ud_name, ud_ctarg);
   4375 			goto out;
   4376 
   4377 		case EBUSY:
   4378 			warn(emsg_pg_changed, ud_ctarg, ud_name);
   4379 			goto out;
   4380 
   4381 		case ECONNABORTED:
   4382 		case ENOMEM:
   4383 		case ENOSPC:
   4384 		case EROFS:
   4385 		case EACCES:
   4386 		case EINVAL:
   4387 			goto out;
   4388 
   4389 		default:
   4390 			bad_error("upgrade_pg", r);
   4391 		}
   4392 		break;
   4393 
   4394 	case 0: {
   4395 		scf_transaction_entry_t *ent;
   4396 		scf_value_t *val;
   4397 
   4398 		internal_pgroup_free(current_pg);
   4399 
   4400 		/* delete old pg */
   4401 		if (g_verbose)
   4402 			warn(upgrading, ient->sc_fmri, ud_name);
   4403 
   4404 		if (entity_get_pg(target_ent, tissvc, ud_name, ud_pg) != 0) {
   4405 			switch (scf_error()) {
   4406 			case SCF_ERROR_CONNECTION_BROKEN:
   4407 				r = scferror2errno(scf_error());
   4408 				goto out;
   4409 
   4410 			case SCF_ERROR_DELETED:
   4411 				warn(cf_missing, ient->sc_fmri, ud_name);
   4412 				r = 0;
   4413 				goto out;
   4414 
   4415 			case SCF_ERROR_NOT_FOUND:
   4416 				break;
   4417 
   4418 			case SCF_ERROR_INVALID_ARGUMENT:
   4419 			case SCF_ERROR_NOT_BOUND:
   4420 			case SCF_ERROR_NOT_SET:
   4421 			case SCF_ERROR_HANDLE_MISMATCH:
   4422 			default:
   4423 				bad_error("entity_get_pg", scf_error());
   4424 			}
   4425 		} else if (scf_pg_delete(ud_pg) != 0) {
   4426 			switch (scf_error()) {
   4427 			case SCF_ERROR_DELETED:
   4428 				break;
   4429 
   4430 			case SCF_ERROR_CONNECTION_BROKEN:
   4431 			case SCF_ERROR_BACKEND_READONLY:
   4432 			case SCF_ERROR_BACKEND_ACCESS:
   4433 				r = scferror2errno(scf_error());
   4434 				goto out;
   4435 
   4436 			case SCF_ERROR_PERMISSION_DENIED:
   4437 				warn(emsg_pg_del_perm, ud_name, ient->sc_fmri);
   4438 				r = scferror2errno(scf_error());
   4439 				goto out;
   4440 
   4441 			case SCF_ERROR_NOT_SET:
   4442 			default:
   4443 				bad_error("scf_pg_delete", scf_error());
   4444 			}
   4445 		}
   4446 
   4447 		/* import new one */
   4448 		cbdata.sc_handle = g_hndl;
   4449 		cbdata.sc_trans = NULL;		/* handled below */
   4450 
   4451 		r = lscf_dependent_import(new_dpt_pgroup, &cbdata);
   4452 		if (r != UU_WALK_NEXT) {
   4453 			if (r != UU_WALK_ERROR)
   4454 				bad_error("lscf_dependent_import", r);
   4455 
   4456 			r = cbdata.sc_err;
   4457 			goto out;
   4458 		}
   4459 
   4460 		if (tx == NULL)
   4461 			break;
   4462 
   4463 		if ((ent = scf_entry_create(g_hndl)) == NULL ||
   4464 		    (val = scf_value_create(g_hndl)) == NULL) {
   4465 			if (scf_error() == SCF_ERROR_NO_MEMORY)
   4466 				return (ENOMEM);
   4467 
   4468 			bad_error("scf_entry_create", scf_error());
   4469 		}
   4470 
   4471 		if (scf_transaction_property_change_type(tx, ent, ud_name,
   4472 		    SCF_TYPE_FMRI) != 0) {
   4473 			switch (scf_error()) {
   4474 			case SCF_ERROR_CONNECTION_BROKEN:
   4475 				r = scferror2errno(scf_error());
   4476 				goto out;
   4477 
   4478 			case SCF_ERROR_DELETED:
   4479 				warn(emsg_pg_deleted, ient->sc_fmri,
   4480 				    "dependents");
   4481 				r = EBUSY;
   4482 				goto out;
   4483 
   4484 			case SCF_ERROR_NOT_FOUND:
   4485 				break;
   4486 
   4487 			case SCF_ERROR_NOT_BOUND:
   4488 			case SCF_ERROR_HANDLE_MISMATCH:
   4489 			case SCF_ERROR_INVALID_ARGUMENT:
   4490 			case SCF_ERROR_NOT_SET:
   4491 			default:
   4492 				bad_error("scf_transaction_property_"
   4493 				    "change_type", scf_error());
   4494 			}
   4495 
   4496 			if (scf_transaction_property_new(tx, ent, ud_name,
   4497 			    SCF_TYPE_FMRI) != 0) {
   4498 				switch (scf_error()) {
   4499 				case SCF_ERROR_CONNECTION_BROKEN:
   4500 					r = scferror2errno(scf_error());
   4501 					goto out;
   4502 
   4503 				case SCF_ERROR_DELETED:
   4504 					warn(emsg_pg_deleted, ient->sc_fmri,
   4505 					    "dependents");
   4506 					r = EBUSY;
   4507 					goto out;
   4508 
   4509 				case SCF_ERROR_EXISTS:
   4510 					warn(emsg_pg_changed, ient->sc_fmri,
   4511 					    "dependents");
   4512 					r = EBUSY;
   4513 					goto out;
   4514 
   4515 				case SCF_ERROR_INVALID_ARGUMENT:
   4516 				case SCF_ERROR_HANDLE_MISMATCH:
   4517 				case SCF_ERROR_NOT_BOUND:
   4518 				case SCF_ERROR_NOT_SET:
   4519 				default:
   4520 					bad_error("scf_transaction_property_"
   4521 					    "new", scf_error());
   4522 				}
   4523 			}
   4524 		}
   4525 
   4526 		if (scf_value_set_from_string(val, SCF_TYPE_FMRI,
   4527 		    new_dpt_pgroup->sc_pgroup_fmri) != 0)
   4528 			/* invalid sc_pgroup_fmri caught above */
   4529 			bad_error("scf_value_set_from_string",
   4530 			    scf_error());
   4531 
   4532 		if (scf_entry_add_value(ent, val) != 0)
   4533 			bad_error("scf_entry_add_value", scf_error());
   4534 		break;
   4535 	}
   4536 
   4537 	case -2:
   4538 		warn(li_corrupt, ient->sc_fmri);
   4539 		internal_pgroup_free(current_pg);
   4540 		r = EBADF;
   4541 		goto out;
   4542 
   4543 	case -1:
   4544 	default:
   4545 		/* invalid sc_pgroup_fmri caught above */
   4546 		bad_error("fmri_equal", r);
   4547 	}
   4548 
   4549 	r = 0;
   4550 
   4551 out:
   4552 	if (old_dpt_pgroup != NULL)
   4553 		internal_pgroup_free(old_dpt_pgroup);
   4554 
   4555 	return (r);
   4556 }
   4557 
   4558 /*
   4559  * new_dpt_pgroup was in the manifest but not the last-import snapshot, so we
   4560  * would import it, except it seems to exist in the service anyway.  Compare
   4561  * the existent dependent with the one we would import, and report any
   4562  * differences (if there are none, be silent).  prop is the property which
   4563  * represents the existent dependent (in the dependents property group) in the
   4564  * entity corresponding to ient.
   4565  *
   4566  * Returns
   4567  *   0 - success (Sort of.  At least, we can continue importing.)
   4568  *   ECONNABORTED - repository connection broken
   4569  *   EBUSY - ancestor of prop was deleted (error printed)
   4570  *   ENOMEM - out of memory
   4571  *   EBADF - corrupt property group (error printed)
   4572  *   EINVAL - new_dpt_pgroup has invalid target (error printed)
   4573  */
   4574 static int
   4575 handle_dependent_conflict(const entity_t * const ient,
   4576     const scf_property_t * const prop, const pgroup_t * const new_dpt_pgroup)
   4577 {
   4578 	int r;
   4579 	scf_type_t ty;
   4580 	scf_error_t scfe;
   4581 	void *tptr;
   4582 	int tissvc;
   4583 	pgroup_t *pgroup;
   4584 
   4585 	if (scf_property_get_value(prop, ud_val) != 0) {
   4586 		switch (scf_error()) {
   4587 		case SCF_ERROR_CONNECTION_BROKEN:
   4588 			return (scferror2errno(scf_error()));
   4589 
   4590 		case SCF_ERROR_DELETED:
   4591 			warn(emsg_pg_deleted, ient->sc_fmri,
   4592 			    new_dpt_pgroup->sc_pgroup_name);
   4593 			return (EBUSY);
   4594 
   4595 		case SCF_ERROR_CONSTRAINT_VIOLATED:
   4596 		case SCF_ERROR_NOT_FOUND:
   4597 			warn(gettext("Conflict upgrading %s (not importing "
   4598 			    "dependent \"%s\" because it already exists.)  "
   4599 			    "Warning: The \"%s/%2$s\" property has more or "
   4600 			    "fewer than one value)).\n"), ient->sc_fmri,
   4601 			    new_dpt_pgroup->sc_pgroup_name, "dependents");
   4602 			return (0);
   4603 
   4604 		case SCF_ERROR_HANDLE_MISMATCH:
   4605 		case SCF_ERROR_NOT_BOUND:
   4606 		case SCF_ERROR_NOT_SET:
   4607 		case SCF_ERROR_PERMISSION_DENIED:
   4608 		default:
   4609 			bad_error("scf_property_get_value",
   4610 			    scf_error());
   4611 		}
   4612 	}
   4613 
   4614 	ty = scf_value_type(ud_val);
   4615 	assert(ty != SCF_TYPE_INVALID);
   4616 	if (!(ty == SCF_TYPE_FMRI || ty == SCF_TYPE_ASTRING)) {
   4617 		warn(gettext("Conflict upgrading %s (not importing dependent "
   4618 		    "\"%s\" because it already exists).  Warning: The "
   4619 		    "\"%s/%s\" property has unexpected type \"%s\")).\n"),
   4620 		    ient->sc_fmri, new_dpt_pgroup->sc_pgroup_name,
   4621 		    scf_type_to_string(ty), "dependents");
   4622 		return (0);
   4623 	}
   4624 
   4625 	if (scf_value_get_as_string(ud_val, ud_ctarg, max_scf_value_len + 1) <
   4626 	    0)
   4627 		bad_error("scf_value_get_as_string", scf_error());
   4628 
   4629 	r = fmri_equal(ud_ctarg, new_dpt_pgroup->sc_pgroup_fmri);
   4630 	switch (r) {
   4631 	case 0:
   4632 		warn(gettext("Conflict upgrading %s (not importing dependent "
   4633 		    "\"%s\" (target \"%s\") because it already exists with "
   4634 		    "target \"%s\").\n"), ient->sc_fmri,
   4635 		    new_dpt_pgroup->sc_pgroup_name,
   4636 		    new_dpt_pgroup->sc_pgroup_fmri, ud_ctarg);
   4637 		return (0);
   4638 
   4639 	case 1:
   4640 		break;
   4641 
   4642 	case -1:
   4643 		warn(gettext("Conflict upgrading %s (not importing dependent "
   4644 		    "\"%s\" because it already exists).  Warning: The current "
   4645 		    "dependent's target (%s) is invalid.\n"), ient->sc_fmri,
   4646 		    new_dpt_pgroup->sc_pgroup_name, ud_ctarg);
   4647 		return (0);
   4648 
   4649 	case -2:
   4650 		warn(gettext("Dependent \"%s\" of %s has invalid target "
   4651 		    "\"%s\".\n"), new_dpt_pgroup->sc_pgroup_name, ient->sc_fmri,
   4652 		    new_dpt_pgroup->sc_pgroup_fmri);
   4653 		return (EINVAL);
   4654 
   4655 	default:
   4656 		bad_error("fmri_equal", r);
   4657 	}
   4658 
   4659 	/* compare dependency pgs in target */
   4660 	scfe = fmri_to_entity(g_hndl, ud_ctarg, &tptr, &tissvc);
   4661 	switch (scfe) {
   4662 	case SCF_ERROR_NONE:
   4663 		break;
   4664 
   4665 	case SCF_ERROR_NO_MEMORY:
   4666 		return (ENOMEM);
   4667 
   4668 	case SCF_ERROR_NOT_FOUND:
   4669 		warn(emsg_dpt_dangling, ient->sc_fmri,
   4670 		    new_dpt_pgroup->sc_pgroup_name, ud_ctarg);
   4671 		return (0);
   4672 
   4673 	case SCF_ERROR_CONSTRAINT_VIOLATED:
   4674 	case SCF_ERROR_INVALID_ARGUMENT:
   4675 	default:
   4676 		bad_error("fmri_to_entity", scfe);
   4677 	}
   4678 
   4679 	r = entity_get_running_pg(tptr, tissvc, new_dpt_pgroup->sc_pgroup_name,
   4680 	    ud_pg, ud_iter, ud_inst, imp_snap, ud_snpl);
   4681 	switch (r) {
   4682 	case 0:
   4683 		break;
   4684 
   4685 	case ECONNABORTED:
   4686 		return (r);
   4687 
   4688 	case ECANCELED:
   4689 		warn(emsg_dpt_dangling, ient->sc_fmri,
   4690 		    new_dpt_pgroup->sc_pgroup_name, ud_ctarg);
   4691 		return (0);
   4692 
   4693 	case EBADF:
   4694 		if (tissvc)
   4695 			warn(gettext("%s has an instance with a \"%s\" "
   4696 			    "snapshot which is missing a snaplevel.\n"),
   4697 			    ud_ctarg, "running");
   4698 		else
   4699 			warn(gettext("%s has a \"%s\" snapshot which is "
   4700 			    "missing a snaplevel.\n"), ud_ctarg, "running");
   4701 		/* FALLTHROUGH */
   4702 
   4703 	case ENOENT:
   4704 		warn(emsg_dpt_no_dep, ient->sc_fmri,
   4705 		    new_dpt_pgroup->sc_pgroup_name, ud_ctarg,
   4706 		    new_dpt_pgroup->sc_pgroup_name);
   4707 		return (0);
   4708 
   4709 	case EINVAL:
   4710 	default:
   4711 		bad_error("entity_get_running_pg", r);
   4712 	}
   4713 
   4714 	pgroup = internal_pgroup_new();
   4715 	if (pgroup == NULL)
   4716 		return (ENOMEM);
   4717 
   4718 	r = load_pg(ud_pg, &pgroup, ud_ctarg, NULL);
   4719 	switch (r) {
   4720 	case 0:
   4721 		break;
   4722 
   4723 	case ECONNABORTED:
   4724 	case EBADF:
   4725 	case ENOMEM:
   4726 		internal_pgroup_free(pgroup);
   4727 		return (r);
   4728 
   4729 	case ECANCELED:
   4730 		warn(emsg_dpt_no_dep, ient->sc_fmri,
   4731 		    new_dpt_pgroup->sc_pgroup_name, ud_ctarg,
   4732 		    new_dpt_pgroup->sc_pgroup_name);
   4733 		internal_pgroup_free(pgroup);
   4734 		return (0);
   4735 
   4736 	case EACCES:
   4737 	default:
   4738 		bad_error("load_pg", r);
   4739 	}
   4740 
   4741 	/* report differences */
   4742 	report_pg_diffs(new_dpt_pgroup, pgroup, ud_ctarg, 1);
   4743 	internal_pgroup_free(pgroup);
   4744 	return (0);
   4745 }
   4746 
   4747 /*
   4748  * lipg is a property group in the last-import snapshot of ent, which is an
   4749  * scf_service_t or an scf_instance_t (according to ient).  If lipg is not in
   4750  * ient's pgroups, delete it from ent if it hasn't been customized.  If it is
   4751  * in ents's property groups, compare and upgrade ent appropriately.
   4752  *
   4753  * Returns
   4754  *   0 - success
   4755  *   ECONNABORTED - repository connection broken
   4756  *   ENOMEM - out of memory
   4757  *   ENOSPC - configd is out of resources
   4758  *   EINVAL - ient has invalid dependent (error printed)
   4759  *	    - ient has invalid pgroup_t (error printed)
   4760  *   ECANCELED - ent has been deleted
   4761  *   ENODEV - entity containing lipg has been deleted
   4762  *	    - entity containing running has been deleted
   4763  *   EPERM - could not delete pg (permission denied) (error printed)
   4764  *	   - couldn't upgrade dependents (permission denied) (error printed)
   4765  *	   - couldn't import pg (permission denied) (error printed)
   4766  *	   - couldn't upgrade pg (permission denied) (error printed)
   4767  *   EROFS - could not delete pg (repository read-only)
   4768  *	   - couldn't upgrade dependents (repository read-only)
   4769  *	   - couldn't import pg (repository read-only)
   4770  *	   - couldn't upgrade pg (repository read-only)
   4771  *   EACCES - could not delete pg (backend access denied)
   4772  *	    - couldn't upgrade dependents (backend access denied)
   4773  *	    - couldn't import pg (backend access denied)
   4774  *	    - couldn't upgrade pg (backend access denied)
   4775  *	    - couldn't read property (backend access denied)
   4776  *   EBUSY - property group was added (error printed)
   4777  *	   - property group was deleted (error printed)
   4778  *	   - property group changed (error printed)
   4779  *	   - "dependents" pg was added, changed, or deleted (error printed)
   4780  *	   - dependent target deleted (error printed)
   4781  *	   - dependent pg changed (error printed)
   4782  *   EBADF - imp_snpl is corrupt (error printed)
   4783  *	   - ent has bad pg (error printed)
   4784  *   EEXIST - dependent collision in target service (error printed)
   4785  */
   4786 static int
   4787 process_old_pg(const scf_propertygroup_t *lipg, entity_t *ient, void *ent,
   4788     const scf_snaplevel_t *running)
   4789 {
   4790 	int r;
   4791 	pgroup_t *mpg, *lipg_i, *curpg_i, pgrp;
   4792 	scf_callback_t cbdata;
   4793 
   4794 	const char * const cf_pg_missing =
   4795 	    gettext("Conflict upgrading %s (property group %s is missing)\n");
   4796 	const char * const deleting =
   4797 	    gettext("%s: Deleting property group \"%s\".\n");
   4798 
   4799 	const int issvc = (ient->sc_etype == SVCCFG_SERVICE_OBJECT);
   4800 
   4801 	/* Skip dependent property groups. */
   4802 	if (scf_pg_get_type(lipg, imp_str, imp_str_sz) < 0) {
   4803 		switch (scf_error()) {
   4804 		case SCF_ERROR_DELETED:
   4805 			return (ENODEV);
   4806 
   4807 		case SCF_ERROR_CONNECTION_BROKEN:
   4808 			return (ECONNABORTED);
   4809 
   4810 		case SCF_ERROR_NOT_SET:
   4811 		case SCF_ERROR_NOT_BOUND:
   4812 		default:
   4813 			bad_error("scf_pg_get_type", scf_error());
   4814 		}
   4815 	}
   4816 
   4817 	if (strcmp(imp_str, SCF_GROUP_DEPENDENCY) == 0) {
   4818 		if (scf_pg_get_property(lipg, "external", NULL) == 0)
   4819 			return (0);
   4820 
   4821 		switch (scf_error()) {
   4822 		case SCF_ERROR_NOT_FOUND:
   4823 			break;
   4824 
   4825 		case SCF_ERROR_CONNECTION_BROKEN:
   4826 			return (ECONNABORTED);
   4827 
   4828 		case SCF_ERROR_DELETED:
   4829 			return (ENODEV);
   4830 
   4831 		case SCF_ERROR_INVALID_ARGUMENT:
   4832 		case SCF_ERROR_NOT_BOUND:
   4833 		case SCF_ERROR_HANDLE_MISMATCH:
   4834 		case SCF_ERROR_NOT_SET:
   4835 		default:
   4836 			bad_error("scf_pg_get_property", scf_error());
   4837 		}
   4838 	}
   4839 
   4840 	/* lookup pg in new properties */
   4841 	if (scf_pg_get_name(lipg, imp_str, imp_str_sz) < 0) {
   4842 		switch (scf_error()) {
   4843 		case SCF_ERROR_DELETED:
   4844 			return (ENODEV);
   4845 
   4846 		case SCF_ERROR_CONNECTION_BROKEN:
   4847 			return (ECONNABORTED);
   4848 
   4849 		case SCF_ERROR_NOT_SET:
   4850 		case SCF_ERROR_NOT_BOUND:
   4851 		default:
   4852 			bad_error("scf_pg_get_name", scf_error());
   4853 		}
   4854 	}
   4855 
   4856 	pgrp.sc_pgroup_name = imp_str;
   4857 	mpg = uu_list_find(ient->sc_pgroups, &pgrp, NULL, NULL);
   4858 
   4859 	if (mpg != NULL)
   4860 		mpg->sc_pgroup_seen = 1;
   4861 
   4862 	/* Special handling for dependents */
   4863 	if (strcmp(imp_str, "dependents") == 0)
   4864 		return (upgrade_dependents(lipg, imp_snpl, ient, running, ent));
   4865 
   4866 	if (strcmp(imp_str, SCF_PG_MANIFESTFILES) == 0)
   4867 		return (upgrade_manifestfiles(NULL, ient, running, ent));
   4868 
   4869 	if (mpg == NULL || mpg->sc_pgroup_delete) {
   4870 		/* property group was deleted from manifest */
   4871 		if (entity_get_pg(ent, issvc, imp_str, imp_pg2) != 0) {
   4872 			switch (scf_error()) {
   4873 			case SCF_ERROR_NOT_FOUND:
   4874 				return (0);
   4875 
   4876 			case SCF_ERROR_DELETED:
   4877 			case SCF_ERROR_CONNECTION_BROKEN:
   4878 				return (scferror2errno(scf_error()));
   4879 
   4880 			case SCF_ERROR_INVALID_ARGUMENT:
   4881 			case SCF_ERROR_HANDLE_MISMATCH:
   4882 			case SCF_ERROR_NOT_BOUND:
   4883 			case SCF_ERROR_NOT_SET:
   4884 			default:
   4885 				bad_error("entity_get_pg", scf_error());
   4886 			}
   4887 		}
   4888 
   4889 		if (mpg != NULL && mpg->sc_pgroup_delete) {
   4890 			if (g_verbose)
   4891 				warn(deleting, ient->sc_fmri, imp_str);
   4892 			if (scf_pg_delete(imp_pg2) == 0)
   4893 				return (0);
   4894 
   4895 			switch (scf_error()) {
   4896 			case SCF_ERROR_DELETED:
   4897 				return (0);
   4898 
   4899 			case SCF_ERROR_CONNECTION_BROKEN:
   4900 			case SCF_ERROR_BACKEND_READONLY:
   4901 			case SCF_ERROR_BACKEND_ACCESS:
   4902 				return (scferror2errno(scf_error()));
   4903 
   4904 			case SCF_ERROR_PERMISSION_DENIED:
   4905 				warn(emsg_pg_del_perm, imp_str, ient->sc_fmri);
   4906 				return (scferror2errno(scf_error()));
   4907 
   4908 			case SCF_ERROR_NOT_SET:
   4909 			default:
   4910 				bad_error("scf_pg_delete", scf_error());
   4911 			}
   4912 		}
   4913 
   4914 		r = load_pg(lipg, &lipg_i, ient->sc_fmri, snap_lastimport);
   4915 		switch (r) {
   4916 		case 0:
   4917 			break;
   4918 
   4919 		case ECANCELED:
   4920 			return (ENODEV);
   4921 
   4922 		case ECONNABORTED:
   4923 		case ENOMEM:
   4924 		case EBADF:
   4925 		case EACCES:
   4926 			return (r);
   4927 
   4928 		default:
   4929 			bad_error("load_pg", r);
   4930 		}
   4931 
   4932 		r = load_pg(imp_pg2, &curpg_i, ient->sc_fmri, NULL);
   4933 		switch (r) {
   4934 		case 0:
   4935 			break;
   4936 
   4937 		case ECANCELED:
   4938 		case ECONNABORTED:
   4939 		case ENOMEM:
   4940 		case EBADF:
   4941 		case EACCES:
   4942 			internal_pgroup_free(lipg_i);
   4943 			return (r);
   4944 
   4945 		default:
   4946 			bad_error("load_pg", r);
   4947 		}
   4948 
   4949 		if (pg_equal(lipg_i, curpg_i)) {
   4950 			if (g_verbose)
   4951 				warn(deleting, ient->sc_fmri, imp_str);
   4952 			if (scf_pg_delete(imp_pg2) != 0) {
   4953 				switch (scf_error()) {
   4954 				case SCF_ERROR_DELETED:
   4955 					break;
   4956 
   4957 				case SCF_ERROR_CONNECTION_BROKEN:
   4958 					internal_pgroup_free(lipg_i);
   4959 					internal_pgroup_free(curpg_i);
   4960 					return (ECONNABORTED);
   4961 
   4962 				case SCF_ERROR_NOT_SET:
   4963 				case SCF_ERROR_NOT_BOUND:
   4964 				default:
   4965 					bad_error("scf_pg_delete", scf_error());
   4966 				}
   4967 			}
   4968 		} else {
   4969 			report_pg_diffs(lipg_i, curpg_i, ient->sc_fmri, 0);
   4970 		}
   4971 
   4972 		internal_pgroup_free(lipg_i);
   4973 		internal_pgroup_free(curpg_i);
   4974 
   4975 		return (0);
   4976 	}
   4977 
   4978 	/*
   4979 	 * Only dependent pgs can have override set, and we skipped those
   4980 	 * above.
   4981 	 */
   4982 	assert(!mpg->sc_pgroup_override);
   4983 
   4984 	/* compare */
   4985 	r = load_pg(lipg, &lipg_i, ient->sc_fmri, snap_lastimport);
   4986 	switch (r) {
   4987 	case 0:
   4988 		break;
   4989 
   4990 	case ECANCELED:
   4991 		return (ENODEV);
   4992 
   4993 	case ECONNABORTED:
   4994 	case EBADF:
   4995 	case ENOMEM:
   4996 	case EACCES:
   4997 		return (r);
   4998 
   4999 	default:
   5000 		bad_error("load_pg", r);
   5001 	}
   5002 
   5003 	if (pg_equal(mpg, lipg_i)) {
   5004 		/* The manifest pg has not changed.  Move on. */
   5005 		r = 0;
   5006 		goto out;
   5007 	}
   5008 
   5009 	/* upgrade current properties according to lipg & mpg */
   5010 	if (running != NULL)
   5011 		r = scf_snaplevel_get_pg(running, imp_str, imp_pg2);
   5012 	else
   5013 		r = entity_get_pg(ent, issvc, imp_str, imp_pg2);
   5014 	if (r != 0) {
   5015 		switch (scf_error()) {
   5016 		case SCF_ERROR_CONNECTION_BROKEN:
   5017 			r = scferror2errno(scf_error());
   5018 			goto out;
   5019 
   5020 		case SCF_ERROR_DELETED:
   5021 			if (running != NULL)
   5022 				r = ENODEV;
   5023 			else
   5024 				r = ECANCELED;
   5025 			goto out;
   5026 
   5027 		case SCF_ERROR_NOT_FOUND:
   5028 			break;
   5029 
   5030 		case SCF_ERROR_INVALID_ARGUMENT:
   5031 		case SCF_ERROR_HANDLE_MISMATCH:
   5032 		case SCF_ERROR_NOT_BOUND:
   5033 		case SCF_ERROR_NOT_SET:
   5034 		default:
   5035 			bad_error("entity_get_pg", scf_error());
   5036 		}
   5037 
   5038 		warn(cf_pg_missing, ient->sc_fmri, imp_str);
   5039 
   5040 		r = 0;
   5041 		goto out;
   5042 	}
   5043 
   5044 	r = load_pg_attrs(imp_pg2, &curpg_i);
   5045 	switch (r) {
   5046 	case 0:
   5047 		break;
   5048 
   5049 	case ECANCELED:
   5050 		warn(cf_pg_missing, ient->sc_fmri, imp_str);
   5051 		r = 0;
   5052 		goto out;
   5053 
   5054 	case ECONNABORTED:
   5055 	case ENOMEM:
   5056 		goto out;
   5057 
   5058 	default:
   5059 		bad_error("load_pg_attrs", r);
   5060 	}
   5061 
   5062 	if (!pg_attrs_equal(lipg_i, curpg_i, NULL, 0)) {
   5063 		(void) pg_attrs_equal(curpg_i, mpg, ient->sc_fmri, 0);
   5064 		internal_pgroup_free(curpg_i);
   5065 		r = 0;
   5066 		goto out;
   5067 	}
   5068 
   5069 	internal_pgroup_free(curpg_i);
   5070 
   5071 	r = load_pg(imp_pg2, &curpg_i, ient->sc_fmri, NULL);
   5072 	switch (r) {
   5073 	case 0:
   5074 		break;
   5075 
   5076 	case ECANCELED:
   5077 		warn(cf_pg_missing, ient->sc_fmri, imp_str);
   5078 		r = 0;
   5079 		goto out;
   5080 
   5081 	case ECONNABORTED:
   5082 	case EBADF:
   5083 	case ENOMEM:
   5084 	case EACCES:
   5085 		goto out;
   5086 
   5087 	default:
   5088 		bad_error("load_pg", r);
   5089 	}
   5090 
   5091 	if (pg_equal(lipg_i, curpg_i) &&
   5092 	    !pg_attrs_equal(lipg_i, mpg, NULL, 0)) {
   5093 		int do_delete = 1;
   5094 
   5095 		if (g_verbose)
   5096 			warn(gettext("%s: Upgrading property group \"%s\".\n"),
   5097 			    ient->sc_fmri, mpg->sc_pgroup_name);
   5098 
   5099 		internal_pgroup_free(curpg_i);
   5100 
   5101 		if (running != NULL &&
   5102 		    entity_get_pg(ent, issvc, imp_str, imp_pg2) != 0) {
   5103 			switch (scf_error()) {
   5104 			case SCF_ERROR_DELETED:
   5105 				r = ECANCELED;
   5106 				goto out;
   5107 
   5108 			case SCF_ERROR_NOT_FOUND:
   5109 				do_delete = 0;
   5110 				break;
   5111 
   5112 			case SCF_ERROR_CONNECTION_BROKEN:
   5113 				r = scferror2errno(scf_error());
   5114 				goto out;
   5115 
   5116 			case SCF_ERROR_HANDLE_MISMATCH:
   5117 			case SCF_ERROR_INVALID_ARGUMENT:
   5118 			case SCF_ERROR_NOT_SET:
   5119 			case SCF_ERROR_NOT_BOUND:
   5120 			default:
   5121 				bad_error("entity_get_pg", scf_error());
   5122 			}
   5123 		}
   5124 
   5125 		if (do_delete && scf_pg_delete(imp_pg2) != 0) {
   5126 			switch (scf_error()) {
   5127 			case SCF_ERROR_DELETED:
   5128 				break;
   5129 
   5130 			case SCF_ERROR_CONNECTION_BROKEN:
   5131 			case SCF_ERROR_BACKEND_READONLY:
   5132 			case SCF_ERROR_BACKEND_ACCESS:
   5133 				r = scferror2errno(scf_error());
   5134 				goto out;
   5135 
   5136 			case SCF_ERROR_PERMISSION_DENIED:
   5137 				warn(emsg_pg_del_perm, mpg->sc_pgroup_name,
   5138 				    ient->sc_fmri);
   5139 				r = scferror2errno(scf_error());
   5140 				goto out;
   5141 
   5142 			case SCF_ERROR_NOT_SET:
   5143 			case SCF_ERROR_NOT_BOUND:
   5144 			default:
   5145 				bad_error("scf_pg_delete", scf_error());
   5146 			}
   5147 		}
   5148 
   5149 		cbdata.sc_handle = g_hndl;
   5150 		cbdata.sc_parent = ent;
   5151 		cbdata.sc_service = issvc;
   5152 		cbdata.sc_flags = 0;
   5153 		cbdata.sc_source_fmri = ient->sc_fmri;
   5154 		cbdata.sc_target_fmri = ient->sc_fmri;
   5155 
   5156 		r = entity_pgroup_import(mpg, &cbdata);
   5157 		switch (r) {
   5158 		case UU_WALK_NEXT:
   5159 			r = 0;
   5160 			goto out;
   5161 
   5162 		case UU_WALK_ERROR:
   5163 			if (cbdata.sc_err == EEXIST) {
   5164 				warn(emsg_pg_added, ient->sc_fmri,
   5165 				    mpg->sc_pgroup_name);
   5166 				r = EBUSY;
   5167 			} else {
   5168 				r = cbdata.sc_err;
   5169 			}
   5170 			goto out;
   5171 
   5172 		default:
   5173 			bad_error("entity_pgroup_import", r);
   5174 		}
   5175 	}
   5176 
   5177 	if (running != NULL &&
   5178 	    entity_get_pg(ent, issvc, imp_str, imp_pg2) != 0) {
   5179 		switch (scf_error()) {
   5180 		case SCF_ERROR_CONNECTION_BROKEN:
   5181 		case SCF_ERROR_DELETED:
   5182 			r = scferror2errno(scf_error());
   5183 			goto out;
   5184 
   5185 		case SCF_ERROR_NOT_FOUND:
   5186 			break;
   5187 
   5188 		case SCF_ERROR_HANDLE_MISMATCH:
   5189 		case SCF_ERROR_INVALID_ARGUMENT:
   5190 		case SCF_ERROR_NOT_SET:
   5191 		case SCF_ERROR_NOT_BOUND:
   5192 		default:
   5193 			bad_error("entity_get_pg", scf_error());
   5194 		}
   5195 
   5196 		cbdata.sc_handle = g_hndl;
   5197 		cbdata.sc_parent = ent;
   5198 		cbdata.sc_service = issvc;
   5199 		cbdata.sc_flags = SCI_FORCE;
   5200 		cbdata.sc_source_fmri = ient->sc_fmri;
   5201 		cbdata.sc_target_fmri = ient->sc_fmri;
   5202 
   5203 		r = entity_pgroup_import(mpg, &cbdata);
   5204 		switch (r) {
   5205 		case UU_WALK_NEXT:
   5206 			r = 0;
   5207 			goto out;
   5208 
   5209 		case UU_WALK_ERROR:
   5210 			if (cbdata.sc_err == EEXIST) {
   5211 				warn(emsg_pg_added, ient->sc_fmri,
   5212 				    mpg->sc_pgroup_name);
   5213 				r = EBUSY;
   5214 			} else {
   5215 				r = cbdata.sc_err;
   5216 			}
   5217 			goto out;
   5218 
   5219 		default:
   5220 			bad_error("entity_pgroup_import", r);
   5221 		}
   5222 	}
   5223 
   5224 	r = upgrade_pg(imp_pg2, curpg_i, lipg_i, mpg, g_verbose, ient->sc_fmri);
   5225 	internal_pgroup_free(curpg_i);
   5226 	switch (r) {
   5227 	case 0:
   5228 		ient->sc_import_state = IMPORT_PROP_BEGUN;
   5229 		break;
   5230 
   5231 	case ECANCELED:
   5232 		warn(emsg_pg_deleted, ient->sc_fmri, mpg->sc_pgroup_name);
   5233 		r = EBUSY;
   5234 		break;
   5235 
   5236 	case EPERM:
   5237 		warn(emsg_pg_mod_perm, mpg->sc_pgroup_name, ient->sc_fmri);
   5238 		break;
   5239 
   5240 	case EBUSY:
   5241 		warn(emsg_pg_changed, ient->sc_fmri, mpg->sc_pgroup_name);
   5242 		break;
   5243 
   5244 	case ECONNABORTED:
   5245 	case ENOMEM:
   5246 	case ENOSPC:
   5247 	case EROFS:
   5248 	case EACCES:
   5249 	case EINVAL:
   5250 		break;
   5251 
   5252 	default:
   5253 		bad_error("upgrade_pg", r);
   5254 	}
   5255 
   5256 out:
   5257 	internal_pgroup_free(lipg_i);
   5258 	return (r);
   5259 }
   5260 
   5261 /*
   5262  * Upgrade the properties of ent according to snpl & ient.
   5263  *
   5264  * Returns
   5265  *   0 - success
   5266  *   ECONNABORTED - repository connection broken
   5267  *   ENOMEM - out of memory
   5268  *   ENOSPC - configd is out of resources
   5269  *   ECANCELED - ent was deleted
   5270  *   ENODEV - entity containing snpl was deleted
   5271  *	    - entity containing running was deleted
   5272  *   EBADF - imp_snpl is corrupt (error printed)
   5273  *	   - ent has corrupt pg (error printed)
   5274  *	   - dependent has corrupt pg (error printed)
   5275  *	   - dependent target has a corrupt snapshot (error printed)
   5276  *   EBUSY - pg was added, changed, or deleted (error printed)
   5277  *	   - dependent target was deleted (error printed)
   5278  *	   - dependent pg changed (error printed)
   5279  *   EINVAL - invalid property group name (error printed)
   5280  *	    - invalid property name (error printed)
   5281  *	    - invalid value (error printed)
   5282  *	    - ient has invalid pgroup or dependent (error printed)
   5283  *   EPERM - could not create property group (permission denied) (error printed)
   5284  *	   - could not modify property group (permission denied) (error printed)
   5285  *	   - couldn't delete, upgrade, or import pg or dependent (error printed)
   5286  *   EROFS - could not create property group (repository read-only)
   5287  *	   - couldn't delete, upgrade, or import pg or dependent
   5288  *   EACCES - could not create property group (backend access denied)
   5289  *	    - couldn't delete, upgrade, or import pg or dependent
   5290  *   EEXIST - dependent collision in target service (error printed)
   5291  */
   5292 static int
   5293 upgrade_props(void *ent, scf_snaplevel_t *running, scf_snaplevel_t *snpl,
   5294     entity_t *ient)
   5295 {
   5296 	pgroup_t *pg, *rpg;
   5297 	int r;
   5298 	uu_list_t *pgs = ient->sc_pgroups;
   5299 
   5300 	const int issvc = (ient->sc_etype == SVCCFG_SERVICE_OBJECT);
   5301 
   5302 	/* clear sc_sceen for pgs */
   5303 	if (uu_list_walk(pgs, clear_int,
   5304 	    (void *)offsetof(pgroup_t, sc_pgroup_seen), UU_DEFAULT) != 0)
   5305 		bad_error("uu_list_walk", uu_error());
   5306 
   5307 	if (scf_iter_snaplevel_pgs(imp_up_iter, snpl) != 0) {
   5308 		switch (scf_error()) {
   5309 		case SCF_ERROR_DELETED:
   5310 			return (ENODEV);
   5311 
   5312 		case SCF_ERROR_CONNECTION_BROKEN:
   5313 			return (ECONNABORTED);
   5314 
   5315 		case SCF_ERROR_NOT_SET:
   5316 		case SCF_ERROR_NOT_BOUND:
   5317 		case SCF_ERROR_HANDLE_MISMATCH:
   5318 		default:
   5319 			bad_error("scf_iter_snaplevel_pgs", scf_error());
   5320 		}
   5321 	}
   5322 
   5323 	for (;;) {
   5324 		r = scf_iter_next_pg(imp_up_iter, imp_pg);
   5325 		if (r == 0)
   5326 			break;
   5327 		if (r == 1) {
   5328 			r = process_old_pg(imp_pg, ient, ent, running);
   5329 			switch (r) {
   5330 			case 0:
   5331 				break;
   5332 
   5333 			case ECONNABORTED:
   5334 			case ENOMEM:
   5335 			case ENOSPC:
   5336 			case ECANCELED:
   5337 			case ENODEV:
   5338 			case EPERM:
   5339 			case EROFS:
   5340 			case EACCES:
   5341 			case EBADF:
   5342 			case EBUSY:
   5343 			case EINVAL:
   5344 			case EEXIST:
   5345 				return (r);
   5346 
   5347 			default:
   5348 				bad_error("process_old_pg", r);
   5349 			}
   5350 			continue;
   5351 		}
   5352 		if (r != -1)
   5353 			bad_error("scf_iter_next_pg", r);
   5354 
   5355 		switch (scf_error()) {
   5356 		case SCF_ERROR_DELETED:
   5357 			return (ENODEV);
   5358 
   5359 		case SCF_ERROR_CONNECTION_BROKEN:
   5360 			return (ECONNABORTED);
   5361 
   5362 		case SCF_ERROR_HANDLE_MISMATCH:
   5363 		case SCF_ERROR_NOT_BOUND:
   5364 		case SCF_ERROR_NOT_SET:
   5365 		case SCF_ERROR_INVALID_ARGUMENT:
   5366 		default:
   5367 			bad_error("scf_iter_next_pg", scf_error());
   5368 		}
   5369 	}
   5370 
   5371 	for (pg = uu_list_first(pgs); pg != NULL; pg = uu_list_next(pgs, pg)) {
   5372 		if (pg->sc_pgroup_seen)
   5373 			continue;
   5374 
   5375 		/* pg is new */
   5376 
   5377 		if (strcmp(pg->sc_pgroup_name, "dependents") == 0) {
   5378 			r = upgrade_dependents(NULL, imp_snpl, ient, running,
   5379 			    ent);
   5380 			switch (r) {
   5381 			case 0:
   5382 				break;
   5383 
   5384 			case ECONNABORTED:
   5385 			case ENOMEM:
   5386 			case ENOSPC:
   5387 			case ECANCELED:
   5388 			case ENODEV:
   5389 			case EBADF:
   5390 			case EBUSY:
   5391 			case EINVAL:
   5392 			case EPERM:
   5393 			case EROFS:
   5394 			case EACCES:
   5395 			case EEXIST:
   5396 				return (r);
   5397 
   5398 			default:
   5399 				bad_error("upgrade_dependents", r);
   5400 			}
   5401 			continue;
   5402 		}
   5403 
   5404 		if (strcmp(pg->sc_pgroup_name, SCF_PG_MANIFESTFILES) == 0) {
   5405 			r = upgrade_manifestfiles(pg, ient, running, ent);
   5406 			switch (r) {
   5407 			case 0:
   5408 				break;
   5409 
   5410 			case ECONNABORTED:
   5411 			case ENOMEM:
   5412 			case ENOSPC:
   5413 			case ECANCELED:
   5414 			case ENODEV:
   5415 			case EBADF:
   5416 			case EBUSY:
   5417 			case EINVAL:
   5418 			case EPERM:
   5419 			case EROFS:
   5420 			case EACCES:
   5421 			case EEXIST:
   5422 				return (r);
   5423 
   5424 			default:
   5425 				bad_error("upgrade_manifestfiles", r);
   5426 			}
   5427 			continue;
   5428 		}
   5429 
   5430 		if (running != NULL) {
   5431 			r = scf_snaplevel_get_pg(running, pg->sc_pgroup_name,
   5432 			    imp_pg);
   5433 		} else {
   5434 			r = entity_get_pg(ent, issvc, pg->sc_pgroup_name,
   5435 			    imp_pg);
   5436 		}
   5437 		if (r != 0) {
   5438 			scf_callback_t cbdata;
   5439 
   5440 			switch (scf_error()) {
   5441 			case SCF_ERROR_NOT_FOUND:
   5442 				break;
   5443 
   5444 			case SCF_ERROR_CONNECTION_BROKEN:
   5445 				return (scferror2errno(scf_error()));
   5446 
   5447 			case SCF_ERROR_DELETED:
   5448 				if (running != NULL)
   5449 					return (ENODEV);
   5450 				else
   5451 					return (scferror2errno(scf_error()));
   5452 
   5453 			case SCF_ERROR_INVALID_ARGUMENT:
   5454 				warn(emsg_fmri_invalid_pg_name, ient->sc_fmri,
   5455 				    pg->sc_pgroup_name);
   5456 				return (EINVAL);
   5457 
   5458 			case SCF_ERROR_NOT_SET:
   5459 			case SCF_ERROR_HANDLE_MISMATCH:
   5460 			case SCF_ERROR_NOT_BOUND:
   5461 			default:
   5462 				bad_error("entity_get_pg", scf_error());
   5463 			}
   5464 
   5465 			/* User doesn't have pg, so import it. */
   5466 
   5467 			cbdata.sc_handle = g_hndl;
   5468 			cbdata.sc_parent = ent;
   5469 			cbdata.sc_service = issvc;
   5470 			cbdata.sc_flags = SCI_FORCE;
   5471 			cbdata.sc_source_fmri = ient->sc_fmri;
   5472 			cbdata.sc_target_fmri = ient->sc_fmri;
   5473 
   5474 			r = entity_pgroup_import(pg, &cbdata);
   5475 			switch (r) {
   5476 			case UU_WALK_NEXT:
   5477 				ient->sc_import_state = IMPORT_PROP_BEGUN;
   5478 				continue;
   5479 
   5480 			case UU_WALK_ERROR:
   5481 				if (cbdata.sc_err == EEXIST) {
   5482 					warn(emsg_pg_added, ient->sc_fmri,
   5483 					    pg->sc_pgroup_name);
   5484 					return (EBUSY);
   5485 				}
   5486 				return (cbdata.sc_err);
   5487 
   5488 			default:
   5489 				bad_error("entity_pgroup_import", r);
   5490 			}
   5491 		}
   5492 
   5493 		/* report differences between pg & current */
   5494 		r = load_pg(imp_pg, &rpg, ient->sc_fmri, NULL);
   5495 		switch (r) {
   5496 		case 0:
   5497 			break;
   5498 
   5499 		case ECANCELED:
   5500 			warn(emsg_pg_deleted, ient->sc_fmri,
   5501 			    pg->sc_pgroup_name);
   5502 			return (EBUSY);
   5503 
   5504 		case ECONNABORTED:
   5505 		case EBADF:
   5506 		case ENOMEM:
   5507 		case EACCES:
   5508 			return (r);
   5509 
   5510 		default:
   5511 			bad_error("load_pg", r);
   5512 		}
   5513 		report_pg_diffs(pg, rpg, ient->sc_fmri, 1);
   5514 		internal_pgroup_free(rpg);
   5515 		rpg = NULL;
   5516 	}
   5517 
   5518 	return (0);
   5519 }
   5520 
   5521 /*
   5522  * Import an instance.  If it doesn't exist, create it.  If it has
   5523  * a last-import snapshot, upgrade its properties.  Finish by updating its
   5524  * last-import snapshot.  If it doesn't have a last-import snapshot then it
   5525  * could have been created for a dependent tag in another manifest.  Import the
   5526  * new properties.  If there's a conflict, don't override, like now?
   5527  *
   5528  * On success, returns UU_WALK_NEXT.  On error returns UU_WALK_ERROR and sets
   5529  * lcbdata->sc_err to
   5530  *   ECONNABORTED - repository connection broken
   5531  *   ENOMEM - out of memory
   5532  *   ENOSPC - svc.configd is out of resources
   5533  *   EEXIST - dependency collision in dependent service (error printed)
   5534  *   EPERM - couldn't create temporary instance (permission denied)
   5535  *	   - couldn't import into temporary instance (permission denied)
   5536  *	   - couldn't take snapshot (permission denied)
   5537  *	   - couldn't upgrade properties (permission denied)
   5538  *	   - couldn't import properties (permission denied)
   5539  *	   - couldn't import dependents (permission denied)
   5540  *   EROFS - couldn't create temporary instance (repository read-only)
   5541  *	   - couldn't import into temporary instance (repository read-only)
   5542  *	   - couldn't upgrade properties (repository read-only)
   5543  *	   - couldn't import properties (repository read-only)
   5544  *	   - couldn't import dependents (repository read-only)
   5545  *   EACCES - couldn't create temporary instance (backend access denied)
   5546  *	    - couldn't import into temporary instance (backend access denied)
   5547  *	    - couldn't upgrade properties (backend access denied)
   5548  *	    - couldn't import properties (backend access denied)
   5549  *	    - couldn't import dependents (backend access denied)
   5550  *   EINVAL - invalid instance name (error printed)
   5551  *	    - invalid pgroup_t's (error printed)
   5552  *	    - invalid dependents (error printed)
   5553  *   EBUSY - temporary service deleted (error printed)
   5554  *	   - temporary instance deleted (error printed)
   5555  *	   - temporary instance changed (error printed)
   5556  *	   - temporary instance already exists (error printed)
   5557  *	   - instance deleted (error printed)
   5558  *   EBADF - instance has corrupt last-import snapshot (error printed)
   5559  *	   - instance is corrupt (error printed)
   5560  *	   - dependent has corrupt pg (error printed)
   5561  *	   - dependent target has a corrupt snapshot (error printed)
   5562  *   -1 - unknown libscf error (error printed)
   5563  */
   5564 static int
   5565 lscf_instance_import(void *v, void *pvt)
   5566 {
   5567 	entity_t *inst = v;
   5568 	scf_callback_t ctx;
   5569 	scf_callback_t *lcbdata = pvt;
   5570 	scf_service_t *rsvc = lcbdata->sc_parent;
   5571 	int r;
   5572 	scf_snaplevel_t *running;
   5573 	int flags = lcbdata->sc_flags;
   5574 
   5575 	const char * const emsg_tdel =
   5576 	    gettext("Temporary instance svc:/%s:%s was deleted.\n");
   5577 	const char * const emsg_tchg = gettext("Temporary instance svc:/%s:%s "
   5578 	    "changed unexpectedly.\n");
   5579 	const char * const emsg_del = gettext("%s changed unexpectedly "
   5580 	    "(instance \"%s\" was deleted.)\n");
   5581 	const char * const emsg_badsnap = gettext(
   5582 	    "\"%s\" snapshot of %s is corrupt (missing a snaplevel).\n");
   5583 
   5584 	/*
   5585 	 * prepare last-import snapshot:
   5586 	 * create temporary instance (service was precreated)
   5587 	 * populate with properties from bundle
   5588 	 * take snapshot
   5589 	 */
   5590 	if (scf_service_add_instance(imp_tsvc, inst->sc_name, imp_tinst) != 0) {
   5591 		switch (scf_error()) {
   5592 		case SCF_ERROR_CONNECTION_BROKEN:
   5593 		case SCF_ERROR_NO_RESOURCES:
   5594 		case SCF_ERROR_BACKEND_READONLY:
   5595 		case SCF_ERROR_BACKEND_ACCESS:
   5596 			return (stash_scferror(lcbdata));
   5597 
   5598 		case SCF_ERROR_EXISTS:
   5599 			warn(gettext("Temporary service svc:/%s "
   5600 			    "changed unexpectedly (instance \"%s\" added).\n"),
   5601 			    imp_tsname, inst->sc_name);
   5602 			lcbdata->sc_err = EBUSY;
   5603 			return (UU_WALK_ERROR);
   5604 
   5605 		case SCF_ERROR_DELETED:
   5606 			warn(gettext("Temporary service svc:/%s "
   5607 			    "was deleted unexpectedly.\n"), imp_tsname);
   5608 			lcbdata->sc_err = EBUSY;
   5609 			return (UU_WALK_ERROR);
   5610 
   5611 		case SCF_ERROR_INVALID_ARGUMENT:
   5612 			warn(gettext("Invalid instance name \"%s\".\n"),
   5613 			    inst->sc_name);
   5614 			return (stash_scferror(lcbdata));
   5615 
   5616 		case SCF_ERROR_PERMISSION_DENIED:
   5617 			warn(gettext("Could not create temporary instance "
   5618 			    "\"%s\" in svc:/%s (permission denied).\n"),
   5619 			    inst->sc_name, imp_tsname);
   5620 			return (stash_scferror(lcbdata));
   5621 
   5622 		case SCF_ERROR_HANDLE_MISMATCH:
   5623 		case SCF_ERROR_NOT_BOUND:
   5624 		case SCF_ERROR_NOT_SET:
   5625 		default:
   5626 			bad_error("scf_service_add_instance", scf_error());
   5627 		}
   5628 	}
   5629 
   5630 	r = snprintf(imp_str, imp_str_sz, "svc:/%s:%s", imp_tsname,
   5631 	    inst->sc_name);
   5632 	if (r < 0)
   5633 		bad_error("snprintf", errno);
   5634 
   5635 	r = lscf_import_instance_pgs(imp_tinst, imp_str, inst,
   5636 	    lcbdata->sc_flags | SCI_NOENABLED);
   5637 	switch (r) {
   5638 	case 0:
   5639 		break;
   5640 
   5641 	case ECANCELED:
   5642 		warn(emsg_tdel, imp_tsname, inst->sc_name);
   5643 		lcbdata->sc_err = EBUSY;
   5644 		r = UU_WALK_ERROR;
   5645 		goto deltemp;
   5646 
   5647 	case EEXIST:
   5648 		warn(emsg_tchg, imp_tsname, inst->sc_name);
   5649 		lcbdata->sc_err = EBUSY;
   5650 		r = UU_WALK_ERROR;
   5651 		goto deltemp;
   5652 
   5653 	case ECONNABORTED:
   5654 		goto connaborted;
   5655 
   5656 	case ENOMEM:
   5657 	case ENOSPC:
   5658 	case EPERM:
   5659 	case EROFS:
   5660 	case EACCES:
   5661 	case EINVAL:
   5662 	case EBUSY:
   5663 		lcbdata->sc_err = r;
   5664 		r = UU_WALK_ERROR;
   5665 		goto deltemp;
   5666 
   5667 	default:
   5668 		bad_error("lscf_import_instance_pgs", r);
   5669 	}
   5670 
   5671 	r = snprintf(imp_str, imp_str_sz, "svc:/%s:%s", imp_tsname,
   5672 	    inst->sc_name);
   5673 	if (r < 0)
   5674 		bad_error("snprintf", errno);
   5675 
   5676 	ctx.sc_handle = lcbdata->sc_handle;
   5677 	ctx.sc_parent = imp_tinst;
   5678 	ctx.sc_service = 0;
   5679 	ctx.sc_source_fmri = inst->sc_fmri;
   5680 	ctx.sc_target_fmri = imp_str;
   5681 	if (uu_list_walk(inst->sc_dependents, entity_pgroup_import, &ctx,
   5682 	    UU_DEFAULT) != 0) {
   5683 		if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   5684 			bad_error("uu_list_walk", uu_error());
   5685 
   5686 		switch (ctx.sc_err) {
   5687 		case ECONNABORTED:
   5688 			goto connaborted;
   5689 
   5690 		case ECANCELED:
   5691 			warn(emsg_tdel, imp_tsname, inst->sc_name);
   5692 			lcbdata->sc_err = EBUSY;
   5693 			break;
   5694 
   5695 		case EEXIST:
   5696 			warn(emsg_tchg, imp_tsname, inst->sc_name);
   5697 			lcbdata->sc_err = EBUSY;
   5698 			break;
   5699 
   5700 		default:
   5701 			lcbdata->sc_err = ctx.sc_err;
   5702 		}
   5703 		r = UU_WALK_ERROR;
   5704 		goto deltemp;
   5705 	}
   5706 
   5707 	if (_scf_snapshot_take_new_named(imp_tinst, inst->sc_parent->sc_name,
   5708 	    inst->sc_name, snap_lastimport, imp_tlisnap) != 0) {
   5709 		switch (scf_error()) {
   5710 		case SCF_ERROR_CONNECTION_BROKEN:
   5711 			goto connaborted;
   5712 
   5713 		case SCF_ERROR_NO_RESOURCES:
   5714 			r = stash_scferror(lcbdata);
   5715 			goto deltemp;
   5716 
   5717 		case SCF_ERROR_EXISTS:
   5718 			warn(emsg_tchg, imp_tsname, inst->sc_name);
   5719 			lcbdata->sc_err = EBUSY;
   5720 			r = UU_WALK_ERROR;
   5721 			goto deltemp;
   5722 
   5723 		case SCF_ERROR_PERMISSION_DENIED:
   5724 			warn(gettext("Could not take \"%s\" snapshot of %s "
   5725 			    "(permission denied).\n"), snap_lastimport,
   5726 			    imp_str);
   5727 			r = stash_scferror(lcbdata);
   5728 			goto deltemp;
   5729 
   5730 		default:
   5731 			scfwarn();
   5732 			lcbdata->sc_err = -1;
   5733 			r = UU_WALK_ERROR;
   5734 			goto deltemp;
   5735 
   5736 		case SCF_ERROR_HANDLE_MISMATCH:
   5737 		case SCF_ERROR_INVALID_ARGUMENT:
   5738 		case SCF_ERROR_NOT_SET:
   5739 			bad_error("_scf_snapshot_take_new_named", scf_error());
   5740 		}
   5741 	}
   5742 
   5743 	if (lcbdata->sc_flags & SCI_FRESH)
   5744 		goto fresh;
   5745 
   5746 	if (scf_service_get_instance(rsvc, inst->sc_name, imp_inst) == 0) {
   5747 		if (scf_instance_get_snapshot(imp_inst, snap_lastimport,
   5748 		    imp_lisnap) != 0) {
   5749 			switch (scf_error()) {
   5750 			case SCF_ERROR_DELETED:
   5751 				warn(emsg_del, inst->sc_parent->sc_fmri,
   5752 				    inst->sc_name);
   5753 				lcbdata->sc_err = EBUSY;
   5754 				r = UU_WALK_ERROR;
   5755 				goto deltemp;
   5756 
   5757 			case SCF_ERROR_NOT_FOUND:
   5758 				flags |= SCI_FORCE;
   5759 				goto nosnap;
   5760 
   5761 			case SCF_ERROR_CONNECTION_BROKEN:
   5762 				goto connaborted;
   5763 
   5764 			case SCF_ERROR_INVALID_ARGUMENT:
   5765 			case SCF_ERROR_HANDLE_MISMATCH:
   5766 			case SCF_ERROR_NOT_BOUND:
   5767 			case SCF_ERROR_NOT_SET:
   5768 			default:
   5769 				bad_error("scf_instance_get_snapshot",
   5770 				    scf_error());
   5771 			}
   5772 		}
   5773 
   5774 		/* upgrade */
   5775 
   5776 		/*
   5777 		 * compare new properties with last-import properties
   5778 		 * upgrade current properties
   5779 		 */
   5780 		/* clear sc_sceen for pgs */
   5781 		if (uu_list_walk(inst->sc_pgroups, clear_int,
   5782 		    (void *)offsetof(pgroup_t, sc_pgroup_seen), UU_DEFAULT) !=
   5783 		    0)
   5784 			bad_error("uu_list_walk", uu_error());
   5785 
   5786 		r = get_snaplevel(imp_lisnap, 0, imp_snpl);
   5787 		switch (r) {
   5788 		case 0:
   5789 			break;
   5790 
   5791 		case ECONNABORTED:
   5792 			goto connaborted;
   5793 
   5794 		case ECANCELED:
   5795 			warn(emsg_del, inst->sc_parent->sc_fmri, inst->sc_name);
   5796 			lcbdata->sc_err = EBUSY;
   5797 			r = UU_WALK_ERROR;
   5798 			goto deltemp;
   5799 
   5800 		case ENOENT:
   5801 			warn(emsg_badsnap, snap_lastimport, inst->sc_fmri);
   5802 			lcbdata->sc_err = EBADF;
   5803 			r = UU_WALK_ERROR;
   5804 			goto deltemp;
   5805 
   5806 		default:
   5807 			bad_error("get_snaplevel", r);
   5808 		}
   5809 
   5810 		if (scf_instance_get_snapshot(imp_inst, snap_running,
   5811 		    imp_rsnap) != 0) {
   5812 			switch (scf_error()) {
   5813 			case SCF_ERROR_DELETED:
   5814 				warn(emsg_del, inst->sc_parent->sc_fmri,
   5815 				    inst->sc_name);
   5816 				lcbdata->sc_err = EBUSY;
   5817 				r = UU_WALK_ERROR;
   5818 				goto deltemp;
   5819 
   5820 			case SCF_ERROR_NOT_FOUND:
   5821 				break;
   5822 
   5823 			case SCF_ERROR_CONNECTION_BROKEN:
   5824 				goto connaborted;
   5825 
   5826 			case SCF_ERROR_INVALID_ARGUMENT:
   5827 			case SCF_ERROR_HANDLE_MISMATCH:
   5828 			case SCF_ERROR_NOT_BOUND:
   5829 			case SCF_ERROR_NOT_SET:
   5830 			default:
   5831 				bad_error("scf_instance_get_snapshot",
   5832 				    scf_error());
   5833 			}
   5834 
   5835 			running = NULL;
   5836 		} else {
   5837 			r = get_snaplevel(imp_rsnap, 0, imp_rsnpl);
   5838 			switch (r) {
   5839 			case 0:
   5840 				running = imp_rsnpl;
   5841 				break;
   5842 
   5843 			case ECONNABORTED:
   5844 				goto connaborted;
   5845 
   5846 			case ECANCELED:
   5847 				warn(emsg_del, inst->sc_parent->sc_fmri,
   5848 				    inst->sc_name);
   5849 				lcbdata->sc_err = EBUSY;
   5850 				r = UU_WALK_ERROR;
   5851 				goto deltemp;
   5852 
   5853 			case ENOENT:
   5854 				warn(emsg_badsnap, snap_running, inst->sc_fmri);
   5855 				lcbdata->sc_err = EBADF;
   5856 				r = UU_WALK_ERROR;
   5857 				goto deltemp;
   5858 
   5859 			default:
   5860 				bad_error("get_snaplevel", r);
   5861 			}
   5862 		}
   5863 
   5864 		r = upgrade_props(imp_inst, running, imp_snpl, inst);
   5865 		switch (r) {
   5866 		case 0:
   5867 			break;
   5868 
   5869 		case ECANCELED:
   5870 		case ENODEV:
   5871 			warn(emsg_del, inst->sc_parent->sc_fmri, inst->sc_name);
   5872 			lcbdata->sc_err = EBUSY;
   5873 			r = UU_WALK_ERROR;
   5874 			goto deltemp;
   5875 
   5876 		case ECONNABORTED:
   5877 			goto connaborted;
   5878 
   5879 		case ENOMEM:
   5880 		case ENOSPC:
   5881 		case EBADF:
   5882 		case EBUSY:
   5883 		case EINVAL:
   5884 		case EPERM:
   5885 		case EROFS:
   5886 		case EACCES:
   5887 		case EEXIST:
   5888 			lcbdata->sc_err = r;
   5889 			r = UU_WALK_ERROR;
   5890 			goto deltemp;
   5891 
   5892 		default:
   5893 			bad_error("upgrade_props", r);
   5894 		}
   5895 
   5896 		inst->sc_import_state = IMPORT_PROP_DONE;
   5897 	} else {
   5898 		switch (scf_error()) {
   5899 		case SCF_ERROR_CONNECTION_BROKEN:
   5900 			goto connaborted;
   5901 
   5902 		case SCF_ERROR_NOT_FOUND:
   5903 			break;
   5904 
   5905 		case SCF_ERROR_INVALID_ARGUMENT:	/* caught above */
   5906 		case SCF_ERROR_HANDLE_MISMATCH:
   5907 		case SCF_ERROR_NOT_BOUND:
   5908 		case SCF_ERROR_NOT_SET:
   5909 		default:
   5910 			bad_error("scf_service_get_instance", scf_error());
   5911 		}
   5912 
   5913 fresh:
   5914 		/* create instance */
   5915 		if (scf_service_add_instance(rsvc, inst->sc_name,
   5916 		    imp_inst) != 0) {
   5917 			switch (scf_error()) {
   5918 			case SCF_ERROR_CONNECTION_BROKEN:
   5919 				goto connaborted;
   5920 
   5921 			case SCF_ERROR_NO_RESOURCES:
   5922 			case SCF_ERROR_BACKEND_READONLY:
   5923 			case SCF_ERROR_BACKEND_ACCESS:
   5924 				r = stash_scferror(lcbdata);
   5925 				goto deltemp;
   5926 
   5927 			case SCF_ERROR_EXISTS:
   5928 				warn(gettext("%s changed unexpectedly "
   5929 				    "(instance \"%s\" added).\n"),
   5930 				    inst->sc_parent->sc_fmri, inst->sc_name);
   5931 				lcbdata->sc_err = EBUSY;
   5932 				r = UU_WALK_ERROR;
   5933 				goto deltemp;
   5934 
   5935 			case SCF_ERROR_PERMISSION_DENIED:
   5936 				warn(gettext("Could not create \"%s\" instance "
   5937 				    "in %s (permission denied).\n"),
   5938 				    inst->sc_name, inst->sc_parent->sc_fmri);
   5939 				r = stash_scferror(lcbdata);
   5940 				goto deltemp;
   5941 
   5942 			case SCF_ERROR_INVALID_ARGUMENT:  /* caught above */
   5943 			case SCF_ERROR_HANDLE_MISMATCH:
   5944 			case SCF_ERROR_NOT_BOUND:
   5945 			case SCF_ERROR_NOT_SET:
   5946 			default:
   5947 				bad_error("scf_service_add_instance",
   5948 				    scf_error());
   5949 			}
   5950 		}
   5951 
   5952 nosnap:
   5953 		/*
   5954 		 * Create a last-import snapshot to serve as an attachment
   5955 		 * point for the real one from the temporary instance.  Since
   5956 		 * the contents is irrelevant, take it now, while the instance
   5957 		 * is empty, to minimize svc.configd's work.
   5958 		 */
   5959 		if (_scf_snapshot_take_new(imp_inst, snap_lastimport,
   5960 		    imp_lisnap) != 0) {
   5961 			switch (scf_error()) {
   5962 			case SCF_ERROR_CONNECTION_BROKEN:
   5963 				goto connaborted;
   5964 
   5965 			case SCF_ERROR_NO_RESOURCES:
   5966 				r = stash_scferror(lcbdata);
   5967 				goto deltemp;
   5968 
   5969 			case SCF_ERROR_EXISTS:
   5970 				warn(gettext("%s changed unexpectedly "
   5971 				    "(snapshot \"%s\" added).\n"),
   5972 				    inst->sc_fmri, snap_lastimport);
   5973 				lcbdata->sc_err = EBUSY;
   5974 				r = UU_WALK_ERROR;
   5975 				goto deltemp;
   5976 
   5977 			case SCF_ERROR_PERMISSION_DENIED:
   5978 				warn(gettext("Could not take \"%s\" snapshot "
   5979 				    "of %s (permission denied).\n"),
   5980 				    snap_lastimport, inst->sc_fmri);
   5981 				r = stash_scferror(lcbdata);
   5982 				goto deltemp;
   5983 
   5984 			default:
   5985 				scfwarn();
   5986 				lcbdata->sc_err = -1;
   5987 				r = UU_WALK_ERROR;
   5988 				goto deltemp;
   5989 
   5990 			case SCF_ERROR_NOT_SET:
   5991 			case SCF_ERROR_INTERNAL:
   5992 			case SCF_ERROR_INVALID_ARGUMENT:
   5993 			case SCF_ERROR_HANDLE_MISMATCH:
   5994 				bad_error("_scf_snapshot_take_new",
   5995 				    scf_error());
   5996 			}
   5997 		}
   5998 
   5999 		if (li_only)
   6000 			goto lionly;
   6001 
   6002 		inst->sc_import_state = IMPORT_PROP_BEGUN;
   6003 
   6004 		r = lscf_import_instance_pgs(imp_inst, inst->sc_fmri, inst,
   6005 		    flags);
   6006 		switch (r) {
   6007 		case 0:
   6008 			break;
   6009 
   6010 		case ECONNABORTED:
   6011 			goto connaborted;
   6012 
   6013 		case ECANCELED:
   6014 			warn(gettext("%s changed unexpectedly "
   6015 			    "(instance \"%s\" deleted).\n"),
   6016 			    inst->sc_parent->sc_fmri, inst->sc_name);
   6017 			lcbdata->sc_err = EBUSY;
   6018 			r = UU_WALK_ERROR;
   6019 			goto deltemp;
   6020 
   6021 		case EEXIST:
   6022 			warn(gettext("%s changed unexpectedly "
   6023 			    "(property group added).\n"), inst->sc_fmri);
   6024 			lcbdata->sc_err = EBUSY;
   6025 			r = UU_WALK_ERROR;
   6026 			goto deltemp;
   6027 
   6028 		default:
   6029 			lcbdata->sc_err = r;
   6030 			r = UU_WALK_ERROR;
   6031 			goto deltemp;
   6032 
   6033 		case EINVAL:	/* caught above */
   6034 			bad_error("lscf_import_instance_pgs", r);
   6035 		}
   6036 
   6037 		ctx.sc_parent = imp_inst;
   6038 		ctx.sc_service = 0;
   6039 		ctx.sc_trans = NULL;
   6040 		if (uu_list_walk(inst->sc_dependents, lscf_dependent_import,
   6041 		    &ctx, UU_DEFAULT) != 0) {
   6042 			if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6043 				bad_error("uu_list_walk", uu_error());
   6044 
   6045 			if (ctx.sc_err == ECONNABORTED)
   6046 				goto connaborted;
   6047 			lcbdata->sc_err = ctx.sc_err;
   6048 			r = UU_WALK_ERROR;
   6049 			goto deltemp;
   6050 		}
   6051 
   6052 		inst->sc_import_state = IMPORT_PROP_DONE;
   6053 
   6054 		if (g_verbose)
   6055 			warn(gettext("Taking \"%s\" snapshot for %s.\n"),
   6056 			    snap_initial, inst->sc_fmri);
   6057 		r = take_snap(imp_inst, snap_initial, imp_snap);
   6058 		switch (r) {
   6059 		case 0:
   6060 			break;
   6061 
   6062 		case ECONNABORTED:
   6063 			goto connaborted;
   6064 
   6065 		case ENOSPC:
   6066 		case -1:
   6067 			lcbdata->sc_err = r;
   6068 			r = UU_WALK_ERROR;
   6069 			goto deltemp;
   6070 
   6071 		case ECANCELED:
   6072 			warn(gettext("%s changed unexpectedly "
   6073 			    "(instance %s deleted).\n"),
   6074 			    inst->sc_parent->sc_fmri, inst->sc_name);
   6075 			lcbdata->sc_err = r;
   6076 			r = UU_WALK_ERROR;
   6077 			goto deltemp;
   6078 
   6079 		case EPERM:
   6080 			warn(emsg_snap_perm, snap_initial, inst->sc_fmri);
   6081 			lcbdata->sc_err = r;
   6082 			r = UU_WALK_ERROR;
   6083 			goto deltemp;
   6084 
   6085 		default:
   6086 			bad_error("take_snap", r);
   6087 		}
   6088 	}
   6089 
   6090 lionly:
   6091 	if (lcbdata->sc_flags & SCI_NOSNAP)
   6092 		goto deltemp;
   6093 
   6094 	/* transfer snapshot from temporary instance */
   6095 	if (g_verbose)
   6096 		warn(gettext("Taking \"%s\" snapshot for %s.\n"),
   6097 		    snap_lastimport, inst->sc_fmri);
   6098 	if (_scf_snapshot_attach(imp_tlisnap, imp_lisnap) != 0) {
   6099 		switch (scf_error()) {
   6100 		case SCF_ERROR_CONNECTION_BROKEN:
   6101 			goto connaborted;
   6102 
   6103 		case SCF_ERROR_NO_RESOURCES:
   6104 			r = stash_scferror(lcbdata);
   6105 			goto deltemp;
   6106 
   6107 		case SCF_ERROR_PERMISSION_DENIED:
   6108 			warn(gettext("Could not take \"%s\" snapshot for %s "
   6109 			    "(permission denied).\n"), snap_lastimport,
   6110 			    inst->sc_fmri);
   6111 			r = stash_scferror(lcbdata);
   6112 			goto deltemp;
   6113 
   6114 		case SCF_ERROR_NOT_SET:
   6115 		case SCF_ERROR_HANDLE_MISMATCH:
   6116 		default:
   6117 			bad_error("_scf_snapshot_attach", scf_error());
   6118 		}
   6119 	}
   6120 
   6121 	inst->sc_import_state = IMPORT_COMPLETE;
   6122 
   6123 	r = UU_WALK_NEXT;
   6124 
   6125 deltemp:
   6126 	/* delete temporary instance */
   6127 	if (scf_instance_delete(imp_tinst) != 0) {
   6128 		switch (scf_error()) {
   6129 		case SCF_ERROR_DELETED:
   6130 			break;
   6131 
   6132 		case SCF_ERROR_CONNECTION_BROKEN:
   6133 			goto connaborted;
   6134 
   6135 		case SCF_ERROR_NOT_SET:
   6136 		case SCF_ERROR_NOT_BOUND:
   6137 		default:
   6138 			bad_error("scf_instance_delete", scf_error());
   6139 		}
   6140 	}
   6141 
   6142 	return (r);
   6143 
   6144 connaborted:
   6145 	warn(gettext("Could not delete svc:/%s:%s "
   6146 	    "(repository connection broken).\n"), imp_tsname, inst->sc_name);
   6147 	lcbdata->sc_err = ECONNABORTED;
   6148 	return (UU_WALK_ERROR);
   6149 }
   6150 
   6151 /*
   6152  * If the service is missing, create it, import its properties, and import the
   6153  * instances.  Since the service is brand new, it should be empty, and if we
   6154  * run into any existing entities (SCF_ERROR_EXISTS), abort.
   6155  *
   6156  * If the service exists, we want to upgrade its properties and import the
   6157  * instances.  Upgrade requires a last-import snapshot, though, which are
   6158  * children of instances, so first we'll have to go through the instances
   6159  * looking for a last-import snapshot.  If we don't find one then we'll just
   6160  * override-import the service properties (but don't delete existing
   6161  * properties: another service might have declared us as a dependent).  Before
   6162  * we change anything, though, we want to take the previous snapshots.  We
   6163  * also give lscf_instance_import() a leg up on taking last-import snapshots
   6164  * by importing the manifest's service properties into a temporary service.
   6165  *
   6166  * On success, returns UU_WALK_NEXT.  On failure, returns UU_WALK_ERROR and
   6167  * sets lcbdata->sc_err to
   6168  *   ECONNABORTED - repository connection broken
   6169  *   ENOMEM - out of memory
   6170  *   ENOSPC - svc.configd is out of resources
   6171  *   EPERM - couldn't create temporary service (error printed)
   6172  *	   - couldn't import into temp service (error printed)
   6173  *	   - couldn't create service (error printed)
   6174  *	   - couldn't import dependent (error printed)
   6175  *	   - couldn't take snapshot (error printed)
   6176  *	   - couldn't create instance (error printed)
   6177  *	   - couldn't create, modify, or delete pg (error printed)
   6178  *	   - couldn't create, modify, or delete dependent (error printed)
   6179  *	   - couldn't import instance (error printed)
   6180  *   EROFS - couldn't create temporary service (repository read-only)
   6181  *	   - couldn't import into temporary service (repository read-only)
   6182  *	   - couldn't create service (repository read-only)
   6183  *	   - couldn't import dependent (repository read-only)
   6184  *	   - couldn't create instance (repository read-only)
   6185  *	   - couldn't create, modify, or delete pg or dependent
   6186  *	   - couldn't import instance (repository read-only)
   6187  *   EACCES - couldn't create temporary service (backend access denied)
   6188  *	    - couldn't import into temporary service (backend access denied)
   6189  *	    - couldn't create service (backend access denied)
   6190  *	    - couldn't import dependent (backend access denied)
   6191  *	    - couldn't create instance (backend access denied)
   6192  *	    - couldn't create, modify, or delete pg or dependent
   6193  *	    - couldn't import instance (backend access denied)
   6194  *   EINVAL - service name is invalid (error printed)
   6195  *	    - service name is too long (error printed)
   6196  *	    - s has invalid pgroup (error printed)
   6197  *	    - s has invalid dependent (error printed)
   6198  *	    - instance name is invalid (error printed)
   6199  *	    - instance entity_t is invalid (error printed)
   6200  *   EEXIST - couldn't create temporary service (already exists) (error printed)
   6201  *	    - couldn't import dependent (dependency pg already exists) (printed)
   6202  *	    - dependency collision in dependent service (error printed)
   6203  *   EBUSY - temporary service deleted (error printed)
   6204  *	   - property group added to temporary service (error printed)
   6205  *	   - new property group changed or was deleted (error printed)
   6206  *	   - service was added unexpectedly (error printed)
   6207  *	   - service was deleted unexpectedly (error printed)
   6208  *	   - property group added to new service (error printed)
   6209  *	   - instance added unexpectedly (error printed)
   6210  *	   - instance deleted unexpectedly (error printed)
   6211  *	   - dependent service deleted unexpectedly (error printed)
   6212  *	   - pg was added, changed, or deleted (error printed)
   6213  *	   - dependent pg changed (error printed)
   6214  *	   - temporary instance added, changed, or deleted (error printed)
   6215  *   EBADF - a last-import snapshot is corrupt (error printed)
   6216  *	   - the service is corrupt (error printed)
   6217  *	   - a dependent is corrupt (error printed)
   6218  *	   - an instance is corrupt (error printed)
   6219  *	   - an instance has a corrupt last-import snapshot (error printed)
   6220  *	   - dependent target has a corrupt snapshot (error printed)
   6221  *   -1 - unknown libscf error (error printed)
   6222  */
   6223 static int
   6224 lscf_service_import(void *v, void *pvt)
   6225 {
   6226 	entity_t *s = v;
   6227 	scf_callback_t cbdata;
   6228 	scf_callback_t *lcbdata = pvt;
   6229 	scf_scope_t *scope = lcbdata->sc_parent;
   6230 	entity_t *inst, linst;
   6231 	int r;
   6232 	int fresh = 0;
   6233 	scf_snaplevel_t *running;
   6234 	int have_ge;
   6235 
   6236 	const char * const ts_deleted = gettext("Temporary service svc:/%s "
   6237 	    "was deleted unexpectedly.\n");
   6238 	const char * const ts_pg_added = gettext("Temporary service svc:/%s "
   6239 	    "changed unexpectedly (property group added).\n");
   6240 	const char * const s_deleted =
   6241 	    gettext("%s was deleted unexpectedly.\n");
   6242 	const char * const i_deleted =
   6243 	    gettext("%s changed unexpectedly (instance \"%s\" deleted).\n");
   6244 	const char * const badsnap = gettext("\"%s\" snapshot of svc:/%s:%s "
   6245 	    "is corrupt (missing service snaplevel).\n");
   6246 
   6247 	/* Validate the service name */
   6248 	if (scf_scope_get_service(scope, s->sc_name, imp_svc) != 0) {
   6249 		switch (scf_error()) {
   6250 		case SCF_ERROR_CONNECTION_BROKEN:
   6251 			return (stash_scferror(lcbdata));
   6252 
   6253 		case SCF_ERROR_INVALID_ARGUMENT:
   6254 			warn(gettext("\"%s\" is an invalid service name.  "
   6255 			    "Cannot import.\n"), s->sc_name);
   6256 			return (stash_scferror(lcbdata));
   6257 
   6258 		case SCF_ERROR_NOT_FOUND:
   6259 			break;
   6260 
   6261 		case SCF_ERROR_HANDLE_MISMATCH:
   6262 		case SCF_ERROR_NOT_BOUND:
   6263 		case SCF_ERROR_NOT_SET:
   6264 		default:
   6265 			bad_error("scf_scope_get_service", scf_error());
   6266 		}
   6267 	}
   6268 
   6269 	/* create temporary service */
   6270 	/*
   6271 	 * the size of the buffer was reduced to max_scf_name_len to prevent
   6272 	 * hitting bug 6681151.  After the bug fix, the size of the buffer
   6273 	 * should be restored to its original value (max_scf_name_len +1)
   6274 	 */
   6275 	r = snprintf(imp_tsname, max_scf_name_len, "TEMP/%s", s->sc_name);
   6276 	if (r < 0)
   6277 		bad_error("snprintf", errno);
   6278 	if (r > max_scf_name_len) {
   6279 		warn(gettext(
   6280 		    "Service name \"%s\" is too long.  Cannot import.\n"),
   6281 		    s->sc_name);
   6282 		lcbdata->sc_err = EINVAL;
   6283 		return (UU_WALK_ERROR);
   6284 	}
   6285 
   6286 	if (scf_scope_add_service(imp_scope, imp_tsname, imp_tsvc) != 0) {
   6287 		switch (scf_error()) {
   6288 		case SCF_ERROR_CONNECTION_BROKEN:
   6289 		case SCF_ERROR_NO_RESOURCES:
   6290 		case SCF_ERROR_BACKEND_READONLY:
   6291 		case SCF_ERROR_BACKEND_ACCESS:
   6292 			return (stash_scferror(lcbdata));
   6293 
   6294 		case SCF_ERROR_EXISTS:
   6295 			warn(gettext(
   6296 			    "Temporary service \"%s\" must be deleted before "
   6297 			    "this manifest can be imported.\n"), imp_tsname);
   6298 			return (stash_scferror(lcbdata));
   6299 
   6300 		case SCF_ERROR_PERMISSION_DENIED:
   6301 			warn(gettext("Could not create temporary service "
   6302 			    "\"%s\" (permission denied).\n"), imp_tsname);
   6303 			return (stash_scferror(lcbdata));
   6304 
   6305 		case SCF_ERROR_INVALID_ARGUMENT:
   6306 		case SCF_ERROR_HANDLE_MISMATCH:
   6307 		case SCF_ERROR_NOT_BOUND:
   6308 		case SCF_ERROR_NOT_SET:
   6309 		default:
   6310 			bad_error("scf_scope_add_service", scf_error());
   6311 		}
   6312 	}
   6313 
   6314 	r = snprintf(imp_str, imp_str_sz, "svc:/%s", imp_tsname);
   6315 	if (r < 0)
   6316 		bad_error("snprintf", errno);
   6317 
   6318 	cbdata.sc_handle = lcbdata->sc_handle;
   6319 	cbdata.sc_parent = imp_tsvc;
   6320 	cbdata.sc_service = 1;
   6321 	cbdata.sc_source_fmri = s->sc_fmri;
   6322 	cbdata.sc_target_fmri = imp_str;
   6323 	cbdata.sc_flags = 0;
   6324 
   6325 	if (uu_list_walk(s->sc_pgroups, entity_pgroup_import, &cbdata,
   6326 	    UU_DEFAULT) != 0) {
   6327 		if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6328 			bad_error("uu_list_walk", uu_error());
   6329 
   6330 		lcbdata->sc_err = cbdata.sc_err;
   6331 		switch (cbdata.sc_err) {
   6332 		case ECONNABORTED:
   6333 			goto connaborted;
   6334 
   6335 		case ECANCELED:
   6336 			warn(ts_deleted, imp_tsname);
   6337 			lcbdata->sc_err = EBUSY;
   6338 			return (UU_WALK_ERROR);
   6339 
   6340 		case EEXIST:
   6341 			warn(ts_pg_added, imp_tsname);
   6342 			lcbdata->sc_err = EBUSY;
   6343 			return (UU_WALK_ERROR);
   6344 		}
   6345 
   6346 		r = UU_WALK_ERROR;
   6347 		goto deltemp;
   6348 	}
   6349 
   6350 	if (uu_list_walk(s->sc_dependents, entity_pgroup_import, &cbdata,
   6351 	    UU_DEFAULT) != 0) {
   6352 		if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6353 			bad_error("uu_list_walk", uu_error());
   6354 
   6355 		lcbdata->sc_err = cbdata.sc_err;
   6356 		switch (cbdata.sc_err) {
   6357 		case ECONNABORTED:
   6358 			goto connaborted;
   6359 
   6360 		case ECANCELED:
   6361 			warn(ts_deleted, imp_tsname);
   6362 			lcbdata->sc_err = EBUSY;
   6363 			return (UU_WALK_ERROR);
   6364 
   6365 		case EEXIST:
   6366 			warn(ts_pg_added, imp_tsname);
   6367 			lcbdata->sc_err = EBUSY;
   6368 			return (UU_WALK_ERROR);
   6369 		}
   6370 
   6371 		r = UU_WALK_ERROR;
   6372 		goto deltemp;
   6373 	}
   6374 
   6375 	if (scf_scope_get_service(scope, s->sc_name, imp_svc) != 0) {
   6376 		switch (scf_error()) {
   6377 		case SCF_ERROR_NOT_FOUND:
   6378 			break;
   6379 
   6380 		case SCF_ERROR_CONNECTION_BROKEN:
   6381 			goto connaborted;
   6382 
   6383 		case SCF_ERROR_INVALID_ARGUMENT:
   6384 		case SCF_ERROR_HANDLE_MISMATCH:
   6385 		case SCF_ERROR_NOT_BOUND:
   6386 		case SCF_ERROR_NOT_SET:
   6387 		default:
   6388 			bad_error("scf_scope_get_service", scf_error());
   6389 		}
   6390 
   6391 		if (scf_scope_add_service(scope, s->sc_name, imp_svc) != 0) {
   6392 			switch (scf_error()) {
   6393 			case SCF_ERROR_CONNECTION_BROKEN:
   6394 				goto connaborted;
   6395 
   6396 			case SCF_ERROR_NO_RESOURCES:
   6397 			case SCF_ERROR_BACKEND_READONLY:
   6398 			case SCF_ERROR_BACKEND_ACCESS:
   6399 				r = stash_scferror(lcbdata);
   6400 				goto deltemp;
   6401 
   6402 			case SCF_ERROR_EXISTS:
   6403 				warn(gettext("Scope \"%s\" changed unexpectedly"
   6404 				    " (service \"%s\" added).\n"),
   6405 				    SCF_SCOPE_LOCAL, s->sc_name);
   6406 				lcbdata->sc_err = EBUSY;
   6407 				goto deltemp;
   6408 
   6409 			case SCF_ERROR_PERMISSION_DENIED:
   6410 				warn(gettext("Could not create service \"%s\" "
   6411 				    "(permission denied).\n"), s->sc_name);
   6412 				goto deltemp;
   6413 
   6414 			case SCF_ERROR_INVALID_ARGUMENT:
   6415 			case SCF_ERROR_HANDLE_MISMATCH:
   6416 			case SCF_ERROR_NOT_BOUND:
   6417 			case SCF_ERROR_NOT_SET:
   6418 			default:
   6419 				bad_error("scf_scope_add_service", scf_error());
   6420 			}
   6421 		}
   6422 
   6423 		s->sc_import_state = IMPORT_PROP_BEGUN;
   6424 
   6425 		/* import service properties */
   6426 		cbdata.sc_handle = lcbdata->sc_handle;
   6427 		cbdata.sc_parent = imp_svc;
   6428 		cbdata.sc_service = 1;
   6429 		cbdata.sc_flags = lcbdata->sc_flags;
   6430 		cbdata.sc_source_fmri = s->sc_fmri;
   6431 		cbdata.sc_target_fmri = s->sc_fmri;
   6432 
   6433 		if (uu_list_walk(s->sc_pgroups, entity_pgroup_import,
   6434 		    &cbdata, UU_DEFAULT) != 0) {
   6435 			if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6436 				bad_error("uu_list_walk", uu_error());
   6437 
   6438 			lcbdata->sc_err = cbdata.sc_err;
   6439 			switch (cbdata.sc_err) {
   6440 			case ECONNABORTED:
   6441 				goto connaborted;
   6442 
   6443 			case ECANCELED:
   6444 				warn(s_deleted, s->sc_fmri);
   6445 				lcbdata->sc_err = EBUSY;
   6446 				return (UU_WALK_ERROR);
   6447 
   6448 			case EEXIST:
   6449 				warn(gettext("%s changed unexpectedly "
   6450 				    "(property group added).\n"), s->sc_fmri);
   6451 				lcbdata->sc_err = EBUSY;
   6452 				return (UU_WALK_ERROR);
   6453 
   6454 			case EINVAL:
   6455 				/* caught above */
   6456 				bad_error("entity_pgroup_import",
   6457 				    cbdata.sc_err);
   6458 			}
   6459 
   6460 			r = UU_WALK_ERROR;
   6461 			goto deltemp;
   6462 		}
   6463 
   6464 		cbdata.sc_trans = NULL;
   6465 		if (uu_list_walk(s->sc_dependents, lscf_dependent_import,
   6466 		    &cbdata, UU_DEFAULT) != 0) {
   6467 			if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6468 				bad_error("uu_list_walk", uu_error());
   6469 
   6470 			lcbdata->sc_err = cbdata.sc_err;
   6471 			if (cbdata.sc_err == ECONNABORTED)
   6472 				goto connaborted;
   6473 			r = UU_WALK_ERROR;
   6474 			goto deltemp;
   6475 		}
   6476 
   6477 		s->sc_import_state = IMPORT_PROP_DONE;
   6478 
   6479 		/*
   6480 		 * This is a new service, so we can't take previous snapshots
   6481 		 * or upgrade service properties.
   6482 		 */
   6483 		fresh = 1;
   6484 		goto instances;
   6485 	}
   6486 
   6487 	/* Clear sc_seen for the instances. */
   6488 	if (uu_list_walk(s->sc_u.sc_service.sc_service_instances, clear_int,
   6489 	    (void *)offsetof(entity_t, sc_seen), UU_DEFAULT) != 0)
   6490 		bad_error("uu_list_walk", uu_error());
   6491 
   6492 	/*
   6493 	 * Take previous snapshots for all instances.  Even for ones not
   6494 	 * mentioned in the bundle, since we might change their service
   6495 	 * properties.
   6496 	 */
   6497 	if (scf_iter_service_instances(imp_iter, imp_svc) != 0) {
   6498 		switch (scf_error()) {
   6499 		case SCF_ERROR_CONNECTION_BROKEN:
   6500 			goto connaborted;
   6501 
   6502 		case SCF_ERROR_DELETED:
   6503 			warn(s_deleted, s->sc_fmri);
   6504 			lcbdata->sc_err = EBUSY;
   6505 			r = UU_WALK_ERROR;
   6506 			goto deltemp;
   6507 
   6508 		case SCF_ERROR_HANDLE_MISMATCH:
   6509 		case SCF_ERROR_NOT_BOUND:
   6510 		case SCF_ERROR_NOT_SET:
   6511 		default:
   6512 			bad_error("scf_iter_service_instances", scf_error());
   6513 		}
   6514 	}
   6515 
   6516 	for (;;) {
   6517 		r = scf_iter_next_instance(imp_iter, imp_inst);
   6518 		if (r == 0)
   6519 			break;
   6520 		if (r != 1) {
   6521 			switch (scf_error()) {
   6522 			case SCF_ERROR_DELETED:
   6523 				warn(s_deleted, s->sc_fmri);
   6524 				lcbdata->sc_err = EBUSY;
   6525 				r = UU_WALK_ERROR;
   6526 				goto deltemp;
   6527 
   6528 			case SCF_ERROR_CONNECTION_BROKEN:
   6529 				goto connaborted;
   6530 
   6531 			case SCF_ERROR_NOT_BOUND:
   6532 			case SCF_ERROR_HANDLE_MISMATCH:
   6533 			case SCF_ERROR_INVALID_ARGUMENT:
   6534 			case SCF_ERROR_NOT_SET:
   6535 			default:
   6536 				bad_error("scf_iter_next_instance",
   6537 				    scf_error());
   6538 			}
   6539 		}
   6540 
   6541 		if (scf_instance_get_name(imp_inst, imp_str, imp_str_sz) < 0) {
   6542 			switch (scf_error()) {
   6543 			case SCF_ERROR_DELETED:
   6544 				continue;
   6545 
   6546 			case SCF_ERROR_CONNECTION_BROKEN:
   6547 				goto connaborted;
   6548 
   6549 			case SCF_ERROR_NOT_SET:
   6550 			case SCF_ERROR_NOT_BOUND:
   6551 			default:
   6552 				bad_error("scf_instance_get_name", scf_error());
   6553 			}
   6554 		}
   6555 
   6556 		if (g_verbose)
   6557 			warn(gettext(
   6558 			    "Taking \"%s\" snapshot for svc:/%s:%s.\n"),
   6559 			    snap_previous, s->sc_name, imp_str);
   6560 
   6561 		r = take_snap(imp_inst, snap_previous, imp_snap);
   6562 		switch (r) {
   6563 		case 0:
   6564 			break;
   6565 
   6566 		case ECANCELED:
   6567 			continue;
   6568 
   6569 		case ECONNABORTED:
   6570 			goto connaborted;
   6571 
   6572 		case EPERM:
   6573 			warn(gettext("Could not take \"%s\" snapshot of "
   6574 			    "svc:/%s:%s (permission denied).\n"),
   6575 			    snap_previous, s->sc_name, imp_str);
   6576 			lcbdata->sc_err = r;
   6577 			return (UU_WALK_ERROR);
   6578 
   6579 		case ENOSPC:
   6580 		case -1:
   6581 			lcbdata->sc_err = r;
   6582 			r = UU_WALK_ERROR;
   6583 			goto deltemp;
   6584 
   6585 		default:
   6586 			bad_error("take_snap", r);
   6587 		}
   6588 
   6589 		linst.sc_name = imp_str;
   6590 		inst = uu_list_find(s->sc_u.sc_service.sc_service_instances,
   6591 		    &linst, NULL, NULL);
   6592 		if (inst != NULL) {
   6593 			inst->sc_import_state = IMPORT_PREVIOUS;
   6594 			inst->sc_seen = 1;
   6595 		}
   6596 	}
   6597 
   6598 	/*
   6599 	 * Create the new instances and take previous snapshots of
   6600 	 * them.  This is not necessary, but it maximizes data preservation.
   6601 	 */
   6602 	for (inst = uu_list_first(s->sc_u.sc_service.sc_service_instances);
   6603 	    inst != NULL;
   6604 	    inst = uu_list_next(s->sc_u.sc_service.sc_service_instances,
   6605 	    inst)) {
   6606 		if (inst->sc_seen)
   6607 			continue;
   6608 
   6609 		if (scf_service_add_instance(imp_svc, inst->sc_name,
   6610 		    imp_inst) != 0) {
   6611 			switch (scf_error()) {
   6612 			case SCF_ERROR_CONNECTION_BROKEN:
   6613 				goto connaborted;
   6614 
   6615 			case SCF_ERROR_BACKEND_READONLY:
   6616 			case SCF_ERROR_BACKEND_ACCESS:
   6617 			case SCF_ERROR_NO_RESOURCES:
   6618 				r = stash_scferror(lcbdata);
   6619 				goto deltemp;
   6620 
   6621 			case SCF_ERROR_EXISTS:
   6622 				warn(gettext("%s changed unexpectedly "
   6623 				    "(instance \"%s\" added).\n"), s->sc_fmri,
   6624 				    inst->sc_name);
   6625 				lcbdata->sc_err = EBUSY;
   6626 				r = UU_WALK_ERROR;
   6627 				goto deltemp;
   6628 
   6629 			case SCF_ERROR_INVALID_ARGUMENT:
   6630 				warn(gettext("Service \"%s\" has instance with "
   6631 				    "invalid name \"%s\".\n"), s->sc_name,
   6632 				    inst->sc_name);
   6633 				r = stash_scferror(lcbdata);
   6634 				goto deltemp;
   6635 
   6636 			case SCF_ERROR_PERMISSION_DENIED:
   6637 				warn(gettext("Could not create instance \"%s\" "
   6638 				    "in %s (permission denied).\n"),
   6639 				    inst->sc_name, s->sc_fmri);
   6640 				r = stash_scferror(lcbdata);
   6641 				goto deltemp;
   6642 
   6643 			case SCF_ERROR_HANDLE_MISMATCH:
   6644 			case SCF_ERROR_NOT_BOUND:
   6645 			case SCF_ERROR_NOT_SET:
   6646 			default:
   6647 				bad_error("scf_service_add_instance",
   6648 				    scf_error());
   6649 			}
   6650 		}
   6651 
   6652 		if (g_verbose)
   6653 			warn(gettext("Taking \"%s\" snapshot for "
   6654 			    "new service %s.\n"), snap_previous, inst->sc_fmri);
   6655 		r = take_snap(imp_inst, snap_previous, imp_snap);
   6656 		switch (r) {
   6657 		case 0:
   6658 			break;
   6659 
   6660 		case ECANCELED:
   6661 			warn(i_deleted, s->sc_fmri, inst->sc_name);
   6662 			lcbdata->sc_err = EBUSY;
   6663 			r = UU_WALK_ERROR;
   6664 			goto deltemp;
   6665 
   6666 		case ECONNABORTED:
   6667 			goto connaborted;
   6668 
   6669 		case EPERM:
   6670 			warn(emsg_snap_perm, snap_previous, inst->sc_fmri);
   6671 			lcbdata->sc_err = r;
   6672 			r = UU_WALK_ERROR;
   6673 			goto deltemp;
   6674 
   6675 		case ENOSPC:
   6676 		case -1:
   6677 			r = UU_WALK_ERROR;
   6678 			goto deltemp;
   6679 
   6680 		default:
   6681 			bad_error("take_snap", r);
   6682 		}
   6683 	}
   6684 
   6685 	s->sc_import_state = IMPORT_PREVIOUS;
   6686 
   6687 	/*
   6688 	 * Upgrade service properties, if we can find a last-import snapshot.
   6689 	 * Any will do because we don't support different service properties
   6690 	 * in different manifests, so all snaplevels of the service in all of
   6691 	 * the last-import snapshots of the instances should be the same.
   6692 	 */
   6693 	if (scf_iter_service_instances(imp_iter, imp_svc) != 0) {
   6694 		switch (scf_error()) {
   6695 		case SCF_ERROR_CONNECTION_BROKEN:
   6696 			goto connaborted;
   6697 
   6698 		case SCF_ERROR_DELETED:
   6699 			warn(s_deleted, s->sc_fmri);
   6700 			lcbdata->sc_err = EBUSY;
   6701 			r = UU_WALK_ERROR;
   6702 			goto deltemp;
   6703 
   6704 		case SCF_ERROR_HANDLE_MISMATCH:
   6705 		case SCF_ERROR_NOT_BOUND:
   6706 		case SCF_ERROR_NOT_SET:
   6707 		default:
   6708 			bad_error("scf_iter_service_instances", scf_error());
   6709 		}
   6710 	}
   6711 
   6712 	have_ge = 0;
   6713 	li_only = 0;
   6714 
   6715 	for (;;) {
   6716 		r = scf_iter_next_instance(imp_iter, imp_inst);
   6717 		if (r == -1) {
   6718 			switch (scf_error()) {
   6719 			case SCF_ERROR_DELETED:
   6720 				warn(s_deleted, s->sc_fmri);
   6721 				lcbdata->sc_err = EBUSY;
   6722 				r = UU_WALK_ERROR;
   6723 				goto deltemp;
   6724 
   6725 			case SCF_ERROR_CONNECTION_BROKEN:
   6726 				goto connaborted;
   6727 
   6728 			case SCF_ERROR_NOT_BOUND:
   6729 			case SCF_ERROR_HANDLE_MISMATCH:
   6730 			case SCF_ERROR_INVALID_ARGUMENT:
   6731 			case SCF_ERROR_NOT_SET:
   6732 			default:
   6733 				bad_error("scf_iter_next_instance",
   6734 				    scf_error());
   6735 			}
   6736 		}
   6737 
   6738 		if (r == 0) {
   6739 			/*
   6740 			 * Didn't find any last-import snapshots.  Override-
   6741 			 * import the properties.  Unless one of the instances
   6742 			 * has a general/enabled property, in which case we're
   6743 			 * probably running a last-import-capable svccfg for
   6744 			 * the first time, and we should only take the
   6745 			 * last-import snapshot.
   6746 			 */
   6747 			if (have_ge) {
   6748 				li_only = 1;
   6749 				no_refresh = 1;
   6750 				break;
   6751 			}
   6752 
   6753 			s->sc_import_state = IMPORT_PROP_BEGUN;
   6754 
   6755 			cbdata.sc_handle = g_hndl;
   6756 			cbdata.sc_parent = imp_svc;
   6757 			cbdata.sc_service = 1;
   6758 			cbdata.sc_flags = SCI_FORCE;
   6759 			cbdata.sc_source_fmri = s->sc_fmri;
   6760 			cbdata.sc_target_fmri = s->sc_fmri;
   6761 			if (uu_list_walk(s->sc_pgroups, entity_pgroup_import,
   6762 			    &cbdata, UU_DEFAULT) != 0) {
   6763 				if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6764 					bad_error("uu_list_walk", uu_error());
   6765 				lcbdata->sc_err = cbdata.sc_err;
   6766 				switch (cbdata.sc_err) {
   6767 				case ECONNABORTED:
   6768 					goto connaborted;
   6769 
   6770 				case ECANCELED:
   6771 					warn(s_deleted, s->sc_fmri);
   6772 					lcbdata->sc_err = EBUSY;
   6773 					break;
   6774 
   6775 				case EINVAL:	/* caught above */
   6776 				case EEXIST:
   6777 					bad_error("entity_pgroup_import",
   6778 					    cbdata.sc_err);
   6779 				}
   6780 
   6781 				r = UU_WALK_ERROR;
   6782 				goto deltemp;
   6783 			}
   6784 
   6785 			cbdata.sc_trans = NULL;
   6786 			if (uu_list_walk(s->sc_dependents,
   6787 			    lscf_dependent_import, &cbdata, UU_DEFAULT) != 0) {
   6788 				if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6789 					bad_error("uu_list_walk", uu_error());
   6790 				lcbdata->sc_err = cbdata.sc_err;
   6791 				if (cbdata.sc_err == ECONNABORTED)
   6792 					goto connaborted;
   6793 				r = UU_WALK_ERROR;
   6794 				goto deltemp;
   6795 			}
   6796 			break;
   6797 		}
   6798 
   6799 		if (scf_instance_get_snapshot(imp_inst, snap_lastimport,
   6800 		    imp_snap) != 0) {
   6801 			switch (scf_error()) {
   6802 			case SCF_ERROR_DELETED:
   6803 				continue;
   6804 
   6805 			case SCF_ERROR_NOT_FOUND:
   6806 				break;
   6807 
   6808 			case SCF_ERROR_CONNECTION_BROKEN:
   6809 				goto connaborted;
   6810 
   6811 			case SCF_ERROR_HANDLE_MISMATCH:
   6812 			case SCF_ERROR_NOT_BOUND:
   6813 			case SCF_ERROR_INVALID_ARGUMENT:
   6814 			case SCF_ERROR_NOT_SET:
   6815 			default:
   6816 				bad_error("scf_instance_get_snapshot",
   6817 				    scf_error());
   6818 			}
   6819 
   6820 			if (have_ge)
   6821 				continue;
   6822 
   6823 			/*
   6824 			 * Check for a general/enabled property.  This is how
   6825 			 * we tell whether to import if there turn out to be
   6826 			 * no last-import snapshots.
   6827 			 */
   6828 			if (scf_instance_get_pg(imp_inst, SCF_PG_GENERAL,
   6829 			    imp_pg) == 0) {
   6830 				if (scf_pg_get_property(imp_pg,
   6831 				    SCF_PROPERTY_ENABLED, imp_prop) == 0) {
   6832 					have_ge = 1;
   6833 				} else {
   6834 					switch (scf_error()) {
   6835 					case SCF_ERROR_DELETED:
   6836 					case SCF_ERROR_NOT_FOUND:
   6837 						continue;
   6838 
   6839 					case SCF_ERROR_INVALID_ARGUMENT:
   6840 					case SCF_ERROR_HANDLE_MISMATCH:
   6841 					case SCF_ERROR_CONNECTION_BROKEN:
   6842 					case SCF_ERROR_NOT_BOUND:
   6843 					case SCF_ERROR_NOT_SET:
   6844 					default:
   6845 						bad_error("scf_pg_get_property",
   6846 						    scf_error());
   6847 					}
   6848 				}
   6849 			} else {
   6850 				switch (scf_error()) {
   6851 				case SCF_ERROR_DELETED:
   6852 				case SCF_ERROR_NOT_FOUND:
   6853 					continue;
   6854 
   6855 				case SCF_ERROR_CONNECTION_BROKEN:
   6856 					goto connaborted;
   6857 
   6858 				case SCF_ERROR_NOT_BOUND:
   6859 				case SCF_ERROR_NOT_SET:
   6860 				case SCF_ERROR_INVALID_ARGUMENT:
   6861 				case SCF_ERROR_HANDLE_MISMATCH:
   6862 				default:
   6863 					bad_error("scf_instance_get_pg",
   6864 					    scf_error());
   6865 				}
   6866 			}
   6867 			continue;
   6868 		}
   6869 
   6870 		/* find service snaplevel */
   6871 		r = get_snaplevel(imp_snap, 1, imp_snpl);
   6872 		switch (r) {
   6873 		case 0:
   6874 			break;
   6875 
   6876 		case ECONNABORTED:
   6877 			goto connaborted;
   6878 
   6879 		case ECANCELED:
   6880 			continue;
   6881 
   6882 		case ENOENT:
   6883 			if (scf_instance_get_name(imp_inst, imp_str,
   6884 			    imp_str_sz) < 0)
   6885 				(void) strcpy(imp_str, "?");
   6886 			warn(badsnap, snap_lastimport, s->sc_name, imp_str);
   6887 			lcbdata->sc_err = EBADF;
   6888 			r = UU_WALK_ERROR;
   6889 			goto deltemp;
   6890 
   6891 		default:
   6892 			bad_error("get_snaplevel", r);
   6893 		}
   6894 
   6895 		if (scf_instance_get_snapshot(imp_inst, snap_running,
   6896 		    imp_rsnap) != 0) {
   6897 			switch (scf_error()) {
   6898 			case SCF_ERROR_DELETED:
   6899 				continue;
   6900 
   6901 			case SCF_ERROR_NOT_FOUND:
   6902 				break;
   6903 
   6904 			case SCF_ERROR_CONNECTION_BROKEN:
   6905 				goto connaborted;
   6906 
   6907 			case SCF_ERROR_INVALID_ARGUMENT:
   6908 			case SCF_ERROR_HANDLE_MISMATCH:
   6909 			case SCF_ERROR_NOT_BOUND:
   6910 			case SCF_ERROR_NOT_SET:
   6911 			default:
   6912 				bad_error("scf_instance_get_snapshot",
   6913 				    scf_error());
   6914 			}
   6915 			running = NULL;
   6916 		} else {
   6917 			r = get_snaplevel(imp_rsnap, 1, imp_rsnpl);
   6918 			switch (r) {
   6919 			case 0:
   6920 				running = imp_rsnpl;
   6921 				break;
   6922 
   6923 			case ECONNABORTED:
   6924 				goto connaborted;
   6925 
   6926 			case ECANCELED:
   6927 				continue;
   6928 
   6929 			case ENOENT:
   6930 				if (scf_instance_get_name(imp_inst, imp_str,
   6931 				    imp_str_sz) < 0)
   6932 					(void) strcpy(imp_str, "?");
   6933 				warn(badsnap, snap_running, s->sc_name,
   6934 				    imp_str);
   6935 				lcbdata->sc_err = EBADF;
   6936 				r = UU_WALK_ERROR;
   6937 				goto deltemp;
   6938 
   6939 			default:
   6940 				bad_error("get_snaplevel", r);
   6941 			}
   6942 		}
   6943 
   6944 		if (g_verbose) {
   6945 			if (scf_instance_get_name(imp_inst, imp_str,
   6946 			    imp_str_sz) < 0)
   6947 				(void) strcpy(imp_str, "?");
   6948 			warn(gettext("Upgrading properties of %s according to "
   6949 			    "instance \"%s\".\n"), s->sc_fmri, imp_str);
   6950 		}
   6951 
   6952 		/* upgrade service properties */
   6953 		r = upgrade_props(imp_svc, running, imp_snpl, s);
   6954 		if (r == 0)
   6955 			break;
   6956 
   6957 		switch (r) {
   6958 		case ECONNABORTED:
   6959 			goto connaborted;
   6960 
   6961 		case ECANCELED:
   6962 			warn(s_deleted, s->sc_fmri);
   6963 			lcbdata->sc_err = EBUSY;
   6964 			break;
   6965 
   6966 		case ENODEV:
   6967 			if (scf_instance_get_name(imp_inst, imp_str,
   6968 			    imp_str_sz) < 0)
   6969 				(void) strcpy(imp_str, "?");
   6970 			warn(i_deleted, s->sc_fmri, imp_str);
   6971 			lcbdata->sc_err = EBUSY;
   6972 			break;
   6973 
   6974 		default:
   6975 			lcbdata->sc_err = r;
   6976 		}
   6977 
   6978 		r = UU_WALK_ERROR;
   6979 		goto deltemp;
   6980 	}
   6981 
   6982 	s->sc_import_state = IMPORT_PROP_DONE;
   6983 
   6984 instances:
   6985 	/* import instances */
   6986 	cbdata.sc_handle = lcbdata->sc_handle;
   6987 	cbdata.sc_parent = imp_svc;
   6988 	cbdata.sc_service = 1;
   6989 	cbdata.sc_flags = lcbdata->sc_flags | (fresh ? SCI_FRESH : 0);
   6990 	cbdata.sc_general = NULL;
   6991 
   6992 	if (uu_list_walk(s->sc_u.sc_service.sc_service_instances,
   6993 	    lscf_instance_import, &cbdata, UU_DEFAULT) != 0) {
   6994 		if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   6995 			bad_error("uu_list_walk", uu_error());
   6996 
   6997 		lcbdata->sc_err = cbdata.sc_err;
   6998 		if (cbdata.sc_err == ECONNABORTED)
   6999 			goto connaborted;
   7000 		r = UU_WALK_ERROR;
   7001 		goto deltemp;
   7002 	}
   7003 
   7004 	s->sc_import_state = IMPORT_COMPLETE;
   7005 	r = UU_WALK_NEXT;
   7006 
   7007 deltemp:
   7008 	/* delete temporary service */
   7009 	if (scf_service_delete(imp_tsvc) != 0) {
   7010 		switch (scf_error()) {
   7011 		case SCF_ERROR_DELETED:
   7012 			break;
   7013 
   7014 		case SCF_ERROR_CONNECTION_BROKEN:
   7015 			goto connaborted;
   7016 
   7017 		case SCF_ERROR_EXISTS:
   7018 			warn(gettext(
   7019 			    "Could not delete svc:/%s (instances exist).\n"),
   7020 			    imp_tsname);
   7021 			break;
   7022 
   7023 		case SCF_ERROR_NOT_SET:
   7024 		case SCF_ERROR_NOT_BOUND:
   7025 		default:
   7026 			bad_error("scf_service_delete", scf_error());
   7027 		}
   7028 	}
   7029 
   7030 	return (r);
   7031 
   7032 connaborted:
   7033 	warn(gettext("Could not delete svc:/%s "
   7034 	    "(repository connection broken).\n"), imp_tsname);
   7035 	lcbdata->sc_err = ECONNABORTED;
   7036 	return (UU_WALK_ERROR);
   7037 }
   7038 
   7039 static const char *
   7040 import_progress(int st)
   7041 {
   7042 	switch (st) {
   7043 	case 0:
   7044 		return (gettext("not reached."));
   7045 
   7046 	case IMPORT_PREVIOUS:
   7047 		return (gettext("previous snapshot taken."));
   7048 
   7049 	case IMPORT_PROP_BEGUN:
   7050 		return (gettext("some properties imported."));
   7051 
   7052 	case IMPORT_PROP_DONE:
   7053 		return (gettext("properties imported."));
   7054 
   7055 	case IMPORT_COMPLETE:
   7056 		return (gettext("imported."));
   7057 
   7058 	case IMPORT_REFRESHED:
   7059 		return (gettext("refresh requested."));
   7060 
   7061 	default:
   7062 #ifndef NDEBUG
   7063 		(void) fprintf(stderr, "%s:%d: Unknown entity state %d.\n",
   7064 		    __FILE__, __LINE__, st);
   7065 #endif
   7066 		abort();
   7067 		/* NOTREACHED */
   7068 	}
   7069 }
   7070 
   7071 /*
   7072  * Returns
   7073  *   0 - success
   7074  *     - fmri wasn't found (error printed)
   7075  *     - entity was deleted (error printed)
   7076  *     - backend denied access (error printed)
   7077  *   ENOMEM - out of memory (error printed)
   7078  *   ECONNABORTED - repository connection broken (error printed)
   7079  *   EPERM - permission denied (error printed)
   7080  *   -1 - unknown libscf error (error printed)
   7081  */
   7082 static int
   7083 imp_refresh_fmri(const char *fmri, const char *name, const char *d_fmri)
   7084 {
   7085 	scf_error_t serr;
   7086 	void *ent;
   7087 	int issvc;
   7088 	int r;
   7089 
   7090 	const char *deleted = gettext("Could not refresh %s (deleted).\n");
   7091 	const char *dpt_deleted = gettext("Could not refresh %s "
   7092 	    "(dependent \"%s\" of %s) (deleted).\n");
   7093 
   7094 	serr = fmri_to_entity(g_hndl, fmri, &ent, &issvc);
   7095 	switch (serr) {
   7096 	case SCF_ERROR_NONE:
   7097 		break;
   7098 
   7099 	case SCF_ERROR_NO_MEMORY:
   7100 		if (name == NULL)
   7101 			warn(gettext("Could not refresh %s (out of memory).\n"),
   7102 			    fmri);
   7103 		else
   7104 			warn(gettext("Could not refresh %s "
   7105 			    "(dependent \"%s\" of %s) (out of memory).\n"),
   7106 			    fmri, name, d_fmri);
   7107 		return (ENOMEM);
   7108 
   7109 	case SCF_ERROR_NOT_FOUND:
   7110 		if (name == NULL)
   7111 			warn(deleted, fmri);
   7112 		else
   7113 			warn(dpt_deleted, fmri, name, d_fmri);
   7114 		return (0);
   7115 
   7116 	case SCF_ERROR_INVALID_ARGUMENT:
   7117 	case SCF_ERROR_CONSTRAINT_VIOLATED:
   7118 	default:
   7119 		bad_error("fmri_to_entity", serr);
   7120 	}
   7121 
   7122 	r = refresh_entity(issvc, ent, fmri, imp_inst, imp_iter, imp_str);
   7123 	switch (r) {
   7124 	case 0:
   7125 		break;
   7126 
   7127 	case ECONNABORTED:
   7128 		if (name != NULL)
   7129 			warn(gettext("Could not refresh %s "
   7130 			    "(dependent \"%s\" of %s) "
   7131 			    "(repository connection broken).\n"), fmri, name,
   7132 			    d_fmri);
   7133 		return (r);
   7134 
   7135 	case ECANCELED:
   7136 		if (name == NULL)
   7137 			warn(deleted, fmri);
   7138 		else
   7139 			warn(dpt_deleted, fmri, name, d_fmri);
   7140 		return (0);
   7141 
   7142 	case EACCES:
   7143 		if (!g_verbose)
   7144 			return (0);
   7145 		if (name == NULL)
   7146 			warn(gettext("Could not refresh %s "
   7147 			    "(backend access denied).\n"), fmri);
   7148 		else
   7149 			warn(gettext("Could not refresh %s "
   7150 			    "(dependent \"%s\" of %s) "
   7151 			    "(backend access denied).\n"), fmri, name, d_fmri);
   7152 		return (0);
   7153 
   7154 	case EPERM:
   7155 		if (name == NULL)
   7156 			warn(gettext("Could not refresh %s "
   7157 			    "(permission denied).\n"), fmri);
   7158 		else
   7159 			warn(gettext("Could not refresh %s "
   7160 			    "(dependent \"%s\" of %s) "
   7161 			    "(permission denied).\n"), fmri, name, d_fmri);
   7162 		return (r);
   7163 
   7164 	case ENOSPC:
   7165 		if (name == NULL)
   7166 			warn(gettext("Could not refresh %s "
   7167 			    "(repository server out of resources).\n"),
   7168 			    fmri);
   7169 		else
   7170 			warn(gettext("Could not refresh %s "
   7171 			    "(dependent \"%s\" of %s) "
   7172 			    "(repository server out of resources).\n"),
   7173 			    fmri, name, d_fmri);
   7174 		return (r);
   7175 
   7176 	case -1:
   7177 		scfwarn();
   7178 		return (r);
   7179 
   7180 	default:
   7181 		bad_error("refresh_entity", r);
   7182 	}
   7183 
   7184 	if (issvc)
   7185 		scf_service_destroy(ent);
   7186 	else
   7187 		scf_instance_destroy(ent);
   7188 
   7189 	return (0);
   7190 }
   7191 
   7192 int
   7193 lscf_bundle_import(bundle_t *bndl, const char *filename, uint_t flags)
   7194 {
   7195 	scf_callback_t cbdata;
   7196 	int result = 0;
   7197 	entity_t *svc, *inst;
   7198 	uu_list_t *insts;
   7199 	int r;
   7200 	pgroup_t *old_dpt;
   7201 	void *cookie;
   7202 	int annotation_set = 0;
   7203 
   7204 	const char * const emsg_nomem = gettext("Out of memory.\n");
   7205 	const char * const emsg_nores =
   7206 	    gettext("svc.configd is out of resources.\n");
   7207 
   7208 	lscf_prep_hndl();
   7209 
   7210 	imp_str_sz = ((max_scf_name_len > max_scf_fmri_len) ?
   7211 	    max_scf_name_len : max_scf_fmri_len) + 1;
   7212 
   7213 	if ((imp_scope = scf_scope_create(g_hndl)) == NULL ||
   7214 	    (imp_svc = scf_service_create(g_hndl)) == NULL ||
   7215 	    (imp_tsvc = scf_service_create(g_hndl)) == NULL ||
   7216 	    (imp_inst = scf_instance_create(g_hndl)) == NULL ||
   7217 	    (imp_tinst = scf_instance_create(g_hndl)) == NULL ||
   7218 	    (imp_snap = scf_snapshot_create(g_hndl)) == NULL ||
   7219 	    (imp_lisnap = scf_snapshot_create(g_hndl)) == NULL ||
   7220 	    (imp_tlisnap = scf_snapshot_create(g_hndl)) == NULL ||
   7221 	    (imp_rsnap = scf_snapshot_create(g_hndl)) == NULL ||
   7222 	    (imp_snpl = scf_snaplevel_create(g_hndl)) == NULL ||
   7223 	    (imp_rsnpl = scf_snaplevel_create(g_hndl)) == NULL ||
   7224 	    (imp_pg = scf_pg_create(g_hndl)) == NULL ||
   7225 	    (imp_pg2 = scf_pg_create(g_hndl)) == NULL ||
   7226 	    (imp_prop = scf_property_create(g_hndl)) == NULL ||
   7227 	    (imp_iter = scf_iter_create(g_hndl)) == NULL ||
   7228 	    (imp_rpg_iter = scf_iter_create(g_hndl)) == NULL ||
   7229 	    (imp_up_iter = scf_iter_create(g_hndl)) == NULL ||
   7230 	    (imp_tx = scf_transaction_create(g_hndl)) == NULL ||
   7231 	    (imp_str = malloc(imp_str_sz)) == NULL ||
   7232 	    (imp_tsname = malloc(max_scf_name_len + 1)) == NULL ||
   7233 	    (imp_fe1 = malloc(max_scf_fmri_len + 1)) == NULL ||
   7234 	    (imp_fe2 = malloc(max_scf_fmri_len + 1)) == NULL ||
   7235 	    (imp_deleted_dpts = uu_list_create(string_pool, NULL, 0)) == NULL ||
   7236 	    (ud_inst = scf_instance_create(g_hndl)) == NULL ||
   7237 	    (ud_snpl = scf_snaplevel_create(g_hndl)) == NULL ||
   7238 	    (ud_pg = scf_pg_create(g_hndl)) == NULL ||
   7239 	    (ud_cur_depts_pg = scf_pg_create(g_hndl)) == NULL ||
   7240 	    (ud_run_dpts_pg = scf_pg_create(g_hndl)) == NULL ||
   7241 	    (ud_prop = scf_property_create(g_hndl)) == NULL ||
   7242 	    (ud_dpt_prop = scf_property_create(g_hndl)) == NULL ||
   7243 	    (ud_val = scf_value_create(g_hndl)) == NULL ||
   7244 	    (ud_iter = scf_iter_create(g_hndl)) == NULL ||
   7245 	    (ud_iter2 = scf_iter_create(g_hndl)) == NULL ||
   7246 	    (ud_tx = scf_transaction_create(g_hndl)) == NULL ||
   7247 	    (ud_ctarg = malloc(max_scf_value_len + 1)) == NULL ||
   7248 	    (ud_oldtarg = malloc(max_scf_value_len + 1)) == NULL ||
   7249 	    (ud_name = malloc(max_scf_name_len + 1)) == NULL) {
   7250 		if (scf_error() == SCF_ERROR_NO_RESOURCES)
   7251 			warn(emsg_nores);
   7252 		else
   7253 			warn(emsg_nomem);
   7254 		result = -1;
   7255 		goto out;
   7256 	}
   7257 
   7258 	r = load_init();
   7259 	switch (r) {
   7260 	case 0:
   7261 		break;
   7262 
   7263 	case ENOMEM:
   7264 		warn(emsg_nomem);
   7265 		result = -1;
   7266 		goto out;
   7267 
   7268 	default:
   7269 		bad_error("load_init", r);
   7270 	}
   7271 
   7272 	if (scf_handle_get_scope(g_hndl, SCF_SCOPE_LOCAL, imp_scope) != 0) {
   7273 		switch (scf_error()) {
   7274 		case SCF_ERROR_CONNECTION_BROKEN:
   7275 			warn(gettext("Repository connection broken.\n"));
   7276 			repository_teardown();
   7277 			result = -1;
   7278 			goto out;
   7279 
   7280 		case SCF_ERROR_NOT_FOUND:
   7281 		case SCF_ERROR_INVALID_ARGUMENT:
   7282 		case SCF_ERROR_NOT_BOUND:
   7283 		case SCF_ERROR_HANDLE_MISMATCH:
   7284 		default:
   7285 			bad_error("scf_handle_get_scope", scf_error());
   7286 		}
   7287 	}
   7288 
   7289 	/* Set up the auditing annotation. */
   7290 	if (_scf_set_annotation(g_hndl, "svccfg import", filename) == 0) {
   7291 		annotation_set = 1;
   7292 	} else {
   7293 		switch (scf_error()) {
   7294 		case SCF_ERROR_CONNECTION_BROKEN:
   7295 			warn(gettext("Repository connection broken.\n"));
   7296 			repository_teardown();
   7297 			result = -1;
   7298 			goto out;
   7299 
   7300 		case SCF_ERROR_INVALID_ARGUMENT:
   7301 		case SCF_ERROR_NOT_BOUND:
   7302 		case SCF_ERROR_NO_RESOURCES:
   7303 		case SCF_ERROR_INTERNAL:
   7304 			bad_error("_scf_set_annotation", scf_error());
   7305 			/* NOTREACHED */
   7306 
   7307 		default:
   7308 			/*
   7309 			 * Do not terminate import because of inability to
   7310 			 * generate annotation audit event.
   7311 			 */
   7312 			warn(gettext("_scf_set_annotation() unexpectedly "
   7313 			    "failed with return code of %d\n"), scf_error());
   7314 			break;
   7315 		}
   7316 	}
   7317 
   7318 	/*
   7319 	 * Clear the sc_import_state's of all services & instances so we can
   7320 	 * report how far we got if we fail.
   7321 	 */
   7322 	for (svc = uu_list_first(bndl->sc_bundle_services);
   7323 	    svc != NULL;
   7324 	    svc = uu_list_next(bndl->sc_bundle_services, svc)) {
   7325 		svc->sc_import_state = 0;
   7326 
   7327 		if (uu_list_walk(svc->sc_u.sc_service.sc_service_instances,
   7328 		    clear_int, (void *)offsetof(entity_t, sc_import_state),
   7329 		    UU_DEFAULT) != 0)
   7330 			bad_error("uu_list_walk", uu_error());
   7331 	}
   7332 
   7333 	cbdata.sc_handle = g_hndl;
   7334 	cbdata.sc_parent = imp_scope;
   7335 	cbdata.sc_flags = flags;
   7336 	cbdata.sc_general = NULL;
   7337 
   7338 	if (uu_list_walk(bndl->sc_bundle_services, lscf_service_import,
   7339 	    &cbdata, UU_DEFAULT) == 0) {
   7340 		/* Success.  Refresh everything. */
   7341 
   7342 		if (flags & SCI_NOREFRESH || no_refresh) {
   7343 			result = 0;
   7344 			goto out;
   7345 		}
   7346 
   7347 		for (svc = uu_list_first(bndl->sc_bundle_services);
   7348 		    svc != NULL;
   7349 		    svc = uu_list_next(bndl->sc_bundle_services, svc)) {
   7350 			pgroup_t *dpt;
   7351 
   7352 			insts = svc->sc_u.sc_service.sc_service_instances;
   7353 
   7354 			for (inst = uu_list_first(insts);
   7355 			    inst != NULL;
   7356 			    inst = uu_list_next(insts, inst)) {
   7357 				r = imp_refresh_fmri(inst->sc_fmri, NULL, NULL);
   7358 				switch (r) {
   7359 				case 0:
   7360 					break;
   7361 
   7362 				case ENOMEM:
   7363 				case ECONNABORTED:
   7364 				case EPERM:
   7365 				case -1:
   7366 					goto progress;
   7367 
   7368 				default:
   7369 					bad_error("imp_refresh_fmri", r);
   7370 				}
   7371 
   7372 				inst->sc_import_state = IMPORT_REFRESHED;
   7373 
   7374 				for (dpt = uu_list_first(inst->sc_dependents);
   7375 				    dpt != NULL;
   7376 				    dpt = uu_list_next(inst->sc_dependents,
   7377 				    dpt))
   7378 					if (imp_refresh_fmri(
   7379 					    dpt->sc_pgroup_fmri,
   7380 					    dpt->sc_pgroup_name,
   7381 					    inst->sc_fmri) != 0)
   7382 						goto progress;
   7383 			}
   7384 
   7385 			for (dpt = uu_list_first(svc->sc_dependents);
   7386 			    dpt != NULL;
   7387 			    dpt = uu_list_next(svc->sc_dependents, dpt))
   7388 				if (imp_refresh_fmri(dpt->sc_pgroup_fmri,
   7389 				    dpt->sc_pgroup_name, svc->sc_fmri) != 0)
   7390 					goto progress;
   7391 		}
   7392 
   7393 		for (old_dpt = uu_list_first(imp_deleted_dpts);
   7394 		    old_dpt != NULL;
   7395 		    old_dpt = uu_list_next(imp_deleted_dpts, old_dpt))
   7396 			if (imp_refresh_fmri(old_dpt->sc_pgroup_fmri,
   7397 			    old_dpt->sc_pgroup_name,
   7398 			    old_dpt->sc_parent->sc_fmri) != 0)
   7399 				goto progress;
   7400 
   7401 		result = 0;
   7402 		goto out;
   7403 	}
   7404 
   7405 	if (uu_error() != UU_ERROR_CALLBACK_FAILED)
   7406 		bad_error("uu_list_walk", uu_error());
   7407 
   7408 printerr:
   7409 	/* If the error hasn't been printed yet, do so here. */
   7410 	switch (cbdata.sc_err) {
   7411 	case ECONNABORTED:
   7412 		warn(gettext("Repository connection broken.\n"));
   7413 		break;
   7414 
   7415 	case ENOMEM:
   7416 		warn(emsg_nomem);
   7417 		break;
   7418 
   7419 	case ENOSPC:
   7420 		warn(emsg_nores);
   7421 		break;
   7422 
   7423 	case EROFS:
   7424 		warn(gettext("Repository is read-only.\n"));
   7425 		break;
   7426 
   7427 	case EACCES:
   7428 		warn(gettext("Repository backend denied access.\n"));
   7429 		break;
   7430 
   7431 	case EPERM:
   7432 	case EINVAL:
   7433 	case EEXIST:
   7434 	case EBUSY:
   7435 	case EBADF:
   7436 	case -1:
   7437 		break;
   7438 
   7439 	default:
   7440 		bad_error("lscf_service_import", cbdata.sc_err);
   7441 	}
   7442 
   7443 progress:
   7444 	warn(gettext("Import of %s failed.  Progress:\n"), filename);
   7445 
   7446 	for (svc = uu_list_first(bndl->sc_bundle_services);
   7447 	    svc != NULL;
   7448 	    svc = uu_list_next(bndl->sc_bundle_services, svc)) {
   7449 		insts = svc->sc_u.sc_service.sc_service_instances;
   7450 
   7451 		warn(gettext("  Service \"%s\": %s\n"), svc->sc_name,
   7452 		    import_progress(svc->sc_import_state));
   7453 
   7454 		for (inst = uu_list_first(insts);
   7455 		    inst != NULL;
   7456 		    inst = uu_list_next(insts, inst))
   7457 			warn(gettext("    Instance \"%s\": %s\n"),
   7458 			    inst->sc_name,
   7459 			    import_progress(inst->sc_import_state));
   7460 	}
   7461 
   7462 	if (cbdata.sc_err == ECONNABORTED)
   7463 		repository_teardown();
   7464 
   7465 
   7466 	result = -1;
   7467 
   7468 out:
   7469 	if (annotation_set != 0) {
   7470 		/* Turn off annotation.  It is no longer needed. */
   7471 		(void) _scf_set_annotation(g_hndl, NULL, NULL);
   7472 	}
   7473 	load_fini();
   7474 
   7475 	free(ud_ctarg);
   7476 	free(ud_oldtarg);
   7477 	free(ud_name);
   7478 	ud_ctarg = ud_oldtarg = ud_name = NULL;
   7479 
   7480 	scf_transaction_destroy(ud_tx);
   7481 	ud_tx = NULL;
   7482 	scf_iter_destroy(ud_iter);
   7483 	scf_iter_destroy(ud_iter2);
   7484 	ud_iter = ud_iter2 = NULL;
   7485 	scf_value_destroy(ud_val);
   7486 	ud_val = NULL;
   7487 	scf_property_destroy(ud_prop);
   7488 	scf_property_destroy(ud_dpt_prop);
   7489 	ud_prop = ud_dpt_prop = NULL;
   7490 	scf_pg_destroy(ud_pg);
   7491 	scf_pg_destroy(ud_cur_depts_pg);
   7492 	scf_pg_destroy(ud_run_dpts_pg);
   7493 	ud_pg = ud_cur_depts_pg = ud_run_dpts_pg = NULL;
   7494 	scf_snaplevel_destroy(ud_snpl);
   7495 	ud_snpl = NULL;
   7496 	scf_instance_destroy(ud_inst);
   7497 	ud_inst = NULL;
   7498 
   7499 	free(imp_str);
   7500 	free(imp_tsname);
   7501 	free(imp_fe1);
   7502 	free(imp_fe2);
   7503 	imp_str = imp_tsname = imp_fe1 = imp_fe2 = NULL;
   7504 
   7505 	cookie = NULL;
   7506 	while ((old_dpt = uu_list_teardown(imp_deleted_dpts, &cookie)) !=
   7507 	    NULL) {
   7508 		free((char *)old_dpt->sc_pgroup_name);
   7509 		free((char *)old_dpt->sc_pgroup_fmri);
   7510 		internal_pgroup_free(old_dpt);
   7511 	}
   7512 	uu_list_destroy(imp_deleted_dpts);
   7513 
   7514 	scf_transaction_destroy(imp_tx);
   7515 	imp_tx = NULL;
   7516 	scf_iter_destroy(imp_iter);
   7517 	scf_iter_destroy(imp_rpg_iter);
   7518 	scf_iter_destroy(imp_up_iter);
   7519 	imp_iter = imp_rpg_iter = imp_up_iter = NULL;
   7520 	scf_property_destroy(imp_prop);
   7521 	imp_prop = NULL;
   7522 	scf_pg_destroy(imp_pg);
   7523 	scf_pg_destroy(imp_pg2);
   7524 	imp_pg = imp_pg2 = NULL;
   7525 	scf_snaplevel_destroy(imp_snpl);
   7526 	scf_snaplevel_destroy(imp_rsnpl);
   7527 	imp_snpl = imp_rsnpl = NULL;
   7528 	scf_snapshot_destroy(imp_snap);
   7529 	scf_snapshot_destroy(imp_lisnap);
   7530 	scf_snapshot_destroy(imp_tlisnap);
   7531 	scf_snapshot_destroy(imp_rsnap);
   7532 	imp_snap = imp_lisnap = imp_tlisnap = imp_rsnap = NULL;
   7533 	scf_instance_destroy(imp_inst);
   7534 	scf_instance_destroy(imp_tinst);
   7535 	imp_inst = imp_tinst = NULL;
   7536 	scf_service_destroy(imp_svc);
   7537 	scf_service_destroy(imp_tsvc);
   7538 	imp_svc = imp_tsvc = NULL;
   7539 	scf_scope_destroy(imp_scope);
   7540 	imp_scope = NULL;
   7541 
   7542 	return (result);
   7543 }
   7544 
   7545 /*
   7546  * _lscf_import_err() summarize the error handling returned by
   7547  * lscf_import_{instance | service}_pgs
   7548  * Return values are:
   7549  * IMPORT_NEXT
   7550  * IMPORT_OUT
   7551  * IMPORT_BAD
   7552  */
   7553 
   7554 #define	IMPORT_BAD	-1
   7555 #define	IMPORT_NEXT	0
   7556 #define	IMPORT_OUT	1
   7557 
   7558 static int
   7559 _lscf_import_err(int err, const char *fmri)
   7560 {
   7561 	switch (err) {
   7562 	case 0:
   7563 		if (g_verbose)
   7564 			warn(gettext("%s updated.\n"), fmri);
   7565 		return (IMPORT_NEXT);
   7566 
   7567 	case ECONNABORTED:
   7568 		warn(gettext("Could not update %s "
   7569 		    "(repository connection broken).\n"), fmri);
   7570 		return (IMPORT_OUT);
   7571 
   7572 	case ENOMEM:
   7573 		warn(gettext("Could not update %s (out of memory).\n"), fmri);
   7574 		return (IMPORT_OUT);
   7575 
   7576 	case ENOSPC:
   7577 		warn(gettext("Could not update %s "
   7578 		    "(repository server out of resources).\n"), fmri);
   7579 		return (IMPORT_OUT);
   7580 
   7581 	case ECANCELED:
   7582 		warn(gettext(
   7583 		    "Could not update %s (deleted).\n"), fmri);
   7584 		return (IMPORT_NEXT);
   7585 
   7586 	case EPERM:
   7587 	case EINVAL:
   7588 	case EBUSY:
   7589 		return (IMPORT_NEXT);
   7590 
   7591 	case EROFS:
   7592 		warn(gettext("Could not update %s (repository read-only).\n"),
   7593 		    fmri);
   7594 		return (IMPORT_OUT);
   7595 
   7596 	case EACCES:
   7597 		warn(gettext("Could not update %s "
   7598 		    "(backend access denied).\n"), fmri);
   7599 		return (IMPORT_NEXT);
   7600 
   7601 	case EEXIST:
   7602 	default:
   7603 		return (IMPORT_BAD);
   7604 	}
   7605 
   7606 	/*NOTREACHED*/
   7607 }
   7608 
   7609 /*
   7610  * Returns
   7611  *   0 - success
   7612  *   -1 - lscf_import_instance_pgs() failed.
   7613  */
   7614 int
   7615 lscf_bundle_apply(bundle_t *bndl, const char *file)
   7616 {
   7617 	entity_t *svc, *inst;
   7618 	scf_scope_t *rscope;
   7619 	scf_service_t *rsvc;
   7620 	scf_instance_t *rinst;
   7621 	scf_iter_t *iter;
   7622 	int annotation_set = 0;
   7623 	int r;
   7624 
   7625 	lscf_prep_hndl();
   7626 
   7627 	if ((rscope = scf_scope_create(g_hndl)) == NULL ||
   7628 	    (rsvc = scf_service_create(g_hndl)) == NULL ||
   7629 	    (rinst = scf_instance_create(g_hndl)) == NULL ||
   7630 	    (iter = scf_iter_create(g_hndl)) == NULL ||
   7631 	    (imp_pg = scf_pg_create(g_hndl)) == NULL ||
   7632 	    (imp_tx = scf_transaction_create(g_hndl)) == NULL)
   7633 		scfdie();
   7634 
   7635 	if (scf_handle_get_scope(g_hndl, SCF_SCOPE_LOCAL, rscope) != 0)
   7636 		scfdie();
   7637 
   7638 	/*
   7639 	 * Set the strings to be used for the security audit annotation
   7640 	 * event.
   7641 	 */
   7642 	if (_scf_set_annotation(g_hndl, "svccfg apply", file) == 0) {
   7643 		annotation_set = 1;
   7644 	} else {
   7645 		switch (scf_error()) {
   7646 		case SCF_ERROR_CONNECTION_BROKEN:
   7647 			warn(gettext("Repository connection broken.\n"));
   7648 			goto out;
   7649 
   7650 		case SCF_ERROR_INVALID_ARGUMENT:
   7651 		case SCF_ERROR_NOT_BOUND:
   7652 		case SCF_ERROR_NO_RESOURCES:
   7653 		case SCF_ERROR_INTERNAL:
   7654 			bad_error("_scf_set_annotation", scf_error());
   7655 			/* NOTREACHED */
   7656 
   7657 		default:
   7658 			/*
   7659 			 * Do not abort apply operation because of
   7660 			 * inability to create annotation audit event.
   7661 			 */
   7662 			warn(gettext("_scf_set_annotation() unexpectedly "
   7663 			    "failed with return code of %d\n"), scf_error());
   7664 			break;
   7665 		}
   7666 	}
   7667 
   7668 	for (svc = uu_list_first(bndl->sc_bundle_services);
   7669 	    svc != NULL;
   7670 	    svc = uu_list_next(bndl->sc_bundle_services, svc)) {
   7671 		int refresh = 0;
   7672 
   7673 		if (scf_scope_get_service(rscope, svc->sc_name, rsvc) != 0) {
   7674 			switch (scf_error()) {
   7675 			case SCF_ERROR_NOT_FOUND:
   7676 				if (g_verbose)
   7677 					warn(gettext("Ignoring nonexistent "
   7678 					    "service %s.\n"), svc->sc_name);
   7679 				continue;
   7680 
   7681 			default:
   7682 				scfdie();
   7683 			}
   7684 		}
   7685 
   7686 		/*
   7687 		 * if we have pgs in the profile, we need to refresh ALL
   7688 		 * instances of the service
   7689 		 */
   7690 		if (uu_list_numnodes(svc->sc_pgroups) != 0) {
   7691 			refresh = 1;
   7692 			r = lscf_import_service_pgs(rsvc, svc->sc_fmri, svc,
   7693 			    SCI_FORCE | SCI_KEEP);
   7694 			switch (_lscf_import_err(r, svc->sc_fmri)) {
   7695 			case IMPORT_NEXT:
   7696 				break;
   7697 
   7698 			case IMPORT_OUT:
   7699 				goto out;
   7700 
   7701 			case IMPORT_BAD:
   7702 			default:
   7703 				bad_error("lscf_import_service_pgs", r);
   7704 			}
   7705 		}
   7706 
   7707 		for (inst = uu_list_first(
   7708 		    svc->sc_u.sc_service.sc_service_instances);
   7709 		    inst != NULL;
   7710 		    inst = uu_list_next(
   7711 		    svc->sc_u.sc_service.sc_service_instances, inst)) {
   7712 			if (scf_service_get_instance(rsvc, inst->sc_name,
   7713 			    rinst) != 0) {
   7714 				switch (scf_error()) {
   7715 				case SCF_ERROR_NOT_FOUND:
   7716 					if (g_verbose)
   7717 						warn(gettext("Ignoring "
   7718 						    "nonexistant instance "
   7719 						    "%s:%s.\n"),
   7720 						    inst->sc_parent->sc_name,
   7721 						    inst->sc_name);
   7722 					continue;
   7723 
   7724 				default:
   7725 					scfdie();
   7726 				}
   7727 			}
   7728 
   7729 			r = lscf_import_instance_pgs(rinst, inst->sc_fmri, inst,
   7730 			    SCI_FORCE | SCI_KEEP);
   7731 			switch (_lscf_import_err(r, inst->sc_fmri)) {
   7732 			case IMPORT_NEXT:
   7733 				break;
   7734 
   7735 			case IMPORT_OUT:
   7736 				goto out;
   7737 
   7738 			case IMPORT_BAD:
   7739 			default:
   7740 				bad_error("lscf_import_instance_pgs", r);
   7741 			}
   7742 
   7743 			/* refresh only if there is no pgs in the service */
   7744 			if (refresh == 0)
   7745 				(void) refresh_entity(0, rinst, inst->sc_fmri,
   7746 				    NULL, NULL, NULL);
   7747 		}
   7748 
   7749 		if (refresh == 1) {
   7750 			char *name_buf = safe_malloc(max_scf_name_len + 1);
   7751 
   7752 			(void) refresh_entity(1, rsvc, svc->sc_name, rinst,
   7753 			    iter, name_buf);
   7754 			free(name_buf);
   7755 		}
   7756 	}
   7757 
   7758 out:
   7759 	if (annotation_set) {
   7760 		/* Remove security audit annotation strings. */
   7761 		(void) _scf_set_annotation(g_hndl, NULL, NULL);
   7762 	}
   7763 
   7764 	scf_transaction_destroy(imp_tx);
   7765 	imp_tx = NULL;
   7766 	scf_pg_destroy(imp_pg);
   7767 	imp_pg = NULL;
   7768 
   7769 	scf_iter_destroy(iter);
   7770 	scf_instance_destroy(rinst);
   7771 	scf_service_destroy(rsvc);
   7772 	scf_scope_destroy(rscope);
   7773 	return (0);
   7774 }
   7775 
   7776 
   7777 /*
   7778  * Export.  These functions create and output an XML tree of a service
   7779  * description from the repository.  This is largely the inverse of
   7780  * lxml_get_bundle() in svccfg_xml.c, but with some kickers:
   7781  *
   7782  * - We must include any properties which are not represented specifically by
   7783  *   a service manifest, e.g., properties created by an admin post-import.  To
   7784  *   do so we'll iterate through all properties and deal with each
   7785  *   apropriately.
   7786  *
   7787  * - Children of services and instances must must be in the order set by the
   7788  *   DTD, but we iterate over the properties in undefined order.  The elements
   7789  *   are not easily (or efficiently) sortable by name.  Since there's a fixed
   7790  *   number of classes of them, however, we'll keep the classes separate and
   7791  *   assemble them in order.
   7792  */
   7793 
   7794 /*
   7795  * Convenience function to handle xmlSetProp errors (and type casting).
   7796  */
   7797 static void
   7798 safe_setprop(xmlNodePtr n, const char *name, const char *val)
   7799 {
   7800 	if (xmlSetProp(n, (const xmlChar *)name, (const xmlChar *)val) == NULL)
   7801 		uu_die(gettext("Could not set XML property.\n"));
   7802 }
   7803 
   7804 /*
   7805  * Convenience function to set an XML attribute to the single value of an
   7806  * astring property.  If the value happens to be the default, don't set the
   7807  * attribute.  "dval" should be the default value supplied by the DTD, or
   7808  * NULL for no default.
   7809  */
   7810 static int
   7811 set_attr_from_prop_default(scf_property_t *prop, xmlNodePtr n,
   7812     const char *name, const char *dval)
   7813 {
   7814 	scf_value_t *val;
   7815 	ssize_t len;
   7816 	char *str;
   7817 
   7818 	val = scf_value_create(g_hndl);
   7819 	if (val == NULL)
   7820 		scfdie();
   7821 
   7822 	if (prop_get_val(prop, val) != 0) {
   7823 		scf_value_destroy(val);
   7824 		return (-1);
   7825 	}
   7826 
   7827 	len = scf_value_get_as_string(val, NULL, 0);
   7828 	if (len < 0)
   7829 		scfdie();
   7830 
   7831 	str = safe_malloc(len + 1);
   7832 
   7833 	if (scf_value_get_as_string(val, str, len + 1) < 0)
   7834 		scfdie();
   7835 
   7836 	scf_value_destroy(val);
   7837 
   7838 	if (dval == NULL || strcmp(str, dval) != 0)
   7839 		safe_setprop(n, name, str);
   7840 
   7841 	free(str);
   7842 
   7843 	return (0);
   7844 }
   7845 
   7846 /*
   7847  * As above, but the attribute is always set.
   7848  */
   7849 static int
   7850 set_attr_from_prop(scf_property_t *prop, xmlNodePtr n, const char *name)
   7851 {
   7852 	return (set_attr_from_prop_default(prop, n, name, NULL));
   7853 }
   7854 
   7855 /*
   7856  * Dump the given document onto f, with "'s replaced by ''s.
   7857  */
   7858 static int
   7859 write_service_bundle(xmlDocPtr doc, FILE *f)
   7860 {
   7861 	xmlChar *mem;
   7862 	int sz, i;
   7863 
   7864 	mem = NULL;
   7865 	xmlDocDumpFormatMemory(doc, &mem, &sz, 1);
   7866 
   7867 	if (mem == NULL) {
   7868 		semerr(gettext("Could not dump XML tree.\n"));
   7869 		return (-1);
   7870 	}
   7871 
   7872 	/*
   7873 	 * Fortunately libxml produces &quot; instead of ", so we can blindly
   7874 	 * replace all " with '.  Cursed libxml2!  Why must you #ifdef out the
   7875 	 * &apos; code?!
   7876 	 */
   7877 	for (i = 0; i < sz; ++i) {
   7878 		char c = (char)mem[i];
   7879 
   7880 		if (c == '"')
   7881 			(void) fputc('\'', f);
   7882 		else if (c == '\'')
   7883 			(void) fwrite("&apos;", sizeof ("&apos;") - 1, 1, f);
   7884 		else
   7885 			(void) fputc(c, f);
   7886 	}
   7887 
   7888 	return (0);
   7889 }
   7890 
   7891 /*
   7892  * Create the DOM elements in elts necessary to (generically) represent prop
   7893  * (i.e., a property or propval element).  If the name of the property is
   7894  * known, it should be passed as name_arg.  Otherwise, pass NULL.
   7895  */
   7896 static void
   7897 export_property(scf_property_t *prop, const char *name_arg,
   7898     struct pg_elts *elts, int flags)
   7899 {
   7900 	const char *type;
   7901 	scf_error_t err = 0;
   7902 	xmlNodePtr pnode, lnode;
   7903 	char *lnname;
   7904 	int ret;
   7905 
   7906 	/* name */
   7907 	if (name_arg != NULL) {
   7908 		(void) strcpy(exp_str, name_arg);
   7909 	} else {
   7910 		if (scf_property_get_name(prop, exp_str, exp_str_sz) < 0)
   7911 			scfdie();
   7912 	}
   7913 
   7914 	/* type */
   7915 	type = prop_to_typestr(prop);
   7916 	if (type == NULL)
   7917 		uu_die(gettext("Can't export property %s: unknown type.\n"),
   7918 		    exp_str);
   7919 
   7920 	/* If we're exporting values, and there's just one, export it here. */
   7921 	if (!(flags & SCE_ALL_VALUES))
   7922 		goto empty;
   7923 
   7924 	if (scf_property_get_value(prop, exp_val) == SCF_SUCCESS) {
   7925 		xmlNodePtr n;
   7926 
   7927 		/* Single value, so use propval */
   7928 		n = xmlNewNode(NULL, (xmlChar *)"propval");
   7929 		if (n == NULL)
   7930 			uu_die(emsg_create_xml);
   7931 
   7932 		safe_setprop(n, name_attr, exp_str);
   7933 		safe_setprop(n, type_attr, type);
   7934 
   7935 		if (scf_value_get_as_string(exp_val, exp_str, exp_str_sz) < 0)
   7936 			scfdie();
   7937 		safe_setprop(n, value_attr, exp_str);
   7938 
   7939 		if (elts->propvals == NULL)
   7940 			elts->propvals = n;
   7941 		else
   7942 			(void) xmlAddSibling(elts->propvals, n);
   7943 
   7944 		return;
   7945 	}
   7946 
   7947 	err = scf_error();
   7948 
   7949 	if (err == SCF_ERROR_PERMISSION_DENIED) {
   7950 		semerr(emsg_permission_denied);
   7951 		return;
   7952 	}
   7953 
   7954 	if (err != SCF_ERROR_CONSTRAINT_VIOLATED &&
   7955 	    err != SCF_ERROR_NOT_FOUND &&
   7956 	    err != SCF_ERROR_PERMISSION_DENIED)
   7957 		scfdie();
   7958 
   7959 empty:
   7960 	/* Multiple (or no) values, so use property */
   7961 	pnode = xmlNewNode(NULL, (xmlChar *)"property");
   7962 	if (pnode == NULL)
   7963 		uu_die(emsg_create_xml);
   7964 
   7965 	safe_setprop(pnode, name_attr, exp_str);
   7966 	safe_setprop(pnode, type_attr, type);
   7967 
   7968 	if (err == SCF_ERROR_CONSTRAINT_VIOLATED) {
   7969 		lnname = uu_msprintf("%s_list", type);
   7970 		if (lnname == NULL)
   7971 			uu_die(gettext("Could not create string"));
   7972 
   7973 		lnode = xmlNewChild(pnode, NULL, (xmlChar *)lnname, NULL);
   7974 		if (lnode == NULL)
   7975 			uu_die(emsg_create_xml);
   7976 
   7977 		uu_free(lnname);
   7978 
   7979 		if (scf_iter_property_values(exp_val_iter, prop) != SCF_SUCCESS)
   7980 			scfdie();
   7981 
   7982 		while ((ret = scf_iter_next_value(exp_val_iter, exp_val)) ==
   7983 		    1) {
   7984 			xmlNodePtr vn;
   7985 
   7986 			vn = xmlNewChild(lnode, NULL, (xmlChar *)"value_node",
   7987 			    NULL);
   7988 			if (vn == NULL)
   7989 				uu_die(emsg_create_xml);
   7990 
   7991 			if (scf_value_get_as_string(exp_val, exp_str,
   7992 			    exp_str_sz) < 0)
   7993 				scfdie();
   7994 			safe_setprop(vn, value_attr, exp_str);
   7995 		}
   7996 		if (ret != 0)
   7997 			scfdie();
   7998 	}
   7999 
   8000 	if (elts->properties == NULL)
   8001 		elts->properties = pnode;
   8002 	else
   8003 		(void) xmlAddSibling(elts->properties, pnode);
   8004 }
   8005 
   8006 /*
   8007  * Add a property_group element for this property group to elts.
   8008  */
   8009 static void
   8010 export_pg(scf_propertygroup_t *pg, struct entity_elts *eelts, int flags)
   8011 {
   8012 	xmlNodePtr n;
   8013 	struct pg_elts elts;
   8014 	int ret;
   8015 	boolean_t read_protected;
   8016 
   8017 	n = xmlNewNode(NULL, (xmlChar *)"property_group");
   8018 
   8019 	/* name */
   8020 	if (scf_pg_get_name(pg, exp_str, max_scf_name_len + 1) < 0)
   8021 		scfdie();
   8022 	safe_setprop(n, name_attr, exp_str);
   8023 
   8024 	/* type */
   8025 	if (scf_pg_get_type(pg, exp_str, exp_str_sz) < 0)
   8026 		scfdie();
   8027 	safe_setprop(n, type_attr, exp_str);
   8028 
   8029 	/* properties */
   8030 	if (scf_iter_pg_properties(exp_prop_iter, pg) != SCF_SUCCESS)
   8031 		scfdie();
   8032 
   8033 	(void) memset(&elts, 0, sizeof (elts));
   8034 
   8035 	/*
   8036 	 * If this property group is not read protected, we always want to
   8037 	 * output all the values.  Otherwise, we only output the values if the
   8038 	 * caller set SCE_ALL_VALUES (i.e., the user gave us export/archive -a).
   8039 	 */
   8040 	if (_scf_pg_is_read_protected(pg, &read_protected) != SCF_SUCCESS)
   8041 		scfdie();
   8042 
   8043 	if (!read_protected)
   8044 		flags |= SCE_ALL_VALUES;
   8045 
   8046 	while ((ret = scf_iter_next_property(exp_prop_iter, exp_prop)) == 1) {
   8047 		if (scf_property_get_name(exp_prop, exp_str, exp_str_sz) < 0)
   8048 			scfdie();
   8049 
   8050 		if (strcmp(exp_str, SCF_PROPERTY_STABILITY) == 0) {
   8051 			xmlNodePtr m;
   8052 
   8053 			m = xmlNewNode(NULL, (xmlChar *)"stability");
   8054 			if (m == NULL)
   8055 				uu_die(emsg_create_xml);
   8056 
   8057 			if (set_attr_from_prop(exp_prop, m, value_attr) == 0) {
   8058 				elts.stability = m;
   8059 				continue;
   8060 			}
   8061 
   8062 			xmlFreeNode(m);
   8063 		}
   8064 
   8065 		export_property(exp_prop, NULL, &elts, flags);
   8066 	}
   8067 	if (ret == -1)
   8068 		scfdie();
   8069 
   8070 	(void) xmlAddChild(n, elts.stability);
   8071 	(void) xmlAddChildList(n, elts.propvals);
   8072 	(void) xmlAddChildList(n, elts.properties);
   8073 
   8074 	if (eelts->property_groups == NULL)
   8075 		eelts->property_groups = n;
   8076 	else
   8077 		(void) xmlAddSibling(eelts->property_groups, n);
   8078 }
   8079 
   8080 /*
   8081  * Create an XML node representing the dependency described by the given
   8082  * property group and put it in eelts.  Unless the dependency is not valid, in
   8083  * which case create a generic property_group element which represents it and
   8084  * put it in eelts.
   8085  */
   8086 static void
   8087 export_dependency(scf_propertygroup_t *pg, struct entity_elts *eelts)
   8088 {
   8089 	xmlNodePtr n;
   8090 	int err = 0, ret;
   8091 	struct pg_elts elts;
   8092 
   8093 	n = xmlNewNode(NULL, (xmlChar *)"dependency");
   8094 	if (n == NULL)
   8095 		uu_die(emsg_create_xml);
   8096 
   8097 	/*
   8098 	 * If the external flag is present, skip this dependency because it
   8099 	 * should have been created by another manifest.
   8100 	 */
   8101 	if (scf_pg_get_property(pg, scf_property_external, exp_prop) == 0) {
   8102 		if (prop_check_type(exp_prop, SCF_TYPE_BOOLEAN) == 0 &&
   8103 		    prop_get_val(exp_prop, exp_val) == 0) {
   8104 			uint8_t b;
   8105 
   8106 			if (scf_value_get_boolean(exp_val, &b) != SCF_SUCCESS)
   8107 				scfdie();
   8108 
   8109 			if (b)
   8110 				return;
   8111 		}
   8112 	} else if (scf_error() != SCF_ERROR_NOT_FOUND)
   8113 		scfdie();
   8114 
   8115 	/* Get the required attributes. */
   8116 
   8117 	/* name */
   8118 	if (scf_pg_get_name(pg, exp_str, max_scf_name_len + 1) < 0)
   8119 		scfdie();
   8120 	safe_setprop(n, name_attr, exp_str);
   8121 
   8122 	/* grouping */
   8123 	if (pg_get_prop(pg, SCF_PROPERTY_GROUPING, exp_prop) != 0 ||
   8124 	    set_attr_from_prop(exp_prop, n, "grouping") != 0)
   8125 		err = 1;
   8126 
   8127 	/* restart_on */
   8128 	if (pg_get_prop(pg, SCF_PROPERTY_RESTART_ON, exp_prop) != 0 ||
   8129 	    set_attr_from_prop(exp_prop, n, "restart_on") != 0)
   8130 		err = 1;
   8131 
   8132 	/* type */
   8133 	if (pg_get_prop(pg, SCF_PROPERTY_TYPE, exp_prop) != 0 ||
   8134 	    set_attr_from_prop(exp_prop, n, type_attr) != 0)
   8135 		err = 1;
   8136 
   8137 	/*
   8138 	 * entities: Not required, but if we create no children, it will be
   8139 	 * created as empty on import, so fail if it's missing.
   8140 	 */
   8141 	if (pg_get_prop(pg, SCF_PROPERTY_ENTITIES, exp_prop) == 0 &&
   8142 	    prop_check_type(exp_prop, SCF_TYPE_FMRI) == 0) {
   8143 		scf_iter_t *eiter;
   8144 		int ret2;
   8145 
   8146 		eiter = scf_iter_create(g_hndl);
   8147 		if (eiter == NULL)
   8148 			scfdie();
   8149 
   8150 		if (scf_iter_property_values(eiter, exp_prop) != SCF_SUCCESS)
   8151 			scfdie();
   8152 
   8153 		while ((ret2 = scf_iter_next_value(eiter, exp_val)) == 1) {
   8154 			xmlNodePtr ch;
   8155 
   8156 			if (scf_value_get_astring(exp_val, exp_str,
   8157 			    exp_str_sz) < 0)
   8158 				scfdie();
   8159 
   8160 			/*
   8161 			 * service_fmri's must be first, so we can add them
   8162 			 * here.
   8163 			 */
   8164 			ch = xmlNewChild(n, NULL, (xmlChar *)"service_fmri",
   8165 			    NULL);
   8166 			if (ch == NULL)
   8167 				uu_die(emsg_create_xml);
   8168 
   8169 			safe_setprop(ch, value_attr, exp_str);
   8170 		}
   8171 		if (ret2 == -1)
   8172 			scfdie();
   8173 
   8174 		scf_iter_destroy(eiter);
   8175 	} else
   8176 		err = 1;
   8177 
   8178 	if (err) {
   8179 		xmlFreeNode(n);
   8180 
   8181 		export_pg(pg, eelts, 0);
   8182 
   8183 		return;
   8184 	}
   8185 
   8186 	/* Iterate through the properties & handle each. */
   8187 	if (scf_iter_pg_properties(exp_prop_iter, pg) != SCF_SUCCESS)
   8188 		scfdie();
   8189 
   8190 	(void) memset(&elts, 0, sizeof (elts));
   8191 
   8192 	while ((ret = scf_iter_next_property(exp_prop_iter, exp_prop)) == 1) {
   8193 		if (scf_property_get_name(exp_prop, exp_str, exp_str_sz) < 0)
   8194 			scfdie();
   8195 
   8196 		if (strcmp(exp_str, SCF_PROPERTY_GROUPING) == 0 ||
   8197 		    strcmp(exp_str, SCF_PROPERTY_RESTART_ON) == 0 ||
   8198 		    strcmp(exp_str, SCF_PROPERTY_TYPE) == 0 ||
   8199 		    strcmp(exp_str, SCF_PROPERTY_ENTITIES) == 0) {
   8200 			continue;
   8201 		} else if (strcmp(exp_str, SCF_PROPERTY_STABILITY) == 0) {
   8202 			xmlNodePtr m;
   8203 
   8204 			m = xmlNewNode(NULL, (xmlChar *)"stability");
   8205 			if (m == NULL)
   8206 				uu_die(emsg_create_xml);
   8207 
   8208 			if (set_attr_from_prop(exp_prop, m, value_attr) == 0) {
   8209 				elts.stability = m;
   8210 				continue;
   8211 			}
   8212 
   8213 			xmlFreeNode(m);
   8214 		}
   8215 
   8216 		export_property(exp_prop, exp_str, &elts, 0);
   8217 	}
   8218 	if (ret == -1)
   8219 		scfdie();
   8220 
   8221 	(void) xmlAddChild(n, elts.stability);
   8222 	(void) xmlAddChildList(n, elts.propvals);
   8223 	(void) xmlAddChildList(n, elts.properties);
   8224 
   8225 	if (eelts->dependencies == NULL)
   8226 		eelts->dependencies = n;
   8227 	else
   8228 		(void) xmlAddSibling(eelts->dependencies, n);
   8229 }
   8230 
   8231 static xmlNodePtr
   8232 export_method_environment(scf_propertygroup_t *pg)
   8233 {
   8234 	xmlNodePtr env;
   8235 	int ret;
   8236 	int children = 0;
   8237 
   8238 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENVIRONMENT, NULL) != 0)
   8239 		return (NULL);
   8240 
   8241 	env = xmlNewNode(NULL, (xmlChar *)"method_environment");
   8242 	if (env == NULL)
   8243 		uu_die(emsg_create_xml);
   8244 
   8245 	if (pg_get_prop(pg, SCF_PROPERTY_ENVIRONMENT, exp_prop) != 0)
   8246 		scfdie();
   8247 
   8248 	if (scf_iter_property_values(exp_val_iter, exp_prop) != SCF_SUCCESS)
   8249 		scfdie();
   8250 
   8251 	while ((ret = scf_iter_next_value(exp_val_iter, exp_val)) == 1) {
   8252 		xmlNodePtr ev;
   8253 		char *cp;
   8254 
   8255 		if (scf_value_get_as_string(exp_val, exp_str, exp_str_sz) < 0)
   8256 			scfdie();
   8257 
   8258 		if ((cp = strchr(exp_str, '=')) == NULL || cp == exp_str) {
   8259 			warn(gettext("Invalid environment variable \"%s\".\n"),
   8260 			    exp_str);
   8261 			continue;
   8262 		} else if (strncmp(exp_str, "SMF_", 4) == 0) {
   8263 			warn(gettext("Invalid environment variable \"%s\"; "
   8264 			    "\"SMF_\" prefix is reserved.\n"), exp_str);
   8265 			continue;
   8266 		}
   8267 
   8268 		*cp = '\0';
   8269 		cp++;
   8270 
   8271 		ev = xmlNewChild(env, NULL, (xmlChar *)"envvar", NULL);
   8272 		if (ev == NULL)
   8273 			uu_die(emsg_create_xml);
   8274 
   8275 		safe_setprop(ev, name_attr, exp_str);
   8276 		safe_setprop(ev, value_attr, cp);
   8277 		children++;
   8278 	}
   8279 
   8280 	if (ret != 0)
   8281 		scfdie();
   8282 
   8283 	if (children == 0) {
   8284 		xmlFreeNode(env);
   8285 		return (NULL);
   8286 	}
   8287 
   8288 	return (env);
   8289 }
   8290 
   8291 /*
   8292  * As above, but for a method property group.
   8293  */
   8294 static void
   8295 export_method(scf_propertygroup_t *pg, struct entity_elts *eelts)
   8296 {
   8297 	xmlNodePtr n, env;
   8298 	char *str;
   8299 	int err = 0, nonenv, ret;
   8300 	uint8_t use_profile;
   8301 	struct pg_elts elts;
   8302 	xmlNodePtr ctxt = NULL;
   8303 
   8304 	n = xmlNewNode(NULL, (xmlChar *)"exec_method");
   8305 
   8306 	/* Get the required attributes. */
   8307 
   8308 	/* name */
   8309 	if (scf_pg_get_name(pg, exp_str, max_scf_name_len + 1) < 0)
   8310 		scfdie();
   8311 	safe_setprop(n, name_attr, exp_str);
   8312 
   8313 	/* type */
   8314 	if (pg_get_prop(pg, SCF_PROPERTY_TYPE, exp_prop) != 0 ||
   8315 	    set_attr_from_prop(exp_prop, n, type_attr) != 0)
   8316 		err = 1;
   8317 
   8318 	/* exec */
   8319 	if (pg_get_prop(pg, SCF_PROPERTY_EXEC, exp_prop) != 0 ||
   8320 	    set_attr_from_prop(exp_prop, n, "exec") != 0)
   8321 		err = 1;
   8322 
   8323 	/* timeout */
   8324 	if (pg_get_prop(pg, SCF_PROPERTY_TIMEOUT, exp_prop) == 0 &&
   8325 	    prop_check_type(exp_prop, SCF_TYPE_COUNT) == 0 &&
   8326 	    prop_get_val(exp_prop, exp_val) == 0) {
   8327 		uint64_t c;
   8328 
   8329 		if (scf_value_get_count(exp_val, &c) != SCF_SUCCESS)
   8330 			scfdie();
   8331 
   8332 		str = uu_msprintf("%llu", c);
   8333 		if (str == NULL)
   8334 			uu_die(gettext("Could not create string"));
   8335 
   8336 		safe_setprop(n, "timeout_seconds", str);
   8337 		free(str);
   8338 	} else
   8339 		err = 1;
   8340 
   8341 	if (err) {
   8342 		xmlFreeNode(n);
   8343 
   8344 		export_pg(pg, eelts, 0);
   8345 
   8346 		return;
   8347 	}
   8348 
   8349 
   8350 	/*
   8351 	 * If we're going to have a method_context child, we need to know
   8352 	 * before we iterate through the properties.  Since method_context's
   8353 	 * are optional, we don't want to complain about any properties
   8354 	 * missing if none of them are there.  Thus we can't use the
   8355 	 * convenience functions.
   8356 	 */
   8357 	nonenv =
   8358 	    scf_pg_get_property(pg, SCF_PROPERTY_WORKING_DIRECTORY, NULL) ==
   8359 	    SCF_SUCCESS ||
   8360 	    scf_pg_get_property(pg, SCF_PROPERTY_PROJECT, NULL) ==
   8361 	    SCF_SUCCESS ||
   8362 	    scf_pg_get_property(pg, SCF_PROPERTY_RESOURCE_POOL, NULL) ==
   8363 	    SCF_SUCCESS ||
   8364 	    scf_pg_get_property(pg, SCF_PROPERTY_USE_PROFILE, NULL) ==
   8365 	    SCF_SUCCESS;
   8366 
   8367 	if (nonenv) {
   8368 		ctxt = xmlNewNode(NULL, (xmlChar *)"method_context");
   8369 		if (ctxt == NULL)
   8370 			uu_die(emsg_create_xml);
   8371 
   8372 		if (pg_get_prop(pg, SCF_PROPERTY_WORKING_DIRECTORY, exp_prop) ==
   8373 		    0 &&
   8374 		    set_attr_from_prop_default(exp_prop, ctxt,
   8375 		    "working_directory", ":default") != 0)
   8376 			err = 1;
   8377 
   8378 		if (pg_get_prop(pg, SCF_PROPERTY_PROJECT, exp_prop) == 0 &&
   8379 		    set_attr_from_prop_default(exp_prop, ctxt, "project",
   8380 		    ":default") != 0)
   8381 			err = 1;
   8382 
   8383 		if (pg_get_prop(pg, SCF_PROPERTY_RESOURCE_POOL, exp_prop) ==
   8384 		    0 &&
   8385 		    set_attr_from_prop_default(exp_prop, ctxt,
   8386 		    "resource_pool", ":default") != 0)
   8387 			err = 1;
   8388 		/*
   8389 		 * We only want to complain about profile or credential
   8390 		 * properties if we will use them.  To determine that we must
   8391 		 * examine USE_PROFILE.
   8392 		 */
   8393 		if (pg_get_prop(pg, SCF_PROPERTY_USE_PROFILE, exp_prop) == 0 &&
   8394 		    prop_check_type(exp_prop, SCF_TYPE_BOOLEAN) == 0 &&
   8395 		    prop_get_val(exp_prop, exp_val) == 0) {
   8396 			if (scf_value_get_boolean(exp_val, &use_profile) !=
   8397 			    SCF_SUCCESS) {
   8398 				scfdie();
   8399 			}
   8400 
   8401 			if (use_profile) {
   8402 				xmlNodePtr prof;
   8403 
   8404 				prof = xmlNewChild(ctxt, NULL,
   8405 				    (xmlChar *)"method_profile", NULL);
   8406 				if (prof == NULL)
   8407 					uu_die(emsg_create_xml);
   8408 
   8409 				if (pg_get_prop(pg, SCF_PROPERTY_PROFILE,
   8410 				    exp_prop) != 0 ||
   8411 				    set_attr_from_prop(exp_prop, prof,
   8412 				    name_attr) != 0)
   8413 					err = 1;
   8414 			} else {
   8415 				xmlNodePtr cred;
   8416 
   8417 				cred = xmlNewChild(ctxt, NULL,
   8418 				    (xmlChar *)"method_credential", NULL);
   8419 				if (cred == NULL)
   8420 					uu_die(emsg_create_xml);
   8421 
   8422 				if (pg_get_prop(pg, SCF_PROPERTY_USER,
   8423 				    exp_prop) != 0 ||
   8424 				    set_attr_from_prop(exp_prop, cred,
   8425 				    "user") != 0) {
   8426 					err = 1;
   8427 				}
   8428 
   8429 				if (pg_get_prop(pg, SCF_PROPERTY_GROUP,
   8430 				    exp_prop) == 0 &&
   8431 				    set_attr_from_prop_default(exp_prop, cred,
   8432 				    "group", ":default") != 0)
   8433 					err = 1;
   8434 
   8435 				if (pg_get_prop(pg, SCF_PROPERTY_SUPP_GROUPS,
   8436 				    exp_prop) == 0 &&
   8437 				    set_attr_from_prop_default(exp_prop, cred,
   8438 				    "supp_groups", ":default") != 0)
   8439 					err = 1;
   8440 
   8441 				if (pg_get_prop(pg, SCF_PROPERTY_PRIVILEGES,
   8442 				    exp_prop) == 0 &&
   8443 				    set_attr_from_prop_default(exp_prop, cred,
   8444 				    "privileges", ":default") != 0)
   8445 					err = 1;
   8446 
   8447 				if (pg_get_prop(pg,
   8448 				    SCF_PROPERTY_LIMIT_PRIVILEGES,
   8449 				    exp_prop) == 0 &&
   8450 				    set_attr_from_prop_default(exp_prop, cred,
   8451 				    "limit_privileges", ":default") != 0)
   8452 					err = 1;
   8453 			}
   8454 		}
   8455 	}
   8456 
   8457 	if ((env = export_method_environment(pg)) != NULL) {
   8458 		if (ctxt == NULL) {
   8459 			ctxt = xmlNewNode(NULL, (xmlChar *)"method_context");
   8460 			if (ctxt == NULL)
   8461 				uu_die(emsg_create_xml);
   8462 		}
   8463 		(void) xmlAddChild(ctxt, env);
   8464 	}
   8465 
   8466 	if (env != NULL || (nonenv && err == 0))
   8467 		(void) xmlAddChild(n, ctxt);
   8468 	else
   8469 		xmlFreeNode(ctxt);
   8470 
   8471 	nonenv = (err == 0);
   8472 
   8473 	if (scf_iter_pg_properties(exp_prop_iter, pg) != SCF_SUCCESS)
   8474 		scfdie();
   8475 
   8476 	(void) memset(&elts, 0, sizeof (elts));
   8477 
   8478 	while ((ret = scf_iter_next_property(exp_prop_iter, exp_prop)) == 1) {
   8479 		if (scf_property_get_name(exp_prop, exp_str, exp_str_sz) < 0)
   8480 			scfdie();
   8481 
   8482 		if (strcmp(exp_str, SCF_PROPERTY_TYPE) == 0 ||
   8483 		    strcmp(exp_str, SCF_PROPERTY_EXEC) == 0 ||
   8484 		    strcmp(exp_str, SCF_PROPERTY_TIMEOUT) == 0) {
   8485 			continue;
   8486 		} else if (strcmp(exp_str, SCF_PROPERTY_STABILITY) == 0) {
   8487 			xmlNodePtr m;
   8488 
   8489 			m = xmlNewNode(NULL, (xmlChar *)"stability");
   8490 			if (m == NULL)
   8491 				uu_die(emsg_create_xml);
   8492 
   8493 			if (set_attr_from_prop(exp_prop, m, value_attr) == 0) {
   8494 				elts.stability = m;
   8495 				continue;
   8496 			}
   8497 
   8498 			xmlFreeNode(m);
   8499 		} else if (strcmp(exp_str, SCF_PROPERTY_WORKING_DIRECTORY) ==
   8500 		    0 ||
   8501 		    strcmp(exp_str, SCF_PROPERTY_PROJECT) == 0 ||
   8502 		    strcmp(exp_str, SCF_PROPERTY_RESOURCE_POOL) == 0 ||
   8503 		    strcmp(exp_str, SCF_PROPERTY_USE_PROFILE) == 0) {
   8504 			if (nonenv)
   8505 				continue;
   8506 		} else if (strcmp(exp_str, SCF_PROPERTY_USER) == 0 ||
   8507 		    strcmp(exp_str, SCF_PROPERTY_GROUP) == 0 ||
   8508 		    strcmp(exp_str, SCF_PROPERTY_SUPP_GROUPS) == 0 ||
   8509 		    strcmp(exp_str, SCF_PROPERTY_PRIVILEGES) == 0 ||
   8510 		    strcmp(exp_str, SCF_PROPERTY_LIMIT_PRIVILEGES) == 0) {
   8511 			if (nonenv && !use_profile)
   8512 				continue;
   8513 		} else if (strcmp(exp_str, SCF_PROPERTY_PROFILE) == 0) {
   8514 			if (nonenv && use_profile)
   8515 				continue;
   8516 		} else if (strcmp(exp_str, SCF_PROPERTY_ENVIRONMENT) == 0) {
   8517 			if (env != NULL)
   8518 				continue;
   8519 		}
   8520 
   8521 		export_property(exp_prop, exp_str, &elts, 0);
   8522 	}
   8523 	if (ret == -1)
   8524 		scfdie();
   8525 
   8526 	(void) xmlAddChild(n, elts.stability);
   8527 	(void) xmlAddChildList(n, elts.propvals);
   8528 	(void) xmlAddChildList(n, elts.properties);
   8529 
   8530 	if (eelts->exec_methods == NULL)
   8531 		eelts->exec_methods = n;
   8532 	else
   8533 		(void) xmlAddSibling(eelts->exec_methods, n);
   8534 }
   8535 
   8536 static void
   8537 export_pg_elts(struct pg_elts *elts, const char *name, const char *type,
   8538     struct entity_elts *eelts)
   8539 {
   8540 	xmlNodePtr pgnode;
   8541 
   8542 	pgnode = xmlNewNode(NULL, (xmlChar *)"property_group");
   8543 	if (pgnode == NULL)
   8544 		uu_die(emsg_create_xml);
   8545 
   8546 	safe_setprop(pgnode, name_attr, name);
   8547 	safe_setprop(pgnode, type_attr, type);
   8548 
   8549 	(void) xmlAddChildList(pgnode, elts->propvals);
   8550 	(void) xmlAddChildList(pgnode, elts->properties);
   8551 
   8552 	if (eelts->property_groups == NULL)
   8553 		eelts->property_groups = pgnode;
   8554 	else
   8555 		(void) xmlAddSibling(eelts->property_groups, pgnode);
   8556 }
   8557 
   8558 /*
   8559  * Process the general property group for a service.  This is the one with the
   8560  * goodies.
   8561  */
   8562 static void
   8563 export_svc_general(scf_propertygroup_t *pg, struct entity_elts *selts)
   8564 {
   8565 	struct pg_elts elts;
   8566 	int ret;
   8567 
   8568 	/*
   8569 	 * In case there are properties which don't correspond to child
   8570 	 * entities of the service entity, we'll set up a pg_elts structure to
   8571 	 * put them in.
   8572 	 */
   8573 	(void) memset(&elts, 0, sizeof (elts));
   8574 
   8575 	/* Walk the properties, looking for special ones. */
   8576 	if (scf_iter_pg_properties(exp_prop_iter, pg) != SCF_SUCCESS)
   8577 		scfdie();
   8578 
   8579 	while ((ret = scf_iter_next_property(exp_prop_iter, exp_prop)) == 1) {
   8580 		if (scf_property_get_name(exp_prop, exp_str, exp_str_sz) < 0)
   8581 			scfdie();
   8582 
   8583 		if (strcmp(exp_str, SCF_PROPERTY_SINGLE_INSTANCE) == 0) {
   8584 			if (prop_check_type(exp_prop, SCF_TYPE_BOOLEAN) == 0 &&
   8585 			    prop_get_val(exp_prop, exp_val) == 0) {
   8586 				uint8_t b;
   8587 
   8588 				if (scf_value_get_boolean(exp_val, &b) !=
   8589 				    SCF_SUCCESS)
   8590 					scfdie();
   8591 
   8592 				if (b) {
   8593 					selts->single_instance =
   8594 					    xmlNewNode(NULL,
   8595 					    (xmlChar *)"single_instance");
   8596 					if (selts->single_instance == NULL)
   8597 						uu_die(emsg_create_xml);
   8598 				}
   8599 
   8600 				continue;
   8601 			}
   8602 		} else if (strcmp(exp_str, SCF_PROPERTY_RESTARTER) == 0) {
   8603 			xmlNodePtr rnode, sfnode;
   8604 
   8605 			rnode = xmlNewNode(NULL, (xmlChar *)"restarter");
   8606 			if (rnode == NULL)
   8607 				uu_die(emsg_create_xml);
   8608 
   8609 			sfnode = xmlNewChild(rnode, NULL,
   8610 			    (xmlChar *)"service_fmri", NULL);
   8611 			if (sfnode == NULL)
   8612 				uu_die(emsg_create_xml);
   8613 
   8614 			if (set_attr_from_prop(exp_prop, sfnode,
   8615 			    value_attr) == 0) {
   8616 				selts->restarter = rnode;
   8617 				continue;
   8618 			}
   8619 
   8620 			xmlFreeNode(rnode);
   8621 		} else if (strcmp(exp_str, SCF_PROPERTY_ENTITY_STABILITY) ==
   8622 		    0) {
   8623 			xmlNodePtr s;
   8624 
   8625 			s = xmlNewNode(NULL, (xmlChar *)"stability");
   8626 			if (s == NULL)
   8627 				uu_die(emsg_create_xml);
   8628 
   8629 			if (set_attr_from_prop(exp_prop, s, value_attr) == 0) {
   8630 </