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