bcfbf88e65aed6c701e5d2e2a458a1c2e9918dd0
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
[] = {
69 { "status", cmd_status
},
70 { "create", cmd_create
},
71 { "destroy", cmd_destroy
},
72 { "start", cmd_start
},
74 { "enable-event", cmd_enable_events
},
75 { "disable-event", cmd_disable_events
},
76 { "enable-channel", cmd_enable_channels
},
77 { "disable-channel", cmd_disable_channels
},
78 { "add-context", cmd_add_context
},
79 { "set-session", cmd_set_session
},
80 { "version", cmd_version
},
81 { "calibrate", cmd_calibrate
},
83 { "snapshot", cmd_snapshot
},
86 { "track", cmd_track
},
87 { "untrack", cmd_untrack
},
88 { "metadata", cmd_metadata
},
90 { NULL
, NULL
} /* Array closure */
93 static void version(FILE *ofp
)
95 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
97 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
101 * Find the MI output type enum from a string. This function is for the support
102 * of machine interface output.
104 static int mi_output_type(const char *output_type
)
108 if (!strncasecmp("xml", output_type
, 3)) {
111 /* Invalid output format */
112 ERR("MI output format not supported");
113 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
122 * List options line by line. This is mostly for bash auto completion and to
123 * avoid difficult parsing.
125 static void list_options(FILE *ofp
)
128 struct option
*option
= NULL
;
130 option
= &long_options
[i
];
131 while (option
->name
!= NULL
) {
132 fprintf(ofp
, "--%s\n", option
->name
);
134 if (isprint(option
->val
)) {
135 fprintf(ofp
, "-%c\n", option
->val
);
139 option
= &long_options
[i
];
146 static void clean_exit(int code
)
155 * Signal handler for the daemon
157 static void sighandler(int sig
)
161 DBG("SIGTERM caught");
162 clean_exit(EXIT_FAILURE
);
165 DBG("Unknown signal %d caught", sig
);
175 * Setup signal handler for SIGCHLD and SIGTERM.
177 static int set_signal_handler(void)
183 if ((ret
= sigemptyset(&sigset
)) < 0) {
184 PERROR("sigemptyset");
188 sa
.sa_handler
= sighandler
;
192 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
204 * Handle the full argv list of a first level command. Will find the command
205 * in the global commands array and call the function callback associated.
207 * If command not found, return -1
208 * else, return function command error code.
210 static int handle_command(int argc
, char **argv
)
213 struct cmd_struct
*cmd
;
220 /* Special case for help command which needs the commands array */
221 if (strcmp(argv
[0], "help") == 0) {
222 ret
= cmd_help(argc
, (const char**) argv
, commands
);
227 while (cmd
->name
!= NULL
) {
229 if (strcmp(argv
[0], cmd
->name
) == 0) {
230 ret
= cmd
->func(argc
, (const char**) argv
);
237 /* Command not found */
245 * Parse command line arguments.
247 * Return 0 if OK, else -1
249 static int parse_args(int argc
, char **argv
)
254 if (lttng_is_setuid_setgid()) {
255 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
256 clean_exit(EXIT_FAILURE
);
260 clean_exit(EXIT_FAILURE
);
263 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
270 ret
= utils_show_man_page(1, "lttng");
273 ERR("Cannot view man page lttng(1)");
278 /* There is only 3 possible level of verbosity. (-vvv) */
279 if (lttng_opt_verbose
< 3) {
280 lttng_opt_verbose
+= 1;
287 lttng_opt_mi
= mi_output_type(optarg
);
288 if (lttng_opt_mi
< 0) {
294 lttng_set_tracing_group(optarg
);
299 case OPT_SESSION_PATH
:
300 opt_sessiond_path
= strdup(optarg
);
301 if (!opt_sessiond_path
) {
306 case OPT_RELAYD_PATH
:
307 opt_relayd_path
= strdup(optarg
);
308 if (!opt_relayd_path
) {
313 case OPT_DUMP_OPTIONS
:
314 list_options(stdout
);
317 case OPT_DUMP_COMMANDS
:
318 list_commands(commands
, stdout
);
327 /* If both options are specified, quiet wins */
328 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
329 lttng_opt_verbose
= 0;
332 /* No leftovers, quit */
333 if ((argc
- optind
) == 0) {
338 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
339 user
= getenv("USER");
340 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
341 strncmp("compudj", user
, 7) == 0))) {
342 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
347 * Handle leftovers which is a first level command with the trailing
350 ret
= handle_command(argc
- optind
, argv
+ optind
);
353 WARN("Some command(s) went wrong");
356 ERR("Command error");
359 ERR("Undefined command or invalid arguments");
364 case CMD_UNSUPPORTED
:
365 ERR("Unsupported command");
388 int main(int argc
, char *argv
[])
392 progname
= argv
[0] ? argv
[0] : "lttng";
394 ret
= set_signal_handler();
399 ret
= parse_args(argc
, argv
);
This page took 0.081147 seconds and 4 git commands to generate.