prepare the 0.3 release
[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
0d220223 61#if 0
6c5f6d07
JD
62static
63void sighandler(int sig)
64{
65 switch (sig) {
66 case SIGTERM:
67 case SIGINT:
68 should_quit = 1;
69 break;
70 default:
71 break;
72 }
73}
74
75/*
76 * TODO: Eventually, this signal handler setup should be done at the
77 * plugin manager level, rather than within this plugin. Beware, we are
78 * not cleaning up the signal handler after plugin execution.
79 */
80static
81int setup_sighandler(void)
82{
83 struct sigaction sa;
84 sigset_t sigset;
85 int ret;
86
87 if ((ret = sigemptyset(&sigset)) < 0) {
88 perror("sigemptyset");
89 return ret;
90 }
91 sa.sa_handler = sighandler;
92 sa.sa_mask = sigset;
93 sa.sa_flags = 0;
94 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
95 perror("sigaction");
96 return ret;
97 }
98 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
99 perror("sigaction");
100 return ret;
101 }
102 return 0;
103}
0d220223
JD
104#endif
105
b1acd2b3 106/*
12a91e9d 107 * hostname parameter needs to hold NAME_MAX chars.
b1acd2b3 108 */
d81179de
JD
109static
110int parse_url(const char *path, struct lttng_live_ctx *ctx)
b1acd2b3 111{
d81179de 112 char remain[3][NAME_MAX];
12a91e9d 113 int ret = -1, proto, proto_offset = 0;
6c5f6d07 114 size_t path_len = strlen(path); /* not accounting \0 */
b1acd2b3 115
12a91e9d
JD
116 /*
117 * Since sscanf API does not allow easily checking string length
118 * against a size defined by a macro. Test it beforehand on the
119 * input. We know the output is always <= than the input length.
120 */
6c5f6d07 121 if (path_len >= NAME_MAX) {
b1acd2b3
JD
122 goto end;
123 }
12a91e9d
JD
124 ret = sscanf(path, "net%d://", &proto);
125 if (ret < 1) {
126 proto = 4;
127 /* net:// */
128 proto_offset = strlen("net://");
129 } else {
130 /* net4:// or net6:// */
131 proto_offset = strlen("netX://");
b1acd2b3 132 }
12a91e9d 133 if (proto_offset > path_len) {
b1acd2b3
JD
134 goto end;
135 }
12a91e9d
JD
136 /* TODO : parse for IPv6 as well */
137 /* Parse the hostname or IP */
138 ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s",
d81179de 139 ctx->relay_hostname, remain[0]);
12a91e9d
JD
140 if (ret == 2) {
141 /* Optional port number */
142 switch (remain[0][0]) {
143 case ':':
d81179de 144 ret = sscanf(remain[0], ":%d%s", &ctx->port, remain[1]);
12a91e9d
JD
145 /* Optional session ID with port number */
146 if (ret == 2) {
d81179de 147 ret = sscanf(remain[1], "/%s", remain[2]);
12a91e9d
JD
148 /* Accept 0 or 1 (optional) */
149 if (ret < 0) {
150 goto end;
151 }
6c5f6d07
JD
152 } else if (ret == 0) {
153 fprintf(stderr, "[error] Missing port number after delimitor ':'\n");
154 ret = -1;
155 goto end;
12a91e9d
JD
156 }
157 break;
158 case '/':
159 /* Optional session ID */
d81179de 160 ret = sscanf(remain[0], "/%s", remain[2]);
12a91e9d
JD
161 /* Accept 0 or 1 (optional) */
162 if (ret < 0) {
163 goto end;
164 }
165 break;
166 default:
167 fprintf(stderr, "[error] wrong delimitor : %c\n",
168 remain[0][0]);
169 ret = -1;
170 goto end;
171 }
172 }
b1acd2b3 173
6c5f6d07 174 if (ctx->port < 0) {
d81179de 175 ctx->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
6c5f6d07 176 }
b1acd2b3 177
d81179de 178 if (strlen(remain[2]) == 0) {
12a91e9d
JD
179 printf_verbose("Connecting to hostname : %s, port : %d, "
180 "proto : IPv%d\n",
d81179de
JD
181 ctx->relay_hostname, ctx->port, proto);
182 ret = 0;
183 goto end;
184 }
185 ret = sscanf(remain[2], "host/%[a-zA-Z.0-9%-]/%s",
186 ctx->traced_hostname, ctx->session_name);
187 if (ret != 2) {
188 fprintf(stderr, "[error] Format : "
189 "net://<hostname>/host/<traced_hostname>/<session_name>\n");
190 goto end;
191 }
192
193 printf_verbose("Connecting to hostname : %s, port : %d, "
194 "traced hostname : %s, session name : %s, "
195 "proto : IPv%d\n",
196 ctx->relay_hostname, ctx->port, ctx->traced_hostname,
197 ctx->session_name, proto);
b1acd2b3
JD
198 ret = 0;
199
200end:
201 return ret;
202}
203
12a91e9d 204static int lttng_live_open_trace_read(const char *path)
b1acd2b3 205{
12a91e9d 206 int ret = 0;
d81179de 207 struct lttng_live_ctx *ctx;
b1acd2b3 208
d81179de
JD
209 ctx = g_new0(struct lttng_live_ctx, 1);
210 ctx->session = g_new0(struct lttng_live_session, 1);
b1acd2b3 211
12a91e9d 212 /* We need a pointer to the context from the packet_seek function. */
d81179de 213 ctx->session->ctx = ctx;
b1acd2b3 214
12a91e9d 215 /* HT to store the CTF traces. */
d81179de 216 ctx->session->ctf_traces = g_hash_table_new(g_direct_hash,
12a91e9d 217 g_direct_equal);
d81179de
JD
218 ctx->port = -1;
219 ctx->session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t));
b1acd2b3 220
d81179de 221 ret = parse_url(path, ctx);
b1acd2b3 222 if (ret < 0) {
12a91e9d 223 goto end_free;
b1acd2b3 224 }
6c5f6d07
JD
225#if 0
226 ret = setup_sighandler();
227 if (ret < 0) {
228 goto end_free;
229 }
230#endif
d81179de 231 ret = lttng_live_connect_viewer(ctx);
b1acd2b3 232 if (ret < 0) {
12a91e9d 233 goto end_free;
b1acd2b3 234 }
12a91e9d 235 printf_verbose("LTTng-live connected to relayd\n");
b1acd2b3 236
d81179de 237 ret = lttng_live_establish_connection(ctx);
b1acd2b3 238 if (ret < 0) {
12a91e9d 239 goto end_free;
b1acd2b3 240 }
6c5f6d07 241
d81179de
JD
242 printf_verbose("Listing sessions\n");
243 ret = lttng_live_list_sessions(ctx, path);
244 if (ret < 0) {
d81179de 245 goto end_free;
b1acd2b3
JD
246 }
247
6c5f6d07
JD
248 if (ctx->session_ids->len > 0) {
249 ret = lttng_live_read(ctx);
250 }
d81179de 251
12a91e9d 252end_free:
d81179de
JD
253 g_hash_table_destroy(ctx->session->ctf_traces);
254 g_free(ctx->session);
255 g_free(ctx->session->streams);
256 g_free(ctx);
6c5f6d07
JD
257
258 if (lttng_live_should_quit()) {
259 ret = 0;
260 }
b1acd2b3
JD
261 return ret;
262}
263
b1acd2b3 264static
12a91e9d
JD
265struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
266 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
267 int whence), FILE *metadata_fp)
b1acd2b3 268{
12a91e9d 269 struct ctf_text_stream_pos *pos;
b1acd2b3 270
12a91e9d
JD
271 switch (flags & O_ACCMODE) {
272 case O_RDONLY:
273 /* OK */
b1acd2b3 274 break;
12a91e9d
JD
275 case O_RDWR:
276 fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n");
277 goto error;
b1acd2b3 278 default:
12a91e9d 279 fprintf(stderr, "[error] Incorrect open flags.\n");
b1acd2b3
JD
280 goto error;
281 }
282
12a91e9d
JD
283 pos = g_new0(struct ctf_text_stream_pos, 1);
284 pos->parent.rw_table = NULL;
285 pos->parent.event_cb = NULL;
286 pos->parent.trace = &pos->trace_descriptor;
6c5f6d07
JD
287 if (lttng_live_open_trace_read(path) < 0) {
288 goto error;
289 }
12a91e9d 290 return &pos->trace_descriptor;
b1acd2b3 291
b1acd2b3 292error:
12a91e9d 293 return NULL;
b1acd2b3
JD
294}
295
b1acd2b3 296static
12a91e9d 297int lttng_live_close_trace(struct bt_trace_descriptor *td)
b1acd2b3 298{
12a91e9d
JD
299 struct ctf_text_stream_pos *pos =
300 container_of(td, struct ctf_text_stream_pos,
301 trace_descriptor);
302 free(pos);
303 return 0;
b1acd2b3
JD
304}
305
12a91e9d
JD
306static
307struct bt_format lttng_live_format = {
308 .open_trace = lttng_live_open_trace,
309 .close_trace = lttng_live_close_trace,
310};
b1acd2b3 311
12a91e9d
JD
312static
313void __attribute__((constructor)) lttng_live_init(void)
b1acd2b3 314{
b1acd2b3
JD
315 int ret;
316
12a91e9d
JD
317 lttng_live_format.name = g_quark_from_static_string("lttng-live");
318 ret = bt_register_format(&lttng_live_format);
319 assert(!ret);
b1acd2b3
JD
320}
321
12a91e9d
JD
322static
323void __attribute__((destructor)) lttng_live_exit(void)
b1acd2b3 324{
12a91e9d 325 bt_unregister_format(&lttng_live_format);
b1acd2b3 326}
This page took 0.056607 seconds and 4 git commands to generate.