Change SIGCHLD to SIGUSR1 when lttng waiting on sessiond
[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_NOT_IMPLEMENTED:
267 ERR("Options not implemented");
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 pid = fork();
301 if (pid == 0) {
302 /*
303 * Spawn session daemon and tell
304 * it to signal us when ready.
305 */
306 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
307 /* execlp only returns if error happened */
308 if (errno == ENOENT) {
309 ERR("No session daemon found. Use --sessiond-path.");
310 } else {
311 perror("execlp");
312 }
313 kill(getppid(), SIGTERM); /* unpause parent */
314 exit(EXIT_FAILURE);
315 } else if (pid > 0) {
316 sessiond_pid = pid;
317 /* Wait for lttng-sessiond to start */
318 pause();
319 if (!sessiond_pid) {
320 exit(EXIT_FAILURE);
321 }
322 goto end;
323 } else {
324 perror("fork");
325 ret = -1;
326 goto end;
327 }
328
329 end:
330 return ret;
331 }
332
333 /*
334 * check_sessiond
335 *
336 * Check if the session daemon is available using
337 * the liblttngctl API for the check. If not, try to
338 * spawn a daemon.
339 */
340 static int check_sessiond(void)
341 {
342 int ret;
343 char *pathname = NULL, *alloc_pathname = NULL;
344
345 ret = lttng_session_daemon_alive();
346 if (ret == 0) { /* not alive */
347 /* Try command line option path */
348 if (opt_sessiond_path != NULL) {
349 ret = access(opt_sessiond_path, F_OK | X_OK);
350 if (ret < 0) {
351 ERR("No such file: %s", opt_sessiond_path);
352 goto end;
353 }
354 pathname = opt_sessiond_path;
355 } else {
356 /* Try LTTNG_SESSIOND_PATH env variable */
357 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
358 }
359
360 /* Let's rock and roll */
361 if (pathname == NULL) {
362 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
363 if (ret < 0) {
364 perror("asprintf spawn sessiond");
365 goto end;
366 }
367 pathname = alloc_pathname;
368 }
369
370 ret = spawn_sessiond(pathname);
371 free(alloc_pathname);
372 if (ret < 0) {
373 ERR("Problem occurs when starting %s", pathname);
374 goto end;
375 }
376 }
377
378 end:
379 return ret;
380 }
381
382 /*
383 * Check for the "help" option in the argv. If found, return 1 else return 0.
384 */
385 static int check_help_command(int argc, char **argv)
386 {
387 int i;
388
389 for (i = 0; i < argc; i++) {
390 if ((strncmp(argv[i], "-h", 2) == 0) ||
391 strncmp(argv[i], "--h", 3) == 0) {
392 return 1;
393 }
394 }
395
396 return 0;
397 }
398
399 /*
400 * parse_args
401 *
402 * Parse command line arguments.
403 * Return 0 if OK, else -1
404 */
405 static int parse_args(int argc, char **argv)
406 {
407 int opt, ret;
408
409 if (argc < 2) {
410 usage(stderr);
411 clean_exit(EXIT_FAILURE);
412 }
413
414 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
415 switch (opt) {
416 case 'h':
417 usage(stderr);
418 goto error;
419 case 'v':
420 opt_verbose += 1;
421 break;
422 case 'q':
423 opt_quiet = 1;
424 break;
425 case 'g':
426 lttng_set_tracing_group(optarg);
427 break;
428 case 'n':
429 opt_no_sessiond = 1;
430 break;
431 case OPT_SESSION_PATH:
432 opt_sessiond_path = strdup(optarg);
433 break;
434 case OPT_DUMP_OPTIONS:
435 list_options(stdout);
436 ret = 0;
437 goto error;
438 case OPT_DUMP_COMMANDS:
439 list_commands(stdout);
440 ret = 0;
441 goto error;
442 default:
443 usage(stderr);
444 goto error;
445 }
446 }
447
448 /* If both options are specified, quiet wins */
449 if (opt_verbose && opt_quiet) {
450 opt_verbose = 0;
451 }
452
453 /* Spawn session daemon if needed */
454 if (opt_no_sessiond == 0 && check_help_command(argc, argv) == 0 &&
455 (check_sessiond() < 0)) {
456 goto error;
457 }
458
459 /* No leftovers, print usage and quit */
460 if ((argc - optind) == 0) {
461 usage(stderr);
462 goto error;
463 }
464
465 /*
466 * Handle leftovers which is a first level command with the trailing
467 * options.
468 */
469 ret = handle_command(argc - optind, argv + optind);
470 if (ret < 0) {
471 if (ret == -1) {
472 usage(stderr);
473 } else {
474 ERR("%s", lttng_strerror(ret));
475 }
476 goto error;
477 }
478
479 return 0;
480
481 error:
482 return -1;
483 }
484
485
486 /*
487 * main
488 */
489 int main(int argc, char *argv[])
490 {
491 int ret;
492
493 progname = argv[0] ? argv[0] : "lttng";
494
495 /* For Mathieu Desnoyers aka Dr Tracing */
496 if (strncmp(progname, "drtrace", 7) == 0) {
497 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
498 }
499
500 ret = set_signal_handler();
501 if (ret < 0) {
502 clean_exit(ret);
503 }
504
505 ret = parse_args(argc, argv);
506 if (ret < 0) {
507 clean_exit(EXIT_FAILURE);
508 }
509
510 return 0;
511 }
This page took 0.040267 seconds and 5 git commands to generate.