| 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 | |
| 36 | /* Variables */ |
| 37 | static char *progname; |
| 38 | int opt_no_sessiond; |
| 39 | char *opt_sessiond_path; |
| 40 | |
| 41 | char *opt_relayd_path; |
| 42 | |
| 43 | enum { |
| 44 | OPT_RELAYD_PATH, |
| 45 | OPT_SESSION_PATH, |
| 46 | OPT_DUMP_OPTIONS, |
| 47 | OPT_DUMP_COMMANDS, |
| 48 | }; |
| 49 | |
| 50 | /* Getopt options. No first level command. */ |
| 51 | static struct option long_options[] = { |
| 52 | {"version", 0, NULL, 'V'}, |
| 53 | {"help", 0, NULL, 'h'}, |
| 54 | {"group", 1, NULL, 'g'}, |
| 55 | {"verbose", 0, NULL, 'v'}, |
| 56 | {"quiet", 0, NULL, 'q'}, |
| 57 | {"mi", 1, NULL, 'm'}, |
| 58 | {"no-sessiond", 0, NULL, 'n'}, |
| 59 | {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, |
| 60 | {"relayd-path", 1, NULL, OPT_RELAYD_PATH}, |
| 61 | {"list-options", 0, NULL, OPT_DUMP_OPTIONS}, |
| 62 | {"list-commands", 0, NULL, OPT_DUMP_COMMANDS}, |
| 63 | {NULL, 0, NULL, 0} |
| 64 | }; |
| 65 | |
| 66 | /* First level command */ |
| 67 | static struct cmd_struct commands[] = { |
| 68 | { "add-context", cmd_add_context}, |
| 69 | { "calibrate", cmd_calibrate}, |
| 70 | { "create", cmd_create}, |
| 71 | { "destroy", cmd_destroy}, |
| 72 | { "disable-channel", cmd_disable_channels}, |
| 73 | { "disable-event", cmd_disable_events}, |
| 74 | { "enable-channel", cmd_enable_channels}, |
| 75 | { "enable-event", cmd_enable_events}, |
| 76 | { "help", NULL}, |
| 77 | { "list", cmd_list}, |
| 78 | { "load", cmd_load}, |
| 79 | { "metadata", cmd_metadata}, |
| 80 | { "save", cmd_save}, |
| 81 | { "set-session", cmd_set_session}, |
| 82 | { "snapshot", cmd_snapshot}, |
| 83 | { "start", cmd_start}, |
| 84 | { "status", cmd_status}, |
| 85 | { "stop", cmd_stop}, |
| 86 | { "track", cmd_track}, |
| 87 | { "untrack", cmd_untrack}, |
| 88 | { "help", NULL}, |
| 89 | { "version", cmd_version}, |
| 90 | { "view", cmd_view}, |
| 91 | { NULL, NULL} /* Array closure */ |
| 92 | }; |
| 93 | |
| 94 | static void version(FILE *ofp) |
| 95 | { |
| 96 | fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n", |
| 97 | progname, |
| 98 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Find the MI output type enum from a string. This function is for the support |
| 103 | * of machine interface output. |
| 104 | */ |
| 105 | static int mi_output_type(const char *output_type) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | |
| 109 | if (!strncasecmp("xml", output_type, 3)) { |
| 110 | ret = LTTNG_MI_XML; |
| 111 | } else { |
| 112 | /* Invalid output format */ |
| 113 | ERR("MI output format not supported"); |
| 114 | ret = -LTTNG_ERR_MI_OUTPUT_TYPE; |
| 115 | } |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * list_options |
| 122 | * |
| 123 | * List options line by line. This is mostly for bash auto completion and to |
| 124 | * avoid difficult parsing. |
| 125 | */ |
| 126 | static void list_options(FILE *ofp) |
| 127 | { |
| 128 | int i = 0; |
| 129 | struct option *option = NULL; |
| 130 | |
| 131 | option = &long_options[i]; |
| 132 | while (option->name != NULL) { |
| 133 | fprintf(ofp, "--%s\n", option->name); |
| 134 | |
| 135 | if (isprint(option->val)) { |
| 136 | fprintf(ofp, "-%c\n", option->val); |
| 137 | } |
| 138 | |
| 139 | i++; |
| 140 | option = &long_options[i]; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * clean_exit |
| 146 | */ |
| 147 | static void clean_exit(int code) |
| 148 | { |
| 149 | DBG("Clean exit"); |
| 150 | exit(code); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * sighandler |
| 155 | * |
| 156 | * Signal handler for the daemon |
| 157 | */ |
| 158 | static void sighandler(int sig) |
| 159 | { |
| 160 | switch (sig) { |
| 161 | case SIGTERM: |
| 162 | DBG("SIGTERM caught"); |
| 163 | clean_exit(EXIT_FAILURE); |
| 164 | break; |
| 165 | default: |
| 166 | DBG("Unknown signal %d caught", sig); |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * set_signal_handler |
| 175 | * |
| 176 | * Setup signal handler for SIGCHLD and SIGTERM. |
| 177 | */ |
| 178 | static int set_signal_handler(void) |
| 179 | { |
| 180 | int ret = 0; |
| 181 | struct sigaction sa; |
| 182 | sigset_t sigset; |
| 183 | |
| 184 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 185 | PERROR("sigemptyset"); |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | sa.sa_handler = sighandler; |
| 190 | sa.sa_mask = sigset; |
| 191 | sa.sa_flags = 0; |
| 192 | |
| 193 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 194 | PERROR("sigaction"); |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | end: |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * handle_command |
| 204 | * |
| 205 | * Handle the full argv list of a first level command. Will find the command |
| 206 | * in the global commands array and call the function callback associated. |
| 207 | * |
| 208 | * If command not found, return -1 |
| 209 | * else, return function command error code. |
| 210 | */ |
| 211 | static int handle_command(int argc, char **argv) |
| 212 | { |
| 213 | int i = 0, ret; |
| 214 | struct cmd_struct *cmd; |
| 215 | |
| 216 | if (*argv == NULL) { |
| 217 | ret = CMD_SUCCESS; |
| 218 | goto end; |
| 219 | } |
| 220 | |
| 221 | /* Special case for help command which needs the commands array */ |
| 222 | if (strcmp(argv[0], "help") == 0) { |
| 223 | ret = cmd_help(argc, (const char**) argv, commands); |
| 224 | goto end; |
| 225 | } |
| 226 | |
| 227 | cmd = &commands[i]; |
| 228 | while (cmd->name != NULL) { |
| 229 | /* Find command */ |
| 230 | if (strcmp(argv[0], cmd->name) == 0) { |
| 231 | ret = cmd->func(argc, (const char**) argv); |
| 232 | goto end; |
| 233 | } |
| 234 | i++; |
| 235 | cmd = &commands[i]; |
| 236 | } |
| 237 | |
| 238 | /* Command not found */ |
| 239 | ret = CMD_UNDEFINED; |
| 240 | |
| 241 | end: |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Parse command line arguments. |
| 247 | * |
| 248 | * Return 0 if OK, else -1 |
| 249 | */ |
| 250 | static int parse_args(int argc, char **argv) |
| 251 | { |
| 252 | int opt, ret; |
| 253 | char *user; |
| 254 | |
| 255 | if (lttng_is_setuid_setgid()) { |
| 256 | ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]); |
| 257 | clean_exit(EXIT_FAILURE); |
| 258 | } |
| 259 | |
| 260 | if (argc < 2) { |
| 261 | clean_exit(EXIT_FAILURE); |
| 262 | } |
| 263 | |
| 264 | while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) { |
| 265 | switch (opt) { |
| 266 | case 'V': |
| 267 | version(stdout); |
| 268 | ret = 0; |
| 269 | goto end; |
| 270 | case 'h': |
| 271 | ret = utils_show_man_page(1, "lttng"); |
| 272 | |
| 273 | if (ret) { |
| 274 | ERR("Cannot view man page lttng(1)"); |
| 275 | perror("exec"); |
| 276 | } |
| 277 | goto end; |
| 278 | case 'v': |
| 279 | /* There is only 3 possible level of verbosity. (-vvv) */ |
| 280 | if (lttng_opt_verbose < 3) { |
| 281 | lttng_opt_verbose += 1; |
| 282 | } |
| 283 | break; |
| 284 | case 'q': |
| 285 | lttng_opt_quiet = 1; |
| 286 | break; |
| 287 | case 'm': |
| 288 | lttng_opt_mi = mi_output_type(optarg); |
| 289 | if (lttng_opt_mi < 0) { |
| 290 | ret = lttng_opt_mi; |
| 291 | goto error; |
| 292 | } |
| 293 | break; |
| 294 | case 'g': |
| 295 | lttng_set_tracing_group(optarg); |
| 296 | break; |
| 297 | case 'n': |
| 298 | opt_no_sessiond = 1; |
| 299 | break; |
| 300 | case OPT_SESSION_PATH: |
| 301 | opt_sessiond_path = strdup(optarg); |
| 302 | if (!opt_sessiond_path) { |
| 303 | ret = -1; |
| 304 | goto error; |
| 305 | } |
| 306 | break; |
| 307 | case OPT_RELAYD_PATH: |
| 308 | opt_relayd_path = strdup(optarg); |
| 309 | if (!opt_relayd_path) { |
| 310 | ret = -1; |
| 311 | goto error; |
| 312 | } |
| 313 | break; |
| 314 | case OPT_DUMP_OPTIONS: |
| 315 | list_options(stdout); |
| 316 | ret = 0; |
| 317 | goto end; |
| 318 | case OPT_DUMP_COMMANDS: |
| 319 | list_commands(commands, stdout); |
| 320 | ret = 0; |
| 321 | goto end; |
| 322 | default: |
| 323 | ret = 1; |
| 324 | goto error; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* If both options are specified, quiet wins */ |
| 329 | if (lttng_opt_verbose && lttng_opt_quiet) { |
| 330 | lttng_opt_verbose = 0; |
| 331 | } |
| 332 | |
| 333 | /* No leftovers, quit */ |
| 334 | if ((argc - optind) == 0) { |
| 335 | ret = 1; |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | /* For Mathieu Desnoyers a.k.a. Dr. Tracing */ |
| 340 | user = getenv("USER"); |
| 341 | if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 || |
| 342 | strncmp("compudj", user, 7) == 0))) { |
| 343 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0); |
| 344 | } |
| 345 | /* Thanks Mathieu */ |
| 346 | |
| 347 | /* |
| 348 | * Handle leftovers which is a first level command with the trailing |
| 349 | * options. |
| 350 | */ |
| 351 | ret = handle_command(argc - optind, argv + optind); |
| 352 | switch (ret) { |
| 353 | case CMD_WARNING: |
| 354 | WARN("Some command(s) went wrong"); |
| 355 | break; |
| 356 | case CMD_ERROR: |
| 357 | ERR("Command error"); |
| 358 | break; |
| 359 | case CMD_UNDEFINED: |
| 360 | ERR("Undefined command or invalid arguments"); |
| 361 | break; |
| 362 | case CMD_FATAL: |
| 363 | ERR("Fatal error"); |
| 364 | break; |
| 365 | case CMD_UNSUPPORTED: |
| 366 | ERR("Unsupported command"); |
| 367 | break; |
| 368 | case -1: |
| 369 | ret = 1; |
| 370 | break; |
| 371 | case 0: |
| 372 | break; |
| 373 | default: |
| 374 | if (ret < 0) { |
| 375 | ret = -ret; |
| 376 | } |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | end: |
| 381 | error: |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /* |
| 387 | * main |
| 388 | */ |
| 389 | int main(int argc, char *argv[]) |
| 390 | { |
| 391 | int ret; |
| 392 | |
| 393 | progname = argv[0] ? argv[0] : "lttng"; |
| 394 | |
| 395 | ret = set_signal_handler(); |
| 396 | if (ret < 0) { |
| 397 | clean_exit(ret); |
| 398 | } |
| 399 | |
| 400 | ret = parse_args(argc, argv); |
| 401 | if (ret != 0) { |
| 402 | clean_exit(ret); |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |