2 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/location-internal.h>
19 #include <common/macros.h>
23 struct lttng_trace_archive_location
*lttng_trace_archive_location_create(
24 enum lttng_trace_archive_location_type type
)
26 struct lttng_trace_archive_location
*location
;
28 location
= zmalloc(sizeof(*location
));
33 location
->type
= type
;
39 void lttng_trace_archive_location_destroy(
40 struct lttng_trace_archive_location
*location
)
46 switch (location
->type
) {
47 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
48 free(location
->types
.local
.absolute_path
);
50 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
51 free(location
->types
.relay
.host
);
52 free(location
->types
.relay
.relative_path
);
62 struct lttng_trace_archive_location
*lttng_trace_archive_location_local_create(
63 const char *absolute_path
)
65 struct lttng_trace_archive_location
*location
= NULL
;
71 location
= lttng_trace_archive_location_create(
72 LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
);
77 location
->types
.local
.absolute_path
= strdup(absolute_path
);
78 if (!location
->types
.local
.absolute_path
) {
85 lttng_trace_archive_location_destroy(location
);
90 struct lttng_trace_archive_location
*lttng_trace_archive_location_relay_create(
92 enum lttng_trace_archive_location_relay_protocol_type protocol
,
93 uint16_t control_port
, uint16_t data_port
,
94 const char *relative_path
)
96 struct lttng_trace_archive_location
*location
= NULL
;
98 if (!host
|| !relative_path
) {
102 location
= lttng_trace_archive_location_create(
103 LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
);
108 location
->types
.relay
.host
= strdup(host
);
109 if (!location
->types
.relay
.host
) {
112 location
->types
.relay
.relative_path
= strdup(relative_path
);
113 if (!location
->types
.relay
.relative_path
) {
117 location
->types
.relay
.protocol
= protocol
;
118 location
->types
.relay
.ports
.control
= control_port
;
119 location
->types
.relay
.ports
.data
= data_port
;
123 lttng_trace_archive_location_destroy(location
);
128 ssize_t
lttng_trace_archive_location_create_from_buffer(
129 const struct lttng_buffer_view
*view
,
130 struct lttng_trace_archive_location
**location
)
133 const struct lttng_trace_archive_location_comm
*location_comm
;
134 struct lttng_buffer_view location_comm_view
;
136 location_comm_view
= lttng_buffer_view_from_view(view
, 0,
137 sizeof(*location_comm
));
138 if (!location_comm_view
.data
) {
141 offset
+= location_comm_view
.size
;
142 location_comm
= (const struct lttng_trace_archive_location_comm
*) view
->data
;
144 switch ((enum lttng_trace_archive_location_type
) location_comm
->type
) {
145 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
147 const struct lttng_buffer_view absolute_path_view
=
148 lttng_buffer_view_from_view(view
, offset
,
149 location_comm
->types
.local
.absolute_path_len
);
151 if (!absolute_path_view
.data
) {
154 if (absolute_path_view
.data
[absolute_path_view
.size
- 1] != '\0') {
157 offset
+= absolute_path_view
.size
;
159 *location
= lttng_trace_archive_location_local_create(
160 absolute_path_view
.data
);
166 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
168 const struct lttng_buffer_view hostname_view
=
169 lttng_buffer_view_from_view(view
, offset
,
170 location_comm
->types
.relay
.hostname_len
);
171 const struct lttng_buffer_view relative_path_view
=
172 lttng_buffer_view_from_view(view
,
173 offset
+ hostname_view
.size
,
174 location_comm
->types
.relay
.relative_path_len
);
176 if (!hostname_view
.data
|| !relative_path_view
.data
) {
179 if (hostname_view
.data
[hostname_view
.size
- 1] != '\0') {
182 if (relative_path_view
.data
[relative_path_view
.size
- 1] != '\0') {
185 offset
+= hostname_view
.size
+ relative_path_view
.size
;
187 *location
= lttng_trace_archive_location_relay_create(
189 (enum lttng_trace_archive_location_relay_protocol_type
) location_comm
->types
.relay
.protocol
,
190 location_comm
->types
.relay
.ports
.control
,
191 location_comm
->types
.relay
.ports
.data
,
192 relative_path_view
.data
);
208 ssize_t
lttng_trace_archive_location_serialize(
209 const struct lttng_trace_archive_location
*location
,
210 struct lttng_dynamic_buffer
*buffer
)
213 struct lttng_trace_archive_location_comm location_comm
;
215 location_comm
.type
= (int8_t) location
->type
;
217 switch (location
->type
) {
218 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
219 location_comm
.types
.local
.absolute_path_len
=
220 strlen(location
->types
.local
.absolute_path
) + 1;
222 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
223 location_comm
.types
.relay
.hostname_len
=
224 strlen(location
->types
.relay
.host
) + 1;
225 location_comm
.types
.relay
.protocol
=
226 (int8_t) location
->types
.relay
.protocol
;
227 location_comm
.types
.relay
.ports
.control
=
228 location
->types
.relay
.ports
.control
;
229 location_comm
.types
.relay
.ports
.data
=
230 location
->types
.relay
.ports
.data
;
231 location_comm
.types
.relay
.relative_path_len
=
232 strlen(location
->types
.relay
.relative_path
) + 1;
238 ret
= lttng_dynamic_buffer_append(buffer
, &location_comm
,
239 sizeof(location_comm
));
244 switch (location
->type
) {
245 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
246 ret
= lttng_dynamic_buffer_append(buffer
,
247 location
->types
.local
.absolute_path
,
248 location_comm
.types
.local
.absolute_path_len
);
253 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
254 ret
= lttng_dynamic_buffer_append(buffer
,
255 location
->types
.relay
.host
,
256 location_comm
.types
.relay
.hostname_len
);
260 ret
= lttng_dynamic_buffer_append(buffer
,
261 location
->types
.relay
.relative_path
,
262 location_comm
.types
.relay
.relative_path_len
);
276 enum lttng_trace_archive_location_type
lttng_trace_archive_location_get_type(
277 const struct lttng_trace_archive_location
*location
)
279 enum lttng_trace_archive_location_type type
;
282 type
= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_UNKNOWN
;
286 type
= location
->type
;
291 enum lttng_trace_archive_location_status
292 lttng_trace_archive_location_local_get_absolute_path(
293 const struct lttng_trace_archive_location
*location
,
294 const char **absolute_path
)
296 enum lttng_trace_archive_location_status status
=
297 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
299 if (!location
|| !absolute_path
||
300 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
) {
301 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
305 *absolute_path
= location
->types
.local
.absolute_path
;
310 enum lttng_trace_archive_location_status
311 lttng_trace_archive_location_relay_get_host(
312 const struct lttng_trace_archive_location
*location
,
313 const char **relay_host
)
315 enum lttng_trace_archive_location_status status
=
316 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
318 if (!location
|| !relay_host
||
319 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
320 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
324 *relay_host
= location
->types
.relay
.host
;
329 enum lttng_trace_archive_location_status
330 lttng_trace_archive_location_relay_get_relative_path(
331 const struct lttng_trace_archive_location
*location
,
332 const char **relative_path
)
334 enum lttng_trace_archive_location_status status
=
335 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
337 if (!location
|| !relative_path
||
338 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
339 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
343 *relative_path
= location
->types
.relay
.relative_path
;
348 enum lttng_trace_archive_location_status
349 lttng_trace_archive_location_relay_get_control_port(
350 const struct lttng_trace_archive_location
*location
,
351 uint16_t *control_port
)
353 enum lttng_trace_archive_location_status status
=
354 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
356 if (!location
|| !control_port
||
357 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
358 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
362 *control_port
= location
->types
.relay
.ports
.control
;
367 enum lttng_trace_archive_location_status
368 lttng_trace_archive_location_relay_get_data_port(
369 const struct lttng_trace_archive_location
*location
,
372 enum lttng_trace_archive_location_status status
=
373 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
375 if (!location
|| !data_port
||
376 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
377 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
381 *data_port
= location
->types
.relay
.ports
.data
;
386 enum lttng_trace_archive_location_status
387 lttng_trace_archive_location_relay_get_protocol_type(
388 const struct lttng_trace_archive_location
*location
,
389 enum lttng_trace_archive_location_relay_protocol_type
*protocol
)
391 enum lttng_trace_archive_location_status status
=
392 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
394 if (!location
|| !protocol
||
395 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
396 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
400 *protocol
= location
->types
.relay
.protocol
;