/[svn]/ircd-hybrid-7.2/src/dynlink.c
ViewVC logotype

Contents of /ircd-hybrid-7.2/src/dynlink.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1011 - (show annotations)
Fri Sep 18 10:14:09 2009 UTC (11 years, 5 months ago) by michael
File MIME type: text/x-chdr
File size: 4362 byte(s)
- move list manipulation routines from tools.c to list.c
- mem_frob() goes to memory.c
- sort out redundant/unneeded header includes

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 extern dlink_list mod_list;
37
38 static char unknown_ver[] = "<unknown>";
39
40 /* This file contains the core functions to use dynamic libraries.
41 * -TimeMr14C
42 */
43
44 void
45 dynlink_init(void)
46 {
47 if (lt_dlinit())
48 {
49 ilog(L_ERROR, "Couldn't initialize the libltdl run time dynamic"
50 " link library. Exiting.");
51 exit(0);
52 }
53 }
54
55 int
56 modules_valid_suffix(const char *name)
57 {
58 return ((name = strrchr(name, '.'))) && !strcmp(name, ".la");
59 }
60
61 /* unload_one_module()
62 *
63 * inputs - name of module to unload
64 * - 1 to say modules unloaded, 0 to not
65 * output - 0 if successful, -1 if error
66 * side effects - module is unloaded
67 */
68 int
69 unload_one_module(const char *name, int warn)
70 {
71 dlink_node *ptr = NULL;
72 struct module *modp = NULL;
73
74 if ((ptr = findmodule_byname(name)) == NULL)
75 return -1;
76
77 modp = ptr->data;
78
79 if (modp->modremove)
80 modp->modremove();
81
82 lt_dlclose(modp->handle);
83
84 assert(dlink_list_length(&mod_list) > 0);
85 dlinkDelete(ptr, &mod_list);
86 MyFree(modp->name);
87 MyFree(modp);
88
89 if (warn == 1)
90 {
91 ilog(L_INFO, "Module %s unloaded", name);
92 sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name);
93 }
94
95 return 0;
96 }
97
98 /* load_a_module()
99 *
100 * inputs - path name of module, int to notice, int of core
101 * output - -1 if error 0 if success
102 * side effects - loads a module if successful
103 */
104 int
105 load_a_module(const char *path, int warn, int core)
106 {
107 lt_dlhandle tmpptr = NULL;
108 const char *mod_basename = NULL;
109 char **verp;
110 char *ver;
111 struct module *modp;
112 void (*mod_init)(void) = NULL;
113 void (*mod_deinit)(void) = NULL;
114
115 mod_basename = libio_basename(path);
116
117 if (findmodule_byname(mod_basename) != NULL)
118 return 1;
119
120 if (!(tmpptr = lt_dlopen(path))) {
121 const char *err = ((err = lt_dlerror())) ? err : "<unknown>";
122
123 sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s",
124 mod_basename, err);
125 ilog(L_WARN, "Error loading module %s: %s", mod_basename, err);
126 return -1;
127 }
128
129 if ((mod_init = lt_dlsym(tmpptr, "_modinit")) == NULL)
130 {
131 sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no _modinit() function",
132 mod_basename);
133 ilog(L_WARN, "Module %s has no _modinit() function", mod_basename);
134 lt_dlclose(tmpptr);
135 return -1;
136 }
137
138 if ((mod_deinit = lt_dlsym(tmpptr, "_moddeinit")) == NULL)
139 {
140 sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no _moddeinit() function",
141 mod_basename);
142 ilog(L_WARN, "Module %s has no _moddeinit() function", mod_basename);
143 /* blah blah soft error, see above. */
144 }
145
146 if ((verp = lt_dlsym(tmpptr, "_version")) == NULL)
147 ver = unknown_ver;
148 else
149 ver = *verp;
150
151 modp = MyMalloc(sizeof(struct module));
152 modp->handle = tmpptr;
153 modp->version = ver;
154 modp->core = core;
155 modp->modremove = mod_deinit;
156
157 DupString(modp->name, mod_basename);
158 dlinkAdd(modp, &modp->node, &mod_list);
159
160 mod_init();
161
162 if (warn == 1)
163 {
164 sendto_realops_flags(UMODE_ALL, L_ALL,
165 "Module %s [version: %s handle: %p] loaded.",
166 mod_basename, ver, tmpptr);
167 ilog(L_WARN, "Module %s [version: %s handle: %p] loaded.",
168 mod_basename, ver, tmpptr);
169 }
170
171 return 0;
172 }

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28