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