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