Use lttng api for the load command
[lttng-tools.git] / src / bin / lttng-consumerd / lttng-consumerd.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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 *
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.
17 */
18
19 #define _LGPL_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>
30 #include <sys/resource.h>
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>
38 #include <sys/mman.h>
39 #include <assert.h>
40 #include <urcu/compiler.h>
41 #include <ulimit.h>
42
43 #include <common/defaults.h>
44 #include <common/common.h>
45 #include <common/consumer/consumer.h>
46 #include <common/consumer/consumer-timer.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/getenv.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/utils.h>
51
52 #include "lttng-consumerd.h"
53 #include "health-consumerd.h"
54
55 /* threads (channel handling, poll, metadata, sessiond) */
56
57 static pthread_t channel_thread, data_thread, metadata_thread,
58 sessiond_thread, metadata_timer_thread, health_thread;
59
60 /* to count the number of times the user pressed ctrl+c */
61 static int sigintcount = 0;
62
63 /* Argument variables */
64 int lttng_opt_quiet; /* not static in error.h */
65 int lttng_opt_verbose; /* not static in error.h */
66 int lttng_opt_mi; /* not static in error.h */
67
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;
73
74 /* the liblttngconsumerd context */
75 static struct lttng_consumer_local_data *ctx;
76
77 /* Consumerd health monitoring */
78 struct health_app *health_consumerd;
79
80 const char *tracing_group_name = DEFAULT_TRACING_GROUP;
81
82 int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY;
83
84 enum lttng_consumer_type lttng_consumer_get_type(void)
85 {
86 if (!ctx) {
87 return LTTNG_CONSUMER_UNKNOWN;
88 }
89 return ctx->type;
90 }
91
92 /*
93 * Signal handler for the daemon
94 */
95 static void sighandler(int sig)
96 {
97 if (sig == SIGINT && sigintcount++ == 0) {
98 DBG("ignoring first SIGINT");
99 return;
100 }
101
102 /*
103 * Ignore SIGPIPE because it should not stop the consumer whenever a
104 * SIGPIPE is caught through a FD operation.
105 */
106 if (sig == SIGPIPE) {
107 return;
108 }
109
110 if (ctx) {
111 lttng_consumer_should_exit(ctx);
112 }
113 }
114
115 /*
116 * Setup signal handler for :
117 * SIGINT, SIGTERM, SIGPIPE
118 */
119 static int set_signal_handler(void)
120 {
121 int ret = 0;
122 struct sigaction sa;
123 sigset_t sigset;
124
125 if ((ret = sigemptyset(&sigset)) < 0) {
126 PERROR("sigemptyset");
127 return ret;
128 }
129
130 sa.sa_handler = sighandler;
131 sa.sa_mask = sigset;
132 sa.sa_flags = 0;
133 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
134 PERROR("sigaction");
135 return ret;
136 }
137
138 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
139 PERROR("sigaction");
140 return ret;
141 }
142
143 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
144 PERROR("sigaction");
145 return ret;
146 }
147
148 return ret;
149 }
150
151 /*
152 * Usage function on stream file.
153 */
154 static void usage(FILE *fp)
155 {
156 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
157 fprintf(fp, " -h, --help "
158 "Display this usage.\n");
159 fprintf(fp, " -c, --consumerd-cmd-sock PATH "
160 "Specify path for the command socket\n");
161 fprintf(fp, " -e, --consumerd-err-sock PATH "
162 "Specify path for the error socket\n");
163 fprintf(fp, " -d, --daemonize "
164 "Start as a daemon.\n");
165 fprintf(fp, " -q, --quiet "
166 "No output at all.\n");
167 fprintf(fp, " -v, --verbose "
168 "Verbose mode. Activate DBG() macro.\n");
169 fprintf(fp, " -V, --version "
170 "Show version number.\n");
171 fprintf(fp, " -g, --group NAME "
172 "Specify the tracing group name. (default: tracing)\n");
173 fprintf(fp, " -k, --kernel "
174 "Consumer kernel buffers (default).\n");
175 fprintf(fp, " -u, --ust "
176 "Consumer UST buffers.%s\n",
177 #ifdef HAVE_LIBLTTNG_UST_CTL
178 ""
179 #else
180 " (support not compiled in)"
181 #endif
182 );
183 }
184
185 /*
186 * daemon argument parsing
187 */
188 static int parse_args(int argc, char **argv)
189 {
190 int c, ret = 0;
191
192 static struct option long_options[] = {
193 { "consumerd-cmd-sock", 1, 0, 'c' },
194 { "consumerd-err-sock", 1, 0, 'e' },
195 { "daemonize", 0, 0, 'd' },
196 { "group", 1, 0, 'g' },
197 { "help", 0, 0, 'h' },
198 { "quiet", 0, 0, 'q' },
199 { "verbose", 0, 0, 'v' },
200 { "version", 0, 0, 'V' },
201 { "kernel", 0, 0, 'k' },
202 #ifdef HAVE_LIBLTTNG_UST_CTL
203 { "ust", 0, 0, 'u' },
204 #endif
205 { NULL, 0, 0, 0 }
206 };
207
208 while (1) {
209 int option_index = 0;
210 c = getopt_long(argc, argv, "dhqvVku" "c:e:g:",
211 long_options, &option_index);
212 if (c == -1) {
213 break;
214 }
215
216 switch (c) {
217 case 0:
218 fprintf(stderr, "option %s",
219 long_options[option_index].name);
220 if (optarg) {
221 fprintf(stderr, " with arg %s\n", optarg);
222 ret = -1;
223 goto end;
224 }
225 break;
226 case 'c':
227 if (lttng_is_setuid_setgid()) {
228 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
229 "-c, --consumerd-cmd-sock");
230 } else {
231 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
232 }
233 break;
234 case 'e':
235 if (lttng_is_setuid_setgid()) {
236 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
237 "-e, --consumerd-err-sock");
238 } else {
239 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
240 }
241 break;
242 case 'd':
243 opt_daemon = 1;
244 break;
245 case 'g':
246 if (lttng_is_setuid_setgid()) {
247 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
248 "-g, --group");
249 } else {
250 tracing_group_name = optarg;
251 }
252 break;
253 case 'h':
254 usage(stdout);
255 exit(EXIT_SUCCESS);
256 case 'q':
257 lttng_opt_quiet = 1;
258 break;
259 case 'v':
260 lttng_opt_verbose = 1;
261 break;
262 case 'V':
263 fprintf(stdout, "%s\n", VERSION);
264 exit(EXIT_SUCCESS);
265 case 'k':
266 opt_type = LTTNG_CONSUMER_KERNEL;
267 break;
268 #ifdef HAVE_LIBLTTNG_UST_CTL
269 case 'u':
270 # if (CAA_BITS_PER_LONG == 64)
271 opt_type = LTTNG_CONSUMER64_UST;
272 # elif (CAA_BITS_PER_LONG == 32)
273 opt_type = LTTNG_CONSUMER32_UST;
274 # else
275 # error "Unknown bitness"
276 # endif
277 break;
278 #endif
279 default:
280 usage(stderr);
281 ret = -1;
282 goto end;
283 }
284 }
285 end:
286 return ret;
287 }
288
289 /*
290 * Set open files limit to unlimited. This daemon can open a large number of
291 * file descriptors in order to consumer multiple kernel traces.
292 */
293 static void set_ulimit(void)
294 {
295 int ret;
296 struct rlimit lim;
297
298 /* The kernel does not allowed an infinite limit for open files */
299 lim.rlim_cur = 65535;
300 lim.rlim_max = 65535;
301
302 ret = setrlimit(RLIMIT_NOFILE, &lim);
303 if (ret < 0) {
304 PERROR("failed to set open files limit");
305 }
306 }
307
308 /*
309 * main
310 */
311 int main(int argc, char **argv)
312 {
313 int ret = 0, retval = 0;
314 void *status;
315 struct lttng_consumer_local_data *tmp_ctx;
316
317 if (set_signal_handler()) {
318 retval = -1;
319 goto exit_set_signal_handler;
320 }
321
322 /* Parse arguments */
323 progname = argv[0];
324 if (parse_args(argc, argv)) {
325 retval = -1;
326 goto exit_options;
327 }
328
329 /* Daemonize */
330 if (opt_daemon) {
331 int i;
332
333 /*
334 * fork
335 * child: setsid, close FD 0, 1, 2, chdir /
336 * parent: exit (if fork is successful)
337 */
338 ret = daemon(0, 0);
339 if (ret < 0) {
340 PERROR("daemon");
341 retval = -1;
342 goto exit_options;
343 }
344 /*
345 * We are in the child. Make sure all other file
346 * descriptors are closed, in case we are called with
347 * more opened file descriptors than the standard ones.
348 */
349 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
350 (void) close(i);
351 }
352 }
353
354 /*
355 * Starting from here, we can create threads. This needs to be after
356 * lttng_daemonize due to RCU.
357 */
358
359 health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
360 if (!health_consumerd) {
361 retval = -1;
362 goto exit_health_consumerd_cleanup;
363 }
364
365 if (*command_sock_path == '\0') {
366 switch (opt_type) {
367 case LTTNG_CONSUMER_KERNEL:
368 ret = snprintf(command_sock_path, PATH_MAX,
369 DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
370 DEFAULT_LTTNG_RUNDIR);
371 if (ret < 0) {
372 retval = -1;
373 goto exit_init_data;
374 }
375 break;
376 case LTTNG_CONSUMER64_UST:
377 ret = snprintf(command_sock_path, PATH_MAX,
378 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
379 DEFAULT_LTTNG_RUNDIR);
380 if (ret < 0) {
381 retval = -1;
382 goto exit_init_data;
383 }
384 break;
385 case LTTNG_CONSUMER32_UST:
386 ret = snprintf(command_sock_path, PATH_MAX,
387 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
388 DEFAULT_LTTNG_RUNDIR);
389 if (ret < 0) {
390 retval = -1;
391 goto exit_init_data;
392 }
393 break;
394 default:
395 ERR("Unknown consumerd type");
396 retval = -1;
397 goto exit_init_data;
398 }
399 }
400
401 /* Init */
402 if (lttng_consumer_init()) {
403 retval = -1;
404 goto exit_init_data;
405 }
406
407 /* Initialize communication library */
408 lttcomm_init();
409 /* Initialize TCP timeout values */
410 lttcomm_inet_init();
411
412 if (!getuid()) {
413 /* Set limit for open files */
414 set_ulimit();
415 }
416
417 if (run_as_create_worker(argv[0]) < 0) {
418 goto exit_init_data;
419 }
420
421 /* create the consumer instance with and assign the callbacks */
422 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
423 NULL, lttng_consumer_on_recv_stream, NULL);
424 if (!ctx) {
425 retval = -1;
426 goto exit_init_data;
427 }
428
429 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
430 if (*error_sock_path == '\0') {
431 switch (opt_type) {
432 case LTTNG_CONSUMER_KERNEL:
433 ret = snprintf(error_sock_path, PATH_MAX,
434 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
435 DEFAULT_LTTNG_RUNDIR);
436 if (ret < 0) {
437 retval = -1;
438 goto exit_init_data;
439 }
440 break;
441 case LTTNG_CONSUMER64_UST:
442 ret = snprintf(error_sock_path, PATH_MAX,
443 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
444 DEFAULT_LTTNG_RUNDIR);
445 if (ret < 0) {
446 retval = -1;
447 goto exit_init_data;
448 }
449 break;
450 case LTTNG_CONSUMER32_UST:
451 ret = snprintf(error_sock_path, PATH_MAX,
452 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
453 DEFAULT_LTTNG_RUNDIR);
454 if (ret < 0) {
455 retval = -1;
456 goto exit_init_data;
457 }
458 break;
459 default:
460 ERR("Unknown consumerd type");
461 retval = -1;
462 goto exit_init_data;
463 }
464 }
465
466 /* Connect to the socket created by lttng-sessiond to report errors */
467 DBG("Connecting to error socket %s", error_sock_path);
468 ret = lttcomm_connect_unix_sock(error_sock_path);
469 /*
470 * Not a fatal error, but all communication with lttng-sessiond will
471 * fail.
472 */
473 if (ret < 0) {
474 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
475 }
476 lttng_consumer_set_error_sock(ctx, ret);
477
478 /*
479 * Block RT signals used for UST periodical metadata flush and the live
480 * timer in main, and create a dedicated thread to handle these signals.
481 */
482 if (consumer_signal_init()) {
483 retval = -1;
484 goto exit_init_data;
485 }
486
487 ctx->type = opt_type;
488
489 if (utils_create_pipe(health_quit_pipe)) {
490 retval = -1;
491 goto exit_health_pipe;
492 }
493
494 /* Create thread to manage the client socket */
495 ret = pthread_create(&health_thread, default_pthread_attr(),
496 thread_manage_health, (void *) NULL);
497 if (ret) {
498 errno = ret;
499 PERROR("pthread_create health");
500 retval = -1;
501 goto exit_health_thread;
502 }
503
504 /*
505 * Wait for health thread to be initialized before letting the
506 * sessiond thread reply to the sessiond that we are ready.
507 */
508 while (uatomic_read(&lttng_consumer_ready)) {
509 usleep(100000);
510 }
511 cmm_smp_mb(); /* Read ready before following operations */
512
513 /* Create thread to manage channels */
514 ret = pthread_create(&channel_thread, default_pthread_attr(),
515 consumer_thread_channel_poll,
516 (void *) ctx);
517 if (ret) {
518 errno = ret;
519 PERROR("pthread_create");
520 retval = -1;
521 goto exit_channel_thread;
522 }
523
524 /* Create thread to manage the polling/writing of trace metadata */
525 ret = pthread_create(&metadata_thread, default_pthread_attr(),
526 consumer_thread_metadata_poll,
527 (void *) ctx);
528 if (ret) {
529 errno = ret;
530 PERROR("pthread_create");
531 retval = -1;
532 goto exit_metadata_thread;
533 }
534
535 /* Create thread to manage the polling/writing of trace data */
536 ret = pthread_create(&data_thread, default_pthread_attr(),
537 consumer_thread_data_poll, (void *) ctx);
538 if (ret) {
539 errno = ret;
540 PERROR("pthread_create");
541 retval = -1;
542 goto exit_data_thread;
543 }
544
545 /* Create the thread to manage the receive of fd */
546 ret = pthread_create(&sessiond_thread, default_pthread_attr(),
547 consumer_thread_sessiond_poll,
548 (void *) ctx);
549 if (ret) {
550 errno = ret;
551 PERROR("pthread_create");
552 retval = -1;
553 goto exit_sessiond_thread;
554 }
555
556 /*
557 * Create the thread to manage the UST metadata periodic timer and
558 * live timer.
559 */
560 ret = pthread_create(&metadata_timer_thread, default_pthread_attr(),
561 consumer_timer_thread, (void *) ctx);
562 if (ret) {
563 errno = ret;
564 PERROR("pthread_create");
565 retval = -1;
566 goto exit_metadata_timer_thread;
567 }
568
569 ret = pthread_detach(metadata_timer_thread);
570 if (ret) {
571 errno = ret;
572 PERROR("pthread_detach");
573 retval = -1;
574 goto exit_metadata_timer_detach;
575 }
576
577 /*
578 * This is where we start awaiting program completion (e.g. through
579 * signal that asks threads to teardown.
580 */
581
582 exit_metadata_timer_detach:
583 exit_metadata_timer_thread:
584 ret = pthread_join(sessiond_thread, &status);
585 if (ret) {
586 errno = ret;
587 PERROR("pthread_join sessiond_thread");
588 retval = -1;
589 }
590 exit_sessiond_thread:
591
592 ret = pthread_join(data_thread, &status);
593 if (ret) {
594 errno = ret;
595 PERROR("pthread_join data_thread");
596 retval = -1;
597 }
598 exit_data_thread:
599
600 ret = pthread_join(metadata_thread, &status);
601 if (ret) {
602 errno = ret;
603 PERROR("pthread_join metadata_thread");
604 retval = -1;
605 }
606 exit_metadata_thread:
607
608 ret = pthread_join(channel_thread, &status);
609 if (ret) {
610 errno = ret;
611 PERROR("pthread_join channel_thread");
612 retval = -1;
613 }
614 exit_channel_thread:
615
616 ret = pthread_join(health_thread, &status);
617 if (ret) {
618 errno = ret;
619 PERROR("pthread_join health_thread");
620 retval = -1;
621 }
622 exit_health_thread:
623
624 utils_close_pipe(health_quit_pipe);
625 exit_health_pipe:
626
627 exit_init_data:
628 tmp_ctx = ctx;
629 ctx = NULL;
630 cmm_barrier(); /* Clear ctx for signal handler. */
631 lttng_consumer_destroy(tmp_ctx);
632 lttng_consumer_cleanup();
633
634 if (health_consumerd) {
635 health_app_destroy(health_consumerd);
636 }
637 /* Ensure all prior call_rcu are done. */
638 rcu_barrier();
639
640 run_as_destroy_worker();
641
642 exit_health_consumerd_cleanup:
643 exit_options:
644 exit_set_signal_handler:
645
646 if (!retval) {
647 exit(EXIT_SUCCESS);
648 } else {
649 exit(EXIT_FAILURE);
650 }
651 }
This page took 0.043435 seconds and 4 git commands to generate.