Change SIGCHLD to SIGUSR1 when lttng waiting on sessiond
[lttng-tools.git] / src / bin / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
fac6795d
DG
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.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
fac6795d 20#include <getopt.h>
f3ed775e 21#include <signal.h>
fac6795d
DG
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
8db8d1dc
DG
25#include <sys/types.h>
26#include <sys/wait.h>
fac6795d 27#include <unistd.h>
3bd1e081 28#include <config.h>
fac6795d 29
5b97ec60 30#include <lttng/lttng.h>
db758600 31#include <common/error.h>
fac6795d 32
c399183f 33#include "command.h"
fac6795d
DG
34
35/* Variables */
36static char *progname;
fac6795d 37
f3ed775e
DG
38int opt_quiet;
39int opt_verbose;
40static int opt_no_sessiond;
41static char *opt_sessiond_path;
8db8d1dc 42static pid_t sessiond_pid;
f3ed775e
DG
43
44enum {
f3ed775e 45 OPT_SESSION_PATH,
865abf65
SM
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
f3ed775e
DG
48};
49
50/* Getopt options. No first level command. */
51static struct option long_options[] = {
52 {"help", 0, NULL, 'h'},
53 {"group", 1, NULL, 'g'},
54 {"verbose", 0, NULL, 'v'},
55 {"quiet", 0, NULL, 'q'},
8490ae7b 56 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 57 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
865abf65
SM
58 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
59 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
60 {NULL, 0, NULL, 0}
61};
62
63/* First level command */
64static struct cmd_struct commands[] = {
65 { "list", cmd_list},
66 { "create", cmd_create},
67 { "destroy", cmd_destroy},
f3ed775e
DG
68 { "start", cmd_start},
69 { "stop", cmd_stop},
70 { "enable-event", cmd_enable_events},
e953ef25 71 { "disable-event", cmd_disable_events},
d36b8583 72 { "enable-channel", cmd_enable_channels},
26cc6b4e 73 { "disable-channel", cmd_disable_channels},
d65106b1 74 { "add-context", cmd_add_context},
3087ef18 75 { "set-session", cmd_set_session},
eb9cb8b7 76 { "version", cmd_version},
d0254c7c 77 { "calibrate", cmd_calibrate},
f3ed775e
DG
78 { NULL, NULL} /* Array closure */
79};
80
81static void usage(FILE *ofp)
8c0faa1d 82{
f3ed775e
DG
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");
8490ae7b 91 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
f3ed775e 92 fprintf(ofp, " --sessiond-path Session daemon full path\n");
865abf65
SM
93 fprintf(ofp, " --list-options Simple listing of lttng options\n");
94 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
f3ed775e
DG
95 fprintf(ofp, "\n");
96 fprintf(ofp, "Commands:\n");
d65106b1 97 fprintf(ofp, " add-context Add context to event or/and channel\n");
d0254c7c 98 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
f3ed775e
DG
99 fprintf(ofp, " create Create tracing session\n");
100 fprintf(ofp, " destroy Teardown tracing session\n");
d36b8583 101 fprintf(ofp, " enable-channel Enable tracing channel\n");
26cc6b4e
DG
102 fprintf(ofp, " enable-event Enable tracing event\n");
103 fprintf(ofp, " disable-channel Disable tracing channel\n");
f3ed775e
DG
104 fprintf(ofp, " disable-event Disable tracing event\n");
105 fprintf(ofp, " list List possible tracing options\n");
3087ef18 106 fprintf(ofp, " set-session Set current session name\n");
f3ed775e
DG
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");
8c0faa1d
DG
113}
114
865abf65
SM
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 */
121static 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 */
145static 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
7442b2ba 158/*
f3ed775e 159 * clean_exit
96243366 160 */
f3ed775e 161static void clean_exit(int code)
96243366 162{
f3ed775e
DG
163 DBG("Clean exit");
164 exit(code);
894be886 165}
96243366 166
894be886 167/*
f3ed775e 168 * sighandler
2ef84c95 169 *
f3ed775e 170 * Signal handler for the daemon
2ef84c95 171 */
f3ed775e 172static void sighandler(int sig)
2ef84c95 173{
8db8d1dc
DG
174 int status;
175
f3ed775e
DG
176 switch (sig) {
177 case SIGTERM:
3cede4fe 178 DBG("SIGTERM caugth");
f3ed775e
DG
179 clean_exit(EXIT_FAILURE);
180 break;
181 case SIGCHLD:
3cede4fe 182 DBG("SIGCHLD caugth");
8db8d1dc
DG
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");
f3ed775e
DG
191 break;
192 default:
3cede4fe 193 DBG("Unknown signal %d caugth", sig);
f3ed775e 194 break;
2ef84c95
DG
195 }
196
f3ed775e 197 return;
2ef84c95
DG
198}
199
200/*
f3ed775e 201 * set_signal_handler
894be886 202 *
f3ed775e 203 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 204 */
f3ed775e 205static int set_signal_handler(void)
894be886 206{
f3ed775e
DG
207 int ret = 0;
208 struct sigaction sa;
209 sigset_t sigset;
33a2b854 210
f3ed775e
DG
211 if ((ret = sigemptyset(&sigset)) < 0) {
212 perror("sigemptyset");
33a2b854
DG
213 goto end;
214 }
215
f3ed775e
DG
216 sa.sa_handler = sighandler;
217 sa.sa_mask = sigset;
218 sa.sa_flags = 0;
8db8d1dc 219 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
f3ed775e 220 perror("sigaction");
96243366
DG
221 goto end;
222 }
223
f3ed775e
DG
224 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
225 perror("sigaction");
226 goto end;
894be886
DG
227 }
228
8db8d1dc
DG
229 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
230 perror("sigaction");
231 goto end;
232 }
233
96243366 234end:
57167058
DG
235 return ret;
236}
237
fac6795d 238/*
f3ed775e 239 * handle_command
fac6795d 240 *
f3ed775e
DG
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.
1c9f7941 243 *
f3ed775e
DG
244 * If command not found, return -1
245 * else, return function command error code.
1c9f7941 246 */
f3ed775e 247static int handle_command(int argc, char **argv)
1c9f7941 248{
f3ed775e
DG
249 int i = 0, ret;
250 struct cmd_struct *cmd;
1c9f7941 251
f3ed775e
DG
252 if (*argv == NULL) {
253 ret = CMD_SUCCESS;
47b74d63 254 goto end;
1c9f7941
DG
255 }
256
f3ed775e
DG
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;
894be886 277 }
f3ed775e
DG
278 i++;
279 cmd = &commands[i];
894be886
DG
280 }
281
f3ed775e
DG
282 /* Command not found */
283 ret = -1;
894be886
DG
284
285end:
f3ed775e 286 return ret;
8548ff30
DG
287}
288
5b8719f5
DG
289/*
290 * spawn_sessiond
291 *
292 * Spawn a session daemon by forking and execv.
293 */
294static int spawn_sessiond(char *pathname)
295{
296 int ret = 0;
297 pid_t pid;
298
f3ed775e 299 MSG("Spawning a session daemon");
5b8719f5
DG
300 pid = fork();
301 if (pid == 0) {
5e16da05
MD
302 /*
303 * Spawn session daemon and tell
5b8719f5
DG
304 * it to signal us when ready.
305 */
32258573 306 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
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");
5b8719f5 312 }
5e16da05
MD
313 kill(getppid(), SIGTERM); /* unpause parent */
314 exit(EXIT_FAILURE);
5b8719f5 315 } else if (pid > 0) {
8db8d1dc 316 sessiond_pid = pid;
32258573 317 /* Wait for lttng-sessiond to start */
5b8719f5 318 pause();
8db8d1dc
DG
319 if (!sessiond_pid) {
320 exit(EXIT_FAILURE);
321 }
5b8719f5
DG
322 goto end;
323 } else {
324 perror("fork");
325 ret = -1;
326 goto end;
327 }
328
329end:
330 return ret;
331}
332
fac6795d 333/*
f3ed775e 334 * check_sessiond
fac6795d
DG
335 *
336 * Check if the session daemon is available using
5b8719f5
DG
337 * the liblttngctl API for the check. If not, try to
338 * spawn a daemon.
fac6795d 339 */
f3ed775e 340static int check_sessiond(void)
fac6795d
DG
341{
342 int ret;
5e16da05 343 char *pathname = NULL, *alloc_pathname = NULL;
fac6795d 344
947308c4
DG
345 ret = lttng_session_daemon_alive();
346 if (ret == 0) { /* not alive */
5b8719f5
DG
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 */
e8f07c63 357 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
5b8719f5
DG
358 }
359
360 /* Let's rock and roll */
361 if (pathname == NULL) {
32258573 362 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
5b8719f5 363 if (ret < 0) {
3f5fa9ed 364 perror("asprintf spawn sessiond");
5b8719f5
DG
365 goto end;
366 }
5e16da05 367 pathname = alloc_pathname;
5b8719f5
DG
368 }
369
370 ret = spawn_sessiond(pathname);
5e16da05 371 free(alloc_pathname);
5b8719f5
DG
372 if (ret < 0) {
373 ERR("Problem occurs when starting %s", pathname);
374 goto end;
375 }
fac6795d
DG
376 }
377
5b8719f5 378end:
fac6795d
DG
379 return ret;
380}
381
bcfa8a05
DG
382/*
383 * Check for the "help" option in the argv. If found, return 1 else return 0.
384 */
385static 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) ||
76150f62 391 strncmp(argv[i], "--h", 3) == 0) {
bcfa8a05
DG
392 return 1;
393 }
394 }
395
396 return 0;
397}
398
5b8719f5 399/*
f3ed775e 400 * parse_args
5b8719f5 401 *
f3ed775e
DG
402 * Parse command line arguments.
403 * Return 0 if OK, else -1
5b8719f5 404 */
f3ed775e 405static int parse_args(int argc, char **argv)
5b8719f5 406{
f3ed775e 407 int opt, ret;
5b8719f5 408
f3ed775e
DG
409 if (argc < 2) {
410 usage(stderr);
411 clean_exit(EXIT_FAILURE);
5b8719f5
DG
412 }
413
8490ae7b 414 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
f3ed775e
DG
415 switch (opt) {
416 case 'h':
417 usage(stderr);
418 goto error;
419 case 'v':
b551a063 420 opt_verbose += 1;
f3ed775e
DG
421 break;
422 case 'q':
423 opt_quiet = 1;
424 break;
425 case 'g':
426 lttng_set_tracing_group(optarg);
427 break;
8490ae7b 428 case 'n':
f3ed775e
DG
429 opt_no_sessiond = 1;
430 break;
431 case OPT_SESSION_PATH:
432 opt_sessiond_path = strdup(optarg);
433 break;
865abf65
SM
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;
f3ed775e
DG
442 default:
443 usage(stderr);
444 goto error;
445 }
5b8719f5 446 }
fac6795d 447
f3ed775e
DG
448 /* If both options are specified, quiet wins */
449 if (opt_verbose && opt_quiet) {
450 opt_verbose = 0;
5b8719f5
DG
451 }
452
f3ed775e 453 /* Spawn session daemon if needed */
bcfa8a05
DG
454 if (opt_no_sessiond == 0 && check_help_command(argc, argv) == 0 &&
455 (check_sessiond() < 0)) {
f3ed775e 456 goto error;
5b8719f5
DG
457 }
458
f3ed775e
DG
459 /* No leftovers, print usage and quit */
460 if ((argc - optind) == 0) {
461 usage(stderr);
462 goto error;
463 }
7442b2ba 464
f3ed775e
DG
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);
f3ed775e 473 } else {
9a745bc7 474 ERR("%s", lttng_strerror(ret));
f3ed775e 475 }
1fff1faa 476 goto error;
96243366
DG
477 }
478
1fff1faa 479 return 0;
f3ed775e
DG
480
481error:
482 return -1;
fac6795d
DG
483}
484
f3ed775e 485
fac6795d 486/*
5b8719f5 487 * main
fac6795d
DG
488 */
489int 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
5b8719f5
DG
500 ret = set_signal_handler();
501 if (ret < 0) {
87378cf5 502 clean_exit(ret);
5b8719f5
DG
503 }
504
f3ed775e 505 ret = parse_args(argc, argv);
1fff1faa
DG
506 if (ret < 0) {
507 clean_exit(EXIT_FAILURE);
508 }
87378cf5 509
fac6795d
DG
510 return 0;
511}
This page took 0.050823 seconds and 4 git commands to generate.