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 | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
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 { | |
42 | OPT_NO_SESSIOND, | |
43 | OPT_SESSION_PATH, | |
44 | }; | |
45 | ||
46 | /* Getopt options. No first level command. */ | |
47 | static struct option long_options[] = { | |
48 | {"help", 0, NULL, 'h'}, | |
49 | {"group", 1, NULL, 'g'}, | |
50 | {"verbose", 0, NULL, 'v'}, | |
51 | {"quiet", 0, NULL, 'q'}, | |
52 | {"no-sessiond", 0, NULL, OPT_NO_SESSIOND}, | |
53 | {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, | |
54 | {NULL, 0, NULL, 0} | |
55 | }; | |
56 | ||
57 | /* First level command */ | |
58 | static struct cmd_struct commands[] = { | |
59 | { "list", cmd_list}, | |
60 | { "create", cmd_create}, | |
61 | { "destroy", cmd_destroy}, | |
62 | { "add-channel", cmd_add_channel}, | |
63 | { "start", cmd_start}, | |
64 | { "stop", cmd_stop}, | |
65 | { "enable-event", cmd_enable_events}, | |
e953ef25 | 66 | { "disable-event", cmd_disable_events}, |
d36b8583 | 67 | { "enable-channel", cmd_enable_channels}, |
26cc6b4e | 68 | { "disable-channel", cmd_disable_channels}, |
f3ed775e DG |
69 | { NULL, NULL} /* Array closure */ |
70 | }; | |
71 | ||
72 | static void usage(FILE *ofp) | |
8c0faa1d | 73 | { |
f3ed775e DG |
74 | fprintf(ofp, "LTTng Trace Control " VERSION"\n\n"); |
75 | fprintf(ofp, "usage: lttng [options] <command>\n"); | |
76 | fprintf(ofp, "\n"); | |
77 | fprintf(ofp, "Options:\n"); | |
78 | fprintf(ofp, " -h, --help Show this help\n"); | |
79 | fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n"); | |
80 | fprintf(ofp, " -v, --verbose Verbose mode\n"); | |
81 | fprintf(ofp, " -q, --quiet Quiet mode\n"); | |
82 | fprintf(ofp, " --no-sessiond Don't spawn a session daemon\n"); | |
83 | fprintf(ofp, " --sessiond-path Session daemon full path\n"); | |
84 | fprintf(ofp, "\n"); | |
85 | fprintf(ofp, "Commands:\n"); | |
86 | fprintf(ofp, " add-channel Add channel to tracer\n"); | |
87 | fprintf(ofp, " create Create tracing session\n"); | |
88 | fprintf(ofp, " destroy Teardown tracing session\n"); | |
d36b8583 | 89 | fprintf(ofp, " enable-channel Enable tracing channel\n"); |
26cc6b4e DG |
90 | fprintf(ofp, " enable-event Enable tracing event\n"); |
91 | fprintf(ofp, " disable-channel Disable tracing channel\n"); | |
f3ed775e DG |
92 | fprintf(ofp, " disable-event Disable tracing event\n"); |
93 | fprintf(ofp, " list List possible tracing options\n"); | |
94 | fprintf(ofp, " start Start tracing\n"); | |
95 | fprintf(ofp, " stop Stop tracing\n"); | |
96 | fprintf(ofp, " version Show version information\n"); | |
97 | fprintf(ofp, "\n"); | |
98 | fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n"); | |
99 | fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n"); | |
8c0faa1d DG |
100 | } |
101 | ||
7442b2ba | 102 | /* |
f3ed775e | 103 | * clean_exit |
96243366 | 104 | */ |
f3ed775e | 105 | static void clean_exit(int code) |
96243366 | 106 | { |
f3ed775e DG |
107 | DBG("Clean exit"); |
108 | exit(code); | |
894be886 | 109 | } |
96243366 | 110 | |
894be886 | 111 | /* |
f3ed775e | 112 | * sighandler |
2ef84c95 | 113 | * |
f3ed775e | 114 | * Signal handler for the daemon |
2ef84c95 | 115 | */ |
f3ed775e | 116 | static void sighandler(int sig) |
2ef84c95 | 117 | { |
f3ed775e DG |
118 | switch (sig) { |
119 | case SIGTERM: | |
120 | DBG("SIGTERM catched"); | |
121 | clean_exit(EXIT_FAILURE); | |
122 | break; | |
123 | case SIGCHLD: | |
124 | /* Notify is done */ | |
125 | DBG("SIGCHLD catched"); | |
126 | break; | |
127 | default: | |
128 | DBG("Unknown signal %d catched", sig); | |
129 | break; | |
2ef84c95 DG |
130 | } |
131 | ||
f3ed775e | 132 | return; |
2ef84c95 DG |
133 | } |
134 | ||
135 | /* | |
f3ed775e | 136 | * set_signal_handler |
894be886 | 137 | * |
f3ed775e | 138 | * Setup signal handler for SIGCHLD and SIGTERM. |
894be886 | 139 | */ |
f3ed775e | 140 | static int set_signal_handler(void) |
894be886 | 141 | { |
f3ed775e DG |
142 | int ret = 0; |
143 | struct sigaction sa; | |
144 | sigset_t sigset; | |
33a2b854 | 145 | |
f3ed775e DG |
146 | if ((ret = sigemptyset(&sigset)) < 0) { |
147 | perror("sigemptyset"); | |
33a2b854 DG |
148 | goto end; |
149 | } | |
150 | ||
f3ed775e DG |
151 | sa.sa_handler = sighandler; |
152 | sa.sa_mask = sigset; | |
153 | sa.sa_flags = 0; | |
154 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { | |
155 | perror("sigaction"); | |
96243366 DG |
156 | goto end; |
157 | } | |
158 | ||
f3ed775e DG |
159 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
160 | perror("sigaction"); | |
161 | goto end; | |
894be886 DG |
162 | } |
163 | ||
96243366 | 164 | end: |
57167058 DG |
165 | return ret; |
166 | } | |
167 | ||
fac6795d | 168 | /* |
f3ed775e | 169 | * handle_command |
fac6795d | 170 | * |
f3ed775e DG |
171 | * Handle the full argv list of a first level command. Will find the command |
172 | * in the global commands array and call the function callback associated. | |
1c9f7941 | 173 | * |
f3ed775e DG |
174 | * If command not found, return -1 |
175 | * else, return function command error code. | |
1c9f7941 | 176 | */ |
f3ed775e | 177 | static int handle_command(int argc, char **argv) |
1c9f7941 | 178 | { |
f3ed775e DG |
179 | int i = 0, ret; |
180 | struct cmd_struct *cmd; | |
1c9f7941 | 181 | |
f3ed775e DG |
182 | if (*argv == NULL) { |
183 | ret = CMD_SUCCESS; | |
47b74d63 | 184 | goto end; |
1c9f7941 DG |
185 | } |
186 | ||
f3ed775e DG |
187 | cmd = &commands[i]; |
188 | while (cmd->func != NULL) { | |
189 | /* Find command */ | |
190 | if (strcmp(argv[0], cmd->name) == 0) { | |
191 | ret = cmd->func(argc, (const char**) argv); | |
192 | switch (ret) { | |
193 | case CMD_ERROR: | |
194 | ERR("Command error"); | |
195 | break; | |
196 | case CMD_NOT_IMPLEMENTED: | |
197 | ERR("Options not implemented"); | |
198 | break; | |
199 | case CMD_UNDEFINED: | |
200 | ERR("Undefined command"); | |
201 | break; | |
202 | case CMD_FATAL: | |
203 | ERR("Fatal error"); | |
204 | break; | |
205 | } | |
206 | goto end; | |
894be886 | 207 | } |
f3ed775e DG |
208 | i++; |
209 | cmd = &commands[i]; | |
894be886 DG |
210 | } |
211 | ||
f3ed775e DG |
212 | /* Command not found */ |
213 | ret = -1; | |
894be886 DG |
214 | |
215 | end: | |
f3ed775e | 216 | return ret; |
8548ff30 DG |
217 | } |
218 | ||
5b8719f5 DG |
219 | /* |
220 | * spawn_sessiond | |
221 | * | |
222 | * Spawn a session daemon by forking and execv. | |
223 | */ | |
224 | static int spawn_sessiond(char *pathname) | |
225 | { | |
226 | int ret = 0; | |
227 | pid_t pid; | |
228 | ||
f3ed775e | 229 | MSG("Spawning a session daemon"); |
5b8719f5 DG |
230 | pid = fork(); |
231 | if (pid == 0) { | |
5e16da05 MD |
232 | /* |
233 | * Spawn session daemon and tell | |
5b8719f5 DG |
234 | * it to signal us when ready. |
235 | */ | |
5e16da05 MD |
236 | execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL); |
237 | /* execlp only returns if error happened */ | |
238 | if (errno == ENOENT) { | |
239 | ERR("No session daemon found. Use --sessiond-path."); | |
240 | } else { | |
241 | perror("execlp"); | |
5b8719f5 | 242 | } |
5e16da05 MD |
243 | kill(getppid(), SIGTERM); /* unpause parent */ |
244 | exit(EXIT_FAILURE); | |
5b8719f5 DG |
245 | } else if (pid > 0) { |
246 | /* Wait for ltt-sessiond to start */ | |
247 | pause(); | |
248 | goto end; | |
249 | } else { | |
250 | perror("fork"); | |
251 | ret = -1; | |
252 | goto end; | |
253 | } | |
254 | ||
255 | end: | |
256 | return ret; | |
257 | } | |
258 | ||
fac6795d | 259 | /* |
f3ed775e | 260 | * check_sessiond |
fac6795d DG |
261 | * |
262 | * Check if the session daemon is available using | |
5b8719f5 DG |
263 | * the liblttngctl API for the check. If not, try to |
264 | * spawn a daemon. | |
fac6795d | 265 | */ |
f3ed775e | 266 | static int check_sessiond(void) |
fac6795d DG |
267 | { |
268 | int ret; | |
5e16da05 | 269 | char *pathname = NULL, *alloc_pathname = NULL; |
fac6795d | 270 | |
947308c4 DG |
271 | ret = lttng_session_daemon_alive(); |
272 | if (ret == 0) { /* not alive */ | |
5b8719f5 DG |
273 | /* Try command line option path */ |
274 | if (opt_sessiond_path != NULL) { | |
275 | ret = access(opt_sessiond_path, F_OK | X_OK); | |
276 | if (ret < 0) { | |
277 | ERR("No such file: %s", opt_sessiond_path); | |
278 | goto end; | |
279 | } | |
280 | pathname = opt_sessiond_path; | |
281 | } else { | |
282 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
e8f07c63 | 283 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
5b8719f5 DG |
284 | } |
285 | ||
286 | /* Let's rock and roll */ | |
287 | if (pathname == NULL) { | |
5e16da05 | 288 | ret = asprintf(&alloc_pathname, "ltt-sessiond"); |
5b8719f5 DG |
289 | if (ret < 0) { |
290 | goto end; | |
291 | } | |
5e16da05 | 292 | pathname = alloc_pathname; |
5b8719f5 DG |
293 | } |
294 | ||
295 | ret = spawn_sessiond(pathname); | |
5e16da05 | 296 | free(alloc_pathname); |
5b8719f5 DG |
297 | if (ret < 0) { |
298 | ERR("Problem occurs when starting %s", pathname); | |
299 | goto end; | |
300 | } | |
fac6795d DG |
301 | } |
302 | ||
5b8719f5 | 303 | end: |
fac6795d DG |
304 | return ret; |
305 | } | |
306 | ||
5b8719f5 | 307 | /* |
f3ed775e | 308 | * parse_args |
5b8719f5 | 309 | * |
f3ed775e DG |
310 | * Parse command line arguments. |
311 | * Return 0 if OK, else -1 | |
5b8719f5 | 312 | */ |
f3ed775e | 313 | static int parse_args(int argc, char **argv) |
5b8719f5 | 314 | { |
f3ed775e | 315 | int opt, ret; |
5b8719f5 | 316 | |
f3ed775e DG |
317 | if (argc < 2) { |
318 | usage(stderr); | |
319 | clean_exit(EXIT_FAILURE); | |
5b8719f5 DG |
320 | } |
321 | ||
f3ed775e DG |
322 | while ((opt = getopt_long(argc, argv, "+hvqg:", long_options, NULL)) != -1) { |
323 | switch (opt) { | |
324 | case 'h': | |
325 | usage(stderr); | |
326 | goto error; | |
327 | case 'v': | |
328 | opt_verbose = 1; | |
329 | break; | |
330 | case 'q': | |
331 | opt_quiet = 1; | |
332 | break; | |
333 | case 'g': | |
334 | lttng_set_tracing_group(optarg); | |
335 | break; | |
336 | case OPT_NO_SESSIOND: | |
337 | opt_no_sessiond = 1; | |
338 | break; | |
339 | case OPT_SESSION_PATH: | |
340 | opt_sessiond_path = strdup(optarg); | |
341 | break; | |
342 | default: | |
343 | usage(stderr); | |
344 | goto error; | |
345 | } | |
5b8719f5 | 346 | } |
fac6795d | 347 | |
f3ed775e DG |
348 | /* If both options are specified, quiet wins */ |
349 | if (opt_verbose && opt_quiet) { | |
350 | opt_verbose = 0; | |
5b8719f5 DG |
351 | } |
352 | ||
f3ed775e DG |
353 | /* Spawn session daemon if needed */ |
354 | if (opt_no_sessiond == 0 && (check_sessiond() < 0)) { | |
355 | goto error; | |
5b8719f5 DG |
356 | } |
357 | ||
f3ed775e DG |
358 | /* No leftovers, print usage and quit */ |
359 | if ((argc - optind) == 0) { | |
360 | usage(stderr); | |
361 | goto error; | |
362 | } | |
7442b2ba | 363 | |
f3ed775e DG |
364 | /* |
365 | * Handle leftovers which is a first level command with the trailing | |
366 | * options. | |
367 | */ | |
368 | ret = handle_command(argc - optind, argv + optind); | |
369 | if (ret < 0) { | |
370 | if (ret == -1) { | |
371 | usage(stderr); | |
372 | goto error; | |
373 | } else { | |
374 | ERR("%s", lttng_get_readable_code(ret)); | |
375 | } | |
96243366 DG |
376 | } |
377 | ||
f3ed775e DG |
378 | return ret; |
379 | ||
380 | error: | |
381 | return -1; | |
fac6795d DG |
382 | } |
383 | ||
f3ed775e | 384 | |
fac6795d | 385 | /* |
5b8719f5 | 386 | * main |
fac6795d DG |
387 | */ |
388 | int main(int argc, char *argv[]) | |
389 | { | |
390 | int ret; | |
391 | ||
392 | progname = argv[0] ? argv[0] : "lttng"; | |
393 | ||
394 | /* For Mathieu Desnoyers aka Dr Tracing */ | |
395 | if (strncmp(progname, "drtrace", 7) == 0) { | |
396 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); | |
397 | } | |
398 | ||
5b8719f5 DG |
399 | ret = set_signal_handler(); |
400 | if (ret < 0) { | |
87378cf5 | 401 | clean_exit(ret); |
5b8719f5 DG |
402 | } |
403 | ||
f3ed775e DG |
404 | ret = parse_args(argc, argv); |
405 | clean_exit(ret); | |
87378cf5 | 406 | |
fac6795d DG |
407 | return 0; |
408 | } |