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