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 <common/defaults.h>
44 #include <common/common.h>
45 #include <common/kernel-consumer/kernel-consumer.h>
46 #include <common/kernel-ctl/kernel-ctl.h>
47 #include <common/sessiond-comm/sessiond-comm.h>
48 #include <common/ust-consumer/ust-consumer.h>
50 #include "lttng-consumerd.h"
52 /* TODO : support UST (all direct kernctl accesses). */
54 /* the two threads (receive fd and poll) */
55 static pthread_t threads
[2];
57 /* to count the number of time the user pressed ctrl+c */
58 static int sigintcount
= 0;
60 /* Argument variables */
63 static int opt_daemon
;
64 static const char *progname
;
65 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
66 static char error_sock_path
[PATH_MAX
]; /* Global error path */
67 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
69 /* the liblttngconsumerd context */
70 static struct lttng_consumer_local_data
*ctx
;
73 * Signal handler for the daemon
75 static void sighandler(int sig
)
77 if (sig
== SIGINT
&& sigintcount
++ == 0) {
78 DBG("ignoring first SIGINT");
82 lttng_consumer_should_exit(ctx
);
86 * Setup signal handler for :
87 * SIGINT, SIGTERM, SIGPIPE
89 static int set_signal_handler(void)
95 if ((ret
= sigemptyset(&sigset
)) < 0) {
96 perror("sigemptyset");
100 sa
.sa_handler
= sighandler
;
103 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
108 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
113 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
122 * usage function on stderr
124 static void usage(void)
126 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
127 fprintf(stderr
, " -h, --help "
128 "Display this usage.\n");
129 fprintf(stderr
, " -c, --consumerd-cmd-sock PATH "
130 "Specify path for the command socket\n");
131 fprintf(stderr
, " -e, --consumerd-err-sock PATH "
132 "Specify path for the error socket\n");
133 fprintf(stderr
, " -d, --daemonize "
134 "Start as a daemon.\n");
135 fprintf(stderr
, " -q, --quiet "
136 "No output at all.\n");
137 fprintf(stderr
, " -v, --verbose "
138 "Verbose mode. Activate DBG() macro.\n");
139 fprintf(stderr
, " -V, --version "
140 "Show version number.\n");
141 fprintf(stderr
, " -k, --kernel "
142 "Consumer kernel buffers (default).\n");
143 fprintf(stderr
, " -u, --ust "
144 "Consumer UST buffers.%s\n",
145 #ifdef HAVE_LIBLTTNG_UST_CTL
148 " (support not compiled in)"
154 * daemon argument parsing
156 static void parse_args(int argc
, char **argv
)
160 static struct option long_options
[] = {
161 { "consumerd-cmd-sock", 1, 0, 'c' },
162 { "consumerd-err-sock", 1, 0, 'e' },
163 { "daemonize", 0, 0, 'd' },
164 { "help", 0, 0, 'h' },
165 { "quiet", 0, 0, 'q' },
166 { "verbose", 0, 0, 'v' },
167 { "version", 0, 0, 'V' },
168 { "kernel", 0, 0, 'k' },
169 #ifdef HAVE_LIBLTTNG_UST_CTL
170 { "ust", 0, 0, 'u' },
176 int option_index
= 0;
177 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:", long_options
, &option_index
);
184 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
186 fprintf(stderr
, " with arg %s\n", optarg
);
190 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
193 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
208 fprintf(stdout
, "%s\n", VERSION
);
211 opt_type
= LTTNG_CONSUMER_KERNEL
;
213 #ifdef HAVE_LIBLTTNG_UST_CTL
215 # if (CAA_BITS_PER_LONG == 64)
216 opt_type
= LTTNG_CONSUMER64_UST
;
217 # elif (CAA_BITS_PER_LONG == 32)
218 opt_type
= LTTNG_CONSUMER32_UST
;
220 # error "Unknown bitness"
234 int main(int argc
, char **argv
)
240 /* Parse arguments */
242 parse_args(argc
, argv
);
253 if (strlen(command_sock_path
) == 0) {
255 case LTTNG_CONSUMER_KERNEL
:
256 snprintf(command_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
257 DEFAULT_LTTNG_RUNDIR
);
259 case LTTNG_CONSUMER64_UST
:
260 snprintf(command_sock_path
, PATH_MAX
,
261 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
263 case LTTNG_CONSUMER32_UST
:
264 snprintf(command_sock_path
, PATH_MAX
,
265 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
268 WARN("Unknown consumerd type");
274 lttng_consumer_init();
276 /* create the consumer instance with and assign the callbacks */
277 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
278 NULL
, lttng_consumer_on_recv_stream
, NULL
);
283 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
284 if (strlen(error_sock_path
) == 0) {
286 case LTTNG_CONSUMER_KERNEL
:
287 snprintf(error_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
288 DEFAULT_LTTNG_RUNDIR
);
290 case LTTNG_CONSUMER64_UST
:
291 snprintf(error_sock_path
, PATH_MAX
,
292 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
294 case LTTNG_CONSUMER32_UST
:
295 snprintf(error_sock_path
, PATH_MAX
,
296 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
299 WARN("Unknown consumerd type");
304 if (set_signal_handler() < 0) {
308 /* Connect to the socket created by lttng-sessiond to report errors */
309 DBG("Connecting to error socket %s", error_sock_path
);
310 ret
= lttcomm_connect_unix_sock(error_sock_path
);
311 /* not a fatal error, but all communication with lttng-sessiond will fail */
313 WARN("Cannot connect to error socket, is lttng-sessiond started ?");
315 lttng_consumer_set_error_sock(ctx
, ret
);
317 /* Create the thread to manage the receive of fd */
318 ret
= pthread_create(&threads
[0], NULL
, lttng_consumer_thread_receive_fds
,
321 perror("pthread_create");
325 /* Create thread to manage the polling/writing of traces */
326 ret
= pthread_create(&threads
[1], NULL
, lttng_consumer_thread_poll_fds
,
329 perror("pthread_create");
333 for (i
= 0; i
< 2; i
++) {
334 ret
= pthread_join(threads
[i
], &status
);
336 perror("pthread_join");
341 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_SUCCESS
);
346 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_FAILURE
);
349 lttng_consumer_destroy(ctx
);
350 lttng_consumer_cleanup();