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