Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
fac6795d | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
fac6795d | 5 | * |
fac6795d DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
fac6795d | 9 | #include <getopt.h> |
f3ed775e | 10 | #include <signal.h> |
fac6795d DG |
11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
8db8d1dc DG |
14 | #include <sys/types.h> |
15 | #include <sys/wait.h> | |
fac6795d | 16 | #include <unistd.h> |
730389d9 | 17 | #include <ctype.h> |
fac6795d | 18 | |
5b97ec60 | 19 | #include <lttng/lttng.h> |
db758600 | 20 | #include <common/error.h> |
e8fa9fb0 | 21 | #include <common/compat/getenv.h> |
f67353ba | 22 | #include <common/utils.h> |
fac6795d | 23 | |
c399183f | 24 | #include "command.h" |
979d476d | 25 | #include "version.h" |
fac6795d | 26 | |
4fc83d94 PP |
27 | static const char *help_msg = |
28 | #ifdef LTTNG_EMBED_HELP | |
29 | #include <lttng.1.h> | |
30 | #else | |
31 | NULL | |
32 | #endif | |
33 | ; | |
34 | ||
fac6795d | 35 | /* Variables */ |
b53d4e59 | 36 | static const char *progname; |
92360082 JG |
37 | int opt_no_sessiond; |
38 | char *opt_sessiond_path; | |
f3ed775e | 39 | |
8960e9cd DG |
40 | char *opt_relayd_path; |
41 | ||
f3ed775e | 42 | enum { |
8960e9cd | 43 | OPT_RELAYD_PATH, |
f3ed775e | 44 | OPT_SESSION_PATH, |
865abf65 SM |
45 | OPT_DUMP_OPTIONS, |
46 | OPT_DUMP_COMMANDS, | |
f3ed775e DG |
47 | }; |
48 | ||
49 | /* Getopt options. No first level command. */ | |
50 | static struct option long_options[] = { | |
70318c38 | 51 | {"version", 0, NULL, 'V'}, |
f3ed775e DG |
52 | {"help", 0, NULL, 'h'}, |
53 | {"group", 1, NULL, 'g'}, | |
54 | {"verbose", 0, NULL, 'v'}, | |
55 | {"quiet", 0, NULL, 'q'}, | |
c7e35b03 | 56 | {"mi", 1, NULL, 'm'}, |
8490ae7b | 57 | {"no-sessiond", 0, NULL, 'n'}, |
f3ed775e | 58 | {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, |
8960e9cd | 59 | {"relayd-path", 1, NULL, OPT_RELAYD_PATH}, |
865abf65 SM |
60 | {"list-options", 0, NULL, OPT_DUMP_OPTIONS}, |
61 | {"list-commands", 0, NULL, OPT_DUMP_COMMANDS}, | |
f3ed775e DG |
62 | {NULL, 0, NULL, 0} |
63 | }; | |
64 | ||
65 | /* First level command */ | |
66 | static struct cmd_struct commands[] = { | |
3ee1477a | 67 | { "add-context", cmd_add_context}, |
4624dad0 | 68 | { "add-trigger", cmd_add_trigger}, |
f3ed775e | 69 | { "create", cmd_create}, |
511ed4e2 | 70 | { "clear", cmd_clear}, |
f3ed775e | 71 | { "destroy", cmd_destroy}, |
3ee1477a | 72 | { "disable-channel", cmd_disable_channels}, |
e953ef25 | 73 | { "disable-event", cmd_disable_events}, |
d36b8583 | 74 | { "enable-channel", cmd_enable_channels}, |
3ee1477a PP |
75 | { "enable-event", cmd_enable_events}, |
76 | { "help", NULL}, | |
77 | { "list", cmd_list}, | |
78 | { "load", cmd_load}, | |
79 | { "metadata", cmd_metadata}, | |
7712f784 | 80 | { "regenerate", cmd_regenerate}, |
d68c9a04 | 81 | { "rotate", cmd_rotate}, |
259c2674 JD |
82 | { "enable-rotation", cmd_enable_rotation}, |
83 | { "disable-rotation", cmd_disable_rotation}, | |
3ee1477a | 84 | { "save", cmd_save}, |
3087ef18 | 85 | { "set-session", cmd_set_session}, |
57f272ed | 86 | { "snapshot", cmd_snapshot}, |
3ee1477a PP |
87 | { "start", cmd_start}, |
88 | { "status", cmd_status}, | |
89 | { "stop", cmd_stop}, | |
ccf10263 MD |
90 | { "track", cmd_track}, |
91 | { "untrack", cmd_untrack}, | |
3ee1477a PP |
92 | { "version", cmd_version}, |
93 | { "view", cmd_view}, | |
f3ed775e DG |
94 | { NULL, NULL} /* Array closure */ |
95 | }; | |
96 | ||
70318c38 SS |
97 | static void version(FILE *ofp) |
98 | { | |
a3bc3918 | 99 | fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s%s\n", |
4c6ac053 | 100 | progname, |
a3bc3918 JR |
101 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION, |
102 | EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME); | |
70318c38 SS |
103 | } |
104 | ||
c7e35b03 JR |
105 | /* |
106 | * Find the MI output type enum from a string. This function is for the support | |
107 | * of machine interface output. | |
108 | */ | |
109 | static int mi_output_type(const char *output_type) | |
110 | { | |
111 | int ret = 0; | |
112 | ||
113 | if (!strncasecmp("xml", output_type, 3)) { | |
114 | ret = LTTNG_MI_XML; | |
115 | } else { | |
116 | /* Invalid output format */ | |
117 | ERR("MI output format not supported"); | |
118 | ret = -LTTNG_ERR_MI_OUTPUT_TYPE; | |
119 | } | |
120 | ||
121 | return ret; | |
122 | } | |
123 | ||
865abf65 SM |
124 | /* |
125 | * list_options | |
126 | * | |
127 | * List options line by line. This is mostly for bash auto completion and to | |
128 | * avoid difficult parsing. | |
129 | */ | |
130 | static void list_options(FILE *ofp) | |
131 | { | |
132 | int i = 0; | |
133 | struct option *option = NULL; | |
134 | ||
135 | option = &long_options[i]; | |
136 | while (option->name != NULL) { | |
137 | fprintf(ofp, "--%s\n", option->name); | |
138 | ||
139 | if (isprint(option->val)) { | |
140 | fprintf(ofp, "-%c\n", option->val); | |
141 | } | |
142 | ||
143 | i++; | |
144 | option = &long_options[i]; | |
145 | } | |
146 | } | |
147 | ||
7442b2ba | 148 | /* |
f3ed775e | 149 | * clean_exit |
96243366 | 150 | */ |
f3ed775e | 151 | static void clean_exit(int code) |
96243366 | 152 | { |
f3ed775e DG |
153 | DBG("Clean exit"); |
154 | exit(code); | |
894be886 | 155 | } |
96243366 | 156 | |
894be886 | 157 | /* |
f3ed775e | 158 | * sighandler |
2ef84c95 | 159 | * |
f3ed775e | 160 | * Signal handler for the daemon |
2ef84c95 | 161 | */ |
f3ed775e | 162 | static void sighandler(int sig) |
2ef84c95 | 163 | { |
f3ed775e DG |
164 | switch (sig) { |
165 | case SIGTERM: | |
99bab54f | 166 | DBG("SIGTERM caught"); |
f3ed775e DG |
167 | clean_exit(EXIT_FAILURE); |
168 | break; | |
f3ed775e | 169 | default: |
99bab54f | 170 | DBG("Unknown signal %d caught", sig); |
f3ed775e | 171 | break; |
2ef84c95 DG |
172 | } |
173 | ||
f3ed775e | 174 | return; |
2ef84c95 DG |
175 | } |
176 | ||
177 | /* | |
f3ed775e | 178 | * set_signal_handler |
894be886 | 179 | * |
f3ed775e | 180 | * Setup signal handler for SIGCHLD and SIGTERM. |
894be886 | 181 | */ |
f3ed775e | 182 | static int set_signal_handler(void) |
894be886 | 183 | { |
f3ed775e DG |
184 | int ret = 0; |
185 | struct sigaction sa; | |
186 | sigset_t sigset; | |
33a2b854 | 187 | |
f3ed775e | 188 | if ((ret = sigemptyset(&sigset)) < 0) { |
6f04ed72 | 189 | PERROR("sigemptyset"); |
33a2b854 DG |
190 | goto end; |
191 | } | |
192 | ||
f3ed775e DG |
193 | sa.sa_handler = sighandler; |
194 | sa.sa_mask = sigset; | |
195 | sa.sa_flags = 0; | |
96243366 | 196 | |
f3ed775e | 197 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
6f04ed72 | 198 | PERROR("sigaction"); |
f3ed775e | 199 | goto end; |
894be886 DG |
200 | } |
201 | ||
96243366 | 202 | end: |
57167058 DG |
203 | return ret; |
204 | } | |
205 | ||
fac6795d | 206 | /* |
f3ed775e | 207 | * handle_command |
fac6795d | 208 | * |
f3ed775e DG |
209 | * Handle the full argv list of a first level command. Will find the command |
210 | * in the global commands array and call the function callback associated. | |
1c9f7941 | 211 | * |
f3ed775e DG |
212 | * If command not found, return -1 |
213 | * else, return function command error code. | |
1c9f7941 | 214 | */ |
f3ed775e | 215 | static int handle_command(int argc, char **argv) |
1c9f7941 | 216 | { |
f3ed775e DG |
217 | int i = 0, ret; |
218 | struct cmd_struct *cmd; | |
1c9f7941 | 219 | |
f3ed775e DG |
220 | if (*argv == NULL) { |
221 | ret = CMD_SUCCESS; | |
47b74d63 | 222 | goto end; |
1c9f7941 DG |
223 | } |
224 | ||
960afba4 PP |
225 | /* Special case for help command which needs the commands array */ |
226 | if (strcmp(argv[0], "help") == 0) { | |
227 | ret = cmd_help(argc, (const char**) argv, commands); | |
228 | goto end; | |
229 | } | |
230 | ||
f3ed775e | 231 | cmd = &commands[i]; |
960afba4 | 232 | while (cmd->name != NULL) { |
f3ed775e DG |
233 | /* Find command */ |
234 | if (strcmp(argv[0], cmd->name) == 0) { | |
235 | ret = cmd->func(argc, (const char**) argv); | |
f3ed775e | 236 | goto end; |
894be886 | 237 | } |
f3ed775e DG |
238 | i++; |
239 | cmd = &commands[i]; | |
894be886 DG |
240 | } |
241 | ||
f3ed775e | 242 | /* Command not found */ |
4ce78777 | 243 | ret = CMD_UNDEFINED; |
894be886 DG |
244 | |
245 | end: | |
f3ed775e | 246 | return ret; |
8548ff30 DG |
247 | } |
248 | ||
6047170f JG |
249 | static bool command_exists(const char *command) |
250 | { | |
251 | const struct cmd_struct *cmd = commands; | |
252 | bool exists = false; | |
253 | ||
254 | while (cmd->name != NULL) { | |
255 | if (!strcmp(command, cmd->name)) { | |
256 | exists = true; | |
257 | goto end; | |
258 | } | |
259 | cmd++; | |
260 | } | |
261 | ||
262 | end: | |
263 | return exists; | |
264 | } | |
265 | ||
9fb6c2db PP |
266 | static void show_basic_help(void) |
267 | { | |
268 | puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]"); | |
269 | puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]"); | |
270 | puts(""); | |
271 | puts("Available commands:"); | |
272 | puts(""); | |
273 | puts("Tracing sessions:"); | |
274 | puts(" create " CONFIG_CMD_DESCR_CREATE); | |
511ed4e2 | 275 | puts(" clear " CONFIG_CMD_DESCR_CLEAR); |
9fb6c2db PP |
276 | puts(" destroy " CONFIG_CMD_DESCR_DESTROY); |
277 | puts(" load " CONFIG_CMD_DESCR_LOAD); | |
faf04ce7 | 278 | puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE); |
9fb6c2db PP |
279 | puts(" save " CONFIG_CMD_DESCR_SAVE); |
280 | puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION); | |
281 | puts(""); | |
282 | puts("Channels:"); | |
283 | puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT); | |
284 | puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL); | |
285 | puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL); | |
286 | puts(""); | |
287 | puts("Event rules:"); | |
288 | puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT); | |
289 | puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT); | |
290 | puts(""); | |
291 | puts("Status:"); | |
292 | puts(" list " CONFIG_CMD_DESCR_LIST); | |
293 | puts(" status " CONFIG_CMD_DESCR_STATUS); | |
294 | puts(""); | |
295 | puts("Control:"); | |
296 | puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT); | |
297 | puts(" start " CONFIG_CMD_DESCR_START); | |
298 | puts(" stop " CONFIG_CMD_DESCR_STOP); | |
299 | puts(""); | |
77927271 JG |
300 | puts("Tracing session rotation:"); |
301 | puts(" disable-rotation " CONFIG_CMD_DESCR_DISABLE_ROTATION); | |
302 | puts(" enable-rotation " CONFIG_CMD_DESCR_ENABLE_ROTATION); | |
303 | puts(" rotate " CONFIG_CMD_DESCR_ROTATE); | |
304 | puts(""); | |
9fb6c2db PP |
305 | puts("Resource tracking:"); |
306 | puts(" track " CONFIG_CMD_DESCR_TRACK); | |
307 | puts(" untrack " CONFIG_CMD_DESCR_UNTRACK); | |
308 | puts(""); | |
309 | puts("Miscellaneous:"); | |
9fb6c2db PP |
310 | puts(" help " CONFIG_CMD_DESCR_HELP); |
311 | puts(" version " CONFIG_CMD_DESCR_VERSION); | |
312 | puts(" view " CONFIG_CMD_DESCR_VIEW); | |
313 | puts(""); | |
314 | puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with"); | |
315 | puts("command COMMAND."); | |
316 | puts(""); | |
317 | puts("See `man lttng` for more help with the lttng command."); | |
318 | } | |
319 | ||
5b8719f5 | 320 | /* |
3183dbb0 | 321 | * Parse command line arguments. |
5b8719f5 | 322 | * |
3183dbb0 | 323 | * Return 0 if OK, else -1 |
5b8719f5 | 324 | */ |
f3ed775e | 325 | static int parse_args(int argc, char **argv) |
5b8719f5 | 326 | { |
f3ed775e | 327 | int opt, ret; |
5b8719f5 | 328 | |
e8fa9fb0 MD |
329 | if (lttng_is_setuid_setgid()) { |
330 | ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]); | |
331 | clean_exit(EXIT_FAILURE); | |
332 | } | |
333 | ||
f3ed775e | 334 | if (argc < 2) { |
9fb6c2db | 335 | show_basic_help(); |
f3ed775e | 336 | clean_exit(EXIT_FAILURE); |
5b8719f5 DG |
337 | } |
338 | ||
c7e35b03 | 339 | while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) { |
f3ed775e | 340 | switch (opt) { |
70318c38 SS |
341 | case 'V': |
342 | version(stdout); | |
343 | ret = 0; | |
344 | goto end; | |
f3ed775e | 345 | case 'h': |
4fc83d94 | 346 | ret = utils_show_help(1, "lttng", help_msg); |
f67353ba | 347 | if (ret) { |
4fc83d94 | 348 | ERR("Cannot show --help for `lttng`"); |
f67353ba PP |
349 | perror("exec"); |
350 | } | |
3183dbb0 | 351 | goto end; |
f3ed775e | 352 | case 'v': |
849e5b7b DG |
353 | /* There is only 3 possible level of verbosity. (-vvv) */ |
354 | if (lttng_opt_verbose < 3) { | |
355 | lttng_opt_verbose += 1; | |
356 | } | |
f3ed775e DG |
357 | break; |
358 | case 'q': | |
97e19046 | 359 | lttng_opt_quiet = 1; |
f3ed775e | 360 | break; |
c7e35b03 JR |
361 | case 'm': |
362 | lttng_opt_mi = mi_output_type(optarg); | |
363 | if (lttng_opt_mi < 0) { | |
364 | ret = lttng_opt_mi; | |
365 | goto error; | |
366 | } | |
367 | break; | |
f3ed775e DG |
368 | case 'g': |
369 | lttng_set_tracing_group(optarg); | |
370 | break; | |
8490ae7b | 371 | case 'n': |
f3ed775e DG |
372 | opt_no_sessiond = 1; |
373 | break; | |
374 | case OPT_SESSION_PATH: | |
3ddaa633 | 375 | free(opt_sessiond_path); |
f3ed775e | 376 | opt_sessiond_path = strdup(optarg); |
d7c5d39f MD |
377 | if (!opt_sessiond_path) { |
378 | ret = -1; | |
379 | goto error; | |
380 | } | |
f3ed775e | 381 | break; |
8960e9cd | 382 | case OPT_RELAYD_PATH: |
3ddaa633 | 383 | free(opt_relayd_path); |
8960e9cd | 384 | opt_relayd_path = strdup(optarg); |
d7c5d39f MD |
385 | if (!opt_relayd_path) { |
386 | ret = -1; | |
387 | goto error; | |
388 | } | |
8960e9cd | 389 | break; |
865abf65 SM |
390 | case OPT_DUMP_OPTIONS: |
391 | list_options(stdout); | |
392 | ret = 0; | |
3183dbb0 | 393 | goto end; |
865abf65 | 394 | case OPT_DUMP_COMMANDS: |
3c9bd23c | 395 | list_commands(commands, stdout); |
865abf65 | 396 | ret = 0; |
3183dbb0 | 397 | goto end; |
f3ed775e | 398 | default: |
ae856491 | 399 | ret = 1; |
f3ed775e DG |
400 | goto error; |
401 | } | |
5b8719f5 | 402 | } |
fac6795d | 403 | |
f3ed775e | 404 | /* If both options are specified, quiet wins */ |
97e19046 DG |
405 | if (lttng_opt_verbose && lttng_opt_quiet) { |
406 | lttng_opt_verbose = 0; | |
5b8719f5 DG |
407 | } |
408 | ||
f67353ba | 409 | /* No leftovers, quit */ |
f3ed775e | 410 | if ((argc - optind) == 0) { |
8005f29a | 411 | ret = 1; |
f3ed775e DG |
412 | goto error; |
413 | } | |
7442b2ba | 414 | |
92360082 | 415 | /* |
f3ed775e DG |
416 | * Handle leftovers which is a first level command with the trailing |
417 | * options. | |
418 | */ | |
419 | ret = handle_command(argc - optind, argv + optind); | |
ae856491 DG |
420 | switch (ret) { |
421 | case CMD_WARNING: | |
ae856491 | 422 | case CMD_ERROR: |
ae856491 DG |
423 | break; |
424 | case CMD_UNDEFINED: | |
6047170f JG |
425 | if (!command_exists(*(argv + optind))) { |
426 | MSG("lttng: %s is not an lttng command. See 'lttng --help'.", | |
427 | *(argv + optind)); | |
428 | } else { | |
429 | ERR("Unrecognized argument used with \'%s\' command", | |
430 | *(argv + optind)); | |
431 | } | |
ae856491 DG |
432 | break; |
433 | case CMD_FATAL: | |
4ce78777 | 434 | case CMD_UNSUPPORTED: |
4ce78777 | 435 | break; |
ae856491 | 436 | case -1: |
ae856491 DG |
437 | ret = 1; |
438 | break; | |
439 | case 0: | |
440 | break; | |
441 | default: | |
42224349 DG |
442 | if (ret < 0) { |
443 | ret = -ret; | |
444 | } | |
ae856491 | 445 | break; |
96243366 DG |
446 | } |
447 | ||
3183dbb0 | 448 | end: |
f3ed775e | 449 | error: |
ae856491 | 450 | return ret; |
fac6795d DG |
451 | } |
452 | ||
f3ed775e | 453 | |
fac6795d | 454 | /* |
5b8719f5 | 455 | * main |
fac6795d DG |
456 | */ |
457 | int main(int argc, char *argv[]) | |
458 | { | |
459 | int ret; | |
460 | ||
461 | progname = argv[0] ? argv[0] : "lttng"; | |
462 | ||
5b8719f5 DG |
463 | ret = set_signal_handler(); |
464 | if (ret < 0) { | |
87378cf5 | 465 | clean_exit(ret); |
5b8719f5 DG |
466 | } |
467 | ||
f3ed775e | 468 | ret = parse_args(argc, argv); |
ae856491 DG |
469 | if (ret != 0) { |
470 | clean_exit(ret); | |
1fff1faa | 471 | } |
87378cf5 | 472 | |
fac6795d DG |
473 | return 0; |
474 | } |