diff options
author | rstone <rstone@FreeBSD.org> | 2015-03-01 00:22:53 +0000 |
---|---|---|
committer | rstone <rstone@FreeBSD.org> | 2015-03-01 00:22:53 +0000 |
commit | 44a67fe2ac9e29cfea89b4640e91c024d5fe3826 (patch) | |
tree | 7f1ffb02d47a0938a375542a2132a1c07b0db167 /lib/libnv | |
parent | eac1d566b46edef765754203bef22c75c1699966 (diff) | |
download | FreeBSD-src-44a67fe2ac9e29cfea89b4640e91c024d5fe3826.zip FreeBSD-src-44a67fe2ac9e29cfea89b4640e91c024d5fe3826.tar.gz |
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
Diffstat (limited to 'lib/libnv')
-rw-r--r-- | lib/libnv/dnv.h | 6 | ||||
-rw-r--r-- | lib/libnv/dnvlist.c | 28 | ||||
-rw-r--r-- | lib/libnv/nv.h | 42 | ||||
-rw-r--r-- | lib/libnv/nv_impl.h | 31 | ||||
-rw-r--r-- | lib/libnv/nvlist.c | 232 | ||||
-rw-r--r-- | lib/libnv/nvlist_impl.h | 2 | ||||
-rw-r--r-- | lib/libnv/nvpair.c | 127 | ||||
-rw-r--r-- | lib/libnv/nvpair_impl.h | 2 |
8 files changed, 349 insertions, 121 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 <sys/cdefs.h> +#ifndef _KERNEL #include <stdarg.h> #include <stdbool.h> #include <stdint.h> +#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 582d19b..8dde6e0 100644 --- a/lib/libnv/dnvlist.c +++ b/lib/libnv/dnvlist.c @@ -30,10 +30,22 @@ #include <sys/cdefs.h> __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 <stdbool.h> #include <stdint.h> #include <stdlib.h> +#endif #include "nv.h" #include "nv_impl.h" @@ -55,7 +67,9 @@ DNVLIST_GET(bool, bool) DNVLIST_GET(uint64_t, number) DNVLIST_GET(const char *, string) DNVLIST_GET(const nvlist_t *, nvlist) +#ifndef _KERNEL DNVLIST_GET(int, descriptor) +#endif #undef DNVLIST_GET @@ -75,6 +89,7 @@ dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep, return (value); } +#ifndef _KERNEL #define DNVLIST_GETF(ftype, type) \ ftype \ 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; 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; @@ -155,6 +170,7 @@ dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval, } return (value); } +#endif #define DNVLIST_TAKE(ftype, type) \ ftype \ @@ -171,7 +187,9 @@ DNVLIST_TAKE(bool, bool) DNVLIST_TAKE(uint64_t, number) DNVLIST_TAKE(char *, string) DNVLIST_TAKE(nvlist_t *, nvlist) +#ifndef _KERNEL DNVLIST_TAKE(int, descriptor) +#endif #undef DNVLIST_TAKE @@ -191,6 +209,7 @@ dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep, return (value); } +#ifndef _KERNEL #define DNVLIST_TAKEF(ftype, type) \ ftype \ 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; 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; @@ -272,3 +291,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 bc80d68..0876e57 100644 --- a/lib/libnv/nv.h +++ b/lib/libnv/nv.h @@ -34,10 +34,12 @@ #include <sys/cdefs.h> +#ifndef _KERNEL #include <stdarg.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> +#endif #ifndef _NVLIST_T_DECLARED #define _NVLIST_T_DECLARED @@ -63,6 +65,10 @@ 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); @@ -73,8 +79,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); @@ -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_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,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_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); +#ifdef _VA_LIST_DECLARED 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); +#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); /* @@ -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_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); /* @@ -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); 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); /* @@ -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); 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); /* @@ -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_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); @@ -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_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); +#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_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_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); +#endif 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); +#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_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); +#endif +#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); @@ -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_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 ce97dd0..da90e79 100644 --- a/lib/libnv/nv_impl.h +++ b/lib/libnv/nv_impl.h @@ -46,6 +46,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 7a07813..95661b4 100644 --- a/lib/libnv/nvlist.c +++ b/lib/libnv/nvlist.c @@ -33,6 +33,18 @@ __FBSDID("$FreeBSD$"); #include <sys/param.h> #include <sys/endian.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 <errno.h> @@ -44,6 +56,7 @@ __FBSDID("$FreeBSD$"); #include <stdlib.h> #include <string.h> #include <unistd.h> +#endif #ifdef HAVE_PJDLOG #include <pjdlog.h> @@ -56,6 +69,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 <assert.h> #define PJDLOG_ASSERT(...) assert(__VA_ARGS__) #define PJDLOG_RASSERT(expr, ...) assert(expr) @@ -66,6 +84,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) @@ -85,6 +104,10 @@ struct nvlist { PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC); \ } while (0) +#ifdef _KERNEL +MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist"); +#endif + #define NVPAIR_ASSERT(nvp) nvpair_assert(nvp) #define NVLIST_HEADER_MAGIC 0x6c @@ -104,7 +127,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_parent = NULL; @@ -123,7 +146,7 @@ nvlist_destroy(nvlist_t *nvl) if (nvl == NULL) return; - serrno = errno; + SAVE_ERRNO(serrno); NVLIST_ASSERT(nvl); @@ -132,9 +155,9 @@ nvlist_destroy(nvlist_t *nvl) nvpair_free(nvp); } nvl->nvl_magic = 0; - free(nvl); + nv_free(nvl); - errno = serrno; + RESTORE_ERRNO(serrno); } void @@ -240,7 +263,7 @@ nvlist_find(const nvlist_t *nvl, int type, const char *name) } if (nvp == NULL) - errno = ENOENT; + RESTORE_ERRNO(ENOENT); 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); } +#ifndef _KERNEL bool 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; bool exists; - vasprintf(&name, namefmt, nameap); + nv_vasprintf(&name, namefmt, nameap); if (name == NULL) return (false); 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) @@ -303,6 +328,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, ...) { @@ -318,12 +344,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, "<unknown>"); nvlist_free_type(nvl, name, type); - free(name); + nv_free(name); } +#endif nvlist_t * nvlist_clone(const nvlist_t *nvl) @@ -334,7 +361,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); } @@ -353,6 +380,7 @@ nvlist_clone(const nvlist_t *nvl) return (newnvl); } +#ifndef _KERNEL static bool 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); nvlist_dump(nvl, fileno(fp)); } +#endif /* * The function obtains size of the nvlist after nvlist_pack(). @@ -501,6 +530,7 @@ out: return (size); } +#ifndef _KERNEL static int * 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); } +#endif +#ifndef _KERNEL int * nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp) { @@ -534,7 +566,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) @@ -544,10 +576,12 @@ nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp) *nitemsp = nitems; return (fds); } +#endif static size_t nvlist_xndescriptors(const nvlist_t *nvl, int level) { +#ifndef _KERNEL const nvpair_t *nvp; size_t ndescs; @@ -570,6 +604,9 @@ nvlist_xndescriptors(const nvlist_t *nvl, int level) } return (ndescs); +#else + return (0); +#endif } size_t @@ -614,12 +651,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); @@ -635,7 +672,7 @@ nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep) nvpair_init_datasize(nvp); ptr = nvpair_pack_header(nvp, ptr, &left); if (ptr == NULL) { - free(buf); + nv_free(buf); return (NULL); } 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); break; +#ifndef _KERNEL case NV_TYPE_DESCRIPTOR: ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left); break; +#endif case NV_TYPE_BINARY: ptr = nvpair_pack_binary(nvp, ptr, &left); 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)); } if (ptr == NULL) { - free(buf); + nv_free(buf); return (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); 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); } @@ -719,11 +758,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 @@ -775,7 +814,7 @@ nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds, return (ptr); failed: - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -822,10 +861,12 @@ nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds) &tmpnvl); nvlist_set_parent(tmpnvl, nvp); break; +#ifndef _KERNEL case NV_TYPE_DESCRIPTOR: ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left, fds, nfds); break; +#endif case NV_TYPE_BINARY: ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left); break; @@ -859,6 +900,7 @@ nvlist_unpack(const void *buf, size_t size) return (nvlist_xunpack(buf, size, NULL, 0)); } +#ifndef _KERNEL int nvlist_send(int sock, const nvlist_t *nvl) { @@ -968,6 +1010,7 @@ nvlist_xfer(int sock, nvlist_t *nvl) nvlist_destroy(nvl); return (nvlist_recv(sock)); } +#endif nvpair_t * nvlist_first_nvpair(const nvlist_t *nvl) @@ -1049,11 +1092,14 @@ NVLIST_EXISTS(bool, BOOL) NVLIST_EXISTS(number, NUMBER) NVLIST_EXISTS(string, STRING) NVLIST_EXISTS(nvlist, NVLIST) +#ifndef _KERNEL NVLIST_EXISTS(descriptor, DESCRIPTOR) +#endif NVLIST_EXISTS(binary, BINARY) #undef NVLIST_EXISTS +#ifndef _KERNEL bool nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...) { @@ -1084,7 +1130,9 @@ NVLIST_EXISTSF(bool) NVLIST_EXISTSF(number) NVLIST_EXISTSF(string) NVLIST_EXISTSF(nvlist) +#ifndef _KERNEL NVLIST_EXISTSF(descriptor) +#endif NVLIST_EXISTSF(binary) #undef NVLIST_EXISTSF @@ -1095,12 +1143,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 (false); exists = nvlist_exists(nvl, name); - free(name); + nv_free(name); return (exists); } @@ -1129,6 +1177,7 @@ NVLIST_EXISTSV(descriptor) NVLIST_EXISTSV(binary) #undef NVLIST_EXISTSV +#endif void 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); if (nvlist_error(nvl) != 0) { - errno = nvlist_error(nvl); + RESTORE_ERRNO(nvlist_error(nvl)); return; } if (nvlist_exists(nvl, nvpair_name(nvp))) { - nvl->nvl_error = errno = EEXIST; + nvl->nvl_error = EEXIST; + RESTORE_ERRNO(nvlist_error(nvl)); return; } newnvp = nvpair_clone(nvp); if (newnvp == NULL) { - nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM); + nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); + RESTORE_ERRNO(nvlist_error(nvl)); return; } @@ -1200,14 +1251,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); } @@ -1218,12 +1270,14 @@ nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value) nvlist_addf_nvlist(nvl, value, "%s", name); } +#ifndef _KERNEL void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value) { nvlist_addf_descriptor(nvl, value, "%s", name); } +#endif void 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); } +#ifndef _KERNEL void 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); va_end(nameap); } +#endif void 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; 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); } @@ -1328,14 +1385,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); } @@ -1346,14 +1404,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); } @@ -1364,14 +1423,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); } @@ -1382,17 +1442,19 @@ 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); } +#ifndef _KERNEL void nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) @@ -1410,6 +1472,7 @@ nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt, else nvlist_move_nvpair(nvl, nvp); } +#endif void 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; 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); } @@ -1438,12 +1502,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; } @@ -1460,7 +1525,9 @@ nvlist_move_##type(nvlist_t *nvl, const char *name, vtype value) \ NVLIST_MOVE(char *, string) NVLIST_MOVE(nvlist_t *, nvlist) +#ifndef _KERNEL NVLIST_MOVE(int, descriptor) +#endif #undef NVLIST_MOVE @@ -1485,7 +1552,9 @@ nvlist_movef_##type(nvlist_t *nvl, vtype value, const char *namefmt, \ NVLIST_MOVEF(char *, string) NVLIST_MOVEF(nvlist_t *, nvlist) +#ifndef _KERNEL NVLIST_MOVEF(int, descriptor) +#endif #undef NVLIST_MOVEF @@ -1507,15 +1576,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); } @@ -1528,17 +1598,19 @@ nvlist_movev_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, if (nvlist_error(nvl) != 0) { if (value != NULL && nvlist_get_nvpair_parent(value) != NULL) 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); } +#ifndef _KERNEL void nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) @@ -1557,6 +1629,7 @@ nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt, else nvlist_move_nvpair(nvl, nvp); } +#endif void 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; 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); } @@ -1600,7 +1674,9 @@ NVLIST_GET(bool, bool, BOOL) NVLIST_GET(uint64_t, number, NUMBER) NVLIST_GET(const char *, string, STRING) NVLIST_GET(const nvlist_t *, nvlist, NVLIST) +#ifndef _KERNEL NVLIST_GET(int, descriptor, DESCRIPTOR) +#endif #undef NVLIST_GET @@ -1630,6 +1706,7 @@ nvlist_getf_##type(const nvlist_t *nvl, const char *namefmt, ...) \ return (value); \ } +#ifndef _KERNEL NVLIST_GETF(bool, bool) NVLIST_GETF(uint64_t, number) NVLIST_GETF(const char *, string) @@ -1683,14 +1760,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, "<unknown>"); binary = nvlist_get_binary(nvl, name, sizep); - free(name); + nv_free(name); return (binary); } +#endif #define NVLIST_TAKE(ftype, type, TYPE) \ ftype \ @@ -1712,7 +1790,9 @@ NVLIST_TAKE(bool, bool, BOOL) NVLIST_TAKE(uint64_t, number, NUMBER) NVLIST_TAKE(char *, string, STRING) NVLIST_TAKE(nvlist_t *, nvlist, NVLIST) +#ifndef _KERNEL NVLIST_TAKE(int, descriptor, DESCRIPTOR) +#endif #undef NVLIST_TAKE @@ -1746,6 +1826,7 @@ nvlist_takef_##type(nvlist_t *nvl, const char *namefmt, ...) \ return (value); \ } +#ifndef _KERNEL NVLIST_TAKEF(bool, bool) NVLIST_TAKEF(uint64_t, number) NVLIST_TAKEF(char *, string) @@ -1797,14 +1878,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, "<unknown>"); 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) @@ -1837,11 +1919,14 @@ NVLIST_FREE(bool, BOOL) NVLIST_FREE(number, NUMBER) NVLIST_FREE(string, STRING) NVLIST_FREE(nvlist, NVLIST) +#ifndef _KERNEL NVLIST_FREE(descriptor, DESCRIPTOR) +#endif NVLIST_FREE(binary, BINARY) #undef NVLIST_FREE +#ifndef _KERNEL void nvlist_freef(nvlist_t *nvl, const char *namefmt, ...) { @@ -1901,6 +1986,7 @@ NVLIST_FREEV(nvlist, NVLIST) NVLIST_FREEV(descriptor, DESCRIPTOR) NVLIST_FREEV(binary, BINARY) #undef NVLIST_FREEV +#endif void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp) diff --git a/lib/libnv/nvlist_impl.h b/lib/libnv/nvlist_impl.h index ef32dea..a59a90e 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 <stdint.h> +#endif #include "nv.h" diff --git a/lib/libnv/nvpair.c b/lib/libnv/nvpair.c index 45cca6a..ad6fb27 100644 --- a/lib/libnv/nvpair.c +++ b/lib/libnv/nvpair.c @@ -34,6 +34,16 @@ __FBSDID("$FreeBSD$"); #include <sys/endian.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 <fcntl.h> #include <stdarg.h> @@ -42,6 +52,7 @@ __FBSDID("$FreeBSD$"); #include <stdlib.h> #include <string.h> #include <unistd.h> +#endif #ifdef HAVE_PJDLOG #include <pjdlog.h> @@ -54,11 +65,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 <assert.h> #define PJDLOG_ASSERT(...) assert(__VA_ARGS__) #define PJDLOG_RASSERT(expr, ...) assert(expr) #define PJDLOG_ABORT(...) abort() #endif +#endif #define NVPAIR_MAGIC 0x6e7670 /* "nvp" */ struct nvpair { @@ -184,10 +201,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); @@ -327,6 +346,7 @@ nvpair_pack_nvlist_up(unsigned char *ptr, size_t *leftp) return (ptr); } +#ifndef _KERNEL unsigned char * nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp, size_t *leftp) @@ -356,6 +376,7 @@ nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp, return (ptr); } +#endif unsigned char * 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); failed: - errno = EINVAL; + RESTORE_ERRNO(EINVAL); 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); if (nvp->nvp_datasize != 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); 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); if (nvp->nvp_datasize != sizeof(value)) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } if (*leftp < sizeof(value)) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -487,7 +508,7 @@ nvpair_unpack_bool(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr, *leftp -= sizeof(value); if (value != 0 && value != 1) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); 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); 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); } @@ -530,17 +551,17 @@ nvpair_unpack_string(bool isbe __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); @@ -559,7 +580,7 @@ nvpair_unpack_nvlist(bool isbe __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); } @@ -577,6 +598,7 @@ nvpair_unpack_nvlist(bool isbe __unused, nvpair_t *nvp, return (ptr); } +#ifndef _KERNEL const unsigned char * nvpair_unpack_descriptor(bool isbe, nvpair_t *nvp, const unsigned char *ptr, 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); } +#endif const unsigned char * 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); 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); @@ -649,7 +672,7 @@ nvpair_unpack(bool isbe, 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); @@ -657,7 +680,7 @@ nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp, ptr = nvpair_unpack_header(isbe, 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; @@ -669,7 +692,7 @@ nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp, *nvpp = nvp; return (ptr); failed: - free(nvp); + nv_free(nvp); 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); - 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); @@ -721,7 +744,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); }; @@ -774,12 +797,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); } @@ -790,12 +813,14 @@ nvpair_create_nvlist(const char *name, const nvlist_t *value) return (nvpair_createf_nvlist(value, "%s", name)); } +#ifndef _KERNEL nvpair_t * nvpair_create_descriptor(const char *name, int value) { return (nvpair_createf_descriptor(value, "%s", name)); } +#endif nvpair_t * 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); } +#ifndef _KERNEL nvpair_t * nvpair_createf_descriptor(int value, const char *namefmt, ...) { @@ -881,6 +907,7 @@ nvpair_createf_descriptor(int value, const char *namefmt, ...) return (nvp); } +#endif nvpair_t * 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; 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; @@ -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, namefmt, nameap); if (nvp == NULL) - free(data); + nv_free(data); return (nvp); } @@ -951,7 +978,7 @@ nvpair_createv_nvlist(const nvlist_t *value, const char *namefmt, nvpair_t *nvp; if (value == NULL) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } @@ -969,6 +996,7 @@ nvpair_createv_nvlist(const nvlist_t *value, const char *namefmt, return (nvp); } +#ifndef _KERNEL nvpair_t * 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); } +#endif nvpair_t * 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; 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); @@ -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, namefmt, nameap); if (nvp == NULL) - free(data); + nv_free(data); return (nvp); } @@ -1030,12 +1059,14 @@ nvpair_move_nvlist(const char *name, nvlist_t *value) return (nvpair_movef_nvlist(value, "%s", name)); } +#ifndef _KERNEL nvpair_t * nvpair_move_descriptor(const char *name, int value) { return (nvpair_movef_descriptor(value, "%s", name)); } +#endif nvpair_t * 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); } +#ifndef _KERNEL nvpair_t * nvpair_movef_descriptor(int value, const char *namefmt, ...) { @@ -1082,6 +1114,7 @@ nvpair_movef_descriptor(int value, const char *namefmt, ...) return (nvp); } +#endif nvpair_t * 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; 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) { - serrno = errno; - free(value); - errno = serrno; + SAVE_ERRNO(serrno); + nv_free(value); + RESTORE_ERRNO(serrno); } return (nvp); @@ -1124,12 +1157,12 @@ nvpair_movev_nvlist(nvlist_t *value, const char *namefmt, va_list nameap) nvpair_t *nvp; if (value == NULL || nvlist_get_nvpair_parent(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); } @@ -1144,6 +1177,7 @@ nvpair_movev_nvlist(nvlist_t *value, const char *namefmt, va_list nameap) return (nvp); } +#ifndef _KERNEL nvpair_t * 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); } +#endif nvpair_t * 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; if (value == NULL || size == 0) { - errno = EINVAL; + RESTORE_ERRNO(EINVAL); return (NULL); } nvp = nvpair_allocv(NV_TYPE_BINARY, (uint64_t)(uintptr_t)value, size, namefmt, nameap); if (nvp == NULL) { - serrno = errno; - free(value); - errno = serrno; + SAVE_ERRNO(serrno); + nv_free(value); + RESTORE_ERRNO(serrno); } return (nvp); @@ -1227,6 +1262,7 @@ nvpair_get_nvlist(const nvpair_t *nvp) return ((const nvlist_t *)(intptr_t)nvp->nvp_data); } +#ifndef _KERNEL int nvpair_get_descriptor(const nvpair_t *nvp) { @@ -1236,6 +1272,7 @@ nvpair_get_descriptor(const nvpair_t *nvp) return ((int)nvp->nvp_data); } +#endif const void * nvpair_get_binary(const nvpair_t *nvp, size_t *sizep) @@ -1258,20 +1295,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 @@ -1282,7 +1321,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 a72000b..121c8ab 100644 --- a/lib/libnv/nvpair_impl.h +++ b/lib/libnv/nvpair_impl.h @@ -34,7 +34,9 @@ #include <sys/queue.h> +#ifndef _KERNEL #include <stdint.h> +#endif #include "nv.h" |