Clean-up: remove unused variable user
[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 { "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 { "regenerate", cmd_regenerate},
92 { NULL, NULL} /* Array closure */
93 };
94
95 static void version(FILE *ofp)
96 {
97 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
98 progname,
99 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
100 }
101
102 /*
103 * Find the MI output type enum from a string. This function is for the support
104 * of machine interface output.
105 */
106 static int mi_output_type(const char *output_type)
107 {
108 int ret = 0;
109
110 if (!strncasecmp("xml", output_type, 3)) {
111 ret = LTTNG_MI_XML;
112 } else {
113 /* Invalid output format */
114 ERR("MI output format not supported");
115 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
116 }
117
118 return ret;
119 }
120
121 /*
122 * list_options
123 *
124 * List options line by line. This is mostly for bash auto completion and to
125 * avoid difficult parsing.
126 */
127 static void list_options(FILE *ofp)
128 {
129 int i = 0;
130 struct option *option = NULL;
131
132 option = &long_options[i];
133 while (option->name != NULL) {
134 fprintf(ofp, "--%s\n", option->name);
135
136 if (isprint(option->val)) {
137 fprintf(ofp, "-%c\n", option->val);
138 }
139
140 i++;
141 option = &long_options[i];
142 }
143 }
144
145 /*
146 * clean_exit
147 */
148 static void clean_exit(int code)
149 {
150 DBG("Clean exit");
151 exit(code);
152 }
153
154 /*
155 * sighandler
156 *
157 * Signal handler for the daemon
158 */
159 static void sighandler(int sig)
160 {
161 switch (sig) {
162 case SIGTERM:
163 DBG("SIGTERM caught");
164 clean_exit(EXIT_FAILURE);
165 break;
166 default:
167 DBG("Unknown signal %d caught", sig);
168 break;
169 }
170
171 return;
172 }
173
174 /*
175 * set_signal_handler
176 *
177 * Setup signal handler for SIGCHLD and SIGTERM.
178 */
179 static int set_signal_handler(void)
180 {
181 int ret = 0;
182 struct sigaction sa;
183 sigset_t sigset;
184
185 if ((ret = sigemptyset(&sigset)) < 0) {
186 PERROR("sigemptyset");
187 goto end;
188 }
189
190 sa.sa_handler = sighandler;
191 sa.sa_mask = sigset;
192 sa.sa_flags = 0;
193
194 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
195 PERROR("sigaction");
196 goto end;
197 }
198
199 end:
200 return ret;
201 }
202
203 /*
204 * handle_command
205 *
206 * Handle the full argv list of a first level command. Will find the command
207 * in the global commands array and call the function callback associated.
208 *
209 * If command not found, return -1
210 * else, return function command error code.
211 */
212 static int handle_command(int argc, char **argv)
213 {
214 int i = 0, ret;
215 struct cmd_struct *cmd;
216
217 if (*argv == NULL) {
218 ret = CMD_SUCCESS;
219 goto end;
220 }
221
222 /* Special case for help command which needs the commands array */
223 if (strcmp(argv[0], "help") == 0) {
224 ret = cmd_help(argc, (const char**) argv, commands);
225 goto end;
226 }
227
228 cmd = &commands[i];
229 while (cmd->name != NULL) {
230 /* Find command */
231 if (strcmp(argv[0], cmd->name) == 0) {
232 ret = cmd->func(argc, (const char**) argv);
233 goto end;
234 }
235 i++;
236 cmd = &commands[i];
237 }
238
239 /* Command not found */
240 ret = CMD_UNDEFINED;
241
242 end:
243 return ret;
244 }
245
246 static void show_basic_help(void)
247 {
248 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
249 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
250 puts("");
251 puts("Available commands:");
252 puts("");
253 puts("Tracing sessions:");
254 puts(" create " CONFIG_CMD_DESCR_CREATE);
255 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
256 puts(" load " CONFIG_CMD_DESCR_LOAD);
257 puts(" metadata " CONFIG_CMD_DESCR_METADATA);
258 puts(" save " CONFIG_CMD_DESCR_SAVE);
259 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
260 puts("");
261 puts("Channels:");
262 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
263 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
264 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
265 puts("");
266 puts("Event rules:");
267 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
268 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
269 puts("");
270 puts("Status:");
271 puts(" list " CONFIG_CMD_DESCR_LIST);
272 puts(" status " CONFIG_CMD_DESCR_STATUS);
273 puts("");
274 puts("Control:");
275 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
276 puts(" start " CONFIG_CMD_DESCR_START);
277 puts(" stop " CONFIG_CMD_DESCR_STOP);
278 puts("");
279 puts("Resource tracking:");
280 puts(" track " CONFIG_CMD_DESCR_TRACK);
281 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
282 puts("");
283 puts("Miscellaneous:");
284 puts(" calibrate " CONFIG_CMD_DESCR_CALIBRATE);
285 puts(" help " CONFIG_CMD_DESCR_HELP);
286 puts(" version " CONFIG_CMD_DESCR_VERSION);
287 puts(" view " CONFIG_CMD_DESCR_VIEW);
288 puts("");
289 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
290 puts("command COMMAND.");
291 puts("");
292 puts("See `man lttng` for more help with the lttng command.");
293 }
294
295 /*
296 * Parse command line arguments.
297 *
298 * Return 0 if OK, else -1
299 */
300 static int parse_args(int argc, char **argv)
301 {
302 int opt, ret;
303
304 if (lttng_is_setuid_setgid()) {
305 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
306 clean_exit(EXIT_FAILURE);
307 }
308
309 if (argc < 2) {
310 show_basic_help();
311 clean_exit(EXIT_FAILURE);
312 }
313
314 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
315 switch (opt) {
316 case 'V':
317 version(stdout);
318 ret = 0;
319 goto end;
320 case 'h':
321 ret = utils_show_man_page(1, "lttng");
322
323 if (ret) {
324 ERR("Cannot view man page lttng(1)");
325 perror("exec");
326 }
327 goto end;
328 case 'v':
329 /* There is only 3 possible level of verbosity. (-vvv) */
330 if (lttng_opt_verbose < 3) {
331 lttng_opt_verbose += 1;
332 }
333 break;
334 case 'q':
335 lttng_opt_quiet = 1;
336 break;
337 case 'm':
338 lttng_opt_mi = mi_output_type(optarg);
339 if (lttng_opt_mi < 0) {
340 ret = lttng_opt_mi;
341 goto error;
342 }
343 break;
344 case 'g':
345 lttng_set_tracing_group(optarg);
346 break;
347 case 'n':
348 opt_no_sessiond = 1;
349 break;
350 case OPT_SESSION_PATH:
351 free(opt_sessiond_path);
352 opt_sessiond_path = strdup(optarg);
353 if (!opt_sessiond_path) {
354 ret = -1;
355 goto error;
356 }
357 break;
358 case OPT_RELAYD_PATH:
359 free(opt_relayd_path);
360 opt_relayd_path = strdup(optarg);
361 if (!opt_relayd_path) {
362 ret = -1;
363 goto error;
364 }
365 break;
366 case OPT_DUMP_OPTIONS:
367 list_options(stdout);
368 ret = 0;
369 goto end;
370 case OPT_DUMP_COMMANDS:
371 list_commands(commands, stdout);
372 ret = 0;
373 goto end;
374 default:
375 ret = 1;
376 goto error;
377 }
378 }
379
380 /* If both options are specified, quiet wins */
381 if (lttng_opt_verbose && lttng_opt_quiet) {
382 lttng_opt_verbose = 0;
383 }
384
385 /* No leftovers, quit */
386 if ((argc - optind) == 0) {
387 ret = 1;
388 goto error;
389 }
390
391 /*
392 * Handle leftovers which is a first level command with the trailing
393 * options.
394 */
395 ret = handle_command(argc - optind, argv + optind);
396 switch (ret) {
397 case CMD_WARNING:
398 WARN("Some command(s) went wrong");
399 break;
400 case CMD_ERROR:
401 ERR("Command error");
402 break;
403 case CMD_UNDEFINED:
404 ERR("Undefined command or invalid arguments");
405 break;
406 case CMD_FATAL:
407 ERR("Fatal error");
408 break;
409 case CMD_UNSUPPORTED:
410 ERR("Unsupported command");
411 break;
412 case -1:
413 ret = 1;
414 break;
415 case 0:
416 break;
417 default:
418 if (ret < 0) {
419 ret = -ret;
420 }
421 break;
422 }
423
424 end:
425 error:
426 return ret;
427 }
428
429
430 /*
431 * main
432 */
433 int main(int argc, char *argv[])
434 {
435 int ret;
436
437 progname = argv[0] ? argv[0] : "lttng";
438
439 ret = set_signal_handler();
440 if (ret < 0) {
441 clean_exit(ret);
442 }
443
444 ret = parse_args(argc, argv);
445 if (ret != 0) {
446 clean_exit(ret);
447 }
448
449 return 0;
450 }
This page took 0.038706 seconds and 5 git commands to generate.