Fix lttng sessiond spawn wait race
[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, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
90 fprintf(ofp, " -v, --verbose Verbose mode\n");
91 fprintf(ofp, " -q, --quiet Quiet mode\n");
92 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
93 fprintf(ofp, " --sessiond-path Session daemon full path\n");
94 fprintf(ofp, " --list-options Simple listing of lttng options\n");
95 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
96 fprintf(ofp, "\n");
97 fprintf(ofp, "Commands:\n");
98 fprintf(ofp, " add-context Add context to event or/and channel\n");
99 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
100 fprintf(ofp, " create Create tracing session\n");
101 fprintf(ofp, " destroy Teardown 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, "Please see the lttng(1) man page for full documentation.\n");
113 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
114 }
115
116 /*
117 * list_options
118 *
119 * List options line by line. This is mostly for bash auto completion and to
120 * avoid difficult parsing.
121 */
122 static void list_options(FILE *ofp)
123 {
124 int i = 0;
125 struct option *option = NULL;
126
127 option = &long_options[i];
128 while (option->name != NULL) {
129 fprintf(ofp, "--%s\n", option->name);
130
131 if (isprint(option->val)) {
132 fprintf(ofp, "-%c\n", option->val);
133 }
134
135 i++;
136 option = &long_options[i];
137 }
138 }
139
140 /*
141 * list_commands
142 *
143 * List commands line by line. This is mostly for bash auto completion and to
144 * avoid difficult parsing.
145 */
146 static void list_commands(FILE *ofp)
147 {
148 int i = 0;
149 struct cmd_struct *cmd = NULL;
150
151 cmd = &commands[i];
152 while (cmd->name != NULL) {
153 fprintf(ofp, "%s\n", cmd->name);
154 i++;
155 cmd = &commands[i];
156 }
157 }
158
159 /*
160 * clean_exit
161 */
162 static void clean_exit(int code)
163 {
164 DBG("Clean exit");
165 exit(code);
166 }
167
168 /*
169 * sighandler
170 *
171 * Signal handler for the daemon
172 */
173 static void sighandler(int sig)
174 {
175 int status;
176
177 switch (sig) {
178 case SIGTERM:
179 DBG("SIGTERM caugth");
180 clean_exit(EXIT_FAILURE);
181 break;
182 case SIGCHLD:
183 DBG("SIGCHLD caugth");
184 waitpid(sessiond_pid, &status, 0);
185 recv_child_signal = 1;
186 /* Indicate that the session daemon died */
187 sessiond_pid = 0;
188 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
189 break;
190 case SIGUSR1:
191 /* Notify is done */
192 recv_child_signal = 1;
193 DBG("SIGUSR1 caugth");
194 break;
195 default:
196 DBG("Unknown signal %d caugth", sig);
197 break;
198 }
199
200 return;
201 }
202
203 /*
204 * set_signal_handler
205 *
206 * Setup signal handler for SIGCHLD and SIGTERM.
207 */
208 static int set_signal_handler(void)
209 {
210 int ret = 0;
211 struct sigaction sa;
212 sigset_t sigset;
213
214 if ((ret = sigemptyset(&sigset)) < 0) {
215 perror("sigemptyset");
216 goto end;
217 }
218
219 sa.sa_handler = sighandler;
220 sa.sa_mask = sigset;
221 sa.sa_flags = 0;
222 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
223 perror("sigaction");
224 goto end;
225 }
226
227 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
228 perror("sigaction");
229 goto end;
230 }
231
232 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
233 perror("sigaction");
234 goto end;
235 }
236
237 end:
238 return ret;
239 }
240
241 /*
242 * handle_command
243 *
244 * Handle the full argv list of a first level command. Will find the command
245 * in the global commands array and call the function callback associated.
246 *
247 * If command not found, return -1
248 * else, return function command error code.
249 */
250 static int handle_command(int argc, char **argv)
251 {
252 int i = 0, ret;
253 struct cmd_struct *cmd;
254
255 if (*argv == NULL) {
256 ret = CMD_SUCCESS;
257 goto end;
258 }
259
260 cmd = &commands[i];
261 while (cmd->func != NULL) {
262 /* Find command */
263 if (strcmp(argv[0], cmd->name) == 0) {
264 ret = cmd->func(argc, (const char**) argv);
265 switch (ret) {
266 case CMD_ERROR:
267 ERR("Command error");
268 break;
269 case CMD_UNDEFINED:
270 ERR("Undefined command");
271 break;
272 case CMD_FATAL:
273 ERR("Fatal error");
274 break;
275 }
276 goto end;
277 }
278 i++;
279 cmd = &commands[i];
280 }
281
282 /* Command not found */
283 ret = -1;
284
285 end:
286 return ret;
287 }
288
289 /*
290 * spawn_sessiond
291 *
292 * Spawn a session daemon by forking and execv.
293 */
294 static int spawn_sessiond(char *pathname)
295 {
296 int ret = 0;
297 pid_t pid;
298
299 MSG("Spawning a session daemon");
300 recv_child_signal = 0;
301 pid = fork();
302 if (pid == 0) {
303 /*
304 * Spawn session daemon and tell
305 * it to signal us when ready.
306 */
307 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
308 /* execlp only returns if error happened */
309 if (errno == ENOENT) {
310 ERR("No session daemon found. Use --sessiond-path.");
311 } else {
312 perror("execlp");
313 }
314 kill(getppid(), SIGTERM); /* wake parent */
315 exit(EXIT_FAILURE);
316 } else if (pid > 0) {
317 sessiond_pid = pid;
318 /*
319 * Wait for lttng-sessiond to start. We need to use a
320 * flag to check if the signal has been sent to us,
321 * because the child can be scheduled before the parent,
322 * and thus send the signal before this check. In the
323 * signal handler, we set the recv_child_signal flag, so
324 * anytime we check it after the fork is fine. Note that
325 * sleep() is interrupted before the 1 second delay as
326 * soon as the signal is received, so it will not cause
327 * visible delay for the user.
328 */
329 while (!recv_child_signal) {
330 sleep(1);
331 }
332 if (!sessiond_pid) {
333 exit(EXIT_FAILURE);
334 }
335 goto end;
336 } else {
337 perror("fork");
338 ret = -1;
339 goto end;
340 }
341
342 end:
343 return ret;
344 }
345
346 /*
347 * check_sessiond
348 *
349 * Check if the session daemon is available using
350 * the liblttngctl API for the check. If not, try to
351 * spawn a daemon.
352 */
353 static int check_sessiond(void)
354 {
355 int ret;
356 char *pathname = NULL, *alloc_pathname = NULL;
357
358 ret = lttng_session_daemon_alive();
359 if (ret == 0) { /* not alive */
360 /* Try command line option path */
361 if (opt_sessiond_path != NULL) {
362 ret = access(opt_sessiond_path, F_OK | X_OK);
363 if (ret < 0) {
364 ERR("No such file: %s", opt_sessiond_path);
365 goto end;
366 }
367 pathname = opt_sessiond_path;
368 } else {
369 /* Try LTTNG_SESSIOND_PATH env variable */
370 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
371 }
372
373 /* Let's rock and roll */
374 if (pathname == NULL) {
375 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
376 if (ret < 0) {
377 perror("asprintf spawn sessiond");
378 goto end;
379 }
380 pathname = alloc_pathname;
381 }
382
383 ret = spawn_sessiond(pathname);
384 free(alloc_pathname);
385 if (ret < 0) {
386 ERR("Problem occurs when starting %s", pathname);
387 goto end;
388 }
389 }
390
391 end:
392 return ret;
393 }
394
395 /*
396 * Check args for specific options that *must* not trigger a session daemon
397 * execution.
398 *
399 * Return 1 if match else 0.
400 */
401 static int check_args_no_sessiond(int argc, char **argv)
402 {
403 int i;
404
405 for (i = 0; i < argc; i++) {
406 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
407 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
408 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0) {
409 return 1;
410 }
411 }
412
413 return 0;
414 }
415
416 /*
417 * parse_args
418 *
419 * Parse command line arguments.
420 * Return 0 if OK, else -1
421 */
422 static int parse_args(int argc, char **argv)
423 {
424 int opt, ret;
425
426 if (argc < 2) {
427 usage(stderr);
428 clean_exit(EXIT_FAILURE);
429 }
430
431 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
432 switch (opt) {
433 case 'h':
434 usage(stderr);
435 goto error;
436 case 'v':
437 opt_verbose += 1;
438 break;
439 case 'q':
440 opt_quiet = 1;
441 break;
442 case 'g':
443 lttng_set_tracing_group(optarg);
444 break;
445 case 'n':
446 opt_no_sessiond = 1;
447 break;
448 case OPT_SESSION_PATH:
449 opt_sessiond_path = strdup(optarg);
450 break;
451 case OPT_DUMP_OPTIONS:
452 list_options(stdout);
453 ret = 0;
454 goto error;
455 case OPT_DUMP_COMMANDS:
456 list_commands(stdout);
457 ret = 0;
458 goto error;
459 default:
460 usage(stderr);
461 goto error;
462 }
463 }
464
465 /* If both options are specified, quiet wins */
466 if (opt_verbose && opt_quiet) {
467 opt_verbose = 0;
468 }
469
470 /* Spawn session daemon if needed */
471 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
472 (check_sessiond() < 0)) {
473 goto error;
474 }
475
476 /* No leftovers, print usage and quit */
477 if ((argc - optind) == 0) {
478 usage(stderr);
479 goto error;
480 }
481
482 /*
483 * Handle leftovers which is a first level command with the trailing
484 * options.
485 */
486 ret = handle_command(argc - optind, argv + optind);
487 if (ret < 0) {
488 if (ret == -1) {
489 usage(stderr);
490 } else {
491 ERR("%s", lttng_strerror(ret));
492 }
493 goto error;
494 }
495
496 return 0;
497
498 error:
499 return -1;
500 }
501
502
503 /*
504 * main
505 */
506 int main(int argc, char *argv[])
507 {
508 int ret;
509
510 progname = argv[0] ? argv[0] : "lttng";
511
512 /* For Mathieu Desnoyers aka Dr Tracing */
513 if (strncmp(progname, "drtrace", 7) == 0 ||
514 strncmp("compudj", getenv("USER"), 7) == 0) {
515 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
516 }
517
518 ret = set_signal_handler();
519 if (ret < 0) {
520 clean_exit(ret);
521 }
522
523 ret = parse_args(argc, argv);
524 if (ret < 0) {
525 clean_exit(EXIT_FAILURE);
526 }
527
528 return 0;
529 }
This page took 0.039608 seconds and 5 git commands to generate.