fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / lib / traceevent / event-plugin.c
blob0c8bf6780e4d1de74ef955dd893a037e510400af
1 /*
2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <dirent.h>
28 #include "event-parse.h"
29 #include "event-utils.h"
31 #define LOCAL_PLUGIN_DIR ".traceevent/plugins"
33 struct plugin_list {
34 struct plugin_list *next;
35 char *name;
36 void *handle;
39 static void
40 load_plugin(struct pevent *pevent, const char *path,
41 const char *file, void *data)
43 struct plugin_list **plugin_list = data;
44 pevent_plugin_load_func func;
45 struct plugin_list *list;
46 const char *alias;
47 char *plugin;
48 void *handle;
50 plugin = malloc(strlen(path) + strlen(file) + 2);
51 if (!plugin) {
52 warning("could not allocate plugin memory\n");
53 return;
56 strcpy(plugin, path);
57 strcat(plugin, "/");
58 strcat(plugin, file);
60 handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
61 if (!handle) {
62 warning("could not load plugin '%s'\n%s\n",
63 plugin, dlerror());
64 goto out_free;
67 alias = dlsym(handle, PEVENT_PLUGIN_ALIAS_NAME);
68 if (!alias)
69 alias = file;
71 func = dlsym(handle, PEVENT_PLUGIN_LOADER_NAME);
72 if (!func) {
73 warning("could not find func '%s' in plugin '%s'\n%s\n",
74 PEVENT_PLUGIN_LOADER_NAME, plugin, dlerror());
75 goto out_free;
78 list = malloc(sizeof(*list));
79 if (!list) {
80 warning("could not allocate plugin memory\n");
81 goto out_free;
84 list->next = *plugin_list;
85 list->handle = handle;
86 list->name = plugin;
87 *plugin_list = list;
89 pr_stat("registering plugin: %s", plugin);
90 func(pevent);
91 return;
93 out_free:
94 free(plugin);
97 static void
98 load_plugins_dir(struct pevent *pevent, const char *suffix,
99 const char *path,
100 void (*load_plugin)(struct pevent *pevent,
101 const char *path,
102 const char *name,
103 void *data),
104 void *data)
106 struct dirent *dent;
107 struct stat st;
108 DIR *dir;
109 int ret;
111 ret = stat(path, &st);
112 if (ret < 0)
113 return;
115 if (!S_ISDIR(st.st_mode))
116 return;
118 dir = opendir(path);
119 if (!dir)
120 return;
122 while ((dent = readdir(dir))) {
123 const char *name = dent->d_name;
125 if (strcmp(name, ".") == 0 ||
126 strcmp(name, "..") == 0)
127 continue;
129 /* Only load plugins that end in suffix */
130 if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0)
131 continue;
133 load_plugin(pevent, path, name, data);
136 closedir(dir);
139 static void
140 load_plugins(struct pevent *pevent, const char *suffix,
141 void (*load_plugin)(struct pevent *pevent,
142 const char *path,
143 const char *name,
144 void *data),
145 void *data)
147 char *home;
148 char *path;
149 char *envdir;
152 * If a system plugin directory was defined,
153 * check that first.
155 #ifdef PLUGIN_DIR
156 load_plugins_dir(pevent, suffix, PLUGIN_DIR, load_plugin, data);
157 #endif
160 * Next let the environment-set plugin directory
161 * override the system defaults.
163 envdir = getenv("TRACEEVENT_PLUGIN_DIR");
164 if (envdir)
165 load_plugins_dir(pevent, suffix, envdir, load_plugin, data);
168 * Now let the home directory override the environment
169 * or system defaults.
171 home = getenv("HOME");
172 if (!home)
173 return;
175 path = malloc(strlen(home) + strlen(LOCAL_PLUGIN_DIR) + 2);
176 if (!path) {
177 warning("could not allocate plugin memory\n");
178 return;
181 strcpy(path, home);
182 strcat(path, "/");
183 strcat(path, LOCAL_PLUGIN_DIR);
185 load_plugins_dir(pevent, suffix, path, load_plugin, data);
187 free(path);
190 struct plugin_list*
191 traceevent_load_plugins(struct pevent *pevent)
193 struct plugin_list *list = NULL;
195 load_plugins(pevent, ".so", load_plugin, &list);
196 return list;
199 void
200 traceevent_unload_plugins(struct plugin_list *plugin_list, struct pevent *pevent)
202 pevent_plugin_unload_func func;
203 struct plugin_list *list;
205 while (plugin_list) {
206 list = plugin_list;
207 plugin_list = list->next;
208 func = dlsym(list->handle, PEVENT_PLUGIN_UNLOADER_NAME);
209 if (func)
210 func(pevent);
211 dlclose(list->handle);
212 free(list->name);
213 free(list);