Fix sessiond needed checks
[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>
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;
fac6795d 37
f3ed775e
DG
38int opt_quiet;
39int opt_verbose;
40static int opt_no_sessiond;
41static char *opt_sessiond_path;
8db8d1dc 42static pid_t sessiond_pid;
f3ed775e
DG
43
44enum {
f3ed775e 45 OPT_SESSION_PATH,
865abf65
SM
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
f3ed775e
DG
48};
49
50/* Getopt options. No first level command. */
51static struct option long_options[] = {
52 {"help", 0, NULL, 'h'},
53 {"group", 1, NULL, 'g'},
54 {"verbose", 0, NULL, 'v'},
55 {"quiet", 0, NULL, 'q'},
8490ae7b 56 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 57 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
865abf65
SM
58 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
59 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
60 {NULL, 0, NULL, 0}
61};
62
63/* First level command */
64static struct cmd_struct commands[] = {
65 { "list", cmd_list},
66 { "create", cmd_create},
67 { "destroy", cmd_destroy},
f3ed775e
DG
68 { "start", cmd_start},
69 { "stop", cmd_stop},
70 { "enable-event", cmd_enable_events},
e953ef25 71 { "disable-event", cmd_disable_events},
d36b8583 72 { "enable-channel", cmd_enable_channels},
26cc6b4e 73 { "disable-channel", cmd_disable_channels},
d65106b1 74 { "add-context", cmd_add_context},
3087ef18 75 { "set-session", cmd_set_session},
eb9cb8b7 76 { "version", cmd_version},
d0254c7c 77 { "calibrate", cmd_calibrate},
f3ed775e
DG
78 { NULL, NULL} /* Array closure */
79};
80
81static void usage(FILE *ofp)
8c0faa1d 82{
f3ed775e
DG
83 fprintf(ofp, "LTTng Trace Control " VERSION"\n\n");
84 fprintf(ofp, "usage: lttng [options] <command>\n");
85 fprintf(ofp, "\n");
86 fprintf(ofp, "Options:\n");
87 fprintf(ofp, " -h, --help Show this help\n");
88 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
89 fprintf(ofp, " -v, --verbose Verbose mode\n");
90 fprintf(ofp, " -q, --quiet Quiet mode\n");
8490ae7b 91 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
f3ed775e 92 fprintf(ofp, " --sessiond-path Session daemon full path\n");
865abf65
SM
93 fprintf(ofp, " --list-options Simple listing of lttng options\n");
94 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
f3ed775e
DG
95 fprintf(ofp, "\n");
96 fprintf(ofp, "Commands:\n");
d65106b1 97 fprintf(ofp, " add-context Add context to event or/and channel\n");
d0254c7c 98 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
f3ed775e
DG
99 fprintf(ofp, " create Create tracing session\n");
100 fprintf(ofp, " destroy Teardown 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");
110 fprintf(ofp, "\n");
111 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
112 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
8c0faa1d
DG
113}
114
865abf65
SM
115/*
116 * list_options
117 *
118 * List options line by line. This is mostly for bash auto completion and to
119 * avoid difficult parsing.
120 */
121static void list_options(FILE *ofp)
122{
123 int i = 0;
124 struct option *option = NULL;
125
126 option = &long_options[i];
127 while (option->name != NULL) {
128 fprintf(ofp, "--%s\n", option->name);
129
130 if (isprint(option->val)) {
131 fprintf(ofp, "-%c\n", option->val);
132 }
133
134 i++;
135 option = &long_options[i];
136 }
137}
138
139/*
140 * list_commands
141 *
142 * List commands line by line. This is mostly for bash auto completion and to
143 * avoid difficult parsing.
144 */
145static void list_commands(FILE *ofp)
146{
147 int i = 0;
148 struct cmd_struct *cmd = NULL;
149
150 cmd = &commands[i];
151 while (cmd->name != NULL) {
152 fprintf(ofp, "%s\n", cmd->name);
153 i++;
154 cmd = &commands[i];
155 }
156}
157
7442b2ba 158/*
f3ed775e 159 * clean_exit
96243366 160 */
f3ed775e 161static void clean_exit(int code)
96243366 162{
f3ed775e
DG
163 DBG("Clean exit");
164 exit(code);
894be886 165}
96243366 166
894be886 167/*
f3ed775e 168 * sighandler
2ef84c95 169 *
f3ed775e 170 * Signal handler for the daemon
2ef84c95 171 */
f3ed775e 172static void sighandler(int sig)
2ef84c95 173{
8db8d1dc
DG
174 int status;
175
f3ed775e
DG
176 switch (sig) {
177 case SIGTERM:
3cede4fe 178 DBG("SIGTERM caugth");
f3ed775e
DG
179 clean_exit(EXIT_FAILURE);
180 break;
181 case SIGCHLD:
3cede4fe 182 DBG("SIGCHLD caugth");
8db8d1dc
DG
183 waitpid(sessiond_pid, &status, 0);
184 /* Indicate that the session daemon died */
185 sessiond_pid = 0;
186 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
187 break;
188 case SIGUSR1:
189 /* Notify is done */
190 DBG("SIGUSR1 caugth");
f3ed775e
DG
191 break;
192 default:
3cede4fe 193 DBG("Unknown signal %d caugth", sig);
f3ed775e 194 break;
2ef84c95
DG
195 }
196
f3ed775e 197 return;
2ef84c95
DG
198}
199
200/*
f3ed775e 201 * set_signal_handler
894be886 202 *
f3ed775e 203 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 204 */
f3ed775e 205static int set_signal_handler(void)
894be886 206{
f3ed775e
DG
207 int ret = 0;
208 struct sigaction sa;
209 sigset_t sigset;
33a2b854 210
f3ed775e
DG
211 if ((ret = sigemptyset(&sigset)) < 0) {
212 perror("sigemptyset");
33a2b854
DG
213 goto end;
214 }
215
f3ed775e
DG
216 sa.sa_handler = sighandler;
217 sa.sa_mask = sigset;
218 sa.sa_flags = 0;
8db8d1dc 219 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
f3ed775e 220 perror("sigaction");
96243366
DG
221 goto end;
222 }
223
f3ed775e
DG
224 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
225 perror("sigaction");
226 goto end;
894be886
DG
227 }
228
8db8d1dc
DG
229 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
230 perror("sigaction");
231 goto end;
232 }
233
96243366 234end:
57167058
DG
235 return ret;
236}
237
fac6795d 238/*
f3ed775e 239 * handle_command
fac6795d 240 *
f3ed775e
DG
241 * Handle the full argv list of a first level command. Will find the command
242 * in the global commands array and call the function callback associated.
1c9f7941 243 *
f3ed775e
DG
244 * If command not found, return -1
245 * else, return function command error code.
1c9f7941 246 */
f3ed775e 247static int handle_command(int argc, char **argv)
1c9f7941 248{
f3ed775e
DG
249 int i = 0, ret;
250 struct cmd_struct *cmd;
1c9f7941 251
f3ed775e
DG
252 if (*argv == NULL) {
253 ret = CMD_SUCCESS;
47b74d63 254 goto end;
1c9f7941
DG
255 }
256
f3ed775e
DG
257 cmd = &commands[i];
258 while (cmd->func != NULL) {
259 /* Find command */
260 if (strcmp(argv[0], cmd->name) == 0) {
261 ret = cmd->func(argc, (const char**) argv);
262 switch (ret) {
263 case CMD_ERROR:
264 ERR("Command error");
265 break;
f3ed775e
DG
266 case CMD_UNDEFINED:
267 ERR("Undefined command");
268 break;
269 case CMD_FATAL:
270 ERR("Fatal error");
271 break;
272 }
273 goto end;
894be886 274 }
f3ed775e
DG
275 i++;
276 cmd = &commands[i];
894be886
DG
277 }
278
f3ed775e
DG
279 /* Command not found */
280 ret = -1;
894be886
DG
281
282end:
f3ed775e 283 return ret;
8548ff30
DG
284}
285
5b8719f5
DG
286/*
287 * spawn_sessiond
288 *
289 * Spawn a session daemon by forking and execv.
290 */
291static int spawn_sessiond(char *pathname)
292{
293 int ret = 0;
294 pid_t pid;
295
f3ed775e 296 MSG("Spawning a session daemon");
5b8719f5
DG
297 pid = fork();
298 if (pid == 0) {
5e16da05
MD
299 /*
300 * Spawn session daemon and tell
5b8719f5
DG
301 * it to signal us when ready.
302 */
32258573 303 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
304 /* execlp only returns if error happened */
305 if (errno == ENOENT) {
306 ERR("No session daemon found. Use --sessiond-path.");
307 } else {
308 perror("execlp");
5b8719f5 309 }
5e16da05
MD
310 kill(getppid(), SIGTERM); /* unpause parent */
311 exit(EXIT_FAILURE);
5b8719f5 312 } else if (pid > 0) {
8db8d1dc 313 sessiond_pid = pid;
32258573 314 /* Wait for lttng-sessiond to start */
5b8719f5 315 pause();
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;
5e16da05 340 char *pathname = NULL, *alloc_pathname = NULL;
fac6795d 341
947308c4
DG
342 ret = lttng_session_daemon_alive();
343 if (ret == 0) { /* not alive */
5b8719f5
DG
344 /* Try command line option path */
345 if (opt_sessiond_path != NULL) {
346 ret = access(opt_sessiond_path, F_OK | X_OK);
347 if (ret < 0) {
348 ERR("No such file: %s", opt_sessiond_path);
349 goto end;
350 }
351 pathname = opt_sessiond_path;
352 } else {
353 /* Try LTTNG_SESSIOND_PATH env variable */
bbccc3d2 354 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
5b8719f5
DG
355 }
356
357 /* Let's rock and roll */
358 if (pathname == NULL) {
32258573 359 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
5b8719f5 360 if (ret < 0) {
3f5fa9ed 361 perror("asprintf spawn sessiond");
5b8719f5
DG
362 goto end;
363 }
5e16da05 364 pathname = alloc_pathname;
5b8719f5
DG
365 }
366
367 ret = spawn_sessiond(pathname);
5e16da05 368 free(alloc_pathname);
5b8719f5
DG
369 if (ret < 0) {
370 ERR("Problem occurs when starting %s", pathname);
371 goto end;
372 }
fac6795d
DG
373 }
374
5b8719f5 375end:
fac6795d
DG
376 return ret;
377}
378
bcfa8a05 379/*
679b4943
SM
380 * Check args for specific options that *must* not trigger a session daemon
381 * execution.
382 *
383 * Return 1 if match else 0.
bcfa8a05 384 */
679b4943 385static int check_args_no_sessiond(int argc, char **argv)
bcfa8a05
DG
386{
387 int i;
388
389 for (i = 0; i < argc; i++) {
ba2926ef
MD
390 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
391 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
392 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0) {
bcfa8a05
DG
393 return 1;
394 }
395 }
396
397 return 0;
398}
399
5b8719f5 400/*
f3ed775e 401 * parse_args
5b8719f5 402 *
f3ed775e
DG
403 * Parse command line arguments.
404 * Return 0 if OK, else -1
5b8719f5 405 */
f3ed775e 406static int parse_args(int argc, char **argv)
5b8719f5 407{
f3ed775e 408 int opt, ret;
5b8719f5 409
f3ed775e
DG
410 if (argc < 2) {
411 usage(stderr);
412 clean_exit(EXIT_FAILURE);
5b8719f5
DG
413 }
414
8490ae7b 415 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
f3ed775e
DG
416 switch (opt) {
417 case 'h':
418 usage(stderr);
419 goto error;
420 case 'v':
b551a063 421 opt_verbose += 1;
f3ed775e
DG
422 break;
423 case 'q':
424 opt_quiet = 1;
425 break;
426 case 'g':
427 lttng_set_tracing_group(optarg);
428 break;
8490ae7b 429 case 'n':
f3ed775e
DG
430 opt_no_sessiond = 1;
431 break;
432 case OPT_SESSION_PATH:
433 opt_sessiond_path = strdup(optarg);
434 break;
865abf65
SM
435 case OPT_DUMP_OPTIONS:
436 list_options(stdout);
437 ret = 0;
438 goto error;
439 case OPT_DUMP_COMMANDS:
440 list_commands(stdout);
441 ret = 0;
442 goto error;
f3ed775e
DG
443 default:
444 usage(stderr);
445 goto error;
446 }
5b8719f5 447 }
fac6795d 448
f3ed775e
DG
449 /* If both options are specified, quiet wins */
450 if (opt_verbose && opt_quiet) {
451 opt_verbose = 0;
5b8719f5
DG
452 }
453
f3ed775e 454 /* Spawn session daemon if needed */
679b4943 455 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
bcfa8a05 456 (check_sessiond() < 0)) {
f3ed775e 457 goto error;
5b8719f5
DG
458 }
459
f3ed775e
DG
460 /* No leftovers, print usage and quit */
461 if ((argc - optind) == 0) {
462 usage(stderr);
463 goto error;
464 }
7442b2ba 465
f3ed775e
DG
466 /*
467 * Handle leftovers which is a first level command with the trailing
468 * options.
469 */
470 ret = handle_command(argc - optind, argv + optind);
471 if (ret < 0) {
472 if (ret == -1) {
473 usage(stderr);
f3ed775e 474 } else {
9a745bc7 475 ERR("%s", lttng_strerror(ret));
f3ed775e 476 }
1fff1faa 477 goto error;
96243366
DG
478 }
479
1fff1faa 480 return 0;
f3ed775e
DG
481
482error:
483 return -1;
fac6795d
DG
484}
485
f3ed775e 486
fac6795d 487/*
5b8719f5 488 * main
fac6795d
DG
489 */
490int main(int argc, char *argv[])
491{
492 int ret;
493
494 progname = argv[0] ? argv[0] : "lttng";
495
496 /* For Mathieu Desnoyers aka Dr Tracing */
4666e71c
DG
497 if (strncmp(progname, "drtrace", 7) == 0 ||
498 strncmp("compudj", getenv("USER"), 7) == 0) {
499 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
fac6795d
DG
500 }
501
5b8719f5
DG
502 ret = set_signal_handler();
503 if (ret < 0) {
87378cf5 504 clean_exit(ret);
5b8719f5
DG
505 }
506
f3ed775e 507 ret = parse_args(argc, argv);
1fff1faa
DG
508 if (ret < 0) {
509 clean_exit(EXIT_FAILURE);
510 }
87378cf5 511
fac6795d
DG
512 return 0;
513}
This page took 0.056289 seconds and 5 git commands to generate.