Fix: Use list rather than ptr array for trace streams
[babeltrace.git] / formats / lttng-live / lttng-live-plugin.c
CommitLineData
4a744367
JD
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>
8ace20bf 39#include <signal.h>
c98627ca 40#include "lttng-live.h"
4a744367 41
8ace20bf
MD
42static volatile int should_quit;
43
c13e6f6d
JG
44void 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
8ace20bf
MD
52int lttng_live_should_quit(void)
53{
54 return should_quit;
55}
56
57static
58void 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 */
75static
76int 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
e84d6f79 100/*
0206bb4a 101 * hostname parameter needs to hold MAXNAMLEN chars.
e84d6f79 102 */
b5a1fa45
JD
103static
104int parse_url(const char *path, struct lttng_live_ctx *ctx)
4a744367 105{
0206bb4a 106 char remain[3][MAXNAMLEN];
e84d6f79 107 int ret = -1, proto, proto_offset = 0;
5fe17d06 108 size_t path_len = strlen(path); /* not accounting \0 */
4a744367 109
e84d6f79
MD
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 */
0206bb4a 115 if (path_len >= MAXNAMLEN) {
e84d6f79
MD
116 goto end;
117 }
118 ret = sscanf(path, "net%d://", &proto);
119 if (ret < 1) {
4a744367
JD
120 proto = 4;
121 /* net:// */
122 proto_offset = strlen("net://");
123 } else {
124 /* net4:// or net6:// */
125 proto_offset = strlen("netX://");
126 }
e84d6f79
MD
127 if (proto_offset > path_len) {
128 goto end;
129 }
fdfb7392
JG
130 if (proto == 6) {
131 fprintf(stderr, "[error] IPv6 is currently unsupported by lttng-live\n");
132 goto end;
133 }
4a744367
JD
134 /* TODO : parse for IPv6 as well */
135 /* Parse the hostname or IP */
e84d6f79 136 ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s",
b5a1fa45 137 ctx->relay_hostname, remain[0]);
4a744367
JD
138 if (ret == 2) {
139 /* Optional port number */
e84d6f79
MD
140 switch (remain[0][0]) {
141 case ':':
b5a1fa45 142 ret = sscanf(remain[0], ":%d%s", &ctx->port, remain[1]);
4a744367
JD
143 /* Optional session ID with port number */
144 if (ret == 2) {
2acdc547 145 ret = sscanf(remain[1], "/%s", remain[2]);
e84d6f79
MD
146 /* Accept 0 or 1 (optional) */
147 if (ret < 0) {
148 goto end;
149 }
9010d19c 150 } else if (ret == 0) {
2a6d197b
MD
151 fprintf(stderr, "[error] Missing port number after delimitor ':'\n");
152 ret = -1;
153 goto end;
4a744367 154 }
e84d6f79
MD
155 break;
156 case '/':
4a744367 157 /* Optional session ID */
2acdc547 158 ret = sscanf(remain[0], "/%s", remain[2]);
e84d6f79
MD
159 /* Accept 0 or 1 (optional) */
160 if (ret < 0) {
161 goto end;
162 }
163 break;
164 default:
4a744367 165 fprintf(stderr, "[error] wrong delimitor : %c\n",
e84d6f79 166 remain[0][0]);
4a744367
JD
167 ret = -1;
168 goto end;
169 }
170 }
171
49a4acef 172 if (ctx->port < 0) {
b5a1fa45 173 ctx->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
49a4acef 174 }
4a744367 175
2acdc547 176 if (strlen(remain[2]) == 0) {
4a744367
JD
177 printf_verbose("Connecting to hostname : %s, port : %d, "
178 "proto : IPv%d\n",
b5a1fa45 179 ctx->relay_hostname, ctx->port, proto);
2acdc547
JD
180 ret = 0;
181 goto end;
182 }
b5a1fa45
JD
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 goto end;
189 }
2acdc547
JD
190
191 printf_verbose("Connecting to hostname : %s, port : %d, "
b5a1fa45
JD
192 "traced hostname : %s, session name : %s, "
193 "proto : IPv%d\n",
194 ctx->relay_hostname, ctx->port, ctx->traced_hostname,
195 ctx->session_name, proto);
4a744367
JD
196 ret = 0;
197
198end:
199 return ret;
200}
201
37307240
MD
202static
203guint g_uint64p_hash(gconstpointer key)
204{
205 uint64_t v = *(uint64_t *) key;
206
207 if (sizeof(gconstpointer) == sizeof(uint64_t)) {
208 return g_direct_hash((gconstpointer) (unsigned long) v);
209 } else {
210 return g_direct_hash((gconstpointer) (unsigned long) (v >> 32))
211 ^ g_direct_hash((gconstpointer) (unsigned long) v);
212 }
213}
214
215static
216gboolean g_uint64p_equal(gconstpointer a, gconstpointer b)
217{
218 uint64_t va = *(uint64_t *) a;
219 uint64_t vb = *(uint64_t *) b;
220
221 if (va != vb)
222 return FALSE;
223 return TRUE;
224}
225
b5e7994d
MD
226static void free_session_streams(struct lttng_live_session *lsession)
227{
228 struct lttng_live_viewer_stream *lvstream, *tmp;
229
230 bt_list_for_each_entry_safe(lvstream, tmp, &lsession->stream_list,
e88db11d
MD
231 session_stream_node) {
232 /*
233 * The stream should not be in trace anymore.
234 */
235 assert(!lvstream->in_trace);
236 bt_list_del(&lvstream->session_stream_node);
b5e7994d
MD
237 g_free(lvstream);
238 }
239}
240
4a744367
JD
241static int lttng_live_open_trace_read(const char *path)
242{
4a744367 243 int ret = 0;
b5a1fa45 244 struct lttng_live_ctx *ctx;
4a744367 245
b5a1fa45
JD
246 ctx = g_new0(struct lttng_live_ctx, 1);
247 ctx->session = g_new0(struct lttng_live_session, 1);
3af4fc48 248
b5e7994d
MD
249 BT_INIT_LIST_HEAD(&ctx->session->stream_list);
250
4a744367 251 /* We need a pointer to the context from the packet_seek function. */
b5a1fa45 252 ctx->session->ctx = ctx;
4a744367
JD
253
254 /* HT to store the CTF traces. */
37307240
MD
255 ctx->session->ctf_traces = g_hash_table_new(g_uint64p_hash,
256 g_uint64p_equal);
b5a1fa45
JD
257 ctx->port = -1;
258 ctx->session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t));
4a744367 259
b5a1fa45 260 ret = parse_url(path, ctx);
4a744367
JD
261 if (ret < 0) {
262 goto end_free;
263 }
8ace20bf
MD
264 ret = setup_sighandler();
265 if (ret < 0) {
266 goto end_free;
267 }
b5a1fa45 268 ret = lttng_live_connect_viewer(ctx);
4a744367 269 if (ret < 0) {
4a744367
JD
270 goto end_free;
271 }
272 printf_verbose("LTTng-live connected to relayd\n");
273
b5a1fa45 274 ret = lttng_live_establish_connection(ctx);
4a744367
JD
275 if (ret < 0) {
276 goto end_free;
277 }
278
b5a1fa45
JD
279 printf_verbose("Listing sessions\n");
280 ret = lttng_live_list_sessions(ctx, path);
281 if (ret < 0) {
b5a1fa45 282 goto end_free;
4a744367
JD
283 }
284
49a4acef 285 if (ctx->session_ids->len > 0) {
28756729 286 ret = lttng_live_read(ctx);
49a4acef 287 }
b5a1fa45 288
4a744367 289end_free:
b5a1fa45 290 g_hash_table_destroy(ctx->session->ctf_traces);
b5e7994d 291 free_session_streams(ctx->session);
b5a1fa45 292 g_free(ctx->session);
b5a1fa45 293 g_free(ctx);
8ace20bf
MD
294
295 if (lttng_live_should_quit()) {
296 ret = 0;
297 }
4a744367
JD
298 return ret;
299}
300
301static
302struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
303 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
304 int whence), FILE *metadata_fp)
305{
306 struct ctf_text_stream_pos *pos;
307
308 switch (flags & O_ACCMODE) {
309 case O_RDONLY:
310 /* OK */
311 break;
312 case O_RDWR:
313 fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n");
314 goto error;
315 default:
316 fprintf(stderr, "[error] Incorrect open flags.\n");
317 goto error;
318 }
319
320 pos = g_new0(struct ctf_text_stream_pos, 1);
321 pos->parent.rw_table = NULL;
322 pos->parent.event_cb = NULL;
323 pos->parent.trace = &pos->trace_descriptor;
857eaa68
MD
324 /*
325 * Since we do *everything* in this function, we are skipping
326 * the output plugin handling that is part of Babeltrace 1.x.
327 * Therefore, don't expect the --output cmd line option to work.
328 * This limits the output of lttng-live to stderr and stdout.
329 */
28756729
JG
330 if (lttng_live_open_trace_read(path) < 0) {
331 goto error;
332 }
4a744367
JD
333 return &pos->trace_descriptor;
334
335error:
336 return NULL;
337}
338
339static
340int lttng_live_close_trace(struct bt_trace_descriptor *td)
341{
342 struct ctf_text_stream_pos *pos =
343 container_of(td, struct ctf_text_stream_pos,
344 trace_descriptor);
9b6ae25b 345 g_free(pos);
4a744367
JD
346 return 0;
347}
348
349static
350struct bt_format lttng_live_format = {
351 .open_trace = lttng_live_open_trace,
352 .close_trace = lttng_live_close_trace,
353};
354
355static
356void __attribute__((constructor)) lttng_live_init(void)
357{
358 int ret;
359
360 lttng_live_format.name = g_quark_from_static_string("lttng-live");
361 ret = bt_register_format(&lttng_live_format);
362 assert(!ret);
363}
364
365static
366void __attribute__((destructor)) lttng_live_exit(void)
367{
368 bt_unregister_format(&lttng_live_format);
369}
This page took 0.042066 seconds and 4 git commands to generate.