Define command short descriptions in configure.ac
[lttng-tools.git] / src / bin / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or modify
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
fac6795d
DG
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.
fac6795d
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
fac6795d 19#include <getopt.h>
f3ed775e 20#include <signal.h>
fac6795d
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
8db8d1dc
DG
24#include <sys/types.h>
25#include <sys/wait.h>
fac6795d 26#include <unistd.h>
730389d9 27#include <ctype.h>
fac6795d 28
5b97ec60 29#include <lttng/lttng.h>
db758600 30#include <common/error.h>
e8fa9fb0 31#include <common/compat/getenv.h>
f67353ba 32#include <common/utils.h>
fac6795d 33
c399183f 34#include "command.h"
fac6795d
DG
35
36/* Variables */
37static char *progname;
92360082
JG
38int opt_no_sessiond;
39char *opt_sessiond_path;
f3ed775e 40
8960e9cd
DG
41char *opt_relayd_path;
42
f3ed775e 43enum {
8960e9cd 44 OPT_RELAYD_PATH,
f3ed775e 45 OPT_SESSION_PATH,
865abf65
SM
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
f3ed775e
DG
48};
49
50/* Getopt options. No first level command. */
51static struct option long_options[] = {
70318c38 52 {"version", 0, NULL, 'V'},
f3ed775e
DG
53 {"help", 0, NULL, 'h'},
54 {"group", 1, NULL, 'g'},
55 {"verbose", 0, NULL, 'v'},
56 {"quiet", 0, NULL, 'q'},
c7e35b03 57 {"mi", 1, NULL, 'm'},
8490ae7b 58 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 59 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
8960e9cd 60 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
865abf65
SM
61 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
62 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
63 {NULL, 0, NULL, 0}
64};
65
66/* First level command */
67static struct cmd_struct commands[] = {
3ee1477a
PP
68 { "add-context", cmd_add_context},
69 { "calibrate", cmd_calibrate},
f3ed775e
DG
70 { "create", cmd_create},
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},
78 { "load", cmd_load},
79 { "metadata", cmd_metadata},
80 { "save", cmd_save},
3087ef18 81 { "set-session", cmd_set_session},
57f272ed 82 { "snapshot", cmd_snapshot},
3ee1477a
PP
83 { "start", cmd_start},
84 { "status", cmd_status},
85 { "stop", cmd_stop},
ccf10263
MD
86 { "track", cmd_track},
87 { "untrack", cmd_untrack},
960afba4 88 { "help", NULL},
3ee1477a
PP
89 { "version", cmd_version},
90 { "view", cmd_view},
f3ed775e
DG
91 { NULL, NULL} /* Array closure */
92};
93
70318c38
SS
94static void version(FILE *ofp)
95{
4c6ac053
MD
96 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
97 progname,
98 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
70318c38
SS
99}
100
c7e35b03
JR
101/*
102 * Find the MI output type enum from a string. This function is for the support
103 * of machine interface output.
104 */
105static 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
865abf65
SM
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 */
126static 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
7442b2ba 144/*
f3ed775e 145 * clean_exit
96243366 146 */
f3ed775e 147static void clean_exit(int code)
96243366 148{
f3ed775e
DG
149 DBG("Clean exit");
150 exit(code);
894be886 151}
96243366 152
894be886 153/*
f3ed775e 154 * sighandler
2ef84c95 155 *
f3ed775e 156 * Signal handler for the daemon
2ef84c95 157 */
f3ed775e 158static void sighandler(int sig)
2ef84c95 159{
f3ed775e
DG
160 switch (sig) {
161 case SIGTERM:
99bab54f 162 DBG("SIGTERM caught");
f3ed775e
DG
163 clean_exit(EXIT_FAILURE);
164 break;
f3ed775e 165 default:
99bab54f 166 DBG("Unknown signal %d caught", sig);
f3ed775e 167 break;
2ef84c95
DG
168 }
169
f3ed775e 170 return;
2ef84c95
DG
171}
172
173/*
f3ed775e 174 * set_signal_handler
894be886 175 *
f3ed775e 176 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 177 */
f3ed775e 178static int set_signal_handler(void)
894be886 179{
f3ed775e
DG
180 int ret = 0;
181 struct sigaction sa;
182 sigset_t sigset;
33a2b854 183
f3ed775e 184 if ((ret = sigemptyset(&sigset)) < 0) {
6f04ed72 185 PERROR("sigemptyset");
33a2b854
DG
186 goto end;
187 }
188
f3ed775e
DG
189 sa.sa_handler = sighandler;
190 sa.sa_mask = sigset;
191 sa.sa_flags = 0;
96243366 192
f3ed775e 193 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
6f04ed72 194 PERROR("sigaction");
f3ed775e 195 goto end;
894be886
DG
196 }
197
96243366 198end:
57167058
DG
199 return ret;
200}
201
fac6795d 202/*
f3ed775e 203 * handle_command
fac6795d 204 *
f3ed775e
DG
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.
1c9f7941 207 *
f3ed775e
DG
208 * If command not found, return -1
209 * else, return function command error code.
1c9f7941 210 */
f3ed775e 211static int handle_command(int argc, char **argv)
1c9f7941 212{
f3ed775e
DG
213 int i = 0, ret;
214 struct cmd_struct *cmd;
1c9f7941 215
f3ed775e
DG
216 if (*argv == NULL) {
217 ret = CMD_SUCCESS;
47b74d63 218 goto end;
1c9f7941
DG
219 }
220
960afba4
PP
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
f3ed775e 227 cmd = &commands[i];
960afba4 228 while (cmd->name != NULL) {
f3ed775e
DG
229 /* Find command */
230 if (strcmp(argv[0], cmd->name) == 0) {
231 ret = cmd->func(argc, (const char**) argv);
f3ed775e 232 goto end;
894be886 233 }
f3ed775e
DG
234 i++;
235 cmd = &commands[i];
894be886
DG
236 }
237
f3ed775e 238 /* Command not found */
4ce78777 239 ret = CMD_UNDEFINED;
894be886
DG
240
241end:
f3ed775e 242 return ret;
8548ff30
DG
243}
244
5b8719f5 245/*
3183dbb0 246 * Parse command line arguments.
5b8719f5 247 *
3183dbb0 248 * Return 0 if OK, else -1
5b8719f5 249 */
f3ed775e 250static int parse_args(int argc, char **argv)
5b8719f5 251{
f3ed775e 252 int opt, ret;
883d80f9 253 char *user;
5b8719f5 254
e8fa9fb0
MD
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
f3ed775e 260 if (argc < 2) {
f3ed775e 261 clean_exit(EXIT_FAILURE);
5b8719f5
DG
262 }
263
c7e35b03 264 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
f3ed775e 265 switch (opt) {
70318c38
SS
266 case 'V':
267 version(stdout);
268 ret = 0;
269 goto end;
f3ed775e 270 case 'h':
f67353ba
PP
271 ret = utils_show_man_page(1, "lttng");
272
273 if (ret) {
274 ERR("Cannot view man page lttng(1)");
275 perror("exec");
276 }
3183dbb0 277 goto end;
f3ed775e 278 case 'v':
849e5b7b
DG
279 /* There is only 3 possible level of verbosity. (-vvv) */
280 if (lttng_opt_verbose < 3) {
281 lttng_opt_verbose += 1;
282 }
f3ed775e
DG
283 break;
284 case 'q':
97e19046 285 lttng_opt_quiet = 1;
f3ed775e 286 break;
c7e35b03
JR
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;
f3ed775e
DG
294 case 'g':
295 lttng_set_tracing_group(optarg);
296 break;
8490ae7b 297 case 'n':
f3ed775e
DG
298 opt_no_sessiond = 1;
299 break;
300 case OPT_SESSION_PATH:
301 opt_sessiond_path = strdup(optarg);
d7c5d39f
MD
302 if (!opt_sessiond_path) {
303 ret = -1;
304 goto error;
305 }
f3ed775e 306 break;
8960e9cd
DG
307 case OPT_RELAYD_PATH:
308 opt_relayd_path = strdup(optarg);
d7c5d39f
MD
309 if (!opt_relayd_path) {
310 ret = -1;
311 goto error;
312 }
8960e9cd 313 break;
865abf65
SM
314 case OPT_DUMP_OPTIONS:
315 list_options(stdout);
316 ret = 0;
3183dbb0 317 goto end;
865abf65 318 case OPT_DUMP_COMMANDS:
3c9bd23c 319 list_commands(commands, stdout);
865abf65 320 ret = 0;
3183dbb0 321 goto end;
f3ed775e 322 default:
ae856491 323 ret = 1;
f3ed775e
DG
324 goto error;
325 }
5b8719f5 326 }
fac6795d 327
f3ed775e 328 /* If both options are specified, quiet wins */
97e19046
DG
329 if (lttng_opt_verbose && lttng_opt_quiet) {
330 lttng_opt_verbose = 0;
5b8719f5
DG
331 }
332
f67353ba 333 /* No leftovers, quit */
f3ed775e 334 if ((argc - optind) == 0) {
8005f29a 335 ret = 1;
f3ed775e
DG
336 goto error;
337 }
7442b2ba 338
883d80f9
DG
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
92360082 347 /*
f3ed775e
DG
348 * Handle leftovers which is a first level command with the trailing
349 * options.
350 */
351 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
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:
f0f2dce9 360 ERR("Undefined command or invalid arguments");
ae856491
DG
361 break;
362 case CMD_FATAL:
363 ERR("Fatal error");
364 break;
4ce78777
DG
365 case CMD_UNSUPPORTED:
366 ERR("Unsupported command");
367 break;
ae856491 368 case -1:
ae856491
DG
369 ret = 1;
370 break;
371 case 0:
372 break;
373 default:
42224349
DG
374 if (ret < 0) {
375 ret = -ret;
376 }
ae856491 377 break;
96243366
DG
378 }
379
3183dbb0 380end:
f3ed775e 381error:
ae856491 382 return ret;
fac6795d
DG
383}
384
f3ed775e 385
fac6795d 386/*
5b8719f5 387 * main
fac6795d
DG
388 */
389int main(int argc, char *argv[])
390{
391 int ret;
392
393 progname = argv[0] ? argv[0] : "lttng";
394
5b8719f5
DG
395 ret = set_signal_handler();
396 if (ret < 0) {
87378cf5 397 clean_exit(ret);
5b8719f5
DG
398 }
399
f3ed775e 400 ret = parse_args(argc, argv);
ae856491
DG
401 if (ret != 0) {
402 clean_exit(ret);
1fff1faa 403 }
87378cf5 404
fac6795d
DG
405 return 0;
406}
This page took 0.12385 seconds and 5 git commands to generate.