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