Leak fix
[babeltrace.git] / lib / plugin-system / component-factory.c
CommitLineData
f3e4505b
JG
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>
38b48196 31#include <babeltrace/plugin/component-class-internal.h>
92893b7b
JG
32#include <babeltrace/plugin/source-internal.h>
33#include <babeltrace/plugin/sink-internal.h>
f3e4505b
JG
34#include <babeltrace/babeltrace-internal.h>
35#include <babeltrace/compiler.h>
b8a06801 36#include <babeltrace/ref.h>
f3e4505b
JG
37#include <unistd.h>
38#include <stdlib.h>
39#include <sys/stat.h>
fb2dcc52 40#include <gmodule.h>
39cfa40f 41#include <stdbool.h>
f3e4505b
JG
42
43#define NATIVE_PLUGIN_SUFFIX ".so"
44#define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
45#define LIBTOOL_PLUGIN_SUFFIX ".la"
46#define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
73299554
JG
47#define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
48 sizeof(LIBTOOL_PLUGIN_SUFFIX))
f3e4505b 49
f3e4505b
JG
50/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
51static
52struct dirent *alloc_dirent(const char *path)
53{
54 size_t len;
55 long name_max;
56 struct dirent *entry;
57
58 name_max = pathconf(path, _PC_NAME_MAX);
59 if (name_max == -1) {
60 name_max = PATH_MAX;
61 }
62 len = offsetof(struct dirent, d_name) + name_max + 1;
63 entry = zmalloc(len);
64 return entry;
65}
66
67static
68enum bt_component_factory_status
69bt_component_factory_load_file(struct bt_component_factory *factory,
70 const char *path)
71{
72 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
fb2dcc52 73 enum bt_component_status component_status;
f3e4505b
JG
74 size_t path_len;
75 GModule *module;
fb2dcc52 76 struct bt_plugin *plugin;
92893b7b 77 bool is_libtool_wrapper = false, is_shared_object = false;
f3e4505b
JG
78
79 if (!factory || !path) {
80 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
81 goto end;
82 }
83
84 path_len = strlen(path);
85 if (path_len <= PLUGIN_SUFFIX_LEN) {
73299554 86 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
f3e4505b
JG
87 goto end;
88 }
89
90 path_len++;
91 /*
92 * Check if the file ends with a known plugin file type suffix (i.e. .so
93 * or .la on Linux).
94 */
92893b7b 95 is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
b8a06801 96 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
92893b7b
JG
97 LIBTOOL_PLUGIN_SUFFIX_LEN);
98 is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
99 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
100 NATIVE_PLUGIN_SUFFIX_LEN);
101 if (!is_shared_object && !is_libtool_wrapper) {
73299554
JG
102 /* Name indicates that this is not a plugin file. */
103 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
f3e4505b
JG
104 goto end;
105 }
106
107 module = g_module_open(path, 0);
108 if (!module) {
109 printf_error("Module open error: %s", g_module_error());
73299554 110 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
f3e4505b
JG
111 goto end;
112 }
113
fb2dcc52 114 /* Load plugin and make sure it defines the required entry points */
7c7c0433 115 plugin = bt_plugin_create(module, path);
73299554 116 if (!plugin) {
fb2dcc52 117 ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
73299554
JG
118 if (!g_module_close(module)) {
119 printf_error("Module close error: %s",
fb2dcc52
JG
120 g_module_error());
121 }
122 goto end;
123 }
124
0cc9e945
JG
125 BT_MOVE(factory->current_plugin, plugin);
126 component_status = bt_plugin_register_component_classes(
127 factory->current_plugin, factory);
128 BT_PUT(factory->current_plugin);
fb2dcc52
JG
129 if (component_status != BT_COMPONENT_STATUS_OK) {
130 switch (component_status) {
131 case BT_COMPONENT_STATUS_NOMEM:
132 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
133 break;
134 default:
135 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
136 break;
73299554 137 }
b8a06801 138
73299554
JG
139 goto end;
140 }
f3e4505b
JG
141end:
142 return ret;
143}
144
145static
146enum bt_component_factory_status
5d4cba3d
JG
147bt_component_factory_load_dir(struct bt_component_factory *factory,
148 const char *path, bool recurse)
f3e4505b
JG
149{
150 DIR *directory = NULL;
151 struct dirent *entry = NULL, *result = NULL;
152 char *file_path = NULL;
153 size_t path_len = strlen(path);
154 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
155
156 if (path_len >= PATH_MAX) {
157 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
158 goto end;
159 }
160
161 entry = alloc_dirent(path);
162 if (!entry) {
163 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
164 goto end;
165 }
166
167 file_path = zmalloc(PATH_MAX);
168 if (!file_path) {
169 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
170 goto end;
171 }
172
173 strncpy(file_path, path, path_len);
174 /* Append a trailing '/' to the path */
175 if (file_path[path_len - 1] != '/') {
176 file_path[path_len++] = '/';
177 }
178
33bceaf8
JG
179 directory = opendir(file_path);
180 if (!directory) {
181 perror("Failed to open plug-in directory");
182 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
183 goto end;
184 }
185
f3e4505b
JG
186 /* Recursively walk directory */
187 while (!readdir_r(directory, entry, &result) && result) {
188 struct stat st;
189 int stat_ret;
33bceaf8
JG
190 size_t file_name_len;
191
192 if (result->d_name[0] == '.') {
193 /* Skip hidden files, . and .. */
194 continue;
195 }
196
197 file_name_len = strlen(result->d_name);
f3e4505b
JG
198
199 if (path_len + file_name_len >= PATH_MAX) {
200 continue;
201 }
202
203 strncpy(file_path + path_len, result->d_name, file_name_len);
204 file_path[path_len + file_name_len] = '\0';
205
206 stat_ret = stat(file_path, &st);
207 if (stat_ret < 0) {
208 /* Continue to next file / directory. */
209 printf_perror("Failed to stat() plugin file");
210 continue;
211 }
212
5d4cba3d
JG
213 if (S_ISDIR(st.st_mode) && recurse) {
214 ret = bt_component_factory_load_dir(factory,
215 file_path, true);
f3e4505b
JG
216 if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
217 goto end;
218 }
219 } else if (S_ISREG(st.st_mode)) {
73299554 220 bt_component_factory_load_file(factory, file_path);
f3e4505b
JG
221 }
222 }
223end:
33bceaf8
JG
224 if (directory) {
225 if (closedir(directory)) {
226 /*
227 * We don't want to override the error since there is
228 * nothing could do.
229 */
230 perror("Failed to close plug-in directory");
231 }
232 }
f3e4505b
JG
233 free(entry);
234 free(file_path);
235 return ret;
236}
237
b8a06801
JG
238static
239void bt_component_factory_destroy(struct bt_object *obj)
f3e4505b 240{
b8a06801
JG
241 struct bt_component_factory *factory = NULL;
242
243 assert(obj);
244 factory = container_of(obj, struct bt_component_factory, base);
f3e4505b 245
92893b7b
JG
246 if (factory->component_classes) {
247 g_ptr_array_free(factory->component_classes, TRUE);
fb2dcc52 248 }
f3e4505b
JG
249 g_free(factory);
250}
251
b8a06801 252struct bt_component_factory *bt_component_factory_create(void)
f3e4505b
JG
253{
254 struct bt_component_factory *factory;
255
256 factory = g_new0(struct bt_component_factory, 1);
257 if (!factory) {
258 goto end;
259 }
260
b8a06801 261 bt_object_init(factory, bt_component_factory_destroy);
92893b7b 262 factory->component_classes = g_ptr_array_new_with_free_func(
b8a06801 263 (GDestroyNotify) bt_put);
92893b7b 264 if (!factory->component_classes) {
fb2dcc52
JG
265 goto error;
266 }
f3e4505b
JG
267end:
268 return factory;
269error:
b8a06801
JG
270 BT_PUT(factory);
271 return factory;
f3e4505b
JG
272}
273
38b48196 274int bt_component_factory_get_component_class_count(
92893b7b
JG
275 struct bt_component_factory *factory)
276{
38b48196
JG
277 return factory ? factory->component_classes->len : -1;
278}
279
280struct bt_component_class *bt_component_factory_get_component_class_index(
281 struct bt_component_factory *factory, int index)
282{
283 struct bt_component_class *component_class = NULL;
284
285 if (!factory || index < 0 || index >= factory->component_classes->len) {
286 goto end;
287 }
288
289 component_class = bt_get(g_ptr_array_index(
290 factory->component_classes, index));
291end:
292 return component_class;
293}
294
295struct bt_component_class *bt_component_factory_get_component_class(
296 struct bt_component_factory *factory,
297 const char *plugin_name, enum bt_component_type type,
298 const char *component_name)
299{
300 size_t i;
301 struct bt_component_class *component_class = NULL;
302
303 if (!factory || (!plugin_name && !component_name &&
304 type == BT_COMPONENT_TYPE_UNKNOWN)) {
305 /* At least one criterion must be provided. */
306 goto no_match;
307 }
308
309 for (i = 0; i < factory->component_classes->len; i++) {
310 struct bt_plugin *plugin = NULL;
311
312 component_class = g_ptr_array_index(factory->component_classes,
313 i);
314 plugin = bt_component_class_get_plugin(component_class);
315 assert(plugin);
316
317 if (type != BT_COMPONENT_TYPE_UNKNOWN) {
318 if (type != bt_component_class_get_type(
319 component_class)) {
0cc9e945 320 bt_put(plugin);
38b48196
JG
321 continue;
322 }
323 }
324
325 if (plugin_name) {
326 const char *cur_plugin_name = bt_plugin_get_name(
327 plugin);
328
329 assert(cur_plugin_name);
330 if (strcmp(plugin_name, cur_plugin_name)) {
0cc9e945 331 bt_put(plugin);
38b48196
JG
332 continue;
333 }
334 }
335
336 if (component_name) {
337 const char *cur_cc_name = bt_component_class_get_name(
338 component_class);
339
340 assert(cur_cc_name);
341 if (strcmp(component_name, cur_cc_name)) {
0cc9e945 342 bt_put(plugin);
38b48196
JG
343 continue;
344 }
345 }
346
0cc9e945 347 bt_put(plugin);
38b48196
JG
348 /* All criteria met. */
349 goto match;
350 }
351
352no_match:
92893b7b 353 return NULL;
38b48196
JG
354match:
355 return bt_get(component_class);
92893b7b
JG
356}
357
5d4cba3d
JG
358static
359enum bt_component_factory_status _bt_component_factory_load(
360 struct bt_component_factory *factory, const char *path,
361 bool recursive)
f3e4505b
JG
362{
363 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
f3e4505b
JG
364
365 if (!factory || !path) {
366 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
367 goto end;
368 }
369
b8a06801
JG
370 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
371 ret = BT_COMPONENT_FACTORY_STATUS_NOENT;
372 goto end;
f3e4505b
JG
373 }
374
b8a06801 375 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
5d4cba3d 376 ret = bt_component_factory_load_dir(factory, path, recursive);
b8a06801
JG
377 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) ||
378 g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
f3e4505b 379 ret = bt_component_factory_load_file(factory, path);
b8a06801
JG
380 } else {
381 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
382 goto end;
f3e4505b
JG
383 }
384end:
f3e4505b
JG
385 return ret;
386}
92893b7b 387
5d4cba3d
JG
388enum bt_component_factory_status bt_component_factory_load_recursive(
389 struct bt_component_factory *factory, const char *path)
390{
391 return _bt_component_factory_load(factory, path, true);
392}
393
394enum bt_component_factory_status bt_component_factory_load(
395 struct bt_component_factory *factory, const char *path)
396{
397 return _bt_component_factory_load(factory, path, false);
398}
399
38b48196 400static
92893b7b 401enum bt_component_factory_status
38b48196 402add_component_class(struct bt_component_factory *factory, const char *name,
7c7c0433
JG
403 const char *description, bt_component_init_cb init,
404 enum bt_component_type type)
92893b7b 405{
0859fe1f 406 struct bt_component_class *component_class;
92893b7b
JG
407 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
408
409 if (!factory || !name || !init) {
410 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
411 goto end;
412 }
0859fe1f 413 assert(factory->current_plugin);
92893b7b 414
0859fe1f
JG
415 /*
416 * Ensure this component class does not clash with a currently
417 * registered class.
418 */
419 component_class = bt_component_factory_get_component_class(factory,
420 bt_plugin_get_name(factory->current_plugin), type, name);
421 if (component_class) {
422 struct bt_plugin *plugin = bt_component_class_get_plugin(
423 component_class);
424
425 printf_warning("Duplicate component class registration attempted. Component class %s being registered by plugin %s (path: %s) conflicts with one already registered by plugin %s (path: %s)",
426 name, bt_plugin_get_name(factory->current_plugin),
427 bt_plugin_get_path(factory->current_plugin),
428 bt_plugin_get_name(plugin),
429 bt_plugin_get_path(plugin));
430 ret = BT_COMPONENT_FACTORY_STATUS_DUPLICATE;
431 BT_PUT(component_class);
432 bt_put(plugin);
433 goto end;
434 }
435
436 component_class = bt_component_class_create(type, name, description,
4b70dd83 437 init, factory->current_plugin);
0859fe1f 438 g_ptr_array_add(factory->component_classes, component_class);
92893b7b
JG
439end:
440 return ret;
441}
38b48196
JG
442
443enum bt_component_factory_status
444bt_component_factory_register_source_component_class(
445 struct bt_component_factory *factory, const char *name,
7c7c0433 446 const char *description, bt_component_init_cb init)
38b48196 447{
7c7c0433 448 return add_component_class(factory, name, description, init,
38b48196
JG
449 BT_COMPONENT_TYPE_SOURCE);
450}
451
452enum bt_component_factory_status
453bt_component_factory_register_sink_component_class(
454 struct bt_component_factory *factory, const char *name,
7c7c0433 455 const char *description, bt_component_init_cb init)
38b48196 456{
7c7c0433 457 return add_component_class(factory, name, description, init,
38b48196
JG
458 BT_COMPONENT_TYPE_SINK);
459}
This page took 0.042644 seconds and 4 git commands to generate.