Commit | Line | Data |
---|---|---|
d4a1283e JD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
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. | |
d4a1283e JD |
8 | * |
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. | |
13 | * | |
d14d33bf AM |
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. | |
d4a1283e JD |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <fcntl.h> | |
21 | #include <getopt.h> | |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
24 | #include <pthread.h> | |
25 | #include <signal.h> | |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <sys/ipc.h> | |
1ccfc0e3 | 30 | #include <sys/resource.h> |
d4a1283e JD |
31 | #include <sys/shm.h> |
32 | #include <sys/socket.h> | |
33 | #include <sys/stat.h> | |
34 | #include <sys/types.h> | |
35 | #include <urcu/list.h> | |
36 | #include <poll.h> | |
37 | #include <unistd.h> | |
03424a9b | 38 | #include <sys/mman.h> |
5348b470 | 39 | #include <assert.h> |
3bd1e081 | 40 | #include <config.h> |
7753dea8 | 41 | #include <urcu/compiler.h> |
1ccfc0e3 | 42 | #include <ulimit.h> |
d4a1283e | 43 | |
db758600 DG |
44 | #include <common/defaults.h> |
45 | #include <common/common.h> | |
f02e1e8a | 46 | #include <common/consumer.h> |
331744e3 | 47 | #include <common/consumer-timer.h> |
fb3a43a9 | 48 | #include <common/compat/poll.h> |
10a8a223 | 49 | #include <common/sessiond-comm/sessiond-comm.h> |
10a8a223 DG |
50 | |
51 | #include "lttng-consumerd.h" | |
d4a1283e | 52 | |
99bab54f | 53 | /* TODO : support UST (all direct kernel-ctl accesses). */ |
3bd1e081 | 54 | |
d8ef542d | 55 | /* threads (channel handling, poll, metadata, sessiond) */ |
331744e3 | 56 | |
d8ef542d | 57 | static pthread_t channel_thread, data_thread, metadata_thread, sessiond_thread; |
331744e3 | 58 | static pthread_t metadata_timer_thread; |
d4a1283e | 59 | |
3183dbb0 | 60 | /* to count the number of times the user pressed ctrl+c */ |
13e44745 JD |
61 | static int sigintcount = 0; |
62 | ||
d4a1283e | 63 | /* Argument variables */ |
97e19046 DG |
64 | int lttng_opt_quiet; /* not static in error.h */ |
65 | int lttng_opt_verbose; /* not static in error.h */ | |
d4a1283e JD |
66 | static int opt_daemon; |
67 | static const char *progname; | |
6533b585 DG |
68 | static char command_sock_path[PATH_MAX]; /* Global command socket path */ |
69 | static char error_sock_path[PATH_MAX]; /* Global error path */ | |
3bd1e081 | 70 | static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL; |
d4a1283e | 71 | |
7753dea8 | 72 | /* the liblttngconsumerd context */ |
3bd1e081 | 73 | static struct lttng_consumer_local_data *ctx; |
cb040cc1 | 74 | |
d4a1283e | 75 | /* |
6533b585 | 76 | * Signal handler for the daemon |
d4a1283e JD |
77 | */ |
78 | static void sighandler(int sig) | |
79 | { | |
13e44745 JD |
80 | if (sig == SIGINT && sigintcount++ == 0) { |
81 | DBG("ignoring first SIGINT"); | |
82 | return; | |
83 | } | |
84 | ||
ab1027f4 DG |
85 | /* |
86 | * Ignore SIGPIPE because it should not stop the consumer whenever a | |
87 | * SIGPIPE is catched through a FD operation. | |
88 | */ | |
89 | if (sig == SIGPIPE) { | |
90 | return; | |
91 | } | |
92 | ||
3bd1e081 | 93 | lttng_consumer_should_exit(ctx); |
d4a1283e JD |
94 | } |
95 | ||
96 | /* | |
6533b585 | 97 | * Setup signal handler for : |
d4a1283e JD |
98 | * SIGINT, SIGTERM, SIGPIPE |
99 | */ | |
100 | static int set_signal_handler(void) | |
101 | { | |
102 | int ret = 0; | |
103 | struct sigaction sa; | |
104 | sigset_t sigset; | |
105 | ||
106 | if ((ret = sigemptyset(&sigset)) < 0) { | |
107 | perror("sigemptyset"); | |
108 | return ret; | |
109 | } | |
110 | ||
111 | sa.sa_handler = sighandler; | |
112 | sa.sa_mask = sigset; | |
113 | sa.sa_flags = 0; | |
114 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
115 | perror("sigaction"); | |
116 | return ret; | |
117 | } | |
118 | ||
119 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
120 | perror("sigaction"); | |
121 | return ret; | |
122 | } | |
123 | ||
124 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
125 | perror("sigaction"); | |
126 | return ret; | |
127 | } | |
128 | ||
129 | return ret; | |
130 | } | |
131 | ||
d4a1283e | 132 | /* |
3183dbb0 | 133 | * Usage function on stream file. |
d4a1283e | 134 | */ |
3183dbb0 | 135 | static void usage(FILE *fp) |
d4a1283e | 136 | { |
3183dbb0 DG |
137 | fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
138 | fprintf(fp, " -h, --help " | |
d4a1283e | 139 | "Display this usage.\n"); |
3183dbb0 | 140 | fprintf(fp, " -c, --consumerd-cmd-sock PATH " |
d4a1283e | 141 | "Specify path for the command socket\n"); |
3183dbb0 | 142 | fprintf(fp, " -e, --consumerd-err-sock PATH " |
d4a1283e | 143 | "Specify path for the error socket\n"); |
3183dbb0 | 144 | fprintf(fp, " -d, --daemonize " |
d4a1283e | 145 | "Start as a daemon.\n"); |
3183dbb0 | 146 | fprintf(fp, " -q, --quiet " |
d4a1283e | 147 | "No output at all.\n"); |
3183dbb0 | 148 | fprintf(fp, " -v, --verbose " |
d4a1283e | 149 | "Verbose mode. Activate DBG() macro.\n"); |
3183dbb0 | 150 | fprintf(fp, " -V, --version " |
d4a1283e | 151 | "Show version number.\n"); |
3183dbb0 | 152 | fprintf(fp, " -k, --kernel " |
3bd1e081 | 153 | "Consumer kernel buffers (default).\n"); |
3183dbb0 | 154 | fprintf(fp, " -u, --ust " |
3bd1e081 | 155 | "Consumer UST buffers.%s\n", |
74d0b642 | 156 | #ifdef HAVE_LIBLTTNG_UST_CTL |
3bd1e081 MD |
157 | "" |
158 | #else | |
159 | " (support not compiled in)" | |
160 | #endif | |
161 | ); | |
d4a1283e JD |
162 | } |
163 | ||
164 | /* | |
165 | * daemon argument parsing | |
166 | */ | |
167 | static void parse_args(int argc, char **argv) | |
168 | { | |
169 | int c; | |
170 | ||
171 | static struct option long_options[] = { | |
7753dea8 MD |
172 | { "consumerd-cmd-sock", 1, 0, 'c' }, |
173 | { "consumerd-err-sock", 1, 0, 'e' }, | |
d4a1283e JD |
174 | { "daemonize", 0, 0, 'd' }, |
175 | { "help", 0, 0, 'h' }, | |
176 | { "quiet", 0, 0, 'q' }, | |
177 | { "verbose", 0, 0, 'v' }, | |
178 | { "version", 0, 0, 'V' }, | |
3bd1e081 | 179 | { "kernel", 0, 0, 'k' }, |
74d0b642 | 180 | #ifdef HAVE_LIBLTTNG_UST_CTL |
3bd1e081 MD |
181 | { "ust", 0, 0, 'u' }, |
182 | #endif | |
d4a1283e JD |
183 | { NULL, 0, 0, 0 } |
184 | }; | |
185 | ||
186 | while (1) { | |
187 | int option_index = 0; | |
3bd1e081 | 188 | c = getopt_long(argc, argv, "dhqvVku" "c:e:", long_options, &option_index); |
d4a1283e JD |
189 | if (c == -1) { |
190 | break; | |
191 | } | |
192 | ||
193 | switch (c) { | |
914a571b JD |
194 | case 0: |
195 | fprintf(stderr, "option %s", long_options[option_index].name); | |
196 | if (optarg) { | |
197 | fprintf(stderr, " with arg %s\n", optarg); | |
198 | } | |
199 | break; | |
200 | case 'c': | |
201 | snprintf(command_sock_path, PATH_MAX, "%s", optarg); | |
202 | break; | |
203 | case 'e': | |
204 | snprintf(error_sock_path, PATH_MAX, "%s", optarg); | |
205 | break; | |
206 | case 'd': | |
207 | opt_daemon = 1; | |
208 | break; | |
209 | case 'h': | |
3183dbb0 DG |
210 | usage(stdout); |
211 | exit(EXIT_SUCCESS); | |
914a571b | 212 | case 'q': |
97e19046 | 213 | lttng_opt_quiet = 1; |
914a571b JD |
214 | break; |
215 | case 'v': | |
97e19046 | 216 | lttng_opt_verbose = 1; |
914a571b JD |
217 | break; |
218 | case 'V': | |
219 | fprintf(stdout, "%s\n", VERSION); | |
220 | exit(EXIT_SUCCESS); | |
3bd1e081 MD |
221 | case 'k': |
222 | opt_type = LTTNG_CONSUMER_KERNEL; | |
223 | break; | |
74d0b642 | 224 | #ifdef HAVE_LIBLTTNG_UST_CTL |
3bd1e081 | 225 | case 'u': |
7753dea8 MD |
226 | # if (CAA_BITS_PER_LONG == 64) |
227 | opt_type = LTTNG_CONSUMER64_UST; | |
228 | # elif (CAA_BITS_PER_LONG == 32) | |
229 | opt_type = LTTNG_CONSUMER32_UST; | |
230 | # else | |
231 | # error "Unknown bitness" | |
232 | # endif | |
3bd1e081 MD |
233 | break; |
234 | #endif | |
914a571b | 235 | default: |
3183dbb0 | 236 | usage(stderr); |
914a571b | 237 | exit(EXIT_FAILURE); |
d4a1283e JD |
238 | } |
239 | } | |
240 | } | |
241 | ||
1ccfc0e3 DG |
242 | /* |
243 | * Set open files limit to unlimited. This daemon can open a large number of | |
244 | * file descriptors in order to consumer multiple kernel traces. | |
245 | */ | |
246 | static void set_ulimit(void) | |
247 | { | |
248 | int ret; | |
249 | struct rlimit lim; | |
250 | ||
251 | /* The kernel does not allowed an infinite limit for open files */ | |
252 | lim.rlim_cur = 65535; | |
253 | lim.rlim_max = 65535; | |
254 | ||
255 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
256 | if (ret < 0) { | |
257 | PERROR("failed to set open files limit"); | |
258 | } | |
259 | } | |
260 | ||
d4a1283e JD |
261 | /* |
262 | * main | |
263 | */ | |
264 | int main(int argc, char **argv) | |
265 | { | |
d4a1283e JD |
266 | int ret = 0; |
267 | void *status; | |
268 | ||
269 | /* Parse arguments */ | |
270 | progname = argv[0]; | |
271 | parse_args(argc, argv); | |
272 | ||
273 | /* Daemonize */ | |
274 | if (opt_daemon) { | |
ceed52b5 MD |
275 | int i; |
276 | ||
277 | /* | |
278 | * fork | |
279 | * child: setsid, close FD 0, 1, 2, chdir / | |
280 | * parent: exit (if fork is successful) | |
281 | */ | |
d4a1283e JD |
282 | ret = daemon(0, 0); |
283 | if (ret < 0) { | |
ceed52b5 | 284 | PERROR("daemon"); |
d4a1283e JD |
285 | goto error; |
286 | } | |
ceed52b5 MD |
287 | /* |
288 | * We are in the child. Make sure all other file | |
289 | * descriptors are closed, in case we are called with | |
290 | * more opened file descriptors than the standard ones. | |
291 | */ | |
292 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
293 | (void) close(i); | |
294 | } | |
d4a1283e JD |
295 | } |
296 | ||
fb3a43a9 DG |
297 | /* Set up max poll set size */ |
298 | lttng_poll_set_max_size(); | |
299 | ||
9d035200 | 300 | if (*command_sock_path == '\0') { |
7753dea8 MD |
301 | switch (opt_type) { |
302 | case LTTNG_CONSUMER_KERNEL: | |
60922cb0 | 303 | snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH, |
990570ed | 304 | DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
305 | break; |
306 | case LTTNG_CONSUMER64_UST: | |
67e40797 | 307 | snprintf(command_sock_path, PATH_MAX, |
60922cb0 | 308 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
309 | break; |
310 | case LTTNG_CONSUMER32_UST: | |
67e40797 | 311 | snprintf(command_sock_path, PATH_MAX, |
60922cb0 | 312 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
313 | break; |
314 | default: | |
315 | WARN("Unknown consumerd type"); | |
316 | goto error; | |
317 | } | |
d4a1283e | 318 | } |
e4421fec DG |
319 | |
320 | /* Init */ | |
ab62752a MD |
321 | if (lttng_consumer_init() < 0) { |
322 | goto error; | |
323 | } | |
324 | ||
30aee250 MD |
325 | /* Init socket timeouts */ |
326 | lttcomm_init(); | |
327 | lttcomm_inet_init(); | |
e4421fec | 328 | |
1ccfc0e3 DG |
329 | if (!getuid()) { |
330 | /* Set limit for open files */ | |
331 | set_ulimit(); | |
332 | } | |
333 | ||
5348b470 | 334 | /* create the consumer instance with and assign the callbacks */ |
d41f73b7 MD |
335 | ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer, |
336 | NULL, lttng_consumer_on_recv_stream, NULL); | |
cb040cc1 JD |
337 | if (ctx == NULL) { |
338 | goto error; | |
339 | } | |
340 | ||
3bd1e081 | 341 | lttng_consumer_set_command_sock_path(ctx, command_sock_path); |
9d035200 | 342 | if (*error_sock_path == '\0') { |
7753dea8 MD |
343 | switch (opt_type) { |
344 | case LTTNG_CONSUMER_KERNEL: | |
60922cb0 | 345 | snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
990570ed | 346 | DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
347 | break; |
348 | case LTTNG_CONSUMER64_UST: | |
67e40797 | 349 | snprintf(error_sock_path, PATH_MAX, |
60922cb0 | 350 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
351 | break; |
352 | case LTTNG_CONSUMER32_UST: | |
67e40797 | 353 | snprintf(error_sock_path, PATH_MAX, |
60922cb0 | 354 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
355 | break; |
356 | default: | |
357 | WARN("Unknown consumerd type"); | |
358 | goto error; | |
359 | } | |
d4a1283e JD |
360 | } |
361 | ||
362 | if (set_signal_handler() < 0) { | |
363 | goto error; | |
364 | } | |
365 | ||
32258573 | 366 | /* Connect to the socket created by lttng-sessiond to report errors */ |
d4a1283e | 367 | DBG("Connecting to error socket %s", error_sock_path); |
1ce86c9a | 368 | ret = lttcomm_connect_unix_sock(error_sock_path); |
32258573 | 369 | /* not a fatal error, but all communication with lttng-sessiond will fail */ |
1ce86c9a | 370 | if (ret < 0) { |
99bab54f | 371 | WARN("Cannot connect to error socket (is lttng-sessiond started?)"); |
d4a1283e | 372 | } |
3bd1e081 | 373 | lttng_consumer_set_error_sock(ctx, ret); |
d4a1283e | 374 | |
331744e3 JD |
375 | /* |
376 | * For UST consumer, we block RT signals used for periodical metadata flush | |
377 | * in main and create a dedicated thread to handle these signals. | |
378 | */ | |
379 | switch (opt_type) { | |
380 | case LTTNG_CONSUMER32_UST: | |
381 | case LTTNG_CONSUMER64_UST: | |
382 | consumer_signal_init(); | |
383 | break; | |
384 | default: | |
385 | break; | |
386 | } | |
387 | ctx->type = opt_type; | |
388 | ||
554831e7 MD |
389 | /* Initialize communication library */ |
390 | lttcomm_init(); | |
391 | ||
d8ef542d MD |
392 | /* Create thread to manage channels */ |
393 | ret = pthread_create(&channel_thread, NULL, consumer_thread_channel_poll, | |
394 | (void *) ctx); | |
395 | if (ret != 0) { | |
396 | perror("pthread_create"); | |
397 | goto error; | |
398 | } | |
399 | ||
7d980def | 400 | /* Create thread to manage the polling/writing of trace metadata */ |
df27ef7b | 401 | ret = pthread_create(&metadata_thread, NULL, consumer_thread_metadata_poll, |
7d980def DG |
402 | (void *) ctx); |
403 | if (ret != 0) { | |
404 | perror("pthread_create"); | |
d8ef542d | 405 | goto metadata_error; |
7d980def DG |
406 | } |
407 | ||
408 | /* Create thread to manage the polling/writing of trace data */ | |
df27ef7b | 409 | ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll, |
cb040cc1 | 410 | (void *) ctx); |
d4a1283e JD |
411 | if (ret != 0) { |
412 | perror("pthread_create"); | |
df27ef7b | 413 | goto data_error; |
d4a1283e JD |
414 | } |
415 | ||
7d980def | 416 | /* Create the thread to manage the receive of fd */ |
df27ef7b | 417 | ret = pthread_create(&sessiond_thread, NULL, consumer_thread_sessiond_poll, |
cb040cc1 | 418 | (void *) ctx); |
d4a1283e JD |
419 | if (ret != 0) { |
420 | perror("pthread_create"); | |
df27ef7b DG |
421 | goto sessiond_error; |
422 | } | |
423 | ||
331744e3 JD |
424 | switch (opt_type) { |
425 | case LTTNG_CONSUMER32_UST: | |
426 | case LTTNG_CONSUMER64_UST: | |
427 | /* Create the thread to manage the metadata periodic timers */ | |
428 | ret = pthread_create(&metadata_timer_thread, NULL, | |
429 | consumer_timer_metadata_thread, (void *) ctx); | |
430 | if (ret != 0) { | |
431 | perror("pthread_create"); | |
432 | goto metadata_timer_error; | |
433 | } | |
434 | ||
435 | ret = pthread_detach(metadata_timer_thread); | |
436 | if (ret) { | |
437 | errno = ret; | |
438 | perror("pthread_detach"); | |
439 | } | |
440 | break; | |
441 | default: | |
442 | break; | |
443 | } | |
444 | ||
445 | metadata_timer_error: | |
df27ef7b DG |
446 | ret = pthread_join(sessiond_thread, &status); |
447 | if (ret != 0) { | |
448 | perror("pthread_join"); | |
d4a1283e JD |
449 | goto error; |
450 | } | |
451 | ||
df27ef7b DG |
452 | sessiond_error: |
453 | ret = pthread_join(data_thread, &status); | |
454 | if (ret != 0) { | |
455 | perror("pthread_join"); | |
456 | goto error; | |
457 | } | |
458 | ||
459 | data_error: | |
460 | ret = pthread_join(metadata_thread, &status); | |
461 | if (ret != 0) { | |
462 | perror("pthread_join"); | |
463 | goto error; | |
464 | } | |
465 | ||
d8ef542d MD |
466 | metadata_error: |
467 | ret = pthread_join(channel_thread, &status); | |
468 | if (ret != 0) { | |
469 | perror("pthread_join"); | |
470 | goto error; | |
471 | } | |
472 | ||
df27ef7b DG |
473 | if (!ret) { |
474 | ret = EXIT_SUCCESS; | |
475 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_EXIT_SUCCESS); | |
476 | goto end; | |
d4a1283e | 477 | } |
d4a1283e JD |
478 | |
479 | error: | |
480 | ret = EXIT_FAILURE; | |
094d1690 DG |
481 | if (ctx) { |
482 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_EXIT_FAILURE); | |
483 | } | |
d4a1283e JD |
484 | |
485 | end: | |
3bd1e081 MD |
486 | lttng_consumer_destroy(ctx); |
487 | lttng_consumer_cleanup(); | |
d4a1283e JD |
488 | |
489 | return ret; | |
490 | } |