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 | |
5 | * it under the terms of the GNU General Public License as published by | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
fac6795d DG |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
fac6795d | 20 | #include <getopt.h> |
f3ed775e | 21 | #include <signal.h> |
fac6795d DG |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
fac6795d DG |
25 | #include <unistd.h> |
26 | ||
5b97ec60 | 27 | #include <lttng/lttng.h> |
fac6795d | 28 | |
f3ed775e | 29 | #include "cmd.h" |
beb8c75a | 30 | #include "conf.h" |
fac6795d DG |
31 | #include "lttngerr.h" |
32 | ||
33 | /* Variables */ | |
34 | static char *progname; | |
fac6795d | 35 | |
f3ed775e DG |
36 | int opt_quiet; |
37 | int opt_verbose; | |
38 | static int opt_no_sessiond; | |
39 | static char *opt_sessiond_path; | |
40 | ||
41 | enum { | |
f3ed775e | 42 | OPT_SESSION_PATH, |
865abf65 SM |
43 | OPT_DUMP_OPTIONS, |
44 | OPT_DUMP_COMMANDS, | |
f3ed775e DG |
45 | }; |
46 | ||
47 | /* Getopt options. No first level command. */ | |
48 | static struct option long_options[] = { | |
49 | {"help", 0, NULL, 'h'}, | |
50 | {"group", 1, NULL, 'g'}, | |
51 | {"verbose", 0, NULL, 'v'}, | |
52 | {"quiet", 0, NULL, 'q'}, | |
8490ae7b | 53 | {"no-sessiond", 0, NULL, 'n'}, |
f3ed775e | 54 | {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, |
865abf65 SM |
55 | {"list-options", 0, NULL, OPT_DUMP_OPTIONS}, |
56 | {"list-commands", 0, NULL, OPT_DUMP_COMMANDS}, | |
f3ed775e DG |
57 | {NULL, 0, NULL, 0} |
58 | }; | |
59 | ||
60 | /* First level command */ | |
61 | static struct cmd_struct commands[] = { | |
62 | { "list", cmd_list}, | |
63 | { "create", cmd_create}, | |
64 | { "destroy", cmd_destroy}, | |
f3ed775e DG |
65 | { "start", cmd_start}, |
66 | { "stop", cmd_stop}, | |
67 | { "enable-event", cmd_enable_events}, | |
e953ef25 | 68 | { "disable-event", cmd_disable_events}, |
d36b8583 | 69 | { "enable-channel", cmd_enable_channels}, |
26cc6b4e | 70 | { "disable-channel", cmd_disable_channels}, |
d65106b1 | 71 | { "add-context", cmd_add_context}, |
3087ef18 | 72 | { "set-session", cmd_set_session}, |
eb9cb8b7 | 73 | { "version", cmd_version}, |
f3ed775e DG |
74 | { NULL, NULL} /* Array closure */ |
75 | }; | |
76 | ||
77 | static void usage(FILE *ofp) | |
8c0faa1d | 78 | { |
f3ed775e DG |
79 | fprintf(ofp, "LTTng Trace Control " VERSION"\n\n"); |
80 | fprintf(ofp, "usage: lttng [options] <command>\n"); | |
81 | fprintf(ofp, "\n"); | |
82 | fprintf(ofp, "Options:\n"); | |
83 | fprintf(ofp, " -h, --help Show this help\n"); | |
84 | fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n"); | |
85 | fprintf(ofp, " -v, --verbose Verbose mode\n"); | |
86 | fprintf(ofp, " -q, --quiet Quiet mode\n"); | |
8490ae7b | 87 | fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n"); |
f3ed775e | 88 | fprintf(ofp, " --sessiond-path Session daemon full path\n"); |
865abf65 SM |
89 | fprintf(ofp, " --list-options Simple listing of lttng options\n"); |
90 | fprintf(ofp, " --list-commands Simple listing of lttng commands\n"); | |
f3ed775e DG |
91 | fprintf(ofp, "\n"); |
92 | fprintf(ofp, "Commands:\n"); | |
d65106b1 | 93 | fprintf(ofp, " add-context Add context to event or/and channel\n"); |
f3ed775e DG |
94 | fprintf(ofp, " create Create tracing session\n"); |
95 | fprintf(ofp, " destroy Teardown tracing session\n"); | |
d36b8583 | 96 | fprintf(ofp, " enable-channel Enable tracing channel\n"); |
26cc6b4e DG |
97 | fprintf(ofp, " enable-event Enable tracing event\n"); |
98 | fprintf(ofp, " disable-channel Disable tracing channel\n"); | |
f3ed775e DG |
99 | fprintf(ofp, " disable-event Disable tracing event\n"); |
100 | fprintf(ofp, " list List possible tracing options\n"); | |
3087ef18 | 101 | fprintf(ofp, " set-session Set current session name\n"); |
f3ed775e DG |
102 | fprintf(ofp, " start Start tracing\n"); |
103 | fprintf(ofp, " stop Stop tracing\n"); | |
104 | fprintf(ofp, " version Show version information\n"); | |
105 | fprintf(ofp, "\n"); | |
106 | fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n"); | |
107 | fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n"); | |
8c0faa1d DG |
108 | } |
109 | ||
865abf65 SM |
110 | /* |
111 | * list_options | |
112 | * | |
113 | * List options line by line. This is mostly for bash auto completion and to | |
114 | * avoid difficult parsing. | |
115 | */ | |
116 | static void list_options(FILE *ofp) | |
117 | { | |
118 | int i = 0; | |
119 | struct option *option = NULL; | |
120 | ||
121 | option = &long_options[i]; | |
122 | while (option->name != NULL) { | |
123 | fprintf(ofp, "--%s\n", option->name); | |
124 | ||
125 | if (isprint(option->val)) { | |
126 | fprintf(ofp, "-%c\n", option->val); | |
127 | } | |
128 | ||
129 | i++; | |
130 | option = &long_options[i]; | |
131 | } | |
132 | } | |
133 | ||
134 | /* | |
135 | * list_commands | |
136 | * | |
137 | * List commands line by line. This is mostly for bash auto completion and to | |
138 | * avoid difficult parsing. | |
139 | */ | |
140 | static void list_commands(FILE *ofp) | |
141 | { | |
142 | int i = 0; | |
143 | struct cmd_struct *cmd = NULL; | |
144 | ||
145 | cmd = &commands[i]; | |
146 | while (cmd->name != NULL) { | |
147 | fprintf(ofp, "%s\n", cmd->name); | |
148 | i++; | |
149 | cmd = &commands[i]; | |
150 | } | |
151 | } | |
152 | ||
7442b2ba | 153 | /* |
f3ed775e | 154 | * clean_exit |
96243366 | 155 | */ |
f3ed775e | 156 | static void clean_exit(int code) |
96243366 | 157 | { |
f3ed775e DG |
158 | DBG("Clean exit"); |
159 | exit(code); | |
894be886 | 160 | } |
96243366 | 161 | |
894be886 | 162 | /* |
f3ed775e | 163 | * sighandler |
2ef84c95 | 164 | * |
f3ed775e | 165 | * Signal handler for the daemon |
2ef84c95 | 166 | */ |
f3ed775e | 167 | static void sighandler(int sig) |
2ef84c95 | 168 | { |
f3ed775e DG |
169 | switch (sig) { |
170 | case SIGTERM: | |
171 | DBG("SIGTERM catched"); | |
172 | clean_exit(EXIT_FAILURE); | |
173 | break; | |
174 | case SIGCHLD: | |
175 | /* Notify is done */ | |
176 | DBG("SIGCHLD catched"); | |
177 | break; | |
178 | default: | |
179 | DBG("Unknown signal %d catched", sig); | |
180 | break; | |
2ef84c95 DG |
181 | } |
182 | ||
f3ed775e | 183 | return; |
2ef84c95 DG |
184 | } |
185 | ||
186 | /* | |
f3ed775e | 187 | * set_signal_handler |
894be886 | 188 | * |
f3ed775e | 189 | * Setup signal handler for SIGCHLD and SIGTERM. |
894be886 | 190 | */ |
f3ed775e | 191 | static int set_signal_handler(void) |
894be886 | 192 | { |
f3ed775e DG |
193 | int ret = 0; |
194 | struct sigaction sa; | |
195 | sigset_t sigset; | |
33a2b854 | 196 | |
f3ed775e DG |
197 | if ((ret = sigemptyset(&sigset)) < 0) { |
198 | perror("sigemptyset"); | |
33a2b854 DG |
199 | goto end; |
200 | } | |
201 | ||
f3ed775e DG |
202 | sa.sa_handler = sighandler; |
203 | sa.sa_mask = sigset; | |
204 | sa.sa_flags = 0; | |
205 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { | |
206 | perror("sigaction"); | |
96243366 DG |
207 | goto end; |
208 | } | |
209 | ||
f3ed775e DG |
210 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
211 | perror("sigaction"); | |
212 | goto end; | |
894be886 DG |
213 | } |
214 | ||
96243366 | 215 | end: |
57167058 DG |
216 | return ret; |
217 | } | |
218 | ||
fac6795d | 219 | /* |
f3ed775e | 220 | * handle_command |
fac6795d | 221 | * |
f3ed775e DG |
222 | * Handle the full argv list of a first level command. Will find the command |
223 | * in the global commands array and call the function callback associated. | |
1c9f7941 | 224 | * |
f3ed775e DG |
225 | * If command not found, return -1 |
226 | * else, return function command error code. | |
1c9f7941 | 227 | */ |
f3ed775e | 228 | static int handle_command(int argc, char **argv) |
1c9f7941 | 229 | { |
f3ed775e DG |
230 | int i = 0, ret; |
231 | struct cmd_struct *cmd; | |
1c9f7941 | 232 | |
f3ed775e DG |
233 | if (*argv == NULL) { |
234 | ret = CMD_SUCCESS; | |
47b74d63 | 235 | goto end; |
1c9f7941 DG |
236 | } |
237 | ||
f3ed775e DG |
238 | cmd = &commands[i]; |
239 | while (cmd->func != NULL) { | |
240 | /* Find command */ | |
241 | if (strcmp(argv[0], cmd->name) == 0) { | |
242 | ret = cmd->func(argc, (const char**) argv); | |
243 | switch (ret) { | |
244 | case CMD_ERROR: | |
245 | ERR("Command error"); | |
246 | break; | |
247 | case CMD_NOT_IMPLEMENTED: | |
248 | ERR("Options not implemented"); | |
249 | break; | |
250 | case CMD_UNDEFINED: | |
251 | ERR("Undefined command"); | |
252 | break; | |
253 | case CMD_FATAL: | |
254 | ERR("Fatal error"); | |
255 | break; | |
256 | } | |
257 | goto end; | |
894be886 | 258 | } |
f3ed775e DG |
259 | i++; |
260 | cmd = &commands[i]; | |
894be886 DG |
261 | } |
262 | ||
f3ed775e DG |
263 | /* Command not found */ |
264 | ret = -1; | |
894be886 DG |
265 | |
266 | end: | |
f3ed775e | 267 | return ret; |
8548ff30 DG |
268 | } |
269 | ||
5b8719f5 DG |
270 | /* |
271 | * spawn_sessiond | |
272 | * | |
273 | * Spawn a session daemon by forking and execv. | |
274 | */ | |
275 | static int spawn_sessiond(char *pathname) | |
276 | { | |
277 | int ret = 0; | |
278 | pid_t pid; | |
279 | ||
f3ed775e | 280 | MSG("Spawning a session daemon"); |
5b8719f5 DG |
281 | pid = fork(); |
282 | if (pid == 0) { | |
5e16da05 MD |
283 | /* |
284 | * Spawn session daemon and tell | |
5b8719f5 DG |
285 | * it to signal us when ready. |
286 | */ | |
5e16da05 MD |
287 | execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL); |
288 | /* execlp only returns if error happened */ | |
289 | if (errno == ENOENT) { | |
290 | ERR("No session daemon found. Use --sessiond-path."); | |
291 | } else { | |
292 | perror("execlp"); | |
5b8719f5 | 293 | } |
5e16da05 MD |
294 | kill(getppid(), SIGTERM); /* unpause parent */ |
295 | exit(EXIT_FAILURE); | |
5b8719f5 DG |
296 | } else if (pid > 0) { |
297 | /* Wait for ltt-sessiond to start */ | |
298 | pause(); | |
299 | goto end; | |
300 | } else { | |
301 | perror("fork"); | |
302 | ret = -1; | |
303 | goto end; | |
304 | } | |
305 | ||
306 | end: | |
307 | return ret; | |
308 | } | |
309 | ||
fac6795d | 310 | /* |
f3ed775e | 311 | * check_sessiond |
fac6795d DG |
312 | * |
313 | * Check if the session daemon is available using | |
5b8719f5 DG |
314 | * the liblttngctl API for the check. If not, try to |
315 | * spawn a daemon. | |
fac6795d | 316 | */ |
f3ed775e | 317 | static int check_sessiond(void) |
fac6795d DG |
318 | { |
319 | int ret; | |
5e16da05 | 320 | char *pathname = NULL, *alloc_pathname = NULL; |
fac6795d | 321 | |
947308c4 DG |
322 | ret = lttng_session_daemon_alive(); |
323 | if (ret == 0) { /* not alive */ | |
5b8719f5 DG |
324 | /* Try command line option path */ |
325 | if (opt_sessiond_path != NULL) { | |
326 | ret = access(opt_sessiond_path, F_OK | X_OK); | |
327 | if (ret < 0) { | |
328 | ERR("No such file: %s", opt_sessiond_path); | |
329 | goto end; | |
330 | } | |
331 | pathname = opt_sessiond_path; | |
332 | } else { | |
333 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
e8f07c63 | 334 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
5b8719f5 DG |
335 | } |
336 | ||
337 | /* Let's rock and roll */ | |
338 | if (pathname == NULL) { | |
36c0262a | 339 | ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/ltt-sessiond"); |
5b8719f5 | 340 | if (ret < 0) { |
3f5fa9ed | 341 | perror("asprintf spawn sessiond"); |
5b8719f5 DG |
342 | goto end; |
343 | } | |
5e16da05 | 344 | pathname = alloc_pathname; |
5b8719f5 DG |
345 | } |
346 | ||
347 | ret = spawn_sessiond(pathname); | |
5e16da05 | 348 | free(alloc_pathname); |
5b8719f5 DG |
349 | if (ret < 0) { |
350 | ERR("Problem occurs when starting %s", pathname); | |
351 | goto end; | |
352 | } | |
fac6795d DG |
353 | } |
354 | ||
5b8719f5 | 355 | end: |
fac6795d DG |
356 | return ret; |
357 | } | |
358 | ||
5b8719f5 | 359 | /* |
f3ed775e | 360 | * parse_args |
5b8719f5 | 361 | * |
f3ed775e DG |
362 | * Parse command line arguments. |
363 | * Return 0 if OK, else -1 | |
5b8719f5 | 364 | */ |
f3ed775e | 365 | static int parse_args(int argc, char **argv) |
5b8719f5 | 366 | { |
f3ed775e | 367 | int opt, ret; |
5b8719f5 | 368 | |
f3ed775e DG |
369 | if (argc < 2) { |
370 | usage(stderr); | |
371 | clean_exit(EXIT_FAILURE); | |
5b8719f5 DG |
372 | } |
373 | ||
8490ae7b | 374 | while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) { |
f3ed775e DG |
375 | switch (opt) { |
376 | case 'h': | |
377 | usage(stderr); | |
378 | goto error; | |
379 | case 'v': | |
380 | opt_verbose = 1; | |
381 | break; | |
382 | case 'q': | |
383 | opt_quiet = 1; | |
384 | break; | |
385 | case 'g': | |
386 | lttng_set_tracing_group(optarg); | |
387 | break; | |
8490ae7b | 388 | case 'n': |
f3ed775e DG |
389 | opt_no_sessiond = 1; |
390 | break; | |
391 | case OPT_SESSION_PATH: | |
392 | opt_sessiond_path = strdup(optarg); | |
393 | break; | |
865abf65 SM |
394 | case OPT_DUMP_OPTIONS: |
395 | list_options(stdout); | |
396 | ret = 0; | |
397 | goto error; | |
398 | case OPT_DUMP_COMMANDS: | |
399 | list_commands(stdout); | |
400 | ret = 0; | |
401 | goto error; | |
f3ed775e DG |
402 | default: |
403 | usage(stderr); | |
404 | goto error; | |
405 | } | |
5b8719f5 | 406 | } |
fac6795d | 407 | |
f3ed775e DG |
408 | /* If both options are specified, quiet wins */ |
409 | if (opt_verbose && opt_quiet) { | |
410 | opt_verbose = 0; | |
5b8719f5 DG |
411 | } |
412 | ||
f3ed775e DG |
413 | /* Spawn session daemon if needed */ |
414 | if (opt_no_sessiond == 0 && (check_sessiond() < 0)) { | |
415 | goto error; | |
5b8719f5 DG |
416 | } |
417 | ||
f3ed775e DG |
418 | /* No leftovers, print usage and quit */ |
419 | if ((argc - optind) == 0) { | |
420 | usage(stderr); | |
421 | goto error; | |
422 | } | |
7442b2ba | 423 | |
f3ed775e DG |
424 | /* |
425 | * Handle leftovers which is a first level command with the trailing | |
426 | * options. | |
427 | */ | |
428 | ret = handle_command(argc - optind, argv + optind); | |
429 | if (ret < 0) { | |
430 | if (ret == -1) { | |
431 | usage(stderr); | |
f3ed775e DG |
432 | } else { |
433 | ERR("%s", lttng_get_readable_code(ret)); | |
434 | } | |
1fff1faa | 435 | goto error; |
96243366 DG |
436 | } |
437 | ||
1fff1faa | 438 | return 0; |
f3ed775e DG |
439 | |
440 | error: | |
441 | return -1; | |
fac6795d DG |
442 | } |
443 | ||
f3ed775e | 444 | |
fac6795d | 445 | /* |
5b8719f5 | 446 | * main |
fac6795d DG |
447 | */ |
448 | int main(int argc, char *argv[]) | |
449 | { | |
450 | int ret; | |
451 | ||
452 | progname = argv[0] ? argv[0] : "lttng"; | |
453 | ||
454 | /* For Mathieu Desnoyers aka Dr Tracing */ | |
455 | if (strncmp(progname, "drtrace", 7) == 0) { | |
456 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); | |
457 | } | |
458 | ||
5b8719f5 DG |
459 | ret = set_signal_handler(); |
460 | if (ret < 0) { | |
87378cf5 | 461 | clean_exit(ret); |
5b8719f5 DG |
462 | } |
463 | ||
f3ed775e | 464 | ret = parse_args(argc, argv); |
1fff1faa DG |
465 | if (ret < 0) { |
466 | clean_exit(EXIT_FAILURE); | |
467 | } | |
87378cf5 | 468 | |
fac6795d DG |
469 | return 0; |
470 | } |