Author: rstone
Date: Sun Mar  1 00:22:53 2015
New Revision: 279438
URL: https://svnweb.freebsd.org/changeset/base/279438

Log:
  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

Modified:
  head/lib/libnv/dnv.h
  head/lib/libnv/dnvlist.c
  head/lib/libnv/nv.h
  head/lib/libnv/nv_impl.h
  head/lib/libnv/nvlist.c
  head/lib/libnv/nvlist_impl.h
  head/lib/libnv/nvpair.c
  head/lib/libnv/nvpair_impl.h

Modified: head/lib/libnv/dnv.h
==============================================================================
--- head/lib/libnv/dnv.h        Sun Mar  1 00:22:45 2015        (r279437)
+++ head/lib/libnv/dnv.h        Sun Mar  1 00:22:53 2015        (r279438)
@@ -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
 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 nv
 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 *
 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
 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
 

Modified: head/lib/libnv/dnvlist.c
==============================================================================
--- head/lib/libnv/dnvlist.c    Sun Mar  1 00:22:45 2015        (r279437)
+++ head/lib/libnv/dnvlist.c    Sun Mar  1 00:22:53 2015        (r279438)
@@ -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, 
        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,
        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,
        }
        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
        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
        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
 
        return (value);
 }
+#endif

Modified: head/lib/libnv/nv.h
==============================================================================
--- head/lib/libnv/nv.h Sun Mar  1 00:22:45 2015        (r279437)
+++ head/lib/libnv/nv.h Sun Mar  1 00:22:53 2015        (r279438)
@@ -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, i
 
 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 *
 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, cons
 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, co
 
 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 *n
 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, c
 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, con
 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_
 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, 
 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
 

Modified: head/lib/libnv/nv_impl.h
==============================================================================
--- head/lib/libnv/nv_impl.h    Sun Mar  1 00:22:45 2015        (r279437)
+++ head/lib/libnv/nv_impl.h    Sun Mar  1 00:22:53 2015        (r279438)
@@ -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);
 

Modified: head/lib/libnv/nvlist.c
==============================================================================
--- head/lib/libnv/nvlist.c     Sun Mar  1 00:22:45 2015        (r279437)
+++ head/lib/libnv/nvlist.c     Sun Mar  1 00:22:53 2015        (r279438)
@@ -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 typ
        }
 
        if (nvp == NULL)
-               errno = ENOENT;
+               RESTORE_ERRNO(ENOENT);
 
        return (nvp);
 }
@@ -257,6 +280,7 @@ nvlist_exists_type(const nvlist_t *nvl, 
        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,
        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 ch
                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 typ
 {
        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 *
        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,
 
        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, 
        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, 
                *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
        }
 
        return (ndescs);
+#else
+       return (0);
+#endif
 }
 
 size_t
@@ -614,12 +651,12 @@ nvlist_xpack(const nvlist_t *nvl, int64_
        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_
                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_
                        }
                        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_
                        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 
        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
 {
 
        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, cons
 
        return (ptr);
 failed:
-       errno = EINVAL;
+       RESTORE_ERRNO(EINVAL);
        return (NULL);
 }
 
@@ -822,10 +861,12 @@ nvlist_xunpack(const void *buf, size_t s
                            &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 si
        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, cons
        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 n
        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 
        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 c
        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 
        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, in
        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 ch
        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 val
        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
        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 
        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 
        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, in
        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 
        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
 
        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 
 
 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
 
 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 
        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

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to