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
);
137 * Remove a fd from the global list protected by a mutex
139 static void kconsumerd_del_fd(struct kconsumerd_fd
*lcf
)
141 pthread_mutex_lock(&kconsumerd_data
.lock
);
142 cds_list_del(&lcf
->list
);
143 if (kconsumerd_data
.fds_count
> 0) {
144 kconsumerd_data
.fds_count
--;
147 close(lcf
->consumerd_fd
);
152 kconsumerd_data
.need_update
= 1;
153 pthread_mutex_unlock(&kconsumerd_data
.lock
);
159 * Add a fd to the global list protected by a mutex
161 static int kconsumerd_add_fd(struct lttcomm_kconsumerd_msg
*buf
, int consumerd_fd
)
164 struct kconsumerd_fd
*tmp_fd
;
166 pthread_mutex_lock(&kconsumerd_data
.lock
);
167 /* Check if already exist */
168 ret
= kconsumerd_find_session_fd(buf
->fd
);
173 tmp_fd
= malloc(sizeof(struct kconsumerd_fd
));
174 tmp_fd
->sessiond_fd
= buf
->fd
;
175 tmp_fd
->consumerd_fd
= consumerd_fd
;
176 tmp_fd
->state
= buf
->state
;
177 tmp_fd
->max_sb_size
= buf
->max_sb_size
;
178 strncpy(tmp_fd
->path_name
, buf
->path_name
, PATH_MAX
);
180 /* Opening the tracefile in write mode */
181 ret
= open(tmp_fd
->path_name
,
182 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
184 ERR("Opening %s", tmp_fd
->path_name
);
188 tmp_fd
->out_fd
= ret
;
189 tmp_fd
->out_fd_offset
= 0;
191 DBG("Adding %s (%d, %d, %d)", tmp_fd
->path_name
,
192 tmp_fd
->sessiond_fd
, tmp_fd
->consumerd_fd
, tmp_fd
->out_fd
);
194 cds_list_add(&tmp_fd
->list
, &kconsumerd_data
.fd_list
.head
);
195 kconsumerd_data
.fds_count
++;
196 kconsumerd_data
.need_update
= 1;
198 pthread_mutex_unlock(&kconsumerd_data
.lock
);
203 * kconsumerd_change_fd_state
205 * Update a fd according to what we just received
207 static void kconsumerd_change_fd_state(int sessiond_fd
,
208 enum kconsumerd_fd_state state
)
210 struct kconsumerd_fd
*iter
;
212 pthread_mutex_lock(&kconsumerd_data
.lock
);
213 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
214 if (iter
->sessiond_fd
== sessiond_fd
) {
219 kconsumerd_data
.need_update
= 1;
220 pthread_mutex_unlock(&kconsumerd_data
.lock
);
224 * kconsumerd_update_poll_array
226 * Allocate the pollfd structure and the local view of the out fds
227 * to avoid doing a lookup in the linked list and concurrency issues
228 * when writing is needed.
229 * Returns the number of fds in the structures
230 * Called with kconsumerd_data.lock held.
232 static int kconsumerd_update_poll_array(struct pollfd
**pollfd
,
233 struct kconsumerd_fd
**local_kconsumerd_fd
)
235 struct kconsumerd_fd
*iter
;
238 DBG("Updating poll fd array");
240 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
241 DBG("Inside for each");
242 if (iter
->state
== ACTIVE_FD
) {
243 DBG("Active FD %d", iter
->consumerd_fd
);
244 (*pollfd
)[i
].fd
= iter
->consumerd_fd
;
245 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
246 local_kconsumerd_fd
[i
] = iter
;
252 * insert the kconsumerd_poll_pipe at the end of the array and don't
253 * increment i so nb_fd is the number of real FD
255 (*pollfd
)[i
].fd
= kconsumerd_poll_pipe
[0];
256 (*pollfd
)[i
].events
= POLLIN
;
262 * kconsumerd_on_read_subbuffer_mmap
264 * mmap the ring buffer, read it and write the data to the tracefile.
265 * Returns the number of bytes written
267 static int kconsumerd_on_read_subbuffer_mmap(
268 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
270 unsigned long mmap_len
, mmap_offset
, padded_len
, padding_len
;
272 char *padding
= NULL
;
274 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
275 int fd
= kconsumerd_fd
->consumerd_fd
;
276 int outfd
= kconsumerd_fd
->out_fd
;
278 /* get the padded subbuffer size to know the padding required */
279 ret
= kernctl_get_padded_subbuf_size(fd
, &padded_len
);
282 perror("kernctl_get_padded_subbuf_size");
285 padding_len
= padded_len
- len
;
286 padding
= malloc(padding_len
* sizeof(char));
287 memset(padding
, '\0', padding_len
);
289 /* get the len of the mmap region */
290 ret
= kernctl_get_mmap_len(fd
, &mmap_len
);
293 perror("kernctl_get_mmap_len");
297 /* get the offset inside the fd to mmap */
298 ret
= kernctl_get_mmap_read_offset(fd
, &mmap_offset
);
301 perror("kernctl_get_mmap_read_offset");
305 mmap_base
= mmap(NULL
, mmap_len
, PROT_READ
, MAP_PRIVATE
, fd
, mmap_offset
);
306 if (mmap_base
== MAP_FAILED
) {
307 perror("Error mmaping");
313 ret
= write(outfd
, mmap_base
, len
);
316 } else if (ret
< 0) {
318 perror("Error in file write");
321 /* This won't block, but will start writeout asynchronously */
322 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
323 SYNC_FILE_RANGE_WRITE
);
324 kconsumerd_fd
->out_fd_offset
+= ret
;
327 /* once all the data is written, write the padding to disk */
328 ret
= write(outfd
, padding
, padding_len
);
331 perror("Error writing padding to file");
336 * This does a blocking write-and-wait on any page that belongs to the
337 * subbuffer prior to the one we just wrote.
338 * Don't care about error values, as these are just hints and ways to
339 * limit the amount of page cache used.
341 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
342 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
343 kconsumerd_fd
->max_sb_size
,
344 SYNC_FILE_RANGE_WAIT_BEFORE
345 | SYNC_FILE_RANGE_WRITE
346 | SYNC_FILE_RANGE_WAIT_AFTER
);
349 * Give hints to the kernel about how we access the file:
350 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
353 * We need to call fadvise again after the file grows because the
354 * kernel does not seem to apply fadvise to non-existing parts of the
357 * Call fadvise _after_ having waited for the page writeback to
358 * complete because the dirty page writeback semantic is not well
359 * defined. So it can be expected to lead to lower throughput in
362 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
363 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
368 if (padding
!= NULL
) {
375 * kconsumerd_on_read_subbuffer
377 * Splice the data from the ring buffer to the tracefile.
378 * Returns the number of bytes spliced
380 static int kconsumerd_on_read_subbuffer(
381 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
385 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
386 int fd
= kconsumerd_fd
->consumerd_fd
;
387 int outfd
= kconsumerd_fd
->out_fd
;
390 DBG("splice chan to pipe offset %lu (fd : %d)",
391 (unsigned long)offset
, fd
);
392 ret
= splice(fd
, &offset
, kconsumerd_thread_pipe
[1], NULL
, len
,
393 SPLICE_F_MOVE
| SPLICE_F_MORE
);
394 DBG("splice chan to pipe ret %ld", ret
);
397 perror("Error in relay splice");
401 ret
= splice(kconsumerd_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
402 SPLICE_F_MOVE
| SPLICE_F_MORE
);
403 DBG("splice pipe to file %ld", ret
);
406 perror("Error in file splice");
412 /* This won't block, but will start writeout asynchronously */
413 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
414 SYNC_FILE_RANGE_WRITE
);
415 kconsumerd_fd
->out_fd_offset
+= ret
;
419 * This does a blocking write-and-wait on any page that belongs to the
420 * subbuffer prior to the one we just wrote.
421 * Don't care about error values, as these are just hints and ways to
422 * limit the amount of page cache used.
424 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
425 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
426 kconsumerd_fd
->max_sb_size
,
427 SYNC_FILE_RANGE_WAIT_BEFORE
428 | SYNC_FILE_RANGE_WRITE
429 | SYNC_FILE_RANGE_WAIT_AFTER
);
431 * Give hints to the kernel about how we access the file:
432 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
435 * We need to call fadvise again after the file grows because the
436 * kernel does not seem to apply fadvise to non-existing parts of the
439 * Call fadvise _after_ having waited for the page writeback to
440 * complete because the dirty page writeback semantic is not well
441 * defined. So it can be expected to lead to lower throughput in
444 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
445 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
450 /* send the appropriate error description to sessiond */
453 kconsumerd_send_error(KCONSUMERD_SPLICE_EBADF
);
456 kconsumerd_send_error(KCONSUMERD_SPLICE_EINVAL
);
459 kconsumerd_send_error(KCONSUMERD_SPLICE_ENOMEM
);
462 kconsumerd_send_error(KCONSUMERD_SPLICE_ESPIPE
);
471 * kconsumerd_read_subbuffer
473 * Consume data on a file descriptor and write it on a trace file
475 static int kconsumerd_read_subbuffer(struct kconsumerd_fd
*kconsumerd_fd
)
480 int infd
= kconsumerd_fd
->consumerd_fd
;
482 DBG("In kconsumerd_read_subbuffer (infd : %d)", infd
);
483 /* Get the next subbuffer */
484 err
= kernctl_get_next_subbuf(infd
);
487 perror("Reserving sub buffer failed (everything is normal, "
488 "it is due to concurrency)");
492 switch (DEFAULT_KERNEL_CHANNEL_OUTPUT
) {
493 case LTTNG_EVENT_SPLICE
:
494 /* read the whole subbuffer */
495 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
498 perror("Getting sub-buffer len failed.");
502 /* splice the subbuffer to the tracefile */
503 ret
= kconsumerd_on_read_subbuffer(kconsumerd_fd
, len
);
506 * display the error but continue processing to try
507 * to release the subbuffer
509 ERR("Error splicing to tracefile");
512 case LTTNG_EVENT_MMAP
:
513 /* read the used subbuffer size */
514 err
= kernctl_get_subbuf_size(infd
, &len
);
517 perror("Getting sub-buffer len failed.");
520 /* write the subbuffer to the tracefile */
521 ret
= kconsumerd_on_read_subbuffer_mmap(kconsumerd_fd
, len
);
524 * display the error but continue processing to try
525 * to release the subbuffer
527 ERR("Error writing to tracefile");
531 ERR("Unknown output method");
535 err
= kernctl_put_next_subbuf(infd
);
538 if (errno
== EFAULT
) {
539 perror("Error in unreserving sub buffer\n");
540 } else if (errno
== EIO
) {
541 /* Should never happen with newer LTTng versions */
542 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
552 * kconsumerd_poll_socket
554 * Poll on the should_quit pipe and the command socket
555 * return -1 on error and should exit, 0 if data is
556 * available on the command socket
558 int kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
)
562 num_rdy
= poll(kconsumerd_sockpoll
, 2, -1);
564 perror("Poll error");
567 if (kconsumerd_sockpoll
[0].revents
== POLLIN
) {
568 DBG("kconsumerd_should_quit wake up");
578 * kconsumerd_consumerd_recv_fd
580 * Receives an array of file descriptors and the associated
581 * structures describing each fd (path name).
582 * Returns the size of received data
584 static int kconsumerd_consumerd_recv_fd(int sfd
,
585 struct pollfd
*kconsumerd_sockpoll
, int size
,
586 enum kconsumerd_command cmd_type
)
590 int ret
= 0, i
, tmp2
;
591 struct cmsghdr
*cmsg
;
593 char recv_fd
[CMSG_SPACE(sizeof(int))];
594 struct lttcomm_kconsumerd_msg lkm
;
596 /* the number of fds we are about to receive */
597 nb_fd
= size
/ sizeof(struct lttcomm_kconsumerd_msg
);
599 for (i
= 0; i
< nb_fd
; i
++) {
600 memset(&msg
, 0, sizeof(msg
));
602 /* Prepare to receive the structures */
603 iov
[0].iov_base
= &lkm
;
604 iov
[0].iov_len
= sizeof(lkm
);
608 msg
.msg_control
= recv_fd
;
609 msg
.msg_controllen
= sizeof(recv_fd
);
611 DBG("Waiting to receive fd");
612 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
616 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
621 if (ret
!= (size
/ nb_fd
)) {
622 ERR("Received only %d, expected %d", ret
, size
);
623 kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD
);
627 cmsg
= CMSG_FIRSTHDR(&msg
);
629 ERR("Invalid control message header");
631 kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD
);
634 /* if we received fds */
635 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
638 DBG("kconsumerd_add_fd %s (%d)", lkm
.path_name
, (CMSG_DATA(cmsg
)[0]));
639 ret
= kconsumerd_add_fd(&lkm
, (CMSG_DATA(cmsg
)[0]));
641 kconsumerd_send_error(KCONSUMERD_OUTFD_ERROR
);
646 kconsumerd_change_fd_state(lkm
.fd
, lkm
.state
);
651 /* signal the poll thread */
652 tmp2
= write(kconsumerd_poll_pipe
[1], "4", 1);
654 perror("write kconsumerd poll");
657 ERR("Didn't received any fd");
658 kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD
);
669 * kconsumerd_thread_poll_fds
671 * This thread polls the fds in the ltt_fd_list to consume the data
672 * and write it to tracefile if necessary.
674 void *kconsumerd_thread_poll_fds(void *data
)
676 int num_rdy
, num_hup
, high_prio
, ret
, i
;
677 struct pollfd
*pollfd
= NULL
;
678 /* local view of the fds */
679 struct kconsumerd_fd
**local_kconsumerd_fd
= NULL
;
680 /* local view of kconsumerd_data.fds_count */
685 ret
= pipe(kconsumerd_thread_pipe
);
687 perror("Error creating pipe");
691 local_kconsumerd_fd
= malloc(sizeof(struct kconsumerd_fd
));
698 * the ltt_fd_list has been updated, we need to update our
699 * local array as well
701 pthread_mutex_lock(&kconsumerd_data
.lock
);
702 if (kconsumerd_data
.need_update
) {
703 if (pollfd
!= NULL
) {
707 if (local_kconsumerd_fd
!= NULL
) {
708 free(local_kconsumerd_fd
);
709 local_kconsumerd_fd
= NULL
;
712 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
713 pollfd
= malloc((kconsumerd_data
.fds_count
+ 1) * sizeof(struct pollfd
));
714 if (pollfd
== NULL
) {
715 perror("pollfd malloc");
716 pthread_mutex_unlock(&kconsumerd_data
.lock
);
720 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
721 local_kconsumerd_fd
= malloc((kconsumerd_data
.fds_count
+ 1) *
722 sizeof(struct kconsumerd_fd
));
723 if (local_kconsumerd_fd
== NULL
) {
724 perror("local_kconsumerd_fd malloc");
725 pthread_mutex_unlock(&kconsumerd_data
.lock
);
728 ret
= kconsumerd_update_poll_array(&pollfd
, local_kconsumerd_fd
);
730 ERR("Error in allocating pollfd or local_outfds");
731 kconsumerd_send_error(KCONSUMERD_POLL_ERROR
);
732 pthread_mutex_unlock(&kconsumerd_data
.lock
);
736 kconsumerd_data
.need_update
= 0;
738 pthread_mutex_unlock(&kconsumerd_data
.lock
);
740 /* poll on the array of fds */
741 DBG("polling on %d fd", nb_fd
+ 1);
742 num_rdy
= poll(pollfd
, nb_fd
+ 1, kconsumerd_poll_timeout
);
743 DBG("poll num_rdy : %d", num_rdy
);
745 perror("Poll error");
746 kconsumerd_send_error(KCONSUMERD_POLL_ERROR
);
748 } else if (num_rdy
== 0) {
749 DBG("Polling thread timed out");
753 /* No FDs and kconsumerd_quit, kconsumerd_cleanup the thread */
754 if (nb_fd
== 0 && kconsumerd_quit
== 1) {
759 * If the kconsumerd_poll_pipe triggered poll go
760 * directly to the beginning of the loop to update the
761 * array. We want to prioritize array update over
762 * low-priority reads.
764 if (pollfd
[nb_fd
].revents
== POLLIN
) {
765 DBG("kconsumerd_poll_pipe wake up");
766 tmp2
= read(kconsumerd_poll_pipe
[0], &tmp
, 1);
768 perror("read kconsumerd poll");
773 /* Take care of high priority channels first. */
774 for (i
= 0; i
< nb_fd
; i
++) {
775 switch(pollfd
[i
].revents
) {
777 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
778 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
782 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
783 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
787 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
788 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
792 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
794 ret
= kconsumerd_read_subbuffer(local_kconsumerd_fd
[i
]);
795 /* it's ok to have an unavailable sub-buffer */
803 /* If every buffer FD has hung up, we end the read loop here */
804 if (nb_fd
> 0 && num_hup
== nb_fd
) {
805 DBG("every buffer FD has hung up\n");
806 if (kconsumerd_quit
== 1) {
812 /* Take care of low priority channels. */
813 if (high_prio
== 0) {
814 for (i
= 0; i
< nb_fd
; i
++) {
815 if (pollfd
[i
].revents
== POLLIN
) {
816 DBG("Normal read on fd %d", pollfd
[i
].fd
);
817 ret
= kconsumerd_read_subbuffer(local_kconsumerd_fd
[i
]);
818 /* it's ok to have an unavailable subbuffer */
827 DBG("polling thread exiting");
828 if (pollfd
!= NULL
) {
832 if (local_kconsumerd_fd
!= NULL
) {
833 free(local_kconsumerd_fd
);
834 local_kconsumerd_fd
= NULL
;
840 * kconsumerd_init(void)
842 * initialise the necessary environnement :
843 * - inform the polling thread to update the polling array
844 * - create the poll_pipe
845 * - create the should_quit pipe (for signal handler)
847 int kconsumerd_init(void)
851 /* need to update the polling array at init time */
852 kconsumerd_data
.need_update
= 1;
854 ret
= pipe(kconsumerd_poll_pipe
);
856 perror("Error creating poll pipe");
860 ret
= pipe(kconsumerd_should_quit
);
862 perror("Error creating recv pipe");
871 * kconsumerd_thread_receive_fds
873 * This thread listens on the consumerd socket and
874 * receives the file descriptors from ltt-sessiond
876 void *kconsumerd_thread_receive_fds(void *data
)
878 int sock
, client_socket
, ret
;
879 struct lttcomm_kconsumerd_header tmp
;
881 * structure to poll for incoming data on communication socket
882 * avoids making blocking sockets
884 struct pollfd kconsumerd_sockpoll
[2];
887 DBG("Creating command socket %s", kconsumerd_command_sock_path
);
888 unlink(kconsumerd_command_sock_path
);
889 client_socket
= lttcomm_create_unix_sock(kconsumerd_command_sock_path
);
890 if (client_socket
< 0) {
891 ERR("Cannot create command socket");
895 ret
= lttcomm_listen_unix_sock(client_socket
);
900 DBG("Sending ready command to ltt-sessiond");
901 ret
= kconsumerd_send_error(KCONSUMERD_COMMAND_SOCK_READY
);
903 ERR("Error sending ready command to ltt-sessiond");
907 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
909 perror("fcntl O_NONBLOCK");
913 /* prepare the FDs to poll : to client socket and the should_quit pipe */
914 kconsumerd_sockpoll
[0].fd
= kconsumerd_should_quit
[0];
915 kconsumerd_sockpoll
[0].events
= POLLIN
| POLLPRI
;
916 kconsumerd_sockpoll
[1].fd
= client_socket
;
917 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
919 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
922 DBG("Connection on client_socket");
924 /* Blocking call, waiting for transmission */
925 sock
= lttcomm_accept_unix_sock(client_socket
);
930 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
932 perror("fcntl O_NONBLOCK");
936 /* update the polling structure to poll on the established socket */
937 kconsumerd_sockpoll
[1].fd
= sock
;
938 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
941 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
944 DBG("Incoming fds on sock");
946 /* We first get the number of fd we are about to receive */
947 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
948 sizeof(struct lttcomm_kconsumerd_header
));
950 ERR("Communication interrupted on command socket");
953 if (tmp
.cmd_type
== STOP
) {
954 DBG("Received STOP command");
957 if (kconsumerd_quit
) {
958 DBG("kconsumerd_thread_receive_fds received quit from signal");
962 /* we received a command to add or update fds */
963 ret
= kconsumerd_consumerd_recv_fd(sock
, kconsumerd_sockpoll
,
964 tmp
.payload_size
, tmp
.cmd_type
);
966 ERR("Receiving the FD, exiting");
969 DBG("received fds on sock");
973 DBG("kconsumerd_thread_receive_fds exiting");
976 * when all fds have hung up, the polling thread
982 * 2s of grace period, if no polling events occur during
983 * this period, the polling thread will exit even if there
984 * are still open FDs (should not happen, but safety mechanism).
986 kconsumerd_poll_timeout
= KCONSUMERD_POLL_GRACE_PERIOD
;
988 /* wake up the polling thread */
989 ret
= write(kconsumerd_poll_pipe
[1], "4", 1);
991 perror("poll pipe write");
999 * Cleanup the daemon's socket on exit
1001 void kconsumerd_cleanup(void)
1003 struct kconsumerd_fd
*iter
;
1005 /* remove the socket file */
1006 unlink(kconsumerd_command_sock_path
);
1009 * close all outfd. Called when there are no more threads
1010 * running (after joining on the threads), no need to protect
1011 * list iteration with mutex.
1013 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
1014 kconsumerd_del_fd(iter
);
1019 * kconsumerd_should_exit
1021 * Called from signal handler.
1023 void kconsumerd_should_exit(void)
1026 kconsumerd_quit
= 1;
1027 ret
= write(kconsumerd_should_quit
[1], "4", 1);
1029 perror("write kconsumerd quit");
1034 * kconsumerd_send_error
1036 * send return code to ltt-sessiond
1038 int kconsumerd_send_error(enum lttcomm_return_code cmd
)
1040 if (kconsumerd_error_socket
> 0) {
1041 return lttcomm_send_unix_sock(kconsumerd_error_socket
, &cmd
,
1042 sizeof(enum lttcomm_sessiond_command
));