ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/modules.c
Revision: 1247
Committed: Sat Oct 1 07:54:24 2011 UTC (13 years, 11 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/modules.c
File size: 14944 byte(s)
Log Message:
- Rewrite and cleanup half-broken logging subsystem.
  Logfile rotating is not working yet

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

Properties

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