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