ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/modules.c
Revision: 1436
Committed: Sun Jun 17 19:31:28 2012 UTC (13 years, 2 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/modules.c
File size: 14135 byte(s)
Log Message:
- Removed "MODRESTART" command. "MODRELOAD *" will now reload all modules.

File Contents

# User Rev Content
1 adx 30 /*
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 knight 31 * $Id$
23 adx 30 */
24    
25 michael 1237 #include "ltdl.h"
26    
27 adx 30 #include "stdinc.h"
28 michael 1011 #include "list.h"
29 adx 30 #include "modules.h"
30 michael 1309 #include "log.h"
31 adx 30 #include "ircd.h"
32     #include "client.h"
33     #include "send.h"
34 michael 1309 #include "conf.h"
35 adx 30 #include "numeric.h"
36     #include "parse.h"
37     #include "ircd_defs.h"
38     #include "irc_string.h"
39     #include "memory.h"
40    
41    
42 michael 1238 static dlink_list modules_list = { NULL, NULL, 0 };
43 adx 30
44 michael 1237 static const char *unknown_ver = "<unknown>";
45    
46 adx 30 static const char *core_module_table[] =
47     {
48 michael 912 "m_die.la",
49 michael 946 "m_error.la",
50 michael 912 "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 adx 30 NULL
62     };
63    
64     static dlink_list mod_paths = { NULL, NULL, 0 };
65     static dlink_list conf_modules = { NULL, NULL, 0 };
66    
67     static void mo_modload(struct Client *, struct Client *, int, char *[]);
68     static void mo_modlist(struct Client *, struct Client *, int, char *[]);
69     static void mo_modreload(struct Client *, struct Client *, int, char *[]);
70     static void mo_modunload(struct Client *, struct Client *, int, char *[]);
71    
72     struct Message modload_msgtab = {
73     "MODLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
74     {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modload, m_ignore}
75     };
76    
77     struct Message modunload_msgtab = {
78     "MODUNLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
79     {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modunload, m_ignore}
80     };
81    
82     struct Message modreload_msgtab = {
83     "MODRELOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
84     {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modreload, m_ignore}
85     };
86    
87     struct Message modlist_msgtab = {
88     "MODLIST", 0, 0, 0, 0, MFLG_SLOW, 0,
89     {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modlist, m_ignore}
90     };
91    
92    
93 michael 1237 int
94     modules_valid_suffix(const char *name)
95     {
96     return ((name = strrchr(name, '.'))) && !strcmp(name, ".la");
97     }
98    
99     /* unload_one_module()
100     *
101     * inputs - name of module to unload
102     * - 1 to say modules unloaded, 0 to not
103     * output - 0 if successful, -1 if error
104     * side effects - module is unloaded
105     */
106     int
107     unload_one_module(const char *name, int warn)
108     {
109     struct module *modp = NULL;
110    
111     if ((modp = findmodule_byname(name)) == NULL)
112     return -1;
113    
114     if (modp->modexit)
115     modp->modexit();
116    
117 michael 1238 assert(dlink_list_length(&modules_list) > 0);
118     dlinkDelete(&modp->node, &modules_list);
119 michael 1237 MyFree(modp->name);
120    
121     lt_dlclose(modp->handle);
122    
123     if (warn == 1)
124     {
125 michael 1247 ilog(LOG_TYPE_IRCD, "Module %s unloaded", name);
126 michael 1237 sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name);
127     }
128    
129     return 0;
130     }
131    
132     /* load_a_module()
133     *
134     * inputs - path name of module, int to notice, int of core
135     * output - -1 if error 0 if success
136     * side effects - loads a module if successful
137     */
138     int
139 michael 1404 load_a_module(const char *path, int warn)
140 michael 1237 {
141     lt_dlhandle tmpptr = NULL;
142     const char *mod_basename = NULL;
143     struct module *modp = NULL;
144    
145     if (findmodule_byname((mod_basename = libio_basename(path))))
146     return 1;
147    
148     if (!(tmpptr = lt_dlopen(path))) {
149     const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
150    
151     sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s",
152     mod_basename, err);
153 michael 1247 ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
154 michael 1237 return -1;
155     }
156    
157     if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL)
158     {
159     sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no module_entry export",
160     mod_basename);
161 michael 1247 ilog(LOG_TYPE_IRCD, "Module %s has no module_entry export", mod_basename);
162 michael 1237 lt_dlclose(tmpptr);
163     return -1;
164     }
165    
166     modp->handle = tmpptr;
167    
168     if (EmptyString(modp->version))
169     modp->version = unknown_ver;
170    
171     DupString(modp->name, mod_basename);
172 michael 1238 dlinkAdd(modp, &modp->node, &modules_list);
173 michael 1237
174     if (modp->modinit)
175     modp->modinit();
176    
177     if (warn == 1)
178     {
179     sendto_realops_flags(UMODE_ALL, L_ALL,
180     "Module %s [version: %s handle: %p] loaded.",
181     modp->name, modp->version, tmpptr);
182 michael 1247 ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
183 michael 1237 modp->name, modp->version, tmpptr);
184     }
185    
186     return 0;
187     }
188    
189 adx 30 /*
190     * modules_init
191     *
192     * input - NONE
193     * output - NONE
194     * side effects - The basic module manipulation modules are loaded
195     */
196     void
197     modules_init(void)
198     {
199 michael 1237 if (lt_dlinit())
200     {
201 michael 1247 ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
202 michael 1237 " link library. Exiting.");
203     exit(0);
204     }
205 michael 912
206 adx 30 mod_add_cmd(&modload_msgtab);
207     mod_add_cmd(&modunload_msgtab);
208     mod_add_cmd(&modreload_msgtab);
209     mod_add_cmd(&modlist_msgtab);
210     }
211    
212     /* mod_find_path()
213     *
214     * input - path
215     * output - none
216     * side effects - returns a module path from path
217     */
218     static struct module_path *
219     mod_find_path(const char *path)
220     {
221     dlink_node *ptr;
222    
223     DLINK_FOREACH(ptr, mod_paths.head)
224     {
225 michael 1404 struct module_path *mpath = ptr->data;
226 adx 30
227     if (!strcmp(path, mpath->path))
228     return mpath;
229     }
230    
231     return NULL;
232     }
233    
234     /* mod_add_path()
235     *
236     * input - path
237     * output - NONE
238     * side effects - adds path to list
239     */
240     void
241     mod_add_path(const char *path)
242     {
243     struct module_path *pathst;
244    
245     if (mod_find_path(path))
246     return;
247    
248     pathst = MyMalloc(sizeof(struct module_path));
249    
250     strlcpy(pathst->path, path, sizeof(pathst->path));
251     dlinkAdd(pathst, &pathst->node, &mod_paths);
252     }
253    
254     /* add_conf_module
255     *
256     * input - module name
257     * output - NONE
258     * side effects - adds module to conf_mod
259     */
260     void
261     add_conf_module(const char *name)
262     {
263     struct module_path *pathst;
264    
265     pathst = MyMalloc(sizeof(struct module_path));
266    
267     strlcpy(pathst->path, name, sizeof(pathst->path));
268     dlinkAdd(pathst, &pathst->node, &conf_modules);
269     }
270    
271     /* mod_clear_paths()
272     *
273     * input - NONE
274     * output - NONE
275     * side effects - clear the lists of paths and conf modules
276     */
277     void
278     mod_clear_paths(void)
279     {
280 michael 1237 dlink_node *ptr = NULL, *next_ptr = NULL;
281 adx 30
282     DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
283     {
284 michael 1404 dlinkDelete(ptr, &mod_paths);
285     MyFree(ptr->data);
286 adx 30 }
287    
288     DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
289     {
290 michael 1404 dlinkDelete(ptr, &conf_modules);
291     MyFree(ptr->data);
292 adx 30 }
293     }
294    
295     /* findmodule_byname
296     *
297     * input - name of module
298     * output - NULL if not found or pointer to module
299     * side effects - NONE
300     */
301 michael 1230 struct module *
302 adx 30 findmodule_byname(const char *name)
303     {
304 michael 1230 dlink_node *ptr = NULL;
305 adx 30
306 michael 1238 DLINK_FOREACH(ptr, modules_list.head)
307 adx 30 {
308 michael 1230 struct module *modp = ptr->data;
309 adx 30
310 db 218 if (strcmp(modp->name, name) == 0)
311 michael 1230 return modp;
312 adx 30 }
313    
314     return NULL;
315     }
316    
317     /* load_all_modules()
318     *
319     * input - int flag warn
320     * output - NONE
321     * side effects - load all modules found in autoload directory
322     */
323     void
324     load_all_modules(int warn)
325     {
326     DIR *system_module_dir = NULL;
327     struct dirent *ldirent = NULL;
328     char module_fq_name[PATH_MAX + 1];
329    
330     if ((system_module_dir = opendir(AUTOMODPATH)) == NULL)
331     {
332 michael 1247 ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
333 adx 30 AUTOMODPATH, strerror(errno));
334     return;
335     }
336    
337     while ((ldirent = readdir(system_module_dir)) != NULL)
338     {
339 michael 912 if (modules_valid_suffix(ldirent->d_name))
340 adx 30 {
341     snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s",
342     AUTOMODPATH, ldirent->d_name);
343 michael 1404 load_a_module(module_fq_name, warn);
344 adx 30 }
345     }
346    
347     closedir(system_module_dir);
348     }
349    
350     /* load_conf_modules()
351     *
352     * input - NONE
353     * output - NONE
354     * side effects - load modules given in ircd.conf
355     */
356     void
357     load_conf_modules(void)
358     {
359     dlink_node *ptr = NULL;
360    
361     DLINK_FOREACH(ptr, conf_modules.head)
362     {
363 michael 1404 struct module_path *mpath = ptr->data;
364 adx 30
365     if (findmodule_byname(mpath->path) == NULL)
366 michael 1404 load_one_module(mpath->path);
367 adx 30 }
368     }
369    
370     /* load_core_modules()
371     *
372     * input - int flag warn
373     * output - NONE
374     * side effects - core modules are loaded, if any fail, kill ircd
375     */
376     void
377     load_core_modules(int warn)
378     {
379     char module_name[PATH_MAX + 1];
380     int i = 0;
381    
382     for (; core_module_table[i]; ++i)
383     {
384 michael 912 snprintf(module_name, sizeof(module_name), "%s%s",
385     MODPATH, core_module_table[i]);
386 adx 30
387 michael 1404 if (load_a_module(module_name, warn) == -1)
388 adx 30 {
389 michael 1247 ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
390 michael 912 core_module_table[i]);
391 adx 30 exit(EXIT_FAILURE);
392     }
393     }
394     }
395    
396     /* load_one_module()
397     *
398     * input - pointer to path
399     * - flagged as core module or not
400     * output - -1 if error
401     * side effects - module is loaded if found.
402     */
403     int
404 michael 1404 load_one_module(const char *path)
405 adx 30 {
406     dlink_node *ptr = NULL;
407     char modpath[PATH_MAX + 1];
408     struct stat statbuf;
409    
410     DLINK_FOREACH(ptr, mod_paths.head)
411     {
412     const struct module_path *mpath = ptr->data;
413    
414     snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path);
415    
416 michael 912 if (!modules_valid_suffix(path))
417     continue;
418    
419 adx 30 if (strstr(modpath, "../") == NULL &&
420     strstr(modpath, "/..") == NULL)
421     if (!stat(modpath, &statbuf))
422 michael 1404 if (S_ISREG(statbuf.st_mode)) /* Regular files only please */
423     return load_a_module(modpath, 1);
424 adx 30 }
425    
426     sendto_realops_flags(UMODE_ALL, L_ALL,
427     "Cannot locate module %s", path);
428 michael 1247 ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path);
429 adx 30 return -1;
430     }
431    
432     /* load a module .. */
433     static void
434     mo_modload(struct Client *client_p, struct Client *source_p,
435     int parc, char *parv[])
436     {
437 michael 978 const char *m_bn = NULL;
438 adx 30
439 michael 1228 if (!HasOFlag(source_p, OPER_FLAG_MODULE))
440 adx 30 {
441     sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
442     me.name, source_p->name);
443     return;
444     }
445    
446 michael 978 m_bn = libio_basename(parv[1]);
447 adx 30
448     if (findmodule_byname(m_bn) != NULL)
449     {
450     sendto_one(source_p, ":%s NOTICE %s :Module %s is already loaded",
451     me.name, source_p->name, m_bn);
452     return;
453     }
454    
455 michael 1404 load_one_module(parv[1]);
456 adx 30 }
457    
458     /* unload a module .. */
459     static void
460     mo_modunload(struct Client *client_p, struct Client *source_p,
461     int parc, char *parv[])
462     {
463 michael 978 const char *m_bn = NULL;
464 michael 1230 struct module *modp = NULL;
465 adx 30
466 michael 1228 if (!HasOFlag(source_p, OPER_FLAG_MODULE))
467 adx 30 {
468     sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
469     me.name, source_p->name);
470     return;
471     }
472    
473 michael 978 m_bn = libio_basename(parv[1]);
474 adx 30
475 michael 1230 if ((modp = findmodule_byname(m_bn)) == NULL)
476 adx 30 {
477     sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
478     me.name, source_p->name, m_bn);
479     return;
480     }
481    
482 michael 1230 if (modp->flags & MODULE_FLAG_CORE)
483 adx 30 {
484     sendto_one(source_p,
485     ":%s NOTICE %s :Module %s is a core module and may not be unloaded",
486     me.name, source_p->name, m_bn);
487     return;
488     }
489    
490     if (unload_one_module(m_bn, 1) == -1)
491     {
492     sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
493     me.name, source_p->name, m_bn);
494     }
495     }
496    
497     /* unload and load in one! */
498     static void
499     mo_modreload(struct Client *client_p, struct Client *source_p,
500     int parc, char *parv[])
501     {
502 michael 978 const char *m_bn = NULL;
503 michael 1230 struct module *modp = NULL;
504 adx 30 int check_core;
505    
506 michael 1228 if (!HasOFlag(source_p, OPER_FLAG_MODULE))
507 adx 30 {
508     sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
509     me.name, source_p->name);
510     return;
511     }
512    
513 michael 1436 if (!strcmp(parv[1], "*"))
514     {
515     unsigned int modnum = 0;
516     dlink_node *ptr = NULL, *ptr_next = NULL;
517    
518     sendto_one(source_p, ":%s NOTICE %s :Reloading all modules",
519     me.name, source_p->name);
520    
521     modnum = dlink_list_length(&modules_list);
522    
523     DLINK_FOREACH_SAFE(ptr, ptr_next, modules_list.head)
524     {
525     modp = ptr->data;
526     unload_one_module(modp->name, 0);
527     }
528    
529     load_all_modules(0);
530     load_conf_modules();
531     load_core_modules(0);
532    
533     sendto_realops_flags(UMODE_ALL, L_ALL,
534     "Module Restart: %u modules unloaded, %u modules loaded",
535     modnum, dlink_list_length(&modules_list));
536     ilog(LOG_TYPE_IRCD, "Module Restart: %u modules unloaded, %u modules loaded",
537     modnum, dlink_list_length(&modules_list));
538     return;
539     }
540    
541 michael 978 m_bn = libio_basename(parv[1]);
542 adx 30
543 michael 1230 if ((modp = findmodule_byname(m_bn)) == NULL)
544 adx 30 {
545     sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
546     me.name, source_p->name, m_bn);
547     return;
548     }
549    
550 michael 1230 check_core = (modp->flags & MODULE_FLAG_CORE) != 0;
551 adx 30
552     if (unload_one_module(m_bn, 1) == -1)
553     {
554     sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
555     me.name, source_p->name, m_bn);
556     return;
557     }
558    
559 michael 1404 if ((load_one_module(parv[1]) == -1) && check_core)
560 adx 30 {
561     sendto_realops_flags(UMODE_ALL, L_ALL, "Error reloading core "
562     "module: %s: terminating ircd", parv[1]);
563 michael 1247 ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd", parv[1]);
564 adx 30 exit(0);
565     }
566     }
567    
568     /* list modules .. */
569     static void
570     mo_modlist(struct Client *client_p, struct Client *source_p,
571     int parc, char *parv[])
572     {
573 michael 912 const dlink_node *ptr = NULL;
574 adx 30
575 michael 1228 if (!HasOFlag(source_p, OPER_FLAG_MODULE))
576 adx 30 {
577     sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
578     me.name, source_p->name);
579     return;
580     }
581    
582 michael 1238 DLINK_FOREACH(ptr, modules_list.head)
583 adx 30 {
584 michael 912 const struct module *modp = ptr->data;
585 adx 30
586 michael 912 if (parc > 1 && !match(parv[1], modp->name))
587     continue;
588    
589 michael 1234 sendto_one(source_p, form_str(RPL_MODLIST), me.name, source_p->name,
590 michael 912 modp->name, modp->handle,
591 michael 1230 modp->version, (modp->flags & MODULE_FLAG_CORE) ?"(core)":"");
592 adx 30 }
593    
594     sendto_one(source_p, form_str(RPL_ENDOFMODLIST),
595     me.name, source_p->name);
596     }

Properties

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