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