Fix: SIGBUS error on reading past a file's end in mmap'ed region
[babeltrace.git] / plugins / ctf / fs / fs.c
1 /*
2 * fs.c
3 *
4 * Babeltrace CTF file system Reader Component
5 *
6 * Copyright 2016 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/plugin-system.h>
30 #include <babeltrace/plugin/notification/iterator.h>
31 #include <glib.h>
32 #include <assert.h>
33 #include <unistd.h>
34 #include "fs.h"
35 #include "metadata.h"
36 #include "data-stream.h"
37 #include "file.h"
38
39 #define PRINT_ERR_STREAM ctf_fs->error_fp
40 #define PRINT_PREFIX "ctf-fs"
41 #include "print.h"
42
43 static bool ctf_fs_debug;
44
45 static
46 struct bt_notification *ctf_fs_iterator_get(
47 struct bt_notification_iterator *iterator)
48 {
49 struct bt_notification *notification = NULL;
50 struct ctf_fs_component *ctf_fs;
51 struct bt_component *component = bt_notification_iterator_get_component(
52 iterator);
53
54 if (!component) {
55 goto end;
56 }
57
58 ctf_fs = bt_component_get_private_data(component);
59 if (!ctf_fs) {
60 goto end;
61 }
62
63 notification = bt_get(ctf_fs->current_notification);
64 end:
65 BT_PUT(component);
66 return notification;
67 }
68
69 static
70 enum bt_notification_iterator_status ctf_fs_iterator_next(
71 struct bt_notification_iterator *iterator)
72 {
73 enum bt_notification_iterator_status ret;
74 struct bt_notification *notification = NULL;
75 struct ctf_fs_component *ctf_fs;
76 struct bt_component *component = bt_notification_iterator_get_component(
77 iterator);
78
79 if (!component) {
80 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
81 goto end;
82 }
83
84 ctf_fs = bt_component_get_private_data(component);
85 assert(ctf_fs);
86
87 ret = ctf_fs_data_stream_get_next_notification(ctf_fs, &notification, 0);
88 if (ret || !notification) {
89 goto end;
90 }
91
92 bt_put(ctf_fs->current_notification);
93 ctf_fs->current_notification = notification;
94 end:
95 BT_PUT(component);
96 return ret;
97 }
98
99 static
100 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it)
101 {
102 g_free(ctf_it);
103 }
104
105 static
106 void ctf_fs_iterator_destroy(struct bt_notification_iterator *it)
107 {
108 void *data = bt_notification_iterator_get_private_data(it);
109
110 ctf_fs_iterator_destroy_data(data);
111 }
112
113 static
114 enum bt_component_status ctf_fs_iterator_init(struct bt_component *source,
115 struct bt_notification_iterator *it)
116 {
117 struct ctf_fs_iterator *ctf_it;
118 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
119
120 assert(source && it);
121 ctf_it = g_new0(struct ctf_fs_iterator, 1);
122 if (!ctf_it) {
123 ret = BT_COMPONENT_STATUS_NOMEM;
124 goto end;
125 }
126
127 ret = bt_notification_iterator_set_get_cb(it, ctf_fs_iterator_get);
128 if (ret) {
129 goto error;
130 }
131
132 ret = bt_notification_iterator_set_next_cb(it, ctf_fs_iterator_next);
133 if (ret) {
134 goto error;
135 }
136
137 ret = bt_notification_iterator_set_destroy_cb(it,
138 ctf_fs_iterator_destroy);
139 if (ret) {
140 goto error;
141 }
142
143 ret = bt_notification_iterator_set_private_data(it, ctf_it);
144 if (ret) {
145 goto error;
146 }
147 end:
148 return ret;
149 error:
150 (void) bt_notification_iterator_set_private_data(it, NULL);
151 ctf_fs_iterator_destroy_data(ctf_it);
152 return ret;
153 }
154
155 static
156 void ctf_fs_destroy_data(struct ctf_fs_component *component)
157 {
158 if (component->trace_path) {
159 g_string_free(component->trace_path, TRUE);
160 }
161
162 ctf_fs_metadata_fini(&component->metadata);
163 BT_PUT(component->current_notification);
164 if (component->streams) {
165 g_ptr_array_free(component->streams, TRUE);
166 }
167 g_free(component);
168 }
169
170 static
171 void ctf_fs_destroy(struct bt_component *component)
172 {
173 void *data = bt_component_get_private_data(component);
174
175 ctf_fs_destroy_data(data);
176 }
177
178 static
179 int open_trace_streams(struct ctf_fs_component *ctf_fs)
180 {
181 int ret = 0;
182 const char *name;
183 GError *error = NULL;
184 GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error);
185
186 if (!dir) {
187 PERR("Cannot open directory \"%s\": %s (code %d)\n",
188 ctf_fs->trace_path->str, error->message,
189 error->code);
190 goto error;
191 }
192
193 while ((name = g_dir_read_name(dir))) {
194 struct ctf_fs_file *file = NULL;
195 struct ctf_fs_stream *stream = NULL;
196
197 if (!strcmp(name, CTF_FS_METADATA_FILENAME)) {
198 /* Ignore the metadata stream. */
199 PDBG("Ignoring metadata file \"%s\"\n",
200 name);
201 continue;
202 }
203
204 if (name[0] == '.') {
205 PDBG("Ignoring hidden file \"%s\"\n",
206 name);
207 continue;
208 }
209
210 /* Create the file. */
211 file = ctf_fs_file_create(ctf_fs);
212 if (!file) {
213 PERR("Cannot create stream file object\n");
214 goto error;
215 }
216
217 /* Create full path string. */
218 g_string_append_printf(file->path, "%s/%s",
219 ctf_fs->trace_path->str, name);
220 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
221 PDBG("Ignoring non-regular file \"%s\"\n", name);
222 ctf_fs_file_destroy(file);
223 continue;
224 }
225
226 /* Open the file. */
227 if (ctf_fs_file_open(ctf_fs, file, "rb")) {
228 ctf_fs_file_destroy(file);
229 goto error;
230 }
231
232 /* Create a private stream; file ownership is passed to it. */
233 stream = ctf_fs_stream_create(ctf_fs, file);
234 if (!stream) {
235 ctf_fs_file_destroy(file);
236 goto error;
237 }
238
239 g_ptr_array_add(ctf_fs->streams, stream);
240 }
241
242 goto end;
243 error:
244 ret = -1;
245 end:
246 if (dir) {
247 g_dir_close(dir);
248 dir = NULL;
249 }
250 if (error) {
251 g_error_free(error);
252 }
253 return ret;
254 }
255
256 static
257 void stream_destroy(void *stream)
258 {
259 ctf_fs_stream_destroy((struct ctf_fs_stream *) stream);
260 }
261
262 static
263 struct ctf_fs_component *ctf_fs_create(struct bt_value *params)
264 {
265 struct ctf_fs_component *ctf_fs;
266 struct bt_value *value = NULL;
267 const char *path;
268 enum bt_value_status ret;
269
270 ctf_fs = g_new0(struct ctf_fs_component, 1);
271 if (!ctf_fs) {
272 goto end;
273 }
274
275 /* FIXME: should probably look for a source URI */
276 value = bt_value_map_get(params, "path");
277 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
278 goto error;
279 }
280
281 ret = bt_value_string_get(value, &path);
282 if (ret != BT_VALUE_STATUS_OK) {
283 goto error;
284 }
285
286 ctf_fs->trace_path = g_string_new(path);
287 if (!ctf_fs->trace_path) {
288 goto error;
289 }
290
291 ctf_fs->streams = g_ptr_array_new_with_free_func(stream_destroy);
292 ctf_fs->error_fp = stderr;
293 ctf_fs->page_size = (size_t) getpagesize();
294
295 // FIXME: check error.
296 ctf_fs_metadata_set_trace(ctf_fs);
297
298 ret = open_trace_streams(ctf_fs);
299 if (ret) {
300 goto error;
301 }
302 goto end;
303
304 error:
305 ctf_fs_destroy_data(ctf_fs);
306 ctf_fs = NULL;
307 end:
308 BT_PUT(value);
309 return ctf_fs;
310 }
311
312 BT_HIDDEN
313 enum bt_component_status ctf_fs_init(struct bt_component *source,
314 struct bt_value *params)
315 {
316 struct ctf_fs_component *ctf_fs;
317 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
318
319 assert(source);
320 ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
321 ctf_fs = ctf_fs_create(params);
322 if (!ctf_fs) {
323 ret = BT_COMPONENT_STATUS_NOMEM;
324 goto end;
325 }
326
327 ret = bt_component_set_destroy_cb(source, ctf_fs_destroy);
328 if (ret != BT_COMPONENT_STATUS_OK) {
329 goto error;
330 }
331
332 ret = bt_component_set_private_data(source, ctf_fs);
333 if (ret != BT_COMPONENT_STATUS_OK) {
334 goto error;
335 }
336
337 ret = bt_component_source_set_iterator_init_cb(source,
338 ctf_fs_iterator_init);
339 if (ret != BT_COMPONENT_STATUS_OK) {
340 goto error;
341 }
342 end:
343 return ret;
344 error:
345 (void) bt_component_set_private_data(source, NULL);
346 ctf_fs_destroy_data(ctf_fs);
347 return ret;
348 }
This page took 0.041451 seconds and 4 git commands to generate.