Fix: generate a full output configuration node
[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
05ca94c0 62enum output_type {
101fa438 63 OUTPUT_UNKNOWN = -1,
227bc702
JR
64 OUTPUT_NONE,
65 OUTPUT_LOCAL,
66 OUTPUT_NET,
67};
05ca94c0 68enum session_type {
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 */
05ca94c0 231static int validate_command_options(enum session_type 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 */
05ca94c0
JR
251 if ((opt_live_timer && type != SESSION_LIVE) ||
252 (opt_snapshot && type != SESSION_SNAPSHOT)) {
4a102b75
JR
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,
05ca94c0 270 enum session_type session_type,
227bc702 271 int live_timer,
05ca94c0 272 enum output_type output_type,
227bc702
JR
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,
05ca94c0 388 enum session_type session_type,
227bc702 389 int live_timer,
05ca94c0 390 enum output_type output_type,
227bc702
JR
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,
05ca94c0 453 enum session_type *session_type,
4a102b75 454 int *live_timer,
05ca94c0 455 enum output_type *output_type,
4a102b75
JR
456 char **url,
457 char **ctrl_url,
458 char **data_url,
459 char **shm_path)
460{
461 int ret = 0;
af7f3c88 462 int printed_byte;
4a102b75
JR
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;
af7f3c88
JR
499 printed_byte = asprintf(url, "file://%s", raw_value);
500 if (printed_byte < 0) {
4a102b75
JR
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;
af7f3c88
JR
536 printed_byte = asprintf(url, "file://%s", raw_value);
537 if (printed_byte < 0) {
4a102b75
JR
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;
05ca94c0
JR
560 case SESSION_UNKNOWN:
561 ret = -1;
562 goto error;
4a102b75
JR
563 }
564
565
566 /* shared memory path */
567 *shm_path = config_document_get_element_value(template,"/sessions/session/shared_memory_path");
568
569error:
570 free(raw_value);
571 return ret;
572
573}
574static int create_session_from_template(struct config_document *template,
575 const char *session_name,
05ca94c0 576 enum session_type session_type,
4a102b75 577 int live_timer,
05ca94c0 578 enum output_type output_type,
4a102b75
JR
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;
af7f3c88 586 int printed_byte;
4a102b75
JR
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")) {
af7f3c88
JR
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
4a102b75
JR
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 }
2f0e04ab 725
4a102b75 726 config_element_free(temp_element);
2f0e04ab 727 temp_element = NULL;
4a102b75
JR
728
729 temp_element = config_element_create("data_uri", tmp_data_uri);
730
731 if (!temp_element_child) {
732 ERR("Could not create data_uri configuration");
733 ret = CMD_ERROR;
734 goto error;
735 }
736
737 ret = config_element_add_child(temp_element_child, temp_element);
738 if (ret) {
739 ERR("Could not append data uri to the net_output node configuration");
740 ret = CMD_ERROR;
741 goto error;
742 }
743 config_element_free(temp_element);
2f0e04ab 744 temp_element = NULL;
4a102b75
JR
745 break;
746 default:
747 ret = CMD_ERROR;
748 goto error;
749 }
750
751 temp_element = config_element_create("destination", NULL);
752 if (!temp_element) {
753 ERR("Could not create destination node configuration");
754 ret = CMD_ERROR;
755 goto error;
756 }
757
758 ret = config_element_add_child(temp_element, temp_element_child);
759 if (ret) {
760 ERR("Could not append output data to the destination node configuration");
761 ret = CMD_ERROR;
762 goto error;
763 }
764
2f0e04ab
JR
765 config_element_free(temp_element_child);
766 temp_element_child = NULL;
767
e0988a8c
JR
768 /* Construct the output/consumer_output/ node*/
769 temp_element_child = temp_element;
770 temp_element = config_element_create("consumer_output", NULL);
771 if (!temp_element) {
772 ERR("Could not create consumer_output node configuration");
773 ret = CMD_ERROR;
774 goto error;
775 }
776
777 ret = config_element_add_child(temp_element, temp_element_child);
778 if (ret) {
779 ERR("Could not append output data to the consumer_output node configuration");
780 ret = CMD_ERROR;
781 goto error;
782 }
783
784 config_element_free(temp_element_child);
785 temp_element_child = config_element_create("enabled", "true");
786 if (!temp_element_child) {
787 ERR("Could not create enbaled node configuration");
788 ret = CMD_ERROR;
789 goto error;
790 }
791
792 ret = config_element_add_child(temp_element, temp_element_child);
793 if (ret) {
794 ERR("Could not append node to the consumer_output node configuration");
795 ret = CMD_ERROR;
796 goto error;
797 }
798
799 config_element_free(temp_element_child);
800 temp_element_child = NULL;
801
802
803 temp_element_child = temp_element;
804 temp_element = config_element_create("output", NULL);
805 if (!temp_element) {
806 ERR("Could not create output node configuration");
807 ret = CMD_ERROR;
808 goto error;
809 }
810
811 ret = config_element_add_child(temp_element, temp_element_child);
812 if (ret) {
813 ERR("Could not append output data to the output node configuration");
814 ret = CMD_ERROR;
815 goto error;
816 }
817
818 config_element_free(temp_element_child);
819 temp_element_child = NULL;
820
2f0e04ab 821
4a102b75
JR
822 /*
823 * validate and replace the destination node for each session type
824 * TODO: export string as const and simply assign a base path for the
825 * destination node based on the session type
826 **/
827 switch (session_type) {
828 case SESSION_NORMAL:
829 case SESSION_LIVE:
e0988a8c
JR
830 break;
831 case SESSION_SNAPSHOT:
832 /* construct the output/snapshots_outputs/ */
833 temp_element_child = temp_element;
834 temp_element = config_element_create("snapshot_outputs", NULL);
835 if (!temp_element) {
836 ERR("Could not create snapshot_outputs node configuration");
4a102b75
JR
837 ret = CMD_ERROR;
838 goto error;
839 }
840
e0988a8c
JR
841 ret = config_element_add_child(temp_element, temp_element_child);
842 if (ret) {
843 ERR("Could not append output data to the snapshot_outputs node configuration");
4a102b75
JR
844 ret = CMD_ERROR;
845 goto error;
846 }
847
e0988a8c
JR
848 config_element_free(temp_element_child);
849 temp_element_child = NULL;
850
851 temp_element_child = temp_element;
852 temp_element = config_element_create("output", NULL);
853 if (!temp_element) {
854 ERR("Could not create output node configuration");
855 ret = CMD_ERROR;
856 goto error;
857 }
858
859 ret = config_element_add_child(temp_element, temp_element_child);
860 if (ret) {
861 ERR("Could not append output data to the output node configuration");
862 ret = CMD_ERROR;
863 goto error;
864 }
865
866 config_element_free(temp_element_child);
867 temp_element_child = NULL;
4a102b75
JR
868 break;
869 default:
870 ERR("Invalid session type");
871 ret = CMD_UNDEFINED;
872 goto error;
873 }
874
e0988a8c
JR
875 if (!config_document_element_exist(template, "/sessions/session/output")) {
876 ret = config_document_insert_element(template, "/sessions/session", temp_element);
877 } else {
878 ret = config_document_replace_element(template, "/sessions/session/output", temp_element);
879 }
880
2f0e04ab
JR
881 config_element_free(temp_element);
882 temp_element = NULL;
4a102b75 883
0709e00e
JR
884 if (ret) {
885 ERR("%s", lttng_strerror(ret));
886 ret = CMD_ERROR;
887 goto error;
888 }
889
4a102b75
JR
890
891 /* Shm path */
892 if (shm_path && config_document_element_exist(template, "/sessions/session/shared_memory_path")) {
893 /* Replace the node value */
894 config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path);
895 } else if (shm_path) {
896 /* insert the node */
897 temp_element = config_element_create("shared_memory_path", shm_path);
898 if (!temp_element) {
899 ERR("Could not create shared_memory_path node configuration");
900 ret = CMD_ERROR;
901 goto error;
902 }
903 ret = config_document_insert_element(template, "/sessions/session", temp_element);
904 if (ret) {
905 ERR("Could not insert shared_memory_path node configuration");
906 ret = CMD_ERROR;
907 goto error;
908 }
2f0e04ab
JR
909
910 config_element_free(temp_element);
911 temp_element = NULL;
4a102b75
JR
912 }
913
914 ret = config_load_configuration_sessions(template, session_name, 0);
915
916
917error:
918 config_element_free(temp_element);
919 config_element_free(temp_element_child);
920 free(tmp_string);
921 free(uris);
922 return ret;
923
924}
925
f3ed775e 926/*
1c8d13c8
TD
927 * Create a tracing session.
928 * If no name is specified, a default name is generated.
f3ed775e 929 *
1c8d13c8 930 * Returns one of the CMD_* result constants.
f3ed775e 931 */
a4b92340 932static int create_session(void)
f3ed775e 933{
a4b92340 934 int ret;
af7f3c88 935 int printed_byte;
227bc702 936
4a102b75
JR
937
938 /* Template */
939 struct config_document *template = NULL;
940
227bc702 941 /* Base data */
05ca94c0
JR
942 enum session_type base_session_type = SESSION_UNKNOWN;
943 enum output_type base_output_type = OUTPUT_UNKNOWN;
227bc702
JR
944 char *base_session_name = NULL;
945 char *base_url = NULL;
946 char *base_ctrl_url = NULL;
947 char *base_data_url = NULL;
948 char *base_shm_path = NULL;
949 int base_live_timer = 0;
950
951 /* Time data */
952 char datetime[16];
f3ed775e 953 time_t rawtime;
227bc702
JR
954 struct tm *timeinfo = NULL;
955
956 /* Temporary variables */
957 char *traces_path = NULL;
958 char *temp_url = NULL;
959 char *session_name_date = NULL;
960 char *tmp_url = NULL;
961 char *tmp_home_path = NULL;
962 struct lttng_uri *uris = NULL;
963 ssize_t uri_array_size = 0;
964
f3ed775e 965
d6175221
DG
966 /* Get date and time for automatic session name/path */
967 time(&rawtime);
968 timeinfo = localtime(&rawtime);
969 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
970
4a102b75
JR
971 if (opt_template_path) {
972 /* Restriction on flags exist when using template */
973 /* Session type flags are not permitted */
974 /* --live & --snapshot */
975 template = config_document_get(opt_template_path);
976 if (!template) {
977 ERR("Template could not be parsed");
978 ret = CMD_ERROR;
979 goto error;
980 }
981 /* Load info from template if any */
982 /* TODO: might want to use a struct in the end for the session... */
983 ret = parse_template(template, &base_session_name,
984 &base_session_type,
985 &base_live_timer,
986 &base_output_type,
987 &base_url,
988 &base_ctrl_url,
989 &base_data_url,
990 &base_shm_path);
8e4afd43
JR
991 if (ret) {
992 goto error;
993 }
4a102b75
JR
994 }
995
996 /* Option validation */
997 if (validate_command_options(base_session_type) != CMD_SUCCESS) {
998 ret = CMD_ERROR;
999 goto error;
1000 }
1001
227bc702
JR
1002 /* Find the session type based on options */
1003 if(base_session_type == SESSION_UNKNOWN) {
1004 if (opt_snapshot) {
1005 base_session_type = SESSION_SNAPSHOT;
1006 } else if (opt_live_timer) {
1007 base_session_type = SESSION_LIVE;
1008 } else {
1009 base_session_type = SESSION_NORMAL;
07424f16 1010 }
227bc702
JR
1011 }
1012
1013 /*
1014 * Session name handling
1015 */
1016 if (opt_session_name) {
1017 /* Override the session name */
487b253b
DG
1018 if (strlen(opt_session_name) > NAME_MAX) {
1019 ERR("Session name too long. Length must be lower or equal to %d",
1020 NAME_MAX);
1021 ret = LTTNG_ERR_SESSION_FAIL;
227bc702 1022 free(session_name_date);
487b253b
DG
1023 goto error;
1024 }
4b861950
DG
1025 /*
1026 * Check if the session name begins with "auto-" or is exactly "auto".
1027 * Both are reserved for the default session name. See bug #449 to
1028 * understand why we need to check both here.
1029 */
1030 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
227bc702
JR
1031 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
1032 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
1033 strlen(DEFAULT_SESSION_NAME)) == 0 &&
67238b20 1034 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
61b35a5a
DG
1035 ERR("%s is a reserved keyword for default session(s)",
1036 DEFAULT_SESSION_NAME);
227bc702
JR
1037
1038 ret = CMD_ERROR;
1039 goto error;
1040 }
1041
1042 base_session_name = strndup(opt_session_name, NAME_MAX);
1043 if (!base_session_name) {
1044 PERROR("Strdup session name");
61b35a5a
DG
1045 ret = CMD_ERROR;
1046 goto error;
1047 }
227bc702 1048
af7f3c88
JR
1049 printed_byte = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
1050 if (printed_byte < 0) {
227bc702 1051 PERROR("Asprintf session name");
af7f3c88 1052 ret = CMD_ERROR;
227bc702
JR
1053 goto error;
1054 }
1055 DBG("Session name from command option set to %s", base_session_name);
1056 } else if (base_session_name) {
af7f3c88
JR
1057 printed_byte = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
1058 if (printed_byte < 0) {
227bc702 1059 PERROR("Asprintf session name");
af7f3c88 1060 ret = CMD_ERROR;
227bc702
JR
1061 goto error;
1062 }
1063 } else {
1064 /* Generate a name */
af7f3c88
JR
1065 printed_byte = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
1066 if (printed_byte < 0) {
6af1d7e6 1067 PERROR("Asprintf session name");
af7f3c88 1068 ret = CMD_ERROR;
07424f16
DG
1069 goto error;
1070 }
227bc702
JR
1071 session_name_date = strdup(base_session_name);
1072 DBG("Auto session name set to %s", base_session_name);
f3ed775e
DG
1073 }
1074
227bc702
JR
1075
1076 /*
1077 * Output handling
1078 */
1079
1080 /*
1081 * If any of those options are present clear all output related data.
1082 */
1083 if (opt_output_path || opt_url || (opt_ctrl_url && opt_data_url) || opt_no_output) {
1084 /* Overwrite output */
1085 free(base_url);
1086 free(base_ctrl_url);
1087 free(base_data_url);
1088 base_url = NULL;
1089 base_ctrl_url = NULL;
1090 base_data_url = NULL;
1a241656
DG
1091 }
1092
227bc702
JR
1093 if (opt_output_path) {
1094
81b86775 1095 traces_path = utils_expand_path(opt_output_path);
227bc702 1096 if (!traces_path) {
00e2e675
DG
1097 ret = CMD_ERROR;
1098 goto error;
1099 }
1100
37d03ff7 1101 /* Create URL string from the local file system path */
af7f3c88
JR
1102 printed_byte = asprintf(&temp_url, "file://%s", traces_path);
1103 if (printed_byte < 0) {
a4b92340 1104 PERROR("asprintf url path");
00e2e675
DG
1105 ret = CMD_FATAL;
1106 goto error;
1107 }
227bc702
JR
1108
1109 base_url = temp_url;
a4b92340 1110 } else if (opt_url) { /* Handling URL (-U opt) */
227bc702 1111 base_url = strdup(opt_url);
1a241656
DG
1112 } else if (opt_data_url && opt_ctrl_url) {
1113 /*
227bc702
JR
1114 * With both control and data, we'll be setting the consumer URL
1115 * after session creation thus use no URL.
1a241656 1116 */
227bc702
JR
1117 base_ctrl_url = strdup(opt_ctrl_url);
1118 base_data_url = strdup(opt_data_url);
1119 } else if (!(opt_no_output || base_output_type == OUTPUT_NONE ||
1120 base_url || base_ctrl_url || base_data_url)) {
1121 /* Generate default output depending on the session type */
1122 switch (base_session_type) {
1123 case SESSION_NORMAL:
1124 /* fallthrough */
1125 case SESSION_SNAPSHOT:
1126 /* Default to a local path */
1127 tmp_home_path = utils_get_home_dir();
1128 if (tmp_home_path == NULL) {
1129 ERR("HOME path not found.\n \
1130 Please specify an output path using -o, --output PATH");
1131 ret = CMD_FATAL;
1132 goto error;
1133 }
ecc48a90 1134
af7f3c88 1135 printed_byte = asprintf(&tmp_url,
227bc702
JR
1136 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
1137 tmp_home_path, session_name_date);
16f6f820 1138
af7f3c88 1139 if (printed_byte < 0) {
227bc702
JR
1140 PERROR("asprintf trace dir name");
1141 ret = CMD_FATAL;
1142 goto error;
1143 }
8960e9cd 1144
227bc702 1145 base_url = tmp_url ;
42224349 1146 break;
227bc702
JR
1147 case SESSION_LIVE:
1148 /* Default to a net output */
af7f3c88
JR
1149 printed_byte = asprintf(&tmp_url, "net://127.0.0.1");
1150 if (printed_byte < 0) {
227bc702
JR
1151 PERROR("asprintf default live URL");
1152 ret = CMD_FATAL;
1153 goto error;
1154 }
1155 base_url = tmp_url ;
60e835ca 1156 break;
227bc702
JR
1157 default:
1158 ERR("Unknown session type");
1159 ret = CMD_FATAL;
a4b92340
DG
1160 goto error;
1161 }
4f50c803
DG
1162 }
1163
227bc702
JR
1164 /*
1165 * Shared memory path handling
1166 */
d7ba1388 1167 if (opt_shm_path) {
af7f3c88
JR
1168 /* Overwrite shm_path so clear any previously defined one */
1169 free(base_shm_path);
1170 printed_byte = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
1171 if (printed_byte < 0) {
227bc702 1172 PERROR("asprintf shm_path");
af7f3c88 1173 ret = CMD_FATAL;
d7ba1388
MD
1174 goto error;
1175 }
1176 }
1177
227bc702
JR
1178 /*
1179 * Live timer handling
1180 */
1181 if (opt_live_timer) {
1182 base_live_timer = opt_live_timer;
1183 }
d73c5802 1184
227bc702
JR
1185 /* Get output type from urls */
1186 if (base_url) {
1187 /* Get lttng uris from single url */
1188 uri_array_size = uri_parse_str_urls(base_url, NULL, &uris);
1189 if (uri_array_size < 0) {
1190 ret = CMD_ERROR;
1191 goto error;
d73c5802 1192 }
227bc702
JR
1193 } else if (base_ctrl_url && base_data_url) {
1194 uri_array_size = uri_parse_str_urls(base_ctrl_url, base_data_url, &uris);
1195 if (uri_array_size < 0) {
1196 ret = CMD_ERROR;
1197 goto error;
16f6f820 1198 }
227bc702
JR
1199 } else {
1200 /* --no-output */
1201 uri_array_size = 0;
1202 }
1203
1204 switch (uri_array_size) {
1205 case 0:
1206 base_output_type = OUTPUT_NONE;
1207 break;
1208 case 1:
1209 base_output_type = OUTPUT_LOCAL;
1210 break;
1211 case 2:
1212 base_output_type = OUTPUT_NET;
1213 break;
1214 default:
1215 ret = CMD_ERROR;
1216 goto error;
a4b92340 1217 }
227bc702 1218
4a102b75
JR
1219 if (template) {
1220 ret = create_session_from_template(template,
1221 base_session_name,
1222 base_session_type,
1223 base_live_timer,
1224 base_output_type,
1225 base_url,
1226 base_ctrl_url,
1227 base_data_url,
1228 base_shm_path,
1229 datetime);
1230 } else {
1231 ret = create_session_basic (base_session_name,
1232 base_session_type,
1233 base_live_timer,
1234 base_output_type,
1235 base_url,
1236 base_ctrl_url,
1237 base_data_url,
1238 base_shm_path,
1239 datetime);
1240 }
227bc702
JR
1241 if (ret) {
1242 goto error;
d7ba1388 1243 }
a4b92340 1244
227bc702
JR
1245 ret = generate_output (base_session_name,
1246 base_session_type,
1247 base_live_timer,
1248 base_output_type,
1249 base_url,
1250 base_ctrl_url,
1251 base_data_url,
1252 base_shm_path);
1253 if (ret) {
1254 goto error;
37d03ff7
JRJ
1255 }
1256
58a97671 1257 /* Init lttng session config */
227bc702 1258 ret = config_init(base_session_name);
f3ed775e 1259 if (ret < 0) {
27089920 1260 ret = CMD_ERROR;
f3ed775e
DG
1261 goto error;
1262 }
1263
f3ed775e
DG
1264 ret = CMD_SUCCESS;
1265
1266error:
227bc702 1267 /* Session temp stuff */
2f0e04ab 1268 config_document_free(template);
227bc702 1269 free(session_name_date);
227bc702 1270 free(uris);
2f0e04ab 1271 free(traces_path);
a4b92340
DG
1272
1273 if (ret < 0) {
1274 ERR("%s", lttng_strerror(ret));
1275 }
67238b20 1276
227bc702
JR
1277 free(base_session_name);
1278 free(base_url);
1279 free(base_ctrl_url);
1280 free(base_data_url);
1281 free(base_shm_path);
f3ed775e
DG
1282 return ret;
1283}
1284
92360082
JG
1285/*
1286 * spawn_sessiond
1287 *
1288 * Spawn a session daemon by forking and execv.
1289 */
1290static int spawn_sessiond(char *pathname)
1291{
1292 int ret = 0;
1293 pid_t pid;
1294
1295 MSG("Spawning a session daemon");
92360082
JG
1296 pid = fork();
1297 if (pid == 0) {
1298 /*
bbd44cae 1299 * Spawn session daemon in daemon mode.
92360082 1300 */
bbd44cae
PP
1301 execlp(pathname, "lttng-sessiond",
1302 "--daemonize", NULL);
92360082
JG
1303 /* execlp only returns if error happened */
1304 if (errno == ENOENT) {
1305 ERR("No session daemon found. Use --sessiond-path.");
1306 } else {
1307 PERROR("execlp");
1308 }
1309 kill(getppid(), SIGTERM); /* wake parent */
1310 exit(EXIT_FAILURE);
1311 } else if (pid > 0) {
92360082 1312 /*
bbd44cae
PP
1313 * In daemon mode (--daemonize), sessiond only exits when
1314 * it's ready to accept commands.
92360082 1315 */
bbd44cae 1316 for (;;) {
81527d36
JG
1317 int status;
1318 pid_t wait_pid_ret = waitpid(pid, &status, 0);
1319
1320 if (wait_pid_ret < 0) {
1321 if (errno == EINTR) {
1322 continue;
1323 }
1324 PERROR("waitpid");
1325 ret = -errno;
1326 goto end;
1327 }
bbd44cae
PP
1328
1329 if (WIFSIGNALED(status)) {
1330 ERR("Session daemon was killed by signal %d",
1331 WTERMSIG(status));
1332 ret = -1;
1333 goto end;
1334 } else if (WIFEXITED(status)) {
1335 DBG("Session daemon terminated normally (exit status: %d)",
1336 WEXITSTATUS(status));
1337
1338 if (WEXITSTATUS(status) != 0) {
1339 ERR("Session daemon terminated with an error (exit status: %d)",
1340 WEXITSTATUS(status));
1341 ret = -1;
1342 goto end;
1343 }
1344 break;
1345 }
92360082 1346 }
bbd44cae 1347
92360082
JG
1348 goto end;
1349 } else {
1350 PERROR("fork");
1351 ret = -1;
1352 goto end;
1353 }
1354
1355end:
1356 return ret;
1357}
1358
1359/*
1360 * launch_sessiond
1361 *
1362 * Check if the session daemon is available using
1363 * the liblttngctl API for the check. If not, try to
1364 * spawn a daemon.
1365 */
1366static int launch_sessiond(void)
1367{
1368 int ret;
1369 char *pathname = NULL;
1370
1371 ret = lttng_session_daemon_alive();
1372 if (ret) {
1373 /* Sessiond is alive, not an error */
1374 ret = 0;
1375 goto end;
1376 }
1377
1378 /* Try command line option path */
1379 pathname = opt_sessiond_path;
1380
1381 /* Try LTTNG_SESSIOND_PATH env variable */
1382 if (pathname == NULL) {
1383 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
1384 }
1385
1386 /* Try with configured path */
1387 if (pathname == NULL) {
1388 if (CONFIG_SESSIOND_BIN[0] != '\0') {
1389 pathname = CONFIG_SESSIOND_BIN;
1390 }
1391 }
1392
1393 /* Try the default path */
1394 if (pathname == NULL) {
1395 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
1396 }
1397
1398 DBG("Session daemon binary path: %s", pathname);
1399
1400 /* Check existence and permissions */
1401 ret = access(pathname, F_OK | X_OK);
1402 if (ret < 0) {
1403 ERR("No such file or access denied: %s", pathname);
1404 goto end;
1405 }
1406
1407 ret = spawn_sessiond(pathname);
92360082 1408end:
0f4fa0d2
JG
1409 if (ret) {
1410 ERR("Problem occurred while launching session daemon (%s)",
1411 pathname);
1412 }
92360082
JG
1413 return ret;
1414}
1415
f3ed775e 1416/*
74cc1d0f 1417 * The 'create <options>' first level command
1c8d13c8
TD
1418 *
1419 * Returns one of the CMD_* result constants.
f3ed775e
DG
1420 */
1421int cmd_create(int argc, const char **argv)
1422{
37d03ff7 1423 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
ecc48a90 1424 char *opt_arg = NULL;
f3ed775e
DG
1425 static poptContext pc;
1426
1427 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1428 poptReadDefaultConfig(pc, 0);
1429
1430 while ((opt = poptGetNextOpt(pc)) != -1) {
1431 switch (opt) {
1432 case OPT_HELP:
4ba92f18 1433 SHOW_HELP();
f3ed775e 1434 goto end;
679b4943
SM
1435 case OPT_LIST_OPTIONS:
1436 list_cmd_options(stdout, long_options);
679b4943 1437 goto end;
ecc48a90
JD
1438 case OPT_LIVE_TIMER:
1439 {
1440 unsigned long v;
1441
1442 errno = 0;
1443 opt_arg = poptGetOptArg(pc);
d73c5802
DG
1444 if (!opt_arg) {
1445 /* Set up default values. */
1446 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
1447 DBG("Session live timer interval set to default value %d",
1448 opt_live_timer);
1449 break;
1450 }
1451
ecc48a90
JD
1452 v = strtoul(opt_arg, NULL, 0);
1453 if (errno != 0 || !isdigit(opt_arg[0])) {
1454 ERR("Wrong value in --live parameter: %s", opt_arg);
1455 ret = CMD_ERROR;
1456 goto end;
1457 }
1458 if (v != (uint32_t) v) {
1459 ERR("32-bit overflow in --live parameter: %s", opt_arg);
1460 ret = CMD_ERROR;
1461 goto end;
1462 }
0ed9e0be
JG
1463 if (v == 0) {
1464 ERR("Live timer interval must be greater than zero");
1465 ret = CMD_ERROR;
1466 goto end;
1467 }
ecc48a90
JD
1468 opt_live_timer = (uint32_t) v;
1469 DBG("Session live timer interval set to %d", opt_live_timer);
1470 break;
1471 }
f3ed775e 1472 default:
f3ed775e
DG
1473 ret = CMD_UNDEFINED;
1474 goto end;
1475 }
1476 }
1477
785d2d0d 1478 if (opt_no_consumer) {
96fe6b8d 1479 MSG("The option --no-consumer is obsolete. Use --no-output now.");
785d2d0d
DG
1480 ret = CMD_WARNING;
1481 goto end;
1482 }
1483
92360082
JG
1484 /* Spawn a session daemon if needed */
1485 if (!opt_no_sessiond) {
1486 ret = launch_sessiond();
1487 if (ret) {
1488 ret = CMD_ERROR;
1489 goto end;
1490 }
1491 }
1492
acc09215 1493 /* MI initialization */
37d03ff7
JRJ
1494 if (lttng_opt_mi) {
1495 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1496 if (!writer) {
1497 ret = -LTTNG_ERR_NOMEM;
1498 goto end;
1499 }
1500
1501 /* Open command element */
1502 ret = mi_lttng_writer_command_open(writer,
1503 mi_lttng_element_command_create);
1504 if (ret) {
1505 ret = CMD_ERROR;
1506 goto end;
1507 }
1508
1509 /* Open output element */
1510 ret = mi_lttng_writer_open_element(writer,
1511 mi_lttng_element_command_output);
1512 if (ret) {
1513 ret = CMD_ERROR;
1514 goto end;
1515 }
1516 }
f3ed775e
DG
1517 opt_session_name = (char*) poptGetArg(pc);
1518
37d03ff7 1519 command_ret = create_session();
227bc702 1520
37d03ff7
JRJ
1521 if (command_ret) {
1522 success = 0;
1523 }
1524
1525 if (lttng_opt_mi) {
1526 /* Close output element */
1527 ret = mi_lttng_writer_close_element(writer);
1528 if (ret) {
1529 ret = CMD_ERROR;
1530 goto end;
1531 }
1532
1533 /* Success ? */
1534 ret = mi_lttng_writer_write_element_bool(writer,
1535 mi_lttng_element_command_success, success);
1536 if (ret) {
1537 ret = CMD_ERROR;
1538 goto end;
1539 }
1540
1541 /* Command element close */
1542 ret = mi_lttng_writer_command_close(writer);
1543 if (ret) {
1544 ret = CMD_ERROR;
1545 goto end;
1546 }
1547 }
f3ed775e
DG
1548
1549end:
37d03ff7
JRJ
1550 /* Mi clean-up */
1551 if (writer && mi_lttng_writer_destroy(writer)) {
1552 /* Preserve original error code */
1553 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
1554 }
1555
acc09215 1556 /* Overwrite ret if an error occurred in create_session() */
37d03ff7
JRJ
1557 ret = command_ret ? command_ret : ret;
1558
ca1c3607 1559 poptFreeContext(pc);
f3ed775e
DG
1560 return ret;
1561}
This page took 0.128812 seconds and 5 git commands to generate.