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