lttng: remove usage strings from commands
[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
34 #include "../command.h"
35 #include "../utils.h"
36
37 #include <common/defaults.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/uri.h>
40 #include <common/utils.h>
41 #include <lttng/snapshot.h>
42
43 static char *opt_output_path;
44 static char *opt_session_name;
45 static char *opt_url;
46 static char *opt_ctrl_url;
47 static char *opt_data_url;
48 static char *opt_shm_path;
49 static int opt_no_consumer;
50 static int opt_no_output;
51 static int opt_snapshot;
52 static unsigned int opt_live_timer;
53
54 enum {
55 OPT_HELP = 1,
56 OPT_LIST_OPTIONS,
57 OPT_LIVE_TIMER,
58 };
59
60 static struct mi_writer *writer;
61
62 static struct poptOption long_options[] = {
63 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
64 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
65 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
66 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
67 {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0},
68 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
69 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
70 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
71 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
72 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
73 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
74 {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
75 {0, 0, 0, 0, 0, 0, 0}
76 };
77
78 /*
79 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
80 * why this declaration exists and used ONLY in for this command.
81 */
82 extern int _lttng_create_session_ext(const char *name, const char *url,
83 const char *datetime);
84
85 /*
86 * Retrieve the created session and mi output it based on provided argument
87 * This is currently a summary of what was pretty printed and is subject to
88 * enhancements.
89 */
90 static int mi_created_session(const char *session_name)
91 {
92 int ret, i, count, found;
93 struct lttng_session *sessions;
94
95 /* session_name should not be null */
96 assert(session_name);
97 assert(writer);
98
99 count = lttng_list_sessions(&sessions);
100 if (count < 0) {
101 ret = count;
102 ERR("%s", lttng_strerror(ret));
103 goto error;
104 }
105
106 if (count == 0) {
107 ERR("Error session creation failed: session %s not found", session_name);
108 ret = -LTTNG_ERR_SESS_NOT_FOUND;
109 goto end;
110 }
111
112 found = 0;
113 for (i = 0; i < count; i++) {
114 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
115 found = 1;
116 ret = mi_lttng_session(writer, &sessions[i], 0);
117 if (ret) {
118 goto error;
119 }
120 break;
121 }
122 }
123
124 if (!found) {
125 ret = -LTTNG_ERR_SESS_NOT_FOUND;
126 } else {
127 ret = CMD_SUCCESS;
128 }
129
130 error:
131 free(sessions);
132 end:
133 return ret;
134 }
135
136 /*
137 * For a session name, set the consumer URLs.
138 */
139 static int set_consumer_url(const char *session_name, const char *ctrl_url,
140 const char *data_url)
141 {
142 int ret;
143 struct lttng_handle *handle;
144 struct lttng_domain dom;
145
146 assert(session_name);
147
148 /*
149 * Set handle with the session name and the domain set to 0. This means to
150 * the session daemon that the next action applies on the tracing session
151 * rather then the domain specific session.
152 */
153 memset(&dom, 0, sizeof(dom));
154
155 handle = lttng_create_handle(session_name, &dom);
156 if (handle == NULL) {
157 ret = CMD_FATAL;
158 goto error;
159 }
160
161 ret = lttng_set_consumer_url(handle, ctrl_url, data_url);
162 if (ret < 0) {
163 goto error;
164 }
165
166 if (ctrl_url) {
167 MSG("Control URL %s set for session %s", ctrl_url, session_name);
168 }
169
170 if (data_url) {
171 MSG("Data URL %s set for session %s", data_url, session_name);
172 }
173
174 error:
175 lttng_destroy_handle(handle);
176 return ret;
177 }
178
179 static int add_snapshot_output(const char *session_name, const char *ctrl_url,
180 const char *data_url)
181 {
182 int ret;
183 struct lttng_snapshot_output *output = NULL;
184
185 assert(session_name);
186
187 output = lttng_snapshot_output_create();
188 if (!output) {
189 ret = CMD_FATAL;
190 goto error_create;
191 }
192
193 if (ctrl_url) {
194 ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output);
195 if (ret < 0) {
196 goto error;
197 }
198 }
199
200 if (data_url) {
201 ret = lttng_snapshot_output_set_data_url(data_url, output);
202 if (ret < 0) {
203 goto error;
204 }
205 }
206
207 /* This call, if successful, populates the id of the output object. */
208 ret = lttng_snapshot_add_output(session_name, output);
209 if (ret < 0) {
210 goto error;
211 }
212
213 error:
214 lttng_snapshot_output_destroy(output);
215 error_create:
216 return ret;
217 }
218
219 /*
220 * Create a tracing session.
221 * If no name is specified, a default name is generated.
222 *
223 * Returns one of the CMD_* result constants.
224 */
225 static int create_session(void)
226 {
227 int ret;
228 char *session_name = NULL, *traces_path = NULL, *alloc_path = NULL;
229 char *alloc_url = NULL, *url = NULL, datetime[16];
230 char session_name_date[NAME_MAX + 17], *print_str_url = NULL;
231 time_t rawtime;
232 struct tm *timeinfo;
233 char shm_path[PATH_MAX] = "";
234
235 /* Get date and time for automatic session name/path */
236 time(&rawtime);
237 timeinfo = localtime(&rawtime);
238 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
239
240 /* Auto session name creation */
241 if (opt_session_name == NULL) {
242 ret = snprintf(session_name_date, sizeof(session_name_date),
243 DEFAULT_SESSION_NAME "-%s", datetime);
244 if (ret < 0) {
245 PERROR("snprintf session name");
246 goto error;
247 }
248 session_name = session_name_date;
249 DBG("Auto session name set to %s", session_name_date);
250 } else {
251 if (strlen(opt_session_name) > NAME_MAX) {
252 ERR("Session name too long. Length must be lower or equal to %d",
253 NAME_MAX);
254 ret = LTTNG_ERR_SESSION_FAIL;
255 goto error;
256 }
257 /*
258 * Check if the session name begins with "auto-" or is exactly "auto".
259 * Both are reserved for the default session name. See bug #449 to
260 * understand why we need to check both here.
261 */
262 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
263 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
264 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
265 strlen(DEFAULT_SESSION_NAME)) == 0 &&
266 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
267 ERR("%s is a reserved keyword for default session(s)",
268 DEFAULT_SESSION_NAME);
269 ret = CMD_ERROR;
270 goto error;
271 }
272 session_name = opt_session_name;
273 ret = snprintf(session_name_date, sizeof(session_name_date),
274 "%s-%s", session_name, datetime);
275 if (ret < 0) {
276 PERROR("snprintf session name");
277 goto error;
278 }
279 }
280
281 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
282 ERR("You need both control and data URL.");
283 ret = CMD_ERROR;
284 goto error;
285 }
286
287 if (opt_output_path != NULL) {
288 traces_path = utils_expand_path(opt_output_path);
289 if (traces_path == NULL) {
290 ret = CMD_ERROR;
291 goto error;
292 }
293
294 /* Create URL string from the local file system path */
295 ret = asprintf(&alloc_url, "file://%s", traces_path);
296 if (ret < 0) {
297 PERROR("asprintf url path");
298 ret = CMD_FATAL;
299 goto error;
300 }
301 /* URL to use in the lttng_create_session() call */
302 url = alloc_url;
303 print_str_url = traces_path;
304 } else if (opt_url) { /* Handling URL (-U opt) */
305 url = opt_url;
306 print_str_url = url;
307 } else if (opt_data_url && opt_ctrl_url) {
308 /*
309 * With both control and data, we'll be setting the consumer URL after
310 * session creation thus use no URL.
311 */
312 url = NULL;
313 } else if (!opt_no_output) {
314 char *tmp_path;
315
316 /* Auto output path */
317 tmp_path = utils_get_home_dir();
318 if (tmp_path == NULL) {
319 ERR("HOME path not found.\n \
320 Please specify an output path using -o, --output PATH");
321 ret = CMD_FATAL;
322 goto error;
323 }
324 alloc_path = strdup(tmp_path);
325 if (!alloc_path) {
326 PERROR("allocating alloc_path");
327 ret = CMD_FATAL;
328 goto error;
329 }
330 ret = asprintf(&alloc_url,
331 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
332 alloc_path, session_name_date);
333 if (ret < 0) {
334 PERROR("asprintf trace dir name");
335 ret = CMD_FATAL;
336 goto error;
337 }
338
339 url = alloc_url;
340 print_str_url = alloc_url + strlen("file://");
341 } else {
342 /* No output means --no-output or --snapshot mode. */
343 url = NULL;
344 }
345
346 /* Use default live URL if NO url is/are found. */
347 if ((opt_live_timer && !opt_url) && (opt_live_timer && !opt_data_url)) {
348 ret = asprintf(&alloc_url, "net://127.0.0.1");
349 if (ret < 0) {
350 PERROR("asprintf default live URL");
351 ret = CMD_FATAL;
352 goto error;
353 }
354 url = alloc_url;
355 print_str_url = url;
356 }
357
358 if (opt_snapshot && opt_live_timer) {
359 ERR("Snapshot and live modes are mutually exclusive.");
360 ret = CMD_ERROR;
361 goto error;
362 }
363
364 if (opt_snapshot) {
365 /* No output by default. */
366 const char *snapshot_url = NULL;
367
368 if (opt_url) {
369 snapshot_url = url;
370 } else if (!opt_data_url && !opt_ctrl_url) {
371 /* This is the session path that we need to use as output. */
372 snapshot_url = url;
373 }
374 ret = lttng_create_session_snapshot(session_name, snapshot_url);
375 } else if (opt_live_timer) {
376 const char *pathname;
377
378 if (opt_relayd_path) {
379 pathname = opt_relayd_path;
380 } else {
381 pathname = INSTALL_BIN_PATH "/lttng-relayd";
382 }
383 if (!opt_url && !opt_data_url && !check_relayd() &&
384 spawn_relayd(pathname, 0) < 0) {
385 goto error;
386 }
387 ret = lttng_create_session_live(session_name, url, opt_live_timer);
388 } else {
389 ret = _lttng_create_session_ext(session_name, url, datetime);
390 }
391 if (ret < 0) {
392 /* Don't set ret so lttng can interpret the sessiond error. */
393 switch (-ret) {
394 case LTTNG_ERR_EXIST_SESS:
395 WARN("Session %s already exists", session_name);
396 break;
397 default:
398 break;
399 }
400 goto error;
401 }
402
403 if (opt_ctrl_url && opt_data_url) {
404 if (opt_snapshot) {
405 ret = add_snapshot_output(session_name, opt_ctrl_url,
406 opt_data_url);
407 } else {
408 /* Setting up control URI (-C or/and -D opt) */
409 ret = set_consumer_url(session_name, opt_ctrl_url, opt_data_url);
410 }
411 if (ret < 0) {
412 /* Destroy created session because the URL are not valid. */
413 lttng_destroy_session(session_name);
414 goto error;
415 }
416 }
417
418 if (opt_shm_path) {
419 ret = snprintf(shm_path, sizeof(shm_path),
420 "%s/%s", opt_shm_path, session_name_date);
421 if (ret < 0) {
422 PERROR("snprintf shm_path");
423 goto error;
424 }
425
426 ret = lttng_set_session_shm_path(session_name, shm_path);
427 if (ret < 0) {
428 lttng_destroy_session(session_name);
429 goto error;
430 }
431 }
432
433 MSG("Session %s created.", session_name);
434 if (print_str_url && !opt_snapshot) {
435 MSG("Traces will be written in %s", print_str_url);
436
437 if (opt_live_timer) {
438 MSG("Live timer set to %u usec", opt_live_timer);
439 }
440 } else if (opt_snapshot) {
441 if (print_str_url) {
442 MSG("Default snapshot output set to: %s", print_str_url);
443 }
444 MSG("Snapshot mode set. Every channel enabled for that session will "
445 "be set in overwrite mode and mmap output.");
446 }
447 if (opt_shm_path) {
448 MSG("Session %s set to shm_path: %s.", session_name,
449 shm_path);
450 }
451
452 /* Mi output */
453 if (lttng_opt_mi) {
454 ret = mi_created_session(session_name);
455 if (ret) {
456 ret = CMD_ERROR;
457 goto error;
458 }
459 }
460
461 /* Init lttng session config */
462 ret = config_init(session_name);
463 if (ret < 0) {
464 ret = CMD_ERROR;
465 goto error;
466 }
467
468 ret = CMD_SUCCESS;
469
470 error:
471 free(alloc_url);
472 free(traces_path);
473 free(alloc_path);
474
475 if (ret < 0) {
476 ERR("%s", lttng_strerror(ret));
477 }
478 return ret;
479 }
480
481 /*
482 * spawn_sessiond
483 *
484 * Spawn a session daemon by forking and execv.
485 */
486 static int spawn_sessiond(char *pathname)
487 {
488 int ret = 0;
489 pid_t pid;
490
491 MSG("Spawning a session daemon");
492 pid = fork();
493 if (pid == 0) {
494 /*
495 * Spawn session daemon in daemon mode.
496 */
497 execlp(pathname, "lttng-sessiond",
498 "--daemonize", NULL);
499 /* execlp only returns if error happened */
500 if (errno == ENOENT) {
501 ERR("No session daemon found. Use --sessiond-path.");
502 } else {
503 PERROR("execlp");
504 }
505 kill(getppid(), SIGTERM); /* wake parent */
506 exit(EXIT_FAILURE);
507 } else if (pid > 0) {
508 /*
509 * In daemon mode (--daemonize), sessiond only exits when
510 * it's ready to accept commands.
511 */
512 for (;;) {
513 int status;
514 pid_t wait_pid_ret = waitpid(pid, &status, 0);
515
516 if (wait_pid_ret < 0) {
517 if (errno == EINTR) {
518 continue;
519 }
520 PERROR("waitpid");
521 ret = -errno;
522 goto end;
523 }
524
525 if (WIFSIGNALED(status)) {
526 ERR("Session daemon was killed by signal %d",
527 WTERMSIG(status));
528 ret = -1;
529 goto end;
530 } else if (WIFEXITED(status)) {
531 DBG("Session daemon terminated normally (exit status: %d)",
532 WEXITSTATUS(status));
533
534 if (WEXITSTATUS(status) != 0) {
535 ERR("Session daemon terminated with an error (exit status: %d)",
536 WEXITSTATUS(status));
537 ret = -1;
538 goto end;
539 }
540 break;
541 }
542 }
543
544 goto end;
545 } else {
546 PERROR("fork");
547 ret = -1;
548 goto end;
549 }
550
551 end:
552 return ret;
553 }
554
555 /*
556 * launch_sessiond
557 *
558 * Check if the session daemon is available using
559 * the liblttngctl API for the check. If not, try to
560 * spawn a daemon.
561 */
562 static int launch_sessiond(void)
563 {
564 int ret;
565 char *pathname = NULL;
566
567 ret = lttng_session_daemon_alive();
568 if (ret) {
569 /* Sessiond is alive, not an error */
570 ret = 0;
571 goto end;
572 }
573
574 /* Try command line option path */
575 pathname = opt_sessiond_path;
576
577 /* Try LTTNG_SESSIOND_PATH env variable */
578 if (pathname == NULL) {
579 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
580 }
581
582 /* Try with configured path */
583 if (pathname == NULL) {
584 if (CONFIG_SESSIOND_BIN[0] != '\0') {
585 pathname = CONFIG_SESSIOND_BIN;
586 }
587 }
588
589 /* Try the default path */
590 if (pathname == NULL) {
591 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
592 }
593
594 DBG("Session daemon binary path: %s", pathname);
595
596 /* Check existence and permissions */
597 ret = access(pathname, F_OK | X_OK);
598 if (ret < 0) {
599 ERR("No such file or access denied: %s", pathname);
600 goto end;
601 }
602
603 ret = spawn_sessiond(pathname);
604 end:
605 if (ret) {
606 ERR("Problem occurred while launching session daemon (%s)",
607 pathname);
608 }
609 return ret;
610 }
611
612 /*
613 * The 'create <options>' first level command
614 *
615 * Returns one of the CMD_* result constants.
616 */
617 int cmd_create(int argc, const char **argv)
618 {
619 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
620 char *opt_arg = NULL;
621 static poptContext pc;
622
623 pc = poptGetContext(NULL, argc, argv, long_options, 0);
624 poptReadDefaultConfig(pc, 0);
625
626 while ((opt = poptGetNextOpt(pc)) != -1) {
627 switch (opt) {
628 case OPT_HELP:
629 SHOW_HELP();
630 goto end;
631 case OPT_LIST_OPTIONS:
632 list_cmd_options(stdout, long_options);
633 goto end;
634 case OPT_LIVE_TIMER:
635 {
636 unsigned long v;
637
638 errno = 0;
639 opt_arg = poptGetOptArg(pc);
640 if (!opt_arg) {
641 /* Set up default values. */
642 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
643 DBG("Session live timer interval set to default value %d",
644 opt_live_timer);
645 break;
646 }
647
648 v = strtoul(opt_arg, NULL, 0);
649 if (errno != 0 || !isdigit(opt_arg[0])) {
650 ERR("Wrong value in --live parameter: %s", opt_arg);
651 ret = CMD_ERROR;
652 goto end;
653 }
654 if (v != (uint32_t) v) {
655 ERR("32-bit overflow in --live parameter: %s", opt_arg);
656 ret = CMD_ERROR;
657 goto end;
658 }
659 if (v == 0) {
660 ERR("Live timer interval must be greater than zero");
661 ret = CMD_ERROR;
662 goto end;
663 }
664 opt_live_timer = (uint32_t) v;
665 DBG("Session live timer interval set to %d", opt_live_timer);
666 break;
667 }
668 default:
669 ret = CMD_UNDEFINED;
670 goto end;
671 }
672 }
673
674 if (opt_no_consumer) {
675 MSG("The option --no-consumer is obsolete. Use --no-output now.");
676 ret = CMD_WARNING;
677 goto end;
678 }
679
680 /* Spawn a session daemon if needed */
681 if (!opt_no_sessiond) {
682 ret = launch_sessiond();
683 if (ret) {
684 ret = CMD_ERROR;
685 goto end;
686 }
687 }
688
689 /* MI initialization */
690 if (lttng_opt_mi) {
691 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
692 if (!writer) {
693 ret = -LTTNG_ERR_NOMEM;
694 goto end;
695 }
696
697 /* Open command element */
698 ret = mi_lttng_writer_command_open(writer,
699 mi_lttng_element_command_create);
700 if (ret) {
701 ret = CMD_ERROR;
702 goto end;
703 }
704
705 /* Open output element */
706 ret = mi_lttng_writer_open_element(writer,
707 mi_lttng_element_command_output);
708 if (ret) {
709 ret = CMD_ERROR;
710 goto end;
711 }
712 }
713 opt_session_name = (char*) poptGetArg(pc);
714
715 command_ret = create_session();
716 if (command_ret) {
717 success = 0;
718 }
719
720 if (lttng_opt_mi) {
721 /* Close output element */
722 ret = mi_lttng_writer_close_element(writer);
723 if (ret) {
724 ret = CMD_ERROR;
725 goto end;
726 }
727
728 /* Success ? */
729 ret = mi_lttng_writer_write_element_bool(writer,
730 mi_lttng_element_command_success, success);
731 if (ret) {
732 ret = CMD_ERROR;
733 goto end;
734 }
735
736 /* Command element close */
737 ret = mi_lttng_writer_command_close(writer);
738 if (ret) {
739 ret = CMD_ERROR;
740 goto end;
741 }
742 }
743
744 end:
745 /* Mi clean-up */
746 if (writer && mi_lttng_writer_destroy(writer)) {
747 /* Preserve original error code */
748 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
749 }
750
751 /* Overwrite ret if an error occurred in create_session() */
752 ret = command_ret ? command_ret : ret;
753
754 poptFreeContext(pc);
755 return ret;
756 }
This page took 0.045367 seconds and 5 git commands to generate.