Fix: lttng-live use g_free rather than free
[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 MAXNAMLEN chars.
94 */
95 static
96 int parse_url(const char *path, struct lttng_live_ctx *ctx)
97 {
98 char remain[3][MAXNAMLEN];
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 >= MAXNAMLEN) {
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 } else if (ret == 0) {
139 fprintf(stderr, "[error] Missing port number after delimitor ':'\n");
140 ret = -1;
141 goto end;
142 }
143 break;
144 case '/':
145 /* Optional session ID */
146 ret = sscanf(remain[0], "/%s", remain[2]);
147 /* Accept 0 or 1 (optional) */
148 if (ret < 0) {
149 goto end;
150 }
151 break;
152 default:
153 fprintf(stderr, "[error] wrong delimitor : %c\n",
154 remain[0][0]);
155 ret = -1;
156 goto end;
157 }
158 }
159
160 if (ctx->port < 0) {
161 ctx->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
162 }
163
164 if (strlen(remain[2]) == 0) {
165 printf_verbose("Connecting to hostname : %s, port : %d, "
166 "proto : IPv%d\n",
167 ctx->relay_hostname, ctx->port, proto);
168 ret = 0;
169 goto end;
170 }
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 }
178
179 printf_verbose("Connecting to hostname : %s, port : %d, "
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);
184 ret = 0;
185
186 end:
187 return ret;
188 }
189
190 static
191 guint g_uint64p_hash(gconstpointer key)
192 {
193 uint64_t v = *(uint64_t *) key;
194
195 if (sizeof(gconstpointer) == sizeof(uint64_t)) {
196 return g_direct_hash((gconstpointer) (unsigned long) v);
197 } else {
198 return g_direct_hash((gconstpointer) (unsigned long) (v >> 32))
199 ^ g_direct_hash((gconstpointer) (unsigned long) v);
200 }
201 }
202
203 static
204 gboolean g_uint64p_equal(gconstpointer a, gconstpointer b)
205 {
206 uint64_t va = *(uint64_t *) a;
207 uint64_t vb = *(uint64_t *) b;
208
209 if (va != vb)
210 return FALSE;
211 return TRUE;
212 }
213
214 static int lttng_live_open_trace_read(const char *path)
215 {
216 int ret = 0;
217 struct lttng_live_ctx *ctx;
218
219 ctx = g_new0(struct lttng_live_ctx, 1);
220 ctx->session = g_new0(struct lttng_live_session, 1);
221
222 /* We need a pointer to the context from the packet_seek function. */
223 ctx->session->ctx = ctx;
224
225 /* HT to store the CTF traces. */
226 ctx->session->ctf_traces = g_hash_table_new(g_uint64p_hash,
227 g_uint64p_equal);
228 ctx->port = -1;
229 ctx->session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t));
230
231 ret = parse_url(path, ctx);
232 if (ret < 0) {
233 goto end_free;
234 }
235 ret = setup_sighandler();
236 if (ret < 0) {
237 goto end_free;
238 }
239 ret = lttng_live_connect_viewer(ctx);
240 if (ret < 0) {
241 goto end_free;
242 }
243 printf_verbose("LTTng-live connected to relayd\n");
244
245 ret = lttng_live_establish_connection(ctx);
246 if (ret < 0) {
247 goto end_free;
248 }
249
250 printf_verbose("Listing sessions\n");
251 ret = lttng_live_list_sessions(ctx, path);
252 if (ret < 0) {
253 goto end_free;
254 }
255
256 if (ctx->session_ids->len > 0) {
257 ret = lttng_live_read(ctx);
258 }
259
260 end_free:
261 g_hash_table_destroy(ctx->session->ctf_traces);
262 g_free(ctx->session);
263 g_free(ctx->session->streams);
264 g_free(ctx);
265
266 if (lttng_live_should_quit()) {
267 ret = 0;
268 }
269 return ret;
270 }
271
272 static
273 struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
274 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
275 int whence), FILE *metadata_fp)
276 {
277 struct ctf_text_stream_pos *pos;
278
279 switch (flags & O_ACCMODE) {
280 case O_RDONLY:
281 /* OK */
282 break;
283 case O_RDWR:
284 fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n");
285 goto error;
286 default:
287 fprintf(stderr, "[error] Incorrect open flags.\n");
288 goto error;
289 }
290
291 pos = g_new0(struct ctf_text_stream_pos, 1);
292 pos->parent.rw_table = NULL;
293 pos->parent.event_cb = NULL;
294 pos->parent.trace = &pos->trace_descriptor;
295 if (lttng_live_open_trace_read(path) < 0) {
296 goto error;
297 }
298 return &pos->trace_descriptor;
299
300 error:
301 return NULL;
302 }
303
304 static
305 int lttng_live_close_trace(struct bt_trace_descriptor *td)
306 {
307 struct ctf_text_stream_pos *pos =
308 container_of(td, struct ctf_text_stream_pos,
309 trace_descriptor);
310 g_free(pos);
311 return 0;
312 }
313
314 static
315 struct bt_format lttng_live_format = {
316 .open_trace = lttng_live_open_trace,
317 .close_trace = lttng_live_close_trace,
318 };
319
320 static
321 void __attribute__((constructor)) lttng_live_init(void)
322 {
323 int ret;
324
325 lttng_live_format.name = g_quark_from_static_string("lttng-live");
326 ret = bt_register_format(&lttng_live_format);
327 assert(!ret);
328 }
329
330 static
331 void __attribute__((destructor)) lttng_live_exit(void)
332 {
333 bt_unregister_format(&lttng_live_format);
334 }
This page took 0.035034 seconds and 4 git commands to generate.