ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/modules.c
Revision: 1447
Committed: Mon Jun 25 20:22:29 2012 UTC (11 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 8869 byte(s)
Log Message:
- Added m_module.c and moved mo_module() into it

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * modules.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 #include "ltdl.h"
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "modules.h"
30 #include "log.h"
31 #include "ircd.h"
32 #include "client.h"
33 #include "send.h"
34 #include "conf.h"
35 #include "numeric.h"
36 #include "parse.h"
37 #include "ircd_defs.h"
38 #include "irc_string.h"
39 #include "memory.h"
40
41
42 dlink_list modules_list = { NULL, NULL, 0 };
43
44 static const char *unknown_ver = "<unknown>";
45
46 static const char *core_module_table[] =
47 {
48 "m_die.la",
49 "m_error.la",
50 "m_join.la",
51 "m_kick.la",
52 "m_kill.la",
53 "m_message.la",
54 "m_mode.la",
55 "m_nick.la",
56 "m_part.la",
57 "m_quit.la",
58 "m_server.la",
59 "m_sjoin.la",
60 "m_squit.la",
61 NULL
62 };
63
64 static dlink_list mod_paths = { NULL, NULL, 0 };
65 static dlink_list conf_modules = { NULL, NULL, 0 };
66
67 int
68 modules_valid_suffix(const char *name)
69 {
70 return ((name = strrchr(name, '.'))) && !strcmp(name, ".la");
71 }
72
73 /* unload_one_module()
74 *
75 * inputs - name of module to unload
76 * - 1 to say modules unloaded, 0 to not
77 * output - 0 if successful, -1 if error
78 * side effects - module is unloaded
79 */
80 int
81 unload_one_module(const char *name, int warn)
82 {
83 struct module *modp = NULL;
84
85 if ((modp = findmodule_byname(name)) == NULL)
86 return -1;
87
88 if (modp->modexit)
89 modp->modexit();
90
91 assert(dlink_list_length(&modules_list) > 0);
92 dlinkDelete(&modp->node, &modules_list);
93 MyFree(modp->name);
94
95 lt_dlclose(modp->handle);
96
97 if (warn == 1)
98 {
99 ilog(LOG_TYPE_IRCD, "Module %s unloaded", name);
100 sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name);
101 }
102
103 return 0;
104 }
105
106 /* load_a_module()
107 *
108 * inputs - path name of module, int to notice, int of core
109 * output - -1 if error 0 if success
110 * side effects - loads a module if successful
111 */
112 int
113 load_a_module(const char *path, int warn)
114 {
115 lt_dlhandle tmpptr = NULL;
116 const char *mod_basename = NULL;
117 struct module *modp = NULL;
118
119 if (findmodule_byname((mod_basename = libio_basename(path))))
120 return 1;
121
122 if (!(tmpptr = lt_dlopen(path))) {
123 const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
124
125 sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s",
126 mod_basename, err);
127 ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
128 return -1;
129 }
130
131 if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL)
132 {
133 sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no module_entry export",
134 mod_basename);
135 ilog(LOG_TYPE_IRCD, "Module %s has no module_entry export", mod_basename);
136 lt_dlclose(tmpptr);
137 return -1;
138 }
139
140 modp->handle = tmpptr;
141
142 if (EmptyString(modp->version))
143 modp->version = unknown_ver;
144
145 DupString(modp->name, mod_basename);
146 dlinkAdd(modp, &modp->node, &modules_list);
147
148 if (modp->modinit)
149 modp->modinit();
150
151 if (warn == 1)
152 {
153 sendto_realops_flags(UMODE_ALL, L_ALL,
154 "Module %s [version: %s handle: %p] loaded.",
155 modp->name, modp->version, tmpptr);
156 ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
157 modp->name, modp->version, tmpptr);
158 }
159
160 return 0;
161 }
162
163 /*
164 * modules_init
165 *
166 * input - NONE
167 * output - NONE
168 * side effects - The basic module manipulation modules are loaded
169 */
170 void
171 modules_init(void)
172 {
173 if (lt_dlinit())
174 {
175 ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
176 " link library. Exiting.");
177 exit(0);
178 }
179 }
180
181 /* mod_find_path()
182 *
183 * input - path
184 * output - none
185 * side effects - returns a module path from path
186 */
187 static struct module_path *
188 mod_find_path(const char *path)
189 {
190 dlink_node *ptr;
191
192 DLINK_FOREACH(ptr, mod_paths.head)
193 {
194 struct module_path *mpath = ptr->data;
195
196 if (!strcmp(path, mpath->path))
197 return mpath;
198 }
199
200 return NULL;
201 }
202
203 /* mod_add_path()
204 *
205 * input - path
206 * output - NONE
207 * side effects - adds path to list
208 */
209 void
210 mod_add_path(const char *path)
211 {
212 struct module_path *pathst;
213
214 if (mod_find_path(path))
215 return;
216
217 pathst = MyMalloc(sizeof(struct module_path));
218
219 strlcpy(pathst->path, path, sizeof(pathst->path));
220 dlinkAdd(pathst, &pathst->node, &mod_paths);
221 }
222
223 /* add_conf_module
224 *
225 * input - module name
226 * output - NONE
227 * side effects - adds module to conf_mod
228 */
229 void
230 add_conf_module(const char *name)
231 {
232 struct module_path *pathst;
233
234 pathst = MyMalloc(sizeof(struct module_path));
235
236 strlcpy(pathst->path, name, sizeof(pathst->path));
237 dlinkAdd(pathst, &pathst->node, &conf_modules);
238 }
239
240 /* mod_clear_paths()
241 *
242 * input - NONE
243 * output - NONE
244 * side effects - clear the lists of paths and conf modules
245 */
246 void
247 mod_clear_paths(void)
248 {
249 dlink_node *ptr = NULL, *next_ptr = NULL;
250
251 DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
252 {
253 dlinkDelete(ptr, &mod_paths);
254 MyFree(ptr->data);
255 }
256
257 DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
258 {
259 dlinkDelete(ptr, &conf_modules);
260 MyFree(ptr->data);
261 }
262 }
263
264 /* findmodule_byname
265 *
266 * input - name of module
267 * output - NULL if not found or pointer to module
268 * side effects - NONE
269 */
270 struct module *
271 findmodule_byname(const char *name)
272 {
273 dlink_node *ptr = NULL;
274
275 DLINK_FOREACH(ptr, modules_list.head)
276 {
277 struct module *modp = ptr->data;
278
279 if (strcmp(modp->name, name) == 0)
280 return modp;
281 }
282
283 return NULL;
284 }
285
286 /* load_all_modules()
287 *
288 * input - int flag warn
289 * output - NONE
290 * side effects - load all modules found in autoload directory
291 */
292 void
293 load_all_modules(int warn)
294 {
295 DIR *system_module_dir = NULL;
296 struct dirent *ldirent = NULL;
297 char module_fq_name[PATH_MAX + 1];
298
299 if ((system_module_dir = opendir(AUTOMODPATH)) == NULL)
300 {
301 ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
302 AUTOMODPATH, strerror(errno));
303 return;
304 }
305
306 while ((ldirent = readdir(system_module_dir)) != NULL)
307 {
308 if (modules_valid_suffix(ldirent->d_name))
309 {
310 snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s",
311 AUTOMODPATH, ldirent->d_name);
312 load_a_module(module_fq_name, warn);
313 }
314 }
315
316 closedir(system_module_dir);
317 }
318
319 /* load_conf_modules()
320 *
321 * input - NONE
322 * output - NONE
323 * side effects - load modules given in ircd.conf
324 */
325 void
326 load_conf_modules(void)
327 {
328 dlink_node *ptr = NULL;
329
330 DLINK_FOREACH(ptr, conf_modules.head)
331 {
332 struct module_path *mpath = ptr->data;
333
334 if (findmodule_byname(mpath->path) == NULL)
335 load_one_module(mpath->path);
336 }
337 }
338
339 /* load_core_modules()
340 *
341 * input - int flag warn
342 * output - NONE
343 * side effects - core modules are loaded, if any fail, kill ircd
344 */
345 void
346 load_core_modules(int warn)
347 {
348 char module_name[PATH_MAX + 1];
349 int i = 0;
350
351 for (; core_module_table[i]; ++i)
352 {
353 snprintf(module_name, sizeof(module_name), "%s%s",
354 MODPATH, core_module_table[i]);
355
356 if (load_a_module(module_name, warn) == -1)
357 {
358 ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
359 core_module_table[i]);
360 exit(EXIT_FAILURE);
361 }
362 }
363 }
364
365 /* load_one_module()
366 *
367 * input - pointer to path
368 * - flagged as core module or not
369 * output - -1 if error
370 * side effects - module is loaded if found.
371 */
372 int
373 load_one_module(const char *path)
374 {
375 dlink_node *ptr = NULL;
376 char modpath[PATH_MAX + 1];
377 struct stat statbuf;
378
379 DLINK_FOREACH(ptr, mod_paths.head)
380 {
381 const struct module_path *mpath = ptr->data;
382
383 snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path);
384
385 if (!modules_valid_suffix(path))
386 continue;
387
388 if (strstr(modpath, "../") == NULL &&
389 strstr(modpath, "/..") == NULL)
390 if (!stat(modpath, &statbuf))
391 if (S_ISREG(statbuf.st_mode)) /* Regular files only please */
392 return load_a_module(modpath, 1);
393 }
394
395 sendto_realops_flags(UMODE_ALL, L_ALL,
396 "Cannot locate module %s", path);
397 ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path);
398 return -1;
399 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision