CNV(9)
π CNV(9) π
okay bestie, welcome to the Kernel Developer's Manual page for cnvlist_get, cnvlist_take, and cnvlist_free β the whole "manage name/value pairs by cookie" situation. yes, cookies. not the edible kind π but honestly still iconic.
β¨ NAME β¨
cnvlist_get, cnvlist_take, cnvlist_free β the API for managing name/value pairs by cookie πͺ
π LIBRARY π
library βlibnvβ β that's the one, no cap.
πΈ 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);
π DESCRIPTION π
so the libnv library is literally the easiest way to manage name/value pairs, and it can also send and receive them over sockets π₯. more deets over in nv(9), mwah.
and the whole "cookies" concept? that's explained in nvlist_next(), nvlist_get_parent(), and nvlist_get_pararr() from nv(9). go read those, iykyk π«Ά
π¦ the getters π¦
cnvlist_name()β returns the name of the element chillin' withcookie. like a little name tag at a party πcnvlist_type()β returns the type of the element associated withcookie. all the possible types that can come back are described innv(9). vibe check on the element, basically β¨- the
cnvlist_getfunctions β these return the value associated withcookie. and bestie, listen: any returned strings, nvlists, descriptors, binaries, or arrays must not be modified by the user, because they still belong to the nvlist π . also the nvlist must not be in an error state. periodt.
π₯ the take functions π₯
- the
cnvlist_takefunctions β they return the value associated with the givencookieand remove the element from the nvlist. main character behaviour, honestly. and because ownership literally changes hands:- if the value is a string, binary, or array value, you (the caller) are responsible for freeing the returned memory with
free(3)ποΈ - if the value is an nvlist, you are responsible for destroying the returned nvlist with
nvlist_destroy() - if the value is a descriptor, you are responsible for closing the returned descriptor with
close(2)
- if the value is a string, binary, or array value, you (the caller) are responsible for freeing the returned memory with
no returns, no take-backs β it's yours now, you own it π
π the free functions π
- the
cnvlist_freefunctions β they remove the element identified bycookieand free any associated resources. clean, cute, done.
β οΈ but okay, warning time: if the element identified by cookie has the wrong type or does not exist, the program aborts. like, full on yeets itself π not a drill, be careful.
π§ EXAMPLES π§
here's a little demo of how to deal with the cnvlist API. copy it, love it, run 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 ALSO π
close(2), free(3), nv(9) β go read 'em, they're all lowkey essential π«Ά
π AUTHORS π
the cnv API was created during the Google Summer Of Code 2016 by Adam Starak. king behaviour, honestly πβ¨
and that's the whole page, bestie ππ this one came from Debian, dated January 3, 2025 β slay, go write some cookies πͺπ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2016 Adam Starak <starak.adam@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms