doc/man: add README.md
[lttng-tools.git] / src / bin / lttng / lttng.c
... / ...
CommitLineData
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 */
37static char *progname;
38int opt_no_sessiond;
39char *opt_sessiond_path;
40
41char *opt_relayd_path;
42
43enum {
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. */
51static 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 */
67static struct cmd_struct commands[] = {
68 { "list", cmd_list},
69 { "status", cmd_status},
70 { "create", cmd_create},
71 { "destroy", cmd_destroy},
72 { "start", cmd_start},
73 { "stop", cmd_stop},
74 { "enable-event", cmd_enable_events},
75 { "disable-event", cmd_disable_events},
76 { "enable-channel", cmd_enable_channels},
77 { "disable-channel", cmd_disable_channels},
78 { "add-context", cmd_add_context},
79 { "set-session", cmd_set_session},
80 { "version", cmd_version},
81 { "calibrate", cmd_calibrate},
82 { "view", cmd_view},
83 { "snapshot", cmd_snapshot},
84 { "save", cmd_save},
85 { "load", cmd_load},
86 { "track", cmd_track},
87 { "untrack", cmd_untrack},
88 { "metadata", cmd_metadata},
89 { "help", NULL},
90 { NULL, NULL} /* Array closure */
91};
92
93static void version(FILE *ofp)
94{
95 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
96 progname,
97 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
98}
99
100/*
101 * Find the MI output type enum from a string. This function is for the support
102 * of machine interface output.
103 */
104static int mi_output_type(const char *output_type)
105{
106 int ret = 0;
107
108 if (!strncasecmp("xml", output_type, 3)) {
109 ret = LTTNG_MI_XML;
110 } else {
111 /* Invalid output format */
112 ERR("MI output format not supported");
113 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
114 }
115
116 return ret;
117}
118
119/*
120 * list_options
121 *
122 * List options line by line. This is mostly for bash auto completion and to
123 * avoid difficult parsing.
124 */
125static void list_options(FILE *ofp)
126{
127 int i = 0;
128 struct option *option = NULL;
129
130 option = &long_options[i];
131 while (option->name != NULL) {
132 fprintf(ofp, "--%s\n", option->name);
133
134 if (isprint(option->val)) {
135 fprintf(ofp, "-%c\n", option->val);
136 }
137
138 i++;
139 option = &long_options[i];
140 }
141}
142
143/*
144 * clean_exit
145 */
146static void clean_exit(int code)
147{
148 DBG("Clean exit");
149 exit(code);
150}
151
152/*
153 * sighandler
154 *
155 * Signal handler for the daemon
156 */
157static void sighandler(int sig)
158{
159 switch (sig) {
160 case SIGTERM:
161 DBG("SIGTERM caught");
162 clean_exit(EXIT_FAILURE);
163 break;
164 default:
165 DBG("Unknown signal %d caught", sig);
166 break;
167 }
168
169 return;
170}
171
172/*
173 * set_signal_handler
174 *
175 * Setup signal handler for SIGCHLD and SIGTERM.
176 */
177static int set_signal_handler(void)
178{
179 int ret = 0;
180 struct sigaction sa;
181 sigset_t sigset;
182
183 if ((ret = sigemptyset(&sigset)) < 0) {
184 PERROR("sigemptyset");
185 goto end;
186 }
187
188 sa.sa_handler = sighandler;
189 sa.sa_mask = sigset;
190 sa.sa_flags = 0;
191
192 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
193 PERROR("sigaction");
194 goto end;
195 }
196
197end:
198 return ret;
199}
200
201/*
202 * handle_command
203 *
204 * Handle the full argv list of a first level command. Will find the command
205 * in the global commands array and call the function callback associated.
206 *
207 * If command not found, return -1
208 * else, return function command error code.
209 */
210static int handle_command(int argc, char **argv)
211{
212 int i = 0, ret;
213 struct cmd_struct *cmd;
214
215 if (*argv == NULL) {
216 ret = CMD_SUCCESS;
217 goto end;
218 }
219
220 /* Special case for help command which needs the commands array */
221 if (strcmp(argv[0], "help") == 0) {
222 ret = cmd_help(argc, (const char**) argv, commands);
223 goto end;
224 }
225
226 cmd = &commands[i];
227 while (cmd->name != NULL) {
228 /* Find command */
229 if (strcmp(argv[0], cmd->name) == 0) {
230 ret = cmd->func(argc, (const char**) argv);
231 goto end;
232 }
233 i++;
234 cmd = &commands[i];
235 }
236
237 /* Command not found */
238 ret = CMD_UNDEFINED;
239
240end:
241 return ret;
242}
243
244/*
245 * Parse command line arguments.
246 *
247 * Return 0 if OK, else -1
248 */
249static int parse_args(int argc, char **argv)
250{
251 int opt, ret;
252 char *user;
253
254 if (lttng_is_setuid_setgid()) {
255 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
256 clean_exit(EXIT_FAILURE);
257 }
258
259 if (argc < 2) {
260 clean_exit(EXIT_FAILURE);
261 }
262
263 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
264 switch (opt) {
265 case 'V':
266 version(stdout);
267 ret = 0;
268 goto end;
269 case 'h':
270 ret = utils_show_man_page(1, "lttng");
271
272 if (ret) {
273 ERR("Cannot view man page lttng(1)");
274 perror("exec");
275 }
276 goto end;
277 case 'v':
278 /* There is only 3 possible level of verbosity. (-vvv) */
279 if (lttng_opt_verbose < 3) {
280 lttng_opt_verbose += 1;
281 }
282 break;
283 case 'q':
284 lttng_opt_quiet = 1;
285 break;
286 case 'm':
287 lttng_opt_mi = mi_output_type(optarg);
288 if (lttng_opt_mi < 0) {
289 ret = lttng_opt_mi;
290 goto error;
291 }
292 break;
293 case 'g':
294 lttng_set_tracing_group(optarg);
295 break;
296 case 'n':
297 opt_no_sessiond = 1;
298 break;
299 case OPT_SESSION_PATH:
300 opt_sessiond_path = strdup(optarg);
301 if (!opt_sessiond_path) {
302 ret = -1;
303 goto error;
304 }
305 break;
306 case OPT_RELAYD_PATH:
307 opt_relayd_path = strdup(optarg);
308 if (!opt_relayd_path) {
309 ret = -1;
310 goto error;
311 }
312 break;
313 case OPT_DUMP_OPTIONS:
314 list_options(stdout);
315 ret = 0;
316 goto end;
317 case OPT_DUMP_COMMANDS:
318 list_commands(commands, stdout);
319 ret = 0;
320 goto end;
321 default:
322 ret = 1;
323 goto error;
324 }
325 }
326
327 /* If both options are specified, quiet wins */
328 if (lttng_opt_verbose && lttng_opt_quiet) {
329 lttng_opt_verbose = 0;
330 }
331
332 /* No leftovers, quit */
333 if ((argc - optind) == 0) {
334 ret = 1;
335 goto error;
336 }
337
338 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
339 user = getenv("USER");
340 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
341 strncmp("compudj", user, 7) == 0))) {
342 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
343 }
344 /* Thanks Mathieu */
345
346 /*
347 * Handle leftovers which is a first level command with the trailing
348 * options.
349 */
350 ret = handle_command(argc - optind, argv + optind);
351 switch (ret) {
352 case CMD_WARNING:
353 WARN("Some command(s) went wrong");
354 break;
355 case CMD_ERROR:
356 ERR("Command error");
357 break;
358 case CMD_UNDEFINED:
359 ERR("Undefined command or invalid arguments");
360 break;
361 case CMD_FATAL:
362 ERR("Fatal error");
363 break;
364 case CMD_UNSUPPORTED:
365 ERR("Unsupported command");
366 break;
367 case -1:
368 ret = 1;
369 break;
370 case 0:
371 break;
372 default:
373 if (ret < 0) {
374 ret = -ret;
375 }
376 break;
377 }
378
379end:
380error:
381 return ret;
382}
383
384
385/*
386 * main
387 */
388int main(int argc, char *argv[])
389{
390 int ret;
391
392 progname = argv[0] ? argv[0] : "lttng";
393
394 ret = set_signal_handler();
395 if (ret < 0) {
396 clean_exit(ret);
397 }
398
399 ret = parse_args(argc, argv);
400 if (ret != 0) {
401 clean_exit(ret);
402 }
403
404 return 0;
405}
This page took 0.025657 seconds and 5 git commands to generate.