-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathnv.9
1099 lines (1081 loc) · 28 KB
/
nv.9
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.\"
.\" Copyright (c) 2013 The FreeBSD Foundation
.\" Copyright (c) 2013-2015 Mariusz Zaborski <[email protected]>
.\" All rights reserved.
.\"
.\" This documentation was written by Pawel Jakub Dawidek under sponsorship
.\" the FreeBSD Foundation.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd January 3, 2025
.Dt NV 9
.Os
.Sh NAME
.Nm nvlist_create ,
.Nm nvlist_destroy ,
.Nm nvlist_error ,
.Nm nvlist_set_error ,
.Nm nvlist_empty ,
.Nm nvlist_flags ,
.Nm nvlist_exists ,
.Nm nvlist_free ,
.Nm nvlist_clone ,
.Nm nvlist_dump ,
.Nm nvlist_fdump ,
.Nm nvlist_size ,
.Nm nvlist_pack ,
.Nm nvlist_unpack ,
.Nm nvlist_send ,
.Nm nvlist_recv ,
.Nm nvlist_xfer ,
.Nm nvlist_in_array ,
.Nm nvlist_next ,
.Nm nvlist_add ,
.Nm nvlist_move ,
.Nm nvlist_get ,
.Nm nvlist_take ,
.Nm nvlist_append
.Nd "library for name/value pairs"
.Sh LIBRARY
.Lb libnv
.Sh SYNOPSIS
.In sys/nv.h
.Ft "nvlist_t *"
.Fn nvlist_create "int flags"
.Ft void
.Fn nvlist_destroy "nvlist_t *nvl"
.Ft int
.Fn nvlist_error "const nvlist_t *nvl"
.Ft void
.Fn nvlist_set_error "nvlist_t *nvl" "int error"
.Ft bool
.Fn nvlist_empty "const nvlist_t *nvl"
.Ft int
.Fn nvlist_flags "const nvlist_t *nvl"
.Ft bool
.Fn nvlist_in_array "const nvlist_t *nvl"
.\"
.Ft "nvlist_t *"
.Fn nvlist_clone "const nvlist_t *nvl"
.\"
.Ft void
.Fn nvlist_dump "const nvlist_t *nvl" "int fd"
.Ft void
.Fn nvlist_fdump "const nvlist_t *nvl" "FILE *fp"
.\"
.Ft size_t
.Fn nvlist_size "const nvlist_t *nvl"
.Ft "void *"
.Fn nvlist_pack "const nvlist_t *nvl" "size_t *sizep"
.Ft "nvlist_t *"
.Fn nvlist_unpack "const void *buf" "size_t size" "int flags"
.\"
.Ft int
.Fn nvlist_send "int sock" "const nvlist_t *nvl"
.Ft "nvlist_t *"
.Fn nvlist_recv "int sock" "int flags"
.Ft "nvlist_t *"
.Fn nvlist_xfer "int sock" "nvlist_t *nvl" "int flags"
.\"
.Ft "const char *"
.Fn nvlist_next "const nvlist_t *nvl" "int *typep" "void **cookiep"
.\"
.Ft bool
.Fn nvlist_exists "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_type "const nvlist_t *nvl" "const char *name" "int type"
.Ft bool
.Fn nvlist_exists_null "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_bool "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_number "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_string "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_nvlist "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_descriptor "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_binary "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_bool_array "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_number_array "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_string_array "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_nvlist_array "const nvlist_t *nvl" "const char *name"
.Ft bool
.Fn nvlist_exists_descriptor_array "const nvlist_t *nvl" "const char *name"
.\"
.Ft void
.Fn nvlist_add_null "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_add_bool "nvlist_t *nvl" "const char *name" "bool value"
.Ft void
.Fn nvlist_add_number "nvlist_t *nvl" "const char *name" "uint64_t value"
.Ft void
.Fn nvlist_add_string "nvlist_t *nvl" "const char *name" "const char *value"
.Ft void
.Fn nvlist_add_stringf "nvlist_t *nvl" "const char *name" "const char *valuefmt" "..."
.Ft void
.Fn nvlist_add_stringv "nvlist_t *nvl" "const char *name" "const char *valuefmt" "va_list valueap"
.Ft void
.Fn nvlist_add_nvlist "nvlist_t *nvl" "const char *name" "const nvlist_t *value"
.Ft void
.Fn nvlist_add_descriptor "nvlist_t *nvl" "const char *name" "int value"
.Ft void
.Fn nvlist_add_binary "nvlist_t *nvl" "const char *name" "const void *value" "size_t size"
.Ft void
.Fn nvlist_add_bool_array "nvlist_t *nvl" "const char *name" "const bool *value" "size_t nitems"
.
.Ft void
.Fn nvlist_add_number_array "nvlist_t *nvl" "const char *name" "const uint64_t *value" "size_t nitems"
.
.Ft void
.Fn nvlist_add_string_array "nvlist_t *nvl" "const char *name" "const char * const * value" "size_t nitems"
.
.Ft void
.Fn nvlist_add_nvlist_array "nvlist_t *nvl" "const char *name" "const nvlist_t * const * value" "size_t nitems"
.
.Ft void
.Fn nvlist_add_descriptor_array "nvlist_t *nvl" "const char *name" "const int *value" "size_t nitems"
.\"
.Ft void
.Fn nvlist_move_string "nvlist_t *nvl" "const char *name" "char *value"
.Ft void
.Fn nvlist_move_nvlist "nvlist_t *nvl" "const char *name" "nvlist_t *value"
.Ft void
.Fn nvlist_move_descriptor "nvlist_t *nvl" "const char *name" "int value"
.Ft void
.Fn nvlist_move_binary "nvlist_t *nvl" "const char *name" "void *value" "size_t size"
.Ft void
.Fn nvlist_move_bool_array "nvlist_t *nvl" "const char *name" "bool *value" "size_t nitems"
.
.Ft void
.Fn nvlist_move_number_array "nvlist_t *nvl" "const char *name" "uint64_t *value" "size_t nitems"
.
.Ft void
.Fn nvlist_move_string_array "nvlist_t *nvl" "const char *name" "char **value" "size_t nitems"
.
.Ft void
.Fn nvlist_move_nvlist_array "nvlist_t *nvl" "const char *name" "nvlist_t **value" "size_t nitems"
.
.Ft void
.Fn nvlist_move_descriptor_array "nvlist_t *nvl" "const char *name" "int *value" "size_t nitems"
.\"
.Ft bool
.Fn nvlist_get_bool "const nvlist_t *nvl" "const char *name"
.Ft uint64_t
.Fn nvlist_get_number "const nvlist_t *nvl" "const char *name"
.Ft "const char *"
.Fn nvlist_get_string "const nvlist_t *nvl" "const char *name"
.Ft "const nvlist_t *"
.Fn nvlist_get_nvlist "const nvlist_t *nvl" "const char *name"
.Ft int
.Fn nvlist_get_descriptor "const nvlist_t *nvl" "const char *name"
.Ft "const void *"
.Fn nvlist_get_binary "const nvlist_t *nvl" "const char *name" "size_t *sizep"
.Ft "const bool *"
.Fn nvlist_get_bool_array "const nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "const uint64_t *"
.Fn nvlist_get_number_array "const nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "const char * const *"
.Fn nvlist_get_string_array "const nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "const nvlist_t * const *"
.Fn nvlist_get_nvlist_array "const nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "const int *"
.Fn nvlist_get_descriptor_array "const nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "const nvlist_t *"
.Fn nvlist_get_parent "const nvlist_t *nvl" "void **cookiep"
.Ft "const nvlist_t *"
.Fn nvlist_get_array_next "const nvlist_t *nvl"
.Ft "const nvlist_t *"
.Fn nvlist_get_pararr "const nvlist_t *nvl" "void **cookiep"
.\"
.Ft bool
.Fn nvlist_take_bool "nvlist_t *nvl" "const char *name"
.Ft uint64_t
.Fn nvlist_take_number "nvlist_t *nvl" "const char *name"
.Ft "char *"
.Fn nvlist_take_string "nvlist_t *nvl" "const char *name"
.Ft "nvlist_t *"
.Fn nvlist_take_nvlist "nvlist_t *nvl" "const char *name"
.Ft int
.Fn nvlist_take_descriptor "nvlist_t *nvl" "const char *name"
.Ft "void *"
.Fn nvlist_take_binary "nvlist_t *nvl" "const char *name" "size_t *sizep"
.Ft "bool *"
.Fn nvlist_take_bool_array "nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "uint64_t **"
.Fn nvlist_take_number_array "nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "char **"
.Fn nvlist_take_string_array "nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "nvlist_t **"
.Fn nvlist_take_nvlist_array "nvlist_t *nvl" "const char *name" "size_t *nitems"
.Ft "int *"
.Fn nvlist_take_descriptor_array "nvlist_t *nvl" "const char *name" "size_t *nitems"
.\"
.Ft void
.Fn nvlist_append_bool_array "nvlist_t *nvl" "const char *name" "const bool value"
.Ft void
.Fn nvlist_append_number_array "nvlist_t *nvl" "const char *name" "const uint64_t value"
.Ft void
.Fn nvlist_append_string_array "nvlist_t *nvl" "const char *name" "const char * const value"
.Ft void
.Fn nvlist_append_nvlist_array "nvlist_t *nvl" "const char *name" "const nvlist_t * const value"
.Ft void
.Fn nvlist_append_descriptor_array "nvlist_t *nvl" "const char *name" "int value"
.\"
.Ft void
.Fn nvlist_free "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_type "nvlist_t *nvl" "const char *name" "int type"
.\"
.Ft void
.Fn nvlist_free_null "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_bool "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_number "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_string "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_nvlist "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_descriptor "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_binary "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_bool_array "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_number_array "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_string_array "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_nvlist_array "nvlist_t *nvl" "const char *name"
.Ft void
.Fn nvlist_free_descriptor_array "nvlist_t *nvl" "const char *name"
.Sh DESCRIPTION
The
.Nm libnv
library permits creating and managing name value pairs as well as
sending and receiving
them over sockets.
A group (list) of name value pairs is called an
.Nm nvlist .
The API supports the following data types for values:
.Bl -ohang -offset indent
.It Sy null ( NV_TYPE_NULL )
There is no data associated with the name.
.It Sy bool ( NV_TYPE_BOOL )
The value can be either
.Dv true
or
.Dv false .
.It Sy number ( NV_TYPE_NUMBER )
The value is a number stored as
.Vt uint64_t .
.It Sy string ( NV_TYPE_STRING )
The value is a C string.
.It Sy nvlist ( NV_TYPE_NVLIST )
The value is a nested nvlist.
.It Sy descriptor ( NV_TYPE_DESCRIPTOR )
The value is a file descriptor.
Note that file descriptors can be sent only over
.Xr unix 4
domain sockets.
.It Sy binary ( NV_TYPE_BINARY )
The value is a binary buffer.
.It Sy bool array ( NV_TYPE_BOOL_ARRAY )
The value is an array of boolean values.
.It Sy number array ( NV_TYPE_NUMBER_ARRAY )
The value is an array of numbers, each stored as
.Vt uint64_t .
.It Sy string array ( NV_TYPE_STRING_ARRAY )
The value is an array of C strings.
.It Sy nvlist array ( NV_TYPE_NVLIST_ARRAY )
The value is an array of nvlists.
When an nvlist is added to an array, it becomes part of the primary nvlist.
Traversing these arrays can be done using the
.Fn nvlist_get_array_next
and
.Fn nvlist_get_pararr
functions.
.It Sy descriptor array ( NV_TYPE_DESCRIPTOR_ARRAY )
The value is an array of files descriptors.
.El
.Pp
The
.Fn nvlist_create
function allocates memory and initializes an nvlist.
.Pp
The following flags can be provided:
.Pp
.Bl -tag -width "NV_FLAG_IGNORE_CASE" -compact -offset indent
.It Dv NV_FLAG_IGNORE_CASE
Perform case-insensitive lookups of provided names.
.It Dv NV_FLAG_NO_UNIQUE
Names in the nvlist do not have to be unique.
.El
.Pp
The
.Fn nvlist_destroy
function destroys the given nvlist.
This function does nothing if
.Fa nvl
is
.Dv NULL .
This function never modifies
.Va errno .
.Pp
The
.Fn nvlist_error
function returns the first error set on
.Fa nvl .
If
.Fa nvl
is not in the error state,
this function returns zero.
If
.Fa nvl
is
.Dv NULL ,
.Er ENOMEM
is returned.
.Pp
The
.Fn nvlist_set_error
function sets an the error value for
.Fa nvl .
Subsequent calls to
.Fn nvlist_error
will return
.Fa error .
This function cannot be used to clear the error state from an nvlist.
This function does nothing if the nvlist is already in the error state.
.Pp
The
.Fn nvlist_empty
function returns
.Dv true
if
.Fa nvl
is empty and
.Dv false
otherwise.
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_flags
function returns the flags used to create
.Fa nvl
with the
.Fn nvlist_create ,
.Fn nvlist_recv ,
.Fn nvlist_unpack ,
or
.Fn nvlist_xfer
functions.
.Pp
The
.Fn nvlist_in_array
function returns
.Dv true
if
.Fa nvl
is part of an array that is a member of another nvlist.
.Pp
The
.Fn nvlist_clone
function clones
.Fa nvl .
The clone shares no resources with its origin.
This also means that all file descriptors that are part of the nvlist will be
duplicated with the
.Xr dup 2
system call before placing them in the clone.
.Pp
The
.Fn nvlist_dump
function dumps nvlist content for debugging purposes to the file descriptor
.Fa fd .
.Pp
The
.Fn nvlist_fdump
dumps nvlist content for debugging purposes to the file stream
.Fa fp .
.Pp
The
.Fn nvlist_size
function returns the size of the binary buffer that would be generated by the
.Fn nvlist_pack
function.
.Pp
The
.Fn nvlist_pack
function converts the given nvlist to a binary buffer.
The function allocates memory for the buffer which should be freed with the
.Xr free 3
function.
If the
.Fa sizep
argument is not
.Dv NULL ,
the size of the buffer is stored there.
This function returns
.Dv NULL
in case of an error (allocation failure).
If the nvlist contains any file descriptors
.Dv NULL
will be returned.
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_unpack
function converts a binary buffer to a new nvlist.
The
.Fa flags
argument has the same meaning as the
.Fa flags
argument passed to
.Fn nvlist_create .
If
.Fa flags
do not match the flags used to create the initial nvlist before it was packed,
this function will fail.
The flags of nested nvlists are not validated by this function.
The caller is responsible for validating the flags on any nested nvlists using
.Fn nvlist_flags .
This function returns the new nvlist on success or
.Dv NULL
in case of an error.
.Pp
The
.Fn nvlist_send
function sends
.Fa nvl
over the socket
.Fa sock .
Note that nvlists that contain file descriptors can only be sent over
.Xr unix 4
domain sockets.
.Pp
The
.Fn nvlist_recv
function receives an nvlist over the socket
.Fa sock .
As with
.Fn nvlist_unpack ,
the
.Fa flags
argument is used to construct the new nvlist and must match the flags used
to construct the original nvlist written to
.Fa sock
by the peer.
The flags of nested nvlists are not validated by this function.
The caller is responsible for validating the flags on any nested nvlists using
.Fn nvlist_flags .
This function returns the new nvlist on success or
.Dv NULL
in case of an error.
.Pp
The
.Fn nvlist_xfer
function sends
.Fa nvl
over the socket
.Fa sock
argument and then receives a new nvlist over the same socket.
The
.Fa flags
argument applies to the new nvlist similar to
.Fn nvlist_recv .
The nvlist
.Fa nvl
is always destroyed.
This function returns the new nvlist on success or
.Dv NULL
in case of an error.
.Pp
The
.Fn nvlist_next
function iterates over
.Fa nvl
returning the names and types of subsequent
elements.
The
.Fa cookiep
argument determines which element is returned.
If
.Va *cookiep
is
.Dv NULL ,
the values for the first element in the list are returned.
Otherwise,
.Va *cookiep
should contain the result of a prior call to
.Fn nvlist_next
in which case values for the next element from
.Fa nvl
are returned.
This function returns
.Dv NULL
when there are no more elements on
.Fa nvl .
The
.Fa typep
argument can be
.Dv NULL .
Elements may not be removed from
.Fa nvl
the nvlist while traversing it.
.Fa nvl
must not be in the error state.
Additional actions can be performed on an element identified by a cookie
via the
.Xr cnv 9
API .
.Pp
The
.Fn nvlist_exists
function returns
.Dv true
if an element named
.Fa name
exists in
.Fa nvl
(regardless of type) or
.Dv false
otherwise.
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_exists_type
function returns
.Dv true
if an element named
.Fa name
of type
.Fa type
exists or
.Dv false
otherwise.
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_exists_null ,
.Fn nvlist_exists_bool ,
.Fn nvlist_exists_number ,
.Fn nvlist_exists_string ,
.Fn nvlist_exists_nvlist ,
.Fn nvlist_exists_descriptor ,
.Fn nvlist_exists_binary ,
.Fn nvlist_exists_bool_array ,
.Fn nvlist_exists_number_array ,
.Fn nvlist_exists_string_array ,
.Fn nvlist_exists_nvlist_array ,
.Fn nvlist_exists_descriptor_array
functions return
.Dv true
if element named
.Fa name
with the type determined by the function name
exists or
.Dv false
otherwise.
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_add_null ,
.Fn nvlist_add_bool ,
.Fn nvlist_add_number ,
.Fn nvlist_add_string ,
.Fn nvlist_add_stringf ,
.Fn nvlist_add_stringv ,
.Fn nvlist_add_nvlist ,
.Fn nvlist_add_descriptor ,
.Fn nvlist_add_binary ,
.Fn nvlist_add_bool_array ,
.Fn nvlist_add_number_array ,
.Fn nvlist_add_string_array ,
.Fn nvlist_add_nvlist_array ,
.Fn nvlist_add_descriptor_array
functions add an element to
.Fa nvl .
When adding a string or binary buffer, these functions allocate memory
and copy the data.
When adding an nvlist, the
.Fa value
nvlist is cloned and the clone is added to
.Fa nvl .
When adding a file descriptor, the descriptor is duplicated via the
.Xr dup 2
system call and the new file descriptor is added.
The array functions fail if there are any
.Dv NULL
elements in the array, or if the array pointer is
.Dv NULL .
If an error occurs while adding a new element,
an internal error is set which can be
examined using the
.Fn nvlist_error
function.
.Pp
The
.Fn nvlist_move_string ,
.Fn nvlist_move_nvlist ,
.Fn nvlist_move_descriptor ,
.Fn nvlist_move_binary ,
.Fn nvlist_move_bool_array ,
.Fn nvlist_move_number_array ,
.Fn nvlist_move_string_array ,
.Fn nvlist_move_nvlist_array ,
.Fn nvlist_move_descriptor_array
functions add an element to
.Fa nvl ,
but unlike the
.Fn nvlist_add_<type>
functions they consume the given resource.
For string, file descriptor, binary buffer, or nvlist values,
no value should be moved into an nvlist multiple times;
doing so will cause that value to be freed multiple times.
Note that strings or binary buffers must be allocated with
.Xr malloc 3 ,
and the pointers will be released via
.Xr free 3
when
.Fa nvl
is destroyed.
The array functions fail if there are any
.Dv NULL
elements, or if the array pointer is
.Dv NULL .
If an error occurs while adding new element, the resource is destroyed and
an internal error is set which can be examined using the
.Fn nvlist_error
function.
.Pp
The
.Fn nvlist_get_bool ,
.Fn nvlist_get_number ,
.Fn nvlist_get_string ,
.Fn nvlist_get_nvlist ,
.Fn nvlist_get_descriptor ,
.Fn nvlist_get_binary ,
.Fn nvlist_get_bool_array ,
.Fn nvlist_get_number_array ,
.Fn nvlist_get_string_array ,
.Fn nvlist_get_nvlist_array ,
.Fn nvlist_get_descriptor_array
functions return the value of the first element in
.Fa nvl
named
.Fa name .
For string, nvlist, file descriptor, binary buffer, or array values,
the returned resource must not be modified - it still belongs to
.Fa nvl .
.Pp
If an element named
.Fa name
does not exist, the program aborts.
To avoid this, the caller should check for the existence of the element before
trying to obtain the value or use the
.Xr dnv 9
extension which provides a default value in the case of a missing element.
.Pp
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_get_parent
function returns the parent nvlist of
.Fa nvl .
.Pp
The
.Fn nvlist_get_array_next
function returns the next element after
.Fa nvl
from an array of nvlists.
If
.Fa nvl
is not in an array of nvlists or it is the last element,
this function returns
.Dv NULL .
An nvlist is only in an nvlist array if it was added to an nvlist array using
.Fn nvlist_add_nvlist_array ,
.Fn nvlist_append_nvlist_array ,
or
.Fn nvlist_move_nvlist_array .
.Pp
The
.Fn nvlist_get_pararr
function returns the next element after
.Fn nvl
from an array of nvlists.
If
.Fn nvl
is the last element in an array of nvlists,
the parent nvlist of
.Fa nvl is
returned.
If
.Fn nvl
is not in an array of nvlists,
.Dv NULL
is returned.
.Pp
The
.Fn nvlist_take_bool ,
.Fn nvlist_take_number ,
.Fn nvlist_take_string ,
.Fn nvlist_take_nvlist ,
.Fn nvlist_take_descriptor ,
.Fn nvlist_take_binary ,
.Fn nvlist_take_bool_array ,
.Fn nvlist_take_number_array ,
.Fn nvlist_take_string_array ,
.Fn nvlist_take_nvlist_array ,
.Fn nvlist_take_descriptor_array
functions return the value of the element named
.Fa name
and remove the element from
.Fa nvl .
For string and binary buffer values, the caller is responsible for freeing
the returned value using the
.Xr free 3
function.
For nvlist values, the caller is responsible for destroying the returned nvlist
using the
.Fn nvlist_destroy
function.
For file descriptor values, the caller is responsible for closing the
returned descriptor
using the
.Fn close 2
system call.
For array values, the caller is responsible for destroying every element of
the array based on the element type.
In addition, the caller must also free the pointer to the array using the
.Xr free 3
function.
.Pp
If an element named
.Fa name
does not exist, the program aborts.
To avoid this, the caller should check for the existence of the element before
trying to obtain the value or use the
.Xr dnv 9
extension which provides a default value in the case of a missing element.
.Pp
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_append_bool_array ,
.Fn nvlist_append_number_array ,
.Fn nvlist_append_string_array ,
.Fn nvlist_append_nvlist_array ,
.Fn nvlist_append_descriptor_array
functions append an element to an existing array using the same semantics
as the add functions (that is, the element will be copied when applicable).
If the array named
.Fa name
does not exist, then it will be created
as if using the
.Fn nvlist_add_<type>_array
function.
If an error occurs while appending a new element,
an internal error is set on
.Fa nvl .
.Pp
The
.Fn nvlist_free
function removes the first element named
.Fa name
from
.Fa nvl
(regardless of type)
and frees all resources associated with it.
If no element named
.Fa name
exists, the program aborts.
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_free_type
function removes the first element named
.Fa name
of type
.Fa type
from
.Fa nvl
and frees all resources associated with it.
If no element named
.Fa name
of type
.Fa type
exists, the program aborts.
The nvlist must not be in the error state.
.Pp
The
.Fn nvlist_free_null ,
.Fn nvlist_free_bool ,
.Fn nvlist_free_number ,
.Fn nvlist_free_string ,
.Fn nvlist_free_nvlist ,
.Fn nvlist_free_descriptor ,
.Fn nvlist_free_binary ,
.Fn nvlist_free_bool_array ,
.Fn nvlist_free_number_array ,
.Fn nvlist_free_string_array ,
.Fn nvlist_free_nvlist_array ,
.Fn nvlist_free_descriptor_array
functions remove the first element named
.Fa name
with the type determined by the function name from
.Fa nvl
free all resources associated with it.
If no element named
.Fa name
with the appropriate type exists, the program aborts.
The nvlist must not be in the error state.
.Ss Notes
The
.Fn nvlist_pack
and
.Fn nvlist_unpack
functions handle byte-order conversions, so binary buffers can be
packed and unpacked on hosts with different endianness.
.Pp
The
.Fn nvlist_recv ,
.Fn nvlist_send ,
and
.Fn nvlist_xfer
functions can transfer nvlists between hosts with different endianness.
.Ss Kernel Considerations
The
.Nm nv ,
.Nm cnv ,
and
.Nm dnv
APIs can be used in the kernel with the following differences:
.Bl -bullet
.It
File descriptor and file descriptor array value types are not supported.
.It
.Fn nvlist_recv ,
.Fn nvlist_send ,
and
.Fn nvlist_xfer
are not supported.
.It
All memory allocations use the
.Dv M_NVLIST
memory type with
.Xr malloc 9
and
.Xr free 9 .
As a result, any allocated buffers moved into an nvlist must be allocated with
.Dv M_NVLIST ,
and buffers returned by functions such as
.Fn nvlist_pack
must be freed with
.Dv M_NVLIST .
.El
.Sh EXAMPLES
The following example demonstrates how to prepare an nvlist and send it over a
.Xr unix 4
domain socket.
.Bd -literal
nvlist_t *nvl;
int fd;
fd = open("/tmp/foo", O_RDONLY);
if (fd < 0)
err(1, "open(\\"/tmp/foo\\") failed");
nvl = nvlist_create(0);
/*
* There is no need to check if nvlist_create() succeeded
* as the nvlist_add_<type>() functions can cope.
* If it failed, nvlist_send() will fail.
*/
nvlist_add_string(nvl, "filename", "/tmp/foo");
nvlist_add_number(nvl, "flags", O_RDONLY);
/*
* We just want to send the descriptor, so we can give it
* for the nvlist to consume (that is why we use nvlist_move
* not nvlist_add).
*/
nvlist_move_descriptor(nvl, "fd", fd);
if (nvlist_send(sock, nvl) < 0) {
nvlist_destroy(nvl);
err(1, "nvlist_send() failed");
}
nvlist_destroy(nvl);
.Ed
.Pp
Receiving an nvlist and retrieving element values:
.Bd -literal
nvlist_t *nvl;
const char *command;
char *filename;
int fd;
nvl = nvlist_recv(sock, 0);
if (nvl == NULL)
err(1, "nvlist_recv() failed");
/* For command we accept a pointer to the nvlist's internal buffer. */
command = nvlist_get_string(nvl, "command");
/*
* For filename we remove it from the nvlist and take
* ownership of the buffer.
*/
filename = nvlist_take_string(nvl, "filename");
/* The same for the file descriptor. */
fd = nvlist_take_descriptor(nvl, "fd");
printf("command=%s filename=%s fd=%d\n", command, filename, fd);
/* command is freed by nvlist_destroy() */
nvlist_destroy(nvl);
free(filename);
close(fd);
.Ed
.Pp
Iterating over an nvlist:
.Bd -literal
nvlist_t *nvl;
const char *name;
void *cookie;
int type;
nvl = nvlist_recv(sock, 0);
if (nvl == NULL)
err(1, "nvlist_recv() failed");
cookie = NULL;
while ((name = nvlist_next(nvl, &type, &cookie)) != NULL) {
printf("%s=", name);
switch (type) {
case NV_TYPE_NUMBER:
printf("%ju", (uintmax_t)nvlist_get_number(nvl, name));
break;
case NV_TYPE_STRING:
printf("%s", nvlist_get_string(nvl, name));
break;
default:
printf("N/A");
break;
}
printf("\\n");
}
.Ed
.Pp