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