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