Fix: lttng-live parse url port check
[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
MD
92/*
93 * hostname parameter needs to hold NAME_MAX chars.
94 */
b5a1fa45
JD
95static
96int parse_url(const char *path, struct lttng_live_ctx *ctx)
4a744367 97{
2acdc547 98 char remain[3][NAME_MAX];
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 */
5fe17d06 107 if (path_len >= NAME_MAX) {
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 }
4a744367
JD
122 /* TODO : parse for IPv6 as well */
123 /* Parse the hostname or IP */
e84d6f79 124 ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s",
b5a1fa45 125 ctx->relay_hostname, remain[0]);
4a744367
JD
126 if (ret == 2) {
127 /* Optional port number */
e84d6f79
MD
128 switch (remain[0][0]) {
129 case ':':
b5a1fa45 130 ret = sscanf(remain[0], ":%d%s", &ctx->port, remain[1]);
4a744367
JD
131 /* Optional session ID with port number */
132 if (ret == 2) {
2acdc547 133 ret = sscanf(remain[1], "/%s", remain[2]);
e84d6f79
MD
134 /* Accept 0 or 1 (optional) */
135 if (ret < 0) {
136 goto end;
137 }
2a6d197b
MD
138 } else {
139 fprintf(stderr, "[error] Missing port number after delimitor ':'\n");
140 ret = -1;
141 goto end;
4a744367 142 }
e84d6f79
MD
143 break;
144 case '/':
4a744367 145 /* Optional session ID */
2acdc547 146 ret = sscanf(remain[0], "/%s", remain[2]);
e84d6f79
MD
147 /* Accept 0 or 1 (optional) */
148 if (ret < 0) {
149 goto end;
150 }
151 break;
152 default:
4a744367 153 fprintf(stderr, "[error] wrong delimitor : %c\n",
e84d6f79 154 remain[0][0]);
4a744367
JD
155 ret = -1;
156 goto end;
157 }
158 }
159
49a4acef 160 if (ctx->port < 0) {
b5a1fa45 161 ctx->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
49a4acef 162 }
4a744367 163
2acdc547 164 if (strlen(remain[2]) == 0) {
4a744367
JD
165 printf_verbose("Connecting to hostname : %s, port : %d, "
166 "proto : IPv%d\n",
b5a1fa45 167 ctx->relay_hostname, ctx->port, proto);
2acdc547
JD
168 ret = 0;
169 goto end;
170 }
b5a1fa45
JD
171 ret = sscanf(remain[2], "host/%[a-zA-Z.0-9%-]/%s",
172 ctx->traced_hostname, ctx->session_name);
173 if (ret != 2) {
174 fprintf(stderr, "[error] Format : "
175 "net://<hostname>/host/<traced_hostname>/<session_name>\n");
176 goto end;
177 }
2acdc547
JD
178
179 printf_verbose("Connecting to hostname : %s, port : %d, "
b5a1fa45
JD
180 "traced hostname : %s, session name : %s, "
181 "proto : IPv%d\n",
182 ctx->relay_hostname, ctx->port, ctx->traced_hostname,
183 ctx->session_name, proto);
4a744367
JD
184 ret = 0;
185
186end:
187 return ret;
188}
189
190static int lttng_live_open_trace_read(const char *path)
191{
4a744367 192 int ret = 0;
b5a1fa45 193 struct lttng_live_ctx *ctx;
4a744367 194
b5a1fa45
JD
195 ctx = g_new0(struct lttng_live_ctx, 1);
196 ctx->session = g_new0(struct lttng_live_session, 1);
3af4fc48 197
4a744367 198 /* We need a pointer to the context from the packet_seek function. */
b5a1fa45 199 ctx->session->ctx = ctx;
4a744367
JD
200
201 /* HT to store the CTF traces. */
b5a1fa45 202 ctx->session->ctf_traces = g_hash_table_new(g_direct_hash,
4a744367 203 g_direct_equal);
b5a1fa45
JD
204 ctx->port = -1;
205 ctx->session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t));
4a744367 206
b5a1fa45 207 ret = parse_url(path, ctx);
4a744367
JD
208 if (ret < 0) {
209 goto end_free;
210 }
8ace20bf
MD
211 ret = setup_sighandler();
212 if (ret < 0) {
213 goto end_free;
214 }
b5a1fa45 215 ret = lttng_live_connect_viewer(ctx);
4a744367 216 if (ret < 0) {
4a744367
JD
217 goto end_free;
218 }
219 printf_verbose("LTTng-live connected to relayd\n");
220
b5a1fa45 221 ret = lttng_live_establish_connection(ctx);
4a744367
JD
222 if (ret < 0) {
223 goto end_free;
224 }
225
b5a1fa45
JD
226 printf_verbose("Listing sessions\n");
227 ret = lttng_live_list_sessions(ctx, path);
228 if (ret < 0) {
b5a1fa45 229 goto end_free;
4a744367
JD
230 }
231
49a4acef 232 if (ctx->session_ids->len > 0) {
b5a1fa45 233 lttng_live_read(ctx);
49a4acef 234 }
b5a1fa45 235
4a744367 236end_free:
b5a1fa45
JD
237 g_hash_table_destroy(ctx->session->ctf_traces);
238 g_free(ctx->session);
239 g_free(ctx->session->streams);
240 g_free(ctx);
8ace20bf
MD
241
242 if (lttng_live_should_quit()) {
243 ret = 0;
244 }
4a744367
JD
245 return ret;
246}
247
248static
249struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
250 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
251 int whence), FILE *metadata_fp)
252{
253 struct ctf_text_stream_pos *pos;
254
255 switch (flags & O_ACCMODE) {
256 case O_RDONLY:
257 /* OK */
258 break;
259 case O_RDWR:
260 fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n");
261 goto error;
262 default:
263 fprintf(stderr, "[error] Incorrect open flags.\n");
264 goto error;
265 }
266
267 pos = g_new0(struct ctf_text_stream_pos, 1);
268 pos->parent.rw_table = NULL;
269 pos->parent.event_cb = NULL;
270 pos->parent.trace = &pos->trace_descriptor;
271 lttng_live_open_trace_read(path);
272 return &pos->trace_descriptor;
273
274error:
275 return NULL;
276}
277
278static
279int lttng_live_close_trace(struct bt_trace_descriptor *td)
280{
281 struct ctf_text_stream_pos *pos =
282 container_of(td, struct ctf_text_stream_pos,
283 trace_descriptor);
284 free(pos);
285 return 0;
286}
287
288static
289struct bt_format lttng_live_format = {
290 .open_trace = lttng_live_open_trace,
291 .close_trace = lttng_live_close_trace,
292};
293
294static
295void __attribute__((constructor)) lttng_live_init(void)
296{
297 int ret;
298
299 lttng_live_format.name = g_quark_from_static_string("lttng-live");
300 ret = bt_register_format(&lttng_live_format);
301 assert(!ret);
302}
303
304static
305void __attribute__((destructor)) lttng_live_exit(void)
306{
307 bt_unregister_format(&lttng_live_format);
308}
This page took 0.036036 seconds and 4 git commands to generate.