ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/modules.c
(Generate patch)

Comparing:
ircd-hybrid-7.2/src/modules.c (file contents), Revision 912 by michael, Wed Nov 7 22:47:44 2007 UTC vs.
ircd-hybrid/trunk/src/modules.c (file contents), Revision 2525 by michael, Sat Nov 2 17:07:38 2013 UTC

# Line 22 | Line 22
22   *  $Id$
23   */
24  
25 + #include "ltdl.h"
26 +
27   #include "stdinc.h"
28 < #include "tools.h"
28 > #include "list.h"
29   #include "modules.h"
30 < #include "s_log.h"
30 > #include "log.h"
31   #include "ircd.h"
32   #include "client.h"
33   #include "send.h"
34 < #include "s_conf.h"
33 < #include "handlers.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"
39 #include "list.h"
40  
41 /* -TimeMr14C:
42 * I have moved the dl* function definitions and
43 * the two functions (load_a_module / unload_a_module) to the
44 * file dynlink.c
45 * And also made the necessary changes to those functions
46 * to comply with shl_load and friends.
47 * In this file, to keep consistency with the makefile,
48 * I added the ability to load *.sl files, too.
49 * 27/02/2002
50 */
41  
42 < dlink_list mod_list = { NULL, NULL, 0 };
42 > dlink_list modules_list = { NULL, NULL, 0 };
43 >
44 > static dlink_list mod_paths = { NULL, NULL, 0 };
45 > static dlink_list conf_modules = { NULL, NULL, 0 };
46  
47 + static const char *unknown_ver = "<unknown>";
48   static const char *core_module_table[] =
49   {
50    "m_die.la",
51 +  "m_error.la",
52    "m_join.la",
53    "m_kick.la",
54    "m_kill.la",
# Line 68 | Line 63 | static const char *core_module_table[] =
63    NULL
64   };
65  
66 < static dlink_list mod_paths = { NULL, NULL, 0 };
67 < static dlink_list conf_modules = { NULL, NULL, 0 };
66 > int
67 > modules_valid_suffix(const char *name)
68 > {
69 >  return ((name = strrchr(name, '.'))) && !strcmp(name, ".la");
70 > }
71  
72 < static void mo_modload(struct Client *, struct Client *, int, char *[]);
73 < static void mo_modlist(struct Client *, struct Client *, int, char *[]);
74 < static void mo_modreload(struct Client *, struct Client *, int, char *[]);
75 < static void mo_modunload(struct Client *, struct Client *, int, char *[]);
76 < static void mo_modrestart(struct Client *, struct Client *, int, char *[]);
77 <
78 < struct Message modload_msgtab = {
79 < "MODLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
80 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modload, m_ignore}
81 < };
72 > /* unload_one_module()
73 > *
74 > * inputs       - name of module to unload
75 > *              - 1 to say modules unloaded, 0 to not
76 > * output       - 0 if successful, -1 if error
77 > * side effects - module is unloaded
78 > */
79 > int
80 > unload_one_module(const char *name, int warn)
81 > {
82 >  struct module *modp = NULL;
83  
84 < struct Message modunload_msgtab = {
85 < "MODUNLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
87 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modunload, m_ignore}
88 < };
84 >  if ((modp = findmodule_byname(name)) == NULL)
85 >    return -1;
86  
87 < struct Message modreload_msgtab = {
88 <  "MODRELOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
92 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modreload, m_ignore}
93 < };
87 >  if (modp->modexit)
88 >   modp->modexit();
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}
98 < };
90 >  assert(dlink_list_length(&modules_list) > 0);
91 >  dlinkDelete(&modp->node, &modules_list);
92 >  MyFree(modp->name);
93  
94 < struct Message modrestart_msgtab = {
95 < "MODRESTART", 0, 0, 0, 0, MFLG_SLOW, 0,
96 < {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modrestart, m_ignore}
97 < };
94 >  lt_dlclose(modp->handle);
95 >
96 >  if (warn == 1)
97 >  {
98 >    ilog(LOG_TYPE_IRCD, "Module %s unloaded", name);
99 >    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
100 >                         "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 < extern struct Message error_msgtab;
122 >  if (!(tmpptr = lt_dlopen(path)))
123 >  {
124 >    const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
125 >
126 >    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
127 >                         "Error loading module %s: %s",
128 >                         mod_basename, err);
129 >    ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
130 >    return -1;
131 >  }
132 >
133 >  if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL)
134 >  {
135 >    const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
136 >
137 >    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
138 >                         "Error loading module %s: %s",
139 >                         mod_basename, err);
140 >    ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
141 >    lt_dlclose(tmpptr);
142 >    return -1;
143 >  }
144 >
145 >  modp->handle = tmpptr;
146 >
147 >  if (EmptyString(modp->version))
148 >    modp->version = unknown_ver;
149 >
150 >  modp->name = xstrdup(mod_basename);
151 >  dlinkAdd(modp, &modp->node, &modules_list);
152 >
153 >  if (modp->modinit)
154 >    modp->modinit();
155 >
156 >  if (warn == 1)
157 >  {
158 >    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
159 >                         "Module %s [version: %s handle: %p] loaded.",
160 >                         modp->name, modp->version, tmpptr);
161 >    ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
162 >         modp->name, modp->version, tmpptr);
163 >  }
164 >
165 >  return 0;
166 > }
167  
168   /*
169   * modules_init
# Line 115 | Line 175 | extern struct Message error_msgtab;
175   void
176   modules_init(void)
177   {
178 <  dynlink_init();
179 <
180 <  mod_add_cmd(&modload_msgtab);
181 <  mod_add_cmd(&modunload_msgtab);
182 <  mod_add_cmd(&modreload_msgtab);
183 <  mod_add_cmd(&modlist_msgtab);
124 <  mod_add_cmd(&modrestart_msgtab);
125 <  mod_add_cmd(&error_msgtab);
178 >  if (lt_dlinit())
179 >  {
180 >    ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
181 >         " link library. Exiting.");
182 >    exit(0);
183 >  }
184   }
185  
186   /* mod_find_path()
# Line 135 | Line 193 | static struct module_path *
193   mod_find_path(const char *path)
194   {
195    dlink_node *ptr;
138  struct module_path *mpath;
196  
197    DLINK_FOREACH(ptr, mod_paths.head)
198    {
199 <    mpath = ptr->data;
199 >    struct module_path *mpath = ptr->data;
200  
201      if (!strcmp(path, mpath->path))
202        return mpath;
# Line 194 | Line 251 | add_conf_module(const char *name)
251   void
252   mod_clear_paths(void)
253   {
254 <  struct module_path *pathst;
198 <  dlink_node *ptr;
199 <  dlink_node *next_ptr;
254 >  dlink_node *ptr = NULL, *next_ptr = NULL;
255  
256    DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
257    {
258 <    pathst = ptr->data;
259 <
205 <    dlinkDelete(&pathst->node, &mod_paths);
206 <    MyFree(pathst);
258 >    dlinkDelete(ptr, &mod_paths);
259 >    MyFree(ptr->data);
260    }
261  
262    DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
263    {
264 <    pathst = ptr->data;
265 <
213 <    dlinkDelete(&pathst->node, &conf_modules);
214 <    MyFree(pathst);
264 >    dlinkDelete(ptr, &conf_modules);
265 >    MyFree(ptr->data);
266    }
267   }
268  
# Line 221 | Line 272 | mod_clear_paths(void)
272   * output       - NULL if not found or pointer to module
273   * side effects - NONE
274   */
275 < dlink_node *
275 > struct module *
276   findmodule_byname(const char *name)
277   {
278 <  dlink_node *ptr;
228 <  struct module *modp;
278 >  dlink_node *ptr = NULL;
279  
280 <  DLINK_FOREACH(ptr, mod_list.head)
280 >  DLINK_FOREACH(ptr, modules_list.head)
281    {
282 <    modp = ptr->data;
282 >    struct module *modp = ptr->data;
283  
284      if (strcmp(modp->name, name) == 0)
285 <      return ptr;
285 >      return modp;
286    }
287  
288    return NULL;
# Line 249 | Line 299 | load_all_modules(int warn)
299   {
300    DIR *system_module_dir = NULL;
301    struct dirent *ldirent = NULL;
302 <  char module_fq_name[PATH_MAX + 1];
253 <
254 <  modules_init();
302 >  char module_fq_name[HYB_PATH_MAX + 1];
303  
304    if ((system_module_dir = opendir(AUTOMODPATH)) == NULL)
305    {
306 <    ilog(L_WARN, "Could not load modules from %s: %s",
306 >    ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
307           AUTOMODPATH, strerror(errno));
308      return;
309    }
# Line 266 | Line 314 | load_all_modules(int warn)
314      {
315         snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s",
316                  AUTOMODPATH, ldirent->d_name);
317 <       load_a_module(module_fq_name, warn, 0);
317 >       load_a_module(module_fq_name, warn);
318      }
319    }
320  
# Line 283 | Line 331 | void
331   load_conf_modules(void)
332   {
333    dlink_node *ptr = NULL;
286  struct module_path *mpath = NULL;
334  
335    DLINK_FOREACH(ptr, conf_modules.head)
336    {
337 <    mpath = ptr->data;
337 >    struct module_path *mpath = ptr->data;
338  
339      if (findmodule_byname(mpath->path) == NULL)
340 <      load_one_module(mpath->path, 0);
340 >      load_one_module(mpath->path);
341    }
342   }
343  
# Line 303 | Line 350 | load_conf_modules(void)
350   void
351   load_core_modules(int warn)
352   {
353 <  char module_name[PATH_MAX + 1];
353 >  char module_name[HYB_PATH_MAX + 1];
354    int i = 0;
355  
356    for (; core_module_table[i]; ++i)
# Line 311 | Line 358 | load_core_modules(int warn)
358      snprintf(module_name, sizeof(module_name), "%s%s",
359               MODPATH, core_module_table[i]);
360  
361 <    if (load_a_module(module_name, warn, 1) == -1)
361 >    if (load_a_module(module_name, warn) == -1)
362      {
363 <      ilog(L_CRIT, "Error loading core module %s: terminating ircd",
363 >      ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
364             core_module_table[i]);
365        exit(EXIT_FAILURE);
366      }
# Line 324 | Line 371 | load_core_modules(int warn)
371   *
372   * input        - pointer to path
373   *              - flagged as core module or not
374 < * output       - -1 if error
374 > * output       - -1 if error
375   * side effects - module is loaded if found.
376   */
377   int
378 < load_one_module(char *path, int coremodule)
378 > load_one_module(const char *path)
379   {
380    dlink_node *ptr = NULL;
381 <  char modpath[PATH_MAX + 1];
381 >  char modpath[HYB_PATH_MAX + 1];
382    struct stat statbuf;
383  
384    DLINK_FOREACH(ptr, mod_paths.head)
# Line 344 | Line 391 | load_one_module(char *path, int coremodu
391        continue;
392  
393      if (strstr(modpath, "../") == NULL &&
394 <        strstr(modpath, "/..") == NULL)
348 <    {
394 >        strstr(modpath, "/..") == NULL)
395        if (!stat(modpath, &statbuf))
396 <      {
397 <        if (S_ISREG(statbuf.st_mode))
352 <        {
353 <          /* Regular files only please */
354 <          return load_a_module(modpath, 1, coremodule);
355 <        }
356 <      }
357 <    }
396 >        if (S_ISREG(statbuf.st_mode))  /* Regular files only please */
397 >          return load_a_module(modpath, 1);
398    }
399  
400 <  sendto_realops_flags(UMODE_ALL, L_ALL,
400 >  sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
401                         "Cannot locate module %s", path);
402 <  ilog(L_WARN, "Cannot locate module %s", path);
402 >  ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path);
403    return -1;
404   }
365
366 /* load a module .. */
367 static void
368 mo_modload(struct Client *client_p, struct Client *source_p,
369           int parc, char *parv[])
370 {
371  char *m_bn;
372
373  if (!IsAdmin(source_p))
374  {
375    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
376               me.name, source_p->name);
377    return;
378  }
379
380  m_bn = basename(parv[1]);
381
382  if (findmodule_byname(m_bn) != NULL)
383  {
384    sendto_one(source_p, ":%s NOTICE %s :Module %s is already loaded",
385               me.name, source_p->name, m_bn);
386    return;
387  }
388
389  load_one_module(parv[1], 0);
390 }
391
392 /* unload a module .. */
393 static void
394 mo_modunload(struct Client *client_p, struct Client *source_p,
395             int parc, char *parv[])
396 {
397  char *m_bn;
398  dlink_node *ptr;
399  struct module *modp;
400
401  if (!IsAdmin(source_p))
402  {
403    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
404               me.name, source_p->name);
405    return;
406  }
407
408  m_bn = basename(parv[1]);
409
410  if ((ptr = findmodule_byname(m_bn)) == NULL)
411  {
412    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
413               me.name, source_p->name, m_bn);
414    return;
415  }
416
417  modp = ptr->data;
418
419  if (modp->core == 1)
420  {
421    sendto_one(source_p,
422               ":%s NOTICE %s :Module %s is a core module and may not be unloaded",
423               me.name, source_p->name, m_bn);
424    return;
425  }
426
427  /* XXX might want to simply un dlink it here */
428
429  if (unload_one_module(m_bn, 1) == -1)
430  {
431    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
432               me.name, source_p->name, m_bn);
433  }
434 }
435
436 /* unload and load in one! */
437 static void
438 mo_modreload(struct Client *client_p, struct Client *source_p,
439             int parc, char *parv[])
440 {
441  char *m_bn;
442  dlink_node *ptr;
443  struct module *modp;
444  int check_core;
445
446  if (!IsAdmin(source_p))
447  {
448    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
449               me.name, source_p->name);
450    return;
451  }
452
453  m_bn = basename(parv[1]);
454
455  if ((ptr = findmodule_byname(m_bn)) == NULL)
456  {
457    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
458               me.name, source_p->name, m_bn);
459    return;
460  }
461
462  modp = ptr->data;
463  check_core = modp->core;
464
465  if (unload_one_module(m_bn, 1) == -1)
466  {
467    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
468               me.name, source_p->name, m_bn);
469    return;
470  }
471
472  if ((load_one_module(parv[1], check_core) == -1) && check_core)
473  {
474    sendto_realops_flags(UMODE_ALL, L_ALL, "Error reloading core "
475                         "module: %s: terminating ircd", parv[1]);
476    ilog(L_CRIT, "Error loading core module %s: terminating ircd", parv[1]);
477    exit(0);
478  }
479 }
480
481 /* list modules .. */
482 static void
483 mo_modlist(struct Client *client_p, struct Client *source_p,
484           int parc, char *parv[])
485 {
486  const dlink_node *ptr = NULL;
487
488  if (!IsAdmin(source_p))
489  {
490    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
491               me.name, source_p->name);
492    return;
493  }
494
495  DLINK_FOREACH(ptr, mod_list.head)
496  {
497    const struct module *modp = ptr->data;
498
499    if (parc > 1 && !match(parv[1], modp->name))
500      continue;
501
502    sendto_one(source_p, form_str(RPL_MODLIST), me.name, parv[0],
503               modp->name, modp->handle,
504               modp->version, modp->core?"(core)":"");
505  }
506
507  sendto_one(source_p, form_str(RPL_ENDOFMODLIST),
508             me.name, source_p->name);
509 }
510
511 /* unload and reload all modules */
512 static void
513 mo_modrestart(struct Client *client_p, struct Client *source_p,
514              int parc, char *parv[])
515 {
516  unsigned int modnum = 0;
517  dlink_node *ptr = NULL, *ptr_next = NULL;
518
519  if (!IsAdmin(source_p))
520  {
521    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
522               me.name, source_p->name);
523    return;
524  }
525
526  sendto_one(source_p, ":%s NOTICE %s :Reloading all modules",
527             me.name, source_p->name);
528
529  modnum = dlink_list_length(&mod_list);
530
531  DLINK_FOREACH_SAFE(ptr, ptr_next, mod_list.head)
532  {
533    struct module *modp = ptr->data;
534    unload_one_module(modp->name, 0);
535  }
536
537  load_all_modules(0);
538  load_conf_modules();
539  load_core_modules(0);
540
541  sendto_realops_flags(UMODE_ALL, L_ALL,
542              "Module Restart: %u modules unloaded, %lu modules loaded",
543                        modnum, dlink_list_length(&mod_list));
544  ilog(L_WARN, "Module Restart: %u modules unloaded, %lu modules loaded",
545       modnum, dlink_list_length(&mod_list));
546 }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)