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