Create commands template feature.
[lttng-tools.git] / src / bin / lttng / commands / create.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
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 *
d14d33bf
AM
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.
f3ed775e
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
a4b92340 19#include <assert.h>
ecc48a90 20#include <ctype.h>
f3ed775e
DG
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>
92360082 29#include <signal.h>
bbd44cae 30#include <sys/wait.h>
f3ed775e 31
37d03ff7 32#include <common/mi-lttng.h>
4a102b75 33#include <common/config/session-config.h>
37d03ff7 34
c399183f 35#include "../command.h"
679b4943 36#include "../utils.h"
f3ed775e 37
00e2e675 38#include <common/defaults.h>
42224349 39#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 40#include <common/uri.h>
81b86775 41#include <common/utils.h>
16f6f820 42#include <lttng/snapshot.h>
42224349 43
f3ed775e
DG
44static char *opt_output_path;
45static char *opt_session_name;
a4b92340
DG
46static char *opt_url;
47static char *opt_ctrl_url;
48static char *opt_data_url;
d7ba1388 49static char *opt_shm_path;
4a102b75 50static char *opt_template_path;
a4b92340 51static int opt_no_consumer;
96fe6b8d 52static int opt_no_output;
16f6f820 53static int opt_snapshot;
ecc48a90 54static unsigned int opt_live_timer;
f3ed775e
DG
55
56enum {
57 OPT_HELP = 1,
679b4943 58 OPT_LIST_OPTIONS,
ecc48a90 59 OPT_LIVE_TIMER,
f3ed775e
DG
60};
61
227bc702 62enum {
101fa438 63 OUTPUT_UNKNOWN = -1,
227bc702
JR
64 OUTPUT_NONE,
65 OUTPUT_LOCAL,
66 OUTPUT_NET,
67};
68enum {
101fa438 69 SESSION_UNKNOWN = -1,
227bc702
JR
70 SESSION_NORMAL,
71 SESSION_LIVE,
72 SESSION_SNAPSHOT,
73};
74
75
37d03ff7
JRJ
76static struct mi_writer *writer;
77
f3ed775e
DG
78static struct poptOption long_options[] = {
79 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
679b4943
SM
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},
23d14dff
DG
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},
96fe6b8d 86 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
2bba9e53 87 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
16f6f820 88 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
d73c5802 89 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
d7ba1388 90 {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
4a102b75 91 {"template-path", 0, POPT_ARG_STRING, &opt_template_path, 0, 0, 0},
f3ed775e
DG
92 {0, 0, 0, 0, 0, 0, 0}
93};
94
16de1a24
DG
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 */
07424f16 99extern int _lttng_create_session_ext(const char *name, const char *url,
d1edd8a8 100 const char *datetime);
07424f16 101
37d03ff7 102/*
485ca16f 103 * Retrieve the created session and mi output it based on provided argument
37d03ff7
JRJ
104 * This is currently a summary of what was pretty printed and is subject to
105 * enhancements.
37d03ff7
JRJ
106 */
107static 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
147error:
148 free(sessions);
149end:
150 return ret;
151}
152
00e2e675 153/*
a4b92340 154 * For a session name, set the consumer URLs.
00e2e675 155 */
a4b92340
DG
156static int set_consumer_url(const char *session_name, const char *ctrl_url,
157 const char *data_url)
00e2e675 158{
a4b92340
DG
159 int ret;
160 struct lttng_handle *handle;
a4b92340
DG
161
162 assert(session_name);
163
164 /*
95681498
JG
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.
a4b92340 168 */
95681498 169 handle = lttng_create_handle(session_name, NULL);
a4b92340
DG
170 if (handle == NULL) {
171 ret = CMD_FATAL;
172 goto error;
173 }
00e2e675 174
a4b92340
DG
175 ret = lttng_set_consumer_url(handle, ctrl_url, data_url);
176 if (ret < 0) {
177 goto error;
00e2e675
DG
178 }
179
a4b92340
DG
180error:
181 lttng_destroy_handle(handle);
182 return ret;
183}
184
16f6f820
DG
185static 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
219error:
220 lttng_snapshot_output_destroy(output);
221error_create:
222 return ret;
223}
224
227bc702
JR
225/*
226 * Validate the combinations of passed options
227 *
228 * CMD_ERROR on error
229 * CMD_SUCCESS on success
230 */
4a102b75 231static int validate_command_options(int session_type)
227bc702
JR
232{
233 int ret = CMD_SUCCESS;
dd4b646b 234
227bc702
JR
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)) {
dd4b646b 242 ERR("Control and data URLs must both be set.");
227bc702
JR
243 ret = CMD_ERROR;
244 goto error;
245 }
246
4a102b75
JR
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
227bc702
JR
259error:
260 return ret;
261}
262
263/*
d49b7031 264 * Create a session using direct calls to liblttng-ctl.
227bc702
JR
265 *
266 * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
267 * value on command errors.
268 */
269static 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) {
8ad05258 329 /* Don't set ret so the sessiond error is propagated. */
227bc702
JR
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:
19ed6174
JR
345 if (!ctrl_url || !data_url) {
346 break;
347 }
348
227bc702
JR
349 if (session_type == SESSION_SNAPSHOT) {
350 ret = add_snapshot_output(session_name, ctrl_url,
351 data_url);
19ed6174 352 } else {
227bc702
JR
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 }
383error:
384 return ret;
385}
386
387static 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 }
447error:
448 return ret;
449}
450
4a102b75
JR
451static 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
565error:
566 free(raw_value);
567 return ret;
568
569}
570static 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
777 /* Shm path */
778 if (shm_path && config_document_element_exist(template, "/sessions/session/shared_memory_path")) {
779 /* Replace the node value */
780 config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path);
781 } else if (shm_path) {
782 /* insert the node */
783 temp_element = config_element_create("shared_memory_path", shm_path);
784 if (!temp_element) {
785 ERR("Could not create shared_memory_path node configuration");
786 ret = CMD_ERROR;
787 goto error;
788 }
789 ret = config_document_insert_element(template, "/sessions/session", temp_element);
790 if (ret) {
791 ERR("Could not insert shared_memory_path node configuration");
792 ret = CMD_ERROR;
793 goto error;
794 }
795 }
796
797 ret = config_load_configuration_sessions(template, session_name, 0);
798
799
800error:
801 config_element_free(temp_element);
802 config_element_free(temp_element_child);
803 free(tmp_string);
804 free(uris);
805 return ret;
806
807}
808
f3ed775e 809/*
1c8d13c8
TD
810 * Create a tracing session.
811 * If no name is specified, a default name is generated.
f3ed775e 812 *
1c8d13c8 813 * Returns one of the CMD_* result constants.
f3ed775e 814 */
a4b92340 815static int create_session(void)
f3ed775e 816{
a4b92340 817 int ret;
227bc702 818
4a102b75
JR
819
820 /* Template */
821 struct config_document *template = NULL;
822
227bc702
JR
823 /* Base data */
824 int base_session_type = SESSION_UNKNOWN;
825 int base_output_type = OUTPUT_UNKNOWN;
826 char *base_session_name = NULL;
827 char *base_url = NULL;
828 char *base_ctrl_url = NULL;
829 char *base_data_url = NULL;
830 char *base_shm_path = NULL;
831 int base_live_timer = 0;
832
833 /* Time data */
834 char datetime[16];
f3ed775e 835 time_t rawtime;
227bc702
JR
836 struct tm *timeinfo = NULL;
837
838 /* Temporary variables */
839 char *traces_path = NULL;
840 char *temp_url = NULL;
841 char *session_name_date = NULL;
842 char *tmp_url = NULL;
843 char *tmp_home_path = NULL;
844 struct lttng_uri *uris = NULL;
845 ssize_t uri_array_size = 0;
846
f3ed775e 847
d6175221
DG
848 /* Get date and time for automatic session name/path */
849 time(&rawtime);
850 timeinfo = localtime(&rawtime);
851 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
852
4a102b75
JR
853 if (opt_template_path) {
854 /* Restriction on flags exist when using template */
855 /* Session type flags are not permitted */
856 /* --live & --snapshot */
857 template = config_document_get(opt_template_path);
858 if (!template) {
859 ERR("Template could not be parsed");
860 ret = CMD_ERROR;
861 goto error;
862 }
863 /* Load info from template if any */
864 /* TODO: might want to use a struct in the end for the session... */
865 ret = parse_template(template, &base_session_name,
866 &base_session_type,
867 &base_live_timer,
868 &base_output_type,
869 &base_url,
870 &base_ctrl_url,
871 &base_data_url,
872 &base_shm_path);
873 }
874
875 /* Option validation */
876 if (validate_command_options(base_session_type) != CMD_SUCCESS) {
877 ret = CMD_ERROR;
878 goto error;
879 }
880
227bc702
JR
881 /* Find the session type based on options */
882 if(base_session_type == SESSION_UNKNOWN) {
883 if (opt_snapshot) {
884 base_session_type = SESSION_SNAPSHOT;
885 } else if (opt_live_timer) {
886 base_session_type = SESSION_LIVE;
887 } else {
888 base_session_type = SESSION_NORMAL;
07424f16 889 }
227bc702
JR
890 }
891
892 /*
893 * Session name handling
894 */
895 if (opt_session_name) {
896 /* Override the session name */
487b253b
DG
897 if (strlen(opt_session_name) > NAME_MAX) {
898 ERR("Session name too long. Length must be lower or equal to %d",
899 NAME_MAX);
900 ret = LTTNG_ERR_SESSION_FAIL;
227bc702 901 free(session_name_date);
487b253b
DG
902 goto error;
903 }
4b861950
DG
904 /*
905 * Check if the session name begins with "auto-" or is exactly "auto".
906 * Both are reserved for the default session name. See bug #449 to
907 * understand why we need to check both here.
908 */
909 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
227bc702
JR
910 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
911 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
912 strlen(DEFAULT_SESSION_NAME)) == 0 &&
67238b20 913 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
61b35a5a
DG
914 ERR("%s is a reserved keyword for default session(s)",
915 DEFAULT_SESSION_NAME);
227bc702
JR
916
917 ret = CMD_ERROR;
918 goto error;
919 }
920
921 base_session_name = strndup(opt_session_name, NAME_MAX);
922 if (!base_session_name) {
923 PERROR("Strdup session name");
61b35a5a
DG
924 ret = CMD_ERROR;
925 goto error;
926 }
227bc702
JR
927
928 ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
929 if (ret < 0) {
930 PERROR("Asprintf session name");
931 goto error;
932 }
933 DBG("Session name from command option set to %s", base_session_name);
934 } else if (base_session_name) {
935 ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
936 if (ret < 0) {
937 PERROR("Asprintf session name");
938 goto error;
939 }
940 } else {
941 /* Generate a name */
227bc702 942 ret = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
07424f16 943 if (ret < 0) {
6af1d7e6 944 PERROR("Asprintf session name");
07424f16
DG
945 goto error;
946 }
227bc702
JR
947 session_name_date = strdup(base_session_name);
948 DBG("Auto session name set to %s", base_session_name);
f3ed775e
DG
949 }
950
227bc702
JR
951
952 /*
953 * Output handling
954 */
955
956 /*
957 * If any of those options are present clear all output related data.
958 */
959 if (opt_output_path || opt_url || (opt_ctrl_url && opt_data_url) || opt_no_output) {
960 /* Overwrite output */
961 free(base_url);
962 free(base_ctrl_url);
963 free(base_data_url);
964 base_url = NULL;
965 base_ctrl_url = NULL;
966 base_data_url = NULL;
1a241656
DG
967 }
968
227bc702
JR
969 if (opt_output_path) {
970
81b86775 971 traces_path = utils_expand_path(opt_output_path);
227bc702 972 if (!traces_path) {
00e2e675
DG
973 ret = CMD_ERROR;
974 goto error;
975 }
976
37d03ff7 977 /* Create URL string from the local file system path */
227bc702 978 ret = asprintf(&temp_url, "file://%s", traces_path);
00e2e675 979 if (ret < 0) {
a4b92340 980 PERROR("asprintf url path");
00e2e675
DG
981 ret = CMD_FATAL;
982 goto error;
983 }
227bc702
JR
984
985 base_url = temp_url;
a4b92340 986 } else if (opt_url) { /* Handling URL (-U opt) */
227bc702 987 base_url = strdup(opt_url);
1a241656
DG
988 } else if (opt_data_url && opt_ctrl_url) {
989 /*
227bc702
JR
990 * With both control and data, we'll be setting the consumer URL
991 * after session creation thus use no URL.
1a241656 992 */
227bc702
JR
993 base_ctrl_url = strdup(opt_ctrl_url);
994 base_data_url = strdup(opt_data_url);
995 } else if (!(opt_no_output || base_output_type == OUTPUT_NONE ||
996 base_url || base_ctrl_url || base_data_url)) {
997 /* Generate default output depending on the session type */
998 switch (base_session_type) {
999 case SESSION_NORMAL:
1000 /* fallthrough */
1001 case SESSION_SNAPSHOT:
1002 /* Default to a local path */
1003 tmp_home_path = utils_get_home_dir();
1004 if (tmp_home_path == NULL) {
1005 ERR("HOME path not found.\n \
1006 Please specify an output path using -o, --output PATH");
1007 ret = CMD_FATAL;
1008 goto error;
1009 }
ecc48a90 1010
227bc702
JR
1011 ret = asprintf(&tmp_url,
1012 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
1013 tmp_home_path, session_name_date);
16f6f820 1014
227bc702
JR
1015 if (ret < 0) {
1016 PERROR("asprintf trace dir name");
1017 ret = CMD_FATAL;
1018 goto error;
1019 }
8960e9cd 1020
227bc702 1021 base_url = tmp_url ;
42224349 1022 break;
227bc702
JR
1023 case SESSION_LIVE:
1024 /* Default to a net output */
1025 ret = asprintf(&tmp_url, "net://127.0.0.1");
1026 if (ret < 0) {
1027 PERROR("asprintf default live URL");
1028 ret = CMD_FATAL;
1029 goto error;
1030 }
1031 base_url = tmp_url ;
60e835ca 1032 break;
227bc702
JR
1033 default:
1034 ERR("Unknown session type");
1035 ret = CMD_FATAL;
a4b92340
DG
1036 goto error;
1037 }
4f50c803
DG
1038 }
1039
227bc702
JR
1040 /*
1041 * Shared memory path handling
1042 */
d7ba1388 1043 if (opt_shm_path) {
227bc702 1044 ret = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
d7ba1388 1045 if (ret < 0) {
227bc702 1046 PERROR("asprintf shm_path");
d7ba1388
MD
1047 goto error;
1048 }
1049 }
1050
227bc702
JR
1051 /*
1052 * Live timer handling
1053 */
1054 if (opt_live_timer) {
1055 base_live_timer = opt_live_timer;
1056 }
d73c5802 1057
227bc702
JR
1058 /* Get output type from urls */
1059 if (base_url) {
1060 /* Get lttng uris from single url */
1061 uri_array_size = uri_parse_str_urls(base_url, NULL, &uris);
1062 if (uri_array_size < 0) {
1063 ret = CMD_ERROR;
1064 goto error;
d73c5802 1065 }
227bc702
JR
1066 } else if (base_ctrl_url && base_data_url) {
1067 uri_array_size = uri_parse_str_urls(base_ctrl_url, base_data_url, &uris);
1068 if (uri_array_size < 0) {
1069 ret = CMD_ERROR;
1070 goto error;
16f6f820 1071 }
227bc702
JR
1072 } else {
1073 /* --no-output */
1074 uri_array_size = 0;
1075 }
1076
1077 switch (uri_array_size) {
1078 case 0:
1079 base_output_type = OUTPUT_NONE;
1080 break;
1081 case 1:
1082 base_output_type = OUTPUT_LOCAL;
1083 break;
1084 case 2:
1085 base_output_type = OUTPUT_NET;
1086 break;
1087 default:
1088 ret = CMD_ERROR;
1089 goto error;
a4b92340 1090 }
227bc702 1091
4a102b75
JR
1092 if (template) {
1093 ret = create_session_from_template(template,
1094 base_session_name,
1095 base_session_type,
1096 base_live_timer,
1097 base_output_type,
1098 base_url,
1099 base_ctrl_url,
1100 base_data_url,
1101 base_shm_path,
1102 datetime);
1103 } else {
1104 ret = create_session_basic (base_session_name,
1105 base_session_type,
1106 base_live_timer,
1107 base_output_type,
1108 base_url,
1109 base_ctrl_url,
1110 base_data_url,
1111 base_shm_path,
1112 datetime);
1113 }
227bc702
JR
1114 if (ret) {
1115 goto error;
d7ba1388 1116 }
a4b92340 1117
227bc702
JR
1118 ret = generate_output (base_session_name,
1119 base_session_type,
1120 base_live_timer,
1121 base_output_type,
1122 base_url,
1123 base_ctrl_url,
1124 base_data_url,
1125 base_shm_path);
1126 if (ret) {
1127 goto error;
37d03ff7
JRJ
1128 }
1129
58a97671 1130 /* Init lttng session config */
227bc702 1131 ret = config_init(base_session_name);
f3ed775e 1132 if (ret < 0) {
27089920 1133 ret = CMD_ERROR;
f3ed775e
DG
1134 goto error;
1135 }
1136
f3ed775e
DG
1137 ret = CMD_SUCCESS;
1138
1139error:
227bc702
JR
1140 /* Session temp stuff */
1141 free(session_name_date);
227bc702 1142 free(uris);
a4b92340
DG
1143
1144 if (ret < 0) {
1145 ERR("%s", lttng_strerror(ret));
1146 }
67238b20 1147
227bc702
JR
1148 free(base_session_name);
1149 free(base_url);
1150 free(base_ctrl_url);
1151 free(base_data_url);
1152 free(base_shm_path);
f3ed775e
DG
1153 return ret;
1154}
1155
92360082
JG
1156/*
1157 * spawn_sessiond
1158 *
1159 * Spawn a session daemon by forking and execv.
1160 */
1161static int spawn_sessiond(char *pathname)
1162{
1163 int ret = 0;
1164 pid_t pid;
1165
1166 MSG("Spawning a session daemon");
92360082
JG
1167 pid = fork();
1168 if (pid == 0) {
1169 /*
bbd44cae 1170 * Spawn session daemon in daemon mode.
92360082 1171 */
bbd44cae
PP
1172 execlp(pathname, "lttng-sessiond",
1173 "--daemonize", NULL);
92360082
JG
1174 /* execlp only returns if error happened */
1175 if (errno == ENOENT) {
1176 ERR("No session daemon found. Use --sessiond-path.");
1177 } else {
1178 PERROR("execlp");
1179 }
1180 kill(getppid(), SIGTERM); /* wake parent */
1181 exit(EXIT_FAILURE);
1182 } else if (pid > 0) {
92360082 1183 /*
bbd44cae
PP
1184 * In daemon mode (--daemonize), sessiond only exits when
1185 * it's ready to accept commands.
92360082 1186 */
bbd44cae 1187 for (;;) {
81527d36
JG
1188 int status;
1189 pid_t wait_pid_ret = waitpid(pid, &status, 0);
1190
1191 if (wait_pid_ret < 0) {
1192 if (errno == EINTR) {
1193 continue;
1194 }
1195 PERROR("waitpid");
1196 ret = -errno;
1197 goto end;
1198 }
bbd44cae
PP
1199
1200 if (WIFSIGNALED(status)) {
1201 ERR("Session daemon was killed by signal %d",
1202 WTERMSIG(status));
1203 ret = -1;
1204 goto end;
1205 } else if (WIFEXITED(status)) {
1206 DBG("Session daemon terminated normally (exit status: %d)",
1207 WEXITSTATUS(status));
1208
1209 if (WEXITSTATUS(status) != 0) {
1210 ERR("Session daemon terminated with an error (exit status: %d)",
1211 WEXITSTATUS(status));
1212 ret = -1;
1213 goto end;
1214 }
1215 break;
1216 }
92360082 1217 }
bbd44cae 1218
92360082
JG
1219 goto end;
1220 } else {
1221 PERROR("fork");
1222 ret = -1;
1223 goto end;
1224 }
1225
1226end:
1227 return ret;
1228}
1229
1230/*
1231 * launch_sessiond
1232 *
1233 * Check if the session daemon is available using
1234 * the liblttngctl API for the check. If not, try to
1235 * spawn a daemon.
1236 */
1237static int launch_sessiond(void)
1238{
1239 int ret;
1240 char *pathname = NULL;
1241
1242 ret = lttng_session_daemon_alive();
1243 if (ret) {
1244 /* Sessiond is alive, not an error */
1245 ret = 0;
1246 goto end;
1247 }
1248
1249 /* Try command line option path */
1250 pathname = opt_sessiond_path;
1251
1252 /* Try LTTNG_SESSIOND_PATH env variable */
1253 if (pathname == NULL) {
1254 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
1255 }
1256
1257 /* Try with configured path */
1258 if (pathname == NULL) {
1259 if (CONFIG_SESSIOND_BIN[0] != '\0') {
1260 pathname = CONFIG_SESSIOND_BIN;
1261 }
1262 }
1263
1264 /* Try the default path */
1265 if (pathname == NULL) {
1266 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
1267 }
1268
1269 DBG("Session daemon binary path: %s", pathname);
1270
1271 /* Check existence and permissions */
1272 ret = access(pathname, F_OK | X_OK);
1273 if (ret < 0) {
1274 ERR("No such file or access denied: %s", pathname);
1275 goto end;
1276 }
1277
1278 ret = spawn_sessiond(pathname);
92360082 1279end:
0f4fa0d2
JG
1280 if (ret) {
1281 ERR("Problem occurred while launching session daemon (%s)",
1282 pathname);
1283 }
92360082
JG
1284 return ret;
1285}
1286
f3ed775e 1287/*
74cc1d0f 1288 * The 'create <options>' first level command
1c8d13c8
TD
1289 *
1290 * Returns one of the CMD_* result constants.
f3ed775e
DG
1291 */
1292int cmd_create(int argc, const char **argv)
1293{
37d03ff7 1294 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
ecc48a90 1295 char *opt_arg = NULL;
f3ed775e
DG
1296 static poptContext pc;
1297
1298 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1299 poptReadDefaultConfig(pc, 0);
1300
1301 while ((opt = poptGetNextOpt(pc)) != -1) {
1302 switch (opt) {
1303 case OPT_HELP:
4ba92f18 1304 SHOW_HELP();
f3ed775e 1305 goto end;
679b4943
SM
1306 case OPT_LIST_OPTIONS:
1307 list_cmd_options(stdout, long_options);
679b4943 1308 goto end;
ecc48a90
JD
1309 case OPT_LIVE_TIMER:
1310 {
1311 unsigned long v;
1312
1313 errno = 0;
1314 opt_arg = poptGetOptArg(pc);
d73c5802
DG
1315 if (!opt_arg) {
1316 /* Set up default values. */
1317 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
1318 DBG("Session live timer interval set to default value %d",
1319 opt_live_timer);
1320 break;
1321 }
1322
ecc48a90
JD
1323 v = strtoul(opt_arg, NULL, 0);
1324 if (errno != 0 || !isdigit(opt_arg[0])) {
1325 ERR("Wrong value in --live parameter: %s", opt_arg);
1326 ret = CMD_ERROR;
1327 goto end;
1328 }
1329 if (v != (uint32_t) v) {
1330 ERR("32-bit overflow in --live parameter: %s", opt_arg);
1331 ret = CMD_ERROR;
1332 goto end;
1333 }
0ed9e0be
JG
1334 if (v == 0) {
1335 ERR("Live timer interval must be greater than zero");
1336 ret = CMD_ERROR;
1337 goto end;
1338 }
ecc48a90
JD
1339 opt_live_timer = (uint32_t) v;
1340 DBG("Session live timer interval set to %d", opt_live_timer);
1341 break;
1342 }
f3ed775e 1343 default:
f3ed775e
DG
1344 ret = CMD_UNDEFINED;
1345 goto end;
1346 }
1347 }
1348
785d2d0d 1349 if (opt_no_consumer) {
96fe6b8d 1350 MSG("The option --no-consumer is obsolete. Use --no-output now.");
785d2d0d
DG
1351 ret = CMD_WARNING;
1352 goto end;
1353 }
1354
92360082
JG
1355 /* Spawn a session daemon if needed */
1356 if (!opt_no_sessiond) {
1357 ret = launch_sessiond();
1358 if (ret) {
1359 ret = CMD_ERROR;
1360 goto end;
1361 }
1362 }
1363
acc09215 1364 /* MI initialization */
37d03ff7
JRJ
1365 if (lttng_opt_mi) {
1366 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1367 if (!writer) {
1368 ret = -LTTNG_ERR_NOMEM;
1369 goto end;
1370 }
1371
1372 /* Open command element */
1373 ret = mi_lttng_writer_command_open(writer,
1374 mi_lttng_element_command_create);
1375 if (ret) {
1376 ret = CMD_ERROR;
1377 goto end;
1378 }
1379
1380 /* Open output element */
1381 ret = mi_lttng_writer_open_element(writer,
1382 mi_lttng_element_command_output);
1383 if (ret) {
1384 ret = CMD_ERROR;
1385 goto end;
1386 }
1387 }
f3ed775e
DG
1388 opt_session_name = (char*) poptGetArg(pc);
1389
37d03ff7 1390 command_ret = create_session();
227bc702 1391
37d03ff7
JRJ
1392 if (command_ret) {
1393 success = 0;
1394 }
1395
1396 if (lttng_opt_mi) {
1397 /* Close output element */
1398 ret = mi_lttng_writer_close_element(writer);
1399 if (ret) {
1400 ret = CMD_ERROR;
1401 goto end;
1402 }
1403
1404 /* Success ? */
1405 ret = mi_lttng_writer_write_element_bool(writer,
1406 mi_lttng_element_command_success, success);
1407 if (ret) {
1408 ret = CMD_ERROR;
1409 goto end;
1410 }
1411
1412 /* Command element close */
1413 ret = mi_lttng_writer_command_close(writer);
1414 if (ret) {
1415 ret = CMD_ERROR;
1416 goto end;
1417 }
1418 }
f3ed775e
DG
1419
1420end:
37d03ff7
JRJ
1421 /* Mi clean-up */
1422 if (writer && mi_lttng_writer_destroy(writer)) {
1423 /* Preserve original error code */
1424 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
1425 }
1426
acc09215 1427 /* Overwrite ret if an error occurred in create_session() */
37d03ff7
JRJ
1428 ret = command_ret ? command_ret : ret;
1429
ca1c3607 1430 poptFreeContext(pc);
f3ed775e
DG
1431 return ret;
1432}
This page took 0.120435 seconds and 5 git commands to generate.