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