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