| 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> |
| 39 | #include "lttng-live-functions.h" |
| 40 | |
| 41 | /* |
| 42 | * hostname parameter needs to hold NAME_MAX chars. |
| 43 | */ |
| 44 | static int parse_url(const char *path, char *hostname, int *port, |
| 45 | uint64_t *session_id, GArray *session_ids) |
| 46 | { |
| 47 | char remain[3][NAME_MAX]; |
| 48 | int ret = -1, proto, proto_offset = 0; |
| 49 | size_t path_len = strlen(path); |
| 50 | char *str, *strctx; |
| 51 | |
| 52 | /* |
| 53 | * Since sscanf API does not allow easily checking string length |
| 54 | * against a size defined by a macro. Test it beforehand on the |
| 55 | * input. We know the output is always <= than the input length. |
| 56 | */ |
| 57 | if (path_len > NAME_MAX) { |
| 58 | goto end; |
| 59 | } |
| 60 | ret = sscanf(path, "net%d://", &proto); |
| 61 | if (ret < 1) { |
| 62 | proto = 4; |
| 63 | /* net:// */ |
| 64 | proto_offset = strlen("net://"); |
| 65 | } else { |
| 66 | /* net4:// or net6:// */ |
| 67 | proto_offset = strlen("netX://"); |
| 68 | } |
| 69 | if (proto_offset > path_len) { |
| 70 | goto end; |
| 71 | } |
| 72 | /* TODO : parse for IPv6 as well */ |
| 73 | /* Parse the hostname or IP */ |
| 74 | ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s", |
| 75 | hostname, remain[0]); |
| 76 | if (ret == 2) { |
| 77 | /* Optional port number */ |
| 78 | switch (remain[0][0]) { |
| 79 | case ':': |
| 80 | ret = sscanf(remain[0], ":%d%s", port, remain[1]); |
| 81 | /* Optional session ID with port number */ |
| 82 | if (ret == 2) { |
| 83 | ret = sscanf(remain[1], "/%s", remain[2]); |
| 84 | /* Accept 0 or 1 (optional) */ |
| 85 | if (ret < 0) { |
| 86 | goto end; |
| 87 | } |
| 88 | } |
| 89 | break; |
| 90 | case '/': |
| 91 | /* Optional session ID */ |
| 92 | ret = sscanf(remain[0], "/%s", remain[2]); |
| 93 | /* Accept 0 or 1 (optional) */ |
| 94 | if (ret < 0) { |
| 95 | goto end; |
| 96 | } |
| 97 | break; |
| 98 | default: |
| 99 | fprintf(stderr, "[error] wrong delimitor : %c\n", |
| 100 | remain[0][0]); |
| 101 | ret = -1; |
| 102 | goto end; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (*port < 0) |
| 107 | *port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT; |
| 108 | |
| 109 | if (strlen(remain[2]) == 0) { |
| 110 | printf_verbose("Connecting to hostname : %s, port : %d, " |
| 111 | "proto : IPv%d\n", |
| 112 | hostname, *port, proto); |
| 113 | ret = 0; |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | printf_verbose("Connecting to hostname : %s, port : %d, " |
| 118 | "session id(s) : %s, proto : IPv%d\n", |
| 119 | hostname, *port, remain[2], proto); |
| 120 | str = strtok_r(remain[2], ",", &strctx); |
| 121 | do { |
| 122 | char *endptr; |
| 123 | uint64_t id; |
| 124 | |
| 125 | id = strtoull(str, &endptr, 0); |
| 126 | if (*endptr != '\0' || str == endptr || errno != 0) { |
| 127 | fprintf(stderr, "[error] parsing session id\n"); |
| 128 | ret = -1; |
| 129 | goto end; |
| 130 | } |
| 131 | g_array_append_val(session_ids, id); |
| 132 | } while ((str = strtok_r(NULL, ",", &strctx))); |
| 133 | |
| 134 | ret = 0; |
| 135 | |
| 136 | end: |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | static int lttng_live_open_trace_read(const char *path) |
| 141 | { |
| 142 | char hostname[NAME_MAX]; |
| 143 | int port = -1; |
| 144 | uint64_t session_id = -1ULL; |
| 145 | int ret = 0; |
| 146 | struct lttng_live_ctx ctx; |
| 147 | |
| 148 | ctx.session = g_new0(struct lttng_live_session, 1); |
| 149 | |
| 150 | /* We need a pointer to the context from the packet_seek function. */ |
| 151 | ctx.session->ctx = &ctx; |
| 152 | |
| 153 | /* HT to store the CTF traces. */ |
| 154 | ctx.session->ctf_traces = g_hash_table_new(g_direct_hash, |
| 155 | g_direct_equal); |
| 156 | |
| 157 | ctx.session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t)); |
| 158 | |
| 159 | ret = parse_url(path, hostname, &port, &session_id, ctx.session_ids); |
| 160 | if (ret < 0) { |
| 161 | goto end_free; |
| 162 | } |
| 163 | |
| 164 | ret = lttng_live_connect_viewer(&ctx, hostname, port); |
| 165 | if (ret < 0) { |
| 166 | fprintf(stderr, "[error] Connection failed\n"); |
| 167 | goto end_free; |
| 168 | } |
| 169 | printf_verbose("LTTng-live connected to relayd\n"); |
| 170 | |
| 171 | ret = lttng_live_establish_connection(&ctx); |
| 172 | if (ret < 0) { |
| 173 | goto end_free; |
| 174 | } |
| 175 | |
| 176 | if (ctx.session_ids->len == 0) { |
| 177 | printf_verbose("Listing sessions\n"); |
| 178 | ret = lttng_live_list_sessions(&ctx, path); |
| 179 | if (ret < 0) { |
| 180 | fprintf(stderr, "[error] List error\n"); |
| 181 | goto end_free; |
| 182 | } |
| 183 | } else { |
| 184 | lttng_live_read(&ctx); |
| 185 | } |
| 186 | |
| 187 | end_free: |
| 188 | g_array_free(ctx.session_ids, TRUE); |
| 189 | g_hash_table_destroy(ctx.session->ctf_traces); |
| 190 | g_free(ctx.session); |
| 191 | g_free(ctx.session->streams); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static |
| 196 | struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags, |
| 197 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
| 198 | int whence), FILE *metadata_fp) |
| 199 | { |
| 200 | struct ctf_text_stream_pos *pos; |
| 201 | |
| 202 | switch (flags & O_ACCMODE) { |
| 203 | case O_RDONLY: |
| 204 | /* OK */ |
| 205 | break; |
| 206 | case O_RDWR: |
| 207 | fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n"); |
| 208 | goto error; |
| 209 | default: |
| 210 | fprintf(stderr, "[error] Incorrect open flags.\n"); |
| 211 | goto error; |
| 212 | } |
| 213 | |
| 214 | pos = g_new0(struct ctf_text_stream_pos, 1); |
| 215 | pos->parent.rw_table = NULL; |
| 216 | pos->parent.event_cb = NULL; |
| 217 | pos->parent.trace = &pos->trace_descriptor; |
| 218 | lttng_live_open_trace_read(path); |
| 219 | return &pos->trace_descriptor; |
| 220 | |
| 221 | error: |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | static |
| 226 | int lttng_live_close_trace(struct bt_trace_descriptor *td) |
| 227 | { |
| 228 | struct ctf_text_stream_pos *pos = |
| 229 | container_of(td, struct ctf_text_stream_pos, |
| 230 | trace_descriptor); |
| 231 | free(pos); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static |
| 236 | struct bt_format lttng_live_format = { |
| 237 | .open_trace = lttng_live_open_trace, |
| 238 | .close_trace = lttng_live_close_trace, |
| 239 | }; |
| 240 | |
| 241 | static |
| 242 | void __attribute__((constructor)) lttng_live_init(void) |
| 243 | { |
| 244 | int ret; |
| 245 | |
| 246 | lttng_live_format.name = g_quark_from_static_string("lttng-live"); |
| 247 | ret = bt_register_format(<tng_live_format); |
| 248 | assert(!ret); |
| 249 | } |
| 250 | |
| 251 | static |
| 252 | void __attribute__((destructor)) lttng_live_exit(void) |
| 253 | { |
| 254 | bt_unregister_format(<tng_live_format); |
| 255 | } |