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/compat/poll.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
50 #include "lttng-consumerd.h"
52 /* TODO : support UST (all direct kernel-ctl accesses). */
54 /* the two threads (receive fd, poll and metadata) */
55 static pthread_t data_thread
, metadata_thread
, sessiond_thread
;
57 /* to count the number of times the user pressed ctrl+c */
58 static int sigintcount
= 0;
60 /* Argument variables */
61 int lttng_opt_quiet
; /* not static in error.h */
62 int lttng_opt_verbose
; /* not static in error.h */
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");
83 * Ignore SIGPIPE because it should not stop the consumer whenever a
84 * SIGPIPE is catched through a FD operation.
90 lttng_consumer_should_exit(ctx
);
94 * Setup signal handler for :
95 * SIGINT, SIGTERM, SIGPIPE
97 static int set_signal_handler(void)
103 if ((ret
= sigemptyset(&sigset
)) < 0) {
104 perror("sigemptyset");
108 sa
.sa_handler
= sighandler
;
111 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
116 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
121 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
130 * Usage function on stream file.
132 static void usage(FILE *fp
)
134 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
135 fprintf(fp
, " -h, --help "
136 "Display this usage.\n");
137 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
138 "Specify path for the command socket\n");
139 fprintf(fp
, " -e, --consumerd-err-sock PATH "
140 "Specify path for the error socket\n");
141 fprintf(fp
, " -d, --daemonize "
142 "Start as a daemon.\n");
143 fprintf(fp
, " -q, --quiet "
144 "No output at all.\n");
145 fprintf(fp
, " -v, --verbose "
146 "Verbose mode. Activate DBG() macro.\n");
147 fprintf(fp
, " -V, --version "
148 "Show version number.\n");
149 fprintf(fp
, " -k, --kernel "
150 "Consumer kernel buffers (default).\n");
151 fprintf(fp
, " -u, --ust "
152 "Consumer UST buffers.%s\n",
153 #ifdef HAVE_LIBLTTNG_UST_CTL
156 " (support not compiled in)"
162 * daemon argument parsing
164 static void parse_args(int argc
, char **argv
)
168 static struct option long_options
[] = {
169 { "consumerd-cmd-sock", 1, 0, 'c' },
170 { "consumerd-err-sock", 1, 0, 'e' },
171 { "daemonize", 0, 0, 'd' },
172 { "help", 0, 0, 'h' },
173 { "quiet", 0, 0, 'q' },
174 { "verbose", 0, 0, 'v' },
175 { "version", 0, 0, 'V' },
176 { "kernel", 0, 0, 'k' },
177 #ifdef HAVE_LIBLTTNG_UST_CTL
178 { "ust", 0, 0, 'u' },
184 int option_index
= 0;
185 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:", long_options
, &option_index
);
192 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
194 fprintf(stderr
, " with arg %s\n", optarg
);
198 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
201 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
213 lttng_opt_verbose
= 1;
216 fprintf(stdout
, "%s\n", VERSION
);
219 opt_type
= LTTNG_CONSUMER_KERNEL
;
221 #ifdef HAVE_LIBLTTNG_UST_CTL
223 # if (CAA_BITS_PER_LONG == 64)
224 opt_type
= LTTNG_CONSUMER64_UST
;
225 # elif (CAA_BITS_PER_LONG == 32)
226 opt_type
= LTTNG_CONSUMER32_UST
;
228 # error "Unknown bitness"
240 * Set open files limit to unlimited. This daemon can open a large number of
241 * file descriptors in order to consumer multiple kernel traces.
243 static void set_ulimit(void)
248 /* The kernel does not allowed an infinite limit for open files */
249 lim
.rlim_cur
= 65535;
250 lim
.rlim_max
= 65535;
252 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
254 PERROR("failed to set open files limit");
261 int main(int argc
, char **argv
)
266 /* Parse arguments */
268 parse_args(argc
, argv
);
276 * child: setsid, close FD 0, 1, 2, chdir /
277 * parent: exit (if fork is successful)
285 * We are in the child. Make sure all other file
286 * descriptors are closed, in case we are called with
287 * more opened file descriptors than the standard ones.
289 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
294 /* Set up max poll set size */
295 lttng_poll_set_max_size();
297 if (*command_sock_path
== '\0') {
299 case LTTNG_CONSUMER_KERNEL
:
300 snprintf(command_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
301 DEFAULT_LTTNG_RUNDIR
);
303 case LTTNG_CONSUMER64_UST
:
304 snprintf(command_sock_path
, PATH_MAX
,
305 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
307 case LTTNG_CONSUMER32_UST
:
308 snprintf(command_sock_path
, PATH_MAX
,
309 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
312 WARN("Unknown consumerd type");
318 lttng_consumer_init();
321 /* Set limit for open files */
325 /* create the consumer instance with and assign the callbacks */
326 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
327 NULL
, lttng_consumer_on_recv_stream
, NULL
);
332 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
333 if (*error_sock_path
== '\0') {
335 case LTTNG_CONSUMER_KERNEL
:
336 snprintf(error_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
337 DEFAULT_LTTNG_RUNDIR
);
339 case LTTNG_CONSUMER64_UST
:
340 snprintf(error_sock_path
, PATH_MAX
,
341 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
343 case LTTNG_CONSUMER32_UST
:
344 snprintf(error_sock_path
, PATH_MAX
,
345 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
348 WARN("Unknown consumerd type");
353 if (set_signal_handler() < 0) {
357 /* Connect to the socket created by lttng-sessiond to report errors */
358 DBG("Connecting to error socket %s", error_sock_path
);
359 ret
= lttcomm_connect_unix_sock(error_sock_path
);
360 /* not a fatal error, but all communication with lttng-sessiond will fail */
362 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
364 lttng_consumer_set_error_sock(ctx
, ret
);
366 /* Create thread to manage the polling/writing of trace metadata */
367 ret
= pthread_create(&metadata_thread
, NULL
, consumer_thread_metadata_poll
,
370 perror("pthread_create");
374 /* Create thread to manage the polling/writing of trace data */
375 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
378 perror("pthread_create");
382 /* Create the thread to manage the receive of fd */
383 ret
= pthread_create(&sessiond_thread
, NULL
, consumer_thread_sessiond_poll
,
386 perror("pthread_create");
390 ret
= pthread_join(sessiond_thread
, &status
);
392 perror("pthread_join");
397 ret
= pthread_join(data_thread
, &status
);
399 perror("pthread_join");
404 ret
= pthread_join(metadata_thread
, &status
);
406 perror("pthread_join");
412 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_SUCCESS
);
418 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_FAILURE
);
421 lttng_consumer_destroy(ctx
);
422 lttng_consumer_cleanup();