| 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 | |
| 37 | #include "lttng-viewer-abi.h" |
| 38 | #include "network-live.h" |
| 39 | #include "lttng-live-comm.h" |
| 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> |
| 52 | #include <lib/babeltrace/ctf/ctf-index.h> |
| 53 | |
| 54 | static volatile int should_quit; |
| 55 | |
| 56 | int lttng_live_should_quit(void) |
| 57 | { |
| 58 | return should_quit; |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | void 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 | */ |
| 79 | static |
| 80 | int 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 | } |
| 103 | /* |
| 104 | * hostname parameter needs to hold NAME_MAX chars. |
| 105 | */ |
| 106 | static |
| 107 | int parse_url(const char *path, struct lttng_live_ctx *ctx) |
| 108 | { |
| 109 | char remain[3][NAME_MAX]; |
| 110 | int ret = -1, proto, proto_offset = 0; |
| 111 | size_t path_len = strlen(path); /* not accounting \0 */ |
| 112 | |
| 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 | */ |
| 118 | if (path_len >= NAME_MAX) { |
| 119 | goto end; |
| 120 | } |
| 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://"); |
| 129 | } |
| 130 | if (proto_offset > path_len) { |
| 131 | goto end; |
| 132 | } |
| 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", |
| 136 | ctx->relay_hostname, remain[0]); |
| 137 | if (ret == 2) { |
| 138 | /* Optional port number */ |
| 139 | switch (remain[0][0]) { |
| 140 | case ':': |
| 141 | ret = sscanf(remain[0], ":%d%s", &ctx->port, remain[1]); |
| 142 | /* Optional session ID with port number */ |
| 143 | if (ret == 2) { |
| 144 | ret = sscanf(remain[1], "/%s", remain[2]); |
| 145 | /* Accept 0 or 1 (optional) */ |
| 146 | if (ret < 0) { |
| 147 | goto end; |
| 148 | } |
| 149 | } else if (ret == 0) { |
| 150 | fprintf(stderr, "[error] Missing port number after delimitor ':'\n"); |
| 151 | ret = -1; |
| 152 | goto end; |
| 153 | } |
| 154 | break; |
| 155 | case '/': |
| 156 | /* Optional session ID */ |
| 157 | ret = sscanf(remain[0], "/%s", remain[2]); |
| 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 | } |
| 170 | |
| 171 | if (ctx->port < 0) { |
| 172 | ctx->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT; |
| 173 | } |
| 174 | |
| 175 | if (strlen(remain[2]) == 0) { |
| 176 | printf_verbose("Connecting to hostname : %s, port : %d, " |
| 177 | "proto : IPv%d\n", |
| 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); |
| 195 | ret = 0; |
| 196 | |
| 197 | end: |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static int lttng_live_open_trace_read(const char *path) |
| 202 | { |
| 203 | int ret = 0; |
| 204 | struct lttng_live_ctx *ctx; |
| 205 | |
| 206 | ctx = g_new0(struct lttng_live_ctx, 1); |
| 207 | ctx->session = g_new0(struct lttng_live_session, 1); |
| 208 | |
| 209 | /* We need a pointer to the context from the packet_seek function. */ |
| 210 | ctx->session->ctx = ctx; |
| 211 | |
| 212 | /* HT to store the CTF traces. */ |
| 213 | ctx->session->ctf_traces = g_hash_table_new(g_direct_hash, |
| 214 | g_direct_equal); |
| 215 | ctx->port = -1; |
| 216 | ctx->session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t)); |
| 217 | |
| 218 | ret = parse_url(path, ctx); |
| 219 | if (ret < 0) { |
| 220 | goto end_free; |
| 221 | } |
| 222 | #if 0 |
| 223 | ret = setup_sighandler(); |
| 224 | if (ret < 0) { |
| 225 | goto end_free; |
| 226 | } |
| 227 | #endif |
| 228 | ret = lttng_live_connect_viewer(ctx); |
| 229 | if (ret < 0) { |
| 230 | goto end_free; |
| 231 | } |
| 232 | printf_verbose("LTTng-live connected to relayd\n"); |
| 233 | |
| 234 | ret = lttng_live_establish_connection(ctx); |
| 235 | if (ret < 0) { |
| 236 | goto end_free; |
| 237 | } |
| 238 | |
| 239 | printf_verbose("Listing sessions\n"); |
| 240 | ret = lttng_live_list_sessions(ctx, path); |
| 241 | if (ret < 0) { |
| 242 | goto end_free; |
| 243 | } |
| 244 | |
| 245 | if (ctx->session_ids->len > 0) { |
| 246 | ret = lttng_live_read(ctx); |
| 247 | } |
| 248 | |
| 249 | end_free: |
| 250 | g_hash_table_destroy(ctx->session->ctf_traces); |
| 251 | g_free(ctx->session); |
| 252 | g_free(ctx->session->streams); |
| 253 | g_free(ctx); |
| 254 | |
| 255 | if (lttng_live_should_quit()) { |
| 256 | ret = 0; |
| 257 | } |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | static |
| 262 | struct 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) |
| 265 | { |
| 266 | struct ctf_text_stream_pos *pos; |
| 267 | |
| 268 | switch (flags & O_ACCMODE) { |
| 269 | case O_RDONLY: |
| 270 | /* OK */ |
| 271 | break; |
| 272 | case O_RDWR: |
| 273 | fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n"); |
| 274 | goto error; |
| 275 | default: |
| 276 | fprintf(stderr, "[error] Incorrect open flags.\n"); |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 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; |
| 284 | if (lttng_live_open_trace_read(path) < 0) { |
| 285 | goto error; |
| 286 | } |
| 287 | return &pos->trace_descriptor; |
| 288 | |
| 289 | error: |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | static |
| 294 | int lttng_live_close_trace(struct bt_trace_descriptor *td) |
| 295 | { |
| 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; |
| 301 | } |
| 302 | |
| 303 | static |
| 304 | struct bt_format lttng_live_format = { |
| 305 | .open_trace = lttng_live_open_trace, |
| 306 | .close_trace = lttng_live_close_trace, |
| 307 | }; |
| 308 | |
| 309 | static |
| 310 | void __attribute__((constructor)) lttng_live_init(void) |
| 311 | { |
| 312 | int ret; |
| 313 | |
| 314 | lttng_live_format.name = g_quark_from_static_string("lttng-live"); |
| 315 | ret = bt_register_format(<tng_live_format); |
| 316 | assert(!ret); |
| 317 | } |
| 318 | |
| 319 | static |
| 320 | void __attribute__((destructor)) lttng_live_exit(void) |
| 321 | { |
| 322 | bt_unregister_format(<tng_live_format); |
| 323 | } |