xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_fmri.c (revision a237e38e9161f0acd6451439d4a7dd597e66291d)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 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 <ctype.h>
30 #include <errno.h>
31 #include <stdarg.h>
32 
33 #include <fmd_alloc.h>
34 #include <fmd_subr.h>
35 #include <fmd_error.h>
36 #include <fmd_string.h>
37 #include <fmd_scheme.h>
38 #include <fmd_fmri.h>
39 #include <fmd_topo.h>
40 #include <fmd.h>
41 
42 /*
43  * Interfaces to be used by the plugins
44  */
45 
46 void *
47 fmd_fmri_alloc(size_t size)
48 {
49 	return (fmd_alloc(size, FMD_SLEEP));
50 }
51 
52 void *
53 fmd_fmri_zalloc(size_t size)
54 {
55 	return (fmd_zalloc(size, FMD_SLEEP));
56 }
57 
58 void
59 fmd_fmri_free(void *data, size_t size)
60 {
61 	fmd_free(data, size);
62 }
63 
64 int
65 fmd_fmri_set_errno(int err)
66 {
67 	errno = err;
68 	return (-1);
69 }
70 
71 void
72 fmd_fmri_warn(const char *format, ...)
73 {
74 	va_list ap;
75 
76 	va_start(ap, format);
77 	fmd_verror(EFMD_FMRI_SCHEME, format, ap);
78 	va_end(ap);
79 }
80 
81 /*
82  * Convert an input string to a URI escaped string and return the new string.
83  * RFC2396 Section 2.4 says that data must be escaped if it does not have a
84  * representation using an unreserved character, where an unreserved character
85  * is one that is either alphanumberic or one of the marks defined in S2.3.
86  */
87 static size_t
88 fmd_fmri_uriescape(const char *s, const char *xmark, char *buf, size_t len)
89 {
90 	static const char rfc2396_mark[] = "-_.!~*'()";
91 	static const char hex_digits[] = "0123456789ABCDEF";
92 	static const char empty_str[] = "";
93 
94 	const char *p;
95 	char c, *q;
96 	size_t n = 0;
97 
98 	if (s == NULL)
99 		s = empty_str;
100 
101 	if (xmark == NULL)
102 		xmark = empty_str;
103 
104 	for (p = s; (c = *p) != '\0'; p++) {
105 		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c))
106 			n++;	/* represent c as itself */
107 		else
108 			n += 3; /* represent c as escape */
109 	}
110 
111 	if (buf == NULL)
112 		return (n);
113 
114 	for (p = s, q = buf; (c = *p) != '\0' && q < buf + len; p++) {
115 		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c)) {
116 			*q++ = c;
117 		} else {
118 			*q++ = '%';
119 			*q++ = hex_digits[((uchar_t)c & 0xf0) >> 4];
120 			*q++ = hex_digits[(uchar_t)c & 0xf];
121 		}
122 	}
123 
124 	if (q == buf + len)
125 		q--; /* len is too small: truncate output string */
126 
127 	*q = '\0';
128 	return (n);
129 }
130 
131 /*
132  * Convert a name-value pair list representing an FMRI authority into the
133  * corresponding RFC2396 string representation and return the new string.
134  */
135 char *
136 fmd_fmri_auth2str(nvlist_t *nvl)
137 {
138 	nvpair_t *nvp;
139 	char *s, *p, *v;
140 	size_t n = 0;
141 
142 	for (nvp = nvlist_next_nvpair(nvl, NULL);
143 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
144 
145 		if (nvpair_type(nvp) != DATA_TYPE_STRING)
146 			continue; /* do not format non-string elements */
147 
148 		n += fmd_fmri_uriescape(nvpair_name(nvp), NULL, NULL, 0) + 1;
149 		(void) nvpair_value_string(nvp, &v);
150 		n += fmd_fmri_uriescape(v, ":", NULL, 0) + 1;
151 	}
152 
153 	p = s = fmd_alloc(n, FMD_SLEEP);
154 
155 	for (nvp = nvlist_next_nvpair(nvl, NULL);
156 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
157 
158 		if (nvpair_type(nvp) != DATA_TYPE_STRING)
159 			continue; /* do not format non-string elements */
160 
161 		if (p != s)
162 			*p++ = ',';
163 
164 		p += fmd_fmri_uriescape(nvpair_name(nvp), NULL, p, n);
165 		*p++ = '=';
166 		(void) nvpair_value_string(nvp, &v);
167 		p += fmd_fmri_uriescape(v, ":", p, n);
168 	}
169 
170 	return (s);
171 }
172 
173 /*
174  * Convert an input string to a URI escaped string and return the new string.
175  * We amend the unreserved character list to include commas and colons,
176  * as both are needed to make FMRIs readable without escaping.  We also permit
177  * "/" to pass through unescaped as any path delimiters used by the event
178  * creator are presumably intended to appear in the final path.
179  */
180 char *
181 fmd_fmri_strescape(const char *s)
182 {
183 	char *s2;
184 	size_t n;
185 
186 	if (s == NULL)
187 		return (NULL);
188 
189 	n = fmd_fmri_uriescape(s, ":,/", NULL, 0);
190 	s2 = fmd_alloc(n + 1, FMD_SLEEP);
191 	(void) fmd_fmri_uriescape(s, ":,/", s2, n + 1);
192 
193 	return (s2);
194 }
195 
196 char *
197 fmd_fmri_strdup(const char *s)
198 {
199 	return (fmd_strdup(s, FMD_SLEEP));
200 }
201 
202 void
203 fmd_fmri_strfree(char *s)
204 {
205 	fmd_strfree(s);
206 }
207 
208 const char *
209 fmd_fmri_get_rootdir(void)
210 {
211 	return (fmd.d_rootdir);
212 }
213 
214 const char *
215 fmd_fmri_get_platform(void)
216 {
217 	return (fmd.d_platform);
218 }
219 
220 uint64_t
221 fmd_fmri_get_drgen(void)
222 {
223 	uint64_t gen;
224 
225 	(void) pthread_mutex_lock(&fmd.d_stats_lock);
226 	gen = fmd.d_stats->ds_dr_gen.fmds_value.ui64;
227 	(void) pthread_mutex_unlock(&fmd.d_stats_lock);
228 
229 	return (gen);
230 }
231 
232 struct topo_hdl *
233 fmd_fmri_topology(int version)
234 {
235 	return (fmd_topo_handle(version));
236 }
237 
238 /*
239  * Interfaces for users of the plugins
240  */
241 
242 static fmd_scheme_t *
243 nvl2scheme(nvlist_t *nvl)
244 {
245 	char *name;
246 
247 	if (nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &name) != 0) {
248 		(void) fmd_set_errno(EFMD_FMRI_INVAL);
249 		return (NULL);
250 	}
251 
252 	return (fmd_scheme_hash_lookup(fmd.d_schemes, name));
253 }
254 
255 ssize_t
256 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
257 {
258 	fmd_scheme_t *sp;
259 	char c;
260 	ssize_t rv;
261 
262 	if (buf == NULL && buflen == 0) {
263 		buf = &c;
264 		buflen = sizeof (c);
265 	}
266 
267 	if ((sp = nvl2scheme(nvl)) == NULL)
268 		return (-1); /* errno is set for us */
269 
270 	(void) pthread_mutex_lock(&sp->sch_opslock);
271 	ASSERT(buf != NULL || buflen == 0);
272 	rv = sp->sch_ops.sop_nvl2str(nvl, buf, buflen);
273 	(void) pthread_mutex_unlock(&sp->sch_opslock);
274 
275 	fmd_scheme_hash_release(fmd.d_schemes, sp);
276 	return (rv);
277 }
278 
279 int
280 fmd_fmri_expand(nvlist_t *nvl)
281 {
282 	fmd_scheme_t *sp;
283 	int rv;
284 
285 	if ((sp = nvl2scheme(nvl)) == NULL)
286 		return (-1); /* errno is set for us */
287 
288 	(void) pthread_mutex_lock(&sp->sch_opslock);
289 	rv = sp->sch_ops.sop_expand(nvl);
290 	(void) pthread_mutex_unlock(&sp->sch_opslock);
291 
292 	fmd_scheme_hash_release(fmd.d_schemes, sp);
293 	return (rv);
294 }
295 
296 int
297 fmd_fmri_present(nvlist_t *nvl)
298 {
299 	fmd_scheme_t *sp;
300 	int rv;
301 
302 	if ((sp = nvl2scheme(nvl)) == NULL)
303 		return (-1); /* errno is set for us */
304 
305 	(void) pthread_mutex_lock(&sp->sch_opslock);
306 	rv = sp->sch_ops.sop_present(nvl);
307 	(void) pthread_mutex_unlock(&sp->sch_opslock);
308 
309 	fmd_scheme_hash_release(fmd.d_schemes, sp);
310 	return (rv);
311 }
312 
313 int
314 fmd_fmri_unusable(nvlist_t *nvl)
315 {
316 	fmd_scheme_t *sp;
317 	int rv;
318 
319 	if ((sp = nvl2scheme(nvl)) == NULL)
320 		return (-1); /* errno is set for us */
321 
322 	(void) pthread_mutex_lock(&sp->sch_opslock);
323 	rv = sp->sch_ops.sop_unusable(nvl);
324 	(void) pthread_mutex_unlock(&sp->sch_opslock);
325 
326 	fmd_scheme_hash_release(fmd.d_schemes, sp);
327 	return (rv);
328 }
329 
330 int
331 fmd_fmri_contains(nvlist_t *er, nvlist_t *ee)
332 {
333 	fmd_scheme_t *sp;
334 	char *ername, *eename;
335 	int rv;
336 
337 	if (nvlist_lookup_string(er, FM_FMRI_SCHEME, &ername) != 0 ||
338 	    nvlist_lookup_string(ee, FM_FMRI_SCHEME, &eename) != 0 ||
339 	    strcmp(ername, eename) != 0)
340 		return (fmd_set_errno(EFMD_FMRI_INVAL));
341 
342 	if ((sp = fmd_scheme_hash_lookup(fmd.d_schemes, ername)) == NULL)
343 		return (-1); /* errno is set for us */
344 
345 	(void) pthread_mutex_lock(&sp->sch_opslock);
346 	rv = sp->sch_ops.sop_contains(er, ee);
347 	(void) pthread_mutex_unlock(&sp->sch_opslock);
348 
349 	fmd_scheme_hash_release(fmd.d_schemes, sp);
350 	return (rv);
351 }
352 
353 nvlist_t *
354 fmd_fmri_translate(nvlist_t *fmri, nvlist_t *auth)
355 {
356 	fmd_scheme_t *sp;
357 	nvlist_t *nvl;
358 
359 	if ((sp = nvl2scheme(fmri)) == NULL)
360 		return (NULL); /* errno is set for us */
361 
362 	(void) pthread_mutex_lock(&sp->sch_opslock);
363 	nvl = sp->sch_ops.sop_translate(fmri, auth);
364 	(void) pthread_mutex_unlock(&sp->sch_opslock);
365 
366 	fmd_scheme_hash_release(fmd.d_schemes, sp);
367 	return (nvl);
368 }
369