CNV(9)
π CNV(9) π
okay bestie, welcome to the Kernel Developer's Manual β¨ this one is all about cnvlist_get, cnvlist_take, and cnvlist_free β the whole API for managing name/value pairs by cookie, living in the library libnv. yes the cookie is the main character here, we'll get to that πͺπ
π NAME π
cnvlist_get, cnvlist_take, cnvlist_free β API for managing name/value pairs by cookie. that's it, that's the whole vibe π«Ά
β¨ LIBRARY β¨
library libnv β that's where all of this lives, periodt.
πΈ SYNOPSIS πΈ
#include <sys/cnv.h>
const char *
cnvlist_name(const void *cookie);
int
cnvlist_type(const void *cookie);
bool
cnvlist_get_bool(const void *cookie);
uint64_t
cnvlist_get_number(const void *cookie);
const char *
cnvlist_get_string(const void *cookie);
const nvlist_t *
cnvlist_get_nvlist(const void *cookie);
const void *
cnvlist_get_binary(const void *cookie, size_t *sizep);
const bool *
cnvlist_get_bool_array(const void *cookie, size_t *nitemsp);
const uint64_t *
cnvlist_get_number_array(const void *cookie, size_t *nitemsp);
const char * const *
cnvlist_get_string_array(const void *cookie, size_t *nitemsp);
const nvlist_t * const *
cnvlist_get_nvlist_array(const void *cookie, size_t *nitemsp);
int
cnvlist_get_descriptor(const void *cookie);
const int *
cnvlist_get_descriptor_array(const void *cookie, size_t *nitemsp);
bool
cnvlist_take_bool(void *cookie);
uint64_t
cnvlist_take_number(void *cookie);
const char *
cnvlist_take_string(void *cookie);
const nvlist_t *
cnvlist_take_nvlist(void *cookie);
const void *
cnvlist_take_binary(void *cookie, size_t *sizep);
const bool *
cnvlist_take_bool_array(void *cookie, size_t *nitemsp);
const uint64_t *
cnvlist_take_number_array(void *cookie, size_t *nitemsp);
const char * const *
cnvlist_take_string_array(void *cookie, size_t *nitemsp);
const nvlist_t * const *
cnvlist_take_nvlist_array(void *cookie, size_t *nitemsp);
int
cnvlist_take_descriptor(void *cookie);
const int *
cnvlist_take_descriptor_array(void *cookie, size_t *nitemsp);
void
cnvlist_free_null(void *cookie);
void
cnvlist_free_bool(void *cookie);
void
cnvlist_free_number(void *cookie);
void
cnvlist_free_string(void *cookie);
void
cnvlist_free_nvlist(void *cookie);
void
cnvlist_free_descriptor(void *cookie);
void
cnvlist_free_binary(void *cookie);
void
cnvlist_free_bool_array(void *cookie);
void
cnvlist_free_number_array(void *cookie);
void
cnvlist_free_string_array(void *cookie);
void
cnvlist_free_nvlist_array(void *cookie);
void
cnvlist_free_descriptor_array(void *cookie);
look at that list. every single one of them, no skips, no shortcuts. iconic behavior honestly π
π¦ DESCRIPTION π¦
so, the libnv library lets you manage name/value pairs super easily, and it can also send and receive them over sockets π«Ά if you want the deep lore, go read nv(9) too.
the whole cookie concept gets explained over in nvlist_next(), nvlist_get_parent(), and nvlist_get_pararr() from nv(9). that's your homework bestie, it's giving required reading πβ¨
cnvlist_name()β hands you the name of the element that's chillin' with the given cookie.cnvlist_type()β hands you the type of the element tied to that cookie. all the types it could possibly return are described innv(9), so go look there for the full menu.
the cnvlist_get family of functions grabs the value associated with the given cookie π and listen β strings, nvlists, descriptors, binaries, or arrays that get returned must not be modified by the user, because they still belong to the nvlist. not yours, bestie, don't touch π also the nvlist must not be in an error state. that part matters.
the cnvlist_take family of functions returns the value associated with the given cookie and removes the element from the nvlist. it's a take, not a copy, iykyk. and because it's a take, cleanup is on you:
- when the value is a string, binary, or array value, the caller is responsible for freeing the returned memory with
free(3). - when the value is an nvlist, the caller is responsible for destroying the returned nvlist with
nvlist_destroy(). - when the value is a descriptor, the caller is responsible for closing the returned descriptor with the
close(2).
no cap: you took it, you own it. forgot to free it? that's a leak, and leaks are not the vibe π
the cnvlist_free family of functions removes an element of the supplied cookie and frees all the resources. and here's the dramatic part:
if an element of the given cookie has the wrong type or does not exist, the program is aborted. like, actually aborted. not a warning, not a gentle nudge. so check your types first bestie π
π EXAMPLE π
here's a lil example showing how to deal with the cnvlist API. it's giving tutorial energy, we love to see it β¨
int type;
void *cookie, *scookie, *bcookie;
nvlist_t *nvl;
char *name;
nvl = nvlist_create(0);
nvlist_add_bool(nvl, "test", 1 == 2);
nvlist_add_string(nvl, "test2", "cnvlist");
cookie = NULL;
while (nvlist_next(nvl, &type, &cookie) != NULL) {
switch (type) {
case NV_TYPE_BOOL:
printf("test: %d\n", cnvlist_get_bool(cookie));
bcookie = cookie;
break;
case NV_TYPE_STRING:
printf("test2: %s\n", cnvlist_get_string(cookie));
scookie = cookie;
break;
}
}
name = cnvlist_take_string(scookie);
cnvlist_free_bool(bcookie);
printf("test2: %s\n", name);
free(name);
printf("nvlist_empty = %d\n", nvlist_empty(nvl));
nvlist_destroy(nvl);
return (0);
see how scookie and bcookie are saved during the loop so they can be used after? yeah. that's the move. and then name gets freed because it was taken, and the bool gets freed with cnvlist_free_bool(). everything balanced, everything slaying π
π SEE ALSO π
go read them, fr fr, they're all connected π¦
π AUTHORS π
the cnv API was created during the Google Summer Of Code 2016 by Adam Starak. shoutout to the icon who built this π«Ά
and that's the whole page bestie π this came to you from Debian, dated June 18, 2018, filed under CNV(9) in the Kernel Developer's Manual. take care of your cookies, free your takes, and never pass the wrong type to cnvlist_free or it will simply end you π
β¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2016 Adam Starak <starak.adam@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms