From c3e30da055fedfa49108cf74b1d1272f710276b9 Mon Sep 17 00:00:00 2001 From: Ryan Stone Date: Sat, 17 May 2014 21:45:06 -0400 Subject: [PATCH 16/17] 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. --- lib/libnv/dnv.h | 6 ++ lib/libnv/dnvlist.c | 24 +++++- lib/libnv/nv.h | 37 +++++++++ lib/libnv/nv_impl.h | 31 +++++++ lib/libnv/nvlist.c | 199 +++++++++++++++++++++++++++------------------ lib/libnv/nvlist_getters.h | 24 +++--- lib/libnv/nvlist_impl.h | 2 + lib/libnv/nvpair.c | 105 +++++++++++++++--------- lib/libnv/nvpair_impl.h | 2 + 9 files changed, 293 insertions(+), 137 deletions(-) diff --git a/lib/libnv/dnv.h b/lib/libnv/dnv.h index dd1d831..8337d78 100644 --- a/lib/libnv/dnv.h +++ b/lib/libnv/dnv.h @@ -34,9 +34,11 @@ #include +#ifndef _KERNEL #include #include #include +#endif #ifndef _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); 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); 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); @@ -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); 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); +#endif /* * 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); 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); 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); @@ -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); 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); +#endif __END_DECLS diff --git a/lib/libnv/dnvlist.c b/lib/libnv/dnvlist.c index 98a1aa6..ada3526 100644 --- a/lib/libnv/dnvlist.c +++ b/lib/libnv/dnvlist.c @@ -30,10 +30,22 @@ #include __FBSDID("$FreeBSD$"); +#ifdef _KERNEL + +#include +#include +#include +#include +#include + +#include + +#else #include #include #include #include +#endif #include "nv.h" #include "nv_impl.h" @@ -62,6 +74,7 @@ dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep, return (value); } +#ifndef _KERNEL DNVLIST_GETF(bool, bool) DNVLIST_GETF(uint64_t, number) DNVLIST_GETF(const char *, string) @@ -94,10 +107,10 @@ dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval, char *name; const void *value; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name != NULL) { value = dnvlist_get_binary(nvl, name, sizep, defval, defsize); - free(name); + nv_free(name); } else { if (sizep != NULL) *sizep = defsize; @@ -105,6 +118,7 @@ dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval, } return (value); } +#endif DNVLIST_TAKE(bool, bool) DNVLIST_TAKE(uint64_t, number) @@ -127,6 +141,7 @@ dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep, return (value); } +#ifndef _KERNEL DNVLIST_TAKEF(bool, bool) DNVLIST_TAKEF(uint64_t, number) DNVLIST_TAKEF(char *, string) @@ -159,10 +174,10 @@ dnvlist_takev_binary(nvlist_t *nvl, size_t *sizep, void *defval, char *name; void *value; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name != NULL) { value = dnvlist_take_binary(nvl, name, sizep, defval, defsize); - free(name); + nv_free(name); } else { if (sizep != NULL) *sizep = defsize; @@ -171,3 +186,4 @@ dnvlist_takev_binary(nvlist_t *nvl, size_t *sizep, void *defval, return (value); } +#endif diff --git a/lib/libnv/nv.h b/lib/libnv/nv.h index f00a925..0ce2c96 100644 --- a/lib/libnv/nv.h +++ b/lib/libnv/nv.h @@ -34,10 +34,12 @@ #include +#ifndef _KERNEL #include #include #include #include +#endif #ifndef _NVLIST_T_DECLARED #define _NVLIST_T_DECLARED @@ -64,7 +66,12 @@ typedef struct nvlist nvlist_t; */ #define NV_FLAG_IGNORE_CASE 0x01 +#if defined(_KERNEL) && defined(MALLOC_DECLARE) +MALLOC_DECLARE(M_NVLIST); +#endif + __BEGIN_DECLS + nvlist_t *nvlist_create(int flags); void nvlist_destroy(nvlist_t *nvl); int nvlist_error(const nvlist_t *nvl); @@ -73,8 +80,10 @@ void nvlist_set_error(nvlist_t *nvl, int error); nvlist_t *nvlist_clone(const nvlist_t *nvl); +#ifndef _KERNEL void nvlist_dump(const nvlist_t *nvl, int fd); void nvlist_fdump(const nvlist_t *nvl, FILE *fp); +#endif size_t nvlist_size(const nvlist_t *nvl); void *nvlist_pack(const nvlist_t *nvl, size_t *sizep); @@ -99,7 +108,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_string(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); +#endif bool nvlist_exists_binary(const nvlist_t *nvl, const char *name); /* @@ -115,7 +126,9 @@ 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_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0); 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); +#endif void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size); /* @@ -125,7 +138,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_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value); +#ifndef _KERNEL 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); /* @@ -138,7 +153,9 @@ bool nvlist_get_bool(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 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); +#endif const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep); /* @@ -151,7 +168,9 @@ bool nvlist_take_bool(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); nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name); +#ifndef _KERNEL int nvlist_take_descriptor(nvlist_t *nvl, const char *name); +#endif void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep); /* @@ -167,14 +186,21 @@ void nvlist_free_bool(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_nvlist(nvlist_t *nvl, const char *name); +#ifndef _KERNEL void nvlist_free_descriptor(nvlist_t *nvl, const char *name); +#endif void nvlist_free_binary(nvlist_t *nvl, const char *name); /* * Below are the same functions, but which operate on format strings and * 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_type(const nvlist_t *nvl, int type, const char *namefmt, ...) __printflike(3, 4); @@ -196,13 +222,16 @@ 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_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); +#endif 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_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_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); +#endif void nvlist_addf_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, ...) __printflike(4, 5); void nvlist_addv_null(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0); @@ -210,19 +239,26 @@ void nvlist_addv_bool(nvlist_t *nvl, bool value, const char *namefmt, va_list na 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_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); +#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_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); +#ifndef _KERNEL 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_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); +#ifndef _KERNEL 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); +#ifndef _KERNEL 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); const char *nvlist_getf_string(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3); @@ -272,6 +308,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_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); +#endif /* _KERNEL */ __END_DECLS diff --git a/lib/libnv/nv_impl.h b/lib/libnv/nv_impl.h index b416b37..e699974 100644 --- a/lib/libnv/nv_impl.h +++ b/lib/libnv/nv_impl.h @@ -44,6 +44,37 @@ typedef struct nvpair nvpair_t; #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); size_t nvlist_ndescriptors(const nvlist_t *nvl); diff --git a/lib/libnv/nvlist.c b/lib/libnv/nvlist.c index ed7ebd9..9a15659 100644 --- a/lib/libnv/nvlist.c +++ b/lib/libnv/nvlist.c @@ -33,17 +33,22 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include +#ifdef _KERNEL + +#include +#include +#include +#include +#include + +#include + +#else #include -#include -#include -#include -#define _WITH_DPRINTF -#include #include #include -#include +#endif #ifdef HAVE_PJDLOG #include @@ -56,6 +61,11 @@ __FBSDID("$FreeBSD$"); #include "nvpair_impl.h" #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 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__) #define PJDLOG_RASSERT(expr, ...) assert(expr) @@ -66,6 +76,7 @@ __FBSDID("$FreeBSD$"); abort(); \ } while (0) #endif +#endif #define NV_FLAG_PRIVATE_MASK (NV_FLAG_BIG_ENDIAN) #define NV_FLAG_PUBLIC_MASK (NV_FLAG_IGNORE_CASE) @@ -76,6 +87,9 @@ __FBSDID("$FreeBSD$"); PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC); \ } while (0) +#ifdef _KERNEL +MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist"); +#endif nvlist_t * nvlist_create(int flags) @@ -84,7 +98,7 @@ nvlist_create(int flags) PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0); - nvl = malloc(sizeof(*nvl)); + nvl = nv_malloc(sizeof(*nvl)); nvl->nvl_error = 0; nvl->nvl_flags = flags; nvl->nvl_depth = 0; @@ -103,7 +117,7 @@ nvlist_destroy(nvlist_t *nvl) if (nvl == NULL) return; - serrno = errno; + SAVE_ERRNO(serrno); NVLIST_ASSERT(nvl); @@ -112,9 +126,9 @@ nvlist_destroy(nvlist_t *nvl) nvpair_free(nvp); } nvl->nvl_magic = 0; - free(nvl); + nv_free(nvl); - errno = serrno; + RESTORE_ERRNO(serrno); } void @@ -180,7 +194,7 @@ nvlist_find(const nvlist_t *nvl, int type, const char *name) } if (nvp == NULL) - errno = ENOENT; + RESTORE_ERRNO(ENOENT); return (nvp); } @@ -217,6 +231,7 @@ nvlist_exists_type(const nvlist_t *nvl, const char *name, int type) return (nvlist_find(nvl, type, name) != NULL); } +#ifndef _KERNEL bool nvlist_existsf_type(const nvlist_t *nvl, int type, const char *namefmt, ...) { @@ -237,14 +252,15 @@ nvlist_existsv_type(const nvlist_t *nvl, int type, const char *namefmt, char *name; bool exists; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name == NULL) return (0); exists = nvlist_exists_type(nvl, name, type); - free(name); + nv_free(name); return (exists); } +#endif void nvlist_free_type(nvlist_t *nvl, const char *name, int type) @@ -263,6 +279,7 @@ nvlist_free_type(nvlist_t *nvl, const char *name, int type) nvlist_report_missing(type, name); } +#ifndef _KERNEL void nvlist_freef_type(nvlist_t *nvl, int type, const char *namefmt, ...) { @@ -278,12 +295,13 @@ nvlist_freev_type(nvlist_t *nvl, int type, const char *namefmt, va_list nameap) { char *name; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name == NULL) nvlist_report_missing(type, ""); nvlist_free_type(nvl, name, type); - free(name); + nv_free(name); } +#endif nvlist_t * nvlist_clone(const nvlist_t *nvl) @@ -294,7 +312,7 @@ nvlist_clone(const nvlist_t *nvl) NVLIST_ASSERT(nvl); if (nvl->nvl_error != 0) { - errno = nvl->nvl_error; + RESTORE_ERRNO(nvl->nvl_error); return (NULL); } @@ -362,10 +380,12 @@ nvlist_xdescriptors(const nvlist_t *nvl, int *descs, int level) for (nvp = nvlist_first_nvpair(nvl); nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { switch (nvpair_type(nvp)) { +#ifndef _KERNEL case NV_TYPE_DESCRIPTOR: *descs = nvpair_get_descriptor(nvp); descs++; break; +#endif case NV_TYPE_NVLIST: descs = nvlist_xdescriptors(nvpair_get_nvlist(nvp), descs, level + 1); @@ -383,7 +403,7 @@ nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp) int *fds; nitems = nvlist_ndescriptors(nvl); - fds = malloc(sizeof(fds[0]) * (nitems + 1)); + fds = nv_malloc(sizeof(fds[0]) * (nitems + 1)); if (fds == NULL) return (NULL); if (nitems > 0) @@ -461,12 +481,12 @@ nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep) NVLIST_ASSERT(nvl); if (nvl->nvl_error != 0) { - errno = nvl->nvl_error; + RESTORE_ERRNO(nvl->nvl_error); return (NULL); } size = nvlist_size(nvl); - buf = malloc(size); + buf = nv_malloc(size); if (buf == NULL) return (NULL); @@ -479,7 +499,7 @@ nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep) nvp = nvlist_next_nvpair(nvl, nvp)) { ptr = nvpair_pack(nvp, ptr, fdidxp, &left); if (ptr == NULL) { - free(buf); + nv_free(buf); return (NULL); } } @@ -496,12 +516,12 @@ nvlist_pack(const nvlist_t *nvl, size_t *sizep) NVLIST_ASSERT(nvl); if (nvl->nvl_error != 0) { - errno = nvl->nvl_error; + RESTORE_ERRNO(nvl->nvl_error); return (NULL); } if (nvlist_ndescriptors(nvl) > 0) { - errno = EOPNOTSUPP; + RESTORE_ERRNO(EOPNOTSUPP); return (NULL); } @@ -513,11 +533,11 @@ nvlist_check_header(struct nvlist_header *nvlhdrp) { if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (false); } if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (false); } #if BYTE_ORDER == BIG_ENDIAN @@ -568,7 +588,7 @@ nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds, return (ptr); failed: - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -690,6 +710,7 @@ NVLIST_EXISTS(string, STRING) NVLIST_EXISTS(nvlist, NVLIST) NVLIST_EXISTS(binary, BINARY) +#ifndef _KERNEL bool nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...) { @@ -715,12 +736,12 @@ nvlist_existsv(const nvlist_t *nvl, const char *namefmt, va_list nameap) char *name; bool exists; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name == NULL) return (0); exists = nvlist_exists(nvl, name); - free(name); + nv_free(name); return (exists); } @@ -730,6 +751,7 @@ NVLIST_EXISTSV(number) NVLIST_EXISTSV(string) NVLIST_EXISTSV(nvlist) NVLIST_EXISTSV(binary) +#endif void nvlist_add_null(nvlist_t *nvl, const char *name) @@ -776,14 +798,15 @@ nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_create_stringv(name, valuefmt, valueap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -870,14 +893,15 @@ nvlist_addv_null(nvlist_t *nvl, const char *namefmt, va_list nameap) nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_createv_null(namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -887,14 +911,15 @@ nvlist_addv_bool(nvlist_t *nvl, bool value, const char *namefmt, va_list nameap) nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_createv_bool(value, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -905,14 +930,15 @@ nvlist_addv_number(nvlist_t *nvl, uint64_t value, const char *namefmt, nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_createv_number(value, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -923,14 +949,15 @@ nvlist_addv_string(nvlist_t *nvl, const char *value, const char *namefmt, nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_createv_string(value, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -941,18 +968,18 @@ nvlist_addv_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_createv_nvlist(value, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } - void nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, va_list nameap) @@ -960,14 +987,15 @@ nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size, nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_createv_binary(value, size, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -982,12 +1010,13 @@ nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp) if (nvlist_error(nvl) != 0) { nvpair_free(nvp); - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } if (nvlist_exists(nvl, nvpair_name(nvp))) { nvpair_free(nvp); - nvl->nvl_error = errno = EEXIST; + nvl->nvl_error = EEXIST; + RESTORE_ERRNO(nvl->nvl_error); return; } if (nvp->nvp_type == NV_TYPE_NVLIST) { @@ -996,7 +1025,7 @@ nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp) if (depth > NVLIST_MAX_LEVEL) { nvpair_free(nvp); nvl->nvl_error = EINVAL; - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return; } @@ -1038,15 +1067,16 @@ nvlist_movev_string(nvlist_t *nvl, char *value, const char *namefmt, nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - free(value); - errno = nvlist_error(nvl); + nv_free(value); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_movev_string(value, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -1058,18 +1088,18 @@ nvlist_movev_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, if (nvlist_error(nvl) != 0) { nvlist_destroy(value); - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_movev_nvlist(value, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } - void nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, va_list nameap) @@ -1077,15 +1107,16 @@ nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size, nvpair_t *nvp; if (nvlist_error(nvl) != 0) { - free(value); - errno = nvlist_error(nvl); + nv_free(value); + RESTORE_ERRNO(nvlist_error(nvl)); return; } nvp = nvpair_movev_binary(value, size, namefmt, nameap); - if (nvp == NULL) - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); - else + if (nvp == NULL) { + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvl->nvl_error); + } else nvlist_move_nvpair(nvl, nvp); } @@ -1113,6 +1144,7 @@ nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep) return (nvpair_get_binary(nvp, sizep)); } +#ifndef _KERNEL NVLIST_GETF(bool, bool) NVLIST_GETF(uint64_t, number) NVLIST_GETF(const char *, string) @@ -1143,14 +1175,15 @@ nvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const char *namefmt, char *name; const void *binary; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name == NULL) nvlist_report_missing(NV_TYPE_BINARY, ""); binary = nvlist_get_binary(nvl, name, sizep); - free(name); + nv_free(name); return (binary); } +#endif nvpair_t * nvlist_take_nvpair(nvlist_t *nvl, const char *name) @@ -1198,6 +1231,7 @@ nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep) return (value); } +#ifndef _KERNEL NVLIST_TAKEF(bool, bool) NVLIST_TAKEF(uint64_t, number) NVLIST_TAKEF(char *, string) @@ -1228,14 +1262,15 @@ nvlist_takev_binary(nvlist_t *nvl, size_t *sizep, const char *namefmt, char *name; void *binary; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name == NULL) nvlist_report_missing(NV_TYPE_BINARY, ""); binary = nvlist_take_binary(nvl, name, sizep); - free(name); + nv_free(name); return (binary); } +#endif void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp) @@ -1268,6 +1303,7 @@ NVLIST_FREE(string, STRING) NVLIST_FREE(nvlist, NVLIST) NVLIST_FREE(binary, BINARY) +#ifndef _KERNEL void nvlist_freef(nvlist_t *nvl, const char *namefmt, ...) { @@ -1290,11 +1326,11 @@ nvlist_freev(nvlist_t *nvl, const char *namefmt, va_list nameap) { char *name; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name == NULL) nvlist_report_missing(NV_TYPE_NONE, ""); nvlist_free(nvl, name); - free(name); + nv_free(name); } NVLIST_FREEV(null, NULL) @@ -1303,6 +1339,7 @@ NVLIST_FREEV(number, NUMBER) NVLIST_FREEV(string, STRING) NVLIST_FREEV(nvlist, NVLIST) NVLIST_FREEV(binary, BINARY) +#endif void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp) diff --git a/lib/libnv/nvlist_getters.h b/lib/libnv/nvlist_getters.h index d40e933..cf6da0a 100644 --- a/lib/libnv/nvlist_getters.h +++ b/lib/libnv/nvlist_getters.h @@ -69,11 +69,11 @@ dnvlist_getv_##type(const nvlist_t *nvl, ftype defval, \ char *name; \ ftype value; \ \ - vasprintf(&name, namefmt, nameap); \ + nv_vasprintf(&name, namefmt, nameap); \ if (name == NULL) \ return (defval); \ value = dnvlist_get_##type(nvl, name, defval); \ - free(name); \ + nv_free(name); \ return (value); \ } @@ -111,11 +111,11 @@ dnvlist_takev_##type(nvlist_t *nvl, ftype defval, const char *namefmt, \ char *name; \ ftype value; \ \ - vasprintf(&name, namefmt, nameap); \ + nv_vasprintf(&name, namefmt, nameap); \ if (name == NULL) \ return (defval); \ value = dnvlist_take_##type(nvl, name, defval); \ - free(name); \ + nv_free(name); \ return (value); \ } @@ -148,11 +148,11 @@ nvlist_existsv_##type(const nvlist_t *nvl, const char *namefmt, \ char *name; \ bool exists; \ \ - vasprintf(&name, namefmt, nameap); \ + nv_vasprintf(&name, namefmt, nameap); \ if (name == NULL) \ return (0); \ exists = nvlist_exists_##type(nvl, name); \ - free(name); \ + nv_free(name); \ return (exists); \ } @@ -210,11 +210,11 @@ nvlist_getv_##type(const nvlist_t *nvl, const char *namefmt, \ char *name; \ ftype value; \ \ - vasprintf(&name, namefmt, nameap); \ + nv_vasprintf(&name, namefmt, nameap); \ if (name == NULL) \ nvlist_report_missing(NV_TYPE_##TYPE, ""); \ value = nvlist_get_##type(nvl, name); \ - free(name); \ + nv_free(name); \ \ return (value); \ } @@ -256,11 +256,11 @@ nvlist_takev_##type(nvlist_t *nvl, const char *namefmt, va_list nameap) \ char *name; \ ftype value; \ \ - vasprintf(&name, namefmt, nameap); \ + nv_vasprintf(&name, namefmt, nameap); \ if (name == NULL) \ nvlist_report_missing(NV_TYPE_##TYPE, ""); \ value = nvlist_take_##type(nvl, name); \ - free(name); \ + nv_free(name); \ return (value); \ } @@ -289,11 +289,11 @@ nvlist_freev_##type(nvlist_t *nvl, const char *namefmt, va_list nameap) \ { \ char *name; \ \ - vasprintf(&name, namefmt, nameap); \ + nv_vasprintf(&name, namefmt, nameap); \ if (name == NULL) \ nvlist_report_missing(NV_TYPE_##TYPE, ""); \ nvlist_free_##type(nvl, name); \ - free(name); \ + nv_free(name); \ } #endif diff --git a/lib/libnv/nvlist_impl.h b/lib/libnv/nvlist_impl.h index 490c81a..ba5a016 100644 --- a/lib/libnv/nvlist_impl.h +++ b/lib/libnv/nvlist_impl.h @@ -32,7 +32,9 @@ #ifndef _NVLIST_IMPL_H_ #define _NVLIST_IMPL_H_ +#ifndef _KERNEL #include +#endif #include "nv.h" diff --git a/lib/libnv/nvpair.c b/lib/libnv/nvpair.c index ae22286..2668ebb 100644 --- a/lib/libnv/nvpair.c +++ b/lib/libnv/nvpair.c @@ -34,6 +34,16 @@ __FBSDID("$FreeBSD$"); #include #include +#ifdef _KERNEL + +#include +#include +#include +#include + +#include + +#else #include #include #include @@ -41,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#endif #ifdef HAVE_PJDLOG #include @@ -53,11 +64,17 @@ __FBSDID("$FreeBSD$"); #include "nvpair_impl.h" #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 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__) #define PJDLOG_RASSERT(expr, ...) assert(expr) #define PJDLOG_ABORT(...) abort() #endif +#endif void nvpair_assert(const nvpair_t *nvp) @@ -146,10 +163,12 @@ nvpair_clone(const nvpair_t *nvp) case NV_TYPE_NVLIST: newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp)); break; +#ifndef _KERNEL case NV_TYPE_DESCRIPTOR: newnvp = nvpair_create_descriptor(name, nvpair_get_descriptor(nvp)); break; +#endif case NV_TYPE_BINARY: data = nvpair_get_binary(nvp, &datasize); newnvp = nvpair_create_binary(name, data, datasize); @@ -287,7 +306,7 @@ nvpair_pack_nvlist(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp, PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize); memcpy(ptr, data, nvp->nvp_datasize); - free(data); + nv_free(data); ptr += nvp->nvp_datasize; *leftp -= nvp->nvp_datasize; @@ -350,9 +369,11 @@ nvpair_pack(nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp, size_t *leftp) case NV_TYPE_NVLIST: ptr = nvpair_pack_nvlist(nvp, ptr, fdidxp, leftp); break; +#ifndef _KERNEL case NV_TYPE_DESCRIPTOR: ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, leftp); break; +#endif case NV_TYPE_BINARY: ptr = nvpair_pack_binary(nvp, ptr, leftp); break; @@ -419,7 +440,7 @@ nvpair_unpack_header(int flags, nvpair_t *nvp, const unsigned char *ptr, return (ptr); failed: - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -431,7 +452,7 @@ nvpair_unpack_null(int flags __unused, nvpair_t *nvp, const unsigned char *ptr, PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL); if (nvp->nvp_datasize != 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -447,11 +468,11 @@ nvpair_unpack_bool(int flags __unused, nvpair_t *nvp, const unsigned char *ptr, PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL); if (nvp->nvp_datasize != sizeof(value)) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } if (*leftp < sizeof(value)) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -460,7 +481,7 @@ nvpair_unpack_bool(int flags __unused, nvpair_t *nvp, const unsigned char *ptr, *leftp -= sizeof(value); if (value != 0 && value != 1) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -477,11 +498,11 @@ nvpair_unpack_number(int flags, nvpair_t *nvp, const unsigned char *ptr, PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER); if (nvp->nvp_datasize != sizeof(uint64_t)) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } if (*leftp < sizeof(uint64_t)) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -503,17 +524,17 @@ nvpair_unpack_string(int flags __unused, nvpair_t *nvp, PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING); if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } if (strnlen((const char *)ptr, nvp->nvp_datasize) != nvp->nvp_datasize - 1) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); 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) return (NULL); @@ -533,7 +554,7 @@ nvpair_unpack_nvlist(int flags __unused, nvpair_t *nvp, PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST); if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -558,11 +579,11 @@ nvpair_unpack_binary(int flags __unused, nvpair_t *nvp, PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY); if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } - value = malloc(nvp->nvp_datasize); + value = nv_malloc(nvp->nvp_datasize); if (value == NULL) return (NULL); @@ -581,7 +602,7 @@ nvpair_unpack(int flags, const unsigned char *ptr, size_t *leftp, { nvpair_t *nvp, *tmp; - nvp = calloc(1, sizeof(*nvp) + NV_NAME_MAX); + nvp = nv_calloc(1, sizeof(*nvp) + NV_NAME_MAX); if (nvp == NULL) return (NULL); nvp->nvp_name = (char *)(nvp + 1); @@ -589,7 +610,7 @@ nvpair_unpack(int flags, const unsigned char *ptr, size_t *leftp, ptr = nvpair_unpack_header(flags, nvp, ptr, leftp); if (ptr == NULL) 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) goto failed; nvp = tmp; @@ -613,10 +634,12 @@ nvpair_unpack(int flags, const unsigned char *ptr, size_t *leftp, ptr = nvpair_unpack_nvlist(flags, nvp, ptr, leftp, fds, nfds, level); break; +#ifndef _KERNEL case NV_TYPE_DESCRIPTOR: ptr = nvpair_unpack_descriptor(flags, nvp, ptr, leftp, fds, nfds); break; +#endif case NV_TYPE_BINARY: ptr = nvpair_unpack_binary(flags, nvp, ptr, leftp); break; @@ -631,7 +654,7 @@ nvpair_unpack(int flags, const unsigned char *ptr, size_t *leftp, *nvpp = nvp; return (ptr); failed: - free(nvp); + nv_free(nvp); return (NULL); } @@ -663,18 +686,18 @@ nvpair_allocv(int type, uint64_t data, size_t datasize, const char *namefmt, PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST); - namelen = vasprintf(&name, namefmt, nameap); + namelen = nv_vasprintf(&name, namefmt, nameap); if (namelen < 0) return (NULL); PJDLOG_ASSERT(namelen > 0); if (namelen >= NV_NAME_MAX) { - free(name); - errno = ENAMETOOLONG; + nv_free(name); + RESTORE_ERRNO(ENAMETOOLONG); return (NULL); } - nvp = calloc(1, sizeof(*nvp) + namelen + 1); + nvp = nv_calloc(1, sizeof(*nvp) + namelen + 1); if (nvp != NULL) { nvp->nvp_name = (char *)(nvp + 1); memcpy(nvp->nvp_name, name, namelen + 1); @@ -683,7 +706,7 @@ nvpair_allocv(int type, uint64_t data, size_t datasize, const char *namefmt, nvp->nvp_datasize = datasize; nvp->nvp_magic = NVPAIR_MAGIC; } - free(name); + nv_free(name); return (nvp); }; @@ -736,12 +759,12 @@ nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) char *str; int len; - len = vasprintf(&str, valuefmt, valueap); + len = nv_vasprintf(&str, valuefmt, valueap); if (len < 0) return (NULL); nvp = nvpair_create_string(name, str); if (nvp == NULL) - free(str); + nv_free(str); return (nvp); } @@ -868,11 +891,11 @@ nvpair_createv_string(const char *value, const char *namefmt, va_list nameap) char *data; if (value == NULL) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } - data = strdup(value); + data = nv_strdup(value); if (data == NULL) return (NULL); size = strlen(value) + 1; @@ -880,7 +903,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, namefmt, nameap); if (nvp == NULL) - free(data); + nv_free(data); return (nvp); } @@ -893,7 +916,7 @@ nvpair_createv_nvlist(const nvlist_t *value, const char *namefmt, nvpair_t *nvp; if (value == NULL) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -917,11 +940,11 @@ nvpair_createv_binary(const void *value, size_t size, const char *namefmt, void *data; if (value == NULL || size == 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } - data = malloc(size); + data = nv_malloc(size); if (data == NULL) return (NULL); memcpy(data, value, size); @@ -929,7 +952,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, namefmt, nameap); if (nvp == NULL) - free(data); + nv_free(data); return (nvp); } @@ -1000,14 +1023,14 @@ nvpair_movev_string(char *value, const char *namefmt, va_list nameap) nvpair_t *nvp; if (value == NULL) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } nvp = nvpair_allocv(NV_TYPE_STRING, (uint64_t)(uintptr_t)value, strlen(value) + 1, namefmt, nameap); if (nvp == NULL) - free(value); + nv_free(value); return (nvp); } @@ -1018,12 +1041,12 @@ nvpair_movev_nvlist(nvlist_t *value, const char *namefmt, va_list nameap) nvpair_t *nvp; if (value == NULL) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } if (nvlist_error(value) != 0) { - errno = nvlist_error(value); + RESTORE_ERRNO(nvlist_error(value)); nvlist_destroy(value); return (NULL); } @@ -1042,7 +1065,7 @@ nvpair_movev_binary(void *value, size_t size, const char *namefmt, { if (value == NULL || size == 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -1108,20 +1131,22 @@ nvpair_free(nvpair_t *nvp) nvp->nvp_magic = 0; switch (nvp->nvp_type) { +#ifndef _KERNEL case NV_TYPE_DESCRIPTOR: close((int)nvp->nvp_data); break; +#endif case NV_TYPE_NVLIST: nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data); break; case NV_TYPE_STRING: - free((char *)(intptr_t)nvp->nvp_data); + nv_free((char *)(intptr_t)nvp->nvp_data); break; case NV_TYPE_BINARY: - free((void *)(intptr_t)nvp->nvp_data); + nv_free((void *)(intptr_t)nvp->nvp_data); break; } - free(nvp); + nv_free(nvp); } void @@ -1132,7 +1157,7 @@ nvpair_free_structure(nvpair_t *nvp) PJDLOG_ASSERT(nvp->nvp_list == NULL); nvp->nvp_magic = 0; - free(nvp); + nv_free(nvp); } const char * diff --git a/lib/libnv/nvpair_impl.h b/lib/libnv/nvpair_impl.h index c09daef..6861a3f 100644 --- a/lib/libnv/nvpair_impl.h +++ b/lib/libnv/nvpair_impl.h @@ -34,7 +34,9 @@ #include +#ifndef _KERNEL #include +#endif #include "nv.h" -- 1.9.2