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 "kernelctl.h"
34 #include "lttkconsumerd.h"
38 struct kconsumerd_global_data
{
40 * kconsumerd_data.lock protects kconsumerd_data.fd_list,
41 * kconsumerd_data.fds_count, and kconsumerd_data.need_update. It
42 * ensures the count matches the number of items in the fd_list.
43 * It ensures the list updates *always* trigger an fd_array
44 * update (therefore need to make list update vs
45 * kconsumerd_data.need_update flag update atomic, and also flag
46 * read, fd array and flag clear atomic).
50 * Number of element for the list below. Protected by
51 * kconsumerd_data.lock.
53 unsigned int fds_count
;
55 * List of FDs. Protected by kconsumerd_data.lock.
57 struct kconsumerd_fd_list fd_list
;
59 * Flag specifying if the local array of FDs needs update in the
60 * poll function. Protected by kconsumerd_data.lock.
62 unsigned int need_update
;
64 .fd_list
.head
= CDS_LIST_HEAD_INIT(kconsumerd_data
.fd_list
.head
),
69 /* timeout parameter, to control the polling thread grace period */
70 static int kconsumerd_poll_timeout
= -1;
73 * flag to inform the polling thread to quit when all fd hung up.
74 * Updated by the kconsumerd_thread_receive_fds when it notices that all
75 * fds has hung up. Also updated by the signal handler
76 * (kconsumerd_should_exit()). Read by the polling threads.
78 static volatile int kconsumerd_quit
= 0;
81 * kconsumerd_set_error_socket
83 * Set the error socket
85 void kconsumerd_set_error_socket(struct kconsumerd_local_data
*ctx
, int sock
)
87 ctx
->kconsumerd_error_socket
= sock
;
91 * kconsumerd_set_command_socket_path
93 * Set the command socket path
95 void kconsumerd_set_command_socket_path(struct kconsumerd_local_data
*ctx
,
98 ctx
->kconsumerd_command_sock_path
= sock
;
102 * kconsumerd_find_session_fd
104 * Find a session fd in the global list.
105 * The kconsumerd_data.lock must be locked during this call
107 * Return 1 if found else 0
109 static int kconsumerd_find_session_fd(int fd
)
111 struct kconsumerd_fd
*iter
;
113 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
114 if (iter
->sessiond_fd
== fd
) {
115 DBG("Duplicate session fd %d", fd
);
126 * Remove a fd from the global list protected by a mutex
128 static void kconsumerd_del_fd(struct kconsumerd_fd
*lcf
)
131 pthread_mutex_lock(&kconsumerd_data
.lock
);
132 cds_list_del(&lcf
->list
);
133 if (kconsumerd_data
.fds_count
> 0) {
134 kconsumerd_data
.fds_count
--;
136 if (lcf
->mmap_base
!= NULL
) {
137 ret
= munmap(lcf
->mmap_base
, lcf
->mmap_len
);
142 if (lcf
->out_fd
!= 0) {
145 close(lcf
->consumerd_fd
);
150 kconsumerd_data
.need_update
= 1;
151 pthread_mutex_unlock(&kconsumerd_data
.lock
);
157 * Add a fd to the global list protected by a mutex
159 static int kconsumerd_add_fd(struct lttcomm_kconsumerd_msg
*buf
, int consumerd_fd
)
161 struct kconsumerd_fd
*tmp_fd
;
164 pthread_mutex_lock(&kconsumerd_data
.lock
);
165 /* Check if already exist */
166 ret
= kconsumerd_find_session_fd(buf
->fd
);
171 tmp_fd
= malloc(sizeof(struct kconsumerd_fd
));
172 tmp_fd
->sessiond_fd
= buf
->fd
;
173 tmp_fd
->consumerd_fd
= consumerd_fd
;
174 tmp_fd
->state
= buf
->state
;
175 tmp_fd
->max_sb_size
= buf
->max_sb_size
;
177 tmp_fd
->out_fd_offset
= 0;
178 tmp_fd
->mmap_len
= 0;
179 tmp_fd
->mmap_base
= NULL
;
180 tmp_fd
->output
= buf
->output
;
181 strncpy(tmp_fd
->path_name
, buf
->path_name
, PATH_MAX
);
182 tmp_fd
->path_name
[PATH_MAX
- 1] = '\0';
184 /* Opening the tracefile in write mode */
185 if (tmp_fd
->path_name
!= NULL
) {
186 ret
= open(tmp_fd
->path_name
,
187 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
189 ERR("Opening %s", tmp_fd
->path_name
);
193 tmp_fd
->out_fd
= ret
;
194 DBG("Adding %s (%d, %d, %d)", tmp_fd
->path_name
,
195 tmp_fd
->sessiond_fd
, tmp_fd
->consumerd_fd
, tmp_fd
->out_fd
);
198 if (tmp_fd
->output
== LTTNG_EVENT_MMAP
) {
199 /* get the len of the mmap region */
200 ret
= kernctl_get_mmap_len(tmp_fd
->consumerd_fd
, &tmp_fd
->mmap_len
);
203 perror("kernctl_get_mmap_len");
207 tmp_fd
->mmap_base
= mmap(NULL
, tmp_fd
->mmap_len
,
208 PROT_READ
, MAP_PRIVATE
, tmp_fd
->consumerd_fd
, 0);
209 if (tmp_fd
->mmap_base
== MAP_FAILED
) {
210 perror("Error mmaping");
216 cds_list_add(&tmp_fd
->list
, &kconsumerd_data
.fd_list
.head
);
217 kconsumerd_data
.fds_count
++;
218 kconsumerd_data
.need_update
= 1;
220 pthread_mutex_unlock(&kconsumerd_data
.lock
);
225 * kconsumerd_change_fd_state
227 * Update a fd according to what we just received
229 static void kconsumerd_change_fd_state(int sessiond_fd
,
230 enum kconsumerd_fd_state state
)
232 struct kconsumerd_fd
*iter
;
234 pthread_mutex_lock(&kconsumerd_data
.lock
);
235 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
236 if (iter
->sessiond_fd
== sessiond_fd
) {
241 kconsumerd_data
.need_update
= 1;
242 pthread_mutex_unlock(&kconsumerd_data
.lock
);
246 * kconsumerd_update_poll_array
248 * Allocate the pollfd structure and the local view of the out fds
249 * to avoid doing a lookup in the linked list and concurrency issues
250 * when writing is needed.
251 * Returns the number of fds in the structures
252 * Called with kconsumerd_data.lock held.
254 static int kconsumerd_update_poll_array(struct kconsumerd_local_data
*ctx
,
255 struct pollfd
**pollfd
, struct kconsumerd_fd
**local_kconsumerd_fd
)
257 struct kconsumerd_fd
*iter
;
260 DBG("Updating poll fd array");
261 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
262 if (iter
->state
== ACTIVE_FD
) {
263 DBG("Active FD %d", iter
->consumerd_fd
);
264 (*pollfd
)[i
].fd
= iter
->consumerd_fd
;
265 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
266 local_kconsumerd_fd
[i
] = iter
;
272 * insert the kconsumerd_poll_pipe at the end of the array and don't
273 * increment i so nb_fd is the number of real FD
275 (*pollfd
)[i
].fd
= ctx
->kconsumerd_poll_pipe
[0];
276 (*pollfd
)[i
].events
= POLLIN
;
282 * kconsumerd_on_read_subbuffer_mmap
284 * mmap the ring buffer, read it and write the data to the tracefile.
285 * Returns the number of bytes written
287 int kconsumerd_on_read_subbuffer_mmap(struct kconsumerd_local_data
*ctx
,
288 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
290 unsigned long mmap_offset
;
291 char *padding
= NULL
;
293 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
294 int fd
= kconsumerd_fd
->consumerd_fd
;
295 int outfd
= kconsumerd_fd
->out_fd
;
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");
306 ret
= write(outfd
, kconsumerd_fd
->mmap_base
+ mmap_offset
, len
);
309 } else if (ret
< 0) {
311 perror("Error in file write");
314 /* This won't block, but will start writeout asynchronously */
315 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
316 SYNC_FILE_RANGE_WRITE
);
317 kconsumerd_fd
->out_fd_offset
+= ret
;
321 * This does a blocking write-and-wait on any page that belongs to the
322 * subbuffer prior to the one we just wrote.
323 * Don't care about error values, as these are just hints and ways to
324 * limit the amount of page cache used.
326 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
327 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
328 kconsumerd_fd
->max_sb_size
,
329 SYNC_FILE_RANGE_WAIT_BEFORE
330 | SYNC_FILE_RANGE_WRITE
331 | SYNC_FILE_RANGE_WAIT_AFTER
);
334 * Give hints to the kernel about how we access the file:
335 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
338 * We need to call fadvise again after the file grows because the
339 * kernel does not seem to apply fadvise to non-existing parts of the
342 * Call fadvise _after_ having waited for the page writeback to
343 * complete because the dirty page writeback semantic is not well
344 * defined. So it can be expected to lead to lower throughput in
347 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
348 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
353 if (padding
!= NULL
) {
360 * kconsumerd_on_read_subbuffer
362 * Splice the data from the ring buffer to the tracefile.
363 * Returns the number of bytes spliced
365 int kconsumerd_on_read_subbuffer_splice(struct kconsumerd_local_data
*ctx
,
366 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
370 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
371 int fd
= kconsumerd_fd
->consumerd_fd
;
372 int outfd
= kconsumerd_fd
->out_fd
;
375 DBG("splice chan to pipe offset %lu (fd : %d)",
376 (unsigned long)offset
, fd
);
377 ret
= splice(fd
, &offset
, ctx
->kconsumerd_thread_pipe
[1], NULL
, len
,
378 SPLICE_F_MOVE
| SPLICE_F_MORE
);
379 DBG("splice chan to pipe ret %ld", ret
);
382 perror("Error in relay splice");
386 ret
= splice(ctx
->kconsumerd_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
387 SPLICE_F_MOVE
| SPLICE_F_MORE
);
388 DBG("splice pipe to file %ld", ret
);
391 perror("Error in file splice");
397 /* This won't block, but will start writeout asynchronously */
398 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
399 SYNC_FILE_RANGE_WRITE
);
400 kconsumerd_fd
->out_fd_offset
+= ret
;
404 * This does a blocking write-and-wait on any page that belongs to the
405 * subbuffer prior to the one we just wrote.
406 * Don't care about error values, as these are just hints and ways to
407 * limit the amount of page cache used.
409 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
410 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
411 kconsumerd_fd
->max_sb_size
,
412 SYNC_FILE_RANGE_WAIT_BEFORE
413 | SYNC_FILE_RANGE_WRITE
414 | SYNC_FILE_RANGE_WAIT_AFTER
);
416 * Give hints to the kernel about how we access the file:
417 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
420 * We need to call fadvise again after the file grows because the
421 * kernel does not seem to apply fadvise to non-existing parts of the
424 * Call fadvise _after_ having waited for the page writeback to
425 * complete because the dirty page writeback semantic is not well
426 * defined. So it can be expected to lead to lower throughput in
429 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
430 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
435 /* send the appropriate error description to sessiond */
438 kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_EBADF
);
441 kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_EINVAL
);
444 kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_ENOMEM
);
447 kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_ESPIPE
);
456 * kconsumerd_poll_socket
458 * Poll on the should_quit pipe and the command socket
459 * return -1 on error and should exit, 0 if data is
460 * available on the command socket
462 int kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
)
466 num_rdy
= poll(kconsumerd_sockpoll
, 2, -1);
468 perror("Poll error");
471 if (kconsumerd_sockpoll
[0].revents
== POLLIN
) {
472 DBG("kconsumerd_should_quit wake up");
482 * kconsumerd_consumerd_recv_fd
484 * Receives an array of file descriptors and the associated
485 * structures describing each fd (path name).
486 * Returns the size of received data
488 static int kconsumerd_consumerd_recv_fd(struct kconsumerd_local_data
*ctx
,
489 int sfd
, struct pollfd
*kconsumerd_sockpoll
, int size
,
490 enum kconsumerd_command cmd_type
)
493 int ret
= 0, i
, tmp2
;
494 struct cmsghdr
*cmsg
;
496 char recv_fd
[CMSG_SPACE(sizeof(int))];
497 struct lttcomm_kconsumerd_msg lkm
;
499 /* the number of fds we are about to receive */
500 nb_fd
= size
/ sizeof(struct lttcomm_kconsumerd_msg
);
503 * nb_fd is the number of fds we receive. One fd per recvmsg.
505 for (i
= 0; i
< nb_fd
; i
++) {
506 struct msghdr msg
= { 0 };
508 /* Prepare to receive the structures */
509 iov
[0].iov_base
= &lkm
;
510 iov
[0].iov_len
= sizeof(lkm
);
514 msg
.msg_control
= recv_fd
;
515 msg
.msg_controllen
= sizeof(recv_fd
);
517 DBG("Waiting to receive fd");
518 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
522 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
527 if (ret
!= (size
/ nb_fd
)) {
528 ERR("Received only %d, expected %d", ret
, size
);
529 kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
533 cmsg
= CMSG_FIRSTHDR(&msg
);
535 ERR("Invalid control message header");
537 kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
541 /* if we received fds */
542 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
545 DBG("kconsumerd_add_fd %s (%d)", lkm
.path_name
, ((int *) CMSG_DATA(cmsg
))[0]);
546 ret
= kconsumerd_add_fd(&lkm
, ((int *) CMSG_DATA(cmsg
))[0]);
548 kconsumerd_send_error(ctx
, KCONSUMERD_OUTFD_ERROR
);
553 kconsumerd_change_fd_state(lkm
.fd
, lkm
.state
);
558 /* signal the poll thread */
559 tmp2
= write(ctx
->kconsumerd_poll_pipe
[1], "4", 1);
561 perror("write kconsumerd poll");
564 ERR("Didn't received any fd");
565 kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
576 * kconsumerd_thread_poll_fds
578 * This thread polls the fds in the ltt_fd_list to consume the data
579 * and write it to tracefile if necessary.
581 void *kconsumerd_thread_poll_fds(void *data
)
583 int num_rdy
, num_hup
, high_prio
, ret
, i
;
584 struct pollfd
*pollfd
= NULL
;
585 /* local view of the fds */
586 struct kconsumerd_fd
**local_kconsumerd_fd
= NULL
;
587 /* local view of kconsumerd_data.fds_count */
591 struct kconsumerd_local_data
*ctx
= data
;
594 local_kconsumerd_fd
= malloc(sizeof(struct kconsumerd_fd
));
601 * the ltt_fd_list has been updated, we need to update our
602 * local array as well
604 pthread_mutex_lock(&kconsumerd_data
.lock
);
605 if (kconsumerd_data
.need_update
) {
606 if (pollfd
!= NULL
) {
610 if (local_kconsumerd_fd
!= NULL
) {
611 free(local_kconsumerd_fd
);
612 local_kconsumerd_fd
= NULL
;
615 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
616 pollfd
= malloc((kconsumerd_data
.fds_count
+ 1) * sizeof(struct pollfd
));
617 if (pollfd
== NULL
) {
618 perror("pollfd malloc");
619 pthread_mutex_unlock(&kconsumerd_data
.lock
);
623 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
624 local_kconsumerd_fd
= malloc((kconsumerd_data
.fds_count
+ 1) *
625 sizeof(struct kconsumerd_fd
));
626 if (local_kconsumerd_fd
== NULL
) {
627 perror("local_kconsumerd_fd malloc");
628 pthread_mutex_unlock(&kconsumerd_data
.lock
);
631 ret
= kconsumerd_update_poll_array(ctx
, &pollfd
, local_kconsumerd_fd
);
633 ERR("Error in allocating pollfd or local_outfds");
634 kconsumerd_send_error(ctx
, KCONSUMERD_POLL_ERROR
);
635 pthread_mutex_unlock(&kconsumerd_data
.lock
);
639 kconsumerd_data
.need_update
= 0;
641 pthread_mutex_unlock(&kconsumerd_data
.lock
);
643 /* poll on the array of fds */
644 DBG("polling on %d fd", nb_fd
+ 1);
645 num_rdy
= poll(pollfd
, nb_fd
+ 1, kconsumerd_poll_timeout
);
646 DBG("poll num_rdy : %d", num_rdy
);
648 perror("Poll error");
649 kconsumerd_send_error(ctx
, KCONSUMERD_POLL_ERROR
);
651 } else if (num_rdy
== 0) {
652 DBG("Polling thread timed out");
656 /* No FDs and kconsumerd_quit, kconsumerd_cleanup the thread */
657 if (nb_fd
== 0 && kconsumerd_quit
== 1) {
662 * If the kconsumerd_poll_pipe triggered poll go
663 * directly to the beginning of the loop to update the
664 * array. We want to prioritize array update over
665 * low-priority reads.
667 if (pollfd
[nb_fd
].revents
== POLLIN
) {
668 DBG("kconsumerd_poll_pipe wake up");
669 tmp2
= read(ctx
->kconsumerd_poll_pipe
[0], &tmp
, 1);
671 perror("read kconsumerd poll");
676 /* Take care of high priority channels first. */
677 for (i
= 0; i
< nb_fd
; i
++) {
678 switch(pollfd
[i
].revents
) {
680 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
681 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
685 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
686 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
690 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
691 kconsumerd_del_fd(local_kconsumerd_fd
[i
]);
695 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
697 ret
= ctx
->on_buffer_ready(local_kconsumerd_fd
[i
]);
698 /* it's ok to have an unavailable sub-buffer */
706 /* If every buffer FD has hung up, we end the read loop here */
707 if (nb_fd
> 0 && num_hup
== nb_fd
) {
708 DBG("every buffer FD has hung up\n");
709 if (kconsumerd_quit
== 1) {
715 /* Take care of low priority channels. */
716 if (high_prio
== 0) {
717 for (i
= 0; i
< nb_fd
; i
++) {
718 if (pollfd
[i
].revents
== POLLIN
) {
719 DBG("Normal read on fd %d", pollfd
[i
].fd
);
720 ret
= ctx
->on_buffer_ready(local_kconsumerd_fd
[i
]);
721 /* it's ok to have an unavailable subbuffer */
730 DBG("polling thread exiting");
731 if (pollfd
!= NULL
) {
735 if (local_kconsumerd_fd
!= NULL
) {
736 free(local_kconsumerd_fd
);
737 local_kconsumerd_fd
= NULL
;
745 * initialise the necessary environnement :
746 * - create a new context
747 * - create the poll_pipe
748 * - create the should_quit pipe (for signal handler)
749 * - create the thread pipe (for splice)
750 * Takes a function pointer as argument, this function is called when data is
751 * available on a buffer. This function is responsible to do the
752 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
753 * buffer configuration and then kernctl_put_next_subbuf at the end.
754 * Returns a pointer to the new context or NULL on error.
756 struct kconsumerd_local_data
*kconsumerd_create(
757 int (*buffer_ready
)(struct kconsumerd_fd
*kconsumerd_fd
))
760 struct kconsumerd_local_data
*ctx
;
762 ctx
= malloc(sizeof(struct kconsumerd_local_data
));
764 perror("allocating context");
768 ctx
->on_buffer_ready
= buffer_ready
;
770 ret
= pipe(ctx
->kconsumerd_poll_pipe
);
772 perror("Error creating poll pipe");
777 ret
= pipe(ctx
->kconsumerd_should_quit
);
779 perror("Error creating recv pipe");
784 ret
= pipe(ctx
->kconsumerd_thread_pipe
);
786 perror("Error creating thread pipe");
798 * Close all fds associated with the instance and free the context
800 void kconsumerd_destroy(struct kconsumerd_local_data
*ctx
)
802 close(ctx
->kconsumerd_error_socket
);
803 close(ctx
->kconsumerd_thread_pipe
[0]);
804 close(ctx
->kconsumerd_thread_pipe
[1]);
805 close(ctx
->kconsumerd_poll_pipe
[0]);
806 close(ctx
->kconsumerd_poll_pipe
[1]);
807 close(ctx
->kconsumerd_should_quit
[0]);
808 close(ctx
->kconsumerd_should_quit
[1]);
809 unlink(ctx
->kconsumerd_command_sock_path
);
815 * kconsumerd_thread_receive_fds
817 * This thread listens on the consumerd socket and
818 * receives the file descriptors from ltt-sessiond
820 void *kconsumerd_thread_receive_fds(void *data
)
822 int sock
, client_socket
, ret
;
823 struct lttcomm_kconsumerd_header tmp
;
825 * structure to poll for incoming data on communication socket
826 * avoids making blocking sockets
828 struct pollfd kconsumerd_sockpoll
[2];
829 struct kconsumerd_local_data
*ctx
= data
;
832 DBG("Creating command socket %s", ctx
->kconsumerd_command_sock_path
);
833 unlink(ctx
->kconsumerd_command_sock_path
);
834 client_socket
= lttcomm_create_unix_sock(ctx
->kconsumerd_command_sock_path
);
835 if (client_socket
< 0) {
836 ERR("Cannot create command socket");
840 ret
= lttcomm_listen_unix_sock(client_socket
);
845 DBG("Sending ready command to ltt-sessiond");
846 ret
= kconsumerd_send_error(ctx
, KCONSUMERD_COMMAND_SOCK_READY
);
848 ERR("Error sending ready command to ltt-sessiond");
852 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
854 perror("fcntl O_NONBLOCK");
858 /* prepare the FDs to poll : to client socket and the should_quit pipe */
859 kconsumerd_sockpoll
[0].fd
= ctx
->kconsumerd_should_quit
[0];
860 kconsumerd_sockpoll
[0].events
= POLLIN
| POLLPRI
;
861 kconsumerd_sockpoll
[1].fd
= client_socket
;
862 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
864 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
867 DBG("Connection on client_socket");
869 /* Blocking call, waiting for transmission */
870 sock
= lttcomm_accept_unix_sock(client_socket
);
875 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
877 perror("fcntl O_NONBLOCK");
881 /* update the polling structure to poll on the established socket */
882 kconsumerd_sockpoll
[1].fd
= sock
;
883 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
886 if (kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
889 DBG("Incoming fds on sock");
891 /* We first get the number of fd we are about to receive */
892 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
893 sizeof(struct lttcomm_kconsumerd_header
));
895 ERR("Communication interrupted on command socket");
898 if (tmp
.cmd_type
== STOP
) {
899 DBG("Received STOP command");
902 if (kconsumerd_quit
) {
903 DBG("kconsumerd_thread_receive_fds received quit from signal");
907 /* we received a command to add or update fds */
908 ret
= kconsumerd_consumerd_recv_fd(ctx
, sock
, kconsumerd_sockpoll
,
909 tmp
.payload_size
, tmp
.cmd_type
);
911 ERR("Receiving the FD, exiting");
914 DBG("received fds on sock");
918 DBG("kconsumerd_thread_receive_fds exiting");
921 * when all fds have hung up, the polling thread
927 * 2s of grace period, if no polling events occur during
928 * this period, the polling thread will exit even if there
929 * are still open FDs (should not happen, but safety mechanism).
931 kconsumerd_poll_timeout
= KCONSUMERD_POLL_GRACE_PERIOD
;
933 /* wake up the polling thread */
934 ret
= write(ctx
->kconsumerd_poll_pipe
[1], "4", 1);
936 perror("poll pipe write");
944 * Close all the tracefiles and stream fds, should be called when all
945 * instances are destroyed.
947 void kconsumerd_cleanup(void)
949 struct kconsumerd_fd
*iter
, *tmp
;
952 * close all outfd. Called when there are no more threads
953 * running (after joining on the threads), no need to protect
954 * list iteration with mutex.
956 cds_list_for_each_entry_safe(iter
, tmp
, &kconsumerd_data
.fd_list
.head
, list
) {
957 kconsumerd_del_fd(iter
);
962 * kconsumerd_should_exit
964 * Called from signal handler.
966 void kconsumerd_should_exit(struct kconsumerd_local_data
*ctx
)
970 ret
= write(ctx
->kconsumerd_should_quit
[1], "4", 1);
972 perror("write kconsumerd quit");
977 * kconsumerd_send_error
979 * send return code to ltt-sessiond
981 int kconsumerd_send_error(struct kconsumerd_local_data
*ctx
, enum lttcomm_return_code cmd
)
983 if (ctx
->kconsumerd_error_socket
> 0) {
984 return lttcomm_send_unix_sock(ctx
->kconsumerd_error_socket
, &cmd
,
985 sizeof(enum lttcomm_sessiond_command
));