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/lttngerr.h>
44 #include <common/kernel-ctl/kernel-ctl.h>
45 #include <common/sessiond-comm/sessiond-comm.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/ust-consumer/ust-consumer.h>
49 #include "lttng-consumerd.h"
51 /* TODO : support UST (all direct kernctl accesses). */
53 /* the two threads (receive fd and poll) */
54 static pthread_t threads
[2];
56 /* to count the number of time the user pressed ctrl+c */
57 static int sigintcount
= 0;
59 /* Argument variables */
62 static int opt_daemon
;
63 static const char *progname
;
64 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
65 static char error_sock_path
[PATH_MAX
]; /* Global error path */
66 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
68 /* the liblttngconsumerd context */
69 static struct lttng_consumer_local_data
*ctx
;
72 * Signal handler for the daemon
74 static void sighandler(int sig
)
76 if (sig
== SIGINT
&& sigintcount
++ == 0) {
77 DBG("ignoring first SIGINT");
81 lttng_consumer_should_exit(ctx
);
85 * Setup signal handler for :
86 * SIGINT, SIGTERM, SIGPIPE
88 static int set_signal_handler(void)
94 if ((ret
= sigemptyset(&sigset
)) < 0) {
95 perror("sigemptyset");
99 sa
.sa_handler
= sighandler
;
102 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
107 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
112 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
121 * usage function on stderr
123 static void usage(void)
125 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
126 fprintf(stderr
, " -h, --help "
127 "Display this usage.\n");
128 fprintf(stderr
, " -c, --consumerd-cmd-sock PATH "
129 "Specify path for the command socket\n");
130 fprintf(stderr
, " -e, --consumerd-err-sock PATH "
131 "Specify path for the error socket\n");
132 fprintf(stderr
, " -d, --daemonize "
133 "Start as a daemon.\n");
134 fprintf(stderr
, " -q, --quiet "
135 "No output at all.\n");
136 fprintf(stderr
, " -v, --verbose "
137 "Verbose mode. Activate DBG() macro.\n");
138 fprintf(stderr
, " -V, --version "
139 "Show version number.\n");
140 fprintf(stderr
, " -k, --kernel "
141 "Consumer kernel buffers (default).\n");
142 fprintf(stderr
, " -u, --ust "
143 "Consumer UST buffers.%s\n",
144 #ifdef HAVE_LIBLTTNG_UST_CTL
147 " (support not compiled in)"
153 * daemon argument parsing
155 static void parse_args(int argc
, char **argv
)
159 static struct option long_options
[] = {
160 { "consumerd-cmd-sock", 1, 0, 'c' },
161 { "consumerd-err-sock", 1, 0, 'e' },
162 { "daemonize", 0, 0, 'd' },
163 { "help", 0, 0, 'h' },
164 { "quiet", 0, 0, 'q' },
165 { "verbose", 0, 0, 'v' },
166 { "version", 0, 0, 'V' },
167 { "kernel", 0, 0, 'k' },
168 #ifdef HAVE_LIBLTTNG_UST_CTL
169 { "ust", 0, 0, 'u' },
175 int option_index
= 0;
176 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:", long_options
, &option_index
);
183 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
185 fprintf(stderr
, " with arg %s\n", optarg
);
189 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
192 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
207 fprintf(stdout
, "%s\n", VERSION
);
210 opt_type
= LTTNG_CONSUMER_KERNEL
;
212 #ifdef HAVE_LIBLTTNG_UST_CTL
214 # if (CAA_BITS_PER_LONG == 64)
215 opt_type
= LTTNG_CONSUMER64_UST
;
216 # elif (CAA_BITS_PER_LONG == 32)
217 opt_type
= LTTNG_CONSUMER32_UST
;
219 # error "Unknown bitness"
233 int main(int argc
, char **argv
)
239 /* Parse arguments */
241 parse_args(argc
, argv
);
252 if (strlen(command_sock_path
) == 0) {
254 case LTTNG_CONSUMER_KERNEL
:
255 snprintf(command_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
,
258 case LTTNG_CONSUMER64_UST
:
259 snprintf(command_sock_path
, PATH_MAX
,
260 USTCONSUMERD64_CMD_SOCK_PATH
, LTTNG_RUNDIR
);
262 case LTTNG_CONSUMER32_UST
:
263 snprintf(command_sock_path
, PATH_MAX
,
264 USTCONSUMERD32_CMD_SOCK_PATH
, LTTNG_RUNDIR
);
267 WARN("Unknown consumerd type");
273 lttng_consumer_init();
275 /* create the consumer instance with and assign the callbacks */
276 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
277 NULL
, lttng_consumer_on_recv_stream
, NULL
);
282 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
283 if (strlen(error_sock_path
) == 0) {
285 case LTTNG_CONSUMER_KERNEL
:
286 snprintf(error_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
,
289 case LTTNG_CONSUMER64_UST
:
290 snprintf(error_sock_path
, PATH_MAX
,
291 USTCONSUMERD64_ERR_SOCK_PATH
, LTTNG_RUNDIR
);
293 case LTTNG_CONSUMER32_UST
:
294 snprintf(error_sock_path
, PATH_MAX
,
295 USTCONSUMERD32_ERR_SOCK_PATH
, LTTNG_RUNDIR
);
298 WARN("Unknown consumerd type");
303 if (set_signal_handler() < 0) {
307 /* Connect to the socket created by lttng-sessiond to report errors */
308 DBG("Connecting to error socket %s", error_sock_path
);
309 ret
= lttcomm_connect_unix_sock(error_sock_path
);
310 /* not a fatal error, but all communication with lttng-sessiond will fail */
312 WARN("Cannot connect to error socket, is lttng-sessiond started ?");
314 lttng_consumer_set_error_sock(ctx
, ret
);
316 /* Create the thread to manage the receive of fd */
317 ret
= pthread_create(&threads
[0], NULL
, lttng_consumer_thread_receive_fds
,
320 perror("pthread_create");
324 /* Create thread to manage the polling/writing of traces */
325 ret
= pthread_create(&threads
[1], NULL
, lttng_consumer_thread_poll_fds
,
328 perror("pthread_create");
332 for (i
= 0; i
< 2; i
++) {
333 ret
= pthread_join(threads
[i
], &status
);
335 perror("pthread_join");
340 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_SUCCESS
);
345 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_FAILURE
);
348 lttng_consumer_destroy(ctx
);
349 lttng_consumer_cleanup();