Commit | Line | Data |
---|---|---|
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 */ | |
37 | static char *progname; | |
92360082 JG |
38 | int opt_no_sessiond; |
39 | char *opt_sessiond_path; | |
f3ed775e | 40 | |
8960e9cd DG |
41 | char *opt_relayd_path; |
42 | ||
f3ed775e | 43 | enum { |
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. */ | |
51 | static 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 */ | |
67 | static 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}, |
f3ed775e DG |
89 | { NULL, NULL} /* Array closure */ |
90 | }; | |
91 | ||
70318c38 SS |
92 | static void version(FILE *ofp) |
93 | { | |
4c6ac053 MD |
94 | fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n", |
95 | progname, | |
96 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); | |
70318c38 SS |
97 | } |
98 | ||
c7e35b03 JR |
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 | ||
865abf65 SM |
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 | ||
7442b2ba | 142 | /* |
f3ed775e | 143 | * clean_exit |
96243366 | 144 | */ |
f3ed775e | 145 | static void clean_exit(int code) |
96243366 | 146 | { |
f3ed775e DG |
147 | DBG("Clean exit"); |
148 | exit(code); | |
894be886 | 149 | } |
96243366 | 150 | |
894be886 | 151 | /* |
f3ed775e | 152 | * sighandler |
2ef84c95 | 153 | * |
f3ed775e | 154 | * Signal handler for the daemon |
2ef84c95 | 155 | */ |
f3ed775e | 156 | static void sighandler(int sig) |
2ef84c95 | 157 | { |
f3ed775e DG |
158 | switch (sig) { |
159 | case SIGTERM: | |
99bab54f | 160 | DBG("SIGTERM caught"); |
f3ed775e DG |
161 | clean_exit(EXIT_FAILURE); |
162 | break; | |
f3ed775e | 163 | default: |
99bab54f | 164 | DBG("Unknown signal %d caught", sig); |
f3ed775e | 165 | break; |
2ef84c95 DG |
166 | } |
167 | ||
f3ed775e | 168 | return; |
2ef84c95 DG |
169 | } |
170 | ||
171 | /* | |
f3ed775e | 172 | * set_signal_handler |
894be886 | 173 | * |
f3ed775e | 174 | * Setup signal handler for SIGCHLD and SIGTERM. |
894be886 | 175 | */ |
f3ed775e | 176 | static int set_signal_handler(void) |
894be886 | 177 | { |
f3ed775e DG |
178 | int ret = 0; |
179 | struct sigaction sa; | |
180 | sigset_t sigset; | |
33a2b854 | 181 | |
f3ed775e | 182 | if ((ret = sigemptyset(&sigset)) < 0) { |
6f04ed72 | 183 | PERROR("sigemptyset"); |
33a2b854 DG |
184 | goto end; |
185 | } | |
186 | ||
f3ed775e DG |
187 | sa.sa_handler = sighandler; |
188 | sa.sa_mask = sigset; | |
189 | sa.sa_flags = 0; | |
96243366 | 190 | |
f3ed775e | 191 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
6f04ed72 | 192 | PERROR("sigaction"); |
f3ed775e | 193 | goto end; |
894be886 DG |
194 | } |
195 | ||
96243366 | 196 | end: |
57167058 DG |
197 | return ret; |
198 | } | |
199 | ||
fac6795d | 200 | /* |
f3ed775e | 201 | * handle_command |
fac6795d | 202 | * |
f3ed775e DG |
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. | |
1c9f7941 | 205 | * |
f3ed775e DG |
206 | * If command not found, return -1 |
207 | * else, return function command error code. | |
1c9f7941 | 208 | */ |
f3ed775e | 209 | static int handle_command(int argc, char **argv) |
1c9f7941 | 210 | { |
f3ed775e DG |
211 | int i = 0, ret; |
212 | struct cmd_struct *cmd; | |
1c9f7941 | 213 | |
f3ed775e DG |
214 | if (*argv == NULL) { |
215 | ret = CMD_SUCCESS; | |
47b74d63 | 216 | goto end; |
1c9f7941 DG |
217 | } |
218 | ||
f3ed775e DG |
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); | |
f3ed775e | 224 | goto end; |
894be886 | 225 | } |
f3ed775e DG |
226 | i++; |
227 | cmd = &commands[i]; | |
894be886 DG |
228 | } |
229 | ||
f3ed775e | 230 | /* Command not found */ |
4ce78777 | 231 | ret = CMD_UNDEFINED; |
894be886 DG |
232 | |
233 | end: | |
f3ed775e | 234 | return ret; |
8548ff30 DG |
235 | } |
236 | ||
5b8719f5 | 237 | /* |
3183dbb0 | 238 | * Parse command line arguments. |
5b8719f5 | 239 | * |
3183dbb0 | 240 | * Return 0 if OK, else -1 |
5b8719f5 | 241 | */ |
f3ed775e | 242 | static int parse_args(int argc, char **argv) |
5b8719f5 | 243 | { |
f3ed775e | 244 | int opt, ret; |
883d80f9 | 245 | char *user; |
5b8719f5 | 246 | |
e8fa9fb0 MD |
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 | ||
f3ed775e | 252 | if (argc < 2) { |
f3ed775e | 253 | clean_exit(EXIT_FAILURE); |
5b8719f5 DG |
254 | } |
255 | ||
c7e35b03 | 256 | while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) { |
f3ed775e | 257 | switch (opt) { |
70318c38 SS |
258 | case 'V': |
259 | version(stdout); | |
260 | ret = 0; | |
261 | goto end; | |
f3ed775e | 262 | case 'h': |
f67353ba PP |
263 | ret = utils_show_man_page(1, "lttng"); |
264 | ||
265 | if (ret) { | |
266 | ERR("Cannot view man page lttng(1)"); | |
267 | perror("exec"); | |
268 | } | |
3183dbb0 | 269 | goto end; |
f3ed775e | 270 | case 'v': |
849e5b7b DG |
271 | /* There is only 3 possible level of verbosity. (-vvv) */ |
272 | if (lttng_opt_verbose < 3) { | |
273 | lttng_opt_verbose += 1; | |
274 | } | |
f3ed775e DG |
275 | break; |
276 | case 'q': | |
97e19046 | 277 | lttng_opt_quiet = 1; |
f3ed775e | 278 | break; |
c7e35b03 JR |
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; | |
f3ed775e DG |
286 | case 'g': |
287 | lttng_set_tracing_group(optarg); | |
288 | break; | |
8490ae7b | 289 | case 'n': |
f3ed775e DG |
290 | opt_no_sessiond = 1; |
291 | break; | |
292 | case OPT_SESSION_PATH: | |
293 | opt_sessiond_path = strdup(optarg); | |
d7c5d39f MD |
294 | if (!opt_sessiond_path) { |
295 | ret = -1; | |
296 | goto error; | |
297 | } | |
f3ed775e | 298 | break; |
8960e9cd DG |
299 | case OPT_RELAYD_PATH: |
300 | opt_relayd_path = strdup(optarg); | |
d7c5d39f MD |
301 | if (!opt_relayd_path) { |
302 | ret = -1; | |
303 | goto error; | |
304 | } | |
8960e9cd | 305 | break; |
865abf65 SM |
306 | case OPT_DUMP_OPTIONS: |
307 | list_options(stdout); | |
308 | ret = 0; | |
3183dbb0 | 309 | goto end; |
865abf65 | 310 | case OPT_DUMP_COMMANDS: |
3c9bd23c | 311 | list_commands(commands, stdout); |
865abf65 | 312 | ret = 0; |
3183dbb0 | 313 | goto end; |
f3ed775e | 314 | default: |
ae856491 | 315 | ret = 1; |
f3ed775e DG |
316 | goto error; |
317 | } | |
5b8719f5 | 318 | } |
fac6795d | 319 | |
f3ed775e | 320 | /* If both options are specified, quiet wins */ |
97e19046 DG |
321 | if (lttng_opt_verbose && lttng_opt_quiet) { |
322 | lttng_opt_verbose = 0; | |
5b8719f5 DG |
323 | } |
324 | ||
f67353ba | 325 | /* No leftovers, quit */ |
f3ed775e | 326 | if ((argc - optind) == 0) { |
8005f29a | 327 | ret = 1; |
f3ed775e DG |
328 | goto error; |
329 | } | |
7442b2ba | 330 | |
883d80f9 DG |
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 | ||
92360082 | 339 | /* |
f3ed775e DG |
340 | * Handle leftovers which is a first level command with the trailing |
341 | * options. | |
342 | */ | |
343 | ret = handle_command(argc - optind, argv + optind); | |
ae856491 DG |
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: | |
f0f2dce9 | 352 | ERR("Undefined command or invalid arguments"); |
ae856491 DG |
353 | break; |
354 | case CMD_FATAL: | |
355 | ERR("Fatal error"); | |
356 | break; | |
4ce78777 DG |
357 | case CMD_UNSUPPORTED: |
358 | ERR("Unsupported command"); | |
359 | break; | |
ae856491 | 360 | case -1: |
ae856491 DG |
361 | ret = 1; |
362 | break; | |
363 | case 0: | |
364 | break; | |
365 | default: | |
42224349 DG |
366 | if (ret < 0) { |
367 | ret = -ret; | |
368 | } | |
ae856491 | 369 | break; |
96243366 DG |
370 | } |
371 | ||
3183dbb0 | 372 | end: |
f3ed775e | 373 | error: |
ae856491 | 374 | return ret; |
fac6795d DG |
375 | } |
376 | ||
f3ed775e | 377 | |
fac6795d | 378 | /* |
5b8719f5 | 379 | * main |
fac6795d DG |
380 | */ |
381 | int main(int argc, char *argv[]) | |
382 | { | |
383 | int ret; | |
384 | ||
385 | progname = argv[0] ? argv[0] : "lttng"; | |
386 | ||
5b8719f5 DG |
387 | ret = set_signal_handler(); |
388 | if (ret < 0) { | |
87378cf5 | 389 | clean_exit(ret); |
5b8719f5 DG |
390 | } |
391 | ||
f3ed775e | 392 | ret = parse_args(argc, argv); |
ae856491 DG |
393 | if (ret != 0) { |
394 | clean_exit(ret); | |
1fff1faa | 395 | } |
87378cf5 | 396 | |
fac6795d DG |
397 | return 0; |
398 | } |