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 int lttng_opt_mi
; /* not static in error.h */
70 static int opt_daemon
;
71 static const char *progname
;
72 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
73 static char error_sock_path
[PATH_MAX
]; /* Global error path */
74 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
76 /* the liblttngconsumerd context */
77 static struct lttng_consumer_local_data
*ctx
;
79 /* Consumerd health monitoring */
80 struct health_app
*health_consumerd
;
82 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
84 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
86 enum lttng_consumer_type
lttng_consumer_get_type(void)
89 return LTTNG_CONSUMER_UNKNOWN
;
95 * Signal handler for the daemon
97 static void sighandler(int sig
)
99 if (sig
== SIGINT
&& sigintcount
++ == 0) {
100 DBG("ignoring first SIGINT");
105 * Ignore SIGPIPE because it should not stop the consumer whenever a
106 * SIGPIPE is catched through a FD operation.
108 if (sig
== SIGPIPE
) {
113 lttng_consumer_should_exit(ctx
);
118 * Setup signal handler for :
119 * SIGINT, SIGTERM, SIGPIPE
121 static int set_signal_handler(void)
127 if ((ret
= sigemptyset(&sigset
)) < 0) {
128 perror("sigemptyset");
132 sa
.sa_handler
= sighandler
;
135 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
140 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
145 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
154 * Usage function on stream file.
156 static void usage(FILE *fp
)
158 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
159 fprintf(fp
, " -h, --help "
160 "Display this usage.\n");
161 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
162 "Specify path for the command socket\n");
163 fprintf(fp
, " -e, --consumerd-err-sock PATH "
164 "Specify path for the error socket\n");
165 fprintf(fp
, " -d, --daemonize "
166 "Start as a daemon.\n");
167 fprintf(fp
, " -q, --quiet "
168 "No output at all.\n");
169 fprintf(fp
, " -v, --verbose "
170 "Verbose mode. Activate DBG() macro.\n");
171 fprintf(fp
, " -V, --version "
172 "Show version number.\n");
173 fprintf(fp
, " -g, --group NAME "
174 "Specify the tracing group name. (default: tracing)\n");
175 fprintf(fp
, " -k, --kernel "
176 "Consumer kernel buffers (default).\n");
177 fprintf(fp
, " -u, --ust "
178 "Consumer UST buffers.%s\n",
179 #ifdef HAVE_LIBLTTNG_UST_CTL
182 " (support not compiled in)"
188 * daemon argument parsing
190 static void parse_args(int argc
, char **argv
)
194 static struct option long_options
[] = {
195 { "consumerd-cmd-sock", 1, 0, 'c' },
196 { "consumerd-err-sock", 1, 0, 'e' },
197 { "daemonize", 0, 0, 'd' },
198 { "group", 1, 0, 'g' },
199 { "help", 0, 0, 'h' },
200 { "quiet", 0, 0, 'q' },
201 { "verbose", 0, 0, 'v' },
202 { "version", 0, 0, 'V' },
203 { "kernel", 0, 0, 'k' },
204 #ifdef HAVE_LIBLTTNG_UST_CTL
205 { "ust", 0, 0, 'u' },
211 int option_index
= 0;
212 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:", long_options
, &option_index
);
219 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
221 fprintf(stderr
, " with arg %s\n", optarg
);
225 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
228 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
234 tracing_group_name
= optarg
;
243 lttng_opt_verbose
= 1;
246 fprintf(stdout
, "%s\n", VERSION
);
249 opt_type
= LTTNG_CONSUMER_KERNEL
;
251 #ifdef HAVE_LIBLTTNG_UST_CTL
253 # if (CAA_BITS_PER_LONG == 64)
254 opt_type
= LTTNG_CONSUMER64_UST
;
255 # elif (CAA_BITS_PER_LONG == 32)
256 opt_type
= LTTNG_CONSUMER32_UST
;
258 # error "Unknown bitness"
270 * Set open files limit to unlimited. This daemon can open a large number of
271 * file descriptors in order to consumer multiple kernel traces.
273 static void set_ulimit(void)
278 /* The kernel does not allowed an infinite limit for open files */
279 lim
.rlim_cur
= 65535;
280 lim
.rlim_max
= 65535;
282 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
284 PERROR("failed to set open files limit");
291 int main(int argc
, char **argv
)
295 struct lttng_consumer_local_data
*tmp_ctx
;
297 /* Parse arguments */
299 parse_args(argc
, argv
);
307 * child: setsid, close FD 0, 1, 2, chdir /
308 * parent: exit (if fork is successful)
316 * We are in the child. Make sure all other file
317 * descriptors are closed, in case we are called with
318 * more opened file descriptors than the standard ones.
320 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
325 /* Set up max poll set size */
326 lttng_poll_set_max_size();
328 if (*command_sock_path
== '\0') {
330 case LTTNG_CONSUMER_KERNEL
:
331 snprintf(command_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
332 DEFAULT_LTTNG_RUNDIR
);
334 case LTTNG_CONSUMER64_UST
:
335 snprintf(command_sock_path
, PATH_MAX
,
336 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
338 case LTTNG_CONSUMER32_UST
:
339 snprintf(command_sock_path
, PATH_MAX
,
340 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
343 WARN("Unknown consumerd type");
349 if (lttng_consumer_init() < 0) {
353 /* Init socket timeouts */
358 /* Set limit for open files */
362 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
363 if (!health_consumerd
) {
367 /* create the consumer instance with and assign the callbacks */
368 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
369 NULL
, lttng_consumer_on_recv_stream
, NULL
);
374 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
375 if (*error_sock_path
== '\0') {
377 case LTTNG_CONSUMER_KERNEL
:
378 snprintf(error_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
379 DEFAULT_LTTNG_RUNDIR
);
381 case LTTNG_CONSUMER64_UST
:
382 snprintf(error_sock_path
, PATH_MAX
,
383 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
385 case LTTNG_CONSUMER32_UST
:
386 snprintf(error_sock_path
, PATH_MAX
,
387 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
390 WARN("Unknown consumerd type");
395 if (set_signal_handler() < 0) {
399 /* Connect to the socket created by lttng-sessiond to report errors */
400 DBG("Connecting to error socket %s", error_sock_path
);
401 ret
= lttcomm_connect_unix_sock(error_sock_path
);
402 /* not a fatal error, but all communication with lttng-sessiond will fail */
404 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
406 lttng_consumer_set_error_sock(ctx
, ret
);
409 * Block RT signals used for UST periodical metadata flush and the live
410 * timer in main, and create a dedicated thread to handle these signals.
412 consumer_signal_init();
414 ctx
->type
= opt_type
;
416 ret
= utils_create_pipe(health_quit_pipe
);
418 goto error_health_pipe
;
421 /* Create thread to manage the client socket */
422 ret
= pthread_create(&health_thread
, NULL
,
423 thread_manage_health
, (void *) NULL
);
425 PERROR("pthread_create health");
430 * Wait for health thread to be initialized before letting the
431 * sessiond thread reply to the sessiond that we are ready.
433 while (uatomic_read(<tng_consumer_ready
)) {
436 cmm_smp_mb(); /* Read ready before following operations */
438 /* Create thread to manage channels */
439 ret
= pthread_create(&channel_thread
, NULL
, consumer_thread_channel_poll
,
442 perror("pthread_create");
446 /* Create thread to manage the polling/writing of trace metadata */
447 ret
= pthread_create(&metadata_thread
, NULL
, consumer_thread_metadata_poll
,
450 perror("pthread_create");
454 /* Create thread to manage the polling/writing of trace data */
455 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
458 perror("pthread_create");
462 /* Create the thread to manage the receive of fd */
463 ret
= pthread_create(&sessiond_thread
, NULL
, consumer_thread_sessiond_poll
,
466 perror("pthread_create");
471 * Create the thread to manage the UST metadata periodic timer and
474 ret
= pthread_create(&metadata_timer_thread
, NULL
,
475 consumer_timer_thread
, (void *) ctx
);
477 perror("pthread_create");
478 goto metadata_timer_error
;
481 ret
= pthread_detach(metadata_timer_thread
);
484 perror("pthread_detach");
487 metadata_timer_error
:
488 ret
= pthread_join(sessiond_thread
, &status
);
490 perror("pthread_join");
495 ret
= pthread_join(data_thread
, &status
);
497 perror("pthread_join");
502 ret
= pthread_join(metadata_thread
, &status
);
504 perror("pthread_join");
509 ret
= pthread_join(channel_thread
, &status
);
511 perror("pthread_join");
516 ret
= pthread_join(health_thread
, &status
);
518 PERROR("pthread_join health thread");
519 goto error
; /* join error, exit without cleanup */
523 utils_close_pipe(health_quit_pipe
);
537 cmm_barrier(); /* Clear ctx for signal handler. */
538 lttng_consumer_destroy(tmp_ctx
);
539 lttng_consumer_cleanup();
540 if (health_consumerd
) {
541 health_app_destroy(health_consumerd
);
544 /* Ensure all prior call_rcu are done. */