Add disable kernel channel support
[lttng-tools.git] / lttng / lttng.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <lttng/lttng.h>
28
29 #include "cmd.h"
30 #include "conf.h"
31 #include "lttngerr.h"
32
33 /* Variables */
34 static char *progname;
35
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},
66 { "disable-event", cmd_disable_events},
67 { "enable-channel", cmd_enable_channels},
68 { "disable-channel", cmd_disable_channels},
69 { NULL, NULL} /* Array closure */
70 };
71
72 static void usage(FILE *ofp)
73 {
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");
89 fprintf(ofp, " enable-channel Enable tracing channel\n");
90 fprintf(ofp, " enable-event Enable tracing event\n");
91 fprintf(ofp, " disable-channel Disable tracing channel\n");
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");
100 }
101
102 /*
103 * clean_exit
104 */
105 static void clean_exit(int code)
106 {
107 DBG("Clean exit");
108 exit(code);
109 }
110
111 /*
112 * sighandler
113 *
114 * Signal handler for the daemon
115 */
116 static void sighandler(int sig)
117 {
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;
130 }
131
132 return;
133 }
134
135 /*
136 * set_signal_handler
137 *
138 * Setup signal handler for SIGCHLD and SIGTERM.
139 */
140 static int set_signal_handler(void)
141 {
142 int ret = 0;
143 struct sigaction sa;
144 sigset_t sigset;
145
146 if ((ret = sigemptyset(&sigset)) < 0) {
147 perror("sigemptyset");
148 goto end;
149 }
150
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");
156 goto end;
157 }
158
159 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
160 perror("sigaction");
161 goto end;
162 }
163
164 end:
165 return ret;
166 }
167
168 /*
169 * handle_command
170 *
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.
173 *
174 * If command not found, return -1
175 * else, return function command error code.
176 */
177 static int handle_command(int argc, char **argv)
178 {
179 int i = 0, ret;
180 struct cmd_struct *cmd;
181
182 if (*argv == NULL) {
183 ret = CMD_SUCCESS;
184 goto end;
185 }
186
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;
207 }
208 i++;
209 cmd = &commands[i];
210 }
211
212 /* Command not found */
213 ret = -1;
214
215 end:
216 return ret;
217 }
218
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
229 MSG("Spawning a session daemon");
230 pid = fork();
231 if (pid == 0) {
232 /*
233 * Spawn session daemon and tell
234 * it to signal us when ready.
235 */
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");
242 }
243 kill(getppid(), SIGTERM); /* unpause parent */
244 exit(EXIT_FAILURE);
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
259 /*
260 * check_sessiond
261 *
262 * Check if the session daemon is available using
263 * the liblttngctl API for the check. If not, try to
264 * spawn a daemon.
265 */
266 static int check_sessiond(void)
267 {
268 int ret;
269 char *pathname = NULL, *alloc_pathname = NULL;
270
271 ret = lttng_session_daemon_alive();
272 if (ret == 0) { /* not alive */
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 */
283 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
284 }
285
286 /* Let's rock and roll */
287 if (pathname == NULL) {
288 ret = asprintf(&alloc_pathname, "ltt-sessiond");
289 if (ret < 0) {
290 goto end;
291 }
292 pathname = alloc_pathname;
293 }
294
295 ret = spawn_sessiond(pathname);
296 free(alloc_pathname);
297 if (ret < 0) {
298 ERR("Problem occurs when starting %s", pathname);
299 goto end;
300 }
301 }
302
303 end:
304 return ret;
305 }
306
307 /*
308 * parse_args
309 *
310 * Parse command line arguments.
311 * Return 0 if OK, else -1
312 */
313 static int parse_args(int argc, char **argv)
314 {
315 int opt, ret;
316
317 if (argc < 2) {
318 usage(stderr);
319 clean_exit(EXIT_FAILURE);
320 }
321
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 }
346 }
347
348 /* If both options are specified, quiet wins */
349 if (opt_verbose && opt_quiet) {
350 opt_verbose = 0;
351 }
352
353 /* Spawn session daemon if needed */
354 if (opt_no_sessiond == 0 && (check_sessiond() < 0)) {
355 goto error;
356 }
357
358 /* No leftovers, print usage and quit */
359 if ((argc - optind) == 0) {
360 usage(stderr);
361 goto error;
362 }
363
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 }
376 }
377
378 return ret;
379
380 error:
381 return -1;
382 }
383
384
385 /*
386 * main
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
399 ret = set_signal_handler();
400 if (ret < 0) {
401 clean_exit(ret);
402 }
403
404 ret = parse_args(argc, argv);
405 clean_exit(ret);
406
407 return 0;
408 }
This page took 0.057118 seconds and 4 git commands to generate.