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