Fix: sessiond: incorrect use of exclusions array leads to crash
[lttng-tools.git] / src / lib / lttng-ctl / load.cpp
1 /*
2 * Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "lttng-ctl-helper.hpp"
10
11 #include <common/compat/string.hpp>
12 #include <common/config/session-config.hpp>
13 #include <common/macros.hpp>
14 #include <common/sessiond-comm/sessiond-comm.hpp>
15 #include <common/uri.hpp>
16
17 #include <lttng/load-internal.hpp>
18 #include <lttng/load.h>
19 #include <lttng/lttng-error.h>
20
21 #include <limits.h>
22 #include <string.h>
23
24 struct lttng_load_session_attr *lttng_load_session_attr_create(void)
25 {
26 return zmalloc<lttng_load_session_attr>();
27 }
28
29 static void reset_load_session_attr_urls(struct lttng_load_session_attr *attr)
30 {
31 free(attr->raw_override_url);
32 free(attr->raw_override_path_url);
33 free(attr->raw_override_ctrl_url);
34 free(attr->raw_override_data_url);
35 if (attr->override_attr) {
36 free(attr->override_attr->path_url);
37 free(attr->override_attr->ctrl_url);
38 free(attr->override_attr->data_url);
39 free(attr->override_attr->session_name);
40 }
41 }
42
43 void lttng_load_session_attr_destroy(struct lttng_load_session_attr *attr)
44 {
45 if (attr) {
46 reset_load_session_attr_urls(attr);
47 free(attr->override_attr);
48 free(attr);
49 }
50 }
51
52 static int validate_attr(const struct lttng_load_session_attr *attr)
53 {
54 int ret = 0;
55
56 if (!attr) {
57 ret = -LTTNG_ERR_INVALID;
58 goto end;
59 }
60
61 if (!attr->override_attr) {
62 goto end;
63 }
64
65 /*
66 * Refuse override name if the objective is to load multiple session
67 * since this operation is ambiguous while loading multiple session.
68 */
69 if (attr->override_attr->session_name && attr->session_name[0] == '\0') {
70 ret = -LTTNG_ERR_INVALID;
71 goto end;
72 }
73 end:
74 return ret;
75 }
76
77 const char *lttng_load_session_attr_get_session_name(struct lttng_load_session_attr *attr)
78 {
79 const char *ret = nullptr;
80
81 if (attr && attr->session_name[0]) {
82 ret = attr->session_name;
83 }
84
85 return ret;
86 }
87
88 const char *lttng_load_session_attr_get_input_url(struct lttng_load_session_attr *attr)
89 {
90 const char *ret = nullptr;
91
92 if (attr && attr->input_url[0]) {
93 ret = attr->input_url;
94 }
95
96 return ret;
97 }
98
99 int lttng_load_session_attr_get_overwrite(struct lttng_load_session_attr *attr)
100 {
101 return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
102 }
103
104 const char *lttng_load_session_attr_get_override_ctrl_url(struct lttng_load_session_attr *attr)
105 {
106 const char *ret = nullptr;
107
108 if (!attr || !attr->override_attr) {
109 goto end;
110 }
111
112 ret = attr->raw_override_ctrl_url;
113 end:
114 return ret;
115 }
116
117 const char *lttng_load_session_attr_get_override_data_url(struct lttng_load_session_attr *attr)
118 {
119 const char *ret = nullptr;
120
121 if (!attr || !attr->override_attr) {
122 goto end;
123 }
124
125 ret = attr->raw_override_data_url;
126 end:
127 return ret;
128 }
129
130 const char *lttng_load_session_attr_get_override_url(struct lttng_load_session_attr *attr)
131 {
132 const char *ret = nullptr;
133
134 if (!attr || !attr->override_attr) {
135 goto end;
136 }
137
138 if ((attr->override_attr->path_url ||
139 (attr->override_attr->ctrl_url && attr->override_attr->data_url))) {
140 ret = attr->raw_override_url;
141 }
142 end:
143 return ret;
144 }
145
146 const char *lttng_load_session_attr_get_override_session_name(struct lttng_load_session_attr *attr)
147 {
148 const char *ret = nullptr;
149
150 if (!attr || !attr->override_attr) {
151 goto end;
152 }
153
154 ret = attr->override_attr->session_name;
155 end:
156 return ret;
157 }
158
159 int lttng_load_session_attr_set_session_name(struct lttng_load_session_attr *attr,
160 const char *session_name)
161 {
162 int ret = 0;
163
164 if (!attr) {
165 ret = -LTTNG_ERR_INVALID;
166 goto error;
167 }
168
169 if (session_name) {
170 size_t len;
171
172 len = strlen(session_name);
173 if (len >= LTTNG_NAME_MAX) {
174 ret = -LTTNG_ERR_INVALID;
175 goto error;
176 }
177
178 ret = lttng_strncpy(attr->session_name, session_name, sizeof(attr->session_name));
179 if (ret) {
180 ret = -LTTNG_ERR_INVALID;
181 goto error;
182 }
183 } else {
184 attr->session_name[0] = '\0';
185 }
186 error:
187 return ret;
188 }
189
190 int lttng_load_session_attr_set_input_url(struct lttng_load_session_attr *attr, const char *url)
191 {
192 int ret = 0;
193 size_t len;
194 ssize_t size;
195 struct lttng_uri *uris = nullptr;
196
197 if (!attr) {
198 ret = -LTTNG_ERR_INVALID;
199 goto error;
200 }
201
202 if (!url) {
203 attr->input_url[0] = '\0';
204 ret = 0;
205 goto end;
206 }
207
208 len = strlen(url);
209 if (len >= PATH_MAX) {
210 ret = -LTTNG_ERR_INVALID;
211 goto error;
212 }
213
214 size = uri_parse_str_urls(url, nullptr, &uris);
215 if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) {
216 ret = -LTTNG_ERR_INVALID;
217 goto error;
218 }
219
220 /* Copy string plus the NULL terminated byte. */
221 ret = lttng_strncpy(attr->input_url, uris[0].dst.path, sizeof(attr->input_url));
222 if (ret) {
223 ret = -LTTNG_ERR_INVALID;
224 goto error;
225 }
226
227 end:
228 error:
229 free(uris);
230 return ret;
231 }
232
233 int lttng_load_session_attr_set_overwrite(struct lttng_load_session_attr *attr, int overwrite)
234 {
235 int ret = 0;
236
237 if (!attr) {
238 ret = -LTTNG_ERR_INVALID;
239 goto end;
240 }
241
242 attr->overwrite = !!overwrite;
243 end:
244 return ret;
245 }
246
247 int lttng_load_session_attr_set_override_ctrl_url(struct lttng_load_session_attr *attr,
248 const char *url)
249 {
250 int ret = 0;
251 ssize_t ret_size;
252 struct lttng_uri *uri = nullptr;
253 char *url_str = nullptr;
254 char *raw_str = nullptr;
255
256 if (!attr) {
257 ret = -LTTNG_ERR_INVALID;
258 goto end;
259 }
260
261 if (!attr->override_attr) {
262 attr->override_attr = zmalloc<config_load_session_override_attr>();
263 if (!attr->override_attr) {
264 ret = -LTTNG_ERR_NOMEM;
265 goto end;
266 }
267 }
268
269 if (attr->override_attr->path_url) {
270 /*
271 * Setting a ctrl override after a path override makes no sense.
272 */
273 ret = -LTTNG_ERR_INVALID;
274 goto end;
275 }
276
277 /*
278 * FIXME: uri_parse should be able to take as parameter the protocol
279 * type to validate "url". For now only check the parsing goes through;
280 * it will fail later on.
281 */
282 ret_size = uri_parse(url, &uri);
283 if (ret_size < 0) {
284 ret = -LTTNG_ERR_INVALID;
285 goto end;
286 }
287
288 if (uri[0].port == 0) {
289 uri[0].port = DEFAULT_NETWORK_CONTROL_PORT;
290 }
291
292 url_str = calloc<char>(PATH_MAX);
293 if (!url_str) {
294 /* FIXME: return valid error */
295 ret = -LTTNG_ERR_NOMEM;
296 goto end;
297 }
298
299 ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
300 if (ret < 0) {
301 ret = -LTTNG_ERR_INVALID;
302 goto end;
303 }
304 ret = 0;
305
306 raw_str = lttng_strndup(url, PATH_MAX);
307 if (!raw_str) {
308 ret = -LTTNG_ERR_NOMEM;
309 goto end;
310 }
311
312 /* Squash old value if any */
313 free(attr->override_attr->ctrl_url);
314 free(attr->raw_override_ctrl_url);
315
316 /* Populate the object */
317 attr->override_attr->ctrl_url = url_str;
318 attr->raw_override_ctrl_url = raw_str;
319
320 /* Ownership passed to attr. */
321 url_str = nullptr;
322 raw_str = nullptr;
323
324 end:
325 free(raw_str);
326 free(url_str);
327 free(uri);
328 return ret;
329 }
330
331 int lttng_load_session_attr_set_override_data_url(struct lttng_load_session_attr *attr,
332 const char *url)
333 {
334 int ret = 0;
335 ssize_t ret_size;
336 struct lttng_uri *uri = nullptr;
337 char *url_str = nullptr;
338 char *raw_str = nullptr;
339
340 if (!attr) {
341 ret = -LTTNG_ERR_INVALID;
342 goto end;
343 }
344
345 if (!attr->override_attr) {
346 attr->override_attr = zmalloc<config_load_session_override_attr>();
347 if (!attr->override_attr) {
348 ret = -LTTNG_ERR_NOMEM;
349 goto end;
350 }
351 }
352
353 if (attr->override_attr->path_url) {
354 /*
355 * Setting a data override after a path override makes no sense.
356 */
357 ret = -LTTNG_ERR_INVALID;
358 goto end;
359 }
360
361 /*
362 * FIXME: uri_parse should be able to take as parameter the protocol
363 * type to validate "url". For now only check the parsing goes through;
364 * it will fail later on.
365 */
366 ret_size = uri_parse(url, &uri);
367 if (ret_size < 0) {
368 ret = -LTTNG_ERR_INVALID;
369 goto end;
370 }
371
372 if (uri[0].port == 0) {
373 uri[0].port = DEFAULT_NETWORK_DATA_PORT;
374 }
375
376 url_str = calloc<char>(PATH_MAX);
377 if (!url_str) {
378 ret = -LTTNG_ERR_NOMEM;
379 goto end;
380 }
381
382 ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
383 if (ret < 0) {
384 ret = -LTTNG_ERR_INVALID;
385 goto end;
386 }
387 ret = 0;
388
389 raw_str = lttng_strndup(url, PATH_MAX);
390 if (!raw_str) {
391 ret = -LTTNG_ERR_NOMEM;
392 goto end;
393 }
394
395 /* Squash old value if any */
396 free(attr->override_attr->data_url);
397 free(attr->raw_override_data_url);
398
399 /* Populate the object */
400 attr->override_attr->data_url = url_str;
401 attr->raw_override_data_url = raw_str;
402
403 /* Ownership passed to attr. */
404 url_str = nullptr;
405 raw_str = nullptr;
406 end:
407 free(raw_str);
408 free(url_str);
409 free(uri);
410 return ret;
411 }
412
413 int lttng_load_session_attr_set_override_url(struct lttng_load_session_attr *attr, const char *url)
414 {
415 int ret = 0;
416 ssize_t ret_size;
417 struct lttng_uri *uri = nullptr;
418 char *raw_url_str = nullptr;
419 char *raw_path_str = nullptr;
420 char *path_str = nullptr;
421 char *raw_ctrl_str = nullptr;
422 char *ctrl_str = nullptr;
423 char *raw_data_str = nullptr;
424 char *data_str = nullptr;
425 char buffer[PATH_MAX];
426
427 if (!attr || !url || strlen(url) >= PATH_MAX) {
428 ret = -LTTNG_ERR_INVALID;
429 goto end;
430 }
431
432 if (!attr->override_attr) {
433 attr->override_attr = zmalloc<config_load_session_override_attr>();
434 if (!attr->override_attr) {
435 ret = -LTTNG_ERR_NOMEM;
436 goto end;
437 }
438 }
439
440 /*
441 * FIXME: uri_parse should be able to take as parameter the protocol
442 * type to validate "url". For now only check the parsing goes through;
443 * it will fail later on.
444 */
445 ret_size = uri_parse_str_urls(url, nullptr, &uri);
446 if (ret_size < 0 || ret_size > 2) {
447 /* Unexpected URL format. */
448 ret = -LTTNG_ERR_INVALID;
449 goto end;
450 }
451
452 raw_url_str = lttng_strndup(url, PATH_MAX);
453 if (!raw_url_str) {
454 ret = -LTTNG_ERR_NOMEM;
455 goto end;
456 }
457
458 /* Get path | ctrl && data string URL. */
459 ret = uri_to_str_url(&uri[0], buffer, sizeof(buffer));
460 if (ret < 0 || ret >= PATH_MAX) {
461 ret = -LTTNG_ERR_INVALID;
462 goto end;
463 }
464 ret = 0;
465
466 switch (uri[0].dtype) {
467 case LTTNG_DST_PATH:
468 raw_path_str = lttng_strndup(buffer, PATH_MAX);
469 if (!raw_path_str) {
470 ret = -LTTNG_ERR_NOMEM;
471 goto end;
472 }
473
474 path_str = lttng_strndup(uri[0].dst.path, PATH_MAX);
475 if (!path_str) {
476 ret = -LTTNG_ERR_NOMEM;
477 goto end;
478 }
479 break;
480 case LTTNG_DST_IPV4:
481 case LTTNG_DST_IPV6:
482 if (ret_size != 2) {
483 ret = -LTTNG_ERR_INVALID;
484 goto end;
485 }
486
487 raw_ctrl_str = lttng_strndup(buffer, PATH_MAX);
488 if (!raw_ctrl_str) {
489 ret = -LTTNG_ERR_NOMEM;
490 goto end;
491 }
492
493 ctrl_str = lttng_strndup(raw_ctrl_str, PATH_MAX);
494 if (!ctrl_str) {
495 ret = -LTTNG_ERR_NOMEM;
496 goto end;
497 }
498
499 /* Get the data uri. */
500 ret = uri_to_str_url(&uri[1], buffer, sizeof(buffer));
501 if (ret < 0) {
502 ret = -LTTNG_ERR_INVALID;
503 goto end;
504 }
505 ret = 0;
506
507 raw_data_str = lttng_strndup(buffer, PATH_MAX);
508 if (!raw_data_str) {
509 ret = -LTTNG_ERR_NOMEM;
510 goto end;
511 }
512
513 data_str = lttng_strndup(raw_data_str, PATH_MAX);
514 if (!data_str) {
515 ret = -LTTNG_ERR_NOMEM;
516 goto end;
517 }
518
519 break;
520 default:
521 ret = -LTTNG_ERR_INVALID;
522 goto end;
523 }
524
525 reset_load_session_attr_urls(attr);
526
527 attr->override_attr->path_url = path_str;
528 attr->override_attr->ctrl_url = ctrl_str;
529 attr->override_attr->data_url = data_str;
530
531 attr->raw_override_url = raw_url_str;
532 attr->raw_override_path_url = raw_path_str;
533 attr->raw_override_ctrl_url = raw_ctrl_str;
534 attr->raw_override_data_url = raw_data_str;
535
536 /* Pass data ownership to attr. */
537 raw_url_str = nullptr;
538 raw_path_str = nullptr;
539 path_str = nullptr;
540 raw_ctrl_str = nullptr;
541 ctrl_str = nullptr;
542 raw_data_str = nullptr;
543 data_str = nullptr;
544
545 end:
546 free(raw_path_str);
547 free(path_str);
548 free(raw_ctrl_str);
549 free(ctrl_str);
550 free(raw_data_str);
551 free(data_str);
552 free(raw_url_str);
553 free(uri);
554 return ret;
555 }
556
557 int lttng_load_session_attr_set_override_session_name(struct lttng_load_session_attr *attr,
558 const char *session_name)
559 {
560 int ret = 0;
561 size_t len;
562
563 if (!attr || !session_name) {
564 ret = -LTTNG_ERR_INVALID;
565 goto end;
566 }
567
568 if (!attr->override_attr) {
569 attr->override_attr = zmalloc<config_load_session_override_attr>();
570 if (!attr->override_attr) {
571 ret = -LTTNG_ERR_NOMEM;
572 goto end;
573 }
574 }
575
576 len = strlen(session_name);
577 if (len >= LTTNG_NAME_MAX) {
578 ret = -LTTNG_ERR_INVALID;
579 goto end;
580 }
581
582 attr->override_attr->session_name = lttng_strndup(session_name, len);
583 if (!attr->override_attr->session_name) {
584 ret = -LTTNG_ERR_NOMEM;
585 goto end;
586 }
587 end:
588 return ret;
589 }
590
591 int lttng_load_session(struct lttng_load_session_attr *attr)
592 {
593 int ret;
594 const char *url, *session_name;
595
596 if (!attr) {
597 ret = -LTTNG_ERR_INVALID;
598 goto end;
599 }
600
601 ret = validate_attr(attr);
602 if (ret) {
603 goto end;
604 }
605
606 url = attr->input_url[0] != '\0' ? attr->input_url : nullptr;
607 session_name = attr->session_name[0] != '\0' ? attr->session_name : nullptr;
608
609 ret = config_load_session(url, session_name, attr->overwrite, 0, attr->override_attr);
610
611 end:
612 return ret;
613 }
This page took 0.041411 seconds and 4 git commands to generate.