summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fm/fmd/common/fmd_fmri.c
blob: 9db1432fc6fba8d5532a0c44d674848b515691ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>

#include <fmd_alloc.h>
#include <fmd_subr.h>
#include <fmd_error.h>
#include <fmd_string.h>
#include <fmd_scheme.h>
#include <fmd_fmri.h>
#include <fmd_topo.h>
#include <fmd.h>

/*
 * Interfaces to be used by the plugins
 */

void *
fmd_fmri_alloc(size_t size)
{
	return (fmd_alloc(size, FMD_SLEEP));
}

void *
fmd_fmri_zalloc(size_t size)
{
	return (fmd_zalloc(size, FMD_SLEEP));
}

void
fmd_fmri_free(void *data, size_t size)
{
	fmd_free(data, size);
}

int
fmd_fmri_set_errno(int err)
{
	errno = err;
	return (-1);
}

void
fmd_fmri_warn(const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	fmd_verror(EFMD_FMRI_SCHEME, format, ap);
	va_end(ap);
}

/*
 * Convert an input string to a URI escaped string and return the new string.
 * RFC2396 Section 2.4 says that data must be escaped if it does not have a
 * representation using an unreserved character, where an unreserved character
 * is one that is either alphanumeric or one of the marks defined in S2.3.
 */
static size_t
fmd_fmri_uriescape(const char *s, const char *xmark, char *buf, size_t len)
{
	static const char rfc2396_mark[] = "-_.!~*'()";
	static const char hex_digits[] = "0123456789ABCDEF";
	static const char empty_str[] = "";

	const char *p;
	char c, *q;
	size_t n = 0;

	if (s == NULL)
		s = empty_str;

	if (xmark == NULL)
		xmark = empty_str;

	for (p = s; (c = *p) != '\0'; p++) {
		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c))
			n++;	/* represent c as itself */
		else
			n += 3; /* represent c as escape */
	}

	if (buf == NULL)
		return (n);

	for (p = s, q = buf; (c = *p) != '\0' && q < buf + len; p++) {
		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c)) {
			*q++ = c;
		} else {
			*q++ = '%';
			*q++ = hex_digits[((uchar_t)c & 0xf0) >> 4];
			*q++ = hex_digits[(uchar_t)c & 0xf];
		}
	}

	if (q == buf + len)
		q--; /* len is too small: truncate output string */

	*q = '\0';
	return (n);
}

/*
 * Convert a name-value pair list representing an FMRI authority into the
 * corresponding RFC2396 string representation and return the new string.
 */
