Add macros to make code compile in kernel

Make it possible to compile libnv in the kernel.  Mostly this
involves wrapping functions that have a different signature in
the kernel and in userland (e.g. malloc()) in a macro that will
conditionally expand to the right API depending on whether the
code is being compiled for the kernel or not.

I have also #ifdef'ed out all of file descriptor-handling code,
as well as the unsafe varargs functions.

Differential Revision:		https://reviews.freebsd.org/D1882
Reviewed by:			jfv
MFC after:			1 month
Sponsored by:			Sandvine Inc
This commit is contained in:
Ryan Stone 2015-03-01 00:22:53 +00:00
parent 296a8144aa
commit 814f9a1824
8 changed files with 349 additions and 121 deletions

View File

@ -34,9 +34,11 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef _KERNEL
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#endif
#ifndef _NVLIST_T_DECLARED #ifndef _NVLIST_T_DECLARED
#define _NVLIST_T_DECLARED #define _NVLIST_T_DECLARED
@ -62,6 +64,7 @@ const nvlist_t *dnvlist_get_nvlist(const nvlist_t *nvl, const char *name, const
int dnvlist_get_descriptor(const nvlist_t *nvl, const char *name, int defval); int dnvlist_get_descriptor(const nvlist_t *nvl, const char *name, int defval);
const void *dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep, const void *defval, size_t defsize); const void *dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep, const void *defval, size_t defsize);
#ifndef _KERNEL
bool dnvlist_getf_bool(const nvlist_t *nvl, bool defval, const char *namefmt, ...) __printflike(3, 4); bool dnvlist_getf_bool(const nvlist_t *nvl, bool defval, const char *namefmt, ...) __printflike(3, 4);
uint64_t dnvlist_getf_number(const nvlist_t *nvl, uint64_t defval, const char *namefmt, ...) __printflike(3, 4); uint64_t dnvlist_getf_number(const nvlist_t *nvl, uint64_t defval, const char *namefmt, ...) __printflike(3, 4);
const char *dnvlist_getf_string(const nvlist_t *nvl, const char *defval, const char *namefmt, ...) __printflike(3, 4); const char *dnvlist_getf_string(const nvlist_t *nvl, const char *defval, const char *namefmt, ...) __printflike(3, 4);
@ -75,6 +78,7 @@ const char *dnvlist_getv_string(const nvlist_t *nvl, const char *defval, const c
const nvlist_t *dnvlist_getv_nvlist(const nvlist_t *nvl, const nvlist_t *defval, const char *namefmt, va_list nameap) __printflike(3, 0); const nvlist_t *dnvlist_getv_nvlist(const nvlist_t *nvl, const nvlist_t *defval, const char *namefmt, va_list nameap) __printflike(3, 0);
int dnvlist_getv_descriptor(const nvlist_t *nvl, int defval, const char *namefmt, va_list nameap) __printflike(3, 0); int dnvlist_getv_descriptor(const nvlist_t *nvl, int defval, const char *namefmt, va_list nameap) __printflike(3, 0);
const void *dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval, size_t defsize, const char *namefmt, va_list nameap) __printflike(5, 0); const void *dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval, size_t defsize, const char *namefmt, va_list nameap) __printflike(5, 0);
#endif
/* /*
* The dnvlist_take functions returns value associated with the given name and * The dnvlist_take functions returns value associated with the given name and
@ -91,6 +95,7 @@ nvlist_t *dnvlist_take_nvlist(nvlist_t *nvl, const char *name, nvlist_t *defval)
int dnvlist_take_descriptor(nvlist_t *nvl, const char *name, int defval); int dnvlist_take_descriptor(nvlist_t *nvl, const char *name, int defval);
void *dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep, void *defval, size_t defsize); void *dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep, void *defval, size_t defsize);
#ifndef _KERNEL
bool dnvlist_takef_bool(nvlist_t *nvl, bool defval, const char *namefmt, ...) __printflike(3, 4); bool dnvlist_takef_bool(nvlist_t *nvl, bool defval, const char *namefmt, ...) __printflike(3, 4);
uint64_t dnvlist_takef_number(nvlist_t *nvl, uint64_t defval, const char *namefmt, ...) __printflike(3, 4); uint64_t dnvlist_takef_number(nvlist_t *nvl, uint64_t defval, const char *namefmt, ...) __printflike(3, 4);
char *dnvlist_takef_string(nvlist_t *nvl, char *defval, const char *namefmt, ...) __printflike(3, 4); char *dnvlist_takef_string(nvlist_t *nvl, char *defval, const char *namefmt, ...) __printflike(3, 4);
@ -104,6 +109,7 @@ char *dnvlist_takev_string(nvlist_t *nvl, char *defval, const char *namefmt, va_
nvlist_t *dnvlist_takev_nvlist(nvlist_t *nvl, nvlist_t *defval, const char *namefmt, va_list nameap) __printflike(3, 0); nvlist_t *dnvlist_takev_nvlist(nvlist_t *nvl, nvlist_t *defval, const char *namefmt, va_list nameap) __printflike(3, 0);
int dnvlist_takev_descriptor(nvlist_t *nvl, int defval, const char *namefmt, va_list nameap) __printflike(3, 0); int dnvlist_takev_descriptor(nvlist_t *nvl, int defval, const char *namefmt, va_list nameap) __printflike(3, 0);
void *dnvlist_takev_binary(nvlist_t *nvl, size_t *sizep, void *defval, size_t defsize, const char *namefmt, va_list nameap) __printflike(5, 0); void *dnvlist_takev_binary(nvlist_t *nvl, size_t *sizep, void *defval, size_t defsize, const char *namefmt, va_list nameap) __printflike(5, 0);
#endif
__END_DECLS __END_DECLS

View File

@ -30,10 +30,22 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#ifdef _KERNEL
#include <sys/types.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <machine/stdarg.h>
#else
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#endif
#include "nv.h" #include "nv.h"
#include "nv_impl.h" #include "nv_impl.h"
@ -55,7 +67,9 @@ DNVLIST_GET(bool, bool)
DNVLIST_GET(uint64_t, number) DNVLIST_GET(uint64_t, number)
DNVLIST_GET(const char *, string) DNVLIST_GET(const char *, string)
DNVLIST_GET(const nvlist_t *, nvlist) DNVLIST_GET(const nvlist_t *, nvlist)
#ifndef _KERNEL
DNVLIST_GET(int, descriptor) DNVLIST_GET(int, descriptor)
#endif
#undef DNVLIST_GET #undef DNVLIST_GET
@ -75,6 +89,7 @@ dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep,
return (value); return (value);
} }
#ifndef _KERNEL
#define DNVLIST_GETF(ftype, type) \ #define DNVLIST_GETF(ftype, type) \
ftype \ ftype \
dnvlist_getf_##type(const nvlist_t *nvl, ftype defval, \ dnvlist_getf_##type(const nvlist_t *nvl, ftype defval, \
@ -144,10 +159,10 @@ dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval,
char *name; char *name;
const void *value; const void *value;
vasprintf(&name, namefmt, nameap); nv_vasprintf(&name, namefmt, nameap);
if (name != NULL) { if (name != NULL) {
value = dnvlist_get_binary(nvl, name, sizep, defval, defsize); value = dnvlist_get_binary(nvl, name, sizep, defval, defsize);
free(name); nv_free(name);
} else { } else {
if (sizep != NULL) if (sizep != NULL)
*sizep = defsize; *sizep = defsize;
@ -155,6 +170,7 @@ dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval,
} }
return (value); return (value);
} }
#endif
#define DNVLIST_TAKE(ftype, type) \ #define DNVLIST_TAKE(ftype, type) \
ftype \ ftype \
@ -171,7 +187,9 @@ DNVLIST_TAKE(bool, bool)
DNVLIST_TAKE(uint64_t, number) DNVLIST_TAKE(uint64_t, number)
DNVLIST_TAKE(char *, string) DNVLIST_TAKE(char *, string)
DNVLIST_TAKE(nvlist_t *, nvlist) DNVLIST_TAKE(nvlist_t *, nvlist)
#ifndef _KERNEL
DNVLIST_TAKE(int, descriptor) DNVLIST_TAKE(int, descriptor)
#endif
#undef DNVLIST_TAKE #undef DNVLIST_TAKE
@ -191,6 +209,7 @@ dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep,
return (value); return (value);
} }
#ifndef _KERNEL
#define DNVLIST_TAKEF(ftype, type) \ #define DNVLIST_TAKEF(ftype, type) \
ftype \ ftype \
dnvlist_takef_##type(nvlist_t *nvl, ftype defval, \ dnvlist_takef_##type(nvlist_t *nvl, ftype defval, \
@ -260,10 +279,10 @@ dnvlist_takev_binary(nvlist_t *nvl, size_t *sizep, void *defval,
char *name; char *name;
void *value; void *value;
vasprintf(&name, namefmt, nameap); nv_vasprintf(&name, namefmt, nameap);
if (name != NULL) { if (name != NULL) {
value = dnvlist_take_binary(nvl, name, sizep, defval, defsize); value = dnvlist_take_binary(nvl, name, sizep, defval, defsize);
free(name); nv_free(name);
} else { } else {
if (sizep != NULL) if (sizep != NULL)
*sizep = defsize; *sizep = defsize;
@ -272,3 +291,4 @@ dnvlist_takev_binary(nvlist_t *nvl, size_t *sizep, void *defval,
return (value); return (value);
} }
#endif

View File

@ -34,10 +34,12 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef _KERNEL
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#endif
#ifndef _NVLIST_T_DECLARED #ifndef _NVLIST_T_DECLARED
#define _NVLIST_T_DECLARED #define _NVLIST_T_DECLARED
@ -63,6 +65,10 @@ typedef struct nvlist nvlist_t;
*/ */
#define NV_FLAG_IGNORE_CASE 0x01 #define NV_FLAG_IGNORE_CASE 0x01
#if defined(_KERNEL) && defined(MALLOC_DECLARE)
MALLOC_DECLARE(M_NVLIST);
#endif
__BEGIN_DECLS __BEGIN_DECLS
nvlist_t *nvlist_create(int flags); nvlist_t *nvlist_create(int flags);
@ -73,8 +79,10 @@ void nvlist_set_error(nvlist_t *nvl, int error);
nvlist_t *nvlist_clone(const nvlist_t *nvl); nvlist_t *nvlist_clone(const nvlist_t *nvl);
#ifndef _KERNEL
void nvlist_dump(const nvlist_t *nvl, int fd); void nvlist_dump(const nvlist_t *nvl, int fd);
void nvlist_fdump(const nvlist_t *nvl, FILE *fp); void nvlist_fdump(const nvlist_t *nvl, FILE *fp);
#endif
size_t nvlist_size(const nvlist_t *nvl); size_t nvlist_size(const nvlist_t *nvl);
void *nvlist_pack(const nvlist_t *nvl, size_t *sizep); void *nvlist_pack(const nvlist_t *nvl, size_t *sizep);
@ -101,7 +109,9 @@ bool nvlist_exists_bool(const nvlist_t *nvl, const char *name);
bool nvlist_exists_number(const nvlist_t *nvl, const char *name); bool nvlist_exists_number(const nvlist_t *nvl, const char *name);
bool nvlist_exists_string(const nvlist_t *nvl, const char *name); bool nvlist_exists_string(const nvlist_t *nvl, const char *name);
bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name); bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name);
#ifndef _KERNEL
bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name); bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name);
#endif
bool nvlist_exists_binary(const nvlist_t *nvl, const char *name); bool nvlist_exists_binary(const nvlist_t *nvl, const char *name);
/* /*
@ -115,9 +125,13 @@ void nvlist_add_bool(nvlist_t *nvl, const char *name, bool value);
void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value); void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value);
void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value); void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value);
void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4); void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4);
#ifdef _VA_LIST_DECLARED
void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0); void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0);
#endif
void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value); void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value);
#ifndef _KERNEL
void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value); void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value);
#endif
void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size); void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size);
/* /*
@ -127,7 +141,9 @@ void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_
void nvlist_move_string(nvlist_t *nvl, const char *name, char *value); void nvlist_move_string(nvlist_t *nvl, const char *name, char *value);
void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value); void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value);
#ifndef _KERNEL
void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value); void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value);
#endif
void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size); void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size);
/* /*
@ -140,7 +156,9 @@ bool nvlist_get_bool(const nvlist_t *nvl, const char *name);
uint64_t nvlist_get_number(const nvlist_t *nvl, const char *name); uint64_t nvlist_get_number(const nvlist_t *nvl, const char *name);
const char *nvlist_get_string(const nvlist_t *nvl, const char *name); const char *nvlist_get_string(const nvlist_t *nvl, const char *name);
const nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name); const nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name);
#ifndef _KERNEL
int nvlist_get_descriptor(const nvlist_t *nvl, const char *name); int nvlist_get_descriptor(const nvlist_t *nvl, const char *name);
#endif
const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep); const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep);
/* /*
@ -153,7 +171,9 @@ bool nvlist_take_bool(nvlist_t *nvl, const char *name);
uint64_t nvlist_take_number(nvlist_t *nvl, const char *name); uint64_t nvlist_take_number(nvlist_t *nvl, const char *name);
char *nvlist_take_string(nvlist_t *nvl, const char *name); char *nvlist_take_string(nvlist_t *nvl, const char *name);
nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name); nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name);
#ifndef _KERNEL
int nvlist_take_descriptor(nvlist_t *nvl, const char *name); int nvlist_take_descriptor(nvlist_t *nvl, const char *name);
#endif
void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep); void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep);
/* /*
@ -169,14 +189,21 @@ void nvlist_free_bool(nvlist_t *nvl, const char *name);
void nvlist_free_number(nvlist_t *nvl, const char *name); void nvlist_free_number(nvlist_t *nvl, const char *name);
void nvlist_free_string(nvlist_t *nvl, const char *name); void nvlist_free_string(nvlist_t *nvl, const char *name);
void nvlist_free_nvlist(nvlist_t *nvl, const char *name); void nvlist_free_nvlist(nvlist_t *nvl, const char *name);
#ifndef _KERNEL
void nvlist_free_descriptor(nvlist_t *nvl, const char *name); void nvlist_free_descriptor(nvlist_t *nvl, const char *name);
#endif
void nvlist_free_binary(nvlist_t *nvl, const char *name); void nvlist_free_binary(nvlist_t *nvl, const char *name);
/* /*
* Below are the same functions, but which operate on format strings and * Below are the same functions, but which operate on format strings and
* variable argument lists. * variable argument lists.
*
* Functions that are not inserting a new pair into the nvlist cannot handle
* a failure to allocate the memory to hold the new name. Therefore these
* functions are not provided in the kernel.
*/ */
#ifndef _KERNEL
bool nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3); bool nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
bool nvlist_existsf_type(const nvlist_t *nvl, int type, const char *namefmt, ...) __printflike(3, 4); bool nvlist_existsf_type(const nvlist_t *nvl, int type, const char *namefmt, ...) __printflike(3, 4);
@ -198,33 +225,47 @@ bool nvlist_existsv_string(const nvlist_t *nvl, const char *namefmt, va_list nam
bool nvlist_existsv_nvlist(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); bool nvlist_existsv_nvlist(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
bool nvlist_existsv_descriptor(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); bool nvlist_existsv_descriptor(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
bool nvlist_existsv_binary(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); bool nvlist_existsv_binary(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
#endif
void nvlist_addf_null(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3); void nvlist_addf_null(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
void nvlist_addf_bool(nvlist_t *nvl, bool value, const char *namefmt, ...) __printflike(3, 4); void nvlist_addf_bool(nvlist_t *nvl, bool value, const char *namefmt, ...) __printflike(3, 4);
void nvlist_addf_number(nvlist_t *nvl, uint64_t value, const char *namefmt, ...) __printflike(3, 4); void nvlist_addf_number(nvlist_t *nvl, uint64_t value, const char *namefmt, ...) __printflike(3, 4);
void nvlist_addf_string(nvlist_t *nvl, const char *value, const char *namefmt, ...) __printflike(3, 4); void nvlist_addf_string(nvlist_t *nvl, const char *value, const char *namefmt, ...) __printflike(3, 4);
void nvlist_addf_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, ...) __printflike(3, 4); void nvlist_addf_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, ...) __printflike(3, 4);
#ifndef _KERNEL
void nvlist_addf_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4); void nvlist_addf_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4);
#endif
void nvlist_addf_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, ...) __printflike(4, 5); void nvlist_addf_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, ...) __printflike(4, 5);
#if !defined(_KERNEL) || defined(_VA_LIST_DECLARED)
void nvlist_addv_null(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); void nvlist_addv_null(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
void nvlist_addv_bool(nvlist_t *nvl, bool value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_addv_bool(nvlist_t *nvl, bool value, const char *namefmt, va_list nameap) __printflike(3, 0);
void nvlist_addv_number(nvlist_t *nvl, uint64_t value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_addv_number(nvlist_t *nvl, uint64_t value, const char *namefmt, va_list nameap) __printflike(3, 0);
void nvlist_addv_string(nvlist_t *nvl, const char *value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_addv_string(nvlist_t *nvl, const char *value, const char *namefmt, va_list nameap) __printflike(3, 0);
void nvlist_addv_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_addv_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0);
#ifndef _KERNEL
void nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0);
#endif
void nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0); void nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0);
#endif
void nvlist_movef_string(nvlist_t *nvl, char *value, const char *namefmt, ...) __printflike(3, 4); void nvlist_movef_string(nvlist_t *nvl, char *value, const char *namefmt, ...) __printflike(3, 4);
void nvlist_movef_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, ...) __printflike(3, 4); void nvlist_movef_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, ...) __printflike(3, 4);
#ifndef _KERNEL
void nvlist_movef_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4); void nvlist_movef_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4);
#endif
void nvlist_movef_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, ...) __printflike(4, 5); void nvlist_movef_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, ...) __printflike(4, 5);
#if !defined(_KERNEL) || defined(_VA_LIST_DECLARED)
void nvlist_movev_string(nvlist_t *nvl, char *value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_movev_string(nvlist_t *nvl, char *value, const char *namefmt, va_list nameap) __printflike(3, 0);
void nvlist_movev_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_movev_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0);
#ifndef _KERNEL
void nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0); void nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0);
#endif
void nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0); void nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0);
#endif
#ifndef _KERNEL
bool nvlist_getf_bool(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3); bool nvlist_getf_bool(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
uint64_t nvlist_getf_number(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3); uint64_t nvlist_getf_number(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
const char *nvlist_getf_string(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3); const char *nvlist_getf_string(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
@ -274,6 +315,7 @@ void nvlist_freev_string(nvlist_t *nvl, const char *namefmt, va_list nameap) __p
void nvlist_freev_nvlist(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); void nvlist_freev_nvlist(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
void nvlist_freev_descriptor(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); void nvlist_freev_descriptor(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
void nvlist_freev_binary(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); void nvlist_freev_binary(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
#endif /* _KERNEL */
__END_DECLS __END_DECLS

View File

@ -46,6 +46,37 @@ typedef struct nvpair nvpair_t;
#define NV_FLAG_BIG_ENDIAN 0x80 #define NV_FLAG_BIG_ENDIAN 0x80
#ifdef _KERNEL
#define nv_malloc(size) malloc((size), M_NVLIST, M_NOWAIT)
#define nv_calloc(n, size) malloc((n) * (size), M_NVLIST, \
M_NOWAIT | M_ZERO)
#define nv_realloc(buf, size) realloc((buf), (size), M_NVLIST, \
M_NOWAIT)
#define nv_free(buf) free((buf), M_NVLIST)
#define nv_strdup(buf) strdup((buf), M_NVLIST)
#define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
#define SAVE_ERRNO(var) ((void)(var))
#define RESTORE_ERRNO(var) ((void)(var))
#define ERRNO_OR_DEFAULT(default) (default)
#else
#define nv_malloc(size) malloc((size))
#define nv_calloc(n, size) calloc((n), (size))
#define nv_realloc(buf, size) realloc((buf), (size))
#define nv_free(buf) free((buf))
#define nv_strdup(buf) strdup((buf))
#define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__)
#define SAVE_ERRNO(var) (var) = errno
#define RESTORE_ERRNO(var) errno = (var)
#define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno)
#endif
int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp); int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp);
size_t nvlist_ndescriptors(const nvlist_t *nvl); size_t nvlist_ndescriptors(const nvlist_t *nvl);

View File

@ -33,6 +33,18 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h> #include <sys/param.h>
#include <sys/endian.h> #include <sys/endian.h>
#include <sys/queue.h> #include <sys/queue.h>
#ifdef _KERNEL
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/stdarg.h>
#else
#include <sys/socket.h> #include <sys/socket.h>
#include <errno.h> #include <errno.h>
@ -44,6 +56,7 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#endif
#ifdef HAVE_PJDLOG #ifdef HAVE_PJDLOG
#include <pjdlog.h> #include <pjdlog.h>
@ -56,6 +69,11 @@ __FBSDID("$FreeBSD$");
#include "nvpair_impl.h" #include "nvpair_impl.h"
#ifndef HAVE_PJDLOG #ifndef HAVE_PJDLOG
#ifdef _KERNEL
#define PJDLOG_ASSERT(...) MPASS(__VA_ARGS__)
#define PJDLOG_RASSERT(expr, ...) KASSERT(expr, (__VA_ARGS__))
#define PJDLOG_ABORT(...) panic(__VA_ARGS__)
#else
#include <assert.h> #include <assert.h>
#define PJDLOG_ASSERT(...) assert(__VA_ARGS__) #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
#define PJDLOG_RASSERT(expr, ...) assert(expr) #define PJDLOG_RASSERT(expr, ...) assert(expr)
@ -66,6 +84,7 @@ __FBSDID("$FreeBSD$");
abort(); \ abort(); \
} while (0) } while (0)
#endif #endif
#endif
#define NV_FLAG_PRIVATE_MASK (NV_FLAG_BIG_ENDIAN) #define NV_FLAG_PRIVATE_MASK (NV_FLAG_BIG_ENDIAN)
#define NV_FLAG_PUBLIC_MASK (NV_FLAG_IGNORE_CASE) #define NV_FLAG_PUBLIC_MASK (NV_FLAG_IGNORE_CASE)
@ -85,6 +104,10 @@ struct nvlist {
PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC); \ PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC); \
} while (0) } while (0)
#ifdef _KERNEL
MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist");
#endif
#define NVPAIR_ASSERT(nvp) nvpair_assert(nvp) #define NVPAIR_ASSERT(nvp) nvpair_assert(nvp)
#define NVLIST_HEADER_MAGIC 0x6c #define NVLIST_HEADER_MAGIC 0x6c
@ -104,7 +127,7 @@ nvlist_create(int flags)
PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0); PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
nvl = malloc(sizeof(*nvl)); nvl = nv_malloc(sizeof(*nvl));
nvl->nvl_error = 0; nvl->nvl_error = 0;
nvl->nvl_flags = flags; nvl->nvl_flags = flags;
nvl->nvl_parent = NULL; nvl->nvl_parent = NULL;
@ -123,7 +146,7 @@ nvlist_destroy(nvlist_t *nvl)
if (nvl == NULL) if (nvl == NULL)
return; return;
serrno = errno; SAVE_ERRNO(serrno);
NVLIST_ASSERT(nvl); NVLIST_ASSERT(nvl);
@ -132,9 +155,9 @@ nvlist_destroy(nvlist_t *nvl)
nvpair_free(nvp); nvpair_free(nvp);
} }
nvl->nvl_magic = 0; nvl->nvl_magic = 0;
free(nvl); nv_free(nvl);
errno = serrno; RESTORE_ERRNO(serrno);
} }
void void
@ -240,7 +263,7 @@ nvlist_find(const nvlist_t *nvl, int type, const char *name)
} }
if (nvp == NULL) if (nvp == NULL)
errno = ENOENT; RESTORE_ERRNO(ENOENT);
return (nvp); return (nvp);
} }
@ -257,6 +280,7 @@ nvlist_exists_type(const nvlist_t *nvl, const char *name, int type)
return (nvlist_find(nvl, type, name) != NULL); return (nvlist_find(nvl, type, name) != NULL);
} }
#ifndef _KERNEL
bool bool
nvlist_existsf_type(const nvlist_t *nvl, int type, const char *namefmt, ...) nvlist_existsf_type(const nvlist_t *nvl, int type, const char *namefmt, ...)
{ {
@ -277,14 +301,15 @@ nvlist_existsv_type(const nvlist_t *nvl, int type, const char *namefmt,
char *name; char *name;
bool exists; bool exists;
vasprintf(&name, namefmt, nameap); nv_vasprintf(&name, namefmt, nameap);
if (name == NULL) if (name == NULL)
return (false); return (false);
exists = nvlist_exists_type(nvl, name, type); exists = nvlist_exists_type(nvl, name, type);
free(name); nv_free(name);
return (exists); return (exists);
} }
#endif
void void
nvlist_free_type(nvlist_t *nvl, const char *name, int type) nvlist_free_type(nvlist_t *nvl, const char *name, int type)
@ -303,6 +328,7 @@ nvlist_free_type(nvlist_t *nvl, const char *name, int type)
nvlist_report_missing(type, name); nvlist_report_missing(type, name);
} }
#ifndef _KERNEL
void void
nvlist_freef_type(nvlist_t *nvl, int type, const char *namefmt, ...) nvlist_freef_type(nvlist_t *nvl, int type, const char *namefmt, ...)
{ {
@ -318,12 +344,13 @@ nvlist_freev_type(nvlist_t *nvl, int type, const char *namefmt, va_list nameap)
{ {
char *name; char *name;
vasprintf(&name, namefmt, nameap); nv_vasprintf(&name, namefmt, nameap);
if (name == NULL) if (name == NULL)
nvlist_report_missing(type, "<unknown>"); nvlist_report_missing(type, "<unknown>");
nvlist_free_type(nvl, name, type); nvlist_free_type(nvl, name, type);
free(name); nv_free(name);
} }
#endif
nvlist_t * nvlist_t *
nvlist_clone(const nvlist_t *nvl) nvlist_clone(const nvlist_t *nvl)
@ -334,7 +361,7 @@ nvlist_clone(const nvlist_t *nvl)
NVLIST_ASSERT(nvl); NVLIST_ASSERT(nvl);
if (nvl->nvl_error != 0) { if (nvl->nvl_error != 0) {
errno = nvl->nvl_error; RESTORE_ERRNO(nvl->nvl_error);
return (NULL); return (NULL);
} }
@ -353,6 +380,7 @@ nvlist_clone(const nvlist_t *nvl)
return (newnvl); return (newnvl);
} }
#ifndef _KERNEL
static bool static bool
nvlist_dump_error_check(const nvlist_t *nvl, int fd, int level) nvlist_dump_error_check(const nvlist_t *nvl, int fd, int level)
{ {
@ -453,6 +481,7 @@ nvlist_fdump(const nvlist_t *nvl, FILE *fp)
fflush(fp); fflush(fp);
nvlist_dump(nvl, fileno(fp)); nvlist_dump(nvl, fileno(fp));
} }
#endif
/* /*
* The function obtains size of the nvlist after nvlist_pack(). * The function obtains size of the nvlist after nvlist_pack().
@ -501,6 +530,7 @@ nvlist_size(const nvlist_t *nvl)
return (size); return (size);
} }
#ifndef _KERNEL
static int * static int *
nvlist_xdescriptors(const nvlist_t *nvl, int *descs, int level) nvlist_xdescriptors(const nvlist_t *nvl, int *descs, int level)
{ {
@ -526,7 +556,9 @@ nvlist_xdescriptors(const nvlist_t *nvl, int *descs, int level)
return (descs); return (descs);
} }
#endif
#ifndef _KERNEL
int * int *
nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp) nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
{ {
@ -534,7 +566,7 @@ nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
int *fds; int *fds;
nitems = nvlist_ndescriptors(nvl); nitems = nvlist_ndescriptors(nvl);
fds = malloc(sizeof(fds[0]) * (nitems + 1)); fds = nv_malloc(sizeof(fds[0]) * (nitems + 1));
if (fds == NULL) if (fds == NULL)
return (NULL); return (NULL);
if (nitems > 0) if (nitems > 0)
@ -544,10 +576,12 @@ nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
*nitemsp = nitems; *nitemsp = nitems;
return (fds); return (fds);
} }
#endif
static size_t static size_t
nvlist_xndescriptors(const nvlist_t *nvl, int level) nvlist_xndescriptors(const nvlist_t *nvl, int level)
{ {
#ifndef _KERNEL
const nvpair_t *nvp; const nvpair_t *nvp;
size_t ndescs; size_t ndescs;
@ -570,6 +604,9 @@ nvlist_xndescriptors(const nvlist_t *nvl, int level)
} }
return (ndescs); return (ndescs);
#else
return (0);
#endif
} }
size_t size_t
@ -614,12 +651,12 @@ nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
NVLIST_ASSERT(nvl); NVLIST_ASSERT(nvl);
if (nvl->nvl_error != 0) { if (nvl->nvl_error != 0) {
errno = nvl->nvl_error; RESTORE_ERRNO(nvl->nvl_error);
return (NULL); return (NULL);
} }
size = nvlist_size(nvl); size = nvlist_size(nvl);
buf = malloc(size); buf = nv_malloc(size);
if (buf == NULL) if (buf == NULL)
return (NULL); return (NULL);
@ -635,7 +672,7 @@ nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
nvpair_init_datasize(nvp); nvpair_init_datasize(nvp);
ptr = nvpair_pack_header(nvp, ptr, &left); ptr = nvpair_pack_header(nvp, ptr, &left);
if (ptr == NULL) { if (ptr == NULL) {
free(buf); nv_free(buf);
return (NULL); return (NULL);
} }
switch (nvpair_type(nvp)) { switch (nvpair_type(nvp)) {
@ -664,9 +701,11 @@ nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
} }
ptr = nvpair_pack_nvlist_up(ptr, &left); ptr = nvpair_pack_nvlist_up(ptr, &left);
break; break;
#ifndef _KERNEL
case NV_TYPE_DESCRIPTOR: case NV_TYPE_DESCRIPTOR:
ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left); ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left);
break; break;
#endif
case NV_TYPE_BINARY: case NV_TYPE_BINARY:
ptr = nvpair_pack_binary(nvp, ptr, &left); ptr = nvpair_pack_binary(nvp, ptr, &left);
break; break;
@ -674,7 +713,7 @@ nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp)); PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
} }
if (ptr == NULL) { if (ptr == NULL) {
free(buf); nv_free(buf);
return (NULL); return (NULL);
} }
while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) { while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
@ -702,12 +741,12 @@ nvlist_pack(const nvlist_t *nvl, size_t *sizep)
NVLIST_ASSERT(nvl); NVLIST_ASSERT(nvl);
if (nvl->nvl_error != 0) { if (nvl->nvl_error != 0) {
errno = nvl->nvl_error; RESTORE_ERRNO(nvl->nvl_error);
return (NULL); return (NULL);
} }
if (nvlist_ndescriptors(nvl) > 0) { if (nvlist_ndescriptors(nvl) > 0) {
errno = EOPNOTSUPP; RESTORE_ERRNO(EOPNOTSUPP);
return (NULL); return (NULL);
} }
@ -719,11 +758,11 @@ nvlist_check_header(struct nvlist_header *nvlhdrp)
{ {
if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) { if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (false); return (false);
} }
if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) { if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (false); return (false);
} }
#if BYTE_ORDER == BIG_ENDIAN #if BYTE_ORDER == BIG_ENDIAN
@ -775,7 +814,7 @@ nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds,
return (ptr); return (ptr);
failed: failed:
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -822,10 +861,12 @@ nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds)
&tmpnvl); &tmpnvl);
nvlist_set_parent(tmpnvl, nvp); nvlist_set_parent(tmpnvl, nvp);
break; break;
#ifndef _KERNEL
case NV_TYPE_DESCRIPTOR: case NV_TYPE_DESCRIPTOR:
ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left, ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left,
fds, nfds); fds, nfds);
break; break;
#endif
case NV_TYPE_BINARY: case NV_TYPE_BINARY:
ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left); ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left);
break; break;
@ -859,6 +900,7 @@ nvlist_unpack(const void *buf, size_t size)
return (nvlist_xunpack(buf, size, NULL, 0)); return (nvlist_xunpack(buf, size, NULL, 0));
} }
#ifndef _KERNEL
int int
nvlist_send(int sock, const nvlist_t *nvl) nvlist_send(int sock, const nvlist_t *nvl)
{ {
@ -968,6 +1010,7 @@ nvlist_xfer(int sock, nvlist_t *nvl)
nvlist_destroy(nvl); nvlist_destroy(nvl);
return (nvlist_recv(sock)); return (nvlist_recv(sock));
} }
#endif
nvpair_t * nvpair_t *
nvlist_first_nvpair(const nvlist_t *nvl) nvlist_first_nvpair(const nvlist_t *nvl)
@ -1049,11 +1092,14 @@ NVLIST_EXISTS(bool, BOOL)
NVLIST_EXISTS(number, NUMBER) NVLIST_EXISTS(number, NUMBER)
NVLIST_EXISTS(string, STRING) NVLIST_EXISTS(string, STRING)
NVLIST_EXISTS(nvlist, NVLIST) NVLIST_EXISTS(nvlist, NVLIST)
#ifndef _KERNEL
NVLIST_EXISTS(descriptor, DESCRIPTOR) NVLIST_EXISTS(descriptor, DESCRIPTOR)
#endif
NVLIST_EXISTS(binary, BINARY) NVLIST_EXISTS(binary, BINARY)
#undef NVLIST_EXISTS #undef NVLIST_EXISTS
#ifndef _KERNEL
bool bool
nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...) nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...)
{ {
@ -1084,7 +1130,9 @@ NVLIST_EXISTSF(bool)
NVLIST_EXISTSF(number) NVLIST_EXISTSF(number)
NVLIST_EXISTSF(string) NVLIST_EXISTSF(string)
NVLIST_EXISTSF(nvlist) NVLIST_EXISTSF(nvlist)
#ifndef _KERNEL
NVLIST_EXISTSF(descriptor) NVLIST_EXISTSF(descriptor)
#endif
NVLIST_EXISTSF(binary) NVLIST_EXISTSF(binary)
#undef NVLIST_EXISTSF #undef NVLIST_EXISTSF
@ -1095,12 +1143,12 @@ nvlist_existsv(const nvlist_t *nvl, const char *namefmt, va_list nameap)
char *name; char *name;
bool exists; bool exists;
vasprintf(&name, namefmt, nameap); nv_vasprintf(&name, namefmt, nameap);
if (name == NULL) if (name == NULL)
return (false); return (false);
exists = nvlist_exists(nvl, name); exists = nvlist_exists(nvl, name);
free(name); nv_free(name);
return (exists); return (exists);
} }
@ -1129,6 +1177,7 @@ NVLIST_EXISTSV(descriptor)
NVLIST_EXISTSV(binary) NVLIST_EXISTSV(binary)
#undef NVLIST_EXISTSV #undef NVLIST_EXISTSV
#endif
void void
nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp) nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
@ -1138,17 +1187,19 @@ nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
NVPAIR_ASSERT(nvp); NVPAIR_ASSERT(nvp);
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
if (nvlist_exists(nvl, nvpair_name(nvp))) { if (nvlist_exists(nvl, nvpair_name(nvp))) {
nvl->nvl_error = errno = EEXIST; nvl->nvl_error = EEXIST;
RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
newnvp = nvpair_clone(nvp); newnvp = nvpair_clone(nvp);
if (newnvp == NULL) { if (newnvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
@ -1200,14 +1251,15 @@ nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt,
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_create_stringv(name, valuefmt, valueap); nvp = nvpair_create_stringv(name, valuefmt, valueap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1218,12 +1270,14 @@ nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value)
nvlist_addf_nvlist(nvl, value, "%s", name); nvlist_addf_nvlist(nvl, value, "%s", name);
} }
#ifndef _KERNEL
void void
nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value) nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value)
{ {
nvlist_addf_descriptor(nvl, value, "%s", name); nvlist_addf_descriptor(nvl, value, "%s", name);
} }
#endif
void void
nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
@ -1284,6 +1338,7 @@ nvlist_addf_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt,
va_end(nameap); va_end(nameap);
} }
#ifndef _KERNEL
void void
nvlist_addf_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) nvlist_addf_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...)
{ {
@ -1293,6 +1348,7 @@ nvlist_addf_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...)
nvlist_addv_descriptor(nvl, value, namefmt, nameap); nvlist_addv_descriptor(nvl, value, namefmt, nameap);
va_end(nameap); va_end(nameap);
} }
#endif
void void
nvlist_addf_binary(nvlist_t *nvl, const void *value, size_t size, nvlist_addf_binary(nvlist_t *nvl, const void *value, size_t size,
@ -1311,14 +1367,15 @@ nvlist_addv_null(nvlist_t *nvl, const char *namefmt, va_list nameap)
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_createv_null(namefmt, nameap); nvp = nvpair_createv_null(namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1328,14 +1385,15 @@ nvlist_addv_bool(nvlist_t *nvl, bool value, const char *namefmt, va_list nameap)
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_createv_bool(value, namefmt, nameap); nvp = nvpair_createv_bool(value, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1346,14 +1404,15 @@ nvlist_addv_number(nvlist_t *nvl, uint64_t value, const char *namefmt,
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_createv_number(value, namefmt, nameap); nvp = nvpair_createv_number(value, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1364,14 +1423,15 @@ nvlist_addv_string(nvlist_t *nvl, const char *value, const char *namefmt,
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_createv_string(value, namefmt, nameap); nvp = nvpair_createv_string(value, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1382,17 +1442,19 @@ nvlist_addv_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt,
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_createv_nvlist(value, namefmt, nameap); nvp = nvpair_createv_nvlist(value, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
#ifndef _KERNEL
void void
nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt, nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt,
va_list nameap) va_list nameap)
@ -1410,6 +1472,7 @@ nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt,
else else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
#endif
void void
nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size, nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size,
@ -1418,14 +1481,15 @@ nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size,
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_createv_binary(value, size, namefmt, nameap); nvp = nvpair_createv_binary(value, size, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1438,12 +1502,13 @@ nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
nvpair_free(nvp); nvpair_free(nvp);
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
if (nvlist_exists(nvl, nvpair_name(nvp))) { if (nvlist_exists(nvl, nvpair_name(nvp))) {
nvpair_free(nvp); nvpair_free(nvp);
nvl->nvl_error = errno = EEXIST; nvl->nvl_error = EEXIST;
RESTORE_ERRNO(nvl->nvl_error);
return; return;
} }
@ -1460,7 +1525,9 @@ nvlist_move_##type(nvlist_t *nvl, const char *name, vtype value) \
NVLIST_MOVE(char *, string) NVLIST_MOVE(char *, string)
NVLIST_MOVE(nvlist_t *, nvlist) NVLIST_MOVE(nvlist_t *, nvlist)
#ifndef _KERNEL
NVLIST_MOVE(int, descriptor) NVLIST_MOVE(int, descriptor)
#endif
#undef NVLIST_MOVE #undef NVLIST_MOVE
@ -1485,7 +1552,9 @@ nvlist_movef_##type(nvlist_t *nvl, vtype value, const char *namefmt, \
NVLIST_MOVEF(char *, string) NVLIST_MOVEF(char *, string)
NVLIST_MOVEF(nvlist_t *, nvlist) NVLIST_MOVEF(nvlist_t *, nvlist)
#ifndef _KERNEL
NVLIST_MOVEF(int, descriptor) NVLIST_MOVEF(int, descriptor)
#endif
#undef NVLIST_MOVEF #undef NVLIST_MOVEF
@ -1507,15 +1576,16 @@ nvlist_movev_string(nvlist_t *nvl, char *value, const char *namefmt,
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
free(value); nv_free(value);
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_movev_string(value, namefmt, nameap); nvp = nvpair_movev_string(value, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1528,17 +1598,19 @@ nvlist_movev_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt,
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
if (value != NULL && nvlist_get_nvpair_parent(value) != NULL) if (value != NULL && nvlist_get_nvpair_parent(value) != NULL)
nvlist_destroy(value); nvlist_destroy(value);
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_movev_nvlist(value, namefmt, nameap); nvp = nvpair_movev_nvlist(value, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
#ifndef _KERNEL
void void
nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt, nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt,
va_list nameap) va_list nameap)
@ -1557,6 +1629,7 @@ nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt,
else else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
#endif
void void
nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size, nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size,
@ -1565,15 +1638,16 @@ nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size,
nvpair_t *nvp; nvpair_t *nvp;
if (nvlist_error(nvl) != 0) { if (nvlist_error(nvl) != 0) {
free(value); nv_free(value);
errno = nvlist_error(nvl); RESTORE_ERRNO(nvlist_error(nvl));
return; return;
} }
nvp = nvpair_movev_binary(value, size, namefmt, nameap); nvp = nvpair_movev_binary(value, size, namefmt, nameap);
if (nvp == NULL) if (nvp == NULL) {
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
else RESTORE_ERRNO(nvl->nvl_error);
} else
nvlist_move_nvpair(nvl, nvp); nvlist_move_nvpair(nvl, nvp);
} }
@ -1600,7 +1674,9 @@ NVLIST_GET(bool, bool, BOOL)
NVLIST_GET(uint64_t, number, NUMBER) NVLIST_GET(uint64_t, number, NUMBER)
NVLIST_GET(const char *, string, STRING) NVLIST_GET(const char *, string, STRING)
NVLIST_GET(const nvlist_t *, nvlist, NVLIST) NVLIST_GET(const nvlist_t *, nvlist, NVLIST)
#ifndef _KERNEL
NVLIST_GET(int, descriptor, DESCRIPTOR) NVLIST_GET(int, descriptor, DESCRIPTOR)
#endif
#undef NVLIST_GET #undef NVLIST_GET
@ -1630,6 +1706,7 @@ nvlist_getf_##type(const nvlist_t *nvl, const char *namefmt, ...) \
return (value); \ return (value); \
} }
#ifndef _KERNEL
NVLIST_GETF(bool, bool) NVLIST_GETF(bool, bool)
NVLIST_GETF(uint64_t, number) NVLIST_GETF(uint64_t, number)
NVLIST_GETF(const char *, string) NVLIST_GETF(const char *, string)
@ -1683,14 +1760,15 @@ nvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const char *namefmt,
char *name; char *name;
const void *binary; const void *binary;
vasprintf(&name, namefmt, nameap); nv_vasprintf(&name, namefmt, nameap);
if (name == NULL) if (name == NULL)
nvlist_report_missing(NV_TYPE_BINARY, "<unknown>"); nvlist_report_missing(NV_TYPE_BINARY, "<unknown>");
binary = nvlist_get_binary(nvl, name, sizep); binary = nvlist_get_binary(nvl, name, sizep);
free(name); nv_free(name);
return (binary); return (binary);
} }
#endif
#define NVLIST_TAKE(ftype, type, TYPE) \ #define NVLIST_TAKE(ftype, type, TYPE) \
ftype \ ftype \
@ -1712,7 +1790,9 @@ NVLIST_TAKE(bool, bool, BOOL)
NVLIST_TAKE(uint64_t, number, NUMBER) NVLIST_TAKE(uint64_t, number, NUMBER)
NVLIST_TAKE(char *, string, STRING) NVLIST_TAKE(char *, string, STRING)
NVLIST_TAKE(nvlist_t *, nvlist, NVLIST) NVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
#ifndef _KERNEL
NVLIST_TAKE(int, descriptor, DESCRIPTOR) NVLIST_TAKE(int, descriptor, DESCRIPTOR)
#endif
#undef NVLIST_TAKE #undef NVLIST_TAKE
@ -1746,6 +1826,7 @@ nvlist_takef_##type(nvlist_t *nvl, const char *namefmt, ...) \
return (value); \ return (value); \
} }
#ifndef _KERNEL
NVLIST_TAKEF(bool, bool) NVLIST_TAKEF(bool, bool)
NVLIST_TAKEF(uint64_t, number) NVLIST_TAKEF(uint64_t, number)
NVLIST_TAKEF(char *, string) NVLIST_TAKEF(char *, string)
@ -1797,14 +1878,15 @@ nvlist_takev_binary(nvlist_t *nvl, size_t *sizep, const char *namefmt,
char *name; char *name;
void *binary; void *binary;
vasprintf(&name, namefmt, nameap); nv_vasprintf(&name, namefmt, nameap);
if (name == NULL) if (name == NULL)
nvlist_report_missing(NV_TYPE_BINARY, "<unknown>"); nvlist_report_missing(NV_TYPE_BINARY, "<unknown>");
binary = nvlist_take_binary(nvl, name, sizep); binary = nvlist_take_binary(nvl, name, sizep);
free(name); nv_free(name);
return (binary); return (binary);
} }
#endif
void void
nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp) nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
@ -1837,11 +1919,14 @@ NVLIST_FREE(bool, BOOL)
NVLIST_FREE(number, NUMBER) NVLIST_FREE(number, NUMBER)
NVLIST_FREE(string, STRING) NVLIST_FREE(string, STRING)
NVLIST_FREE(nvlist, NVLIST) NVLIST_FREE(nvlist, NVLIST)
#ifndef _KERNEL
NVLIST_FREE(descriptor, DESCRIPTOR) NVLIST_FREE(descriptor, DESCRIPTOR)
#endif
NVLIST_FREE(binary, BINARY) NVLIST_FREE(binary, BINARY)
#undef NVLIST_FREE #undef NVLIST_FREE
#ifndef _KERNEL
void void
nvlist_freef(nvlist_t *nvl, const char *namefmt, ...) nvlist_freef(nvlist_t *nvl, const char *namefmt, ...)
{ {
@ -1901,6 +1986,7 @@ NVLIST_FREEV(nvlist, NVLIST)
NVLIST_FREEV(descriptor, DESCRIPTOR) NVLIST_FREEV(descriptor, DESCRIPTOR)
NVLIST_FREEV(binary, BINARY) NVLIST_FREEV(binary, BINARY)
#undef NVLIST_FREEV #undef NVLIST_FREEV
#endif
void void
nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp) nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp)

View File

@ -32,7 +32,9 @@
#ifndef _NVLIST_IMPL_H_ #ifndef _NVLIST_IMPL_H_
#define _NVLIST_IMPL_H_ #define _NVLIST_IMPL_H_
#ifndef _KERNEL
#include <stdint.h> #include <stdint.h>
#endif
#include "nv.h" #include "nv.h"

View File

@ -34,6 +34,16 @@ __FBSDID("$FreeBSD$");
#include <sys/endian.h> #include <sys/endian.h>
#include <sys/queue.h> #include <sys/queue.h>
#ifdef _KERNEL
#include <sys/errno.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/stdarg.h>
#else
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdarg.h> #include <stdarg.h>
@ -42,6 +52,7 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#endif
#ifdef HAVE_PJDLOG #ifdef HAVE_PJDLOG
#include <pjdlog.h> #include <pjdlog.h>
@ -54,11 +65,17 @@ __FBSDID("$FreeBSD$");
#include "nvpair_impl.h" #include "nvpair_impl.h"
#ifndef HAVE_PJDLOG #ifndef HAVE_PJDLOG
#ifdef _KERNEL
#define PJDLOG_ASSERT(...) MPASS(__VA_ARGS__)
#define PJDLOG_RASSERT(expr, ...) KASSERT(expr, (__VA_ARGS__))
#define PJDLOG_ABORT(...) panic(__VA_ARGS__)
#else
#include <assert.h> #include <assert.h>
#define PJDLOG_ASSERT(...) assert(__VA_ARGS__) #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
#define PJDLOG_RASSERT(expr, ...) assert(expr) #define PJDLOG_RASSERT(expr, ...) assert(expr)
#define PJDLOG_ABORT(...) abort() #define PJDLOG_ABORT(...) abort()
#endif #endif
#endif
#define NVPAIR_MAGIC 0x6e7670 /* "nvp" */ #define NVPAIR_MAGIC 0x6e7670 /* "nvp" */
struct nvpair { struct nvpair {
@ -184,10 +201,12 @@ nvpair_clone(const nvpair_t *nvp)
case NV_TYPE_NVLIST: case NV_TYPE_NVLIST:
newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp)); newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp));
break; break;
#ifndef _KERNEL
case NV_TYPE_DESCRIPTOR: case NV_TYPE_DESCRIPTOR:
newnvp = nvpair_create_descriptor(name, newnvp = nvpair_create_descriptor(name,
nvpair_get_descriptor(nvp)); nvpair_get_descriptor(nvp));
break; break;
#endif
case NV_TYPE_BINARY: case NV_TYPE_BINARY:
data = nvpair_get_binary(nvp, &datasize); data = nvpair_get_binary(nvp, &datasize);
newnvp = nvpair_create_binary(name, data, datasize); newnvp = nvpair_create_binary(name, data, datasize);
@ -327,6 +346,7 @@ nvpair_pack_nvlist_up(unsigned char *ptr, size_t *leftp)
return (ptr); return (ptr);
} }
#ifndef _KERNEL
unsigned char * unsigned char *
nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp, nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp,
size_t *leftp) size_t *leftp)
@ -356,6 +376,7 @@ nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp,
return (ptr); return (ptr);
} }
#endif
unsigned char * unsigned char *
nvpair_pack_binary(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp) nvpair_pack_binary(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
@ -446,7 +467,7 @@ nvpair_unpack_header(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
return (ptr); return (ptr);
failed: failed:
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -458,7 +479,7 @@ nvpair_unpack_null(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL); PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL);
if (nvp->nvp_datasize != 0) { if (nvp->nvp_datasize != 0) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -474,11 +495,11 @@ nvpair_unpack_bool(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL); PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL);
if (nvp->nvp_datasize != sizeof(value)) { if (nvp->nvp_datasize != sizeof(value)) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
if (*leftp < sizeof(value)) { if (*leftp < sizeof(value)) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -487,7 +508,7 @@ nvpair_unpack_bool(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
*leftp -= sizeof(value); *leftp -= sizeof(value);
if (value != 0 && value != 1) { if (value != 0 && value != 1) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -504,11 +525,11 @@ nvpair_unpack_number(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER); PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER);
if (nvp->nvp_datasize != sizeof(uint64_t)) { if (nvp->nvp_datasize != sizeof(uint64_t)) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
if (*leftp < sizeof(uint64_t)) { if (*leftp < sizeof(uint64_t)) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -530,17 +551,17 @@ nvpair_unpack_string(bool isbe __unused, nvpair_t *nvp,
PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING); PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) { if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
if (strnlen((const char *)ptr, nvp->nvp_datasize) != if (strnlen((const char *)ptr, nvp->nvp_datasize) !=
nvp->nvp_datasize - 1) { nvp->nvp_datasize - 1) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
nvp->nvp_data = (uint64_t)(uintptr_t)strdup((const char *)ptr); nvp->nvp_data = (uint64_t)(uintptr_t)nv_strdup((const char *)ptr);
if (nvp->nvp_data == 0) if (nvp->nvp_data == 0)
return (NULL); return (NULL);
@ -559,7 +580,7 @@ nvpair_unpack_nvlist(bool isbe __unused, nvpair_t *nvp,
PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST); PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) { if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -577,6 +598,7 @@ nvpair_unpack_nvlist(bool isbe __unused, nvpair_t *nvp,
return (ptr); return (ptr);
} }
#ifndef _KERNEL
const unsigned char * const unsigned char *
nvpair_unpack_descriptor(bool isbe, nvpair_t *nvp, const unsigned char *ptr, nvpair_unpack_descriptor(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
size_t *leftp, const int *fds, size_t nfds) size_t *leftp, const int *fds, size_t nfds)
@ -616,6 +638,7 @@ nvpair_unpack_descriptor(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
return (ptr); return (ptr);
} }
#endif
const unsigned char * const unsigned char *
nvpair_unpack_binary(bool isbe __unused, nvpair_t *nvp, nvpair_unpack_binary(bool isbe __unused, nvpair_t *nvp,
@ -626,11 +649,11 @@ nvpair_unpack_binary(bool isbe __unused, nvpair_t *nvp,
PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY); PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) { if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
value = malloc(nvp->nvp_datasize); value = nv_malloc(nvp->nvp_datasize);
if (value == NULL) if (value == NULL)
return (NULL); return (NULL);
@ -649,7 +672,7 @@ nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
{ {
nvpair_t *nvp, *tmp; nvpair_t *nvp, *tmp;
nvp = calloc(1, sizeof(*nvp) + NV_NAME_MAX); nvp = nv_calloc(1, sizeof(*nvp) + NV_NAME_MAX);
if (nvp == NULL) if (nvp == NULL)
return (NULL); return (NULL);
nvp->nvp_name = (char *)(nvp + 1); nvp->nvp_name = (char *)(nvp + 1);
@ -657,7 +680,7 @@ nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
ptr = nvpair_unpack_header(isbe, nvp, ptr, leftp); ptr = nvpair_unpack_header(isbe, nvp, ptr, leftp);
if (ptr == NULL) if (ptr == NULL)
goto failed; goto failed;
tmp = realloc(nvp, sizeof(*nvp) + strlen(nvp->nvp_name) + 1); tmp = nv_realloc(nvp, sizeof(*nvp) + strlen(nvp->nvp_name) + 1);
if (tmp == NULL) if (tmp == NULL)
goto failed; goto failed;
nvp = tmp; nvp = tmp;
@ -669,7 +692,7 @@ nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
*nvpp = nvp; *nvpp = nvp;
return (ptr); return (ptr);
failed: failed:
free(nvp); nv_free(nvp);
return (NULL); return (NULL);
} }
@ -701,18 +724,18 @@ nvpair_allocv(int type, uint64_t data, size_t datasize, const char *namefmt,
PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST); PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST);
namelen = vasprintf(&name, namefmt, nameap); namelen = nv_vasprintf(&name, namefmt, nameap);
if (namelen < 0) if (namelen < 0)
return (NULL); return (NULL);
PJDLOG_ASSERT(namelen > 0); PJDLOG_ASSERT(namelen > 0);
if (namelen >= NV_NAME_MAX) { if (namelen >= NV_NAME_MAX) {
free(name); nv_free(name);
errno = ENAMETOOLONG; RESTORE_ERRNO(ENAMETOOLONG);
return (NULL); return (NULL);
} }
nvp = calloc(1, sizeof(*nvp) + namelen + 1); nvp = nv_calloc(1, sizeof(*nvp) + namelen + 1);
if (nvp != NULL) { if (nvp != NULL) {
nvp->nvp_name = (char *)(nvp + 1); nvp->nvp_name = (char *)(nvp + 1);
memcpy(nvp->nvp_name, name, namelen + 1); memcpy(nvp->nvp_name, name, namelen + 1);
@ -721,7 +744,7 @@ nvpair_allocv(int type, uint64_t data, size_t datasize, const char *namefmt,
nvp->nvp_datasize = datasize; nvp->nvp_datasize = datasize;
nvp->nvp_magic = NVPAIR_MAGIC; nvp->nvp_magic = NVPAIR_MAGIC;
} }
free(name); nv_free(name);
return (nvp); return (nvp);
}; };
@ -774,12 +797,12 @@ nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap)
char *str; char *str;
int len; int len;
len = vasprintf(&str, valuefmt, valueap); len = nv_vasprintf(&str, valuefmt, valueap);
if (len < 0) if (len < 0)
return (NULL); return (NULL);
nvp = nvpair_create_string(name, str); nvp = nvpair_create_string(name, str);
if (nvp == NULL) if (nvp == NULL)
free(str); nv_free(str);
return (nvp); return (nvp);
} }
@ -790,12 +813,14 @@ nvpair_create_nvlist(const char *name, const nvlist_t *value)
return (nvpair_createf_nvlist(value, "%s", name)); return (nvpair_createf_nvlist(value, "%s", name));
} }
#ifndef _KERNEL
nvpair_t * nvpair_t *
nvpair_create_descriptor(const char *name, int value) nvpair_create_descriptor(const char *name, int value)
{ {
return (nvpair_createf_descriptor(value, "%s", name)); return (nvpair_createf_descriptor(value, "%s", name));
} }
#endif
nvpair_t * nvpair_t *
nvpair_create_binary(const char *name, const void *value, size_t size) nvpair_create_binary(const char *name, const void *value, size_t size)
@ -869,6 +894,7 @@ nvpair_createf_nvlist(const nvlist_t *value, const char *namefmt, ...)
return (nvp); return (nvp);
} }
#ifndef _KERNEL
nvpair_t * nvpair_t *
nvpair_createf_descriptor(int value, const char *namefmt, ...) nvpair_createf_descriptor(int value, const char *namefmt, ...)
{ {
@ -881,6 +907,7 @@ nvpair_createf_descriptor(int value, const char *namefmt, ...)
return (nvp); return (nvp);
} }
#endif
nvpair_t * nvpair_t *
nvpair_createf_binary(const void *value, size_t size, const char *namefmt, ...) nvpair_createf_binary(const void *value, size_t size, const char *namefmt, ...)
@ -926,11 +953,11 @@ nvpair_createv_string(const char *value, const char *namefmt, va_list nameap)
char *data; char *data;
if (value == NULL) { if (value == NULL) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
data = strdup(value); data = nv_strdup(value);
if (data == NULL) if (data == NULL)
return (NULL); return (NULL);
size = strlen(value) + 1; size = strlen(value) + 1;
@ -938,7 +965,7 @@ nvpair_createv_string(const char *value, const char *namefmt, va_list nameap)
nvp = nvpair_allocv(NV_TYPE_STRING, (uint64_t)(uintptr_t)data, size, nvp = nvpair_allocv(NV_TYPE_STRING, (uint64_t)(uintptr_t)data, size,
namefmt, nameap); namefmt, nameap);
if (nvp == NULL) if (nvp == NULL)
free(data); nv_free(data);
return (nvp); return (nvp);
} }
@ -951,7 +978,7 @@ nvpair_createv_nvlist(const nvlist_t *value, const char *namefmt,
nvpair_t *nvp; nvpair_t *nvp;
if (value == NULL) { if (value == NULL) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
@ -969,6 +996,7 @@ nvpair_createv_nvlist(const nvlist_t *value, const char *namefmt,
return (nvp); return (nvp);
} }
#ifndef _KERNEL
nvpair_t * nvpair_t *
nvpair_createv_descriptor(int value, const char *namefmt, va_list nameap) nvpair_createv_descriptor(int value, const char *namefmt, va_list nameap)
{ {
@ -990,6 +1018,7 @@ nvpair_createv_descriptor(int value, const char *namefmt, va_list nameap)
return (nvp); return (nvp);
} }
#endif
nvpair_t * nvpair_t *
nvpair_createv_binary(const void *value, size_t size, const char *namefmt, nvpair_createv_binary(const void *value, size_t size, const char *namefmt,
@ -999,11 +1028,11 @@ nvpair_createv_binary(const void *value, size_t size, const char *namefmt,
void *data; void *data;
if (value == NULL || size == 0) { if (value == NULL || size == 0) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
data = malloc(size); data = nv_malloc(size);
if (data == NULL) if (data == NULL)
return (NULL); return (NULL);
memcpy(data, value, size); memcpy(data, value, size);
@ -1011,7 +1040,7 @@ nvpair_createv_binary(const void *value, size_t size, const char *namefmt,
nvp = nvpair_allocv(NV_TYPE_BINARY, (uint64_t)(uintptr_t)data, size, nvp = nvpair_allocv(NV_TYPE_BINARY, (uint64_t)(uintptr_t)data, size,
namefmt, nameap); namefmt, nameap);
if (nvp == NULL) if (nvp == NULL)
free(data); nv_free(data);
return (nvp); return (nvp);
} }
@ -1030,12 +1059,14 @@ nvpair_move_nvlist(const char *name, nvlist_t *value)
return (nvpair_movef_nvlist(value, "%s", name)); return (nvpair_movef_nvlist(value, "%s", name));
} }
#ifndef _KERNEL
nvpair_t * nvpair_t *
nvpair_move_descriptor(const char *name, int value) nvpair_move_descriptor(const char *name, int value)
{ {
return (nvpair_movef_descriptor(value, "%s", name)); return (nvpair_movef_descriptor(value, "%s", name));
} }
#endif
nvpair_t * nvpair_t *
nvpair_move_binary(const char *name, void *value, size_t size) nvpair_move_binary(const char *name, void *value, size_t size)
@ -1070,6 +1101,7 @@ nvpair_movef_nvlist(nvlist_t *value, const char *namefmt, ...)
return (nvp); return (nvp);
} }
#ifndef _KERNEL
nvpair_t * nvpair_t *
nvpair_movef_descriptor(int value, const char *namefmt, ...) nvpair_movef_descriptor(int value, const char *namefmt, ...)
{ {
@ -1082,6 +1114,7 @@ nvpair_movef_descriptor(int value, const char *namefmt, ...)
return (nvp); return (nvp);
} }
#endif
nvpair_t * nvpair_t *
nvpair_movef_binary(void *value, size_t size, const char *namefmt, ...) nvpair_movef_binary(void *value, size_t size, const char *namefmt, ...)
@ -1103,16 +1136,16 @@ nvpair_movev_string(char *value, const char *namefmt, va_list nameap)
int serrno; int serrno;
if (value == NULL) { if (value == NULL) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
nvp = nvpair_allocv(NV_TYPE_STRING, (uint64_t)(uintptr_t)value, nvp = nvpair_allocv(NV_TYPE_STRING, (uint64_t)(uintptr_t)value,
strlen(value) + 1, namefmt, nameap); strlen(value) + 1, namefmt, nameap);
if (nvp == NULL) { if (nvp == NULL) {
serrno = errno; SAVE_ERRNO(serrno);
free(value); nv_free(value);
errno = serrno; RESTORE_ERRNO(serrno);
} }
return (nvp); return (nvp);
@ -1124,12 +1157,12 @@ nvpair_movev_nvlist(nvlist_t *value, const char *namefmt, va_list nameap)
nvpair_t *nvp; nvpair_t *nvp;
if (value == NULL || nvlist_get_nvpair_parent(value) != NULL) { if (value == NULL || nvlist_get_nvpair_parent(value) != NULL) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
if (nvlist_error(value) != 0) { if (nvlist_error(value) != 0) {
errno = nvlist_error(value); RESTORE_ERRNO(nvlist_error(value));
nvlist_destroy(value); nvlist_destroy(value);
return (NULL); return (NULL);
} }
@ -1144,6 +1177,7 @@ nvpair_movev_nvlist(nvlist_t *value, const char *namefmt, va_list nameap)
return (nvp); return (nvp);
} }
#ifndef _KERNEL
nvpair_t * nvpair_t *
nvpair_movev_descriptor(int value, const char *namefmt, va_list nameap) nvpair_movev_descriptor(int value, const char *namefmt, va_list nameap)
{ {
@ -1165,6 +1199,7 @@ nvpair_movev_descriptor(int value, const char *namefmt, va_list nameap)
return (nvp); return (nvp);
} }
#endif
nvpair_t * nvpair_t *
nvpair_movev_binary(void *value, size_t size, const char *namefmt, nvpair_movev_binary(void *value, size_t size, const char *namefmt,
@ -1174,16 +1209,16 @@ nvpair_movev_binary(void *value, size_t size, const char *namefmt,
int serrno; int serrno;
if (value == NULL || size == 0) { if (value == NULL || size == 0) {
errno = EINVAL; RESTORE_ERRNO(EINVAL);
return (NULL); return (NULL);
} }
nvp = nvpair_allocv(NV_TYPE_BINARY, (uint64_t)(uintptr_t)value, size, nvp = nvpair_allocv(NV_TYPE_BINARY, (uint64_t)(uintptr_t)value, size,
namefmt, nameap); namefmt, nameap);
if (nvp == NULL) { if (nvp == NULL) {
serrno = errno; SAVE_ERRNO(serrno);
free(value); nv_free(value);
errno = serrno; RESTORE_ERRNO(serrno);
} }
return (nvp); return (nvp);
@ -1227,6 +1262,7 @@ nvpair_get_nvlist(const nvpair_t *nvp)
return ((const nvlist_t *)(intptr_t)nvp->nvp_data); return ((const nvlist_t *)(intptr_t)nvp->nvp_data);
} }
#ifndef _KERNEL
int int
nvpair_get_descriptor(const nvpair_t *nvp) nvpair_get_descriptor(const nvpair_t *nvp)
{ {
@ -1236,6 +1272,7 @@ nvpair_get_descriptor(const nvpair_t *nvp)
return ((int)nvp->nvp_data); return ((int)nvp->nvp_data);
} }
#endif
const void * const void *
nvpair_get_binary(const nvpair_t *nvp, size_t *sizep) nvpair_get_binary(const nvpair_t *nvp, size_t *sizep)
@ -1258,20 +1295,22 @@ nvpair_free(nvpair_t *nvp)
nvp->nvp_magic = 0; nvp->nvp_magic = 0;
switch (nvp->nvp_type) { switch (nvp->nvp_type) {
#ifndef _KERNEL
case NV_TYPE_DESCRIPTOR: case NV_TYPE_DESCRIPTOR:
close((int)nvp->nvp_data); close((int)nvp->nvp_data);
break; break;
#endif
case NV_TYPE_NVLIST: case NV_TYPE_NVLIST:
nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data); nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data);
break; break;
case NV_TYPE_STRING: case NV_TYPE_STRING:
free((char *)(intptr_t)nvp->nvp_data); nv_free((char *)(intptr_t)nvp->nvp_data);
break; break;
case NV_TYPE_BINARY: case NV_TYPE_BINARY:
free((void *)(intptr_t)nvp->nvp_data); nv_free((void *)(intptr_t)nvp->nvp_data);
break; break;
} }
free(nvp); nv_free(nvp);
} }
void void
@ -1282,7 +1321,7 @@ nvpair_free_structure(nvpair_t *nvp)
PJDLOG_ASSERT(nvp->nvp_list == NULL); PJDLOG_ASSERT(nvp->nvp_list == NULL);
nvp->nvp_magic = 0; nvp->nvp_magic = 0;
free(nvp); nv_free(nvp);
} }
const char * const char *

View File

@ -34,7 +34,9 @@
#include <sys/queue.h> #include <sys/queue.h>
#ifndef _KERNEL
#include <stdint.h> #include <stdint.h>
#endif
#include "nv.h" #include "nv.h"