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