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