lib: open trace error handling fix
[babeltrace.git] / lib / context.c
1 /*
2 * context.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/context.h>
24 #include <babeltrace/context-internal.h>
25 #include <babeltrace/trace-handle.h>
26 #include <babeltrace/trace-handle-internal.h>
27 #include <babeltrace/trace-collection.h>
28 #include <babeltrace/format.h>
29 #include <stdlib.h>
30
31 #include <fts.h>
32 #include <fcntl.h> /* For O_RDONLY */
33
34 /* TODO ybrosseau: should be hidden in the CTF format */
35 #include <babeltrace/ctf/types.h> /* for ctf_move_pos_slow */
36
37 #include <glib.h>
38
39 struct bt_context *bt_context_create(void)
40 {
41 struct bt_context *ctx;
42
43 ctx = g_new0(struct bt_context, 1);
44 ctx->refcount = 1;
45 /* Negative handle id are errors. */
46 ctx->last_trace_handle_id = 0;
47
48 /* Instanciate the trace handle container */
49 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
50 g_direct_equal, NULL,
51 (GDestroyNotify) bt_trace_handle_destroy);
52
53 ctx->tc = g_new0(struct trace_collection, 1);
54 init_trace_collection(ctx->tc);
55
56 return ctx;
57 }
58
59 int bt_context_add_trace(struct bt_context *ctx, const char *path,
60 const char *format_str)
61 {
62 struct trace_descriptor *td;
63 struct format *fmt;
64 struct bt_trace_handle *handle;
65
66 fmt = bt_lookup_format(g_quark_from_string(format_str));
67 td = fmt->open_trace(path, O_RDONLY,
68 ctf_move_pos_slow, NULL);
69 if (!td) {
70 fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n",
71 path);
72 return -1;
73 }
74
75 /* Create an handle for the trace */
76 handle = bt_trace_handle_create(ctx);
77 if (handle < 0) {
78 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
79 path);
80 return -1;
81 }
82 handle->format = fmt;
83 handle->td = td;
84 strncpy(handle->path, path, PATH_MAX);
85 handle->path[PATH_MAX - 1] = '\0';
86
87 /* Add new handle to container */
88 g_hash_table_insert(ctx->trace_handles,
89 (gpointer) (unsigned long) handle->id,
90 handle);
91 return trace_collection_add(ctx->tc, td);
92 }
93
94 /*
95 * Unable to open toplevel: failure.
96 * Unable to open some subdirectory or file: warn and continue;
97 */
98 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
99 const char *format_str)
100 {
101 FTS *tree;
102 FTSENT *node;
103 GArray *trace_ids;
104 char lpath[PATH_MAX];
105 char * const paths[2] = { lpath, NULL };
106 int ret;
107
108 /*
109 * Need to copy path, because fts_open can change it.
110 * It is the pointer array, not the strings, that are constant.
111 */
112 strncpy(lpath, path, PATH_MAX);
113 lpath[PATH_MAX - 1] = '\0';
114
115 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
116 if (tree == NULL) {
117 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
118 path);
119 return -EINVAL;
120 }
121
122 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
123
124 while ((node = fts_read(tree))) {
125 int dirfd, metafd;
126
127 if (!(node->fts_info & FTS_D))
128 continue;
129
130 dirfd = open(node->fts_accpath, 0);
131 if (dirfd < 0) {
132 fprintf(stderr, "[error] [Context] Unable to open trace "
133 "directory file descriptor.\n");
134 ret = dirfd;
135 goto error;
136 }
137 metafd = openat(dirfd, "metadata", O_RDONLY);
138 if (metafd < 0) {
139 ret = close(dirfd);
140 if (ret < 0) {
141 perror("close");
142 goto error;
143 }
144 } else {
145 int trace_id;
146
147 ret = close(metafd);
148 if (ret < 0) {
149 perror("close");
150 goto error;
151 }
152 ret = close(dirfd);
153 if (ret < 0) {
154 perror("close");
155 goto error;
156 }
157
158 trace_id = bt_context_add_trace(ctx,
159 node->fts_accpath, format_str);
160 if (trace_id < 0) {
161 fprintf(stderr, "[error] [Context] opening trace \"%s\" from %s "
162 "for reading.\n", node->fts_accpath, path);
163 ret = trace_id;
164 goto error;
165 }
166 g_array_append_val(trace_ids, trace_id);
167 }
168 }
169
170 g_array_free(trace_ids, TRUE);
171 return 0;
172
173 error:
174 return ret;
175 }
176
177 void bt_context_remove_trace(struct bt_context *ctx, int handle_id)
178 {
179 struct bt_trace_handle *handle;
180
181 handle = g_hash_table_lookup(ctx->trace_handles,
182 (gpointer) (unsigned long) handle_id);
183 assert(handle != NULL);
184
185 /* Remove from containers */
186 trace_collection_remove(ctx->tc, handle->td);
187 g_hash_table_remove(ctx->trace_handles,
188 (gpointer) (unsigned long) handle_id);
189
190 /* Close the trace */
191 handle->format->close_trace(handle->td);
192
193 /* Destory the handle */
194 bt_trace_handle_destroy(handle);
195 }
196
197 static
198 void bt_context_destroy(struct bt_context *ctx)
199 {
200 finalize_trace_collection(ctx->tc);
201
202 /* Remote all traces. The g_hash_table_destroy will call
203 * bt_trace_handle_destroy on each elements.
204 */
205 g_hash_table_destroy(ctx->trace_handles);
206
207 /* ctx->tc should always be valid */
208 assert(ctx->tc != NULL);
209 g_free(ctx->tc);
210 g_free(ctx);
211 }
212
213 void bt_context_get(struct bt_context *ctx)
214 {
215 ctx->refcount++;
216 }
217
218 void bt_context_put(struct bt_context *ctx)
219 {
220 ctx->refcount--;
221 if (ctx->refcount == 0)
222 bt_context_destroy(ctx);
223 }
This page took 0.034413 seconds and 5 git commands to generate.