1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*
* ion/ioncore/modules.c
*
* Copyright (c) Arnout Engelen 2011
* Copyright (c) Tuomo Valkonen 1999-2009.
*
* See the included file LICENSE for details.
*/
#include <string.h>
#include <ctype.h>
#include <dlfcn.h>
#include <unistd.h>
#include <libtu/rb.h>
#include <libextl/readconfig.h>
#include "common.h"
#include "modules.h"
#include "../version.h"
#ifndef CF_PRELOAD_MODULES
/*{{{ Module list */
typedef void *dlhandle;
static Rb_node modules=NULL;
static dlhandle get_handle(const char *name)
{
int found=0;
Rb_node nd;
nd=rb_find_key_n(modules, name, &found);
if(found)
return nd->v.val;
return NULL;
}
static const char *get_name(dlhandle handle)
{
Rb_node nd;
rb_traverse(nd, modules){
if(nd->v.val==handle)
return (const char *)(nd->k.key);
}
return NULL;
}
static Rb_node add_module(char *name, dlhandle handle)
{
return rb_insert(modules, name, handle);
}
/*}}}*/
/*{{{ Module symbol access */
static void *get_module_symbol(dlhandle handle,
const char *modulename,
const char *name)
{
char *p;
void *ret;
p=scat(modulename, name);
if(p==NULL)
return NULL;
ret=dlsym(handle, p);
free(p);
return ret;
}
static void (*get_module_fptr(dlhandle handle,
const char *modulename,
const char *name))(void **)
{
/* This is not valid ISO C. However, it is 'tried and tested'. The
* workaround originally recommended[1] is not alias-safe[2]. The approach
* we chose, while not valid ISO C, *is* valid under POSIX 2008[3]. Newer
* versions of GCC should not warn about it anymore[4].
*
* [1] http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_06)
* [2] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289#c1
* [3] http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12_03
* [4] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289#c10
*/
return (void (*)(void**)) get_module_symbol(handle, modulename, name);
}
static char *get_version(dlhandle handle, const char *modulename)
{
return (char*)get_module_symbol(handle, modulename,
"_ion_api_version");
}
static bool check_has_version(dlhandle handle, const char *modulename)
{
return get_version(handle, modulename) != NULL;
}
static bool check_version(dlhandle handle, const char *modulename)
{
char *versionstr=get_version(handle, modulename);
if(versionstr==NULL)
return FALSE;
return (strcmp(versionstr, NOTION_API_VERSION)==0);
}
static bool call_init(dlhandle handle, const char *modulename)
{
bool (*initfn)(void);
initfn=(bool (*)())get_module_fptr(handle, modulename, "_init");
if(initfn==NULL)
return TRUE;
return initfn();
}
static void call_deinit(dlhandle handle, const char *modulename)
{
void (*deinitfn)(void);
deinitfn=(void (*)())get_module_fptr(handle, modulename, "_deinit");
if(deinitfn!=NULL)
deinitfn();
}
/*}}}*/
/*{{{ Init */
bool ioncore_init_module_support()
{
modules=make_rb();
return (modules!=NULL);
}
static int try_load(const char *file, void *UNUSED(param))
{
dlhandle handle=NULL;
const char *slash, *dot;
char *name;
Rb_node mod;
if(access(file, F_OK)!=0)
return EXTL_TRYCONFIG_NOTFOUND;
slash=strrchr(file, '/');
dot=strrchr(file, '.');
if(slash==NULL)
slash=file;
else
slash++;
if(dot<=slash){
warn(TR("Invalid module name."));
goto err1;
}
name=ALLOC_N(char, dot-slash+1);
if(name==NULL)
goto err1;
strncpy(name, slash, dot-slash);
name[dot-slash]='\0';
if(get_handle(name)){
warn_obj(file, TR("The module is already loaded."));
goto err2;
}
handle=dlopen(file, RTLD_NOW|RTLD_GLOBAL);
if(handle==NULL){
warn_obj(file, "%s", dlerror());
goto err2;
}
if(get_name(handle))
return EXTL_TRYCONFIG_OK;
if(!check_has_version(handle, name)){
warn_obj(file, TR("Module version information for %s not found. "
"Refusing to use."), name);
goto err3;
}
if(!check_version(handle, name)){
warn_obj(file, TR("Module version mismatch: expected '%s', found '%s'."
" Refusing to use."),
NOTION_API_VERSION, get_version(handle, name));
goto err3;
}
mod=add_module(name, handle);
if(mod==NULL)
goto err3;
if(!call_init(handle, name)){
warn_obj(file, TR("Unable to initialise module %s."), name);
rb_delete_node(mod);
goto err3;
}
return EXTL_TRYCONFIG_OK;
err3:
dlclose(handle);
err2:
free(name);
err1:
return EXTL_TRYCONFIG_LOAD_FAILED;
}
static bool do_load_module(const char *modname)
{
int retval;
const char *extension = "so";
retval=extl_try_config(modname, NULL, (ExtlTryConfigFn*)try_load,
NULL, extension, NULL);
if(retval==EXTL_TRYCONFIG_NOTFOUND)
warn(TR("Unable to find '%s.%s' on search path."), modname, extension);
return (retval==EXTL_TRYCONFIG_OK);
}
/*}}}*/
/*{{{ Deinit */
static void do_unload_module(Rb_node mod)
{
char *name=(char*)mod->k.key;
dlhandle handle=mod->v.val;
call_deinit(handle, name);
dlclose(handle);
free(name);
}
void ioncore_unload_modules()
{
Rb_node mod;
rb_traverse(mod, modules){
do_unload_module(mod);
}
}
/*}}}*/
#else
/*{{{ Static module support */
static bool call_init(WStaticModuleInfo *handle)
{
if(handle->init!=NULL)
return handle->init();
return TRUE;
}
static void call_deinit(WStaticModuleInfo *handle)
{
if(handle->deinit!=NULL)
handle->deinit();
}
extern WStaticModuleInfo ioncore_static_modules[];
static bool do_load_module(const char *name)
{
WStaticModuleInfo *mod;
for(mod=ioncore_static_modules; mod->name!=NULL; mod++){
if(strcmp(mod->name, name)==0)
break;
}
if(mod->name==NULL){
warn_obj(name, TR("Unknown module."));
return FALSE;
}
if(mod->loaded)
return TRUE;
if(!call_init(mod)){
warn_obj(name, TR("Unable to initialise module."));
return FALSE;
}
mod->loaded=TRUE;
return TRUE;
}
void ioncore_unload_modules()
{
WStaticModuleInfo *mod;
for(mod=ioncore_static_modules; mod->name!=NULL; mod++){
if(mod->loaded){
call_deinit(mod);
mod->loaded=FALSE;
}
}
}
bool ioncore_init_module_support()
{
return TRUE;
}
/*}}}*/
#endif
/*{{{ Exports */
/*EXTL_DOC
* Attempt to load a C-side module.
*/
EXTL_EXPORT
bool ioncore_load_module(const char *modname)
{
if(modname==NULL){
warn(TR("No module to load given."));
return FALSE;
}
return do_load_module(modname);
}
/*}}}*/
|