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 978 by michael, Sun Aug 9 09:47:58 2009 UTC vs.
ircd-hybrid-8/src/modules.c (file contents), Revision 1574 by michael, Wed Oct 17 22:08:15 2012 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  
42 < dlink_list mod_list = { NULL, NULL, 0 };
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   {
# Line 62 | Line 64 | static const char *core_module_table[] =
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 *[]);
70 <
71 < struct Message modload_msgtab = {
72 < "MODLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
73 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modload, m_ignore}
74 < };
67 > int
68 > modules_valid_suffix(const char *name)
69 > {
70 >  return ((name = strrchr(name, '.'))) && !strcmp(name, ".la");
71 > }
72  
73 < struct Message modunload_msgtab = {
74 < "MODUNLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
75 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modunload, m_ignore}
76 < };
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 < struct Message modreload_msgtab = {
86 <  "MODRELOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
83 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modreload, m_ignore}
84 < };
85 >  if ((modp = findmodule_byname(name)) == NULL)
86 >    return -1;
87  
88 < struct Message modlist_msgtab = {
89 < "MODLIST", 0, 0, 0, 0, MFLG_SLOW, 0,
88 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modlist, m_ignore}
89 < };
88 >  if (modp->modexit)
89 >   modp->modexit();
90  
91 < struct Message modrestart_msgtab = {
92 < "MODRESTART", 0, 0, 0, 0, MFLG_SLOW, 0,
93 < {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modrestart, m_ignore}
94 < };
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 >    const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
134 >
135 >    sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s",
136 >                         mod_basename, err);
137 >    ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
138 >    lt_dlclose(tmpptr);
139 >    return -1;
140 >  }
141 >
142 >  modp->handle = tmpptr;
143  
144 +  if (EmptyString(modp->version))
145 +    modp->version = unknown_ver;
146  
147 < extern struct Message error_msgtab;
147 >  DupString(modp->name, mod_basename);
148 >  dlinkAdd(modp, &modp->node, &modules_list);
149 >
150 >  if (modp->modinit)
151 >    modp->modinit();
152 >
153 >  if (warn == 1)
154 >  {
155 >    sendto_realops_flags(UMODE_ALL, L_ALL,
156 >                         "Module %s [version: %s handle: %p] loaded.",
157 >                         modp->name, modp->version, tmpptr);
158 >    ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
159 >         modp->name, modp->version, tmpptr);
160 >  }
161 >
162 >  return 0;
163 > }
164  
165   /*
166   * modules_init
# Line 106 | Line 172 | extern struct Message error_msgtab;
172   void
173   modules_init(void)
174   {
175 <  dynlink_init();
176 <
177 <  mod_add_cmd(&modload_msgtab);
178 <  mod_add_cmd(&modunload_msgtab);
179 <  mod_add_cmd(&modreload_msgtab);
180 <  mod_add_cmd(&modlist_msgtab);
115 <  mod_add_cmd(&modrestart_msgtab);
175 >  if (lt_dlinit())
176 >  {
177 >    ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
178 >         " link library. Exiting.");
179 >    exit(0);
180 >  }
181   }
182  
183   /* mod_find_path()
# Line 125 | Line 190 | static struct module_path *
190   mod_find_path(const char *path)
191   {
192    dlink_node *ptr;
128 –  struct module_path *mpath;
193  
194    DLINK_FOREACH(ptr, mod_paths.head)
195    {
196 <    mpath = ptr->data;
196 >    struct module_path *mpath = ptr->data;
197  
198      if (!strcmp(path, mpath->path))
199        return mpath;
# Line 184 | Line 248 | add_conf_module(const char *name)
248   void
249   mod_clear_paths(void)
250   {
251 <  struct module_path *pathst;
188 <  dlink_node *ptr;
189 <  dlink_node *next_ptr;
251 >  dlink_node *ptr = NULL, *next_ptr = NULL;
252  
253    DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
254    {
255 <    pathst = ptr->data;
256 <
195 <    dlinkDelete(&pathst->node, &mod_paths);
196 <    MyFree(pathst);
255 >    dlinkDelete(ptr, &mod_paths);
256 >    MyFree(ptr->data);
257    }
258  
259    DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
260    {
261 <    pathst = ptr->data;
262 <
203 <    dlinkDelete(&pathst->node, &conf_modules);
204 <    MyFree(pathst);
261 >    dlinkDelete(ptr, &conf_modules);
262 >    MyFree(ptr->data);
263    }
264   }
265  
# Line 211 | Line 269 | mod_clear_paths(void)
269   * output       - NULL if not found or pointer to module
270   * side effects - NONE
271   */
272 < dlink_node *
272 > struct module *
273   findmodule_byname(const char *name)
274   {
275 <  dlink_node *ptr;
218 <  struct module *modp;
275 >  dlink_node *ptr = NULL;
276  
277 <  DLINK_FOREACH(ptr, mod_list.head)
277 >  DLINK_FOREACH(ptr, modules_list.head)
278    {
279 <    modp = ptr->data;
279 >    struct module *modp = ptr->data;
280  
281      if (strcmp(modp->name, name) == 0)
282 <      return ptr;
282 >      return modp;
283    }
284  
285    return NULL;
# Line 241 | Line 298 | load_all_modules(int warn)
298    struct dirent *ldirent = NULL;
299    char module_fq_name[PATH_MAX + 1];
300  
244 –  modules_init();
245 –
301    if ((system_module_dir = opendir(AUTOMODPATH)) == NULL)
302    {
303 <    ilog(L_WARN, "Could not load modules from %s: %s",
303 >    ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
304           AUTOMODPATH, strerror(errno));
305      return;
306    }
# Line 256 | Line 311 | load_all_modules(int warn)
311      {
312         snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s",
313                  AUTOMODPATH, ldirent->d_name);
314 <       load_a_module(module_fq_name, warn, 0);
314 >       load_a_module(module_fq_name, warn);
315      }
316    }
317  
# Line 273 | Line 328 | void
328   load_conf_modules(void)
329   {
330    dlink_node *ptr = NULL;
276 –  struct module_path *mpath = NULL;
331  
332    DLINK_FOREACH(ptr, conf_modules.head)
333    {
334 <    mpath = ptr->data;
334 >    struct module_path *mpath = ptr->data;
335  
336      if (findmodule_byname(mpath->path) == NULL)
337 <      load_one_module(mpath->path, 0);
337 >      load_one_module(mpath->path);
338    }
339   }
340  
# Line 301 | Line 355 | load_core_modules(int warn)
355      snprintf(module_name, sizeof(module_name), "%s%s",
356               MODPATH, core_module_table[i]);
357  
358 <    if (load_a_module(module_name, warn, 1) == -1)
358 >    if (load_a_module(module_name, warn) == -1)
359      {
360 <      ilog(L_CRIT, "Error loading core module %s: terminating ircd",
360 >      ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
361             core_module_table[i]);
362        exit(EXIT_FAILURE);
363      }
# Line 318 | Line 372 | load_core_modules(int warn)
372   * side effects - module is loaded if found.
373   */
374   int
375 < load_one_module(const char *path, int coremodule)
375 > load_one_module(const char *path)
376   {
377    dlink_node *ptr = NULL;
378    char modpath[PATH_MAX + 1];
# Line 335 | Line 389 | load_one_module(const char *path, int co
389  
390      if (strstr(modpath, "../") == NULL &&
391          strstr(modpath, "/..") == NULL)
338 –    {
392        if (!stat(modpath, &statbuf))
393 <      {
394 <        if (S_ISREG(statbuf.st_mode))
342 <        {
343 <          /* Regular files only please */
344 <          return load_a_module(modpath, 1, coremodule);
345 <        }
346 <      }
347 <    }
393 >        if (S_ISREG(statbuf.st_mode))  /* Regular files only please */
394 >          return load_a_module(modpath, 1);
395    }
396  
397    sendto_realops_flags(UMODE_ALL, L_ALL,
398                         "Cannot locate module %s", path);
399 <  ilog(L_WARN, "Cannot locate module %s", path);
399 >  ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path);
400    return -1;
401   }
355 –
356 – /* load a module .. */
357 – static void
358 – mo_modload(struct Client *client_p, struct Client *source_p,
359 –           int parc, char *parv[])
360 – {
361 –  const char *m_bn = NULL;
362 –
363 –  if (!IsAdmin(source_p))
364 –  {
365 –    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
366 –               me.name, source_p->name);
367 –    return;
368 –  }
369 –
370 –  m_bn = libio_basename(parv[1]);
371 –
372 –  if (findmodule_byname(m_bn) != NULL)
373 –  {
374 –    sendto_one(source_p, ":%s NOTICE %s :Module %s is already loaded",
375 –               me.name, source_p->name, m_bn);
376 –    return;
377 –  }
378 –
379 –  load_one_module(parv[1], 0);
380 – }
381 –
382 – /* unload a module .. */
383 – static void
384 – mo_modunload(struct Client *client_p, struct Client *source_p,
385 –             int parc, char *parv[])
386 – {
387 –  const char *m_bn = NULL;
388 –  dlink_node *ptr;
389 –  struct module *modp;
390 –
391 –  if (!IsAdmin(source_p))
392 –  {
393 –    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
394 –               me.name, source_p->name);
395 –    return;
396 –  }
397 –
398 –  m_bn = libio_basename(parv[1]);
399 –
400 –  if ((ptr = findmodule_byname(m_bn)) == NULL)
401 –  {
402 –    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
403 –               me.name, source_p->name, m_bn);
404 –    return;
405 –  }
406 –
407 –  modp = ptr->data;
408 –
409 –  if (modp->core == 1)
410 –  {
411 –    sendto_one(source_p,
412 –               ":%s NOTICE %s :Module %s is a core module and may not be unloaded",
413 –               me.name, source_p->name, m_bn);
414 –    return;
415 –  }
416 –
417 –  /* XXX might want to simply un dlink it here */
418 –
419 –  if (unload_one_module(m_bn, 1) == -1)
420 –  {
421 –    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
422 –               me.name, source_p->name, m_bn);
423 –  }
424 – }
425 –
426 – /* unload and load in one! */
427 – static void
428 – mo_modreload(struct Client *client_p, struct Client *source_p,
429 –             int parc, char *parv[])
430 – {
431 –  const char *m_bn = NULL;
432 –  dlink_node *ptr;
433 –  struct module *modp;
434 –  int check_core;
435 –
436 –  if (!IsAdmin(source_p))
437 –  {
438 –    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
439 –               me.name, source_p->name);
440 –    return;
441 –  }
442 –
443 –  m_bn = libio_basename(parv[1]);
444 –
445 –  if ((ptr = findmodule_byname(m_bn)) == NULL)
446 –  {
447 –    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
448 –               me.name, source_p->name, m_bn);
449 –    return;
450 –  }
451 –
452 –  modp = ptr->data;
453 –  check_core = modp->core;
454 –
455 –  if (unload_one_module(m_bn, 1) == -1)
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 –  if ((load_one_module(parv[1], check_core) == -1) && check_core)
463 –  {
464 –    sendto_realops_flags(UMODE_ALL, L_ALL, "Error reloading core "
465 –                         "module: %s: terminating ircd", parv[1]);
466 –    ilog(L_CRIT, "Error loading core module %s: terminating ircd", parv[1]);
467 –    exit(0);
468 –  }
469 – }
470 –
471 – /* list modules .. */
472 – static void
473 – mo_modlist(struct Client *client_p, struct Client *source_p,
474 –           int parc, char *parv[])
475 – {
476 –  const dlink_node *ptr = NULL;
477 –
478 –  if (!IsAdmin(source_p))
479 –  {
480 –    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
481 –               me.name, source_p->name);
482 –    return;
483 –  }
484 –
485 –  DLINK_FOREACH(ptr, mod_list.head)
486 –  {
487 –    const struct module *modp = ptr->data;
488 –
489 –    if (parc > 1 && !match(parv[1], modp->name))
490 –      continue;
491 –
492 –    sendto_one(source_p, form_str(RPL_MODLIST), me.name, parv[0],
493 –               modp->name, modp->handle,
494 –               modp->version, modp->core?"(core)":"");
495 –  }
496 –
497 –  sendto_one(source_p, form_str(RPL_ENDOFMODLIST),
498 –             me.name, source_p->name);
499 – }
500 –
501 – /* unload and reload all modules */
502 – static void
503 – mo_modrestart(struct Client *client_p, struct Client *source_p,
504 –              int parc, char *parv[])
505 – {
506 –  unsigned int modnum = 0;
507 –  dlink_node *ptr = NULL, *ptr_next = NULL;
508 –
509 –  if (!IsAdmin(source_p))
510 –  {
511 –    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
512 –               me.name, source_p->name);
513 –    return;
514 –  }
515 –
516 –  sendto_one(source_p, ":%s NOTICE %s :Reloading all modules",
517 –             me.name, source_p->name);
518 –
519 –  modnum = dlink_list_length(&mod_list);
520 –
521 –  DLINK_FOREACH_SAFE(ptr, ptr_next, mod_list.head)
522 –  {
523 –    struct module *modp = ptr->data;
524 –    unload_one_module(modp->name, 0);
525 –  }
526 –
527 –  load_all_modules(0);
528 –  load_conf_modules();
529 –  load_core_modules(0);
530 –
531 –  sendto_realops_flags(UMODE_ALL, L_ALL,
532 –              "Module Restart: %u modules unloaded, %lu modules loaded",
533 –                        modnum, dlink_list_length(&mod_list));
534 –  ilog(L_WARN, "Module Restart: %u modules unloaded, %lu modules loaded",
535 –       modnum, dlink_list_length(&mod_list));
536 – }

Diff Legend

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