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 5*2832Sjeanm * Common Development and Distribution License (the "License"). 6*2832Sjeanm * 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 /* 22*2832Sjeanm * 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 client layer for svc.configd. All direct protocol interactions 300Sstevel@tonic-gate * are handled here. 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * Essentially, the job of this layer is to turn the idempotent protocol 330Sstevel@tonic-gate * into a series of non-idempotent calls into the object layer, while 340Sstevel@tonic-gate * also handling the necessary locking. 350Sstevel@tonic-gate */ 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include <alloca.h> 380Sstevel@tonic-gate #include <assert.h> 390Sstevel@tonic-gate #include <door.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate #include <limits.h> 420Sstevel@tonic-gate #include <pthread.h> 430Sstevel@tonic-gate #include <stdio.h> 440Sstevel@tonic-gate #include <stdlib.h> 450Sstevel@tonic-gate #include <string.h> 460Sstevel@tonic-gate #include <unistd.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <libuutil.h> 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include "configd.h" 510Sstevel@tonic-gate #include "repcache_protocol.h" 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define INVALID_CHANGEID (0) 540Sstevel@tonic-gate #define INVALID_DOORID ((door_id_t)-1) 550Sstevel@tonic-gate #define INVALID_RESULT ((rep_protocol_responseid_t)INT_MIN) 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* 580Sstevel@tonic-gate * lint doesn't like constant assertions 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate #ifdef lint 610Sstevel@tonic-gate #define assert_nolint(x) (void)0 620Sstevel@tonic-gate #else 630Sstevel@tonic-gate #define assert_nolint(x) assert(x) 640Sstevel@tonic-gate #endif 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * Protects client linkage and the freelist 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate #define CLIENT_HASH_SIZE 64 700Sstevel@tonic-gate 710Sstevel@tonic-gate #pragma align 64(client_hash) 720Sstevel@tonic-gate static client_bucket_t client_hash[CLIENT_HASH_SIZE]; 730Sstevel@tonic-gate 74407Sjwadams static uu_avl_pool_t *entity_pool; 75407Sjwadams static uu_avl_pool_t *iter_pool; 760Sstevel@tonic-gate static uu_list_pool_t *client_pool; 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define CLIENT_HASH(id) (&client_hash[((id) & (CLIENT_HASH_SIZE - 1))]) 790Sstevel@tonic-gate 800Sstevel@tonic-gate uint_t request_log_size = 1024; /* tunable, before we start */ 810Sstevel@tonic-gate 820Sstevel@tonic-gate static pthread_mutex_t request_log_lock = PTHREAD_MUTEX_INITIALIZER; 830Sstevel@tonic-gate static uint_t request_log_cur; 840Sstevel@tonic-gate request_log_entry_t *request_log; 850Sstevel@tonic-gate 860Sstevel@tonic-gate static uint32_t client_maxid; 870Sstevel@tonic-gate static pthread_mutex_t client_lock; /* protects client_maxid */ 880Sstevel@tonic-gate 890Sstevel@tonic-gate static request_log_entry_t * 900Sstevel@tonic-gate get_log(void) 910Sstevel@tonic-gate { 920Sstevel@tonic-gate thread_info_t *ti = thread_self(); 930Sstevel@tonic-gate return (&ti->ti_log); 940Sstevel@tonic-gate } 950Sstevel@tonic-gate 960Sstevel@tonic-gate void 970Sstevel@tonic-gate log_enter(request_log_entry_t *rlp) 980Sstevel@tonic-gate { 990Sstevel@tonic-gate if (rlp->rl_start != 0 && request_log != NULL) { 1000Sstevel@tonic-gate request_log_entry_t *logrlp; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate (void) pthread_mutex_lock(&request_log_lock); 1030Sstevel@tonic-gate assert(request_log_cur < request_log_size); 1040Sstevel@tonic-gate logrlp = &request_log[request_log_cur++]; 1050Sstevel@tonic-gate if (request_log_cur == request_log_size) 1060Sstevel@tonic-gate request_log_cur = 0; 1070Sstevel@tonic-gate (void) memcpy(logrlp, rlp, sizeof (*rlp)); 1080Sstevel@tonic-gate (void) pthread_mutex_unlock(&request_log_lock); 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Note that the svc.configd dmod will join all of the per-thread log entries 1140Sstevel@tonic-gate * with the main log, so that even if the log is disabled, there is some 1150Sstevel@tonic-gate * information available. 1160Sstevel@tonic-gate */ 1170Sstevel@tonic-gate static request_log_entry_t * 1180Sstevel@tonic-gate start_log(uint32_t clientid) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate request_log_entry_t *rlp = get_log(); 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate log_enter(rlp); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate (void) memset(rlp, 0, sizeof (*rlp)); 1250Sstevel@tonic-gate rlp->rl_start = gethrtime(); 1260Sstevel@tonic-gate rlp->rl_tid = pthread_self(); 1270Sstevel@tonic-gate rlp->rl_clientid = clientid; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate return (rlp); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate void 1330Sstevel@tonic-gate end_log(void) 1340Sstevel@tonic-gate { 1350Sstevel@tonic-gate request_log_entry_t *rlp = get_log(); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate rlp->rl_end = gethrtime(); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate static void 1410Sstevel@tonic-gate add_log_ptr(request_log_entry_t *rlp, enum rc_ptr_type type, uint32_t id, 1420Sstevel@tonic-gate void *ptr) 1430Sstevel@tonic-gate { 1440Sstevel@tonic-gate request_log_ptr_t *rpp; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate if (rlp == NULL) 1470Sstevel@tonic-gate return; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate if (rlp->rl_num_ptrs >= MAX_PTRS) 1500Sstevel@tonic-gate return; 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate rpp = &rlp->rl_ptrs[rlp->rl_num_ptrs++]; 1530Sstevel@tonic-gate rpp->rlp_type = type; 1540Sstevel@tonic-gate rpp->rlp_id = id; 1550Sstevel@tonic-gate rpp->rlp_ptr = ptr; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1580Sstevel@tonic-gate * For entities, it's useful to have the node pointer at the start 1590Sstevel@tonic-gate * of the request. 1600Sstevel@tonic-gate */ 1610Sstevel@tonic-gate if (type == RC_PTR_TYPE_ENTITY && ptr != NULL) 1620Sstevel@tonic-gate rpp->rlp_data = ((repcache_entity_t *)ptr)->re_node.rnp_node; 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate int 1660Sstevel@tonic-gate client_is_privileged(void) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate thread_info_t *ti = thread_self(); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate ucred_t *uc; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if (ti->ti_active_client != NULL && 1730Sstevel@tonic-gate ti->ti_active_client->rc_all_auths) 1740Sstevel@tonic-gate return (1); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if ((uc = get_ucred()) == NULL) 1770Sstevel@tonic-gate return (0); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate return (ucred_is_privileged(uc)); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /*ARGSUSED*/ 1830Sstevel@tonic-gate static int 1840Sstevel@tonic-gate client_compare(const void *lc_arg, const void *rc_arg, void *private) 1850Sstevel@tonic-gate { 1860Sstevel@tonic-gate uint32_t l_id = ((const repcache_client_t *)lc_arg)->rc_id; 1870Sstevel@tonic-gate uint32_t r_id = ((const repcache_client_t *)rc_arg)->rc_id; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate if (l_id > r_id) 1900Sstevel@tonic-gate return (1); 1910Sstevel@tonic-gate if (l_id < r_id) 1920Sstevel@tonic-gate return (-1); 1930Sstevel@tonic-gate return (0); 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /*ARGSUSED*/ 1970Sstevel@tonic-gate static int 1980Sstevel@tonic-gate entity_compare(const void *lc_arg, const void *rc_arg, void *private) 1990Sstevel@tonic-gate { 2000Sstevel@tonic-gate uint32_t l_id = ((const repcache_entity_t *)lc_arg)->re_id; 2010Sstevel@tonic-gate uint32_t r_id = ((const repcache_entity_t *)rc_arg)->re_id; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if (l_id > r_id) 2040Sstevel@tonic-gate return (1); 2050Sstevel@tonic-gate if (l_id < r_id) 2060Sstevel@tonic-gate return (-1); 2070Sstevel@tonic-gate return (0); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /*ARGSUSED*/ 2110Sstevel@tonic-gate static int 2120Sstevel@tonic-gate iter_compare(const void *lc_arg, const void *rc_arg, void *private) 2130Sstevel@tonic-gate { 2140Sstevel@tonic-gate uint32_t l_id = ((const repcache_iter_t *)lc_arg)->ri_id; 2150Sstevel@tonic-gate uint32_t r_id = ((const repcache_iter_t *)rc_arg)->ri_id; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if (l_id > r_id) 2180Sstevel@tonic-gate return (1); 2190Sstevel@tonic-gate if (l_id < r_id) 2200Sstevel@tonic-gate return (-1); 2210Sstevel@tonic-gate return (0); 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate static int 2250Sstevel@tonic-gate client_hash_init(void) 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate int x; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate assert_nolint(offsetof(repcache_entity_t, re_id) == 0); 230407Sjwadams entity_pool = uu_avl_pool_create("repcache_entitys", 2310Sstevel@tonic-gate sizeof (repcache_entity_t), offsetof(repcache_entity_t, re_link), 232407Sjwadams entity_compare, UU_AVL_POOL_DEBUG); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate assert_nolint(offsetof(repcache_iter_t, ri_id) == 0); 235407Sjwadams iter_pool = uu_avl_pool_create("repcache_iters", 2360Sstevel@tonic-gate sizeof (repcache_iter_t), offsetof(repcache_iter_t, ri_link), 237407Sjwadams iter_compare, UU_AVL_POOL_DEBUG); 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate assert_nolint(offsetof(repcache_client_t, rc_id) == 0); 2400Sstevel@tonic-gate client_pool = uu_list_pool_create("repcache_clients", 2410Sstevel@tonic-gate sizeof (repcache_client_t), offsetof(repcache_client_t, rc_link), 2420Sstevel@tonic-gate client_compare, UU_LIST_POOL_DEBUG); 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate if (entity_pool == NULL || iter_pool == NULL || client_pool == NULL) 2450Sstevel@tonic-gate return (0); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate for (x = 0; x < CLIENT_HASH_SIZE; x++) { 2480Sstevel@tonic-gate uu_list_t *lp = uu_list_create(client_pool, &client_hash[x], 2490Sstevel@tonic-gate UU_LIST_SORTED); 2500Sstevel@tonic-gate if (lp == NULL) 2510Sstevel@tonic-gate return (0); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate (void) pthread_mutex_init(&client_hash[x].cb_lock, NULL); 2540Sstevel@tonic-gate client_hash[x].cb_list = lp; 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate return (1); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate static repcache_client_t * 2610Sstevel@tonic-gate client_alloc(void) 2620Sstevel@tonic-gate { 2630Sstevel@tonic-gate repcache_client_t *cp; 2640Sstevel@tonic-gate cp = uu_zalloc(sizeof (*cp)); 2650Sstevel@tonic-gate if (cp == NULL) 2660Sstevel@tonic-gate return (NULL); 2670Sstevel@tonic-gate 268407Sjwadams cp->rc_entities = uu_avl_create(entity_pool, cp, 0); 269407Sjwadams if (cp->rc_entities == NULL) 2700Sstevel@tonic-gate goto fail; 2710Sstevel@tonic-gate 272407Sjwadams cp->rc_iters = uu_avl_create(iter_pool, cp, 0); 273407Sjwadams if (cp->rc_iters == NULL) 2740Sstevel@tonic-gate goto fail; 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate uu_list_node_init(cp, &cp->rc_link, client_pool); 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate cp->rc_doorfd = -1; 2790Sstevel@tonic-gate cp->rc_doorid = INVALID_DOORID; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate (void) pthread_mutex_init(&cp->rc_lock, NULL); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate rc_node_ptr_init(&cp->rc_notify_ptr); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate return (cp); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate fail: 288407Sjwadams if (cp->rc_iters != NULL) 289407Sjwadams uu_avl_destroy(cp->rc_iters); 290407Sjwadams if (cp->rc_entities != NULL) 291407Sjwadams uu_avl_destroy(cp->rc_entities); 2920Sstevel@tonic-gate uu_free(cp); 2930Sstevel@tonic-gate return (NULL); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate static void 2970Sstevel@tonic-gate client_free(repcache_client_t *cp) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate assert(cp->rc_insert_thr == 0); 3000Sstevel@tonic-gate assert(cp->rc_refcnt == 0); 3010Sstevel@tonic-gate assert(cp->rc_doorfd == -1); 3020Sstevel@tonic-gate assert(cp->rc_doorid == INVALID_DOORID); 303407Sjwadams assert(uu_avl_first(cp->rc_entities) == NULL); 304407Sjwadams assert(uu_avl_first(cp->rc_iters) == NULL); 305407Sjwadams uu_avl_destroy(cp->rc_entities); 306407Sjwadams uu_avl_destroy(cp->rc_iters); 3070Sstevel@tonic-gate uu_list_node_fini(cp, &cp->rc_link, client_pool); 3080Sstevel@tonic-gate (void) pthread_mutex_destroy(&cp->rc_lock); 3090Sstevel@tonic-gate uu_free(cp); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate static void 3130Sstevel@tonic-gate client_insert(repcache_client_t *cp) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate client_bucket_t *bp = CLIENT_HASH(cp->rc_id); 3160Sstevel@tonic-gate uu_list_index_t idx; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate assert(cp->rc_id > 0); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate (void) pthread_mutex_lock(&bp->cb_lock); 3210Sstevel@tonic-gate /* 3220Sstevel@tonic-gate * We assume it does not already exist 3230Sstevel@tonic-gate */ 3240Sstevel@tonic-gate (void) uu_list_find(bp->cb_list, cp, NULL, &idx); 3250Sstevel@tonic-gate uu_list_insert(bp->cb_list, cp, idx); 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate (void) pthread_mutex_unlock(&bp->cb_lock); 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate static repcache_client_t * 3310Sstevel@tonic-gate client_lookup(uint32_t id) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate client_bucket_t *bp = CLIENT_HASH(id); 3340Sstevel@tonic-gate repcache_client_t *cp; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate (void) pthread_mutex_lock(&bp->cb_lock); 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate cp = uu_list_find(bp->cb_list, &id, NULL, NULL); 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate /* 3410Sstevel@tonic-gate * Bump the reference count 3420Sstevel@tonic-gate */ 3430Sstevel@tonic-gate if (cp != NULL) { 3440Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 3450Sstevel@tonic-gate assert(!(cp->rc_flags & RC_CLIENT_DEAD)); 3460Sstevel@tonic-gate cp->rc_refcnt++; 3470Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate (void) pthread_mutex_unlock(&bp->cb_lock); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate return (cp); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate static void 3550Sstevel@tonic-gate client_release(repcache_client_t *cp) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 3580Sstevel@tonic-gate assert(cp->rc_refcnt > 0); 3590Sstevel@tonic-gate assert(cp->rc_insert_thr != pthread_self()); 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate --cp->rc_refcnt; 3620Sstevel@tonic-gate (void) pthread_cond_broadcast(&cp->rc_cv); 3630Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate /* 3670Sstevel@tonic-gate * We only allow one thread to be inserting at a time, to prevent 3680Sstevel@tonic-gate * insert/insert races. 3690Sstevel@tonic-gate */ 3700Sstevel@tonic-gate static void 3710Sstevel@tonic-gate client_start_insert(repcache_client_t *cp) 3720Sstevel@tonic-gate { 3730Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 3740Sstevel@tonic-gate assert(cp->rc_refcnt > 0); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate while (cp->rc_insert_thr != 0) { 3770Sstevel@tonic-gate assert(cp->rc_insert_thr != pthread_self()); 3780Sstevel@tonic-gate (void) pthread_cond_wait(&cp->rc_cv, &cp->rc_lock); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate cp->rc_insert_thr = pthread_self(); 3810Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate static void 3850Sstevel@tonic-gate client_end_insert(repcache_client_t *cp) 3860Sstevel@tonic-gate { 3870Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 3880Sstevel@tonic-gate assert(cp->rc_insert_thr == pthread_self()); 3890Sstevel@tonic-gate cp->rc_insert_thr = 0; 3900Sstevel@tonic-gate (void) pthread_cond_broadcast(&cp->rc_cv); 3910Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /*ARGSUSED*/ 3950Sstevel@tonic-gate static repcache_entity_t * 3960Sstevel@tonic-gate entity_alloc(repcache_client_t *cp) 3970Sstevel@tonic-gate { 3980Sstevel@tonic-gate repcache_entity_t *ep = uu_zalloc(sizeof (repcache_entity_t)); 3990Sstevel@tonic-gate if (ep != NULL) { 400407Sjwadams uu_avl_node_init(ep, &ep->re_link, entity_pool); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate return (ep); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate static void 4060Sstevel@tonic-gate entity_add(repcache_client_t *cp, repcache_entity_t *ep) 4070Sstevel@tonic-gate { 408407Sjwadams uu_avl_index_t idx; 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 4110Sstevel@tonic-gate assert(cp->rc_insert_thr == pthread_self()); 4120Sstevel@tonic-gate 413407Sjwadams (void) uu_avl_find(cp->rc_entities, ep, NULL, &idx); 414407Sjwadams uu_avl_insert(cp->rc_entities, ep, idx); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate static repcache_entity_t * 4200Sstevel@tonic-gate entity_find(repcache_client_t *cp, uint32_t id) 4210Sstevel@tonic-gate { 4220Sstevel@tonic-gate repcache_entity_t *ep; 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 425407Sjwadams ep = uu_avl_find(cp->rc_entities, &id, NULL, NULL); 4260Sstevel@tonic-gate if (ep != NULL) { 4270Sstevel@tonic-gate add_log_ptr(get_log(), RC_PTR_TYPE_ENTITY, id, ep); 4280Sstevel@tonic-gate (void) pthread_mutex_lock(&ep->re_lock); 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate return (ep); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* 4360Sstevel@tonic-gate * Fails with 4370Sstevel@tonic-gate * _DUPLICATE_ID - the ids are equal 4380Sstevel@tonic-gate * _UNKNOWN_ID - an id does not designate an active register 4390Sstevel@tonic-gate */ 4400Sstevel@tonic-gate static int 4410Sstevel@tonic-gate entity_find2(repcache_client_t *cp, uint32_t id1, repcache_entity_t **out1, 4420Sstevel@tonic-gate uint32_t id2, repcache_entity_t **out2) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate repcache_entity_t *e1, *e2; 4450Sstevel@tonic-gate request_log_entry_t *rlp; 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate if (id1 == id2) 4480Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_DUPLICATE_ID); 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 451407Sjwadams e1 = uu_avl_find(cp->rc_entities, &id1, NULL, NULL); 452407Sjwadams e2 = uu_avl_find(cp->rc_entities, &id2, NULL, NULL); 4530Sstevel@tonic-gate if (e1 == NULL || e2 == NULL) { 4540Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 4550Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate assert(e1 != e2); 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * locks are ordered by id number 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate if (id1 < id2) { 4640Sstevel@tonic-gate (void) pthread_mutex_lock(&e1->re_lock); 4650Sstevel@tonic-gate (void) pthread_mutex_lock(&e2->re_lock); 4660Sstevel@tonic-gate } else { 4670Sstevel@tonic-gate (void) pthread_mutex_lock(&e2->re_lock); 4680Sstevel@tonic-gate (void) pthread_mutex_lock(&e1->re_lock); 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate *out1 = e1; 4710Sstevel@tonic-gate *out2 = e2; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate if ((rlp = get_log()) != NULL) { 4760Sstevel@tonic-gate add_log_ptr(rlp, RC_PTR_TYPE_ENTITY, id1, e1); 4770Sstevel@tonic-gate add_log_ptr(rlp, RC_PTR_TYPE_ENTITY, id2, e2); 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate static void 4840Sstevel@tonic-gate entity_release(repcache_entity_t *ep) 4850Sstevel@tonic-gate { 4860Sstevel@tonic-gate assert(ep->re_node.rnp_node == NULL || 4870Sstevel@tonic-gate !MUTEX_HELD(&ep->re_node.rnp_node->rn_lock)); 4880Sstevel@tonic-gate (void) pthread_mutex_unlock(&ep->re_lock); 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate static void 4920Sstevel@tonic-gate entity_destroy(repcache_entity_t *entity) 4930Sstevel@tonic-gate { 4940Sstevel@tonic-gate (void) pthread_mutex_lock(&entity->re_lock); 4950Sstevel@tonic-gate rc_node_clear(&entity->re_node, 0); 4960Sstevel@tonic-gate (void) pthread_mutex_unlock(&entity->re_lock); 4970Sstevel@tonic-gate 498407Sjwadams uu_avl_node_fini(entity, &entity->re_link, entity_pool); 4990Sstevel@tonic-gate (void) pthread_mutex_destroy(&entity->re_lock); 5000Sstevel@tonic-gate uu_free(entity); 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate static void 5040Sstevel@tonic-gate entity_remove(repcache_client_t *cp, uint32_t id) 5050Sstevel@tonic-gate { 5060Sstevel@tonic-gate repcache_entity_t *entity; 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 509407Sjwadams entity = uu_avl_find(cp->rc_entities, &id, NULL, NULL); 5100Sstevel@tonic-gate if (entity != NULL) 511407Sjwadams uu_avl_remove(cp->rc_entities, entity); 5120Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate if (entity != NULL) 5150Sstevel@tonic-gate entity_destroy(entity); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate static void 5190Sstevel@tonic-gate entity_cleanup(repcache_client_t *cp) 5200Sstevel@tonic-gate { 5210Sstevel@tonic-gate repcache_entity_t *ep; 5220Sstevel@tonic-gate void *cookie = NULL; 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 525407Sjwadams while ((ep = uu_avl_teardown(cp->rc_entities, &cookie)) != NULL) { 5260Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 5270Sstevel@tonic-gate entity_destroy(ep); 5280Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /*ARGSUSED*/ 5340Sstevel@tonic-gate static repcache_iter_t * 5350Sstevel@tonic-gate iter_alloc(repcache_client_t *cp) 5360Sstevel@tonic-gate { 5370Sstevel@tonic-gate repcache_iter_t *iter; 5380Sstevel@tonic-gate iter = uu_zalloc(sizeof (repcache_iter_t)); 5390Sstevel@tonic-gate if (iter != NULL) 540407Sjwadams uu_avl_node_init(iter, &iter->ri_link, iter_pool); 5410Sstevel@tonic-gate return (iter); 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate static void 5450Sstevel@tonic-gate iter_add(repcache_client_t *cp, repcache_iter_t *iter) 5460Sstevel@tonic-gate { 5470Sstevel@tonic-gate uu_list_index_t idx; 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 5500Sstevel@tonic-gate assert(cp->rc_insert_thr == pthread_self()); 5510Sstevel@tonic-gate 552407Sjwadams (void) uu_avl_find(cp->rc_iters, iter, NULL, &idx); 553407Sjwadams uu_avl_insert(cp->rc_iters, iter, idx); 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate static repcache_iter_t * 5590Sstevel@tonic-gate iter_find(repcache_client_t *cp, uint32_t id) 5600Sstevel@tonic-gate { 5610Sstevel@tonic-gate repcache_iter_t *iter; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 5640Sstevel@tonic-gate 565407Sjwadams iter = uu_avl_find(cp->rc_iters, &id, NULL, NULL); 5660Sstevel@tonic-gate if (iter != NULL) { 5670Sstevel@tonic-gate add_log_ptr(get_log(), RC_PTR_TYPE_ITER, id, iter); 5680Sstevel@tonic-gate (void) pthread_mutex_lock(&iter->ri_lock); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate return (iter); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate /* 5760Sstevel@tonic-gate * Fails with 5770Sstevel@tonic-gate * _UNKNOWN_ID - iter_id or entity_id does not designate an active register 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate static int 5800Sstevel@tonic-gate iter_find_w_entity(repcache_client_t *cp, uint32_t iter_id, 5810Sstevel@tonic-gate repcache_iter_t **iterp, uint32_t entity_id, repcache_entity_t **epp) 5820Sstevel@tonic-gate { 5830Sstevel@tonic-gate repcache_iter_t *iter; 5840Sstevel@tonic-gate repcache_entity_t *ep; 5850Sstevel@tonic-gate request_log_entry_t *rlp; 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 588407Sjwadams iter = uu_avl_find(cp->rc_iters, &iter_id, NULL, NULL); 589407Sjwadams ep = uu_avl_find(cp->rc_entities, &entity_id, NULL, NULL); 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate assert(iter == NULL || !MUTEX_HELD(&iter->ri_lock)); 5920Sstevel@tonic-gate assert(ep == NULL || !MUTEX_HELD(&ep->re_lock)); 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate if (iter == NULL || ep == NULL) { 5950Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 5960Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate (void) pthread_mutex_lock(&iter->ri_lock); 6000Sstevel@tonic-gate (void) pthread_mutex_lock(&ep->re_lock); 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate *iterp = iter; 6050Sstevel@tonic-gate *epp = ep; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate if ((rlp = get_log()) != NULL) { 6080Sstevel@tonic-gate add_log_ptr(rlp, RC_PTR_TYPE_ENTITY, entity_id, ep); 6090Sstevel@tonic-gate add_log_ptr(rlp, RC_PTR_TYPE_ITER, iter_id, iter); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate static void 6160Sstevel@tonic-gate iter_release(repcache_iter_t *iter) 6170Sstevel@tonic-gate { 6180Sstevel@tonic-gate (void) pthread_mutex_unlock(&iter->ri_lock); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate static void 6220Sstevel@tonic-gate iter_destroy(repcache_iter_t *iter) 6230Sstevel@tonic-gate { 6240Sstevel@tonic-gate (void) pthread_mutex_lock(&iter->ri_lock); 6250Sstevel@tonic-gate rc_iter_destroy(&iter->ri_iter); 6260Sstevel@tonic-gate (void) pthread_mutex_unlock(&iter->ri_lock); 6270Sstevel@tonic-gate 628407Sjwadams uu_avl_node_fini(iter, &iter->ri_link, iter_pool); 6290Sstevel@tonic-gate (void) pthread_mutex_destroy(&iter->ri_lock); 6300Sstevel@tonic-gate uu_free(iter); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate static void 6340Sstevel@tonic-gate iter_remove(repcache_client_t *cp, uint32_t id) 6350Sstevel@tonic-gate { 6360Sstevel@tonic-gate repcache_iter_t *iter; 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 639407Sjwadams iter = uu_avl_find(cp->rc_iters, &id, NULL, NULL); 6400Sstevel@tonic-gate if (iter != NULL) 641407Sjwadams uu_avl_remove(cp->rc_iters, iter); 6420Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate if (iter != NULL) 6450Sstevel@tonic-gate iter_destroy(iter); 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate static void 6490Sstevel@tonic-gate iter_cleanup(repcache_client_t *cp) 6500Sstevel@tonic-gate { 6510Sstevel@tonic-gate repcache_iter_t *iter; 6520Sstevel@tonic-gate void *cookie = NULL; 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 655407Sjwadams while ((iter = uu_avl_teardown(cp->rc_iters, &cookie)) != NULL) { 6560Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 6570Sstevel@tonic-gate iter_destroy(iter); 6580Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * Ensure that the passed client id is no longer usable, wait for any 6650Sstevel@tonic-gate * outstanding invocations to complete, then destroy the client 6660Sstevel@tonic-gate * structure. 6670Sstevel@tonic-gate */ 6680Sstevel@tonic-gate static void 6690Sstevel@tonic-gate client_destroy(uint32_t id) 6700Sstevel@tonic-gate { 6710Sstevel@tonic-gate client_bucket_t *bp = CLIENT_HASH(id); 6720Sstevel@tonic-gate repcache_client_t *cp; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate (void) pthread_mutex_lock(&bp->cb_lock); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate cp = uu_list_find(bp->cb_list, &id, NULL, NULL); 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate if (cp == NULL) { 6790Sstevel@tonic-gate (void) pthread_mutex_unlock(&bp->cb_lock); 6800Sstevel@tonic-gate return; 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate uu_list_remove(bp->cb_list, cp); 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate (void) pthread_mutex_unlock(&bp->cb_lock); 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate /* kick the waiters out */ 6880Sstevel@tonic-gate rc_notify_info_fini(&cp->rc_notify_info); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 6910Sstevel@tonic-gate assert(!(cp->rc_flags & RC_CLIENT_DEAD)); 6920Sstevel@tonic-gate cp->rc_flags |= RC_CLIENT_DEAD; 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate if (cp->rc_doorfd != -1) { 6950Sstevel@tonic-gate if (door_revoke(cp->rc_doorfd) < 0) 6960Sstevel@tonic-gate perror("door_revoke"); 6970Sstevel@tonic-gate cp->rc_doorfd = -1; 6980Sstevel@tonic-gate cp->rc_doorid = INVALID_DOORID; 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate while (cp->rc_refcnt > 0) 7020Sstevel@tonic-gate (void) pthread_cond_wait(&cp->rc_cv, &cp->rc_lock); 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate assert(cp->rc_insert_thr == 0 && cp->rc_notify_thr == 0); 7050Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate /* 7080Sstevel@tonic-gate * destroy outstanding objects 7090Sstevel@tonic-gate */ 7100Sstevel@tonic-gate entity_cleanup(cp); 7110Sstevel@tonic-gate iter_cleanup(cp); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate /* 7140Sstevel@tonic-gate * clean up notifications 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate rc_pg_notify_fini(&cp->rc_pg_notify); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate client_free(cp); 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* 7220Sstevel@tonic-gate * Fails with 7230Sstevel@tonic-gate * _TYPE_MISMATCH - the entity is already set up with a different type 7240Sstevel@tonic-gate * _NO_RESOURCES - out of memory 7250Sstevel@tonic-gate */ 7260Sstevel@tonic-gate static int 7270Sstevel@tonic-gate entity_setup(repcache_client_t *cp, struct rep_protocol_entity_setup *rpr) 7280Sstevel@tonic-gate { 7290Sstevel@tonic-gate repcache_entity_t *ep; 7300Sstevel@tonic-gate uint32_t type; 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate client_start_insert(cp); 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate if ((ep = entity_find(cp, rpr->rpr_entityid)) != NULL) { 7350Sstevel@tonic-gate type = ep->re_type; 7360Sstevel@tonic-gate entity_release(ep); 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate client_end_insert(cp); 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate if (type != rpr->rpr_entitytype) 7410Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_TYPE_MISMATCH); 7420Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate switch (type = rpr->rpr_entitytype) { 7460Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SCOPE: 7470Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SERVICE: 7480Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_INSTANCE: 7490Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SNAPSHOT: 7500Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_SNAPLEVEL: 7510Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_PROPERTYGRP: 7520Sstevel@tonic-gate case REP_PROTOCOL_ENTITY_PROPERTY: 7530Sstevel@tonic-gate break; 7540Sstevel@tonic-gate default: 7550Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate ep = entity_alloc(cp); 7590Sstevel@tonic-gate if (ep == NULL) { 7600Sstevel@tonic-gate client_end_insert(cp); 7610Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate ep->re_id = rpr->rpr_entityid; 7650Sstevel@tonic-gate ep->re_changeid = INVALID_CHANGEID; 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate ep->re_type = type; 7680Sstevel@tonic-gate rc_node_ptr_init(&ep->re_node); 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate entity_add(cp, ep); 7710Sstevel@tonic-gate client_end_insert(cp); 7720Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate /*ARGSUSED*/ 7760Sstevel@tonic-gate static void 7770Sstevel@tonic-gate entity_name(repcache_client_t *cp, const void *in, size_t insz, void *out_arg, 7780Sstevel@tonic-gate size_t *outsz, void *arg) 7790Sstevel@tonic-gate { 7800Sstevel@tonic-gate const struct rep_protocol_entity_name *rpr = in; 7810Sstevel@tonic-gate struct rep_protocol_name_response *out = out_arg; 7820Sstevel@tonic-gate repcache_entity_t *ep; 7830Sstevel@tonic-gate size_t sz = sizeof (out->rpr_name); 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate ep = entity_find(cp, rpr->rpr_entityid); 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate if (ep == NULL) { 7900Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_UNKNOWN_ID; 7910Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 7920Sstevel@tonic-gate return; 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate out->rpr_response = rc_node_name(&ep->re_node, out->rpr_name, 7950Sstevel@tonic-gate sz, rpr->rpr_answertype, &sz); 7960Sstevel@tonic-gate entity_release(ep); 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate /* 7990Sstevel@tonic-gate * If we fail, we only return the response code. 8000Sstevel@tonic-gate * If we succeed, we don't return anything after the '\0' in rpr_name. 8010Sstevel@tonic-gate */ 8020Sstevel@tonic-gate if (out->rpr_response != REP_PROTOCOL_SUCCESS) 8030Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 8040Sstevel@tonic-gate else 8050Sstevel@tonic-gate *outsz = offsetof(struct rep_protocol_name_response, 8060Sstevel@tonic-gate rpr_name[sz + 1]); 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate /*ARGSUSED*/ 8100Sstevel@tonic-gate static void 8110Sstevel@tonic-gate entity_parent_type(repcache_client_t *cp, const void *in, size_t insz, 8120Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg) 8130Sstevel@tonic-gate { 8140Sstevel@tonic-gate const struct rep_protocol_entity_name *rpr = in; 8150Sstevel@tonic-gate struct rep_protocol_integer_response *out = out_arg; 8160Sstevel@tonic-gate repcache_entity_t *ep; 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate ep = entity_find(cp, rpr->rpr_entityid); 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate if (ep == NULL) { 8230Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_UNKNOWN_ID; 8240Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 8250Sstevel@tonic-gate return; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate out->rpr_response = rc_node_parent_type(&ep->re_node, &out->rpr_value); 8290Sstevel@tonic-gate entity_release(ep); 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate if (out->rpr_response != REP_PROTOCOL_SUCCESS) 8320Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * Fails with 8370Sstevel@tonic-gate * _DUPLICATE_ID - the ids are equal 8380Sstevel@tonic-gate * _UNKNOWN_ID - an id does not designate an active register 8390Sstevel@tonic-gate * _INVALID_TYPE - type is invalid 8400Sstevel@tonic-gate * _TYPE_MISMATCH - np doesn't carry children of type type 8410Sstevel@tonic-gate * _DELETED - np has been deleted 8420Sstevel@tonic-gate * _NOT_FOUND - no child with that name/type combo found 8430Sstevel@tonic-gate * _NO_RESOURCES 8440Sstevel@tonic-gate * _BACKEND_ACCESS 8450Sstevel@tonic-gate */ 8460Sstevel@tonic-gate static int 8470Sstevel@tonic-gate entity_get_child(repcache_client_t *cp, 8480Sstevel@tonic-gate struct rep_protocol_entity_get_child *rpr) 8490Sstevel@tonic-gate { 8500Sstevel@tonic-gate repcache_entity_t *parent, *child; 8510Sstevel@tonic-gate int result; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate uint32_t parentid = rpr->rpr_entityid; 8540Sstevel@tonic-gate uint32_t childid = rpr->rpr_childid; 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate result = entity_find2(cp, childid, &child, parentid, &parent); 8570Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 8580Sstevel@tonic-gate return (result); 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate rpr->rpr_name[sizeof (rpr->rpr_name) - 1] = 0; 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate result = rc_node_get_child(&parent->re_node, rpr->rpr_name, 8630Sstevel@tonic-gate child->re_type, &child->re_node); 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate entity_release(child); 8660Sstevel@tonic-gate entity_release(parent); 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate return (result); 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate /* 8720Sstevel@tonic-gate * Returns _FAIL_DUPLICATE_ID, _FAIL_UNKNOWN_ID, _FAIL_NOT_SET, _FAIL_DELETED, 8730Sstevel@tonic-gate * _FAIL_TYPE_MISMATCH, _FAIL_NOT_FOUND (scope has no parent), or _SUCCESS. 8740Sstevel@tonic-gate * Fails with 8750Sstevel@tonic-gate * _DUPLICATE_ID - the ids are equal 8760Sstevel@tonic-gate * _UNKNOWN_ID - an id does not designate an active register 8770Sstevel@tonic-gate * _NOT_SET - child is not set 8780Sstevel@tonic-gate * _DELETED - child has been deleted 8790Sstevel@tonic-gate * _TYPE_MISMATCH - child's parent does not match that of the parent register 8800Sstevel@tonic-gate * _NOT_FOUND - child has no parent (and is a scope) 8810Sstevel@tonic-gate */ 8820Sstevel@tonic-gate static int 8830Sstevel@tonic-gate entity_get_parent(repcache_client_t *cp, struct rep_protocol_entity_parent *rpr) 8840Sstevel@tonic-gate { 8850Sstevel@tonic-gate repcache_entity_t *child, *parent; 8860Sstevel@tonic-gate int result; 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate uint32_t childid = rpr->rpr_entityid; 8890Sstevel@tonic-gate uint32_t outid = rpr->rpr_outid; 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate result = entity_find2(cp, childid, &child, outid, &parent); 8920Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 8930Sstevel@tonic-gate return (result); 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate result = rc_node_get_parent(&child->re_node, parent->re_type, 8960Sstevel@tonic-gate &parent->re_node); 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate entity_release(child); 8990Sstevel@tonic-gate entity_release(parent); 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate return (result); 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate static int 9050Sstevel@tonic-gate entity_get(repcache_client_t *cp, struct rep_protocol_entity_get *rpr) 9060Sstevel@tonic-gate { 9070Sstevel@tonic-gate repcache_entity_t *ep; 9080Sstevel@tonic-gate int result; 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate ep = entity_find(cp, rpr->rpr_entityid); 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate if (ep == NULL) 9130Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate switch (rpr->rpr_object) { 9160Sstevel@tonic-gate case RP_ENTITY_GET_INVALIDATE: 9170Sstevel@tonic-gate rc_node_clear(&ep->re_node, 0); 9180Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 9190Sstevel@tonic-gate break; 9200Sstevel@tonic-gate case RP_ENTITY_GET_MOST_LOCAL_SCOPE: 9210Sstevel@tonic-gate result = rc_local_scope(ep->re_type, &ep->re_node); 9220Sstevel@tonic-gate break; 9230Sstevel@tonic-gate default: 9240Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_BAD_REQUEST; 9250Sstevel@tonic-gate break; 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate entity_release(ep); 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate return (result); 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate static int 9340Sstevel@tonic-gate entity_update(repcache_client_t *cp, struct rep_protocol_entity_update *rpr) 9350Sstevel@tonic-gate { 9360Sstevel@tonic-gate repcache_entity_t *ep; 9370Sstevel@tonic-gate int result; 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate if (rpr->rpr_changeid == INVALID_CHANGEID) 9400Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate ep = entity_find(cp, rpr->rpr_entityid); 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate if (ep == NULL) 9450Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate if (ep->re_changeid == rpr->rpr_changeid) { 9480Sstevel@tonic-gate result = REP_PROTOCOL_DONE; 9490Sstevel@tonic-gate } else { 9500Sstevel@tonic-gate result = rc_node_update(&ep->re_node); 9510Sstevel@tonic-gate if (result == REP_PROTOCOL_DONE) 9520Sstevel@tonic-gate ep->re_changeid = rpr->rpr_changeid; 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate entity_release(ep); 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate return (result); 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate static int 9610Sstevel@tonic-gate entity_reset(repcache_client_t *cp, struct rep_protocol_entity_reset *rpr) 9620Sstevel@tonic-gate { 9630Sstevel@tonic-gate repcache_entity_t *ep; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate ep = entity_find(cp, rpr->rpr_entityid); 9660Sstevel@tonic-gate if (ep == NULL) 9670Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate rc_node_clear(&ep->re_node, 0); 9700Sstevel@tonic-gate ep->re_txstate = REPCACHE_TX_INIT; 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate entity_release(ep); 9730Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate /* 9770Sstevel@tonic-gate * Fails with 9780Sstevel@tonic-gate * _BAD_REQUEST - request has invalid changeid 9790Sstevel@tonic-gate * rpr_name is invalid 9800Sstevel@tonic-gate * cannot create children for parent's type of node 9810Sstevel@tonic-gate * _DUPLICATE_ID - request has duplicate ids 9820Sstevel@tonic-gate * _UNKNOWN_ID - request has unknown id 9830Sstevel@tonic-gate * _DELETED - parent has been deleted 9840Sstevel@tonic-gate * _NOT_SET - parent is reset 9850Sstevel@tonic-gate * _NOT_APPLICABLE - rpr_childtype is _PROPERTYGRP 9860Sstevel@tonic-gate * _INVALID_TYPE - parent is corrupt or rpr_childtype is invalid 9870Sstevel@tonic-gate * _TYPE_MISMATCH - parent cannot have children of type rpr_childtype 9880Sstevel@tonic-gate * _NO_RESOURCES 9890Sstevel@tonic-gate * _PERMISSION_DENIED 9900Sstevel@tonic-gate * _BACKEND_ACCESS 9910Sstevel@tonic-gate * _BACKEND_READONLY 9920Sstevel@tonic-gate * _EXISTS - child already exists 9930Sstevel@tonic-gate */ 9940Sstevel@tonic-gate static int 9950Sstevel@tonic-gate entity_create_child(repcache_client_t *cp, 9960Sstevel@tonic-gate struct rep_protocol_entity_create_child *rpr) 9970Sstevel@tonic-gate { 9980Sstevel@tonic-gate repcache_entity_t *parent; 9990Sstevel@tonic-gate repcache_entity_t *child; 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate uint32_t parentid = rpr->rpr_entityid; 10020Sstevel@tonic-gate uint32_t childid = rpr->rpr_childid; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate int result; 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate if (rpr->rpr_changeid == INVALID_CHANGEID) 10070Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate result = entity_find2(cp, parentid, &parent, childid, &child); 10100Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 10110Sstevel@tonic-gate return (result); 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate rpr->rpr_name[sizeof (rpr->rpr_name) - 1] = 0; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate if (child->re_changeid == rpr->rpr_changeid) { 10160Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 10170Sstevel@tonic-gate } else { 10180Sstevel@tonic-gate result = rc_node_create_child(&parent->re_node, 10190Sstevel@tonic-gate rpr->rpr_childtype, rpr->rpr_name, &child->re_node); 10200Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) 10210Sstevel@tonic-gate child->re_changeid = rpr->rpr_changeid; 10220Sstevel@tonic-gate } 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate entity_release(parent); 10250Sstevel@tonic-gate entity_release(child); 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate return (result); 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate static int 10310Sstevel@tonic-gate entity_create_pg(repcache_client_t *cp, 10320Sstevel@tonic-gate struct rep_protocol_entity_create_pg *rpr) 10330Sstevel@tonic-gate { 10340Sstevel@tonic-gate repcache_entity_t *parent; 10350Sstevel@tonic-gate repcache_entity_t *child; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate uint32_t parentid = rpr->rpr_entityid; 10380Sstevel@tonic-gate uint32_t childid = rpr->rpr_childid; 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate int result; 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate if (rpr->rpr_changeid == INVALID_CHANGEID) 10430Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate result = entity_find2(cp, parentid, &parent, childid, &child); 10460Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 10470Sstevel@tonic-gate return (result); 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate rpr->rpr_name[sizeof (rpr->rpr_name) - 1] = 0; 10500Sstevel@tonic-gate rpr->rpr_type[sizeof (rpr->rpr_type) - 1] = 0; 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate if (child->re_changeid == rpr->rpr_changeid) { 10530Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 10540Sstevel@tonic-gate } else { 10550Sstevel@tonic-gate result = rc_node_create_child_pg(&parent->re_node, 10560Sstevel@tonic-gate child->re_type, rpr->rpr_name, rpr->rpr_type, 10570Sstevel@tonic-gate rpr->rpr_flags, &child->re_node); 10580Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) 10590Sstevel@tonic-gate child->re_changeid = rpr->rpr_changeid; 10600Sstevel@tonic-gate } 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate entity_release(parent); 10630Sstevel@tonic-gate entity_release(child); 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate return (result); 10660Sstevel@tonic-gate } 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate static int 10690Sstevel@tonic-gate entity_delete(repcache_client_t *cp, 10700Sstevel@tonic-gate struct rep_protocol_entity_delete *rpr) 10710Sstevel@tonic-gate { 10720Sstevel@tonic-gate repcache_entity_t *entity; 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate uint32_t entityid = rpr->rpr_entityid; 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate int result; 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate if (rpr->rpr_changeid == INVALID_CHANGEID) 10790Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate entity = entity_find(cp, entityid); 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate if (entity == NULL) 10840Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate if (entity->re_changeid == rpr->rpr_changeid) { 10870Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 10880Sstevel@tonic-gate } else { 10890Sstevel@tonic-gate result = rc_node_delete(&entity->re_node); 10900Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) 10910Sstevel@tonic-gate entity->re_changeid = rpr->rpr_changeid; 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate entity_release(entity); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate return (result); 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate static rep_protocol_responseid_t 11000Sstevel@tonic-gate entity_teardown(repcache_client_t *cp, struct rep_protocol_entity_teardown *rpr) 11010Sstevel@tonic-gate { 11020Sstevel@tonic-gate entity_remove(cp, rpr->rpr_entityid); 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate /* 11080Sstevel@tonic-gate * Fails with 11090Sstevel@tonic-gate * _MISORDERED - the iterator exists and is not reset 11100Sstevel@tonic-gate * _NO_RESOURCES - out of memory 11110Sstevel@tonic-gate */ 11120Sstevel@tonic-gate static int 11130Sstevel@tonic-gate iter_setup(repcache_client_t *cp, struct rep_protocol_iter_request *rpr) 11140Sstevel@tonic-gate { 11150Sstevel@tonic-gate repcache_iter_t *iter; 11160Sstevel@tonic-gate uint32_t sequence; 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate client_start_insert(cp); 11190Sstevel@tonic-gate /* 11200Sstevel@tonic-gate * If the iter already exists, and hasn't been read from, 11210Sstevel@tonic-gate * we assume the previous call succeeded. 11220Sstevel@tonic-gate */ 11230Sstevel@tonic-gate if ((iter = iter_find(cp, rpr->rpr_iterid)) != NULL) { 11240Sstevel@tonic-gate sequence = iter->ri_sequence; 11250Sstevel@tonic-gate iter_release(iter); 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate client_end_insert(cp); 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate if (sequence != 0) 11300Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_MISORDERED); 11310Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate iter = iter_alloc(cp); 11350Sstevel@tonic-gate if (iter == NULL) { 11360Sstevel@tonic-gate client_end_insert(cp); 11370Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES); 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate iter->ri_id = rpr->rpr_iterid; 11410Sstevel@tonic-gate iter->ri_type = REP_PROTOCOL_TYPE_INVALID; 11420Sstevel@tonic-gate iter->ri_sequence = 0; 11430Sstevel@tonic-gate iter_add(cp, iter); 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate client_end_insert(cp); 11460Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate /* 11500Sstevel@tonic-gate * Fails with 11510Sstevel@tonic-gate * _UNKNOWN_ID 11520Sstevel@tonic-gate * _MISORDERED - iterator has already been started 11530Sstevel@tonic-gate * _NOT_SET 11540Sstevel@tonic-gate * _DELETED 11550Sstevel@tonic-gate * _TYPE_MISMATCH - entity cannot have type children 11560Sstevel@tonic-gate * _BAD_REQUEST - rpr_flags is invalid 11570Sstevel@tonic-gate * rpr_pattern is invalid 11580Sstevel@tonic-gate * _NO_RESOURCES 11590Sstevel@tonic-gate * _INVALID_TYPE 11600Sstevel@tonic-gate * _BACKEND_ACCESS 11610Sstevel@tonic-gate */ 11620Sstevel@tonic-gate static int 11630Sstevel@tonic-gate iter_start(repcache_client_t *cp, struct rep_protocol_iter_start *rpr) 11640Sstevel@tonic-gate { 11650Sstevel@tonic-gate int result; 11660Sstevel@tonic-gate repcache_iter_t *iter; 11670Sstevel@tonic-gate repcache_entity_t *ep; 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate result = iter_find_w_entity(cp, rpr->rpr_iterid, &iter, 11700Sstevel@tonic-gate rpr->rpr_entity, &ep); 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 11730Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate if (iter->ri_sequence > 1) { 11760Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_MISORDERED; 11770Sstevel@tonic-gate goto end; 11780Sstevel@tonic-gate } 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate if (iter->ri_sequence == 1) { 11810Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 11820Sstevel@tonic-gate goto end; 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate rpr->rpr_pattern[sizeof (rpr->rpr_pattern) - 1] = 0; 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate result = rc_node_setup_iter(&ep->re_node, &iter->ri_iter, 11880Sstevel@tonic-gate rpr->rpr_itertype, rpr->rpr_flags, rpr->rpr_pattern); 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) 11910Sstevel@tonic-gate iter->ri_sequence++; 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate end: 11940Sstevel@tonic-gate iter_release(iter); 11950Sstevel@tonic-gate entity_release(ep); 11960Sstevel@tonic-gate return (result); 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate /* 12000Sstevel@tonic-gate * Returns 12010Sstevel@tonic-gate * _UNKNOWN_ID 12020Sstevel@tonic-gate * _NOT_SET - iter has not been started 12030Sstevel@tonic-gate * _MISORDERED 12040Sstevel@tonic-gate * _BAD_REQUEST - iter walks values 12050Sstevel@tonic-gate * _TYPE_MISMATCH - iter does not walk type entities 12060Sstevel@tonic-gate * _DELETED - parent was deleted 12070Sstevel@tonic-gate * _NO_RESOURCES 12080Sstevel@tonic-gate * _INVALID_TYPE - type is invalid 12090Sstevel@tonic-gate * _DONE 12100Sstevel@tonic-gate * _SUCCESS 12110Sstevel@tonic-gate * 12120Sstevel@tonic-gate * For composed property group iterators, can also return 12130Sstevel@tonic-gate * _TYPE_MISMATCH - parent cannot have type children 12140Sstevel@tonic-gate * _BACKEND_ACCESS 12150Sstevel@tonic-gate */ 12160Sstevel@tonic-gate static rep_protocol_responseid_t 12170Sstevel@tonic-gate iter_read(repcache_client_t *cp, struct rep_protocol_iter_read *rpr) 12180Sstevel@tonic-gate { 12190Sstevel@tonic-gate rep_protocol_responseid_t result; 12200Sstevel@tonic-gate repcache_iter_t *iter; 12210Sstevel@tonic-gate repcache_entity_t *ep; 12220Sstevel@tonic-gate uint32_t sequence; 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate result = iter_find_w_entity(cp, rpr->rpr_iterid, &iter, 12250Sstevel@tonic-gate rpr->rpr_entityid, &ep); 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 12280Sstevel@tonic-gate return (result); 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate sequence = rpr->rpr_sequence; 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate if (iter->ri_sequence == 0) { 12330Sstevel@tonic-gate iter_release(iter); 12340Sstevel@tonic-gate entity_release(ep); 12350Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NOT_SET); 12360Sstevel@tonic-gate } 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate if (sequence == 1) { 12390Sstevel@tonic-gate iter_release(iter); 12400Sstevel@tonic-gate entity_release(ep); 12410Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_MISORDERED); 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate if (sequence == iter->ri_sequence) { 12450Sstevel@tonic-gate iter_release(iter); 12460Sstevel@tonic-gate entity_release(ep); 12470Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate if (sequence == iter->ri_sequence + 1) { 12510Sstevel@tonic-gate result = rc_iter_next(iter->ri_iter, &ep->re_node, 12520Sstevel@tonic-gate ep->re_type); 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) 12550Sstevel@tonic-gate iter->ri_sequence++; 12560Sstevel@tonic-gate 12570Sstevel@tonic-gate iter_release(iter); 12580Sstevel@tonic-gate entity_release(ep); 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate return (result); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate iter_release(iter); 12640Sstevel@tonic-gate entity_release(ep); 12650Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_MISORDERED); 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate /*ARGSUSED*/ 12690Sstevel@tonic-gate static void 12700Sstevel@tonic-gate iter_read_value(repcache_client_t *cp, const void *in, size_t insz, 12710Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg) 12720Sstevel@tonic-gate { 12730Sstevel@tonic-gate const struct rep_protocol_iter_read_value *rpr = in; 12740Sstevel@tonic-gate struct rep_protocol_value_response *out = out_arg; 12750Sstevel@tonic-gate rep_protocol_responseid_t result; 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate repcache_iter_t *iter; 12780Sstevel@tonic-gate uint32_t sequence; 12790Sstevel@tonic-gate int repeat; 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate iter = iter_find(cp, rpr->rpr_iterid); 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate if (iter == NULL) { 12860Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_UNKNOWN_ID; 12870Sstevel@tonic-gate goto out; 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate sequence = rpr->rpr_sequence; 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate if (iter->ri_sequence == 0) { 12930Sstevel@tonic-gate iter_release(iter); 12940Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_NOT_SET; 12950Sstevel@tonic-gate goto out; 12960Sstevel@tonic-gate } 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate repeat = (sequence == iter->ri_sequence); 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate if (sequence == 1 || (!repeat && sequence != iter->ri_sequence + 1)) { 13010Sstevel@tonic-gate iter_release(iter); 13020Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_MISORDERED; 13030Sstevel@tonic-gate goto out; 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate result = rc_iter_next_value(iter->ri_iter, out, outsz, repeat); 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate if (!repeat && result == REP_PROTOCOL_SUCCESS) 13090Sstevel@tonic-gate iter->ri_sequence++; 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate iter_release(iter); 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate out: 13140Sstevel@tonic-gate /* 13150Sstevel@tonic-gate * If we fail, we only return the response code. 13160Sstevel@tonic-gate * If we succeed, rc_iter_next_value has shortened *outsz 13170Sstevel@tonic-gate * to only include the value bytes needed. 13180Sstevel@tonic-gate */ 13190Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS && result != REP_PROTOCOL_DONE) 13200Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 13210Sstevel@tonic-gate 13220Sstevel@tonic-gate out->rpr_response = result; 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate static int 13260Sstevel@tonic-gate iter_reset(repcache_client_t *cp, struct rep_protocol_iter_request *rpr) 13270Sstevel@tonic-gate { 13280Sstevel@tonic-gate repcache_iter_t *iter = iter_find(cp, rpr->rpr_iterid); 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate if (iter == NULL) 13310Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN_ID); 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate if (iter->ri_sequence != 0) { 13340Sstevel@tonic-gate iter->ri_sequence = 0; 13350Sstevel@tonic-gate rc_iter_destroy(&iter->ri_iter); 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate iter_release(iter); 13380Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 13390Sstevel@tonic-gate } 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate static rep_protocol_responseid_t 13420Sstevel@tonic-gate iter_teardown(repcache_client_t *cp, struct rep_protocol_iter_request *rpr) 13430Sstevel@tonic-gate { 13440Sstevel@tonic-gate iter_remove(cp, rpr->rpr_iterid); 13450Sstevel@tonic-gate 13460Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate static rep_protocol_responseid_t 13500Sstevel@tonic-gate tx_start(repcache_client_t *cp, struct rep_protocol_transaction_start *rpr) 13510Sstevel@tonic-gate { 13520Sstevel@tonic-gate repcache_entity_t *tx; 13530Sstevel@tonic-gate repcache_entity_t *ep; 13540Sstevel@tonic-gate rep_protocol_responseid_t result; 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate uint32_t txid = rpr->rpr_entityid_tx; 13570Sstevel@tonic-gate uint32_t epid = rpr->rpr_entityid; 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate result = entity_find2(cp, txid, &tx, epid, &ep); 13600Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 13610Sstevel@tonic-gate return (result); 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate if (tx->re_txstate == REPCACHE_TX_SETUP) { 13640Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 13650Sstevel@tonic-gate goto end; 13660Sstevel@tonic-gate } 13670Sstevel@tonic-gate if (tx->re_txstate != REPCACHE_TX_INIT) { 13680Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_MISORDERED; 13690Sstevel@tonic-gate goto end; 13700Sstevel@tonic-gate } 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate result = rc_node_setup_tx(&ep->re_node, &tx->re_node); 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate end: 13750Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) 13760Sstevel@tonic-gate tx->re_txstate = REPCACHE_TX_SETUP; 13770Sstevel@tonic-gate else 13780Sstevel@tonic-gate rc_node_clear(&tx->re_node, 0); 13790Sstevel@tonic-gate 13800Sstevel@tonic-gate entity_release(ep); 13810Sstevel@tonic-gate entity_release(tx); 13820Sstevel@tonic-gate return (result); 13830Sstevel@tonic-gate } 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate /*ARGSUSED*/ 13860Sstevel@tonic-gate static void 13870Sstevel@tonic-gate tx_commit(repcache_client_t *cp, const void *in, size_t insz, 13880Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg) 13890Sstevel@tonic-gate { 13900Sstevel@tonic-gate struct rep_protocol_response *out = out_arg; 13910Sstevel@tonic-gate const struct rep_protocol_transaction_commit *rpr = in; 13920Sstevel@tonic-gate repcache_entity_t *tx; 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 13950Sstevel@tonic-gate assert(insz >= REP_PROTOCOL_TRANSACTION_COMMIT_MIN_SIZE); 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate if (rpr->rpr_size != insz) { 13980Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_BAD_REQUEST; 13990Sstevel@tonic-gate return; 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate tx = entity_find(cp, rpr->rpr_entityid); 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate if (tx == NULL) { 14050Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_UNKNOWN_ID; 14060Sstevel@tonic-gate return; 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate switch (tx->re_txstate) { 14100Sstevel@tonic-gate case REPCACHE_TX_INIT: 14110Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_MISORDERED; 14120Sstevel@tonic-gate break; 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate case REPCACHE_TX_SETUP: 14150Sstevel@tonic-gate out->rpr_response = rc_tx_commit(&tx->re_node, rpr->rpr_cmd, 14160Sstevel@tonic-gate insz - REP_PROTOCOL_TRANSACTION_COMMIT_MIN_SIZE); 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate if (out->rpr_response == REP_PROTOCOL_SUCCESS) { 14190Sstevel@tonic-gate tx->re_txstate = REPCACHE_TX_COMMITTED; 14200Sstevel@tonic-gate rc_node_clear(&tx->re_node, 0); 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate break; 14240Sstevel@tonic-gate case REPCACHE_TX_COMMITTED: 14250Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_SUCCESS; 14260Sstevel@tonic-gate break; 14270Sstevel@tonic-gate default: 14280Sstevel@tonic-gate assert(0); /* CAN'T HAPPEN */ 14290Sstevel@tonic-gate break; 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate entity_release(tx); 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate static rep_protocol_responseid_t 14360Sstevel@tonic-gate next_snaplevel(repcache_client_t *cp, struct rep_protocol_entity_pair *rpr) 14370Sstevel@tonic-gate { 14380Sstevel@tonic-gate repcache_entity_t *src; 14390Sstevel@tonic-gate repcache_entity_t *dest; 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate uint32_t srcid = rpr->rpr_entity_src; 14420Sstevel@tonic-gate uint32_t destid = rpr->rpr_entity_dst; 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate int result; 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate result = entity_find2(cp, srcid, &src, destid, &dest); 14470Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 14480Sstevel@tonic-gate return (result); 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate result = rc_node_next_snaplevel(&src->re_node, &dest->re_node); 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate entity_release(src); 14530Sstevel@tonic-gate entity_release(dest); 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate return (result); 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate 14580Sstevel@tonic-gate static rep_protocol_responseid_t 14590Sstevel@tonic-gate snapshot_take(repcache_client_t *cp, struct rep_protocol_snapshot_take *rpr) 14600Sstevel@tonic-gate { 14610Sstevel@tonic-gate repcache_entity_t *src; 14620Sstevel@tonic-gate uint32_t srcid = rpr->rpr_entityid_src; 14630Sstevel@tonic-gate repcache_entity_t *dest; 14640Sstevel@tonic-gate uint32_t destid = rpr->rpr_entityid_dest; 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate int result; 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate result = entity_find2(cp, srcid, &src, destid, &dest); 14690Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 14700Sstevel@tonic-gate return (result); 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate if (dest->re_type != REP_PROTOCOL_ENTITY_SNAPSHOT) { 14730Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_TYPE_MISMATCH; 14740Sstevel@tonic-gate } else { 14750Sstevel@tonic-gate rpr->rpr_name[sizeof (rpr->rpr_name) - 1] = 0; 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate if (rpr->rpr_flags == REP_SNAPSHOT_NEW) 14780Sstevel@tonic-gate result = rc_snapshot_take_new(&src->re_node, NULL, 14790Sstevel@tonic-gate NULL, rpr->rpr_name, &dest->re_node); 14800Sstevel@tonic-gate else if (rpr->rpr_flags == REP_SNAPSHOT_ATTACH && 14810Sstevel@tonic-gate rpr->rpr_name[0] == 0) 14820Sstevel@tonic-gate result = rc_snapshot_take_attach(&src->re_node, 14830Sstevel@tonic-gate &dest->re_node); 14840Sstevel@tonic-gate else 14850Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_BAD_REQUEST; 14860Sstevel@tonic-gate } 14870Sstevel@tonic-gate entity_release(src); 14880Sstevel@tonic-gate entity_release(dest); 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate return (result); 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate static rep_protocol_responseid_t 14940Sstevel@tonic-gate snapshot_take_named(repcache_client_t *cp, 14950Sstevel@tonic-gate struct rep_protocol_snapshot_take_named *rpr) 14960Sstevel@tonic-gate { 14970Sstevel@tonic-gate repcache_entity_t *src; 14980Sstevel@tonic-gate uint32_t srcid = rpr->rpr_entityid_src; 14990Sstevel@tonic-gate repcache_entity_t *dest; 15000Sstevel@tonic-gate uint32_t destid = rpr->rpr_entityid_dest; 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate int result; 15030Sstevel@tonic-gate 15040Sstevel@tonic-gate result = entity_find2(cp, srcid, &src, destid, &dest); 15050Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 15060Sstevel@tonic-gate return (result); 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate if (dest->re_type != REP_PROTOCOL_ENTITY_SNAPSHOT) { 15090Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_TYPE_MISMATCH; 15100Sstevel@tonic-gate } else { 15110Sstevel@tonic-gate rpr->rpr_svcname[sizeof (rpr->rpr_svcname) - 1] = 0; 15120Sstevel@tonic-gate rpr->rpr_instname[sizeof (rpr->rpr_instname) - 1] = 0; 15130Sstevel@tonic-gate rpr->rpr_name[sizeof (rpr->rpr_name) - 1] = 0; 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate result = rc_snapshot_take_new(&src->re_node, rpr->rpr_svcname, 15160Sstevel@tonic-gate rpr->rpr_instname, rpr->rpr_name, &dest->re_node); 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate entity_release(src); 15190Sstevel@tonic-gate entity_release(dest); 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate return (result); 15220Sstevel@tonic-gate } 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate static rep_protocol_responseid_t 15250Sstevel@tonic-gate snapshot_attach(repcache_client_t *cp, struct rep_protocol_snapshot_attach *rpr) 15260Sstevel@tonic-gate { 15270Sstevel@tonic-gate repcache_entity_t *src; 15280Sstevel@tonic-gate uint32_t srcid = rpr->rpr_entityid_src; 15290Sstevel@tonic-gate repcache_entity_t *dest; 15300Sstevel@tonic-gate uint32_t destid = rpr->rpr_entityid_dest; 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate int result; 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate result = entity_find2(cp, srcid, &src, destid, &dest); 15350Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 15360Sstevel@tonic-gate return (result); 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate result = rc_snapshot_attach(&src->re_node, &dest->re_node); 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate entity_release(src); 15410Sstevel@tonic-gate entity_release(dest); 15420Sstevel@tonic-gate 15430Sstevel@tonic-gate return (result); 15440Sstevel@tonic-gate } 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate /*ARGSUSED*/ 15470Sstevel@tonic-gate static void 15480Sstevel@tonic-gate property_get_type(repcache_client_t *cp, const void *in, size_t insz, 15490Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg) 15500Sstevel@tonic-gate { 15510Sstevel@tonic-gate const struct rep_protocol_property_request *rpr = in; 15520Sstevel@tonic-gate struct rep_protocol_integer_response *out = out_arg; 15530Sstevel@tonic-gate repcache_entity_t *ep; 15540Sstevel@tonic-gate rep_protocol_value_type_t t = 0; 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate ep = entity_find(cp, rpr->rpr_entityid); 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate if (ep == NULL) { 15610Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_UNKNOWN_ID; 15620Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 15630Sstevel@tonic-gate return; 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate out->rpr_response = rc_node_get_property_type(&ep->re_node, &t); 15670Sstevel@tonic-gate 15680Sstevel@tonic-gate entity_release(ep); 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate if (out->rpr_response != REP_PROTOCOL_SUCCESS) 15710Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 15720Sstevel@tonic-gate else 15730Sstevel@tonic-gate out->rpr_value = t; 15740Sstevel@tonic-gate } 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate /* 15770Sstevel@tonic-gate * Fails with: 15780Sstevel@tonic-gate * _UNKNOWN_ID - an id does not designate an active register 15790Sstevel@tonic-gate * _NOT_SET - The property is not set 15800Sstevel@tonic-gate * _DELETED - The property has been deleted 15810Sstevel@tonic-gate * _TYPE_MISMATCH - The object is not a property 15820Sstevel@tonic-gate * _NOT_FOUND - The property has no values. 15830Sstevel@tonic-gate * 15840Sstevel@tonic-gate * Succeeds with: 15850Sstevel@tonic-gate * _SUCCESS - The property has 1 value. 15860Sstevel@tonic-gate * _TRUNCATED - The property has >1 value. 15870Sstevel@tonic-gate */ 15880Sstevel@tonic-gate /*ARGSUSED*/ 15890Sstevel@tonic-gate static void 15900Sstevel@tonic-gate property_get_value(repcache_client_t *cp, const void *in, size_t insz, 15910Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg) 15920Sstevel@tonic-gate { 15930Sstevel@tonic-gate const struct rep_protocol_property_request *rpr = in; 15940Sstevel@tonic-gate struct rep_protocol_value_response *out = out_arg; 15950Sstevel@tonic-gate repcache_entity_t *ep; 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate ep = entity_find(cp, rpr->rpr_entityid); 16000Sstevel@tonic-gate if (ep == NULL) { 16010Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_UNKNOWN_ID; 16020Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 16030Sstevel@tonic-gate return; 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate out->rpr_response = rc_node_get_property_value(&ep->re_node, out, 16070Sstevel@tonic-gate outsz); 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate entity_release(ep); 16100Sstevel@tonic-gate 16110Sstevel@tonic-gate /* 16120Sstevel@tonic-gate * If we fail, we only return the response code. 16130Sstevel@tonic-gate * If we succeed, rc_node_get_property_value has shortened *outsz 16140Sstevel@tonic-gate * to only include the value bytes needed. 16150Sstevel@tonic-gate */ 16160Sstevel@tonic-gate if (out->rpr_response != REP_PROTOCOL_SUCCESS && 16170Sstevel@tonic-gate out->rpr_response != REP_PROTOCOL_FAIL_TRUNCATED) 16180Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 16190Sstevel@tonic-gate } 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate static rep_protocol_responseid_t 16220Sstevel@tonic-gate propertygrp_notify(repcache_client_t *cp, 16230Sstevel@tonic-gate struct rep_protocol_propertygrp_request *rpr, int *out_fd) 16240Sstevel@tonic-gate { 16250Sstevel@tonic-gate int fds[2]; 16260Sstevel@tonic-gate int ours, theirs; 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate rep_protocol_responseid_t result; 16290Sstevel@tonic-gate repcache_entity_t *ep; 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate if (pipe(fds) < 0) 16320Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES); 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate ours = fds[0]; 16350Sstevel@tonic-gate theirs = fds[1]; 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate if ((ep = entity_find(cp, rpr->rpr_entityid)) == NULL) { 16380Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_UNKNOWN_ID; 16390Sstevel@tonic-gate goto fail; 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate /* 16430Sstevel@tonic-gate * While the following can race with other threads setting up a 16440Sstevel@tonic-gate * notification, the worst that can happen is that our fd has 16450Sstevel@tonic-gate * already been closed before we return. 16460Sstevel@tonic-gate */ 16470Sstevel@tonic-gate result = rc_pg_notify_setup(&cp->rc_pg_notify, &ep->re_node, 16480Sstevel@tonic-gate ours); 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate entity_release(ep); 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 16530Sstevel@tonic-gate goto fail; 16540Sstevel@tonic-gate 16550Sstevel@tonic-gate *out_fd = theirs; 16560Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate fail: 16590Sstevel@tonic-gate (void) close(ours); 16600Sstevel@tonic-gate (void) close(theirs); 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate return (result); 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate 16650Sstevel@tonic-gate static rep_protocol_responseid_t 16660Sstevel@tonic-gate client_add_notify(repcache_client_t *cp, 16670Sstevel@tonic-gate struct rep_protocol_notify_request *rpr) 16680Sstevel@tonic-gate { 16690Sstevel@tonic-gate rpr->rpr_pattern[sizeof (rpr->rpr_pattern) - 1] = 0; 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate switch (rpr->rpr_type) { 16720Sstevel@tonic-gate case REP_PROTOCOL_NOTIFY_PGNAME: 16730Sstevel@tonic-gate return (rc_notify_info_add_name(&cp->rc_notify_info, 16740Sstevel@tonic-gate rpr->rpr_pattern)); 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate case REP_PROTOCOL_NOTIFY_PGTYPE: 16770Sstevel@tonic-gate return (rc_notify_info_add_type(&cp->rc_notify_info, 16780Sstevel@tonic-gate rpr->rpr_pattern)); 16790Sstevel@tonic-gate 16800Sstevel@tonic-gate default: 16810Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 16820Sstevel@tonic-gate } 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate /*ARGSUSED*/ 16860Sstevel@tonic-gate static void 16870Sstevel@tonic-gate client_wait(repcache_client_t *cp, const void *in, size_t insz, 16880Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg) 16890Sstevel@tonic-gate { 16900Sstevel@tonic-gate int result; 16910Sstevel@tonic-gate repcache_entity_t *ep; 16920Sstevel@tonic-gate const struct rep_protocol_wait_request *rpr = in; 16930Sstevel@tonic-gate struct rep_protocol_fmri_response *out = out_arg; 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 16980Sstevel@tonic-gate if (cp->rc_notify_thr != 0) { 16990Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 17000Sstevel@tonic-gate out->rpr_response = REP_PROTOCOL_FAIL_EXISTS; 17010Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 17020Sstevel@tonic-gate return; 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate cp->rc_notify_thr = pthread_self(); 17050Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate result = rc_notify_info_wait(&cp->rc_notify_info, &cp->rc_notify_ptr, 17080Sstevel@tonic-gate out->rpr_fmri, sizeof (out->rpr_fmri)); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) { 17110Sstevel@tonic-gate if ((ep = entity_find(cp, rpr->rpr_entityid)) != NULL) { 17120Sstevel@tonic-gate if (ep->re_type == REP_PROTOCOL_ENTITY_PROPERTYGRP) { 17130Sstevel@tonic-gate rc_node_ptr_assign(&ep->re_node, 17140Sstevel@tonic-gate &cp->rc_notify_ptr); 17150Sstevel@tonic-gate } else { 17160Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_TYPE_MISMATCH; 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate entity_release(ep); 17190Sstevel@tonic-gate } else { 17200Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_UNKNOWN_ID; 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate rc_node_clear(&cp->rc_notify_ptr, 0); 17230Sstevel@tonic-gate } 17240Sstevel@tonic-gate 17250Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 17260Sstevel@tonic-gate assert(cp->rc_notify_thr == pthread_self()); 17270Sstevel@tonic-gate cp->rc_notify_thr = 0; 17280Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate out->rpr_response = result; 17310Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 17320Sstevel@tonic-gate *outsz = sizeof (out->rpr_response); 17330Sstevel@tonic-gate } 17340Sstevel@tonic-gate 17350Sstevel@tonic-gate /* 17360Sstevel@tonic-gate * Can return: 17370Sstevel@tonic-gate * _PERMISSION_DENIED not enough privileges to do request. 17380Sstevel@tonic-gate * _BAD_REQUEST name is not valid or reserved 17390Sstevel@tonic-gate * _TRUNCATED name is too long for current repository path 17400Sstevel@tonic-gate * _UNKNOWN failed for unknown reason (details written to 17410Sstevel@tonic-gate * console) 17420Sstevel@tonic-gate * _BACKEND_READONLY backend is not writable 17430Sstevel@tonic-gate * 17440Sstevel@tonic-gate * _SUCCESS Backup completed successfully. 17450Sstevel@tonic-gate */ 17460Sstevel@tonic-gate static rep_protocol_responseid_t 17470Sstevel@tonic-gate backup_repository(repcache_client_t *cp, 17480Sstevel@tonic-gate struct rep_protocol_backup_request *rpr) 17490Sstevel@tonic-gate { 17500Sstevel@tonic-gate rep_protocol_responseid_t result; 17510Sstevel@tonic-gate ucred_t *uc = get_ucred(); 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate if (!client_is_privileged() && (uc == NULL || ucred_geteuid(uc) != 0)) 17540Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_PERMISSION_DENIED); 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate rpr->rpr_name[REP_PROTOCOL_NAME_LEN - 1] = 0; 17570Sstevel@tonic-gate if (strcmp(rpr->rpr_name, REPOSITORY_BOOT_BACKUP) == 0) 17580Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate (void) pthread_mutex_lock(&cp->rc_lock); 17610Sstevel@tonic-gate if (rpr->rpr_changeid != cp->rc_changeid) { 17620Sstevel@tonic-gate result = backend_create_backup(rpr->rpr_name); 17630Sstevel@tonic-gate if (result == REP_PROTOCOL_SUCCESS) 17640Sstevel@tonic-gate cp->rc_changeid = rpr->rpr_changeid; 17650Sstevel@tonic-gate } else { 17660Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 17670Sstevel@tonic-gate } 17680Sstevel@tonic-gate (void) pthread_mutex_unlock(&cp->rc_lock); 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate return (result); 17710Sstevel@tonic-gate } 17720Sstevel@tonic-gate 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate typedef rep_protocol_responseid_t protocol_simple_f(repcache_client_t *cp, 17750Sstevel@tonic-gate const void *rpr); 17760Sstevel@tonic-gate 17770Sstevel@tonic-gate /*ARGSUSED*/ 17780Sstevel@tonic-gate static void 17790Sstevel@tonic-gate simple_handler(repcache_client_t *cp, const void *in, size_t insz, 17800Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg) 17810Sstevel@tonic-gate { 17820Sstevel@tonic-gate protocol_simple_f *f = (protocol_simple_f *)arg; 17830Sstevel@tonic-gate rep_protocol_response_t *out = out_arg; 17840Sstevel@tonic-gate 17850Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 17860Sstevel@tonic-gate assert(f != NULL); 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate out->rpr_response = (*f)(cp, in); 17890Sstevel@tonic-gate } 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate typedef rep_protocol_responseid_t protocol_simple_fd_f(repcache_client_t *cp, 17920Sstevel@tonic-gate const void *rpr, int *out_fd); 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate /*ARGSUSED*/ 17950Sstevel@tonic-gate static void 17960Sstevel@tonic-gate simple_fd_handler(repcache_client_t *cp, const void *in, size_t insz, 17970Sstevel@tonic-gate void *out_arg, size_t *outsz, void *arg, int *out_fd) 17980Sstevel@tonic-gate { 17990Sstevel@tonic-gate protocol_simple_fd_f *f = (protocol_simple_fd_f *)arg; 18000Sstevel@tonic-gate rep_protocol_response_t *out = out_arg; 18010Sstevel@tonic-gate 18020Sstevel@tonic-gate assert(*outsz == sizeof (*out)); 18030Sstevel@tonic-gate assert(f != NULL); 18040Sstevel@tonic-gate 18050Sstevel@tonic-gate out->rpr_response = (*f)(cp, in, out_fd); 18060Sstevel@tonic-gate } 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate typedef void protocol_handler_f(repcache_client_t *, const void *in, 18090Sstevel@tonic-gate size_t insz, void *out, size_t *outsz, void *arg); 18100Sstevel@tonic-gate 18110Sstevel@tonic-gate typedef void protocol_handler_fdret_f(repcache_client_t *, const void *in, 18120Sstevel@tonic-gate size_t insz, void *out, size_t *outsz, void *arg, int *fd_out); 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate #define PROTO(p, f, in) { \ 18150Sstevel@tonic-gate p, #p, simple_handler, (void *)(&f), NULL, \ 18160Sstevel@tonic-gate sizeof (in), sizeof (rep_protocol_response_t), 0 \ 18170Sstevel@tonic-gate } 18180Sstevel@tonic-gate 18190Sstevel@tonic-gate #define PROTO_FD_OUT(p, f, in) { \ 18200Sstevel@tonic-gate p, #p, NULL, (void *)(&f), simple_fd_handler, \ 18210Sstevel@tonic-gate sizeof (in), \ 18220Sstevel@tonic-gate sizeof (rep_protocol_response_t), \ 18230Sstevel@tonic-gate PROTO_FLAG_RETFD \ 18240Sstevel@tonic-gate } 18250Sstevel@tonic-gate 18260Sstevel@tonic-gate #define PROTO_VARIN(p, f, insz) { \ 18270Sstevel@tonic-gate p, #p, &(f), NULL, NULL, \ 18280Sstevel@tonic-gate insz, sizeof (rep_protocol_response_t), \ 18290Sstevel@tonic-gate PROTO_FLAG_VARINPUT \ 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate #define PROTO_UINT_OUT(p, f, in) { \ 18330Sstevel@tonic-gate p, #p, &(f), NULL, NULL, \ 18340Sstevel@tonic-gate sizeof (in), \ 18350Sstevel@tonic-gate sizeof (struct rep_protocol_integer_response), 0 \ 18360Sstevel@tonic-gate } 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate #define PROTO_NAME_OUT(p, f, in) { \ 18390Sstevel@tonic-gate p, #p, &(f), NULL, NULL, \ 18400Sstevel@tonic-gate sizeof (in), \ 18410Sstevel@tonic-gate sizeof (struct rep_protocol_name_response), 0 \ 18420Sstevel@tonic-gate } 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate #define PROTO_FMRI_OUT(p, f, in) { \ 18450Sstevel@tonic-gate p, #p, &(f), NULL, NULL, \ 18460Sstevel@tonic-gate sizeof (in), \ 18470Sstevel@tonic-gate sizeof (struct rep_protocol_fmri_response), 0 \ 18480Sstevel@tonic-gate } 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate #define PROTO_VALUE_OUT(p, f, in) { \ 18510Sstevel@tonic-gate p, #p, &(f), NULL, NULL, \ 18520Sstevel@tonic-gate sizeof (in), \ 18530Sstevel@tonic-gate sizeof (struct rep_protocol_value_response), 0 \ 18540Sstevel@tonic-gate } 18550Sstevel@tonic-gate 18560Sstevel@tonic-gate #define PROTO_PANIC(p) { p, #p, NULL, NULL, NULL, 0, 0, PROTO_FLAG_PANIC } 18570Sstevel@tonic-gate #define PROTO_END() { 0, NULL, NULL, NULL, NULL, 0, 0, PROTO_FLAG_PANIC } 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate #define PROTO_FLAG_PANIC 0x00000001 /* should never be called */ 18600Sstevel@tonic-gate #define PROTO_FLAG_VARINPUT 0x00000004 /* in_size is minimum size */ 18610Sstevel@tonic-gate #define PROTO_FLAG_RETFD 0x00000008 /* can also return an FD */ 18620Sstevel@tonic-gate 18630Sstevel@tonic-gate #define PROTO_ALL_FLAGS 0x0000000f /* all flags */ 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate static struct protocol_entry { 18660Sstevel@tonic-gate enum rep_protocol_requestid pt_request; 18670Sstevel@tonic-gate const char *pt_name; 18680Sstevel@tonic-gate protocol_handler_f *pt_handler; 18690Sstevel@tonic-gate void *pt_arg; 18700Sstevel@tonic-gate protocol_handler_fdret_f *pt_fd_handler; 18710Sstevel@tonic-gate size_t pt_in_size; 18720Sstevel@tonic-gate size_t pt_out_max; 18730Sstevel@tonic-gate uint32_t pt_flags; 18740Sstevel@tonic-gate } protocol_table[] = { 18750Sstevel@tonic-gate PROTO_PANIC(REP_PROTOCOL_CLOSE), /* special case */ 18760Sstevel@tonic-gate 18770Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_SETUP, entity_setup, 18780Sstevel@tonic-gate struct rep_protocol_entity_setup), 18790Sstevel@tonic-gate PROTO_NAME_OUT(REP_PROTOCOL_ENTITY_NAME, entity_name, 18800Sstevel@tonic-gate struct rep_protocol_entity_name), 18810Sstevel@tonic-gate PROTO_UINT_OUT(REP_PROTOCOL_ENTITY_PARENT_TYPE, entity_parent_type, 18820Sstevel@tonic-gate struct rep_protocol_entity_parent_type), 18830Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_GET_CHILD, entity_get_child, 18840Sstevel@tonic-gate struct rep_protocol_entity_get_child), 18850Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_GET_PARENT, entity_get_parent, 18860Sstevel@tonic-gate struct rep_protocol_entity_parent), 18870Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_GET, entity_get, 18880Sstevel@tonic-gate struct rep_protocol_entity_get), 18890Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_UPDATE, entity_update, 18900Sstevel@tonic-gate struct rep_protocol_entity_update), 18910Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_CREATE_CHILD, entity_create_child, 18920Sstevel@tonic-gate struct rep_protocol_entity_create_child), 18930Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_CREATE_PG, entity_create_pg, 18940Sstevel@tonic-gate struct rep_protocol_entity_create_pg), 18950Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_DELETE, entity_delete, 18960Sstevel@tonic-gate struct rep_protocol_entity_delete), 18970Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_RESET, entity_reset, 18980Sstevel@tonic-gate struct rep_protocol_entity_reset), 18990Sstevel@tonic-gate PROTO(REP_PROTOCOL_ENTITY_TEARDOWN, entity_teardown, 19000Sstevel@tonic-gate struct rep_protocol_entity_teardown), 19010Sstevel@tonic-gate 19020Sstevel@tonic-gate PROTO(REP_PROTOCOL_ITER_SETUP, iter_setup, 19030Sstevel@tonic-gate struct rep_protocol_iter_request), 19040Sstevel@tonic-gate PROTO(REP_PROTOCOL_ITER_START, iter_start, 19050Sstevel@tonic-gate struct rep_protocol_iter_start), 19060Sstevel@tonic-gate PROTO(REP_PROTOCOL_ITER_READ, iter_read, 19070Sstevel@tonic-gate struct rep_protocol_iter_read), 19080Sstevel@tonic-gate PROTO_VALUE_OUT(REP_PROTOCOL_ITER_READ_VALUE, iter_read_value, 19090Sstevel@tonic-gate struct rep_protocol_iter_read_value), 19100Sstevel@tonic-gate PROTO(REP_PROTOCOL_ITER_RESET, iter_reset, 19110Sstevel@tonic-gate struct rep_protocol_iter_request), 19120Sstevel@tonic-gate PROTO(REP_PROTOCOL_ITER_TEARDOWN, iter_teardown, 19130Sstevel@tonic-gate struct rep_protocol_iter_request), 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate PROTO(REP_PROTOCOL_NEXT_SNAPLEVEL, next_snaplevel, 19160Sstevel@tonic-gate struct rep_protocol_entity_pair), 19170Sstevel@tonic-gate 19180Sstevel@tonic-gate PROTO(REP_PROTOCOL_SNAPSHOT_TAKE, snapshot_take, 19190Sstevel@tonic-gate struct rep_protocol_snapshot_take), 19200Sstevel@tonic-gate PROTO(REP_PROTOCOL_SNAPSHOT_TAKE_NAMED, snapshot_take_named, 19210Sstevel@tonic-gate struct rep_protocol_snapshot_take_named), 19220Sstevel@tonic-gate PROTO(REP_PROTOCOL_SNAPSHOT_ATTACH, snapshot_attach, 19230Sstevel@tonic-gate struct rep_protocol_snapshot_attach), 19240Sstevel@tonic-gate 19250Sstevel@tonic-gate PROTO_UINT_OUT(REP_PROTOCOL_PROPERTY_GET_TYPE, property_get_type, 19260Sstevel@tonic-gate struct rep_protocol_property_request), 19270Sstevel@tonic-gate PROTO_VALUE_OUT(REP_PROTOCOL_PROPERTY_GET_VALUE, property_get_value, 19280Sstevel@tonic-gate struct rep_protocol_property_request), 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate PROTO_FD_OUT(REP_PROTOCOL_PROPERTYGRP_SETUP_WAIT, propertygrp_notify, 19310Sstevel@tonic-gate struct rep_protocol_propertygrp_request), 19320Sstevel@tonic-gate PROTO(REP_PROTOCOL_PROPERTYGRP_TX_START, tx_start, 19330Sstevel@tonic-gate struct rep_protocol_transaction_start), 19340Sstevel@tonic-gate PROTO_VARIN(REP_PROTOCOL_PROPERTYGRP_TX_COMMIT, tx_commit, 19350Sstevel@tonic-gate REP_PROTOCOL_TRANSACTION_COMMIT_MIN_SIZE), 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate PROTO(REP_PROTOCOL_CLIENT_ADD_NOTIFY, client_add_notify, 19380Sstevel@tonic-gate struct rep_protocol_notify_request), 19390Sstevel@tonic-gate PROTO_FMRI_OUT(REP_PROTOCOL_CLIENT_WAIT, client_wait, 19400Sstevel@tonic-gate struct rep_protocol_wait_request), 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate PROTO(REP_PROTOCOL_BACKUP, backup_repository, 19430Sstevel@tonic-gate struct rep_protocol_backup_request), 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate PROTO_END() 19460Sstevel@tonic-gate }; 19470Sstevel@tonic-gate #undef PROTO 19480Sstevel@tonic-gate #undef PROTO_FMRI_OUT 19490Sstevel@tonic-gate #undef PROTO_NAME_OUT 19500Sstevel@tonic-gate #undef PROTO_UINT_OUT 19510Sstevel@tonic-gate #undef PROTO_PANIC 19520Sstevel@tonic-gate #undef PROTO_END 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate /* 19550Sstevel@tonic-gate * The number of entries, sans PROTO_END() 19560Sstevel@tonic-gate */ 19570Sstevel@tonic-gate #define PROTOCOL_ENTRIES \ 19580Sstevel@tonic-gate (sizeof (protocol_table) / sizeof (*protocol_table) - 1) 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate #define PROTOCOL_PREFIX "REP_PROTOCOL_" 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate int 19630Sstevel@tonic-gate client_init(void) 19640Sstevel@tonic-gate { 19650Sstevel@tonic-gate int i; 19660Sstevel@tonic-gate struct protocol_entry *e; 19670Sstevel@tonic-gate 19680Sstevel@tonic-gate if (!client_hash_init()) 19690Sstevel@tonic-gate return (0); 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate if (request_log_size > 0) { 19720Sstevel@tonic-gate request_log = uu_zalloc(request_log_size * 19730Sstevel@tonic-gate sizeof (request_log_entry_t)); 19740Sstevel@tonic-gate } 19750Sstevel@tonic-gate 19760Sstevel@tonic-gate /* 19770Sstevel@tonic-gate * update the names to not include REP_PROTOCOL_ 19780Sstevel@tonic-gate */ 19790Sstevel@tonic-gate for (i = 0; i < PROTOCOL_ENTRIES; i++) { 19800Sstevel@tonic-gate e = &protocol_table[i]; 19810Sstevel@tonic-gate assert(strncmp(e->pt_name, PROTOCOL_PREFIX, 19820Sstevel@tonic-gate strlen(PROTOCOL_PREFIX)) == 0); 19830Sstevel@tonic-gate e->pt_name += strlen(PROTOCOL_PREFIX); 19840Sstevel@tonic-gate } 19850Sstevel@tonic-gate /* 19860Sstevel@tonic-gate * verify the protocol table is consistent 19870Sstevel@tonic-gate */ 19880Sstevel@tonic-gate for (i = 0; i < PROTOCOL_ENTRIES; i++) { 19890Sstevel@tonic-gate e = &protocol_table[i]; 19900Sstevel@tonic-gate assert(e->pt_request == (REP_PROTOCOL_BASE + i)); 19910Sstevel@tonic-gate 19920Sstevel@tonic-gate assert((e->pt_flags & ~PROTO_ALL_FLAGS) == 0); 19930Sstevel@tonic-gate 19940Sstevel@tonic-gate if (e->pt_flags & PROTO_FLAG_PANIC) 19950Sstevel@tonic-gate assert(e->pt_in_size == 0 && e->pt_out_max == 0 && 19960Sstevel@tonic-gate e->pt_handler == NULL); 19970Sstevel@tonic-gate else 19980Sstevel@tonic-gate assert(e->pt_in_size != 0 && e->pt_out_max != 0 && 19990Sstevel@tonic-gate (e->pt_handler != NULL || 20000Sstevel@tonic-gate e->pt_fd_handler != NULL)); 20010Sstevel@tonic-gate } 20020Sstevel@tonic-gate assert((REP_PROTOCOL_BASE + i) == REP_PROTOCOL_MAX_REQUEST); 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate assert(protocol_table[i].pt_request == 0); 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate return (1); 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate static void 20100Sstevel@tonic-gate client_switcher(void *cookie, char *argp, size_t arg_size, door_desc_t *desc_in, 20110Sstevel@tonic-gate uint_t n_desc) 20120Sstevel@tonic-gate { 20130Sstevel@tonic-gate thread_info_t *ti = thread_self(); 20140Sstevel@tonic-gate 20150Sstevel@tonic-gate repcache_client_t *cp; 20160Sstevel@tonic-gate uint32_t id = (uint32_t)cookie; 20170Sstevel@tonic-gate enum rep_protocol_requestid request_code; 20180Sstevel@tonic-gate 20190Sstevel@tonic-gate rep_protocol_responseid_t result = INVALID_RESULT; 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate struct protocol_entry *e; 20220Sstevel@tonic-gate 20230Sstevel@tonic-gate char *retval = NULL; 20240Sstevel@tonic-gate size_t retsize = 0; 20250Sstevel@tonic-gate 20260Sstevel@tonic-gate int retfd = -1; 20270Sstevel@tonic-gate door_desc_t desc; 20280Sstevel@tonic-gate request_log_entry_t *rlp; 20290Sstevel@tonic-gate 20300Sstevel@tonic-gate rlp = start_log(id); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate if (n_desc != 0) 20330Sstevel@tonic-gate uu_die("can't happen: %d descriptors @%p (cookie %p)", 20340Sstevel@tonic-gate n_desc, desc_in, cookie); 20350Sstevel@tonic-gate 20360Sstevel@tonic-gate if (argp == DOOR_UNREF_DATA) { 20370Sstevel@tonic-gate client_destroy(id); 20380Sstevel@tonic-gate goto bad_end; 20390Sstevel@tonic-gate } 20400Sstevel@tonic-gate 20410Sstevel@tonic-gate thread_newstate(ti, TI_CLIENT_CALL); 20420Sstevel@tonic-gate 20430Sstevel@tonic-gate /* 20440Sstevel@tonic-gate * To simplify returning just a result code, we set up for 20450Sstevel@tonic-gate * that case here. 20460Sstevel@tonic-gate */ 20470Sstevel@tonic-gate retval = (char *)&result; 20480Sstevel@tonic-gate retsize = sizeof (result); 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate if (arg_size < sizeof (request_code)) { 20510Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_BAD_REQUEST; 20520Sstevel@tonic-gate goto end_unheld; 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate 20550Sstevel@tonic-gate ti->ti_client_request = (void *)argp; 20560Sstevel@tonic-gate 20570Sstevel@tonic-gate /* LINTED alignment */ 20580Sstevel@tonic-gate request_code = *(uint32_t *)argp; 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate if (rlp != NULL) { 20610Sstevel@tonic-gate rlp->rl_request = request_code; 20620Sstevel@tonic-gate } 20630Sstevel@tonic-gate /* 20640Sstevel@tonic-gate * In order to avoid locking problems on removal, we handle the 20650Sstevel@tonic-gate * "close" case before doing a lookup. 20660Sstevel@tonic-gate */ 20670Sstevel@tonic-gate if (request_code == REP_PROTOCOL_CLOSE) { 20680Sstevel@tonic-gate client_destroy(id); 20690Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 20700Sstevel@tonic-gate goto end_unheld; 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate cp = client_lookup(id); 20740Sstevel@tonic-gate /* 20750Sstevel@tonic-gate * cp is held 20760Sstevel@tonic-gate */ 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate if (cp == NULL) 20790Sstevel@tonic-gate goto bad_end; 20800Sstevel@tonic-gate 20810Sstevel@tonic-gate if (rlp != NULL) 20820Sstevel@tonic-gate rlp->rl_client = cp; 20830Sstevel@tonic-gate 20840Sstevel@tonic-gate ti->ti_active_client = cp; 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate if (request_code < REP_PROTOCOL_BASE || 20870Sstevel@tonic-gate request_code >= REP_PROTOCOL_BASE + PROTOCOL_ENTRIES) { 20880Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_BAD_REQUEST; 20890Sstevel@tonic-gate goto end; 20900Sstevel@tonic-gate } 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate e = &protocol_table[request_code - REP_PROTOCOL_BASE]; 20930Sstevel@tonic-gate 20940Sstevel@tonic-gate assert(!(e->pt_flags & PROTO_FLAG_PANIC)); 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate if (e->pt_flags & PROTO_FLAG_VARINPUT) { 20970Sstevel@tonic-gate if (arg_size < e->pt_in_size) { 20980Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_BAD_REQUEST; 20990Sstevel@tonic-gate goto end; 21000Sstevel@tonic-gate } 21010Sstevel@tonic-gate } else if (arg_size != e->pt_in_size) { 21020Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_BAD_REQUEST; 21030Sstevel@tonic-gate goto end; 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate if (retsize != e->pt_out_max) { 21070Sstevel@tonic-gate retsize = e->pt_out_max; 21080Sstevel@tonic-gate retval = alloca(retsize); 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate if (e->pt_flags & PROTO_FLAG_RETFD) 21120Sstevel@tonic-gate e->pt_fd_handler(cp, argp, arg_size, retval, &retsize, 21130Sstevel@tonic-gate e->pt_arg, &retfd); 21140Sstevel@tonic-gate else 21150Sstevel@tonic-gate e->pt_handler(cp, argp, arg_size, retval, &retsize, e->pt_arg); 21160Sstevel@tonic-gate 21170Sstevel@tonic-gate end: 21180Sstevel@tonic-gate ti->ti_active_client = NULL; 21190Sstevel@tonic-gate client_release(cp); 21200Sstevel@tonic-gate 21210Sstevel@tonic-gate end_unheld: 21220Sstevel@tonic-gate if (rlp != NULL) { 21230Sstevel@tonic-gate /* LINTED alignment */ 21240Sstevel@tonic-gate rlp->rl_response = *(uint32_t *)retval; 21250Sstevel@tonic-gate end_log(); 21260Sstevel@tonic-gate rlp = NULL; 21270Sstevel@tonic-gate } 21280Sstevel@tonic-gate ti->ti_client_request = NULL; 21290Sstevel@tonic-gate thread_newstate(ti, TI_DOOR_RETURN); 21300Sstevel@tonic-gate 21310Sstevel@tonic-gate if (retval == (char *)&result) { 21320Sstevel@tonic-gate assert(result != INVALID_RESULT && retsize == sizeof (result)); 21330Sstevel@tonic-gate } else { 21340Sstevel@tonic-gate /* LINTED alignment */ 21350Sstevel@tonic-gate result = *(uint32_t *)retval; 21360Sstevel@tonic-gate } 21370Sstevel@tonic-gate if (retfd != -1) { 21380Sstevel@tonic-gate desc.d_attributes = DOOR_DESCRIPTOR | DOOR_RELEASE; 21390Sstevel@tonic-gate desc.d_data.d_desc.d_descriptor = retfd; 21400Sstevel@tonic-gate (void) door_return(retval, retsize, &desc, 1); 21410Sstevel@tonic-gate } else { 21420Sstevel@tonic-gate (void) door_return(retval, retsize, NULL, 0); 21430Sstevel@tonic-gate } 21440Sstevel@tonic-gate bad_end: 21450Sstevel@tonic-gate if (rlp != NULL) { 21460Sstevel@tonic-gate rlp->rl_response = -1; 21470Sstevel@tonic-gate end_log(); 21480Sstevel@tonic-gate rlp = NULL; 21490Sstevel@tonic-gate } 21500Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0); 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate 21530Sstevel@tonic-gate int 21540Sstevel@tonic-gate create_client(pid_t pid, uint32_t debugflags, int privileged, int *out_fd) 21550Sstevel@tonic-gate { 21560Sstevel@tonic-gate int fd; 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate repcache_client_t *cp; 21590Sstevel@tonic-gate 21600Sstevel@tonic-gate struct door_info info; 21610Sstevel@tonic-gate 21620Sstevel@tonic-gate int door_flags = DOOR_UNREF | DOOR_REFUSE_DESC; 21630Sstevel@tonic-gate #ifdef DOOR_NO_CANCEL 21640Sstevel@tonic-gate door_flags |= DOOR_NO_CANCEL; 21650Sstevel@tonic-gate #endif 21660Sstevel@tonic-gate 21670Sstevel@tonic-gate cp = client_alloc(); 21680Sstevel@tonic-gate if (cp == NULL) 21690Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_NO_RESOURCES); 21700Sstevel@tonic-gate 21710Sstevel@tonic-gate (void) pthread_mutex_lock(&client_lock); 21720Sstevel@tonic-gate cp->rc_id = ++client_maxid; 21730Sstevel@tonic-gate (void) pthread_mutex_unlock(&client_lock); 21740Sstevel@tonic-gate 21750Sstevel@tonic-gate cp->rc_all_auths = privileged; 21760Sstevel@tonic-gate cp->rc_pid = pid; 21770Sstevel@tonic-gate cp->rc_debug = debugflags; 21780Sstevel@tonic-gate 21790Sstevel@tonic-gate cp->rc_doorfd = door_create(client_switcher, (void *)cp->rc_id, 21800Sstevel@tonic-gate door_flags); 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate if (cp->rc_doorfd < 0) { 21830Sstevel@tonic-gate client_free(cp); 21840Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_NO_RESOURCES); 21850Sstevel@tonic-gate } 21860Sstevel@tonic-gate #ifdef DOOR_PARAM_DATA_MIN 21870Sstevel@tonic-gate (void) door_setparam(cp->rc_doorfd, DOOR_PARAM_DATA_MIN, 21880Sstevel@tonic-gate sizeof (enum rep_protocol_requestid)); 21890Sstevel@tonic-gate #endif 21900Sstevel@tonic-gate 21910Sstevel@tonic-gate if ((fd = dup(cp->rc_doorfd)) < 0 || 21920Sstevel@tonic-gate door_info(cp->rc_doorfd, &info) < 0) { 21930Sstevel@tonic-gate if (fd >= 0) 21940Sstevel@tonic-gate (void) close(fd); 21950Sstevel@tonic-gate (void) door_revoke(cp->rc_doorfd); 21960Sstevel@tonic-gate cp->rc_doorfd = -1; 21970Sstevel@tonic-gate client_free(cp); 21980Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_NO_RESOURCES); 21990Sstevel@tonic-gate } 22000Sstevel@tonic-gate 22010Sstevel@tonic-gate rc_pg_notify_init(&cp->rc_pg_notify); 22020Sstevel@tonic-gate rc_notify_info_init(&cp->rc_notify_info); 22030Sstevel@tonic-gate 22040Sstevel@tonic-gate client_insert(cp); 22050Sstevel@tonic-gate 22060Sstevel@tonic-gate cp->rc_doorid = info.di_uniquifier; 22070Sstevel@tonic-gate *out_fd = fd; 22080Sstevel@tonic-gate 22090Sstevel@tonic-gate return (REPOSITORY_DOOR_SUCCESS); 22100Sstevel@tonic-gate } 2211