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.
Revision 1447 by michael, Mon Jun 25 20:22:29 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  
# Line 166 | Line 132 | load_a_module(const char *path, int warn
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);
135 >    ilog(LOG_TYPE_IRCD, "Module %s has no module_entry export", mod_basename);
136      lt_dlclose(tmpptr);
137      return -1;
138    }
# Line 176 | Line 142 | load_a_module(const char *path, int warn
142    if (EmptyString(modp->version))
143      modp->version = unknown_ver;
144  
179  if (core)
180    modp->flags |= MODULE_FLAG_CORE;
181
145    DupString(modp->name, mod_basename);
146 <  dlinkAdd(modp, &modp->node, &mod_list);
146 >  dlinkAdd(modp, &modp->node, &modules_list);
147  
148    if (modp->modinit)
149      modp->modinit();
# Line 190 | Line 153 | load_a_module(const char *path, int warn
153      sendto_realops_flags(UMODE_ALL, L_ALL,
154                           "Module %s [version: %s handle: %p] loaded.",
155                           modp->name, modp->version, tmpptr);
156 <    ilog(L_WARN, "Module %s [version: %s handle: %p] loaded.",
156 >    ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
157           modp->name, modp->version, tmpptr);
158    }
159  
# Line 209 | Line 172 | modules_init(void)
172   {
173    if (lt_dlinit())
174    {
175 <    ilog(L_ERROR, "Couldn't initialize the libltdl run time dynamic"
175 >    ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
176           " link library. Exiting.");
177      exit(0);
178    }
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);
179   }
180  
181   /* mod_find_path()
# Line 231 | Line 188 | static struct module_path *
188   mod_find_path(const char *path)
189   {
190    dlink_node *ptr;
234  struct module_path *mpath;
191  
192    DLINK_FOREACH(ptr, mod_paths.head)
193    {
194 <    mpath = ptr->data;
194 >    struct module_path *mpath = ptr->data;
195  
196      if (!strcmp(path, mpath->path))
197        return mpath;
# Line 290 | Line 246 | add_conf_module(const char *name)
246   void
247   mod_clear_paths(void)
248   {
293  struct module_path *pathst = NULL;
249    dlink_node *ptr = NULL, *next_ptr = NULL;
250  
251    DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
252    {
253 <    pathst = ptr->data;
254 <
300 <    dlinkDelete(&pathst->node, &mod_paths);
301 <    MyFree(pathst);
253 >    dlinkDelete(ptr, &mod_paths);
254 >    MyFree(ptr->data);
255    }
256  
257    DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
258    {
259 <    pathst = ptr->data;
260 <
308 <    dlinkDelete(&pathst->node, &conf_modules);
309 <    MyFree(pathst);
259 >    dlinkDelete(ptr, &conf_modules);
260 >    MyFree(ptr->data);
261    }
262   }
263  
# Line 321 | Line 272 | findmodule_byname(const char *name)
272   {
273    dlink_node *ptr = NULL;
274  
275 <  DLINK_FOREACH(ptr, mod_list.head)
275 >  DLINK_FOREACH(ptr, modules_list.head)
276    {
277      struct module *modp = ptr->data;
278  
# Line 345 | Line 296 | load_all_modules(int warn)
296    struct dirent *ldirent = NULL;
297    char module_fq_name[PATH_MAX + 1];
298  
348  modules_init();
349
299    if ((system_module_dir = opendir(AUTOMODPATH)) == NULL)
300    {
301 <    ilog(L_WARN, "Could not load modules from %s: %s",
301 >    ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
302           AUTOMODPATH, strerror(errno));
303      return;
304    }
# Line 360 | Line 309 | load_all_modules(int warn)
309      {
310         snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s",
311                  AUTOMODPATH, ldirent->d_name);
312 <       load_a_module(module_fq_name, warn, 0);
312 >       load_a_module(module_fq_name, warn);
313      }
314    }
315  
# Line 377 | Line 326 | void
326   load_conf_modules(void)
327   {
328    dlink_node *ptr = NULL;
380  struct module_path *mpath = NULL;
329  
330    DLINK_FOREACH(ptr, conf_modules.head)
331    {
332 <    mpath = ptr->data;
332 >    struct module_path *mpath = ptr->data;
333  
334      if (findmodule_byname(mpath->path) == NULL)
335 <      load_one_module(mpath->path, 0);
335 >      load_one_module(mpath->path);
336    }
337   }
338  
# Line 405 | Line 353 | load_core_modules(int warn)
353      snprintf(module_name, sizeof(module_name), "%s%s",
354               MODPATH, core_module_table[i]);
355  
356 <    if (load_a_module(module_name, warn, 1) == -1)
356 >    if (load_a_module(module_name, warn) == -1)
357      {
358 <      ilog(L_CRIT, "Error loading core module %s: terminating ircd",
358 >      ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
359             core_module_table[i]);
360        exit(EXIT_FAILURE);
361      }
# Line 422 | Line 370 | load_core_modules(int warn)
370   * side effects - module is loaded if found.
371   */
372   int
373 < load_one_module(const char *path, int coremodule)
373 > load_one_module(const char *path)
374   {
375    dlink_node *ptr = NULL;
376    char modpath[PATH_MAX + 1];
# Line 439 | Line 387 | load_one_module(const char *path, int co
387  
388      if (strstr(modpath, "../") == NULL &&
389          strstr(modpath, "/..") == NULL)
442    {
390        if (!stat(modpath, &statbuf))
391 <      {
392 <        if (S_ISREG(statbuf.st_mode))
446 <        {
447 <          /* Regular files only please */
448 <          return load_a_module(modpath, 1, coremodule);
449 <        }
450 <      }
451 <    }
391 >        if (S_ISREG(statbuf.st_mode))  /* Regular files only please */
392 >          return load_a_module(modpath, 1);
393    }
394  
395    sendto_realops_flags(UMODE_ALL, L_ALL,
396                         "Cannot locate module %s", path);
397 <  ilog(L_WARN, "Cannot locate module %s", path);
397 >  ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path);
398    return -1;
399   }
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)