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