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
|
/*
* ion/ioncore/modules.c
*
* Copyright (c) Tuomo Valkonen 1999-2004.
*
* Ion is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*/
#include <ltdl.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
#include "modules.h"
#include "readconfig.h"
#include "../version.h"
static lt_dlcaller_id ltid=0;
/*{{{ Loading and initialization code */
static void *get_module_symbol(lt_dlhandle handle, char *name)
{
const lt_dlinfo *info;
char *p;
void *ret;
info=lt_dlgetinfo(handle);
if(info==NULL || info->name==NULL){
warn("lt_dlgetinfo() failed to return module name.");
return NULL;
}
p=scat(info->name, name);
if(p==NULL){
warn_err();
return NULL;
}
ret=lt_dlsym(handle, p);
free(p);
return ret;
}
static bool check_version(lt_dlhandle handle)
{
char *versionstr=(char*)get_module_symbol(handle, "_module_ion_api_version");
if(versionstr==NULL)
return FALSE;
return (strcmp(versionstr, ION_API_VERSION)==0);
}
static bool call_init(lt_dlhandle handle)
{
bool (*initfn)(void);
initfn=(bool (*)())get_module_symbol(handle, "_module_init");
if(initfn==NULL)
return TRUE;
return initfn();
}
bool init_module_support()
{
#ifdef CF_PRELOAD_MODULES
LTDL_SET_PRELOADED_SYMBOLS();
#endif
if(lt_dlinit()!=0){
warn("lt_dlinit: %s", lt_dlerror());
return FALSE;
}
ltid=lt_dlcaller_register();
return TRUE;
}
/*EXTL_DOC
* Attempt to load module \var{modname}. Ion will use libltdl to search
* the library path (the default setting is \file{\~{}/.ion2/libs} and
* \file{\$PREFIX/lib/ion}) and also try diffent extensions, so only
* the module name should usually be necessary to give here.
*/
EXTL_EXPORT
bool load_module(const char *modname)
{
lt_dlhandle handle=NULL;
if(modname==NULL)
return FALSE;
handle=lt_dlopenext(modname);
if(handle==NULL){
warn("Failed to load module %s: %s", modname, lt_dlerror());
return FALSE;
}
if(lt_dlcaller_set_data(ltid, handle, <id)!=NULL){
warn("Module %s already loaded", modname);
return TRUE;
}
if(!check_version(handle)){
warn_obj(modname, "Module version information not found or version "
"mismatch. Refusing to use.");
goto err;
}
if(!call_init(handle))
goto err;
return TRUE;
err:
lt_dlclose(handle);
return FALSE;
}
/*}}}*/
/*{{{ Deinit */
static void call_deinit(lt_dlhandle handle, char *name)
{
void (*deinitfn)(void);
deinitfn=(void (*)())get_module_symbol(handle, "_module_deinit");
if(deinitfn!=NULL)
deinitfn();
}
static int do_unload_module(lt_dlhandle handle, void *unused)
{
const lt_dlinfo *info;
info=lt_dlgetinfo(handle);
if(info==NULL || info->name==NULL){
warn("Unable to get module name.");
}else{
call_deinit(handle, info->name);
}
lt_dlclose(handle);
return 0;
}
void unload_modules()
{
lt_dlforeach(do_unload_module, NULL);
}
/*}}}*/
|