Add lttng-modules detection for tests
[lttng-tools.git] / liblttngkconsumerd / lttngkconsumerd.c
CommitLineData
1ce86c9a
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.
1ce86c9a
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 <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <unistd.h>
30#include <urcu/list.h>
5348b470 31#include <assert.h>
6533b585
DG
32
33#include <lttng/lttng-kconsumerd.h>
1ce86c9a 34
50ecdf72 35#include "kernelctl.h"
1ce86c9a 36#include "lttngerr.h"
6533b585 37#include "lttng-sessiond-comm.h"
1ce86c9a 38
6533b585 39static struct lttng_kconsumerd_global_data {
242cd187
MD
40 /*
41 * kconsumerd_data.lock protects kconsumerd_data.fd_list,
6533b585
DG
42 * kconsumerd_data.fds_count, and kconsumerd_data.need_update. It ensures
43 * the count matches the number of items in the fd_list. It ensures the
44 * list updates *always* trigger an fd_array update (therefore need to make
45 * list update vs kconsumerd_data.need_update flag update atomic, and also
46 * flag read, fd array and flag clear atomic).
242cd187
MD
47 */
48 pthread_mutex_t lock;
49 /*
6533b585 50 * Number of element for the list below. Protected by kconsumerd_data.lock.
242cd187
MD
51 */
52 unsigned int fds_count;
53 /*
6533b585 54 * List of FDs. Protected by kconsumerd_data.lock.
242cd187 55 */
6533b585 56 struct lttng_kconsumerd_fd_list fd_list;
242cd187 57 /*
6533b585
DG
58 * Flag specifying if the local array of FDs needs update in the poll
59 * function. Protected by kconsumerd_data.lock.
242cd187
MD
60 */
61 unsigned int need_update;
62} kconsumerd_data = {
63 .fd_list.head = CDS_LIST_HEAD_INIT(kconsumerd_data.fd_list.head),
cb040cc1
JD
64 .fds_count = 0,
65 .need_update = 1,
1ce86c9a
JD
66};
67
6533b585 68/* timeout parameter, to control the polling thread grace period. */
1ce86c9a
JD
69static int kconsumerd_poll_timeout = -1;
70
3dcd2721 71/*
6533b585
DG
72 * Flag to inform the polling thread to quit when all fd hung up. Updated by
73 * the kconsumerd_thread_receive_fds when it notices that all fds has hung up.
74 * Also updated by the signal handler (kconsumerd_should_exit()). Read by the
75 * polling threads.
3dcd2721
MD
76 */
77static volatile int kconsumerd_quit = 0;
1ce86c9a
JD
78
79/*
6533b585
DG
80 * Find a session fd in the global list. The kconsumerd_data.lock must be
81 * locked during this call.
38079a1b 82 *
6533b585 83 * Return 1 if found else 0.
38079a1b
DG
84 */
85static int kconsumerd_find_session_fd(int fd)
86{
6533b585 87 struct lttng_kconsumerd_fd *iter;
38079a1b 88
242cd187 89 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
38079a1b
DG
90 if (iter->sessiond_fd == fd) {
91 DBG("Duplicate session fd %d", fd);
38079a1b
DG
92 return 1;
93 }
94 }
38079a1b
DG
95
96 return 0;
97}
98
1ce86c9a 99/*
6533b585 100 * Remove a fd from the global list protected by a mutex.
1ce86c9a 101 */
6533b585 102static void kconsumerd_del_fd(struct lttng_kconsumerd_fd *lcf)
1ce86c9a 103{
8b270bdb 104 int ret;
242cd187 105 pthread_mutex_lock(&kconsumerd_data.lock);
1ce86c9a 106 cds_list_del(&lcf->list);
242cd187
MD
107 if (kconsumerd_data.fds_count > 0) {
108 kconsumerd_data.fds_count--;
1ce86c9a 109 if (lcf != NULL) {
8b270bdb
JD
110 if (lcf->mmap_base != NULL) {
111 ret = munmap(lcf->mmap_base, lcf->mmap_len);
112 if (ret != 0) {
113 perror("munmap");
114 }
115 }
cb040cc1
JD
116 if (lcf->out_fd != 0) {
117 close(lcf->out_fd);
118 }
1ce86c9a
JD
119 close(lcf->consumerd_fd);
120 free(lcf);
121 lcf = NULL;
122 }
123 }
242cd187
MD
124 kconsumerd_data.need_update = 1;
125 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
126}
127
128/*
5348b470
JD
129 * Create a struct lttcomm_kconsumerd_msg from the
130 * information received on the receiving socket
1ce86c9a 131 */
5348b470
JD
132struct lttng_kconsumerd_fd *kconsumerd_allocate_fd(
133 struct lttcomm_kconsumerd_msg *buf,
6533b585 134 int consumerd_fd)
1ce86c9a 135{
6533b585 136 struct lttng_kconsumerd_fd *tmp_fd;
38079a1b 137
5348b470
JD
138 tmp_fd = malloc(sizeof(struct lttng_kconsumerd_fd));
139 if (tmp_fd == NULL) {
140 perror("malloc struct lttng_kconsumerd_fd");
38079a1b
DG
141 goto end;
142 }
1ce86c9a 143
1ce86c9a
JD
144 tmp_fd->sessiond_fd = buf->fd;
145 tmp_fd->consumerd_fd = consumerd_fd;
146 tmp_fd->state = buf->state;
147 tmp_fd->max_sb_size = buf->max_sb_size;
cb040cc1
JD
148 tmp_fd->out_fd = 0;
149 tmp_fd->out_fd_offset = 0;
8b270bdb
JD
150 tmp_fd->mmap_len = 0;
151 tmp_fd->mmap_base = NULL;
152 tmp_fd->output = buf->output;
1ce86c9a 153 strncpy(tmp_fd->path_name, buf->path_name, PATH_MAX);
99497cd0 154 tmp_fd->path_name[PATH_MAX - 1] = '\0';
5348b470
JD
155 DBG("Allocated %s (sessiond_fd %d, consumerd_fd %d, out_fd %d)",
156 tmp_fd->path_name, tmp_fd->sessiond_fd,
157 tmp_fd->consumerd_fd, tmp_fd->out_fd);
1ce86c9a 158
5348b470
JD
159end:
160 return tmp_fd;
161}
1ce86c9a 162
5348b470
JD
163/*
164 * Add a fd to the global list protected by a mutex.
165 */
166static int kconsumerd_add_fd(struct lttng_kconsumerd_fd *tmp_fd)
167{
168 int ret;
8b270bdb 169
5348b470
JD
170 pthread_mutex_lock(&kconsumerd_data.lock);
171 /* Check if already exist */
172 ret = kconsumerd_find_session_fd(tmp_fd->sessiond_fd);
173 if (ret == 1) {
174 goto end;
8b270bdb 175 }
242cd187
MD
176 cds_list_add(&tmp_fd->list, &kconsumerd_data.fd_list.head);
177 kconsumerd_data.fds_count++;
178 kconsumerd_data.need_update = 1;
5348b470 179
1ce86c9a 180end:
242cd187 181 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
182 return ret;
183}
184
185/*
6533b585 186 * Update a fd according to what we just received.
1ce86c9a
JD
187 */
188static void kconsumerd_change_fd_state(int sessiond_fd,
6533b585 189 enum lttng_kconsumerd_fd_state state)
1ce86c9a 190{
6533b585 191 struct lttng_kconsumerd_fd *iter;
0237248c 192
242cd187
MD
193 pthread_mutex_lock(&kconsumerd_data.lock);
194 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
195 if (iter->sessiond_fd == sessiond_fd) {
196 iter->state = state;
197 break;
198 }
199 }
242cd187
MD
200 kconsumerd_data.need_update = 1;
201 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
202}
203
204/*
6533b585
DG
205 * Allocate the pollfd structure and the local view of the out fds to avoid
206 * doing a lookup in the linked list and concurrency issues when writing is
207 * needed. Called with kconsumerd_data.lock held.
1ce86c9a 208 *
6533b585 209 * Returns the number of fds in the structures.
1ce86c9a 210 */
6533b585
DG
211static int kconsumerd_update_poll_array(
212 struct lttng_kconsumerd_local_data *ctx, struct pollfd **pollfd,
213 struct lttng_kconsumerd_fd **local_kconsumerd_fd)
1ce86c9a 214{
6533b585 215 struct lttng_kconsumerd_fd *iter;
1ce86c9a
JD
216 int i = 0;
217
218 DBG("Updating poll fd array");
242cd187 219 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
220 if (iter->state == ACTIVE_FD) {
221 DBG("Active FD %d", iter->consumerd_fd);
222 (*pollfd)[i].fd = iter->consumerd_fd;
223 (*pollfd)[i].events = POLLIN | POLLPRI;
224 local_kconsumerd_fd[i] = iter;
225 i++;
226 }
227 }
228
229 /*
6533b585
DG
230 * Insert the kconsumerd_poll_pipe at the end of the array and don't
231 * increment i so nb_fd is the number of real FD.
1ce86c9a 232 */
cb040cc1 233 (*pollfd)[i].fd = ctx->kconsumerd_poll_pipe[0];
1ce86c9a 234 (*pollfd)[i].events = POLLIN;
1ce86c9a
JD
235 return i;
236}
237
6533b585
DG
238/*
239 * Receives an array of file descriptors and the associated structures
240 * describing each fd (path name).
241 *
242 * Returns the size of received data
243 */
244static int kconsumerd_consumerd_recv_fd(
245 struct lttng_kconsumerd_local_data *ctx, int sfd,
246 struct pollfd *kconsumerd_sockpoll, int size,
247 enum lttng_kconsumerd_command cmd_type)
248{
249 struct iovec iov[1];
250 int ret = 0, i, tmp2;
251 struct cmsghdr *cmsg;
252 int nb_fd;
253 char recv_fd[CMSG_SPACE(sizeof(int))];
254 struct lttcomm_kconsumerd_msg lkm;
5348b470 255 struct lttng_kconsumerd_fd *new_fd;
6533b585
DG
256
257 /* the number of fds we are about to receive */
258 nb_fd = size / sizeof(struct lttcomm_kconsumerd_msg);
259
260 /*
261 * nb_fd is the number of fds we receive. One fd per recvmsg.
262 */
263 for (i = 0; i < nb_fd; i++) {
264 struct msghdr msg = { 0 };
265
266 /* Prepare to receive the structures */
267 iov[0].iov_base = &lkm;
268 iov[0].iov_len = sizeof(lkm);
269 msg.msg_iov = iov;
270 msg.msg_iovlen = 1;
271
272 msg.msg_control = recv_fd;
273 msg.msg_controllen = sizeof(recv_fd);
274
275 DBG("Waiting to receive fd");
276 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
277 goto end;
278 }
279
280 if ((ret = recvmsg(sfd, &msg, 0)) < 0) {
281 perror("recvmsg");
282 continue;
283 }
284
285 if (ret != (size / nb_fd)) {
286 ERR("Received only %d, expected %d", ret, size);
287 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
288 goto end;
289 }
290
291 cmsg = CMSG_FIRSTHDR(&msg);
292 if (!cmsg) {
293 ERR("Invalid control message header");
294 ret = -1;
295 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
296 goto end;
297 }
298
299 /* if we received fds */
300 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
301 switch (cmd_type) {
302 case ADD_STREAM:
303 DBG("kconsumerd_add_fd %s (%d)", lkm.path_name,
304 ((int *) CMSG_DATA(cmsg))[0]);
305
5348b470
JD
306 new_fd = kconsumerd_allocate_fd(&lkm, ((int *) CMSG_DATA(cmsg))[0]);
307 if (new_fd == NULL) {
6533b585
DG
308 lttng_kconsumerd_send_error(ctx, KCONSUMERD_OUTFD_ERROR);
309 goto end;
310 }
5348b470
JD
311
312 if (ctx->on_recv_fd != NULL) {
313 ret = ctx->on_recv_fd(new_fd);
314 if (ret == 0) {
315 kconsumerd_add_fd(new_fd);
316 } else if (ret < 0) {
317 goto end;
318 }
319 } else {
320 kconsumerd_add_fd(new_fd);
321 }
6533b585
DG
322 break;
323 case UPDATE_STREAM:
5348b470
JD
324 if (ctx->on_update_fd != NULL) {
325 ret = ctx->on_update_fd(lkm.fd, lkm.state);
326 if (ret == 0) {
327 kconsumerd_change_fd_state(lkm.fd, lkm.state);
328 } else if (ret < 0) {
329 goto end;
330 }
331 } else {
332 kconsumerd_change_fd_state(lkm.fd, lkm.state);
333 }
6533b585
DG
334 break;
335 default:
336 break;
337 }
338 /* signal the poll thread */
339 tmp2 = write(ctx->kconsumerd_poll_pipe[1], "4", 1);
340 if (tmp2 < 0) {
341 perror("write kconsumerd poll");
342 }
343 } else {
344 ERR("Didn't received any fd");
345 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
346 ret = -1;
347 goto end;
348 }
349 }
350
351end:
352 return ret;
353}
354
355/*
356 * Set the error socket.
357 */
358void lttng_kconsumerd_set_error_sock(
359 struct lttng_kconsumerd_local_data *ctx, int sock)
360{
361 ctx->kconsumerd_error_socket = sock;
362}
363
364/*
365 * Set the command socket path.
366 */
367
368void lttng_kconsumerd_set_command_sock_path(
369 struct lttng_kconsumerd_local_data *ctx, char *sock)
370{
371 ctx->kconsumerd_command_sock_path = sock;
372}
1ce86c9a 373
92ab9ab6
JD
374static void lttng_kconsumerd_sync_trace_file(
375 struct lttng_kconsumerd_fd *kconsumerd_fd, off_t orig_offset)
376{
377 int outfd = kconsumerd_fd->out_fd;
378 /*
379 * This does a blocking write-and-wait on any page that belongs to the
380 * subbuffer prior to the one we just wrote.
381 * Don't care about error values, as these are just hints and ways to
382 * limit the amount of page cache used.
383 */
384 if (orig_offset >= kconsumerd_fd->max_sb_size) {
385 sync_file_range(outfd, orig_offset - kconsumerd_fd->max_sb_size,
386 kconsumerd_fd->max_sb_size,
387 SYNC_FILE_RANGE_WAIT_BEFORE
388 | SYNC_FILE_RANGE_WRITE
389 | SYNC_FILE_RANGE_WAIT_AFTER);
390 /*
391 * Give hints to the kernel about how we access the file:
392 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
393 * we write it.
394 *
395 * We need to call fadvise again after the file grows because the
396 * kernel does not seem to apply fadvise to non-existing parts of the
397 * file.
398 *
399 * Call fadvise _after_ having waited for the page writeback to
400 * complete because the dirty page writeback semantic is not well
401 * defined. So it can be expected to lead to lower throughput in
402 * streaming.
403 */
404 posix_fadvise(outfd, orig_offset - kconsumerd_fd->max_sb_size,
405 kconsumerd_fd->max_sb_size, POSIX_FADV_DONTNEED);
406 }
407}
408
409
1ce86c9a 410/*
6533b585 411 * Mmap the ring buffer, read it and write the data to the tracefile.
1ce86c9a 412 *
1ce86c9a
JD
413 * Returns the number of bytes written
414 */
6533b585
DG
415int lttng_kconsumerd_on_read_subbuffer_mmap(
416 struct lttng_kconsumerd_local_data *ctx,
417 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len)
1ce86c9a 418{
8b270bdb 419 unsigned long mmap_offset;
1ce86c9a
JD
420 long ret = 0;
421 off_t orig_offset = kconsumerd_fd->out_fd_offset;
422 int fd = kconsumerd_fd->consumerd_fd;
423 int outfd = kconsumerd_fd->out_fd;
424
1ce86c9a
JD
425 /* get the offset inside the fd to mmap */
426 ret = kernctl_get_mmap_read_offset(fd, &mmap_offset);
427 if (ret != 0) {
428 ret = errno;
429 perror("kernctl_get_mmap_read_offset");
430 goto end;
431 }
432
1ce86c9a 433 while (len > 0) {
8b270bdb 434 ret = write(outfd, kconsumerd_fd->mmap_base + mmap_offset, len);
1ce86c9a
JD
435 if (ret >= len) {
436 len = 0;
437 } else if (ret < 0) {
438 ret = errno;
439 perror("Error in file write");
440 goto end;
441 }
442 /* This won't block, but will start writeout asynchronously */
443 sync_file_range(outfd, kconsumerd_fd->out_fd_offset, ret,
444 SYNC_FILE_RANGE_WRITE);
445 kconsumerd_fd->out_fd_offset += ret;
446 }
447
92ab9ab6 448 lttng_kconsumerd_sync_trace_file(kconsumerd_fd, orig_offset);
1ce86c9a 449
1ce86c9a
JD
450 goto end;
451
452end:
1ce86c9a
JD
453 return ret;
454}
455
456/*
1ce86c9a 457 * Splice the data from the ring buffer to the tracefile.
6533b585
DG
458 *
459 * Returns the number of bytes spliced.
1ce86c9a 460 */
6533b585
DG
461int lttng_kconsumerd_on_read_subbuffer_splice(
462 struct lttng_kconsumerd_local_data *ctx,
463 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len)
1ce86c9a
JD
464{
465 long ret = 0;
466 loff_t offset = 0;
467 off_t orig_offset = kconsumerd_fd->out_fd_offset;
468 int fd = kconsumerd_fd->consumerd_fd;
469 int outfd = kconsumerd_fd->out_fd;
470
471 while (len > 0) {
472 DBG("splice chan to pipe offset %lu (fd : %d)",
473 (unsigned long)offset, fd);
cb040cc1 474 ret = splice(fd, &offset, ctx->kconsumerd_thread_pipe[1], NULL, len,
1ce86c9a
JD
475 SPLICE_F_MOVE | SPLICE_F_MORE);
476 DBG("splice chan to pipe ret %ld", ret);
477 if (ret < 0) {
478 ret = errno;
479 perror("Error in relay splice");
480 goto splice_error;
481 }
482
cb040cc1 483 ret = splice(ctx->kconsumerd_thread_pipe[0], NULL, outfd, NULL, ret,
1ce86c9a
JD
484 SPLICE_F_MOVE | SPLICE_F_MORE);
485 DBG("splice pipe to file %ld", ret);
486 if (ret < 0) {
487 ret = errno;
488 perror("Error in file splice");
489 goto splice_error;
490 }
751667bd 491 len -= ret;
1ce86c9a
JD
492 /* This won't block, but will start writeout asynchronously */
493 sync_file_range(outfd, kconsumerd_fd->out_fd_offset, ret,
494 SYNC_FILE_RANGE_WRITE);
495 kconsumerd_fd->out_fd_offset += ret;
496 }
92ab9ab6 497 lttng_kconsumerd_sync_trace_file(kconsumerd_fd, orig_offset);
1ce86c9a 498
1ce86c9a
JD
499 goto end;
500
501splice_error:
502 /* send the appropriate error description to sessiond */
503 switch(ret) {
504 case EBADF:
6533b585 505 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_EBADF);
1ce86c9a
JD
506 break;
507 case EINVAL:
6533b585 508 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_EINVAL);
1ce86c9a
JD
509 break;
510 case ENOMEM:
6533b585 511 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_ENOMEM);
1ce86c9a
JD
512 break;
513 case ESPIPE:
6533b585 514 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_ESPIPE);
1ce86c9a 515 break;
1ce86c9a
JD
516 }
517
518end:
519 return ret;
520}
521
92ab9ab6
JD
522/*
523 * Take a snapshot for a specific fd
524 *
525 * Returns 0 on success, < 0 on error
526 */
527int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data *ctx,
528 struct lttng_kconsumerd_fd *kconsumerd_fd)
529{
530 int ret = 0;
531 int infd = kconsumerd_fd->consumerd_fd;
532
533 ret = kernctl_snapshot(infd);
534 if (ret != 0) {
535 ret = errno;
536 perror("Getting sub-buffer snapshot.");
537 }
538
539 return ret;
540}
541
542/*
543 * Get the produced position
544 *
545 * Returns 0 on success, < 0 on error
546 */
547int lttng_kconsumerd_get_produced_snapshot(
548 struct lttng_kconsumerd_local_data *ctx,
549 struct lttng_kconsumerd_fd *kconsumerd_fd,
550 unsigned long *pos)
551{
552 int ret;
553 int infd = kconsumerd_fd->consumerd_fd;
554
555 ret = kernctl_snapshot_get_produced(infd, pos);
556 if (ret != 0) {
557 ret = errno;
558 perror("kernctl_snapshot_get_produced");
559 }
560
561 return ret;
562}
563
4de84ad9 564/*
6533b585
DG
565 * Poll on the should_quit pipe and the command socket return -1 on error and
566 * should exit, 0 if data is available on the command socket
4de84ad9 567 */
6533b585 568int lttng_kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll)
4de84ad9
JD
569{
570 int num_rdy;
571
572 num_rdy = poll(kconsumerd_sockpoll, 2, -1);
573 if (num_rdy == -1) {
574 perror("Poll error");
575 goto exit;
576 }
577 if (kconsumerd_sockpoll[0].revents == POLLIN) {
578 DBG("kconsumerd_should_quit wake up");
579 goto exit;
580 }
581 return 0;
582
583exit:
584 return -1;
585}
586
1ce86c9a 587/*
6533b585
DG
588 * This thread polls the fds in the ltt_fd_list to consume the data and write
589 * it to tracefile if necessary.
1ce86c9a 590 */
6533b585 591void *lttng_kconsumerd_thread_poll_fds(void *data)
1ce86c9a
JD
592{
593 int num_rdy, num_hup, high_prio, ret, i;
594 struct pollfd *pollfd = NULL;
595 /* local view of the fds */
6533b585 596 struct lttng_kconsumerd_fd **local_kconsumerd_fd = NULL;
242cd187 597 /* local view of kconsumerd_data.fds_count */
1ce86c9a
JD
598 int nb_fd = 0;
599 char tmp;
600 int tmp2;
6533b585 601 struct lttng_kconsumerd_local_data *ctx = data;
1ce86c9a 602
1ce86c9a 603
6533b585 604 local_kconsumerd_fd = malloc(sizeof(struct lttng_kconsumerd_fd));
1ce86c9a
JD
605
606 while (1) {
607 high_prio = 0;
608 num_hup = 0;
609
610 /*
611 * the ltt_fd_list has been updated, we need to update our
612 * local array as well
613 */
242cd187
MD
614 pthread_mutex_lock(&kconsumerd_data.lock);
615 if (kconsumerd_data.need_update) {
1ce86c9a
JD
616 if (pollfd != NULL) {
617 free(pollfd);
618 pollfd = NULL;
619 }
620 if (local_kconsumerd_fd != NULL) {
621 free(local_kconsumerd_fd);
622 local_kconsumerd_fd = NULL;
623 }
0237248c 624
1ce86c9a 625 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
242cd187 626 pollfd = malloc((kconsumerd_data.fds_count + 1) * sizeof(struct pollfd));
1ce86c9a
JD
627 if (pollfd == NULL) {
628 perror("pollfd malloc");
242cd187 629 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
630 goto end;
631 }
0237248c 632
1ce86c9a 633 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
242cd187 634 local_kconsumerd_fd = malloc((kconsumerd_data.fds_count + 1) *
6533b585 635 sizeof(struct lttng_kconsumerd_fd));
1ce86c9a
JD
636 if (local_kconsumerd_fd == NULL) {
637 perror("local_kconsumerd_fd malloc");
242cd187 638 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
639 goto end;
640 }
cb040cc1 641 ret = kconsumerd_update_poll_array(ctx, &pollfd, local_kconsumerd_fd);
1ce86c9a
JD
642 if (ret < 0) {
643 ERR("Error in allocating pollfd or local_outfds");
6533b585 644 lttng_kconsumerd_send_error(ctx, KCONSUMERD_POLL_ERROR);
242cd187 645 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
646 goto end;
647 }
648 nb_fd = ret;
242cd187 649 kconsumerd_data.need_update = 0;
1ce86c9a 650 }
242cd187 651 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
652
653 /* poll on the array of fds */
654 DBG("polling on %d fd", nb_fd + 1);
655 num_rdy = poll(pollfd, nb_fd + 1, kconsumerd_poll_timeout);
656 DBG("poll num_rdy : %d", num_rdy);
657 if (num_rdy == -1) {
658 perror("Poll error");
6533b585 659 lttng_kconsumerd_send_error(ctx, KCONSUMERD_POLL_ERROR);
1ce86c9a
JD
660 goto end;
661 } else if (num_rdy == 0) {
662 DBG("Polling thread timed out");
663 goto end;
664 }
665
666 /* No FDs and kconsumerd_quit, kconsumerd_cleanup the thread */
667 if (nb_fd == 0 && kconsumerd_quit == 1) {
668 goto end;
669 }
670
671 /*
242cd187
MD
672 * If the kconsumerd_poll_pipe triggered poll go
673 * directly to the beginning of the loop to update the
674 * array. We want to prioritize array update over
675 * low-priority reads.
1ce86c9a 676 */
242cd187 677 if (pollfd[nb_fd].revents == POLLIN) {
1ce86c9a 678 DBG("kconsumerd_poll_pipe wake up");
cb040cc1 679 tmp2 = read(ctx->kconsumerd_poll_pipe[0], &tmp, 1);
f40799e8
DG
680 if (tmp2 < 0) {
681 perror("read kconsumerd poll");
682 }
1ce86c9a
JD
683 continue;
684 }
685
686 /* Take care of high priority channels first. */
687 for (i = 0; i < nb_fd; i++) {
688 switch(pollfd[i].revents) {
689 case POLLERR:
690 ERR("Error returned in polling fd %d.", pollfd[i].fd);
691 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
692 num_hup++;
693 break;
694 case POLLHUP:
695 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
696 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
697 num_hup++;
698 break;
699 case POLLNVAL:
700 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
701 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
702 num_hup++;
703 break;
704 case POLLPRI:
705 DBG("Urgent read on fd %d", pollfd[i].fd);
706 high_prio = 1;
cb040cc1 707 ret = ctx->on_buffer_ready(local_kconsumerd_fd[i]);
1ce86c9a
JD
708 /* it's ok to have an unavailable sub-buffer */
709 if (ret == EAGAIN) {
710 ret = 0;
711 }
712 break;
713 }
714 }
715
716 /* If every buffer FD has hung up, we end the read loop here */
717 if (nb_fd > 0 && num_hup == nb_fd) {
718 DBG("every buffer FD has hung up\n");
719 if (kconsumerd_quit == 1) {
720 goto end;
721 }
722 continue;
723 }
724
725 /* Take care of low priority channels. */
726 if (high_prio == 0) {
727 for (i = 0; i < nb_fd; i++) {
728 if (pollfd[i].revents == POLLIN) {
729 DBG("Normal read on fd %d", pollfd[i].fd);
cb040cc1 730 ret = ctx->on_buffer_ready(local_kconsumerd_fd[i]);
1ce86c9a
JD
731 /* it's ok to have an unavailable subbuffer */
732 if (ret == EAGAIN) {
733 ret = 0;
734 }
735 }
736 }
737 }
738 }
739end:
740 DBG("polling thread exiting");
741 if (pollfd != NULL) {
742 free(pollfd);
743 pollfd = NULL;
744 }
745 if (local_kconsumerd_fd != NULL) {
746 free(local_kconsumerd_fd);
747 local_kconsumerd_fd = NULL;
748 }
1ce86c9a
JD
749 return NULL;
750}
751
752/*
6533b585 753 * Initialise the necessary environnement :
cb040cc1 754 * - create a new context
4de84ad9
JD
755 * - create the poll_pipe
756 * - create the should_quit pipe (for signal handler)
cb040cc1 757 * - create the thread pipe (for splice)
6533b585 758 *
cb040cc1
JD
759 * Takes a function pointer as argument, this function is called when data is
760 * available on a buffer. This function is responsible to do the
761 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
762 * buffer configuration and then kernctl_put_next_subbuf at the end.
6533b585 763 *
cb040cc1 764 * Returns a pointer to the new context or NULL on error.
1ce86c9a 765 */
6533b585 766struct lttng_kconsumerd_local_data *lttng_kconsumerd_create(
5348b470
JD
767 int (*buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd),
768 int (*recv_fd)(struct lttng_kconsumerd_fd *kconsumerd_fd),
769 int (*update_fd)(int sessiond_fd, uint32_t state))
1ce86c9a 770{
5348b470 771 int ret, i;
6533b585 772 struct lttng_kconsumerd_local_data *ctx;
4de84ad9 773
6533b585 774 ctx = malloc(sizeof(struct lttng_kconsumerd_local_data));
cb040cc1
JD
775 if (ctx == NULL) {
776 perror("allocating context");
5348b470 777 goto error;
cb040cc1
JD
778 }
779
5348b470 780 /* assign the callbacks */
cb040cc1 781 ctx->on_buffer_ready = buffer_ready;
5348b470
JD
782 ctx->on_recv_fd = recv_fd;
783 ctx->on_update_fd = update_fd;
4de84ad9 784
cb040cc1 785 ret = pipe(ctx->kconsumerd_poll_pipe);
4de84ad9
JD
786 if (ret < 0) {
787 perror("Error creating poll pipe");
5348b470 788 goto error_poll_pipe;
4de84ad9
JD
789 }
790
cb040cc1 791 ret = pipe(ctx->kconsumerd_should_quit);
4de84ad9
JD
792 if (ret < 0) {
793 perror("Error creating recv pipe");
5348b470 794 goto error_quit_pipe;
cb040cc1
JD
795 }
796
797 ret = pipe(ctx->kconsumerd_thread_pipe);
798 if (ret < 0) {
799 perror("Error creating thread pipe");
5348b470 800 goto error_thread_pipe;
4de84ad9
JD
801 }
802
cb040cc1 803 return ctx;
5348b470
JD
804
805
806error_thread_pipe:
807 for (i = 0; i < 2; i++) {
808 int err;
809
810 err = close(ctx->kconsumerd_should_quit[i]);
811 assert(!err);
812 }
813error_quit_pipe:
814 for (i = 0; i < 2; i++) {
815 int err;
816
817 err = close(ctx->kconsumerd_poll_pipe[i]);
818 assert(!err);
819 }
820error_poll_pipe:
821 free(ctx);
822error:
823 return NULL;
cb040cc1
JD
824}
825
826/*
6533b585 827 * Close all fds associated with the instance and free the context.
cb040cc1 828 */
6533b585 829void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data *ctx)
cb040cc1
JD
830{
831 close(ctx->kconsumerd_error_socket);
832 close(ctx->kconsumerd_thread_pipe[0]);
833 close(ctx->kconsumerd_thread_pipe[1]);
834 close(ctx->kconsumerd_poll_pipe[0]);
835 close(ctx->kconsumerd_poll_pipe[1]);
836 close(ctx->kconsumerd_should_quit[0]);
837 close(ctx->kconsumerd_should_quit[1]);
838 unlink(ctx->kconsumerd_command_sock_path);
839 free(ctx);
840 ctx = NULL;
1ce86c9a
JD
841}
842
843/*
6533b585
DG
844 * This thread listens on the consumerd socket and receives the file
845 * descriptors from the session daemon.
1ce86c9a 846 */
6533b585 847void *lttng_kconsumerd_thread_receive_fds(void *data)
1ce86c9a
JD
848{
849 int sock, client_socket, ret;
850 struct lttcomm_kconsumerd_header tmp;
4de84ad9 851 /*
6533b585
DG
852 * structure to poll for incoming data on communication socket avoids
853 * making blocking sockets.
4de84ad9
JD
854 */
855 struct pollfd kconsumerd_sockpoll[2];
6533b585 856 struct lttng_kconsumerd_local_data *ctx = data;
4de84ad9 857
1ce86c9a 858
cb040cc1
JD
859 DBG("Creating command socket %s", ctx->kconsumerd_command_sock_path);
860 unlink(ctx->kconsumerd_command_sock_path);
861 client_socket = lttcomm_create_unix_sock(ctx->kconsumerd_command_sock_path);
1ce86c9a
JD
862 if (client_socket < 0) {
863 ERR("Cannot create command socket");
864 goto end;
865 }
866
867 ret = lttcomm_listen_unix_sock(client_socket);
868 if (ret < 0) {
869 goto end;
870 }
871
872 DBG("Sending ready command to ltt-sessiond");
6533b585 873 ret = lttng_kconsumerd_send_error(ctx, KCONSUMERD_COMMAND_SOCK_READY);
1ce86c9a
JD
874 if (ret < 0) {
875 ERR("Error sending ready command to ltt-sessiond");
876 goto end;
877 }
878
4de84ad9
JD
879 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
880 if (ret < 0) {
881 perror("fcntl O_NONBLOCK");
882 goto end;
883 }
884
885 /* prepare the FDs to poll : to client socket and the should_quit pipe */
cb040cc1 886 kconsumerd_sockpoll[0].fd = ctx->kconsumerd_should_quit[0];
4de84ad9
JD
887 kconsumerd_sockpoll[0].events = POLLIN | POLLPRI;
888 kconsumerd_sockpoll[1].fd = client_socket;
889 kconsumerd_sockpoll[1].events = POLLIN | POLLPRI;
890
6533b585 891 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
4de84ad9
JD
892 goto end;
893 }
894 DBG("Connection on client_socket");
895
1ce86c9a
JD
896 /* Blocking call, waiting for transmission */
897 sock = lttcomm_accept_unix_sock(client_socket);
898 if (sock <= 0) {
899 WARN("On accept");
900 goto end;
901 }
4de84ad9
JD
902 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
903 if (ret < 0) {
904 perror("fcntl O_NONBLOCK");
905 goto end;
906 }
907
908 /* update the polling structure to poll on the established socket */
909 kconsumerd_sockpoll[1].fd = sock;
910 kconsumerd_sockpoll[1].events = POLLIN | POLLPRI;
911
1ce86c9a 912 while (1) {
6533b585 913 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
4de84ad9
JD
914 goto end;
915 }
916 DBG("Incoming fds on sock");
917
1ce86c9a
JD
918 /* We first get the number of fd we are about to receive */
919 ret = lttcomm_recv_unix_sock(sock, &tmp,
920 sizeof(struct lttcomm_kconsumerd_header));
921 if (ret <= 0) {
922 ERR("Communication interrupted on command socket");
923 goto end;
924 }
925 if (tmp.cmd_type == STOP) {
926 DBG("Received STOP command");
927 goto end;
928 }
3dcd2721
MD
929 if (kconsumerd_quit) {
930 DBG("kconsumerd_thread_receive_fds received quit from signal");
931 goto end;
932 }
4de84ad9 933
1ce86c9a 934 /* we received a command to add or update fds */
cb040cc1 935 ret = kconsumerd_consumerd_recv_fd(ctx, sock, kconsumerd_sockpoll,
4de84ad9 936 tmp.payload_size, tmp.cmd_type);
8b270bdb 937 if (ret < 0) {
1ce86c9a
JD
938 ERR("Receiving the FD, exiting");
939 goto end;
940 }
4de84ad9 941 DBG("received fds on sock");
1ce86c9a
JD
942 }
943
944end:
945 DBG("kconsumerd_thread_receive_fds exiting");
946
947 /*
948 * when all fds have hung up, the polling thread
949 * can exit cleanly
950 */
951 kconsumerd_quit = 1;
952
953 /*
954 * 2s of grace period, if no polling events occur during
955 * this period, the polling thread will exit even if there
956 * are still open FDs (should not happen, but safety mechanism).
957 */
6533b585 958 kconsumerd_poll_timeout = LTTNG_KCONSUMERD_POLL_GRACE_PERIOD;
1ce86c9a
JD
959
960 /* wake up the polling thread */
cb040cc1 961 ret = write(ctx->kconsumerd_poll_pipe[1], "4", 1);
1ce86c9a
JD
962 if (ret < 0) {
963 perror("poll pipe write");
964 }
965 return NULL;
966}
967
968/*
6533b585
DG
969 * Close all the tracefiles and stream fds, should be called when all instances
970 * are destroyed.
1ce86c9a 971 */
6533b585 972void lttng_kconsumerd_cleanup(void)
1ce86c9a 973{
6533b585 974 struct lttng_kconsumerd_fd *iter, *tmp;
1ce86c9a 975
3dcd2721
MD
976 /*
977 * close all outfd. Called when there are no more threads
978 * running (after joining on the threads), no need to protect
979 * list iteration with mutex.
980 */
6533b585
DG
981 cds_list_for_each_entry_safe(iter, tmp,
982 &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
983 kconsumerd_del_fd(iter);
984 }
985}
986
3dcd2721
MD
987/*
988 * Called from signal handler.
989 */
6533b585 990void lttng_kconsumerd_should_exit(struct lttng_kconsumerd_local_data *ctx)
3dcd2721 991{
4de84ad9 992 int ret;
3dcd2721 993 kconsumerd_quit = 1;
cb040cc1 994 ret = write(ctx->kconsumerd_should_quit[1], "4", 1);
f40799e8
DG
995 if (ret < 0) {
996 perror("write kconsumerd quit");
997 }
3dcd2721
MD
998}
999
1ce86c9a 1000/*
6533b585 1001 * Send return code to the session daemon.
1ce86c9a 1002 */
6533b585
DG
1003int lttng_kconsumerd_send_error(
1004 struct lttng_kconsumerd_local_data *ctx, int cmd)
1ce86c9a 1005{
cb040cc1
JD
1006 if (ctx->kconsumerd_error_socket > 0) {
1007 return lttcomm_send_unix_sock(ctx->kconsumerd_error_socket, &cmd,
1ce86c9a
JD
1008 sizeof(enum lttcomm_sessiond_command));
1009 }
1010
1011 return 0;
1012}
This page took 0.070344 seconds and 4 git commands to generate.