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 modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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.
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.
30 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
41 #include <urcu/compiler.h>
44 #include <common/defaults.h>
45 #include <common/common.h>
46 #include <common/consumer.h>
47 #include <common/consumer-timer.h>
48 #include <common/compat/poll.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/utils.h>
52 #include "lttng-consumerd.h"
53 #include "health-consumerd.h"
55 /* TODO : support UST (all direct kernel-ctl accesses). */
57 /* threads (channel handling, poll, metadata, sessiond) */
59 static pthread_t channel_thread
, data_thread
, metadata_thread
,
60 sessiond_thread
, metadata_timer_thread
, health_thread
;
62 /* to count the number of times the user pressed ctrl+c */
63 static int sigintcount
= 0;
65 /* Argument variables */
66 int lttng_opt_quiet
; /* not static in error.h */
67 int lttng_opt_verbose
; /* not static in error.h */
68 static int opt_daemon
;
69 static const char *progname
;
70 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
71 static char error_sock_path
[PATH_MAX
]; /* Global error path */
72 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
74 /* the liblttngconsumerd context */
75 static struct lttng_consumer_local_data
*ctx
;
77 /* Consumerd health monitoring */
78 struct health_app
*health_consumerd
;
80 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
82 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
84 enum lttng_consumer_type
lttng_consumer_get_type(void)
87 return LTTNG_CONSUMER_UNKNOWN
;
93 * Signal handler for the daemon
95 static void sighandler(int sig
)
97 if (sig
== SIGINT
&& sigintcount
++ == 0) {
98 DBG("ignoring first SIGINT");
103 * Ignore SIGPIPE because it should not stop the consumer whenever a
104 * SIGPIPE is catched through a FD operation.
106 if (sig
== SIGPIPE
) {
110 lttng_consumer_should_exit(ctx
);
114 * Setup signal handler for :
115 * SIGINT, SIGTERM, SIGPIPE
117 static int set_signal_handler(void)
123 if ((ret
= sigemptyset(&sigset
)) < 0) {
124 perror("sigemptyset");
128 sa
.sa_handler
= sighandler
;
131 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
136 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
141 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
150 * Usage function on stream file.
152 static void usage(FILE *fp
)
154 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
155 fprintf(fp
, " -h, --help "
156 "Display this usage.\n");
157 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
158 "Specify path for the command socket\n");
159 fprintf(fp
, " -e, --consumerd-err-sock PATH "
160 "Specify path for the error socket\n");
161 fprintf(fp
, " -d, --daemonize "
162 "Start as a daemon.\n");
163 fprintf(fp
, " -q, --quiet "
164 "No output at all.\n");
165 fprintf(fp
, " -v, --verbose "
166 "Verbose mode. Activate DBG() macro.\n");
167 fprintf(fp
, " -V, --version "
168 "Show version number.\n");
169 fprintf(fp
, " -g, --group NAME "
170 "Specify the tracing group name. (default: tracing)\n");
171 fprintf(fp
, " -k, --kernel "
172 "Consumer kernel buffers (default).\n");
173 fprintf(fp
, " -u, --ust "
174 "Consumer UST buffers.%s\n",
175 #ifdef HAVE_LIBLTTNG_UST_CTL
178 " (support not compiled in)"
184 * daemon argument parsing
186 static void parse_args(int argc
, char **argv
)
190 static struct option long_options
[] = {
191 { "consumerd-cmd-sock", 1, 0, 'c' },
192 { "consumerd-err-sock", 1, 0, 'e' },
193 { "daemonize", 0, 0, 'd' },
194 { "group", 1, 0, 'g' },
195 { "help", 0, 0, 'h' },
196 { "quiet", 0, 0, 'q' },
197 { "verbose", 0, 0, 'v' },
198 { "version", 0, 0, 'V' },
199 { "kernel", 0, 0, 'k' },
200 #ifdef HAVE_LIBLTTNG_UST_CTL
201 { "ust", 0, 0, 'u' },
207 int option_index
= 0;
208 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:", long_options
, &option_index
);
215 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
217 fprintf(stderr
, " with arg %s\n", optarg
);
221 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
224 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
230 tracing_group_name
= optarg
;
239 lttng_opt_verbose
= 1;
242 fprintf(stdout
, "%s\n", VERSION
);
245 opt_type
= LTTNG_CONSUMER_KERNEL
;
247 #ifdef HAVE_LIBLTTNG_UST_CTL
249 # if (CAA_BITS_PER_LONG == 64)
250 opt_type
= LTTNG_CONSUMER64_UST
;
251 # elif (CAA_BITS_PER_LONG == 32)
252 opt_type
= LTTNG_CONSUMER32_UST
;
254 # error "Unknown bitness"
266 * Set open files limit to unlimited. This daemon can open a large number of
267 * file descriptors in order to consumer multiple kernel traces.
269 static void set_ulimit(void)
274 /* The kernel does not allowed an infinite limit for open files */
275 lim
.rlim_cur
= 65535;
276 lim
.rlim_max
= 65535;
278 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
280 PERROR("failed to set open files limit");
287 int main(int argc
, char **argv
)
292 /* Parse arguments */
294 parse_args(argc
, argv
);
302 * child: setsid, close FD 0, 1, 2, chdir /
303 * parent: exit (if fork is successful)
311 * We are in the child. Make sure all other file
312 * descriptors are closed, in case we are called with
313 * more opened file descriptors than the standard ones.
315 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
320 /* Set up max poll set size */
321 lttng_poll_set_max_size();
323 if (*command_sock_path
== '\0') {
325 case LTTNG_CONSUMER_KERNEL
:
326 snprintf(command_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
327 DEFAULT_LTTNG_RUNDIR
);
329 case LTTNG_CONSUMER64_UST
:
330 snprintf(command_sock_path
, PATH_MAX
,
331 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
333 case LTTNG_CONSUMER32_UST
:
334 snprintf(command_sock_path
, PATH_MAX
,
335 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
338 WARN("Unknown consumerd type");
344 if (lttng_consumer_init() < 0) {
348 /* Init socket timeouts */
353 /* Set limit for open files */
357 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
358 if (!health_consumerd
) {
362 /* create the consumer instance with and assign the callbacks */
363 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
364 NULL
, lttng_consumer_on_recv_stream
, NULL
);
369 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
370 if (*error_sock_path
== '\0') {
372 case LTTNG_CONSUMER_KERNEL
:
373 snprintf(error_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
374 DEFAULT_LTTNG_RUNDIR
);
376 case LTTNG_CONSUMER64_UST
:
377 snprintf(error_sock_path
, PATH_MAX
,
378 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
380 case LTTNG_CONSUMER32_UST
:
381 snprintf(error_sock_path
, PATH_MAX
,
382 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
385 WARN("Unknown consumerd type");
390 if (set_signal_handler() < 0) {
394 /* Connect to the socket created by lttng-sessiond to report errors */
395 DBG("Connecting to error socket %s", error_sock_path
);
396 ret
= lttcomm_connect_unix_sock(error_sock_path
);
397 /* not a fatal error, but all communication with lttng-sessiond will fail */
399 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
401 lttng_consumer_set_error_sock(ctx
, ret
);
404 * Block RT signals used for UST periodical metadata flush and the live
405 * timer in main, and create a dedicated thread to handle these signals.
407 consumer_signal_init();
409 ctx
->type
= opt_type
;
411 ret
= utils_create_pipe(health_quit_pipe
);
413 goto error_health_pipe
;
416 /* Create thread to manage the client socket */
417 ret
= pthread_create(&health_thread
, NULL
,
418 thread_manage_health
, (void *) NULL
);
420 PERROR("pthread_create health");
425 * Wait for health thread to be initialized before letting the
426 * sessiond thread reply to the sessiond that we are ready.
428 while (uatomic_read(<tng_consumer_ready
)) {
431 cmm_smp_mb(); /* Read ready before following operations */
433 /* Create thread to manage channels */
434 ret
= pthread_create(&channel_thread
, NULL
, consumer_thread_channel_poll
,
437 perror("pthread_create");
441 /* Create thread to manage the polling/writing of trace metadata */
442 ret
= pthread_create(&metadata_thread
, NULL
, consumer_thread_metadata_poll
,
445 perror("pthread_create");
449 /* Create thread to manage the polling/writing of trace data */
450 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
453 perror("pthread_create");
457 /* Create the thread to manage the receive of fd */
458 ret
= pthread_create(&sessiond_thread
, NULL
, consumer_thread_sessiond_poll
,
461 perror("pthread_create");
466 * Create the thread to manage the UST metadata periodic timer and
469 ret
= pthread_create(&metadata_timer_thread
, NULL
,
470 consumer_timer_thread
, (void *) ctx
);
472 perror("pthread_create");
473 goto metadata_timer_error
;
476 ret
= pthread_detach(metadata_timer_thread
);
479 perror("pthread_detach");
482 metadata_timer_error
:
483 ret
= pthread_join(sessiond_thread
, &status
);
485 perror("pthread_join");
490 ret
= pthread_join(data_thread
, &status
);
492 perror("pthread_join");
497 ret
= pthread_join(metadata_thread
, &status
);
499 perror("pthread_join");
504 ret
= pthread_join(channel_thread
, &status
);
506 perror("pthread_join");
511 ret
= pthread_join(health_thread
, &status
);
513 PERROR("pthread_join health thread");
514 goto error
; /* join error, exit without cleanup */
518 utils_close_pipe(health_quit_pipe
);
523 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_SUCCESS
);
530 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_FAILURE
);
534 lttng_consumer_destroy(ctx
);
535 lttng_consumer_cleanup();
536 if (health_consumerd
) {
537 health_app_destroy(health_consumerd
);