doc/man: add README.md
[lttng-tools.git] / src / bin / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or modify
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
fac6795d
DG
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.
fac6795d
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
fac6795d 19#include <getopt.h>
f3ed775e 20#include <signal.h>
fac6795d
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
8db8d1dc
DG
24#include <sys/types.h>
25#include <sys/wait.h>
fac6795d 26#include <unistd.h>
730389d9 27#include <ctype.h>
fac6795d 28
5b97ec60 29#include <lttng/lttng.h>
db758600 30#include <common/error.h>
e8fa9fb0 31#include <common/compat/getenv.h>
f67353ba 32#include <common/utils.h>
fac6795d 33
c399183f 34#include "command.h"
fac6795d
DG
35
36/* Variables */
37static char *progname;
92360082
JG
38int opt_no_sessiond;
39char *opt_sessiond_path;
f3ed775e 40
8960e9cd
DG
41char *opt_relayd_path;
42
f3ed775e 43enum {
8960e9cd 44 OPT_RELAYD_PATH,
f3ed775e 45 OPT_SESSION_PATH,
865abf65
SM
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
f3ed775e
DG
48};
49
50/* Getopt options. No first level command. */
51static struct option long_options[] = {
70318c38 52 {"version", 0, NULL, 'V'},
f3ed775e
DG
53 {"help", 0, NULL, 'h'},
54 {"group", 1, NULL, 'g'},
55 {"verbose", 0, NULL, 'v'},
56 {"quiet", 0, NULL, 'q'},
c7e35b03 57 {"mi", 1, NULL, 'm'},
8490ae7b 58 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 59 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
8960e9cd 60 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
865abf65
SM
61 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
62 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
63 {NULL, 0, NULL, 0}
64};
65
66/* First level command */
67static struct cmd_struct commands[] = {
68 { "list", cmd_list},
54a0adbf 69 { "status", cmd_status},
f3ed775e
DG
70 { "create", cmd_create},
71 { "destroy", cmd_destroy},
f3ed775e
DG
72 { "start", cmd_start},
73 { "stop", cmd_stop},
74 { "enable-event", cmd_enable_events},
e953ef25 75 { "disable-event", cmd_disable_events},
d36b8583 76 { "enable-channel", cmd_enable_channels},
26cc6b4e 77 { "disable-channel", cmd_disable_channels},
d65106b1 78 { "add-context", cmd_add_context},
3087ef18 79 { "set-session", cmd_set_session},
eb9cb8b7 80 { "version", cmd_version},
d0254c7c 81 { "calibrate", cmd_calibrate},
0c95f5b2 82 { "view", cmd_view},
57f272ed 83 { "snapshot", cmd_snapshot},
c864d6d7 84 { "save", cmd_save},
8c42d845 85 { "load", cmd_load},
ccf10263
MD
86 { "track", cmd_track},
87 { "untrack", cmd_untrack},
93ec662e 88 { "metadata", cmd_metadata},
960afba4 89 { "help", NULL},
f3ed775e
DG
90 { NULL, NULL} /* Array closure */
91};
92
70318c38
SS
93static void version(FILE *ofp)
94{
4c6ac053
MD
95 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
96 progname,
97 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
70318c38
SS
98}
99
c7e35b03
JR
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
865abf65
SM
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
7442b2ba 143/*
f3ed775e 144 * clean_exit
96243366 145 */
f3ed775e 146static void clean_exit(int code)
96243366 147{
f3ed775e
DG
148 DBG("Clean exit");
149 exit(code);
894be886 150}
96243366 151
894be886 152/*
f3ed775e 153 * sighandler
2ef84c95 154 *
f3ed775e 155 * Signal handler for the daemon
2ef84c95 156 */
f3ed775e 157static void sighandler(int sig)
2ef84c95 158{
f3ed775e
DG
159 switch (sig) {
160 case SIGTERM:
99bab54f 161 DBG("SIGTERM caught");
f3ed775e
DG
162 clean_exit(EXIT_FAILURE);
163 break;
f3ed775e 164 default:
99bab54f 165 DBG("Unknown signal %d caught", sig);
f3ed775e 166 break;
2ef84c95
DG
167 }
168
f3ed775e 169 return;
2ef84c95
DG
170}
171
172/*
f3ed775e 173 * set_signal_handler
894be886 174 *
f3ed775e 175 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 176 */
f3ed775e 177static int set_signal_handler(void)
894be886 178{
f3ed775e
DG
179 int ret = 0;
180 struct sigaction sa;
181 sigset_t sigset;
33a2b854 182
f3ed775e 183 if ((ret = sigemptyset(&sigset)) < 0) {
6f04ed72 184 PERROR("sigemptyset");
33a2b854
DG
185 goto end;
186 }
187
f3ed775e
DG
188 sa.sa_handler = sighandler;
189 sa.sa_mask = sigset;
190 sa.sa_flags = 0;
96243366 191
f3ed775e 192 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
6f04ed72 193 PERROR("sigaction");
f3ed775e 194 goto end;
894be886
DG
195 }
196
96243366 197end:
57167058
DG
198 return ret;
199}
200
fac6795d 201/*
f3ed775e 202 * handle_command
fac6795d 203 *
f3ed775e
DG
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.
1c9f7941 206 *
f3ed775e
DG
207 * If command not found, return -1
208 * else, return function command error code.
1c9f7941 209 */
f3ed775e 210static int handle_command(int argc, char **argv)
1c9f7941 211{
f3ed775e
DG
212 int i = 0, ret;
213 struct cmd_struct *cmd;
1c9f7941 214
f3ed775e
DG
215 if (*argv == NULL) {
216 ret = CMD_SUCCESS;
47b74d63 217 goto end;
1c9f7941
DG
218 }
219
960afba4
PP
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
f3ed775e 226 cmd = &commands[i];
960afba4 227 while (cmd->name != NULL) {
f3ed775e
DG
228 /* Find command */
229 if (strcmp(argv[0], cmd->name) == 0) {
230 ret = cmd->func(argc, (const char**) argv);
f3ed775e 231 goto end;
894be886 232 }
f3ed775e
DG
233 i++;
234 cmd = &commands[i];
894be886
DG
235 }
236
f3ed775e 237 /* Command not found */
4ce78777 238 ret = CMD_UNDEFINED;
894be886
DG
239
240end:
f3ed775e 241 return ret;
8548ff30
DG
242}
243
5b8719f5 244/*
3183dbb0 245 * Parse command line arguments.
5b8719f5 246 *
3183dbb0 247 * Return 0 if OK, else -1
5b8719f5 248 */
f3ed775e 249static int parse_args(int argc, char **argv)
5b8719f5 250{
f3ed775e 251 int opt, ret;
883d80f9 252 char *user;
5b8719f5 253
e8fa9fb0
MD
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
f3ed775e 259 if (argc < 2) {
f3ed775e 260 clean_exit(EXIT_FAILURE);
5b8719f5
DG
261 }
262
c7e35b03 263 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
f3ed775e 264 switch (opt) {
70318c38
SS
265 case 'V':
266 version(stdout);
267 ret = 0;
268 goto end;
f3ed775e 269 case 'h':
f67353ba
PP
270 ret = utils_show_man_page(1, "lttng");
271
272 if (ret) {
273 ERR("Cannot view man page lttng(1)");
274 perror("exec");
275 }
3183dbb0 276 goto end;
f3ed775e 277 case 'v':
849e5b7b
DG
278 /* There is only 3 possible level of verbosity. (-vvv) */
279 if (lttng_opt_verbose < 3) {
280 lttng_opt_verbose += 1;
281 }
f3ed775e
DG
282 break;
283 case 'q':
97e19046 284 lttng_opt_quiet = 1;
f3ed775e 285 break;
c7e35b03
JR
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;
f3ed775e
DG
293 case 'g':
294 lttng_set_tracing_group(optarg);
295 break;
8490ae7b 296 case 'n':
f3ed775e
DG
297 opt_no_sessiond = 1;
298 break;
299 case OPT_SESSION_PATH:
300 opt_sessiond_path = strdup(optarg);
d7c5d39f
MD
301 if (!opt_sessiond_path) {
302 ret = -1;
303 goto error;
304 }
f3ed775e 305 break;
8960e9cd
DG
306 case OPT_RELAYD_PATH:
307 opt_relayd_path = strdup(optarg);
d7c5d39f
MD
308 if (!opt_relayd_path) {
309 ret = -1;
310 goto error;
311 }
8960e9cd 312 break;
865abf65
SM
313 case OPT_DUMP_OPTIONS:
314 list_options(stdout);
315 ret = 0;
3183dbb0 316 goto end;
865abf65 317 case OPT_DUMP_COMMANDS:
3c9bd23c 318 list_commands(commands, stdout);
865abf65 319 ret = 0;
3183dbb0 320 goto end;
f3ed775e 321 default:
ae856491 322 ret = 1;
f3ed775e
DG
323 goto error;
324 }
5b8719f5 325 }
fac6795d 326
f3ed775e 327 /* If both options are specified, quiet wins */
97e19046
DG
328 if (lttng_opt_verbose && lttng_opt_quiet) {
329 lttng_opt_verbose = 0;
5b8719f5
DG
330 }
331
f67353ba 332 /* No leftovers, quit */
f3ed775e 333 if ((argc - optind) == 0) {
8005f29a 334 ret = 1;
f3ed775e
DG
335 goto error;
336 }
7442b2ba 337
883d80f9
DG
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
92360082 346 /*
f3ed775e
DG
347 * Handle leftovers which is a first level command with the trailing
348 * options.
349 */
350 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
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:
f0f2dce9 359 ERR("Undefined command or invalid arguments");
ae856491
DG
360 break;
361 case CMD_FATAL:
362 ERR("Fatal error");
363 break;
4ce78777
DG
364 case CMD_UNSUPPORTED:
365 ERR("Unsupported command");
366 break;
ae856491 367 case -1:
ae856491
DG
368 ret = 1;
369 break;
370 case 0:
371 break;
372 default:
42224349
DG
373 if (ret < 0) {
374 ret = -ret;
375 }
ae856491 376 break;
96243366
DG
377 }
378
3183dbb0 379end:
f3ed775e 380error:
ae856491 381 return ret;
fac6795d
DG
382}
383
f3ed775e 384
fac6795d 385/*
5b8719f5 386 * main
fac6795d
DG
387 */
388int main(int argc, char *argv[])
389{
390 int ret;
391
392 progname = argv[0] ? argv[0] : "lttng";
393
5b8719f5
DG
394 ret = set_signal_handler();
395 if (ret < 0) {
87378cf5 396 clean_exit(ret);
5b8719f5
DG
397 }
398
f3ed775e 399 ret = parse_args(argc, argv);
ae856491
DG
400 if (ret != 0) {
401 clean_exit(ret);
1fff1faa 402 }
87378cf5 403
fac6795d
DG
404 return 0;
405}
This page took 0.125764 seconds and 5 git commands to generate.