Fix: use temporary data for return of asprintf
[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 output_type {
63 OUTPUT_UNKNOWN = -1,
64 OUTPUT_NONE,
65 OUTPUT_LOCAL,
66 OUTPUT_NET,
67 };
68 enum session_type {
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(enum session_type 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 && type != SESSION_LIVE) ||
252 (opt_snapshot && 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 enum session_type session_type,
271 int live_timer,
272 enum output_type 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 enum session_type session_type,
389 int live_timer,
390 enum output_type 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 enum session_type *session_type,
454 int *live_timer,
455 enum output_type *output_type,
456 char **url,
457 char **ctrl_url,
458 char **data_url,
459 char **shm_path)
460 {
461 int ret = 0;
462 int printed_byte;
463 char *raw_value = NULL;
464
465 assert(template);
466
467 /* Session name */
468 *session_name = config_document_get_element_value(template,"/sessions/session/name");
469
470 /* Check the type of session we have in the template */
471 if(config_document_element_exist(template, "/sessions/session/attributes/snapshot_mode")) {
472 *session_type = SESSION_SNAPSHOT;
473 } else if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
474 *session_type = SESSION_LIVE;
475 raw_value = config_document_get_element_value(template,"/sessions/session/attributes/live_timer_interval");
476 *live_timer = config_parse_value(raw_value);
477 free(raw_value);
478 raw_value = NULL;
479 } else {
480 *session_type = SESSION_NORMAL;
481 }
482
483 /* Output */
484 switch (*session_type) {
485 case SESSION_NORMAL:
486 case SESSION_LIVE:
487 if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")){
488 break;
489 }
490 if (config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/path")){
491 raw_value = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/path");
492 if (!raw_value) {
493 ret = -1;
494 goto error;
495 }
496
497 if (strlen(raw_value) > 0) {
498 *output_type = OUTPUT_LOCAL;
499 printed_byte = asprintf(url, "file://%s", raw_value);
500 if (printed_byte < 0) {
501 ret = -1;
502 goto error;
503 }
504 } else {
505 *output_type = OUTPUT_NONE;
506 }
507
508 free(raw_value);
509 raw_value = NULL;
510 break;
511 } else if(config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/net_output")) {
512 *ctrl_url = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/control_uri");
513 *data_url = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/data_uri");
514 if (!*ctrl_url || ! *data_url) {
515 ret = -1;
516 goto error;
517 }
518 *output_type = OUTPUT_NET;
519 } else {
520 /* There is no output definition */
521 }
522 break;
523 case SESSION_SNAPSHOT:
524 if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")){
525 break;
526 }
527 if (config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path")){
528 raw_value = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path");
529 if (!raw_value) {
530 ret = -1;
531 goto error;
532 }
533
534 if (strlen(raw_value) > 0) {
535 *output_type = OUTPUT_LOCAL;
536 printed_byte = asprintf(url, "file://%s", raw_value);
537 if (printed_byte < 0) {
538 ret = -1;
539 goto error;
540 }
541 } else {
542 *output_type = OUTPUT_NONE;
543 }
544
545 free(raw_value);
546 raw_value = NULL;
547 break;
548 } else if(config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output")) {
549 *ctrl_url = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/control_uri");
550 *data_url = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/data_uri");
551 if (!*ctrl_url || ! *data_url) {
552 ret = -1;
553 goto error;
554 }
555 *output_type = OUTPUT_NET;
556 } else {
557 /* There is no output definition */
558 }
559 break;
560 case SESSION_UNKNOWN:
561 ret = -1;
562 goto error;
563 }
564
565
566 /* shared memory path */
567 *shm_path = config_document_get_element_value(template,"/sessions/session/shared_memory_path");
568
569 error:
570 free(raw_value);
571 return ret;
572
573 }
574 static int create_session_from_template(struct config_document *template,
575 const char *session_name,
576 enum session_type session_type,
577 int live_timer,
578 enum output_type output_type,
579 const char *url,
580 const char *ctrl_url,
581 const char *data_url,
582 const char *shm_path,
583 const char *datetime)
584 {
585 int ret = CMD_SUCCESS;
586 int printed_byte;
587 struct config_element *temp_element = NULL;
588 struct config_element *temp_element_child = NULL;
589 char tmp_ctrl_uri[PATH_MAX];
590 char tmp_data_uri[PATH_MAX];
591 struct lttng_uri *uris = NULL;
592 ssize_t uri_array_size = 0;
593 char *tmp_string = NULL;
594
595 assert(template);
596 assert(session_name);
597
598 memset(tmp_ctrl_uri, 0, sizeof(tmp_ctrl_uri));
599 memset(tmp_data_uri, 0, sizeof(tmp_data_uri));
600
601 /* Session name */
602 if (config_document_element_exist(template, "/sessions/session/name")) {
603 /* Replace the node value */
604 config_document_replace_element_value(template, "/sessions/session/name", session_name);
605 } else {
606 /* insert the node */
607 temp_element = config_element_create("name", session_name);
608 if (!temp_element) {
609 ERR("Could not create session name node configuration");
610 ret = CMD_ERROR;
611 goto error;
612 }
613 ret = config_document_insert_element(template, "/sessions/session", temp_element);
614 if (ret) {
615 ERR("Could not insert session name node configuration");
616 ret = CMD_ERROR;
617 goto error;
618 }
619 config_element_free(temp_element);
620 }
621
622 /*
623 * Live timer
624 */
625 if (session_type == SESSION_LIVE) {
626 if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
627 printed_byte = asprintf(&tmp_string, "%d", live_timer);
628 if (printed_byte < 0) {
629 ERR("Asprintf failed for live timer");
630 ret = CMD_ERROR;
631 goto error;
632 }
633 ret = config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string);
634
635 if (ret) {
636 printf("error: %d\n", ret);
637 ERR("Replacement of live_timer_interval failed");
638 ret = CMD_ERROR;
639 goto error;
640 }
641
642 free(tmp_string);
643 tmp_string = NULL;
644 } else {
645 ERR("Invalid live timer template. Missing live timer node");
646 ret = CMD_ERROR;
647 goto error;
648 }
649
650 }
651
652 /*
653 * Generate the output node
654 */
655
656 /* Get output from urls */
657 if (url) {
658 /* Get lttng uris from single url */
659 uri_array_size = uri_parse_str_urls(url, NULL, &uris);
660 if (uri_array_size < 0) {
661 ret = CMD_ERROR;
662 goto error;
663 }
664 } else if (ctrl_url && data_url) {
665 uri_array_size = uri_parse_str_urls(ctrl_url, data_url, &uris);
666 if (uri_array_size < 0) {
667 ret = CMD_ERROR;
668 goto error;
669 }
670 } else {
671 /* --no-output */
672 uri_array_size = 0;
673 }
674
675 /* Validate if the session output type still match the passed data */
676 if ( (uri_array_size == 0 && output_type != OUTPUT_NONE) ||
677 (uri_array_size == 1 && output_type != OUTPUT_LOCAL) ||
678 (uri_array_size == 2 && output_type != OUTPUT_NET)) {
679 ERR("Overwriting value for output do not match the base output type");
680 ret = CMD_ERROR;
681 goto error;
682 }
683
684 switch (output_type) {
685 case OUTPUT_NONE:
686 temp_element_child = config_element_create("path", "");
687 if (!temp_element_child) {
688 ERR("Could not create empty path node configuration");
689 ret = CMD_ERROR;
690 goto error;
691 }
692 break;
693 case OUTPUT_LOCAL:
694 temp_element_child = config_element_create("path", uris[0].dst.path);
695 if (!temp_element_child) {
696 ERR("Could not create local path node configuration");
697 ret = CMD_ERROR;
698 goto error;
699 }
700 break;
701 case OUTPUT_NET:
702 uri_to_str_url(&uris[0], tmp_ctrl_uri, sizeof(tmp_ctrl_uri));
703 uri_to_str_url(&uris[1], tmp_data_uri, sizeof(tmp_data_uri));
704
705 temp_element_child = config_element_create("net_output", NULL);
706 if (!temp_element_child) {
707 ERR("Could not create net_output node configuration");
708 ret = CMD_ERROR;
709 goto error;
710 }
711
712 temp_element = config_element_create("control_uri", tmp_ctrl_uri);
713 if (!temp_element_child) {
714 ERR("Could not create ctrl uri node configuration");
715 ret = CMD_ERROR;
716 goto error;
717 }
718
719 ret = config_element_add_child(temp_element_child, temp_element);
720 if (ret) {
721 ERR("Could not append control uri to the net_output node configuration");
722 ret = CMD_ERROR;
723 goto error;
724 }
725 config_element_free(temp_element);
726
727 temp_element = config_element_create("data_uri", tmp_data_uri);
728
729 if (!temp_element_child) {
730 ERR("Could not create data_uri configuration");
731 ret = CMD_ERROR;
732 goto error;
733 }
734
735 ret = config_element_add_child(temp_element_child, temp_element);
736 if (ret) {
737 ERR("Could not append data uri to the net_output node configuration");
738 ret = CMD_ERROR;
739 goto error;
740 }
741 config_element_free(temp_element);
742 break;
743 default:
744 ret = CMD_ERROR;
745 goto error;
746 }
747
748 temp_element = config_element_create("destination", NULL);
749 if (!temp_element) {
750 ERR("Could not create destination node configuration");
751 ret = CMD_ERROR;
752 goto error;
753 }
754
755 ret = config_element_add_child(temp_element, temp_element_child);
756 if (ret) {
757 ERR("Could not append output data to the destination node configuration");
758 ret = CMD_ERROR;
759 goto error;
760 }
761
762 /*
763 * validate and replace the destination node for each session type
764 * TODO: export string as const and simply assign a base path for the
765 * destination node based on the session type
766 **/
767 switch (session_type) {
768 case SESSION_NORMAL:
769 case SESSION_LIVE:
770 if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")) {
771 ERR("Invalid template no destination node configuration present");
772 ret = CMD_ERROR;
773 goto error;
774 }
775
776 ret = config_document_replace_element(template, "/sessions/session/output/consumer_output/destination", temp_element);
777 break;
778 case SESSION_SNAPSHOT:
779 if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")) {
780 ERR("Invalid template no destination node configuration present");
781 ret = CMD_ERROR;
782 goto error;
783 }
784
785 ret = config_document_replace_element(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination", temp_element);
786 break;
787 default:
788 ERR("Invalid session type");
789 ret = CMD_UNDEFINED;
790 goto error;
791 }
792
793
794 if (ret) {
795 ERR("%s", lttng_strerror(ret));
796 ret = CMD_ERROR;
797 goto error;
798 }
799
800
801 /* Shm path */
802 if (shm_path && config_document_element_exist(template, "/sessions/session/shared_memory_path")) {
803 /* Replace the node value */
804 config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path);
805 } else if (shm_path) {
806 /* insert the node */
807 temp_element = config_element_create("shared_memory_path", shm_path);
808 if (!temp_element) {
809 ERR("Could not create shared_memory_path node configuration");
810 ret = CMD_ERROR;
811 goto error;
812 }
813 ret = config_document_insert_element(template, "/sessions/session", temp_element);
814 if (ret) {
815 ERR("Could not insert shared_memory_path node configuration");
816 ret = CMD_ERROR;
817 goto error;
818 }
819 }
820
821 ret = config_load_configuration_sessions(template, session_name, 0);
822
823
824 error:
825 config_element_free(temp_element);
826 config_element_free(temp_element_child);
827 free(tmp_string);
828 free(uris);
829 return ret;
830
831 }
832
833 /*
834 * Create a tracing session.
835 * If no name is specified, a default name is generated.
836 *
837 * Returns one of the CMD_* result constants.
838 */
839 static int create_session(void)
840 {
841 int ret;
842 int printed_byte;
843
844
845 /* Template */
846 struct config_document *template = NULL;
847
848 /* Base data */
849 enum session_type base_session_type = SESSION_UNKNOWN;
850 enum output_type base_output_type = OUTPUT_UNKNOWN;
851 char *base_session_name = NULL;
852 char *base_url = NULL;
853 char *base_ctrl_url = NULL;
854 char *base_data_url = NULL;
855 char *base_shm_path = NULL;
856 int base_live_timer = 0;
857
858 /* Time data */
859 char datetime[16];
860 time_t rawtime;
861 struct tm *timeinfo = NULL;
862
863 /* Temporary variables */
864 char *traces_path = NULL;
865 char *temp_url = NULL;
866 char *session_name_date = NULL;
867 char *tmp_url = NULL;
868 char *tmp_home_path = NULL;
869 struct lttng_uri *uris = NULL;
870 ssize_t uri_array_size = 0;
871
872
873 /* Get date and time for automatic session name/path */
874 time(&rawtime);
875 timeinfo = localtime(&rawtime);
876 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
877
878 if (opt_template_path) {
879 /* Restriction on flags exist when using template */
880 /* Session type flags are not permitted */
881 /* --live & --snapshot */
882 template = config_document_get(opt_template_path);
883 if (!template) {
884 ERR("Template could not be parsed");
885 ret = CMD_ERROR;
886 goto error;
887 }
888 /* Load info from template if any */
889 /* TODO: might want to use a struct in the end for the session... */
890 ret = parse_template(template, &base_session_name,
891 &base_session_type,
892 &base_live_timer,
893 &base_output_type,
894 &base_url,
895 &base_ctrl_url,
896 &base_data_url,
897 &base_shm_path);
898 if (ret) {
899 goto error;
900 }
901 }
902
903 /* Option validation */
904 if (validate_command_options(base_session_type) != CMD_SUCCESS) {
905 ret = CMD_ERROR;
906 goto error;
907 }
908
909 /* Find the session type based on options */
910 if(base_session_type == SESSION_UNKNOWN) {
911 if (opt_snapshot) {
912 base_session_type = SESSION_SNAPSHOT;
913 } else if (opt_live_timer) {
914 base_session_type = SESSION_LIVE;
915 } else {
916 base_session_type = SESSION_NORMAL;
917 }
918 }
919
920 /*
921 * Session name handling
922 */
923 if (opt_session_name) {
924 /* Override the session name */
925 if (strlen(opt_session_name) > NAME_MAX) {
926 ERR("Session name too long. Length must be lower or equal to %d",
927 NAME_MAX);
928 ret = LTTNG_ERR_SESSION_FAIL;
929 free(session_name_date);
930 goto error;
931 }
932 /*
933 * Check if the session name begins with "auto-" or is exactly "auto".
934 * Both are reserved for the default session name. See bug #449 to
935 * understand why we need to check both here.
936 */
937 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
938 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
939 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
940 strlen(DEFAULT_SESSION_NAME)) == 0 &&
941 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
942 ERR("%s is a reserved keyword for default session(s)",
943 DEFAULT_SESSION_NAME);
944
945 ret = CMD_ERROR;
946 goto error;
947 }
948
949 base_session_name = strndup(opt_session_name, NAME_MAX);
950 if (!base_session_name) {
951 PERROR("Strdup session name");
952 ret = CMD_ERROR;
953 goto error;
954 }
955
956 printed_byte = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
957 if (printed_byte < 0) {
958 PERROR("Asprintf session name");
959 ret = CMD_ERROR;
960 goto error;
961 }
962 DBG("Session name from command option set to %s", base_session_name);
963 } else if (base_session_name) {
964 printed_byte = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
965 if (printed_byte < 0) {
966 PERROR("Asprintf session name");
967 ret = CMD_ERROR;
968 goto error;
969 }
970 } else {
971 /* Generate a name */
972 printed_byte = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
973 if (printed_byte < 0) {
974 PERROR("Asprintf session name");
975 ret = CMD_ERROR;
976 goto error;
977 }
978 session_name_date = strdup(base_session_name);
979 DBG("Auto session name set to %s", base_session_name);
980 }
981
982
983 /*
984 * Output handling
985 */
986
987 /*
988 * If any of those options are present clear all output related data.
989 */
990 if (opt_output_path || opt_url || (opt_ctrl_url && opt_data_url) || opt_no_output) {
991 /* Overwrite output */
992 free(base_url);
993 free(base_ctrl_url);
994 free(base_data_url);
995 base_url = NULL;
996 base_ctrl_url = NULL;
997 base_data_url = NULL;
998 }
999
1000 if (opt_output_path) {
1001
1002 traces_path = utils_expand_path(opt_output_path);
1003 if (!traces_path) {
1004 ret = CMD_ERROR;
1005 goto error;
1006 }
1007
1008 /* Create URL string from the local file system path */
1009 printed_byte = asprintf(&temp_url, "file://%s", traces_path);
1010 if (printed_byte < 0) {
1011 PERROR("asprintf url path");
1012 ret = CMD_FATAL;
1013 goto error;
1014 }
1015
1016 base_url = temp_url;
1017 } else if (opt_url) { /* Handling URL (-U opt) */
1018 base_url = strdup(opt_url);
1019 } else if (opt_data_url && opt_ctrl_url) {
1020 /*
1021 * With both control and data, we'll be setting the consumer URL
1022 * after session creation thus use no URL.
1023 */
1024 base_ctrl_url = strdup(opt_ctrl_url);
1025 base_data_url = strdup(opt_data_url);
1026 } else if (!(opt_no_output || base_output_type == OUTPUT_NONE ||
1027 base_url || base_ctrl_url || base_data_url)) {
1028 /* Generate default output depending on the session type */
1029 switch (base_session_type) {
1030 case SESSION_NORMAL:
1031 /* fallthrough */
1032 case SESSION_SNAPSHOT:
1033 /* Default to a local path */
1034 tmp_home_path = utils_get_home_dir();
1035 if (tmp_home_path == NULL) {
1036 ERR("HOME path not found.\n \
1037 Please specify an output path using -o, --output PATH");
1038 ret = CMD_FATAL;
1039 goto error;
1040 }
1041
1042 printed_byte = asprintf(&tmp_url,
1043 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
1044 tmp_home_path, session_name_date);
1045
1046 if (printed_byte < 0) {
1047 PERROR("asprintf trace dir name");
1048 ret = CMD_FATAL;
1049 goto error;
1050 }
1051
1052 base_url = tmp_url ;
1053 break;
1054 case SESSION_LIVE:
1055 /* Default to a net output */
1056 printed_byte = asprintf(&tmp_url, "net://127.0.0.1");
1057 if (printed_byte < 0) {
1058 PERROR("asprintf default live URL");
1059 ret = CMD_FATAL;
1060 goto error;
1061 }
1062 base_url = tmp_url ;
1063 break;
1064 default:
1065 ERR("Unknown session type");
1066 ret = CMD_FATAL;
1067 goto error;
1068 }
1069 }
1070
1071 /*
1072 * Shared memory path handling
1073 */
1074 if (opt_shm_path) {
1075 /* Overwrite shm_path so clear any previously defined one */
1076 free(base_shm_path);
1077 printed_byte = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
1078 if (printed_byte < 0) {
1079 PERROR("asprintf shm_path");
1080 ret = CMD_FATAL;
1081 goto error;
1082 }
1083 }
1084
1085 /*
1086 * Live timer handling
1087 */
1088 if (opt_live_timer) {
1089 base_live_timer = opt_live_timer;
1090 }
1091
1092 /* Get output type from urls */
1093 if (base_url) {
1094 /* Get lttng uris from single url */
1095 uri_array_size = uri_parse_str_urls(base_url, NULL, &uris);
1096 if (uri_array_size < 0) {
1097 ret = CMD_ERROR;
1098 goto error;
1099 }
1100 } else if (base_ctrl_url && base_data_url) {
1101 uri_array_size = uri_parse_str_urls(base_ctrl_url, base_data_url, &uris);
1102 if (uri_array_size < 0) {
1103 ret = CMD_ERROR;
1104 goto error;
1105 }
1106 } else {
1107 /* --no-output */
1108 uri_array_size = 0;
1109 }
1110
1111 switch (uri_array_size) {
1112 case 0:
1113 base_output_type = OUTPUT_NONE;
1114 break;
1115 case 1:
1116 base_output_type = OUTPUT_LOCAL;
1117 break;
1118 case 2:
1119 base_output_type = OUTPUT_NET;
1120 break;
1121 default:
1122 ret = CMD_ERROR;
1123 goto error;
1124 }
1125
1126 if (template) {
1127 ret = create_session_from_template(template,
1128 base_session_name,
1129 base_session_type,
1130 base_live_timer,
1131 base_output_type,
1132 base_url,
1133 base_ctrl_url,
1134 base_data_url,
1135 base_shm_path,
1136 datetime);
1137 } else {
1138 ret = create_session_basic (base_session_name,
1139 base_session_type,
1140 base_live_timer,
1141 base_output_type,
1142 base_url,
1143 base_ctrl_url,
1144 base_data_url,
1145 base_shm_path,
1146 datetime);
1147 }
1148 if (ret) {
1149 goto error;
1150 }
1151
1152 ret = generate_output (base_session_name,
1153 base_session_type,
1154 base_live_timer,
1155 base_output_type,
1156 base_url,
1157 base_ctrl_url,
1158 base_data_url,
1159 base_shm_path);
1160 if (ret) {
1161 goto error;
1162 }
1163
1164 /* Init lttng session config */
1165 ret = config_init(base_session_name);
1166 if (ret < 0) {
1167 ret = CMD_ERROR;
1168 goto error;
1169 }
1170
1171 ret = CMD_SUCCESS;
1172
1173 error:
1174 /* Session temp stuff */
1175 free(session_name_date);
1176 free(uris);
1177
1178 if (ret < 0) {
1179 ERR("%s", lttng_strerror(ret));
1180 }
1181
1182 free(base_session_name);
1183 free(base_url);
1184 free(base_ctrl_url);
1185 free(base_data_url);
1186 free(base_shm_path);
1187 return ret;
1188 }
1189
1190 /*
1191 * spawn_sessiond
1192 *
1193 * Spawn a session daemon by forking and execv.
1194 */
1195 static int spawn_sessiond(char *pathname)
1196 {
1197 int ret = 0;
1198 pid_t pid;
1199
1200 MSG("Spawning a session daemon");
1201 pid = fork();
1202 if (pid == 0) {
1203 /*
1204 * Spawn session daemon in daemon mode.
1205 */
1206 execlp(pathname, "lttng-sessiond",
1207 "--daemonize", NULL);
1208 /* execlp only returns if error happened */
1209 if (errno == ENOENT) {
1210 ERR("No session daemon found. Use --sessiond-path.");
1211 } else {
1212 PERROR("execlp");
1213 }
1214 kill(getppid(), SIGTERM); /* wake parent */
1215 exit(EXIT_FAILURE);
1216 } else if (pid > 0) {
1217 /*
1218 * In daemon mode (--daemonize), sessiond only exits when
1219 * it's ready to accept commands.
1220 */
1221 for (;;) {
1222 int status;
1223 pid_t wait_pid_ret = waitpid(pid, &status, 0);
1224
1225 if (wait_pid_ret < 0) {
1226 if (errno == EINTR) {
1227 continue;
1228 }
1229 PERROR("waitpid");
1230 ret = -errno;
1231 goto end;
1232 }
1233
1234 if (WIFSIGNALED(status)) {
1235 ERR("Session daemon was killed by signal %d",
1236 WTERMSIG(status));
1237 ret = -1;
1238 goto end;
1239 } else if (WIFEXITED(status)) {
1240 DBG("Session daemon terminated normally (exit status: %d)",
1241 WEXITSTATUS(status));
1242
1243 if (WEXITSTATUS(status) != 0) {
1244 ERR("Session daemon terminated with an error (exit status: %d)",
1245 WEXITSTATUS(status));
1246 ret = -1;
1247 goto end;
1248 }
1249 break;
1250 }
1251 }
1252
1253 goto end;
1254 } else {
1255 PERROR("fork");
1256 ret = -1;
1257 goto end;
1258 }
1259
1260 end:
1261 return ret;
1262 }
1263
1264 /*
1265 * launch_sessiond
1266 *
1267 * Check if the session daemon is available using
1268 * the liblttngctl API for the check. If not, try to
1269 * spawn a daemon.
1270 */
1271 static int launch_sessiond(void)
1272 {
1273 int ret;
1274 char *pathname = NULL;
1275
1276 ret = lttng_session_daemon_alive();
1277 if (ret) {
1278 /* Sessiond is alive, not an error */
1279 ret = 0;
1280 goto end;
1281 }
1282
1283 /* Try command line option path */
1284 pathname = opt_sessiond_path;
1285
1286 /* Try LTTNG_SESSIOND_PATH env variable */
1287 if (pathname == NULL) {
1288 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
1289 }
1290
1291 /* Try with configured path */
1292 if (pathname == NULL) {
1293 if (CONFIG_SESSIOND_BIN[0] != '\0') {
1294 pathname = CONFIG_SESSIOND_BIN;
1295 }
1296 }
1297
1298 /* Try the default path */
1299 if (pathname == NULL) {
1300 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
1301 }
1302
1303 DBG("Session daemon binary path: %s", pathname);
1304
1305 /* Check existence and permissions */
1306 ret = access(pathname, F_OK | X_OK);
1307 if (ret < 0) {
1308 ERR("No such file or access denied: %s", pathname);
1309 goto end;
1310 }
1311
1312 ret = spawn_sessiond(pathname);
1313 end:
1314 if (ret) {
1315 ERR("Problem occurred while launching session daemon (%s)",
1316 pathname);
1317 }
1318 return ret;
1319 }
1320
1321 /*
1322 * The 'create <options>' first level command
1323 *
1324 * Returns one of the CMD_* result constants.
1325 */
1326 int cmd_create(int argc, const char **argv)
1327 {
1328 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
1329 char *opt_arg = NULL;
1330 static poptContext pc;
1331
1332 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1333 poptReadDefaultConfig(pc, 0);
1334
1335 while ((opt = poptGetNextOpt(pc)) != -1) {
1336 switch (opt) {
1337 case OPT_HELP:
1338 SHOW_HELP();
1339 goto end;
1340 case OPT_LIST_OPTIONS:
1341 list_cmd_options(stdout, long_options);
1342 goto end;
1343 case OPT_LIVE_TIMER:
1344 {
1345 unsigned long v;
1346
1347 errno = 0;
1348 opt_arg = poptGetOptArg(pc);
1349 if (!opt_arg) {
1350 /* Set up default values. */
1351 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
1352 DBG("Session live timer interval set to default value %d",
1353 opt_live_timer);
1354 break;
1355 }
1356
1357 v = strtoul(opt_arg, NULL, 0);
1358 if (errno != 0 || !isdigit(opt_arg[0])) {
1359 ERR("Wrong value in --live parameter: %s", opt_arg);
1360 ret = CMD_ERROR;
1361 goto end;
1362 }
1363 if (v != (uint32_t) v) {
1364 ERR("32-bit overflow in --live parameter: %s", opt_arg);
1365 ret = CMD_ERROR;
1366 goto end;
1367 }
1368 if (v == 0) {
1369 ERR("Live timer interval must be greater than zero");
1370 ret = CMD_ERROR;
1371 goto end;
1372 }
1373 opt_live_timer = (uint32_t) v;
1374 DBG("Session live timer interval set to %d", opt_live_timer);
1375 break;
1376 }
1377 default:
1378 ret = CMD_UNDEFINED;
1379 goto end;
1380 }
1381 }
1382
1383 if (opt_no_consumer) {
1384 MSG("The option --no-consumer is obsolete. Use --no-output now.");
1385 ret = CMD_WARNING;
1386 goto end;
1387 }
1388
1389 /* Spawn a session daemon if needed */
1390 if (!opt_no_sessiond) {
1391 ret = launch_sessiond();
1392 if (ret) {
1393 ret = CMD_ERROR;
1394 goto end;
1395 }
1396 }
1397
1398 /* MI initialization */
1399 if (lttng_opt_mi) {
1400 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1401 if (!writer) {
1402 ret = -LTTNG_ERR_NOMEM;
1403 goto end;
1404 }
1405
1406 /* Open command element */
1407 ret = mi_lttng_writer_command_open(writer,
1408 mi_lttng_element_command_create);
1409 if (ret) {
1410 ret = CMD_ERROR;
1411 goto end;
1412 }
1413
1414 /* Open output element */
1415 ret = mi_lttng_writer_open_element(writer,
1416 mi_lttng_element_command_output);
1417 if (ret) {
1418 ret = CMD_ERROR;
1419 goto end;
1420 }
1421 }
1422 opt_session_name = (char*) poptGetArg(pc);
1423
1424 command_ret = create_session();
1425
1426 if (command_ret) {
1427 success = 0;
1428 }
1429
1430 if (lttng_opt_mi) {
1431 /* Close output element */
1432 ret = mi_lttng_writer_close_element(writer);
1433 if (ret) {
1434 ret = CMD_ERROR;
1435 goto end;
1436 }
1437
1438 /* Success ? */
1439 ret = mi_lttng_writer_write_element_bool(writer,
1440 mi_lttng_element_command_success, success);
1441 if (ret) {
1442 ret = CMD_ERROR;
1443 goto end;
1444 }
1445
1446 /* Command element close */
1447 ret = mi_lttng_writer_command_close(writer);
1448 if (ret) {
1449 ret = CMD_ERROR;
1450 goto end;
1451 }
1452 }
1453
1454 end:
1455 /* Mi clean-up */
1456 if (writer && mi_lttng_writer_destroy(writer)) {
1457 /* Preserve original error code */
1458 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
1459 }
1460
1461 /* Overwrite ret if an error occurred in create_session() */
1462 ret = command_ret ? command_ret : ret;
1463
1464 poptFreeContext(pc);
1465 return ret;
1466 }
This page took 0.061102 seconds and 5 git commands to generate.