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