Commit | Line | Data |
---|---|---|
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 */ | |
36 | static char *progname; | |
fac6795d | 37 | |
f3ed775e DG |
38 | int opt_quiet; |
39 | int opt_verbose; | |
40 | static int opt_no_sessiond; | |
41 | static char *opt_sessiond_path; | |
8db8d1dc | 42 | static pid_t sessiond_pid; |
f3ed775e DG |
43 | |
44 | enum { | |
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. */ | |
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'}, | |
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 */ | |
64 | static 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 | ||
81 | static 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 | */ | |
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 | ||
7442b2ba | 158 | /* |
f3ed775e | 159 | * clean_exit |
96243366 | 160 | */ |
f3ed775e | 161 | static 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 | 172 | static 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 | 205 | static 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 | 234 | end: |
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 | 247 | static 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; | |
f3ed775e DG |
266 | case CMD_UNDEFINED: |
267 | ERR("Undefined command"); | |
268 | break; | |
269 | case CMD_FATAL: | |
270 | ERR("Fatal error"); | |
271 | break; | |
272 | } | |
273 | goto end; | |
894be886 | 274 | } |
f3ed775e DG |
275 | i++; |
276 | cmd = &commands[i]; | |
894be886 DG |
277 | } |
278 | ||
f3ed775e DG |
279 | /* Command not found */ |
280 | ret = -1; | |
894be886 DG |
281 | |
282 | end: | |
f3ed775e | 283 | return ret; |
8548ff30 DG |
284 | } |
285 | ||
5b8719f5 DG |
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 | ||
f3ed775e | 296 | MSG("Spawning a session daemon"); |
5b8719f5 DG |
297 | pid = fork(); |
298 | if (pid == 0) { | |
5e16da05 MD |
299 | /* |
300 | * Spawn session daemon and tell | |
5b8719f5 DG |
301 | * it to signal us when ready. |
302 | */ | |
32258573 | 303 | execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL); |
5e16da05 MD |
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"); | |
5b8719f5 | 309 | } |
5e16da05 MD |
310 | kill(getppid(), SIGTERM); /* unpause parent */ |
311 | exit(EXIT_FAILURE); | |
5b8719f5 | 312 | } else if (pid > 0) { |
8db8d1dc | 313 | sessiond_pid = pid; |
32258573 | 314 | /* Wait for lttng-sessiond to start */ |
5b8719f5 | 315 | pause(); |
8db8d1dc DG |
316 | if (!sessiond_pid) { |
317 | exit(EXIT_FAILURE); | |
318 | } | |
5b8719f5 DG |
319 | goto end; |
320 | } else { | |
321 | perror("fork"); | |
322 | ret = -1; | |
323 | goto end; | |
324 | } | |
325 | ||
326 | end: | |
327 | return ret; | |
328 | } | |
329 | ||
fac6795d | 330 | /* |
f3ed775e | 331 | * check_sessiond |
fac6795d DG |
332 | * |
333 | * Check if the session daemon is available using | |
5b8719f5 DG |
334 | * the liblttngctl API for the check. If not, try to |
335 | * spawn a daemon. | |
fac6795d | 336 | */ |
f3ed775e | 337 | static int check_sessiond(void) |
fac6795d DG |
338 | { |
339 | int ret; | |
5e16da05 | 340 | char *pathname = NULL, *alloc_pathname = NULL; |
fac6795d | 341 | |
947308c4 DG |
342 | ret = lttng_session_daemon_alive(); |
343 | if (ret == 0) { /* not alive */ | |
5b8719f5 DG |
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 */ | |
e8f07c63 | 354 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
5b8719f5 DG |
355 | } |
356 | ||
357 | /* Let's rock and roll */ | |
358 | if (pathname == NULL) { | |
32258573 | 359 | ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond"); |
5b8719f5 | 360 | if (ret < 0) { |
3f5fa9ed | 361 | perror("asprintf spawn sessiond"); |
5b8719f5 DG |
362 | goto end; |
363 | } | |
5e16da05 | 364 | pathname = alloc_pathname; |
5b8719f5 DG |
365 | } |
366 | ||
367 | ret = spawn_sessiond(pathname); | |
5e16da05 | 368 | free(alloc_pathname); |
5b8719f5 DG |
369 | if (ret < 0) { |
370 | ERR("Problem occurs when starting %s", pathname); | |
371 | goto end; | |
372 | } | |
fac6795d DG |
373 | } |
374 | ||
5b8719f5 | 375 | end: |
fac6795d DG |
376 | return ret; |
377 | } | |
378 | ||
bcfa8a05 DG |
379 | /* |
380 | * Check for the "help" option in the argv. If found, return 1 else return 0. | |
381 | */ | |
382 | static int check_help_command(int argc, char **argv) | |
383 | { | |
384 | int i; | |
385 | ||
386 | for (i = 0; i < argc; i++) { | |
387 | if ((strncmp(argv[i], "-h", 2) == 0) || | |
76150f62 | 388 | strncmp(argv[i], "--h", 3) == 0) { |
bcfa8a05 DG |
389 | return 1; |
390 | } | |
391 | } | |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
5b8719f5 | 396 | /* |
f3ed775e | 397 | * parse_args |
5b8719f5 | 398 | * |
f3ed775e DG |
399 | * Parse command line arguments. |
400 | * Return 0 if OK, else -1 | |
5b8719f5 | 401 | */ |
f3ed775e | 402 | static int parse_args(int argc, char **argv) |
5b8719f5 | 403 | { |
f3ed775e | 404 | int opt, ret; |
5b8719f5 | 405 | |
f3ed775e DG |
406 | if (argc < 2) { |
407 | usage(stderr); | |
408 | clean_exit(EXIT_FAILURE); | |
5b8719f5 DG |
409 | } |
410 | ||
8490ae7b | 411 | while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) { |
f3ed775e DG |
412 | switch (opt) { |
413 | case 'h': | |
414 | usage(stderr); | |
415 | goto error; | |
416 | case 'v': | |
b551a063 | 417 | opt_verbose += 1; |
f3ed775e DG |
418 | break; |
419 | case 'q': | |
420 | opt_quiet = 1; | |
421 | break; | |
422 | case 'g': | |
423 | lttng_set_tracing_group(optarg); | |
424 | break; | |
8490ae7b | 425 | case 'n': |
f3ed775e DG |
426 | opt_no_sessiond = 1; |
427 | break; | |
428 | case OPT_SESSION_PATH: | |
429 | opt_sessiond_path = strdup(optarg); | |
430 | break; | |
865abf65 SM |
431 | case OPT_DUMP_OPTIONS: |
432 | list_options(stdout); | |
433 | ret = 0; | |
434 | goto error; | |
435 | case OPT_DUMP_COMMANDS: | |
436 | list_commands(stdout); | |
437 | ret = 0; | |
438 | goto error; | |
f3ed775e DG |
439 | default: |
440 | usage(stderr); | |
441 | goto error; | |
442 | } | |
5b8719f5 | 443 | } |
fac6795d | 444 | |
f3ed775e DG |
445 | /* If both options are specified, quiet wins */ |
446 | if (opt_verbose && opt_quiet) { | |
447 | opt_verbose = 0; | |
5b8719f5 DG |
448 | } |
449 | ||
f3ed775e | 450 | /* Spawn session daemon if needed */ |
bcfa8a05 DG |
451 | if (opt_no_sessiond == 0 && check_help_command(argc, argv) == 0 && |
452 | (check_sessiond() < 0)) { | |
f3ed775e | 453 | goto error; |
5b8719f5 DG |
454 | } |
455 | ||
f3ed775e DG |
456 | /* No leftovers, print usage and quit */ |
457 | if ((argc - optind) == 0) { | |
458 | usage(stderr); | |
459 | goto error; | |
460 | } | |
7442b2ba | 461 | |
f3ed775e DG |
462 | /* |
463 | * Handle leftovers which is a first level command with the trailing | |
464 | * options. | |
465 | */ | |
466 | ret = handle_command(argc - optind, argv + optind); | |
467 | if (ret < 0) { | |
468 | if (ret == -1) { | |
469 | usage(stderr); | |
f3ed775e | 470 | } else { |
9a745bc7 | 471 | ERR("%s", lttng_strerror(ret)); |
f3ed775e | 472 | } |
1fff1faa | 473 | goto error; |
96243366 DG |
474 | } |
475 | ||
1fff1faa | 476 | return 0; |
f3ed775e DG |
477 | |
478 | error: | |
479 | return -1; | |
fac6795d DG |
480 | } |
481 | ||
f3ed775e | 482 | |
fac6795d | 483 | /* |
5b8719f5 | 484 | * main |
fac6795d DG |
485 | */ |
486 | int main(int argc, char *argv[]) | |
487 | { | |
488 | int ret; | |
489 | ||
490 | progname = argv[0] ? argv[0] : "lttng"; | |
491 | ||
492 | /* For Mathieu Desnoyers aka Dr Tracing */ | |
493 | if (strncmp(progname, "drtrace", 7) == 0) { | |
494 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); | |
495 | } | |
496 | ||
5b8719f5 DG |
497 | ret = set_signal_handler(); |
498 | if (ret < 0) { | |
87378cf5 | 499 | clean_exit(ret); |
5b8719f5 DG |
500 | } |
501 | ||
f3ed775e | 502 | ret = parse_args(argc, argv); |
1fff1faa DG |
503 | if (ret < 0) { |
504 | clean_exit(EXIT_FAILURE); | |
505 | } | |
87378cf5 | 506 | |
fac6795d DG |
507 | return 0; |
508 | } |