Fix: leak on error in lttng-crash
[lttng-tools.git] / src / bin / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or modify
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
fac6795d
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 *
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.
fac6795d
DG
16 */
17
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
fac6795d 20#include <getopt.h>
f3ed775e 21#include <signal.h>
fac6795d
DG
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
8db8d1dc
DG
25#include <sys/types.h>
26#include <sys/wait.h>
fac6795d 27#include <unistd.h>
3bd1e081 28#include <config.h>
730389d9 29#include <ctype.h>
fac6795d 30
5b97ec60 31#include <lttng/lttng.h>
db758600 32#include <common/error.h>
e8fa9fb0 33#include <common/compat/getenv.h>
fac6795d 34
c399183f 35#include "command.h"
fac6795d
DG
36
37/* Variables */
38static char *progname;
f3ed775e
DG
39static int opt_no_sessiond;
40static char *opt_sessiond_path;
8db8d1dc 41static pid_t sessiond_pid;
5a532b68 42static volatile int recv_child_signal;
f3ed775e 43
8960e9cd
DG
44char *opt_relayd_path;
45
f3ed775e 46enum {
8960e9cd 47 OPT_RELAYD_PATH,
f3ed775e 48 OPT_SESSION_PATH,
865abf65
SM
49 OPT_DUMP_OPTIONS,
50 OPT_DUMP_COMMANDS,
f3ed775e
DG
51};
52
53/* Getopt options. No first level command. */
54static struct option long_options[] = {
70318c38 55 {"version", 0, NULL, 'V'},
f3ed775e
DG
56 {"help", 0, NULL, 'h'},
57 {"group", 1, NULL, 'g'},
58 {"verbose", 0, NULL, 'v'},
59 {"quiet", 0, NULL, 'q'},
c7e35b03 60 {"mi", 1, NULL, 'm'},
8490ae7b 61 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 62 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
8960e9cd 63 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
865abf65
SM
64 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
65 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
66 {NULL, 0, NULL, 0}
67};
68
69/* First level command */
70static struct cmd_struct commands[] = {
71 { "list", cmd_list},
72 { "create", cmd_create},
73 { "destroy", cmd_destroy},
f3ed775e
DG
74 { "start", cmd_start},
75 { "stop", cmd_stop},
76 { "enable-event", cmd_enable_events},
e953ef25 77 { "disable-event", cmd_disable_events},
d36b8583 78 { "enable-channel", cmd_enable_channels},
26cc6b4e 79 { "disable-channel", cmd_disable_channels},
d65106b1 80 { "add-context", cmd_add_context},
3087ef18 81 { "set-session", cmd_set_session},
eb9cb8b7 82 { "version", cmd_version},
d0254c7c 83 { "calibrate", cmd_calibrate},
0c95f5b2 84 { "view", cmd_view},
57f272ed 85 { "snapshot", cmd_snapshot},
c864d6d7 86 { "save", cmd_save},
8c42d845 87 { "load", cmd_load},
f3ed775e
DG
88 { NULL, NULL} /* Array closure */
89};
90
91static void usage(FILE *ofp)
8c0faa1d 92{
4c6ac053
MD
93 fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n",
94 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
32a6298d 95 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
f3ed775e
DG
96 fprintf(ofp, "\n");
97 fprintf(ofp, "Options:\n");
70318c38 98 fprintf(ofp, " -V, --version Show version\n");
99bab54f
TD
99 fprintf(ofp, " -h, --help Show this help\n");
100 fprintf(ofp, " --list-options Simple listing of lttng options\n");
101 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
102 fprintf(ofp, " -v, --verbose Increase verbosity\n");
103 fprintf(ofp, " -q, --quiet Quiet mode\n");
c7e35b03
JR
104 fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n");
105 fprintf(ofp, " Type: xml\n");
99bab54f
TD
106 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
107 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
108 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
8960e9cd 109 fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n");
f3ed775e
DG
110 fprintf(ofp, "\n");
111 fprintf(ofp, "Commands:\n");
00e2e675
DG
112 fprintf(ofp, " add-context Add context to event and/or channel\n");
113 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
114 fprintf(ofp, " create Create tracing session\n");
115 fprintf(ofp, " destroy Tear down tracing session\n");
116 fprintf(ofp, " enable-channel Enable tracing channel\n");
117 fprintf(ofp, " enable-event Enable tracing event\n");
118 fprintf(ofp, " disable-channel Disable tracing channel\n");
119 fprintf(ofp, " disable-event Disable tracing event\n");
00e2e675
DG
120 fprintf(ofp, " list List possible tracing options\n");
121 fprintf(ofp, " set-session Set current session name\n");
57f272ed 122 fprintf(ofp, " snapshot Snapshot buffers of current session name\n");
00e2e675
DG
123 fprintf(ofp, " start Start tracing\n");
124 fprintf(ofp, " stop Stop tracing\n");
125 fprintf(ofp, " version Show version information\n");
126 fprintf(ofp, " view Start trace viewer\n");
c864d6d7 127 fprintf(ofp, " save Save session configuration\n");
8c42d845 128 fprintf(ofp, " load Load session configuration\n");
f3ed775e 129 fprintf(ofp, "\n");
99bab54f
TD
130 fprintf(ofp, "Each command also has its own -h, --help option.\n");
131 fprintf(ofp, "\n");
f3ed775e
DG
132 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
133 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
8c0faa1d
DG
134}
135
70318c38
SS
136static void version(FILE *ofp)
137{
4c6ac053
MD
138 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
139 progname,
140 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
70318c38
SS
141}
142
c7e35b03
JR
143/*
144 * Find the MI output type enum from a string. This function is for the support
145 * of machine interface output.
146 */
147static int mi_output_type(const char *output_type)
148{
149 int ret = 0;
150
151 if (!strncasecmp("xml", output_type, 3)) {
152 ret = LTTNG_MI_XML;
153 } else {
154 /* Invalid output format */
155 ERR("MI output format not supported");
156 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
157 }
158
159 return ret;
160}
161
865abf65
SM
162/*
163 * list_options
164 *
165 * List options line by line. This is mostly for bash auto completion and to
166 * avoid difficult parsing.
167 */
168static void list_options(FILE *ofp)
169{
170 int i = 0;
171 struct option *option = NULL;
172
173 option = &long_options[i];
174 while (option->name != NULL) {
175 fprintf(ofp, "--%s\n", option->name);
176
177 if (isprint(option->val)) {
178 fprintf(ofp, "-%c\n", option->val);
179 }
180
181 i++;
182 option = &long_options[i];
183 }
184}
185
7442b2ba 186/*
f3ed775e 187 * clean_exit
96243366 188 */
f3ed775e 189static void clean_exit(int code)
96243366 190{
f3ed775e
DG
191 DBG("Clean exit");
192 exit(code);
894be886 193}
96243366 194
894be886 195/*
f3ed775e 196 * sighandler
2ef84c95 197 *
f3ed775e 198 * Signal handler for the daemon
2ef84c95 199 */
f3ed775e 200static void sighandler(int sig)
2ef84c95 201{
8db8d1dc
DG
202 int status;
203
f3ed775e
DG
204 switch (sig) {
205 case SIGTERM:
99bab54f 206 DBG("SIGTERM caught");
f3ed775e
DG
207 clean_exit(EXIT_FAILURE);
208 break;
209 case SIGCHLD:
99bab54f 210 DBG("SIGCHLD caught");
8db8d1dc 211 waitpid(sessiond_pid, &status, 0);
5a532b68 212 recv_child_signal = 1;
8db8d1dc
DG
213 /* Indicate that the session daemon died */
214 sessiond_pid = 0;
215 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
216 break;
217 case SIGUSR1:
218 /* Notify is done */
5a532b68 219 recv_child_signal = 1;
99bab54f 220 DBG("SIGUSR1 caught");
f3ed775e
DG
221 break;
222 default:
99bab54f 223 DBG("Unknown signal %d caught", sig);
f3ed775e 224 break;
2ef84c95
DG
225 }
226
f3ed775e 227 return;
2ef84c95
DG
228}
229
230/*
f3ed775e 231 * set_signal_handler
894be886 232 *
f3ed775e 233 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 234 */
f3ed775e 235static int set_signal_handler(void)
894be886 236{
f3ed775e
DG
237 int ret = 0;
238 struct sigaction sa;
239 sigset_t sigset;
33a2b854 240
f3ed775e 241 if ((ret = sigemptyset(&sigset)) < 0) {
6f04ed72 242 PERROR("sigemptyset");
33a2b854
DG
243 goto end;
244 }
245
f3ed775e
DG
246 sa.sa_handler = sighandler;
247 sa.sa_mask = sigset;
248 sa.sa_flags = 0;
8db8d1dc 249 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
6f04ed72 250 PERROR("sigaction");
96243366
DG
251 goto end;
252 }
253
f3ed775e 254 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
6f04ed72 255 PERROR("sigaction");
f3ed775e 256 goto end;
894be886
DG
257 }
258
8db8d1dc 259 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
6f04ed72 260 PERROR("sigaction");
8db8d1dc
DG
261 goto end;
262 }
263
96243366 264end:
57167058
DG
265 return ret;
266}
267
fac6795d 268/*
f3ed775e 269 * handle_command
fac6795d 270 *
f3ed775e
DG
271 * Handle the full argv list of a first level command. Will find the command
272 * in the global commands array and call the function callback associated.
1c9f7941 273 *
f3ed775e
DG
274 * If command not found, return -1
275 * else, return function command error code.
1c9f7941 276 */
f3ed775e 277static int handle_command(int argc, char **argv)
1c9f7941 278{
f3ed775e
DG
279 int i = 0, ret;
280 struct cmd_struct *cmd;
1c9f7941 281
f3ed775e
DG
282 if (*argv == NULL) {
283 ret = CMD_SUCCESS;
47b74d63 284 goto end;
1c9f7941
DG
285 }
286
f3ed775e
DG
287 cmd = &commands[i];
288 while (cmd->func != NULL) {
289 /* Find command */
290 if (strcmp(argv[0], cmd->name) == 0) {
291 ret = cmd->func(argc, (const char**) argv);
f3ed775e 292 goto end;
894be886 293 }
f3ed775e
DG
294 i++;
295 cmd = &commands[i];
894be886
DG
296 }
297
f3ed775e 298 /* Command not found */
4ce78777 299 ret = CMD_UNDEFINED;
894be886
DG
300
301end:
f3ed775e 302 return ret;
8548ff30
DG
303}
304
5b8719f5
DG
305/*
306 * spawn_sessiond
307 *
308 * Spawn a session daemon by forking and execv.
309 */
310static int spawn_sessiond(char *pathname)
311{
312 int ret = 0;
313 pid_t pid;
314
f3ed775e 315 MSG("Spawning a session daemon");
5a532b68 316 recv_child_signal = 0;
5b8719f5
DG
317 pid = fork();
318 if (pid == 0) {
5e16da05
MD
319 /*
320 * Spawn session daemon and tell
5b8719f5
DG
321 * it to signal us when ready.
322 */
32258573 323 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
324 /* execlp only returns if error happened */
325 if (errno == ENOENT) {
326 ERR("No session daemon found. Use --sessiond-path.");
327 } else {
6f04ed72 328 PERROR("execlp");
5b8719f5 329 }
5a532b68 330 kill(getppid(), SIGTERM); /* wake parent */
5e16da05 331 exit(EXIT_FAILURE);
5b8719f5 332 } else if (pid > 0) {
8db8d1dc 333 sessiond_pid = pid;
5a532b68 334 /*
af87c45a
DG
335 * Wait for lttng-sessiond to start. We need to use a flag to check if
336 * the signal has been sent to us, because the child can be scheduled
337 * before the parent, and thus send the signal before this check. In
338 * the signal handler, we set the recv_child_signal flag, so anytime we
339 * check it after the fork is fine. Note that sleep() is interrupted
340 * before the 1 second delay as soon as the signal is received, so it
341 * will not cause visible delay for the user.
5a532b68
MD
342 */
343 while (!recv_child_signal) {
344 sleep(1);
345 }
af87c45a
DG
346 /*
347 * The signal handler will nullify sessiond_pid on SIGCHLD
348 */
8db8d1dc
DG
349 if (!sessiond_pid) {
350 exit(EXIT_FAILURE);
351 }
5b8719f5
DG
352 goto end;
353 } else {
6f04ed72 354 PERROR("fork");
5b8719f5
DG
355 ret = -1;
356 goto end;
357 }
358
359end:
360 return ret;
361}
362
fac6795d 363/*
f3ed775e 364 * check_sessiond
fac6795d
DG
365 *
366 * Check if the session daemon is available using
5b8719f5
DG
367 * the liblttngctl API for the check. If not, try to
368 * spawn a daemon.
fac6795d 369 */
f3ed775e 370static int check_sessiond(void)
fac6795d
DG
371{
372 int ret;
b8ec3da8 373 char *pathname = NULL;
fac6795d 374
947308c4
DG
375 ret = lttng_session_daemon_alive();
376 if (ret == 0) { /* not alive */
5b8719f5 377 /* Try command line option path */
b8ec3da8
SM
378 pathname = opt_sessiond_path;
379
380 /* Try LTTNG_SESSIOND_PATH env variable */
381 if (pathname == NULL) {
bbccc3d2 382 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
5b8719f5
DG
383 }
384
b8ec3da8 385 /* Try with configured path */
5b8719f5 386 if (pathname == NULL) {
b8ec3da8
SM
387 if (CONFIG_SESSIOND_BIN[0] != '\0') {
388 pathname = CONFIG_SESSIOND_BIN;
5b8719f5 389 }
b8ec3da8
SM
390 }
391
392 /* Let's rock and roll while trying the default path */
393 if (pathname == NULL) {
394 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
395 }
396
397 DBG("Session daemon at: %s", pathname);
398
399 /* Check existence and permissions */
400 ret = access(pathname, F_OK | X_OK);
401 if (ret < 0) {
402 ERR("No such file or access denied: %s", pathname);
403 goto end;
5b8719f5
DG
404 }
405
406 ret = spawn_sessiond(pathname);
5b8719f5 407 if (ret < 0) {
3183dbb0 408 ERR("Problem occurred when starting %s", pathname);
5b8719f5 409 }
c2a79a67 410 }
5b8719f5 411end:
fac6795d
DG
412 return ret;
413}
414
bcfa8a05 415/*
679b4943
SM
416 * Check args for specific options that *must* not trigger a session daemon
417 * execution.
418 *
419 * Return 1 if match else 0.
bcfa8a05 420 */
679b4943 421static int check_args_no_sessiond(int argc, char **argv)
bcfa8a05
DG
422{
423 int i;
424
425 for (i = 0; i < argc; i++) {
ba2926ef
MD
426 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
427 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
3183dbb0 428 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
4747a49b 429 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
0c95f5b2
DG
430 strncmp(argv[i], "version", sizeof("version")) == 0 ||
431 strncmp(argv[i], "view", sizeof("view")) == 0) {
bcfa8a05
DG
432 return 1;
433 }
434 }
435
436 return 0;
437}
438
5b8719f5 439/*
3183dbb0 440 * Parse command line arguments.
5b8719f5 441 *
3183dbb0 442 * Return 0 if OK, else -1
5b8719f5 443 */
f3ed775e 444static int parse_args(int argc, char **argv)
5b8719f5 445{
f3ed775e 446 int opt, ret;
883d80f9 447 char *user;
5b8719f5 448
e8fa9fb0
MD
449 if (lttng_is_setuid_setgid()) {
450 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
451 clean_exit(EXIT_FAILURE);
452 }
453
f3ed775e
DG
454 if (argc < 2) {
455 usage(stderr);
456 clean_exit(EXIT_FAILURE);
5b8719f5
DG
457 }
458
c7e35b03 459 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
f3ed775e 460 switch (opt) {
70318c38
SS
461 case 'V':
462 version(stdout);
463 ret = 0;
464 goto end;
f3ed775e 465 case 'h':
3183dbb0 466 usage(stdout);
ae856491 467 ret = 0;
3183dbb0 468 goto end;
f3ed775e 469 case 'v':
849e5b7b
DG
470 /* There is only 3 possible level of verbosity. (-vvv) */
471 if (lttng_opt_verbose < 3) {
472 lttng_opt_verbose += 1;
473 }
f3ed775e
DG
474 break;
475 case 'q':
97e19046 476 lttng_opt_quiet = 1;
f3ed775e 477 break;
c7e35b03
JR
478 case 'm':
479 lttng_opt_mi = mi_output_type(optarg);
480 if (lttng_opt_mi < 0) {
481 ret = lttng_opt_mi;
482 goto error;
483 }
484 break;
f3ed775e
DG
485 case 'g':
486 lttng_set_tracing_group(optarg);
487 break;
8490ae7b 488 case 'n':
f3ed775e
DG
489 opt_no_sessiond = 1;
490 break;
491 case OPT_SESSION_PATH:
492 opt_sessiond_path = strdup(optarg);
d7c5d39f
MD
493 if (!opt_sessiond_path) {
494 ret = -1;
495 goto error;
496 }
f3ed775e 497 break;
8960e9cd
DG
498 case OPT_RELAYD_PATH:
499 opt_relayd_path = strdup(optarg);
d7c5d39f
MD
500 if (!opt_relayd_path) {
501 ret = -1;
502 goto error;
503 }
8960e9cd 504 break;
865abf65
SM
505 case OPT_DUMP_OPTIONS:
506 list_options(stdout);
507 ret = 0;
3183dbb0 508 goto end;
865abf65 509 case OPT_DUMP_COMMANDS:
3c9bd23c 510 list_commands(commands, stdout);
865abf65 511 ret = 0;
3183dbb0 512 goto end;
f3ed775e
DG
513 default:
514 usage(stderr);
ae856491 515 ret = 1;
f3ed775e
DG
516 goto error;
517 }
5b8719f5 518 }
fac6795d 519
f3ed775e 520 /* If both options are specified, quiet wins */
97e19046
DG
521 if (lttng_opt_verbose && lttng_opt_quiet) {
522 lttng_opt_verbose = 0;
5b8719f5
DG
523 }
524
f3ed775e 525 /* Spawn session daemon if needed */
679b4943 526 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
bcfa8a05 527 (check_sessiond() < 0)) {
8005f29a 528 ret = 1;
f3ed775e 529 goto error;
5b8719f5
DG
530 }
531
f3ed775e
DG
532 /* No leftovers, print usage and quit */
533 if ((argc - optind) == 0) {
534 usage(stderr);
8005f29a 535 ret = 1;
f3ed775e
DG
536 goto error;
537 }
7442b2ba 538
883d80f9
DG
539 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
540 user = getenv("USER");
541 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
542 strncmp("compudj", user, 7) == 0))) {
543 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
544 }
545 /* Thanks Mathieu */
546
f3ed775e
DG
547 /*
548 * Handle leftovers which is a first level command with the trailing
549 * options.
550 */
551 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
552 switch (ret) {
553 case CMD_WARNING:
554 WARN("Some command(s) went wrong");
555 break;
556 case CMD_ERROR:
557 ERR("Command error");
558 break;
559 case CMD_UNDEFINED:
560 ERR("Undefined command");
561 break;
562 case CMD_FATAL:
563 ERR("Fatal error");
564 break;
4ce78777
DG
565 case CMD_UNSUPPORTED:
566 ERR("Unsupported command");
567 break;
ae856491
DG
568 case -1:
569 usage(stderr);
570 ret = 1;
571 break;
572 case 0:
573 break;
574 default:
42224349
DG
575 if (ret < 0) {
576 ret = -ret;
577 }
ae856491 578 break;
96243366
DG
579 }
580
3183dbb0 581end:
f3ed775e 582error:
ae856491 583 return ret;
fac6795d
DG
584}
585
f3ed775e 586
fac6795d 587/*
5b8719f5 588 * main
fac6795d
DG
589 */
590int main(int argc, char *argv[])
591{
592 int ret;
593
594 progname = argv[0] ? argv[0] : "lttng";
595
5b8719f5
DG
596 ret = set_signal_handler();
597 if (ret < 0) {
87378cf5 598 clean_exit(ret);
5b8719f5
DG
599 }
600
f3ed775e 601 ret = parse_args(argc, argv);
ae856491
DG
602 if (ret != 0) {
603 clean_exit(ret);
1fff1faa 604 }
87378cf5 605
fac6795d
DG
606 return 0;
607}
This page took 0.082196 seconds and 5 git commands to generate.