trigger: use condition and action ref counting to ease internal objects management
[lttng-tools.git] / src / common / location.c
CommitLineData
434131e4 1/*
ab5be9fa 2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
434131e4 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
434131e4 5 *
434131e4
JG
6 */
7
8#include <lttng/location-internal.h>
9#include <common/macros.h>
10#include <stdlib.h>
11
12static
13struct lttng_trace_archive_location *lttng_trace_archive_location_create(
14 enum lttng_trace_archive_location_type type)
15{
16 struct lttng_trace_archive_location *location;
17
18 location = zmalloc(sizeof(*location));
19 if (!location) {
20 goto end;
21 }
22
23 location->type = type;
24end:
25 return location;
26}
27
28LTTNG_HIDDEN
29void lttng_trace_archive_location_destroy(
30 struct lttng_trace_archive_location *location)
31{
32 if (!location) {
33 return;
34 }
35
36 switch (location->type) {
37 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
38 free(location->types.local.absolute_path);
39 break;
40 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
41 free(location->types.relay.host);
42 free(location->types.relay.relative_path);
43 break;
44 default:
45 abort();
46 }
47
48 free(location);
49}
50
51LTTNG_HIDDEN
52struct lttng_trace_archive_location *lttng_trace_archive_location_local_create(
53 const char *absolute_path)
54{
55 struct lttng_trace_archive_location *location = NULL;
56
57 if (!absolute_path) {
58 goto end;
59 }
60
61 location = lttng_trace_archive_location_create(
62 LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL);
63 if (!location) {
64 goto end;
65 }
66
67 location->types.local.absolute_path = strdup(absolute_path);
68 if (!location->types.local.absolute_path) {
69 goto error;
70 }
71
72end:
73 return location;
74error:
75 lttng_trace_archive_location_destroy(location);
76 return NULL;
77}
78
79LTTNG_HIDDEN
80struct lttng_trace_archive_location *lttng_trace_archive_location_relay_create(
81 const char *host,
82 enum lttng_trace_archive_location_relay_protocol_type protocol,
83 uint16_t control_port, uint16_t data_port,
84 const char *relative_path)
85{
86 struct lttng_trace_archive_location *location = NULL;
87
88 if (!host || !relative_path) {
89 goto end;
90 }
91
92 location = lttng_trace_archive_location_create(
93 LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY);
94 if (!location) {
95 goto end;
96 }
97
98 location->types.relay.host = strdup(host);
99 if (!location->types.relay.host) {
100 goto error;
101 }
102 location->types.relay.relative_path = strdup(relative_path);
103 if (!location->types.relay.relative_path) {
104 goto error;
105 }
106
107 location->types.relay.protocol = protocol;
108 location->types.relay.ports.control = control_port;
109 location->types.relay.ports.data = data_port;
110end:
111 return location;
112error:
113 lttng_trace_archive_location_destroy(location);
114 return NULL;
115}
116
1e9f1cf8
JG
117LTTNG_HIDDEN
118ssize_t lttng_trace_archive_location_create_from_buffer(
119 const struct lttng_buffer_view *view,
120 struct lttng_trace_archive_location **location)
121{
122 size_t offset = 0;
123 const struct lttng_trace_archive_location_comm *location_comm;
124 struct lttng_buffer_view location_comm_view;
125
126 location_comm_view = lttng_buffer_view_from_view(view, 0,
127 sizeof(*location_comm));
128 if (!location_comm_view.data) {
129 goto error;
130 }
131 offset += location_comm_view.size;
132 location_comm = (const struct lttng_trace_archive_location_comm *) view->data;
133
134 switch ((enum lttng_trace_archive_location_type) location_comm->type) {
135 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
136 {
137 const struct lttng_buffer_view absolute_path_view =
138 lttng_buffer_view_from_view(view, offset,
139 location_comm->types.local.absolute_path_len);
140
141 if (!absolute_path_view.data) {
142 goto error;
143 }
144 if (absolute_path_view.data[absolute_path_view.size - 1] != '\0') {
145 goto error;
146 }
147 offset += absolute_path_view.size;
148
149 *location = lttng_trace_archive_location_local_create(
150 absolute_path_view.data);
151 if (!*location) {
152 goto error;
153 }
154 break;
155 }
156 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
157 {
158 const struct lttng_buffer_view hostname_view =
159 lttng_buffer_view_from_view(view, offset,
160 location_comm->types.relay.hostname_len);
161 const struct lttng_buffer_view relative_path_view =
162 lttng_buffer_view_from_view(view,
163 offset + hostname_view.size,
164 location_comm->types.relay.relative_path_len);
165
166 if (!hostname_view.data || !relative_path_view.data) {
167 goto error;
168 }
169 if (hostname_view.data[hostname_view.size - 1] != '\0') {
170 goto error;
171 }
172 if (relative_path_view.data[relative_path_view.size - 1] != '\0') {
173 goto error;
174 }
175 offset += hostname_view.size + relative_path_view.size;
176
177 *location = lttng_trace_archive_location_relay_create(
178 hostname_view.data,
179 (enum lttng_trace_archive_location_relay_protocol_type) location_comm->types.relay.protocol,
180 location_comm->types.relay.ports.control,
181 location_comm->types.relay.ports.data,
182 relative_path_view.data);
183 if (!*location) {
184 goto error;
185 }
186 break;
187 }
188 default:
189 goto error;
190 }
191
e4b6e50f 192 return offset;
1e9f1cf8
JG
193error:
194 return -1;
195}
196
197LTTNG_HIDDEN
198ssize_t lttng_trace_archive_location_serialize(
199 const struct lttng_trace_archive_location *location,
200 struct lttng_dynamic_buffer *buffer)
201{
202 int ret;
203 struct lttng_trace_archive_location_comm location_comm;
1e9f1cf8
JG
204
205 location_comm.type = (int8_t) location->type;
206
207 switch (location->type) {
208 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
209 location_comm.types.local.absolute_path_len =
210 strlen(location->types.local.absolute_path) + 1;
211 break;
212 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
213 location_comm.types.relay.hostname_len =
214 strlen(location->types.relay.host) + 1;
215 location_comm.types.relay.protocol =
216 (int8_t) location->types.relay.protocol;
217 location_comm.types.relay.ports.control =
218 location->types.relay.ports.control;
219 location_comm.types.relay.ports.data =
220 location->types.relay.ports.data;
221 location_comm.types.relay.relative_path_len =
222 strlen(location->types.relay.relative_path) + 1;
223 break;
224 default:
225 abort();
226 }
227
228 ret = lttng_dynamic_buffer_append(buffer, &location_comm,
229 sizeof(location_comm));
230 if (ret) {
231 goto error;
232 }
233
234 switch (location->type) {
235 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
236 ret = lttng_dynamic_buffer_append(buffer,
237 location->types.local.absolute_path,
238 location_comm.types.local.absolute_path_len);
239 if (ret) {
240 goto error;
241 }
242 break;
243 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
244 ret = lttng_dynamic_buffer_append(buffer,
245 location->types.relay.host,
246 location_comm.types.relay.hostname_len);
247 if (ret) {
248 goto error;
249 }
250 ret = lttng_dynamic_buffer_append(buffer,
251 location->types.relay.relative_path,
252 location_comm.types.relay.relative_path_len);
253 if (ret) {
254 goto error;
255 }
256 break;
257 default:
258 abort();
259 }
260
f7daf93a 261 return 0;
1e9f1cf8
JG
262error:
263 return -1;
264}
265
434131e4
JG
266enum lttng_trace_archive_location_type lttng_trace_archive_location_get_type(
267 const struct lttng_trace_archive_location *location)
268{
269 enum lttng_trace_archive_location_type type;
270
271 if (!location) {
272 type = LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_UNKNOWN;
273 goto end;
274 }
275
276 type = location->type;
277end:
278 return type;
279}
280
281enum lttng_trace_archive_location_status
282lttng_trace_archive_location_local_get_absolute_path(
283 const struct lttng_trace_archive_location *location,
284 const char **absolute_path)
285{
286 enum lttng_trace_archive_location_status status =
287 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK;
288
289 if (!location || !absolute_path ||
290 location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL) {
291 status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID;
292 goto end;
293 }
294
295 *absolute_path = location->types.local.absolute_path;
296end:
297 return status;
298}
299
300enum lttng_trace_archive_location_status
301lttng_trace_archive_location_relay_get_host(
302 const struct lttng_trace_archive_location *location,
303 const char **relay_host)
304{
305 enum lttng_trace_archive_location_status status =
306 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK;
307
308 if (!location || !relay_host ||
309 location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) {
310 status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID;
311 goto end;
312 }
313
314 *relay_host = location->types.relay.host;
315end:
316 return status;
317}
318
319enum lttng_trace_archive_location_status
320lttng_trace_archive_location_relay_get_relative_path(
321 const struct lttng_trace_archive_location *location,
322 const char **relative_path)
323{
324 enum lttng_trace_archive_location_status status =
325 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK;
326
327 if (!location || !relative_path ||
328 location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) {
329 status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID;
330 goto end;
331 }
332
333 *relative_path = location->types.relay.relative_path;
334end:
335 return status;
336}
337
338enum lttng_trace_archive_location_status
339lttng_trace_archive_location_relay_get_control_port(
340 const struct lttng_trace_archive_location *location,
341 uint16_t *control_port)
342{
343 enum lttng_trace_archive_location_status status =
344 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK;
345
346 if (!location || !control_port ||
347 location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) {
348 status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID;
349 goto end;
350 }
351
352 *control_port = location->types.relay.ports.control;
353end:
354 return status;
355}
356
357enum lttng_trace_archive_location_status
358lttng_trace_archive_location_relay_get_data_port(
359 const struct lttng_trace_archive_location *location,
360 uint16_t *data_port)
361{
362 enum lttng_trace_archive_location_status status =
363 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK;
364
365 if (!location || !data_port ||
366 location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) {
367 status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID;
368 goto end;
369 }
370
371 *data_port = location->types.relay.ports.data;
372end:
373 return status;
374}
375
376enum lttng_trace_archive_location_status
377lttng_trace_archive_location_relay_get_protocol_type(
378 const struct lttng_trace_archive_location *location,
379 enum lttng_trace_archive_location_relay_protocol_type *protocol)
380{
381 enum lttng_trace_archive_location_status status =
382 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK;
383
384 if (!location || !protocol ||
385 location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) {
386 status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID;
387 goto end;
388 }
389
390 *protocol = location->types.relay.protocol;
391end:
392 return status;
393}
This page took 0.04486 seconds and 4 git commands to generate.