lttng rotate command
[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 static const char *help_msg =
37 #ifdef LTTNG_EMBED_HELP
38 #include <lttng.1.h>
39 #else
40 NULL
41 #endif
42 ;
43
44 /* Variables */
45 static char *progname;
46 int opt_no_sessiond;
47 char *opt_sessiond_path;
48
49 char *opt_relayd_path;
50
51 enum {
52 OPT_RELAYD_PATH,
53 OPT_SESSION_PATH,
54 OPT_DUMP_OPTIONS,
55 OPT_DUMP_COMMANDS,
56 };
57
58 /* Getopt options. No first level command. */
59 static struct option long_options[] = {
60 {"version", 0, NULL, 'V'},
61 {"help", 0, NULL, 'h'},
62 {"group", 1, NULL, 'g'},
63 {"verbose", 0, NULL, 'v'},
64 {"quiet", 0, NULL, 'q'},
65 {"mi", 1, NULL, 'm'},
66 {"no-sessiond", 0, NULL, 'n'},
67 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
68 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
69 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
70 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
71 {NULL, 0, NULL, 0}
72 };
73
74 /* First level command */
75 static struct cmd_struct commands[] = {
76 { "add-context", cmd_add_context},
77 { "create", cmd_create},
78 { "destroy", cmd_destroy},
79 { "disable-channel", cmd_disable_channels},
80 { "disable-event", cmd_disable_events},
81 { "enable-channel", cmd_enable_channels},
82 { "enable-event", cmd_enable_events},
83 { "help", NULL},
84 { "list", cmd_list},
85 { "load", cmd_load},
86 { "metadata", cmd_metadata},
87 { "regenerate", cmd_regenerate},
88 { "rotate", cmd_rotate},
89 { "save", cmd_save},
90 { "set-session", cmd_set_session},
91 { "snapshot", cmd_snapshot},
92 { "start", cmd_start},
93 { "status", cmd_status},
94 { "stop", cmd_stop},
95 { "track", cmd_track},
96 { "untrack", cmd_untrack},
97 { "version", cmd_version},
98 { "view", cmd_view},
99 { NULL, NULL} /* Array closure */
100 };
101
102 static void version(FILE *ofp)
103 {
104 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
105 progname,
106 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
107 }
108
109 /*
110 * Find the MI output type enum from a string. This function is for the support
111 * of machine interface output.
112 */
113 static 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
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 */
134 static 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
152 /*
153 * clean_exit
154 */
155 static void clean_exit(int code)
156 {
157 DBG("Clean exit");
158 exit(code);
159 }
160
161 /*
162 * sighandler
163 *
164 * Signal handler for the daemon
165 */
166 static void sighandler(int sig)
167 {
168 switch (sig) {
169 case SIGTERM:
170 DBG("SIGTERM caught");
171 clean_exit(EXIT_FAILURE);
172 break;
173 default:
174 DBG("Unknown signal %d caught", sig);
175 break;
176 }
177
178 return;
179 }
180
181 /*
182 * set_signal_handler
183 *
184 * Setup signal handler for SIGCHLD and SIGTERM.
185 */
186 static int set_signal_handler(void)
187 {
188 int ret = 0;
189 struct sigaction sa;
190 sigset_t sigset;
191
192 if ((ret = sigemptyset(&sigset)) < 0) {
193 PERROR("sigemptyset");
194 goto end;
195 }
196
197 sa.sa_handler = sighandler;
198 sa.sa_mask = sigset;
199 sa.sa_flags = 0;
200
201 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
202 PERROR("sigaction");
203 goto end;
204 }
205
206 end:
207 return ret;
208 }
209
210 /*
211 * handle_command
212 *
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.
215 *
216 * If command not found, return -1
217 * else, return function command error code.
218 */
219 static int handle_command(int argc, char **argv)
220 {
221 int i = 0, ret;
222 struct cmd_struct *cmd;
223
224 if (*argv == NULL) {
225 ret = CMD_SUCCESS;
226 goto end;
227 }
228
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
235 cmd = &commands[i];
236 while (cmd->name != NULL) {
237 /* Find command */
238 if (strcmp(argv[0], cmd->name) == 0) {
239 ret = cmd->func(argc, (const char**) argv);
240 goto end;
241 }
242 i++;
243 cmd = &commands[i];
244 }
245
246 /* Command not found */
247 ret = CMD_UNDEFINED;
248
249 end:
250 return ret;
251 }
252
253 static 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);
264 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
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:");
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
301 /*
302 * Parse command line arguments.
303 *
304 * Return 0 if OK, else -1
305 */
306 static int parse_args(int argc, char **argv)
307 {
308 int opt, ret;
309
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
315 if (argc < 2) {
316 show_basic_help();
317 clean_exit(EXIT_FAILURE);
318 }
319
320 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
321 switch (opt) {
322 case 'V':
323 version(stdout);
324 ret = 0;
325 goto end;
326 case 'h':
327 ret = utils_show_help(1, "lttng", help_msg);
328 if (ret) {
329 ERR("Cannot show --help for `lttng`");
330 perror("exec");
331 }
332 goto end;
333 case 'v':
334 /* There is only 3 possible level of verbosity. (-vvv) */
335 if (lttng_opt_verbose < 3) {
336 lttng_opt_verbose += 1;
337 }
338 break;
339 case 'q':
340 lttng_opt_quiet = 1;
341 break;
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;
349 case 'g':
350 lttng_set_tracing_group(optarg);
351 break;
352 case 'n':
353 opt_no_sessiond = 1;
354 break;
355 case OPT_SESSION_PATH:
356 free(opt_sessiond_path);
357 opt_sessiond_path = strdup(optarg);
358 if (!opt_sessiond_path) {
359 ret = -1;
360 goto error;
361 }
362 break;
363 case OPT_RELAYD_PATH:
364 free(opt_relayd_path);
365 opt_relayd_path = strdup(optarg);
366 if (!opt_relayd_path) {
367 ret = -1;
368 goto error;
369 }
370 break;
371 case OPT_DUMP_OPTIONS:
372 list_options(stdout);
373 ret = 0;
374 goto end;
375 case OPT_DUMP_COMMANDS:
376 list_commands(commands, stdout);
377 ret = 0;
378 goto end;
379 default:
380 ret = 1;
381 goto error;
382 }
383 }
384
385 /* If both options are specified, quiet wins */
386 if (lttng_opt_verbose && lttng_opt_quiet) {
387 lttng_opt_verbose = 0;
388 }
389
390 /* No leftovers, quit */
391 if ((argc - optind) == 0) {
392 ret = 1;
393 goto error;
394 }
395
396 /*
397 * Handle leftovers which is a first level command with the trailing
398 * options.
399 */
400 ret = handle_command(argc - optind, argv + optind);
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:
409 ERR("Undefined command or invalid arguments");
410 break;
411 case CMD_FATAL:
412 ERR("Fatal error");
413 break;
414 case CMD_UNSUPPORTED:
415 ERR("Unsupported command");
416 break;
417 case -1:
418 ret = 1;
419 break;
420 case 0:
421 break;
422 default:
423 if (ret < 0) {
424 ret = -ret;
425 }
426 break;
427 }
428
429 end:
430 error:
431 return ret;
432 }
433
434
435 /*
436 * main
437 */
438 int main(int argc, char *argv[])
439 {
440 int ret;
441
442 progname = argv[0] ? argv[0] : "lttng";
443
444 ret = set_signal_handler();
445 if (ret < 0) {
446 clean_exit(ret);
447 }
448
449 ret = parse_args(argc, argv);
450 if (ret != 0) {
451 clean_exit(ret);
452 }
453
454 return 0;
455 }
This page took 0.039102 seconds and 5 git commands to generate.