1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* dynlink.c: A module loader. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
* |
24 |
*/ |
25 |
|
26 |
#include "ltdl.h" |
27 |
|
28 |
#include "stdinc.h" |
29 |
#include "list.h" |
30 |
#include "irc_string.h" |
31 |
#include "modules.h" |
32 |
#include "s_log.h" |
33 |
#include "client.h" |
34 |
#include "send.h" |
35 |
|
36 |
|
37 |
static const char *unknown_ver = "<unknown>"; |
38 |
|
39 |
/* This file contains the core functions to use dynamic libraries. |
40 |
* -TimeMr14C |
41 |
*/ |
42 |
|
43 |
void |
44 |
dynlink_init(void) |
45 |
{ |
46 |
if (lt_dlinit()) |
47 |
{ |
48 |
ilog(L_ERROR, "Couldn't initialize the libltdl run time dynamic" |
49 |
" link library. Exiting."); |
50 |
exit(0); |
51 |
} |
52 |
} |
53 |
|
54 |
int |
55 |
modules_valid_suffix(const char *name) |
56 |
{ |
57 |
return ((name = strrchr(name, '.'))) && !strcmp(name, ".la"); |
58 |
} |
59 |
|
60 |
/* unload_one_module() |
61 |
* |
62 |
* inputs - name of module to unload |
63 |
* - 1 to say modules unloaded, 0 to not |
64 |
* output - 0 if successful, -1 if error |
65 |
* side effects - module is unloaded |
66 |
*/ |
67 |
int |
68 |
unload_one_module(const char *name, int warn) |
69 |
{ |
70 |
struct module *modp = NULL; |
71 |
|
72 |
if ((modp = findmodule_byname(name)) == NULL) |
73 |
return -1; |
74 |
|
75 |
if (modp->modexit) |
76 |
modp->modexit(); |
77 |
|
78 |
assert(dlink_list_length(&mod_list) > 0); |
79 |
dlinkDelete(&modp->node, &mod_list); |
80 |
MyFree(modp->name); |
81 |
|
82 |
lt_dlclose(modp->handle); |
83 |
|
84 |
if (warn == 1) |
85 |
{ |
86 |
ilog(L_INFO, "Module %s unloaded", name); |
87 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name); |
88 |
} |
89 |
|
90 |
return 0; |
91 |
} |
92 |
|
93 |
/* load_a_module() |
94 |
* |
95 |
* inputs - path name of module, int to notice, int of core |
96 |
* output - -1 if error 0 if success |
97 |
* side effects - loads a module if successful |
98 |
*/ |
99 |
int |
100 |
load_a_module(const char *path, int warn, int core) |
101 |
{ |
102 |
lt_dlhandle tmpptr = NULL; |
103 |
const char *mod_basename = NULL; |
104 |
struct module *modp = NULL; |
105 |
|
106 |
if (findmodule_byname((mod_basename = libio_basename(path)))) |
107 |
return 1; |
108 |
|
109 |
if (!(tmpptr = lt_dlopen(path))) { |
110 |
const char *err = ((err = lt_dlerror())) ? err : "<unknown>"; |
111 |
|
112 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s", |
113 |
mod_basename, err); |
114 |
ilog(L_WARN, "Error loading module %s: %s", mod_basename, err); |
115 |
return -1; |
116 |
} |
117 |
|
118 |
if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL) |
119 |
{ |
120 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no module_entry export", |
121 |
mod_basename); |
122 |
ilog(L_WARN, "Module %s has no module_entry export", mod_basename); |
123 |
lt_dlclose(tmpptr); |
124 |
return -1; |
125 |
} |
126 |
|
127 |
modp->handle = tmpptr; |
128 |
|
129 |
if (EmptyString(modp->version)) |
130 |
modp->version = unknown_ver; |
131 |
|
132 |
if (core) |
133 |
modp->flags |= MODULE_FLAG_CORE; |
134 |
|
135 |
DupString(modp->name, mod_basename); |
136 |
dlinkAdd(modp, &modp->node, &mod_list); |
137 |
|
138 |
if (modp->modinit) |
139 |
modp->modinit(); |
140 |
|
141 |
if (warn == 1) |
142 |
{ |
143 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
144 |
"Module %s [version: %s handle: %p] loaded.", |
145 |
modp->name, modp->version, tmpptr); |
146 |
ilog(L_WARN, "Module %s [version: %s handle: %p] loaded.", |
147 |
modp->name, modp->version, tmpptr); |
148 |
} |
149 |
|
150 |
return 0; |
151 |
} |