ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/modules.c
Revision: 1237
Committed: Thu Sep 29 11:32:21 2011 UTC (13 years, 11 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/modules.c
File size: 14843 byte(s)
Log Message:
- move remaining functions in dynlink.c into modules.c

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

Properties

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