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 <lttng/lttng-kconsumerd.h>
34 #include "kernelctl.h"
36 #include "lttng-sessiond-comm.h"
38 static struct lttng_kconsumerd_global_data
{
40 * kconsumerd_data.lock protects kconsumerd_data.fd_list,
41 * kconsumerd_data.fds_count, and kconsumerd_data.need_update. It ensures
42 * the count matches the number of items in the fd_list. It ensures the
43 * list updates *always* trigger an fd_array update (therefore need to make
44 * list update vs kconsumerd_data.need_update flag update atomic, and also
45 * flag read, fd array and flag clear atomic).
49 * Number of element for the list below. Protected by kconsumerd_data.lock.
51 unsigned int fds_count
;
53 * List of FDs. Protected by kconsumerd_data.lock.
55 struct lttng_kconsumerd_fd_list fd_list
;
57 * Flag specifying if the local array of FDs needs update in the poll
58 * function. Protected by kconsumerd_data.lock.
60 unsigned int need_update
;
62 .fd_list
.head
= CDS_LIST_HEAD_INIT(kconsumerd_data
.fd_list
.head
),
67 /* timeout parameter, to control the polling thread grace period. */
68 static int kconsumerd_poll_timeout
= -1;
71 * Flag to inform the polling thread to quit when all fd hung up. Updated by
72 * the kconsumerd_thread_receive_fds when it notices that all fds has hung up.
73 * Also updated by the signal handler (kconsumerd_should_exit()). Read by the
76 static volatile int kconsumerd_quit
= 0;
79 * Find a session fd in the global list. The kconsumerd_data.lock must be
80 * locked during this call.
82 * Return 1 if found else 0.
84 static int kconsumerd_find_session_fd(int fd
)
86 struct lttng_kconsumerd_fd
*iter
;
88 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
89 if (iter
->sessiond_fd
== fd
) {
90 DBG("Duplicate session fd %d", fd
);
99 * Remove a fd from the global list protected by a mutex.
101 static void kconsumerd_del_fd(struct lttng_kconsumerd_fd
*lcf
)
104 pthread_mutex_lock(&kconsumerd_data
.lock
);
105 cds_list_del(&lcf
->list
);
106 if (kconsumerd_data
.fds_count
> 0) {
107 kconsumerd_data
.fds_count
--;
109 if (lcf
->mmap_base
!= NULL
) {
110 ret
= munmap(lcf
->mmap_base
, lcf
->mmap_len
);
115 if (lcf
->out_fd
!= 0) {
118 close(lcf
->consumerd_fd
);
123 kconsumerd_data
.need_update
= 1;
124 pthread_mutex_unlock(&kconsumerd_data
.lock
);
128 * Add a fd to the global list protected by a mutex.
130 static int kconsumerd_add_fd(struct lttcomm_kconsumerd_msg
*buf
,
133 struct lttng_kconsumerd_fd
*tmp_fd
;
136 pthread_mutex_lock(&kconsumerd_data
.lock
);
137 /* Check if already exist */
138 ret
= kconsumerd_find_session_fd(buf
->fd
);
143 tmp_fd
= malloc(sizeof(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';
156 /* Opening the tracefile in write mode */
157 if (tmp_fd
->path_name
!= NULL
) {
158 ret
= open(tmp_fd
->path_name
,
159 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
161 ERR("Opening %s", tmp_fd
->path_name
);
165 tmp_fd
->out_fd
= ret
;
166 DBG("Adding %s (%d, %d, %d)", tmp_fd
->path_name
,
167 tmp_fd
->sessiond_fd
, tmp_fd
->consumerd_fd
, tmp_fd
->out_fd
);
170 if (tmp_fd
->output
== LTTNG_EVENT_MMAP
) {
171 /* get the len of the mmap region */
172 ret
= kernctl_get_mmap_len(tmp_fd
->consumerd_fd
, &tmp_fd
->mmap_len
);
175 perror("kernctl_get_mmap_len");
179 tmp_fd
->mmap_base
= mmap(NULL
, tmp_fd
->mmap_len
,
180 PROT_READ
, MAP_PRIVATE
, tmp_fd
->consumerd_fd
, 0);
181 if (tmp_fd
->mmap_base
== MAP_FAILED
) {
182 perror("Error mmaping");
188 cds_list_add(&tmp_fd
->list
, &kconsumerd_data
.fd_list
.head
);
189 kconsumerd_data
.fds_count
++;
190 kconsumerd_data
.need_update
= 1;
192 pthread_mutex_unlock(&kconsumerd_data
.lock
);
197 * Update a fd according to what we just received.
199 static void kconsumerd_change_fd_state(int sessiond_fd
,
200 enum lttng_kconsumerd_fd_state state
)
202 struct lttng_kconsumerd_fd
*iter
;
204 pthread_mutex_lock(&kconsumerd_data
.lock
);
205 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
206 if (iter
->sessiond_fd
== sessiond_fd
) {
211 kconsumerd_data
.need_update
= 1;
212 pthread_mutex_unlock(&kconsumerd_data
.lock
);
216 * Allocate the pollfd structure and the local view of the out fds to avoid
217 * doing a lookup in the linked list and concurrency issues when writing is
218 * needed. Called with kconsumerd_data.lock held.
220 * Returns the number of fds in the structures.
222 static int kconsumerd_update_poll_array(
223 struct lttng_kconsumerd_local_data
*ctx
, struct pollfd
**pollfd
,
224 struct lttng_kconsumerd_fd
**local_kconsumerd_fd
)
226 struct lttng_kconsumerd_fd
*iter
;
229 DBG("Updating poll fd array");
230 cds_list_for_each_entry(iter
, &kconsumerd_data
.fd_list
.head
, list
) {
231 if (iter
->state
== ACTIVE_FD
) {
232 DBG("Active FD %d", iter
->consumerd_fd
);
233 (*pollfd
)[i
].fd
= iter
->consumerd_fd
;
234 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
235 local_kconsumerd_fd
[i
] = iter
;
241 * Insert the kconsumerd_poll_pipe at the end of the array and don't
242 * increment i so nb_fd is the number of real FD.
244 (*pollfd
)[i
].fd
= ctx
->kconsumerd_poll_pipe
[0];
245 (*pollfd
)[i
].events
= POLLIN
;
250 * Receives an array of file descriptors and the associated structures
251 * describing each fd (path name).
253 * Returns the size of received data
255 static int kconsumerd_consumerd_recv_fd(
256 struct lttng_kconsumerd_local_data
*ctx
, int sfd
,
257 struct pollfd
*kconsumerd_sockpoll
, int size
,
258 enum lttng_kconsumerd_command cmd_type
)
261 int ret
= 0, i
, tmp2
;
262 struct cmsghdr
*cmsg
;
264 char recv_fd
[CMSG_SPACE(sizeof(int))];
265 struct lttcomm_kconsumerd_msg lkm
;
267 /* the number of fds we are about to receive */
268 nb_fd
= size
/ sizeof(struct lttcomm_kconsumerd_msg
);
271 * nb_fd is the number of fds we receive. One fd per recvmsg.
273 for (i
= 0; i
< nb_fd
; i
++) {
274 struct msghdr msg
= { 0 };
276 /* Prepare to receive the structures */
277 iov
[0].iov_base
= &lkm
;
278 iov
[0].iov_len
= sizeof(lkm
);
282 msg
.msg_control
= recv_fd
;
283 msg
.msg_controllen
= sizeof(recv_fd
);
285 DBG("Waiting to receive fd");
286 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
290 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
295 if (ret
!= (size
/ nb_fd
)) {
296 ERR("Received only %d, expected %d", ret
, size
);
297 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
301 cmsg
= CMSG_FIRSTHDR(&msg
);
303 ERR("Invalid control message header");
305 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
309 /* if we received fds */
310 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
313 DBG("kconsumerd_add_fd %s (%d)", lkm
.path_name
,
314 ((int *) CMSG_DATA(cmsg
))[0]);
316 ret
= kconsumerd_add_fd(&lkm
, ((int *) CMSG_DATA(cmsg
))[0]);
318 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_OUTFD_ERROR
);
323 kconsumerd_change_fd_state(lkm
.fd
, lkm
.state
);
328 /* signal the poll thread */
329 tmp2
= write(ctx
->kconsumerd_poll_pipe
[1], "4", 1);
331 perror("write kconsumerd poll");
334 ERR("Didn't received any fd");
335 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_ERROR_RECV_FD
);
346 * Set the error socket.
348 void lttng_kconsumerd_set_error_sock(
349 struct lttng_kconsumerd_local_data
*ctx
, int sock
)
351 ctx
->kconsumerd_error_socket
= sock
;
355 * Set the command socket path.
358 void lttng_kconsumerd_set_command_sock_path(
359 struct lttng_kconsumerd_local_data
*ctx
, char *sock
)
361 ctx
->kconsumerd_command_sock_path
= sock
;
364 static void lttng_kconsumerd_sync_trace_file(
365 struct lttng_kconsumerd_fd
*kconsumerd_fd
, off_t orig_offset
)
367 int outfd
= kconsumerd_fd
->out_fd
;
369 * This does a blocking write-and-wait on any page that belongs to the
370 * subbuffer prior to the one we just wrote.
371 * Don't care about error values, as these are just hints and ways to
372 * limit the amount of page cache used.
374 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
375 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
376 kconsumerd_fd
->max_sb_size
,
377 SYNC_FILE_RANGE_WAIT_BEFORE
378 | SYNC_FILE_RANGE_WRITE
379 | SYNC_FILE_RANGE_WAIT_AFTER
);
381 * Give hints to the kernel about how we access the file:
382 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
385 * We need to call fadvise again after the file grows because the
386 * kernel does not seem to apply fadvise to non-existing parts of the
389 * Call fadvise _after_ having waited for the page writeback to
390 * complete because the dirty page writeback semantic is not well
391 * defined. So it can be expected to lead to lower throughput in
394 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
395 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
401 * Mmap the ring buffer, read it and write the data to the tracefile.
403 * Returns the number of bytes written
405 int lttng_kconsumerd_on_read_subbuffer_mmap(
406 struct lttng_kconsumerd_local_data
*ctx
,
407 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
409 unsigned long mmap_offset
;
411 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
412 int fd
= kconsumerd_fd
->consumerd_fd
;
413 int outfd
= kconsumerd_fd
->out_fd
;
415 /* get the offset inside the fd to mmap */
416 ret
= kernctl_get_mmap_read_offset(fd
, &mmap_offset
);
419 perror("kernctl_get_mmap_read_offset");
424 ret
= write(outfd
, kconsumerd_fd
->mmap_base
+ mmap_offset
, len
);
427 } else if (ret
< 0) {
429 perror("Error in file write");
432 /* This won't block, but will start writeout asynchronously */
433 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
434 SYNC_FILE_RANGE_WRITE
);
435 kconsumerd_fd
->out_fd_offset
+= ret
;
438 lttng_kconsumerd_sync_trace_file(kconsumerd_fd
, orig_offset
);
447 * Splice the data from the ring buffer to the tracefile.
449 * Returns the number of bytes spliced.
451 int lttng_kconsumerd_on_read_subbuffer_splice(
452 struct lttng_kconsumerd_local_data
*ctx
,
453 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
)
457 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
458 int fd
= kconsumerd_fd
->consumerd_fd
;
459 int outfd
= kconsumerd_fd
->out_fd
;
462 DBG("splice chan to pipe offset %lu (fd : %d)",
463 (unsigned long)offset
, fd
);
464 ret
= splice(fd
, &offset
, ctx
->kconsumerd_thread_pipe
[1], NULL
, len
,
465 SPLICE_F_MOVE
| SPLICE_F_MORE
);
466 DBG("splice chan to pipe ret %ld", ret
);
469 perror("Error in relay splice");
473 ret
= splice(ctx
->kconsumerd_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
474 SPLICE_F_MOVE
| SPLICE_F_MORE
);
475 DBG("splice pipe to file %ld", ret
);
478 perror("Error in file splice");
482 /* This won't block, but will start writeout asynchronously */
483 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
484 SYNC_FILE_RANGE_WRITE
);
485 kconsumerd_fd
->out_fd_offset
+= ret
;
487 lttng_kconsumerd_sync_trace_file(kconsumerd_fd
, orig_offset
);
492 /* send the appropriate error description to sessiond */
495 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_EBADF
);
498 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_EINVAL
);
501 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_ENOMEM
);
504 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_SPLICE_ESPIPE
);
513 * Take a snapshot for a specific fd
515 * Returns 0 on success, < 0 on error
517 int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data
*ctx
,
518 struct lttng_kconsumerd_fd
*kconsumerd_fd
)
521 int infd
= kconsumerd_fd
->consumerd_fd
;
523 ret
= kernctl_snapshot(infd
);
526 perror("Getting sub-buffer snapshot.");
533 * Get the produced position
535 * Returns 0 on success, < 0 on error
537 int lttng_kconsumerd_get_produced_snapshot(
538 struct lttng_kconsumerd_local_data
*ctx
,
539 struct lttng_kconsumerd_fd
*kconsumerd_fd
,
543 int infd
= kconsumerd_fd
->consumerd_fd
;
545 ret
= kernctl_snapshot_get_produced(infd
, pos
);
548 perror("kernctl_snapshot_get_produced");
555 * Poll on the should_quit pipe and the command socket return -1 on error and
556 * should exit, 0 if data is available on the command socket
558 int lttng_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 * This thread polls the fds in the ltt_fd_list to consume the data and write
579 * it to tracefile if necessary.
581 void *lttng_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 lttng_kconsumerd_fd
**local_kconsumerd_fd
= NULL
;
587 /* local view of kconsumerd_data.fds_count */
591 struct lttng_kconsumerd_local_data
*ctx
= data
;
594 local_kconsumerd_fd
= malloc(sizeof(struct lttng_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 lttng_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 lttng_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 lttng_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
;
743 * Initialise the necessary environnement :
744 * - create a new context
745 * - create the poll_pipe
746 * - create the should_quit pipe (for signal handler)
747 * - create the thread pipe (for splice)
749 * Takes a function pointer as argument, this function is called when data is
750 * available on a buffer. This function is responsible to do the
751 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
752 * 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 lttng_kconsumerd_local_data
*lttng_kconsumerd_create(
757 int (*buffer_ready
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
))
760 struct lttng_kconsumerd_local_data
*ctx
;
762 ctx
= malloc(sizeof(struct lttng_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");
796 * Close all fds associated with the instance and free the context.
798 void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data
*ctx
)
800 close(ctx
->kconsumerd_error_socket
);
801 close(ctx
->kconsumerd_thread_pipe
[0]);
802 close(ctx
->kconsumerd_thread_pipe
[1]);
803 close(ctx
->kconsumerd_poll_pipe
[0]);
804 close(ctx
->kconsumerd_poll_pipe
[1]);
805 close(ctx
->kconsumerd_should_quit
[0]);
806 close(ctx
->kconsumerd_should_quit
[1]);
807 unlink(ctx
->kconsumerd_command_sock_path
);
813 * This thread listens on the consumerd socket and receives the file
814 * descriptors from the session daemon.
816 void *lttng_kconsumerd_thread_receive_fds(void *data
)
818 int sock
, client_socket
, ret
;
819 struct lttcomm_kconsumerd_header tmp
;
821 * structure to poll for incoming data on communication socket avoids
822 * making blocking sockets.
824 struct pollfd kconsumerd_sockpoll
[2];
825 struct lttng_kconsumerd_local_data
*ctx
= data
;
828 DBG("Creating command socket %s", ctx
->kconsumerd_command_sock_path
);
829 unlink(ctx
->kconsumerd_command_sock_path
);
830 client_socket
= lttcomm_create_unix_sock(ctx
->kconsumerd_command_sock_path
);
831 if (client_socket
< 0) {
832 ERR("Cannot create command socket");
836 ret
= lttcomm_listen_unix_sock(client_socket
);
841 DBG("Sending ready command to ltt-sessiond");
842 ret
= lttng_kconsumerd_send_error(ctx
, KCONSUMERD_COMMAND_SOCK_READY
);
844 ERR("Error sending ready command to ltt-sessiond");
848 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
850 perror("fcntl O_NONBLOCK");
854 /* prepare the FDs to poll : to client socket and the should_quit pipe */
855 kconsumerd_sockpoll
[0].fd
= ctx
->kconsumerd_should_quit
[0];
856 kconsumerd_sockpoll
[0].events
= POLLIN
| POLLPRI
;
857 kconsumerd_sockpoll
[1].fd
= client_socket
;
858 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
860 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
863 DBG("Connection on client_socket");
865 /* Blocking call, waiting for transmission */
866 sock
= lttcomm_accept_unix_sock(client_socket
);
871 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
873 perror("fcntl O_NONBLOCK");
877 /* update the polling structure to poll on the established socket */
878 kconsumerd_sockpoll
[1].fd
= sock
;
879 kconsumerd_sockpoll
[1].events
= POLLIN
| POLLPRI
;
882 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll
) < 0) {
885 DBG("Incoming fds on sock");
887 /* We first get the number of fd we are about to receive */
888 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
889 sizeof(struct lttcomm_kconsumerd_header
));
891 ERR("Communication interrupted on command socket");
894 if (tmp
.cmd_type
== STOP
) {
895 DBG("Received STOP command");
898 if (kconsumerd_quit
) {
899 DBG("kconsumerd_thread_receive_fds received quit from signal");
903 /* we received a command to add or update fds */
904 ret
= kconsumerd_consumerd_recv_fd(ctx
, sock
, kconsumerd_sockpoll
,
905 tmp
.payload_size
, tmp
.cmd_type
);
907 ERR("Receiving the FD, exiting");
910 DBG("received fds on sock");
914 DBG("kconsumerd_thread_receive_fds exiting");
917 * when all fds have hung up, the polling thread
923 * 2s of grace period, if no polling events occur during
924 * this period, the polling thread will exit even if there
925 * are still open FDs (should not happen, but safety mechanism).
927 kconsumerd_poll_timeout
= LTTNG_KCONSUMERD_POLL_GRACE_PERIOD
;
929 /* wake up the polling thread */
930 ret
= write(ctx
->kconsumerd_poll_pipe
[1], "4", 1);
932 perror("poll pipe write");
938 * Close all the tracefiles and stream fds, should be called when all instances
941 void lttng_kconsumerd_cleanup(void)
943 struct lttng_kconsumerd_fd
*iter
, *tmp
;
946 * close all outfd. Called when there are no more threads
947 * running (after joining on the threads), no need to protect
948 * list iteration with mutex.
950 cds_list_for_each_entry_safe(iter
, tmp
,
951 &kconsumerd_data
.fd_list
.head
, list
) {
952 kconsumerd_del_fd(iter
);
957 * Called from signal handler.
959 void lttng_kconsumerd_should_exit(struct lttng_kconsumerd_local_data
*ctx
)
963 ret
= write(ctx
->kconsumerd_should_quit
[1], "4", 1);
965 perror("write kconsumerd quit");
970 * Send return code to the session daemon.
972 int lttng_kconsumerd_send_error(
973 struct lttng_kconsumerd_local_data
*ctx
, int cmd
)
975 if (ctx
->kconsumerd_error_socket
> 0) {
976 return lttcomm_send_unix_sock(ctx
->kconsumerd_error_socket
, &cmd
,
977 sizeof(enum lttcomm_sessiond_command
));