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