1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 31*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 32*7c478bd9Sstevel@tonic-gate #include <stdio.h> 33*7c478bd9Sstevel@tonic-gate #include <strings.h> 34*7c478bd9Sstevel@tonic-gate #include <unistd.h> 35*7c478bd9Sstevel@tonic-gate #include <errno.h> 36*7c478bd9Sstevel@tonic-gate #include <libintl.h> 37*7c478bd9Sstevel@tonic-gate #include <libnvpair.h> 38*7c478bd9Sstevel@tonic-gate #include <thread.h> 39*7c478bd9Sstevel@tonic-gate #include <synch.h> 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #include "libcpc.h" 42*7c478bd9Sstevel@tonic-gate #include "libcpc_impl.h" 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate /* 45*7c478bd9Sstevel@tonic-gate * Pack a request set into a buffer using libnvpair. Size of buffer is returned 46*7c478bd9Sstevel@tonic-gate * in buflen. 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate char * 49*7c478bd9Sstevel@tonic-gate __cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen) 50*7c478bd9Sstevel@tonic-gate { 51*7c478bd9Sstevel@tonic-gate cpc_request_t *req; 52*7c478bd9Sstevel@tonic-gate nvlist_t *setlist, **reqlist; 53*7c478bd9Sstevel@tonic-gate size_t packsize = 0; 54*7c478bd9Sstevel@tonic-gate char *buf = NULL; 55*7c478bd9Sstevel@tonic-gate int i; 56*7c478bd9Sstevel@tonic-gate int j; 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate if (nvlist_alloc(&setlist, 0, 0) == ENOMEM) { 59*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 60*7c478bd9Sstevel@tonic-gate return (NULL); 61*7c478bd9Sstevel@tonic-gate } 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate if ((reqlist = (nvlist_t **)malloc(set->cs_nreqs * sizeof (*reqlist))) 64*7c478bd9Sstevel@tonic-gate == NULL) { 65*7c478bd9Sstevel@tonic-gate nvlist_free(setlist); 66*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 67*7c478bd9Sstevel@tonic-gate return (NULL); 68*7c478bd9Sstevel@tonic-gate } 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate bzero((void *)reqlist, set->cs_nreqs * sizeof (*reqlist)); 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate i = 0; 73*7c478bd9Sstevel@tonic-gate for (req = set->cs_request; req != NULL; req = req->cr_next) { 74*7c478bd9Sstevel@tonic-gate if (nvlist_alloc(&reqlist[i], 0, 0) == ENOMEM) 75*7c478bd9Sstevel@tonic-gate goto nomem; 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate if (nvlist_add_string(reqlist[i], "cr_event", 78*7c478bd9Sstevel@tonic-gate req->cr_event) != 0) 79*7c478bd9Sstevel@tonic-gate goto nomem; 80*7c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(reqlist[i], "cr_preset", 81*7c478bd9Sstevel@tonic-gate req->cr_preset) != 0) 82*7c478bd9Sstevel@tonic-gate goto nomem; 83*7c478bd9Sstevel@tonic-gate if (nvlist_add_uint32(reqlist[i], "cr_flags", 84*7c478bd9Sstevel@tonic-gate req->cr_flags) != 0) 85*7c478bd9Sstevel@tonic-gate goto nomem; 86*7c478bd9Sstevel@tonic-gate if (nvlist_add_uint32(reqlist[i], "cr_index", 87*7c478bd9Sstevel@tonic-gate req->cr_index) != 0) 88*7c478bd9Sstevel@tonic-gate goto nomem; 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate if (req->cr_nattrs != 0) { 91*7c478bd9Sstevel@tonic-gate nvlist_t *attrs; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate if (nvlist_alloc(&attrs, NV_UNIQUE_NAME, 0) == ENOMEM) 94*7c478bd9Sstevel@tonic-gate goto nomem; 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate for (j = 0; j < req->cr_nattrs; j++) { 97*7c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(attrs, 98*7c478bd9Sstevel@tonic-gate req->cr_attr[j].ka_name, 99*7c478bd9Sstevel@tonic-gate req->cr_attr[j].ka_val) != 0) { 100*7c478bd9Sstevel@tonic-gate nvlist_free(attrs); 101*7c478bd9Sstevel@tonic-gate goto nomem; 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate if (nvlist_add_nvlist(reqlist[i], "cr_attr", 106*7c478bd9Sstevel@tonic-gate attrs) != 0) { 107*7c478bd9Sstevel@tonic-gate nvlist_free(attrs); 108*7c478bd9Sstevel@tonic-gate goto nomem; 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate nvlist_free(attrs); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate i++; 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if (nvlist_add_nvlist_array(setlist, "reqs", reqlist, 117*7c478bd9Sstevel@tonic-gate set->cs_nreqs) != 0) 118*7c478bd9Sstevel@tonic-gate goto nomem; 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate if (nvlist_add_uint32(setlist, "flags", flags) != 0) 121*7c478bd9Sstevel@tonic-gate goto nomem; 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate if (nvlist_pack(setlist, &buf, &packsize, NV_ENCODE_NATIVE, 124*7c478bd9Sstevel@tonic-gate 0) != 0) 125*7c478bd9Sstevel@tonic-gate goto nomem; 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate for (i = 0; i < set->cs_nreqs; i++) 128*7c478bd9Sstevel@tonic-gate nvlist_free(reqlist[i]); 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate nvlist_free(setlist); 131*7c478bd9Sstevel@tonic-gate free(reqlist); 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate *buflen = packsize; 134*7c478bd9Sstevel@tonic-gate return (buf); 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate nomem: 137*7c478bd9Sstevel@tonic-gate for (i = 0; i < set->cs_nreqs; i++) { 138*7c478bd9Sstevel@tonic-gate if (reqlist[i] != 0) 139*7c478bd9Sstevel@tonic-gate nvlist_free(reqlist[i]); 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate nvlist_free(setlist); 142*7c478bd9Sstevel@tonic-gate free(reqlist); 143*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 144*7c478bd9Sstevel@tonic-gate return (NULL); 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate cpc_strhash_t * 148*7c478bd9Sstevel@tonic-gate __cpc_strhash_alloc(void) 149*7c478bd9Sstevel@tonic-gate { 150*7c478bd9Sstevel@tonic-gate cpc_strhash_t *p; 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate if ((p = malloc(sizeof (cpc_strhash_t))) == NULL) 153*7c478bd9Sstevel@tonic-gate return (NULL); 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate p->str = ""; 156*7c478bd9Sstevel@tonic-gate p->cur = NULL; 157*7c478bd9Sstevel@tonic-gate p->next = NULL; 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate return (p); 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate void 163*7c478bd9Sstevel@tonic-gate __cpc_strhash_free(cpc_strhash_t *hash) 164*7c478bd9Sstevel@tonic-gate { 165*7c478bd9Sstevel@tonic-gate cpc_strhash_t *p = hash, *f; 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate while (p != NULL) { 168*7c478bd9Sstevel@tonic-gate f = p; 169*7c478bd9Sstevel@tonic-gate p = p->next; 170*7c478bd9Sstevel@tonic-gate free(f); 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate /* 175*7c478bd9Sstevel@tonic-gate * Insert a new key into the hash table. 176*7c478bd9Sstevel@tonic-gate * 177*7c478bd9Sstevel@tonic-gate * Returns 0 if key was unique and insert successful. 178*7c478bd9Sstevel@tonic-gate * 179*7c478bd9Sstevel@tonic-gate * Returns 1 if key was already in table and no insert took place. 180*7c478bd9Sstevel@tonic-gate * 181*7c478bd9Sstevel@tonic-gate * Returns -1 if out of memory. 182*7c478bd9Sstevel@tonic-gate */ 183*7c478bd9Sstevel@tonic-gate int 184*7c478bd9Sstevel@tonic-gate __cpc_strhash_add(cpc_strhash_t *hash, char *key) 185*7c478bd9Sstevel@tonic-gate { 186*7c478bd9Sstevel@tonic-gate cpc_strhash_t *p, *tmp; 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate for (p = hash; p != NULL; p = p->next) { 189*7c478bd9Sstevel@tonic-gate if (strcmp(p->str, key) == 0) 190*7c478bd9Sstevel@tonic-gate return (1); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate if ((p = malloc(sizeof (*p))) == NULL) 194*7c478bd9Sstevel@tonic-gate return (-1); 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate p->str = key; 197*7c478bd9Sstevel@tonic-gate tmp = hash->next; 198*7c478bd9Sstevel@tonic-gate hash->next = p; 199*7c478bd9Sstevel@tonic-gate p->next = tmp; 200*7c478bd9Sstevel@tonic-gate /* 201*7c478bd9Sstevel@tonic-gate * The head node's current pointer must stay pointed at the first 202*7c478bd9Sstevel@tonic-gate * real node. We just inserted at the head. 203*7c478bd9Sstevel@tonic-gate */ 204*7c478bd9Sstevel@tonic-gate hash->cur = p; 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate return (0); 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate char * 210*7c478bd9Sstevel@tonic-gate __cpc_strhash_next(cpc_strhash_t *hash) 211*7c478bd9Sstevel@tonic-gate { 212*7c478bd9Sstevel@tonic-gate cpc_strhash_t *p; 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate if (hash->cur != NULL) { 215*7c478bd9Sstevel@tonic-gate p = hash->cur; 216*7c478bd9Sstevel@tonic-gate hash->cur = hash->cur->next; 217*7c478bd9Sstevel@tonic-gate return (p->str); 218*7c478bd9Sstevel@tonic-gate } 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate return (NULL); 221*7c478bd9Sstevel@tonic-gate } 222