Fix: check return value of config_document_replace_element
[lttng-tools.git] / src / bin / lttng / commands / create.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <ctype.h>
21 #include <popt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include <signal.h>
30 #include <sys/wait.h>
31
32 #include <common/mi-lttng.h>
33 #include <common/config/session-config.h>
34
35 #include "../command.h"
36 #include "../utils.h"
37
38 #include <common/defaults.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/uri.h>
41 #include <common/utils.h>
42 #include <lttng/snapshot.h>
43
44 static char *opt_output_path;
45 static char *opt_session_name;
46 static char *opt_url;
47 static char *opt_ctrl_url;
48 static char *opt_data_url;
49 static char *opt_shm_path;
50 static char *opt_template_path;
51 static int opt_no_consumer;
52 static int opt_no_output;
53 static int opt_snapshot;
54 static unsigned int opt_live_timer;
55
56 enum {
57 OPT_HELP = 1,
58 OPT_LIST_OPTIONS,
59 OPT_LIVE_TIMER,
60 };
61
62 enum {
63 OUTPUT_UNKNOWN = -1,
64 OUTPUT_NONE,
65 OUTPUT_LOCAL,
66 OUTPUT_NET,
67 };
68 enum {
69 SESSION_UNKNOWN = -1,
70 SESSION_NORMAL,
71 SESSION_LIVE,
72 SESSION_SNAPSHOT,
73 };
74
75
76 static struct mi_writer *writer;
77
78 static struct poptOption long_options[] = {
79 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
80 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
81 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
82 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
83 {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0},
84 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
85 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
86 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
87 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
88 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
89 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
90 {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
91 {"template-path", 0, POPT_ARG_STRING, &opt_template_path, 0, 0, 0},
92 {0, 0, 0, 0, 0, 0, 0}
93 };
94
95 /*
96 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
97 * why this declaration exists and used ONLY in for this command.
98 */
99 extern int _lttng_create_session_ext(const char *name, const char *url,
100 const char *datetime);
101
102 /*
103 * Retrieve the created session and mi output it based on provided argument
104 * This is currently a summary of what was pretty printed and is subject to
105 * enhancements.
106 */
107 static int mi_created_session(const char *session_name)
108 {
109 int ret, i, count, found;
110 struct lttng_session *sessions;
111
112 /* session_name should not be null */
113 assert(session_name);
114 assert(writer);
115
116 count = lttng_list_sessions(&sessions);
117 if (count < 0) {
118 ret = count;
119 ERR("%s", lttng_strerror(ret));
120 goto error;
121 }
122
123 if (count == 0) {
124 ERR("Error session creation failed: session %s not found", session_name);
125 ret = -LTTNG_ERR_SESS_NOT_FOUND;
126 goto end;
127 }
128
129 found = 0;
130 for (i = 0; i < count; i++) {
131 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
132 found = 1;
133 ret = mi_lttng_session(writer, &sessions[i], 0);
134 if (ret) {
135 goto error;
136 }
137 break;
138 }
139 }
140
141 if (!found) {
142 ret = -LTTNG_ERR_SESS_NOT_FOUND;
143 } else {
144 ret = CMD_SUCCESS;
145 }
146
147 error:
148 free(sessions);
149 end:
150 return ret;
151 }
152
153 /*
154 * For a session name, set the consumer URLs.
155 */
156 static int set_consumer_url(const char *session_name, const char *ctrl_url,
157 const char *data_url)
158 {
159 int ret;
160 struct lttng_handle *handle;
161
162 assert(session_name);
163
164 /*
165 * Set handle with the session_name, but no domain. This implies that
166 * the actions taken with this handle apply on the tracing session
167 * rather then the domain-specific session.
168 */
169 handle = lttng_create_handle(session_name, NULL);
170 if (handle == NULL) {
171 ret = CMD_FATAL;
172 goto error;
173 }
174
175 ret = lttng_set_consumer_url(handle, ctrl_url, data_url);
176 if (ret < 0) {
177 goto error;
178 }
179
180 error:
181 lttng_destroy_handle(handle);
182 return ret;
183 }
184
185 static int add_snapshot_output(const char *session_name, const char *ctrl_url,
186 const char *data_url)
187 {
188 int ret;
189 struct lttng_snapshot_output *output = NULL;
190
191 assert(session_name);
192
193 output = lttng_snapshot_output_create();
194 if (!output) {
195 ret = CMD_FATAL;
196 goto error_create;
197 }
198
199 if (ctrl_url) {
200 ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output);
201 if (ret < 0) {
202 goto error;
203 }
204 }
205
206 if (data_url) {
207 ret = lttng_snapshot_output_set_data_url(data_url, output);
208 if (ret < 0) {
209 goto error;
210 }
211 }
212
213 /* This call, if successful, populates the id of the output object. */
214 ret = lttng_snapshot_add_output(session_name, output);
215 if (ret < 0) {
216 goto error;
217 }
218
219 error:
220 lttng_snapshot_output_destroy(output);
221 error_create:
222 return ret;
223 }
224
225 /*
226 * Validate the combinations of passed options
227 *
228 * CMD_ERROR on error
229 * CMD_SUCCESS on success
230 */
231 static int validate_command_options(int session_type)
232 {
233 int ret = CMD_SUCCESS;
234
235 if (opt_snapshot && opt_live_timer) {
236 ERR("Snapshot and live modes are mutually exclusive.");
237 ret = CMD_ERROR;
238 goto error;
239 }
240
241 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
242 ERR("Control and data URLs must both be set.");
243 ret = CMD_ERROR;
244 goto error;
245 }
246
247 if (opt_template_path) {
248 /* Restriction on flags exist when using template */
249 /* Session type flags are not permitted */
250 /* --live & --snapshot */
251 if ((opt_live_timer && session_type != SESSION_LIVE) ||
252 (opt_snapshot && session_type != SESSION_SNAPSHOT)) {
253 ERR("It is not possible to change the session type of a template");
254 ret = CMD_ERROR;
255 goto error;
256 }
257 }
258
259 error:
260 return ret;
261 }
262
263 /*
264 * Create a session using direct calls to liblttng-ctl.
265 *
266 * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
267 * value on command errors.
268 */
269 static int create_session_basic (const char *session_name,
270 int session_type,
271 int live_timer,
272 int output_type,
273 const char* url,
274 const char* ctrl_url,
275 const char* data_url,
276 const char* shm_path,
277 const char* datetime)
278 {
279 /* Create session based on session creation */
280 int ret = CMD_SUCCESS;
281 const char *pathname;
282
283 assert(datetime);
284
285 if (opt_relayd_path) {
286 pathname = opt_relayd_path;
287 } else {
288 pathname = INSTALL_BIN_PATH "/lttng-relayd";
289 }
290
291 switch (session_type) {
292 case SESSION_NORMAL:
293 ret = _lttng_create_session_ext(session_name, url, datetime);
294 break;
295 case SESSION_SNAPSHOT:
296 if (output_type == OUTPUT_NONE) {
297 ERR("--no-output on a snapshot session is invalid");
298 ret = CMD_UNSUPPORTED;
299 goto error;
300 }
301 ret = lttng_create_session_snapshot(session_name, url);
302 break;
303 case SESSION_LIVE:
304 if (output_type == OUTPUT_NONE) {
305 ERR("--no-output on a live session is invalid");
306 ret = CMD_UNSUPPORTED;
307 goto error;
308 }
309
310 if (output_type == OUTPUT_LOCAL) {
311 ERR("Local file output on a live session is invalid");
312 ret = CMD_UNSUPPORTED;
313 goto error;
314 }
315 if (output_type != OUTPUT_NET && !check_relayd() &&
316 spawn_relayd(pathname, 0) < 0) {
317 ret = CMD_FATAL;
318 goto error;
319 }
320 ret = lttng_create_session_live(session_name, url, live_timer);
321 break;
322 default:
323 ERR("Unknown session type");
324 ret = CMD_UNDEFINED;
325 goto error;
326 }
327
328 if (ret < 0) {
329 /* Don't set ret so the sessiond error is propagated. */
330 switch (-ret) {
331 case LTTNG_ERR_EXIST_SESS:
332 WARN("Session %s already exists", session_name);
333 break;
334 default:
335 break;
336 }
337 goto error;
338 }
339
340 /* Configure the session based on the output type */
341 switch (output_type) {
342 case OUTPUT_LOCAL:
343 break;
344 case OUTPUT_NET:
345 if (!ctrl_url || !data_url) {
346 break;
347 }
348
349 if (session_type == SESSION_SNAPSHOT) {
350 ret = add_snapshot_output(session_name, ctrl_url,
351 data_url);
352 } else {
353 /*
354 * Normal sessions and live sessions behave the same way
355 * regarding consumer url.
356 */
357 ret = set_consumer_url(session_name, ctrl_url, data_url);
358 }
359 if (ret < 0) {
360 /* Destroy created session on errors */
361 lttng_destroy_session(session_name);
362 goto error;
363 }
364 break;
365 case OUTPUT_NONE:
366 break;
367 default:
368 ERR("Unknown output type");
369 ret = CMD_UNDEFINED;
370 goto error;
371 }
372
373 /*
374 * Set the session shared memory path
375 */
376 if (shm_path) {
377 ret = lttng_set_session_shm_path(session_name, shm_path);
378 if (ret < 0) {
379 lttng_destroy_session(session_name);
380 goto error;
381 }
382 }
383 error:
384 return ret;
385 }
386
387 static int generate_output(const char *session_name,
388 int session_type,
389 int live_timer,
390 int output_type,
391 const char* url,
392 const char* ctrl_url,
393 const char* data_url,
394 const char* shm_path)
395 {
396 int ret = CMD_SUCCESS;
397
398 /*
399 * TODO move this to after session name
400 * for now we only emulate previous behaviour.
401 */
402 if (session_type != SESSION_SNAPSHOT) {
403 if (ctrl_url) {
404 MSG("Control URL %s set for session %s", ctrl_url, session_name);
405 }
406
407 if (data_url) {
408 MSG("Data URL %s set for session %s", data_url, session_name);
409 }
410 }
411
412 if (url && output_type == OUTPUT_LOCAL) {
413 /* Remove the file:// */
414 if (strlen(url) > strlen("file://")){
415 url = url + strlen("file://");
416 }
417 }
418
419 MSG("Session %s created.", session_name);
420 if (url && session_type != SESSION_SNAPSHOT) {
421 MSG("Traces will be written in %s", url);
422
423 if (live_timer) {
424 MSG("Live timer set to %u usec", live_timer);
425 }
426 } else if (session_type == SESSION_SNAPSHOT) {
427 if (url) {
428 MSG("Default snapshot output set to: %s", url);
429 }
430 MSG("Snapshot mode set. Every channel enabled for that session will "
431 "be set to mmap output, and default to overwrite mode.");
432 }
433
434 if (shm_path) {
435 MSG("Session %s set to shm_path: %s.", session_name,
436 shm_path);
437 }
438
439 /* Mi output */
440 if (lttng_opt_mi) {
441 ret = mi_created_session(session_name);
442 if (ret) {
443 ret = CMD_ERROR;
444 goto error;
445 }
446 }
447 error:
448 return ret;
449 }
450
451 static int parse_template (struct config_document *template,
452 char **session_name,
453 int *session_type,
454 int *live_timer,
455 int *output_type,
456 char **url,
457 char **ctrl_url,
458 char **data_url,
459 char **shm_path)
460 {
461 int ret = 0;
462 char *raw_value = NULL;
463
464 assert(template);
465
466 /* Session name */
467 *session_name = config_document_get_element_value(template,"/sessions/session/name");
468
469 /* Check the type of session we have in the template */
470 if(config_document_element_exist(template, "/sessions/session/attributes/snapshot_mode")) {
471 *session_type = SESSION_SNAPSHOT;
472 } else if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
473 *session_type = SESSION_LIVE;
474 raw_value = config_document_get_element_value(template,"/sessions/session/attributes/live_timer_interval");
475 *live_timer = config_parse_value(raw_value);
476 free(raw_value);
477 raw_value = NULL;
478 } else {
479 *session_type = SESSION_NORMAL;
480 }
481
482 /* Output */
483 switch (*session_type) {
484 case SESSION_NORMAL:
485 case SESSION_LIVE:
486 if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")){
487 break;
488 }
489 if (config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/path")){
490 raw_value = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/path");
491 if (!raw_value) {
492 ret = -1;
493 goto error;
494 }
495
496 if (strlen(raw_value) > 0) {
497 *output_type = OUTPUT_LOCAL;
498 ret = asprintf(url, "file://%s", raw_value);
499 if (ret < 0) {
500 ret = -1;
501 goto error;
502 }
503 } else {
504 *output_type = OUTPUT_NONE;
505 }
506
507 free(raw_value);
508 raw_value = NULL;
509 break;
510 } else if(config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/net_output")) {
511 *ctrl_url = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/control_uri");
512 *data_url = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/data_uri");
513 if (!*ctrl_url || ! *data_url) {
514 ret = -1;
515 goto error;
516 }
517 *output_type = OUTPUT_NET;
518 } else {
519 /* There is no output definition */
520 }
521 break;
522 case SESSION_SNAPSHOT:
523 if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")){
524 break;
525 }
526 if (config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path")){
527 raw_value = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path");
528 if (!raw_value) {
529 ret = -1;
530 goto error;
531 }
532
533 if (strlen(raw_value) > 0) {
534 *output_type = OUTPUT_LOCAL;
535 ret = asprintf(url, "file://%s", raw_value);
536 if (ret < 0) {
537 ret = -1;
538 goto error;
539 }
540 } else {
541 *output_type = OUTPUT_NONE;
542 }
543
544 free(raw_value);
545 raw_value = NULL;
546 break;
547 } else if(config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output")) {
548 *ctrl_url = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/control_uri");
549 *data_url = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/data_uri");
550 if (!*ctrl_url || ! *data_url) {
551 ret = -1;
552 goto error;
553 }
554 *output_type = OUTPUT_NET;
555 } else {
556 /* There is no output definition */
557 }
558 break;
559 }
560
561
562 /* shared memory path */
563 *shm_path = config_document_get_element_value(template,"/sessions/session/shared_memory_path");
564
565 error:
566 free(raw_value);
567 return ret;
568
569 }
570 static int create_session_from_template(struct config_document *template,
571 const char *session_name,
572 int session_type,
573 int live_timer,
574 int output_type,
575 const char *url,
576 const char *ctrl_url,
577 const char *data_url,
578 const char *shm_path,
579 const char *datetime)
580 {
581 int ret = CMD_SUCCESS;
582 struct config_element *temp_element = NULL;
583 struct config_element *temp_element_child = NULL;
584 char tmp_ctrl_uri[PATH_MAX];
585 char tmp_data_uri[PATH_MAX];
586 struct lttng_uri *uris = NULL;
587 ssize_t uri_array_size = 0;
588 char *tmp_string = NULL;
589
590 assert(template);
591 assert(session_name);
592
593 memset(tmp_ctrl_uri, 0, sizeof(tmp_ctrl_uri));
594 memset(tmp_data_uri, 0, sizeof(tmp_data_uri));
595
596 /* Session name */
597 if (config_document_element_exist(template, "/sessions/session/name")) {
598 /* Replace the node value */
599 config_document_replace_element_value(template, "/sessions/session/name", session_name);
600 } else {
601 /* insert the node */
602 temp_element = config_element_create("name", session_name);
603 if (!temp_element) {
604 ERR("Could not create session name node configuration");
605 ret = CMD_ERROR;
606 goto error;
607 }
608 ret = config_document_insert_element(template, "/sessions/session", temp_element);
609 if (ret) {
610 ERR("Could not insert session name node configuration");
611 ret = CMD_ERROR;
612 goto error;
613 }
614 config_element_free(temp_element);
615 }
616
617 /*
618 * Live timer
619 */
620 if (session_type == SESSION_LIVE) {
621 if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
622 asprintf(&tmp_string, "%d", live_timer);
623 config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string);
624 free(tmp_string);
625 tmp_string = NULL;
626 } else {
627 ERR("Invalid live timer template. Missing live timer node");
628 ret = CMD_ERROR;
629 goto error;
630 }
631
632 }
633
634 /*
635 * Generate the output node
636 */
637
638 /* Get output from urls */
639 if (url) {
640 /* Get lttng uris from single url */
641 uri_array_size = uri_parse_str_urls(url, NULL, &uris);
642 if (uri_array_size < 0) {
643 ret = CMD_ERROR;
644 goto error;
645 }
646 } else if (ctrl_url && data_url) {
647 uri_array_size = uri_parse_str_urls(ctrl_url, data_url, &uris);
648 if (uri_array_size < 0) {
649 ret = CMD_ERROR;
650 goto error;
651 }
652 } else {
653 /* --no-output */
654 uri_array_size = 0;
655 }
656
657 /* Validate if the session output type still match the passed data */
658 if ( (uri_array_size == 0 && output_type != OUTPUT_NONE) ||
659 (uri_array_size == 1 && output_type != OUTPUT_LOCAL) ||
660 (uri_array_size == 2 && output_type != OUTPUT_NET)) {
661 ERR("Overwriting value for output do not match the base output type");
662 ret = CMD_ERROR;
663 goto error;
664 }
665
666 switch (output_type) {
667 case OUTPUT_NONE:
668 temp_element_child = config_element_create("path", "");
669 if (!temp_element_child) {
670 ERR("Could not create empty path node configuration");
671 ret = CMD_ERROR;
672 goto error;
673 }
674 break;
675 case OUTPUT_LOCAL:
676 temp_element_child = config_element_create("path", uris[0].dst.path);
677 if (!temp_element_child) {
678 ERR("Could not create local path node configuration");
679 ret = CMD_ERROR;
680 goto error;
681 }
682 break;
683 case OUTPUT_NET:
684 uri_to_str_url(&uris[0], tmp_ctrl_uri, sizeof(tmp_ctrl_uri));
685 uri_to_str_url(&uris[1], tmp_data_uri, sizeof(tmp_data_uri));
686
687 temp_element_child = config_element_create("net_output", NULL);
688 if (!temp_element_child) {
689 ERR("Could not create net_output node configuration");
690 ret = CMD_ERROR;
691 goto error;
692 }
693
694 temp_element = config_element_create("control_uri", tmp_ctrl_uri);
695 if (!temp_element_child) {
696 ERR("Could not create ctrl uri node configuration");
697 ret = CMD_ERROR;
698 goto error;
699 }
700
701 ret = config_element_add_child(temp_element_child, temp_element);
702 if (ret) {
703 ERR("Could not append control uri to the net_output node configuration");
704 ret = CMD_ERROR;
705 goto error;
706 }
707 config_element_free(temp_element);
708
709 temp_element = config_element_create("data_uri", tmp_data_uri);
710
711 if (!temp_element_child) {
712 ERR("Could not create data_uri configuration");
713 ret = CMD_ERROR;
714 goto error;
715 }
716
717 ret = config_element_add_child(temp_element_child, temp_element);
718 if (ret) {
719 ERR("Could not append data uri to the net_output node configuration");
720 ret = CMD_ERROR;
721 goto error;
722 }
723 config_element_free(temp_element);
724 break;
725 default:
726 ret = CMD_ERROR;
727 goto error;
728 }
729
730 temp_element = config_element_create("destination", NULL);
731 if (!temp_element) {
732 ERR("Could not create destination node configuration");
733 ret = CMD_ERROR;
734 goto error;
735 }
736
737 ret = config_element_add_child(temp_element, temp_element_child);
738 if (ret) {
739 ERR("Could not append output data to the destination node configuration");
740 ret = CMD_ERROR;
741 goto error;
742 }
743
744 /*
745 * validate and replace the destination node for each session type
746 * TODO: export string as const and simply assign a base path for the
747 * destination node based on the session type
748 **/
749 switch (session_type) {
750 case SESSION_NORMAL:
751 case SESSION_LIVE:
752 if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")) {
753 ERR("Invalid template no destination node configuration present");
754 ret = CMD_ERROR;
755 goto error;
756 }
757
758 ret = config_document_replace_element(template, "/sessions/session/output/consumer_output/destination", temp_element);
759 break;
760 case SESSION_SNAPSHOT:
761 if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")) {
762 ERR("Invalid template no destination node configuration present");
763 ret = CMD_ERROR;
764 goto error;
765 }
766
767 ret = config_document_replace_element(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination", temp_element);
768 break;
769 default:
770 ERR("Invalid session type");
771 ret = CMD_UNDEFINED;
772 goto error;
773 }
774
775
776 if (ret) {
777 ERR("%s", lttng_strerror(ret));
778 ret = CMD_ERROR;
779 goto error;
780 }
781
782
783 /* Shm path */
784 if (shm_path && config_document_element_exist(template, "/sessions/session/shared_memory_path")) {
785 /* Replace the node value */
786 config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path);
787 } else if (shm_path) {
788 /* insert the node */
789 temp_element = config_element_create("shared_memory_path", shm_path);
790 if (!temp_element) {
791 ERR("Could not create shared_memory_path node configuration");
792 ret = CMD_ERROR;
793 goto error;
794 }
795 ret = config_document_insert_element(template, "/sessions/session", temp_element);
796 if (ret) {
797 ERR("Could not insert shared_memory_path node configuration");
798 ret = CMD_ERROR;
799 goto error;
800 }
801 }
802
803 ret = config_load_configuration_sessions(template, session_name, 0);
804
805
806 error:
807 config_element_free(temp_element);
808 config_element_free(temp_element_child);
809 free(tmp_string);
810 free(uris);
811 return ret;
812
813 }
814
815 /*
816 * Create a tracing session.
817 * If no name is specified, a default name is generated.
818 *
819 * Returns one of the CMD_* result constants.
820 */
821 static int create_session(void)
822 {
823 int ret;
824
825
826 /* Template */
827 struct config_document *template = NULL;
828
829 /* Base data */
830 int base_session_type = SESSION_UNKNOWN;
831 int base_output_type = OUTPUT_UNKNOWN;
832 char *base_session_name = NULL;
833 char *base_url = NULL;
834 char *base_ctrl_url = NULL;
835 char *base_data_url = NULL;
836 char *base_shm_path = NULL;
837 int base_live_timer = 0;
838
839 /* Time data */
840 char datetime[16];
841 time_t rawtime;
842 struct tm *timeinfo = NULL;
843
844 /* Temporary variables */
845 char *traces_path = NULL;
846 char *temp_url = NULL;
847 char *session_name_date = NULL;
848 char *tmp_url = NULL;
849 char *tmp_home_path = NULL;
850 struct lttng_uri *uris = NULL;
851 ssize_t uri_array_size = 0;
852
853
854 /* Get date and time for automatic session name/path */
855 time(&rawtime);
856 timeinfo = localtime(&rawtime);
857 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
858
859 if (opt_template_path) {
860 /* Restriction on flags exist when using template */
861 /* Session type flags are not permitted */
862 /* --live & --snapshot */
863 template = config_document_get(opt_template_path);
864 if (!template) {
865 ERR("Template could not be parsed");
866 ret = CMD_ERROR;
867 goto error;
868 }
869 /* Load info from template if any */
870 /* TODO: might want to use a struct in the end for the session... */
871 ret = parse_template(template, &base_session_name,
872 &base_session_type,
873 &base_live_timer,
874 &base_output_type,
875 &base_url,
876 &base_ctrl_url,
877 &base_data_url,
878 &base_shm_path);
879 }
880
881 /* Option validation */
882 if (validate_command_options(base_session_type) != CMD_SUCCESS) {
883 ret = CMD_ERROR;
884 goto error;
885 }
886
887 /* Find the session type based on options */
888 if(base_session_type == SESSION_UNKNOWN) {
889 if (opt_snapshot) {
890 base_session_type = SESSION_SNAPSHOT;
891 } else if (opt_live_timer) {
892 base_session_type = SESSION_LIVE;
893 } else {
894 base_session_type = SESSION_NORMAL;
895 }
896 }
897
898 /*
899 * Session name handling
900 */
901 if (opt_session_name) {
902 /* Override the session name */
903 if (strlen(opt_session_name) > NAME_MAX) {
904 ERR("Session name too long. Length must be lower or equal to %d",
905 NAME_MAX);
906 ret = LTTNG_ERR_SESSION_FAIL;
907 free(session_name_date);
908 goto error;
909 }
910 /*
911 * Check if the session name begins with "auto-" or is exactly "auto".
912 * Both are reserved for the default session name. See bug #449 to
913 * understand why we need to check both here.
914 */
915 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
916 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
917 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
918 strlen(DEFAULT_SESSION_NAME)) == 0 &&
919 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
920 ERR("%s is a reserved keyword for default session(s)",
921 DEFAULT_SESSION_NAME);
922
923 ret = CMD_ERROR;
924 goto error;
925 }
926
927 base_session_name = strndup(opt_session_name, NAME_MAX);
928 if (!base_session_name) {
929 PERROR("Strdup session name");
930 ret = CMD_ERROR;
931 goto error;
932 }
933
934 ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
935 if (ret < 0) {
936 PERROR("Asprintf session name");
937 goto error;
938 }
939 DBG("Session name from command option set to %s", base_session_name);
940 } else if (base_session_name) {
941 ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
942 if (ret < 0) {
943 PERROR("Asprintf session name");
944 goto error;
945 }
946 } else {
947 /* Generate a name */
948 ret = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
949 if (ret < 0) {
950 PERROR("Asprintf session name");
951 goto error;
952 }
953 session_name_date = strdup(base_session_name);
954 DBG("Auto session name set to %s", base_session_name);
955 }
956
957
958 /*
959 * Output handling
960 */
961
962 /*
963 * If any of those options are present clear all output related data.
964 */
965 if (opt_output_path || opt_url || (opt_ctrl_url && opt_data_url) || opt_no_output) {
966 /* Overwrite output */
967 free(base_url);
968 free(base_ctrl_url);
969 free(base_data_url);
970 base_url = NULL;
971 base_ctrl_url = NULL;
972 base_data_url = NULL;
973 }
974
975 if (opt_output_path) {
976
977 traces_path = utils_expand_path(opt_output_path);
978 if (!traces_path) {
979 ret = CMD_ERROR;
980 goto error;
981 }
982
983 /* Create URL string from the local file system path */
984 ret = asprintf(&temp_url, "file://%s", traces_path);
985 if (ret < 0) {
986 PERROR("asprintf url path");
987 ret = CMD_FATAL;
988 goto error;
989 }
990
991 base_url = temp_url;
992 } else if (opt_url) { /* Handling URL (-U opt) */
993 base_url = strdup(opt_url);
994 } else if (opt_data_url && opt_ctrl_url) {
995 /*
996 * With both control and data, we'll be setting the consumer URL
997 * after session creation thus use no URL.
998 */
999 base_ctrl_url = strdup(opt_ctrl_url);
1000 base_data_url = strdup(opt_data_url);
1001 } else if (!(opt_no_output || base_output_type == OUTPUT_NONE ||
1002 base_url || base_ctrl_url || base_data_url)) {
1003 /* Generate default output depending on the session type */
1004 switch (base_session_type) {
1005 case SESSION_NORMAL:
1006 /* fallthrough */
1007 case SESSION_SNAPSHOT:
1008 /* Default to a local path */
1009 tmp_home_path = utils_get_home_dir();
1010 if (tmp_home_path == NULL) {
1011 ERR("HOME path not found.\n \
1012 Please specify an output path using -o, --output PATH");
1013 ret = CMD_FATAL;
1014 goto error;
1015 }
1016
1017 ret = asprintf(&tmp_url,
1018 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
1019 tmp_home_path, session_name_date);
1020
1021 if (ret < 0) {
1022 PERROR("asprintf trace dir name");
1023 ret = CMD_FATAL;
1024 goto error;
1025 }
1026
1027 base_url = tmp_url ;
1028 break;
1029 case SESSION_LIVE:
1030 /* Default to a net output */
1031 ret = asprintf(&tmp_url, "net://127.0.0.1");
1032 if (ret < 0) {
1033 PERROR("asprintf default live URL");
1034 ret = CMD_FATAL;
1035 goto error;
1036 }
1037 base_url = tmp_url ;
1038 break;
1039 default:
1040 ERR("Unknown session type");
1041 ret = CMD_FATAL;
1042 goto error;
1043 }
1044 }
1045
1046 /*
1047 * Shared memory path handling
1048 */
1049 if (opt_shm_path) {
1050 ret = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
1051 if (ret < 0) {
1052 PERROR("asprintf shm_path");
1053 goto error;
1054 }
1055 }
1056
1057 /*
1058 * Live timer handling
1059 */
1060 if (opt_live_timer) {
1061 base_live_timer = opt_live_timer;
1062 }
1063
1064 /* Get output type from urls */
1065 if (base_url) {
1066 /* Get lttng uris from single url */
1067 uri_array_size = uri_parse_str_urls(base_url, NULL, &uris);
1068 if (uri_array_size < 0) {
1069 ret = CMD_ERROR;
1070 goto error;
1071 }
1072 } else if (base_ctrl_url && base_data_url) {
1073 uri_array_size = uri_parse_str_urls(base_ctrl_url, base_data_url, &uris);
1074 if (uri_array_size < 0) {
1075 ret = CMD_ERROR;
1076 goto error;
1077 }
1078 } else {
1079 /* --no-output */
1080 uri_array_size = 0;
1081 }
1082
1083 switch (uri_array_size) {
1084 case 0:
1085 base_output_type = OUTPUT_NONE;
1086 break;
1087 case 1:
1088 base_output_type = OUTPUT_LOCAL;
1089 break;
1090 case 2:
1091 base_output_type = OUTPUT_NET;
1092 break;
1093 default:
1094 ret = CMD_ERROR;
1095 goto error;
1096 }
1097
1098 if (template) {
1099 ret = create_session_from_template(template,
1100 base_session_name,
1101 base_session_type,
1102 base_live_timer,
1103 base_output_type,
1104 base_url,
1105 base_ctrl_url,
1106 base_data_url,
1107 base_shm_path,
1108 datetime);
1109 } else {
1110 ret = create_session_basic (base_session_name,
1111 base_session_type,
1112 base_live_timer,
1113 base_output_type,
1114 base_url,
1115 base_ctrl_url,
1116 base_data_url,
1117 base_shm_path,
1118 datetime);
1119 }
1120 if (ret) {
1121 goto error;
1122 }
1123
1124 ret = generate_output (base_session_name,
1125 base_session_type,
1126 base_live_timer,
1127 base_output_type,
1128 base_url,
1129 base_ctrl_url,
1130 base_data_url,
1131 base_shm_path);
1132 if (ret) {
1133 goto error;
1134 }
1135
1136 /* Init lttng session config */
1137 ret = config_init(base_session_name);
1138 if (ret < 0) {
1139 ret = CMD_ERROR;
1140 goto error;
1141 }
1142
1143 ret = CMD_SUCCESS;
1144
1145 error:
1146 /* Session temp stuff */
1147 free(session_name_date);
1148 free(uris);
1149
1150 if (ret < 0) {
1151 ERR("%s", lttng_strerror(ret));
1152 }
1153
1154 free(base_session_name);
1155 free(base_url);
1156 free(base_ctrl_url);
1157 free(base_data_url);
1158 free(base_shm_path);
1159 return ret;
1160 }
1161
1162 /*
1163 * spawn_sessiond
1164 *
1165 * Spawn a session daemon by forking and execv.
1166 */
1167 static int spawn_sessiond(char *pathname)
1168 {
1169 int ret = 0;
1170 pid_t pid;
1171
1172 MSG("Spawning a session daemon");
1173 pid = fork();
1174 if (pid == 0) {
1175 /*
1176 * Spawn session daemon in daemon mode.
1177 */
1178 execlp(pathname, "lttng-sessiond",
1179 "--daemonize", NULL);
1180 /* execlp only returns if error happened */
1181 if (errno == ENOENT) {
1182 ERR("No session daemon found. Use --sessiond-path.");
1183 } else {
1184 PERROR("execlp");
1185 }
1186 kill(getppid(), SIGTERM); /* wake parent */
1187 exit(EXIT_FAILURE);
1188 } else if (pid > 0) {
1189 /*
1190 * In daemon mode (--daemonize), sessiond only exits when
1191 * it's ready to accept commands.
1192 */
1193 for (;;) {
1194 int status;
1195 pid_t wait_pid_ret = waitpid(pid, &status, 0);
1196
1197 if (wait_pid_ret < 0) {
1198 if (errno == EINTR) {
1199 continue;
1200 }
1201 PERROR("waitpid");
1202 ret = -errno;
1203 goto end;
1204 }
1205
1206 if (WIFSIGNALED(status)) {
1207 ERR("Session daemon was killed by signal %d",
1208 WTERMSIG(status));
1209 ret = -1;
1210 goto end;
1211 } else if (WIFEXITED(status)) {
1212 DBG("Session daemon terminated normally (exit status: %d)",
1213 WEXITSTATUS(status));
1214
1215 if (WEXITSTATUS(status) != 0) {
1216 ERR("Session daemon terminated with an error (exit status: %d)",
1217 WEXITSTATUS(status));
1218 ret = -1;
1219 goto end;
1220 }
1221 break;
1222 }
1223 }
1224
1225 goto end;
1226 } else {
1227 PERROR("fork");
1228 ret = -1;
1229 goto end;
1230 }
1231
1232 end:
1233 return ret;
1234 }
1235
1236 /*
1237 * launch_sessiond
1238 *
1239 * Check if the session daemon is available using
1240 * the liblttngctl API for the check. If not, try to
1241 * spawn a daemon.
1242 */
1243 static int launch_sessiond(void)
1244 {
1245 int ret;
1246 char *pathname = NULL;
1247
1248 ret = lttng_session_daemon_alive();
1249 if (ret) {
1250 /* Sessiond is alive, not an error */
1251 ret = 0;
1252 goto end;
1253 }
1254
1255 /* Try command line option path */
1256 pathname = opt_sessiond_path;
1257
1258 /* Try LTTNG_SESSIOND_PATH env variable */
1259 if (pathname == NULL) {
1260 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
1261 }
1262
1263 /* Try with configured path */
1264 if (pathname == NULL) {
1265 if (CONFIG_SESSIOND_BIN[0] != '\0') {
1266 pathname = CONFIG_SESSIOND_BIN;
1267 }
1268 }
1269
1270 /* Try the default path */
1271 if (pathname == NULL) {
1272 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
1273 }
1274
1275 DBG("Session daemon binary path: %s", pathname);
1276
1277 /* Check existence and permissions */
1278 ret = access(pathname, F_OK | X_OK);
1279 if (ret < 0) {
1280 ERR("No such file or access denied: %s", pathname);
1281 goto end;
1282 }
1283
1284 ret = spawn_sessiond(pathname);
1285 end:
1286 if (ret) {
1287 ERR("Problem occurred while launching session daemon (%s)",
1288 pathname);
1289 }
1290 return ret;
1291 }
1292
1293 /*
1294 * The 'create <options>' first level command
1295 *
1296 * Returns one of the CMD_* result constants.
1297 */
1298 int cmd_create(int argc, const char **argv)
1299 {
1300 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
1301 char *opt_arg = NULL;
1302 static poptContext pc;
1303
1304 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1305 poptReadDefaultConfig(pc, 0);
1306
1307 while ((opt = poptGetNextOpt(pc)) != -1) {
1308 switch (opt) {
1309 case OPT_HELP:
1310 SHOW_HELP();
1311 goto end;
1312 case OPT_LIST_OPTIONS:
1313 list_cmd_options(stdout, long_options);
1314 goto end;
1315 case OPT_LIVE_TIMER:
1316 {
1317 unsigned long v;
1318
1319 errno = 0;
1320 opt_arg = poptGetOptArg(pc);
1321 if (!opt_arg) {
1322 /* Set up default values. */
1323 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
1324 DBG("Session live timer interval set to default value %d",
1325 opt_live_timer);
1326 break;
1327 }
1328
1329 v = strtoul(opt_arg, NULL, 0);
1330 if (errno != 0 || !isdigit(opt_arg[0])) {
1331 ERR("Wrong value in --live parameter: %s", opt_arg);
1332 ret = CMD_ERROR;
1333 goto end;
1334 }
1335 if (v != (uint32_t) v) {
1336 ERR("32-bit overflow in --live parameter: %s", opt_arg);
1337 ret = CMD_ERROR;
1338 goto end;
1339 }
1340 if (v == 0) {
1341 ERR("Live timer interval must be greater than zero");
1342 ret = CMD_ERROR;
1343 goto end;
1344 }
1345 opt_live_timer = (uint32_t) v;
1346 DBG("Session live timer interval set to %d", opt_live_timer);
1347 break;
1348 }
1349 default:
1350 ret = CMD_UNDEFINED;
1351 goto end;
1352 }
1353 }
1354
1355 if (opt_no_consumer) {
1356 MSG("The option --no-consumer is obsolete. Use --no-output now.");
1357 ret = CMD_WARNING;
1358 goto end;
1359 }
1360
1361 /* Spawn a session daemon if needed */
1362 if (!opt_no_sessiond) {
1363 ret = launch_sessiond();
1364 if (ret) {
1365 ret = CMD_ERROR;
1366 goto end;
1367 }
1368 }
1369
1370 /* MI initialization */
1371 if (lttng_opt_mi) {
1372 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1373 if (!writer) {
1374 ret = -LTTNG_ERR_NOMEM;
1375 goto end;
1376 }
1377
1378 /* Open command element */
1379 ret = mi_lttng_writer_command_open(writer,
1380 mi_lttng_element_command_create);
1381 if (ret) {
1382 ret = CMD_ERROR;
1383 goto end;
1384 }
1385
1386 /* Open output element */
1387 ret = mi_lttng_writer_open_element(writer,
1388 mi_lttng_element_command_output);
1389 if (ret) {
1390 ret = CMD_ERROR;
1391 goto end;
1392 }
1393 }
1394 opt_session_name = (char*) poptGetArg(pc);
1395
1396 command_ret = create_session();
1397
1398 if (command_ret) {
1399 success = 0;
1400 }
1401
1402 if (lttng_opt_mi) {
1403 /* Close output element */
1404 ret = mi_lttng_writer_close_element(writer);
1405 if (ret) {
1406 ret = CMD_ERROR;
1407 goto end;
1408 }
1409
1410 /* Success ? */
1411 ret = mi_lttng_writer_write_element_bool(writer,
1412 mi_lttng_element_command_success, success);
1413 if (ret) {
1414 ret = CMD_ERROR;
1415 goto end;
1416 }
1417
1418 /* Command element close */
1419 ret = mi_lttng_writer_command_close(writer);
1420 if (ret) {
1421 ret = CMD_ERROR;
1422 goto end;
1423 }
1424 }
1425
1426 end:
1427 /* Mi clean-up */
1428 if (writer && mi_lttng_writer_destroy(writer)) {
1429 /* Preserve original error code */
1430 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
1431 }
1432
1433 /* Overwrite ret if an error occurred in create_session() */
1434 ret = command_ret ? command_ret : ret;
1435
1436 poptFreeContext(pc);
1437 return ret;
1438 }
This page took 0.059723 seconds and 5 git commands to generate.