Remove lttng_load_session_attr_get_override_path_url
[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 }
51 }
52
53 void lttng_load_session_attr_destroy(struct lttng_load_session_attr *attr)
54 {
55 if (attr) {
56 reset_load_session_attr_urls(attr);
57 free(attr->override_attr);
58 free(attr);
59 }
60 }
61
62 const char *lttng_load_session_attr_get_session_name(
63 struct lttng_load_session_attr *attr)
64 {
65 const char *ret = NULL;
66
67 if (attr && attr->session_name[0]) {
68 ret = attr->session_name;
69 }
70
71 return ret;
72 }
73
74 const char *lttng_load_session_attr_get_input_url(
75 struct lttng_load_session_attr *attr)
76 {
77 const char *ret = NULL;
78
79 if (attr && attr->input_url[0]) {
80 ret = attr->input_url;
81 }
82
83 return ret;
84 }
85
86 int lttng_load_session_attr_get_overwrite(
87 struct lttng_load_session_attr *attr)
88 {
89 return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
90 }
91
92 const char *lttng_load_session_attr_get_override_ctrl_url(
93 struct lttng_load_session_attr *attr)
94 {
95 const char *ret = NULL;
96
97 if (attr && attr->override_attr->ctrl_url) {
98 ret = attr->raw_override_ctrl_url;
99 }
100
101 return ret;
102 }
103
104 const char *lttng_load_session_attr_get_override_data_url(
105 struct lttng_load_session_attr *attr)
106 {
107 const char *ret = NULL;
108
109 if (attr && attr->override_attr->data_url) {
110 ret = attr->raw_override_data_url;
111 }
112
113 return ret;
114 }
115
116 const char *lttng_load_session_attr_get_override_url(
117 struct lttng_load_session_attr *attr)
118 {
119 const char *ret = NULL;
120
121 if (attr && (attr->override_attr->path_url ||
122 (attr->override_attr->ctrl_url &&
123 attr->override_attr->data_url))) {
124 ret = attr->raw_override_url;
125 }
126
127 return ret;
128 }
129
130 int lttng_load_session_attr_set_session_name(
131 struct lttng_load_session_attr *attr, const char *session_name)
132 {
133 int ret = 0;
134
135 if (!attr) {
136 ret = -LTTNG_ERR_INVALID;
137 goto error;
138 }
139
140 if (session_name) {
141 size_t len;
142
143 len = strlen(session_name);
144 if (len >= LTTNG_NAME_MAX) {
145 ret = -LTTNG_ERR_INVALID;
146 goto error;
147 }
148
149 strncpy(attr->session_name, session_name, len);
150 } else {
151 attr->session_name[0] = '\0';
152 }
153 error:
154 return ret;
155 }
156
157 int lttng_load_session_attr_set_input_url(
158 struct lttng_load_session_attr *attr, const char *url)
159 {
160 int ret = 0;
161 size_t len;
162 ssize_t size;
163 struct lttng_uri *uris = NULL;
164
165 if (!attr) {
166 ret = -LTTNG_ERR_INVALID;
167 goto error;
168 }
169
170 if (!url) {
171 attr->input_url[0] = '\0';
172 ret = 0;
173 goto end;
174 }
175
176 len = strlen(url);
177 if (len >= PATH_MAX) {
178 ret = -LTTNG_ERR_INVALID;
179 goto error;
180 }
181
182 size = uri_parse_str_urls(url, NULL, &uris);
183 if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) {
184 ret = -LTTNG_ERR_INVALID;
185 goto error;
186 }
187
188 /* Copy string plus the NULL terminated byte. */
189 lttng_ctl_copy_string(attr->input_url, uris[0].dst.path,
190 sizeof(attr->input_url));
191
192 end:
193 error:
194 free(uris);
195 return ret;
196 }
197
198 int lttng_load_session_attr_set_overwrite(
199 struct lttng_load_session_attr *attr, int overwrite)
200 {
201 int ret = 0;
202
203 if (!attr) {
204 ret = -LTTNG_ERR_INVALID;
205 goto end;
206 }
207
208 attr->overwrite = !!overwrite;
209 end:
210 return ret;
211 }
212
213 int lttng_load_session_attr_set_override_ctrl_url(
214 struct lttng_load_session_attr *attr, const char *url)
215 {
216 int ret = 0;
217 ssize_t ret_size;
218 struct lttng_uri *uri = NULL;
219 char *url_str = NULL;
220 char *raw_str = NULL;
221
222 if (!attr) {
223 ret = -LTTNG_ERR_INVALID;
224 goto end;
225 }
226
227 if (!attr->override_attr) {
228 attr->override_attr = zmalloc(
229 sizeof(struct config_load_session_override_attr));
230 if (!attr->override_attr) {
231 ret = -LTTNG_ERR_NOMEM;
232 goto end;
233 }
234 }
235
236 if (attr->override_attr->path_url) {
237 /*
238 * Setting a ctrl override after a path override makes no sense.
239 */
240 ret = -LTTNG_ERR_INVALID;
241 goto end;
242 }
243
244 /*
245 * FIXME: uri_parse should be able to take as parameter the protocol
246 * type to validate "url". For now only check the parsing goes through;
247 * it will fail later on.
248 */
249 ret_size = uri_parse(url, &uri);
250 if (ret_size < 0) {
251 ret = -LTTNG_ERR_INVALID;
252 goto end;
253 }
254
255 if (uri[0].port == 0) {
256 uri[0].port = DEFAULT_NETWORK_CONTROL_PORT;
257 }
258
259 url_str = zmalloc(PATH_MAX);
260 if (!url_str) {
261 /* FIXME: return valid error */
262 ret = -LTTNG_ERR_NOMEM;
263 goto end;
264 }
265
266 ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
267 if (ret < 0) {
268 ret = -LTTNG_ERR_INVALID;
269 goto end;
270 }
271 ret = 0;
272
273 raw_str = lttng_strndup(url, PATH_MAX);
274 if (!raw_str) {
275 ret = -LTTNG_ERR_NOMEM;
276 goto end;
277 }
278
279 /* Squash old value if any */
280 free(attr->override_attr->ctrl_url);
281 free(attr->raw_override_ctrl_url);
282
283 /* Populate the object */
284 attr->override_attr->ctrl_url = url_str;
285 attr->raw_override_ctrl_url = raw_str;
286
287 /* Ownership passed to attr. */
288 url_str = NULL;
289 raw_str = NULL;
290
291 end:
292 free(raw_str);
293 free(url_str);
294 free(uri);
295 return ret;
296 }
297
298 int lttng_load_session_attr_set_override_data_url(
299 struct lttng_load_session_attr *attr, const char *url)
300 {
301 int ret = 0;
302 ssize_t ret_size;
303 struct lttng_uri *uri = NULL;
304 char *url_str = NULL;
305 char *raw_str = NULL;
306
307 if (!attr) {
308 ret = -LTTNG_ERR_INVALID;
309 goto end;
310 }
311
312 if (!attr->override_attr) {
313 attr->override_attr = zmalloc(
314 sizeof(struct config_load_session_override_attr));
315 if (!attr->override_attr) {
316 ret = -LTTNG_ERR_NOMEM;
317 goto end;
318 }
319 }
320
321 if (attr->override_attr->path_url) {
322 /*
323 * Setting a data override after a path override makes no sense.
324 */
325 ret = -LTTNG_ERR_INVALID;
326 goto end;
327 }
328
329 /*
330 * FIXME: uri_parse should be able to take as parameter the protocol
331 * type to validate "url". For now only check the parsing goes through;
332 * it will fail later on.
333 */
334 ret_size = uri_parse(url, &uri);
335 if (ret_size < 0) {
336 ret = -LTTNG_ERR_INVALID;
337 goto end;
338 }
339
340 if (uri[0].port == 0) {
341 uri[0].port = DEFAULT_NETWORK_DATA_PORT;
342 }
343
344 url_str = zmalloc(PATH_MAX);
345 if (!url_str) {
346 ret = -LTTNG_ERR_NOMEM;
347 goto end;
348 }
349
350 ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
351 if (ret < 0) {
352 ret = -LTTNG_ERR_INVALID;
353 goto end;
354 }
355 ret = 0;
356
357 raw_str = lttng_strndup(url, PATH_MAX);
358 if (!raw_str) {
359 ret = -LTTNG_ERR_NOMEM;
360 goto end;
361 }
362
363 /* Squash old value if any */
364 free(attr->override_attr->data_url);
365 free(attr->raw_override_data_url);
366
367 /* Populate the object */
368 attr->override_attr->data_url = url_str;
369 attr->raw_override_data_url = raw_str;
370
371 /* Ownership passed to attr. */
372 url_str = NULL;
373 raw_str = NULL;
374 end:
375 free(raw_str);
376 free(url_str);
377 free(uri);
378 return ret;
379 }
380
381 int lttng_load_session_attr_set_override_url(
382 struct lttng_load_session_attr *attr, const char *url)
383 {
384 int ret = 0;
385 ssize_t ret_size;
386 struct lttng_uri *uri = NULL;
387 char *raw_url_str = NULL;
388 char *raw_path_str = NULL;
389 char *path_str = NULL;
390 char *raw_ctrl_str = NULL;
391 char *ctrl_str = NULL;
392 char *raw_data_str = NULL;
393 char *data_str = NULL;
394 char buffer[PATH_MAX];
395
396 if (!attr || !url || strlen(url) >= PATH_MAX) {
397 ret = -LTTNG_ERR_INVALID;
398 goto end;
399 }
400
401 if (!attr->override_attr) {
402 attr->override_attr = zmalloc(
403 sizeof(struct config_load_session_override_attr));
404 if (!attr->override_attr) {
405 ret = -LTTNG_ERR_NOMEM;
406 goto end;
407 }
408 }
409
410 /*
411 * FIXME: uri_parse should be able to take as parameter the protocol
412 * type to validate "url". For now only check the parsing goes through;
413 * it will fail later on.
414 */
415 ret_size = uri_parse_str_urls(url, NULL, &uri);
416 if (ret_size < 0 || ret_size > 2) {
417 /* Unexpected URL format. */
418 ret = -LTTNG_ERR_INVALID;
419 goto end;
420 }
421
422 raw_url_str = lttng_strndup(url, PATH_MAX);
423 if (!raw_url_str) {
424 ret = -LTTNG_ERR_NOMEM;
425 goto end;
426 }
427
428 /* Get path | ctrl && data string URL. */
429 ret = uri_to_str_url(&uri[0], buffer, sizeof(buffer));
430 if (ret < 0 || ret >= PATH_MAX) {
431 ret = -LTTNG_ERR_INVALID;
432 goto end;
433 }
434 ret = 0;
435
436 switch (uri[0].dtype) {
437 case LTTNG_DST_PATH:
438 raw_path_str = lttng_strndup(buffer, PATH_MAX);
439 if (!raw_path_str) {
440 ret = -LTTNG_ERR_NOMEM;
441 goto end;
442 }
443
444 path_str = lttng_strndup(raw_path_str, PATH_MAX);
445 if (!path_str) {
446 ret = -LTTNG_ERR_NOMEM;
447 goto end;
448 }
449 break;
450 case LTTNG_DST_IPV4:
451 case LTTNG_DST_IPV6:
452 if (ret_size != 2) {
453 ret = -LTTNG_ERR_INVALID;
454 goto end;
455 }
456
457 raw_ctrl_str = lttng_strndup(buffer, PATH_MAX);
458 if (!raw_ctrl_str) {
459 ret = -LTTNG_ERR_NOMEM;
460 goto end;
461 }
462
463 ctrl_str = lttng_strndup(raw_ctrl_str, PATH_MAX);
464 if (!ctrl_str) {
465 ret = -LTTNG_ERR_NOMEM;
466 goto end;
467 }
468
469 /* Get the data uri. */
470 ret = uri_to_str_url(&uri[1], buffer, sizeof(buffer));
471 if (ret < 0) {
472 ret = -LTTNG_ERR_INVALID;
473 goto end;
474 }
475 ret = 0;
476
477 raw_data_str = lttng_strndup(buffer, PATH_MAX);
478 if (!raw_data_str) {
479 ret = -LTTNG_ERR_NOMEM;
480 goto end;
481 }
482
483 data_str = lttng_strndup(raw_data_str, PATH_MAX);
484 if (!data_str) {
485 ret = -LTTNG_ERR_NOMEM;
486 goto end;
487 }
488
489 break;
490 default:
491 ret = -LTTNG_ERR_INVALID;
492 goto end;
493 }
494
495 reset_load_session_attr_urls(attr);
496
497 attr->override_attr->path_url = path_str;
498 attr->override_attr->ctrl_url = ctrl_str;
499 attr->override_attr->data_url = data_str;
500
501 attr->raw_override_url = raw_url_str;
502 attr->raw_override_path_url = raw_path_str;
503 attr->raw_override_ctrl_url = raw_ctrl_str;
504 attr->raw_override_data_url = raw_data_str;
505
506 /* Pass data ownership to attr. */
507 raw_url_str = NULL;
508 raw_path_str = NULL;
509 path_str = NULL;
510 raw_ctrl_str = NULL;
511 ctrl_str = NULL;
512 raw_data_str = NULL;
513 data_str = NULL;
514
515 end:
516 free(raw_path_str);
517 free(path_str);
518 free(raw_ctrl_str);
519 free(ctrl_str);
520 free(raw_data_str);
521 free(data_str);
522 free(raw_url_str);
523 free(uri);
524 return ret;
525 }
526
527 int lttng_load_session(struct lttng_load_session_attr *attr)
528 {
529 int ret;
530 const char *url, *session_name;
531
532 if (!attr) {
533 ret = -LTTNG_ERR_INVALID;
534 goto end;
535 }
536
537 url = attr->input_url[0] != '\0' ? attr->input_url : NULL;
538 session_name = attr->session_name[0] != '\0' ?
539 attr->session_name : NULL;
540
541 ret = config_load_session(url, session_name, attr->overwrite, 0,
542 attr->override_attr);
543
544 end:
545 return ret;
546 }
This page took 0.530286 seconds and 5 git commands to generate.