char *
fmd_fmri_auth2str(nvlist_t *nvl)
{
	nvpair_t *nvp;
	char *s, *p, *v;
	size_t n = 0;

	for (nvp = nvlist_next_nvpair(nvl, NULL);
	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {

		if (nvpair_type(nvp) != DATA_TYPE_STRING)
			continue; /* do not format non-string elements */

		n += fmd_fmri_uriescape(nvpair_name(nvp), NULL, NULL, 0) + 1;
		(void) nvpair_value_string(nvp, &v);
		n += fmd_fmri_uriescape(v, ":", NULL, 0) + 1;
	}

	p = s = fmd_alloc(n, FMD_SLEEP);

	for (nvp = nvlist_next_nvpair(nvl, NULL);
	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {

		if (nvpair_type(nvp) != DATA_TYPE_STRING)
			continue; /* do not format non-string elements */

		if (p != s)
			*p++ = ',';

		p += fmd_fmri_uriescape(nvpair_name(nvp), NULL, p, n);
		*p++ = '=';
		(void) nvpair_value_string(nvp, &v);
		p += fmd_fmri_uriescape(v, ":", p, n);
	}

	return (s);
}

/*
 * Convert an input string to a URI escaped string and return the new string.
 * We amend the unreserved character list to include commas and colons,
 * as both are needed to make FMRIs readable without escaping.  We also permit
 * "/" to pass through unescaped as any path delimiters used by the event
 * creator are presumably intended to appear in the final path.
 */
char *
fmd_fmri_strescape(const char *s)
{
	char *s2;
	size_t n;

	if (s == NULL)
		return (NULL);

	n = fmd_fmri_uriescape(s, ":,/", NULL, 0);
	s2 = fmd_alloc(n + 1, FMD_SLEEP);
	(void) fmd_fmri_uriescape(s, ":,/", s2, n + 1);

	return (s2);
}

char *
fmd_fmri_strdup(const char *s)
{
	return (fmd_strdup(s, FMD_SLEEP));
}

void
fmd_fmri_strfree(char *s)
{
	fmd_strfree(s);
}

const char *
fmd_fmri_get_rootdir(void)
{
	return (fmd.d_rootdir);
}

const char *
fmd_fmri_get_platform(void)
{
	return (fmd.d_platform);
}

uint64_t
fmd_fmri_get_drgen(void)
{
	uint64_t gen;

	(void) pthread_mutex_lock(&fmd.d_stats_lock);
	gen = fmd.d_stats->ds_dr_gen.fmds_value.ui64;
	(void) pthread_mutex_unlock(&fmd.d_stats_lock);

	return (gen);
}

struct topo_hdl *
fmd_fmri_topo_hold(int version)
{
	fmd_topo_t *ftp;

	if (version != TOPO_VERSION)
		return (NULL);

	ftp = fmd_topo_hold();

	return (ftp->ft_hdl);
}

void
fmd_fmri_topo_rele(struct topo_hdl *thp)
{
	fmd_topo_rele_hdl(thp);
}

/*
 * Interfaces for users of the plugins
 */

static fmd_scheme_t *
nvl2scheme(nvlist_t *nvl)
{
	char *name;

	if (nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &name) != 0) {
		(void) fmd_set_errno(EFMD_FMRI_INVAL);
		return (NULL);
	}

	return (fmd_scheme_hash_lookup(fmd.d_schemes, name));
}

ssize_t
fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
{
	fmd_scheme_t *sp;
	char c;
	ssize_t rv;

	if (buf == NULL && buflen == 0) {
		buf = &c;
		buflen = sizeof (c);
	}

	if ((sp = nvl2scheme(nvl)) == NULL)
		return (-1); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	ASSERT(buf != NULL || buflen == 0);
	rv = sp->sch_ops.sop_nvl2str(nvl, buf, buflen);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (rv);
}

int
fmd_fmri_expand(nvlist_t *nvl)
{
	fmd_scheme_t *sp;
	int rv;

	if ((sp = nvl2scheme(nvl)) == NULL)
		return (-1); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	rv = sp->sch_ops.sop_expand(nvl);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (rv);
}

int
fmd_fmri_present(nvlist_t *nvl)
{
	fmd_scheme_t *sp;
	int rv;

	if ((sp = nvl2scheme(nvl)) == NULL)
		return (-1); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	rv = sp->sch_ops.sop_present(nvl);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (rv);
}

int
fmd_fmri_replaced(nvlist_t *nvl)
{
	fmd_scheme_t *sp;
	int rv;

	if ((sp = nvl2scheme(nvl)) == NULL)
		return (-1); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	rv = sp->sch_ops.sop_replaced(nvl);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (rv);
}

int
fmd_fmri_service_state(nvlist_t *nvl)
{
	fmd_scheme_t *sp;
	int rv;

	if ((sp = nvl2scheme(nvl)) == NULL)
		return (-1); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	rv = sp->sch_ops.sop_service_state(nvl);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (rv);
}

int
fmd_fmri_unusable(nvlist_t *nvl)
{
	fmd_scheme_t *sp;
	int rv;

	if ((sp = nvl2scheme(nvl)) == NULL)
		return (-1); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	rv = sp->sch_ops.sop_unusable(nvl);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (rv);
}

/*
 * Someday we'll retire the scheme plugins.  For the
 * retire/unretire operations, the topo interfaces
 * are called directly.
 */
int
fmd_fmri_retire(nvlist_t *nvl)
{
	topo_hdl_t *thp;
	int rv, err;

	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
		return (-1);

	rv = topo_fmri_retire(thp, nvl, &err);
	fmd_fmri_topo_rele(thp);

	return (rv);
}

int
fmd_fmri_unretire(nvlist_t *nvl)
{
	topo_hdl_t *thp;
	int rv, err;

	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
		return (-1);

	rv = topo_fmri_unretire(thp, nvl, &err);
	fmd_fmri_topo_rele(thp);

	return (rv);
}

int
fmd_fmri_contains(nvlist_t *er, nvlist_t *ee)
{
	fmd_scheme_t *sp;
	char *ername, *eename;
	int rv;

	if (nvlist_lookup_string(er, FM_FMRI_SCHEME, &ername) != 0 ||
	    nvlist_lookup_string(ee, FM_FMRI_SCHEME, &eename) != 0 ||
	    strcmp(ername, eename) != 0)
		return (fmd_set_errno(EFMD_FMRI_INVAL));

	if ((sp = fmd_scheme_hash_lookup(fmd.d_schemes, ername)) == NULL)
		return (-1); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	rv = sp->sch_ops.sop_contains(er, ee);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (rv);
}

nvlist_t *
fmd_fmri_translate(nvlist_t *fmri, nvlist_t *auth)
{
	fmd_scheme_t *sp;
	nvlist_t *nvl;

	if ((sp = nvl2scheme(fmri)) == NULL)
		return (NULL); /* errno is set for us */

	(void) pthread_mutex_lock(&sp->sch_opslock);
	nvl = sp->sch_ops.sop_translate(fmri, auth);
	(void) pthread_mutex_unlock(&sp->sch_opslock);

	fmd_scheme_hash_release(fmd.d_schemes, sp);
	return (nvl);
}