Fix: lttng-live: lttng_live_open_trace_read memory leak
[babeltrace.git] / formats / lttng-live / lttng-live-plugin.c
1 /*
2 * BabelTrace - LTTng live Output
3 *
4 * Copyright 2013 Julien Desfossez <jdesfossez@efficios.com>
5 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <babeltrace/ctf-text/types.h>
27 #include <babeltrace/format.h>
28 #include <babeltrace/babeltrace-internal.h>
29 #include <inttypes.h>
30 #include <sys/mman.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <dirent.h>
36 #include <glib.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <signal.h>
40 #include "lttng-live.h"
41
42 static volatile int should_quit;
43
44 void bt_lttng_live_hook(void)
45 {
46 /*
47 * Dummy function to prevent the linker from discarding this format as
48 * "unused" in static builds.
49 */
50 }
51
52 int lttng_live_should_quit(void)
53 {
54 return should_quit;
55 }
56
57 static
58 void sighandler(int sig)
59 {
60 switch (sig) {
61 case SIGTERM:
62 case SIGINT:
63 should_quit = 1;
64 break;
65 default:
66 break;
67 }
68 }
69
70 /*
71 * TODO: Eventually, this signal handler setup should be done at the
72 * plugin manager level, rather than within this plugin. Beware, we are
73 * not cleaning up the signal handler after plugin execution.
74 */
75 static
76 int setup_sighandler(void)
77 {
78 struct sigaction sa;
79 sigset_t sigset;
80 int ret;
81
82 if ((ret = sigemptyset(&sigset)) < 0) {
83 perror("sigemptyset");
84 return ret;
85 }
86 sa.sa_handler = sighandler;
87 sa.sa_mask = sigset;
88 sa.sa_flags = 0;
89 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
90 perror("sigaction");
91 return ret;
92 }
93 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
94 perror("sigaction");
95 return ret;
96 }
97 return 0;
98 }
99
100 /*
101 * hostname parameter needs to hold MAXNAMLEN chars.
102 */
103 static
104 int parse_url(const char *path, struct lttng_live_ctx *ctx)
105 {
106 char remain[3][MAXNAMLEN] = { { 0 } };
107 int ret = -1, proto, proto_offset = 0;
108 size_t path_len = strlen(path); /* not accounting \0 */
109
110 /*
111 * Since sscanf API does not allow easily checking string length
112 * against a size defined by a macro. Test it beforehand on the
113 * input. We know the output is always <= than the input length.
114 */
115 if (path_len >= MAXNAMLEN) {
116 goto end;
117 }
118 ret = sscanf(path, "net%d://", &proto);
119 if (ret < 1) {
120 proto = 4;
121 /* net:// */
122 proto_offset = strlen("net://");
123 } else {
124 /* net4:// or net6:// */
125 proto_offset = strlen("netX://");
126 }
127 if (proto_offset > path_len) {
128 goto end;
129 }
130 if (proto == 6) {
131 fprintf(stderr, "[error] IPv6 is currently unsupported by lttng-live\n");
132 goto end;
133 }
134 /* TODO : parse for IPv6 as well */
135 /* Parse the hostname or IP */
136 ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s",
137 ctx->relay_hostname, remain[0]);
138 if (ret == 2) {
139 /* Optional port number */
140 switch (remain[0][0]) {
141 case ':':
142 ret = sscanf(remain[0], ":%d%s", &ctx->port, remain[1]);
143 /* Optional session ID with port number */
144 if (ret == 2) {
145 ret = sscanf(remain[1], "/%s", remain[2]);
146 /* Accept 0 or 1 (optional) */
147 if (ret < 0) {
148 goto end;
149 }
150 } else if (ret == 0) {
151 fprintf(stderr, "[error] Missing port number after delimitor ':'\n");
152 ret = -1;
153 goto end;
154 }
155 break;
156 case '/':
157 /* Optional session ID */
158 ret = sscanf(remain[0], "/%s", remain[2]);
159 /* Accept 0 or 1 (optional) */
160 if (ret < 0) {
161 goto end;
162 }
163 break;
164 default:
165 fprintf(stderr, "[error] wrong delimitor : %c\n",
166 remain[0][0]);
167 ret = -1;
168 goto end;
169 }
170 }
171
172 if (ctx->port < 0) {
173 ctx->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
174 }
175
176 if (strlen(remain[2]) == 0) {
177 printf_verbose("Connecting to hostname : %s, port : %d, "
178 "proto : IPv%d\n",
179 ctx->relay_hostname, ctx->port, proto);
180 ret = 0;
181 goto end;
182 }
183 ret = sscanf(remain[2], "host/%[a-zA-Z.0-9%-]/%s",
184 ctx->traced_hostname, ctx->session_name);
185 if (ret != 2) {
186 fprintf(stderr, "[error] Format : "
187 "net://<hostname>/host/<traced_hostname>/<session_name>\n");
188 ret = -1;
189 goto end;
190 }
191
192 printf_verbose("Connecting to hostname : %s, port : %d, "
193 "traced hostname : %s, session name : %s, "
194 "proto : IPv%d\n",
195 ctx->relay_hostname, ctx->port, ctx->traced_hostname,
196 ctx->session_name, proto);
197 ret = 0;
198
199 end:
200 return ret;
201 }
202
203 static
204 guint g_uint64p_hash(gconstpointer key)
205 {
206 uint64_t v = *(uint64_t *) key;
207
208 if (sizeof(gconstpointer) == sizeof(uint64_t)) {
209 return g_direct_hash((gconstpointer) (unsigned long) v);
210 } else {
211 return g_direct_hash((gconstpointer) (unsigned long) (v >> 32))
212 ^ g_direct_hash((gconstpointer) (unsigned long) v);
213 }
214 }
215
216 static
217 gboolean g_uint64p_equal(gconstpointer a, gconstpointer b)
218 {
219 uint64_t va = *(uint64_t *) a;
220 uint64_t vb = *(uint64_t *) b;
221
222 if (va != vb)
223 return FALSE;
224 return TRUE;
225 }
226
227 static void free_session_streams(struct lttng_live_session *lsession)
228 {
229 struct lttng_live_viewer_stream *lvstream, *tmp;
230
231 bt_list_for_each_entry_safe(lvstream, tmp, &lsession->stream_list,
232 session_stream_node) {
233 /*
234 * The stream should not be in trace anymore.
235 */
236 assert(!lvstream->in_trace);
237 bt_list_del(&lvstream->session_stream_node);
238 g_free(lvstream);
239 }
240 }
241
242 static int lttng_live_open_trace_read(const char *path)
243 {
244 int ret = 0;
245 struct lttng_live_ctx *ctx;
246
247 ctx = g_new0(struct lttng_live_ctx, 1);
248 ctx->session = g_new0(struct lttng_live_session, 1);
249
250 BT_INIT_LIST_HEAD(&ctx->session->stream_list);
251
252 /* We need a pointer to the context from the packet_seek function. */
253 ctx->session->ctx = ctx;
254
255 /* HT to store the CTF traces. */
256 ctx->session->ctf_traces = g_hash_table_new(g_uint64p_hash,
257 g_uint64p_equal);
258 ctx->port = -1;
259 ctx->session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t));
260
261 ret = parse_url(path, ctx);
262 if (ret < 0) {
263 goto end_free;
264 }
265 ret = setup_sighandler();
266 if (ret < 0) {
267 goto end_free;
268 }
269 ret = lttng_live_connect_viewer(ctx);
270 if (ret < 0) {
271 goto end_free;
272 }
273 printf_verbose("LTTng-live connected to relayd\n");
274
275 ret = lttng_live_establish_connection(ctx);
276 if (ret < 0) {
277 goto end_free;
278 }
279
280 printf_verbose("Listing sessions\n");
281 ret = lttng_live_list_sessions(ctx, path);
282 if (ret < 0) {
283 goto end_free;
284 }
285
286 if (ctx->session_ids->len > 0) {
287 ret = lttng_live_read(ctx);
288 }
289
290 end_free:
291 g_array_free(ctx->session_ids, TRUE);
292 g_hash_table_destroy(ctx->session->ctf_traces);
293 free_session_streams(ctx->session);
294 g_free(ctx->session);
295 g_free(ctx);
296
297 if (lttng_live_should_quit()) {
298 ret = 0;
299 }
300 return ret;
301 }
302
303 static
304 struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
305 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
306 int whence), FILE *metadata_fp)
307 {
308 struct ctf_text_stream_pos *pos;
309
310 switch (flags & O_ACCMODE) {
311 case O_RDONLY:
312 /* OK */
313 break;
314 case O_RDWR:
315 fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n");
316 goto error;
317 default:
318 fprintf(stderr, "[error] Incorrect open flags.\n");
319 goto error;
320 }
321
322 pos = g_new0(struct ctf_text_stream_pos, 1);
323 pos->parent.rw_table = NULL;
324 pos->parent.event_cb = NULL;
325 pos->parent.trace = &pos->trace_descriptor;
326 /*
327 * Since we do *everything* in this function, we are skipping
328 * the output plugin handling that is part of Babeltrace 1.x.
329 * Therefore, don't expect the --output cmd line option to work.
330 * This limits the output of lttng-live to stderr and stdout.
331 */
332 if (lttng_live_open_trace_read(path) < 0) {
333 goto error;
334 }
335 return &pos->trace_descriptor;
336
337 error:
338 return NULL;
339 }
340
341 static
342 int lttng_live_close_trace(struct bt_trace_descriptor *td)
343 {
344 struct ctf_text_stream_pos *pos =
345 container_of(td, struct ctf_text_stream_pos,
346 trace_descriptor);
347 g_free(pos);
348 return 0;
349 }
350
351 static
352 struct bt_format lttng_live_format = {
353 .open_trace = lttng_live_open_trace,
354 .close_trace = lttng_live_close_trace,
355 };
356
357 static
358 void __attribute__((constructor)) lttng_live_init(void)
359 {
360 int ret;
361
362 lttng_live_format.name = g_quark_from_static_string("lttng-live");
363 ret = bt_register_format(&lttng_live_format);
364 assert(!ret);
365 }
366
367 static
368 void __attribute__((destructor)) lttng_live_exit(void)
369 {
370 bt_unregister_format(&lttng_live_format);
371 }
This page took 0.035936 seconds and 4 git commands to generate.