1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
14 */
15
16 #ifndef _COMPAT_FREEBSD_SYS_NV_H_
17 #define _COMPAT_FREEBSD_SYS_NV_H_
18
19 #include <assert.h>
20 #include <libnvpair.h>
21
22 #define NV_TYPE_NVLIST DATA_TYPE_NVLIST
23 #define NV_TYPE_STRING DATA_TYPE_STRING
24
25 static inline const char *
nvlist_next(const nvlist_t * nvl,int * type,void ** cookie)26 nvlist_next(const nvlist_t *nvl, int *type, void **cookie)
27 {
28 nvpair_t *nvp = *cookie;
29
30 nvp = nvlist_next_nvpair((nvlist_t *)nvl, nvp);
31 if (nvp == NULL)
32 return (NULL);
33
34 *cookie = nvp;
35 *type = nvpair_type(nvp);
36 return (nvpair_name(nvp));
37 }
38
39 static inline nvlist_t *
nvlist_create(int flag)40 nvlist_create(int flag)
41 {
42 nvlist_t *nvl;
43
44 /*
45 * We only emulate this with flag == 0, which is equivalent to the
46 * illumos NV_UNIQUE_NAME.
47 */
48 assert(flag == 0);
49
50 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
51 return (NULL);
52 return (nvl);
53 }
54
55 static inline bool
nvlist_exists_nvlist(const nvlist_t * nvl,const char * name)56 nvlist_exists_nvlist(const nvlist_t *nvl, const char *name)
57 {
58 nvlist_t *snvl;
59
60 return (nvlist_lookup_nvlist((nvlist_t *)nvl, name, &snvl) == 0);
61 }
62
63 static inline nvlist_t *
nvlist_get_nvlist(const nvlist_t * nvl,const char * name)64 nvlist_get_nvlist(const nvlist_t *nvl, const char *name)
65 {
66 nvlist_t *snvl;
67
68 if (nvlist_lookup_nvlist((nvlist_t *)nvl, name, &snvl) == 0)
69 return (snvl);
70 return (NULL);
71 }
72
73 static inline bool
nvlist_exists_string(const nvlist_t * nvl,const char * name)74 nvlist_exists_string(const nvlist_t *nvl, const char *name)
75 {
76 char *str;
77
78 return (nvlist_lookup_string((nvlist_t *)nvl, name, &str) == 0);
79 }
80
81 static inline char *
nvlist_get_string(const nvlist_t * nvl,const char * name)82 nvlist_get_string(const nvlist_t *nvl, const char *name)
83 {
84 char *str;
85
86 if (nvlist_lookup_string((nvlist_t *)nvl, name, &str) == 0)
87 return (str);
88 return (NULL);
89 }
90
91 #define nvlist_free_string(nvl, name) nvlist_remove_all((nvl), (name))
92
93 #endif /* _COMPAT_FREEBSD_SYS_NV_H_ */
94