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