1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* dynlink.c: A module loader. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
* |
24 |
*/ |
25 |
|
26 |
#include "ltdl.h" |
27 |
|
28 |
#include "stdinc.h" |
29 |
#include "tools.h" |
30 |
#include "irc_string.h" |
31 |
#include "modules.h" |
32 |
#include "s_log.h" |
33 |
#include "client.h" |
34 |
#include "send.h" |
35 |
|
36 |
extern dlink_list mod_list; |
37 |
|
38 |
static char unknown_ver[] = "<unknown>"; |
39 |
|
40 |
/* This file contains the core functions to use dynamic libraries. |
41 |
* -TimeMr14C |
42 |
*/ |
43 |
|
44 |
void |
45 |
dynlink_init(void) |
46 |
{ |
47 |
if (lt_dlinit()) { |
48 |
ilog(L_ERROR, "Couldn't initialize the libltdl run time dynamic" |
49 |
" link library. Exiting."); |
50 |
exit(0); |
51 |
} |
52 |
} |
53 |
|
54 |
int |
55 |
modules_valid_suffix(const char *name) |
56 |
{ |
57 |
return ((name = strrchr(name, '.'))) && !strcmp(name, ".la"); |
58 |
} |
59 |
|
60 |
/* unload_one_module() |
61 |
* |
62 |
* inputs - name of module to unload |
63 |
* - 1 to say modules unloaded, 0 to not |
64 |
* output - 0 if successful, -1 if error |
65 |
* side effects - module is unloaded |
66 |
*/ |
67 |
int |
68 |
unload_one_module(char *name, int warn) |
69 |
{ |
70 |
dlink_node *ptr = NULL; |
71 |
struct module *modp = NULL; |
72 |
|
73 |
if ((ptr = findmodule_byname(name)) == NULL) |
74 |
return -1; |
75 |
|
76 |
modp = ptr->data; |
77 |
|
78 |
if (modp->modremove) |
79 |
modp->modremove(); |
80 |
|
81 |
lt_dlclose(modp->handle); |
82 |
|
83 |
assert(dlink_list_length(&mod_list) > 0); |
84 |
dlinkDelete(ptr, &mod_list); |
85 |
MyFree(modp->name); |
86 |
MyFree(modp); |
87 |
|
88 |
if (warn == 1) |
89 |
{ |
90 |
ilog(L_INFO, "Module %s unloaded", name); |
91 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name); |
92 |
} |
93 |
|
94 |
return 0; |
95 |
} |
96 |
|
97 |
/* load_a_module() |
98 |
* |
99 |
* inputs - path name of module, int to notice, int of core |
100 |
* output - -1 if error 0 if success |
101 |
* side effects - loads a module if successful |
102 |
*/ |
103 |
int |
104 |
load_a_module(char *path, int warn, int core) |
105 |
{ |
106 |
lt_dlhandle tmpptr = NULL; |
107 |
char *mod_basename; |
108 |
char **verp; |
109 |
char *ver; |
110 |
struct module *modp; |
111 |
void (*mod_init)(void) = NULL; |
112 |
void (*mod_deinit)(void) = NULL; |
113 |
|
114 |
mod_basename = basename(path); |
115 |
|
116 |
if (findmodule_byname(mod_basename) != NULL) |
117 |
return 1; |
118 |
|
119 |
if (!(tmpptr = lt_dlopen(path))) { |
120 |
const char *err = ((err = lt_dlerror())) ? err : "<unknown>"; |
121 |
|
122 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s", |
123 |
mod_basename, err); |
124 |
ilog(L_WARN, "Error loading module %s: %s", mod_basename, err); |
125 |
return -1; |
126 |
} |
127 |
|
128 |
if ((mod_init = lt_dlsym(tmpptr, "_modinit")) == NULL) |
129 |
{ |
130 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no _modinit() function", |
131 |
mod_basename); |
132 |
ilog(L_WARN, "Module %s has no _modinit() function", mod_basename); |
133 |
lt_dlclose(tmpptr); |
134 |
return -1; |
135 |
} |
136 |
|
137 |
if ((mod_deinit = lt_dlsym(tmpptr, "_moddeinit")) == NULL) |
138 |
{ |
139 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no _moddeinit() function", |
140 |
mod_basename); |
141 |
ilog(L_WARN, "Module %s has no _moddeinit() function", mod_basename); |
142 |
/* blah blah soft error, see above. */ |
143 |
} |
144 |
|
145 |
if ((verp = lt_dlsym(tmpptr, "_version")) == NULL) |
146 |
ver = unknown_ver; |
147 |
else |
148 |
ver = *verp; |
149 |
|
150 |
modp = MyMalloc(sizeof(struct module)); |
151 |
modp->handle = tmpptr; |
152 |
modp->version = ver; |
153 |
modp->core = core; |
154 |
modp->modremove = mod_deinit; |
155 |
|
156 |
DupString(modp->name, mod_basename); |
157 |
dlinkAdd(modp, &modp->node, &mod_list); |
158 |
|
159 |
mod_init(); |
160 |
|
161 |
if (warn == 1) |
162 |
{ |
163 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
164 |
"Module %s [version: %s handle: %p] loaded.", |
165 |
mod_basename, ver, tmpptr); |
166 |
ilog(L_WARN, "Module %s [version: %s handle: %p] loaded.", |
167 |
mod_basename, ver, tmpptr); |
168 |
} |
169 |
|
170 |
return 0; |
171 |
} |