2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <sys/types.h>
29 #include <lttng/lttng.h>
30 #include <common/error.h>
31 #include <common/compat/getenv.h>
32 #include <common/utils.h>
37 static char *progname
;
39 char *opt_sessiond_path
;
41 char *opt_relayd_path
;
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'},
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
},
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
},
79 { "metadata", cmd_metadata
},
81 { "set-session", cmd_set_session
},
82 { "snapshot", cmd_snapshot
},
83 { "start", cmd_start
},
84 { "status", cmd_status
},
86 { "track", cmd_track
},
87 { "untrack", cmd_untrack
},
89 { "version", cmd_version
},
91 { NULL
, NULL
} /* Array closure */
94 static void version(FILE *ofp
)
96 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
98 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
102 * Find the MI output type enum from a string. This function is for the support
103 * of machine interface output.
105 static int mi_output_type(const char *output_type
)
109 if (!strncasecmp("xml", output_type
, 3)) {
112 /* Invalid output format */
113 ERR("MI output format not supported");
114 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
123 * List options line by line. This is mostly for bash auto completion and to
124 * avoid difficult parsing.
126 static void list_options(FILE *ofp
)
129 struct option
*option
= NULL
;
131 option
= &long_options
[i
];
132 while (option
->name
!= NULL
) {
133 fprintf(ofp
, "--%s\n", option
->name
);
135 if (isprint(option
->val
)) {
136 fprintf(ofp
, "-%c\n", option
->val
);
140 option
= &long_options
[i
];
147 static void clean_exit(int code
)
156 * Signal handler for the daemon
158 static void sighandler(int sig
)
162 DBG("SIGTERM caught");
163 clean_exit(EXIT_FAILURE
);
166 DBG("Unknown signal %d caught", sig
);
176 * Setup signal handler for SIGCHLD and SIGTERM.
178 static int set_signal_handler(void)
184 if ((ret
= sigemptyset(&sigset
)) < 0) {
185 PERROR("sigemptyset");
189 sa
.sa_handler
= sighandler
;
193 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
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.
208 * If command not found, return -1
209 * else, return function command error code.
211 static int handle_command(int argc
, char **argv
)
214 struct cmd_struct
*cmd
;
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
);
228 while (cmd
->name
!= NULL
) {
230 if (strcmp(argv
[0], cmd
->name
) == 0) {
231 ret
= cmd
->func(argc
, (const char**) argv
);
238 /* Command not found */
246 * Parse command line arguments.
248 * Return 0 if OK, else -1
250 static int parse_args(int argc
, char **argv
)
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
);
261 clean_exit(EXIT_FAILURE
);
264 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
271 ret
= utils_show_man_page(1, "lttng");
274 ERR("Cannot view man page lttng(1)");
279 /* There is only 3 possible level of verbosity. (-vvv) */
280 if (lttng_opt_verbose
< 3) {
281 lttng_opt_verbose
+= 1;
288 lttng_opt_mi
= mi_output_type(optarg
);
289 if (lttng_opt_mi
< 0) {
295 lttng_set_tracing_group(optarg
);
300 case OPT_SESSION_PATH
:
301 opt_sessiond_path
= strdup(optarg
);
302 if (!opt_sessiond_path
) {
307 case OPT_RELAYD_PATH
:
308 opt_relayd_path
= strdup(optarg
);
309 if (!opt_relayd_path
) {
314 case OPT_DUMP_OPTIONS
:
315 list_options(stdout
);
318 case OPT_DUMP_COMMANDS
:
319 list_commands(commands
, stdout
);
328 /* If both options are specified, quiet wins */
329 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
330 lttng_opt_verbose
= 0;
333 /* No leftovers, quit */
334 if ((argc
- optind
) == 0) {
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);
348 * Handle leftovers which is a first level command with the trailing
351 ret
= handle_command(argc
- optind
, argv
+ optind
);
354 WARN("Some command(s) went wrong");
357 ERR("Command error");
360 ERR("Undefined command or invalid arguments");
365 case CMD_UNSUPPORTED
:
366 ERR("Unsupported command");
389 int main(int argc
, char *argv
[])
393 progname
= argv
[0] ? argv
[0] : "lttng";
395 ret
= set_signal_handler();
400 ret
= parse_args(argc
, argv
);
This page took 0.038453 seconds and 5 git commands to generate.