Add ctf-text entry point initialization comment
[babeltrace.git] / plugins / 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>
31#include <babeltrace/babeltrace-internal.h>
32#include <babeltrace/compiler.h>
33#include <unistd.h>
34#include <stdlib.h>
35#include <sys/stat.h>
fb2dcc52 36#include <gmodule.h>
f3e4505b
JG
37
38#define NATIVE_PLUGIN_SUFFIX ".so"
39#define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
40#define LIBTOOL_PLUGIN_SUFFIX ".la"
41#define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
73299554
JG
42#define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
43 sizeof(LIBTOOL_PLUGIN_SUFFIX))
f3e4505b 44
f3e4505b
JG
45/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
46static
47struct dirent *alloc_dirent(const char *path)
48{
49 size_t len;
50 long name_max;
51 struct dirent *entry;
52
53 name_max = pathconf(path, _PC_NAME_MAX);
54 if (name_max == -1) {
55 name_max = PATH_MAX;
56 }
57 len = offsetof(struct dirent, d_name) + name_max + 1;
58 entry = zmalloc(len);
59 return entry;
60}
61
62static
63enum bt_component_factory_status
64bt_component_factory_load_file(struct bt_component_factory *factory,
65 const char *path)
66{
67 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
fb2dcc52 68 enum bt_component_status component_status;
f3e4505b
JG
69 size_t path_len;
70 GModule *module;
fb2dcc52 71 struct bt_plugin *plugin;
f3e4505b
JG
72
73 if (!factory || !path) {
74 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
75 goto end;
76 }
77
78 path_len = strlen(path);
79 if (path_len <= PLUGIN_SUFFIX_LEN) {
73299554 80 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
f3e4505b
JG
81 goto end;
82 }
83
84 path_len++;
85 /*
86 * Check if the file ends with a known plugin file type suffix (i.e. .so
87 * or .la on Linux).
88 */
89 if (strncmp(NATIVE_PLUGIN_SUFFIX,
90 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
91 NATIVE_PLUGIN_SUFFIX_LEN) &&
92 strncmp(LIBTOOL_PLUGIN_SUFFIX,
93 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
94 LIBTOOL_PLUGIN_SUFFIX_LEN)) {
73299554
JG
95 /* Name indicates that this is not a plugin file. */
96 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
f3e4505b
JG
97 goto end;
98 }
99
100 module = g_module_open(path, 0);
101 if (!module) {
102 printf_error("Module open error: %s", g_module_error());
73299554 103 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
f3e4505b
JG
104 goto end;
105 }
106
fb2dcc52
JG
107 /* Load plugin and make sure it defines the required entry points */
108 plugin = bt_plugin_create(module);
73299554 109 if (!plugin) {
fb2dcc52 110 ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
73299554
JG
111 if (!g_module_close(module)) {
112 printf_error("Module close error: %s",
fb2dcc52
JG
113 g_module_error());
114 }
115 goto end;
116 }
117
118 component_status = bt_plugin_register_component_classes(plugin,
119 factory);
120 if (component_status != BT_COMPONENT_STATUS_OK) {
121 switch (component_status) {
122 case BT_COMPONENT_STATUS_NOMEM:
123 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
124 break;
125 default:
126 ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
127 break;
73299554 128 }
fb2dcc52
JG
129 bt_plugin_put(plugin);
130 plugin = NULL;
73299554
JG
131 goto end;
132 }
fb2dcc52 133 g_ptr_array_add(factory->plugins, plugin);
f3e4505b
JG
134end:
135 return ret;
136}
137
138static
139enum bt_component_factory_status
140bt_component_factory_load_dir_recursive(struct bt_component_factory *factory,
141 const char *path)
142{
143 DIR *directory = NULL;
144 struct dirent *entry = NULL, *result = NULL;
145 char *file_path = NULL;
146 size_t path_len = strlen(path);
147 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
148
149 if (path_len >= PATH_MAX) {
150 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
151 goto end;
152 }
153
154 entry = alloc_dirent(path);
155 if (!entry) {
156 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
157 goto end;
158 }
159
160 file_path = zmalloc(PATH_MAX);
161 if (!file_path) {
162 ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
163 goto end;
164 }
165
166 strncpy(file_path, path, path_len);
167 /* Append a trailing '/' to the path */
168 if (file_path[path_len - 1] != '/') {
169 file_path[path_len++] = '/';
170 }
171
172 /* Recursively walk directory */
173 while (!readdir_r(directory, entry, &result) && result) {
174 struct stat st;
175 int stat_ret;
176 size_t file_name_len = strlen(result->d_name);
177
178 if (path_len + file_name_len >= PATH_MAX) {
179 continue;
180 }
181
182 strncpy(file_path + path_len, result->d_name, file_name_len);
183 file_path[path_len + file_name_len] = '\0';
184
185 stat_ret = stat(file_path, &st);
186 if (stat_ret < 0) {
187 /* Continue to next file / directory. */
188 printf_perror("Failed to stat() plugin file");
189 continue;
190 }
191
192 if (S_ISDIR(st.st_mode)) {
193 ret = bt_component_factory_load_dir_recursive(factory,
194 file_path);
195 if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
196 goto end;
197 }
198 } else if (S_ISREG(st.st_mode)) {
73299554 199 bt_component_factory_load_file(factory, file_path);
f3e4505b
JG
200 }
201 }
202end:
203 free(entry);
204 free(file_path);
205 return ret;
206}
207
208void bt_component_factory_destroy(struct bt_component_factory *factory)
209{
210 if (!factory) {
211 return;
212 }
213
fb2dcc52
JG
214 if (factory->plugins) {
215 g_ptr_array_free(factory->plugins, TRUE);
216 }
217 if (factory->components) {
218 g_ptr_array_free(factory->components, TRUE);
219 }
f3e4505b
JG
220 g_free(factory);
221}
222
223struct bt_component_factory *
224bt_component_factory_create(void)
225{
226 struct bt_component_factory *factory;
227
228 factory = g_new0(struct bt_component_factory, 1);
229 if (!factory) {
230 goto end;
231 }
232
fb2dcc52
JG
233 factory->plugins = g_ptr_array_new_with_free_func(
234 (GDestroyNotify) bt_plugin_put);
73299554 235 if (!factory->plugins) {
f3e4505b
JG
236 goto error;
237 }
fb2dcc52
JG
238 factory->components = g_ptr_array_new_with_free_func(
239 (GDestroyNotify) bt_component_put);
240 if (!factory->components) {
241 goto error;
242 }
f3e4505b
JG
243end:
244 return factory;
245error:
246 bt_component_factory_destroy(factory);
247 return NULL;
248}
249
250enum bt_component_factory_status bt_component_factory_load(
251 struct bt_component_factory *factory, const char *path)
252{
253 enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
254 DIR *directory = NULL;
255
256 if (!factory || !path) {
257 ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
258 goto end;
259 }
260
261 directory = opendir(path) ;
262 if (!directory) {
263 switch (errno) {
264 case ENOTDIR:
265 /* Try loading as a file. */
266 break;
267 case ENOENT:
268 ret = BT_COMPONENT_FACTORY_STATUS_NOENT;
269 goto end;
270 default:
271 ret = BT_COMPONENT_FACTORY_STATUS_IO;
272 goto end;
273 }
274 }
275
276 if (directory) {
277 ret = bt_component_factory_load_dir_recursive(factory, path);
278 } else {
279 ret = bt_component_factory_load_file(factory, path);
280 }
281end:
282 if (directory) {
283 closedir(directory);
284 }
285 return ret;
286}
This page took 0.042555 seconds and 4 git commands to generate.