10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51780Sgww * Common Development and Distribution License (the "License"). 61780Sgww * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221780Sgww * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * This is the main implementation file for the low-level repository 300Sstevel@tonic-gate * interface. 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include "lowlevel_impl.h" 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include "repcache_protocol.h" 360Sstevel@tonic-gate #include "scf_type.h" 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <assert.h> 390Sstevel@tonic-gate #include <alloca.h> 400Sstevel@tonic-gate #include <door.h> 410Sstevel@tonic-gate #include <errno.h> 420Sstevel@tonic-gate #include <fcntl.h> 430Sstevel@tonic-gate #include <fnmatch.h> 440Sstevel@tonic-gate #include <libuutil.h> 450Sstevel@tonic-gate #include <poll.h> 460Sstevel@tonic-gate #include <pthread.h> 470Sstevel@tonic-gate #include <stddef.h> 480Sstevel@tonic-gate #include <stdio.h> 490Sstevel@tonic-gate #include <stdlib.h> 500Sstevel@tonic-gate #include <string.h> 510Sstevel@tonic-gate #include <sys/mman.h> 520Sstevel@tonic-gate #include <sys/sysmacros.h> 530Sstevel@tonic-gate #include <unistd.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate #define ENV_SCF_DEBUG "LIBSCF_DEBUG" 560Sstevel@tonic-gate #define ENV_SCF_DOORPATH "LIBSCF_DOORPATH" 570Sstevel@tonic-gate 580Sstevel@tonic-gate static uint32_t default_debug = 0; 590Sstevel@tonic-gate static const char *default_door_path = REPOSITORY_DOOR_NAME; 600Sstevel@tonic-gate 610Sstevel@tonic-gate #define CALL_FAILED -1 620Sstevel@tonic-gate #define RESULT_TOO_BIG -2 630Sstevel@tonic-gate #define NOT_BOUND -3 640Sstevel@tonic-gate 650Sstevel@tonic-gate static pthread_mutex_t lowlevel_init_lock; 660Sstevel@tonic-gate static int32_t lowlevel_inited; 670Sstevel@tonic-gate 680Sstevel@tonic-gate static uu_list_pool_t *tran_entry_pool; 690Sstevel@tonic-gate static uu_list_pool_t *datael_pool; 700Sstevel@tonic-gate static uu_list_pool_t *iter_pool; 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * We want MUTEX_HELD, but we also want pthreads. 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate struct _lwp_mutex; 760Sstevel@tonic-gate extern int _mutex_held(struct _lwp_mutex *); 770Sstevel@tonic-gate #define MUTEX_HELD(m) _mutex_held((struct _lwp_mutex *)(m)) 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * no cancellation, please 810Sstevel@tonic-gate */ 820Sstevel@tonic-gate struct _lwp_cond; 830Sstevel@tonic-gate extern int _cond_wait(struct _lwp_cond *, struct _lwp_mutex *); 840Sstevel@tonic-gate #define PTHREAD_COND_WAIT(cv, mx) \ 850Sstevel@tonic-gate _cond_wait((struct _lwp_cond *)(cv), (struct _lwp_mutex *)(mx)) 860Sstevel@tonic-gate 870Sstevel@tonic-gate #ifdef lint 880Sstevel@tonic-gate #define assert_nolint(x) (void)0 890Sstevel@tonic-gate #else 900Sstevel@tonic-gate #define assert_nolint(x) assert(x) 910Sstevel@tonic-gate #endif 920Sstevel@tonic-gate 930Sstevel@tonic-gate static void scf_iter_reset_locked(scf_iter_t *iter); 940Sstevel@tonic-gate static void scf_value_reset_locked(scf_value_t *val, int and_destroy); 950Sstevel@tonic-gate 960Sstevel@tonic-gate #define TYPE_VALUE (-100) 970Sstevel@tonic-gate 980Sstevel@tonic-gate /* 990Sstevel@tonic-gate * Hold and release subhandles. We only allow one thread access to the 1000Sstevel@tonic-gate * subhandles at a time, and he can use any subset, grabbing and releasing 1010Sstevel@tonic-gate * them in any order. The only restrictions are that you cannot hold an 1020Sstevel@tonic-gate * already-held subhandle, and all subhandles must be released before 1030Sstevel@tonic-gate * returning to the original caller. 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate static void 1060Sstevel@tonic-gate handle_hold_subhandles(scf_handle_t *h, int mask) 1070Sstevel@tonic-gate { 1080Sstevel@tonic-gate assert(mask != 0 && (mask & ~RH_HOLD_ALL) == 0); 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 1110Sstevel@tonic-gate while (h->rh_hold_flags != 0 && h->rh_holder != pthread_self()) 1120Sstevel@tonic-gate (void) PTHREAD_COND_WAIT(&h->rh_cv, &h->rh_lock); 1130Sstevel@tonic-gate if (h->rh_hold_flags == 0) 1140Sstevel@tonic-gate h->rh_holder = pthread_self(); 1150Sstevel@tonic-gate assert(!(h->rh_hold_flags & mask)); 1160Sstevel@tonic-gate h->rh_hold_flags |= mask; 1170Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate static void 1210Sstevel@tonic-gate handle_rele_subhandles(scf_handle_t *h, int mask) 1220Sstevel@tonic-gate { 1230Sstevel@tonic-gate assert(mask != 0 && (mask & ~RH_HOLD_ALL) == 0); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 1260Sstevel@tonic-gate assert(h->rh_holder == pthread_self()); 1270Sstevel@tonic-gate assert((h->rh_hold_flags & mask)); 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate h->rh_hold_flags &= ~mask; 1300Sstevel@tonic-gate if (h->rh_hold_flags == 0) 1310Sstevel@tonic-gate (void) pthread_cond_signal(&h->rh_cv); 1320Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate #define HOLD_HANDLE(h, flag, field) \ 1360Sstevel@tonic-gate (handle_hold_subhandles((h), (flag)), (h)->field) 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate #define RELE_HANDLE(h, flag) \ 1390Sstevel@tonic-gate (handle_rele_subhandles((h), (flag))) 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * convenience macros, for functions that only need a one or two handles at 1430Sstevel@tonic-gate * any given time 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate #define HANDLE_HOLD_ITER(h) HOLD_HANDLE((h), RH_HOLD_ITER, rh_iter) 1460Sstevel@tonic-gate #define HANDLE_HOLD_SCOPE(h) HOLD_HANDLE((h), RH_HOLD_SCOPE, rh_scope) 1470Sstevel@tonic-gate #define HANDLE_HOLD_SERVICE(h) HOLD_HANDLE((h), RH_HOLD_SERVICE, rh_service) 1480Sstevel@tonic-gate #define HANDLE_HOLD_INSTANCE(h) HOLD_HANDLE((h), RH_HOLD_INSTANCE, rh_instance) 1490Sstevel@tonic-gate #define HANDLE_HOLD_SNAPSHOT(h) HOLD_HANDLE((h), RH_HOLD_SNAPSHOT, rh_snapshot) 1500Sstevel@tonic-gate #define HANDLE_HOLD_SNAPLVL(h) HOLD_HANDLE((h), RH_HOLD_SNAPLVL, rh_snaplvl) 1510Sstevel@tonic-gate #define HANDLE_HOLD_PG(h) HOLD_HANDLE((h), RH_HOLD_PG, rh_pg) 1520Sstevel@tonic-gate #define HANDLE_HOLD_PROPERTY(h) HOLD_HANDLE((h), RH_HOLD_PROPERTY, rh_property) 1530Sstevel@tonic-gate #define HANDLE_HOLD_VALUE(h) HOLD_HANDLE((h), RH_HOLD_VALUE, rh_value) 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate #define HANDLE_RELE_ITER(h) RELE_HANDLE((h), RH_HOLD_ITER) 1560Sstevel@tonic-gate #define HANDLE_RELE_SCOPE(h) RELE_HANDLE((h), RH_HOLD_SCOPE) 1570Sstevel@tonic-gate #define HANDLE_RELE_SERVICE(h) RELE_HANDLE((h), RH_HOLD_SERVICE) 1580Sstevel@tonic-gate #define HANDLE_RELE_INSTANCE(h) RELE_HANDLE((h), RH_HOLD_INSTANCE) 1590Sstevel@tonic-gate #define HANDLE_RELE_SNAPSHOT(h) RELE_HANDLE((h), RH_HOLD_SNAPSHOT) 1600Sstevel@tonic-gate #define HANDLE_RELE_SNAPLVL(h) RELE_HANDLE((h), RH_HOLD_SNAPLVL) 1610Sstevel@tonic-gate #define HANDLE_RELE_PG(h) RELE_HANDLE((h), RH_HOLD_PG) 1620Sstevel@tonic-gate #define HANDLE_RELE_PROPERTY(h) RELE_HANDLE((h), RH_HOLD_PROPERTY) 1630Sstevel@tonic-gate #define HANDLE_RELE_VALUE(h) RELE_HANDLE((h), RH_HOLD_VALUE) 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /*ARGSUSED*/ 1660Sstevel@tonic-gate static int 1670Sstevel@tonic-gate transaction_entry_compare(const void *l_arg, const void *r_arg, void *private) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate const char *l_prop = 1700Sstevel@tonic-gate ((scf_transaction_entry_t *)l_arg)->entry_property; 1710Sstevel@tonic-gate const char *r_prop = 1720Sstevel@tonic-gate ((scf_transaction_entry_t *)r_arg)->entry_property; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate int ret; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate ret = strcmp(l_prop, r_prop); 1770Sstevel@tonic-gate if (ret > 0) 1780Sstevel@tonic-gate return (1); 179407Sjwadams if (ret < 0) 180407Sjwadams return (-1); 181407Sjwadams return (0); 182407Sjwadams } 183407Sjwadams 184407Sjwadams static int 185407Sjwadams datael_compare(const void *l_arg, const void *r_arg, void *private) 186407Sjwadams { 187407Sjwadams uint32_t l_id = ((scf_datael_t *)l_arg)->rd_entity; 188407Sjwadams uint32_t r_id = (r_arg != NULL) ? ((scf_datael_t *)r_arg)->rd_entity : 189407Sjwadams *(uint32_t *)private; 190407Sjwadams 191407Sjwadams if (l_id > r_id) 192407Sjwadams return (1); 193407Sjwadams if (l_id < r_id) 194407Sjwadams return (-1); 195407Sjwadams return (0); 196407Sjwadams } 197407Sjwadams 198407Sjwadams static int 199407Sjwadams iter_compare(const void *l_arg, const void *r_arg, void *private) 200407Sjwadams { 201407Sjwadams uint32_t l_id = ((scf_iter_t *)l_arg)->iter_id; 202407Sjwadams uint32_t r_id = (r_arg != NULL) ? ((scf_iter_t *)r_arg)->iter_id : 203407Sjwadams *(uint32_t *)private; 204407Sjwadams 205407Sjwadams if (l_id > r_id) 206407Sjwadams return (1); 207407Sjwadams if (l_id < r_id) 2080Sstevel@tonic-gate return (-1); 2090Sstevel@tonic-gate return (0); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate static int 2130Sstevel@tonic-gate lowlevel_init(void) 2140Sstevel@tonic-gate { 2150Sstevel@tonic-gate const char *debug; 2160Sstevel@tonic-gate const char *door_path; 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate (void) pthread_mutex_lock(&lowlevel_init_lock); 2190Sstevel@tonic-gate if (lowlevel_inited == 0) { 2200Sstevel@tonic-gate if (!issetugid() && 2210Sstevel@tonic-gate (debug = getenv(ENV_SCF_DEBUG)) != NULL && debug[0] != 0 && 2220Sstevel@tonic-gate uu_strtoint(debug, &default_debug, sizeof (default_debug), 2230Sstevel@tonic-gate 0, 0, 0) == -1) { 2240Sstevel@tonic-gate (void) fprintf(stderr, "LIBSCF: $%s (%s): %s", 2250Sstevel@tonic-gate ENV_SCF_DEBUG, debug, 2260Sstevel@tonic-gate uu_strerror(uu_error())); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate if (!issetugid() && 2300Sstevel@tonic-gate (door_path = getenv(ENV_SCF_DOORPATH)) != NULL && 2310Sstevel@tonic-gate door_path[0] != 0) { 2320Sstevel@tonic-gate default_door_path = strdup(door_path); 2330Sstevel@tonic-gate if (default_door_path == NULL) 2340Sstevel@tonic-gate default_door_path = door_path; 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate datael_pool = uu_list_pool_create("SUNW,libscf_datael", 2380Sstevel@tonic-gate sizeof (scf_datael_t), offsetof(scf_datael_t, rd_node), 239407Sjwadams datael_compare, UU_LIST_POOL_DEBUG); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate iter_pool = uu_list_pool_create("SUNW,libscf_iter", 2420Sstevel@tonic-gate sizeof (scf_iter_t), offsetof(scf_iter_t, iter_node), 243407Sjwadams iter_compare, UU_LIST_POOL_DEBUG); 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate assert_nolint(offsetof(scf_transaction_entry_t, 2460Sstevel@tonic-gate entry_property) == 0); 2470Sstevel@tonic-gate tran_entry_pool = uu_list_pool_create( 2480Sstevel@tonic-gate "SUNW,libscf_transaction_entity", 2490Sstevel@tonic-gate sizeof (scf_transaction_entry_t), 2500Sstevel@tonic-gate offsetof(scf_transaction_entry_t, entry_link), 2510Sstevel@tonic-gate transaction_entry_compare, UU_LIST_POOL_DEBUG); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if (datael_pool == NULL || iter_pool == NULL || 2540Sstevel@tonic-gate tran_entry_pool == NULL) { 2550Sstevel@tonic-gate lowlevel_inited = -1; 2560Sstevel@tonic-gate goto end; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate if (!scf_setup_error()) { 2600Sstevel@tonic-gate lowlevel_inited = -1; 2610Sstevel@tonic-gate goto end; 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate lowlevel_inited = 1; 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate end: 2660Sstevel@tonic-gate (void) pthread_mutex_unlock(&lowlevel_init_lock); 2670Sstevel@tonic-gate if (lowlevel_inited > 0) 2680Sstevel@tonic-gate return (1); 2690Sstevel@tonic-gate return (0); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate static const struct { 2730Sstevel@tonic-gate scf_type_t ti_type; 2740Sstevel@tonic-gate rep_protocol_value_type_t ti_proto_type; 2750Sstevel@tonic-gate const char *ti_name; 2760Sstevel@tonic-gate } scf_type_info[] = { 2770Sstevel@tonic-gate {SCF_TYPE_BOOLEAN, REP_PROTOCOL_TYPE_BOOLEAN, "boolean"}, 2780Sstevel@tonic-gate {SCF_TYPE_COUNT, REP_PROTOCOL_TYPE_COUNT, "count"}, 2790Sstevel@tonic-gate {SCF_TYPE_INTEGER, REP_PROTOCOL_TYPE_INTEGER, "integer"}, 2800Sstevel@tonic-gate {SCF_TYPE_TIME, REP_PROTOCOL_TYPE_TIME, "time"}, 2810Sstevel@tonic-gate {SCF_TYPE_ASTRING, REP_PROTOCOL_TYPE_STRING, "astring"}, 2820Sstevel@tonic-gate {SCF_TYPE_OPAQUE, REP_PROTOCOL_TYPE_OPAQUE, "opaque"}, 2830Sstevel@tonic-gate {SCF_TYPE_USTRING, REP_PROTOCOL_SUBTYPE_USTRING, "ustring"}, 2840Sstevel@tonic-gate {SCF_TYPE_URI, REP_PROTOCOL_SUBTYPE_URI, "uri"}, 2850Sstevel@tonic-gate {SCF_TYPE_FMRI, REP_PROTOCOL_SUBTYPE_FMRI, "fmri"}, 2860Sstevel@tonic-gate {SCF_TYPE_HOST, REP_PROTOCOL_SUBTYPE_HOST, "host"}, 2870Sstevel@tonic-gate {SCF_TYPE_HOSTNAME, REP_PROTOCOL_SUBTYPE_HOSTNAME, "hostname"}, 2880Sstevel@tonic-gate {SCF_TYPE_NET_ADDR_V4, REP_PROTOCOL_SUBTYPE_NETADDR_V4, 2890Sstevel@tonic-gate "net_address_v4"}, 2900Sstevel@tonic-gate {SCF_TYPE_NET_ADDR_V6, REP_PROTOCOL_SUBTYPE_NETADDR_V6, 2910Sstevel@tonic-gate "net_address_v6"} 2920Sstevel@tonic-gate }; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate #define SCF_TYPE_INFO_COUNT (sizeof (scf_type_info) / sizeof (*scf_type_info)) 2950Sstevel@tonic-gate static rep_protocol_value_type_t 2960Sstevel@tonic-gate scf_type_to_protocol_type(scf_type_t t) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate int i; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate for (i = 0; i < SCF_TYPE_INFO_COUNT; i++) 3010Sstevel@tonic-gate if (scf_type_info[i].ti_type == t) 3020Sstevel@tonic-gate return (scf_type_info[i].ti_proto_type); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate return (REP_PROTOCOL_TYPE_INVALID); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate static scf_type_t 3080Sstevel@tonic-gate scf_protocol_type_to_type(rep_protocol_value_type_t t) 3090Sstevel@tonic-gate { 3100Sstevel@tonic-gate int i; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate for (i = 0; i < SCF_TYPE_INFO_COUNT; i++) 3130Sstevel@tonic-gate if (scf_type_info[i].ti_proto_type == t) 3140Sstevel@tonic-gate return (scf_type_info[i].ti_type); 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate return (SCF_TYPE_INVALID); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate const char * 3200Sstevel@tonic-gate scf_type_to_string(scf_type_t ty) 3210Sstevel@tonic-gate { 3220Sstevel@tonic-gate int i; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate for (i = 0; i < SCF_TYPE_INFO_COUNT; i++) 3250Sstevel@tonic-gate if (scf_type_info[i].ti_type == ty) 3260Sstevel@tonic-gate return (scf_type_info[i].ti_name); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate return ("unknown"); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate scf_type_t 3320Sstevel@tonic-gate scf_string_to_type(const char *name) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate int i; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate for (i = 0; i < sizeof (scf_type_info) / sizeof (*scf_type_info); i++) 3370Sstevel@tonic-gate if (strcmp(scf_type_info[i].ti_name, name) == 0) 3380Sstevel@tonic-gate return (scf_type_info[i].ti_type); 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate return (SCF_TYPE_INVALID); 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate int 3440Sstevel@tonic-gate scf_type_base_type(scf_type_t type, scf_type_t *out) 3450Sstevel@tonic-gate { 3460Sstevel@tonic-gate rep_protocol_value_type_t t = scf_type_to_protocol_type(type); 3470Sstevel@tonic-gate if (t == REP_PROTOCOL_TYPE_INVALID) 3480Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate *out = scf_protocol_type_to_type(scf_proto_underlying_type(t)); 3510Sstevel@tonic-gate return (SCF_SUCCESS); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * Convert a protocol error code into an SCF_ERROR_* code. 3560Sstevel@tonic-gate */ 3570Sstevel@tonic-gate static scf_error_t 3580Sstevel@tonic-gate proto_error(rep_protocol_responseid_t e) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate switch (e) { 3610Sstevel@tonic-gate case REP_PROTOCOL_FAIL_MISORDERED: 3620Sstevel@tonic-gate case REP_PROTOCOL_FAIL_UNKNOWN_ID: 3630Sstevel@tonic-gate case REP_PROTOCOL_FAIL_INVALID_TYPE: 3640Sstevel@tonic-gate case REP_PROTOCOL_FAIL_TRUNCATED: 3650Sstevel@tonic-gate case REP_PROTOCOL_FAIL_TYPE_MISMATCH: 3660Sstevel@tonic-gate case REP_PROTOCOL_FAIL_NOT_APPLICABLE: 3670Sstevel@tonic-gate case REP_PROTOCOL_FAIL_UNKNOWN: 3680Sstevel@tonic-gate return (SCF_ERROR_INTERNAL); 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate case REP_PROTOCOL_FAIL_BAD_TX: 3710Sstevel@tonic-gate return (SCF_ERROR_INVALID_ARGUMENT); 3720Sstevel@tonic-gate case REP_PROTOCOL_FAIL_BAD_REQUEST: 3730Sstevel@tonic-gate return (SCF_ERROR_INVALID_ARGUMENT); 3740Sstevel@tonic-gate case REP_PROTOCOL_FAIL_NO_RESOURCES: 3750Sstevel@tonic-gate return (SCF_ERROR_NO_RESOURCES); 3760Sstevel@tonic-gate case REP_PROTOCOL_FAIL_NOT_FOUND: 3770Sstevel@tonic-gate return (SCF_ERROR_NOT_FOUND); 3780Sstevel@tonic-gate case REP_PROTOCOL_FAIL_DELETED: 3790Sstevel@tonic-gate return (SCF_ERROR_DELETED); 3800Sstevel@tonic-gate case REP_PROTOCOL_FAIL_NOT_SET: 3810Sstevel@tonic-gate return (SCF_ERROR_NOT_SET); 3820Sstevel@tonic-gate case REP_PROTOCOL_FAIL_EXISTS: 3830Sstevel@tonic-gate return (SCF_ERROR_EXISTS); 3840Sstevel@tonic-gate case REP_PROTOCOL_FAIL_DUPLICATE_ID: 3850Sstevel@tonic-gate return (SCF_ERROR_EXISTS); 3860Sstevel@tonic-gate case REP_PROTOCOL_FAIL_PERMISSION_DENIED: 3870Sstevel@tonic-gate return (SCF_ERROR_PERMISSION_DENIED); 3880Sstevel@tonic-gate case REP_PROTOCOL_FAIL_BACKEND_ACCESS: 3890Sstevel@tonic-gate return (SCF_ERROR_BACKEND_ACCESS); 3900Sstevel@tonic-gate case REP_PROTOCOL_FAIL_BACKEND_READONLY: 3910Sstevel@tonic-gate return (SCF_ERROR_BACKEND_READONLY); 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate case REP_PROTOCOL_SUCCESS: 3940Sstevel@tonic-gate case REP_PROTOCOL_DONE: 3950Sstevel@tonic-gate case REP_PROTOCOL_FAIL_NOT_LATEST: /* TX code should handle this */ 3960Sstevel@tonic-gate default: 3970Sstevel@tonic-gate #ifndef NDEBUG 3980Sstevel@tonic-gate uu_warn("%s:%d: Bad error code %d passed to proto_error().\n", 3990Sstevel@tonic-gate __FILE__, __LINE__, e); 4000Sstevel@tonic-gate #endif 4010Sstevel@tonic-gate abort(); 4020Sstevel@tonic-gate /*NOTREACHED*/ 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate ssize_t 4070Sstevel@tonic-gate scf_limit(uint32_t limit) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate switch (limit) { 4100Sstevel@tonic-gate case SCF_LIMIT_MAX_NAME_LENGTH: 4110Sstevel@tonic-gate case SCF_LIMIT_MAX_PG_TYPE_LENGTH: 4120Sstevel@tonic-gate return (REP_PROTOCOL_NAME_LEN - 1); 4130Sstevel@tonic-gate case SCF_LIMIT_MAX_VALUE_LENGTH: 4140Sstevel@tonic-gate return (REP_PROTOCOL_VALUE_LEN - 1); 4150Sstevel@tonic-gate case SCF_LIMIT_MAX_FMRI_LENGTH: 4160Sstevel@tonic-gate return (SCF_FMRI_PREFIX_MAX_LEN + 4170Sstevel@tonic-gate sizeof (SCF_FMRI_SCOPE_PREFIX) - 1 + 4180Sstevel@tonic-gate sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1 + 4190Sstevel@tonic-gate sizeof (SCF_FMRI_SERVICE_PREFIX) - 1 + 4200Sstevel@tonic-gate sizeof (SCF_FMRI_INSTANCE_PREFIX) - 1 + 4210Sstevel@tonic-gate sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 + 4220Sstevel@tonic-gate sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 + 4230Sstevel@tonic-gate 5 * (REP_PROTOCOL_NAME_LEN - 1)); 4240Sstevel@tonic-gate default: 4250Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate static size_t 4300Sstevel@tonic-gate scf_opaque_decode(char *out_arg, const char *in, size_t max_out) 4310Sstevel@tonic-gate { 4320Sstevel@tonic-gate char a, b; 4330Sstevel@tonic-gate char *out = out_arg; 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate while (max_out > 0 && (a = in[0]) != 0 && (b = in[1]) != 0) { 4360Sstevel@tonic-gate in += 2; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate if (a >= '0' && a <= '9') 4390Sstevel@tonic-gate a -= '0'; 4400Sstevel@tonic-gate else if (a >= 'a' && a <= 'f') 4410Sstevel@tonic-gate a = a - 'a' + 10; 4420Sstevel@tonic-gate else if (a >= 'A' && a <= 'F') 4430Sstevel@tonic-gate a = a - 'A' + 10; 4440Sstevel@tonic-gate else 4450Sstevel@tonic-gate break; 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate if (b >= '0' && b <= '9') 4480Sstevel@tonic-gate b -= '0'; 4490Sstevel@tonic-gate else if (b >= 'a' && b <= 'f') 4500Sstevel@tonic-gate b = b - 'a' + 10; 4510Sstevel@tonic-gate else if (b >= 'A' && b <= 'F') 4520Sstevel@tonic-gate b = b - 'A' + 10; 4530Sstevel@tonic-gate else 4540Sstevel@tonic-gate break; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate *out++ = (a << 4) | b; 4570Sstevel@tonic-gate max_out--; 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate return (out - out_arg); 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate static size_t 4640Sstevel@tonic-gate scf_opaque_encode(char *out_arg, const char *in_arg, size_t in_sz) 4650Sstevel@tonic-gate { 4660Sstevel@tonic-gate uint8_t *in = (uint8_t *)in_arg; 4670Sstevel@tonic-gate uint8_t *end = in + in_sz; 4680Sstevel@tonic-gate char *out = out_arg; 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate if (out == NULL) 4710Sstevel@tonic-gate return (2 * in_sz); 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate while (in < end) { 4740Sstevel@tonic-gate uint8_t c = *in++; 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate uint8_t a = (c & 0xf0) >> 4; 4770Sstevel@tonic-gate uint8_t b = (c & 0x0f); 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate if (a <= 9) 4800Sstevel@tonic-gate *out++ = a + '0'; 4810Sstevel@tonic-gate else 4820Sstevel@tonic-gate *out++ = a + 'a' - 10; 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate if (b <= 9) 4850Sstevel@tonic-gate *out++ = b + '0'; 4860Sstevel@tonic-gate else 4870Sstevel@tonic-gate *out++ = b + 'a' - 10; 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate *out = 0; 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate return (out - out_arg); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate static void 4960Sstevel@tonic-gate handle_do_close(scf_handle_t *h) 4970Sstevel@tonic-gate { 4980Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 4990Sstevel@tonic-gate assert(h->rh_doorfd != -1); 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate /* 5020Sstevel@tonic-gate * if there are any active FD users, we just move the FD over 5030Sstevel@tonic-gate * to rh_doorfd_old -- they'll close it when they finish. 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate if (h->rh_fd_users > 0) { 5060Sstevel@tonic-gate h->rh_doorfd_old = h->rh_doorfd; 5070Sstevel@tonic-gate h->rh_doorfd = -1; 5080Sstevel@tonic-gate } else { 5090Sstevel@tonic-gate assert(h->rh_doorfd_old == -1); 5100Sstevel@tonic-gate (void) close(h->rh_doorfd); 5110Sstevel@tonic-gate h->rh_doorfd = -1; 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate /* 5160Sstevel@tonic-gate * Check if a handle is currently bound. fork()ing implicitly unbinds 5170Sstevel@tonic-gate * the handle in the child. 5180Sstevel@tonic-gate */ 5190Sstevel@tonic-gate static int 5200Sstevel@tonic-gate handle_is_bound(scf_handle_t *h) 5210Sstevel@tonic-gate { 5220Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate if (h->rh_doorfd == -1) 5250Sstevel@tonic-gate return (0); 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate if (getpid() == h->rh_doorpid) 5280Sstevel@tonic-gate return (1); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* forked since our last bind -- initiate handle close */ 5310Sstevel@tonic-gate handle_do_close(h); 5320Sstevel@tonic-gate return (0); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate static int 536407Sjwadams handle_has_server_locked(scf_handle_t *h) 5370Sstevel@tonic-gate { 5380Sstevel@tonic-gate door_info_t i; 539407Sjwadams assert(MUTEX_HELD(&h->rh_lock)); 540407Sjwadams 541407Sjwadams return (handle_is_bound(h) && door_info(h->rh_doorfd, &i) != -1 && 542407Sjwadams i.di_target != -1); 543407Sjwadams } 544407Sjwadams 545407Sjwadams static int 546407Sjwadams handle_has_server(scf_handle_t *h) 547407Sjwadams { 5480Sstevel@tonic-gate int ret; 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 551407Sjwadams ret = handle_has_server_locked(h); 5520Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate return (ret); 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate /* 5580Sstevel@tonic-gate * This makes a door request on the client door associated with handle h. 5590Sstevel@tonic-gate * It will automatically retry calls which fail on EINTR. If h is not bound, 5600Sstevel@tonic-gate * returns NOT_BOUND. If the door call fails or the server response is too 5610Sstevel@tonic-gate * small, returns CALL_FAILED. If the server response is too big, truncates the 5620Sstevel@tonic-gate * response and returns RESULT_TOO_BIG. Otherwise, the size of the result is 5630Sstevel@tonic-gate * returned. 5640Sstevel@tonic-gate */ 5650Sstevel@tonic-gate static ssize_t 5660Sstevel@tonic-gate make_door_call(scf_handle_t *h, const void *req, size_t req_sz, 5670Sstevel@tonic-gate void *res, size_t res_sz) 5680Sstevel@tonic-gate { 5690Sstevel@tonic-gate door_arg_t arg; 5700Sstevel@tonic-gate int r; 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate if (!handle_is_bound(h)) { 5750Sstevel@tonic-gate return (NOT_BOUND); 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate arg.data_ptr = (void *)req; 5790Sstevel@tonic-gate arg.data_size = req_sz; 5800Sstevel@tonic-gate arg.desc_ptr = NULL; 5810Sstevel@tonic-gate arg.desc_num = 0; 5820Sstevel@tonic-gate arg.rbuf = res; 5830Sstevel@tonic-gate arg.rsize = res_sz; 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate while ((r = door_call(h->rh_doorfd, &arg)) < 0) { 5860Sstevel@tonic-gate if (errno != EINTR) 5870Sstevel@tonic-gate break; 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate if (r < 0) { 5910Sstevel@tonic-gate return (CALL_FAILED); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate if (arg.desc_num > 0) { 5950Sstevel@tonic-gate while (arg.desc_num > 0) { 5960Sstevel@tonic-gate if (arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR) { 5970Sstevel@tonic-gate int cfd = arg.desc_ptr->d_data.d_desc.d_id; 5980Sstevel@tonic-gate (void) close(cfd); 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate arg.desc_ptr++; 6010Sstevel@tonic-gate arg.desc_num--; 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate if (arg.data_ptr != res && arg.data_size > 0) 6050Sstevel@tonic-gate (void) memmove(res, arg.data_ptr, MIN(arg.data_size, res_sz)); 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate if (arg.rbuf != res) 6080Sstevel@tonic-gate (void) munmap(arg.rbuf, arg.rsize); 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate if (arg.data_size > res_sz) 6110Sstevel@tonic-gate return (RESULT_TOO_BIG); 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate if (arg.data_size < sizeof (uint32_t)) 6140Sstevel@tonic-gate return (CALL_FAILED); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate return (arg.data_size); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* 6200Sstevel@tonic-gate * Should only be used when r < 0. 6210Sstevel@tonic-gate */ 6220Sstevel@tonic-gate #define DOOR_ERRORS_BLOCK(r) { \ 6230Sstevel@tonic-gate switch (r) { \ 6240Sstevel@tonic-gate case NOT_BOUND: \ 6250Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_BOUND)); \ 6260Sstevel@tonic-gate \ 6270Sstevel@tonic-gate case CALL_FAILED: \ 6280Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN)); \ 6290Sstevel@tonic-gate \ 6300Sstevel@tonic-gate case RESULT_TOO_BIG: \ 6310Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); \ 6320Sstevel@tonic-gate \ 6330Sstevel@tonic-gate default: \ 6340Sstevel@tonic-gate assert(r == NOT_BOUND || r == CALL_FAILED || \ 6350Sstevel@tonic-gate r == RESULT_TOO_BIG); \ 6360Sstevel@tonic-gate abort(); \ 6370Sstevel@tonic-gate } \ 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * Like make_door_call(), but takes an fd instead of a handle, and expects 6420Sstevel@tonic-gate * a single file descriptor, returned via res_fd. 6430Sstevel@tonic-gate * 6440Sstevel@tonic-gate * If no file descriptor is returned, *res_fd == -1. 6450Sstevel@tonic-gate */ 6460Sstevel@tonic-gate static int 6470Sstevel@tonic-gate make_door_call_retfd(int fd, const void *req, size_t req_sz, void *res, 6480Sstevel@tonic-gate size_t res_sz, int *res_fd) 6490Sstevel@tonic-gate { 6500Sstevel@tonic-gate door_arg_t arg; 6510Sstevel@tonic-gate int r; 6520Sstevel@tonic-gate char rbuf[256]; 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate *res_fd = -1; 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate if (fd == -1) 6570Sstevel@tonic-gate return (NOT_BOUND); 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate arg.data_ptr = (void *)req; 6600Sstevel@tonic-gate arg.data_size = req_sz; 6610Sstevel@tonic-gate arg.desc_ptr = NULL; 6620Sstevel@tonic-gate arg.desc_num = 0; 6630Sstevel@tonic-gate arg.rbuf = rbuf; 6640Sstevel@tonic-gate arg.rsize = sizeof (rbuf); 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate while ((r = door_call(fd, &arg)) < 0) { 6670Sstevel@tonic-gate if (errno != EINTR) 6680Sstevel@tonic-gate break; 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate if (r < 0) 6720Sstevel@tonic-gate return (CALL_FAILED); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate if (arg.desc_num > 1) { 6750Sstevel@tonic-gate while (arg.desc_num > 0) { 6760Sstevel@tonic-gate if (arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR) { 6770Sstevel@tonic-gate int cfd = 6780Sstevel@tonic-gate arg.desc_ptr->d_data.d_desc.d_descriptor; 6790Sstevel@tonic-gate (void) close(cfd); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate arg.desc_ptr++; 6820Sstevel@tonic-gate arg.desc_num--; 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate if (arg.desc_num == 1 && arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR) 6860Sstevel@tonic-gate *res_fd = arg.desc_ptr->d_data.d_desc.d_descriptor; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate if (arg.data_size > 0) 6890Sstevel@tonic-gate (void) memmove(res, arg.data_ptr, MIN(arg.data_size, res_sz)); 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate if (arg.rbuf != rbuf) 6920Sstevel@tonic-gate (void) munmap(arg.rbuf, arg.rsize); 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate if (arg.data_size > res_sz) 6950Sstevel@tonic-gate return (RESULT_TOO_BIG); 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate if (arg.data_size < sizeof (uint32_t)) 6980Sstevel@tonic-gate return (CALL_FAILED); 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate return (arg.data_size); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate /* 7040Sstevel@tonic-gate * Fails with 7050Sstevel@tonic-gate * _VERSION_MISMATCH 7060Sstevel@tonic-gate * _NO_MEMORY 7070Sstevel@tonic-gate */ 7080Sstevel@tonic-gate scf_handle_t * 7090Sstevel@tonic-gate scf_handle_create(scf_version_t v) 7100Sstevel@tonic-gate { 7110Sstevel@tonic-gate scf_handle_t *ret; 7120Sstevel@tonic-gate int failed; 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * This will need to be revisited when we bump SCF_VERSION 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate if (v != SCF_VERSION) { 7180Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_VERSION_MISMATCH); 7190Sstevel@tonic-gate return (NULL); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate if (!lowlevel_init()) { 7230Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 7240Sstevel@tonic-gate return (NULL); 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 7280Sstevel@tonic-gate if (ret == NULL) { 7290Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 7300Sstevel@tonic-gate return (NULL); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate ret->rh_dataels = uu_list_create(datael_pool, ret, 0); 7340Sstevel@tonic-gate ret->rh_iters = uu_list_create(iter_pool, ret, 0); 7350Sstevel@tonic-gate if (ret->rh_dataels == NULL || ret->rh_iters == NULL) { 7360Sstevel@tonic-gate if (ret->rh_dataels != NULL) 7370Sstevel@tonic-gate uu_list_destroy(ret->rh_dataels); 7380Sstevel@tonic-gate if (ret->rh_iters != NULL) 7390Sstevel@tonic-gate uu_list_destroy(ret->rh_iters); 7400Sstevel@tonic-gate uu_free(ret); 7410Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 7420Sstevel@tonic-gate return (NULL); 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate ret->rh_doorfd = -1; 7460Sstevel@tonic-gate ret->rh_doorfd_old = -1; 7470Sstevel@tonic-gate (void) pthread_mutex_init(&ret->rh_lock, NULL); 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate handle_hold_subhandles(ret, RH_HOLD_ALL); 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate failed = ((ret->rh_iter = scf_iter_create(ret)) == NULL || 7520Sstevel@tonic-gate (ret->rh_scope = scf_scope_create(ret)) == NULL || 7530Sstevel@tonic-gate (ret->rh_service = scf_service_create(ret)) == NULL || 7540Sstevel@tonic-gate (ret->rh_instance = scf_instance_create(ret)) == NULL || 7550Sstevel@tonic-gate (ret->rh_snapshot = scf_snapshot_create(ret)) == NULL || 7560Sstevel@tonic-gate (ret->rh_snaplvl = scf_snaplevel_create(ret)) == NULL || 7570Sstevel@tonic-gate (ret->rh_pg = scf_pg_create(ret)) == NULL || 7580Sstevel@tonic-gate (ret->rh_property = scf_property_create(ret)) == NULL || 7590Sstevel@tonic-gate (ret->rh_value = scf_value_create(ret)) == NULL); 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate /* 7620Sstevel@tonic-gate * these subhandles count as internal references, not external ones. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate ret->rh_intrefs = ret->rh_extrefs; 7650Sstevel@tonic-gate ret->rh_extrefs = 0; 7660Sstevel@tonic-gate handle_rele_subhandles(ret, RH_HOLD_ALL); 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate if (failed) { 7690Sstevel@tonic-gate scf_handle_destroy(ret); 7700Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 7710Sstevel@tonic-gate return (NULL); 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate scf_value_set_count(ret->rh_value, default_debug); 7750Sstevel@tonic-gate (void) scf_handle_decorate(ret, "debug", ret->rh_value); 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate return (ret); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate int 7810Sstevel@tonic-gate scf_handle_decorate(scf_handle_t *handle, const char *name, scf_value_t *v) 7820Sstevel@tonic-gate { 7830Sstevel@tonic-gate if (v != SCF_DECORATE_CLEAR && handle != v->value_handle) 7840Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 7870Sstevel@tonic-gate if (handle_is_bound(handle)) { 7880Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 7890Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_IN_USE)); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate if (strcmp(name, "debug") == 0) { 7940Sstevel@tonic-gate if (v == SCF_DECORATE_CLEAR) { 7950Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 7960Sstevel@tonic-gate handle->rh_debug = 0; 7970Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 7980Sstevel@tonic-gate } else { 7990Sstevel@tonic-gate uint64_t val; 8000Sstevel@tonic-gate if (scf_value_get_count(v, &val) < 0) 8010Sstevel@tonic-gate return (-1); /* error already set */ 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 8040Sstevel@tonic-gate handle->rh_debug = (uid_t)val; 8050Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate return (0); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate if (strcmp(name, "door_path") == 0) { 8100Sstevel@tonic-gate char name[sizeof (handle->rh_doorpath)]; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate if (v == SCF_DECORATE_CLEAR) { 8130Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 8140Sstevel@tonic-gate handle->rh_doorpath[0] = 0; 8150Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 8160Sstevel@tonic-gate } else { 8170Sstevel@tonic-gate ssize_t len; 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate if ((len = scf_value_get_astring(v, name, 8200Sstevel@tonic-gate sizeof (name))) < 0) { 8210Sstevel@tonic-gate return (-1); /* error already set */ 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate if (len == 0 || len >= sizeof (name)) { 8240Sstevel@tonic-gate return (scf_set_error( 8250Sstevel@tonic-gate SCF_ERROR_INVALID_ARGUMENT)); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 8280Sstevel@tonic-gate (void) strlcpy(handle->rh_doorpath, name, 8290Sstevel@tonic-gate sizeof (handle->rh_doorpath)); 8300Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate return (0); 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 8350Sstevel@tonic-gate } 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate /* 8380Sstevel@tonic-gate * fails with INVALID_ARGUMENT and HANDLE_MISMATCH. 8390Sstevel@tonic-gate */ 8400Sstevel@tonic-gate int 8410Sstevel@tonic-gate _scf_handle_decorations(scf_handle_t *handle, scf_decoration_func *f, 8420Sstevel@tonic-gate scf_value_t *v, void *data) 8430Sstevel@tonic-gate { 8440Sstevel@tonic-gate scf_decoration_info_t i; 8450Sstevel@tonic-gate char name[sizeof (handle->rh_doorpath)]; 8460Sstevel@tonic-gate uint64_t debug; 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate if (f == NULL || v == NULL) 8490Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate if (v->value_handle != handle) 8520Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate i.sdi_name = (const char *)"debug"; 8550Sstevel@tonic-gate i.sdi_type = SCF_TYPE_COUNT; 8560Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 8570Sstevel@tonic-gate debug = handle->rh_debug; 8580Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 8590Sstevel@tonic-gate if (debug != 0) { 8600Sstevel@tonic-gate scf_value_set_count(v, debug); 8610Sstevel@tonic-gate i.sdi_value = v; 8620Sstevel@tonic-gate } else { 8630Sstevel@tonic-gate i.sdi_value = SCF_DECORATE_CLEAR; 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate if ((*f)(&i, data) == 0) 8670Sstevel@tonic-gate return (0); 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate i.sdi_name = (const char *)"door_path"; 8700Sstevel@tonic-gate i.sdi_type = SCF_TYPE_ASTRING; 8710Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 8720Sstevel@tonic-gate (void) strlcpy(name, handle->rh_doorpath, sizeof (name)); 8730Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 8740Sstevel@tonic-gate if (name[0] != 0) { 8750Sstevel@tonic-gate (void) scf_value_set_astring(v, name); 8760Sstevel@tonic-gate i.sdi_value = v; 8770Sstevel@tonic-gate } else { 8780Sstevel@tonic-gate i.sdi_value = SCF_DECORATE_CLEAR; 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate if ((*f)(&i, data) == 0) 8820Sstevel@tonic-gate return (0); 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate return (1); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate /* 8880Sstevel@tonic-gate * Fails if handle is not bound. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate static int 8910Sstevel@tonic-gate handle_unbind_unlocked(scf_handle_t *handle) 8920Sstevel@tonic-gate { 8930Sstevel@tonic-gate rep_protocol_request_t request; 8940Sstevel@tonic-gate rep_protocol_response_t response; 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate if (!handle_is_bound(handle)) 8970Sstevel@tonic-gate return (-1); 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_CLOSE; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate (void) make_door_call(handle, &request, sizeof (request), 9020Sstevel@tonic-gate &response, sizeof (response)); 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate handle_do_close(handle); 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate return (SCF_SUCCESS); 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate /* 9100Sstevel@tonic-gate * Fails with 9110Sstevel@tonic-gate * _HANDLE_DESTROYED - dp's handle has been destroyed 9120Sstevel@tonic-gate * _INTERNAL - server response too big 9130Sstevel@tonic-gate * entity already set up with different type 9140Sstevel@tonic-gate * _NO_RESOURCES - server out of memory 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate static int 9170Sstevel@tonic-gate datael_attach(scf_datael_t *dp) 9180Sstevel@tonic-gate { 9190Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate struct rep_protocol_entity_setup request; 9220Sstevel@tonic-gate rep_protocol_response_t response; 9230Sstevel@tonic-gate ssize_t r; 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate dp->rd_reset = 0; /* setup implicitly resets */ 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate if (h->rh_flags & HANDLE_DEAD) 9300Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED)); 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate if (!handle_is_bound(h)) 9330Sstevel@tonic-gate return (SCF_SUCCESS); /* nothing to do */ 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_SETUP; 9360Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 9370Sstevel@tonic-gate request.rpr_entitytype = dp->rd_type; 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 9400Sstevel@tonic-gate &response, sizeof (response)); 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate if (r == NOT_BOUND || r == CALL_FAILED) 9430Sstevel@tonic-gate return (SCF_SUCCESS); 9440Sstevel@tonic-gate if (r == RESULT_TOO_BIG) 9450Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 9480Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate return (SCF_SUCCESS); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate /* 9540Sstevel@tonic-gate * Fails with 9550Sstevel@tonic-gate * _HANDLE_DESTROYED - iter's handle has been destroyed 9560Sstevel@tonic-gate * _INTERNAL - server response too big 9570Sstevel@tonic-gate * iter already existed 9580Sstevel@tonic-gate * _NO_RESOURCES 9590Sstevel@tonic-gate */ 9600Sstevel@tonic-gate static int 9610Sstevel@tonic-gate iter_attach(scf_iter_t *iter) 9620Sstevel@tonic-gate { 9630Sstevel@tonic-gate scf_handle_t *h = iter->iter_handle; 9640Sstevel@tonic-gate struct rep_protocol_iter_request request; 9650Sstevel@tonic-gate struct rep_protocol_response response; 9660Sstevel@tonic-gate int r; 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate if (h->rh_flags & HANDLE_DEAD) 9710Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED)); 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate if (!handle_is_bound(h)) 9740Sstevel@tonic-gate return (SCF_SUCCESS); /* nothing to do */ 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_SETUP; 9770Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 9800Sstevel@tonic-gate &response, sizeof (response)); 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate if (r == NOT_BOUND || r == CALL_FAILED) 9830Sstevel@tonic-gate return (SCF_SUCCESS); 9840Sstevel@tonic-gate if (r == RESULT_TOO_BIG) 9850Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 9880Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate return (SCF_SUCCESS); 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate /* 9940Sstevel@tonic-gate * Fails with 9950Sstevel@tonic-gate * _IN_USE - handle already bound 9960Sstevel@tonic-gate * _NO_SERVER - server door could not be open()ed 9970Sstevel@tonic-gate * door call failed 9980Sstevel@tonic-gate * door_info() failed 9990Sstevel@tonic-gate * _VERSION_MISMATCH - server returned bad file descriptor 10000Sstevel@tonic-gate * server claimed bad request 10010Sstevel@tonic-gate * server reported version mismatch 10020Sstevel@tonic-gate * server refused with unknown reason 10030Sstevel@tonic-gate * _INVALID_ARGUMENT 10040Sstevel@tonic-gate * _NO_RESOURCES - server is out of memory 10050Sstevel@tonic-gate * _PERMISSION_DENIED 10060Sstevel@tonic-gate * _INTERNAL - could not set up entities or iters 10070Sstevel@tonic-gate * server response too big 10080Sstevel@tonic-gate * 10090Sstevel@tonic-gate * perhaps this should try multiple times. 10100Sstevel@tonic-gate */ 10110Sstevel@tonic-gate int 10120Sstevel@tonic-gate scf_handle_bind(scf_handle_t *handle) 10130Sstevel@tonic-gate { 10140Sstevel@tonic-gate scf_datael_t *el; 10150Sstevel@tonic-gate scf_iter_t *iter; 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate pid_t pid; 10180Sstevel@tonic-gate int fd; 10190Sstevel@tonic-gate int res; 10200Sstevel@tonic-gate door_info_t info; 10210Sstevel@tonic-gate repository_door_request_t request; 10220Sstevel@tonic-gate repository_door_response_t response; 10230Sstevel@tonic-gate const char *door_name = default_door_path; 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 10260Sstevel@tonic-gate if (handle_is_bound(handle)) { 10270Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 10280Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_IN_USE)); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate /* wait until any active fd users have cleared out */ 10320Sstevel@tonic-gate while (handle->rh_fd_users > 0) 10330Sstevel@tonic-gate (void) PTHREAD_COND_WAIT(&handle->rh_cv, &handle->rh_lock); 10340Sstevel@tonic-gate 10350Sstevel@tonic-gate /* check again, since we had to drop the lock */ 10360Sstevel@tonic-gate if (handle_is_bound(handle)) { 10370Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 10380Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_IN_USE)); 10390Sstevel@tonic-gate } 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate assert(handle->rh_doorfd == -1 && handle->rh_doorfd_old == -1); 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate if (handle->rh_doorpath[0] != 0) 10440Sstevel@tonic-gate door_name = handle->rh_doorpath; 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate fd = open(door_name, O_RDONLY, 0); 10470Sstevel@tonic-gate if (fd == -1) { 10480Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 10490Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NO_SERVER)); 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate request.rdr_version = REPOSITORY_DOOR_VERSION; 10530Sstevel@tonic-gate request.rdr_request = REPOSITORY_DOOR_REQUEST_CONNECT; 10540Sstevel@tonic-gate request.rdr_flags = handle->rh_flags; 10550Sstevel@tonic-gate request.rdr_debug = handle->rh_debug; 10560Sstevel@tonic-gate 10570Sstevel@tonic-gate pid = getpid(); 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate res = make_door_call_retfd(fd, &request, sizeof (request), 10600Sstevel@tonic-gate &response, sizeof (response), &handle->rh_doorfd); 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate (void) close(fd); 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate if (res < 0) { 10650Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate assert(res != NOT_BOUND); 10680Sstevel@tonic-gate if (res == CALL_FAILED) 10690Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NO_SERVER)); 10700Sstevel@tonic-gate assert(res == RESULT_TOO_BIG); 10710Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate if (handle->rh_doorfd < 0) { 10750Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate switch (response.rdr_status) { 10780Sstevel@tonic-gate case REPOSITORY_DOOR_SUCCESS: 10790Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_VERSION_MISMATCH)); 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate case REPOSITORY_DOOR_FAIL_BAD_REQUEST: 10820Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_VERSION_MISMATCH)); 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate case REPOSITORY_DOOR_FAIL_VERSION_MISMATCH: 10850Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_VERSION_MISMATCH)); 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate case REPOSITORY_DOOR_FAIL_BAD_FLAG: 10880Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate case REPOSITORY_DOOR_FAIL_NO_RESOURCES: 10910Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NO_RESOURCES)); 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate case REPOSITORY_DOOR_FAIL_PERMISSION_DENIED: 10940Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_PERMISSION_DENIED)); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate default: 10970Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_VERSION_MISMATCH)); 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate (void) fcntl(handle->rh_doorfd, F_SETFD, FD_CLOEXEC); 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate if (door_info(handle->rh_doorfd, &info) < 0) { 11040Sstevel@tonic-gate (void) close(handle->rh_doorfd); 11050Sstevel@tonic-gate handle->rh_doorfd = -1; 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 11080Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NO_SERVER)); 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate handle->rh_doorpid = pid; 11120Sstevel@tonic-gate handle->rh_doorid = info.di_uniquifier; 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate /* 11150Sstevel@tonic-gate * Now, re-attach everything 11160Sstevel@tonic-gate */ 11170Sstevel@tonic-gate for (el = uu_list_first(handle->rh_dataels); el != NULL; 11180Sstevel@tonic-gate el = uu_list_next(handle->rh_dataels, el)) { 11190Sstevel@tonic-gate if (datael_attach(el) == -1) { 11200Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_HANDLE_DESTROYED); 11210Sstevel@tonic-gate (void) handle_unbind_unlocked(handle); 11220Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 11230Sstevel@tonic-gate return (-1); 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate } 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate for (iter = uu_list_first(handle->rh_iters); iter != NULL; 11280Sstevel@tonic-gate iter = uu_list_next(handle->rh_iters, iter)) { 11290Sstevel@tonic-gate if (iter_attach(iter) == -1) { 11300Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_HANDLE_DESTROYED); 11310Sstevel@tonic-gate (void) handle_unbind_unlocked(handle); 11320Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 11330Sstevel@tonic-gate return (-1); 11340Sstevel@tonic-gate } 11350Sstevel@tonic-gate } 11360Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 11370Sstevel@tonic-gate return (SCF_SUCCESS); 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate int 11410Sstevel@tonic-gate scf_handle_unbind(scf_handle_t *handle) 11420Sstevel@tonic-gate { 11430Sstevel@tonic-gate int ret; 11440Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 11450Sstevel@tonic-gate ret = handle_unbind_unlocked(handle); 11460Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 11470Sstevel@tonic-gate return (ret == SCF_SUCCESS ? ret : scf_set_error(SCF_ERROR_NOT_BOUND)); 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate static scf_handle_t * 11510Sstevel@tonic-gate handle_get(scf_handle_t *h) 11520Sstevel@tonic-gate { 11530Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 11540Sstevel@tonic-gate if (h->rh_flags & HANDLE_DEAD) { 11550Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 11560Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_HANDLE_DESTROYED); 11570Sstevel@tonic-gate return (NULL); 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 11600Sstevel@tonic-gate return (h); 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * Called when an object is removed from the handle. On the last remove, 11650Sstevel@tonic-gate * cleans up and frees the handle. 11660Sstevel@tonic-gate */ 11670Sstevel@tonic-gate static void 11680Sstevel@tonic-gate handle_unrefed(scf_handle_t *handle) 11690Sstevel@tonic-gate { 11700Sstevel@tonic-gate scf_iter_t *iter; 11710Sstevel@tonic-gate scf_value_t *v; 11720Sstevel@tonic-gate scf_scope_t *sc; 11730Sstevel@tonic-gate scf_service_t *svc; 11740Sstevel@tonic-gate scf_instance_t *inst; 11750Sstevel@tonic-gate scf_snapshot_t *snap; 11760Sstevel@tonic-gate scf_snaplevel_t *snaplvl; 11770Sstevel@tonic-gate scf_propertygroup_t *pg; 11780Sstevel@tonic-gate scf_property_t *prop; 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate assert(MUTEX_HELD(&handle->rh_lock)); 11810Sstevel@tonic-gate 11820Sstevel@tonic-gate /* 11830Sstevel@tonic-gate * Don't do anything if the handle has not yet been destroyed, there 11840Sstevel@tonic-gate * are still external references, or we're already doing unrefed 11850Sstevel@tonic-gate * handling. 11860Sstevel@tonic-gate */ 11870Sstevel@tonic-gate if (!(handle->rh_flags & HANDLE_DEAD) || 11880Sstevel@tonic-gate handle->rh_extrefs > 0 || 11890Sstevel@tonic-gate handle->rh_fd_users > 0 || 11900Sstevel@tonic-gate (handle->rh_flags & HANDLE_UNREFED)) { 11910Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 11920Sstevel@tonic-gate return; 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate handle->rh_flags |= HANDLE_UNREFED; 11960Sstevel@tonic-gate 11970Sstevel@tonic-gate /* 11980Sstevel@tonic-gate * Now that we know that there are no external references, and the 11990Sstevel@tonic-gate * HANDLE_DEAD flag keeps new ones from appearing, we can clean up 12000Sstevel@tonic-gate * our subhandles and destroy the handle completely. 12010Sstevel@tonic-gate */ 12020Sstevel@tonic-gate assert(handle->rh_intrefs >= 0); 12030Sstevel@tonic-gate handle->rh_extrefs = handle->rh_intrefs; 12040Sstevel@tonic-gate handle->rh_intrefs = 0; 12050Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate handle_hold_subhandles(handle, RH_HOLD_ALL); 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate iter = handle->rh_iter; 12100Sstevel@tonic-gate sc = handle->rh_scope; 12110Sstevel@tonic-gate svc = handle->rh_service; 12120Sstevel@tonic-gate inst = handle->rh_instance; 12130Sstevel@tonic-gate snap = handle->rh_snapshot; 12140Sstevel@tonic-gate snaplvl = handle->rh_snaplvl; 12150Sstevel@tonic-gate pg = handle->rh_pg; 12160Sstevel@tonic-gate prop = handle->rh_property; 12170Sstevel@tonic-gate v = handle->rh_value; 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate handle->rh_iter = NULL; 12200Sstevel@tonic-gate handle->rh_scope = NULL; 12210Sstevel@tonic-gate handle->rh_service = NULL; 12220Sstevel@tonic-gate handle->rh_instance = NULL; 12230Sstevel@tonic-gate handle->rh_snapshot = NULL; 12240Sstevel@tonic-gate handle->rh_snaplvl = NULL; 12250Sstevel@tonic-gate handle->rh_pg = NULL; 12260Sstevel@tonic-gate handle->rh_property = NULL; 12270Sstevel@tonic-gate handle->rh_value = NULL; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate if (iter != NULL) 12300Sstevel@tonic-gate scf_iter_destroy(iter); 12310Sstevel@tonic-gate if (sc != NULL) 12320Sstevel@tonic-gate scf_scope_destroy(sc); 12330Sstevel@tonic-gate if (svc != NULL) 12340Sstevel@tonic-gate scf_service_destroy(svc); 12350Sstevel@tonic-gate if (inst != NULL) 12360Sstevel@tonic-gate scf_instance_destroy(inst); 12370Sstevel@tonic-gate if (snap != NULL) 12380Sstevel@tonic-gate scf_snapshot_destroy(snap); 12390Sstevel@tonic-gate if (snaplvl != NULL) 12400Sstevel@tonic-gate scf_snaplevel_destroy(snaplvl); 12410Sstevel@tonic-gate if (pg != NULL) 12420Sstevel@tonic-gate scf_pg_destroy(pg); 12430Sstevel@tonic-gate if (prop != NULL) 12440Sstevel@tonic-gate scf_property_destroy(prop); 12450Sstevel@tonic-gate if (v != NULL) 12460Sstevel@tonic-gate scf_value_destroy(v); 12470Sstevel@tonic-gate 12480Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate /* there should be no outstanding children at this point */ 12510Sstevel@tonic-gate assert(handle->rh_extrefs == 0); 12520Sstevel@tonic-gate assert(handle->rh_intrefs == 0); 12530Sstevel@tonic-gate assert(handle->rh_values == 0); 12540Sstevel@tonic-gate assert(handle->rh_entries == 0); 12550Sstevel@tonic-gate assert(uu_list_numnodes(handle->rh_dataels) == 0); 12560Sstevel@tonic-gate assert(uu_list_numnodes(handle->rh_iters) == 0); 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate uu_list_destroy(handle->rh_dataels); 12590Sstevel@tonic-gate uu_list_destroy(handle->rh_iters); 12600Sstevel@tonic-gate handle->rh_dataels = NULL; 12610Sstevel@tonic-gate handle->rh_iters = NULL; 12620Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 12630Sstevel@tonic-gate 12640Sstevel@tonic-gate (void) pthread_mutex_destroy(&handle->rh_lock); 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate uu_free(handle); 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate 12690Sstevel@tonic-gate void 12700Sstevel@tonic-gate scf_handle_destroy(scf_handle_t *handle) 12710Sstevel@tonic-gate { 12720Sstevel@tonic-gate if (handle == NULL) 12730Sstevel@tonic-gate return; 12740Sstevel@tonic-gate 12750Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 12760Sstevel@tonic-gate if (handle->rh_flags & HANDLE_DEAD) { 12770Sstevel@tonic-gate /* 12780Sstevel@tonic-gate * This is an error (you are not allowed to reference the 12790Sstevel@tonic-gate * handle after it is destroyed), but we can't report it. 12800Sstevel@tonic-gate */ 12810Sstevel@tonic-gate (void) pthread_mutex_unlock(&handle->rh_lock); 12820Sstevel@tonic-gate return; 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate handle->rh_flags |= HANDLE_DEAD; 12850Sstevel@tonic-gate (void) handle_unbind_unlocked(handle); 12860Sstevel@tonic-gate handle_unrefed(handle); 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate ssize_t 12900Sstevel@tonic-gate scf_myname(scf_handle_t *h, char *out, size_t len) 12910Sstevel@tonic-gate { 12920Sstevel@tonic-gate char *cp; 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate if (!handle_has_server(h)) 12950Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN)); 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate cp = getenv("SMF_FMRI"); 12980Sstevel@tonic-gate if (cp == NULL) 12990Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET)); 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate return (strlcpy(out, cp, len)); 13020Sstevel@tonic-gate } 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate static uint32_t 1305407Sjwadams handle_alloc_entityid(scf_handle_t *h) 1306407Sjwadams { 1307407Sjwadams uint32_t nextid; 1308407Sjwadams 1309407Sjwadams assert(MUTEX_HELD(&h->rh_lock)); 1310407Sjwadams 1311407Sjwadams if (uu_list_numnodes(h->rh_dataels) == UINT32_MAX) 1312407Sjwadams return (0); /* no ids available */ 1313407Sjwadams 1314407Sjwadams /* 1315407Sjwadams * The following loop assumes that there are not a huge number of 1316407Sjwadams * outstanding entities when we've wrapped. If that ends up not 1317407Sjwadams * being the case, the O(N^2) nature of this search will hurt a lot, 1318407Sjwadams * and the data structure should be switched to an AVL tree. 1319407Sjwadams */ 1320407Sjwadams nextid = h->rh_nextentity + 1; 1321407Sjwadams for (;;) { 1322407Sjwadams scf_datael_t *cur; 1323407Sjwadams 1324407Sjwadams if (nextid == 0) { 1325407Sjwadams nextid++; 1326407Sjwadams h->rh_flags |= HANDLE_WRAPPED_ENTITY; 1327407Sjwadams } 1328407Sjwadams if (!(h->rh_flags & HANDLE_WRAPPED_ENTITY)) 1329407Sjwadams break; 1330407Sjwadams 1331407Sjwadams cur = uu_list_find(h->rh_dataels, NULL, &nextid, NULL); 1332407Sjwadams if (cur == NULL) 1333407Sjwadams break; /* not in use */ 1334407Sjwadams 1335407Sjwadams if (nextid == h->rh_nextentity) 1336407Sjwadams return (0); /* wrapped around; no ids available */ 1337407Sjwadams nextid++; 1338407Sjwadams } 1339407Sjwadams 1340407Sjwadams h->rh_nextentity = nextid; 1341407Sjwadams return (nextid); 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate static uint32_t 1345407Sjwadams handle_alloc_iterid(scf_handle_t *h) 1346407Sjwadams { 1347407Sjwadams uint32_t nextid; 1348407Sjwadams 1349407Sjwadams assert(MUTEX_HELD(&h->rh_lock)); 1350407Sjwadams 1351407Sjwadams if (uu_list_numnodes(h->rh_iters) == UINT32_MAX) 1352407Sjwadams return (0); /* no ids available */ 1353407Sjwadams 1354407Sjwadams /* see the comment in handle_alloc_entityid */ 1355407Sjwadams nextid = h->rh_nextiter + 1; 1356407Sjwadams for (;;) { 1357407Sjwadams scf_iter_t *cur; 1358407Sjwadams 1359407Sjwadams if (nextid == 0) { 1360407Sjwadams nextid++; 1361407Sjwadams h->rh_flags |= HANDLE_WRAPPED_ITER; 1362407Sjwadams } 1363407Sjwadams if (!(h->rh_flags & HANDLE_WRAPPED_ITER)) 1364407Sjwadams break; /* not yet wrapped */ 1365407Sjwadams 1366407Sjwadams cur = uu_list_find(h->rh_iters, NULL, &nextid, NULL); 1367407Sjwadams if (cur == NULL) 1368407Sjwadams break; /* not in use */ 1369407Sjwadams 1370407Sjwadams if (nextid == h->rh_nextiter) 1371407Sjwadams return (0); /* wrapped around; no ids available */ 1372407Sjwadams nextid++; 1373407Sjwadams } 1374407Sjwadams 1375407Sjwadams h->rh_nextiter = nextid; 1376407Sjwadams return (nextid); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate static uint32_t 1380407Sjwadams handle_next_changeid(scf_handle_t *handle) 1381407Sjwadams { 1382407Sjwadams uint32_t nextid; 1383407Sjwadams 13840Sstevel@tonic-gate assert(MUTEX_HELD(&handle->rh_lock)); 1385407Sjwadams 1386407Sjwadams nextid = ++handle->rh_nextchangeid; 1387407Sjwadams if (nextid == 0) 1388407Sjwadams nextid = ++handle->rh_nextchangeid; 1389407Sjwadams return (nextid); 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate /* 13930Sstevel@tonic-gate * Fails with 13940Sstevel@tonic-gate * _INVALID_ARGUMENT - h is NULL 13950Sstevel@tonic-gate * _HANDLE_DESTROYED 13960Sstevel@tonic-gate * _INTERNAL - server response too big 13970Sstevel@tonic-gate * entity already set up with different type 13980Sstevel@tonic-gate * _NO_RESOURCES 13990Sstevel@tonic-gate */ 14000Sstevel@tonic-gate static int 14010Sstevel@tonic-gate datael_init(scf_datael_t *dp, scf_handle_t *h, uint32_t type) 14020Sstevel@tonic-gate { 14030Sstevel@tonic-gate int ret; 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate if (h == NULL) 14060Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate uu_list_node_init(dp, &dp->rd_node, datael_pool); 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate dp->rd_handle = h; 14110Sstevel@tonic-gate dp->rd_type = type; 14120Sstevel@tonic-gate dp->rd_reset = 0; 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 14150Sstevel@tonic-gate if (h->rh_flags & HANDLE_DEAD) { 14160Sstevel@tonic-gate /* 14170Sstevel@tonic-gate * we're in undefined territory (the user cannot use a handle 14180Sstevel@tonic-gate * directly after it has been destroyed), but we don't want 14190Sstevel@tonic-gate * to allow any new references to happen, so we fail here. 14200Sstevel@tonic-gate */ 14210Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 14220Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED)); 14230Sstevel@tonic-gate } 14240Sstevel@tonic-gate dp->rd_entity = handle_alloc_entityid(h); 1425407Sjwadams if (dp->rd_entity == 0) { 1426407Sjwadams (void) pthread_mutex_unlock(&h->rh_lock); 1427407Sjwadams uu_list_node_fini(dp, &dp->rd_node, datael_pool); 1428407Sjwadams return (scf_set_error(SCF_ERROR_NO_MEMORY)); 1429407Sjwadams } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate ret = datael_attach(dp); 14320Sstevel@tonic-gate if (ret == 0) { 14330Sstevel@tonic-gate (void) uu_list_insert_before(h->rh_dataels, NULL, dp); 14340Sstevel@tonic-gate h->rh_extrefs++; 14350Sstevel@tonic-gate } else { 14360Sstevel@tonic-gate uu_list_node_fini(dp, &dp->rd_node, datael_pool); 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 14390Sstevel@tonic-gate 14400Sstevel@tonic-gate return (ret); 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate static void 14440Sstevel@tonic-gate datael_destroy(scf_datael_t *dp) 14450Sstevel@tonic-gate { 14460Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate struct rep_protocol_entity_teardown request; 14490Sstevel@tonic-gate rep_protocol_response_t response; 14500Sstevel@tonic-gate 14510Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 14520Sstevel@tonic-gate uu_list_remove(h->rh_dataels, dp); 14530Sstevel@tonic-gate --h->rh_extrefs; 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate if (handle_is_bound(h)) { 14560Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_TEARDOWN; 14570Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate (void) make_door_call(h, &request, sizeof (request), 14600Sstevel@tonic-gate &response, sizeof (response)); 14610Sstevel@tonic-gate } 14620Sstevel@tonic-gate handle_unrefed(h); /* drops h->rh_lock */ 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate dp->rd_handle = NULL; 14650Sstevel@tonic-gate } 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate static scf_handle_t * 14680Sstevel@tonic-gate datael_handle(const scf_datael_t *dp) 14690Sstevel@tonic-gate { 14700Sstevel@tonic-gate return (handle_get(dp->rd_handle)); 14710Sstevel@tonic-gate } 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate /* 14740Sstevel@tonic-gate * We delay ENTITY_RESETs until right before the entity is used. By doing 14750Sstevel@tonic-gate * them lazily, we remove quite a few unnecessary calls. 14760Sstevel@tonic-gate */ 14770Sstevel@tonic-gate static void 14780Sstevel@tonic-gate datael_do_reset_locked(scf_datael_t *dp) 14790Sstevel@tonic-gate { 14800Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate struct rep_protocol_entity_reset request; 14830Sstevel@tonic-gate rep_protocol_response_t response; 14840Sstevel@tonic-gate 14850Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_RESET; 14880Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate (void) make_door_call(h, &request, sizeof (request), 14910Sstevel@tonic-gate &response, sizeof (response)); 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate dp->rd_reset = 0; 14940Sstevel@tonic-gate } 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate static void 14970Sstevel@tonic-gate datael_reset_locked(scf_datael_t *dp) 14980Sstevel@tonic-gate { 14990Sstevel@tonic-gate assert(MUTEX_HELD(&dp->rd_handle->rh_lock)); 15000Sstevel@tonic-gate dp->rd_reset = 1; 15010Sstevel@tonic-gate } 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate static void 15040Sstevel@tonic-gate datael_reset(scf_datael_t *dp) 15050Sstevel@tonic-gate { 15060Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 15090Sstevel@tonic-gate dp->rd_reset = 1; 15100Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate static void 15140Sstevel@tonic-gate datael_finish_reset(const scf_datael_t *dp_arg) 15150Sstevel@tonic-gate { 15160Sstevel@tonic-gate scf_datael_t *dp = (scf_datael_t *)dp_arg; 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate if (dp->rd_reset) 15190Sstevel@tonic-gate datael_do_reset_locked(dp); 15200Sstevel@tonic-gate } 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate /* 15230Sstevel@tonic-gate * Fails with _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response too 15240Sstevel@tonic-gate * big, bad entity id, request not applicable to entity, name too long for 15250Sstevel@tonic-gate * buffer), _NOT_SET, _DELETED, or _CONSTRAINT_VIOLATED (snaplevel is not of an 15260Sstevel@tonic-gate * instance). 15270Sstevel@tonic-gate */ 15280Sstevel@tonic-gate static ssize_t 15290Sstevel@tonic-gate datael_get_name(const scf_datael_t *dp, char *buf, size_t size, uint32_t type) 15300Sstevel@tonic-gate { 15310Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 15320Sstevel@tonic-gate 15330Sstevel@tonic-gate struct rep_protocol_entity_name request; 15340Sstevel@tonic-gate struct rep_protocol_name_response response; 15350Sstevel@tonic-gate ssize_t r; 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 15380Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_NAME; 15390Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 15400Sstevel@tonic-gate request.rpr_answertype = type; 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate datael_finish_reset(dp); 15430Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 15440Sstevel@tonic-gate &response, sizeof (response)); 15450Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate if (r < 0) 15480Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 15510Sstevel@tonic-gate assert(response.rpr_response != REP_PROTOCOL_FAIL_BAD_REQUEST); 15520Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_FOUND) 15530Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED)); 15540Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate return (strlcpy(buf, response.rpr_name, size)); 15570Sstevel@tonic-gate } 15580Sstevel@tonic-gate 15590Sstevel@tonic-gate /* 15600Sstevel@tonic-gate * Fails with _HANDLE_MISMATCH, _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL 15610Sstevel@tonic-gate * (server response too big, bad element id), _EXISTS (elements have same id), 15620Sstevel@tonic-gate * _NOT_SET, _DELETED, _CONSTRAINT_VIOLATED, _NOT_FOUND (scope has no parent), 15630Sstevel@tonic-gate * or _SUCCESS. 15640Sstevel@tonic-gate */ 15650Sstevel@tonic-gate static int 15660Sstevel@tonic-gate datael_get_parent(const scf_datael_t *dp, scf_datael_t *pp) 15670Sstevel@tonic-gate { 15680Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate struct rep_protocol_entity_parent request; 15710Sstevel@tonic-gate struct rep_protocol_response response; 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate ssize_t r; 15740Sstevel@tonic-gate 15750Sstevel@tonic-gate if (h != pp->rd_handle) 15760Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 15790Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_GET_PARENT; 15800Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 15810Sstevel@tonic-gate request.rpr_outid = pp->rd_entity; 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate datael_finish_reset(dp); 15840Sstevel@tonic-gate datael_finish_reset(pp); 15850Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 15860Sstevel@tonic-gate &response, sizeof (response)); 15870Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate if (r < 0) 15900Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 15930Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_FAIL_TYPE_MISMATCH) 15940Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED)); 15950Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate return (SCF_SUCCESS); 15990Sstevel@tonic-gate } 16000Sstevel@tonic-gate 16010Sstevel@tonic-gate /* 16020Sstevel@tonic-gate * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type, 16030Sstevel@tonic-gate * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response 16040Sstevel@tonic-gate * too big, bad id, iter already exists, element cannot have children of type, 16050Sstevel@tonic-gate * type is invalid, iter was reset, sequence was bad, iter walks values, iter 16060Sstevel@tonic-gate * does not walk type entities), _NOT_SET, _DELETED, _NO_RESOURCES, 16070Sstevel@tonic-gate * _BACKEND_ACCESS. 16080Sstevel@tonic-gate */ 16090Sstevel@tonic-gate static int 16100Sstevel@tonic-gate datael_get_child_composed_locked(const scf_datael_t *dp, const char *name, 16110Sstevel@tonic-gate uint32_t type, scf_datael_t *out, scf_iter_t *iter) 16120Sstevel@tonic-gate { 16130Sstevel@tonic-gate struct rep_protocol_iter_start request; 16140Sstevel@tonic-gate struct rep_protocol_iter_read read_request; 16150Sstevel@tonic-gate struct rep_protocol_response response; 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 16180Sstevel@tonic-gate ssize_t r; 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate if (h != out->rd_handle) 16210Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate if (out->rd_type != type) 16240Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 16250Sstevel@tonic-gate 16260Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 16270Sstevel@tonic-gate assert(iter != NULL); 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate scf_iter_reset_locked(iter); 16300Sstevel@tonic-gate iter->iter_type = type; 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_START; 16330Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 16340Sstevel@tonic-gate request.rpr_entity = dp->rd_entity; 16350Sstevel@tonic-gate request.rpr_itertype = type; 16360Sstevel@tonic-gate request.rpr_flags = RP_ITER_START_EXACT | RP_ITER_START_COMPOSED; 16370Sstevel@tonic-gate 16380Sstevel@tonic-gate if (name == NULL || strlcpy(request.rpr_pattern, name, 16390Sstevel@tonic-gate sizeof (request.rpr_pattern)) >= sizeof (request.rpr_pattern)) { 16400Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 16410Sstevel@tonic-gate } 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate datael_finish_reset(dp); 16440Sstevel@tonic-gate datael_finish_reset(out); 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate /* 16470Sstevel@tonic-gate * We hold the handle lock across both door calls, so that they 16480Sstevel@tonic-gate * appear atomic. 16490Sstevel@tonic-gate */ 16500Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 16510Sstevel@tonic-gate &response, sizeof (response)); 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate if (r < 0) 16540Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 16570Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate iter->iter_sequence++; 16600Sstevel@tonic-gate 16610Sstevel@tonic-gate read_request.rpr_request = REP_PROTOCOL_ITER_READ; 16620Sstevel@tonic-gate read_request.rpr_iterid = iter->iter_id; 16630Sstevel@tonic-gate read_request.rpr_sequence = iter->iter_sequence; 16640Sstevel@tonic-gate read_request.rpr_entityid = out->rd_entity; 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate r = make_door_call(h, &read_request, sizeof (read_request), 16670Sstevel@tonic-gate &response, sizeof (response)); 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate scf_iter_reset_locked(iter); 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate if (r < 0) 16720Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_DONE) { 16750Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_FOUND)); 16760Sstevel@tonic-gate } 16770Sstevel@tonic-gate 16780Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 16790Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_SET || 16800Sstevel@tonic-gate response.rpr_response == REP_PROTOCOL_FAIL_BAD_REQUEST) 16810Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 16820Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate return (0); 16860Sstevel@tonic-gate } 16870Sstevel@tonic-gate 16880Sstevel@tonic-gate /* 16890Sstevel@tonic-gate * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type, 16900Sstevel@tonic-gate * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response 16910Sstevel@tonic-gate * too big, bad id, element cannot have children of type, type is invalid), 16920Sstevel@tonic-gate * _NOT_SET, _DELETED, _NO_RESOURCES, _BACKEND_ACCESS. 16930Sstevel@tonic-gate */ 16940Sstevel@tonic-gate static int 16950Sstevel@tonic-gate datael_get_child_locked(const scf_datael_t *dp, const char *name, 16960Sstevel@tonic-gate uint32_t type, scf_datael_t *out) 16970Sstevel@tonic-gate { 16980Sstevel@tonic-gate struct rep_protocol_entity_get_child request; 16990Sstevel@tonic-gate struct rep_protocol_response response; 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 17020Sstevel@tonic-gate ssize_t r; 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate if (h != out->rd_handle) 17050Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate if (out->rd_type != type) 17080Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_GET_CHILD; 17130Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 17140Sstevel@tonic-gate request.rpr_childid = out->rd_entity; 17150Sstevel@tonic-gate 17160Sstevel@tonic-gate if (name == NULL || strlcpy(request.rpr_name, name, 17170Sstevel@tonic-gate sizeof (request.rpr_name)) >= sizeof (request.rpr_name)) { 17180Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate 17210Sstevel@tonic-gate datael_finish_reset(dp); 17220Sstevel@tonic-gate datael_finish_reset(out); 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 17250Sstevel@tonic-gate &response, sizeof (response)); 17260Sstevel@tonic-gate 17270Sstevel@tonic-gate if (r < 0) 17280Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 17310Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 17320Sstevel@tonic-gate return (0); 17330Sstevel@tonic-gate } 17340Sstevel@tonic-gate 17350Sstevel@tonic-gate static int 17360Sstevel@tonic-gate datael_get_child(const scf_datael_t *dp, const char *name, uint32_t type, 17370Sstevel@tonic-gate scf_datael_t *out, boolean_t composed) 17380Sstevel@tonic-gate { 17390Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 17400Sstevel@tonic-gate uint32_t held = 0; 17410Sstevel@tonic-gate int ret; 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate scf_iter_t *iter = NULL; 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate if (composed) 17460Sstevel@tonic-gate iter = HANDLE_HOLD_ITER(h); 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate if (out == NULL) { 17490Sstevel@tonic-gate switch (type) { 17500Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SERVICE: 17510Sstevel@tonic-gate out = &HANDLE_HOLD_SERVICE(h)->rd_d; 17520Sstevel@tonic-gate held = RH_HOLD_SERVICE; 17530Sstevel@tonic-gate break; 17540Sstevel@tonic-gate 17550Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_INSTANCE: 17560Sstevel@tonic-gate out = &HANDLE_HOLD_INSTANCE(h)->rd_d; 17570Sstevel@tonic-gate held = RH_HOLD_INSTANCE; 17580Sstevel@tonic-gate break; 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SNAPSHOT: 17610Sstevel@tonic-gate out = &HANDLE_HOLD_SNAPSHOT(h)->rd_d; 17620Sstevel@tonic-gate held = RH_HOLD_SNAPSHOT; 17630Sstevel@tonic-gate break; 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SNAPLEVEL: 17660Sstevel@tonic-gate out = &HANDLE_HOLD_SNAPLVL(h)->rd_d; 17670Sstevel@tonic-gate held = RH_HOLD_SNAPLVL; 17680Sstevel@tonic-gate break; 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_PROPERTYGRP: 17710Sstevel@tonic-gate out = &HANDLE_HOLD_PG(h)->rd_d; 17720Sstevel@tonic-gate held = RH_HOLD_PG; 17730Sstevel@tonic-gate break; 17740Sstevel@tonic-gate 17750Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_PROPERTY: 17760Sstevel@tonic-gate out = &HANDLE_HOLD_PROPERTY(h)->rd_d; 17770Sstevel@tonic-gate held = RH_HOLD_PROPERTY; 17780Sstevel@tonic-gate break; 17790Sstevel@tonic-gate 17800Sstevel@tonic-gate default: 17810Sstevel@tonic-gate assert(0); 17820Sstevel@tonic-gate abort(); 17830Sstevel@tonic-gate } 17840Sstevel@tonic-gate } 17850Sstevel@tonic-gate 17860Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 17870Sstevel@tonic-gate if (composed) 17880Sstevel@tonic-gate ret = datael_get_child_composed_locked(dp, name, type, out, 17890Sstevel@tonic-gate iter); 17900Sstevel@tonic-gate else 17910Sstevel@tonic-gate ret = datael_get_child_locked(dp, name, type, out); 17920Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate if (composed) 17950Sstevel@tonic-gate HANDLE_RELE_ITER(h); 17960Sstevel@tonic-gate 17970Sstevel@tonic-gate if (held) 17980Sstevel@tonic-gate handle_rele_subhandles(h, held); 17990Sstevel@tonic-gate 18000Sstevel@tonic-gate return (ret); 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate /* 18040Sstevel@tonic-gate * Fails with 18050Sstevel@tonic-gate * _HANDLE_MISMATCH 18060Sstevel@tonic-gate * _INVALID_ARGUMENT - name is too long 18070Sstevel@tonic-gate * invalid changeid 18080Sstevel@tonic-gate * name is invalid 18090Sstevel@tonic-gate * cannot create children for dp's type of node 18100Sstevel@tonic-gate * _NOT_BOUND - handle is not bound 18110Sstevel@tonic-gate * _CONNECTION_BROKEN - server is not reachable 18120Sstevel@tonic-gate * _INTERNAL - server response too big 18130Sstevel@tonic-gate * dp or cp has unknown id 18140Sstevel@tonic-gate * type is _PROPERTYGRP 18150Sstevel@tonic-gate * type is invalid 18160Sstevel@tonic-gate * dp cannot have children of type type 18170Sstevel@tonic-gate * database is corrupt 18180Sstevel@tonic-gate * _EXISTS - dp & cp have the same id 18190Sstevel@tonic-gate * _EXISTS - child already exists 18200Sstevel@tonic-gate * _DELETED - dp has been deleted 18210Sstevel@tonic-gate * _NOT_SET - dp is reset 18220Sstevel@tonic-gate * _NO_RESOURCES 18230Sstevel@tonic-gate * _PERMISSION_DENIED 18240Sstevel@tonic-gate * _BACKEND_ACCESS 18250Sstevel@tonic-gate * _BACKEND_READONLY 18260Sstevel@tonic-gate */ 18270Sstevel@tonic-gate static int 18280Sstevel@tonic-gate datael_add_child(const scf_datael_t *dp, const char *name, uint32_t type, 18290Sstevel@tonic-gate scf_datael_t *cp) 18300Sstevel@tonic-gate { 18310Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 18320Sstevel@tonic-gate 18330Sstevel@tonic-gate struct rep_protocol_entity_create_child request; 18340Sstevel@tonic-gate struct rep_protocol_response response; 18350Sstevel@tonic-gate ssize_t r; 18360Sstevel@tonic-gate uint32_t held = 0; 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate if (cp == NULL) { 18390Sstevel@tonic-gate switch (type) { 18400Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SCOPE: 18410Sstevel@tonic-gate cp = &HANDLE_HOLD_SCOPE(h)->rd_d; 18420Sstevel@tonic-gate held = RH_HOLD_SCOPE; 18430Sstevel@tonic-gate break; 18440Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SERVICE: 18450Sstevel@tonic-gate cp = &HANDLE_HOLD_SERVICE(h)->rd_d; 18460Sstevel@tonic-gate held = RH_HOLD_SERVICE; 18470Sstevel@tonic-gate break; 18480Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_INSTANCE: 18490Sstevel@tonic-gate cp = &HANDLE_HOLD_INSTANCE(h)->rd_d; 18500Sstevel@tonic-gate held = RH_HOLD_INSTANCE; 18510Sstevel@tonic-gate break; 18520Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SNAPSHOT: 18530Sstevel@tonic-gate default: 18540Sstevel@tonic-gate assert(0); 18550Sstevel@tonic-gate abort(); 18560Sstevel@tonic-gate } 18570Sstevel@tonic-gate assert(h == cp->rd_handle); 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate } else if (h != cp->rd_handle) { 18600Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 18610Sstevel@tonic-gate } 18620Sstevel@tonic-gate 18630Sstevel@tonic-gate if (strlcpy(request.rpr_name, name, sizeof (request.rpr_name)) >= 18640Sstevel@tonic-gate sizeof (request.rpr_name)) { 18650Sstevel@tonic-gate r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 18660Sstevel@tonic-gate goto err; 18670Sstevel@tonic-gate } 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 18700Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_CREATE_CHILD; 18710Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 18720Sstevel@tonic-gate request.rpr_childtype = type; 18730Sstevel@tonic-gate request.rpr_childid = cp->rd_entity; 18740Sstevel@tonic-gate 18750Sstevel@tonic-gate datael_finish_reset(dp); 1876407Sjwadams request.rpr_changeid = handle_next_changeid(h); 18770Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 18780Sstevel@tonic-gate &response, sizeof (response)); 18790Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate if (held) 18820Sstevel@tonic-gate handle_rele_subhandles(h, held); 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate if (r < 0) 18850Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 18860Sstevel@tonic-gate 18870Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 18880Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate return (SCF_SUCCESS); 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate err: 18930Sstevel@tonic-gate if (held) 18940Sstevel@tonic-gate handle_rele_subhandles(h, held); 18950Sstevel@tonic-gate return (r); 18960Sstevel@tonic-gate } 18970Sstevel@tonic-gate 18980Sstevel@tonic-gate static int 18990Sstevel@tonic-gate datael_add_pg(const scf_datael_t *dp, const char *name, const char *type, 19000Sstevel@tonic-gate uint32_t flags, scf_datael_t *cp) 19010Sstevel@tonic-gate { 19020Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate struct rep_protocol_entity_create_pg request; 19050Sstevel@tonic-gate struct rep_protocol_response response; 19060Sstevel@tonic-gate ssize_t r; 19070Sstevel@tonic-gate 19080Sstevel@tonic-gate int holding_els = 0; 19090Sstevel@tonic-gate 19100Sstevel@tonic-gate if (cp == NULL) { 19110Sstevel@tonic-gate holding_els = 1; 19120Sstevel@tonic-gate cp = &HANDLE_HOLD_PG(h)->rd_d; 19130Sstevel@tonic-gate assert(h == cp->rd_handle); 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate } else if (h != cp->rd_handle) { 19160Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 19170Sstevel@tonic-gate } 19180Sstevel@tonic-gate 19190Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_CREATE_PG; 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate if (name == NULL || strlcpy(request.rpr_name, name, 19220Sstevel@tonic-gate sizeof (request.rpr_name)) > sizeof (request.rpr_name)) { 19230Sstevel@tonic-gate r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 19240Sstevel@tonic-gate goto err; 19250Sstevel@tonic-gate } 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate if (type == NULL || strlcpy(request.rpr_type, type, 19280Sstevel@tonic-gate sizeof (request.rpr_type)) > sizeof (request.rpr_type)) { 19290Sstevel@tonic-gate r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 19300Sstevel@tonic-gate goto err; 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate 19330Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 19340Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 19350Sstevel@tonic-gate request.rpr_childid = cp->rd_entity; 19360Sstevel@tonic-gate request.rpr_flags = flags; 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate datael_finish_reset(dp); 19390Sstevel@tonic-gate datael_finish_reset(cp); 1940407Sjwadams request.rpr_changeid = handle_next_changeid(h); 19410Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 19420Sstevel@tonic-gate &response, sizeof (response)); 19430Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate if (holding_els) 19460Sstevel@tonic-gate HANDLE_RELE_PG(h); 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate if (r < 0) 19490Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 19500Sstevel@tonic-gate 19510Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 19520Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate return (SCF_SUCCESS); 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate err: 19570Sstevel@tonic-gate if (holding_els) 19580Sstevel@tonic-gate HANDLE_RELE_PG(h); 19590Sstevel@tonic-gate return (r); 19600Sstevel@tonic-gate } 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate static int 19630Sstevel@tonic-gate datael_delete(const scf_datael_t *dp) 19640Sstevel@tonic-gate { 19650Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 19660Sstevel@tonic-gate 19670Sstevel@tonic-gate struct rep_protocol_entity_delete request; 19680Sstevel@tonic-gate struct rep_protocol_response response; 19690Sstevel@tonic-gate ssize_t r; 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 19720Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_DELETE; 19730Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate datael_finish_reset(dp); 1976407Sjwadams request.rpr_changeid = handle_next_changeid(h); 19770Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 19780Sstevel@tonic-gate &response, sizeof (response)); 19790Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 19800Sstevel@tonic-gate 19810Sstevel@tonic-gate if (r < 0) 19820Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 19830Sstevel@tonic-gate 19840Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 19850Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 19860Sstevel@tonic-gate 19870Sstevel@tonic-gate return (SCF_SUCCESS); 19880Sstevel@tonic-gate } 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate /* 19910Sstevel@tonic-gate * Fails with 19920Sstevel@tonic-gate * _INVALID_ARGUMENT - h is NULL 19930Sstevel@tonic-gate * _NO_MEMORY 19940Sstevel@tonic-gate * _HANDLE_DESTROYED - h has been destroyed 19950Sstevel@tonic-gate * _INTERNAL - server response too big 19960Sstevel@tonic-gate * iter already exists 19970Sstevel@tonic-gate * _NO_RESOURCES 19980Sstevel@tonic-gate */ 19990Sstevel@tonic-gate scf_iter_t * 20000Sstevel@tonic-gate scf_iter_create(scf_handle_t *h) 20010Sstevel@tonic-gate { 20020Sstevel@tonic-gate scf_iter_t *iter; 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate if (h == NULL) { 20050Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 20060Sstevel@tonic-gate return (NULL); 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate iter = uu_zalloc(sizeof (*iter)); 20100Sstevel@tonic-gate if (iter == NULL) { 20110Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 20120Sstevel@tonic-gate return (NULL); 20130Sstevel@tonic-gate } 20140Sstevel@tonic-gate 20150Sstevel@tonic-gate uu_list_node_init(iter, &iter->iter_node, iter_pool); 20160Sstevel@tonic-gate iter->iter_handle = h; 20170Sstevel@tonic-gate iter->iter_sequence = 1; 20180Sstevel@tonic-gate iter->iter_type = REP_PROTOCOL_ENTITY_NONE; 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 20210Sstevel@tonic-gate iter->iter_id = handle_alloc_iterid(h); 2022407Sjwadams if (iter->iter_id == 0) { 2023407Sjwadams (void) pthread_mutex_unlock(&h->rh_lock); 2024407Sjwadams uu_list_node_fini(iter, &iter->iter_node, iter_pool); 2025407Sjwadams (void) scf_set_error(SCF_ERROR_NO_MEMORY); 2026407Sjwadams return (NULL); 2027407Sjwadams } 20280Sstevel@tonic-gate if (iter_attach(iter) == -1) { 20290Sstevel@tonic-gate uu_list_node_fini(iter, &iter->iter_node, iter_pool); 20300Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 20310Sstevel@tonic-gate uu_free(iter); 20320Sstevel@tonic-gate return (NULL); 20330Sstevel@tonic-gate } 20340Sstevel@tonic-gate (void) uu_list_insert_before(h->rh_iters, NULL, iter); 20350Sstevel@tonic-gate h->rh_extrefs++; 20360Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 20370Sstevel@tonic-gate return (iter); 20380Sstevel@tonic-gate } 20390Sstevel@tonic-gate 20400Sstevel@tonic-gate scf_handle_t * 20410Sstevel@tonic-gate scf_iter_handle(const scf_iter_t *iter) 20420Sstevel@tonic-gate { 20430Sstevel@tonic-gate return (handle_get(iter->iter_handle)); 20440Sstevel@tonic-gate } 20450Sstevel@tonic-gate 20460Sstevel@tonic-gate static void 20470Sstevel@tonic-gate scf_iter_reset_locked(scf_iter_t *iter) 20480Sstevel@tonic-gate { 20490Sstevel@tonic-gate struct rep_protocol_iter_request request; 20500Sstevel@tonic-gate struct rep_protocol_response response; 20510Sstevel@tonic-gate 20520Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_RESET; 20530Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 20540Sstevel@tonic-gate 20550Sstevel@tonic-gate assert(MUTEX_HELD(&iter->iter_handle->rh_lock)); 20560Sstevel@tonic-gate 20570Sstevel@tonic-gate (void) make_door_call(iter->iter_handle, 20580Sstevel@tonic-gate &request, sizeof (request), &response, sizeof (response)); 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate iter->iter_type = REP_PROTOCOL_ENTITY_NONE; 20610Sstevel@tonic-gate iter->iter_sequence = 1; 20620Sstevel@tonic-gate } 20630Sstevel@tonic-gate 20640Sstevel@tonic-gate void 20650Sstevel@tonic-gate scf_iter_reset(scf_iter_t *iter) 20660Sstevel@tonic-gate { 20670Sstevel@tonic-gate (void) pthread_mutex_lock(&iter->iter_handle->rh_lock); 20680Sstevel@tonic-gate scf_iter_reset_locked(iter); 20690Sstevel@tonic-gate (void) pthread_mutex_unlock(&iter->iter_handle->rh_lock); 20700Sstevel@tonic-gate } 20710Sstevel@tonic-gate 20720Sstevel@tonic-gate void 20730Sstevel@tonic-gate scf_iter_destroy(scf_iter_t *iter) 20740Sstevel@tonic-gate { 20750Sstevel@tonic-gate scf_handle_t *handle; 20760Sstevel@tonic-gate 20770Sstevel@tonic-gate struct rep_protocol_iter_request request; 20780Sstevel@tonic-gate struct rep_protocol_response response; 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate if (iter == NULL) 20810Sstevel@tonic-gate return; 20820Sstevel@tonic-gate 20830Sstevel@tonic-gate handle = iter->iter_handle; 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate (void) pthread_mutex_lock(&handle->rh_lock); 20860Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_TEARDOWN; 20870Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate (void) make_door_call(handle, &request, sizeof (request), 20900Sstevel@tonic-gate &response, sizeof (response)); 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate uu_list_remove(handle->rh_iters, iter); 20930Sstevel@tonic-gate --handle->rh_extrefs; 20940Sstevel@tonic-gate handle_unrefed(handle); /* drops h->rh_lock */ 20950Sstevel@tonic-gate iter->iter_handle = NULL; 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate uu_list_node_fini(iter, &iter->iter_node, iter_pool); 20980Sstevel@tonic-gate uu_free(iter); 20990Sstevel@tonic-gate } 21000Sstevel@tonic-gate 21010Sstevel@tonic-gate static int 21020Sstevel@tonic-gate handle_get_local_scope_locked(scf_handle_t *handle, scf_scope_t *out) 21030Sstevel@tonic-gate { 21040Sstevel@tonic-gate struct rep_protocol_entity_get request; 21050Sstevel@tonic-gate struct rep_protocol_name_response response; 21060Sstevel@tonic-gate ssize_t r; 21070Sstevel@tonic-gate 21080Sstevel@tonic-gate assert(MUTEX_HELD(&handle->rh_lock)); 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate if (handle != out->rd_d.rd_handle) 21110Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 21120Sstevel@tonic-gate 21130Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_GET; 21140Sstevel@tonic-gate request.rpr_entityid = out->rd_d.rd_entity; 21150Sstevel@tonic-gate request.rpr_object = RP_ENTITY_GET_MOST_LOCAL_SCOPE; 21160Sstevel@tonic-gate 21170Sstevel@tonic-gate datael_finish_reset(&out->rd_d); 21180Sstevel@tonic-gate r = make_door_call(handle, &request, sizeof (request), 21190Sstevel@tonic-gate &response, sizeof (response)); 21200Sstevel@tonic-gate 21210Sstevel@tonic-gate if (r < 0) 21220Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 21230Sstevel@tonic-gate 21240Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 21250Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 21260Sstevel@tonic-gate 21270Sstevel@tonic-gate return (SCF_SUCCESS); 21280Sstevel@tonic-gate } 21290Sstevel@tonic-gate 21300Sstevel@tonic-gate int 21310Sstevel@tonic-gate scf_iter_handle_scopes(scf_iter_t *iter, const scf_handle_t *handle) 21320Sstevel@tonic-gate { 21330Sstevel@tonic-gate scf_handle_t *h = iter->iter_handle; 21340Sstevel@tonic-gate if (h != handle) 21350Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 21360Sstevel@tonic-gate 21370Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 21380Sstevel@tonic-gate scf_iter_reset_locked(iter); 21390Sstevel@tonic-gate 21400Sstevel@tonic-gate if (!handle_is_bound(h)) { 21410Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 21420Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_BOUND)); 21430Sstevel@tonic-gate } 21440Sstevel@tonic-gate 2145407Sjwadams if (!handle_has_server_locked(h)) { 21460Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 21470Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN)); 21480Sstevel@tonic-gate } 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate iter->iter_type = REP_PROTOCOL_ENTITY_SCOPE; 21510Sstevel@tonic-gate iter->iter_sequence = 1; 21520Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 21530Sstevel@tonic-gate return (0); 21540Sstevel@tonic-gate } 21550Sstevel@tonic-gate 21560Sstevel@tonic-gate int 21570Sstevel@tonic-gate scf_iter_next_scope(scf_iter_t *iter, scf_scope_t *out) 21580Sstevel@tonic-gate { 21590Sstevel@tonic-gate int ret; 21600Sstevel@tonic-gate scf_handle_t *h = iter->iter_handle; 21610Sstevel@tonic-gate 21620Sstevel@tonic-gate if (h != out->rd_d.rd_handle) 21630Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 21640Sstevel@tonic-gate 21650Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 21660Sstevel@tonic-gate if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE) { 21670Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 21680Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET)); 21690Sstevel@tonic-gate } 21700Sstevel@tonic-gate if (iter->iter_type != REP_PROTOCOL_ENTITY_SCOPE) { 21710Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 21720Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 21730Sstevel@tonic-gate } 21740Sstevel@tonic-gate if (iter->iter_sequence == 1) { 21750Sstevel@tonic-gate if ((ret = handle_get_local_scope_locked(h, out)) == 21760Sstevel@tonic-gate SCF_SUCCESS) { 21770Sstevel@tonic-gate iter->iter_sequence++; 21780Sstevel@tonic-gate ret = 1; 21790Sstevel@tonic-gate } 21800Sstevel@tonic-gate } else { 21810Sstevel@tonic-gate datael_reset_locked(&out->rd_d); 21820Sstevel@tonic-gate ret = 0; 21830Sstevel@tonic-gate } 21840Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 21850Sstevel@tonic-gate return (ret); 21860Sstevel@tonic-gate } 21870Sstevel@tonic-gate 21880Sstevel@tonic-gate int 21890Sstevel@tonic-gate scf_handle_get_scope(scf_handle_t *h, const char *name, scf_scope_t *out) 21900Sstevel@tonic-gate { 21910Sstevel@tonic-gate int ret; 21920Sstevel@tonic-gate 21930Sstevel@tonic-gate if (h != out->rd_d.rd_handle) 21940Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 21970Sstevel@tonic-gate if (strcmp(name, SCF_SCOPE_LOCAL) == 0) { 21980Sstevel@tonic-gate ret = handle_get_local_scope_locked(h, out); 21990Sstevel@tonic-gate } else { 22000Sstevel@tonic-gate datael_reset_locked(&out->rd_d); 22010Sstevel@tonic-gate if (uu_check_name(name, 0) == -1) 22020Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 22030Sstevel@tonic-gate else 22040Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_NOT_FOUND); 22050Sstevel@tonic-gate } 22060Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 22070Sstevel@tonic-gate return (ret); 22080Sstevel@tonic-gate } 22090Sstevel@tonic-gate 22100Sstevel@tonic-gate static int 22110Sstevel@tonic-gate datael_setup_iter(scf_iter_t *iter, const scf_datael_t *dp, uint32_t res_type, 22120Sstevel@tonic-gate boolean_t composed) 22130Sstevel@tonic-gate { 22140Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 22150Sstevel@tonic-gate 22160Sstevel@tonic-gate struct rep_protocol_iter_start request; 22170Sstevel@tonic-gate struct rep_protocol_response response; 22180Sstevel@tonic-gate 22190Sstevel@tonic-gate ssize_t r; 22200Sstevel@tonic-gate 22210Sstevel@tonic-gate if (h != iter->iter_handle) 22220Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 22230Sstevel@tonic-gate 22240Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 22250Sstevel@tonic-gate scf_iter_reset_locked(iter); 22260Sstevel@tonic-gate iter->iter_type = res_type; 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_START; 22290Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 22300Sstevel@tonic-gate request.rpr_entity = dp->rd_entity; 22310Sstevel@tonic-gate request.rpr_itertype = res_type; 22320Sstevel@tonic-gate request.rpr_flags = RP_ITER_START_ALL | 22330Sstevel@tonic-gate (composed ? RP_ITER_START_COMPOSED : 0); 22340Sstevel@tonic-gate request.rpr_pattern[0] = 0; 22350Sstevel@tonic-gate 22360Sstevel@tonic-gate datael_finish_reset(dp); 22370Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 22380Sstevel@tonic-gate &response, sizeof (response)); 22390Sstevel@tonic-gate 22400Sstevel@tonic-gate if (r < 0) { 22410Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 22420Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 22430Sstevel@tonic-gate } 22440Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 22450Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 22460Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 22470Sstevel@tonic-gate } 22480Sstevel@tonic-gate iter->iter_sequence++; 22490Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 22500Sstevel@tonic-gate return (SCF_SUCCESS); 22510Sstevel@tonic-gate } 22520Sstevel@tonic-gate 22530Sstevel@tonic-gate static int 22540Sstevel@tonic-gate datael_setup_iter_pgtyped(scf_iter_t *iter, const scf_datael_t *dp, 22550Sstevel@tonic-gate const char *pgtype, boolean_t composed) 22560Sstevel@tonic-gate { 22570Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 22580Sstevel@tonic-gate 22590Sstevel@tonic-gate struct rep_protocol_iter_start request; 22600Sstevel@tonic-gate struct rep_protocol_response response; 22610Sstevel@tonic-gate 22620Sstevel@tonic-gate ssize_t r; 22630Sstevel@tonic-gate 22640Sstevel@tonic-gate if (h != iter->iter_handle) 22650Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 22660Sstevel@tonic-gate 22670Sstevel@tonic-gate if (pgtype == NULL || strlcpy(request.rpr_pattern, pgtype, 22680Sstevel@tonic-gate sizeof (request.rpr_pattern)) >= sizeof (request.rpr_pattern)) { 22690Sstevel@tonic-gate scf_iter_reset(iter); 22700Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate 22730Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 22740Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_START; 22750Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 22760Sstevel@tonic-gate request.rpr_entity = dp->rd_entity; 22770Sstevel@tonic-gate request.rpr_itertype = REP_PROTOCOL_ENTITY_PROPERTYGRP; 22780Sstevel@tonic-gate request.rpr_flags = RP_ITER_START_PGTYPE | 22790Sstevel@tonic-gate (composed ? RP_ITER_START_COMPOSED : 0); 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate datael_finish_reset(dp); 22820Sstevel@tonic-gate scf_iter_reset_locked(iter); 22830Sstevel@tonic-gate iter->iter_type = REP_PROTOCOL_ENTITY_PROPERTYGRP; 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 22860Sstevel@tonic-gate &response, sizeof (response)); 22870Sstevel@tonic-gate 22880Sstevel@tonic-gate if (r < 0) { 22890Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 22920Sstevel@tonic-gate } 22930Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 22940Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 22950Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 22960Sstevel@tonic-gate } 22970Sstevel@tonic-gate iter->iter_sequence++; 22980Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 22990Sstevel@tonic-gate return (SCF_SUCCESS); 23000Sstevel@tonic-gate } 23010Sstevel@tonic-gate 23020Sstevel@tonic-gate static int 23030Sstevel@tonic-gate datael_iter_next(scf_iter_t *iter, scf_datael_t *out) 23040Sstevel@tonic-gate { 23050Sstevel@tonic-gate scf_handle_t *h = iter->iter_handle; 23060Sstevel@tonic-gate 23070Sstevel@tonic-gate struct rep_protocol_iter_read request; 23080Sstevel@tonic-gate struct rep_protocol_response response; 23090Sstevel@tonic-gate ssize_t r; 23100Sstevel@tonic-gate 23110Sstevel@tonic-gate if (h != out->rd_handle) 23120Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 23130Sstevel@tonic-gate 23140Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 23150Sstevel@tonic-gate if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE || 23160Sstevel@tonic-gate iter->iter_sequence == 1) { 23170Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 23180Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET)); 23190Sstevel@tonic-gate } 23200Sstevel@tonic-gate 23210Sstevel@tonic-gate if (out->rd_type != iter->iter_type) { 23220Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 23230Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 23240Sstevel@tonic-gate } 23250Sstevel@tonic-gate 23260Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_READ; 23270Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 23280Sstevel@tonic-gate request.rpr_sequence = iter->iter_sequence; 23290Sstevel@tonic-gate request.rpr_entityid = out->rd_entity; 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate datael_finish_reset(out); 23320Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 23330Sstevel@tonic-gate &response, sizeof (response)); 23340Sstevel@tonic-gate 23350Sstevel@tonic-gate if (r < 0) { 23360Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 23370Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 23380Sstevel@tonic-gate } 23390Sstevel@tonic-gate 23400Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_DONE) { 23410Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 23420Sstevel@tonic-gate return (0); 23430Sstevel@tonic-gate } 23440Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 23450Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 23460Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 23470Sstevel@tonic-gate } 23480Sstevel@tonic-gate iter->iter_sequence++; 23490Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 23500Sstevel@tonic-gate 23510Sstevel@tonic-gate return (1); 23520Sstevel@tonic-gate } 23530Sstevel@tonic-gate 23540Sstevel@tonic-gate int 23550Sstevel@tonic-gate scf_iter_scope_services(scf_iter_t *iter, const scf_scope_t *s) 23560Sstevel@tonic-gate { 23570Sstevel@tonic-gate return (datael_setup_iter(iter, &s->rd_d, 23580Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SERVICE, 0)); 23590Sstevel@tonic-gate } 23600Sstevel@tonic-gate 23610Sstevel@tonic-gate int 23620Sstevel@tonic-gate scf_iter_next_service(scf_iter_t *iter, scf_service_t *out) 23630Sstevel@tonic-gate { 23640Sstevel@tonic-gate return (datael_iter_next(iter, &out->rd_d)); 23650Sstevel@tonic-gate } 23660Sstevel@tonic-gate 23670Sstevel@tonic-gate int 23680Sstevel@tonic-gate scf_iter_service_instances(scf_iter_t *iter, const scf_service_t *svc) 23690Sstevel@tonic-gate { 23700Sstevel@tonic-gate return (datael_setup_iter(iter, &svc->rd_d, 23710Sstevel@tonic-gate REP_PROTOCOL_ENTITY_INSTANCE, 0)); 23720Sstevel@tonic-gate } 23730Sstevel@tonic-gate 23740Sstevel@tonic-gate int 23750Sstevel@tonic-gate scf_iter_next_instance(scf_iter_t *iter, scf_instance_t *out) 23760Sstevel@tonic-gate { 23770Sstevel@tonic-gate return (datael_iter_next(iter, &out->rd_d)); 23780Sstevel@tonic-gate } 23790Sstevel@tonic-gate 23800Sstevel@tonic-gate int 23810Sstevel@tonic-gate scf_iter_service_pgs(scf_iter_t *iter, const scf_service_t *svc) 23820Sstevel@tonic-gate { 23830Sstevel@tonic-gate return (datael_setup_iter(iter, &svc->rd_d, 23840Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, 0)); 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate int 23880Sstevel@tonic-gate scf_iter_service_pgs_typed(scf_iter_t *iter, const scf_service_t *svc, 23890Sstevel@tonic-gate const char *type) 23900Sstevel@tonic-gate { 23910Sstevel@tonic-gate return (datael_setup_iter_pgtyped(iter, &svc->rd_d, type, 0)); 23920Sstevel@tonic-gate } 23930Sstevel@tonic-gate 23940Sstevel@tonic-gate int 23950Sstevel@tonic-gate scf_iter_instance_snapshots(scf_iter_t *iter, const scf_instance_t *inst) 23960Sstevel@tonic-gate { 23970Sstevel@tonic-gate return (datael_setup_iter(iter, &inst->rd_d, 23980Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SNAPSHOT, 0)); 23990Sstevel@tonic-gate } 24000Sstevel@tonic-gate 24010Sstevel@tonic-gate int 24020Sstevel@tonic-gate scf_iter_next_snapshot(scf_iter_t *iter, scf_snapshot_t *out) 24030Sstevel@tonic-gate { 24040Sstevel@tonic-gate return (datael_iter_next(iter, &out->rd_d)); 24050Sstevel@tonic-gate } 24060Sstevel@tonic-gate 24070Sstevel@tonic-gate int 24080Sstevel@tonic-gate scf_iter_instance_pgs(scf_iter_t *iter, const scf_instance_t *inst) 24090Sstevel@tonic-gate { 24100Sstevel@tonic-gate return (datael_setup_iter(iter, &inst->rd_d, 24110Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, 0)); 24120Sstevel@tonic-gate } 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate int 24150Sstevel@tonic-gate scf_iter_instance_pgs_typed(scf_iter_t *iter, const scf_instance_t *inst, 24160Sstevel@tonic-gate const char *type) 24170Sstevel@tonic-gate { 24180Sstevel@tonic-gate return (datael_setup_iter_pgtyped(iter, &inst->rd_d, type, 0)); 24190Sstevel@tonic-gate } 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate int 24220Sstevel@tonic-gate scf_iter_instance_pgs_composed(scf_iter_t *iter, const scf_instance_t *inst, 24230Sstevel@tonic-gate const scf_snapshot_t *snap) 24240Sstevel@tonic-gate { 24250Sstevel@tonic-gate if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle) 24260Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 24270Sstevel@tonic-gate 24280Sstevel@tonic-gate return (datael_setup_iter(iter, snap ? &snap->rd_d : &inst->rd_d, 24290Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, 1)); 24300Sstevel@tonic-gate } 24310Sstevel@tonic-gate 24320Sstevel@tonic-gate int 24330Sstevel@tonic-gate scf_iter_instance_pgs_typed_composed(scf_iter_t *iter, 24340Sstevel@tonic-gate const scf_instance_t *inst, const scf_snapshot_t *snap, const char *type) 24350Sstevel@tonic-gate { 24360Sstevel@tonic-gate if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle) 24370Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 24380Sstevel@tonic-gate 24390Sstevel@tonic-gate return (datael_setup_iter_pgtyped(iter, 24400Sstevel@tonic-gate snap ? &snap->rd_d : &inst->rd_d, type, 1)); 24410Sstevel@tonic-gate } 24420Sstevel@tonic-gate 24430Sstevel@tonic-gate int 24440Sstevel@tonic-gate scf_iter_snaplevel_pgs(scf_iter_t *iter, const scf_snaplevel_t *inst) 24450Sstevel@tonic-gate { 24460Sstevel@tonic-gate return (datael_setup_iter(iter, &inst->rd_d, 24470Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, 0)); 24480Sstevel@tonic-gate } 24490Sstevel@tonic-gate 24500Sstevel@tonic-gate int 24510Sstevel@tonic-gate scf_iter_snaplevel_pgs_typed(scf_iter_t *iter, const scf_snaplevel_t *inst, 24520Sstevel@tonic-gate const char *type) 24530Sstevel@tonic-gate { 24540Sstevel@tonic-gate return (datael_setup_iter_pgtyped(iter, &inst->rd_d, type, 0)); 24550Sstevel@tonic-gate } 24560Sstevel@tonic-gate 24570Sstevel@tonic-gate int 24580Sstevel@tonic-gate scf_iter_next_pg(scf_iter_t *iter, scf_propertygroup_t *out) 24590Sstevel@tonic-gate { 24600Sstevel@tonic-gate return (datael_iter_next(iter, &out->rd_d)); 24610Sstevel@tonic-gate } 24620Sstevel@tonic-gate 24630Sstevel@tonic-gate int 24640Sstevel@tonic-gate scf_iter_pg_properties(scf_iter_t *iter, const scf_propertygroup_t *pg) 24650Sstevel@tonic-gate { 24660Sstevel@tonic-gate return (datael_setup_iter(iter, &pg->rd_d, 24670Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTY, 0)); 24680Sstevel@tonic-gate } 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate int 24710Sstevel@tonic-gate scf_iter_next_property(scf_iter_t *iter, scf_property_t *out) 24720Sstevel@tonic-gate { 24730Sstevel@tonic-gate return (datael_iter_next(iter, &out->rd_d)); 24740Sstevel@tonic-gate } 24750Sstevel@tonic-gate 24760Sstevel@tonic-gate /* 24770Sstevel@tonic-gate * Fails with 24780Sstevel@tonic-gate * _INVALID_ARGUMENT - handle is NULL 24790Sstevel@tonic-gate * _INTERNAL - server response too big 24800Sstevel@tonic-gate * entity already set up with different type 24810Sstevel@tonic-gate * _NO_RESOURCES 24820Sstevel@tonic-gate * _NO_MEMORY 24830Sstevel@tonic-gate */ 24840Sstevel@tonic-gate scf_scope_t * 24850Sstevel@tonic-gate scf_scope_create(scf_handle_t *handle) 24860Sstevel@tonic-gate { 24870Sstevel@tonic-gate scf_scope_t *ret; 24880Sstevel@tonic-gate 24890Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 24900Sstevel@tonic-gate if (ret != NULL) { 24910Sstevel@tonic-gate if (datael_init(&ret->rd_d, handle, 24920Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SCOPE) == -1) { 24930Sstevel@tonic-gate uu_free(ret); 24940Sstevel@tonic-gate return (NULL); 24950Sstevel@tonic-gate } 24960Sstevel@tonic-gate } else { 24970Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 24980Sstevel@tonic-gate } 24990Sstevel@tonic-gate 25000Sstevel@tonic-gate return (ret); 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate 25030Sstevel@tonic-gate scf_handle_t * 25040Sstevel@tonic-gate scf_scope_handle(const scf_scope_t *val) 25050Sstevel@tonic-gate { 25060Sstevel@tonic-gate return (datael_handle(&val->rd_d)); 25070Sstevel@tonic-gate } 25080Sstevel@tonic-gate 25090Sstevel@tonic-gate void 25100Sstevel@tonic-gate scf_scope_destroy(scf_scope_t *val) 25110Sstevel@tonic-gate { 25120Sstevel@tonic-gate if (val == NULL) 25130Sstevel@tonic-gate return; 25140Sstevel@tonic-gate 25150Sstevel@tonic-gate datael_destroy(&val->rd_d); 25160Sstevel@tonic-gate uu_free(val); 25170Sstevel@tonic-gate } 25180Sstevel@tonic-gate 25190Sstevel@tonic-gate ssize_t 25200Sstevel@tonic-gate scf_scope_get_name(const scf_scope_t *rep, char *out, size_t len) 25210Sstevel@tonic-gate { 25220Sstevel@tonic-gate return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME)); 25230Sstevel@tonic-gate } 25240Sstevel@tonic-gate 25250Sstevel@tonic-gate /*ARGSUSED*/ 25260Sstevel@tonic-gate int 25270Sstevel@tonic-gate scf_scope_get_parent(const scf_scope_t *child, scf_scope_t *parent) 25280Sstevel@tonic-gate { 25290Sstevel@tonic-gate char name[1]; 25300Sstevel@tonic-gate 25310Sstevel@tonic-gate /* fake up the side-effects */ 25320Sstevel@tonic-gate datael_reset(&parent->rd_d); 25330Sstevel@tonic-gate if (scf_scope_get_name(child, name, sizeof (name)) < 0) 25340Sstevel@tonic-gate return (-1); 25350Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_FOUND)); 25360Sstevel@tonic-gate } 25370Sstevel@tonic-gate 25380Sstevel@tonic-gate /* 25390Sstevel@tonic-gate * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL 25400Sstevel@tonic-gate * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY. 25410Sstevel@tonic-gate */ 25420Sstevel@tonic-gate scf_service_t * 25430Sstevel@tonic-gate scf_service_create(scf_handle_t *handle) 25440Sstevel@tonic-gate { 25450Sstevel@tonic-gate scf_service_t *ret; 25460Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 25470Sstevel@tonic-gate if (ret != NULL) { 25480Sstevel@tonic-gate if (datael_init(&ret->rd_d, handle, 25490Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SERVICE) == -1) { 25500Sstevel@tonic-gate uu_free(ret); 25510Sstevel@tonic-gate return (NULL); 25520Sstevel@tonic-gate } 25530Sstevel@tonic-gate } else { 25540Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 25550Sstevel@tonic-gate } 25560Sstevel@tonic-gate 25570Sstevel@tonic-gate return (ret); 25580Sstevel@tonic-gate } 25590Sstevel@tonic-gate 25600Sstevel@tonic-gate int 25610Sstevel@tonic-gate scf_scope_add_service(const scf_scope_t *scope, const char *name, 25620Sstevel@tonic-gate scf_service_t *svc) 25630Sstevel@tonic-gate { 25640Sstevel@tonic-gate return (datael_add_child(&scope->rd_d, name, 25650Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SERVICE, (svc != NULL)? &svc->rd_d : NULL)); 25660Sstevel@tonic-gate } 25670Sstevel@tonic-gate 25680Sstevel@tonic-gate int 25690Sstevel@tonic-gate scf_scope_get_service(const scf_scope_t *s, const char *name, 25700Sstevel@tonic-gate scf_service_t *svc) 25710Sstevel@tonic-gate { 25720Sstevel@tonic-gate return (datael_get_child(&s->rd_d, name, REP_PROTOCOL_ENTITY_SERVICE, 25730Sstevel@tonic-gate svc ? &svc->rd_d : NULL, 0)); 25740Sstevel@tonic-gate } 25750Sstevel@tonic-gate 25760Sstevel@tonic-gate scf_handle_t * 25770Sstevel@tonic-gate scf_service_handle(const scf_service_t *val) 25780Sstevel@tonic-gate { 25790Sstevel@tonic-gate return (datael_handle(&val->rd_d)); 25800Sstevel@tonic-gate } 25810Sstevel@tonic-gate 25820Sstevel@tonic-gate int 25830Sstevel@tonic-gate scf_service_delete(scf_service_t *svc) 25840Sstevel@tonic-gate { 25850Sstevel@tonic-gate return (datael_delete(&svc->rd_d)); 25860Sstevel@tonic-gate } 25870Sstevel@tonic-gate 25880Sstevel@tonic-gate int 25890Sstevel@tonic-gate scf_instance_delete(scf_instance_t *inst) 25900Sstevel@tonic-gate { 25910Sstevel@tonic-gate return (datael_delete(&inst->rd_d)); 25920Sstevel@tonic-gate } 25930Sstevel@tonic-gate 25940Sstevel@tonic-gate int 25950Sstevel@tonic-gate scf_pg_delete(scf_propertygroup_t *pg) 25960Sstevel@tonic-gate { 25970Sstevel@tonic-gate return (datael_delete(&pg->rd_d)); 25980Sstevel@tonic-gate } 25990Sstevel@tonic-gate 26000Sstevel@tonic-gate int 26010Sstevel@tonic-gate _scf_snapshot_delete(scf_snapshot_t *snap) 26020Sstevel@tonic-gate { 26030Sstevel@tonic-gate return (datael_delete(&snap->rd_d)); 26040Sstevel@tonic-gate } 26050Sstevel@tonic-gate 26060Sstevel@tonic-gate int 26070Sstevel@tonic-gate scf_service_add_instance(const scf_service_t *svc, const char *name, 26080Sstevel@tonic-gate scf_instance_t *instance) 26090Sstevel@tonic-gate { 26100Sstevel@tonic-gate return (datael_add_child(&svc->rd_d, name, 26110Sstevel@tonic-gate REP_PROTOCOL_ENTITY_INSTANCE, 26120Sstevel@tonic-gate (instance != NULL)? &instance->rd_d : NULL)); 26130Sstevel@tonic-gate } 26140Sstevel@tonic-gate 26150Sstevel@tonic-gate int 26160Sstevel@tonic-gate scf_service_get_instance(const scf_service_t *svc, const char *name, 26170Sstevel@tonic-gate scf_instance_t *inst) 26180Sstevel@tonic-gate { 26190Sstevel@tonic-gate return (datael_get_child(&svc->rd_d, name, REP_PROTOCOL_ENTITY_INSTANCE, 26200Sstevel@tonic-gate inst ? &inst->rd_d : NULL, 0)); 26210Sstevel@tonic-gate } 26220Sstevel@tonic-gate 26230Sstevel@tonic-gate int 26240Sstevel@tonic-gate scf_service_add_pg(const scf_service_t *svc, const char *name, 26250Sstevel@tonic-gate const char *type, uint32_t flags, scf_propertygroup_t *pg) 26260Sstevel@tonic-gate { 26270Sstevel@tonic-gate return (datael_add_pg(&svc->rd_d, name, type, flags, 26280Sstevel@tonic-gate (pg != NULL)?&pg->rd_d : NULL)); 26290Sstevel@tonic-gate } 26300Sstevel@tonic-gate 26310Sstevel@tonic-gate int 26320Sstevel@tonic-gate scf_service_get_pg(const scf_service_t *svc, const char *name, 26330Sstevel@tonic-gate scf_propertygroup_t *pg) 26340Sstevel@tonic-gate { 26350Sstevel@tonic-gate return (datael_get_child(&svc->rd_d, name, 26360Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0)); 26370Sstevel@tonic-gate } 26380Sstevel@tonic-gate 26390Sstevel@tonic-gate int 26400Sstevel@tonic-gate scf_instance_add_pg(const scf_instance_t *inst, const char *name, 26410Sstevel@tonic-gate const char *type, uint32_t flags, scf_propertygroup_t *pg) 26420Sstevel@tonic-gate { 26430Sstevel@tonic-gate return (datael_add_pg(&inst->rd_d, name, type, flags, 26440Sstevel@tonic-gate (pg != NULL)?&pg->rd_d : NULL)); 26450Sstevel@tonic-gate } 26460Sstevel@tonic-gate 26470Sstevel@tonic-gate int 26480Sstevel@tonic-gate scf_instance_get_snapshot(const scf_instance_t *inst, const char *name, 26490Sstevel@tonic-gate scf_snapshot_t *pg) 26500Sstevel@tonic-gate { 26510Sstevel@tonic-gate return (datael_get_child(&inst->rd_d, name, 26520Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SNAPSHOT, pg ? &pg->rd_d : NULL, 0)); 26530Sstevel@tonic-gate } 26540Sstevel@tonic-gate 26550Sstevel@tonic-gate int 26560Sstevel@tonic-gate scf_instance_get_pg(const scf_instance_t *inst, const char *name, 26570Sstevel@tonic-gate scf_propertygroup_t *pg) 26580Sstevel@tonic-gate { 26590Sstevel@tonic-gate return (datael_get_child(&inst->rd_d, name, 26600Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0)); 26610Sstevel@tonic-gate } 26620Sstevel@tonic-gate 26630Sstevel@tonic-gate int 26640Sstevel@tonic-gate scf_instance_get_pg_composed(const scf_instance_t *inst, 26650Sstevel@tonic-gate const scf_snapshot_t *snap, const char *name, scf_propertygroup_t *pg) 26660Sstevel@tonic-gate { 26670Sstevel@tonic-gate if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle) 26680Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 26690Sstevel@tonic-gate 26700Sstevel@tonic-gate return (datael_get_child(snap ? &snap->rd_d : &inst->rd_d, name, 26710Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 1)); 26720Sstevel@tonic-gate } 26730Sstevel@tonic-gate 26740Sstevel@tonic-gate int 26750Sstevel@tonic-gate scf_pg_get_property(const scf_propertygroup_t *pg, const char *name, 26760Sstevel@tonic-gate scf_property_t *prop) 26770Sstevel@tonic-gate { 26780Sstevel@tonic-gate return (datael_get_child(&pg->rd_d, name, REP_PROTOCOL_ENTITY_PROPERTY, 26790Sstevel@tonic-gate prop ? &prop->rd_d : NULL, 0)); 26800Sstevel@tonic-gate } 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate void 26830Sstevel@tonic-gate scf_service_destroy(scf_service_t *val) 26840Sstevel@tonic-gate { 26850Sstevel@tonic-gate if (val == NULL) 26860Sstevel@tonic-gate return; 26870Sstevel@tonic-gate 26880Sstevel@tonic-gate datael_destroy(&val->rd_d); 26890Sstevel@tonic-gate uu_free(val); 26900Sstevel@tonic-gate } 26910Sstevel@tonic-gate 26920Sstevel@tonic-gate ssize_t 26930Sstevel@tonic-gate scf_service_get_name(const scf_service_t *rep, char *out, size_t len) 26940Sstevel@tonic-gate { 26950Sstevel@tonic-gate return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME)); 26960Sstevel@tonic-gate } 26970Sstevel@tonic-gate 26980Sstevel@tonic-gate /* 26990Sstevel@tonic-gate * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL 27000Sstevel@tonic-gate * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY. 27010Sstevel@tonic-gate */ 27020Sstevel@tonic-gate scf_instance_t * 27030Sstevel@tonic-gate scf_instance_create(scf_handle_t *handle) 27040Sstevel@tonic-gate { 27050Sstevel@tonic-gate scf_instance_t *ret; 27060Sstevel@tonic-gate 27070Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 27080Sstevel@tonic-gate if (ret != NULL) { 27090Sstevel@tonic-gate if (datael_init(&ret->rd_d, handle, 27100Sstevel@tonic-gate REP_PROTOCOL_ENTITY_INSTANCE) == -1) { 27110Sstevel@tonic-gate uu_free(ret); 27120Sstevel@tonic-gate return (NULL); 27130Sstevel@tonic-gate } 27140Sstevel@tonic-gate } else { 27150Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 27160Sstevel@tonic-gate } 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate return (ret); 27190Sstevel@tonic-gate } 27200Sstevel@tonic-gate 27210Sstevel@tonic-gate scf_handle_t * 27220Sstevel@tonic-gate scf_instance_handle(const scf_instance_t *val) 27230Sstevel@tonic-gate { 27240Sstevel@tonic-gate return (datael_handle(&val->rd_d)); 27250Sstevel@tonic-gate } 27260Sstevel@tonic-gate 27270Sstevel@tonic-gate void 27280Sstevel@tonic-gate scf_instance_destroy(scf_instance_t *val) 27290Sstevel@tonic-gate { 27300Sstevel@tonic-gate if (val == NULL) 27310Sstevel@tonic-gate return; 27320Sstevel@tonic-gate 27330Sstevel@tonic-gate datael_destroy(&val->rd_d); 27340Sstevel@tonic-gate uu_free(val); 27350Sstevel@tonic-gate } 27360Sstevel@tonic-gate 27370Sstevel@tonic-gate ssize_t 27380Sstevel@tonic-gate scf_instance_get_name(const scf_instance_t *rep, char *out, size_t len) 27390Sstevel@tonic-gate { 27400Sstevel@tonic-gate return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME)); 27410Sstevel@tonic-gate } 27420Sstevel@tonic-gate 27430Sstevel@tonic-gate /* 27440Sstevel@tonic-gate * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL 27450Sstevel@tonic-gate * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY. 27460Sstevel@tonic-gate */ 27470Sstevel@tonic-gate scf_snapshot_t * 27480Sstevel@tonic-gate scf_snapshot_create(scf_handle_t *handle) 27490Sstevel@tonic-gate { 27500Sstevel@tonic-gate scf_snapshot_t *ret; 27510Sstevel@tonic-gate 27520Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 27530Sstevel@tonic-gate if (ret != NULL) { 27540Sstevel@tonic-gate if (datael_init(&ret->rd_d, handle, 27550Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SNAPSHOT) == -1) { 27560Sstevel@tonic-gate uu_free(ret); 27570Sstevel@tonic-gate return (NULL); 27580Sstevel@tonic-gate } 27590Sstevel@tonic-gate } else { 27600Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 27610Sstevel@tonic-gate } 27620Sstevel@tonic-gate 27630Sstevel@tonic-gate return (ret); 27640Sstevel@tonic-gate } 27650Sstevel@tonic-gate 27660Sstevel@tonic-gate scf_handle_t * 27670Sstevel@tonic-gate scf_snapshot_handle(const scf_snapshot_t *val) 27680Sstevel@tonic-gate { 27690Sstevel@tonic-gate return (datael_handle(&val->rd_d)); 27700Sstevel@tonic-gate } 27710Sstevel@tonic-gate 27720Sstevel@tonic-gate void 27730Sstevel@tonic-gate scf_snapshot_destroy(scf_snapshot_t *val) 27740Sstevel@tonic-gate { 27750Sstevel@tonic-gate if (val == NULL) 27760Sstevel@tonic-gate return; 27770Sstevel@tonic-gate 27780Sstevel@tonic-gate datael_destroy(&val->rd_d); 27790Sstevel@tonic-gate uu_free(val); 27800Sstevel@tonic-gate } 27810Sstevel@tonic-gate 27820Sstevel@tonic-gate ssize_t 27830Sstevel@tonic-gate scf_snapshot_get_name(const scf_snapshot_t *rep, char *out, size_t len) 27840Sstevel@tonic-gate { 27850Sstevel@tonic-gate return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME)); 27860Sstevel@tonic-gate } 27870Sstevel@tonic-gate 27880Sstevel@tonic-gate /* 27890Sstevel@tonic-gate * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL 27900Sstevel@tonic-gate * (bad server response or id in use), _NO_RESOURCES, _NO_MEMORY. 27910Sstevel@tonic-gate */ 27920Sstevel@tonic-gate scf_snaplevel_t * 27930Sstevel@tonic-gate scf_snaplevel_create(scf_handle_t *handle) 27940Sstevel@tonic-gate { 27950Sstevel@tonic-gate scf_snaplevel_t *ret; 27960Sstevel@tonic-gate 27970Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 27980Sstevel@tonic-gate if (ret != NULL) { 27990Sstevel@tonic-gate if (datael_init(&ret->rd_d, handle, 28000Sstevel@tonic-gate REP_PROTOCOL_ENTITY_SNAPLEVEL) == -1) { 28010Sstevel@tonic-gate uu_free(ret); 28020Sstevel@tonic-gate return (NULL); 28030Sstevel@tonic-gate } 28040Sstevel@tonic-gate } else { 28050Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 28060Sstevel@tonic-gate } 28070Sstevel@tonic-gate 28080Sstevel@tonic-gate return (ret); 28090Sstevel@tonic-gate } 28100Sstevel@tonic-gate 28110Sstevel@tonic-gate scf_handle_t * 28120Sstevel@tonic-gate scf_snaplevel_handle(const scf_snaplevel_t *val) 28130Sstevel@tonic-gate { 28140Sstevel@tonic-gate return (datael_handle(&val->rd_d)); 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate 28170Sstevel@tonic-gate void 28180Sstevel@tonic-gate scf_snaplevel_destroy(scf_snaplevel_t *val) 28190Sstevel@tonic-gate { 28200Sstevel@tonic-gate if (val == NULL) 28210Sstevel@tonic-gate return; 28220Sstevel@tonic-gate 28230Sstevel@tonic-gate datael_destroy(&val->rd_d); 28240Sstevel@tonic-gate uu_free(val); 28250Sstevel@tonic-gate } 28260Sstevel@tonic-gate 28270Sstevel@tonic-gate ssize_t 28280Sstevel@tonic-gate scf_snaplevel_get_scope_name(const scf_snaplevel_t *rep, char *out, size_t len) 28290Sstevel@tonic-gate { 28300Sstevel@tonic-gate return (datael_get_name(&rep->rd_d, out, len, 28310Sstevel@tonic-gate RP_ENTITY_NAME_SNAPLEVEL_SCOPE)); 28320Sstevel@tonic-gate } 28330Sstevel@tonic-gate 28340Sstevel@tonic-gate ssize_t 28350Sstevel@tonic-gate scf_snaplevel_get_service_name(const scf_snaplevel_t *rep, char *out, 28360Sstevel@tonic-gate size_t len) 28370Sstevel@tonic-gate { 28380Sstevel@tonic-gate return (datael_get_name(&rep->rd_d, out, len, 28390Sstevel@tonic-gate RP_ENTITY_NAME_SNAPLEVEL_SERVICE)); 28400Sstevel@tonic-gate } 28410Sstevel@tonic-gate 28420Sstevel@tonic-gate ssize_t 28430Sstevel@tonic-gate scf_snaplevel_get_instance_name(const scf_snaplevel_t *rep, char *out, 28440Sstevel@tonic-gate size_t len) 28450Sstevel@tonic-gate { 28460Sstevel@tonic-gate return (datael_get_name(&rep->rd_d, out, len, 28470Sstevel@tonic-gate RP_ENTITY_NAME_SNAPLEVEL_INSTANCE)); 28480Sstevel@tonic-gate } 28490Sstevel@tonic-gate 28500Sstevel@tonic-gate int 28510Sstevel@tonic-gate scf_snaplevel_get_pg(const scf_snaplevel_t *snap, const char *name, 28520Sstevel@tonic-gate scf_propertygroup_t *pg) 28530Sstevel@tonic-gate { 28540Sstevel@tonic-gate return (datael_get_child(&snap->rd_d, name, 28550Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0)); 28560Sstevel@tonic-gate } 28570Sstevel@tonic-gate 28580Sstevel@tonic-gate static int 28590Sstevel@tonic-gate snaplevel_next(const scf_datael_t *src, scf_snaplevel_t *dst_arg) 28600Sstevel@tonic-gate { 28610Sstevel@tonic-gate scf_handle_t *h = src->rd_handle; 28620Sstevel@tonic-gate scf_snaplevel_t *dst = dst_arg; 28630Sstevel@tonic-gate struct rep_protocol_entity_pair request; 28640Sstevel@tonic-gate struct rep_protocol_response response; 28650Sstevel@tonic-gate int r; 28660Sstevel@tonic-gate int dups = 0; 28670Sstevel@tonic-gate 28680Sstevel@tonic-gate if (h != dst->rd_d.rd_handle) 28690Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 28700Sstevel@tonic-gate 28710Sstevel@tonic-gate if (src == &dst->rd_d) { 28720Sstevel@tonic-gate dups = 1; 28730Sstevel@tonic-gate dst = HANDLE_HOLD_SNAPLVL(h); 28740Sstevel@tonic-gate } 28750Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 28760Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_NEXT_SNAPLEVEL; 28770Sstevel@tonic-gate request.rpr_entity_src = src->rd_entity; 28780Sstevel@tonic-gate request.rpr_entity_dst = dst->rd_d.rd_entity; 28790Sstevel@tonic-gate 28800Sstevel@tonic-gate datael_finish_reset(src); 28810Sstevel@tonic-gate datael_finish_reset(&dst->rd_d); 28820Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 28830Sstevel@tonic-gate &response, sizeof (response)); 28840Sstevel@tonic-gate /* 28850Sstevel@tonic-gate * if we succeeded, we need to swap dst and dst_arg's identity. We 28860Sstevel@tonic-gate * take advantage of the fact that the only in-library knowledge is 28870Sstevel@tonic-gate * their entity ids. 28880Sstevel@tonic-gate */ 28890Sstevel@tonic-gate if (dups && r >= 0 && 28900Sstevel@tonic-gate (response.rpr_response == REP_PROTOCOL_SUCCESS || 28910Sstevel@tonic-gate response.rpr_response == REP_PROTOCOL_DONE)) { 28920Sstevel@tonic-gate int entity = dst->rd_d.rd_entity; 28930Sstevel@tonic-gate 28940Sstevel@tonic-gate dst->rd_d.rd_entity = dst_arg->rd_d.rd_entity; 28950Sstevel@tonic-gate dst_arg->rd_d.rd_entity = entity; 28960Sstevel@tonic-gate } 28970Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 28980Sstevel@tonic-gate 28990Sstevel@tonic-gate if (dups) 29000Sstevel@tonic-gate HANDLE_RELE_SNAPLVL(h); 29010Sstevel@tonic-gate 29020Sstevel@tonic-gate if (r < 0) 29030Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 29040Sstevel@tonic-gate 29050Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS && 29060Sstevel@tonic-gate response.rpr_response != REP_PROTOCOL_DONE) { 29070Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 29080Sstevel@tonic-gate } 29090Sstevel@tonic-gate 29100Sstevel@tonic-gate return (response.rpr_response == REP_PROTOCOL_SUCCESS) ? 29110Sstevel@tonic-gate SCF_SUCCESS : SCF_COMPLETE; 29120Sstevel@tonic-gate } 29130Sstevel@tonic-gate 29140Sstevel@tonic-gate int scf_snapshot_get_base_snaplevel(const scf_snapshot_t *base, 29150Sstevel@tonic-gate scf_snaplevel_t *out) 29160Sstevel@tonic-gate { 29170Sstevel@tonic-gate return (snaplevel_next(&base->rd_d, out)); 29180Sstevel@tonic-gate } 29190Sstevel@tonic-gate 29200Sstevel@tonic-gate int scf_snaplevel_get_next_snaplevel(const scf_snaplevel_t *base, 29210Sstevel@tonic-gate scf_snaplevel_t *out) 29220Sstevel@tonic-gate { 29230Sstevel@tonic-gate return (snaplevel_next(&base->rd_d, out)); 29240Sstevel@tonic-gate } 29250Sstevel@tonic-gate 29260Sstevel@tonic-gate /* 29270Sstevel@tonic-gate * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL 29280Sstevel@tonic-gate * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY. 29290Sstevel@tonic-gate */ 29300Sstevel@tonic-gate scf_propertygroup_t * 29310Sstevel@tonic-gate scf_pg_create(scf_handle_t *handle) 29320Sstevel@tonic-gate { 29330Sstevel@tonic-gate scf_propertygroup_t *ret; 29340Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 29350Sstevel@tonic-gate if (ret != NULL) { 29360Sstevel@tonic-gate if (datael_init(&ret->rd_d, handle, 29370Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP) == -1) { 29380Sstevel@tonic-gate uu_free(ret); 29390Sstevel@tonic-gate return (NULL); 29400Sstevel@tonic-gate } 29410Sstevel@tonic-gate } else { 29420Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 29430Sstevel@tonic-gate } 29440Sstevel@tonic-gate 29450Sstevel@tonic-gate return (ret); 29460Sstevel@tonic-gate } 29470Sstevel@tonic-gate 29480Sstevel@tonic-gate scf_handle_t * 29490Sstevel@tonic-gate scf_pg_handle(const scf_propertygroup_t *val) 29500Sstevel@tonic-gate { 29510Sstevel@tonic-gate return (datael_handle(&val->rd_d)); 29520Sstevel@tonic-gate } 29530Sstevel@tonic-gate 29540Sstevel@tonic-gate void 29550Sstevel@tonic-gate scf_pg_destroy(scf_propertygroup_t *val) 29560Sstevel@tonic-gate { 29570Sstevel@tonic-gate if (val == NULL) 29580Sstevel@tonic-gate return; 29590Sstevel@tonic-gate 29600Sstevel@tonic-gate datael_destroy(&val->rd_d); 29610Sstevel@tonic-gate uu_free(val); 29620Sstevel@tonic-gate } 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate ssize_t 29650Sstevel@tonic-gate scf_pg_get_name(const scf_propertygroup_t *pg, char *out, size_t len) 29660Sstevel@tonic-gate { 29670Sstevel@tonic-gate return (datael_get_name(&pg->rd_d, out, len, RP_ENTITY_NAME_NAME)); 29680Sstevel@tonic-gate } 29690Sstevel@tonic-gate 29700Sstevel@tonic-gate ssize_t 29710Sstevel@tonic-gate scf_pg_get_type(const scf_propertygroup_t *pg, char *out, size_t len) 29720Sstevel@tonic-gate { 29730Sstevel@tonic-gate return (datael_get_name(&pg->rd_d, out, len, RP_ENTITY_NAME_PGTYPE)); 29740Sstevel@tonic-gate } 29750Sstevel@tonic-gate 29760Sstevel@tonic-gate int 29770Sstevel@tonic-gate scf_pg_get_flags(const scf_propertygroup_t *pg, uint32_t *out) 29780Sstevel@tonic-gate { 29790Sstevel@tonic-gate char buf[REP_PROTOCOL_NAME_LEN]; 29800Sstevel@tonic-gate ssize_t res; 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate res = datael_get_name(&pg->rd_d, buf, sizeof (buf), 29830Sstevel@tonic-gate RP_ENTITY_NAME_PGFLAGS); 29840Sstevel@tonic-gate 29850Sstevel@tonic-gate if (res == -1) 29860Sstevel@tonic-gate return (-1); 29870Sstevel@tonic-gate 29880Sstevel@tonic-gate if (uu_strtouint(buf, out, sizeof (*out), 0, 0, UINT32_MAX) == -1) 29890Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 29900Sstevel@tonic-gate 29910Sstevel@tonic-gate return (0); 29920Sstevel@tonic-gate } 29930Sstevel@tonic-gate 29940Sstevel@tonic-gate static int 29950Sstevel@tonic-gate datael_update(scf_datael_t *dp) 29960Sstevel@tonic-gate { 29970Sstevel@tonic-gate scf_handle_t *h = dp->rd_handle; 29980Sstevel@tonic-gate 29990Sstevel@tonic-gate struct rep_protocol_entity_update request; 30000Sstevel@tonic-gate struct rep_protocol_response response; 30010Sstevel@tonic-gate 30020Sstevel@tonic-gate int r; 30030Sstevel@tonic-gate 30040Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 30050Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_UPDATE; 30060Sstevel@tonic-gate request.rpr_entityid = dp->rd_entity; 30070Sstevel@tonic-gate 30080Sstevel@tonic-gate datael_finish_reset(dp); 3009407Sjwadams request.rpr_changeid = handle_next_changeid(h); 30100Sstevel@tonic-gate 30110Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 30120Sstevel@tonic-gate &response, sizeof (response)); 30130Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 30140Sstevel@tonic-gate 30150Sstevel@tonic-gate if (r < 0) 30160Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 30170Sstevel@tonic-gate 30180Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS && 30190Sstevel@tonic-gate response.rpr_response != REP_PROTOCOL_DONE) { 30200Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 30210Sstevel@tonic-gate } 30220Sstevel@tonic-gate 30230Sstevel@tonic-gate return (response.rpr_response == REP_PROTOCOL_SUCCESS) ? 30240Sstevel@tonic-gate SCF_SUCCESS : SCF_COMPLETE; 30250Sstevel@tonic-gate } 30260Sstevel@tonic-gate 30270Sstevel@tonic-gate int 30280Sstevel@tonic-gate scf_pg_update(scf_propertygroup_t *pg) 30290Sstevel@tonic-gate { 30300Sstevel@tonic-gate return (datael_update(&pg->rd_d)); 30310Sstevel@tonic-gate } 30320Sstevel@tonic-gate 30330Sstevel@tonic-gate int 30340Sstevel@tonic-gate scf_snapshot_update(scf_snapshot_t *snap) 30350Sstevel@tonic-gate { 30360Sstevel@tonic-gate return (datael_update(&snap->rd_d)); 30370Sstevel@tonic-gate } 30380Sstevel@tonic-gate 30390Sstevel@tonic-gate int 30400Sstevel@tonic-gate _scf_pg_wait(scf_propertygroup_t *pg, int timeout) 30410Sstevel@tonic-gate { 30420Sstevel@tonic-gate scf_handle_t *h = pg->rd_d.rd_handle; 30430Sstevel@tonic-gate 30440Sstevel@tonic-gate struct rep_protocol_propertygrp_request request; 30450Sstevel@tonic-gate struct rep_protocol_response response; 30460Sstevel@tonic-gate 30470Sstevel@tonic-gate struct pollfd pollfd; 30480Sstevel@tonic-gate 30490Sstevel@tonic-gate int r; 30500Sstevel@tonic-gate 30510Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 30520Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_PROPERTYGRP_SETUP_WAIT; 30530Sstevel@tonic-gate request.rpr_entityid = pg->rd_d.rd_entity; 30540Sstevel@tonic-gate 30550Sstevel@tonic-gate datael_finish_reset(&pg->rd_d); 30560Sstevel@tonic-gate if (!handle_is_bound(h)) { 30570Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 30580Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN)); 30590Sstevel@tonic-gate } 30600Sstevel@tonic-gate r = make_door_call_retfd(h->rh_doorfd, &request, sizeof (request), 30610Sstevel@tonic-gate &response, sizeof (response), &pollfd.fd); 30620Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 30630Sstevel@tonic-gate 30640Sstevel@tonic-gate if (r < 0) 30650Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 30660Sstevel@tonic-gate 30670Sstevel@tonic-gate assert((response.rpr_response == REP_PROTOCOL_SUCCESS) == 30680Sstevel@tonic-gate (pollfd.fd != -1)); 30690Sstevel@tonic-gate 30700Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_LATEST) 30710Sstevel@tonic-gate return (SCF_SUCCESS); 30720Sstevel@tonic-gate 30730Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 30740Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 30750Sstevel@tonic-gate 30760Sstevel@tonic-gate pollfd.events = 0; 30770Sstevel@tonic-gate pollfd.revents = 0; 30780Sstevel@tonic-gate 30790Sstevel@tonic-gate r = poll(&pollfd, 1, timeout * MILLISEC); 30800Sstevel@tonic-gate 30810Sstevel@tonic-gate (void) close(pollfd.fd); 30820Sstevel@tonic-gate return (pollfd.revents ? SCF_SUCCESS : SCF_COMPLETE); 30830Sstevel@tonic-gate } 30840Sstevel@tonic-gate 30850Sstevel@tonic-gate static int 30860Sstevel@tonic-gate scf_notify_add_pattern(scf_handle_t *h, int type, const char *name) 30870Sstevel@tonic-gate { 30880Sstevel@tonic-gate struct rep_protocol_notify_request request; 30890Sstevel@tonic-gate struct rep_protocol_response response; 30900Sstevel@tonic-gate int r; 30910Sstevel@tonic-gate 30920Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 30930Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_CLIENT_ADD_NOTIFY; 30940Sstevel@tonic-gate request.rpr_type = type; 30950Sstevel@tonic-gate (void) strlcpy(request.rpr_pattern, name, sizeof (request.rpr_pattern)); 30960Sstevel@tonic-gate 30970Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 30980Sstevel@tonic-gate &response, sizeof (response)); 30990Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 31000Sstevel@tonic-gate 31010Sstevel@tonic-gate if (r < 0) 31020Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 31030Sstevel@tonic-gate 31040Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 31050Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 31060Sstevel@tonic-gate 31070Sstevel@tonic-gate return (SCF_SUCCESS); 31080Sstevel@tonic-gate } 31090Sstevel@tonic-gate 31100Sstevel@tonic-gate int 31110Sstevel@tonic-gate _scf_notify_add_pgname(scf_handle_t *h, const char *name) 31120Sstevel@tonic-gate { 31130Sstevel@tonic-gate return (scf_notify_add_pattern(h, REP_PROTOCOL_NOTIFY_PGNAME, name)); 31140Sstevel@tonic-gate } 31150Sstevel@tonic-gate 31160Sstevel@tonic-gate int 31170Sstevel@tonic-gate _scf_notify_add_pgtype(scf_handle_t *h, const char *type) 31180Sstevel@tonic-gate { 31190Sstevel@tonic-gate return (scf_notify_add_pattern(h, REP_PROTOCOL_NOTIFY_PGTYPE, type)); 31200Sstevel@tonic-gate } 31210Sstevel@tonic-gate 31220Sstevel@tonic-gate int 31230Sstevel@tonic-gate _scf_notify_wait(scf_propertygroup_t *pg, char *out, size_t sz) 31240Sstevel@tonic-gate { 31250Sstevel@tonic-gate struct rep_protocol_wait_request request; 31260Sstevel@tonic-gate struct rep_protocol_fmri_response response; 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate scf_handle_t *h = pg->rd_d.rd_handle; 31290Sstevel@tonic-gate int dummy; 31300Sstevel@tonic-gate int fd; 31310Sstevel@tonic-gate int r; 31320Sstevel@tonic-gate 31330Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 31340Sstevel@tonic-gate datael_finish_reset(&pg->rd_d); 31350Sstevel@tonic-gate if (!handle_is_bound(h)) { 31360Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 31370Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN)); 31380Sstevel@tonic-gate } 31390Sstevel@tonic-gate fd = h->rh_doorfd; 31400Sstevel@tonic-gate ++h->rh_fd_users; 31410Sstevel@tonic-gate assert(h->rh_fd_users > 0); 31420Sstevel@tonic-gate 31430Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_CLIENT_WAIT; 31440Sstevel@tonic-gate request.rpr_entityid = pg->rd_d.rd_entity; 31450Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 31460Sstevel@tonic-gate 31470Sstevel@tonic-gate r = make_door_call_retfd(fd, &request, sizeof (request), 31480Sstevel@tonic-gate &response, sizeof (response), &dummy); 31490Sstevel@tonic-gate 31500Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 31510Sstevel@tonic-gate assert(h->rh_fd_users > 0); 31520Sstevel@tonic-gate if (--h->rh_fd_users == 0) { 31530Sstevel@tonic-gate (void) pthread_cond_broadcast(&h->rh_cv); 31540Sstevel@tonic-gate /* 31550Sstevel@tonic-gate * check for a delayed close, now that there are no other 31560Sstevel@tonic-gate * users. 31570Sstevel@tonic-gate */ 31580Sstevel@tonic-gate if (h->rh_doorfd_old != -1) { 31590Sstevel@tonic-gate assert(h->rh_doorfd == -1); 31600Sstevel@tonic-gate assert(fd == h->rh_doorfd_old); 31610Sstevel@tonic-gate (void) close(h->rh_doorfd_old); 31620Sstevel@tonic-gate h->rh_doorfd_old = -1; 31630Sstevel@tonic-gate } 31640Sstevel@tonic-gate } 31650Sstevel@tonic-gate handle_unrefed(h); /* drops h->rh_lock */ 31660Sstevel@tonic-gate 31670Sstevel@tonic-gate if (r < 0) 31680Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 31690Sstevel@tonic-gate 31700Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_DONE) 31710Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET)); 31720Sstevel@tonic-gate 31730Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 31740Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 31750Sstevel@tonic-gate 31760Sstevel@tonic-gate /* the following will be non-zero for delete notifications */ 31770Sstevel@tonic-gate return (strlcpy(out, response.rpr_fmri, sz)); 31780Sstevel@tonic-gate } 31790Sstevel@tonic-gate 31800Sstevel@tonic-gate static int 31810Sstevel@tonic-gate _scf_snapshot_take(scf_instance_t *inst, const char *name, 31820Sstevel@tonic-gate scf_snapshot_t *snap, int flags) 31830Sstevel@tonic-gate { 31840Sstevel@tonic-gate scf_handle_t *h = inst->rd_d.rd_handle; 31850Sstevel@tonic-gate 31860Sstevel@tonic-gate struct rep_protocol_snapshot_take request; 31870Sstevel@tonic-gate struct rep_protocol_response response; 31880Sstevel@tonic-gate 31890Sstevel@tonic-gate int r; 31900Sstevel@tonic-gate 31910Sstevel@tonic-gate if (h != snap->rd_d.rd_handle) 31920Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 31930Sstevel@tonic-gate 31940Sstevel@tonic-gate if (strlcpy(request.rpr_name, (name != NULL)? name : "", 31950Sstevel@tonic-gate sizeof (request.rpr_name)) >= sizeof (request.rpr_name)) 31960Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 31970Sstevel@tonic-gate 31980Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 31990Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_SNAPSHOT_TAKE; 32000Sstevel@tonic-gate request.rpr_entityid_src = inst->rd_d.rd_entity; 32010Sstevel@tonic-gate request.rpr_entityid_dest = snap->rd_d.rd_entity; 32020Sstevel@tonic-gate request.rpr_flags = flags; 32030Sstevel@tonic-gate 32040Sstevel@tonic-gate datael_finish_reset(&inst->rd_d); 32050Sstevel@tonic-gate datael_finish_reset(&snap->rd_d); 32060Sstevel@tonic-gate 32070Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 32080Sstevel@tonic-gate &response, sizeof (response)); 32090Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 32100Sstevel@tonic-gate 32110Sstevel@tonic-gate if (r < 0) 32120Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 32130Sstevel@tonic-gate 32140Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 32150Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 32160Sstevel@tonic-gate 32170Sstevel@tonic-gate return (SCF_SUCCESS); 32180Sstevel@tonic-gate } 32190Sstevel@tonic-gate 32200Sstevel@tonic-gate int 32210Sstevel@tonic-gate _scf_snapshot_take_new_named(scf_instance_t *inst, 32220Sstevel@tonic-gate const char *svcname, const char *instname, const char *snapname, 32230Sstevel@tonic-gate scf_snapshot_t *snap) 32240Sstevel@tonic-gate { 32250Sstevel@tonic-gate scf_handle_t *h = inst->rd_d.rd_handle; 32260Sstevel@tonic-gate 32270Sstevel@tonic-gate struct rep_protocol_snapshot_take_named request; 32280Sstevel@tonic-gate struct rep_protocol_response response; 32290Sstevel@tonic-gate 32300Sstevel@tonic-gate int r; 32310Sstevel@tonic-gate 32320Sstevel@tonic-gate if (h != snap->rd_d.rd_handle) 32330Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 32340Sstevel@tonic-gate 32350Sstevel@tonic-gate if (strlcpy(request.rpr_svcname, svcname, 32360Sstevel@tonic-gate sizeof (request.rpr_svcname)) >= sizeof (request.rpr_svcname)) 32370Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 32380Sstevel@tonic-gate 32390Sstevel@tonic-gate if (strlcpy(request.rpr_instname, instname, 32400Sstevel@tonic-gate sizeof (request.rpr_instname)) >= sizeof (request.rpr_instname)) 32410Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 32420Sstevel@tonic-gate 32430Sstevel@tonic-gate if (strlcpy(request.rpr_name, snapname, 32440Sstevel@tonic-gate sizeof (request.rpr_name)) >= sizeof (request.rpr_name)) 32450Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 32460Sstevel@tonic-gate 32470Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 32480Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_SNAPSHOT_TAKE_NAMED; 32490Sstevel@tonic-gate request.rpr_entityid_src = inst->rd_d.rd_entity; 32500Sstevel@tonic-gate request.rpr_entityid_dest = snap->rd_d.rd_entity; 32510Sstevel@tonic-gate 32520Sstevel@tonic-gate datael_finish_reset(&inst->rd_d); 32530Sstevel@tonic-gate datael_finish_reset(&snap->rd_d); 32540Sstevel@tonic-gate 32550Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 32560Sstevel@tonic-gate &response, sizeof (response)); 32570Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 32580Sstevel@tonic-gate 32590Sstevel@tonic-gate if (r < 0) 32600Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 32610Sstevel@tonic-gate 32620Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 32630Sstevel@tonic-gate assert(response.rpr_response != 32640Sstevel@tonic-gate REP_PROTOCOL_FAIL_TYPE_MISMATCH); 32650Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 32660Sstevel@tonic-gate } 32670Sstevel@tonic-gate 32680Sstevel@tonic-gate return (SCF_SUCCESS); 32690Sstevel@tonic-gate } 32700Sstevel@tonic-gate 32710Sstevel@tonic-gate int 32720Sstevel@tonic-gate _scf_snapshot_take_new(scf_instance_t *inst, const char *name, 32730Sstevel@tonic-gate scf_snapshot_t *snap) 32740Sstevel@tonic-gate { 32750Sstevel@tonic-gate return (_scf_snapshot_take(inst, name, snap, REP_SNAPSHOT_NEW)); 32760Sstevel@tonic-gate } 32770Sstevel@tonic-gate 32780Sstevel@tonic-gate int 32790Sstevel@tonic-gate _scf_snapshot_take_attach(scf_instance_t *inst, scf_snapshot_t *snap) 32800Sstevel@tonic-gate { 32810Sstevel@tonic-gate return (_scf_snapshot_take(inst, NULL, snap, REP_SNAPSHOT_ATTACH)); 32820Sstevel@tonic-gate } 32830Sstevel@tonic-gate 32840Sstevel@tonic-gate int 32850Sstevel@tonic-gate _scf_snapshot_attach(scf_snapshot_t *src, scf_snapshot_t *dest) 32860Sstevel@tonic-gate { 32870Sstevel@tonic-gate scf_handle_t *h = dest->rd_d.rd_handle; 32880Sstevel@tonic-gate 32890Sstevel@tonic-gate struct rep_protocol_snapshot_attach request; 32900Sstevel@tonic-gate struct rep_protocol_response response; 32910Sstevel@tonic-gate 32920Sstevel@tonic-gate int r; 32930Sstevel@tonic-gate 32940Sstevel@tonic-gate if (h != src->rd_d.rd_handle) 32950Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 32960Sstevel@tonic-gate 32970Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 32980Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_SNAPSHOT_ATTACH; 32990Sstevel@tonic-gate request.rpr_entityid_src = src->rd_d.rd_entity; 33000Sstevel@tonic-gate request.rpr_entityid_dest = dest->rd_d.rd_entity; 33010Sstevel@tonic-gate 33020Sstevel@tonic-gate datael_finish_reset(&src->rd_d); 33030Sstevel@tonic-gate datael_finish_reset(&dest->rd_d); 33040Sstevel@tonic-gate 33050Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 33060Sstevel@tonic-gate &response, sizeof (response)); 33070Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 33080Sstevel@tonic-gate 33090Sstevel@tonic-gate if (r < 0) 33100Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 33110Sstevel@tonic-gate 33120Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 33130Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 33140Sstevel@tonic-gate 33150Sstevel@tonic-gate return (SCF_SUCCESS); 33160Sstevel@tonic-gate } 33170Sstevel@tonic-gate 33180Sstevel@tonic-gate /* 33190Sstevel@tonic-gate * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL 33200Sstevel@tonic-gate * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY. 33210Sstevel@tonic-gate */ 33220Sstevel@tonic-gate scf_property_t * 33230Sstevel@tonic-gate scf_property_create(scf_handle_t *handle) 33240Sstevel@tonic-gate { 33250Sstevel@tonic-gate scf_property_t *ret; 33260Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 33270Sstevel@tonic-gate if (ret != NULL) { 33280Sstevel@tonic-gate if (datael_init(&ret->rd_d, handle, 33290Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTY) == -1) { 33300Sstevel@tonic-gate uu_free(ret); 33310Sstevel@tonic-gate return (NULL); 33320Sstevel@tonic-gate } 33330Sstevel@tonic-gate } else { 33340Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 33350Sstevel@tonic-gate } 33360Sstevel@tonic-gate 33370Sstevel@tonic-gate return (ret); 33380Sstevel@tonic-gate } 33390Sstevel@tonic-gate 33400Sstevel@tonic-gate scf_handle_t * 33410Sstevel@tonic-gate scf_property_handle(const scf_property_t *val) 33420Sstevel@tonic-gate { 33430Sstevel@tonic-gate return (datael_handle(&val->rd_d)); 33440Sstevel@tonic-gate } 33450Sstevel@tonic-gate 33460Sstevel@tonic-gate void 33470Sstevel@tonic-gate scf_property_destroy(scf_property_t *val) 33480Sstevel@tonic-gate { 33490Sstevel@tonic-gate if (val == NULL) 33500Sstevel@tonic-gate return; 33510Sstevel@tonic-gate 33520Sstevel@tonic-gate datael_destroy(&val->rd_d); 33530Sstevel@tonic-gate uu_free(val); 33540Sstevel@tonic-gate } 33550Sstevel@tonic-gate 33560Sstevel@tonic-gate static int 33570Sstevel@tonic-gate property_type_locked(const scf_property_t *prop, 33580Sstevel@tonic-gate rep_protocol_value_type_t *out) 33590Sstevel@tonic-gate { 33600Sstevel@tonic-gate scf_handle_t *h = prop->rd_d.rd_handle; 33610Sstevel@tonic-gate 33620Sstevel@tonic-gate struct rep_protocol_property_request request; 33630Sstevel@tonic-gate struct rep_protocol_integer_response response; 33640Sstevel@tonic-gate 33650Sstevel@tonic-gate int r; 33660Sstevel@tonic-gate 33670Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 33680Sstevel@tonic-gate 33690Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_PROPERTY_GET_TYPE; 33700Sstevel@tonic-gate request.rpr_entityid = prop->rd_d.rd_entity; 33710Sstevel@tonic-gate 33720Sstevel@tonic-gate datael_finish_reset(&prop->rd_d); 33730Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 33740Sstevel@tonic-gate &response, sizeof (response)); 33750Sstevel@tonic-gate 33760Sstevel@tonic-gate if (r < 0) 33770Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 33780Sstevel@tonic-gate 33790Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS || 33800Sstevel@tonic-gate r < sizeof (response)) { 33810Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 33820Sstevel@tonic-gate } 33830Sstevel@tonic-gate *out = response.rpr_value; 33840Sstevel@tonic-gate return (SCF_SUCCESS); 33850Sstevel@tonic-gate } 33860Sstevel@tonic-gate 33870Sstevel@tonic-gate int 33880Sstevel@tonic-gate scf_property_type(const scf_property_t *prop, scf_type_t *out) 33890Sstevel@tonic-gate { 33900Sstevel@tonic-gate scf_handle_t *h = prop->rd_d.rd_handle; 33910Sstevel@tonic-gate rep_protocol_value_type_t out_raw; 33920Sstevel@tonic-gate int ret; 33930Sstevel@tonic-gate 33940Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 33950Sstevel@tonic-gate ret = property_type_locked(prop, &out_raw); 33960Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 33970Sstevel@tonic-gate 33980Sstevel@tonic-gate if (ret == SCF_SUCCESS) 33990Sstevel@tonic-gate *out = scf_protocol_type_to_type(out_raw); 34000Sstevel@tonic-gate 34010Sstevel@tonic-gate return (ret); 34020Sstevel@tonic-gate } 34030Sstevel@tonic-gate 34040Sstevel@tonic-gate int 34050Sstevel@tonic-gate scf_property_is_type(const scf_property_t *prop, scf_type_t base_arg) 34060Sstevel@tonic-gate { 34070Sstevel@tonic-gate scf_handle_t *h = prop->rd_d.rd_handle; 34080Sstevel@tonic-gate rep_protocol_value_type_t base = scf_type_to_protocol_type(base_arg); 34090Sstevel@tonic-gate rep_protocol_value_type_t type; 34100Sstevel@tonic-gate int ret; 34110Sstevel@tonic-gate 34120Sstevel@tonic-gate if (base == REP_PROTOCOL_TYPE_INVALID) 34130Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 34140Sstevel@tonic-gate 34150Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 34160Sstevel@tonic-gate ret = property_type_locked(prop, &type); 34170Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 34180Sstevel@tonic-gate 34190Sstevel@tonic-gate if (ret == SCF_SUCCESS) { 34200Sstevel@tonic-gate if (!scf_is_compatible_type(base, type)) 34210Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_TYPE_MISMATCH)); 34220Sstevel@tonic-gate } 34230Sstevel@tonic-gate return (ret); 34240Sstevel@tonic-gate } 34250Sstevel@tonic-gate 34260Sstevel@tonic-gate ssize_t 34270Sstevel@tonic-gate scf_property_get_name(const scf_property_t *prop, char *out, size_t len) 34280Sstevel@tonic-gate { 34290Sstevel@tonic-gate return (datael_get_name(&prop->rd_d, out, len, RP_ENTITY_NAME_NAME)); 34300Sstevel@tonic-gate } 34310Sstevel@tonic-gate 34320Sstevel@tonic-gate /* 34330Sstevel@tonic-gate * transaction functions 34340Sstevel@tonic-gate */ 34350Sstevel@tonic-gate 34360Sstevel@tonic-gate /* 34370Sstevel@tonic-gate * Fails with _NO_MEMORY, _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, 34380Sstevel@tonic-gate * _INTERNAL (bad server response or id in use), or _NO_RESOURCES. 34390Sstevel@tonic-gate */ 34400Sstevel@tonic-gate scf_transaction_t * 34410Sstevel@tonic-gate scf_transaction_create(scf_handle_t *handle) 34420Sstevel@tonic-gate { 34430Sstevel@tonic-gate scf_transaction_t *ret; 34440Sstevel@tonic-gate 34450Sstevel@tonic-gate ret = uu_zalloc(sizeof (scf_transaction_t)); 34460Sstevel@tonic-gate if (ret == NULL) { 34470Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 34480Sstevel@tonic-gate return (NULL); 34490Sstevel@tonic-gate } 34500Sstevel@tonic-gate if (datael_init(&ret->tran_pg.rd_d, handle, 34510Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTYGRP) == -1) { 34520Sstevel@tonic-gate uu_free(ret); 34530Sstevel@tonic-gate return (NULL); /* error already set */ 34540Sstevel@tonic-gate } 34550Sstevel@tonic-gate ret->tran_state = TRAN_STATE_NEW; 34560Sstevel@tonic-gate ret->tran_props = uu_list_create(tran_entry_pool, ret, UU_LIST_SORTED); 34570Sstevel@tonic-gate if (ret->tran_props == NULL) { 34580Sstevel@tonic-gate datael_destroy(&ret->tran_pg.rd_d); 34590Sstevel@tonic-gate uu_free(ret); 34600Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 34610Sstevel@tonic-gate return (NULL); 34620Sstevel@tonic-gate } 34630Sstevel@tonic-gate 34640Sstevel@tonic-gate return (ret); 34650Sstevel@tonic-gate } 34660Sstevel@tonic-gate 34670Sstevel@tonic-gate scf_handle_t * 34680Sstevel@tonic-gate scf_transaction_handle(const scf_transaction_t *val) 34690Sstevel@tonic-gate { 34700Sstevel@tonic-gate return (handle_get(val->tran_pg.rd_d.rd_handle)); 34710Sstevel@tonic-gate } 34720Sstevel@tonic-gate 34730Sstevel@tonic-gate int 34740Sstevel@tonic-gate scf_transaction_start(scf_transaction_t *tran, scf_propertygroup_t *pg) 34750Sstevel@tonic-gate { 34760Sstevel@tonic-gate scf_handle_t *h = tran->tran_pg.rd_d.rd_handle; 34770Sstevel@tonic-gate 34780Sstevel@tonic-gate struct rep_protocol_transaction_start request; 34790Sstevel@tonic-gate struct rep_protocol_response response; 34800Sstevel@tonic-gate int r; 34810Sstevel@tonic-gate 34820Sstevel@tonic-gate if (h != pg->rd_d.rd_handle) 34830Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 34840Sstevel@tonic-gate 34850Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 34860Sstevel@tonic-gate if (tran->tran_state != TRAN_STATE_NEW) { 34870Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 34880Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_IN_USE)); 34890Sstevel@tonic-gate } 34900Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_PROPERTYGRP_TX_START; 34910Sstevel@tonic-gate request.rpr_entityid_tx = tran->tran_pg.rd_d.rd_entity; 34920Sstevel@tonic-gate request.rpr_entityid = pg->rd_d.rd_entity; 34930Sstevel@tonic-gate 34940Sstevel@tonic-gate datael_finish_reset(&tran->tran_pg.rd_d); 34950Sstevel@tonic-gate datael_finish_reset(&pg->rd_d); 34960Sstevel@tonic-gate 34970Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 34980Sstevel@tonic-gate &response, sizeof (response)); 34990Sstevel@tonic-gate 35000Sstevel@tonic-gate if (r < 0) { 35010Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 35020Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 35030Sstevel@tonic-gate } 35040Sstevel@tonic-gate 35050Sstevel@tonic-gate /* r < sizeof (response) cannot happen because sizeof (response) == 4 */ 35060Sstevel@tonic-gate 35070Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS || 35080Sstevel@tonic-gate r < sizeof (response)) { 35090Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 35100Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 35110Sstevel@tonic-gate } 35120Sstevel@tonic-gate 35130Sstevel@tonic-gate tran->tran_state = TRAN_STATE_SETUP; 35140Sstevel@tonic-gate tran->tran_invalid = 0; 35150Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 35160Sstevel@tonic-gate return (SCF_SUCCESS); 35170Sstevel@tonic-gate } 35180Sstevel@tonic-gate 35190Sstevel@tonic-gate static void 35200Sstevel@tonic-gate entry_invalidate(scf_transaction_entry_t *cur, int and_destroy, 35210Sstevel@tonic-gate int and_reset_value) 35220Sstevel@tonic-gate { 35230Sstevel@tonic-gate scf_value_t *v, *next; 35240Sstevel@tonic-gate scf_transaction_t *tx; 35250Sstevel@tonic-gate scf_handle_t *h = cur->entry_handle; 35260Sstevel@tonic-gate 35270Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 35280Sstevel@tonic-gate 35290Sstevel@tonic-gate if ((tx = cur->entry_tx) != NULL) { 35300Sstevel@tonic-gate tx->tran_invalid = 1; 35310Sstevel@tonic-gate uu_list_remove(tx->tran_props, cur); 35320Sstevel@tonic-gate cur->entry_tx = NULL; 35330Sstevel@tonic-gate } 35340Sstevel@tonic-gate 35350Sstevel@tonic-gate cur->entry_property = NULL; 35360Sstevel@tonic-gate cur->entry_state = ENTRY_STATE_INVALID; 35370Sstevel@tonic-gate cur->entry_action = REP_PROTOCOL_TX_ENTRY_INVALID; 35380Sstevel@tonic-gate cur->entry_type = REP_PROTOCOL_TYPE_INVALID; 35390Sstevel@tonic-gate 35400Sstevel@tonic-gate for (v = cur->entry_head; v != NULL; v = next) { 35410Sstevel@tonic-gate next = v->value_next; 35420Sstevel@tonic-gate v->value_tx = NULL; 35430Sstevel@tonic-gate v->value_next = NULL; 35440Sstevel@tonic-gate if (and_destroy || and_reset_value) 35450Sstevel@tonic-gate scf_value_reset_locked(v, and_destroy); 35460Sstevel@tonic-gate } 35470Sstevel@tonic-gate cur->entry_head = NULL; 35480Sstevel@tonic-gate } 35490Sstevel@tonic-gate 35500Sstevel@tonic-gate static void 35510Sstevel@tonic-gate entry_destroy_locked(scf_transaction_entry_t *entry) 35520Sstevel@tonic-gate { 35530Sstevel@tonic-gate scf_handle_t *h = entry->entry_handle; 35540Sstevel@tonic-gate 35550Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 35560Sstevel@tonic-gate 35570Sstevel@tonic-gate entry_invalidate(entry, 0, 0); 35580Sstevel@tonic-gate 35590Sstevel@tonic-gate entry->entry_handle = NULL; 35600Sstevel@tonic-gate assert(h->rh_entries > 0); 35610Sstevel@tonic-gate --h->rh_entries; 35620Sstevel@tonic-gate --h->rh_extrefs; 35630Sstevel@tonic-gate uu_list_node_fini(entry, &entry->entry_link, tran_entry_pool); 35640Sstevel@tonic-gate uu_free(entry); 35650Sstevel@tonic-gate } 35660Sstevel@tonic-gate 35670Sstevel@tonic-gate static int 35680Sstevel@tonic-gate transaction_add(scf_transaction_t *tran, scf_transaction_entry_t *entry, 35690Sstevel@tonic-gate enum rep_protocol_transaction_action action, 35700Sstevel@tonic-gate const char *prop, rep_protocol_value_type_t type) 35710Sstevel@tonic-gate { 35720Sstevel@tonic-gate scf_handle_t *h = tran->tran_pg.rd_d.rd_handle; 35730Sstevel@tonic-gate scf_transaction_entry_t *old; 35740Sstevel@tonic-gate scf_property_t *prop_p; 35750Sstevel@tonic-gate rep_protocol_value_type_t oldtype; 35760Sstevel@tonic-gate scf_error_t error = SCF_ERROR_NONE; 35770Sstevel@tonic-gate int ret; 35780Sstevel@tonic-gate uu_list_index_t idx; 35790Sstevel@tonic-gate 35800Sstevel@tonic-gate if (h != entry->entry_handle) 35810Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 35820Sstevel@tonic-gate 35830Sstevel@tonic-gate if (action == REP_PROTOCOL_TX_ENTRY_DELETE) 35840Sstevel@tonic-gate assert(type == REP_PROTOCOL_TYPE_INVALID); 35850Sstevel@tonic-gate else if (type == REP_PROTOCOL_TYPE_INVALID) 35860Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 35870Sstevel@tonic-gate 35880Sstevel@tonic-gate prop_p = HANDLE_HOLD_PROPERTY(h); 35890Sstevel@tonic-gate 35900Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 35910Sstevel@tonic-gate if (tran->tran_state != TRAN_STATE_SETUP) { 35920Sstevel@tonic-gate error = SCF_ERROR_NOT_SET; 35930Sstevel@tonic-gate goto error; 35940Sstevel@tonic-gate } 35950Sstevel@tonic-gate if (tran->tran_invalid) { 35960Sstevel@tonic-gate error = SCF_ERROR_NOT_SET; 35970Sstevel@tonic-gate goto error; 35980Sstevel@tonic-gate } 35990Sstevel@tonic-gate 36000Sstevel@tonic-gate if (entry->entry_state != ENTRY_STATE_INVALID) 36010Sstevel@tonic-gate entry_invalidate(entry, 0, 0); 36020Sstevel@tonic-gate 36030Sstevel@tonic-gate old = uu_list_find(tran->tran_props, &prop, NULL, &idx); 36040Sstevel@tonic-gate if (old != NULL) { 36050Sstevel@tonic-gate error = SCF_ERROR_IN_USE; 36060Sstevel@tonic-gate goto error; 36070Sstevel@tonic-gate } 36080Sstevel@tonic-gate 36090Sstevel@tonic-gate ret = datael_get_child_locked(&tran->tran_pg.rd_d, prop, 36100Sstevel@tonic-gate REP_PROTOCOL_ENTITY_PROPERTY, &prop_p->rd_d); 36110Sstevel@tonic-gate if (ret == -1 && (error = scf_error()) != SCF_ERROR_NOT_FOUND) { 36120Sstevel@tonic-gate goto error; 36130Sstevel@tonic-gate } 36140Sstevel@tonic-gate 36150Sstevel@tonic-gate switch (action) { 36160Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_DELETE: 36170Sstevel@tonic-gate if (ret == -1) { 36180Sstevel@tonic-gate error = SCF_ERROR_NOT_FOUND; 36190Sstevel@tonic-gate goto error; 36200Sstevel@tonic-gate } 36210Sstevel@tonic-gate break; 36220Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_NEW: 36230Sstevel@tonic-gate if (ret != -1) { 36240Sstevel@tonic-gate error = SCF_ERROR_EXISTS; 36250Sstevel@tonic-gate goto error; 36260Sstevel@tonic-gate } 36270Sstevel@tonic-gate break; 36280Sstevel@tonic-gate 36290Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_CLEAR: 36300Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_REPLACE: 36310Sstevel@tonic-gate if (ret == -1) { 36320Sstevel@tonic-gate error = SCF_ERROR_NOT_FOUND; 36330Sstevel@tonic-gate goto error; 36340Sstevel@tonic-gate } 36350Sstevel@tonic-gate if (action == REP_PROTOCOL_TX_ENTRY_CLEAR) { 36360Sstevel@tonic-gate if (property_type_locked(prop_p, &oldtype) == -1) { 36370Sstevel@tonic-gate error = scf_error(); 36380Sstevel@tonic-gate goto error; 36390Sstevel@tonic-gate } 36400Sstevel@tonic-gate if (oldtype != type) { 36410Sstevel@tonic-gate error = SCF_ERROR_TYPE_MISMATCH; 36420Sstevel@tonic-gate goto error; 36430Sstevel@tonic-gate } 36440Sstevel@tonic-gate } 36450Sstevel@tonic-gate break; 36460Sstevel@tonic-gate default: 36470Sstevel@tonic-gate assert(0); 36480Sstevel@tonic-gate abort(); 36490Sstevel@tonic-gate } 36500Sstevel@tonic-gate 36510Sstevel@tonic-gate (void) strlcpy(entry->entry_namebuf, prop, 36520Sstevel@tonic-gate sizeof (entry->entry_namebuf)); 36530Sstevel@tonic-gate entry->entry_property = entry->entry_namebuf; 36540Sstevel@tonic-gate entry->entry_action = action; 36550Sstevel@tonic-gate entry->entry_type = type; 36560Sstevel@tonic-gate 36570Sstevel@tonic-gate entry->entry_state = ENTRY_STATE_IN_TX_ACTION; 36580Sstevel@tonic-gate entry->entry_tx = tran; 36590Sstevel@tonic-gate uu_list_insert(tran->tran_props, entry, idx); 36600Sstevel@tonic-gate 36610Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 36620Sstevel@tonic-gate 36630Sstevel@tonic-gate HANDLE_RELE_PROPERTY(h); 36640Sstevel@tonic-gate 36650Sstevel@tonic-gate return (SCF_SUCCESS); 36660Sstevel@tonic-gate 36670Sstevel@tonic-gate error: 36680Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 36690Sstevel@tonic-gate 36700Sstevel@tonic-gate HANDLE_RELE_PROPERTY(h); 36710Sstevel@tonic-gate 36720Sstevel@tonic-gate return (scf_set_error(error)); 36730Sstevel@tonic-gate } 36740Sstevel@tonic-gate 36750Sstevel@tonic-gate int 36760Sstevel@tonic-gate scf_transaction_property_new(scf_transaction_t *tx, 36770Sstevel@tonic-gate scf_transaction_entry_t *entry, const char *prop, scf_type_t type) 36780Sstevel@tonic-gate { 36790Sstevel@tonic-gate return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_NEW, 36800Sstevel@tonic-gate prop, scf_type_to_protocol_type(type))); 36810Sstevel@tonic-gate } 36820Sstevel@tonic-gate 36830Sstevel@tonic-gate int 36840Sstevel@tonic-gate scf_transaction_property_change(scf_transaction_t *tx, 36850Sstevel@tonic-gate scf_transaction_entry_t *entry, const char *prop, scf_type_t type) 36860Sstevel@tonic-gate { 36870Sstevel@tonic-gate return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_CLEAR, 36880Sstevel@tonic-gate prop, scf_type_to_protocol_type(type))); 36890Sstevel@tonic-gate } 36900Sstevel@tonic-gate 36910Sstevel@tonic-gate int 36920Sstevel@tonic-gate scf_transaction_property_change_type(scf_transaction_t *tx, 36930Sstevel@tonic-gate scf_transaction_entry_t *entry, const char *prop, scf_type_t type) 36940Sstevel@tonic-gate { 36950Sstevel@tonic-gate return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_REPLACE, 36960Sstevel@tonic-gate prop, scf_type_to_protocol_type(type))); 36970Sstevel@tonic-gate } 36980Sstevel@tonic-gate 36990Sstevel@tonic-gate int 37000Sstevel@tonic-gate scf_transaction_property_delete(scf_transaction_t *tx, 37010Sstevel@tonic-gate scf_transaction_entry_t *entry, const char *prop) 37020Sstevel@tonic-gate { 37030Sstevel@tonic-gate return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_DELETE, 37040Sstevel@tonic-gate prop, REP_PROTOCOL_TYPE_INVALID)); 37050Sstevel@tonic-gate } 37060Sstevel@tonic-gate 37070Sstevel@tonic-gate #define BAD_SIZE (-1UL) 37080Sstevel@tonic-gate 37090Sstevel@tonic-gate static size_t 37100Sstevel@tonic-gate commit_value(caddr_t data, scf_value_t *val, rep_protocol_value_type_t t) 37110Sstevel@tonic-gate { 37120Sstevel@tonic-gate size_t len; 37130Sstevel@tonic-gate 37140Sstevel@tonic-gate assert(val->value_type == t); 37150Sstevel@tonic-gate 37160Sstevel@tonic-gate if (t == REP_PROTOCOL_TYPE_OPAQUE) { 37170Sstevel@tonic-gate len = scf_opaque_encode(data, val->value_value, 37180Sstevel@tonic-gate val->value_size); 37190Sstevel@tonic-gate } else { 37200Sstevel@tonic-gate if (data != NULL) 37210Sstevel@tonic-gate len = strlcpy(data, val->value_value, 37220Sstevel@tonic-gate REP_PROTOCOL_VALUE_LEN); 37230Sstevel@tonic-gate else 37240Sstevel@tonic-gate len = strlen(val->value_value); 37250Sstevel@tonic-gate if (len >= REP_PROTOCOL_VALUE_LEN) 37260Sstevel@tonic-gate return (BAD_SIZE); 37270Sstevel@tonic-gate } 37280Sstevel@tonic-gate return (len + 1); /* count the '\0' */ 37290Sstevel@tonic-gate } 37300Sstevel@tonic-gate 37310Sstevel@tonic-gate static size_t 37320Sstevel@tonic-gate commit_process(scf_transaction_entry_t *cur, 37330Sstevel@tonic-gate struct rep_protocol_transaction_cmd *out) 37340Sstevel@tonic-gate { 37350Sstevel@tonic-gate scf_value_t *child; 37360Sstevel@tonic-gate size_t sz = 0; 37370Sstevel@tonic-gate size_t len; 37380Sstevel@tonic-gate caddr_t data = (caddr_t)out->rptc_data; 37390Sstevel@tonic-gate caddr_t val_data; 37400Sstevel@tonic-gate 37410Sstevel@tonic-gate if (out != NULL) { 37420Sstevel@tonic-gate len = strlcpy(data, cur->entry_property, REP_PROTOCOL_NAME_LEN); 37430Sstevel@tonic-gate 37440Sstevel@tonic-gate out->rptc_action = cur->entry_action; 37450Sstevel@tonic-gate out->rptc_type = cur->entry_type; 37460Sstevel@tonic-gate out->rptc_name_len = len + 1; 37470Sstevel@tonic-gate } else { 37480Sstevel@tonic-gate len = strlen(cur->entry_property); 37490Sstevel@tonic-gate } 37500Sstevel@tonic-gate 37510Sstevel@tonic-gate if (len >= REP_PROTOCOL_NAME_LEN) 37520Sstevel@tonic-gate return (BAD_SIZE); 37530Sstevel@tonic-gate 37540Sstevel@tonic-gate len = TX_SIZE(len + 1); 37550Sstevel@tonic-gate 37560Sstevel@tonic-gate sz += len; 37570Sstevel@tonic-gate val_data = data + len; 37580Sstevel@tonic-gate 37590Sstevel@tonic-gate for (child = cur->entry_head; child != NULL; 37600Sstevel@tonic-gate child = child->value_next) { 37610Sstevel@tonic-gate assert(cur->entry_action != REP_PROTOCOL_TX_ENTRY_DELETE); 37620Sstevel@tonic-gate if (out != NULL) { 37630Sstevel@tonic-gate len = commit_value(val_data + sizeof (uint32_t), child, 37640Sstevel@tonic-gate cur->entry_type); 37650Sstevel@tonic-gate /* LINTED alignment */ 37660Sstevel@tonic-gate *(uint32_t *)val_data = len; 37670Sstevel@tonic-gate } else 37680Sstevel@tonic-gate len = commit_value(NULL, child, cur->entry_type); 37690Sstevel@tonic-gate 37700Sstevel@tonic-gate if (len == BAD_SIZE) 37710Sstevel@tonic-gate return (BAD_SIZE); 37720Sstevel@tonic-gate 37730Sstevel@tonic-gate len += sizeof (uint32_t); 37740Sstevel@tonic-gate len = TX_SIZE(len); 37750Sstevel@tonic-gate 37760Sstevel@tonic-gate sz += len; 37770Sstevel@tonic-gate val_data += len; 37780Sstevel@tonic-gate } 37790Sstevel@tonic-gate 37800Sstevel@tonic-gate assert(val_data - data == sz); 37810Sstevel@tonic-gate 37820Sstevel@tonic-gate if (out != NULL) 37830Sstevel@tonic-gate out->rptc_size = REP_PROTOCOL_TRANSACTION_CMD_SIZE(sz); 37840Sstevel@tonic-gate 37850Sstevel@tonic-gate return (REP_PROTOCOL_TRANSACTION_CMD_SIZE(sz)); 37860Sstevel@tonic-gate } 37870Sstevel@tonic-gate 37880Sstevel@tonic-gate int 37890Sstevel@tonic-gate scf_transaction_commit(scf_transaction_t *tran) 37900Sstevel@tonic-gate { 37910Sstevel@tonic-gate scf_handle_t *h = tran->tran_pg.rd_d.rd_handle; 37920Sstevel@tonic-gate 37930Sstevel@tonic-gate struct rep_protocol_transaction_commit *request; 37940Sstevel@tonic-gate struct rep_protocol_response response; 37950Sstevel@tonic-gate uintptr_t cmd; 37960Sstevel@tonic-gate scf_transaction_entry_t *cur; 37970Sstevel@tonic-gate size_t total, size; 37980Sstevel@tonic-gate size_t request_size; 37990Sstevel@tonic-gate size_t new_total; 38000Sstevel@tonic-gate int r; 38010Sstevel@tonic-gate 38020Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 38030Sstevel@tonic-gate if (tran->tran_state != TRAN_STATE_SETUP || 38040Sstevel@tonic-gate tran->tran_invalid) { 38050Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 38060Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 38070Sstevel@tonic-gate } 38080Sstevel@tonic-gate 38090Sstevel@tonic-gate total = 0; 38100Sstevel@tonic-gate for (cur = uu_list_first(tran->tran_props); cur != NULL; 38110Sstevel@tonic-gate cur = uu_list_next(tran->tran_props, cur)) { 38120Sstevel@tonic-gate size = commit_process(cur, NULL); 38130Sstevel@tonic-gate if (size == BAD_SIZE) { 38140Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 38150Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 38160Sstevel@tonic-gate } 38170Sstevel@tonic-gate assert(TX_SIZE(size) == size); 38180Sstevel@tonic-gate total += size; 38190Sstevel@tonic-gate } 38200Sstevel@tonic-gate 38210Sstevel@tonic-gate request_size = REP_PROTOCOL_TRANSACTION_COMMIT_SIZE(total); 38220Sstevel@tonic-gate request = alloca(request_size); 38230Sstevel@tonic-gate (void) memset(request, '\0', request_size); 38240Sstevel@tonic-gate request->rpr_request = REP_PROTOCOL_PROPERTYGRP_TX_COMMIT; 38250Sstevel@tonic-gate request->rpr_entityid = tran->tran_pg.rd_d.rd_entity; 38260Sstevel@tonic-gate request->rpr_size = request_size; 38270Sstevel@tonic-gate cmd = (uintptr_t)request->rpr_cmd; 38280Sstevel@tonic-gate 38290Sstevel@tonic-gate datael_finish_reset(&tran->tran_pg.rd_d); 38300Sstevel@tonic-gate 38310Sstevel@tonic-gate new_total = 0; 38320Sstevel@tonic-gate for (cur = uu_list_first(tran->tran_props); cur != NULL; 38330Sstevel@tonic-gate cur = uu_list_next(tran->tran_props, cur)) { 38340Sstevel@tonic-gate size = commit_process(cur, (void *)cmd); 38350Sstevel@tonic-gate if (size == BAD_SIZE) { 38360Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 38370Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 38380Sstevel@tonic-gate } 38390Sstevel@tonic-gate cmd += size; 38400Sstevel@tonic-gate new_total += size; 38410Sstevel@tonic-gate } 38420Sstevel@tonic-gate assert(new_total == total); 38430Sstevel@tonic-gate 38440Sstevel@tonic-gate r = make_door_call(h, request, request_size, 38450Sstevel@tonic-gate &response, sizeof (response)); 38460Sstevel@tonic-gate 38470Sstevel@tonic-gate if (r < 0) { 38480Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 38490Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 38500Sstevel@tonic-gate } 38510Sstevel@tonic-gate 38520Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS && 38530Sstevel@tonic-gate response.rpr_response != REP_PROTOCOL_FAIL_NOT_LATEST) { 38540Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 38550Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 38560Sstevel@tonic-gate } 38570Sstevel@tonic-gate 38580Sstevel@tonic-gate tran->tran_state = TRAN_STATE_COMMITTED; 38590Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 38600Sstevel@tonic-gate return (response.rpr_response == REP_PROTOCOL_SUCCESS); 38610Sstevel@tonic-gate } 38620Sstevel@tonic-gate 38630Sstevel@tonic-gate static void 38640Sstevel@tonic-gate transaction_reset(scf_transaction_t *tran) 38650Sstevel@tonic-gate { 38660Sstevel@tonic-gate assert(MUTEX_HELD(&tran->tran_pg.rd_d.rd_handle->rh_lock)); 38670Sstevel@tonic-gate 38680Sstevel@tonic-gate tran->tran_state = TRAN_STATE_NEW; 38690Sstevel@tonic-gate datael_reset_locked(&tran->tran_pg.rd_d); 38700Sstevel@tonic-gate } 38710Sstevel@tonic-gate 38720Sstevel@tonic-gate static void 38730Sstevel@tonic-gate scf_transaction_reset_impl(scf_transaction_t *tran, int and_destroy, 38740Sstevel@tonic-gate int and_reset_value) 38750Sstevel@tonic-gate { 38760Sstevel@tonic-gate scf_transaction_entry_t *cur; 38770Sstevel@tonic-gate void *cookie; 38780Sstevel@tonic-gate 38790Sstevel@tonic-gate (void) pthread_mutex_lock(&tran->tran_pg.rd_d.rd_handle->rh_lock); 38800Sstevel@tonic-gate cookie = NULL; 38810Sstevel@tonic-gate while ((cur = uu_list_teardown(tran->tran_props, &cookie)) != NULL) { 38820Sstevel@tonic-gate cur->entry_tx = NULL; 38830Sstevel@tonic-gate 38840Sstevel@tonic-gate assert(cur->entry_state == ENTRY_STATE_IN_TX_ACTION); 38850Sstevel@tonic-gate cur->entry_state = ENTRY_STATE_INVALID; 38860Sstevel@tonic-gate 38870Sstevel@tonic-gate entry_invalidate(cur, and_destroy, and_reset_value); 38880Sstevel@tonic-gate if (and_destroy) 38890Sstevel@tonic-gate entry_destroy_locked(cur); 38900Sstevel@tonic-gate } 38910Sstevel@tonic-gate transaction_reset(tran); 38920Sstevel@tonic-gate handle_unrefed(tran->tran_pg.rd_d.rd_handle); 38930Sstevel@tonic-gate } 38940Sstevel@tonic-gate 38950Sstevel@tonic-gate void 38960Sstevel@tonic-gate scf_transaction_reset(scf_transaction_t *tran) 38970Sstevel@tonic-gate { 38980Sstevel@tonic-gate scf_transaction_reset_impl(tran, 0, 0); 38990Sstevel@tonic-gate } 39000Sstevel@tonic-gate 39010Sstevel@tonic-gate void 39020Sstevel@tonic-gate scf_transaction_reset_all(scf_transaction_t *tran) 39030Sstevel@tonic-gate { 39040Sstevel@tonic-gate scf_transaction_reset_impl(tran, 0, 1); 39050Sstevel@tonic-gate } 39060Sstevel@tonic-gate 39070Sstevel@tonic-gate void 39080Sstevel@tonic-gate scf_transaction_destroy(scf_transaction_t *val) 39090Sstevel@tonic-gate { 39100Sstevel@tonic-gate if (val == NULL) 39110Sstevel@tonic-gate return; 39120Sstevel@tonic-gate 39130Sstevel@tonic-gate scf_transaction_reset(val); 39140Sstevel@tonic-gate 39150Sstevel@tonic-gate datael_destroy(&val->tran_pg.rd_d); 39160Sstevel@tonic-gate 39170Sstevel@tonic-gate uu_list_destroy(val->tran_props); 39180Sstevel@tonic-gate uu_free(val); 39190Sstevel@tonic-gate } 39200Sstevel@tonic-gate 39210Sstevel@tonic-gate void 39220Sstevel@tonic-gate scf_transaction_destroy_children(scf_transaction_t *tran) 39230Sstevel@tonic-gate { 39240Sstevel@tonic-gate scf_transaction_reset_impl(tran, 1, 0); 39250Sstevel@tonic-gate } 39260Sstevel@tonic-gate 39270Sstevel@tonic-gate scf_transaction_entry_t * 39280Sstevel@tonic-gate scf_entry_create(scf_handle_t *h) 39290Sstevel@tonic-gate { 39300Sstevel@tonic-gate scf_transaction_entry_t *ret; 39310Sstevel@tonic-gate 39320Sstevel@tonic-gate if (h == NULL) { 39330Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 39340Sstevel@tonic-gate return (NULL); 39350Sstevel@tonic-gate } 39360Sstevel@tonic-gate 39370Sstevel@tonic-gate ret = uu_zalloc(sizeof (scf_transaction_entry_t)); 39380Sstevel@tonic-gate if (ret == NULL) { 39390Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 39400Sstevel@tonic-gate return (NULL); 39410Sstevel@tonic-gate } 39420Sstevel@tonic-gate ret->entry_action = REP_PROTOCOL_TX_ENTRY_INVALID; 39430Sstevel@tonic-gate ret->entry_handle = h; 39440Sstevel@tonic-gate 39450Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 39460Sstevel@tonic-gate if (h->rh_flags & HANDLE_DEAD) { 39470Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 39480Sstevel@tonic-gate uu_free(ret); 39490Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_HANDLE_DESTROYED); 39500Sstevel@tonic-gate return (NULL); 39510Sstevel@tonic-gate } 39520Sstevel@tonic-gate h->rh_entries++; 39530Sstevel@tonic-gate h->rh_extrefs++; 39540Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 39550Sstevel@tonic-gate 39560Sstevel@tonic-gate uu_list_node_init(ret, &ret->entry_link, tran_entry_pool); 39570Sstevel@tonic-gate 39580Sstevel@tonic-gate return (ret); 39590Sstevel@tonic-gate } 39600Sstevel@tonic-gate 39610Sstevel@tonic-gate scf_handle_t * 39620Sstevel@tonic-gate scf_entry_handle(const scf_transaction_entry_t *val) 39630Sstevel@tonic-gate { 39640Sstevel@tonic-gate return (handle_get(val->entry_handle)); 39650Sstevel@tonic-gate } 39660Sstevel@tonic-gate 39670Sstevel@tonic-gate void 39680Sstevel@tonic-gate scf_entry_reset(scf_transaction_entry_t *entry) 39690Sstevel@tonic-gate { 39700Sstevel@tonic-gate scf_handle_t *h = entry->entry_handle; 39710Sstevel@tonic-gate 39720Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 39730Sstevel@tonic-gate entry_invalidate(entry, 0, 0); 39740Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 39750Sstevel@tonic-gate } 39760Sstevel@tonic-gate 39770Sstevel@tonic-gate void 39780Sstevel@tonic-gate scf_entry_destroy_children(scf_transaction_entry_t *entry) 39790Sstevel@tonic-gate { 39800Sstevel@tonic-gate scf_handle_t *h = entry->entry_handle; 39810Sstevel@tonic-gate 39820Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 39830Sstevel@tonic-gate entry_invalidate(entry, 1, 0); 39840Sstevel@tonic-gate handle_unrefed(h); /* drops h->rh_lock */ 39850Sstevel@tonic-gate } 39860Sstevel@tonic-gate 39870Sstevel@tonic-gate void 39880Sstevel@tonic-gate scf_entry_destroy(scf_transaction_entry_t *entry) 39890Sstevel@tonic-gate { 39900Sstevel@tonic-gate scf_handle_t *h; 39910Sstevel@tonic-gate 39920Sstevel@tonic-gate if (entry == NULL) 39930Sstevel@tonic-gate return; 39940Sstevel@tonic-gate 39950Sstevel@tonic-gate h = entry->entry_handle; 39960Sstevel@tonic-gate 39970Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 39980Sstevel@tonic-gate entry_destroy_locked(entry); 39990Sstevel@tonic-gate handle_unrefed(h); /* drops h->rh_lock */ 40000Sstevel@tonic-gate } 40010Sstevel@tonic-gate 40020Sstevel@tonic-gate /* 40030Sstevel@tonic-gate * Fails with 40040Sstevel@tonic-gate * _HANDLE_MISMATCH 40050Sstevel@tonic-gate * _NOT_SET - has not been added to a transaction 40060Sstevel@tonic-gate * _INTERNAL - entry is corrupt 40070Sstevel@tonic-gate * _INVALID_ARGUMENT - entry's transaction is not started or corrupt 40080Sstevel@tonic-gate * entry is set to delete a property 40090Sstevel@tonic-gate * v is reset or corrupt 40100Sstevel@tonic-gate * _TYPE_MISMATCH - entry & v's types aren't compatible 40110Sstevel@tonic-gate * _IN_USE - v has been added to another entry 40120Sstevel@tonic-gate */ 40130Sstevel@tonic-gate int 40140Sstevel@tonic-gate scf_entry_add_value(scf_transaction_entry_t *entry, scf_value_t *v) 40150Sstevel@tonic-gate { 40160Sstevel@tonic-gate scf_handle_t *h = entry->entry_handle; 40170Sstevel@tonic-gate 40180Sstevel@tonic-gate if (h != v->value_handle) 40190Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 40200Sstevel@tonic-gate 40210Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 40220Sstevel@tonic-gate 40230Sstevel@tonic-gate if (entry->entry_state == ENTRY_STATE_INVALID) { 40240Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40250Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET)); 40260Sstevel@tonic-gate } 4027*2832Sjeanm 4028*2832Sjeanm if (entry->entry_state != ENTRY_STATE_IN_TX_ACTION) { 4029*2832Sjeanm (void) pthread_mutex_unlock(&h->rh_lock); 4030*2832Sjeanm return (scf_set_error(SCF_ERROR_INTERNAL)); 4031*2832Sjeanm } 40320Sstevel@tonic-gate 40330Sstevel@tonic-gate if (entry->entry_tx->tran_state != TRAN_STATE_SETUP) { 40340Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40350Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 40360Sstevel@tonic-gate } 40370Sstevel@tonic-gate 40380Sstevel@tonic-gate if (entry->entry_action == REP_PROTOCOL_TX_ENTRY_DELETE) { 40390Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40400Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 40410Sstevel@tonic-gate } 40420Sstevel@tonic-gate 40430Sstevel@tonic-gate if (v->value_type == REP_PROTOCOL_TYPE_INVALID) { 40440Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40450Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 40460Sstevel@tonic-gate } 40470Sstevel@tonic-gate 40480Sstevel@tonic-gate if (!scf_is_compatible_type(entry->entry_type, v->value_type)) { 40490Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40500Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_TYPE_MISMATCH)); 40510Sstevel@tonic-gate } 40520Sstevel@tonic-gate 40530Sstevel@tonic-gate if (v->value_tx != NULL) { 40540Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40550Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_IN_USE)); 40560Sstevel@tonic-gate } 40570Sstevel@tonic-gate 40580Sstevel@tonic-gate v->value_tx = entry; 40590Sstevel@tonic-gate v->value_next = entry->entry_head; 40600Sstevel@tonic-gate entry->entry_head = v; 40610Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40620Sstevel@tonic-gate 40630Sstevel@tonic-gate return (SCF_SUCCESS); 40640Sstevel@tonic-gate } 40650Sstevel@tonic-gate 40660Sstevel@tonic-gate /* 40670Sstevel@tonic-gate * value functions 40680Sstevel@tonic-gate */ 40690Sstevel@tonic-gate scf_value_t * 40700Sstevel@tonic-gate scf_value_create(scf_handle_t *h) 40710Sstevel@tonic-gate { 40720Sstevel@tonic-gate scf_value_t *ret; 40730Sstevel@tonic-gate 40740Sstevel@tonic-gate if (h == NULL) { 40750Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 40760Sstevel@tonic-gate return (NULL); 40770Sstevel@tonic-gate } 40780Sstevel@tonic-gate 40790Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 40800Sstevel@tonic-gate if (ret != NULL) { 40810Sstevel@tonic-gate ret->value_type = REP_PROTOCOL_TYPE_INVALID; 40820Sstevel@tonic-gate ret->value_handle = h; 40830Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 40840Sstevel@tonic-gate if (h->rh_flags & HANDLE_DEAD) { 40850Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40860Sstevel@tonic-gate uu_free(ret); 40870Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 40880Sstevel@tonic-gate return (NULL); 40890Sstevel@tonic-gate } 40900Sstevel@tonic-gate h->rh_values++; 40910Sstevel@tonic-gate h->rh_extrefs++; 40920Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 40930Sstevel@tonic-gate } else { 40940Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY); 40950Sstevel@tonic-gate } 40960Sstevel@tonic-gate 40970Sstevel@tonic-gate return (ret); 40980Sstevel@tonic-gate } 40990Sstevel@tonic-gate 41000Sstevel@tonic-gate static void 41010Sstevel@tonic-gate scf_value_reset_locked(scf_value_t *val, int and_destroy) 41020Sstevel@tonic-gate { 41030Sstevel@tonic-gate scf_value_t **curp; 41040Sstevel@tonic-gate scf_transaction_entry_t *te; 41050Sstevel@tonic-gate 41060Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 41070Sstevel@tonic-gate assert(MUTEX_HELD(&h->rh_lock)); 41080Sstevel@tonic-gate if (val->value_tx != NULL) { 41090Sstevel@tonic-gate te = val->value_tx; 41100Sstevel@tonic-gate te->entry_tx->tran_invalid = 1; 41110Sstevel@tonic-gate 41120Sstevel@tonic-gate val->value_tx = NULL; 41130Sstevel@tonic-gate 41140Sstevel@tonic-gate for (curp = &te->entry_head; *curp != NULL; 41150Sstevel@tonic-gate curp = &(*curp)->value_next) { 41160Sstevel@tonic-gate if (*curp == val) { 41170Sstevel@tonic-gate *curp = val->value_next; 41180Sstevel@tonic-gate curp = NULL; 41190Sstevel@tonic-gate break; 41200Sstevel@tonic-gate } 41210Sstevel@tonic-gate } 41220Sstevel@tonic-gate assert(curp == NULL); 41230Sstevel@tonic-gate } 41240Sstevel@tonic-gate val->value_type = REP_PROTOCOL_TYPE_INVALID; 41250Sstevel@tonic-gate 41260Sstevel@tonic-gate if (and_destroy) { 41270Sstevel@tonic-gate val->value_handle = NULL; 41280Sstevel@tonic-gate assert(h->rh_values > 0); 41290Sstevel@tonic-gate --h->rh_values; 41300Sstevel@tonic-gate --h->rh_extrefs; 41310Sstevel@tonic-gate uu_free(val); 41320Sstevel@tonic-gate } 41330Sstevel@tonic-gate } 41340Sstevel@tonic-gate 41350Sstevel@tonic-gate void 41360Sstevel@tonic-gate scf_value_reset(scf_value_t *val) 41370Sstevel@tonic-gate { 41380Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 41390Sstevel@tonic-gate 41400Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 41410Sstevel@tonic-gate scf_value_reset_locked(val, 0); 41420Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 41430Sstevel@tonic-gate } 41440Sstevel@tonic-gate 41450Sstevel@tonic-gate scf_handle_t * 41460Sstevel@tonic-gate scf_value_handle(const scf_value_t *val) 41470Sstevel@tonic-gate { 41480Sstevel@tonic-gate return (handle_get(val->value_handle)); 41490Sstevel@tonic-gate } 41500Sstevel@tonic-gate 41510Sstevel@tonic-gate void 41520Sstevel@tonic-gate scf_value_destroy(scf_value_t *val) 41530Sstevel@tonic-gate { 41540Sstevel@tonic-gate scf_handle_t *h; 41550Sstevel@tonic-gate 41560Sstevel@tonic-gate if (val == NULL) 41570Sstevel@tonic-gate return; 41580Sstevel@tonic-gate 41590Sstevel@tonic-gate h = val->value_handle; 41600Sstevel@tonic-gate 41610Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 41620Sstevel@tonic-gate scf_value_reset_locked(val, 1); 41630Sstevel@tonic-gate handle_unrefed(h); /* drops h->rh_lock */ 41640Sstevel@tonic-gate } 41650Sstevel@tonic-gate 41660Sstevel@tonic-gate scf_type_t 41670Sstevel@tonic-gate scf_value_base_type(const scf_value_t *val) 41680Sstevel@tonic-gate { 41690Sstevel@tonic-gate rep_protocol_value_type_t t, cur; 41700Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 41710Sstevel@tonic-gate 41720Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 41730Sstevel@tonic-gate t = val->value_type; 41740Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 41750Sstevel@tonic-gate 41760Sstevel@tonic-gate for (;;) { 41770Sstevel@tonic-gate cur = scf_proto_underlying_type(t); 41780Sstevel@tonic-gate if (cur == t) 41790Sstevel@tonic-gate break; 41800Sstevel@tonic-gate t = cur; 41810Sstevel@tonic-gate } 41820Sstevel@tonic-gate 41830Sstevel@tonic-gate return (scf_protocol_type_to_type(t)); 41840Sstevel@tonic-gate } 41850Sstevel@tonic-gate 41860Sstevel@tonic-gate scf_type_t 41870Sstevel@tonic-gate scf_value_type(const scf_value_t *val) 41880Sstevel@tonic-gate { 41890Sstevel@tonic-gate rep_protocol_value_type_t t; 41900Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 41910Sstevel@tonic-gate 41920Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 41930Sstevel@tonic-gate t = val->value_type; 41940Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 41950Sstevel@tonic-gate 41960Sstevel@tonic-gate return (scf_protocol_type_to_type(t)); 41970Sstevel@tonic-gate } 41980Sstevel@tonic-gate 41990Sstevel@tonic-gate int 42000Sstevel@tonic-gate scf_value_is_type(const scf_value_t *val, scf_type_t base_arg) 42010Sstevel@tonic-gate { 42020Sstevel@tonic-gate rep_protocol_value_type_t t; 42030Sstevel@tonic-gate rep_protocol_value_type_t base = scf_type_to_protocol_type(base_arg); 42040Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 42050Sstevel@tonic-gate 42060Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 42070Sstevel@tonic-gate t = val->value_type; 42080Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 42090Sstevel@tonic-gate 42100Sstevel@tonic-gate if (t == REP_PROTOCOL_TYPE_INVALID) 42110Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET)); 42120Sstevel@tonic-gate if (base == REP_PROTOCOL_TYPE_INVALID) 42130Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 42140Sstevel@tonic-gate if (!scf_is_compatible_type(base, t)) 42150Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_TYPE_MISMATCH)); 42160Sstevel@tonic-gate 42170Sstevel@tonic-gate return (SCF_SUCCESS); 42180Sstevel@tonic-gate } 42190Sstevel@tonic-gate 42200Sstevel@tonic-gate /* 42210Sstevel@tonic-gate * Fails with 42220Sstevel@tonic-gate * _NOT_SET - val is reset 42230Sstevel@tonic-gate * _TYPE_MISMATCH - val's type is not compatible with t 42240Sstevel@tonic-gate */ 42250Sstevel@tonic-gate static int 42260Sstevel@tonic-gate scf_value_check_type(const scf_value_t *val, rep_protocol_value_type_t t) 42270Sstevel@tonic-gate { 42280Sstevel@tonic-gate if (val->value_type == REP_PROTOCOL_TYPE_INVALID) { 42290Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_SET); 42300Sstevel@tonic-gate return (0); 42310Sstevel@tonic-gate } 42320Sstevel@tonic-gate if (!scf_is_compatible_type(t, val->value_type)) { 42330Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_TYPE_MISMATCH); 42340Sstevel@tonic-gate return (0); 42350Sstevel@tonic-gate } 42360Sstevel@tonic-gate return (1); 42370Sstevel@tonic-gate } 42380Sstevel@tonic-gate 42390Sstevel@tonic-gate /* 42400Sstevel@tonic-gate * Fails with 42410Sstevel@tonic-gate * _NOT_SET - val is reset 42420Sstevel@tonic-gate * _TYPE_MISMATCH - val is not _TYPE_BOOLEAN 42430Sstevel@tonic-gate */ 42440Sstevel@tonic-gate int 42450Sstevel@tonic-gate scf_value_get_boolean(const scf_value_t *val, uint8_t *out) 42460Sstevel@tonic-gate { 42470Sstevel@tonic-gate char c; 42480Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 42490Sstevel@tonic-gate uint8_t o; 42500Sstevel@tonic-gate 42510Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 42520Sstevel@tonic-gate if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_BOOLEAN)) { 42530Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 42540Sstevel@tonic-gate return (-1); 42550Sstevel@tonic-gate } 42560Sstevel@tonic-gate 42570Sstevel@tonic-gate c = val->value_value[0]; 42580Sstevel@tonic-gate assert((c == '0' || c == '1') && val->value_value[1] == 0); 42590Sstevel@tonic-gate 42600Sstevel@tonic-gate o = (c != '0'); 42610Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 42620Sstevel@tonic-gate if (out != NULL) 42630Sstevel@tonic-gate *out = o; 42640Sstevel@tonic-gate return (SCF_SUCCESS); 42650Sstevel@tonic-gate } 42660Sstevel@tonic-gate 42670Sstevel@tonic-gate int 42680Sstevel@tonic-gate scf_value_get_count(const scf_value_t *val, uint64_t *out) 42690Sstevel@tonic-gate { 42700Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 42710Sstevel@tonic-gate uint64_t o; 42720Sstevel@tonic-gate 42730Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 42740Sstevel@tonic-gate if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_COUNT)) { 42750Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 42760Sstevel@tonic-gate return (-1); 42770Sstevel@tonic-gate } 42780Sstevel@tonic-gate 42790Sstevel@tonic-gate o = strtoull(val->value_value, NULL, 10); 42800Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 42810Sstevel@tonic-gate if (out != NULL) 42820Sstevel@tonic-gate *out = o; 42830Sstevel@tonic-gate return (SCF_SUCCESS); 42840Sstevel@tonic-gate } 42850Sstevel@tonic-gate 42860Sstevel@tonic-gate int 42870Sstevel@tonic-gate scf_value_get_integer(const scf_value_t *val, int64_t *out) 42880Sstevel@tonic-gate { 42890Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 42900Sstevel@tonic-gate int64_t o; 42910Sstevel@tonic-gate 42920Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 42930Sstevel@tonic-gate if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_INTEGER)) { 42940Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 42950Sstevel@tonic-gate return (-1); 42960Sstevel@tonic-gate } 42970Sstevel@tonic-gate 42980Sstevel@tonic-gate o = strtoll(val->value_value, NULL, 10); 42990Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43000Sstevel@tonic-gate if (out != NULL) 43010Sstevel@tonic-gate *out = o; 43020Sstevel@tonic-gate return (SCF_SUCCESS); 43030Sstevel@tonic-gate } 43040Sstevel@tonic-gate 43050Sstevel@tonic-gate int 43060Sstevel@tonic-gate scf_value_get_time(const scf_value_t *val, int64_t *sec_out, int32_t *nsec_out) 43070Sstevel@tonic-gate { 43080Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 43090Sstevel@tonic-gate char *p; 43100Sstevel@tonic-gate int64_t os; 43110Sstevel@tonic-gate int32_t ons; 43120Sstevel@tonic-gate 43130Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 43140Sstevel@tonic-gate if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_TIME)) { 43150Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43160Sstevel@tonic-gate return (-1); 43170Sstevel@tonic-gate } 43180Sstevel@tonic-gate 43190Sstevel@tonic-gate os = strtoll(val->value_value, &p, 10); 43200Sstevel@tonic-gate if (*p == '.') 43210Sstevel@tonic-gate ons = strtoul(p + 1, NULL, 10); 43220Sstevel@tonic-gate else 43230Sstevel@tonic-gate ons = 0; 43240Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43250Sstevel@tonic-gate if (sec_out != NULL) 43260Sstevel@tonic-gate *sec_out = os; 43270Sstevel@tonic-gate if (nsec_out != NULL) 43280Sstevel@tonic-gate *nsec_out = ons; 43290Sstevel@tonic-gate 43300Sstevel@tonic-gate return (SCF_SUCCESS); 43310Sstevel@tonic-gate } 43320Sstevel@tonic-gate 43330Sstevel@tonic-gate /* 43340Sstevel@tonic-gate * Fails with 43350Sstevel@tonic-gate * _NOT_SET - val is reset 43360Sstevel@tonic-gate * _TYPE_MISMATCH - val's type is not compatible with _TYPE_STRING. 43370Sstevel@tonic-gate */ 43380Sstevel@tonic-gate ssize_t 43390Sstevel@tonic-gate scf_value_get_astring(const scf_value_t *val, char *out, size_t len) 43400Sstevel@tonic-gate { 43410Sstevel@tonic-gate ssize_t ret; 43420Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 43430Sstevel@tonic-gate 43440Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 43450Sstevel@tonic-gate if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_STRING)) { 43460Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43470Sstevel@tonic-gate return ((ssize_t)-1); 43480Sstevel@tonic-gate } 43490Sstevel@tonic-gate ret = (ssize_t)strlcpy(out, val->value_value, len); 43500Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43510Sstevel@tonic-gate return (ret); 43520Sstevel@tonic-gate } 43530Sstevel@tonic-gate 43540Sstevel@tonic-gate ssize_t 43550Sstevel@tonic-gate scf_value_get_ustring(const scf_value_t *val, char *out, size_t len) 43560Sstevel@tonic-gate { 43570Sstevel@tonic-gate ssize_t ret; 43580Sstevel@tonic-gate scf_handle_t *h = val->value_handle; 43590Sstevel@tonic-gate 43600Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 43610Sstevel@tonic-gate if (!scf_value_check_type(val, REP_PROTOCOL_SUBTYPE_USTRING)) { 43620Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43630Sstevel@tonic-gate return ((ssize_t)-1); 43640Sstevel@tonic-gate } 43650Sstevel@tonic-gate ret = (ssize_t)strlcpy(out, val->value_value, len); 43660Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43670Sstevel@tonic-gate return (ret); 43680Sstevel@tonic-gate } 43690Sstevel@tonic-gate 43700Sstevel@tonic-gate ssize_t 43710Sstevel@tonic-gate scf_value_get_opaque(const scf_value_t *v, void *out, size_t len) 43720Sstevel@tonic-gate { 43730Sstevel@tonic-gate ssize_t ret; 43740Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 43750Sstevel@tonic-gate 43760Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 43770Sstevel@tonic-gate if (!scf_value_check_type(v, REP_PROTOCOL_TYPE_OPAQUE)) { 43780Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43790Sstevel@tonic-gate return ((ssize_t)-1); 43800Sstevel@tonic-gate } 43810Sstevel@tonic-gate if (len > v->value_size) 43820Sstevel@tonic-gate len = v->value_size; 43830Sstevel@tonic-gate ret = len; 43840Sstevel@tonic-gate 43850Sstevel@tonic-gate (void) memcpy(out, v->value_value, len); 43860Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 43870Sstevel@tonic-gate return (ret); 43880Sstevel@tonic-gate } 43890Sstevel@tonic-gate 43900Sstevel@tonic-gate void 43910Sstevel@tonic-gate scf_value_set_boolean(scf_value_t *v, uint8_t new) 43920Sstevel@tonic-gate { 43930Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 43940Sstevel@tonic-gate 43950Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 43960Sstevel@tonic-gate scf_value_reset_locked(v, 0); 43970Sstevel@tonic-gate v->value_type = REP_PROTOCOL_TYPE_BOOLEAN; 43980Sstevel@tonic-gate (void) sprintf(v->value_value, "%d", (new != 0)); 43990Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44000Sstevel@tonic-gate } 44010Sstevel@tonic-gate 44020Sstevel@tonic-gate void 44030Sstevel@tonic-gate scf_value_set_count(scf_value_t *v, uint64_t new) 44040Sstevel@tonic-gate { 44050Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 44060Sstevel@tonic-gate 44070Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 44080Sstevel@tonic-gate scf_value_reset_locked(v, 0); 44090Sstevel@tonic-gate v->value_type = REP_PROTOCOL_TYPE_COUNT; 44100Sstevel@tonic-gate (void) sprintf(v->value_value, "%llu", (unsigned long long)new); 44110Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44120Sstevel@tonic-gate } 44130Sstevel@tonic-gate 44140Sstevel@tonic-gate void 44150Sstevel@tonic-gate scf_value_set_integer(scf_value_t *v, int64_t new) 44160Sstevel@tonic-gate { 44170Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 44180Sstevel@tonic-gate 44190Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 44200Sstevel@tonic-gate scf_value_reset_locked(v, 0); 44210Sstevel@tonic-gate v->value_type = REP_PROTOCOL_TYPE_INTEGER; 44220Sstevel@tonic-gate (void) sprintf(v->value_value, "%lld", (long long)new); 44230Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44240Sstevel@tonic-gate } 44250Sstevel@tonic-gate 44260Sstevel@tonic-gate int 44270Sstevel@tonic-gate scf_value_set_time(scf_value_t *v, int64_t new_sec, int32_t new_nsec) 44280Sstevel@tonic-gate { 44290Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 44300Sstevel@tonic-gate 44310Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 44320Sstevel@tonic-gate scf_value_reset_locked(v, 0); 44330Sstevel@tonic-gate if (new_nsec < 0 || new_nsec >= NANOSEC) { 44340Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44350Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 44360Sstevel@tonic-gate } 44370Sstevel@tonic-gate v->value_type = REP_PROTOCOL_TYPE_TIME; 44380Sstevel@tonic-gate if (new_nsec == 0) 44390Sstevel@tonic-gate (void) sprintf(v->value_value, "%lld", (long long)new_sec); 44400Sstevel@tonic-gate else 44410Sstevel@tonic-gate (void) sprintf(v->value_value, "%lld.%09u", (long long)new_sec, 44420Sstevel@tonic-gate (unsigned)new_nsec); 44430Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44440Sstevel@tonic-gate return (0); 44450Sstevel@tonic-gate } 44460Sstevel@tonic-gate 44470Sstevel@tonic-gate int 44480Sstevel@tonic-gate scf_value_set_astring(scf_value_t *v, const char *new) 44490Sstevel@tonic-gate { 44500Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 44510Sstevel@tonic-gate 44520Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 44530Sstevel@tonic-gate scf_value_reset_locked(v, 0); 44540Sstevel@tonic-gate if (!scf_validate_encoded_value(REP_PROTOCOL_TYPE_STRING, new)) { 44550Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44560Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 44570Sstevel@tonic-gate } 44580Sstevel@tonic-gate if (strlcpy(v->value_value, new, sizeof (v->value_value)) >= 44590Sstevel@tonic-gate sizeof (v->value_value)) { 44600Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44610Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 44620Sstevel@tonic-gate } 44630Sstevel@tonic-gate v->value_type = REP_PROTOCOL_TYPE_STRING; 44640Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44650Sstevel@tonic-gate return (0); 44660Sstevel@tonic-gate } 44670Sstevel@tonic-gate 44680Sstevel@tonic-gate int 44690Sstevel@tonic-gate scf_value_set_ustring(scf_value_t *v, const char *new) 44700Sstevel@tonic-gate { 44710Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 44720Sstevel@tonic-gate 44730Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 44740Sstevel@tonic-gate scf_value_reset_locked(v, 0); 44750Sstevel@tonic-gate if (!scf_validate_encoded_value(REP_PROTOCOL_SUBTYPE_USTRING, new)) { 44760Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44770Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 44780Sstevel@tonic-gate } 44790Sstevel@tonic-gate if (strlcpy(v->value_value, new, sizeof (v->value_value)) >= 44800Sstevel@tonic-gate sizeof (v->value_value)) { 44810Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44820Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 44830Sstevel@tonic-gate } 44840Sstevel@tonic-gate v->value_type = REP_PROTOCOL_SUBTYPE_USTRING; 44850Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44860Sstevel@tonic-gate return (0); 44870Sstevel@tonic-gate } 44880Sstevel@tonic-gate 44890Sstevel@tonic-gate int 44900Sstevel@tonic-gate scf_value_set_opaque(scf_value_t *v, const void *new, size_t len) 44910Sstevel@tonic-gate { 44920Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 44930Sstevel@tonic-gate 44940Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 44950Sstevel@tonic-gate scf_value_reset_locked(v, 0); 44960Sstevel@tonic-gate if (len > sizeof (v->value_value)) { 44970Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 44980Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 44990Sstevel@tonic-gate } 45000Sstevel@tonic-gate (void) memcpy(v->value_value, new, len); 45010Sstevel@tonic-gate v->value_size = len; 45020Sstevel@tonic-gate v->value_type = REP_PROTOCOL_TYPE_OPAQUE; 45030Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 45040Sstevel@tonic-gate return (0); 45050Sstevel@tonic-gate } 45060Sstevel@tonic-gate 45070Sstevel@tonic-gate /* 45080Sstevel@tonic-gate * Fails with 45090Sstevel@tonic-gate * _NOT_SET - v_arg is reset 45100Sstevel@tonic-gate * _INTERNAL - v_arg is corrupt 45110Sstevel@tonic-gate * 45120Sstevel@tonic-gate * If t is not _TYPE_INVALID, fails with 45130Sstevel@tonic-gate * _TYPE_MISMATCH - v_arg's type is not compatible with t 45140Sstevel@tonic-gate */ 45150Sstevel@tonic-gate static ssize_t 45160Sstevel@tonic-gate scf_value_get_as_string_common(const scf_value_t *v_arg, 45170Sstevel@tonic-gate rep_protocol_value_type_t t, char *buf, size_t bufsz) 45180Sstevel@tonic-gate { 45190Sstevel@tonic-gate scf_handle_t *h = v_arg->value_handle; 45200Sstevel@tonic-gate scf_value_t v_s; 45210Sstevel@tonic-gate scf_value_t *v = &v_s; 45220Sstevel@tonic-gate ssize_t r; 45230Sstevel@tonic-gate uint8_t b; 45240Sstevel@tonic-gate 45250Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 45260Sstevel@tonic-gate if (t != REP_PROTOCOL_TYPE_INVALID && !scf_value_check_type(v_arg, t)) { 45270Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 45280Sstevel@tonic-gate return (-1); 45290Sstevel@tonic-gate } 45300Sstevel@tonic-gate 45310Sstevel@tonic-gate v_s = *v_arg; /* copy locally so we can unlock */ 45320Sstevel@tonic-gate h->rh_values++; /* keep the handle from going away */ 45330Sstevel@tonic-gate h->rh_extrefs++; 45340Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 45350Sstevel@tonic-gate 45360Sstevel@tonic-gate 45370Sstevel@tonic-gate switch (REP_PROTOCOL_BASE_TYPE(v->value_type)) { 45380Sstevel@tonic-gate case REP_PROTOCOL_TYPE_BOOLEAN: 45390Sstevel@tonic-gate r = scf_value_get_boolean(v, &b); 45400Sstevel@tonic-gate assert(r == SCF_SUCCESS); 45410Sstevel@tonic-gate 45420Sstevel@tonic-gate r = strlcpy(buf, b ? "true" : "false", bufsz); 45430Sstevel@tonic-gate break; 45440Sstevel@tonic-gate 45450Sstevel@tonic-gate case REP_PROTOCOL_TYPE_COUNT: 45460Sstevel@tonic-gate case REP_PROTOCOL_TYPE_INTEGER: 45470Sstevel@tonic-gate case REP_PROTOCOL_TYPE_TIME: 45480Sstevel@tonic-gate case REP_PROTOCOL_TYPE_STRING: 45490Sstevel@tonic-gate r = strlcpy(buf, v->value_value, bufsz); 45500Sstevel@tonic-gate break; 45510Sstevel@tonic-gate 45520Sstevel@tonic-gate case REP_PROTOCOL_TYPE_OPAQUE: 45530Sstevel@tonic-gate /* 45540Sstevel@tonic-gate * Note that we only write out full hex bytes -- if they're 45550Sstevel@tonic-gate * short, and bufsz is even, we'll only fill (bufsz - 2) bytes 45560Sstevel@tonic-gate * with data. 45570Sstevel@tonic-gate */ 45580Sstevel@tonic-gate if (bufsz > 0) 45590Sstevel@tonic-gate (void) scf_opaque_encode(buf, v->value_value, 45600Sstevel@tonic-gate MIN(v->value_size, (bufsz - 1)/2)); 45610Sstevel@tonic-gate r = (v->value_size * 2); 45620Sstevel@tonic-gate break; 45630Sstevel@tonic-gate 45640Sstevel@tonic-gate case REP_PROTOCOL_TYPE_INVALID: 45650Sstevel@tonic-gate r = scf_set_error(SCF_ERROR_NOT_SET); 45660Sstevel@tonic-gate break; 45670Sstevel@tonic-gate 45680Sstevel@tonic-gate default: 45690Sstevel@tonic-gate r = (scf_set_error(SCF_ERROR_INTERNAL)); 45700Sstevel@tonic-gate break; 45710Sstevel@tonic-gate } 45720Sstevel@tonic-gate 45730Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 45740Sstevel@tonic-gate h->rh_values--; 45750Sstevel@tonic-gate h->rh_extrefs--; 45760Sstevel@tonic-gate handle_unrefed(h); 45770Sstevel@tonic-gate 45780Sstevel@tonic-gate return (r); 45790Sstevel@tonic-gate } 45800Sstevel@tonic-gate 45810Sstevel@tonic-gate ssize_t 45820Sstevel@tonic-gate scf_value_get_as_string(const scf_value_t *v, char *buf, size_t bufsz) 45830Sstevel@tonic-gate { 45840Sstevel@tonic-gate return (scf_value_get_as_string_common(v, REP_PROTOCOL_TYPE_INVALID, 45850Sstevel@tonic-gate buf, bufsz)); 45860Sstevel@tonic-gate } 45870Sstevel@tonic-gate 45880Sstevel@tonic-gate ssize_t 45890Sstevel@tonic-gate scf_value_get_as_string_typed(const scf_value_t *v, scf_type_t type, 45900Sstevel@tonic-gate char *buf, size_t bufsz) 45910Sstevel@tonic-gate { 45920Sstevel@tonic-gate rep_protocol_value_type_t ty = scf_type_to_protocol_type(type); 45930Sstevel@tonic-gate if (ty == REP_PROTOCOL_TYPE_INVALID) 45940Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 45950Sstevel@tonic-gate 45960Sstevel@tonic-gate return (scf_value_get_as_string_common(v, ty, buf, bufsz)); 45970Sstevel@tonic-gate } 45980Sstevel@tonic-gate 45990Sstevel@tonic-gate int 46000Sstevel@tonic-gate scf_value_set_from_string(scf_value_t *v, scf_type_t type, const char *str) 46010Sstevel@tonic-gate { 46020Sstevel@tonic-gate scf_handle_t *h = v->value_handle; 46030Sstevel@tonic-gate rep_protocol_value_type_t ty; 46040Sstevel@tonic-gate 46050Sstevel@tonic-gate switch (type) { 46060Sstevel@tonic-gate case SCF_TYPE_BOOLEAN: { 46070Sstevel@tonic-gate uint8_t b; 46080Sstevel@tonic-gate 46090Sstevel@tonic-gate if (strcmp(str, "true") == 0 || strcmp(str, "t") == 0 || 46100Sstevel@tonic-gate strcmp(str, "1") == 0) 46110Sstevel@tonic-gate b = 1; 46120Sstevel@tonic-gate else if (strcmp(str, "false") == 0 || 46130Sstevel@tonic-gate strcmp(str, "f") == 0 || strcmp(str, "0") == 0) 46140Sstevel@tonic-gate b = 0; 46150Sstevel@tonic-gate else { 46160Sstevel@tonic-gate goto bad; 46170Sstevel@tonic-gate } 46180Sstevel@tonic-gate 46190Sstevel@tonic-gate scf_value_set_boolean(v, b); 46200Sstevel@tonic-gate return (0); 46210Sstevel@tonic-gate } 46220Sstevel@tonic-gate 46230Sstevel@tonic-gate case SCF_TYPE_COUNT: { 46240Sstevel@tonic-gate uint64_t c; 46250Sstevel@tonic-gate char *endp; 46260Sstevel@tonic-gate 46270Sstevel@tonic-gate errno = 0; 4628*2832Sjeanm c = strtoull(str, &endp, 0); 46290Sstevel@tonic-gate 46300Sstevel@tonic-gate if (errno != 0 || endp == str || *endp != '\0') 46310Sstevel@tonic-gate goto bad; 46320Sstevel@tonic-gate 46330Sstevel@tonic-gate scf_value_set_count(v, c); 46340Sstevel@tonic-gate return (0); 46350Sstevel@tonic-gate } 46360Sstevel@tonic-gate 46370Sstevel@tonic-gate case SCF_TYPE_INTEGER: { 46380Sstevel@tonic-gate int64_t i; 46390Sstevel@tonic-gate char *endp; 46400Sstevel@tonic-gate 46410Sstevel@tonic-gate errno = 0; 4642*2832Sjeanm i = strtoll(str, &endp, 0); 46430Sstevel@tonic-gate 46440Sstevel@tonic-gate if (errno != 0 || endp == str || *endp != '\0') 46450Sstevel@tonic-gate goto bad; 46460Sstevel@tonic-gate 46470Sstevel@tonic-gate scf_value_set_integer(v, i); 46480Sstevel@tonic-gate return (0); 46490Sstevel@tonic-gate } 46500Sstevel@tonic-gate 46510Sstevel@tonic-gate case SCF_TYPE_TIME: { 46520Sstevel@tonic-gate int64_t s; 46530Sstevel@tonic-gate uint32_t ns = 0; 46540Sstevel@tonic-gate char *endp, *ns_str; 46550Sstevel@tonic-gate size_t len; 46560Sstevel@tonic-gate 46570Sstevel@tonic-gate errno = 0; 46580Sstevel@tonic-gate s = strtoll(str, &endp, 10); 46590Sstevel@tonic-gate if (errno != 0 || endp == str || 46600Sstevel@tonic-gate (*endp != '\0' && *endp != '.')) 46610Sstevel@tonic-gate goto bad; 46620Sstevel@tonic-gate 46630Sstevel@tonic-gate if (*endp == '.') { 46640Sstevel@tonic-gate ns_str = endp + 1; 46650Sstevel@tonic-gate len = strlen(ns_str); 46660Sstevel@tonic-gate if (len == 0 || len > 9) 46670Sstevel@tonic-gate goto bad; 46680Sstevel@tonic-gate 46690Sstevel@tonic-gate ns = strtoul(ns_str, &endp, 10); 46700Sstevel@tonic-gate if (errno != 0 || endp == ns_str || *endp != '\0') 46710Sstevel@tonic-gate goto bad; 46720Sstevel@tonic-gate 46730Sstevel@tonic-gate while (len++ < 9) 46740Sstevel@tonic-gate ns *= 10; 46750Sstevel@tonic-gate assert(ns < NANOSEC); 46760Sstevel@tonic-gate } 46770Sstevel@tonic-gate 46780Sstevel@tonic-gate return (scf_value_set_time(v, s, ns)); 46790Sstevel@tonic-gate } 46800Sstevel@tonic-gate 46810Sstevel@tonic-gate case SCF_TYPE_ASTRING: 46820Sstevel@tonic-gate case SCF_TYPE_USTRING: 46830Sstevel@tonic-gate case SCF_TYPE_OPAQUE: 46840Sstevel@tonic-gate case SCF_TYPE_URI: 46850Sstevel@tonic-gate case SCF_TYPE_FMRI: 46860Sstevel@tonic-gate case SCF_TYPE_HOST: 46870Sstevel@tonic-gate case SCF_TYPE_HOSTNAME: 46880Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V4: 46890Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V6: 46900Sstevel@tonic-gate ty = scf_type_to_protocol_type(type); 46910Sstevel@tonic-gate 46920Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 46930Sstevel@tonic-gate scf_value_reset_locked(v, 0); 46940Sstevel@tonic-gate if (type == SCF_TYPE_OPAQUE) { 46950Sstevel@tonic-gate v->value_size = scf_opaque_decode(v->value_value, 46960Sstevel@tonic-gate str, sizeof (v->value_value)); 46970Sstevel@tonic-gate if (!scf_validate_encoded_value(ty, str)) { 46980Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 46990Sstevel@tonic-gate goto bad; 47000Sstevel@tonic-gate } 47010Sstevel@tonic-gate } else { 47020Sstevel@tonic-gate (void) strlcpy(v->value_value, str, 47030Sstevel@tonic-gate sizeof (v->value_value)); 47040Sstevel@tonic-gate if (!scf_validate_encoded_value(ty, v->value_value)) { 47050Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 47060Sstevel@tonic-gate goto bad; 47070Sstevel@tonic-gate } 47080Sstevel@tonic-gate } 47090Sstevel@tonic-gate v->value_type = ty; 47100Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 47110Sstevel@tonic-gate return (SCF_SUCCESS); 47120Sstevel@tonic-gate 47130Sstevel@tonic-gate case REP_PROTOCOL_TYPE_INVALID: 47140Sstevel@tonic-gate default: 47150Sstevel@tonic-gate scf_value_reset(v); 47160Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_TYPE_MISMATCH)); 47170Sstevel@tonic-gate } 47180Sstevel@tonic-gate bad: 47190Sstevel@tonic-gate scf_value_reset(v); 47200Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 47210Sstevel@tonic-gate } 47220Sstevel@tonic-gate 47230Sstevel@tonic-gate int 47240Sstevel@tonic-gate scf_iter_property_values(scf_iter_t *iter, const scf_property_t *prop) 47250Sstevel@tonic-gate { 47260Sstevel@tonic-gate return (datael_setup_iter(iter, &prop->rd_d, 47270Sstevel@tonic-gate REP_PROTOCOL_ENTITY_VALUE, 0)); 47280Sstevel@tonic-gate } 47290Sstevel@tonic-gate 47300Sstevel@tonic-gate int 47310Sstevel@tonic-gate scf_iter_next_value(scf_iter_t *iter, scf_value_t *v) 47320Sstevel@tonic-gate { 47330Sstevel@tonic-gate scf_handle_t *h = iter->iter_handle; 47340Sstevel@tonic-gate 47350Sstevel@tonic-gate struct rep_protocol_iter_read_value request; 47360Sstevel@tonic-gate struct rep_protocol_value_response response; 47370Sstevel@tonic-gate 47380Sstevel@tonic-gate int r; 47390Sstevel@tonic-gate 47400Sstevel@tonic-gate if (h != v->value_handle) 47410Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 47420Sstevel@tonic-gate 47430Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 47440Sstevel@tonic-gate 47450Sstevel@tonic-gate scf_value_reset_locked(v, 0); 47460Sstevel@tonic-gate 47470Sstevel@tonic-gate if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE) { 47480Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 47490Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET)); 47500Sstevel@tonic-gate } 47510Sstevel@tonic-gate 47520Sstevel@tonic-gate if (iter->iter_type != REP_PROTOCOL_ENTITY_VALUE) { 47530Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 47540Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 47550Sstevel@tonic-gate } 47560Sstevel@tonic-gate 47570Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ITER_READ_VALUE; 47580Sstevel@tonic-gate request.rpr_iterid = iter->iter_id; 47590Sstevel@tonic-gate request.rpr_sequence = iter->iter_sequence; 47600Sstevel@tonic-gate 47610Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 47620Sstevel@tonic-gate &response, sizeof (response)); 47630Sstevel@tonic-gate 47640Sstevel@tonic-gate if (r < 0) { 47650Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 47660Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 47670Sstevel@tonic-gate } 47680Sstevel@tonic-gate 47690Sstevel@tonic-gate if (response.rpr_response == REP_PROTOCOL_DONE) { 47700Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 47710Sstevel@tonic-gate return (0); 47720Sstevel@tonic-gate } 47730Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) { 47740Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 47750Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 47760Sstevel@tonic-gate } 47770Sstevel@tonic-gate iter->iter_sequence++; 47780Sstevel@tonic-gate 47790Sstevel@tonic-gate v->value_type = response.rpr_type; 47800Sstevel@tonic-gate 47810Sstevel@tonic-gate assert(scf_validate_encoded_value(response.rpr_type, 47820Sstevel@tonic-gate response.rpr_value)); 47830Sstevel@tonic-gate 47840Sstevel@tonic-gate if (v->value_type != REP_PROTOCOL_TYPE_OPAQUE) { 47850Sstevel@tonic-gate (void) strlcpy(v->value_value, response.rpr_value, 47860Sstevel@tonic-gate sizeof (v->value_value)); 47870Sstevel@tonic-gate } else { 47880Sstevel@tonic-gate v->value_size = scf_opaque_decode(v->value_value, 47890Sstevel@tonic-gate response.rpr_value, sizeof (v->value_value)); 47900Sstevel@tonic-gate } 47910Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 47920Sstevel@tonic-gate 47930Sstevel@tonic-gate return (1); 47940Sstevel@tonic-gate } 47950Sstevel@tonic-gate 47960Sstevel@tonic-gate int 47970Sstevel@tonic-gate scf_property_get_value(const scf_property_t *prop, scf_value_t *v) 47980Sstevel@tonic-gate { 47990Sstevel@tonic-gate scf_handle_t *h = prop->rd_d.rd_handle; 48000Sstevel@tonic-gate struct rep_protocol_property_request request; 48010Sstevel@tonic-gate struct rep_protocol_value_response response; 48020Sstevel@tonic-gate int r; 48030Sstevel@tonic-gate 48040Sstevel@tonic-gate if (h != v->value_handle) 48050Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 48060Sstevel@tonic-gate 48070Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 48080Sstevel@tonic-gate 48090Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_PROPERTY_GET_VALUE; 48100Sstevel@tonic-gate request.rpr_entityid = prop->rd_d.rd_entity; 48110Sstevel@tonic-gate 48120Sstevel@tonic-gate scf_value_reset_locked(v, 0); 48130Sstevel@tonic-gate datael_finish_reset(&prop->rd_d); 48140Sstevel@tonic-gate 48150Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 48160Sstevel@tonic-gate &response, sizeof (response)); 48170Sstevel@tonic-gate 48180Sstevel@tonic-gate if (r < 0) { 48190Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 48200Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 48210Sstevel@tonic-gate } 48220Sstevel@tonic-gate 48230Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS && 48240Sstevel@tonic-gate response.rpr_response != REP_PROTOCOL_FAIL_TRUNCATED) { 48250Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 48260Sstevel@tonic-gate assert(response.rpr_response != 48270Sstevel@tonic-gate REP_PROTOCOL_FAIL_TYPE_MISMATCH); 48280Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 48290Sstevel@tonic-gate } 48300Sstevel@tonic-gate 48310Sstevel@tonic-gate v->value_type = response.rpr_type; 48320Sstevel@tonic-gate if (v->value_type != REP_PROTOCOL_TYPE_OPAQUE) { 48330Sstevel@tonic-gate (void) strlcpy(v->value_value, response.rpr_value, 48340Sstevel@tonic-gate sizeof (v->value_value)); 48350Sstevel@tonic-gate } else { 48360Sstevel@tonic-gate v->value_size = scf_opaque_decode(v->value_value, 48370Sstevel@tonic-gate response.rpr_value, sizeof (v->value_value)); 48380Sstevel@tonic-gate } 48390Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 48400Sstevel@tonic-gate return ((response.rpr_response == REP_PROTOCOL_SUCCESS)? 48410Sstevel@tonic-gate SCF_SUCCESS : scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED)); 48420Sstevel@tonic-gate } 48430Sstevel@tonic-gate 48440Sstevel@tonic-gate int 48450Sstevel@tonic-gate scf_pg_get_parent_service(const scf_propertygroup_t *pg, scf_service_t *svc) 48460Sstevel@tonic-gate { 48470Sstevel@tonic-gate return (datael_get_parent(&pg->rd_d, &svc->rd_d)); 48480Sstevel@tonic-gate } 48490Sstevel@tonic-gate 48500Sstevel@tonic-gate int 48510Sstevel@tonic-gate scf_pg_get_parent_instance(const scf_propertygroup_t *pg, scf_instance_t *inst) 48520Sstevel@tonic-gate { 48530Sstevel@tonic-gate return (datael_get_parent(&pg->rd_d, &inst->rd_d)); 48540Sstevel@tonic-gate } 48550Sstevel@tonic-gate 48560Sstevel@tonic-gate int 48570Sstevel@tonic-gate scf_pg_get_parent_snaplevel(const scf_propertygroup_t *pg, 48580Sstevel@tonic-gate scf_snaplevel_t *level) 48590Sstevel@tonic-gate { 48600Sstevel@tonic-gate return (datael_get_parent(&pg->rd_d, &level->rd_d)); 48610Sstevel@tonic-gate } 48620Sstevel@tonic-gate 48630Sstevel@tonic-gate int 48640Sstevel@tonic-gate scf_service_get_parent(const scf_service_t *svc, scf_scope_t *s) 48650Sstevel@tonic-gate { 48660Sstevel@tonic-gate return (datael_get_parent(&svc->rd_d, &s->rd_d)); 48670Sstevel@tonic-gate } 48680Sstevel@tonic-gate 48690Sstevel@tonic-gate int 48700Sstevel@tonic-gate scf_instance_get_parent(const scf_instance_t *inst, scf_service_t *svc) 48710Sstevel@tonic-gate { 48720Sstevel@tonic-gate return (datael_get_parent(&inst->rd_d, &svc->rd_d)); 48730Sstevel@tonic-gate } 48740Sstevel@tonic-gate 48750Sstevel@tonic-gate int 48760Sstevel@tonic-gate scf_snapshot_get_parent(const scf_snapshot_t *inst, scf_instance_t *svc) 48770Sstevel@tonic-gate { 48780Sstevel@tonic-gate return (datael_get_parent(&inst->rd_d, &svc->rd_d)); 48790Sstevel@tonic-gate } 48800Sstevel@tonic-gate 48810Sstevel@tonic-gate int 48820Sstevel@tonic-gate scf_snaplevel_get_parent(const scf_snaplevel_t *inst, scf_snapshot_t *svc) 48830Sstevel@tonic-gate { 48840Sstevel@tonic-gate return (datael_get_parent(&inst->rd_d, &svc->rd_d)); 48850Sstevel@tonic-gate } 48860Sstevel@tonic-gate 48870Sstevel@tonic-gate /* 48880Sstevel@tonic-gate * FMRI functions 48890Sstevel@tonic-gate * 48900Sstevel@tonic-gate * Note: In the scf_parse_svc_fmri(), scf_parse_file_fmri() and 48910Sstevel@tonic-gate * scf_parse_fmri(), fmri isn't const because that would require 48920Sstevel@tonic-gate * allocating memory. Also, note that scope, at least, is not necessarily 48930Sstevel@tonic-gate * in the passed in fmri. 48940Sstevel@tonic-gate */ 48950Sstevel@tonic-gate 48960Sstevel@tonic-gate int 48970Sstevel@tonic-gate scf_parse_svc_fmri(char *fmri, const char **scope, const char **service, 48980Sstevel@tonic-gate const char **instance, const char **propertygroup, const char **property) 48990Sstevel@tonic-gate { 49000Sstevel@tonic-gate char *s, *e, *te, *tpg; 49010Sstevel@tonic-gate char *my_s = NULL, *my_i = NULL, *my_pg = NULL, *my_p = NULL; 49020Sstevel@tonic-gate 49030Sstevel@tonic-gate if (scope != NULL) 49040Sstevel@tonic-gate *scope = NULL; 49050Sstevel@tonic-gate if (service != NULL) 49060Sstevel@tonic-gate *service = NULL; 49070Sstevel@tonic-gate if (instance != NULL) 49080Sstevel@tonic-gate *instance = NULL; 49090Sstevel@tonic-gate if (propertygroup != NULL) 49100Sstevel@tonic-gate *propertygroup = NULL; 49110Sstevel@tonic-gate if (property != NULL) 49120Sstevel@tonic-gate *property = NULL; 49130Sstevel@tonic-gate 49140Sstevel@tonic-gate s = fmri; 49150Sstevel@tonic-gate e = strchr(s, '\0'); 49160Sstevel@tonic-gate 49170Sstevel@tonic-gate if (strncmp(s, SCF_FMRI_SVC_PREFIX, 49180Sstevel@tonic-gate sizeof (SCF_FMRI_SVC_PREFIX) - 1) == 0) 49190Sstevel@tonic-gate s += sizeof (SCF_FMRI_SVC_PREFIX) - 1; 49200Sstevel@tonic-gate 49210Sstevel@tonic-gate if (strncmp(s, SCF_FMRI_SCOPE_PREFIX, 49220Sstevel@tonic-gate sizeof (SCF_FMRI_SCOPE_PREFIX) - 1) == 0) { 49230Sstevel@tonic-gate char *my_scope; 49240Sstevel@tonic-gate 49250Sstevel@tonic-gate s += sizeof (SCF_FMRI_SCOPE_PREFIX) - 1; 49260Sstevel@tonic-gate te = strstr(s, SCF_FMRI_SERVICE_PREFIX); 49270Sstevel@tonic-gate if (te == NULL) 49280Sstevel@tonic-gate te = e; 49290Sstevel@tonic-gate 49300Sstevel@tonic-gate *te = 0; 49310Sstevel@tonic-gate my_scope = s; 49320Sstevel@tonic-gate 49330Sstevel@tonic-gate s = te; 49340Sstevel@tonic-gate if (s < e) 49350Sstevel@tonic-gate s += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1; 49360Sstevel@tonic-gate 49370Sstevel@tonic-gate /* If the scope ends with the suffix, remove it. */ 49380Sstevel@tonic-gate te = strstr(my_scope, SCF_FMRI_SCOPE_SUFFIX); 49390Sstevel@tonic-gate if (te != NULL && te[sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1] == 0) 49400Sstevel@tonic-gate *te = 0; 49410Sstevel@tonic-gate 49420Sstevel@tonic-gate /* Validate the scope. */ 49430Sstevel@tonic-gate if (my_scope[0] == '\0') 49440Sstevel@tonic-gate my_scope = SCF_FMRI_LOCAL_SCOPE; 49450Sstevel@tonic-gate else if (uu_check_name(my_scope, 0) == -1) { 49460Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 49470Sstevel@tonic-gate } 49480Sstevel@tonic-gate 49490Sstevel@tonic-gate if (scope != NULL) 49500Sstevel@tonic-gate *scope = my_scope; 49510Sstevel@tonic-gate } else { 49520Sstevel@tonic-gate if (scope != NULL) 49530Sstevel@tonic-gate *scope = SCF_FMRI_LOCAL_SCOPE; 49540Sstevel@tonic-gate } 49550Sstevel@tonic-gate 49560Sstevel@tonic-gate if (s[0] != 0) { 49570Sstevel@tonic-gate if (strncmp(s, SCF_FMRI_SERVICE_PREFIX, 49580Sstevel@tonic-gate sizeof (SCF_FMRI_SERVICE_PREFIX) - 1) == 0) 49590Sstevel@tonic-gate s += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1; 49600Sstevel@tonic-gate 49610Sstevel@tonic-gate /* 49620Sstevel@tonic-gate * Can't validate service here because it might not be null 49630Sstevel@tonic-gate * terminated. 49640Sstevel@tonic-gate */ 49650Sstevel@tonic-gate my_s = s; 49660Sstevel@tonic-gate } 49670Sstevel@tonic-gate 49680Sstevel@tonic-gate tpg = strstr(s, SCF_FMRI_PROPERTYGRP_PREFIX); 49690Sstevel@tonic-gate te = strstr(s, SCF_FMRI_INSTANCE_PREFIX); 49700Sstevel@tonic-gate if (te != NULL && (tpg == NULL || te < tpg)) { 49710Sstevel@tonic-gate *te = 0; 49720Sstevel@tonic-gate te += sizeof (SCF_FMRI_INSTANCE_PREFIX) - 1; 49730Sstevel@tonic-gate 49740Sstevel@tonic-gate /* Can't validate instance here either. */ 49750Sstevel@tonic-gate my_i = s = te; 49760Sstevel@tonic-gate 49770Sstevel@tonic-gate te = strstr(s, SCF_FMRI_PROPERTYGRP_PREFIX); 49780Sstevel@tonic-gate } else { 49790Sstevel@tonic-gate te = tpg; 49800Sstevel@tonic-gate } 49810Sstevel@tonic-gate 49820Sstevel@tonic-gate if (te != NULL) { 49830Sstevel@tonic-gate *te = 0; 49840Sstevel@tonic-gate te += sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1; 49850Sstevel@tonic-gate 49860Sstevel@tonic-gate my_pg = s = te; 49870Sstevel@tonic-gate te = strstr(s, SCF_FMRI_PROPERTY_PREFIX); 49880Sstevel@tonic-gate if (te != NULL) { 49890Sstevel@tonic-gate *te = 0; 49900Sstevel@tonic-gate te += sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1; 49910Sstevel@tonic-gate 49920Sstevel@tonic-gate my_p = te; 49930Sstevel@tonic-gate s = te; 49940Sstevel@tonic-gate } 49950Sstevel@tonic-gate } 49960Sstevel@tonic-gate 49970Sstevel@tonic-gate if (my_s != NULL) { 49980Sstevel@tonic-gate if (uu_check_name(my_s, UU_NAME_DOMAIN | UU_NAME_PATH) == -1) 49990Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 50000Sstevel@tonic-gate 50010Sstevel@tonic-gate if (service != NULL) 50020Sstevel@tonic-gate *service = my_s; 50030Sstevel@tonic-gate } 50040Sstevel@tonic-gate 50050Sstevel@tonic-gate if (my_i != NULL) { 50060Sstevel@tonic-gate if (uu_check_name(my_i, UU_NAME_DOMAIN) == -1) 50070Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 50080Sstevel@tonic-gate 50090Sstevel@tonic-gate if (instance != NULL) 50100Sstevel@tonic-gate *instance = my_i; 50110Sstevel@tonic-gate } 50120Sstevel@tonic-gate 50130Sstevel@tonic-gate if (my_pg != NULL) { 50140Sstevel@tonic-gate if (uu_check_name(my_pg, UU_NAME_DOMAIN) == -1) 50150Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 50160Sstevel@tonic-gate 50170Sstevel@tonic-gate if (propertygroup != NULL) 50180Sstevel@tonic-gate *propertygroup = my_pg; 50190Sstevel@tonic-gate } 50200Sstevel@tonic-gate 50210Sstevel@tonic-gate if (my_p != NULL) { 50220Sstevel@tonic-gate if (uu_check_name(my_p, UU_NAME_DOMAIN) == -1) 50230Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 50240Sstevel@tonic-gate 50250Sstevel@tonic-gate if (property != NULL) 50260Sstevel@tonic-gate *property = my_p; 50270Sstevel@tonic-gate } 50280Sstevel@tonic-gate 50290Sstevel@tonic-gate return (0); 50300Sstevel@tonic-gate } 50310Sstevel@tonic-gate 50320Sstevel@tonic-gate int 50330Sstevel@tonic-gate scf_parse_file_fmri(char *fmri, const char **scope, const char **path) 50340Sstevel@tonic-gate { 50350Sstevel@tonic-gate char *s, *e, *te; 50360Sstevel@tonic-gate 50370Sstevel@tonic-gate if (scope != NULL) 50380Sstevel@tonic-gate *scope = NULL; 50390Sstevel@tonic-gate 50400Sstevel@tonic-gate s = fmri; 50410Sstevel@tonic-gate e = strchr(s, '\0'); 50420Sstevel@tonic-gate 50430Sstevel@tonic-gate if (strncmp(s, SCF_FMRI_FILE_PREFIX, 50440Sstevel@tonic-gate sizeof (SCF_FMRI_FILE_PREFIX) - 1) == 0) 50450Sstevel@tonic-gate s += sizeof (SCF_FMRI_FILE_PREFIX) - 1; 50460Sstevel@tonic-gate 50470Sstevel@tonic-gate if (strncmp(s, SCF_FMRI_SCOPE_PREFIX, 50480Sstevel@tonic-gate sizeof (SCF_FMRI_SCOPE_PREFIX) - 1) == 0) { 50490Sstevel@tonic-gate char *my_scope; 50500Sstevel@tonic-gate 50510Sstevel@tonic-gate s += sizeof (SCF_FMRI_SCOPE_PREFIX) - 1; 50520Sstevel@tonic-gate te = strstr(s, SCF_FMRI_SERVICE_PREFIX); 50530Sstevel@tonic-gate if (te == NULL) 50540Sstevel@tonic-gate te = e; 50550Sstevel@tonic-gate 50560Sstevel@tonic-gate *te = 0; 50570Sstevel@tonic-gate my_scope = s; 50580Sstevel@tonic-gate 50590Sstevel@tonic-gate s = te; 50600Sstevel@tonic-gate 50610Sstevel@tonic-gate /* Validate the scope. */ 50620Sstevel@tonic-gate if (my_scope[0] != '\0' && 50630Sstevel@tonic-gate strcmp(my_scope, SCF_FMRI_LOCAL_SCOPE) != 0) { 50640Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 50650Sstevel@tonic-gate } 50660Sstevel@tonic-gate 50670Sstevel@tonic-gate if (scope != NULL) 50680Sstevel@tonic-gate *scope = my_scope; 50690Sstevel@tonic-gate } else { 50700Sstevel@tonic-gate /* 50710Sstevel@tonic-gate * FMRI paths must be absolute 50720Sstevel@tonic-gate */ 50730Sstevel@tonic-gate if (s[0] != '/') 50740Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 50750Sstevel@tonic-gate } 50760Sstevel@tonic-gate 50770Sstevel@tonic-gate s += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1; 50780Sstevel@tonic-gate 50790Sstevel@tonic-gate if (s >= e) 50800Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 50810Sstevel@tonic-gate 50820Sstevel@tonic-gate /* 50830Sstevel@tonic-gate * If the user requests it, return the full path of the file. 50840Sstevel@tonic-gate */ 50850Sstevel@tonic-gate if (path != NULL) { 50860Sstevel@tonic-gate assert(s > fmri); 50870Sstevel@tonic-gate s[-1] = '/'; 50880Sstevel@tonic-gate *path = s - 1; 50890Sstevel@tonic-gate } 50900Sstevel@tonic-gate 50910Sstevel@tonic-gate return (0); 50920Sstevel@tonic-gate } 50930Sstevel@tonic-gate 50940Sstevel@tonic-gate int 50950Sstevel@tonic-gate scf_parse_fmri(char *fmri, int *type, const char **scope, const char **service, 50960Sstevel@tonic-gate const char **instance, const char **propertygroup, const char **property) 50970Sstevel@tonic-gate { 50980Sstevel@tonic-gate if (strncmp(fmri, SCF_FMRI_SVC_PREFIX, 50990Sstevel@tonic-gate sizeof (SCF_FMRI_SVC_PREFIX) - 1) == 0) { 51000Sstevel@tonic-gate if (type) 51010Sstevel@tonic-gate *type = SCF_FMRI_TYPE_SVC; 51020Sstevel@tonic-gate return (scf_parse_svc_fmri(fmri, scope, service, instance, 51030Sstevel@tonic-gate propertygroup, property)); 51040Sstevel@tonic-gate } else if (strncmp(fmri, SCF_FMRI_FILE_PREFIX, 51050Sstevel@tonic-gate sizeof (SCF_FMRI_FILE_PREFIX) - 1) == 0) { 51060Sstevel@tonic-gate if (type) 51070Sstevel@tonic-gate *type = SCF_FMRI_TYPE_FILE; 51080Sstevel@tonic-gate return (scf_parse_file_fmri(fmri, scope, NULL)); 51090Sstevel@tonic-gate } else { 51100Sstevel@tonic-gate /* 51110Sstevel@tonic-gate * Parse as a svc if the fmri type is not explicitly 51120Sstevel@tonic-gate * specified. 51130Sstevel@tonic-gate */ 51140Sstevel@tonic-gate if (type) 51150Sstevel@tonic-gate *type = SCF_FMRI_TYPE_SVC; 51160Sstevel@tonic-gate return (scf_parse_svc_fmri(fmri, scope, service, instance, 51170Sstevel@tonic-gate propertygroup, property)); 51180Sstevel@tonic-gate } 51190Sstevel@tonic-gate } 51200Sstevel@tonic-gate 51210Sstevel@tonic-gate /* 51220Sstevel@tonic-gate * Fails with _INVALID_ARGUMENT. fmri and buf may be equal. 51230Sstevel@tonic-gate */ 51240Sstevel@tonic-gate ssize_t 51250Sstevel@tonic-gate scf_canonify_fmri(const char *fmri, char *buf, size_t bufsz) 51260Sstevel@tonic-gate { 51270Sstevel@tonic-gate const char *scope, *service, *instance, *pg, *property; 51280Sstevel@tonic-gate char local[6 * REP_PROTOCOL_NAME_LEN]; 51290Sstevel@tonic-gate int r; 51300Sstevel@tonic-gate size_t len; 51310Sstevel@tonic-gate 51320Sstevel@tonic-gate if (strlcpy(local, fmri, sizeof (local)) >= sizeof (local)) { 51330Sstevel@tonic-gate /* Should this be CONSTRAINT_VIOLATED? */ 51340Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 51350Sstevel@tonic-gate return (-1); 51360Sstevel@tonic-gate } 51370Sstevel@tonic-gate 51380Sstevel@tonic-gate 51390Sstevel@tonic-gate r = scf_parse_svc_fmri(local, &scope, &service, &instance, &pg, 51400Sstevel@tonic-gate &property); 51410Sstevel@tonic-gate if (r != 0) 51420Sstevel@tonic-gate return (-1); 51430Sstevel@tonic-gate 51440Sstevel@tonic-gate len = strlcpy(buf, "svc:/", bufsz); 51450Sstevel@tonic-gate 51460Sstevel@tonic-gate if (scope != NULL && strcmp(scope, SCF_SCOPE_LOCAL) != 0) { 51470Sstevel@tonic-gate len += strlcat(buf, "/", bufsz); 51480Sstevel@tonic-gate len += strlcat(buf, scope, bufsz); 51490Sstevel@tonic-gate } 51500Sstevel@tonic-gate 51510Sstevel@tonic-gate if (service) 51520Sstevel@tonic-gate len += strlcat(buf, service, bufsz); 51530Sstevel@tonic-gate 51540Sstevel@tonic-gate if (instance) { 51550Sstevel@tonic-gate len += strlcat(buf, ":", bufsz); 51560Sstevel@tonic-gate len += strlcat(buf, instance, bufsz); 51570Sstevel@tonic-gate } 51580Sstevel@tonic-gate 51590Sstevel@tonic-gate if (pg) { 51600Sstevel@tonic-gate len += strlcat(buf, "/:properties/", bufsz); 51610Sstevel@tonic-gate len += strlcat(buf, pg, bufsz); 51620Sstevel@tonic-gate } 51630Sstevel@tonic-gate 51640Sstevel@tonic-gate if (property) { 51650Sstevel@tonic-gate len += strlcat(buf, "/", bufsz); 51660Sstevel@tonic-gate len += strlcat(buf, property, bufsz); 51670Sstevel@tonic-gate } 51680Sstevel@tonic-gate 51690Sstevel@tonic-gate return (len); 51700Sstevel@tonic-gate } 51710Sstevel@tonic-gate 51720Sstevel@tonic-gate int 51730Sstevel@tonic-gate scf_handle_decode_fmri(scf_handle_t *h, const char *fmri, scf_scope_t *sc, 51740Sstevel@tonic-gate scf_service_t *svc, scf_instance_t *inst, scf_propertygroup_t *pg, 51750Sstevel@tonic-gate scf_property_t *prop, int flags) 51760Sstevel@tonic-gate { 51770Sstevel@tonic-gate const char *scope, *service, *instance, *propertygroup, *property; 51780Sstevel@tonic-gate int last; 51790Sstevel@tonic-gate char local[6 * REP_PROTOCOL_NAME_LEN]; 51800Sstevel@tonic-gate int ret; 51810Sstevel@tonic-gate const uint32_t holds = RH_HOLD_SCOPE | RH_HOLD_SERVICE | 51820Sstevel@tonic-gate RH_HOLD_INSTANCE | RH_HOLD_PG | RH_HOLD_PROPERTY; 51830Sstevel@tonic-gate 51840Sstevel@tonic-gate /* 51850Sstevel@tonic-gate * verify that all handles match 51860Sstevel@tonic-gate */ 51870Sstevel@tonic-gate if ((sc != NULL && h != sc->rd_d.rd_handle) || 51880Sstevel@tonic-gate (svc != NULL && h != svc->rd_d.rd_handle) || 51890Sstevel@tonic-gate (inst != NULL && h != inst->rd_d.rd_handle) || 51900Sstevel@tonic-gate (pg != NULL && h != pg->rd_d.rd_handle) || 51910Sstevel@tonic-gate (prop != NULL && h != prop->rd_d.rd_handle)) 51920Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 51930Sstevel@tonic-gate 51940Sstevel@tonic-gate if (strlcpy(local, fmri, sizeof (local)) >= sizeof (local)) { 51950Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_INVALID_ARGUMENT); 51960Sstevel@tonic-gate goto reset_args; 51970Sstevel@tonic-gate } 51980Sstevel@tonic-gate 51990Sstevel@tonic-gate /* 52000Sstevel@tonic-gate * We can simply return from an error in parsing, because 52010Sstevel@tonic-gate * scf_parse_fmri sets the error code correctly. 52020Sstevel@tonic-gate */ 52030Sstevel@tonic-gate if (scf_parse_svc_fmri(local, &scope, &service, &instance, 52040Sstevel@tonic-gate &propertygroup, &property) == -1) { 52050Sstevel@tonic-gate ret = -1; 52060Sstevel@tonic-gate goto reset_args; 52070Sstevel@tonic-gate } 52080Sstevel@tonic-gate 52090Sstevel@tonic-gate /* 52100Sstevel@tonic-gate * the FMRI looks valid at this point -- do constraint checks. 52110Sstevel@tonic-gate */ 52120Sstevel@tonic-gate 52130Sstevel@tonic-gate if (instance != NULL && (flags & SCF_DECODE_FMRI_REQUIRE_NO_INSTANCE)) { 52140Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED); 52150Sstevel@tonic-gate goto reset_args; 52160Sstevel@tonic-gate } 52170Sstevel@tonic-gate if (instance == NULL && (flags & SCF_DECODE_FMRI_REQUIRE_INSTANCE)) { 52180Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED); 52190Sstevel@tonic-gate goto reset_args; 52200Sstevel@tonic-gate } 52210Sstevel@tonic-gate 52220Sstevel@tonic-gate if (prop != NULL) 52230Sstevel@tonic-gate last = REP_PROTOCOL_ENTITY_PROPERTY; 52240Sstevel@tonic-gate else if (pg != NULL) 52250Sstevel@tonic-gate last = REP_PROTOCOL_ENTITY_PROPERTYGRP; 52260Sstevel@tonic-gate else if (inst != NULL) 52270Sstevel@tonic-gate last = REP_PROTOCOL_ENTITY_INSTANCE; 52280Sstevel@tonic-gate else if (svc != NULL) 52290Sstevel@tonic-gate last = REP_PROTOCOL_ENTITY_SERVICE; 52300Sstevel@tonic-gate else if (sc != NULL) 52310Sstevel@tonic-gate last = REP_PROTOCOL_ENTITY_SCOPE; 52320Sstevel@tonic-gate else 52330Sstevel@tonic-gate last = REP_PROTOCOL_ENTITY_NONE; 52340Sstevel@tonic-gate 52350Sstevel@tonic-gate if (flags & SCF_DECODE_FMRI_EXACT) { 52360Sstevel@tonic-gate int last_fmri; 52370Sstevel@tonic-gate 52380Sstevel@tonic-gate if (property != NULL) 52390Sstevel@tonic-gate last_fmri = REP_PROTOCOL_ENTITY_PROPERTY; 52400Sstevel@tonic-gate else if (propertygroup != NULL) 52410Sstevel@tonic-gate last_fmri = REP_PROTOCOL_ENTITY_PROPERTYGRP; 52420Sstevel@tonic-gate else if (instance != NULL) 52430Sstevel@tonic-gate last_fmri = REP_PROTOCOL_ENTITY_INSTANCE; 52440Sstevel@tonic-gate else if (service != NULL) 52450Sstevel@tonic-gate last_fmri = REP_PROTOCOL_ENTITY_SERVICE; 52460Sstevel@tonic-gate else if (scope != NULL) 52470Sstevel@tonic-gate last_fmri = REP_PROTOCOL_ENTITY_SCOPE; 52480Sstevel@tonic-gate else 52490Sstevel@tonic-gate last_fmri = REP_PROTOCOL_ENTITY_NONE; 52500Sstevel@tonic-gate 52510Sstevel@tonic-gate if (last != last_fmri) { 52520Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED); 52530Sstevel@tonic-gate goto reset_args; 52540Sstevel@tonic-gate } 52550Sstevel@tonic-gate } 52560Sstevel@tonic-gate 52570Sstevel@tonic-gate if ((flags & SCF_DECODE_FMRI_TRUNCATE) && 52580Sstevel@tonic-gate last == REP_PROTOCOL_ENTITY_NONE) { 52590Sstevel@tonic-gate ret = 0; /* nothing to do */ 52600Sstevel@tonic-gate goto reset_args; 52610Sstevel@tonic-gate } 52620Sstevel@tonic-gate 52630Sstevel@tonic-gate if (!(flags & SCF_DECODE_FMRI_TRUNCATE)) 52640Sstevel@tonic-gate last = REP_PROTOCOL_ENTITY_NONE; /* never stop */ 52650Sstevel@tonic-gate 52660Sstevel@tonic-gate /* 52670Sstevel@tonic-gate * passed the constraint checks -- try to grab the thing itself. 52680Sstevel@tonic-gate */ 52690Sstevel@tonic-gate 52700Sstevel@tonic-gate handle_hold_subhandles(h, holds); 52710Sstevel@tonic-gate if (sc == NULL) 52720Sstevel@tonic-gate sc = h->rh_scope; 52730Sstevel@tonic-gate else 52740Sstevel@tonic-gate datael_reset(&sc->rd_d); 52750Sstevel@tonic-gate 52760Sstevel@tonic-gate if (svc == NULL) 52770Sstevel@tonic-gate svc = h->rh_service; 52780Sstevel@tonic-gate else 52790Sstevel@tonic-gate datael_reset(&svc->rd_d); 52800Sstevel@tonic-gate 52810Sstevel@tonic-gate if (inst == NULL) 52820Sstevel@tonic-gate inst = h->rh_instance; 52830Sstevel@tonic-gate else 52840Sstevel@tonic-gate datael_reset(&inst->rd_d); 52850Sstevel@tonic-gate 52860Sstevel@tonic-gate if (pg == NULL) 52870Sstevel@tonic-gate pg = h->rh_pg; 52880Sstevel@tonic-gate else 52890Sstevel@tonic-gate datael_reset(&pg->rd_d); 52900Sstevel@tonic-gate 52910Sstevel@tonic-gate if (prop == NULL) 52920Sstevel@tonic-gate prop = h->rh_property; 52930Sstevel@tonic-gate else 52940Sstevel@tonic-gate datael_reset(&prop->rd_d); 52950Sstevel@tonic-gate 52960Sstevel@tonic-gate /* 52970Sstevel@tonic-gate * We only support local scopes, but we check *after* getting 52980Sstevel@tonic-gate * the local scope, so that any repository-related errors take 52990Sstevel@tonic-gate * precedence. 53000Sstevel@tonic-gate */ 53010Sstevel@tonic-gate if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) == -1) { 53020Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53030Sstevel@tonic-gate ret = -1; 53040Sstevel@tonic-gate goto reset_args; 53050Sstevel@tonic-gate } 53060Sstevel@tonic-gate 53070Sstevel@tonic-gate if (scope != NULL && strcmp(scope, SCF_FMRI_LOCAL_SCOPE) != 0) { 53080Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53090Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_NOT_FOUND); 53100Sstevel@tonic-gate goto reset_args; 53110Sstevel@tonic-gate } 53120Sstevel@tonic-gate 53130Sstevel@tonic-gate 53140Sstevel@tonic-gate if (service == NULL || last == REP_PROTOCOL_ENTITY_SCOPE) { 53150Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53160Sstevel@tonic-gate return (0); 53170Sstevel@tonic-gate } 53180Sstevel@tonic-gate 53190Sstevel@tonic-gate if (scf_scope_get_service(sc, service, svc) == -1) { 53200Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53210Sstevel@tonic-gate ret = -1; 53220Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_NOT_SET); 53230Sstevel@tonic-gate if (scf_error() == SCF_ERROR_DELETED) 53240Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_FOUND); 53250Sstevel@tonic-gate goto reset_args; 53260Sstevel@tonic-gate } 53270Sstevel@tonic-gate 53280Sstevel@tonic-gate if (last == REP_PROTOCOL_ENTITY_SERVICE) { 53290Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53300Sstevel@tonic-gate return (0); 53310Sstevel@tonic-gate } 53320Sstevel@tonic-gate 53330Sstevel@tonic-gate if (instance == NULL) { 53340Sstevel@tonic-gate if (propertygroup == NULL || 53350Sstevel@tonic-gate last == REP_PROTOCOL_ENTITY_INSTANCE) { 53360Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53370Sstevel@tonic-gate return (0); 53380Sstevel@tonic-gate } 53390Sstevel@tonic-gate 53400Sstevel@tonic-gate if (scf_service_get_pg(svc, propertygroup, pg) == -1) { 53410Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53420Sstevel@tonic-gate ret = -1; 53430Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_NOT_SET); 53440Sstevel@tonic-gate if (scf_error() == SCF_ERROR_DELETED) 53450Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_FOUND); 53460Sstevel@tonic-gate goto reset_args; 53470Sstevel@tonic-gate } 53480Sstevel@tonic-gate } else { 53490Sstevel@tonic-gate if (scf_service_get_instance(svc, instance, inst) == -1) { 53500Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53510Sstevel@tonic-gate ret = -1; 53520Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_NOT_SET); 53530Sstevel@tonic-gate if (scf_error() == SCF_ERROR_DELETED) 53540Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_FOUND); 53550Sstevel@tonic-gate goto reset_args; 53560Sstevel@tonic-gate } 53570Sstevel@tonic-gate 53580Sstevel@tonic-gate if (propertygroup == NULL || 53590Sstevel@tonic-gate last == REP_PROTOCOL_ENTITY_INSTANCE) { 53600Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53610Sstevel@tonic-gate return (0); 53620Sstevel@tonic-gate } 53630Sstevel@tonic-gate 53640Sstevel@tonic-gate if (scf_instance_get_pg(inst, propertygroup, pg) == -1) { 53650Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53660Sstevel@tonic-gate ret = -1; 53670Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_NOT_SET); 53680Sstevel@tonic-gate if (scf_error() == SCF_ERROR_DELETED) 53690Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_FOUND); 53700Sstevel@tonic-gate goto reset_args; 53710Sstevel@tonic-gate } 53720Sstevel@tonic-gate } 53730Sstevel@tonic-gate 53740Sstevel@tonic-gate if (property == NULL || last == REP_PROTOCOL_ENTITY_PROPERTYGRP) { 53750Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53760Sstevel@tonic-gate return (0); 53770Sstevel@tonic-gate } 53780Sstevel@tonic-gate 53790Sstevel@tonic-gate if (scf_pg_get_property(pg, property, prop) == -1) { 53800Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53810Sstevel@tonic-gate ret = -1; 53820Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_NOT_SET); 53830Sstevel@tonic-gate if (scf_error() == SCF_ERROR_DELETED) 53840Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_FOUND); 53850Sstevel@tonic-gate goto reset_args; 53860Sstevel@tonic-gate } 53870Sstevel@tonic-gate 53880Sstevel@tonic-gate handle_rele_subhandles(h, holds); 53890Sstevel@tonic-gate return (0); 53900Sstevel@tonic-gate 53910Sstevel@tonic-gate reset_args: 53920Sstevel@tonic-gate if (sc != NULL) 53930Sstevel@tonic-gate datael_reset(&sc->rd_d); 53940Sstevel@tonic-gate if (svc != NULL) 53950Sstevel@tonic-gate datael_reset(&svc->rd_d); 53960Sstevel@tonic-gate if (inst != NULL) 53970Sstevel@tonic-gate datael_reset(&inst->rd_d); 53980Sstevel@tonic-gate if (pg != NULL) 53990Sstevel@tonic-gate datael_reset(&pg->rd_d); 54000Sstevel@tonic-gate if (prop != NULL) 54010Sstevel@tonic-gate datael_reset(&prop->rd_d); 54020Sstevel@tonic-gate 54030Sstevel@tonic-gate return (ret); 54040Sstevel@tonic-gate } 54050Sstevel@tonic-gate 54060Sstevel@tonic-gate /* 54070Sstevel@tonic-gate * Fails with _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response too 54080Sstevel@tonic-gate * big, bad entity id, request not applicable to entity, name too long for 54090Sstevel@tonic-gate * buffer), _NOT_SET, or _DELETED. 54100Sstevel@tonic-gate */ 54110Sstevel@tonic-gate ssize_t 54120Sstevel@tonic-gate scf_scope_to_fmri(const scf_scope_t *scope, char *out, size_t sz) 54130Sstevel@tonic-gate { 54140Sstevel@tonic-gate ssize_t r, len; 54150Sstevel@tonic-gate 54160Sstevel@tonic-gate char tmp[REP_PROTOCOL_NAME_LEN]; 54170Sstevel@tonic-gate 54180Sstevel@tonic-gate r = scf_scope_get_name(scope, tmp, sizeof (tmp)); 54190Sstevel@tonic-gate 54200Sstevel@tonic-gate if (r <= 0) 54210Sstevel@tonic-gate return (r); 54220Sstevel@tonic-gate 54230Sstevel@tonic-gate len = strlcpy(out, SCF_FMRI_SVC_PREFIX, sz); 54240Sstevel@tonic-gate if (strcmp(tmp, SCF_FMRI_LOCAL_SCOPE) != 0) { 54250Sstevel@tonic-gate if (len >= sz) 54260Sstevel@tonic-gate return (len + r + sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1); 54270Sstevel@tonic-gate 54280Sstevel@tonic-gate len = strlcat(out, tmp, sz); 54290Sstevel@tonic-gate if (len >= sz) 54300Sstevel@tonic-gate return (len + sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1); 54310Sstevel@tonic-gate len = strlcat(out, 54320Sstevel@tonic-gate SCF_FMRI_SCOPE_SUFFIX SCF_FMRI_SERVICE_PREFIX, sz); 54330Sstevel@tonic-gate } 54340Sstevel@tonic-gate 54350Sstevel@tonic-gate return (len); 54360Sstevel@tonic-gate } 54370Sstevel@tonic-gate 54380Sstevel@tonic-gate /* 54390Sstevel@tonic-gate * Fails with _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response too 54400Sstevel@tonic-gate * big, bad element id, bad ids, bad types, scope has no parent, request not 54410Sstevel@tonic-gate * applicable to entity, name too long), _NOT_SET, _DELETED, 54420Sstevel@tonic-gate */ 54430Sstevel@tonic-gate ssize_t 54440Sstevel@tonic-gate scf_service_to_fmri(const scf_service_t *svc, char *out, size_t sz) 54450Sstevel@tonic-gate { 54460Sstevel@tonic-gate scf_handle_t *h = svc->rd_d.rd_handle; 54470Sstevel@tonic-gate scf_scope_t *scope = HANDLE_HOLD_SCOPE(h); 54480Sstevel@tonic-gate ssize_t r, len; 54490Sstevel@tonic-gate 54500Sstevel@tonic-gate char tmp[REP_PROTOCOL_NAME_LEN]; 54510Sstevel@tonic-gate 54520Sstevel@tonic-gate r = datael_get_parent(&svc->rd_d, &scope->rd_d); 54530Sstevel@tonic-gate if (r != SCF_SUCCESS) { 54540Sstevel@tonic-gate HANDLE_RELE_SCOPE(h); 54550Sstevel@tonic-gate 54560Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_HANDLE_MISMATCH); 54570Sstevel@tonic-gate return (-1); 54580Sstevel@tonic-gate } 54590Sstevel@tonic-gate if (out != NULL && sz > 0) 54600Sstevel@tonic-gate len = scf_scope_to_fmri(scope, out, sz); 54610Sstevel@tonic-gate else 54620Sstevel@tonic-gate len = scf_scope_to_fmri(scope, tmp, 2); 54630Sstevel@tonic-gate 54640Sstevel@tonic-gate HANDLE_RELE_SCOPE(h); 54650Sstevel@tonic-gate 54660Sstevel@tonic-gate if (len < 0) 54670Sstevel@tonic-gate return (-1); 54680Sstevel@tonic-gate 54690Sstevel@tonic-gate if (out == NULL || len >= sz) 54700Sstevel@tonic-gate len += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1; 54710Sstevel@tonic-gate else 54720Sstevel@tonic-gate len = strlcat(out, SCF_FMRI_SERVICE_PREFIX, sz); 54730Sstevel@tonic-gate 54740Sstevel@tonic-gate r = scf_service_get_name(svc, tmp, sizeof (tmp)); 54750Sstevel@tonic-gate if (r < 0) 54760Sstevel@tonic-gate return (r); 54770Sstevel@tonic-gate 54780Sstevel@tonic-gate if (out == NULL || len >= sz) 54790Sstevel@tonic-gate len += r; 54800Sstevel@tonic-gate else 54810Sstevel@tonic-gate len = strlcat(out, tmp, sz); 54820Sstevel@tonic-gate 54830Sstevel@tonic-gate return (len); 54840Sstevel@tonic-gate } 54850Sstevel@tonic-gate 54860Sstevel@tonic-gate ssize_t 54870Sstevel@tonic-gate scf_instance_to_fmri(const scf_instance_t *inst, char *out, size_t sz) 54880Sstevel@tonic-gate { 54890Sstevel@tonic-gate scf_handle_t *h = inst->rd_d.rd_handle; 54900Sstevel@tonic-gate scf_service_t *svc = HANDLE_HOLD_SERVICE(h); 54910Sstevel@tonic-gate ssize_t r, len; 54920Sstevel@tonic-gate 54930Sstevel@tonic-gate char tmp[REP_PROTOCOL_NAME_LEN]; 54940Sstevel@tonic-gate 54950Sstevel@tonic-gate r = datael_get_parent(&inst->rd_d, &svc->rd_d); 54960Sstevel@tonic-gate if (r != SCF_SUCCESS) { 54970Sstevel@tonic-gate HANDLE_RELE_SERVICE(h); 54980Sstevel@tonic-gate return (-1); 54990Sstevel@tonic-gate } 55000Sstevel@tonic-gate 55010Sstevel@tonic-gate len = scf_service_to_fmri(svc, out, sz); 55020Sstevel@tonic-gate 55030Sstevel@tonic-gate HANDLE_RELE_SERVICE(h); 55040Sstevel@tonic-gate 55050Sstevel@tonic-gate if (len < 0) 55060Sstevel@tonic-gate return (len); 55070Sstevel@tonic-gate 55080Sstevel@tonic-gate if (len >= sz) 55090Sstevel@tonic-gate len += sizeof (SCF_FMRI_INSTANCE_PREFIX) - 1; 55100Sstevel@tonic-gate else 55110Sstevel@tonic-gate len = strlcat(out, SCF_FMRI_INSTANCE_PREFIX, sz); 55120Sstevel@tonic-gate 55130Sstevel@tonic-gate r = scf_instance_get_name(inst, tmp, sizeof (tmp)); 55140Sstevel@tonic-gate if (r < 0) 55150Sstevel@tonic-gate return (r); 55160Sstevel@tonic-gate 55170Sstevel@tonic-gate if (len >= sz) 55180Sstevel@tonic-gate len += r; 55190Sstevel@tonic-gate else 55200Sstevel@tonic-gate len = strlcat(out, tmp, sz); 55210Sstevel@tonic-gate 55220Sstevel@tonic-gate return (len); 55230Sstevel@tonic-gate } 55240Sstevel@tonic-gate 55250Sstevel@tonic-gate ssize_t 55260Sstevel@tonic-gate scf_pg_to_fmri(const scf_propertygroup_t *pg, char *out, size_t sz) 55270Sstevel@tonic-gate { 55280Sstevel@tonic-gate scf_handle_t *h = pg->rd_d.rd_handle; 55290Sstevel@tonic-gate 55300Sstevel@tonic-gate struct rep_protocol_entity_parent_type request; 55310Sstevel@tonic-gate struct rep_protocol_integer_response response; 55320Sstevel@tonic-gate 55330Sstevel@tonic-gate char tmp[REP_PROTOCOL_NAME_LEN]; 55340Sstevel@tonic-gate ssize_t len, r; 55350Sstevel@tonic-gate 55360Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 55370Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_ENTITY_PARENT_TYPE; 55380Sstevel@tonic-gate request.rpr_entityid = pg->rd_d.rd_entity; 55390Sstevel@tonic-gate 55400Sstevel@tonic-gate datael_finish_reset(&pg->rd_d); 55410Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 55420Sstevel@tonic-gate &response, sizeof (response)); 55430Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 55440Sstevel@tonic-gate 55450Sstevel@tonic-gate if (r < 0) 55460Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 55470Sstevel@tonic-gate 55480Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS || 55490Sstevel@tonic-gate r < sizeof (response)) { 55500Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 55510Sstevel@tonic-gate } 55520Sstevel@tonic-gate 55530Sstevel@tonic-gate switch (response.rpr_value) { 55540Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SERVICE: { 55550Sstevel@tonic-gate scf_service_t *svc; 55560Sstevel@tonic-gate 55570Sstevel@tonic-gate svc = HANDLE_HOLD_SERVICE(h); 55580Sstevel@tonic-gate 55590Sstevel@tonic-gate r = datael_get_parent(&pg->rd_d, &svc->rd_d); 55600Sstevel@tonic-gate 55610Sstevel@tonic-gate if (r == SCF_SUCCESS) 55620Sstevel@tonic-gate len = scf_service_to_fmri(svc, out, sz); 55630Sstevel@tonic-gate 55640Sstevel@tonic-gate HANDLE_RELE_SERVICE(h); 55650Sstevel@tonic-gate break; 55660Sstevel@tonic-gate } 55670Sstevel@tonic-gate 55680Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_INSTANCE: { 55690Sstevel@tonic-gate scf_instance_t *inst; 55700Sstevel@tonic-gate 55710Sstevel@tonic-gate inst = HANDLE_HOLD_INSTANCE(h); 55720Sstevel@tonic-gate 55730Sstevel@tonic-gate r = datael_get_parent(&pg->rd_d, &inst->rd_d); 55740Sstevel@tonic-gate 55750Sstevel@tonic-gate if (r == SCF_SUCCESS) 55760Sstevel@tonic-gate len = scf_instance_to_fmri(inst, out, sz); 55770Sstevel@tonic-gate 55780Sstevel@tonic-gate HANDLE_RELE_INSTANCE(h); 55790Sstevel@tonic-gate break; 55800Sstevel@tonic-gate } 55810Sstevel@tonic-gate 55820Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SNAPLEVEL: { 55830Sstevel@tonic-gate scf_instance_t *inst = HANDLE_HOLD_INSTANCE(h); 55840Sstevel@tonic-gate scf_snapshot_t *snap = HANDLE_HOLD_SNAPSHOT(h); 55850Sstevel@tonic-gate scf_snaplevel_t *level = HANDLE_HOLD_SNAPLVL(h); 55860Sstevel@tonic-gate 55870Sstevel@tonic-gate r = datael_get_parent(&pg->rd_d, &level->rd_d); 55880Sstevel@tonic-gate 55890Sstevel@tonic-gate if (r == SCF_SUCCESS) 55900Sstevel@tonic-gate r = datael_get_parent(&level->rd_d, &snap->rd_d); 55910Sstevel@tonic-gate 55920Sstevel@tonic-gate if (r == SCF_SUCCESS) 55930Sstevel@tonic-gate r = datael_get_parent(&snap->rd_d, &inst->rd_d); 55940Sstevel@tonic-gate 55950Sstevel@tonic-gate if (r == SCF_SUCCESS) 55960Sstevel@tonic-gate len = scf_instance_to_fmri(inst, out, sz); 55970Sstevel@tonic-gate 55980Sstevel@tonic-gate HANDLE_RELE_INSTANCE(h); 55990Sstevel@tonic-gate HANDLE_RELE_SNAPSHOT(h); 56000Sstevel@tonic-gate HANDLE_RELE_SNAPLVL(h); 56010Sstevel@tonic-gate break; 56020Sstevel@tonic-gate } 56030Sstevel@tonic-gate 56040Sstevel@tonic-gate default: 56050Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INTERNAL)); 56060Sstevel@tonic-gate } 56070Sstevel@tonic-gate 56080Sstevel@tonic-gate if (r != SCF_SUCCESS) 56090Sstevel@tonic-gate return (r); 56100Sstevel@tonic-gate 56110Sstevel@tonic-gate if (len >= sz) 56120Sstevel@tonic-gate len += sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1; 56130Sstevel@tonic-gate else 56140Sstevel@tonic-gate len = strlcat(out, SCF_FMRI_PROPERTYGRP_PREFIX, sz); 56150Sstevel@tonic-gate 56160Sstevel@tonic-gate r = scf_pg_get_name(pg, tmp, sizeof (tmp)); 56170Sstevel@tonic-gate 56180Sstevel@tonic-gate if (r < 0) 56190Sstevel@tonic-gate return (r); 56200Sstevel@tonic-gate 56210Sstevel@tonic-gate if (len >= sz) 56220Sstevel@tonic-gate len += r; 56230Sstevel@tonic-gate else 56240Sstevel@tonic-gate len = strlcat(out, tmp, sz); 56250Sstevel@tonic-gate 56260Sstevel@tonic-gate return (len); 56270Sstevel@tonic-gate } 56280Sstevel@tonic-gate 56290Sstevel@tonic-gate ssize_t 56300Sstevel@tonic-gate scf_property_to_fmri(const scf_property_t *prop, char *out, size_t sz) 56310Sstevel@tonic-gate { 56320Sstevel@tonic-gate scf_handle_t *h = prop->rd_d.rd_handle; 56330Sstevel@tonic-gate scf_propertygroup_t *pg = HANDLE_HOLD_PG(h); 56340Sstevel@tonic-gate 56350Sstevel@tonic-gate char tmp[REP_PROTOCOL_NAME_LEN]; 56360Sstevel@tonic-gate ssize_t len; 56370Sstevel@tonic-gate int r; 56380Sstevel@tonic-gate 56390Sstevel@tonic-gate r = datael_get_parent(&prop->rd_d, &pg->rd_d); 56400Sstevel@tonic-gate if (r != SCF_SUCCESS) { 56410Sstevel@tonic-gate HANDLE_RELE_PG(h); 56420Sstevel@tonic-gate return (-1); 56430Sstevel@tonic-gate } 56440Sstevel@tonic-gate 56450Sstevel@tonic-gate len = scf_pg_to_fmri(pg, out, sz); 56460Sstevel@tonic-gate 56470Sstevel@tonic-gate HANDLE_RELE_PG(h); 56480Sstevel@tonic-gate 56490Sstevel@tonic-gate if (len >= sz) 56500Sstevel@tonic-gate len += sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1; 56510Sstevel@tonic-gate else 56520Sstevel@tonic-gate len = strlcat(out, SCF_FMRI_PROPERTY_PREFIX, sz); 56530Sstevel@tonic-gate 56540Sstevel@tonic-gate r = scf_property_get_name(prop, tmp, sizeof (tmp)); 56550Sstevel@tonic-gate 56560Sstevel@tonic-gate if (r < 0) 56570Sstevel@tonic-gate return (r); 56580Sstevel@tonic-gate 56590Sstevel@tonic-gate if (len >= sz) 56600Sstevel@tonic-gate len += r; 56610Sstevel@tonic-gate else 56620Sstevel@tonic-gate len = strlcat(out, tmp, sz); 56630Sstevel@tonic-gate 56640Sstevel@tonic-gate return (len); 56650Sstevel@tonic-gate } 56660Sstevel@tonic-gate 56670Sstevel@tonic-gate int 56680Sstevel@tonic-gate scf_pg_get_underlying_pg(const scf_propertygroup_t *pg, 56690Sstevel@tonic-gate scf_propertygroup_t *out) 56700Sstevel@tonic-gate { 56710Sstevel@tonic-gate scf_handle_t *h = pg->rd_d.rd_handle; 56720Sstevel@tonic-gate scf_service_t *svc; 56730Sstevel@tonic-gate scf_instance_t *inst; 56740Sstevel@tonic-gate 56750Sstevel@tonic-gate char me[REP_PROTOCOL_NAME_LEN]; 56760Sstevel@tonic-gate int r; 56770Sstevel@tonic-gate 56780Sstevel@tonic-gate if (h != out->rd_d.rd_handle) 56790Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH)); 56800Sstevel@tonic-gate 56810Sstevel@tonic-gate r = scf_pg_get_name(pg, me, sizeof (me)); 56820Sstevel@tonic-gate 56830Sstevel@tonic-gate if (r < 0) 56840Sstevel@tonic-gate return (r); 56850Sstevel@tonic-gate 56860Sstevel@tonic-gate svc = HANDLE_HOLD_SERVICE(h); 56870Sstevel@tonic-gate inst = HANDLE_HOLD_INSTANCE(h); 56880Sstevel@tonic-gate 56890Sstevel@tonic-gate r = datael_get_parent(&pg->rd_d, &inst->rd_d); 56900Sstevel@tonic-gate 56910Sstevel@tonic-gate if (r == SCF_SUCCESS) { 56920Sstevel@tonic-gate r = datael_get_parent(&inst->rd_d, &svc->rd_d); 56930Sstevel@tonic-gate if (r != SCF_SUCCESS) { 56940Sstevel@tonic-gate goto out; 56950Sstevel@tonic-gate } 56960Sstevel@tonic-gate r = scf_service_get_pg(svc, me, out); 56970Sstevel@tonic-gate } else { 56980Sstevel@tonic-gate r = scf_set_error(SCF_ERROR_NOT_FOUND); 56990Sstevel@tonic-gate } 57000Sstevel@tonic-gate 57010Sstevel@tonic-gate out: 57020Sstevel@tonic-gate HANDLE_RELE_SERVICE(h); 57030Sstevel@tonic-gate HANDLE_RELE_INSTANCE(h); 57040Sstevel@tonic-gate return (r); 57050Sstevel@tonic-gate } 57060Sstevel@tonic-gate 57070Sstevel@tonic-gate #define LEGACY_SCHEME "lrc:" 57080Sstevel@tonic-gate #define LEGACY_UNKNOWN "unknown" 57090Sstevel@tonic-gate 57100Sstevel@tonic-gate /* 57110Sstevel@tonic-gate * Implementation of scf_walk_fmri() 57120Sstevel@tonic-gate * 57130Sstevel@tonic-gate * This is a little tricky due to the many-to-many relationship between patterns 57140Sstevel@tonic-gate * and matches. We need to be able to satisfy the following requirements: 57150Sstevel@tonic-gate * 57160Sstevel@tonic-gate * 1) Detect patterns which match more than one FMRI, and be able to 57170Sstevel@tonic-gate * report which FMRIs have been matched. 57180Sstevel@tonic-gate * 2) Detect patterns which have not matched any FMRIs 57190Sstevel@tonic-gate * 3) Visit each matching FMRI exactly once across all patterns 57200Sstevel@tonic-gate * 4) Ignore FMRIs which have only been matched due to multiply-matching 57210Sstevel@tonic-gate * patterns. 57220Sstevel@tonic-gate * 57230Sstevel@tonic-gate * We maintain an array of scf_pattern_t structures, one for each argument, and 57240Sstevel@tonic-gate * maintain a linked list of scf_match_t structures for each one. We first 57250Sstevel@tonic-gate * qualify each pattern's type: 57260Sstevel@tonic-gate * 57270Sstevel@tonic-gate * PATTERN_INVALID The argument is invalid (too long). 57280Sstevel@tonic-gate * 57290Sstevel@tonic-gate * PATTERN_EXACT The pattern is a complete FMRI. The list of 57300Sstevel@tonic-gate * matches contains only a single entry. 57310Sstevel@tonic-gate * 57320Sstevel@tonic-gate * PATTERN_GLOB The pattern will be matched against all 57330Sstevel@tonic-gate * FMRIs via fnmatch() in the second phase. 57340Sstevel@tonic-gate * Matches will be added to the pattern's list 57350Sstevel@tonic-gate * as they are found. 57360Sstevel@tonic-gate * 57370Sstevel@tonic-gate * PATTERN_PARTIAL Everything else. We will assume that this is 57380Sstevel@tonic-gate * an abbreviated FMRI, and match according to 57390Sstevel@tonic-gate * our abbreviated FMRI rules. Matches will be 57400Sstevel@tonic-gate * added to the pattern's list as they are found. 57410Sstevel@tonic-gate * 57420Sstevel@tonic-gate * The first pass searches for arguments that are complete FMRIs. These are 57430Sstevel@tonic-gate * classified as EXACT patterns and do not necessitate searching the entire 57440Sstevel@tonic-gate * tree. 57450Sstevel@tonic-gate * 57460Sstevel@tonic-gate * Once this is done, if we have any GLOB or PARTIAL patterns (or if no 57470Sstevel@tonic-gate * arguments were given), we iterate over all services and instances in the 57480Sstevel@tonic-gate * repository, looking for matches. 57490Sstevel@tonic-gate * 57500Sstevel@tonic-gate * When a match is found, we add the match to the pattern's list. We also enter 57510Sstevel@tonic-gate * the match into a hash table, resulting in something like this: 57520Sstevel@tonic-gate * 57530Sstevel@tonic-gate * scf_pattern_t scf_match_t 57540Sstevel@tonic-gate * +---------------+ +-------+ +-------+ 57550Sstevel@tonic-gate * | pattern 'foo' |----->| match |---->| match | 57560Sstevel@tonic-gate * +---------------+ +-------+ +-------+ 57570Sstevel@tonic-gate * | | 57580Sstevel@tonic-gate * scf_match_key_t | | 57590Sstevel@tonic-gate * +--------------+ | | 57600Sstevel@tonic-gate * | FMRI bar/foo |<----+ | 57610Sstevel@tonic-gate * +--------------+ | 57620Sstevel@tonic-gate * | FMRI baz/foo |<------------------+ 57630Sstevel@tonic-gate * +--------------+ 57640Sstevel@tonic-gate * 57650Sstevel@tonic-gate * Once we have all of this set up, we do one pass to report patterns matching 57660Sstevel@tonic-gate * multiple FMRIs (if SCF_WALK_MULTIPLE is not set) and patterns for which no 57670Sstevel@tonic-gate * match was found. 57680Sstevel@tonic-gate * 57690Sstevel@tonic-gate * Finally, we walk through all valid patterns, and for each match, if we 57700Sstevel@tonic-gate * haven't already seen the match (as recorded in the hash table), then we 57710Sstevel@tonic-gate * execute the callback. 57720Sstevel@tonic-gate */ 57730Sstevel@tonic-gate 57740Sstevel@tonic-gate struct scf_matchkey; 57750Sstevel@tonic-gate struct scf_match; 57760Sstevel@tonic-gate 57770Sstevel@tonic-gate /* 57780Sstevel@tonic-gate * scf_matchkey_t 57790Sstevel@tonic-gate */ 57800Sstevel@tonic-gate typedef struct scf_matchkey { 57810Sstevel@tonic-gate char *sk_fmri; /* Matching FMRI */ 57820Sstevel@tonic-gate char *sk_legacy; /* Legacy name */ 57830Sstevel@tonic-gate int sk_seen; /* If we've been seen */ 57840Sstevel@tonic-gate struct scf_matchkey *sk_next; /* Next in hash chain */ 57850Sstevel@tonic-gate } scf_matchkey_t; 57860Sstevel@tonic-gate 57870Sstevel@tonic-gate /* 57880Sstevel@tonic-gate * scf_match_t 57890Sstevel@tonic-gate */ 57900Sstevel@tonic-gate typedef struct scf_match { 57910Sstevel@tonic-gate scf_matchkey_t *sm_key; 57920Sstevel@tonic-gate struct scf_match *sm_next; 57930Sstevel@tonic-gate } scf_match_t; 57940Sstevel@tonic-gate 57950Sstevel@tonic-gate #define WALK_HTABLE_SIZE 123 57960Sstevel@tonic-gate 57970Sstevel@tonic-gate /* 57980Sstevel@tonic-gate * scf_get_key() 57990Sstevel@tonic-gate * 58000Sstevel@tonic-gate * Given an FMRI and a hash table, returns the scf_matchkey_t corresponding to 58010Sstevel@tonic-gate * this FMRI. If the FMRI does not exist, it is added to the hash table. If a 58020Sstevel@tonic-gate * new entry cannot be allocated due to lack of memory, NULL is returned. 58030Sstevel@tonic-gate */ 58040Sstevel@tonic-gate static scf_matchkey_t * 58050Sstevel@tonic-gate scf_get_key(scf_matchkey_t **htable, const char *fmri, const char *legacy) 58060Sstevel@tonic-gate { 58070Sstevel@tonic-gate uint_t h = 0, g; 58080Sstevel@tonic-gate const char *p, *k; 58090Sstevel@tonic-gate scf_matchkey_t *key; 58100Sstevel@tonic-gate 58110Sstevel@tonic-gate k = strstr(fmri, ":/"); 58120Sstevel@tonic-gate assert(k != NULL); 58130Sstevel@tonic-gate k += 2; 58140Sstevel@tonic-gate 58150Sstevel@tonic-gate /* 58160Sstevel@tonic-gate * Generic hash function from uts/common/os/modhash.c. 58170Sstevel@tonic-gate */ 58180Sstevel@tonic-gate for (p = k; *p != '\0'; ++p) { 58190Sstevel@tonic-gate h = (h << 4) + *p; 58200Sstevel@tonic-gate if ((g = (h & 0xf0000000)) != 0) { 58210Sstevel@tonic-gate h ^= (g >> 24); 58220Sstevel@tonic-gate h ^= g; 58230Sstevel@tonic-gate } 58240Sstevel@tonic-gate } 58250Sstevel@tonic-gate 58260Sstevel@tonic-gate h %= WALK_HTABLE_SIZE; 58270Sstevel@tonic-gate 58280Sstevel@tonic-gate /* 58290Sstevel@tonic-gate * Search for an existing key 58300Sstevel@tonic-gate */ 58310Sstevel@tonic-gate for (key = htable[h]; key != NULL; key = key->sk_next) { 58320Sstevel@tonic-gate if (strcmp(key->sk_fmri, fmri) == 0) 58330Sstevel@tonic-gate return (key); 58340Sstevel@tonic-gate } 58350Sstevel@tonic-gate 58360Sstevel@tonic-gate if ((key = calloc(sizeof (scf_matchkey_t), 1)) == NULL) 58370Sstevel@tonic-gate return (NULL); 58380Sstevel@tonic-gate 58390Sstevel@tonic-gate /* 58400Sstevel@tonic-gate * Add new key to hash table. 58410Sstevel@tonic-gate */ 58420Sstevel@tonic-gate if ((key->sk_fmri = strdup(fmri)) == NULL) { 58430Sstevel@tonic-gate free(key); 58440Sstevel@tonic-gate return (NULL); 58450Sstevel@tonic-gate } 58460Sstevel@tonic-gate 58470Sstevel@tonic-gate if (legacy == NULL) { 58480Sstevel@tonic-gate key->sk_legacy = NULL; 58490Sstevel@tonic-gate } else if ((key->sk_legacy = strdup(legacy)) == NULL) { 58500Sstevel@tonic-gate free(key->sk_fmri); 58510Sstevel@tonic-gate free(key); 58520Sstevel@tonic-gate return (NULL); 58530Sstevel@tonic-gate } 58540Sstevel@tonic-gate 58550Sstevel@tonic-gate key->sk_next = htable[h]; 58560Sstevel@tonic-gate htable[h] = key; 58570Sstevel@tonic-gate 58580Sstevel@tonic-gate return (key); 58590Sstevel@tonic-gate } 58600Sstevel@tonic-gate 58610Sstevel@tonic-gate /* 58620Sstevel@tonic-gate * Given an FMRI, insert it into the pattern's list appropriately. 58630Sstevel@tonic-gate * svc_explicit indicates whether matching services should take 58640Sstevel@tonic-gate * precedence over matching instances. 58650Sstevel@tonic-gate */ 58660Sstevel@tonic-gate static scf_error_t 58670Sstevel@tonic-gate scf_add_match(scf_matchkey_t **htable, const char *fmri, const char *legacy, 58680Sstevel@tonic-gate scf_pattern_t *pattern, int svc_explicit) 58690Sstevel@tonic-gate { 58700Sstevel@tonic-gate scf_match_t *match; 58710Sstevel@tonic-gate 58720Sstevel@tonic-gate /* 58730Sstevel@tonic-gate * If svc_explicit is set, enforce the constaint that matching 58740Sstevel@tonic-gate * instances take precedence over matching services. Otherwise, 58750Sstevel@tonic-gate * matching services take precedence over matching instances. 58760Sstevel@tonic-gate */ 58770Sstevel@tonic-gate if (svc_explicit) { 58780Sstevel@tonic-gate scf_match_t *next, *prev; 58790Sstevel@tonic-gate /* 58800Sstevel@tonic-gate * If we match an instance, check to see if we must remove 58810Sstevel@tonic-gate * any matching services (for SCF_WALK_EXPLICIT). 58820Sstevel@tonic-gate */ 58830Sstevel@tonic-gate for (prev = match = pattern->sp_matches; match != NULL; 58840Sstevel@tonic-gate match = next) { 58850Sstevel@tonic-gate size_t len = strlen(match->sm_key->sk_fmri); 58860Sstevel@tonic-gate next = match->sm_next; 58870Sstevel@tonic-gate if (strncmp(match->sm_key->sk_fmri, fmri, len) == 0 && 58880Sstevel@tonic-gate fmri[len] == ':') { 58890Sstevel@tonic-gate if (prev == match) 58900Sstevel@tonic-gate pattern->sp_matches = match->sm_next; 58910Sstevel@tonic-gate else 58920Sstevel@tonic-gate prev->sm_next = match->sm_next; 58930Sstevel@tonic-gate pattern->sp_matchcount--; 58940Sstevel@tonic-gate free(match); 58950Sstevel@tonic-gate } else 58960Sstevel@tonic-gate prev = match; 58970Sstevel@tonic-gate } 58980Sstevel@tonic-gate } else { 58990Sstevel@tonic-gate /* 59000Sstevel@tonic-gate * If we've matched a service don't add any instances (for 59010Sstevel@tonic-gate * SCF_WALK_SERVICE). 59020Sstevel@tonic-gate */ 59030Sstevel@tonic-gate for (match = pattern->sp_matches; match != NULL; 59040Sstevel@tonic-gate match = match->sm_next) { 59050Sstevel@tonic-gate size_t len = strlen(match->sm_key->sk_fmri); 59060Sstevel@tonic-gate if (strncmp(match->sm_key->sk_fmri, fmri, len) == 0 && 59070Sstevel@tonic-gate fmri[len] == ':') 59080Sstevel@tonic-gate return (0); 59090Sstevel@tonic-gate } 59100Sstevel@tonic-gate } 59110Sstevel@tonic-gate 59120Sstevel@tonic-gate if ((match = malloc(sizeof (scf_match_t))) == NULL) 59130Sstevel@tonic-gate return (SCF_ERROR_NO_MEMORY); 59140Sstevel@tonic-gate 59150Sstevel@tonic-gate if ((match->sm_key = scf_get_key(htable, fmri, legacy)) == NULL) { 59160Sstevel@tonic-gate free(match); 59170Sstevel@tonic-gate return (SCF_ERROR_NO_MEMORY); 59180Sstevel@tonic-gate } 59190Sstevel@tonic-gate 59200Sstevel@tonic-gate match->sm_next = pattern->sp_matches; 59210Sstevel@tonic-gate pattern->sp_matches = match; 59220Sstevel@tonic-gate pattern->sp_matchcount++; 59230Sstevel@tonic-gate 59240Sstevel@tonic-gate return (0); 59250Sstevel@tonic-gate } 59260Sstevel@tonic-gate 59270Sstevel@tonic-gate /* 59280Sstevel@tonic-gate * Returns 1 if the fmri matches the given pattern, 0 otherwise. 59290Sstevel@tonic-gate */ 59301780Sgww int 59310Sstevel@tonic-gate scf_cmp_pattern(char *fmri, scf_pattern_t *pattern) 59320Sstevel@tonic-gate { 59330Sstevel@tonic-gate char *tmp; 59340Sstevel@tonic-gate 59350Sstevel@tonic-gate if (pattern->sp_type == PATTERN_GLOB) { 59360Sstevel@tonic-gate if (fnmatch(pattern->sp_arg, fmri, 0) == 0) 59370Sstevel@tonic-gate return (1); 59380Sstevel@tonic-gate } else if (pattern->sp_type == PATTERN_PARTIAL && 59390Sstevel@tonic-gate (tmp = strstr(fmri, pattern->sp_arg)) != NULL) { 59400Sstevel@tonic-gate /* 59410Sstevel@tonic-gate * We only allow partial matches anchored on the end of 59420Sstevel@tonic-gate * a service or instance, and beginning on an element 59430Sstevel@tonic-gate * boundary. 59440Sstevel@tonic-gate */ 59450Sstevel@tonic-gate if (tmp != fmri && tmp[-1] != '/' && tmp[-1] != ':' && 59460Sstevel@tonic-gate tmp[0] != ':') 59470Sstevel@tonic-gate return (0); 59480Sstevel@tonic-gate tmp += strlen(pattern->sp_arg); 59490Sstevel@tonic-gate if (tmp != fmri + strlen(fmri) && tmp[0] != ':' && 59500Sstevel@tonic-gate tmp[-1] != ':') 59510Sstevel@tonic-gate return (0); 59520Sstevel@tonic-gate 59530Sstevel@tonic-gate /* 59540Sstevel@tonic-gate * If the user has supplied a short pattern that matches 59550Sstevel@tonic-gate * 'svc:/' or 'lrc:/', ignore it. 59560Sstevel@tonic-gate */ 59570Sstevel@tonic-gate if (tmp <= fmri + 4) 59580Sstevel@tonic-gate return (0); 59590Sstevel@tonic-gate 59600Sstevel@tonic-gate return (1); 59610Sstevel@tonic-gate } 59620Sstevel@tonic-gate 59630Sstevel@tonic-gate return (0); 59640Sstevel@tonic-gate } 59650Sstevel@tonic-gate 59660Sstevel@tonic-gate /* 59670Sstevel@tonic-gate * Attempts to match the given FMRI against a set of patterns, keeping track of 59680Sstevel@tonic-gate * the results. 59690Sstevel@tonic-gate */ 59700Sstevel@tonic-gate static scf_error_t 59710Sstevel@tonic-gate scf_pattern_match(scf_matchkey_t **htable, char *fmri, const char *legacy, 59720Sstevel@tonic-gate int npattern, scf_pattern_t *pattern, int svc_explicit) 59730Sstevel@tonic-gate { 59740Sstevel@tonic-gate int i; 59750Sstevel@tonic-gate int ret = 0; 59760Sstevel@tonic-gate 59770Sstevel@tonic-gate for (i = 0; i < npattern; i++) { 59780Sstevel@tonic-gate if (scf_cmp_pattern(fmri, &pattern[i]) && 59790Sstevel@tonic-gate (ret = scf_add_match(htable, fmri, 59800Sstevel@tonic-gate legacy, &pattern[i], svc_explicit)) != 0) 59810Sstevel@tonic-gate return (ret); 59820Sstevel@tonic-gate } 59830Sstevel@tonic-gate 59840Sstevel@tonic-gate return (0); 59850Sstevel@tonic-gate } 59860Sstevel@tonic-gate 59870Sstevel@tonic-gate scf_error_t 59880Sstevel@tonic-gate scf_walk_fmri(scf_handle_t *h, int argc, char **argv, int flags, 59890Sstevel@tonic-gate scf_walk_callback callback, void *data, int *err, 59900Sstevel@tonic-gate void (*errfunc)(const char *, ...)) 59910Sstevel@tonic-gate { 59920Sstevel@tonic-gate scf_pattern_t *pattern = NULL; 59930Sstevel@tonic-gate int i; 59940Sstevel@tonic-gate char *fmri = NULL; 59950Sstevel@tonic-gate ssize_t max_fmri_length; 59960Sstevel@tonic-gate scf_service_t *svc = NULL; 59970Sstevel@tonic-gate scf_instance_t *inst = NULL; 59980Sstevel@tonic-gate scf_iter_t *iter = NULL, *sciter = NULL, *siter = NULL; 59990Sstevel@tonic-gate scf_scope_t *scope = NULL; 60000Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 60010Sstevel@tonic-gate scf_property_t *prop = NULL; 60020Sstevel@tonic-gate scf_value_t *value = NULL; 60030Sstevel@tonic-gate int ret = 0; 60040Sstevel@tonic-gate scf_matchkey_t **htable = NULL; 60050Sstevel@tonic-gate int pattern_search = 0; 60060Sstevel@tonic-gate ssize_t max_name_length; 60070Sstevel@tonic-gate char *pgname = NULL; 60080Sstevel@tonic-gate scf_walkinfo_t info; 60090Sstevel@tonic-gate 60100Sstevel@tonic-gate #ifndef NDEBUG 60110Sstevel@tonic-gate if (flags & SCF_WALK_EXPLICIT) 60120Sstevel@tonic-gate assert(flags & SCF_WALK_SERVICE); 60130Sstevel@tonic-gate if (flags & SCF_WALK_NOINSTANCE) 60140Sstevel@tonic-gate assert(flags & SCF_WALK_SERVICE); 60150Sstevel@tonic-gate if (flags & SCF_WALK_PROPERTY) 60160Sstevel@tonic-gate assert(!(flags & SCF_WALK_LEGACY)); 60170Sstevel@tonic-gate #endif 60180Sstevel@tonic-gate 60190Sstevel@tonic-gate /* 60200Sstevel@tonic-gate * Setup initial variables 60210Sstevel@tonic-gate */ 60220Sstevel@tonic-gate if ((max_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH)) == -1 || 60230Sstevel@tonic-gate (max_name_length = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) == -1) 60240Sstevel@tonic-gate return (SCF_ERROR_INTERNAL); 60250Sstevel@tonic-gate 60260Sstevel@tonic-gate if ((fmri = malloc(max_fmri_length + 1)) == NULL || 60270Sstevel@tonic-gate (pgname = malloc(max_name_length + 1)) == NULL) { 60280Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 60290Sstevel@tonic-gate goto error; 60300Sstevel@tonic-gate } 60310Sstevel@tonic-gate 60320Sstevel@tonic-gate if (argc == 0) { 60330Sstevel@tonic-gate pattern = NULL; 60340Sstevel@tonic-gate } else if ((pattern = calloc(argc, sizeof (scf_pattern_t))) 60350Sstevel@tonic-gate == NULL) { 60360Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 60370Sstevel@tonic-gate goto error; 60380Sstevel@tonic-gate } 60390Sstevel@tonic-gate 60400Sstevel@tonic-gate if ((htable = calloc(WALK_HTABLE_SIZE, sizeof (void *))) == NULL) { 60410Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 60420Sstevel@tonic-gate goto error; 60430Sstevel@tonic-gate } 60440Sstevel@tonic-gate 60450Sstevel@tonic-gate if ((inst = scf_instance_create(h)) == NULL || 60460Sstevel@tonic-gate (svc = scf_service_create(h)) == NULL || 60470Sstevel@tonic-gate (iter = scf_iter_create(h)) == NULL || 60480Sstevel@tonic-gate (sciter = scf_iter_create(h)) == NULL || 60490Sstevel@tonic-gate (siter = scf_iter_create(h)) == NULL || 60500Sstevel@tonic-gate (scope = scf_scope_create(h)) == NULL || 60510Sstevel@tonic-gate (pg = scf_pg_create(h)) == NULL || 60520Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL || 60530Sstevel@tonic-gate (value = scf_value_create(h)) == NULL) { 60540Sstevel@tonic-gate ret = scf_error(); 60550Sstevel@tonic-gate goto error; 60560Sstevel@tonic-gate } 60570Sstevel@tonic-gate 60580Sstevel@tonic-gate /* 60590Sstevel@tonic-gate * For each fmri given, we first check to see if it's a full service, 60600Sstevel@tonic-gate * instance, property group, or property FMRI. This avoids having to do 60610Sstevel@tonic-gate * the (rather expensive) walk of all instances. Any element which does 60620Sstevel@tonic-gate * not match a full fmri is identified as a globbed pattern or a partial 60630Sstevel@tonic-gate * fmri and stored in a private array when walking instances. 60640Sstevel@tonic-gate */ 60650Sstevel@tonic-gate for (i = 0; i < argc; i++) { 60660Sstevel@tonic-gate const char *scope_name, *svc_name, *inst_name, *pg_name; 60670Sstevel@tonic-gate const char *prop_name; 60680Sstevel@tonic-gate 60690Sstevel@tonic-gate if (strlen(argv[i]) > max_fmri_length) { 60700Sstevel@tonic-gate errfunc(scf_get_msg(SCF_MSG_ARGTOOLONG), argv[i]); 60710Sstevel@tonic-gate if (err != NULL) 60720Sstevel@tonic-gate *err = UU_EXIT_FATAL; 60730Sstevel@tonic-gate continue; 60740Sstevel@tonic-gate } 60750Sstevel@tonic-gate 60760Sstevel@tonic-gate (void) strcpy(fmri, argv[i]); 60770Sstevel@tonic-gate if (scf_parse_svc_fmri(fmri, &scope_name, &svc_name, &inst_name, 60780Sstevel@tonic-gate &pg_name, &prop_name) != SCF_SUCCESS) 60790Sstevel@tonic-gate goto badfmri; 60800Sstevel@tonic-gate 60810Sstevel@tonic-gate /* 60820Sstevel@tonic-gate * If the user has specified SCF_WALK_PROPERTY, allow property 60830Sstevel@tonic-gate * groups and properties. 60840Sstevel@tonic-gate */ 60850Sstevel@tonic-gate if (pg_name != NULL || prop_name != NULL) { 60860Sstevel@tonic-gate if (!(flags & SCF_WALK_PROPERTY)) 60870Sstevel@tonic-gate goto badfmri; 60880Sstevel@tonic-gate 60890Sstevel@tonic-gate if (scf_handle_decode_fmri(h, argv[i], NULL, NULL, 60900Sstevel@tonic-gate NULL, pg, prop, 0) != 0) 60910Sstevel@tonic-gate goto badfmri; 60920Sstevel@tonic-gate 60930Sstevel@tonic-gate if (scf_pg_get_name(pg, NULL, 0) < 0 && 60940Sstevel@tonic-gate scf_property_get_name(prop, NULL, 0) < 0) 60950Sstevel@tonic-gate goto badfmri; 60960Sstevel@tonic-gate 60970Sstevel@tonic-gate if (scf_canonify_fmri(argv[i], fmri, max_fmri_length) 60980Sstevel@tonic-gate <= 0) { 60990Sstevel@tonic-gate /* 61000Sstevel@tonic-gate * scf_parse_fmri() should have caught this. 61010Sstevel@tonic-gate */ 61020Sstevel@tonic-gate abort(); 61030Sstevel@tonic-gate } 61040Sstevel@tonic-gate 61050Sstevel@tonic-gate if ((ret = scf_add_match(htable, fmri, NULL, 61060Sstevel@tonic-gate &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0) 61070Sstevel@tonic-gate goto error; 61080Sstevel@tonic-gate 61090Sstevel@tonic-gate if ((pattern[i].sp_arg = strdup(argv[i])) == NULL) { 61100Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 61110Sstevel@tonic-gate goto error; 61120Sstevel@tonic-gate } 61130Sstevel@tonic-gate pattern[i].sp_type = PATTERN_EXACT; 61140Sstevel@tonic-gate } 61150Sstevel@tonic-gate 61160Sstevel@tonic-gate /* 61170Sstevel@tonic-gate * We need at least a service name 61180Sstevel@tonic-gate */ 61190Sstevel@tonic-gate if (scope_name == NULL || svc_name == NULL) 61200Sstevel@tonic-gate goto badfmri; 61210Sstevel@tonic-gate 61220Sstevel@tonic-gate /* 61230Sstevel@tonic-gate * If we have a fully qualified instance, add it to our list of 61240Sstevel@tonic-gate * fmris to watch. 61250Sstevel@tonic-gate */ 61260Sstevel@tonic-gate if (inst_name != NULL) { 61270Sstevel@tonic-gate if (flags & SCF_WALK_NOINSTANCE) 61280Sstevel@tonic-gate goto badfmri; 61290Sstevel@tonic-gate 61300Sstevel@tonic-gate if (scf_handle_decode_fmri(h, argv[i], NULL, NULL, 61310Sstevel@tonic-gate inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) 61320Sstevel@tonic-gate goto badfmri; 61330Sstevel@tonic-gate 61340Sstevel@tonic-gate if (scf_canonify_fmri(argv[i], fmri, max_fmri_length) 61350Sstevel@tonic-gate <= 0) 61360Sstevel@tonic-gate goto badfmri; 61370Sstevel@tonic-gate 61380Sstevel@tonic-gate if ((ret = scf_add_match(htable, fmri, NULL, 61390Sstevel@tonic-gate &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0) 61400Sstevel@tonic-gate goto error; 61410Sstevel@tonic-gate 61420Sstevel@tonic-gate if ((pattern[i].sp_arg = strdup(argv[i])) == NULL) { 61430Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 61440Sstevel@tonic-gate goto error; 61450Sstevel@tonic-gate } 61460Sstevel@tonic-gate pattern[i].sp_type = PATTERN_EXACT; 61470Sstevel@tonic-gate 61480Sstevel@tonic-gate continue; 61490Sstevel@tonic-gate } 61500Sstevel@tonic-gate 61510Sstevel@tonic-gate if (scf_handle_decode_fmri(h, argv[i], NULL, svc, 61520Sstevel@tonic-gate NULL, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 61530Sstevel@tonic-gate SCF_SUCCESS) 61540Sstevel@tonic-gate goto badfmri; 61550Sstevel@tonic-gate 61560Sstevel@tonic-gate /* 61570Sstevel@tonic-gate * If the user allows for bare services, then simply 61580Sstevel@tonic-gate * pass this service on. 61590Sstevel@tonic-gate */ 61600Sstevel@tonic-gate if (flags & SCF_WALK_SERVICE) { 61610Sstevel@tonic-gate if (scf_service_to_fmri(svc, fmri, 61620Sstevel@tonic-gate max_fmri_length + 1) <= 0) { 61630Sstevel@tonic-gate ret = scf_error(); 61640Sstevel@tonic-gate goto error; 61650Sstevel@tonic-gate } 61660Sstevel@tonic-gate 61670Sstevel@tonic-gate if ((ret = scf_add_match(htable, fmri, NULL, 61680Sstevel@tonic-gate &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0) 61690Sstevel@tonic-gate goto error; 61700Sstevel@tonic-gate 61710Sstevel@tonic-gate if ((pattern[i].sp_arg = strdup(argv[i])) 61720Sstevel@tonic-gate == NULL) { 61730Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 61740Sstevel@tonic-gate goto error; 61750Sstevel@tonic-gate } 61760Sstevel@tonic-gate pattern[i].sp_type = PATTERN_EXACT; 61770Sstevel@tonic-gate continue; 61780Sstevel@tonic-gate } 61790Sstevel@tonic-gate 61800Sstevel@tonic-gate if (flags & SCF_WALK_NOINSTANCE) 61810Sstevel@tonic-gate goto badfmri; 61820Sstevel@tonic-gate 61830Sstevel@tonic-gate /* 61840Sstevel@tonic-gate * Otherwise, iterate over all instances in the service. 61850Sstevel@tonic-gate */ 61860Sstevel@tonic-gate if (scf_iter_service_instances(iter, svc) != 61870Sstevel@tonic-gate SCF_SUCCESS) { 61880Sstevel@tonic-gate ret = scf_error(); 61890Sstevel@tonic-gate goto error; 61900Sstevel@tonic-gate } 61910Sstevel@tonic-gate 61920Sstevel@tonic-gate for (;;) { 61930Sstevel@tonic-gate ret = scf_iter_next_instance(iter, inst); 61940Sstevel@tonic-gate if (ret == 0) 61950Sstevel@tonic-gate break; 61960Sstevel@tonic-gate if (ret != 1) { 61970Sstevel@tonic-gate ret = scf_error(); 61980Sstevel@tonic-gate goto error; 61990Sstevel@tonic-gate } 62000Sstevel@tonic-gate 62010Sstevel@tonic-gate if (scf_instance_to_fmri(inst, fmri, 62020Sstevel@tonic-gate max_fmri_length + 1) == -1) 62030Sstevel@tonic-gate goto badfmri; 62040Sstevel@tonic-gate 62050Sstevel@tonic-gate if ((ret = scf_add_match(htable, fmri, NULL, 62060Sstevel@tonic-gate &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0) 62070Sstevel@tonic-gate goto error; 62080Sstevel@tonic-gate } 62090Sstevel@tonic-gate 62100Sstevel@tonic-gate if ((pattern[i].sp_arg = strdup(argv[i])) == NULL) { 62110Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 62120Sstevel@tonic-gate goto error; 62130Sstevel@tonic-gate } 62140Sstevel@tonic-gate pattern[i].sp_type = PATTERN_EXACT; 62150Sstevel@tonic-gate 62160Sstevel@tonic-gate continue; 62170Sstevel@tonic-gate 62180Sstevel@tonic-gate badfmri: 62190Sstevel@tonic-gate 62200Sstevel@tonic-gate /* 62210Sstevel@tonic-gate * If we got here because of a fatal error, bail out 62220Sstevel@tonic-gate * immediately. 62230Sstevel@tonic-gate */ 62240Sstevel@tonic-gate if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) { 62250Sstevel@tonic-gate ret = scf_error(); 62260Sstevel@tonic-gate goto error; 62270Sstevel@tonic-gate } 62280Sstevel@tonic-gate 62290Sstevel@tonic-gate /* 62300Sstevel@tonic-gate * At this point we failed to interpret the argument as a 62310Sstevel@tonic-gate * complete fmri, so mark it as a partial or globbed FMRI for 62320Sstevel@tonic-gate * later processing. 62330Sstevel@tonic-gate */ 62340Sstevel@tonic-gate if (strpbrk(argv[i], "*?[") != NULL) { 62350Sstevel@tonic-gate /* 62360Sstevel@tonic-gate * Prepend svc:/ to patterns which don't begin with * or 62370Sstevel@tonic-gate * svc: or lrc:. 62380Sstevel@tonic-gate */ 62390Sstevel@tonic-gate pattern[i].sp_type = PATTERN_GLOB; 62400Sstevel@tonic-gate if (argv[i][0] == '*' || 62410Sstevel@tonic-gate (strlen(argv[i]) >= 4 && argv[i][3] == ':')) 62420Sstevel@tonic-gate pattern[i].sp_arg = strdup(argv[i]); 62430Sstevel@tonic-gate else { 62440Sstevel@tonic-gate pattern[i].sp_arg = malloc(strlen(argv[i]) + 6); 62450Sstevel@tonic-gate if (pattern[i].sp_arg != NULL) 62460Sstevel@tonic-gate (void) snprintf(pattern[i].sp_arg, 62470Sstevel@tonic-gate strlen(argv[i]) + 6, "svc:/%s", 62480Sstevel@tonic-gate argv[i]); 62490Sstevel@tonic-gate } 62500Sstevel@tonic-gate } else { 62510Sstevel@tonic-gate pattern[i].sp_type = PATTERN_PARTIAL; 62520Sstevel@tonic-gate pattern[i].sp_arg = strdup(argv[i]); 62530Sstevel@tonic-gate } 62540Sstevel@tonic-gate pattern_search = 1; 62550Sstevel@tonic-gate if (pattern[i].sp_arg == NULL) { 62560Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 62570Sstevel@tonic-gate goto error; 62580Sstevel@tonic-gate } 62590Sstevel@tonic-gate } 62600Sstevel@tonic-gate 62610Sstevel@tonic-gate if (pattern_search || argc == 0) { 62620Sstevel@tonic-gate /* 62630Sstevel@tonic-gate * We have a set of patterns to search for. Iterate over all 62640Sstevel@tonic-gate * instances and legacy services searching for matches. 62650Sstevel@tonic-gate */ 62660Sstevel@tonic-gate if (scf_handle_get_local_scope(h, scope) != 0) { 62670Sstevel@tonic-gate ret = scf_error(); 62680Sstevel@tonic-gate goto error; 62690Sstevel@tonic-gate } 62700Sstevel@tonic-gate 62710Sstevel@tonic-gate if (scf_iter_scope_services(sciter, scope) != 0) { 62720Sstevel@tonic-gate ret = scf_error(); 62730Sstevel@tonic-gate goto error; 62740Sstevel@tonic-gate } 62750Sstevel@tonic-gate 62760Sstevel@tonic-gate for (;;) { 62770Sstevel@tonic-gate ret = scf_iter_next_service(sciter, svc); 62780Sstevel@tonic-gate if (ret == 0) 62790Sstevel@tonic-gate break; 62800Sstevel@tonic-gate if (ret != 1) { 62810Sstevel@tonic-gate ret = scf_error(); 62820Sstevel@tonic-gate goto error; 62830Sstevel@tonic-gate } 62840Sstevel@tonic-gate 62850Sstevel@tonic-gate if (flags & SCF_WALK_SERVICE) { 62860Sstevel@tonic-gate /* 62870Sstevel@tonic-gate * If the user is requesting bare services, try 62880Sstevel@tonic-gate * to match the service first. 62890Sstevel@tonic-gate */ 62900Sstevel@tonic-gate if (scf_service_to_fmri(svc, fmri, 62910Sstevel@tonic-gate max_fmri_length + 1) < 0) { 62920Sstevel@tonic-gate ret = scf_error(); 62930Sstevel@tonic-gate goto error; 62940Sstevel@tonic-gate } 62950Sstevel@tonic-gate 62960Sstevel@tonic-gate if (argc == 0) { 62970Sstevel@tonic-gate info.fmri = fmri; 62980Sstevel@tonic-gate info.scope = scope; 62990Sstevel@tonic-gate info.svc = svc; 63000Sstevel@tonic-gate info.inst = NULL; 63010Sstevel@tonic-gate info.pg = NULL; 63020Sstevel@tonic-gate info.prop = NULL; 63030Sstevel@tonic-gate if ((ret = callback(data, &info)) != 0) 63040Sstevel@tonic-gate goto error; 63050Sstevel@tonic-gate continue; 63060Sstevel@tonic-gate } else if ((ret = scf_pattern_match(htable, 63070Sstevel@tonic-gate fmri, NULL, argc, pattern, 63080Sstevel@tonic-gate flags & SCF_WALK_EXPLICIT)) != 0) { 63090Sstevel@tonic-gate goto error; 63100Sstevel@tonic-gate } 63110Sstevel@tonic-gate } 63120Sstevel@tonic-gate 63130Sstevel@tonic-gate if (flags & SCF_WALK_NOINSTANCE) 63140Sstevel@tonic-gate continue; 63150Sstevel@tonic-gate 63160Sstevel@tonic-gate /* 63170Sstevel@tonic-gate * Iterate over all instances in the service. 63180Sstevel@tonic-gate */ 63190Sstevel@tonic-gate if (scf_iter_service_instances(siter, svc) != 0) { 63200Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED) { 63210Sstevel@tonic-gate ret = scf_error(); 63220Sstevel@tonic-gate goto error; 63230Sstevel@tonic-gate } 63240Sstevel@tonic-gate continue; 63250Sstevel@tonic-gate } 63260Sstevel@tonic-gate 63270Sstevel@tonic-gate for (;;) { 63280Sstevel@tonic-gate ret = scf_iter_next_instance(siter, inst); 63290Sstevel@tonic-gate if (ret == 0) 63300Sstevel@tonic-gate break; 63310Sstevel@tonic-gate if (ret != 1) { 63320Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED) { 63330Sstevel@tonic-gate ret = scf_error(); 63340Sstevel@tonic-gate goto error; 63350Sstevel@tonic-gate } 63360Sstevel@tonic-gate break; 63370Sstevel@tonic-gate } 63380Sstevel@tonic-gate 63390Sstevel@tonic-gate if (scf_instance_to_fmri(inst, fmri, 63400Sstevel@tonic-gate max_fmri_length + 1) < 0) { 63410Sstevel@tonic-gate ret = scf_error(); 63420Sstevel@tonic-gate goto error; 63430Sstevel@tonic-gate } 63440Sstevel@tonic-gate 63450Sstevel@tonic-gate /* 63460Sstevel@tonic-gate * Without arguments, execute the callback 63470Sstevel@tonic-gate * immediately. 63480Sstevel@tonic-gate */ 63490Sstevel@tonic-gate if (argc == 0) { 63500Sstevel@tonic-gate info.fmri = fmri; 63510Sstevel@tonic-gate info.scope = scope; 63520Sstevel@tonic-gate info.svc = svc; 63530Sstevel@tonic-gate info.inst = inst; 63540Sstevel@tonic-gate info.pg = NULL; 63550Sstevel@tonic-gate info.prop = NULL; 63560Sstevel@tonic-gate if ((ret = callback(data, &info)) != 0) 63570Sstevel@tonic-gate goto error; 63580Sstevel@tonic-gate } else if ((ret = scf_pattern_match(htable, 63590Sstevel@tonic-gate fmri, NULL, argc, pattern, 63600Sstevel@tonic-gate flags & SCF_WALK_EXPLICIT)) != 0) { 63610Sstevel@tonic-gate goto error; 63620Sstevel@tonic-gate } 63630Sstevel@tonic-gate } 63640Sstevel@tonic-gate } 63650Sstevel@tonic-gate 63660Sstevel@tonic-gate /* 63670Sstevel@tonic-gate * Search legacy services 63680Sstevel@tonic-gate */ 63690Sstevel@tonic-gate if ((flags & SCF_WALK_LEGACY)) { 63700Sstevel@tonic-gate if (scf_scope_get_service(scope, SCF_LEGACY_SERVICE, 63710Sstevel@tonic-gate svc) != 0) { 63720Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) { 63730Sstevel@tonic-gate ret = scf_error(); 63740Sstevel@tonic-gate goto error; 63750Sstevel@tonic-gate } 63760Sstevel@tonic-gate 63770Sstevel@tonic-gate goto nolegacy; 63780Sstevel@tonic-gate } 63790Sstevel@tonic-gate 63800Sstevel@tonic-gate if (scf_iter_service_pgs_typed(iter, svc, 63810Sstevel@tonic-gate SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) { 63820Sstevel@tonic-gate ret = scf_error(); 63830Sstevel@tonic-gate goto error; 63840Sstevel@tonic-gate } 63850Sstevel@tonic-gate 63860Sstevel@tonic-gate (void) strcpy(fmri, LEGACY_SCHEME); 63870Sstevel@tonic-gate 63880Sstevel@tonic-gate for (;;) { 63890Sstevel@tonic-gate ret = scf_iter_next_pg(iter, pg); 63900Sstevel@tonic-gate if (ret == -1) { 63910Sstevel@tonic-gate ret = scf_error(); 63920Sstevel@tonic-gate goto error; 63930Sstevel@tonic-gate } 63940Sstevel@tonic-gate if (ret == 0) 63950Sstevel@tonic-gate break; 63960Sstevel@tonic-gate 63970Sstevel@tonic-gate if (scf_pg_get_property(pg, 63980Sstevel@tonic-gate SCF_LEGACY_PROPERTY_NAME, prop) == -1) { 63990Sstevel@tonic-gate ret = scf_error(); 64000Sstevel@tonic-gate if (ret == SCF_ERROR_DELETED || 64010Sstevel@tonic-gate ret == SCF_ERROR_NOT_FOUND) { 64020Sstevel@tonic-gate ret = 0; 64030Sstevel@tonic-gate continue; 64040Sstevel@tonic-gate } 64050Sstevel@tonic-gate goto error; 64060Sstevel@tonic-gate } 64070Sstevel@tonic-gate 64080Sstevel@tonic-gate if (scf_property_is_type(prop, SCF_TYPE_ASTRING) 64090Sstevel@tonic-gate != SCF_SUCCESS) { 64100Sstevel@tonic-gate if (scf_error() == SCF_ERROR_DELETED) 64110Sstevel@tonic-gate continue; 64120Sstevel@tonic-gate ret = scf_error(); 64130Sstevel@tonic-gate goto error; 64140Sstevel@tonic-gate } 64150Sstevel@tonic-gate 64160Sstevel@tonic-gate if (scf_property_get_value(prop, value) != 64170Sstevel@tonic-gate SCF_SUCCESS) 64180Sstevel@tonic-gate continue; 64190Sstevel@tonic-gate 64200Sstevel@tonic-gate if (scf_value_get_astring(value, 64210Sstevel@tonic-gate fmri + sizeof (LEGACY_SCHEME) - 1, 64220Sstevel@tonic-gate max_fmri_length + 2 - 64230Sstevel@tonic-gate sizeof (LEGACY_SCHEME)) <= 0) 64240Sstevel@tonic-gate continue; 64250Sstevel@tonic-gate 64260Sstevel@tonic-gate if (scf_pg_get_name(pg, pgname, 64270Sstevel@tonic-gate max_name_length + 1) <= 0) { 64280Sstevel@tonic-gate if (scf_error() == SCF_ERROR_DELETED) 64290Sstevel@tonic-gate continue; 64300Sstevel@tonic-gate ret = scf_error(); 64310Sstevel@tonic-gate goto error; 64320Sstevel@tonic-gate } 64330Sstevel@tonic-gate 64340Sstevel@tonic-gate if (argc == 0) { 64350Sstevel@tonic-gate info.fmri = fmri; 64360Sstevel@tonic-gate info.scope = scope; 64370Sstevel@tonic-gate info.svc = NULL; 64380Sstevel@tonic-gate info.inst = NULL; 64390Sstevel@tonic-gate info.pg = pg; 64400Sstevel@tonic-gate info.prop = NULL; 64410Sstevel@tonic-gate if ((ret = callback(data, &info)) != 0) 64420Sstevel@tonic-gate goto error; 64430Sstevel@tonic-gate } else if ((ret = scf_pattern_match(htable, 64440Sstevel@tonic-gate fmri, pgname, argc, pattern, 64450Sstevel@tonic-gate flags & SCF_WALK_EXPLICIT)) != 0) 64460Sstevel@tonic-gate goto error; 64470Sstevel@tonic-gate } 64480Sstevel@tonic-gate 64490Sstevel@tonic-gate } 64500Sstevel@tonic-gate } 64510Sstevel@tonic-gate nolegacy: 64520Sstevel@tonic-gate ret = 0; 64530Sstevel@tonic-gate 64540Sstevel@tonic-gate if (argc == 0) 64550Sstevel@tonic-gate goto error; 64560Sstevel@tonic-gate 64570Sstevel@tonic-gate /* 64580Sstevel@tonic-gate * Check all patterns, and see if we have that any that didn't match 64590Sstevel@tonic-gate * or any that matched multiple instances. For svcprop, add up the 64600Sstevel@tonic-gate * total number of matching keys. 64610Sstevel@tonic-gate */ 64620Sstevel@tonic-gate info.count = 0; 64630Sstevel@tonic-gate for (i = 0; i < argc; i++) { 64640Sstevel@tonic-gate scf_match_t *match; 64650Sstevel@tonic-gate 64660Sstevel@tonic-gate if (pattern[i].sp_type == PATTERN_INVALID) 64670Sstevel@tonic-gate continue; 64680Sstevel@tonic-gate if (pattern[i].sp_matchcount == 0) { 64690Sstevel@tonic-gate scf_msg_t msgid; 64700Sstevel@tonic-gate /* 64710Sstevel@tonic-gate * Provide a useful error message based on the argument 64720Sstevel@tonic-gate * and the type of entity requested. 64730Sstevel@tonic-gate */ 64740Sstevel@tonic-gate if (!(flags & SCF_WALK_LEGACY) && 64750Sstevel@tonic-gate strncmp(pattern[i].sp_arg, "lrc:/", 5) == 0) 64760Sstevel@tonic-gate msgid = SCF_MSG_PATTERN_LEGACY; 64770Sstevel@tonic-gate else if (flags & SCF_WALK_PROPERTY) 64780Sstevel@tonic-gate msgid = SCF_MSG_PATTERN_NOENTITY; 64790Sstevel@tonic-gate else if (flags & SCF_WALK_NOINSTANCE) 64800Sstevel@tonic-gate msgid = SCF_MSG_PATTERN_NOSERVICE; 64810Sstevel@tonic-gate else if (flags & SCF_WALK_SERVICE) 64820Sstevel@tonic-gate msgid = SCF_MSG_PATTERN_NOINSTSVC; 64830Sstevel@tonic-gate else 64840Sstevel@tonic-gate msgid = SCF_MSG_PATTERN_NOINSTANCE; 64850Sstevel@tonic-gate 64860Sstevel@tonic-gate errfunc(scf_get_msg(msgid), pattern[i].sp_arg); 64870Sstevel@tonic-gate if (err) 64880Sstevel@tonic-gate *err = UU_EXIT_FATAL; 64890Sstevel@tonic-gate } else if (!(flags & SCF_WALK_MULTIPLE) && 64900Sstevel@tonic-gate pattern[i].sp_matchcount > 1) { 64910Sstevel@tonic-gate size_t len, off; 64920Sstevel@tonic-gate char *msg; 64930Sstevel@tonic-gate 64940Sstevel@tonic-gate /* 64950Sstevel@tonic-gate * Construct a message with all possible FMRIs before 64960Sstevel@tonic-gate * passing off to error handling function. 64970Sstevel@tonic-gate * 64980Sstevel@tonic-gate * Note that strlen(scf_get_msg(...)) includes the 64990Sstevel@tonic-gate * length of '%s', which accounts for the terminating 65000Sstevel@tonic-gate * null byte. 65010Sstevel@tonic-gate */ 65020Sstevel@tonic-gate len = strlen(scf_get_msg(SCF_MSG_PATTERN_MULTIMATCH)) + 65030Sstevel@tonic-gate strlen(pattern[i].sp_arg); 65040Sstevel@tonic-gate for (match = pattern[i].sp_matches; match != NULL; 65050Sstevel@tonic-gate match = match->sm_next) { 65060Sstevel@tonic-gate len += strlen(match->sm_key->sk_fmri) + 2; 65070Sstevel@tonic-gate } 65080Sstevel@tonic-gate if ((msg = malloc(len)) == NULL) { 65090Sstevel@tonic-gate ret = SCF_ERROR_NO_MEMORY; 65100Sstevel@tonic-gate goto error; 65110Sstevel@tonic-gate } 65120Sstevel@tonic-gate 65130Sstevel@tonic-gate /* LINTED - format argument */ 65140Sstevel@tonic-gate (void) snprintf(msg, len, 65150Sstevel@tonic-gate scf_get_msg(SCF_MSG_PATTERN_MULTIMATCH), 65160Sstevel@tonic-gate pattern[i].sp_arg); 65170Sstevel@tonic-gate off = strlen(msg); 65180Sstevel@tonic-gate for (match = pattern[i].sp_matches; match != NULL; 65190Sstevel@tonic-gate match = match->sm_next) { 65200Sstevel@tonic-gate off += snprintf(msg + off, len - off, "\t%s\n", 65210Sstevel@tonic-gate match->sm_key->sk_fmri); 65220Sstevel@tonic-gate } 65230Sstevel@tonic-gate 65240Sstevel@tonic-gate errfunc(msg); 65250Sstevel@tonic-gate if (err != NULL) 65260Sstevel@tonic-gate *err = UU_EXIT_FATAL; 65270Sstevel@tonic-gate 65280Sstevel@tonic-gate free(msg); 65290Sstevel@tonic-gate } else { 65300Sstevel@tonic-gate for (match = pattern[i].sp_matches; match != NULL; 65310Sstevel@tonic-gate match = match->sm_next) { 65320Sstevel@tonic-gate if (!match->sm_key->sk_seen) 65330Sstevel@tonic-gate info.count++; 65340Sstevel@tonic-gate match->sm_key->sk_seen = 1; 65350Sstevel@tonic-gate } 65360Sstevel@tonic-gate } 65370Sstevel@tonic-gate } 65380Sstevel@tonic-gate 65390Sstevel@tonic-gate /* 65400Sstevel@tonic-gate * Clear 'sk_seen' for all keys. 65410Sstevel@tonic-gate */ 65420Sstevel@tonic-gate for (i = 0; i < WALK_HTABLE_SIZE; i++) { 65430Sstevel@tonic-gate scf_matchkey_t *key; 65440Sstevel@tonic-gate for (key = htable[i]; key != NULL; key = key->sk_next) 65450Sstevel@tonic-gate key->sk_seen = 0; 65460Sstevel@tonic-gate } 65470Sstevel@tonic-gate 65480Sstevel@tonic-gate /* 65490Sstevel@tonic-gate * Iterate over all the FMRIs in our hash table and execute the 65500Sstevel@tonic-gate * callback. 65510Sstevel@tonic-gate */ 65520Sstevel@tonic-gate for (i = 0; i < argc; i++) { 65530Sstevel@tonic-gate scf_match_t *match; 65540Sstevel@tonic-gate scf_matchkey_t *key; 65550Sstevel@tonic-gate 65560Sstevel@tonic-gate /* 65570Sstevel@tonic-gate * Ignore patterns which didn't match anything or matched too 65580Sstevel@tonic-gate * many FMRIs. 65590Sstevel@tonic-gate */ 65600Sstevel@tonic-gate if (pattern[i].sp_matchcount == 0 || 65610Sstevel@tonic-gate (!(flags & SCF_WALK_MULTIPLE) && 65620Sstevel@tonic-gate pattern[i].sp_matchcount > 1)) 65630Sstevel@tonic-gate continue; 65640Sstevel@tonic-gate 65650Sstevel@tonic-gate for (match = pattern[i].sp_matches; match != NULL; 65660Sstevel@tonic-gate match = match->sm_next) { 65670Sstevel@tonic-gate 65680Sstevel@tonic-gate key = match->sm_key; 65690Sstevel@tonic-gate if (key->sk_seen) 65700Sstevel@tonic-gate continue; 65710Sstevel@tonic-gate 65720Sstevel@tonic-gate key->sk_seen = 1; 65730Sstevel@tonic-gate 65740Sstevel@tonic-gate if (key->sk_legacy != NULL) { 65750Sstevel@tonic-gate if (scf_scope_get_service(scope, 65760Sstevel@tonic-gate "smf/legacy_run", svc) != 0) { 65770Sstevel@tonic-gate ret = scf_error(); 65780Sstevel@tonic-gate goto error; 65790Sstevel@tonic-gate } 65800Sstevel@tonic-gate 65810Sstevel@tonic-gate if (scf_service_get_pg(svc, key->sk_legacy, 65820Sstevel@tonic-gate pg) != 0) 65830Sstevel@tonic-gate continue; 65840Sstevel@tonic-gate 65850Sstevel@tonic-gate info.fmri = key->sk_fmri; 65860Sstevel@tonic-gate info.scope = scope; 65870Sstevel@tonic-gate info.svc = NULL; 65880Sstevel@tonic-gate info.inst = NULL; 65890Sstevel@tonic-gate info.pg = pg; 65900Sstevel@tonic-gate info.prop = NULL; 65910Sstevel@tonic-gate if ((ret = callback(data, &info)) != 0) 65920Sstevel@tonic-gate goto error; 65930Sstevel@tonic-gate } else { 65940Sstevel@tonic-gate if (scf_handle_decode_fmri(h, key->sk_fmri, 65950Sstevel@tonic-gate scope, svc, inst, pg, prop, 0) != 65960Sstevel@tonic-gate SCF_SUCCESS) 65970Sstevel@tonic-gate continue; 65980Sstevel@tonic-gate 65990Sstevel@tonic-gate info.fmri = key->sk_fmri; 66000Sstevel@tonic-gate info.scope = scope; 66010Sstevel@tonic-gate info.svc = svc; 66020Sstevel@tonic-gate if (scf_instance_get_name(inst, NULL, 0) < 0) { 66030Sstevel@tonic-gate if (scf_error() == 66040Sstevel@tonic-gate SCF_ERROR_CONNECTION_BROKEN) { 66050Sstevel@tonic-gate ret = scf_error(); 66060Sstevel@tonic-gate goto error; 66070Sstevel@tonic-gate } 66080Sstevel@tonic-gate info.inst = NULL; 66090Sstevel@tonic-gate } else { 66100Sstevel@tonic-gate info.inst = inst; 66110Sstevel@tonic-gate } 66120Sstevel@tonic-gate if (scf_pg_get_name(pg, NULL, 0) < 0) { 66130Sstevel@tonic-gate if (scf_error() == 66140Sstevel@tonic-gate SCF_ERROR_CONNECTION_BROKEN) { 66150Sstevel@tonic-gate ret = scf_error(); 66160Sstevel@tonic-gate goto error; 66170Sstevel@tonic-gate } 66180Sstevel@tonic-gate info.pg = NULL; 66190Sstevel@tonic-gate } else { 66200Sstevel@tonic-gate info.pg = pg; 66210Sstevel@tonic-gate } 66220Sstevel@tonic-gate if (scf_property_get_name(prop, NULL, 0) < 0) { 66230Sstevel@tonic-gate if (scf_error() == 66240Sstevel@tonic-gate SCF_ERROR_CONNECTION_BROKEN) { 66250Sstevel@tonic-gate ret = scf_error(); 66260Sstevel@tonic-gate goto error; 66270Sstevel@tonic-gate } 66280Sstevel@tonic-gate info.prop = NULL; 66290Sstevel@tonic-gate } else { 66300Sstevel@tonic-gate info.prop = prop; 66310Sstevel@tonic-gate } 66320Sstevel@tonic-gate 66330Sstevel@tonic-gate if ((ret = callback(data, &info)) != 0) 66340Sstevel@tonic-gate goto error; 66350Sstevel@tonic-gate } 66360Sstevel@tonic-gate } 66370Sstevel@tonic-gate } 66380Sstevel@tonic-gate 66390Sstevel@tonic-gate error: 66400Sstevel@tonic-gate if (htable) { 66410Sstevel@tonic-gate scf_matchkey_t *key, *next; 66420Sstevel@tonic-gate 66430Sstevel@tonic-gate for (i = 0; i < WALK_HTABLE_SIZE; i++) { 66440Sstevel@tonic-gate 66450Sstevel@tonic-gate for (key = htable[i]; key != NULL; 66460Sstevel@tonic-gate key = next) { 66470Sstevel@tonic-gate 66480Sstevel@tonic-gate next = key->sk_next; 66490Sstevel@tonic-gate 66500Sstevel@tonic-gate if (key->sk_fmri != NULL) 66510Sstevel@tonic-gate free(key->sk_fmri); 66520Sstevel@tonic-gate if (key->sk_legacy != NULL) 66530Sstevel@tonic-gate free(key->sk_legacy); 66540Sstevel@tonic-gate free(key); 66550Sstevel@tonic-gate } 66560Sstevel@tonic-gate } 66570Sstevel@tonic-gate free(htable); 66580Sstevel@tonic-gate } 66590Sstevel@tonic-gate if (pattern != NULL) { 66600Sstevel@tonic-gate for (i = 0; i < argc; i++) { 66610Sstevel@tonic-gate scf_match_t *match, *next; 66620Sstevel@tonic-gate 66630Sstevel@tonic-gate if (pattern[i].sp_arg != NULL) 66640Sstevel@tonic-gate free(pattern[i].sp_arg); 66650Sstevel@tonic-gate 66660Sstevel@tonic-gate for (match = pattern[i].sp_matches; match != NULL; 66670Sstevel@tonic-gate match = next) { 66680Sstevel@tonic-gate 66690Sstevel@tonic-gate next = match->sm_next; 66700Sstevel@tonic-gate 66710Sstevel@tonic-gate free(match); 66720Sstevel@tonic-gate } 66730Sstevel@tonic-gate } 66740Sstevel@tonic-gate free(pattern); 66750Sstevel@tonic-gate } 66760Sstevel@tonic-gate 66770Sstevel@tonic-gate free(fmri); 66780Sstevel@tonic-gate free(pgname); 66790Sstevel@tonic-gate 66800Sstevel@tonic-gate scf_value_destroy(value); 66810Sstevel@tonic-gate scf_property_destroy(prop); 66820Sstevel@tonic-gate scf_pg_destroy(pg); 66830Sstevel@tonic-gate scf_scope_destroy(scope); 66840Sstevel@tonic-gate scf_iter_destroy(siter); 66850Sstevel@tonic-gate scf_iter_destroy(sciter); 66860Sstevel@tonic-gate scf_iter_destroy(iter); 66870Sstevel@tonic-gate scf_instance_destroy(inst); 66880Sstevel@tonic-gate scf_service_destroy(svc); 66890Sstevel@tonic-gate 66900Sstevel@tonic-gate return (ret); 66910Sstevel@tonic-gate } 66920Sstevel@tonic-gate 66930Sstevel@tonic-gate /* 66940Sstevel@tonic-gate * _scf_request_backup: a simple wrapper routine 66950Sstevel@tonic-gate */ 66960Sstevel@tonic-gate int 66970Sstevel@tonic-gate _scf_request_backup(scf_handle_t *h, const char *name) 66980Sstevel@tonic-gate { 66990Sstevel@tonic-gate struct rep_protocol_backup_request request; 67000Sstevel@tonic-gate struct rep_protocol_response response; 67010Sstevel@tonic-gate 67020Sstevel@tonic-gate int r; 67030Sstevel@tonic-gate 67040Sstevel@tonic-gate if (strlcpy(request.rpr_name, name, sizeof (request.rpr_name)) >= 67050Sstevel@tonic-gate sizeof (request.rpr_name)) 67060Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT)); 67070Sstevel@tonic-gate 67080Sstevel@tonic-gate (void) pthread_mutex_lock(&h->rh_lock); 67090Sstevel@tonic-gate request.rpr_request = REP_PROTOCOL_BACKUP; 6710407Sjwadams request.rpr_changeid = handle_next_changeid(h); 67110Sstevel@tonic-gate 67120Sstevel@tonic-gate r = make_door_call(h, &request, sizeof (request), 67130Sstevel@tonic-gate &response, sizeof (response)); 67140Sstevel@tonic-gate (void) pthread_mutex_unlock(&h->rh_lock); 67150Sstevel@tonic-gate 67160Sstevel@tonic-gate if (r < 0) { 67170Sstevel@tonic-gate DOOR_ERRORS_BLOCK(r); 67180Sstevel@tonic-gate } 67190Sstevel@tonic-gate 67200Sstevel@tonic-gate if (response.rpr_response != REP_PROTOCOL_SUCCESS) 67210Sstevel@tonic-gate return (scf_set_error(proto_error(response.rpr_response))); 67220Sstevel@tonic-gate return (SCF_SUCCESS); 67230Sstevel@tonic-gate } 6724