xref: /illumos-gate/usr/src/lib/libcpc/common/subr.c (revision 1da57d55)
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 #include <sys/types.h>
28*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <errno.h>
34*7c478bd9Sstevel@tonic-gate #include <libintl.h>
35*7c478bd9Sstevel@tonic-gate #include <libnvpair.h>
36*7c478bd9Sstevel@tonic-gate #include <thread.h>
37*7c478bd9Sstevel@tonic-gate #include <synch.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "libcpc.h"
40*7c478bd9Sstevel@tonic-gate #include "libcpc_impl.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Pack a request set into a buffer using libnvpair. Size of buffer is returned
44*7c478bd9Sstevel@tonic-gate  * in buflen.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate char *
__cpc_pack_set(cpc_set_t * set,uint_t flags,size_t * buflen)47*7c478bd9Sstevel@tonic-gate __cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	cpc_request_t	*req;
50*7c478bd9Sstevel@tonic-gate 	nvlist_t	*setlist, **reqlist;
51*7c478bd9Sstevel@tonic-gate 	size_t		packsize = 0;
52*7c478bd9Sstevel@tonic-gate 	char		*buf = NULL;
53*7c478bd9Sstevel@tonic-gate 	int		i;
54*7c478bd9Sstevel@tonic-gate 	int		j;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&setlist, 0, 0) == ENOMEM) {
57*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
58*7c478bd9Sstevel@tonic-gate 		return (NULL);
59*7c478bd9Sstevel@tonic-gate 	}
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	if ((reqlist = (nvlist_t **)malloc(set->cs_nreqs * sizeof (*reqlist)))
62*7c478bd9Sstevel@tonic-gate 	    == NULL) {
63*7c478bd9Sstevel@tonic-gate 		nvlist_free(setlist);
64*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
65*7c478bd9Sstevel@tonic-gate 		return (NULL);
66*7c478bd9Sstevel@tonic-gate 	}
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	bzero((void *)reqlist, set->cs_nreqs * sizeof (*reqlist));
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	i = 0;
71*7c478bd9Sstevel@tonic-gate 	for (req = set->cs_request; req != NULL; req = req->cr_next) {
72*7c478bd9Sstevel@tonic-gate 		if (nvlist_alloc(&reqlist[i], 0, 0) == ENOMEM)
73*7c478bd9Sstevel@tonic-gate 			goto nomem;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_string(reqlist[i], "cr_event",
76*7c478bd9Sstevel@tonic-gate 		    req->cr_event) != 0)
77*7c478bd9Sstevel@tonic-gate 			goto nomem;
78*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint64(reqlist[i], "cr_preset",
79*7c478bd9Sstevel@tonic-gate 		    req->cr_preset) != 0)
80*7c478bd9Sstevel@tonic-gate 			goto nomem;
81*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint32(reqlist[i], "cr_flags",
82*7c478bd9Sstevel@tonic-gate 		    req->cr_flags) != 0)
83*7c478bd9Sstevel@tonic-gate 			goto nomem;
84*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint32(reqlist[i], "cr_index",
85*7c478bd9Sstevel@tonic-gate 		    req->cr_index) != 0)
86*7c478bd9Sstevel@tonic-gate 			goto nomem;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 		if (req->cr_nattrs != 0) {
89*7c478bd9Sstevel@tonic-gate 			nvlist_t	*attrs;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&attrs, NV_UNIQUE_NAME, 0) == ENOMEM)
92*7c478bd9Sstevel@tonic-gate 				goto nomem;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < req->cr_nattrs; j++) {
95*7c478bd9Sstevel@tonic-gate 				if (nvlist_add_uint64(attrs,
96*7c478bd9Sstevel@tonic-gate 				    req->cr_attr[j].ka_name,
97*7c478bd9Sstevel@tonic-gate 				    req->cr_attr[j].ka_val) != 0) {
98*7c478bd9Sstevel@tonic-gate 					nvlist_free(attrs);
99*7c478bd9Sstevel@tonic-gate 					goto nomem;
100*7c478bd9Sstevel@tonic-gate 				}
101*7c478bd9Sstevel@tonic-gate 			}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 			if (nvlist_add_nvlist(reqlist[i], "cr_attr",
104*7c478bd9Sstevel@tonic-gate 			    attrs) != 0) {
105*7c478bd9Sstevel@tonic-gate 				nvlist_free(attrs);
106*7c478bd9Sstevel@tonic-gate 				goto nomem;
107*7c478bd9Sstevel@tonic-gate 			}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 			nvlist_free(attrs);
110*7c478bd9Sstevel@tonic-gate 		}
111*7c478bd9Sstevel@tonic-gate 		i++;
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (nvlist_add_nvlist_array(setlist, "reqs", reqlist,
115*7c478bd9Sstevel@tonic-gate 	    set->cs_nreqs) != 0)
116*7c478bd9Sstevel@tonic-gate 		goto nomem;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(setlist, "flags", flags) != 0)
119*7c478bd9Sstevel@tonic-gate 		goto nomem;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (nvlist_pack(setlist, &buf, &packsize, NV_ENCODE_NATIVE,
122*7c478bd9Sstevel@tonic-gate 	    0) != 0)
123*7c478bd9Sstevel@tonic-gate 		goto nomem;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < set->cs_nreqs; i++)
126*7c478bd9Sstevel@tonic-gate 		nvlist_free(reqlist[i]);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	nvlist_free(setlist);
129*7c478bd9Sstevel@tonic-gate 	free(reqlist);
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	*buflen = packsize;
132*7c478bd9Sstevel@tonic-gate 	return (buf);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate nomem:
135*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < set->cs_nreqs; i++) {
136*7c478bd9Sstevel@tonic-gate 		if (reqlist[i] != 0)
137*7c478bd9Sstevel@tonic-gate 			nvlist_free(reqlist[i]);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	nvlist_free(setlist);
140*7c478bd9Sstevel@tonic-gate 	free(reqlist);
141*7c478bd9Sstevel@tonic-gate 	errno = ENOMEM;
142*7c478bd9Sstevel@tonic-gate 	return (NULL);
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate cpc_strhash_t *
__cpc_strhash_alloc(void)146*7c478bd9Sstevel@tonic-gate __cpc_strhash_alloc(void)
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	cpc_strhash_t *p;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (cpc_strhash_t))) == NULL)
151*7c478bd9Sstevel@tonic-gate 		return (NULL);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	p->str = "";
154*7c478bd9Sstevel@tonic-gate 	p->cur = NULL;
155*7c478bd9Sstevel@tonic-gate 	p->next = NULL;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	return (p);
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate void
__cpc_strhash_free(cpc_strhash_t * hash)161*7c478bd9Sstevel@tonic-gate __cpc_strhash_free(cpc_strhash_t *hash)
162*7c478bd9Sstevel@tonic-gate {
163*7c478bd9Sstevel@tonic-gate 	cpc_strhash_t *p = hash, *f;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	while (p != NULL) {
166*7c478bd9Sstevel@tonic-gate 		f = p;
167*7c478bd9Sstevel@tonic-gate 		p = p->next;
168*7c478bd9Sstevel@tonic-gate 		free(f);
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate /*
173*7c478bd9Sstevel@tonic-gate  * Insert a new key into the hash table.
174*7c478bd9Sstevel@tonic-gate  *
175*7c478bd9Sstevel@tonic-gate  * Returns 0 if key was unique and insert successful.
176*7c478bd9Sstevel@tonic-gate  *
177*7c478bd9Sstevel@tonic-gate  * Returns 1 if key was already in table and no insert took place.
178*7c478bd9Sstevel@tonic-gate  *
179*7c478bd9Sstevel@tonic-gate  * Returns -1 if out of memory.
180*7c478bd9Sstevel@tonic-gate  */
181*7c478bd9Sstevel@tonic-gate int
__cpc_strhash_add(cpc_strhash_t * hash,char * key)182*7c478bd9Sstevel@tonic-gate __cpc_strhash_add(cpc_strhash_t *hash, char *key)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 	cpc_strhash_t *p, *tmp;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	for (p = hash; p != NULL; p = p->next) {
187*7c478bd9Sstevel@tonic-gate 		if (strcmp(p->str, key) == 0)
188*7c478bd9Sstevel@tonic-gate 			return (1);
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (*p))) == NULL)
192*7c478bd9Sstevel@tonic-gate 		return (-1);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	p->str = key;
195*7c478bd9Sstevel@tonic-gate 	tmp = hash->next;
196*7c478bd9Sstevel@tonic-gate 	hash->next = p;
197*7c478bd9Sstevel@tonic-gate 	p->next = tmp;
198*7c478bd9Sstevel@tonic-gate 	/*
199*7c478bd9Sstevel@tonic-gate 	 * The head node's current pointer must stay pointed at the first
200*7c478bd9Sstevel@tonic-gate 	 * real node. We just inserted at the head.
201*7c478bd9Sstevel@tonic-gate 	 */
202*7c478bd9Sstevel@tonic-gate 	hash->cur = p;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	return (0);
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate char *
__cpc_strhash_next(cpc_strhash_t * hash)208*7c478bd9Sstevel@tonic-gate __cpc_strhash_next(cpc_strhash_t *hash)
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	cpc_strhash_t *p;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	if (hash->cur != NULL) {
213*7c478bd9Sstevel@tonic-gate 		p = hash->cur;
214*7c478bd9Sstevel@tonic-gate 		hash->cur = hash->cur->next;
215*7c478bd9Sstevel@tonic-gate 		return (p->str);
216*7c478bd9Sstevel@tonic-gate 	}
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	return (NULL);
219*7c478bd9Sstevel@tonic-gate }
220