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>
32 #include "libkernelctl.h"
33 #include "liblttkconsumerd.h"
37 struct kconsumerd_global_data
{
39 * kconsumerd_data.lock protects kconsumerd_data.fd_list,
40 * kconsumerd_data.fds_count, and kconsumerd_data.need_update. It
41 * ensures the count matches the number of items in the fd_list.
42 * It ensures the list updates *always* trigger an fd_array
43 * update (therefore need to make list update vs
44 * kconsumerd_data.need_update flag update atomic, and also flag
45 * read, fd array and flag clear atomic).
49 * Number of element for the list below. Protected by
50 * kconsumerd_data.lock.
52 unsigned int fds_count
;
54 * List of FDs. Protected by kconsumerd_data.lock.
56 struct kconsumerd_fd_list fd_list
;
58 * Flag specifying if the local array of FDs needs update in the
59 * poll function. Protected by kconsumerd_data.lock.
61 unsigned int need_update
;
63 .fd_list
.head
= CDS_LIST_HEAD_INIT(kconsumerd_data
.fd_list
.head
),
66 /* communication with splice */
67 static int kconsumerd_thread_pipe
[2];
69 /* pipe to wake the poll thread when necessary */
70 static int kconsumerd_poll_pipe
[2];
72 /* to let the signal handler wake up the fd receiver thread */
73 static int kconsumerd_should_quit
[2];
75 /* timeout parameter, to control the polling thread grace period */
76 static int kconsumerd_poll_timeout
= -1;
78 /* socket to communicate errors with sessiond */
79 static int kconsumerd_error_socket
;
81 /* socket to exchange commands with sessiond */
82 static char *kconsumerd_command_sock_path
;
85 * flag to inform the polling thread to quit when all fd hung up.
86 * Updated by the kconsumerd_thread_receive_fds when it notices that all
87 * fds has hung up. Also updated by the signal handler
88 * (kconsumerd_should_exit()). Read by the polling threads.
90 static volatile int kconsumerd_quit
= 0;
93 * kconsumerd_set_error_socket
95 * Set the error socket
97 void kconsumerd_set_error_socket(int sock
)
99 kconsumerd_error_socket
= sock
;
103 * kconsumerd_set_command_socket_path
105 * Set the command socket path
107 void kconsumerd_set_command_socket_path(char *sock
)
109 kconsumerd_command_sock_path
= sock
;
113 * kconsumerd_find_session_fd
115 * Find a session fd in the global list.
116 * The kconsumerd_data.lock must be locked during this call
118 * Return 1 if found else 0
120 static int kconsumerd_find_session_fd(int fd
)
122 struct kconsumerd_fd
*iter
;
124 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
125 if (iter
->sessiond_fd
== fd
) {
126 DBG("Duplicate session fd %d", fd
);
127 pthread_mutex_unlock(&kconsumerd_data
.lock
);
138 * Remove a fd from the global list protected by a mutex
140 static void kconsumerd_del_fd(struct kconsumerd_fd
*lcf
)
142 pthread_mutex_lock(&kconsumerd_data
.lock
);
143 cds_list_del(&lcf
->list
);
144 if (kconsumerd_data
.fds_count
> 0) {
145 kconsumerd_data
.fds_count
--;
148 close(lcf
->consumerd_fd
);
153 kconsumerd_data
.need_update
= 1;
154 pthread_mutex_unlock(&kconsumerd_data
.lock
);
160 * Add a fd to the global list protected by a mutex
162 static int kconsumerd_add_fd(struct lttcomm_kconsumerd_msg
*buf
, int consumerd_fd
)
165 struct kconsumerd_fd
*tmp_fd
;
167 pthread_mutex_lock(&kconsumerd_data
.lock
);
168 /* Check if already exist */
169 ret
= kconsumerd_find_session_fd(buf
->fd
);
174 tmp_fd
= malloc(sizeof(struct kconsumerd_fd
));
175 tmp_fd
->sessiond_fd
= buf
->fd
;
176 tmp_fd
->consumerd_fd
= consumerd_fd
;
177 tmp_fd
->state
= buf
->state
;
178 tmp_fd
->max_sb_size
= buf
->max_sb_size
;
179 strncpy(tmp_fd
->path_name
, buf
->path_name
, PATH_MAX
);
181 /* Opening the tracefile in write mode */
182 ret
= open(tmp_fd
->path_name
,
183 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
185 ERR("Opening %s", tmp_fd
->path_name
);
189 tmp_fd
->out_fd
= ret
;
190 tmp_fd
->out_fd_offset
= 0;
192 DBG("Adding %s (%d, %d, %d)", tmp_fd
->path_name
,
193 tmp_fd
->sessiond_fd
, tmp_fd
->consumerd_fd
, tmp_fd
->out_fd
);
195 cds_list_add(&tmp_fd
->list
, &kconsumerd_data
.fd_list
.head
);
196 kconsumerd_data
.fds_count
++;
197 kconsumerd_data
.need_update
= 1;
199 pthread_mutex_unlock(&kconsumerd_data
.lock
);
204 * kconsumerd_change_fd_state
206 * Update a fd according to what we just received
208 static void kconsumerd_change_fd_state(int sessiond_fd
,
209 enum kconsumerd_fd_state state
)
211 struct kconsumerd_fd
*iter
;
213 pthread_mutex_lock(&kconsumerd_data
.lock
);
214 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
215 if (iter
->sessiond_fd
== sessiond_fd
) {
220 kconsumerd_data
.need_update
= 1;
221 pthread_mutex_unlock(&kconsumerd_data
.lock
);
225 * kconsumerd_update_poll_array
227 * Allocate the pollfd structure and the local view of the out fds
228 * to avoid doing a lookup in the linked list and concurrency issues
229 * when writing is needed.
230 * Returns the number of fds in the structures
231 * Called with kconsumerd_data.lock held.
233 static int kconsumerd_update_poll_array(struct pollfd
**pollfd
,
234 struct kconsumerd_fd
**local_kconsumerd_fd
)
236 struct kconsumerd_fd
*iter
;
239 DBG("Updating poll fd array");
241 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
242 DBG("Inside for each");
243 if (iter
->state
== ACTIVE_FD
) {
244 DBG("Active FD %d", iter
->consumerd_fd
);
245 (*pollfd
)[i
].fd
= iter
->consumerd_fd
;
246 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
247 local_kconsumerd_fd
[i
] = iter
;
253 * insert the kconsumerd_poll_pipe at the end of the array and don't
254 * increment i so nb_fd is the number of real FD
256 (*pollfd
)[i
].fd
= kconsumerd_poll_pipe
[0];
257 (*pollfd
)[i
].events
= POLLIN
;
263 * kconsumerd_on_read_subbuffer_mmap
265 * mmap the ring buffer, read it and write the data to the tracefile.
266 * Returns the number of bytes written
268 static int kconsumerd_on_read_subbuffer_mmap(
269 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
271 unsigned long mmap_len
, mmap_offset
, padded_len
, padding_len
;
273 char *padding
= NULL
;
275 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
276 int fd
= kconsumerd_fd
->consumerd_fd
;
277 int outfd
= kconsumerd_fd
->out_fd
;
279 /* get the padded subbuffer size to know the padding required */
280 ret
= kernctl_get_padded_subbuf_size(fd
, &padded_len
);
283 perror("kernctl_get_padded_subbuf_size");
286 padding_len
= padded_len
- len
;
287 padding
= malloc(padding_len
* sizeof(char));
288 memset(padding
, '\0', padding_len
);
290 /* get the len of the mmap region */
291 ret
= kernctl_get_mmap_len(fd
, &mmap_len
);
294 perror("kernctl_get_mmap_len");
298 /* get the offset inside the fd to mmap */
299 ret
= kernctl_get_mmap_read_offset(fd
, &mmap_offset
);
302 perror("kernctl_get_mmap_read_offset");
306 mmap_base
= mmap(NULL
, mmap_len
, PROT_READ
, MAP_PRIVATE
, fd
, mmap_offset
);
307 if (mmap_base
== MAP_FAILED
) {
308 perror("Error mmaping");
314 ret
= write(outfd
, mmap_base
, len
);
317 } else if (ret
< 0) {
319 perror("Error in file write");
322 /* This won't block, but will start writeout asynchronously */
323 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
324 SYNC_FILE_RANGE_WRITE
);
325 kconsumerd_fd
->out_fd_offset
+= ret
;
328 /* once all the data is written, write the padding to disk */
329 ret
= write(outfd
, padding
, padding_len
);
332 perror("Error writing padding to file");
337 * This does a blocking write-and-wait on any page that belongs to the
338 * subbuffer prior to the one we just wrote.
339 * Don't care about error values, as these are just hints and ways to
340 * limit the amount of page cache used.
342 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
343 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
344 kconsumerd_fd
->max_sb_size
,
345 SYNC_FILE_RANGE_WAIT_BEFORE
346 | SYNC_FILE_RANGE_WRITE
347 | SYNC_FILE_RANGE_WAIT_AFTER
);
350 * Give hints to the kernel about how we access the file:
351 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
354 * We need to call fadvise again after the file grows because the
355 * kernel does not seem to apply fadvise to non-existing parts of the
358 * Call fadvise _after_ having waited for the page writeback to
359 * complete because the dirty page writeback semantic is not well
360 * defined. So it can be expected to lead to lower throughput in
363 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
364 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
369 if (padding
!= NULL
) {
376 * kconsumerd_on_read_subbuffer
378 * Splice the data from the ring buffer to the tracefile.
379 * Returns the number of bytes spliced
381 static int kconsumerd_on_read_subbuffer(
382 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
386 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
387 int fd
= kconsumerd_fd
->consumerd_fd
;
388 int outfd
= kconsumerd_fd
->out_fd
;
391 DBG("splice chan to pipe offset %lu (fd : %d)",
392 (unsigned long)offset
, fd
);
393 ret
= splice(fd
, &offset
, kconsumerd_thread_pipe
[1], NULL
, len
,
394 SPLICE_F_MOVE
| SPLICE_F_MORE
);
395 DBG("splice chan to pipe ret %ld", ret
);
398 perror("Error in relay splice");
402 ret
= splice(kconsumerd_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
403 SPLICE_F_MOVE
| SPLICE_F_MORE
);
404 DBG("splice pipe to file %ld", ret
);
407 perror("Error in file splice");
413 /* This won't block, but will start writeout asynchronously */
414 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
415 SYNC_FILE_RANGE_WRITE
);
416 kconsumerd_fd
->out_fd_offset
+= ret
;
420 * This does a blocking write-and-wait on any page that belongs to the
421 * subbuffer prior to the one we just wrote.
422 * Don't care about error values, as these are just hints and ways to
423 * limit the amount of page cache used.
425 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
426 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
427 kconsumerd_fd
->max_sb_size
,
428 SYNC_FILE_RANGE_WAIT_BEFORE
429 | SYNC_FILE_RANGE_WRITE
430 | SYNC_FILE_RANGE_WAIT_AFTER
);
432 * Give hints to the kernel about how we access the file:
433 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
436 * We need to call fadvise again after the file grows because the
437 * kernel does not seem to apply fadvise to non-existing parts of the
440 * Call fadvise _after_ having waited for the page writeback to
441 * complete because the dirty page writeback semantic is not well
442 * defined. So it can be expected to lead to lower throughput in
445 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
446 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
451 /* send the appropriate error description to sessiond */
454 kconsumerd_send_error(KCONSUMERD_SPLICE_EBADF
);
457 kconsumerd_send_error(KCONSUMERD_SPLICE_EINVAL
);
460 kconsumerd_send_error(KCONSUMERD_SPLICE_ENOMEM
);
463 kconsumerd_send_error(KCONSUMERD_SPLICE_ESPIPE
);
472 * kconsumerd_read_subbuffer
474 * Consume data on a file descriptor and write it on a trace file
476 static int kconsumerd_read_subbuffer(struct kconsumerd_fd
*kconsumerd_fd
)
481 int infd
= kconsumerd_fd
->consumerd_fd
;
483 DBG("In kconsumerd_read_subbuffer (infd : %d)", infd
);
484 /* Get the next subbuffer */
485 err
= kernctl_get_next_subbuf(infd
);
488 perror("Reserving sub buffer failed (everything is normal, "
489 "it is due to concurrency)");
493 switch (DEFAULT_KERNEL_CHANNEL_OUTPUT
) {
494 case LTTNG_EVENT_SPLICE
:
495 /* read the whole subbuffer */
496 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
499 perror("Getting sub-buffer len failed.");
503 /* splice the subbuffer to the tracefile */
504 ret
= kconsumerd_on_read_subbuffer(kconsumerd_fd
, len
);
507 * display the error but continue processing to try
508 * to release the subbuffer
510 ERR("Error splicing to tracefile");
513 case LTTNG_EVENT_MMAP
:
514 /* read the used subbuffer size */
515 err
= kernctl_get_subbuf_size(infd
, &len
);
518 perror("Getting sub-buffer len failed.");
521 /* write the subbuffer to the tracefile */
522 ret
= kconsumerd_on_read_subbuffer_mmap(kconsumerd_fd
, len
);
525 * display the error but continue processing to try
526 * to release the subbuffer
528 ERR("Error writing to tracefile");
532 ERR("Unknown output method");
536 err
= kernctl_put_next_subbuf(infd
);
539 if (errno
== EFAULT
) {
540 perror("Error in unreserving sub buffer\n");
541 } else if (errno
== EIO
) {
542 /* Should never happen with newer LTTng versions */
543 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
553 * kconsumerd_poll_socket
555 * Poll on the should_quit pipe and the command socket
556 * return -1 on error and should exit, 0 if data is
557 * available on the command socket
559 int kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
)
563 num_rdy
= poll(kconsumerd_sockpoll
, 2, -1);
565 perror("Poll error");
568 if (kconsumerd_sockpoll
[0].revents
== POLLIN
) {
569 DBG("kconsumerd_should_quit wake up");
579 * kconsumerd_consumerd_recv_fd
581 * Receives an array of file descriptors and the associated
582 * structures describing each fd (path name).
583 * Returns the size of received data
585 static int kconsumerd_consumerd_recv_fd(int sfd
,
586 struct pollfd
*kconsumerd_sockpoll
, int size
,
587 enum kconsumerd_command cmd_type
)
591 int ret
= 0, i
, tmp2
;
592 struct cmsghdr
*cmsg
;
594 char recv_fd
[CMSG_SPACE(sizeof(int))];
595 struct lttcomm_kconsumerd_msg lkm
;
597 /* the number of fds we are about to receive */
598 nb_fd
= size
/ sizeof(struct lttcomm_kconsumerd_msg
);
600 for (i
= 0; i
< nb_fd
; i
++) {
601 memset(&msg
, 0, sizeof(msg
));
603 /* Prepare to receive the structures */
604 iov
[0].iov_base
= &lkm
;
605 iov
[0].iov_len
= sizeof(lkm
);
609 msg
.msg_control
= recv_fd
;
610 msg
.msg_controllen
= sizeof(recv_fd
);
612 DBG("Waiting to receive fd");
613 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
617 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
622 if (ret
!= (size
/ nb_fd
)) {
623 ERR("Received only %d, expected %d", ret
, size
);
624 kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD
);
628 cmsg
= CMSG_FIRSTHDR(&msg
);
630 ERR("Invalid control message header");
632 kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD
);
635 /* if we received fds */
636 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
639 DBG("kconsumerd_add_fd %s (%d)", lkm
.path_name
, (CMSG_DATA(cmsg
)[0]));
640 ret
= kconsumerd_add_fd(&lkm
, (CMSG_DATA(cmsg
)[0]));
642 kconsumerd_send_error(KCONSUMERD_OUTFD_ERROR
);
647 kconsumerd_change_fd_state(lkm
.fd
, lkm
.state
);
652 /* signal the poll thread */
653 tmp2
= write(kconsumerd_poll_pipe
[1], "4", 1);
655 perror("write kconsumerd poll");
658 ERR("Didn't received any fd");
659 kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD
);
670 * kconsumerd_thread_poll_fds
672 * This thread polls the fds in the ltt_fd_list to consume the data
673 * and write it to tracefile if necessary.
675 void *kconsumerd_thread_poll_fds(void *data
)
677 int num_rdy
, num_hup
, high_prio
, ret
, i
;
678 struct pollfd
*pollfd
= NULL
;
679 /* local view of the fds */
680 struct kconsumerd_fd
**local_kconsumerd_fd
= NULL
;
681 /* local view of kconsumerd_data.fds_count */
686 ret
= pipe(kconsumerd_thread_pipe
);
688 perror("Error creating pipe");
692 local_kconsumerd_fd
= malloc(sizeof(struct kconsumerd_fd
));
699 * the ltt_fd_list has been updated, we need to update our
700 * local array as well
702 pthread_mutex_lock(&kconsumerd_data
.lock
);
703 if (kconsumerd_data
.need_update
) {
704 if (pollfd
!= NULL
) {
708 if (local_kconsumerd_fd
!= NULL
) {
709 free(local_kconsumerd_fd
);
710 local_kconsumerd_fd
= NULL
;
713 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
714 pollfd
= malloc((kconsumerd_data
.fds_count
+ 1) * sizeof(struct pollfd
));
715 if (pollfd
== NULL
) {
716 perror("pollfd malloc");
717 pthread_mutex_unlock(&kconsumerd_data
.lock
);
721 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
722 local_kconsumerd_fd
= malloc((kconsumerd_data
.fds_count
+ 1) *
723 sizeof(struct kconsumerd_fd
));
724 if (local_kconsumerd_fd
== NULL
) {
725 perror("local_kconsumerd_fd malloc");
726 pthread_mutex_unlock(&kconsumerd_data
.lock
);
729 ret
= kconsumerd_update_poll_array(&pollfd
, local_kconsumerd_fd
);
731 ERR("Error in allocating pollfd or local_outfds");
732 kconsumerd_send_error(KCONSUMERD_POLL_ERROR
);
733 pthread_mutex_unlock(&kconsumerd_data
.lock
);
737 kconsumerd_data
.need_update
= 0;
739 pthread_mutex_unlock(&kconsumerd_data
.lock
);
741 /* poll on the array of fds */
742 DBG("polling on %d fd", nb_fd
+ 1);
743 num_rdy
= poll(pollfd
, nb_fd
+ 1, kconsumerd_poll_timeout
);
744 DBG("poll num_rdy : %d", num_rdy
);
746 perror("Poll error");
747 kconsumerd_send_error(KCONSUMERD_POLL_ERROR
);
749 } else if (num_rdy
== 0) {
750 DBG("Polling thread timed out");
754 /* No FDs and kconsumerd_quit, kconsumerd_cleanup the thread */
755 if (nb_fd
== 0 && kconsumerd_quit
== 1) {
760 * If the kconsumerd_poll_pipe triggered poll go
761 * directly to the beginning of the loop to update the
762 * array. We want to prioritize array update over
763 * low-priority reads.
765 if (pollfd
[nb_fd
].revents
== POLLIN
) {
766 DBG("kconsumerd_poll_pipe wake up");
767 tmp2
= read(kconsumerd_poll_pipe
[0], &tmp
, 1);
769 perror("read kconsumerd poll");
774 /* Take care of high priority channels first. */
775 for (i
= 0; i
< nb_fd
; i
++) {
776 switch(pollfd
[i
].revents
) {
778 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
779 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
783 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
784 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
788 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
789 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
793 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
795 ret
= kconsumerd_read_subbuffer(local_kconsumerd_fd
[i
]);
796 /* it's ok to have an unavailable sub-buffer */
804 /* If every buffer FD has hung up, we end the read loop here */
805 if (nb_fd
> 0 && num_hup
== nb_fd
) {
806 DBG("every buffer FD has hung up\n");
807 if (kconsumerd_quit
== 1) {
813 /* Take care of low priority channels. */
814 if (high_prio
== 0) {
815 for (i
= 0; i
< nb_fd
; i
++) {
816 if (pollfd
[i
].revents
== POLLIN
) {
817 DBG("Normal read on fd %d", pollfd
[i
].fd
);
818 ret
= kconsumerd_read_subbuffer(local_kconsumerd_fd
[i
]);
819 /* it's ok to have an unavailable subbuffer */
828 DBG("polling thread exiting");
829 if (pollfd
!= NULL
) {
833 if (local_kconsumerd_fd
!= NULL
) {
834 free(local_kconsumerd_fd
);
835 local_kconsumerd_fd
= NULL
;
841 * kconsumerd_init(void)
843 * initialise the necessary environnement :
844 * - inform the polling thread to update the polling array
845 * - create the poll_pipe
846 * - create the should_quit pipe (for signal handler)
848 int kconsumerd_init(void)
852 /* need to update the polling array at init time */
853 kconsumerd_data
.need_update
= 1;
855 ret
= pipe(kconsumerd_poll_pipe
);
857 perror("Error creating poll pipe");
861 ret
= pipe(kconsumerd_should_quit
);
863 perror("Error creating recv pipe");
872 * kconsumerd_thread_receive_fds
874 * This thread listens on the consumerd socket and
875 * receives the file descriptors from ltt-sessiond
877 void *kconsumerd_thread_receive_fds(void *data
)
879 int sock
, client_socket
, ret
;
880 struct lttcomm_kconsumerd_header tmp
;
882 * structure to poll for incoming data on communication socket
883 * avoids making blocking sockets
885 struct pollfd kconsumerd_sockpoll
[2];
888 DBG("Creating command socket %s", kconsumerd_command_sock_path
);
889 unlink(kconsumerd_command_sock_path
);
890 client_socket
= lttcomm_create_unix_sock(kconsumerd_command_sock_path
);
891 if (client_socket
< 0) {
892 ERR("Cannot create command socket");
896 ret
= lttcomm_listen_unix_sock(client_socket
);
901 DBG("Sending ready command to ltt-sessiond");
902 ret
= kconsumerd_send_error(KCONSUMERD_COMMAND_SOCK_READY
);
904 ERR("Error sending ready command to ltt-sessiond");
908 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
910 perror("fcntl O_NONBLOCK");
914 /* prepare the FDs to poll : to client socket and the should_quit pipe */
915 kconsumerd_sockpoll
[0].fd
= kconsumerd_should_quit
[0];
916 kconsumerd_sockpoll
[0].events
= POLLIN
| POLLPRI
;
917 kconsumerd_sockpoll
[1].fd
= client_socket
;
918 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
920 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
923 DBG("Connection on client_socket");
925 /* Blocking call, waiting for transmission */
926 sock
= lttcomm_accept_unix_sock(client_socket
);
931 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
933 perror("fcntl O_NONBLOCK");
937 /* update the polling structure to poll on the established socket */
938 kconsumerd_sockpoll
[1].fd
= sock
;
939 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
942 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
945 DBG("Incoming fds on sock");
947 /* We first get the number of fd we are about to receive */
948 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
949 sizeof(struct lttcomm_kconsumerd_header
));
951 ERR("Communication interrupted on command socket");
954 if (tmp
.cmd_type
== STOP
) {
955 DBG("Received STOP command");
958 if (kconsumerd_quit
) {
959 DBG("kconsumerd_thread_receive_fds received quit from signal");
963 /* we received a command to add or update fds */
964 ret
= kconsumerd_consumerd_recv_fd(sock
, kconsumerd_sockpoll
,
965 tmp
.payload_size
, tmp
.cmd_type
);
967 ERR("Receiving the FD, exiting");
970 DBG("received fds on sock");
974 DBG("kconsumerd_thread_receive_fds exiting");
977 * when all fds have hung up, the polling thread
983 * 2s of grace period, if no polling events occur during
984 * this period, the polling thread will exit even if there
985 * are still open FDs (should not happen, but safety mechanism).
987 kconsumerd_poll_timeout
= KCONSUMERD_POLL_GRACE_PERIOD
;
989 /* wake up the polling thread */
990 ret
= write(kconsumerd_poll_pipe
[1], "4", 1);
992 perror("poll pipe write");
1000 * Cleanup the daemon's socket on exit
1002 void kconsumerd_cleanup(void)
1004 struct kconsumerd_fd
*iter
;
1006 /* remove the socket file */
1007 unlink(kconsumerd_command_sock_path
);
1010 * close all outfd. Called when there are no more threads
1011 * running (after joining on the threads), no need to protect
1012 * list iteration with mutex.
1014 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
1015 kconsumerd_del_fd(iter
);
1020 * kconsumerd_should_exit
1022 * Called from signal handler.
1024 void kconsumerd_should_exit(void)
1027 kconsumerd_quit
= 1;
1028 ret
= write(kconsumerd_should_quit
[1], "4", 1);
1030 perror("write kconsumerd quit");
1035 * kconsumerd_send_error
1037 * send return code to ltt-sessiond
1039 int kconsumerd_send_error(enum lttcomm_return_code cmd
)
1041 if (kconsumerd_error_socket
> 0) {
1042 return lttcomm_send_unix_sock(kconsumerd_error_socket
, &cmd
,
1043 sizeof(enum lttcomm_sessiond_command
));