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