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