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-8/src/modules.c (file contents), Revision 1237 by michael, Thu Sep 29 11:32:21 2011 UTC vs.
ircd-hybrid/trunk/src/modules.c (file contents), Revision 1592 by michael, Sat Oct 27 21:02:32 2012 UTC

# Line 27 | Line 27
27   #include "stdinc.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"
35 < #include "handlers.h"
34 > #include "conf.h"
35   #include "numeric.h"
36   #include "parse.h"
37   #include "ircd_defs.h"
# Line 40 | Line 39
39   #include "memory.h"
40  
41  
42 < dlink_list mod_list = { NULL, NULL, 0 };
44 <
42 > dlink_list modules_list = { NULL, NULL, 0 };
43  
44   static const char *unknown_ver = "<unknown>";
45  
# Line 66 | 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  
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
67   int
68   modules_valid_suffix(const char *name)
69   {
# Line 122 | Line 88 | unload_one_module(const char *name, int
88    if (modp->modexit)
89     modp->modexit();
90  
91 <  assert(dlink_list_length(&mod_list) > 0);
92 <  dlinkDelete(&modp->node, &mod_list);
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(L_INFO, "Module %s unloaded", name);
99 >    ilog(LOG_TYPE_IRCD, "Module %s unloaded", name);
100      sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name);
101    }
102  
# Line 144 | Line 110 | unload_one_module(const char *name, int
110   * side effects - loads a module if successful
111   */
112   int
113 < load_a_module(const char *path, int warn, int core)
113 > load_a_module(const char *path, int warn)
114   {
115    lt_dlhandle tmpptr = NULL;
116    const char *mod_basename = NULL;
# Line 158 | Line 124 | load_a_module(const char *path, int warn
124  
125      sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s",
126                           mod_basename, err);
127 <    ilog(L_WARN, "Error loading module %s: %s", 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 <    sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no module_entry export",
134 <                         mod_basename);
135 <    ilog(L_WARN, "Module %s has no module_entry export", mod_basename);
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    }
# Line 176 | Line 144 | load_a_module(const char *path, int warn
144    if (EmptyString(modp->version))
145      modp->version = unknown_ver;
146  
179  if (core)
180    modp->flags |= MODULE_FLAG_CORE;
181
147    DupString(modp->name, mod_basename);
148 <  dlinkAdd(modp, &modp->node, &mod_list);
148 >  dlinkAdd(modp, &modp->node, &modules_list);
149  
150    if (modp->modinit)
151      modp->modinit();
# Line 190 | Line 155 | load_a_module(const char *path, int warn
155      sendto_realops_flags(UMODE_ALL, L_ALL,
156                           "Module %s [version: %s handle: %p] loaded.",
157                           modp->name, modp->version, tmpptr);
158 <    ilog(L_WARN, "Module %s [version: %s handle: %p] loaded.",
158 >    ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
159           modp->name, modp->version, tmpptr);
160    }
161  
# Line 209 | Line 174 | modules_init(void)
174   {
175    if (lt_dlinit())
176    {
177 <    ilog(L_ERROR, "Couldn't initialize the libltdl run time dynamic"
177 >    ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
178           " link library. Exiting.");
179      exit(0);
180    }
216
217  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);
181   }
182  
183   /* mod_find_path()
# Line 231 | Line 190 | static struct module_path *
190   mod_find_path(const char *path)
191   {
192    dlink_node *ptr;
234  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 290 | Line 248 | add_conf_module(const char *name)
248   void
249   mod_clear_paths(void)
250   {
293  struct module_path *pathst = NULL;
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 <
300 <    dlinkDelete(&pathst->node, &mod_paths);
301 <    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 <
308 <    dlinkDelete(&pathst->node, &conf_modules);
309 <    MyFree(pathst);
261 >    dlinkDelete(ptr, &conf_modules);
262 >    MyFree(ptr->data);
263    }
264   }
265  
# Line 321 | Line 274 | findmodule_byname(const char *name)
274   {
275    dlink_node *ptr = NULL;
276  
277 <  DLINK_FOREACH(ptr, mod_list.head)
277 >  DLINK_FOREACH(ptr, modules_list.head)
278    {
279      struct module *modp = ptr->data;
280  
# Line 345 | Line 298 | load_all_modules(int warn)
298    struct dirent *ldirent = NULL;
299    char module_fq_name[PATH_MAX + 1];
300  
348  modules_init();
349
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 360 | 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 377 | Line 328 | void
328   load_conf_modules(void)
329   {
330    dlink_node *ptr = NULL;
380  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 405 | 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 422 | 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 439 | Line 389 | load_one_module(const char *path, int co
389  
390      if (strstr(modpath, "../") == NULL &&
391          strstr(modpath, "/..") == NULL)
442    {
392        if (!stat(modpath, &statbuf))
393 <      {
394 <        if (S_ISREG(statbuf.st_mode))
446 <        {
447 <          /* Regular files only please */
448 <          return load_a_module(modpath, 1, coremodule);
449 <        }
450 <      }
451 <    }
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   }
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  const char *m_bn = NULL;
466
467  if (!HasOFlag(source_p, OPER_FLAG_MODULE))
468  {
469    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
470               me.name, source_p->name);
471    return;
472  }
473
474  m_bn = libio_basename(parv[1]);
475
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  const char *m_bn = NULL;
492  struct module *modp = NULL;
493
494  if (!HasOFlag(source_p, OPER_FLAG_MODULE))
495  {
496    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
497               me.name, source_p->name);
498    return;
499  }
500
501  m_bn = libio_basename(parv[1]);
502
503  if ((modp = findmodule_byname(m_bn)) == NULL)
504  {
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  if (modp->flags & MODULE_FLAG_CORE)
511  {
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  const char *m_bn = NULL;
531  struct module *modp = NULL;
532  int check_core;
533
534  if (!HasOFlag(source_p, OPER_FLAG_MODULE))
535  {
536    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
537               me.name, source_p->name);
538    return;
539  }
540
541  m_bn = libio_basename(parv[1]);
542
543  if ((modp = findmodule_byname(m_bn)) == NULL)
544  {
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  check_core = (modp->flags & MODULE_FLAG_CORE) != 0;
551
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  const dlink_node *ptr = NULL;
574
575  if (!HasOFlag(source_p, OPER_FLAG_MODULE))
576  {
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    const struct module *modp = ptr->data;
585
586    if (parc > 1 && !match(parv[1], modp->name))
587      continue;
588
589    sendto_one(source_p, form_str(RPL_MODLIST), me.name, source_p->name,
590               modp->name, modp->handle,
591               modp->version, (modp->flags & MODULE_FLAG_CORE) ?"(core)":"");
592  }
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  dlink_node *ptr = NULL, *ptr_next = NULL;
605
606  if (!HasOFlag(source_p, OPER_FLAG_MODULE))
607  {
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  DLINK_FOREACH_SAFE(ptr, ptr_next, mod_list.head)
619  {
620    struct module *modp = ptr->data;
621    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              "Module Restart: %u modules unloaded, %u modules loaded",
630                        modnum, dlink_list_length(&mod_list));
631  ilog(L_WARN, "Module Restart: %u modules unloaded, %u modules loaded",
632       modnum, dlink_list_length(&mod_list));
633 }

Diff Legend

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