Remove `metadata` command from various help resources
[lttng-tools.git] / src / bin / lttng / lttng.c
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 { "create", cmd_create},
70 { "destroy", cmd_destroy},
71 { "disable-channel", cmd_disable_channels},
72 { "disable-event", cmd_disable_events},
73 { "enable-channel", cmd_enable_channels},
74 { "enable-event", cmd_enable_events},
75 { "help", NULL},
76 { "list", cmd_list},
77 { "load", cmd_load},
78 { "metadata", cmd_metadata},
79 { "save", cmd_save},
80 { "set-session", cmd_set_session},
81 { "snapshot", cmd_snapshot},
82 { "start", cmd_start},
83 { "status", cmd_status},
84 { "stop", cmd_stop},
85 { "track", cmd_track},
86 { "untrack", cmd_untrack},
87 { "help", NULL},
88 { "version", cmd_version},
89 { "view", cmd_view},
90 { "regenerate", cmd_regenerate},
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 static void show_basic_help(void)
246 {
247 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
248 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
249 puts("");
250 puts("Available commands:");
251 puts("");
252 puts("Tracing sessions:");
253 puts(" create " CONFIG_CMD_DESCR_CREATE);
254 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
255 puts(" load " CONFIG_CMD_DESCR_LOAD);
256 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
257 puts(" save " CONFIG_CMD_DESCR_SAVE);
258 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
259 puts("");
260 puts("Channels:");
261 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
262 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
263 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
264 puts("");
265 puts("Event rules:");
266 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
267 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
268 puts("");
269 puts("Status:");
270 puts(" list " CONFIG_CMD_DESCR_LIST);
271 puts(" status " CONFIG_CMD_DESCR_STATUS);
272 puts("");
273 puts("Control:");
274 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
275 puts(" start " CONFIG_CMD_DESCR_START);
276 puts(" stop " CONFIG_CMD_DESCR_STOP);
277 puts("");
278 puts("Resource tracking:");
279 puts(" track " CONFIG_CMD_DESCR_TRACK);
280 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
281 puts("");
282 puts("Miscellaneous:");
283 puts(" help " CONFIG_CMD_DESCR_HELP);
284 puts(" version " CONFIG_CMD_DESCR_VERSION);
285 puts(" view " CONFIG_CMD_DESCR_VIEW);
286 puts("");
287 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
288 puts("command COMMAND.");
289 puts("");
290 puts("See `man lttng` for more help with the lttng command.");
291 }
292
293 /*
294 * Parse command line arguments.
295 *
296 * Return 0 if OK, else -1
297 */
298 static int parse_args(int argc, char **argv)
299 {
300 int opt, ret;
301
302 if (lttng_is_setuid_setgid()) {
303 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
304 clean_exit(EXIT_FAILURE);
305 }
306
307 if (argc < 2) {
308 show_basic_help();
309 clean_exit(EXIT_FAILURE);
310 }
311
312 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
313 switch (opt) {
314 case 'V':
315 version(stdout);
316 ret = 0;
317 goto end;
318 case 'h':
319 ret = utils_show_man_page(1, "lttng");
320
321 if (ret) {
322 ERR("Cannot view man page lttng(1)");
323 perror("exec");
324 }
325 goto end;
326 case 'v':
327 /* There is only 3 possible level of verbosity. (-vvv) */
328 if (lttng_opt_verbose < 3) {
329 lttng_opt_verbose += 1;
330 }
331 break;
332 case 'q':
333 lttng_opt_quiet = 1;
334 break;
335 case 'm':
336 lttng_opt_mi = mi_output_type(optarg);
337 if (lttng_opt_mi < 0) {
338 ret = lttng_opt_mi;
339 goto error;
340 }
341 break;
342 case 'g':
343 lttng_set_tracing_group(optarg);
344 break;
345 case 'n':
346 opt_no_sessiond = 1;
347 break;
348 case OPT_SESSION_PATH:
349 free(opt_sessiond_path);
350 opt_sessiond_path = strdup(optarg);
351 if (!opt_sessiond_path) {
352 ret = -1;
353 goto error;
354 }
355 break;
356 case OPT_RELAYD_PATH:
357 free(opt_relayd_path);
358 opt_relayd_path = strdup(optarg);
359 if (!opt_relayd_path) {
360 ret = -1;
361 goto error;
362 }
363 break;
364 case OPT_DUMP_OPTIONS:
365 list_options(stdout);
366 ret = 0;
367 goto end;
368 case OPT_DUMP_COMMANDS:
369 list_commands(commands, stdout);
370 ret = 0;
371 goto end;
372 default:
373 ret = 1;
374 goto error;
375 }
376 }
377
378 /* If both options are specified, quiet wins */
379 if (lttng_opt_verbose && lttng_opt_quiet) {
380 lttng_opt_verbose = 0;
381 }
382
383 /* No leftovers, quit */
384 if ((argc - optind) == 0) {
385 ret = 1;
386 goto error;
387 }
388
389 /*
390 * Handle leftovers which is a first level command with the trailing
391 * options.
392 */
393 ret = handle_command(argc - optind, argv + optind);
394 switch (ret) {
395 case CMD_WARNING:
396 WARN("Some command(s) went wrong");
397 break;
398 case CMD_ERROR:
399 ERR("Command error");
400 break;
401 case CMD_UNDEFINED:
402 ERR("Undefined command or invalid arguments");
403 break;
404 case CMD_FATAL:
405 ERR("Fatal error");
406 break;
407 case CMD_UNSUPPORTED:
408 ERR("Unsupported command");
409 break;
410 case -1:
411 ret = 1;
412 break;
413 case 0:
414 break;
415 default:
416 if (ret < 0) {
417 ret = -ret;
418 }
419 break;
420 }
421
422 end:
423 error:
424 return ret;
425 }
426
427
428 /*
429 * main
430 */
431 int main(int argc, char *argv[])
432 {
433 int ret;
434
435 progname = argv[0] ? argv[0] : "lttng";
436
437 ret = set_signal_handler();
438 if (ret < 0) {
439 clean_exit(ret);
440 }
441
442 ret = parse_args(argc, argv);
443 if (ret != 0) {
444 clean_exit(ret);
445 }
446
447 return 0;
448 }
This page took 0.062746 seconds and 5 git commands to generate.