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