1#ifndef	_DLFCN_H
2#define	_DLFCN_H
3
4#include <features.h>
5#include <stdbool.h>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#define RTLD_LAZY   1
12#define RTLD_NOW    2
13#define RTLD_NOLOAD 4
14#define RTLD_NODELETE 4096
15#define RTLD_GLOBAL 256
16#define RTLD_LOCAL  0
17
18#define RTLD_NEXT    ((void *)-1)
19#define RTLD_DEFAULT ((void *)0)
20
21#define RTLD_DI_LINKMAP 2
22
23
24/* create flags for dlns_create */
25#define CREATE_INHERIT_DEFAULT 0x1
26#define CREATE_INHERIT_CURRENT 0x2
27#define LOCAL_NS_PREFERED 0x4 /* Use app's library when app's library has same name as system library. */
28
29int    dlclose(void *);
30char  *dlerror(void);
31void  *dlopen(const char *, int);
32void  *dlsym(void *__restrict, const char *__restrict);
33void  *dlvsym(void *__restrict, const char *__restrict, const char *__restrict);
34
35/* namespace apis */
36#define NS_NAME_MAX 255
37typedef struct {
38	char name[NS_NAME_MAX+1];
39} Dl_namespace;
40
41/**
42  * @brief Initialize a namespace structure for operating namespaces through related functional interfaces.
43  * @param Dl_namespace * namespace handle.
44  * @param char * namespace name.
45  * @return void.
46  * @retval none.
47  */
48void dlns_init(Dl_namespace *, const char *);
49
50/**
51  * @brief Gets the current namespace handle, or verifies that the given name namespace exists.
52  * @param char * Namespace name.Gets the current caller namespace handle when name is null.
53  * @param Dl_namespace * namespace handle.
54  * @return return 0 on success,fail other values.
55  * @retval
56  *    EINVAL(22) Invalid argument.
57  *    ENOKEY(126) Required key not available.
58  */
59int dlns_get(const char *, Dl_namespace *);
60
61/**
62  * @brief open dso in given namespace which has own lib search paths, when namespace is null, it's same to dlopen().
63  *        avoid using "default" as namespace, which is the default namespace.
64  * @param Dl_namespace * namespace handle.
65  * @param char * the name of the so file you want to open.
66  * @param int open file mode.
67  *    -- RTLD_LAZY.
68  *    -- RTLD_NOW.
69  *    -- RTLD_NOLOAD.
70  *    -- RTLD_NODELETE.
71  *    -- RTLD_GLOBAL.
72  *    -- RTLD_LOCAL.
73  * @return success: dynamic library handleoid,failed: NULL.
74  * @retval none.
75  */
76void *dlopen_ns(Dl_namespace *, const char *, int);
77
78/**
79  * @brief create the namespace and set lib search paths of namespace,
80  *        the paths should be splited by ':'. When namespace already exist,return error.
81  *        avoid using "default" as namespace, which is the default namespace.
82  * @param Dl_namespace * namespace handle.
83  * @param char * lib path library that can be specified.
84  * @return return 0 on success,fail other values.
85  * @retval
86  *    EINVAL(22) Invalid argument.
87  *    EEXIST(17) File exists.
88  *    ENOMEM(12) Out of memory.
89  */
90int dlns_create(Dl_namespace *, const char *);
91
92/**
93  * @brief create the namespace and set lib search paths of namespace,
94  *        like dlns_create, except can use flags to set parent ns.
95  * @param Dl_namespace * namespace handle.
96  * @param char * lib path library that can be specified.
97  * #param int flags for create namespace, CREATE_INHERIT_CURRENT or CREATE_INHERIT_DEFAULT.
98  * @return return 0 on success,fail other values.
99  * @retval
100  *    EINVAL(22) Invalid argument.
101  *    EEXIST(17) File exists.
102  *    ENOMEM(12) Out of memory.
103  */
104int dlns_create2(Dl_namespace *, const char *, int);
105
106/**
107  * @brief make one namespace inherit another, and so it can use shared libs by the inherited one.
108  *        param1: namespace, param2: inherited namespace, param3: shared libs.
109  *        the shared libs should be splited by ':'. when it is null or empty, all libs can be shared.
110  *        one namespace can inherit or be inherited by multiple ones.
111  *        When namespaces do not exist, return error.
112  * @param Dl_namespace * The first parameter is the namespace to inherit from.
113  * @param Dl_namespace * The second parameter is the inherited namespace.
114  * @param char * some library names to inherit.
115  * @return return 0 on success,fail other values.
116  * @retval
117  *    EINVAL(22) Invalid argument.
118  *    ENOKEY(126) Required key not available.
119  */
120int dlns_inherit(Dl_namespace *, Dl_namespace *, const char *);
121
122/**
123  * @brief Set namespace lib_path.
124  * @param name namespace name.
125  * @param lib_path The lib path name that needs to be reset, it can be multiple, link with ":".
126  * @return Returns 0 on success, other on failure.
127  * @retval
128  *    EINVAL(22) Invalid argument.
129  *    ENOKEY(126) Required key not available.
130  */
131int dlns_set_namespace_lib_path(const char *name, const char *lib_path);
132
133/**
134  * @brief Set namespace separated.
135  * @param name namespace name.
136  * @param separated separated.
137  * @return Returns 0 on success, other on failure.
138  * @retval
139  *    EINVAL(22) Invalid argument.
140  *    ENOKEY(126) Required key not available.
141  */
142int dlns_set_namespace_separated(const char *name, const bool separated);
143
144/**
145  * @brief Set namespace permitted_paths.
146  * @param name namespace name.
147  * @param permitted_paths set new permitted_paths.
148  * @return Returns 0 on success, other on failure.
149  * @retval
150  *    EINVAL(22) Invalid argument.
151  *    ENOKEY(126) Required key not available.
152  */
153int dlns_set_namespace_permitted_paths(const char *name, const char *permitted_paths);
154
155/**
156  * @brief Set namespace allowed_libs.
157  * @param name namespace name.
158  * @param allowed_libs set new allowed_libs.
159  * @return Returns 0 on success, other on failure.
160  * @retval
161  *    EINVAL(22) Invalid argument.
162  *    ENOKEY(126) Required key not available.
163  */
164int dlns_set_namespace_allowed_libs(const char *name, const char *allowed_libs);
165
166#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
167typedef struct {
168	const char *dli_fname;
169	void *dli_fbase;
170	const char *dli_sname;
171	void *dli_saddr;
172} Dl_info;
173int dladdr(const void *, Dl_info *);
174int dlinfo(void *, int, void *);
175#endif
176
177#if _REDIR_TIME64
178__REDIR(dlsym, __dlsym_time64);
179#endif
180
181#ifdef __cplusplus
182}
183#endif
184
185#endif
186