Fix sessiond needed checks
[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
44 enum {
45 OPT_SESSION_PATH,
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
48 };
49
50 /* Getopt options. No first level command. */
51 static struct option long_options[] = {
52 {"help", 0, NULL, 'h'},
53 {"group", 1, NULL, 'g'},
54 {"verbose", 0, NULL, 'v'},
55 {"quiet", 0, NULL, 'q'},
56 {"no-sessiond", 0, NULL, 'n'},
57 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
58 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
59 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
60 {NULL, 0, NULL, 0}
61 };
62
63 /* First level command */
64 static struct cmd_struct commands[] = {
65 { "list", cmd_list},
66 { "create", cmd_create},
67 { "destroy", cmd_destroy},
68 { "start", cmd_start},
69 { "stop", cmd_stop},
70 { "enable-event", cmd_enable_events},
71 { "disable-event", cmd_disable_events},
72 { "enable-channel", cmd_enable_channels},
73 { "disable-channel", cmd_disable_channels},
74 { "add-context", cmd_add_context},
75 { "set-session", cmd_set_session},
76 { "version", cmd_version},
77 { "calibrate", cmd_calibrate},
78 { NULL, NULL} /* Array closure */
79 };
80
81 static void usage(FILE *ofp)
82 {
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");
91 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
92 fprintf(ofp, " --sessiond-path Session daemon full path\n");
93 fprintf(ofp, " --list-options Simple listing of lttng options\n");
94 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
95 fprintf(ofp, "\n");
96 fprintf(ofp, "Commands:\n");
97 fprintf(ofp, " add-context Add context to event or/and channel\n");
98 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
99 fprintf(ofp, " create Create tracing session\n");
100 fprintf(ofp, " destroy Teardown tracing session\n");
101 fprintf(ofp, " enable-channel Enable tracing channel\n");
102 fprintf(ofp, " enable-event Enable tracing event\n");
103 fprintf(ofp, " disable-channel Disable tracing channel\n");
104 fprintf(ofp, " disable-event Disable tracing event\n");
105 fprintf(ofp, " list List possible tracing options\n");
106 fprintf(ofp, " set-session Set current session name\n");
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");
113 }
114
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 */
121 static 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 */
145 static 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
158 /*
159 * clean_exit
160 */
161 static void clean_exit(int code)
162 {
163 DBG("Clean exit");
164 exit(code);
165 }
166
167 /*
168 * sighandler
169 *
170 * Signal handler for the daemon
171 */
172 static void sighandler(int sig)
173 {
174 int status;
175
176 switch (sig) {
177 case SIGTERM:
178 DBG("SIGTERM caugth");
179 clean_exit(EXIT_FAILURE);
180 break;
181 case SIGCHLD:
182 DBG("SIGCHLD caugth");
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");
191 break;
192 default:
193 DBG("Unknown signal %d caugth", sig);
194 break;
195 }
196
197 return;
198 }
199
200 /*
201 * set_signal_handler
202 *
203 * Setup signal handler for SIGCHLD and SIGTERM.
204 */
205 static int set_signal_handler(void)
206 {
207 int ret = 0;
208 struct sigaction sa;
209 sigset_t sigset;
210
211 if ((ret = sigemptyset(&sigset)) < 0) {
212 perror("sigemptyset");
213 goto end;
214 }
215
216 sa.sa_handler = sighandler;
217 sa.sa_mask = sigset;
218 sa.sa_flags = 0;
219 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
220 perror("sigaction");
221 goto end;
222 }
223
224 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
225 perror("sigaction");
226 goto end;
227 }
228
229 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
230 perror("sigaction");
231 goto end;
232 }
233
234 end:
235 return ret;
236 }
237
238 /*
239 * handle_command
240 *
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.
243 *
244 * If command not found, return -1
245 * else, return function command error code.
246 */
247 static int handle_command(int argc, char **argv)
248 {
249 int i = 0, ret;
250 struct cmd_struct *cmd;
251
252 if (*argv == NULL) {
253 ret = CMD_SUCCESS;
254 goto end;
255 }
256
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;
266 case CMD_UNDEFINED:
267 ERR("Undefined command");
268 break;
269 case CMD_FATAL:
270 ERR("Fatal error");
271 break;
272 }
273 goto end;
274 }
275 i++;
276 cmd = &commands[i];
277 }
278
279 /* Command not found */
280 ret = -1;
281
282 end:
283 return ret;
284 }
285
286 /*
287 * spawn_sessiond
288 *
289 * Spawn a session daemon by forking and execv.
290 */
291 static int spawn_sessiond(char *pathname)
292 {
293 int ret = 0;
294 pid_t pid;
295
296 MSG("Spawning a session daemon");
297 pid = fork();
298 if (pid == 0) {
299 /*
300 * Spawn session daemon and tell
301 * it to signal us when ready.
302 */
303 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
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");
309 }
310 kill(getppid(), SIGTERM); /* unpause parent */
311 exit(EXIT_FAILURE);
312 } else if (pid > 0) {
313 sessiond_pid = pid;
314 /* Wait for lttng-sessiond to start */
315 pause();
316 if (!sessiond_pid) {
317 exit(EXIT_FAILURE);
318 }
319 goto end;
320 } else {
321 perror("fork");
322 ret = -1;
323 goto end;
324 }
325
326 end:
327 return ret;
328 }
329
330 /*
331 * check_sessiond
332 *
333 * Check if the session daemon is available using
334 * the liblttngctl API for the check. If not, try to
335 * spawn a daemon.
336 */
337 static int check_sessiond(void)
338 {
339 int ret;
340 char *pathname = NULL, *alloc_pathname = NULL;
341
342 ret = lttng_session_daemon_alive();
343 if (ret == 0) { /* not alive */
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 */
354 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
355 }
356
357 /* Let's rock and roll */
358 if (pathname == NULL) {
359 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
360 if (ret < 0) {
361 perror("asprintf spawn sessiond");
362 goto end;
363 }
364 pathname = alloc_pathname;
365 }
366
367 ret = spawn_sessiond(pathname);
368 free(alloc_pathname);
369 if (ret < 0) {
370 ERR("Problem occurs when starting %s", pathname);
371 goto end;
372 }
373 }
374
375 end:
376 return ret;
377 }
378
379 /*
380 * Check args for specific options that *must* not trigger a session daemon
381 * execution.
382 *
383 * Return 1 if match else 0.
384 */
385 static int check_args_no_sessiond(int argc, char **argv)
386 {
387 int i;
388
389 for (i = 0; i < argc; i++) {
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) {
393 return 1;
394 }
395 }
396
397 return 0;
398 }
399
400 /*
401 * parse_args
402 *
403 * Parse command line arguments.
404 * Return 0 if OK, else -1
405 */
406 static int parse_args(int argc, char **argv)
407 {
408 int opt, ret;
409
410 if (argc < 2) {
411 usage(stderr);
412 clean_exit(EXIT_FAILURE);
413 }
414
415 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
416 switch (opt) {
417 case 'h':
418 usage(stderr);
419 goto error;
420 case 'v':
421 opt_verbose += 1;
422 break;
423 case 'q':
424 opt_quiet = 1;
425 break;
426 case 'g':
427 lttng_set_tracing_group(optarg);
428 break;
429 case 'n':
430 opt_no_sessiond = 1;
431 break;
432 case OPT_SESSION_PATH:
433 opt_sessiond_path = strdup(optarg);
434 break;
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;
443 default:
444 usage(stderr);
445 goto error;
446 }
447 }
448
449 /* If both options are specified, quiet wins */
450 if (opt_verbose && opt_quiet) {
451 opt_verbose = 0;
452 }
453
454 /* Spawn session daemon if needed */
455 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
456 (check_sessiond() < 0)) {
457 goto error;
458 }
459
460 /* No leftovers, print usage and quit */
461 if ((argc - optind) == 0) {
462 usage(stderr);
463 goto error;
464 }
465
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);
474 } else {
475 ERR("%s", lttng_strerror(ret));
476 }
477 goto error;
478 }
479
480 return 0;
481
482 error:
483 return -1;
484 }
485
486
487 /*
488 * main
489 */
490 int 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 */
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);
500 }
501
502 ret = set_signal_handler();
503 if (ret < 0) {
504 clean_exit(ret);
505 }
506
507 ret = parse_args(argc, argv);
508 if (ret < 0) {
509 clean_exit(EXIT_FAILURE);
510 }
511
512 return 0;
513 }
This page took 0.041858 seconds and 5 git commands to generate.