Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/lib/libnvpair/libnvpair.c
48375 views
1
// SPDX-License-Identifier: CDDL-1.0
2
/*
3
* CDDL HEADER START
4
*
5
* The contents of this file are subject to the terms of the
6
* Common Development and Distribution License (the "License").
7
* You may not use this file except in compliance with the License.
8
*
9
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10
* or https://opensource.org/licenses/CDDL-1.0.
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 (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24
* Copyright (c) 2012 by Delphix. All rights reserved.
25
*/
26
27
#include <unistd.h>
28
#include <string.h>
29
#include <libintl.h>
30
#include <sys/types.h>
31
#include <sys/inttypes.h>
32
#include <stdarg.h>
33
#include "libnvpair.h"
34
35
/*
36
* libnvpair - A tools library for manipulating <name, value> pairs.
37
*
38
* This library provides routines packing an unpacking nv pairs
39
* for transporting data across process boundaries, transporting
40
* between kernel and userland, and possibly saving onto disk files.
41
*/
42
43
/*
44
* Print control structure.
45
*/
46
47
#define DEFINEOP(opname, vtype) \
48
struct { \
49
int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
50
const char *, vtype); \
51
void *arg; \
52
} opname
53
54
#define DEFINEARROP(opname, vtype) \
55
struct { \
56
int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
57
const char *, vtype, uint_t); \
58
void *arg; \
59
} opname
60
61
struct nvlist_printops {
62
DEFINEOP(print_boolean, int);
63
DEFINEOP(print_boolean_value, boolean_t);
64
DEFINEOP(print_byte, uchar_t);
65
DEFINEOP(print_int8, int8_t);
66
DEFINEOP(print_uint8, uint8_t);
67
DEFINEOP(print_int16, int16_t);
68
DEFINEOP(print_uint16, uint16_t);
69
DEFINEOP(print_int32, int32_t);
70
DEFINEOP(print_uint32, uint32_t);
71
DEFINEOP(print_int64, int64_t);
72
DEFINEOP(print_uint64, uint64_t);
73
DEFINEOP(print_double, double);
74
DEFINEOP(print_string, const char *);
75
DEFINEOP(print_hrtime, hrtime_t);
76
DEFINEOP(print_nvlist, nvlist_t *);
77
DEFINEARROP(print_boolean_array, boolean_t *);
78
DEFINEARROP(print_byte_array, uchar_t *);
79
DEFINEARROP(print_int8_array, int8_t *);
80
DEFINEARROP(print_uint8_array, uint8_t *);
81
DEFINEARROP(print_int16_array, int16_t *);
82
DEFINEARROP(print_uint16_array, uint16_t *);
83
DEFINEARROP(print_int32_array, int32_t *);
84
DEFINEARROP(print_uint32_array, uint32_t *);
85
DEFINEARROP(print_int64_array, int64_t *);
86
DEFINEARROP(print_uint64_array, uint64_t *);
87
DEFINEARROP(print_string_array, const char **);
88
DEFINEARROP(print_nvlist_array, nvlist_t **);
89
};
90
91
struct nvlist_prtctl {
92
FILE *nvprt_fp; /* output destination */
93
enum nvlist_indent_mode nvprt_indent_mode; /* see above */
94
int nvprt_indent; /* absolute indent, or tab depth */
95
int nvprt_indentinc; /* indent or tab increment */
96
const char *nvprt_nmfmt; /* member name format, max one %s */
97
const char *nvprt_eomfmt; /* after member format, e.g. "\n" */
98
const char *nvprt_btwnarrfmt; /* between array members */
99
int nvprt_btwnarrfmt_nl; /* nvprt_eoamfmt includes newline? */
100
struct nvlist_printops *nvprt_dfltops;
101
struct nvlist_printops *nvprt_custops;
102
};
103
104
#define DFLTPRTOP(pctl, type) \
105
((pctl)->nvprt_dfltops->print_##type.op)
106
107
#define DFLTPRTOPARG(pctl, type) \
108
((pctl)->nvprt_dfltops->print_##type.arg)
109
110
#define CUSTPRTOP(pctl, type) \
111
((pctl)->nvprt_custops->print_##type.op)
112
113
#define CUSTPRTOPARG(pctl, type) \
114
((pctl)->nvprt_custops->print_##type.arg)
115
116
#define RENDER(pctl, type, nvl, name, val) \
117
{ \
118
int done = 0; \
119
if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
120
done = CUSTPRTOP(pctl, type)(pctl, \
121
CUSTPRTOPARG(pctl, type), nvl, name, val); \
122
} \
123
if (!done) { \
124
(void) DFLTPRTOP(pctl, type)(pctl, \
125
DFLTPRTOPARG(pctl, type), nvl, name, val); \
126
} \
127
(void) fprintf(pctl->nvprt_fp, "%s", pctl->nvprt_eomfmt); \
128
}
129
130
#define ARENDER(pctl, type, nvl, name, arrp, count) \
131
{ \
132
int done = 0; \
133
if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
134
done = CUSTPRTOP(pctl, type)(pctl, \
135
CUSTPRTOPARG(pctl, type), nvl, name, arrp, count); \
136
} \
137
if (!done) { \
138
(void) DFLTPRTOP(pctl, type)(pctl, \
139
DFLTPRTOPARG(pctl, type), nvl, name, arrp, count); \
140
} \
141
(void) fprintf(pctl->nvprt_fp, "%s", pctl->nvprt_eomfmt); \
142
}
143
144
static void nvlist_print_with_indent(nvlist_t *, nvlist_prtctl_t);
145
146
/*
147
* ======================================================================
148
* | |
149
* | Indentation |
150
* | |
151
* ======================================================================
152
*/
153
154
static void
155
indent(nvlist_prtctl_t pctl, int onemore)
156
{
157
int depth;
158
159
switch (pctl->nvprt_indent_mode) {
160
case NVLIST_INDENT_ABS:
161
(void) fprintf(pctl->nvprt_fp, "%*s",
162
pctl->nvprt_indent + onemore * pctl->nvprt_indentinc, "");
163
break;
164
165
case NVLIST_INDENT_TABBED:
166
depth = pctl->nvprt_indent + onemore;
167
while (depth-- > 0)
168
(void) fprintf(pctl->nvprt_fp, "\t");
169
}
170
}
171
172
/*
173
* ======================================================================
174
* | |
175
* | Default nvlist member rendering functions. |
176
* | |
177
* ======================================================================
178
*/
179
180
/*
181
* Generate functions to print single-valued nvlist members.
182
*
183
* type_and_variant - suffix to form function name
184
* vtype - C type for the member value
185
* ptype - C type to cast value to for printing
186
* vfmt - format string for pair value, e.g "%d" or "0x%llx"
187
*/
188
189
#define NVLIST_PRTFUNC(type_and_variant, vtype, ptype, vfmt) \
190
static int \
191
nvprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
192
nvlist_t *nvl, const char *name, vtype value) \
193
{ \
194
(void) private; \
195
(void) nvl; \
196
FILE *fp = pctl->nvprt_fp; \
197
indent(pctl, 1); \
198
(void) fprintf(fp, pctl->nvprt_nmfmt, name); \
199
(void) fprintf(fp, vfmt, (ptype)value); \
200
return (1); \
201
}
202
203
/*
204
* Workaround for GCC 12+ with UBSan enabled deficencies.
205
*
206
* GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
207
* below as violating -Wformat-overflow.
208
*/
209
#if defined(__GNUC__) && !defined(__clang__) && \
210
defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
211
#pragma GCC diagnostic push
212
#pragma GCC diagnostic ignored "-Wformat-overflow"
213
#endif
214
NVLIST_PRTFUNC(boolean, int, int, "%d")
215
NVLIST_PRTFUNC(boolean_value, boolean_t, int, "%d")
216
NVLIST_PRTFUNC(byte, uchar_t, uchar_t, "0x%2.2x")
217
NVLIST_PRTFUNC(int8, int8_t, int, "%d")
218
NVLIST_PRTFUNC(uint8, uint8_t, uint8_t, "0x%x")
219
NVLIST_PRTFUNC(int16, int16_t, int16_t, "%d")
220
NVLIST_PRTFUNC(uint16, uint16_t, uint16_t, "0x%x")
221
NVLIST_PRTFUNC(int32, int32_t, int32_t, "%d")
222
NVLIST_PRTFUNC(uint32, uint32_t, uint32_t, "0x%x")
223
NVLIST_PRTFUNC(int64, int64_t, longlong_t, "%lld")
224
NVLIST_PRTFUNC(uint64, uint64_t, u_longlong_t, "0x%llx")
225
NVLIST_PRTFUNC(double, double, double, "0x%f")
226
NVLIST_PRTFUNC(string, const char *, const char *, "%s")
227
NVLIST_PRTFUNC(hrtime, hrtime_t, hrtime_t, "0x%llx")
228
#if defined(__GNUC__) && !defined(__clang__) && \
229
defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
230
#pragma GCC diagnostic pop
231
#endif
232
233
/*
234
* Generate functions to print array-valued nvlist members.
235
*/
236
237
#define NVLIST_ARRPRTFUNC(type_and_variant, vtype, ptype, vfmt) \
238
static int \
239
nvaprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
240
nvlist_t *nvl, const char *name, vtype *valuep, uint_t count) \
241
{ \
242
(void) private; \
243
(void) nvl; \
244
FILE *fp = pctl->nvprt_fp; \
245
uint_t i; \
246
for (i = 0; i < count; i++) { \
247
if (i == 0 || pctl->nvprt_btwnarrfmt_nl) { \
248
indent(pctl, 1); \
249
(void) fprintf(fp, pctl->nvprt_nmfmt, name); \
250
if (pctl->nvprt_btwnarrfmt_nl) \
251
(void) fprintf(fp, "[%d]: ", i); \
252
} \
253
if (i != 0) \
254
(void) fprintf(fp, "%s", pctl->nvprt_btwnarrfmt); \
255
(void) fprintf(fp, vfmt, (ptype)valuep[i]); \
256
} \
257
return (1); \
258
}
259
260
NVLIST_ARRPRTFUNC(boolean_array, boolean_t, boolean_t, "%d")
261
NVLIST_ARRPRTFUNC(byte_array, uchar_t, uchar_t, "0x%2.2x")
262
NVLIST_ARRPRTFUNC(int8_array, int8_t, int8_t, "%d")
263
NVLIST_ARRPRTFUNC(uint8_array, uint8_t, uint8_t, "0x%x")
264
NVLIST_ARRPRTFUNC(int16_array, int16_t, int16_t, "%d")
265
NVLIST_ARRPRTFUNC(uint16_array, uint16_t, uint16_t, "0x%x")
266
NVLIST_ARRPRTFUNC(int32_array, int32_t, int32_t, "%d")
267
NVLIST_ARRPRTFUNC(uint32_array, uint32_t, uint32_t, "0x%x")
268
NVLIST_ARRPRTFUNC(int64_array, int64_t, longlong_t, "%lld")
269
NVLIST_ARRPRTFUNC(uint64_array, uint64_t, u_longlong_t, "0x%llx")
270
NVLIST_ARRPRTFUNC(string_array, const char *, const char *, "%s")
271
272
static int
273
nvprint_nvlist(nvlist_prtctl_t pctl, void *private,
274
nvlist_t *nvl, const char *name, nvlist_t *value)
275
{
276
(void) private, (void) nvl;
277
FILE *fp = pctl->nvprt_fp;
278
279
indent(pctl, 1);
280
(void) fprintf(fp, "%s = (embedded nvlist)\n", name);
281
282
pctl->nvprt_indent += pctl->nvprt_indentinc;
283
nvlist_print_with_indent(value, pctl);
284
pctl->nvprt_indent -= pctl->nvprt_indentinc;
285
286
indent(pctl, 1);
287
(void) fprintf(fp, "(end %s)\n", name);
288
289
return (1);
290
}
291
292
static int
293
nvaprint_nvlist_array(nvlist_prtctl_t pctl, void *private,
294
nvlist_t *nvl, const char *name, nvlist_t **valuep, uint_t count)
295
{
296
(void) private, (void) nvl;
297
FILE *fp = pctl->nvprt_fp;
298
uint_t i;
299
300
indent(pctl, 1);
301
(void) fprintf(fp, "%s = (array of embedded nvlists)\n", name);
302
303
for (i = 0; i < count; i++) {
304
indent(pctl, 1);
305
(void) fprintf(fp, "(start %s[%d])\n", name, i);
306
307
pctl->nvprt_indent += pctl->nvprt_indentinc;
308
nvlist_print_with_indent(valuep[i], pctl);
309
pctl->nvprt_indent -= pctl->nvprt_indentinc;
310
311
indent(pctl, 1);
312
(void) fprintf(fp, "(end %s[%d])\n", name, i);
313
}
314
315
return (1);
316
}
317
318
/*
319
* ======================================================================
320
* | |
321
* | Interfaces that allow control over formatting. |
322
* | |
323
* ======================================================================
324
*/
325
326
void
327
nvlist_prtctl_setdest(nvlist_prtctl_t pctl, FILE *fp)
328
{
329
pctl->nvprt_fp = fp;
330
}
331
332
FILE *
333
nvlist_prtctl_getdest(nvlist_prtctl_t pctl)
334
{
335
return (pctl->nvprt_fp);
336
}
337
338
339
void
340
nvlist_prtctl_setindent(nvlist_prtctl_t pctl, enum nvlist_indent_mode mode,
341
int start, int inc)
342
{
343
if (mode < NVLIST_INDENT_ABS || mode > NVLIST_INDENT_TABBED)
344
mode = NVLIST_INDENT_TABBED;
345
346
if (start < 0)
347
start = 0;
348
349
if (inc < 0)
350
inc = 1;
351
352
pctl->nvprt_indent_mode = mode;
353
pctl->nvprt_indent = start;
354
pctl->nvprt_indentinc = inc;
355
}
356
357
void
358
nvlist_prtctl_doindent(nvlist_prtctl_t pctl, int onemore)
359
{
360
indent(pctl, onemore);
361
}
362
363
364
void
365
nvlist_prtctl_setfmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which,
366
const char *fmt)
367
{
368
switch (which) {
369
case NVLIST_FMT_MEMBER_NAME:
370
if (fmt == NULL)
371
fmt = "%s = ";
372
pctl->nvprt_nmfmt = fmt;
373
break;
374
375
case NVLIST_FMT_MEMBER_POSTAMBLE:
376
if (fmt == NULL)
377
fmt = "\n";
378
pctl->nvprt_eomfmt = fmt;
379
break;
380
381
case NVLIST_FMT_BTWN_ARRAY:
382
if (fmt == NULL) {
383
pctl->nvprt_btwnarrfmt = " ";
384
pctl->nvprt_btwnarrfmt_nl = 0;
385
} else {
386
pctl->nvprt_btwnarrfmt = fmt;
387
pctl->nvprt_btwnarrfmt_nl = (strchr(fmt, '\n') != NULL);
388
}
389
break;
390
391
default:
392
break;
393
}
394
}
395
396
397
void
398
nvlist_prtctl_dofmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which, ...)
399
{
400
FILE *fp = pctl->nvprt_fp;
401
va_list ap;
402
const char *name;
403
404
va_start(ap, which);
405
406
switch (which) {
407
case NVLIST_FMT_MEMBER_NAME:
408
name = va_arg(ap, const char *);
409
(void) fprintf(fp, pctl->nvprt_nmfmt, name);
410
break;
411
412
case NVLIST_FMT_MEMBER_POSTAMBLE:
413
(void) fprintf(fp, "%s", pctl->nvprt_eomfmt);
414
break;
415
416
case NVLIST_FMT_BTWN_ARRAY:
417
(void) fprintf(fp, "%s", pctl->nvprt_btwnarrfmt);
418
break;
419
420
default:
421
break;
422
}
423
424
va_end(ap);
425
}
426
427
/*
428
* ======================================================================
429
* | |
430
* | Interfaces to allow appointment of replacement rendering functions.|
431
* | |
432
* ======================================================================
433
*/
434
435
#define NVLIST_PRINTCTL_REPLACE(type, vtype) \
436
void \
437
nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
438
int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype), \
439
void *private) \
440
{ \
441
CUSTPRTOP(pctl, type) = func; \
442
CUSTPRTOPARG(pctl, type) = private; \
443
}
444
445
NVLIST_PRINTCTL_REPLACE(boolean, int)
446
NVLIST_PRINTCTL_REPLACE(boolean_value, boolean_t)
447
NVLIST_PRINTCTL_REPLACE(byte, uchar_t)
448
NVLIST_PRINTCTL_REPLACE(int8, int8_t)
449
NVLIST_PRINTCTL_REPLACE(uint8, uint8_t)
450
NVLIST_PRINTCTL_REPLACE(int16, int16_t)
451
NVLIST_PRINTCTL_REPLACE(uint16, uint16_t)
452
NVLIST_PRINTCTL_REPLACE(int32, int32_t)
453
NVLIST_PRINTCTL_REPLACE(uint32, uint32_t)
454
NVLIST_PRINTCTL_REPLACE(int64, int64_t)
455
NVLIST_PRINTCTL_REPLACE(uint64, uint64_t)
456
NVLIST_PRINTCTL_REPLACE(double, double)
457
NVLIST_PRINTCTL_REPLACE(string, const char *)
458
NVLIST_PRINTCTL_REPLACE(hrtime, hrtime_t)
459
NVLIST_PRINTCTL_REPLACE(nvlist, nvlist_t *)
460
461
#define NVLIST_PRINTCTL_AREPLACE(type, vtype) \
462
void \
463
nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
464
int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, \
465
uint_t), void *private) \
466
{ \
467
CUSTPRTOP(pctl, type) = func; \
468
CUSTPRTOPARG(pctl, type) = private; \
469
}
470
471
NVLIST_PRINTCTL_AREPLACE(boolean_array, boolean_t *)
472
NVLIST_PRINTCTL_AREPLACE(byte_array, uchar_t *)
473
NVLIST_PRINTCTL_AREPLACE(int8_array, int8_t *)
474
NVLIST_PRINTCTL_AREPLACE(uint8_array, uint8_t *)
475
NVLIST_PRINTCTL_AREPLACE(int16_array, int16_t *)
476
NVLIST_PRINTCTL_AREPLACE(uint16_array, uint16_t *)
477
NVLIST_PRINTCTL_AREPLACE(int32_array, int32_t *)
478
NVLIST_PRINTCTL_AREPLACE(uint32_array, uint32_t *)
479
NVLIST_PRINTCTL_AREPLACE(int64_array, int64_t *)
480
NVLIST_PRINTCTL_AREPLACE(uint64_array, uint64_t *)
481
NVLIST_PRINTCTL_AREPLACE(string_array, const char **)
482
NVLIST_PRINTCTL_AREPLACE(nvlist_array, nvlist_t **)
483
484
/*
485
* ======================================================================
486
* | |
487
* | Interfaces to manage nvlist_prtctl_t cookies. |
488
* | |
489
* ======================================================================
490
*/
491
492
493
static const struct nvlist_printops defprtops =
494
{
495
{ nvprint_boolean, NULL },
496
{ nvprint_boolean_value, NULL },
497
{ nvprint_byte, NULL },
498
{ nvprint_int8, NULL },
499
{ nvprint_uint8, NULL },
500
{ nvprint_int16, NULL },
501
{ nvprint_uint16, NULL },
502
{ nvprint_int32, NULL },
503
{ nvprint_uint32, NULL },
504
{ nvprint_int64, NULL },
505
{ nvprint_uint64, NULL },
506
{ nvprint_double, NULL },
507
{ nvprint_string, NULL },
508
{ nvprint_hrtime, NULL },
509
{ nvprint_nvlist, NULL },
510
{ nvaprint_boolean_array, NULL },
511
{ nvaprint_byte_array, NULL },
512
{ nvaprint_int8_array, NULL },
513
{ nvaprint_uint8_array, NULL },
514
{ nvaprint_int16_array, NULL },
515
{ nvaprint_uint16_array, NULL },
516
{ nvaprint_int32_array, NULL },
517
{ nvaprint_uint32_array, NULL },
518
{ nvaprint_int64_array, NULL },
519
{ nvaprint_uint64_array, NULL },
520
{ nvaprint_string_array, NULL },
521
{ nvaprint_nvlist_array, NULL },
522
};
523
524
static void
525
prtctl_defaults(FILE *fp, struct nvlist_prtctl *pctl,
526
struct nvlist_printops *ops)
527
{
528
pctl->nvprt_fp = fp;
529
pctl->nvprt_indent_mode = NVLIST_INDENT_TABBED;
530
pctl->nvprt_indent = 0;
531
pctl->nvprt_indentinc = 1;
532
pctl->nvprt_nmfmt = "%s = ";
533
pctl->nvprt_eomfmt = "\n";
534
pctl->nvprt_btwnarrfmt = " ";
535
pctl->nvprt_btwnarrfmt_nl = 0;
536
537
pctl->nvprt_dfltops = (struct nvlist_printops *)&defprtops;
538
pctl->nvprt_custops = ops;
539
}
540
541
nvlist_prtctl_t
542
nvlist_prtctl_alloc(void)
543
{
544
struct nvlist_prtctl *pctl;
545
struct nvlist_printops *ops;
546
547
if ((pctl = malloc(sizeof (*pctl))) == NULL)
548
return (NULL);
549
550
if ((ops = calloc(1, sizeof (*ops))) == NULL) {
551
free(pctl);
552
return (NULL);
553
}
554
555
prtctl_defaults(stdout, pctl, ops);
556
557
return (pctl);
558
}
559
560
void
561
nvlist_prtctl_free(nvlist_prtctl_t pctl)
562
{
563
if (pctl != NULL) {
564
free(pctl->nvprt_custops);
565
free(pctl);
566
}
567
}
568
569
/*
570
* ======================================================================
571
* | |
572
* | Top-level print request interfaces. |
573
* | |
574
* ======================================================================
575
*/
576
577
/*
578
* nvlist_print - Prints elements in an event buffer
579
*/
580
static void
581
nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl)
582
{
583
FILE *fp = pctl->nvprt_fp;
584
const char *name;
585
uint_t nelem;
586
nvpair_t *nvp;
587
588
if (nvl == NULL)
589
return;
590
591
indent(pctl, 0);
592
(void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl));
593
594
nvp = nvlist_next_nvpair(nvl, NULL);
595
596
while (nvp) {
597
data_type_t type = nvpair_type(nvp);
598
599
name = nvpair_name(nvp);
600
nelem = 0;
601
602
switch (type) {
603
case DATA_TYPE_BOOLEAN: {
604
RENDER(pctl, boolean, nvl, name, 1);
605
break;
606
}
607
case DATA_TYPE_BOOLEAN_VALUE: {
608
boolean_t val;
609
(void) nvpair_value_boolean_value(nvp, &val);
610
RENDER(pctl, boolean_value, nvl, name, val);
611
break;
612
}
613
case DATA_TYPE_BYTE: {
614
uchar_t val;
615
(void) nvpair_value_byte(nvp, &val);
616
RENDER(pctl, byte, nvl, name, val);
617
break;
618
}
619
case DATA_TYPE_INT8: {
620
int8_t val;
621
(void) nvpair_value_int8(nvp, &val);
622
RENDER(pctl, int8, nvl, name, val);
623
break;
624
}
625
case DATA_TYPE_UINT8: {
626
uint8_t val;
627
(void) nvpair_value_uint8(nvp, &val);
628
RENDER(pctl, uint8, nvl, name, val);
629
break;
630
}
631
case DATA_TYPE_INT16: {
632
int16_t val;
633
(void) nvpair_value_int16(nvp, &val);
634
RENDER(pctl, int16, nvl, name, val);
635
break;
636
}
637
case DATA_TYPE_UINT16: {
638
uint16_t val;
639
(void) nvpair_value_uint16(nvp, &val);
640
RENDER(pctl, uint16, nvl, name, val);
641
break;
642
}
643
case DATA_TYPE_INT32: {
644
int32_t val;
645
(void) nvpair_value_int32(nvp, &val);
646
RENDER(pctl, int32, nvl, name, val);
647
break;
648
}
649
case DATA_TYPE_UINT32: {
650
uint32_t val;
651
(void) nvpair_value_uint32(nvp, &val);
652
RENDER(pctl, uint32, nvl, name, val);
653
break;
654
}
655
case DATA_TYPE_INT64: {
656
int64_t val;
657
(void) nvpair_value_int64(nvp, &val);
658
RENDER(pctl, int64, nvl, name, val);
659
break;
660
}
661
case DATA_TYPE_UINT64: {
662
uint64_t val;
663
(void) nvpair_value_uint64(nvp, &val);
664
RENDER(pctl, uint64, nvl, name, val);
665
break;
666
}
667
case DATA_TYPE_DOUBLE: {
668
double val;
669
(void) nvpair_value_double(nvp, &val);
670
RENDER(pctl, double, nvl, name, val);
671
break;
672
}
673
case DATA_TYPE_STRING: {
674
const char *val;
675
(void) nvpair_value_string(nvp, &val);
676
RENDER(pctl, string, nvl, name, val);
677
break;
678
}
679
case DATA_TYPE_BOOLEAN_ARRAY: {
680
boolean_t *val;
681
(void) nvpair_value_boolean_array(nvp, &val, &nelem);
682
ARENDER(pctl, boolean_array, nvl, name, val, nelem);
683
break;
684
}
685
case DATA_TYPE_BYTE_ARRAY: {
686
uchar_t *val;
687
(void) nvpair_value_byte_array(nvp, &val, &nelem);
688
ARENDER(pctl, byte_array, nvl, name, val, nelem);
689
break;
690
}
691
case DATA_TYPE_INT8_ARRAY: {
692
int8_t *val;
693
(void) nvpair_value_int8_array(nvp, &val, &nelem);
694
ARENDER(pctl, int8_array, nvl, name, val, nelem);
695
break;
696
}
697
case DATA_TYPE_UINT8_ARRAY: {
698
uint8_t *val;
699
(void) nvpair_value_uint8_array(nvp, &val, &nelem);
700
ARENDER(pctl, uint8_array, nvl, name, val, nelem);
701
break;
702
}
703
case DATA_TYPE_INT16_ARRAY: {
704
int16_t *val;
705
(void) nvpair_value_int16_array(nvp, &val, &nelem);
706
ARENDER(pctl, int16_array, nvl, name, val, nelem);
707
break;
708
}
709
case DATA_TYPE_UINT16_ARRAY: {
710
uint16_t *val;
711
(void) nvpair_value_uint16_array(nvp, &val, &nelem);
712
ARENDER(pctl, uint16_array, nvl, name, val, nelem);
713
break;
714
}
715
case DATA_TYPE_INT32_ARRAY: {
716
int32_t *val;
717
(void) nvpair_value_int32_array(nvp, &val, &nelem);
718
ARENDER(pctl, int32_array, nvl, name, val, nelem);
719
break;
720
}
721
case DATA_TYPE_UINT32_ARRAY: {
722
uint32_t *val;
723
(void) nvpair_value_uint32_array(nvp, &val, &nelem);
724
ARENDER(pctl, uint32_array, nvl, name, val, nelem);
725
break;
726
}
727
case DATA_TYPE_INT64_ARRAY: {
728
int64_t *val;
729
(void) nvpair_value_int64_array(nvp, &val, &nelem);
730
ARENDER(pctl, int64_array, nvl, name, val, nelem);
731
break;
732
}
733
case DATA_TYPE_UINT64_ARRAY: {
734
uint64_t *val;
735
(void) nvpair_value_uint64_array(nvp, &val, &nelem);
736
ARENDER(pctl, uint64_array, nvl, name, val, nelem);
737
break;
738
}
739
case DATA_TYPE_STRING_ARRAY: {
740
const char **val;
741
(void) nvpair_value_string_array(nvp, &val, &nelem);
742
ARENDER(pctl, string_array, nvl, name, val, nelem);
743
break;
744
}
745
case DATA_TYPE_HRTIME: {
746
hrtime_t val;
747
(void) nvpair_value_hrtime(nvp, &val);
748
RENDER(pctl, hrtime, nvl, name, val);
749
break;
750
}
751
case DATA_TYPE_NVLIST: {
752
nvlist_t *val;
753
(void) nvpair_value_nvlist(nvp, &val);
754
RENDER(pctl, nvlist, nvl, name, val);
755
break;
756
}
757
case DATA_TYPE_NVLIST_ARRAY: {
758
nvlist_t **val;
759
(void) nvpair_value_nvlist_array(nvp, &val, &nelem);
760
ARENDER(pctl, nvlist_array, nvl, name, val, nelem);
761
break;
762
}
763
default:
764
(void) fprintf(fp, " unknown data type (%d)", type);
765
break;
766
}
767
nvp = nvlist_next_nvpair(nvl, nvp);
768
}
769
}
770
771
void
772
nvlist_print(FILE *fp, nvlist_t *nvl)
773
{
774
struct nvlist_prtctl pc;
775
776
prtctl_defaults(fp, &pc, NULL);
777
nvlist_print_with_indent(nvl, &pc);
778
}
779
780
void
781
nvlist_prt(nvlist_t *nvl, nvlist_prtctl_t pctl)
782
{
783
nvlist_print_with_indent(nvl, pctl);
784
}
785
786
/*
787
* ======================================================================
788
* | |
789
* | Misc private interface. |
790
* | |
791
* ======================================================================
792
*/
793
794
/*
795
* Determine if string 'value' matches 'nvp' value. The 'value' string is
796
* converted, depending on the type of 'nvp', prior to match. For numeric
797
* types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
798
* is an array type, 'ai' is the index into the array against which we are
799
* checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
800
* in a regex_t compilation of value in 'value_regex' to trigger regular
801
* expression string match instead of simple strcmp().
802
*
803
* Return 1 on match, 0 on no-match, and -1 on error. If the error is
804
* related to value syntax error and 'ep' is non-NULL, *ep will point into
805
* the 'value' string at the location where the error exists.
806
*
807
* NOTE: It may be possible to move the non-regex_t version of this into
808
* common code used by library/kernel/boot.
809
*/
810
int
811
nvpair_value_match_regex(nvpair_t *nvp, int ai,
812
const char *value, regex_t *value_regex, const char **ep)
813
{
814
const char *evalue;
815
uint_t a_len;
816
int sr;
817
818
if (ep)
819
*ep = NULL;
820
821
if ((nvp == NULL) || (value == NULL))
822
return (-1); /* error fail match - invalid args */
823
824
/* make sure array and index combination make sense */
825
if ((nvpair_type_is_array(nvp) && (ai < 0)) ||
826
(!nvpair_type_is_array(nvp) && (ai >= 0)))
827
return (-1); /* error fail match - bad index */
828
829
/* non-string values should be single 'chunk' */
830
if ((nvpair_type(nvp) != DATA_TYPE_STRING) &&
831
(nvpair_type(nvp) != DATA_TYPE_STRING_ARRAY)) {
832
value += strspn(value, " \t");
833
evalue = value + strcspn(value, " \t");
834
if (*evalue) {
835
if (ep)
836
*ep = evalue;
837
return (-1); /* error fail match - syntax */
838
}
839
}
840
841
sr = EOF;
842
switch (nvpair_type(nvp)) {
843
case DATA_TYPE_STRING: {
844
const char *val;
845
846
/* check string value for match */
847
if (nvpair_value_string(nvp, &val) == 0) {
848
if (value_regex) {
849
if (regexec(value_regex, val,
850
(size_t)0, NULL, 0) == 0)
851
return (1); /* match */
852
} else {
853
if (strcmp(value, val) == 0)
854
return (1); /* match */
855
}
856
}
857
break;
858
}
859
case DATA_TYPE_STRING_ARRAY: {
860
const char **val_array;
861
862
/* check indexed string value of array for match */
863
if ((nvpair_value_string_array(nvp, &val_array, &a_len) == 0) &&
864
(ai < a_len)) {
865
if (value_regex) {
866
if (regexec(value_regex, val_array[ai],
867
(size_t)0, NULL, 0) == 0)
868
return (1);
869
} else {
870
if (strcmp(value, val_array[ai]) == 0)
871
return (1);
872
}
873
}
874
break;
875
}
876
case DATA_TYPE_BYTE: {
877
uchar_t val, val_arg;
878
879
/* scanf uchar_t from value and check for match */
880
sr = sscanf(value, "%c", &val_arg);
881
if ((sr == 1) && (nvpair_value_byte(nvp, &val) == 0) &&
882
(val == val_arg))
883
return (1);
884
break;
885
}
886
case DATA_TYPE_BYTE_ARRAY: {
887
uchar_t *val_array, val_arg;
888
889
890
/* check indexed value of array for match */
891
sr = sscanf(value, "%c", &val_arg);
892
if ((sr == 1) &&
893
(nvpair_value_byte_array(nvp, &val_array, &a_len) == 0) &&
894
(ai < a_len) &&
895
(val_array[ai] == val_arg))
896
return (1);
897
break;
898
}
899
case DATA_TYPE_INT8: {
900
int8_t val, val_arg;
901
902
/* scanf int8_t from value and check for match */
903
sr = sscanf(value, "%"SCNi8, &val_arg);
904
if ((sr == 1) &&
905
(nvpair_value_int8(nvp, &val) == 0) &&
906
(val == val_arg))
907
return (1);
908
break;
909
}
910
case DATA_TYPE_INT8_ARRAY: {
911
int8_t *val_array, val_arg;
912
913
/* check indexed value of array for match */
914
sr = sscanf(value, "%"SCNi8, &val_arg);
915
if ((sr == 1) &&
916
(nvpair_value_int8_array(nvp, &val_array, &a_len) == 0) &&
917
(ai < a_len) &&
918
(val_array[ai] == val_arg))
919
return (1);
920
break;
921
}
922
case DATA_TYPE_UINT8: {
923
uint8_t val, val_arg;
924
925
/* scanf uint8_t from value and check for match */
926
sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
927
if ((sr == 1) &&
928
(nvpair_value_uint8(nvp, &val) == 0) &&
929
(val == val_arg))
930
return (1);
931
break;
932
}
933
case DATA_TYPE_UINT8_ARRAY: {
934
uint8_t *val_array, val_arg;
935
936
/* check indexed value of array for match */
937
sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
938
if ((sr == 1) &&
939
(nvpair_value_uint8_array(nvp, &val_array, &a_len) == 0) &&
940
(ai < a_len) &&
941
(val_array[ai] == val_arg))
942
return (1);
943
break;
944
}
945
case DATA_TYPE_INT16: {
946
int16_t val, val_arg;
947
948
/* scanf int16_t from value and check for match */
949
sr = sscanf(value, "%"SCNi16, &val_arg);
950
if ((sr == 1) &&
951
(nvpair_value_int16(nvp, &val) == 0) &&
952
(val == val_arg))
953
return (1);
954
break;
955
}
956
case DATA_TYPE_INT16_ARRAY: {
957
int16_t *val_array, val_arg;
958
959
/* check indexed value of array for match */
960
sr = sscanf(value, "%"SCNi16, &val_arg);
961
if ((sr == 1) &&
962
(nvpair_value_int16_array(nvp, &val_array, &a_len) == 0) &&
963
(ai < a_len) &&
964
(val_array[ai] == val_arg))
965
return (1);
966
break;
967
}
968
case DATA_TYPE_UINT16: {
969
uint16_t val, val_arg;
970
971
/* scanf uint16_t from value and check for match */
972
sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
973
if ((sr == 1) &&
974
(nvpair_value_uint16(nvp, &val) == 0) &&
975
(val == val_arg))
976
return (1);
977
break;
978
}
979
case DATA_TYPE_UINT16_ARRAY: {
980
uint16_t *val_array, val_arg;
981
982
/* check indexed value of array for match */
983
sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
984
if ((sr == 1) &&
985
(nvpair_value_uint16_array(nvp, &val_array, &a_len) == 0) &&
986
(ai < a_len) &&
987
(val_array[ai] == val_arg))
988
return (1);
989
break;
990
}
991
case DATA_TYPE_INT32: {
992
int32_t val, val_arg;
993
994
/* scanf int32_t from value and check for match */
995
sr = sscanf(value, "%"SCNi32, &val_arg);
996
if ((sr == 1) &&
997
(nvpair_value_int32(nvp, &val) == 0) &&
998
(val == val_arg))
999
return (1);
1000
break;
1001
}
1002
case DATA_TYPE_INT32_ARRAY: {
1003
int32_t *val_array, val_arg;
1004
1005
/* check indexed value of array for match */
1006
sr = sscanf(value, "%"SCNi32, &val_arg);
1007
if ((sr == 1) &&
1008
(nvpair_value_int32_array(nvp, &val_array, &a_len) == 0) &&
1009
(ai < a_len) &&
1010
(val_array[ai] == val_arg))
1011
return (1);
1012
break;
1013
}
1014
case DATA_TYPE_UINT32: {
1015
uint32_t val, val_arg;
1016
1017
/* scanf uint32_t from value and check for match */
1018
sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1019
if ((sr == 1) &&
1020
(nvpair_value_uint32(nvp, &val) == 0) &&
1021
(val == val_arg))
1022
return (1);
1023
break;
1024
}
1025
case DATA_TYPE_UINT32_ARRAY: {
1026
uint32_t *val_array, val_arg;
1027
1028
/* check indexed value of array for match */
1029
sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1030
if ((sr == 1) &&
1031
(nvpair_value_uint32_array(nvp, &val_array, &a_len) == 0) &&
1032
(ai < a_len) &&
1033
(val_array[ai] == val_arg))
1034
return (1);
1035
break;
1036
}
1037
case DATA_TYPE_INT64: {
1038
int64_t val, val_arg;
1039
1040
/* scanf int64_t from value and check for match */
1041
sr = sscanf(value, "%"SCNi64, &val_arg);
1042
if ((sr == 1) &&
1043
(nvpair_value_int64(nvp, &val) == 0) &&
1044
(val == val_arg))
1045
return (1);
1046
break;
1047
}
1048
case DATA_TYPE_INT64_ARRAY: {
1049
int64_t *val_array, val_arg;
1050
1051
/* check indexed value of array for match */
1052
sr = sscanf(value, "%"SCNi64, &val_arg);
1053
if ((sr == 1) &&
1054
(nvpair_value_int64_array(nvp, &val_array, &a_len) == 0) &&
1055
(ai < a_len) &&
1056
(val_array[ai] == val_arg))
1057
return (1);
1058
break;
1059
}
1060
case DATA_TYPE_UINT64: {
1061
uint64_t val_arg, val;
1062
1063
/* scanf uint64_t from value and check for match */
1064
sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1065
if ((sr == 1) &&
1066
(nvpair_value_uint64(nvp, &val) == 0) &&
1067
(val == val_arg))
1068
return (1);
1069
break;
1070
}
1071
case DATA_TYPE_UINT64_ARRAY: {
1072
uint64_t *val_array, val_arg;
1073
1074
/* check indexed value of array for match */
1075
sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1076
if ((sr == 1) &&
1077
(nvpair_value_uint64_array(nvp, &val_array, &a_len) == 0) &&
1078
(ai < a_len) &&
1079
(val_array[ai] == val_arg))
1080
return (1);
1081
break;
1082
}
1083
case DATA_TYPE_BOOLEAN_VALUE: {
1084
int32_t val_arg;
1085
boolean_t val;
1086
1087
/* scanf boolean_t from value and check for match */
1088
sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1089
if ((sr == 1) &&
1090
(nvpair_value_boolean_value(nvp, &val) == 0) &&
1091
(val == val_arg))
1092
return (1);
1093
break;
1094
}
1095
case DATA_TYPE_BOOLEAN_ARRAY: {
1096
boolean_t *val_array;
1097
int32_t val_arg;
1098
1099
/* check indexed value of array for match */
1100
sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1101
if ((sr == 1) &&
1102
(nvpair_value_boolean_array(nvp,
1103
&val_array, &a_len) == 0) &&
1104
(ai < a_len) &&
1105
(val_array[ai] == val_arg))
1106
return (1);
1107
break;
1108
}
1109
case DATA_TYPE_HRTIME:
1110
case DATA_TYPE_NVLIST:
1111
case DATA_TYPE_NVLIST_ARRAY:
1112
case DATA_TYPE_BOOLEAN:
1113
case DATA_TYPE_DOUBLE:
1114
case DATA_TYPE_UNKNOWN:
1115
default:
1116
/*
1117
* unknown/unsupported data type
1118
*/
1119
return (-1); /* error fail match */
1120
}
1121
1122
/*
1123
* check to see if sscanf failed conversion, return approximate
1124
* pointer to problem
1125
*/
1126
if (sr != 1) {
1127
if (ep)
1128
*ep = value;
1129
return (-1); /* error fail match - syntax */
1130
}
1131
1132
return (0); /* fail match */
1133
}
1134
1135
int
1136
nvpair_value_match(nvpair_t *nvp, int ai, const char *value, const char **ep)
1137
{
1138
return (nvpair_value_match_regex(nvp, ai, value, NULL, ep));
1139
}
1140
1141
/*
1142
* Similar to nvlist_print() but handles arrays slightly differently.
1143
*/
1144
void
1145
dump_nvlist(nvlist_t *list, int indent)
1146
{
1147
int len;
1148
char *buf;
1149
1150
len = nvlist_snprintf(NULL, 0, list, indent);
1151
len++; /* Add null terminator */
1152
1153
buf = malloc(len);
1154
if (buf == NULL)
1155
return;
1156
1157
(void) nvlist_snprintf(buf, len, list, indent);
1158
1159
/*
1160
* fputs does not have limitations on the size of the buffer being
1161
* printed (unlike printf).
1162
*/
1163
fputs(buf, stdout);
1164
1165
free(buf);
1166
}
1167
1168