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