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