ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/modules.c
Revision: 1592
Committed: Sat Oct 27 21:02:32 2012 UTC (12 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 8928 byte(s)
Log Message:
- Second time's the charm? Moving svnroot/ircd-hybrid-8 to
  svnroot/ircd-hybrid/trunk

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * modules.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 #include "ltdl.h"
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "modules.h"
30 #include "log.h"
31 #include "ircd.h"
32 #include "client.h"
33 #include "send.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"
40
41
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 {
48 "m_die.la",
49 "m_error.la",
50 "m_join.la",
51 "m_kick.la",
52 "m_kill.la",
53 "m_message.la",
54 "m_mode.la",
55 "m_nick.la",
56 "m_part.la",
57 "m_quit.la",
58 "m_server.la",
59 "m_sjoin.la",
60 "m_squit.la",
61 NULL
62 };
63
64 static dlink_list mod_paths = { NULL, NULL, 0 };
65 static dlink_list conf_modules = { NULL, NULL, 0 };
66
67 int
68 modules_valid_suffix(const char *name)
69 {
70 return ((name = strrchr(name, '.'))) && !strcmp(name, ".la");
71 }
72
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 if ((modp = findmodule_byname(name)) == NULL)
86 return -1;
87
88 if (modp->modexit)
89 modp->modexit();
90
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 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
167 *
168 * input - NONE
169 * output - NONE
170 * side effects - The basic module manipulation modules are loaded
171 */
172 void
173 modules_init(void)
174 {
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()
184 *
185 * input - path
186 * output - none
187 * side effects - returns a module path from path
188 */
189 static struct module_path *
190 mod_find_path(const char *path)
191 {
192 dlink_node *ptr;
193
194 DLINK_FOREACH(ptr, mod_paths.head)
195 {
196 struct module_path *mpath = ptr->data;
197
198 if (!strcmp(path, mpath->path))
199 return mpath;
200 }
201
202 return NULL;
203 }
204
205 /* mod_add_path()
206 *
207 * input - path
208 * output - NONE
209 * side effects - adds path to list
210 */
211 void
212 mod_add_path(const char *path)
213 {
214 struct module_path *pathst;
215
216 if (mod_find_path(path))
217 return;
218
219 pathst = MyMalloc(sizeof(struct module_path));
220
221 strlcpy(pathst->path, path, sizeof(pathst->path));
222 dlinkAdd(pathst, &pathst->node, &mod_paths);
223 }
224
225 /* add_conf_module
226 *
227 * input - module name
228 * output - NONE
229 * side effects - adds module to conf_mod
230 */
231 void
232 add_conf_module(const char *name)
233 {
234 struct module_path *pathst;
235
236 pathst = MyMalloc(sizeof(struct module_path));
237
238 strlcpy(pathst->path, name, sizeof(pathst->path));
239 dlinkAdd(pathst, &pathst->node, &conf_modules);
240 }
241
242 /* mod_clear_paths()
243 *
244 * input - NONE
245 * output - NONE
246 * side effects - clear the lists of paths and conf modules
247 */
248 void
249 mod_clear_paths(void)
250 {
251 dlink_node *ptr = NULL, *next_ptr = NULL;
252
253 DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
254 {
255 dlinkDelete(ptr, &mod_paths);
256 MyFree(ptr->data);
257 }
258
259 DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
260 {
261 dlinkDelete(ptr, &conf_modules);
262 MyFree(ptr->data);
263 }
264 }
265
266 /* findmodule_byname
267 *
268 * input - name of module
269 * output - NULL if not found or pointer to module
270 * side effects - NONE
271 */
272 struct module *
273 findmodule_byname(const char *name)
274 {
275 dlink_node *ptr = NULL;
276
277 DLINK_FOREACH(ptr, modules_list.head)
278 {
279 struct module *modp = ptr->data;
280
281 if (strcmp(modp->name, name) == 0)
282 return modp;
283 }
284
285 return NULL;
286 }
287
288 /* load_all_modules()
289 *
290 * input - int flag warn
291 * output - NONE
292 * side effects - load all modules found in autoload directory
293 */
294 void
295 load_all_modules(int warn)
296 {
297 DIR *system_module_dir = NULL;
298 struct dirent *ldirent = NULL;
299 char module_fq_name[PATH_MAX + 1];
300
301 if ((system_module_dir = opendir(AUTOMODPATH)) == NULL)
302 {
303 ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
304 AUTOMODPATH, strerror(errno));
305 return;
306 }
307
308 while ((ldirent = readdir(system_module_dir)) != NULL)
309 {
310 if (modules_valid_suffix(ldirent->d_name))
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);
315 }
316 }
317
318 closedir(system_module_dir);
319 }
320
321 /* load_conf_modules()
322 *
323 * input - NONE
324 * output - NONE
325 * side effects - load modules given in ircd.conf
326 */
327 void
328 load_conf_modules(void)
329 {
330 dlink_node *ptr = NULL;
331
332 DLINK_FOREACH(ptr, conf_modules.head)
333 {
334 struct module_path *mpath = ptr->data;
335
336 if (findmodule_byname(mpath->path) == NULL)
337 load_one_module(mpath->path);
338 }
339 }
340
341 /* load_core_modules()
342 *
343 * input - int flag warn
344 * output - NONE
345 * side effects - core modules are loaded, if any fail, kill ircd
346 */
347 void
348 load_core_modules(int warn)
349 {
350 char module_name[PATH_MAX + 1];
351 int i = 0;
352
353 for (; core_module_table[i]; ++i)
354 {
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)
359 {
360 ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
361 core_module_table[i]);
362 exit(EXIT_FAILURE);
363 }
364 }
365 }
366
367 /* load_one_module()
368 *
369 * input - pointer to path
370 * - flagged as core module or not
371 * output - -1 if error
372 * side effects - module is loaded if found.
373 */
374 int
375 load_one_module(const char *path)
376 {
377 dlink_node *ptr = NULL;
378 char modpath[PATH_MAX + 1];
379 struct stat statbuf;
380
381 DLINK_FOREACH(ptr, mod_paths.head)
382 {
383 const struct module_path *mpath = ptr->data;
384
385 snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path);
386
387 if (!modules_valid_suffix(path))
388 continue;
389
390 if (strstr(modpath, "../") == NULL &&
391 strstr(modpath, "/..") == NULL)
392 if (!stat(modpath, &statbuf))
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(LOG_TYPE_IRCD, "Cannot locate module %s", path);
400 return -1;
401 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision