nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /* plugins.c
2 * plugin routines
3 *
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22  
23 #include "config.h"
24  
25 #ifdef HAVE_PLUGINS
26  
27 #include <time.h>
28  
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <errno.h>
33  
34 #include <glib.h>
35 #include <gmodule.h>
36  
37 #include <wsutil/filesystem.h>
38 #include <wsutil/privileges.h>
39 #include <wsutil/file_util.h>
40 #include <wsutil/report_err.h>
41  
42 #include <wsutil/plugins.h>
43  
44 /* linked list of all plugins */
45 typedef struct _plugin {
46 GModule *handle; /* handle returned by g_module_open */
47 gchar *name; /* plugin name */
48 gchar *version; /* plugin version */
49 guint32 types; /* bitmask of plugin types this plugin supports */
50 struct _plugin *next; /* forward link */
51 } plugin;
52  
53 static plugin *plugin_list = NULL;
54  
55 /*
56 * Add a new plugin type.
57 * Takes a callback routine as an argument; it is called for each plugin
58 * we find, and handed a handle for the plugin, the name of the plugin,
59 * and the version string for the plugin. The plugin returns TRUE if
60 * it's a plugin for that type and FALSE if not.
61 */
62 typedef struct {
63 const char *type;
64 plugin_callback callback;
65 guint type_val;
66 } plugin_type;
67  
68 static GSList *plugin_types = NULL;
69  
70 void
71 add_plugin_type(const char *type, plugin_callback callback)
72 {
73 plugin_type *new_type;
74 static guint type_val;
75  
76 if (type_val >= 32) {
77 /*
78 * There's a bitmask of types that a plugin provides, and it's
79 * 32 bits, so we don't support types > 31.
80 */
81 report_failure("At most 32 plugin types can be supported, so the plugin type '%s' won't be supported.",
82 type);
83 return;
84 }
85 new_type = (plugin_type *)g_malloc(sizeof (plugin_type));
86 new_type->type = type;
87 new_type->callback = callback;
88 new_type->type_val = type_val;
89 plugin_types = g_slist_append(plugin_types, new_type);
90 type_val++;
91 }
92  
93 /*
94 * add a new plugin to the list
95 * returns :
96 * - 0 : OK
97 * - EEXIST : the same plugin (i.e. name/version) was already registered.
98 */
99 static int
100 add_plugin(plugin *new_plug)
101 {
102 plugin *pt_plug;
103  
104 pt_plug = plugin_list;
105 if (!pt_plug) /* the list is empty */
106 {
107 plugin_list = new_plug;
108 }
109 else
110 {
111 while (1)
112 {
113 /* check if the same name/version is already registered */
114 if (strcmp(pt_plug->name, new_plug->name) == 0 &&
115 strcmp(pt_plug->version, new_plug->version) == 0)
116 {
117 return EEXIST;
118 }
119  
120 /* we found the last plugin in the list */
121 if (pt_plug->next == NULL)
122 break;
123  
124 pt_plug = pt_plug->next;
125 }
126 pt_plug->next = new_plug;
127 }
128  
129 return 0;
130 }
131  
132 static void
133 call_plugin_callback(gpointer data, gpointer user_data)
134 {
135 plugin_type *type = (plugin_type *)data;
136 plugin *new_plug = (plugin *)user_data;
137  
138 if ((*type->callback)(new_plug->handle)) {
139 /* The plugin supports this type */
140 new_plug->types |= 1 << type->type_val;
141 }
142 }
143  
144 static void
145 plugins_scan_dir(const char *dirname)
146 {
147 #define FILENAME_LEN 1024
148 WS_DIR *dir; /* scanned directory */
149 WS_DIRENT *file; /* current file */
150 const char *name;
151 gchar filename[FILENAME_LEN]; /* current file name */
152 GModule *handle; /* handle returned by g_module_open */
153 gpointer gp;
154 plugin *new_plug;
155 gchar *dot;
156 int cr;
157  
158 if ((dir = ws_dir_open(dirname, 0, NULL)) != NULL)
159 {
160 while ((file = ws_dir_read_name(dir)) != NULL)
161 {
162 name = ws_dir_get_name(file);
163  
164 /*
165 * GLib 2.x defines G_MODULE_SUFFIX as the extension used on
166 * this platform for loadable modules.
167 */
168 /* skip anything but files with G_MODULE_SUFFIX */
169 dot = strrchr(name, '.');
170 if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0)
171 continue;
172  
173 g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
174 dirname, name);
175 if ((handle = g_module_open(filename, G_MODULE_BIND_LOCAL)) == NULL)
176 {
177 report_failure("Couldn't load module %s: %s", filename,
178 g_module_error());
179 continue;
180 }
181  
182 if (!g_module_symbol(handle, "version", &gp))
183 {
184 report_failure("The plugin %s has no version symbol", name);
185 g_module_close(handle);
186 continue;
187 }
188  
189 new_plug = (plugin *)g_malloc(sizeof(plugin));
190 new_plug->handle = handle;
191 new_plug->name = g_strdup(name);
192 new_plug->version = (char *)gp;
193 new_plug->types = 0;
194 new_plug->next = NULL;
195  
196 /*
197 * Hand the plugin to each of the plugin type callbacks.
198 */
199 g_slist_foreach(plugin_types, call_plugin_callback, new_plug);
200  
201 /*
202 * Does this dissector do anything useful?
203 */
204 if (new_plug->types == 0)
205 {
206 /*
207 * No.
208 */
209 report_failure("The plugin '%s' has no registration routines",
210 name);
211 g_module_close(handle);
212 g_free(new_plug->name);
213 g_free(new_plug);
214 continue;
215 }
216  
217 /*
218 * OK, attempt to add it to the list of plugins.
219 */
220 cr = add_plugin(new_plug);
221 if (cr != 0)
222 {
223 g_assert(cr == EEXIST);
224 fprintf(stderr, "The plugin '%s' version %s "
225 "was found in multiple directories.\n",
226 new_plug->name, new_plug->version);
227 g_module_close(handle);
228 g_free(new_plug->name);
229 g_free(new_plug);
230 continue;
231 }
232  
233 }
234 ws_dir_close(dir);
235 }
236 }
237  
238  
239 /*
240 * Scan for plugins.
241 */
242 void
243 scan_plugins(void)
244 {
245 const char *plugin_dir;
246 const char *name;
247 char *plugin_dir_path;
248 char *plugins_pers_dir;
249 WS_DIR *dir; /* scanned directory */
250 WS_DIRENT *file; /* current file */
251  
252 if (plugin_list == NULL) /* ensure scan_plugins is only run once */
253 {
254 /*
255 * Scan the global plugin directory.
256 * If we're running from a build directory, scan the "plugins"
257 * subdirectory, as that's where plugins are located in an
258 * out-of-tree build. If we find subdirectories scan those since
259 * they will contain plugins in the case of an in-tree build.
260 */
261 plugin_dir = get_plugin_dir();
262 if (running_in_build_directory())
263 {
264 if ((dir = ws_dir_open(plugin_dir, 0, NULL)) != NULL)
265 {
266 plugins_scan_dir(plugin_dir);
267 while ((file = ws_dir_read_name(dir)) != NULL)
268 {
269 name = ws_dir_get_name(file);
270 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
271 continue; /* skip "." and ".." */
272 /*
273 * Get the full path of a ".libs" subdirectory of that
274 * directory.
275 */
276 plugin_dir_path = g_strdup_printf(
277 "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S ".libs",
278 plugin_dir, name);
279 if (test_for_directory(plugin_dir_path) != EISDIR) {
280 /*
281 * Either it doesn't refer to a directory or it
282 * refers to something that doesn't exist.
283 *
284 * Assume that means that the plugins are in
285 * the subdirectory of the plugin directory, not
286 * a ".libs" subdirectory of that subdirectory.
287 */
288 g_free(plugin_dir_path);
289 plugin_dir_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
290 plugin_dir, name);
291 }
292 plugins_scan_dir(plugin_dir_path);
293 g_free(plugin_dir_path);
294 }
295 ws_dir_close(dir);
296 }
297 }
298 else
299 plugins_scan_dir(plugin_dir);
300  
301 /*
302 * If the program wasn't started with special privileges,
303 * scan the users plugin directory. (Even if we relinquish
304 * them, plugins aren't safe unless we've *permanently*
305 * relinquished them, and we can't do that in Wireshark as,
306 * if we need privileges to start capturing, we'd need to
307 * reclaim them before each time we start capturing.)
308 */
309 if (!started_with_special_privs())
310 {
311 plugins_pers_dir = get_plugins_pers_dir();
312 plugins_scan_dir(plugins_pers_dir);
313 g_free(plugins_pers_dir);
314 }
315 }
316 }
317  
318 /*
319 * Iterate over all plugins, calling a callback with information about
320 * the plugin.
321 */
322 typedef struct {
323 plugin *pt_plug;
324 GString *types;
325 const char *sep;
326 } type_callback_info;
327  
328 static void
329 add_plugin_type_description(gpointer data, gpointer user_data)
330 {
331 plugin_type *type = (plugin_type *)data;
332 type_callback_info *info = (type_callback_info *)user_data;
333  
334 /*
335 * If the plugin handles this type, add the type to the list of types.
336 */
337 if (info->pt_plug->types & (1 << type->type_val)) {
338 g_string_append_printf(info->types, "%s%s", info->sep, type->type);
339 info->sep = ", ";
340 }
341 }
342  
343 WS_DLL_PUBLIC void
344 plugins_get_descriptions(plugin_description_callback callback, void *user_data)
345 {
346 type_callback_info info;
347  
348 info.types = NULL; /* Certain compiler suites need a init state for this variable */
349 for (info.pt_plug = plugin_list; info.pt_plug != NULL;
350 info.pt_plug = info.pt_plug->next)
351 {
352 info.sep = "";
353 info.types = g_string_new("");
354  
355 /*
356 * Build a list of all the plugin types.
357 */
358 g_slist_foreach(plugin_types, add_plugin_type_description, &info);
359  
360 /*
361 * And hand the information to the callback.
362 */
363 callback(info.pt_plug->name, info.pt_plug->version, info.types->str,
364 g_module_name(info.pt_plug->handle), user_data);
365  
366 g_string_free(info.types, TRUE);
367 }
368 }
369  
370 static void
371 print_plugin_description(const char *name, const char *version,
372 const char *description, const char *filename,
373 void *user_data _U_)
374 {
375 printf("%s\t%s\t%s\t%s\n", name, version, description, filename);
376 }
377  
378 void
379 plugins_dump_all(void)
380 {
381 plugins_get_descriptions(print_plugin_description, NULL);
382 }
383  
384 #endif /* HAVE_PLUGINS */
385  
386 /*
387 * Editor modelines
388 *
389 * Local Variables:
390 * c-basic-offset: 4
391 * tab-width: 8
392 * indent-tabs-mode: nil
393 * End:
394 *
395 * ex: set shiftwidth=4 tabstop=8 expandtab:
396 * :indentSize=4:tabSize=8:noTabs=true:
397 */