Merge branch 'master' of git://git.lttng.org/lttng-tools
[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 as published by
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
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.
17 */
18
19 #define _GNU_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
30 #include <lttng/lttng.h>
31 #include <common/error.h>
32
33 #include "command.h"
34
35 /* Variables */
36 static char *progname;
37
38 int opt_quiet;
39 int opt_verbose;
40 static int opt_no_sessiond;
41 static char *opt_sessiond_path;
42 static pid_t sessiond_pid;
43 static volatile int recv_child_signal;
44
45 enum {
46 OPT_SESSION_PATH,
47 OPT_DUMP_OPTIONS,
48 OPT_DUMP_COMMANDS,
49 };
50
51 /* Getopt options. No first level command. */
52 static struct option long_options[] = {
53 {"help", 0, NULL, 'h'},
54 {"group", 1, NULL, 'g'},
55 {"verbose", 0, NULL, 'v'},
56 {"quiet", 0, NULL, 'q'},
57 {"no-sessiond", 0, NULL, 'n'},
58 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
59 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
60 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
61 {NULL, 0, NULL, 0}
62 };
63
64 /* First level command */
65 static struct cmd_struct commands[] = {
66 { "list", cmd_list},
67 { "create", cmd_create},
68 { "destroy", cmd_destroy},
69 { "start", cmd_start},
70 { "stop", cmd_stop},
71 { "enable-event", cmd_enable_events},
72 { "disable-event", cmd_disable_events},
73 { "enable-channel", cmd_enable_channels},
74 { "disable-channel", cmd_disable_channels},
75 { "add-context", cmd_add_context},
76 { "set-session", cmd_set_session},
77 { "version", cmd_version},
78 { "calibrate", cmd_calibrate},
79 { NULL, NULL} /* Array closure */
80 };
81
82 static void usage(FILE *ofp)
83 {
84 fprintf(ofp, "LTTng Trace Control " VERSION"\n\n");
85 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND>\n");
86 fprintf(ofp, "\n");
87 fprintf(ofp, "Options:\n");
88 fprintf(ofp, " -h, --help Show this help\n");
89 fprintf(ofp, " --list-options Simple listing of lttng options\n");
90 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
91 fprintf(ofp, " -v, --verbose Increase verbosity\n");
92 fprintf(ofp, " -q, --quiet Quiet mode\n");
93 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
94 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
95 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
96 fprintf(ofp, "\n");
97 fprintf(ofp, "Commands:\n");
98 fprintf(ofp, " add-context Add context to event and/or channel\n");
99 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
100 fprintf(ofp, " create Create tracing session\n");
101 fprintf(ofp, " destroy Tear down tracing session\n");
102 fprintf(ofp, " enable-channel Enable tracing channel\n");
103 fprintf(ofp, " enable-event Enable tracing event\n");
104 fprintf(ofp, " disable-channel Disable tracing channel\n");
105 fprintf(ofp, " disable-event Disable tracing event\n");
106 fprintf(ofp, " list List possible tracing options\n");
107 fprintf(ofp, " set-session Set current session name\n");
108 fprintf(ofp, " start Start tracing\n");
109 fprintf(ofp, " stop Stop tracing\n");
110 fprintf(ofp, " version Show version information\n");
111 fprintf(ofp, "\n");
112 fprintf(ofp, "Each command also has its own -h, --help option.\n");
113 fprintf(ofp, "\n");
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");
116 }
117
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 */
124 static 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 */
148 static 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
161 /*
162 * clean_exit
163 */
164 static void clean_exit(int code)
165 {
166 DBG("Clean exit");
167 exit(code);
168 }
169
170 /*
171 * sighandler
172 *
173 * Signal handler for the daemon
174 */
175 static void sighandler(int sig)
176 {
177 int status;
178
179 switch (sig) {
180 case SIGTERM:
181 DBG("SIGTERM caught");
182 clean_exit(EXIT_FAILURE);
183 break;
184 case SIGCHLD:
185 DBG("SIGCHLD caught");
186 waitpid(sessiond_pid, &status, 0);
187 recv_child_signal = 1;
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 */
194 recv_child_signal = 1;
195 DBG("SIGUSR1 caught");
196 break;
197 default:
198 DBG("Unknown signal %d caught", sig);
199 break;
200 }
201
202 return;
203 }
204
205 /*
206 * set_signal_handler
207 *
208 * Setup signal handler for SIGCHLD and SIGTERM.
209 */
210 static int set_signal_handler(void)
211 {
212 int ret = 0;
213 struct sigaction sa;
214 sigset_t sigset;
215
216 if ((ret = sigemptyset(&sigset)) < 0) {
217 perror("sigemptyset");
218 goto end;
219 }
220
221 sa.sa_handler = sighandler;
222 sa.sa_mask = sigset;
223 sa.sa_flags = 0;
224 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
225 perror("sigaction");
226 goto end;
227 }
228
229 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
230 perror("sigaction");
231 goto end;
232 }
233
234 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
235 perror("sigaction");
236 goto end;
237 }
238
239 end:
240 return ret;
241 }
242
243 /*
244 * handle_command
245 *
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.
248 *
249 * If command not found, return -1
250 * else, return function command error code.
251 */
252 static int handle_command(int argc, char **argv)
253 {
254 int i = 0, ret;
255 struct cmd_struct *cmd;
256
257 if (*argv == NULL) {
258 ret = CMD_SUCCESS;
259 goto end;
260 }
261
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);
267 goto end;
268 }
269 i++;
270 cmd = &commands[i];
271 }
272
273 /* Command not found */
274 ret = -1;
275
276 end:
277 return ret;
278 }
279
280 /*
281 * spawn_sessiond
282 *
283 * Spawn a session daemon by forking and execv.
284 */
285 static int spawn_sessiond(char *pathname)
286 {
287 int ret = 0;
288 pid_t pid;
289
290 MSG("Spawning a session daemon");
291 recv_child_signal = 0;
292 pid = fork();
293 if (pid == 0) {
294 /*
295 * Spawn session daemon and tell
296 * it to signal us when ready.
297 */
298 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
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");
304 }
305 kill(getppid(), SIGTERM); /* wake parent */
306 exit(EXIT_FAILURE);
307 } else if (pid > 0) {
308 sessiond_pid = pid;
309 /*
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.
317 */
318 while (!recv_child_signal) {
319 sleep(1);
320 }
321 /*
322 * The signal handler will nullify sessiond_pid on SIGCHLD
323 */
324 if (!sessiond_pid) {
325 exit(EXIT_FAILURE);
326 }
327 goto end;
328 } else {
329 perror("fork");
330 ret = -1;
331 goto end;
332 }
333
334 end:
335 return ret;
336 }
337
338 /*
339 * check_sessiond
340 *
341 * Check if the session daemon is available using
342 * the liblttngctl API for the check. If not, try to
343 * spawn a daemon.
344 */
345 static int check_sessiond(void)
346 {
347 int ret;
348 char *pathname = NULL, *alloc_pathname = NULL;
349
350 ret = lttng_session_daemon_alive();
351 if (ret == 0) { /* not alive */
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) {
356 ERR("No such file or access denied: %s", opt_sessiond_path);
357 goto end;
358 }
359 pathname = opt_sessiond_path;
360 } else {
361 /* Try LTTNG_SESSIOND_PATH env variable */
362 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
363 }
364
365 /* Let's rock and roll */
366 if (pathname == NULL) {
367 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
368 if (ret < 0) {
369 perror("asprintf spawn sessiond");
370 goto end;
371 }
372 pathname = alloc_pathname;
373 }
374
375 ret = spawn_sessiond(pathname);
376 free(alloc_pathname);
377 if (ret < 0) {
378 ERR("Problem occurred when starting %s", pathname);
379 goto end;
380 }
381 }
382
383 end:
384 return ret;
385 }
386
387 /*
388 * Check args for specific options that *must* not trigger a session daemon
389 * execution.
390 *
391 * Return 1 if match else 0.
392 */
393 static int check_args_no_sessiond(int argc, char **argv)
394 {
395 int i;
396
397 for (i = 0; i < argc; i++) {
398 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
399 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
400 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
401 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
402 strncmp(argv[i], "version", sizeof("version")) == 0) {
403 return 1;
404 }
405 }
406
407 return 0;
408 }
409
410 /*
411 * Parse command line arguments.
412 *
413 * Return 0 if OK, else -1
414 */
415 static int parse_args(int argc, char **argv)
416 {
417 int opt, ret;
418
419 if (argc < 2) {
420 usage(stderr);
421 clean_exit(EXIT_FAILURE);
422 }
423
424 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
425 switch (opt) {
426 case 'h':
427 usage(stdout);
428 ret = 0;
429 goto end;
430 case 'v':
431 opt_verbose += 1;
432 break;
433 case 'q':
434 opt_quiet = 1;
435 break;
436 case 'g':
437 lttng_set_tracing_group(optarg);
438 break;
439 case 'n':
440 opt_no_sessiond = 1;
441 break;
442 case OPT_SESSION_PATH:
443 opt_sessiond_path = strdup(optarg);
444 break;
445 case OPT_DUMP_OPTIONS:
446 list_options(stdout);
447 ret = 0;
448 goto end;
449 case OPT_DUMP_COMMANDS:
450 list_commands(stdout);
451 ret = 0;
452 goto end;
453 default:
454 usage(stderr);
455 ret = 1;
456 goto error;
457 }
458 }
459
460 /* If both options are specified, quiet wins */
461 if (opt_verbose && opt_quiet) {
462 opt_verbose = 0;
463 }
464
465 /* Spawn session daemon if needed */
466 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
467 (check_sessiond() < 0)) {
468 ret = 1;
469 goto error;
470 }
471
472 /* No leftovers, print usage and quit */
473 if ((argc - optind) == 0) {
474 usage(stderr);
475 ret = 1;
476 goto error;
477 }
478
479 /*
480 * Handle leftovers which is a first level command with the trailing
481 * options.
482 */
483 ret = handle_command(argc - optind, argv + optind);
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:
504 ERR("%s", lttng_strerror(ret));
505 break;
506 }
507
508 end:
509 error:
510 return ret;
511 }
512
513
514 /*
515 * main
516 */
517 int main(int argc, char *argv[])
518 {
519 int ret;
520
521 progname = argv[0] ? argv[0] : "lttng";
522
523 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
524 if (strncmp(progname, "drtrace", 7) == 0 ||
525 strncmp("compudj", getenv("USER"), 7) == 0) {
526 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
527 }
528
529 ret = set_signal_handler();
530 if (ret < 0) {
531 clean_exit(ret);
532 }
533
534 ret = parse_args(argc, argv);
535 if (ret != 0) {
536 clean_exit(ret);
537 }
538
539 return 0;
540 }
This page took 0.041175 seconds and 5 git commands to generate.