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 1013 by michael, Sun Oct 18 14:26:49 2009 UTC vs.
ircd-hybrid/trunk/src/modules.c (file contents), Revision 1997 by michael, Sat May 11 17:35:07 2013 UTC

# Line 22 | Line 22
22   *  $Id$
23   */
24  
25 + #include "ltdl.h"
26 +
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"
33 < #include "handlers.h"
34 > #include "conf.h"
35   #include "numeric.h"
36   #include "parse.h"
37   #include "ircd_defs.h"
# Line 38 | Line 39
39   #include "memory.h"
40  
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",
# Line 58 | 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,
77 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modunload, m_ignore}
78 < };
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,
82 <  {m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modreload, m_ignore}
83 < };
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}
88 < };
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 >  if (!(tmpptr = lt_dlopen(path))) {
123 >    const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
124 >
125 >    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
126 >                         "Error loading module %s: %s",
127 >                         mod_basename, err);
128 >    ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
129 >    return -1;
130 >  }
131 >
132 >  if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL)
133 >  {
134 >    const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
135 >
136 >    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
137 >                         "Error loading module %s: %s",
138 >                         mod_basename, err);
139 >    ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
140 >    lt_dlclose(tmpptr);
141 >    return -1;
142 >  }
143  
144 +  modp->handle = tmpptr;
145 +
146 +  if (EmptyString(modp->version))
147 +    modp->version = unknown_ver;
148 +
149 +  modp->name = xstrdup(mod_basename);
150 +  dlinkAdd(modp, &modp->node, &modules_list);
151 +
152 +  if (modp->modinit)
153 +    modp->modinit();
154 +
155 +  if (warn == 1)
156 +  {
157 +    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
158 +                         "Module %s [version: %s handle: %p] loaded.",
159 +                         modp->name, modp->version, tmpptr);
160 +    ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
161 +         modp->name, modp->version, tmpptr);
162 +  }
163 +
164 +  return 0;
165 + }
166  
167   /*
168   * modules_init
# Line 103 | Line 174 | struct Message modrestart_msgtab = {
174   void
175   modules_init(void)
176   {
177 <  dynlink_init();
178 <
179 <  mod_add_cmd(&modload_msgtab);
180 <  mod_add_cmd(&modunload_msgtab);
181 <  mod_add_cmd(&modreload_msgtab);
182 <  mod_add_cmd(&modlist_msgtab);
112 <  mod_add_cmd(&modrestart_msgtab);
177 >  if (lt_dlinit())
178 >  {
179 >    ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
180 >         " link library. Exiting.");
181 >    exit(0);
182 >  }
183   }
184  
185   /* mod_find_path()
# Line 122 | Line 192 | static struct module_path *
192   mod_find_path(const char *path)
193   {
194    dlink_node *ptr;
125  struct module_path *mpath;
195  
196    DLINK_FOREACH(ptr, mod_paths.head)
197    {
198 <    mpath = ptr->data;
198 >    struct module_path *mpath = ptr->data;
199  
200      if (!strcmp(path, mpath->path))
201        return mpath;
# Line 181 | Line 250 | add_conf_module(const char *name)
250   void
251   mod_clear_paths(void)
252   {
253 <  struct module_path *pathst;
185 <  dlink_node *ptr;
186 <  dlink_node *next_ptr;
253 >  dlink_node *ptr = NULL, *next_ptr = NULL;
254  
255    DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
256    {
257 <    pathst = ptr->data;
258 <
192 <    dlinkDelete(&pathst->node, &mod_paths);
193 <    MyFree(pathst);
257 >    dlinkDelete(ptr, &mod_paths);
258 >    MyFree(ptr->data);
259    }
260  
261    DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
262    {
263 <    pathst = ptr->data;
264 <
200 <    dlinkDelete(&pathst->node, &conf_modules);
201 <    MyFree(pathst);
263 >    dlinkDelete(ptr, &conf_modules);
264 >    MyFree(ptr->data);
265    }
266   }
267  
# Line 208 | Line 271 | mod_clear_paths(void)
271   * output       - NULL if not found or pointer to module
272   * side effects - NONE
273   */
274 < dlink_node *
274 > struct module *
275   findmodule_byname(const char *name)
276   {
277 <  dlink_node *ptr;
215 <  struct module *modp;
277 >  dlink_node *ptr = NULL;
278  
279 <  DLINK_FOREACH(ptr, mod_list.head)
279 >  DLINK_FOREACH(ptr, modules_list.head)
280    {
281 <    modp = ptr->data;
281 >    struct module *modp = ptr->data;
282  
283      if (strcmp(modp->name, name) == 0)
284 <      return ptr;
284 >      return modp;
285    }
286  
287    return NULL;
# Line 236 | Line 298 | load_all_modules(int warn)
298   {
299    DIR *system_module_dir = NULL;
300    struct dirent *ldirent = NULL;
301 <  char module_fq_name[PATH_MAX + 1];
240 <
241 <  modules_init();
301 >  char module_fq_name[HYB_PATH_MAX + 1];
302  
303    if ((system_module_dir = opendir(AUTOMODPATH)) == NULL)
304    {
305 <    ilog(L_WARN, "Could not load modules from %s: %s",
305 >    ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
306           AUTOMODPATH, strerror(errno));
307      return;
308    }
# Line 253 | Line 313 | load_all_modules(int warn)
313      {
314         snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s",
315                  AUTOMODPATH, ldirent->d_name);
316 <       load_a_module(module_fq_name, warn, 0);
316 >       load_a_module(module_fq_name, warn);
317      }
318    }
319  
# Line 270 | Line 330 | void
330   load_conf_modules(void)
331   {
332    dlink_node *ptr = NULL;
273  struct module_path *mpath = NULL;
333  
334    DLINK_FOREACH(ptr, conf_modules.head)
335    {
336 <    mpath = ptr->data;
336 >    struct module_path *mpath = ptr->data;
337  
338      if (findmodule_byname(mpath->path) == NULL)
339 <      load_one_module(mpath->path, 0);
339 >      load_one_module(mpath->path);
340    }
341   }
342  
# Line 290 | Line 349 | load_conf_modules(void)
349   void
350   load_core_modules(int warn)
351   {
352 <  char module_name[PATH_MAX + 1];
352 >  char module_name[HYB_PATH_MAX + 1];
353    int i = 0;
354  
355    for (; core_module_table[i]; ++i)
# Line 298 | Line 357 | load_core_modules(int warn)
357      snprintf(module_name, sizeof(module_name), "%s%s",
358               MODPATH, core_module_table[i]);
359  
360 <    if (load_a_module(module_name, warn, 1) == -1)
360 >    if (load_a_module(module_name, warn) == -1)
361      {
362 <      ilog(L_CRIT, "Error loading core module %s: terminating ircd",
362 >      ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
363             core_module_table[i]);
364        exit(EXIT_FAILURE);
365      }
# Line 315 | Line 374 | load_core_modules(int warn)
374   * side effects - module is loaded if found.
375   */
376   int
377 < load_one_module(const char *path, int coremodule)
377 > load_one_module(const char *path)
378   {
379    dlink_node *ptr = NULL;
380 <  char modpath[PATH_MAX + 1];
380 >  char modpath[HYB_PATH_MAX + 1];
381    struct stat statbuf;
382  
383    DLINK_FOREACH(ptr, mod_paths.head)
# Line 332 | Line 391 | load_one_module(const char *path, int co
391  
392      if (strstr(modpath, "../") == NULL &&
393          strstr(modpath, "/..") == NULL)
335    {
394        if (!stat(modpath, &statbuf))
395 <      {
396 <        if (S_ISREG(statbuf.st_mode))
339 <        {
340 <          /* Regular files only please */
341 <          return load_a_module(modpath, 1, coremodule);
342 <        }
343 <      }
344 <    }
395 >        if (S_ISREG(statbuf.st_mode))  /* Regular files only please */
396 >          return load_a_module(modpath, 1);
397    }
398  
399 <  sendto_realops_flags(UMODE_ALL, L_ALL,
399 >  sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
400                         "Cannot locate module %s", path);
401 <  ilog(L_WARN, "Cannot locate module %s", path);
401 >  ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path);
402    return -1;
403   }
352
353 /* load a module .. */
354 static void
355 mo_modload(struct Client *client_p, struct Client *source_p,
356           int parc, char *parv[])
357 {
358  const char *m_bn = NULL;
359
360  if (!IsAdmin(source_p))
361  {
362    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
363               me.name, source_p->name);
364    return;
365  }
366
367  m_bn = libio_basename(parv[1]);
368
369  if (findmodule_byname(m_bn) != NULL)
370  {
371    sendto_one(source_p, ":%s NOTICE %s :Module %s is already loaded",
372               me.name, source_p->name, m_bn);
373    return;
374  }
375
376  load_one_module(parv[1], 0);
377 }
378
379 /* unload a module .. */
380 static void
381 mo_modunload(struct Client *client_p, struct Client *source_p,
382             int parc, char *parv[])
383 {
384  const char *m_bn = NULL;
385  dlink_node *ptr;
386  struct module *modp;
387
388  if (!IsAdmin(source_p))
389  {
390    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
391               me.name, source_p->name);
392    return;
393  }
394
395  m_bn = libio_basename(parv[1]);
396
397  if ((ptr = findmodule_byname(m_bn)) == NULL)
398  {
399    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
400               me.name, source_p->name, m_bn);
401    return;
402  }
403
404  modp = ptr->data;
405
406  if (modp->core == 1)
407  {
408    sendto_one(source_p,
409               ":%s NOTICE %s :Module %s is a core module and may not be unloaded",
410               me.name, source_p->name, m_bn);
411    return;
412  }
413
414  /* XXX might want to simply un dlink it here */
415
416  if (unload_one_module(m_bn, 1) == -1)
417  {
418    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
419               me.name, source_p->name, m_bn);
420  }
421 }
422
423 /* unload and load in one! */
424 static void
425 mo_modreload(struct Client *client_p, struct Client *source_p,
426             int parc, char *parv[])
427 {
428  const char *m_bn = NULL;
429  dlink_node *ptr;
430  struct module *modp;
431  int check_core;
432
433  if (!IsAdmin(source_p))
434  {
435    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
436               me.name, source_p->name);
437    return;
438  }
439
440  m_bn = libio_basename(parv[1]);
441
442  if ((ptr = findmodule_byname(m_bn)) == NULL)
443  {
444    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
445               me.name, source_p->name, m_bn);
446    return;
447  }
448
449  modp = ptr->data;
450  check_core = modp->core;
451
452  if (unload_one_module(m_bn, 1) == -1)
453  {
454    sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
455               me.name, source_p->name, m_bn);
456    return;
457  }
458
459  if ((load_one_module(parv[1], check_core) == -1) && check_core)
460  {
461    sendto_realops_flags(UMODE_ALL, L_ALL, "Error reloading core "
462                         "module: %s: terminating ircd", parv[1]);
463    ilog(L_CRIT, "Error loading core module %s: terminating ircd", parv[1]);
464    exit(0);
465  }
466 }
467
468 /* list modules .. */
469 static void
470 mo_modlist(struct Client *client_p, struct Client *source_p,
471           int parc, char *parv[])
472 {
473  const dlink_node *ptr = NULL;
474
475  if (!IsAdmin(source_p))
476  {
477    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
478               me.name, source_p->name);
479    return;
480  }
481
482  DLINK_FOREACH(ptr, mod_list.head)
483  {
484    const struct module *modp = ptr->data;
485
486    if (parc > 1 && !match(parv[1], modp->name))
487      continue;
488
489    sendto_one(source_p, form_str(RPL_MODLIST), me.name, parv[0],
490               modp->name, modp->handle,
491               modp->version, modp->core?"(core)":"");
492  }
493
494  sendto_one(source_p, form_str(RPL_ENDOFMODLIST),
495             me.name, source_p->name);
496 }
497
498 /* unload and reload all modules */
499 static void
500 mo_modrestart(struct Client *client_p, struct Client *source_p,
501              int parc, char *parv[])
502 {
503  unsigned int modnum = 0;
504  dlink_node *ptr = NULL, *ptr_next = NULL;
505
506  if (!IsAdmin(source_p))
507  {
508    sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
509               me.name, source_p->name);
510    return;
511  }
512
513  sendto_one(source_p, ":%s NOTICE %s :Reloading all modules",
514             me.name, source_p->name);
515
516  modnum = dlink_list_length(&mod_list);
517
518  DLINK_FOREACH_SAFE(ptr, ptr_next, mod_list.head)
519  {
520    struct module *modp = ptr->data;
521    unload_one_module(modp->name, 0);
522  }
523
524  load_all_modules(0);
525  load_conf_modules();
526  load_core_modules(0);
527
528  sendto_realops_flags(UMODE_ALL, L_ALL,
529              "Module Restart: %u modules unloaded, %u modules loaded",
530                        modnum, dlink_list_length(&mod_list));
531  ilog(L_WARN, "Module Restart: %u modules unloaded, %u modules loaded",
532       modnum, dlink_list_length(&mod_list));
533 }

Diff Legend

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