Load plugins and components
[babeltrace.git] / plugins / component-factory.c
1 /*
2 * component-factory.c
3 *
4 * Babeltrace Plugin Component Factory
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/plugin/component-factory.h>
30 #include <babeltrace/plugin/component-factory-internal.h>
31 #include <babeltrace/babeltrace-internal.h>
32 #include <babeltrace/compiler.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <sys/stat.h>
36
37 #define NATIVE_PLUGIN_SUFFIX ".so"
38 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
39 #define LIBTOOL_PLUGIN_SUFFIX ".la"
40 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
41 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
42 sizeof(LIBTOOL_PLUGIN_SUFFIX))
43
44 static
45 void component_destroy(gpointer data)
46 {
47 g_free(data);
48 }
49
50 static
51 void plugin_destroy(gpointer data)
52 {
53 struct plugin *plugin = data;
54
55 if (plugin->module && g_module_close(plugin->module)) {
56 printf_error("Failed to close plugin");
57 }
58
59 if (plugin->components) {
60 g_ptr_array_free(plugin->components, TRUE);
61 }
62
63 g_free(plugin);
64 }
65
66 static
67 struct plugin *plugin_create(void)
68 {
69 struct plugin *plugin = g_new0(struct plugin, 1);
70
71 if (!plugin) {
72 goto end;
73 }
74
75 plugin->components = g_ptr_array_new_with_free_func(component_destroy);
76 if (!plugin->components) {
77 g_free(plugin);
78 plugin = NULL;
79 goto end;
80 }
81 end:
82 return plugin;
83 }
84
85 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
86 static
87 struct dirent *alloc_dirent(const char *path)
88 {
89 size_t len;
90 long name_max;
91 struct dirent *entry;
92
93 name_max = pathconf(path, _PC_NAME_MAX);
94 if (name_max == -1) {
95 name_max = PATH_MAX;
96 }
97 len = offsetof(struct dirent, d_name) + name_max + 1;
98 entry = zmalloc(len);
99 return entry;
100 }
101
102 static
103 struct plugin *load_plugin(GModule *module)
104 {
105 return NULL;
106 }
107
108 static
109 enum bt_component_factory_status
110 bt_component_factory_load_file(struct bt_component_factory *factory,
111 const char *path)
112 {
113 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
114 size_t path_len;
115 GModule *module;
116 struct plugin *plugin;
117
118 if (!factory || !path) {
119 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
120 goto end;
121 }
122
123 path_len = strlen(path);
124 if (path_len <= PLUGIN_SUFFIX_LEN) {
125 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
126 goto end;
127 }
128
129 path_len++;
130 /*
131 * Check if the file ends with a known plugin file type suffix (i.e. .so
132 * or .la on Linux).
133 */
134 if (strncmp(NATIVE_PLUGIN_SUFFIX,
135 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
136 NATIVE_PLUGIN_SUFFIX_LEN) &&
137 strncmp(LIBTOOL_PLUGIN_SUFFIX,
138 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
139 LIBTOOL_PLUGIN_SUFFIX_LEN)) {
140 /* Name indicates that this is not a plugin file. */
141 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
142 goto end;
143 }
144
145 module = g_module_open(path, 0);
146 if (!module) {
147 printf_error("Module open error: %s", g_module_error());
148 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
149 goto end;
150 }
151
152 /* Check if the module defines the appropriate entry points */
153 plugin = load_plugin(module);
154 if (!plugin) {
155 /* Not a Babeltrace plugin */
156 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
157 if (!g_module_close(module)) {
158 printf_error("Module close error: %s",
159 g_module_error());
160 }
161 goto end;
162 }
163 end:
164 return ret;
165 }
166
167 static
168 enum bt_component_factory_status
169 bt_component_factory_load_dir_recursive(struct bt_component_factory *factory,
170 const char *path)
171 {
172 DIR *directory = NULL;
173 struct dirent *entry = NULL, *result = NULL;
174 char *file_path = NULL;
175 size_t path_len = strlen(path);
176 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
177
178 if (path_len >= PATH_MAX) {
179 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
180 goto end;
181 }
182
183 entry = alloc_dirent(path);
184 if (!entry) {
185 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
186 goto end;
187 }
188
189 file_path = zmalloc(PATH_MAX);
190 if (!file_path) {
191 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
192 goto end;
193 }
194
195 strncpy(file_path, path, path_len);
196 /* Append a trailing '/' to the path */
197 if (file_path[path_len - 1] != '/') {
198 file_path[path_len++] = '/';
199 }
200
201 /* Recursively walk directory */
202 while (!readdir_r(directory, entry, &result) && result) {
203 struct stat st;
204 int stat_ret;
205 size_t file_name_len = strlen(result->d_name);
206
207 if (path_len + file_name_len >= PATH_MAX) {
208 continue;
209 }
210
211 strncpy(file_path + path_len, result->d_name, file_name_len);
212 file_path[path_len + file_name_len] = '\0';
213
214 stat_ret = stat(file_path, &st);
215 if (stat_ret < 0) {
216 /* Continue to next file / directory. */
217 printf_perror("Failed to stat() plugin file");
218 continue;
219 }
220
221 if (S_ISDIR(st.st_mode)) {
222 ret = bt_component_factory_load_dir_recursive(factory,
223 file_path);
224 if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
225 goto end;
226 }
227 } else if (S_ISREG(st.st_mode)) {
228 bt_component_factory_load_file(factory, file_path);
229 }
230 }
231 end:
232 free(entry);
233 free(file_path);
234 return ret;
235 }
236
237 void bt_component_factory_destroy(struct bt_component_factory *factory)
238 {
239 if (!factory) {
240 return;
241 }
242
243 g_free(factory);
244 }
245
246 struct bt_component_factory *
247 bt_component_factory_create(void)
248 {
249 struct bt_component_factory *factory;
250
251 factory = g_new0(struct bt_component_factory, 1);
252 if (!factory) {
253 goto end;
254 }
255
256 factory->plugins = g_ptr_array_new_with_free_func(plugin_destroy);
257 if (!factory->plugins) {
258 goto error;
259 }
260 end:
261 return factory;
262 error:
263 bt_component_factory_destroy(factory);
264 return NULL;
265 }
266
267 enum bt_component_factory_status bt_component_factory_load(
268 struct bt_component_factory *factory, const char *path)
269 {
270 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
271 DIR *directory = NULL;
272
273 if (!factory || !path) {
274 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
275 goto end;
276 }
277
278 directory = opendir(path) ;
279 if (!directory) {
280 switch (errno) {
281 case ENOTDIR:
282 /* Try loading as a file. */
283 break;
284 case ENOENT:
285 ret = BT_COMPONENT_FACTORY_STATUS_NOENT;
286 goto end;
287 default:
288 ret = BT_COMPONENT_FACTORY_STATUS_IO;
289 goto end;
290 }
291 }
292
293 if (directory) {
294 ret = bt_component_factory_load_dir_recursive(factory, path);
295 } else {
296 ret = bt_component_factory_load_file(factory, path);
297 }
298 end:
299 if (directory) {
300 closedir(directory);
301 }
302 return ret;
303 }
This page took 0.034627 seconds and 4 git commands to generate.