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