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