8cf45410ed28957b0752d8ca05dfde78991114f0
[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(" metadata " CONFIG_CMD_DESCR_METADATA);
257 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
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(" help " CONFIG_CMD_DESCR_HELP);
285 puts(" version " CONFIG_CMD_DESCR_VERSION);
286 puts(" view " CONFIG_CMD_DESCR_VIEW);
287 puts("");
288 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
289 puts("command COMMAND.");
290 puts("");
291 puts("See `man lttng` for more help with the lttng command.");
292 }
293
294 /*
295 * Parse command line arguments.
296 *
297 * Return 0 if OK, else -1
298 */
299 static int parse_args(int argc, char **argv)
300 {
301 int opt, ret;
302
303 if (lttng_is_setuid_setgid()) {
304 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
305 clean_exit(EXIT_FAILURE);
306 }
307
308 if (argc < 2) {
309 show_basic_help();
310 clean_exit(EXIT_FAILURE);
311 }
312
313 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
314 switch (opt) {
315 case 'V':
316 version(stdout);
317 ret = 0;
318 goto end;
319 case 'h':
320 ret = utils_show_man_page(1, "lttng");
321
322 if (ret) {
323 ERR("Cannot view man page lttng(1)");
324 perror("exec");
325 }
326 goto end;
327 case 'v':
328 /* There is only 3 possible level of verbosity. (-vvv) */
329 if (lttng_opt_verbose < 3) {
330 lttng_opt_verbose += 1;
331 }
332 break;
333 case 'q':
334 lttng_opt_quiet = 1;
335 break;
336 case 'm':
337 lttng_opt_mi = mi_output_type(optarg);
338 if (lttng_opt_mi < 0) {
339 ret = lttng_opt_mi;
340 goto error;
341 }
342 break;
343 case 'g':
344 lttng_set_tracing_group(optarg);
345 break;
346 case 'n':
347 opt_no_sessiond = 1;
348 break;
349 case OPT_SESSION_PATH:
350 free(opt_sessiond_path);
351 opt_sessiond_path = strdup(optarg);
352 if (!opt_sessiond_path) {
353 ret = -1;
354 goto error;
355 }
356 break;
357 case OPT_RELAYD_PATH:
358 free(opt_relayd_path);
359 opt_relayd_path = strdup(optarg);
360 if (!opt_relayd_path) {
361 ret = -1;
362 goto error;
363 }
364 break;
365 case OPT_DUMP_OPTIONS:
366 list_options(stdout);
367 ret = 0;
368 goto end;
369 case OPT_DUMP_COMMANDS:
370 list_commands(commands, stdout);
371 ret = 0;
372 goto end;
373 default:
374 ret = 1;
375 goto error;
376 }
377 }
378
379 /* If both options are specified, quiet wins */
380 if (lttng_opt_verbose && lttng_opt_quiet) {
381 lttng_opt_verbose = 0;
382 }
383
384 /* No leftovers, quit */
385 if ((argc - optind) == 0) {
386 ret = 1;
387 goto error;
388 }
389
390 /*
391 * Handle leftovers which is a first level command with the trailing
392 * options.
393 */
394 ret = handle_command(argc - optind, argv + optind);
395 switch (ret) {
396 case CMD_WARNING:
397 WARN("Some command(s) went wrong");
398 break;
399 case CMD_ERROR:
400 ERR("Command error");
401 break;
402 case CMD_UNDEFINED:
403 ERR("Undefined command or invalid arguments");
404 break;
405 case CMD_FATAL:
406 ERR("Fatal error");
407 break;
408 case CMD_UNSUPPORTED:
409 ERR("Unsupported command");
410 break;
411 case -1:
412 ret = 1;
413 break;
414 case 0:
415 break;
416 default:
417 if (ret < 0) {
418 ret = -ret;
419 }
420 break;
421 }
422
423 end:
424 error:
425 return ret;
426 }
427
428
429 /*
430 * main
431 */
432 int main(int argc, char *argv[])
433 {
434 int ret;
435
436 progname = argv[0] ? argv[0] : "lttng";
437
438 ret = set_signal_handler();
439 if (ret < 0) {
440 clean_exit(ret);
441 }
442
443 ret = parse_args(argc, argv);
444 if (ret != 0) {
445 clean_exit(ret);
446 }
447
448 return 0;
449 }
This page took 0.055232 seconds and 4 git commands to generate.