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
|
/*
* dl.c -- load/unload modules
*
* Copyright (C) 1999-2001 by Massimiliano Ghilardi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#include <dlfcn.h>
#include "twin.h"
#include "methods.h"
#include "data.h"
#include "util.h"
#include "dl.h"
#include "version.h"
static void WrongVer(uldat ver) {
static byte buf[80];
sprintf(buf, "version mismatch: module is %d.%d.%d, this twin is " TWIN_VERSION_STR,
TWVER_MAJOR(ver), TWVER_MINOR(ver), TWVER_PATCH(ver));
ErrStr = buf;
}
byte DlOpen(module Module) {
void *Handle = NULL;
uldat len0 = LenStr(conf_destdir_lib_twin_modules_), len;
byte *name;
byte (*init_dl)(module);
uldat *version_dl;
if (Module && !Module->Handle && Module->Name && Module->NameLen) {
/* dlopen(NULL, ...) returns a handle for the main program */
if (Module->NameLen == 4 && !CmpMem(Module->Name, "main", 4))
name = NULL;
else {
len = len0 + Module->NameLen;
if ((name = AllocMem(len+1))) {
CopyMem(conf_destdir_lib_twin_modules_, name, len0);
CopyMem(Module->Name, name+len0, Module->NameLen);
name[len] = '\0';
} else
return FALSE;
}
Handle = dlopen(name, RTLD_NOW|RTLD_GLOBAL);
if (name)
FreeMem(name);
}
if (!Handle) {
Error(DLERROR);
ErrStr = dlerror();
return FALSE;
}
if (name) {
version_dl = dlsym(Handle, "VersionModule");
if (version_dl && *version_dl == TWIN_VERSION) {
init_dl = dlsym(Handle, "InitModule");
if (!init_dl || init_dl(Module)) {
Module->Handle = Handle;
return TRUE;
}
} else {
Error(USERERROR);
WrongVer(version_dl ? *version_dl : 0);
}
dlclose(Handle);
return FALSE;
}
return TRUE;
}
void DlClose(module Module) {
void (*quit_dl)(module);
if (Module && Module->Handle) {
if (Module->NameLen != 4 || CmpMem(Module->Name, "main", 4)) {
quit_dl = dlsym(Module->Handle, "QuitModule");
if (quit_dl)
quit_dl(Module);
}
dlclose(Module->Handle);
Module->Handle = NULL;
}
}
module DlLoadAny(uldat len, byte *name) {
module Module;
for (Module = All->FirstModule; Module; Module = Module->Next) {
if (len == Module->NameLen && !CmpMem(name, Module->Name, len))
/* already loaded! */
return Module;
}
if ((Module = Do(Create,Module)(FnModule, len, name))) {
if (Act(DlOpen,Module)(Module))
return Module;
Delete(Module);
}
return (module)0;
}
static module So[MAX_So];
module DlLoad(uldat code) {
module M;
if (code < MAX_So) {
if (!(M = So[code])) {
switch (code) {
#ifndef CONF_WM
case WMSo: M = DlLoadAny(5, "wm.so"); break;
#endif
#ifndef CONF_TERM
case TermSo: M = DlLoadAny(7, "term.so"); break;
#endif
#ifndef CONF_SOCKET
case SocketSo: M = DlLoadAny(9, "socket.so"); break;
#endif
#ifndef CONF_WM_RC
case RCParseSo: M = DlLoadAny(10, "rcparse.so"); break;
#endif
case MainSo:
default: M = DlLoadAny(4, "main"); break;
}
if ((So[code] = M)) {
if (All->FnHookModule)
All->FnHookModule(All->HookModule);
}
}
}
return M;
}
void DlUnLoad(uldat code) {
if (code < MAX_So) {
if (So[code]) {
Delete(So[code]);
So[code] = (module)0;
if (All->FnHookModule)
All->FnHookModule(All->HookModule);
}
}
}
module DlIsLoaded(uldat code) {
if (code < MAX_So)
return So[code];
return (module)0;
}
udat DlName2Code(byte *name) {
if (!CmpStr(name, "wm.so"))
return WMSo;
if (!CmpStr(name, "term.so"))
return TermSo;
if (!CmpStr(name, "socket.so"))
return SocketSo;
if (!CmpStr(name, "rcparse.so"))
return RCParseSo;
return MainSo;
}
|