Change linking libs using new UST names
[lttng-tools.git] / lttng-consumerd / lttng-consumerd.c
CommitLineData
d4a1283e
JD
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
6 * modify it under the terms of the GNU General Public License
82a3637f
DG
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
d4a1283e
JD
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define _GNU_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/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>
03424a9b 38#include <sys/mman.h>
5348b470 39#include <assert.h>
3bd1e081 40#include <config.h>
d4a1283e 41
3bd1e081 42#include <lttng-consumerd.h>
1e307fab
DG
43#include <lttng-kernel-ctl.h>
44#include <lttng-sessiond-comm.h>
3bd1e081
MD
45#include <lttng/lttng-kconsumer.h>
46#include <lttng/lttng-ustconsumer.h>
1e307fab 47#include <lttngerr.h>
d4a1283e 48
3bd1e081
MD
49/* TODO : support UST (all direct kernctl accesses). */
50
d4a1283e 51/* the two threads (receive fd and poll) */
6533b585 52static pthread_t threads[2];
d4a1283e 53
13e44745
JD
54/* to count the number of time the user pressed ctrl+c */
55static int sigintcount = 0;
56
d4a1283e
JD
57/* Argument variables */
58int opt_quiet;
59int opt_verbose;
60static int opt_daemon;
61static const char *progname;
6533b585
DG
62static char command_sock_path[PATH_MAX]; /* Global command socket path */
63static char error_sock_path[PATH_MAX]; /* Global error path */
3bd1e081 64static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
d4a1283e 65
6533b585 66/* the liblttngkconsumerd context */
3bd1e081 67static struct lttng_consumer_local_data *ctx;
cb040cc1 68
d4a1283e 69/*
6533b585 70 * Signal handler for the daemon
d4a1283e
JD
71 */
72static void sighandler(int sig)
73{
13e44745
JD
74 if (sig == SIGINT && sigintcount++ == 0) {
75 DBG("ignoring first SIGINT");
76 return;
77 }
78
3bd1e081 79 lttng_consumer_should_exit(ctx);
d4a1283e
JD
80}
81
82/*
6533b585 83 * Setup signal handler for :
d4a1283e
JD
84 * SIGINT, SIGTERM, SIGPIPE
85 */
86static int set_signal_handler(void)
87{
88 int ret = 0;
89 struct sigaction sa;
90 sigset_t sigset;
91
92 if ((ret = sigemptyset(&sigset)) < 0) {
93 perror("sigemptyset");
94 return ret;
95 }
96
97 sa.sa_handler = sighandler;
98 sa.sa_mask = sigset;
99 sa.sa_flags = 0;
100 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
101 perror("sigaction");
102 return ret;
103 }
104
105 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
106 perror("sigaction");
107 return ret;
108 }
109
110 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
111 perror("sigaction");
112 return ret;
113 }
114
115 return ret;
116}
117
d4a1283e
JD
118/*
119 * usage function on stderr
120 */
121static void usage(void)
122{
123 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
124 fprintf(stderr, " -h, --help "
125 "Display this usage.\n");
126 fprintf(stderr, " -c, --kconsumerd-cmd-sock PATH "
127 "Specify path for the command socket\n");
128 fprintf(stderr, " -e, --kconsumerd-err-sock PATH "
129 "Specify path for the error socket\n");
130 fprintf(stderr, " -d, --daemonize "
131 "Start as a daemon.\n");
132 fprintf(stderr, " -q, --quiet "
133 "No output at all.\n");
134 fprintf(stderr, " -v, --verbose "
135 "Verbose mode. Activate DBG() macro.\n");
136 fprintf(stderr, " -V, --version "
137 "Show version number.\n");
3bd1e081
MD
138 fprintf(stderr, " -k, --kernel "
139 "Consumer kernel buffers (default).\n");
140 fprintf(stderr, " -u, --ust "
141 "Consumer UST buffers.%s\n",
142#ifdef CONFIG_LTTNG_TOOLS_HAVE_UST
143 ""
144#else
145 " (support not compiled in)"
146#endif
147 );
d4a1283e
JD
148}
149
150/*
151 * daemon argument parsing
152 */
153static void parse_args(int argc, char **argv)
154{
155 int c;
156
157 static struct option long_options[] = {
158 { "kconsumerd-cmd-sock", 1, 0, 'c' },
159 { "kconsumerd-err-sock", 1, 0, 'e' },
160 { "daemonize", 0, 0, 'd' },
161 { "help", 0, 0, 'h' },
162 { "quiet", 0, 0, 'q' },
163 { "verbose", 0, 0, 'v' },
164 { "version", 0, 0, 'V' },
3bd1e081
MD
165 { "kernel", 0, 0, 'k' },
166#ifdef CONFIG_LTTNG_TOOLS_HAVE_UST
167 { "ust", 0, 0, 'u' },
168#endif
d4a1283e
JD
169 { NULL, 0, 0, 0 }
170 };
171
172 while (1) {
173 int option_index = 0;
3bd1e081 174 c = getopt_long(argc, argv, "dhqvVku" "c:e:", long_options, &option_index);
d4a1283e
JD
175 if (c == -1) {
176 break;
177 }
178
179 switch (c) {
914a571b
JD
180 case 0:
181 fprintf(stderr, "option %s", long_options[option_index].name);
182 if (optarg) {
183 fprintf(stderr, " with arg %s\n", optarg);
184 }
185 break;
186 case 'c':
187 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
188 break;
189 case 'e':
190 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
191 break;
192 case 'd':
193 opt_daemon = 1;
194 break;
195 case 'h':
196 usage();
197 exit(EXIT_FAILURE);
198 case 'q':
199 opt_quiet = 1;
200 break;
201 case 'v':
202 opt_verbose = 1;
203 break;
204 case 'V':
205 fprintf(stdout, "%s\n", VERSION);
206 exit(EXIT_SUCCESS);
3bd1e081
MD
207 case 'k':
208 opt_type = LTTNG_CONSUMER_KERNEL;
209 break;
210#ifdef CONFIG_LTTNG_TOOLS_HAVE_UST
211 case 'u':
212 opt_type = LTTNG_CONSUMER_UST;
213 break;
214#endif
914a571b
JD
215 default:
216 usage();
217 exit(EXIT_FAILURE);
d4a1283e
JD
218 }
219 }
220}
221
cb040cc1 222/*
6533b585 223 * Consume data on a file descriptor and write it on a trace file.
cb040cc1 224 */
3bd1e081 225static int read_subbuffer(struct lttng_consumer_stream *stream)
cb040cc1
JD
226{
227 unsigned long len;
228 int err;
229 long ret = 0;
3bd1e081 230 int infd = stream->wait_fd;
cb040cc1 231
3bd1e081 232 DBG("In read_subbuffer (infd : %d)", infd);
cb040cc1
JD
233 /* Get the next subbuffer */
234 err = kernctl_get_next_subbuf(infd);
235 if (err != 0) {
236 ret = errno;
81ffcd20
MD
237 /*
238 * This is a debug message even for single-threaded consumer,
239 * because poll() have more relaxed criterions than get subbuf,
240 * so get_subbuf may fail for short race windows where poll()
241 * would issue wakeups.
242 */
243 DBG("Reserving sub buffer failed (everything is normal, "
cb040cc1
JD
244 "it is due to concurrency)");
245 goto end;
246 }
247
3bd1e081 248 switch (stream->output) {
cb040cc1
JD
249 case LTTNG_EVENT_SPLICE:
250 /* read the whole subbuffer */
251 err = kernctl_get_padded_subbuf_size(infd, &len);
252 if (err != 0) {
253 ret = errno;
254 perror("Getting sub-buffer len failed.");
255 goto end;
256 }
257
258 /* splice the subbuffer to the tracefile */
3bd1e081 259 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, len);
cb040cc1
JD
260 if (ret < 0) {
261 /*
262 * display the error but continue processing to try
263 * to release the subbuffer
264 */
265 ERR("Error splicing to tracefile");
266 }
267 break;
268 case LTTNG_EVENT_MMAP:
269 /* read the used subbuffer size */
8b270bdb 270 err = kernctl_get_padded_subbuf_size(infd, &len);
cb040cc1
JD
271 if (err != 0) {
272 ret = errno;
273 perror("Getting sub-buffer len failed.");
274 goto end;
275 }
276 /* write the subbuffer to the tracefile */
3bd1e081 277 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
cb040cc1
JD
278 if (ret < 0) {
279 /*
280 * display the error but continue processing to try
281 * to release the subbuffer
282 */
283 ERR("Error writing to tracefile");
284 }
285 break;
286 default:
287 ERR("Unknown output method");
288 ret = -1;
289 }
290
291 err = kernctl_put_next_subbuf(infd);
292 if (err != 0) {
293 ret = errno;
294 if (errno == EFAULT) {
295 perror("Error in unreserving sub buffer\n");
296 } else if (errno == EIO) {
297 /* Should never happen with newer LTTng versions */
298 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
299 }
300 goto end;
301 }
302
303end:
304 return ret;
305}
d4a1283e 306
3bd1e081 307static int on_recv_stream(struct lttng_consumer_stream *stream)
5348b470
JD
308{
309 int ret;
310
311 /* Opening the tracefile in write mode */
3bd1e081
MD
312 if (stream->path_name != NULL) {
313 ret = open(stream->path_name,
5348b470
JD
314 O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
315 if (ret < 0) {
3bd1e081 316 ERR("Opening %s", stream->path_name);
5348b470
JD
317 perror("open");
318 goto error;
319 }
3bd1e081 320 stream->out_fd = ret;
5348b470
JD
321 }
322
3bd1e081 323 if (stream->output == LTTNG_EVENT_MMAP) {
5348b470 324 /* get the len of the mmap region */
558e70e9
MD
325 unsigned long mmap_len;
326
3bd1e081 327 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
5348b470
JD
328 if (ret != 0) {
329 ret = errno;
330 perror("kernctl_get_mmap_len");
331 goto error_close_fd;
332 }
3bd1e081 333 stream->mmap_len = (size_t) mmap_len;
5348b470 334
3bd1e081
MD
335 stream->mmap_base = mmap(NULL, stream->mmap_len,
336 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
337 if (stream->mmap_base == MAP_FAILED) {
5348b470
JD
338 perror("Error mmaping");
339 ret = -1;
340 goto error_close_fd;
341 }
342 }
343
344 /* we return 0 to let the library handle the FD internally */
345 return 0;
346
347error_close_fd:
348 {
349 int err;
350
3bd1e081 351 err = close(stream->out_fd);
5348b470
JD
352 assert(!err);
353 }
354error:
355 return ret;
356}
357
d4a1283e
JD
358/*
359 * main
360 */
361int main(int argc, char **argv)
362{
363 int i;
364 int ret = 0;
365 void *status;
366
367 /* Parse arguments */
368 progname = argv[0];
369 parse_args(argc, argv);
370
371 /* Daemonize */
372 if (opt_daemon) {
373 ret = daemon(0, 0);
374 if (ret < 0) {
375 perror("daemon");
376 goto error;
377 }
378 }
379
380 if (strlen(command_sock_path) == 0) {
381 snprintf(command_sock_path, PATH_MAX,
3bd1e081
MD
382 opt_type == LTTNG_CONSUMER_KERNEL ?
383 KCONSUMERD_CMD_SOCK_PATH :
384 USTCONSUMERD_CMD_SOCK_PATH);
d4a1283e 385 }
5348b470 386 /* create the consumer instance with and assign the callbacks */
3bd1e081 387 ctx = lttng_consumer_create(opt_type, read_subbuffer, NULL, on_recv_stream, NULL);
cb040cc1
JD
388 if (ctx == NULL) {
389 goto error;
390 }
391
3bd1e081 392 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
d4a1283e
JD
393 if (strlen(error_sock_path) == 0) {
394 snprintf(error_sock_path, PATH_MAX,
3bd1e081
MD
395 opt_type == LTTNG_CONSUMER_KERNEL ?
396 KCONSUMERD_ERR_SOCK_PATH :
397 USTCONSUMERD_ERR_SOCK_PATH);
d4a1283e
JD
398 }
399
400 if (set_signal_handler() < 0) {
401 goto error;
402 }
403
32258573 404 /* Connect to the socket created by lttng-sessiond to report errors */
d4a1283e 405 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 406 ret = lttcomm_connect_unix_sock(error_sock_path);
32258573 407 /* not a fatal error, but all communication with lttng-sessiond will fail */
1ce86c9a 408 if (ret < 0) {
32258573 409 WARN("Cannot connect to error socket, is lttng-sessiond started ?");
d4a1283e 410 }
3bd1e081 411 lttng_consumer_set_error_sock(ctx, ret);
d4a1283e
JD
412
413 /* Create the thread to manage the receive of fd */
3bd1e081 414 ret = pthread_create(&threads[0], NULL, lttng_consumer_thread_receive_fds,
cb040cc1 415 (void *) ctx);
d4a1283e
JD
416 if (ret != 0) {
417 perror("pthread_create");
418 goto error;
419 }
420
421 /* Create thread to manage the polling/writing of traces */
3bd1e081 422 ret = pthread_create(&threads[1], NULL, lttng_consumer_thread_poll_fds,
cb040cc1 423 (void *) ctx);
d4a1283e
JD
424 if (ret != 0) {
425 perror("pthread_create");
426 goto error;
427 }
428
429 for (i = 0; i < 2; i++) {
430 ret = pthread_join(threads[i], &status);
431 if (ret != 0) {
432 perror("pthread_join");
433 goto error;
434 }
435 }
436 ret = EXIT_SUCCESS;
3bd1e081 437 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_SUCCESS);
d4a1283e
JD
438 goto end;
439
440error:
441 ret = EXIT_FAILURE;
3bd1e081 442 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_FAILURE);
d4a1283e
JD
443
444end:
3bd1e081
MD
445 lttng_consumer_destroy(ctx);
446 lttng_consumer_cleanup();
d4a1283e
JD
447
448 return ret;
449}
This page took 0.044037 seconds and 4 git commands to generate.