Fix: consumerd pthread error flow
[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/compat/poll.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49
50 #include "lttng-consumerd.h"
51
52 /* TODO : support UST (all direct kernel-ctl accesses). */
53
54 /* the two threads (receive fd, poll and metadata) */
55 static pthread_t data_thread, metadata_thread, sessiond_thread;
56
57 /* to count the number of times the user pressed ctrl+c */
58 static int sigintcount = 0;
59
60 /* Argument variables */
61 int lttng_opt_quiet; /* not static in error.h */
62 int lttng_opt_verbose; /* not static in error.h */
63 static int opt_daemon;
64 static const char *progname;
65 static char command_sock_path[PATH_MAX]; /* Global command socket path */
66 static char error_sock_path[PATH_MAX]; /* Global error path */
67 static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
68
69 /* the liblttngconsumerd context */
70 static struct lttng_consumer_local_data *ctx;
71
72 /*
73 * Signal handler for the daemon
74 */
75 static void sighandler(int sig)
76 {
77 if (sig == SIGINT && sigintcount++ == 0) {
78 DBG("ignoring first SIGINT");
79 return;
80 }
81
82 lttng_consumer_should_exit(ctx);
83 }
84
85 /*
86 * Setup signal handler for :
87 * SIGINT, SIGTERM, SIGPIPE
88 */
89 static int set_signal_handler(void)
90 {
91 int ret = 0;
92 struct sigaction sa;
93 sigset_t sigset;
94
95 if ((ret = sigemptyset(&sigset)) < 0) {
96 perror("sigemptyset");
97 return ret;
98 }
99
100 sa.sa_handler = sighandler;
101 sa.sa_mask = sigset;
102 sa.sa_flags = 0;
103 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
104 perror("sigaction");
105 return ret;
106 }
107
108 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
109 perror("sigaction");
110 return ret;
111 }
112
113 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
114 perror("sigaction");
115 return ret;
116 }
117
118 return ret;
119 }
120
121 /*
122 * Usage function on stream file.
123 */
124 static void usage(FILE *fp)
125 {
126 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
127 fprintf(fp, " -h, --help "
128 "Display this usage.\n");
129 fprintf(fp, " -c, --consumerd-cmd-sock PATH "
130 "Specify path for the command socket\n");
131 fprintf(fp, " -e, --consumerd-err-sock PATH "
132 "Specify path for the error socket\n");
133 fprintf(fp, " -d, --daemonize "
134 "Start as a daemon.\n");
135 fprintf(fp, " -q, --quiet "
136 "No output at all.\n");
137 fprintf(fp, " -v, --verbose "
138 "Verbose mode. Activate DBG() macro.\n");
139 fprintf(fp, " -V, --version "
140 "Show version number.\n");
141 fprintf(fp, " -k, --kernel "
142 "Consumer kernel buffers (default).\n");
143 fprintf(fp, " -u, --ust "
144 "Consumer UST buffers.%s\n",
145 #ifdef HAVE_LIBLTTNG_UST_CTL
146 ""
147 #else
148 " (support not compiled in)"
149 #endif
150 );
151 }
152
153 /*
154 * daemon argument parsing
155 */
156 static void parse_args(int argc, char **argv)
157 {
158 int c;
159
160 static struct option long_options[] = {
161 { "consumerd-cmd-sock", 1, 0, 'c' },
162 { "consumerd-err-sock", 1, 0, 'e' },
163 { "daemonize", 0, 0, 'd' },
164 { "help", 0, 0, 'h' },
165 { "quiet", 0, 0, 'q' },
166 { "verbose", 0, 0, 'v' },
167 { "version", 0, 0, 'V' },
168 { "kernel", 0, 0, 'k' },
169 #ifdef HAVE_LIBLTTNG_UST_CTL
170 { "ust", 0, 0, 'u' },
171 #endif
172 { NULL, 0, 0, 0 }
173 };
174
175 while (1) {
176 int option_index = 0;
177 c = getopt_long(argc, argv, "dhqvVku" "c:e:", long_options, &option_index);
178 if (c == -1) {
179 break;
180 }
181
182 switch (c) {
183 case 0:
184 fprintf(stderr, "option %s", long_options[option_index].name);
185 if (optarg) {
186 fprintf(stderr, " with arg %s\n", optarg);
187 }
188 break;
189 case 'c':
190 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
191 break;
192 case 'e':
193 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
194 break;
195 case 'd':
196 opt_daemon = 1;
197 break;
198 case 'h':
199 usage(stdout);
200 exit(EXIT_SUCCESS);
201 case 'q':
202 lttng_opt_quiet = 1;
203 break;
204 case 'v':
205 lttng_opt_verbose = 1;
206 break;
207 case 'V':
208 fprintf(stdout, "%s\n", VERSION);
209 exit(EXIT_SUCCESS);
210 case 'k':
211 opt_type = LTTNG_CONSUMER_KERNEL;
212 break;
213 #ifdef HAVE_LIBLTTNG_UST_CTL
214 case 'u':
215 # if (CAA_BITS_PER_LONG == 64)
216 opt_type = LTTNG_CONSUMER64_UST;
217 # elif (CAA_BITS_PER_LONG == 32)
218 opt_type = LTTNG_CONSUMER32_UST;
219 # else
220 # error "Unknown bitness"
221 # endif
222 break;
223 #endif
224 default:
225 usage(stderr);
226 exit(EXIT_FAILURE);
227 }
228 }
229 }
230
231 /*
232 * Set open files limit to unlimited. This daemon can open a large number of
233 * file descriptors in order to consumer multiple kernel traces.
234 */
235 static void set_ulimit(void)
236 {
237 int ret;
238 struct rlimit lim;
239
240 /* The kernel does not allowed an infinite limit for open files */
241 lim.rlim_cur = 65535;
242 lim.rlim_max = 65535;
243
244 ret = setrlimit(RLIMIT_NOFILE, &lim);
245 if (ret < 0) {
246 PERROR("failed to set open files limit");
247 }
248 }
249
250 /*
251 * main
252 */
253 int main(int argc, char **argv)
254 {
255 int ret = 0;
256 void *status;
257
258 /* Parse arguments */
259 progname = argv[0];
260 parse_args(argc, argv);
261
262 /* Daemonize */
263 if (opt_daemon) {
264 int i;
265
266 /*
267 * fork
268 * child: setsid, close FD 0, 1, 2, chdir /
269 * parent: exit (if fork is successful)
270 */
271 ret = daemon(0, 0);
272 if (ret < 0) {
273 PERROR("daemon");
274 goto error;
275 }
276 /*
277 * We are in the child. Make sure all other file
278 * descriptors are closed, in case we are called with
279 * more opened file descriptors than the standard ones.
280 */
281 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
282 (void) close(i);
283 }
284 }
285
286 /* Set up max poll set size */
287 lttng_poll_set_max_size();
288
289 if (strlen(command_sock_path) == 0) {
290 switch (opt_type) {
291 case LTTNG_CONSUMER_KERNEL:
292 snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
293 DEFAULT_LTTNG_RUNDIR);
294 break;
295 case LTTNG_CONSUMER64_UST:
296 snprintf(command_sock_path, PATH_MAX,
297 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
298 break;
299 case LTTNG_CONSUMER32_UST:
300 snprintf(command_sock_path, PATH_MAX,
301 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
302 break;
303 default:
304 WARN("Unknown consumerd type");
305 goto error;
306 }
307 }
308
309 /* Init */
310 lttng_consumer_init();
311
312 if (!getuid()) {
313 /* Set limit for open files */
314 set_ulimit();
315 }
316
317 /* create the consumer instance with and assign the callbacks */
318 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
319 NULL, lttng_consumer_on_recv_stream, NULL);
320 if (ctx == NULL) {
321 goto error;
322 }
323
324 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
325 if (strlen(error_sock_path) == 0) {
326 switch (opt_type) {
327 case LTTNG_CONSUMER_KERNEL:
328 snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
329 DEFAULT_LTTNG_RUNDIR);
330 break;
331 case LTTNG_CONSUMER64_UST:
332 snprintf(error_sock_path, PATH_MAX,
333 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
334 break;
335 case LTTNG_CONSUMER32_UST:
336 snprintf(error_sock_path, PATH_MAX,
337 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
338 break;
339 default:
340 WARN("Unknown consumerd type");
341 goto error;
342 }
343 }
344
345 if (set_signal_handler() < 0) {
346 goto error;
347 }
348
349 /* Connect to the socket created by lttng-sessiond to report errors */
350 DBG("Connecting to error socket %s", error_sock_path);
351 ret = lttcomm_connect_unix_sock(error_sock_path);
352 /* not a fatal error, but all communication with lttng-sessiond will fail */
353 if (ret < 0) {
354 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
355 }
356 lttng_consumer_set_error_sock(ctx, ret);
357
358 /* Create thread to manage the polling/writing of trace metadata */
359 ret = pthread_create(&metadata_thread, NULL, consumer_thread_metadata_poll,
360 (void *) ctx);
361 if (ret != 0) {
362 perror("pthread_create");
363 goto error;
364 }
365
366 /* Create thread to manage the polling/writing of trace data */
367 ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll,
368 (void *) ctx);
369 if (ret != 0) {
370 perror("pthread_create");
371 goto data_error;
372 }
373
374 /* Create the thread to manage the receive of fd */
375 ret = pthread_create(&sessiond_thread, NULL, consumer_thread_sessiond_poll,
376 (void *) ctx);
377 if (ret != 0) {
378 perror("pthread_create");
379 goto sessiond_error;
380 }
381
382 ret = pthread_join(sessiond_thread, &status);
383 if (ret != 0) {
384 perror("pthread_join");
385 goto error;
386 }
387
388 sessiond_error:
389 ret = pthread_join(data_thread, &status);
390 if (ret != 0) {
391 perror("pthread_join");
392 goto error;
393 }
394
395 data_error:
396 ret = pthread_join(metadata_thread, &status);
397 if (ret != 0) {
398 perror("pthread_join");
399 goto error;
400 }
401
402 if (!ret) {
403 ret = EXIT_SUCCESS;
404 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_EXIT_SUCCESS);
405 goto end;
406 }
407
408 error:
409 ret = EXIT_FAILURE;
410 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_EXIT_FAILURE);
411
412 end:
413 lttng_consumer_destroy(ctx);
414 lttng_consumer_cleanup();
415
416 return ret;
417 }
This page took 0.038013 seconds and 4 git commands to generate.