2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
41 #include <urcu/compiler.h>
43 #include <lttng-consumerd.h>
44 #include <lttng-kernel-ctl.h>
45 #include <lttng-sessiond-comm.h>
46 #include <lttng/lttng-kconsumer.h>
47 #include <lttng/lttng-ustconsumer.h>
50 /* TODO : support UST (all direct kernctl accesses). */
52 /* the two threads (receive fd and poll) */
53 static pthread_t threads
[2];
55 /* to count the number of time the user pressed ctrl+c */
56 static int sigintcount
= 0;
58 /* Argument variables */
61 static int opt_daemon
;
62 static const char *progname
;
63 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
64 static char error_sock_path
[PATH_MAX
]; /* Global error path */
65 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
67 /* the liblttngconsumerd context */
68 static struct lttng_consumer_local_data
*ctx
;
71 * Signal handler for the daemon
73 static void sighandler(int sig
)
75 if (sig
== SIGINT
&& sigintcount
++ == 0) {
76 DBG("ignoring first SIGINT");
80 lttng_consumer_should_exit(ctx
);
84 * Setup signal handler for :
85 * SIGINT, SIGTERM, SIGPIPE
87 static int set_signal_handler(void)
93 if ((ret
= sigemptyset(&sigset
)) < 0) {
94 perror("sigemptyset");
98 sa
.sa_handler
= sighandler
;
101 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
106 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
111 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
120 * usage function on stderr
122 static void usage(void)
124 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
125 fprintf(stderr
, " -h, --help "
126 "Display this usage.\n");
127 fprintf(stderr
, " -c, --consumerd-cmd-sock PATH "
128 "Specify path for the command socket\n");
129 fprintf(stderr
, " -e, --consumerd-err-sock PATH "
130 "Specify path for the error socket\n");
131 fprintf(stderr
, " -d, --daemonize "
132 "Start as a daemon.\n");
133 fprintf(stderr
, " -q, --quiet "
134 "No output at all.\n");
135 fprintf(stderr
, " -v, --verbose "
136 "Verbose mode. Activate DBG() macro.\n");
137 fprintf(stderr
, " -V, --version "
138 "Show version number.\n");
139 fprintf(stderr
, " -k, --kernel "
140 "Consumer kernel buffers (default).\n");
141 fprintf(stderr
, " -u, --ust "
142 "Consumer UST buffers.%s\n",
143 #ifdef HAVE_LIBLTTNG_UST_CTL
146 " (support not compiled in)"
152 * daemon argument parsing
154 static void parse_args(int argc
, char **argv
)
158 static struct option long_options
[] = {
159 { "consumerd-cmd-sock", 1, 0, 'c' },
160 { "consumerd-err-sock", 1, 0, 'e' },
161 { "daemonize", 0, 0, 'd' },
162 { "help", 0, 0, 'h' },
163 { "quiet", 0, 0, 'q' },
164 { "verbose", 0, 0, 'v' },
165 { "version", 0, 0, 'V' },
166 { "kernel", 0, 0, 'k' },
167 #ifdef HAVE_LIBLTTNG_UST_CTL
168 { "ust", 0, 0, 'u' },
174 int option_index
= 0;
175 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:", long_options
, &option_index
);
182 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
184 fprintf(stderr
, " with arg %s\n", optarg
);
188 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
191 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
206 fprintf(stdout
, "%s\n", VERSION
);
209 opt_type
= LTTNG_CONSUMER_KERNEL
;
211 #ifdef HAVE_LIBLTTNG_UST_CTL
213 # if (CAA_BITS_PER_LONG == 64)
214 opt_type
= LTTNG_CONSUMER64_UST
;
215 # elif (CAA_BITS_PER_LONG == 32)
216 opt_type
= LTTNG_CONSUMER32_UST
;
218 # error "Unknown bitness"
232 int main(int argc
, char **argv
)
238 /* Parse arguments */
240 parse_args(argc
, argv
);
251 if (strlen(command_sock_path
) == 0) {
253 case LTTNG_CONSUMER_KERNEL
:
254 snprintf(command_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
,
257 case LTTNG_CONSUMER64_UST
:
258 snprintf(command_sock_path
, PATH_MAX
,
259 USTCONSUMERD64_CMD_SOCK_PATH
, LTTNG_RUNDIR
);
261 case LTTNG_CONSUMER32_UST
:
262 snprintf(command_sock_path
, PATH_MAX
,
263 USTCONSUMERD32_CMD_SOCK_PATH
, LTTNG_RUNDIR
);
266 WARN("Unknown consumerd type");
272 lttng_consumer_init();
274 /* create the consumer instance with and assign the callbacks */
275 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
276 NULL
, lttng_consumer_on_recv_stream
, NULL
);
281 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
282 if (strlen(error_sock_path
) == 0) {
284 case LTTNG_CONSUMER_KERNEL
:
285 snprintf(error_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
,
288 case LTTNG_CONSUMER64_UST
:
289 snprintf(error_sock_path
, PATH_MAX
,
290 USTCONSUMERD64_ERR_SOCK_PATH
, LTTNG_RUNDIR
);
292 case LTTNG_CONSUMER32_UST
:
293 snprintf(error_sock_path
, PATH_MAX
,
294 USTCONSUMERD32_ERR_SOCK_PATH
, LTTNG_RUNDIR
);
297 WARN("Unknown consumerd type");
302 if (set_signal_handler() < 0) {
306 /* Connect to the socket created by lttng-sessiond to report errors */
307 DBG("Connecting to error socket %s", error_sock_path
);
308 ret
= lttcomm_connect_unix_sock(error_sock_path
);
309 /* not a fatal error, but all communication with lttng-sessiond will fail */
311 WARN("Cannot connect to error socket, is lttng-sessiond started ?");
313 lttng_consumer_set_error_sock(ctx
, ret
);
315 /* Create the thread to manage the receive of fd */
316 ret
= pthread_create(&threads
[0], NULL
, lttng_consumer_thread_receive_fds
,
319 perror("pthread_create");
323 /* Create thread to manage the polling/writing of traces */
324 ret
= pthread_create(&threads
[1], NULL
, lttng_consumer_thread_poll_fds
,
327 perror("pthread_create");
331 for (i
= 0; i
< 2; i
++) {
332 ret
= pthread_join(threads
[i
], &status
);
334 perror("pthread_join");
339 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_SUCCESS
);
344 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_FAILURE
);
347 lttng_consumer_destroy(ctx
);
348 lttng_consumer_cleanup();