2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
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.
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.
27 #include <sys/socket.h>
28 #include <sys/types.h>
30 #include <urcu/list.h>
33 #include <lttng/lttng-kconsumerd.h>
35 #include "kernelctl.h"
37 #include "lttng-sessiond-comm.h"
39 static struct lttng_kconsumerd_global_data
{
41 * kconsumerd_data.lock protects kconsumerd_data.fd_list,
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).
50 * Number of element for the list below. Protected by kconsumerd_data.lock.
52 unsigned int fds_count
;
54 * List of FDs. Protected by kconsumerd_data.lock.
56 struct lttng_kconsumerd_fd_list fd_list
;
58 * Flag specifying if the local array of FDs needs update in the poll
59 * function. Protected by kconsumerd_data.lock.
61 unsigned int need_update
;
63 .fd_list
.head
= CDS_LIST_HEAD_INIT(kconsumerd_data
.fd_list
.head
),
68 /* timeout parameter, to control the polling thread grace period. */
69 static int kconsumerd_poll_timeout
= -1;
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
77 static volatile int kconsumerd_quit
= 0;
80 * Find a session fd in the global list. The kconsumerd_data.lock must be
81 * locked during this call.
83 * Return 1 if found else 0.
85 static int kconsumerd_find_session_fd(int fd
)
87 struct lttng_kconsumerd_fd
*iter
;
89 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
90 if (iter
->sessiond_fd
== fd
) {
91 DBG("Duplicate session fd %d", fd
);
100 * Remove a fd from the global list protected by a mutex.
102 static void kconsumerd_del_fd(struct lttng_kconsumerd_fd
*lcf
)
105 pthread_mutex_lock(&kconsumerd_data
.lock
);
106 cds_list_del(&lcf
->list
);
107 if (kconsumerd_data
.fds_count
> 0) {
108 kconsumerd_data
.fds_count
--;
110 if (lcf
->mmap_base
!= NULL
) {
111 ret
= munmap(lcf
->mmap_base
, lcf
->mmap_len
);
116 if (lcf
->out_fd
!= 0) {
119 close(lcf
->consumerd_fd
);
124 kconsumerd_data
.need_update
= 1;
125 pthread_mutex_unlock(&kconsumerd_data
.lock
);
129 * Create a struct lttcomm_kconsumerd_msg from the
130 * information received on the receiving socket
132 struct lttng_kconsumerd_fd
*kconsumerd_allocate_fd(
133 struct lttcomm_kconsumerd_msg
*buf
,
136 struct lttng_kconsumerd_fd
*tmp_fd
;
138 tmp_fd
= malloc(sizeof(struct lttng_kconsumerd_fd
));
139 if (tmp_fd
== NULL
) {
140 perror("malloc struct lttng_kconsumerd_fd");
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
;
149 tmp_fd
->out_fd_offset
= 0;
150 tmp_fd
->mmap_len
= 0;
151 tmp_fd
->mmap_base
= NULL
;
152 tmp_fd
->output
= buf
->output
;
153 strncpy(tmp_fd
->path_name
, buf
->path_name
, PATH_MAX
);
154 tmp_fd
->path_name
[PATH_MAX
- 1] = '\0';
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
);
164 * Add a fd to the global list protected by a mutex.
166 static int kconsumerd_add_fd(struct lttng_kconsumerd_fd
*tmp_fd
)
170 pthread_mutex_lock(&kconsumerd_data
.lock
);
171 /* Check if already exist */
172 ret
= kconsumerd_find_session_fd(tmp_fd
->sessiond_fd
);
176 cds_list_add(&tmp_fd
->list
, &kconsumerd_data
.fd_list
.head
);
177 kconsumerd_data
.fds_count
++;
178 kconsumerd_data
.need_update
= 1;
181 pthread_mutex_unlock(&kconsumerd_data
.lock
);
186 * Update a fd according to what we just received.
188 static void kconsumerd_change_fd_state(int sessiond_fd
,
189 enum lttng_kconsumerd_fd_state state
)
191 struct lttng_kconsumerd_fd
*iter
;
193 pthread_mutex_lock(&kconsumerd_data
.lock
);
194 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
195 if (iter
->sessiond_fd
== sessiond_fd
) {
200 kconsumerd_data
.need_update
= 1;
201 pthread_mutex_unlock(&kconsumerd_data
.lock
);
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.
209 * Returns the number of fds in the structures.
211 static int kconsumerd_update_poll_array(
212 struct lttng_kconsumerd_local_data
*ctx
, struct pollfd
**pollfd
,
213 struct lttng_kconsumerd_fd
**local_kconsumerd_fd
)
215 struct lttng_kconsumerd_fd
*iter
;
218 DBG("Updating poll fd array");
219 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
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
;
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.
233 (*pollfd
)[i
].fd
= ctx
->kconsumerd_poll_pipe
[0];
234 (*pollfd
)[i
].events
= POLLIN
;
239 * Receives an array of file descriptors and the associated structures
240 * describing each fd (path name).
242 * Returns the size of received data
244 static 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
)
250 int ret
= 0, i
, tmp2
;
251 struct cmsghdr
*cmsg
;
253 char recv_fd
[CMSG_SPACE(sizeof(int))];
254 struct lttcomm_kconsumerd_msg lkm
;
255 struct lttng_kconsumerd_fd
*new_fd
;
257 /* the number of fds we are about to receive */
258 nb_fd
= size
/ sizeof(struct lttcomm_kconsumerd_msg
);
261 * nb_fd is the number of fds we receive. One fd per recvmsg.
263 for (i
= 0; i
< nb_fd
; i
++) {
264 struct msghdr msg
= { 0 };
266 /* Prepare to receive the structures */
267 iov
[0].iov_base
= &lkm
;
268 iov
[0].iov_len
= sizeof(lkm
);
272 msg
.msg_control
= recv_fd
;
273 msg
.msg_controllen
= sizeof(recv_fd
);
275 DBG("Waiting to receive fd");
276 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
280 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
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
);
291 cmsg
= CMSG_FIRSTHDR(&msg
);
293 ERR("Invalid control message header");
295 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
299 /* if we received fds */
300 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
303 DBG("kconsumerd_add_fd %s (%d)", lkm
.path_name
,
304 ((int *) CMSG_DATA(cmsg
))[0]);
306 new_fd
= kconsumerd_allocate_fd(&lkm
, ((int *) CMSG_DATA(cmsg
))[0]);
307 if (new_fd
== NULL
) {
308 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_OUTFD_ERROR
);
312 if (ctx
->on_recv_fd
!= NULL
) {
313 ret
= ctx
->on_recv_fd(new_fd
);
315 kconsumerd_add_fd(new_fd
);
316 } else if (ret
< 0) {
320 kconsumerd_add_fd(new_fd
);
324 if (ctx
->on_update_fd
!= NULL
) {
325 ret
= ctx
->on_update_fd(lkm
.fd
, lkm
.state
);
327 kconsumerd_change_fd_state(lkm
.fd
, lkm
.state
);
328 } else if (ret
< 0) {
332 kconsumerd_change_fd_state(lkm
.fd
, lkm
.state
);
338 /* signal the poll thread */
339 tmp2
= write(ctx
->kconsumerd_poll_pipe
[1], "4", 1);
341 perror("write kconsumerd poll");
344 ERR("Didn't received any fd");
345 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
356 * Set the error socket.
358 void lttng_kconsumerd_set_error_sock(
359 struct lttng_kconsumerd_local_data
*ctx
, int sock
)
361 ctx
->kconsumerd_error_socket
= sock
;
365 * Set the command socket path.
368 void lttng_kconsumerd_set_command_sock_path(
369 struct lttng_kconsumerd_local_data
*ctx
, char *sock
)
371 ctx
->kconsumerd_command_sock_path
= sock
;
374 static void lttng_kconsumerd_sync_trace_file(
375 struct lttng_kconsumerd_fd
*kconsumerd_fd
, off_t orig_offset
)
377 int outfd
= kconsumerd_fd
->out_fd
;
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.
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
);
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
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
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
404 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
405 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
411 * Mmap the ring buffer, read it and write the data to the tracefile.
413 * Returns the number of bytes written
415 int lttng_kconsumerd_on_read_subbuffer_mmap(
416 struct lttng_kconsumerd_local_data
*ctx
,
417 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
419 unsigned long mmap_offset
;
421 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
422 int fd
= kconsumerd_fd
->consumerd_fd
;
423 int outfd
= kconsumerd_fd
->out_fd
;
425 /* get the offset inside the fd to mmap */
426 ret
= kernctl_get_mmap_read_offset(fd
, &mmap_offset
);
429 perror("kernctl_get_mmap_read_offset");
434 ret
= write(outfd
, kconsumerd_fd
->mmap_base
+ mmap_offset
, len
);
437 } else if (ret
< 0) {
439 perror("Error in file write");
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
;
448 lttng_kconsumerd_sync_trace_file(kconsumerd_fd
, orig_offset
);
457 * Splice the data from the ring buffer to the tracefile.
459 * Returns the number of bytes spliced.
461 int lttng_kconsumerd_on_read_subbuffer_splice(
462 struct lttng_kconsumerd_local_data
*ctx
,
463 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
467 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
468 int fd
= kconsumerd_fd
->consumerd_fd
;
469 int outfd
= kconsumerd_fd
->out_fd
;
472 DBG("splice chan to pipe offset %lu (fd : %d)",
473 (unsigned long)offset
, fd
);
474 ret
= splice(fd
, &offset
, ctx
->kconsumerd_thread_pipe
[1], NULL
, len
,
475 SPLICE_F_MOVE
| SPLICE_F_MORE
);
476 DBG("splice chan to pipe ret %ld", ret
);
479 perror("Error in relay splice");
483 ret
= splice(ctx
->kconsumerd_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
484 SPLICE_F_MOVE
| SPLICE_F_MORE
);
485 DBG("splice pipe to file %ld", ret
);
488 perror("Error in file splice");
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
;
497 lttng_kconsumerd_sync_trace_file(kconsumerd_fd
, orig_offset
);
502 /* send the appropriate error description to sessiond */
505 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_EBADF
);
508 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_EINVAL
);
511 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_ENOMEM
);
514 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_ESPIPE
);
523 * Take a snapshot for a specific fd
525 * Returns 0 on success, < 0 on error
527 int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data
*ctx
,
528 struct lttng_kconsumerd_fd
*kconsumerd_fd
)
531 int infd
= kconsumerd_fd
->consumerd_fd
;
533 ret
= kernctl_snapshot(infd
);
536 perror("Getting sub-buffer snapshot.");
543 * Get the produced position
545 * Returns 0 on success, < 0 on error
547 int lttng_kconsumerd_get_produced_snapshot(
548 struct lttng_kconsumerd_local_data
*ctx
,
549 struct lttng_kconsumerd_fd
*kconsumerd_fd
,
553 int infd
= kconsumerd_fd
->consumerd_fd
;
555 ret
= kernctl_snapshot_get_produced(infd
, pos
);
558 perror("kernctl_snapshot_get_produced");
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
568 int lttng_kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
)
572 num_rdy
= poll(kconsumerd_sockpoll
, 2, -1);
574 perror("Poll error");
577 if (kconsumerd_sockpoll
[0].revents
== POLLIN
) {
578 DBG("kconsumerd_should_quit wake up");
588 * This thread polls the fds in the ltt_fd_list to consume the data and write
589 * it to tracefile if necessary.
591 void *lttng_kconsumerd_thread_poll_fds(void *data
)
593 int num_rdy
, num_hup
, high_prio
, ret
, i
;
594 struct pollfd
*pollfd
= NULL
;
595 /* local view of the fds */
596 struct lttng_kconsumerd_fd
**local_kconsumerd_fd
= NULL
;
597 /* local view of kconsumerd_data.fds_count */
601 struct lttng_kconsumerd_local_data
*ctx
= data
;
604 local_kconsumerd_fd
= malloc(sizeof(struct lttng_kconsumerd_fd
));
611 * the ltt_fd_list has been updated, we need to update our
612 * local array as well
614 pthread_mutex_lock(&kconsumerd_data
.lock
);
615 if (kconsumerd_data
.need_update
) {
616 if (pollfd
!= NULL
) {
620 if (local_kconsumerd_fd
!= NULL
) {
621 free(local_kconsumerd_fd
);
622 local_kconsumerd_fd
= NULL
;
625 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
626 pollfd
= malloc((kconsumerd_data
.fds_count
+ 1) * sizeof(struct pollfd
));
627 if (pollfd
== NULL
) {
628 perror("pollfd malloc");
629 pthread_mutex_unlock(&kconsumerd_data
.lock
);
633 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
634 local_kconsumerd_fd
= malloc((kconsumerd_data
.fds_count
+ 1) *
635 sizeof(struct lttng_kconsumerd_fd
));
636 if (local_kconsumerd_fd
== NULL
) {
637 perror("local_kconsumerd_fd malloc");
638 pthread_mutex_unlock(&kconsumerd_data
.lock
);
641 ret
= kconsumerd_update_poll_array(ctx
, &pollfd
, local_kconsumerd_fd
);
643 ERR("Error in allocating pollfd or local_outfds");
644 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_POLL_ERROR
);
645 pthread_mutex_unlock(&kconsumerd_data
.lock
);
649 kconsumerd_data
.need_update
= 0;
651 pthread_mutex_unlock(&kconsumerd_data
.lock
);
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
);
658 perror("Poll error");
659 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_POLL_ERROR
);
661 } else if (num_rdy
== 0) {
662 DBG("Polling thread timed out");
666 /* No FDs and kconsumerd_quit, kconsumerd_cleanup the thread */
667 if (nb_fd
== 0 && kconsumerd_quit
== 1) {
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.
677 if (pollfd
[nb_fd
].revents
== POLLIN
) {
678 DBG("kconsumerd_poll_pipe wake up");
679 tmp2
= read(ctx
->kconsumerd_poll_pipe
[0], &tmp
, 1);
681 perror("read kconsumerd poll");
686 /* Take care of high priority channels first. */
687 for (i
= 0; i
< nb_fd
; i
++) {
688 switch(pollfd
[i
].revents
) {
690 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
691 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
695 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
696 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
700 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
701 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
705 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
707 ret
= ctx
->on_buffer_ready(local_kconsumerd_fd
[i
]);
708 /* it's ok to have an unavailable sub-buffer */
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) {
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
);
730 ret
= ctx
->on_buffer_ready(local_kconsumerd_fd
[i
]);
731 /* it's ok to have an unavailable subbuffer */
740 DBG("polling thread exiting");
741 if (pollfd
!= NULL
) {
745 if (local_kconsumerd_fd
!= NULL
) {
746 free(local_kconsumerd_fd
);
747 local_kconsumerd_fd
= NULL
;
753 * Initialise the necessary environnement :
754 * - create a new context
755 * - create the poll_pipe
756 * - create the should_quit pipe (for signal handler)
757 * - create the thread pipe (for splice)
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.
764 * Returns a pointer to the new context or NULL on error.
766 struct lttng_kconsumerd_local_data
*lttng_kconsumerd_create(
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
))
772 struct lttng_kconsumerd_local_data
*ctx
;
774 ctx
= malloc(sizeof(struct lttng_kconsumerd_local_data
));
776 perror("allocating context");
780 /* assign the callbacks */
781 ctx
->on_buffer_ready
= buffer_ready
;
782 ctx
->on_recv_fd
= recv_fd
;
783 ctx
->on_update_fd
= update_fd
;
785 ret
= pipe(ctx
->kconsumerd_poll_pipe
);
787 perror("Error creating poll pipe");
788 goto error_poll_pipe
;
791 ret
= pipe(ctx
->kconsumerd_should_quit
);
793 perror("Error creating recv pipe");
794 goto error_quit_pipe
;
797 ret
= pipe(ctx
->kconsumerd_thread_pipe
);
799 perror("Error creating thread pipe");
800 goto error_thread_pipe
;
807 for (i
= 0; i
< 2; i
++) {
810 err
= close(ctx
->kconsumerd_should_quit
[i
]);
814 for (i
= 0; i
< 2; i
++) {
817 err
= close(ctx
->kconsumerd_poll_pipe
[i
]);
827 * Close all fds associated with the instance and free the context.
829 void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data
*ctx
)
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
);
844 * This thread listens on the consumerd socket and receives the file
845 * descriptors from the session daemon.
847 void *lttng_kconsumerd_thread_receive_fds(void *data
)
849 int sock
, client_socket
, ret
;
850 struct lttcomm_kconsumerd_header tmp
;
852 * structure to poll for incoming data on communication socket avoids
853 * making blocking sockets.
855 struct pollfd kconsumerd_sockpoll
[2];
856 struct lttng_kconsumerd_local_data
*ctx
= data
;
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
);
862 if (client_socket
< 0) {
863 ERR("Cannot create command socket");
867 ret
= lttcomm_listen_unix_sock(client_socket
);
872 DBG("Sending ready command to ltt-sessiond");
873 ret
= lttng_kconsumerd_send_error(ctx
, KCONSUMERD_COMMAND_SOCK_READY
);
875 ERR("Error sending ready command to ltt-sessiond");
879 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
881 perror("fcntl O_NONBLOCK");
885 /* prepare the FDs to poll : to client socket and the should_quit pipe */
886 kconsumerd_sockpoll
[0].fd
= ctx
->kconsumerd_should_quit
[0];
887 kconsumerd_sockpoll
[0].events
= POLLIN
| POLLPRI
;
888 kconsumerd_sockpoll
[1].fd
= client_socket
;
889 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
891 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
894 DBG("Connection on client_socket");
896 /* Blocking call, waiting for transmission */
897 sock
= lttcomm_accept_unix_sock(client_socket
);
902 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
904 perror("fcntl O_NONBLOCK");
908 /* update the polling structure to poll on the established socket */
909 kconsumerd_sockpoll
[1].fd
= sock
;
910 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
913 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
916 DBG("Incoming fds on sock");
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
));
922 ERR("Communication interrupted on command socket");
925 if (tmp
.cmd_type
== STOP
) {
926 DBG("Received STOP command");
929 if (kconsumerd_quit
) {
930 DBG("kconsumerd_thread_receive_fds received quit from signal");
934 /* we received a command to add or update fds */
935 ret
= kconsumerd_consumerd_recv_fd(ctx
, sock
, kconsumerd_sockpoll
,
936 tmp
.payload_size
, tmp
.cmd_type
);
938 ERR("Receiving the FD, exiting");
941 DBG("received fds on sock");
945 DBG("kconsumerd_thread_receive_fds exiting");
948 * when all fds have hung up, the polling thread
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).
958 kconsumerd_poll_timeout
= LTTNG_KCONSUMERD_POLL_GRACE_PERIOD
;
960 /* wake up the polling thread */
961 ret
= write(ctx
->kconsumerd_poll_pipe
[1], "4", 1);
963 perror("poll pipe write");
969 * Close all the tracefiles and stream fds, should be called when all instances
972 void lttng_kconsumerd_cleanup(void)
974 struct lttng_kconsumerd_fd
*iter
, *tmp
;
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.
981 cds_list_for_each_entry_safe(iter
, tmp
,
982 &kconsumerd_data
.fd_list
.head
, list
) {
983 kconsumerd_del_fd(iter
);
988 * Called from signal handler.
990 void lttng_kconsumerd_should_exit(struct lttng_kconsumerd_local_data
*ctx
)
994 ret
= write(ctx
->kconsumerd_should_quit
[1], "4", 1);
996 perror("write kconsumerd quit");
1001 * Send return code to the session daemon.
1003 int lttng_kconsumerd_send_error(
1004 struct lttng_kconsumerd_local_data
*ctx
, int cmd
)
1006 if (ctx
->kconsumerd_error_socket
> 0) {
1007 return lttcomm_send_unix_sock(ctx
->kconsumerd_error_socket
, &cmd
,
1008 sizeof(enum lttcomm_sessiond_command
));