Parametrize enable-channel help
[lttng-tools.git] / liblttngkconsumerd / lttngkconsumerd.c
CommitLineData
1ce86c9a
JD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
82a3637f
DG
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
1ce86c9a
JD
9 *
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.
14 *
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.
18 */
19
20#define _GNU_SOURCE
21#include <fcntl.h>
22#include <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <unistd.h>
30#include <urcu/list.h>
6533b585
DG
31
32#include <lttng/lttng-kconsumerd.h>
1ce86c9a 33
50ecdf72 34#include "kernelctl.h"
1ce86c9a 35#include "lttngerr.h"
6533b585 36#include "lttng-sessiond-comm.h"
1ce86c9a 37
6533b585 38static struct lttng_kconsumerd_global_data {
242cd187
MD
39 /*
40 * kconsumerd_data.lock protects kconsumerd_data.fd_list,
6533b585
DG
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).
242cd187
MD
46 */
47 pthread_mutex_t lock;
48 /*
6533b585 49 * Number of element for the list below. Protected by kconsumerd_data.lock.
242cd187
MD
50 */
51 unsigned int fds_count;
52 /*
6533b585 53 * List of FDs. Protected by kconsumerd_data.lock.
242cd187 54 */
6533b585 55 struct lttng_kconsumerd_fd_list fd_list;
242cd187 56 /*
6533b585
DG
57 * Flag specifying if the local array of FDs needs update in the poll
58 * function. Protected by kconsumerd_data.lock.
242cd187
MD
59 */
60 unsigned int need_update;
61} kconsumerd_data = {
62 .fd_list.head = CDS_LIST_HEAD_INIT(kconsumerd_data.fd_list.head),
cb040cc1
JD
63 .fds_count = 0,
64 .need_update = 1,
1ce86c9a
JD
65};
66
6533b585 67/* timeout parameter, to control the polling thread grace period. */
1ce86c9a
JD
68static int kconsumerd_poll_timeout = -1;
69
3dcd2721 70/*
6533b585
DG
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
74 * polling threads.
3dcd2721
MD
75 */
76static volatile int kconsumerd_quit = 0;
1ce86c9a
JD
77
78/*
6533b585
DG
79 * Find a session fd in the global list. The kconsumerd_data.lock must be
80 * locked during this call.
38079a1b 81 *
6533b585 82 * Return 1 if found else 0.
38079a1b
DG
83 */
84static int kconsumerd_find_session_fd(int fd)
85{
6533b585 86 struct lttng_kconsumerd_fd *iter;
38079a1b 87
242cd187 88 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
38079a1b
DG
89 if (iter->sessiond_fd == fd) {
90 DBG("Duplicate session fd %d", fd);
38079a1b
DG
91 return 1;
92 }
93 }
38079a1b
DG
94
95 return 0;
96}
97
1ce86c9a 98/*
6533b585 99 * Remove a fd from the global list protected by a mutex.
1ce86c9a 100 */
6533b585 101static void kconsumerd_del_fd(struct lttng_kconsumerd_fd *lcf)
1ce86c9a 102{
8b270bdb 103 int ret;
242cd187 104 pthread_mutex_lock(&kconsumerd_data.lock);
1ce86c9a 105 cds_list_del(&lcf->list);
242cd187
MD
106 if (kconsumerd_data.fds_count > 0) {
107 kconsumerd_data.fds_count--;
1ce86c9a 108 if (lcf != NULL) {
8b270bdb
JD
109 if (lcf->mmap_base != NULL) {
110 ret = munmap(lcf->mmap_base, lcf->mmap_len);
111 if (ret != 0) {
112 perror("munmap");
113 }
114 }
cb040cc1
JD
115 if (lcf->out_fd != 0) {
116 close(lcf->out_fd);
117 }
1ce86c9a
JD
118 close(lcf->consumerd_fd);
119 free(lcf);
120 lcf = NULL;
121 }
122 }
242cd187
MD
123 kconsumerd_data.need_update = 1;
124 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
125}
126
127/*
6533b585 128 * Add a fd to the global list protected by a mutex.
1ce86c9a 129 */
6533b585
DG
130static int kconsumerd_add_fd(struct lttcomm_kconsumerd_msg *buf,
131 int consumerd_fd)
1ce86c9a 132{
6533b585 133 struct lttng_kconsumerd_fd *tmp_fd;
cb040cc1 134 int ret = 0;
38079a1b 135
242cd187 136 pthread_mutex_lock(&kconsumerd_data.lock);
38079a1b
DG
137 /* Check if already exist */
138 ret = kconsumerd_find_session_fd(buf->fd);
139 if (ret == 1) {
140 goto end;
141 }
1ce86c9a 142
6533b585 143 tmp_fd = malloc(sizeof(struct lttng_kconsumerd_fd));
1ce86c9a
JD
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;
cb040cc1
JD
148 tmp_fd->out_fd = 0;
149 tmp_fd->out_fd_offset = 0;
8b270bdb
JD
150 tmp_fd->mmap_len = 0;
151 tmp_fd->mmap_base = NULL;
152 tmp_fd->output = buf->output;
1ce86c9a 153 strncpy(tmp_fd->path_name, buf->path_name, PATH_MAX);
99497cd0 154 tmp_fd->path_name[PATH_MAX - 1] = '\0';
1ce86c9a
JD
155
156 /* Opening the tracefile in write mode */
cb040cc1
JD
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);
160 if (ret < 0) {
161 ERR("Opening %s", tmp_fd->path_name);
162 perror("open");
163 goto end;
164 }
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);
1ce86c9a 168 }
1ce86c9a 169
8b270bdb
JD
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);
173 if (ret != 0) {
174 ret = errno;
175 perror("kernctl_get_mmap_len");
176 goto end;
177 }
178
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");
183 ret = -1;
184 goto end;
185 }
186 }
187
242cd187
MD
188 cds_list_add(&tmp_fd->list, &kconsumerd_data.fd_list.head);
189 kconsumerd_data.fds_count++;
190 kconsumerd_data.need_update = 1;
1ce86c9a 191end:
242cd187 192 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
193 return ret;
194}
195
196/*
6533b585 197 * Update a fd according to what we just received.
1ce86c9a
JD
198 */
199static void kconsumerd_change_fd_state(int sessiond_fd,
6533b585 200 enum lttng_kconsumerd_fd_state state)
1ce86c9a 201{
6533b585 202 struct lttng_kconsumerd_fd *iter;
0237248c 203
242cd187
MD
204 pthread_mutex_lock(&kconsumerd_data.lock);
205 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
206 if (iter->sessiond_fd == sessiond_fd) {
207 iter->state = state;
208 break;
209 }
210 }
242cd187
MD
211 kconsumerd_data.need_update = 1;
212 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
213}
214
215/*
6533b585
DG
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.
1ce86c9a 219 *
6533b585 220 * Returns the number of fds in the structures.
1ce86c9a 221 */
6533b585
DG
222static int kconsumerd_update_poll_array(
223 struct lttng_kconsumerd_local_data *ctx, struct pollfd **pollfd,
224 struct lttng_kconsumerd_fd **local_kconsumerd_fd)
1ce86c9a 225{
6533b585 226 struct lttng_kconsumerd_fd *iter;
1ce86c9a
JD
227 int i = 0;
228
229 DBG("Updating poll fd array");
242cd187 230 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
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;
236 i++;
237 }
238 }
239
240 /*
6533b585
DG
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.
1ce86c9a 243 */
cb040cc1 244 (*pollfd)[i].fd = ctx->kconsumerd_poll_pipe[0];
1ce86c9a 245 (*pollfd)[i].events = POLLIN;
1ce86c9a
JD
246 return i;
247}
248
6533b585
DG
249/*
250 * Receives an array of file descriptors and the associated structures
251 * describing each fd (path name).
252 *
253 * Returns the size of received data
254 */
255static 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)
259{
260 struct iovec iov[1];
261 int ret = 0, i, tmp2;
262 struct cmsghdr *cmsg;
263 int nb_fd;
264 char recv_fd[CMSG_SPACE(sizeof(int))];
265 struct lttcomm_kconsumerd_msg lkm;
266
267 /* the number of fds we are about to receive */
268 nb_fd = size / sizeof(struct lttcomm_kconsumerd_msg);
269
270 /*
271 * nb_fd is the number of fds we receive. One fd per recvmsg.
272 */
273 for (i = 0; i < nb_fd; i++) {
274 struct msghdr msg = { 0 };
275
276 /* Prepare to receive the structures */
277 iov[0].iov_base = &lkm;
278 iov[0].iov_len = sizeof(lkm);
279 msg.msg_iov = iov;
280 msg.msg_iovlen = 1;
281
282 msg.msg_control = recv_fd;
283 msg.msg_controllen = sizeof(recv_fd);
284
285 DBG("Waiting to receive fd");
286 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
287 goto end;
288 }
289
290 if ((ret = recvmsg(sfd, &msg, 0)) < 0) {
291 perror("recvmsg");
292 continue;
293 }
294
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);
298 goto end;
299 }
300
301 cmsg = CMSG_FIRSTHDR(&msg);
302 if (!cmsg) {
303 ERR("Invalid control message header");
304 ret = -1;
305 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
306 goto end;
307 }
308
309 /* if we received fds */
310 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
311 switch (cmd_type) {
312 case ADD_STREAM:
313 DBG("kconsumerd_add_fd %s (%d)", lkm.path_name,
314 ((int *) CMSG_DATA(cmsg))[0]);
315
316 ret = kconsumerd_add_fd(&lkm, ((int *) CMSG_DATA(cmsg))[0]);
317 if (ret < 0) {
318 lttng_kconsumerd_send_error(ctx, KCONSUMERD_OUTFD_ERROR);
319 goto end;
320 }
321 break;
322 case UPDATE_STREAM:
323 kconsumerd_change_fd_state(lkm.fd, lkm.state);
324 break;
325 default:
326 break;
327 }
328 /* signal the poll thread */
329 tmp2 = write(ctx->kconsumerd_poll_pipe[1], "4", 1);
330 if (tmp2 < 0) {
331 perror("write kconsumerd poll");
332 }
333 } else {
334 ERR("Didn't received any fd");
335 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
336 ret = -1;
337 goto end;
338 }
339 }
340
341end:
342 return ret;
343}
344
345/*
346 * Set the error socket.
347 */
348void lttng_kconsumerd_set_error_sock(
349 struct lttng_kconsumerd_local_data *ctx, int sock)
350{
351 ctx->kconsumerd_error_socket = sock;
352}
353
354/*
355 * Set the command socket path.
356 */
357
358void lttng_kconsumerd_set_command_sock_path(
359 struct lttng_kconsumerd_local_data *ctx, char *sock)
360{
361 ctx->kconsumerd_command_sock_path = sock;
362}
1ce86c9a 363
92ab9ab6
JD
364static void lttng_kconsumerd_sync_trace_file(
365 struct lttng_kconsumerd_fd *kconsumerd_fd, off_t orig_offset)
366{
367 int outfd = kconsumerd_fd->out_fd;
368 /*
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.
373 */
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);
380 /*
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
383 * we write it.
384 *
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
387 * file.
388 *
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
392 * streaming.
393 */
394 posix_fadvise(outfd, orig_offset - kconsumerd_fd->max_sb_size,
395 kconsumerd_fd->max_sb_size, POSIX_FADV_DONTNEED);
396 }
397}
398
399
1ce86c9a 400/*
6533b585 401 * Mmap the ring buffer, read it and write the data to the tracefile.
1ce86c9a 402 *
1ce86c9a
JD
403 * Returns the number of bytes written
404 */
6533b585
DG
405int lttng_kconsumerd_on_read_subbuffer_mmap(
406 struct lttng_kconsumerd_local_data *ctx,
407 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len)
1ce86c9a 408{
8b270bdb 409 unsigned long mmap_offset;
1ce86c9a
JD
410 long ret = 0;
411 off_t orig_offset = kconsumerd_fd->out_fd_offset;
412 int fd = kconsumerd_fd->consumerd_fd;
413 int outfd = kconsumerd_fd->out_fd;
414
1ce86c9a
JD
415 /* get the offset inside the fd to mmap */
416 ret = kernctl_get_mmap_read_offset(fd, &mmap_offset);
417 if (ret != 0) {
418 ret = errno;
419 perror("kernctl_get_mmap_read_offset");
420 goto end;
421 }
422
1ce86c9a 423 while (len > 0) {
8b270bdb 424 ret = write(outfd, kconsumerd_fd->mmap_base + mmap_offset, len);
1ce86c9a
JD
425 if (ret >= len) {
426 len = 0;
427 } else if (ret < 0) {
428 ret = errno;
429 perror("Error in file write");
430 goto end;
431 }
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;
436 }
437
92ab9ab6 438 lttng_kconsumerd_sync_trace_file(kconsumerd_fd, orig_offset);
1ce86c9a 439
1ce86c9a
JD
440 goto end;
441
442end:
1ce86c9a
JD
443 return ret;
444}
445
446/*
1ce86c9a 447 * Splice the data from the ring buffer to the tracefile.
6533b585
DG
448 *
449 * Returns the number of bytes spliced.
1ce86c9a 450 */
6533b585
DG
451int lttng_kconsumerd_on_read_subbuffer_splice(
452 struct lttng_kconsumerd_local_data *ctx,
453 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len)
1ce86c9a
JD
454{
455 long ret = 0;
456 loff_t offset = 0;
457 off_t orig_offset = kconsumerd_fd->out_fd_offset;
458 int fd = kconsumerd_fd->consumerd_fd;
459 int outfd = kconsumerd_fd->out_fd;
460
461 while (len > 0) {
462 DBG("splice chan to pipe offset %lu (fd : %d)",
463 (unsigned long)offset, fd);
cb040cc1 464 ret = splice(fd, &offset, ctx->kconsumerd_thread_pipe[1], NULL, len,
1ce86c9a
JD
465 SPLICE_F_MOVE | SPLICE_F_MORE);
466 DBG("splice chan to pipe ret %ld", ret);
467 if (ret < 0) {
468 ret = errno;
469 perror("Error in relay splice");
470 goto splice_error;
471 }
472
cb040cc1 473 ret = splice(ctx->kconsumerd_thread_pipe[0], NULL, outfd, NULL, ret,
1ce86c9a
JD
474 SPLICE_F_MOVE | SPLICE_F_MORE);
475 DBG("splice pipe to file %ld", ret);
476 if (ret < 0) {
477 ret = errno;
478 perror("Error in file splice");
479 goto splice_error;
480 }
751667bd 481 len -= ret;
1ce86c9a
JD
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;
486 }
92ab9ab6 487 lttng_kconsumerd_sync_trace_file(kconsumerd_fd, orig_offset);
1ce86c9a 488
1ce86c9a
JD
489 goto end;
490
491splice_error:
492 /* send the appropriate error description to sessiond */
493 switch(ret) {
494 case EBADF:
6533b585 495 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_EBADF);
1ce86c9a
JD
496 break;
497 case EINVAL:
6533b585 498 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_EINVAL);
1ce86c9a
JD
499 break;
500 case ENOMEM:
6533b585 501 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_ENOMEM);
1ce86c9a
JD
502 break;
503 case ESPIPE:
6533b585 504 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_ESPIPE);
1ce86c9a 505 break;
1ce86c9a
JD
506 }
507
508end:
509 return ret;
510}
511
92ab9ab6
JD
512/*
513 * Take a snapshot for a specific fd
514 *
515 * Returns 0 on success, < 0 on error
516 */
517int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data *ctx,
518 struct lttng_kconsumerd_fd *kconsumerd_fd)
519{
520 int ret = 0;
521 int infd = kconsumerd_fd->consumerd_fd;
522
523 ret = kernctl_snapshot(infd);
524 if (ret != 0) {
525 ret = errno;
526 perror("Getting sub-buffer snapshot.");
527 }
528
529 return ret;
530}
531
532/*
533 * Get the produced position
534 *
535 * Returns 0 on success, < 0 on error
536 */
537int lttng_kconsumerd_get_produced_snapshot(
538 struct lttng_kconsumerd_local_data *ctx,
539 struct lttng_kconsumerd_fd *kconsumerd_fd,
540 unsigned long *pos)
541{
542 int ret;
543 int infd = kconsumerd_fd->consumerd_fd;
544
545 ret = kernctl_snapshot_get_produced(infd, pos);
546 if (ret != 0) {
547 ret = errno;
548 perror("kernctl_snapshot_get_produced");
549 }
550
551 return ret;
552}
553
4de84ad9 554/*
6533b585
DG
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
4de84ad9 557 */
6533b585 558int lttng_kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll)
4de84ad9
JD
559{
560 int num_rdy;
561
562 num_rdy = poll(kconsumerd_sockpoll, 2, -1);
563 if (num_rdy == -1) {
564 perror("Poll error");
565 goto exit;
566 }
567 if (kconsumerd_sockpoll[0].revents == POLLIN) {
568 DBG("kconsumerd_should_quit wake up");
569 goto exit;
570 }
571 return 0;
572
573exit:
574 return -1;
575}
576
1ce86c9a 577/*
6533b585
DG
578 * This thread polls the fds in the ltt_fd_list to consume the data and write
579 * it to tracefile if necessary.
1ce86c9a 580 */
6533b585 581void *lttng_kconsumerd_thread_poll_fds(void *data)
1ce86c9a
JD
582{
583 int num_rdy, num_hup, high_prio, ret, i;
584 struct pollfd *pollfd = NULL;
585 /* local view of the fds */
6533b585 586 struct lttng_kconsumerd_fd **local_kconsumerd_fd = NULL;
242cd187 587 /* local view of kconsumerd_data.fds_count */
1ce86c9a
JD
588 int nb_fd = 0;
589 char tmp;
590 int tmp2;
6533b585 591 struct lttng_kconsumerd_local_data *ctx = data;
1ce86c9a 592
1ce86c9a 593
6533b585 594 local_kconsumerd_fd = malloc(sizeof(struct lttng_kconsumerd_fd));
1ce86c9a
JD
595
596 while (1) {
597 high_prio = 0;
598 num_hup = 0;
599
600 /*
601 * the ltt_fd_list has been updated, we need to update our
602 * local array as well
603 */
242cd187
MD
604 pthread_mutex_lock(&kconsumerd_data.lock);
605 if (kconsumerd_data.need_update) {
1ce86c9a
JD
606 if (pollfd != NULL) {
607 free(pollfd);
608 pollfd = NULL;
609 }
610 if (local_kconsumerd_fd != NULL) {
611 free(local_kconsumerd_fd);
612 local_kconsumerd_fd = NULL;
613 }
0237248c 614
1ce86c9a 615 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
242cd187 616 pollfd = malloc((kconsumerd_data.fds_count + 1) * sizeof(struct pollfd));
1ce86c9a
JD
617 if (pollfd == NULL) {
618 perror("pollfd malloc");
242cd187 619 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
620 goto end;
621 }
0237248c 622
1ce86c9a 623 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
242cd187 624 local_kconsumerd_fd = malloc((kconsumerd_data.fds_count + 1) *
6533b585 625 sizeof(struct lttng_kconsumerd_fd));
1ce86c9a
JD
626 if (local_kconsumerd_fd == NULL) {
627 perror("local_kconsumerd_fd malloc");
242cd187 628 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
629 goto end;
630 }
cb040cc1 631 ret = kconsumerd_update_poll_array(ctx, &pollfd, local_kconsumerd_fd);
1ce86c9a
JD
632 if (ret < 0) {
633 ERR("Error in allocating pollfd or local_outfds");
6533b585 634 lttng_kconsumerd_send_error(ctx, KCONSUMERD_POLL_ERROR);
242cd187 635 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
636 goto end;
637 }
638 nb_fd = ret;
242cd187 639 kconsumerd_data.need_update = 0;
1ce86c9a 640 }
242cd187 641 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
642
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);
647 if (num_rdy == -1) {
648 perror("Poll error");
6533b585 649 lttng_kconsumerd_send_error(ctx, KCONSUMERD_POLL_ERROR);
1ce86c9a
JD
650 goto end;
651 } else if (num_rdy == 0) {
652 DBG("Polling thread timed out");
653 goto end;
654 }
655
656 /* No FDs and kconsumerd_quit, kconsumerd_cleanup the thread */
657 if (nb_fd == 0 && kconsumerd_quit == 1) {
658 goto end;
659 }
660
661 /*
242cd187
MD
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.
1ce86c9a 666 */
242cd187 667 if (pollfd[nb_fd].revents == POLLIN) {
1ce86c9a 668 DBG("kconsumerd_poll_pipe wake up");
cb040cc1 669 tmp2 = read(ctx->kconsumerd_poll_pipe[0], &tmp, 1);
f40799e8
DG
670 if (tmp2 < 0) {
671 perror("read kconsumerd poll");
672 }
1ce86c9a
JD
673 continue;
674 }
675
676 /* Take care of high priority channels first. */
677 for (i = 0; i < nb_fd; i++) {
678 switch(pollfd[i].revents) {
679 case POLLERR:
680 ERR("Error returned in polling fd %d.", pollfd[i].fd);
681 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
682 num_hup++;
683 break;
684 case POLLHUP:
685 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
686 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
687 num_hup++;
688 break;
689 case POLLNVAL:
690 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
691 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
692 num_hup++;
693 break;
694 case POLLPRI:
695 DBG("Urgent read on fd %d", pollfd[i].fd);
696 high_prio = 1;
cb040cc1 697 ret = ctx->on_buffer_ready(local_kconsumerd_fd[i]);
1ce86c9a
JD
698 /* it's ok to have an unavailable sub-buffer */
699 if (ret == EAGAIN) {
700 ret = 0;
701 }
702 break;
703 }
704 }
705
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) {
710 goto end;
711 }
712 continue;
713 }
714
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);
cb040cc1 720 ret = ctx->on_buffer_ready(local_kconsumerd_fd[i]);
1ce86c9a
JD
721 /* it's ok to have an unavailable subbuffer */
722 if (ret == EAGAIN) {
723 ret = 0;
724 }
725 }
726 }
727 }
728 }
729end:
730 DBG("polling thread exiting");
731 if (pollfd != NULL) {
732 free(pollfd);
733 pollfd = NULL;
734 }
735 if (local_kconsumerd_fd != NULL) {
736 free(local_kconsumerd_fd);
737 local_kconsumerd_fd = NULL;
738 }
1ce86c9a
JD
739 return NULL;
740}
741
742/*
6533b585 743 * Initialise the necessary environnement :
cb040cc1 744 * - create a new context
4de84ad9
JD
745 * - create the poll_pipe
746 * - create the should_quit pipe (for signal handler)
cb040cc1 747 * - create the thread pipe (for splice)
6533b585 748 *
cb040cc1
JD
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.
6533b585 753 *
cb040cc1 754 * Returns a pointer to the new context or NULL on error.
1ce86c9a 755 */
6533b585
DG
756struct lttng_kconsumerd_local_data *lttng_kconsumerd_create(
757 int (*buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd))
1ce86c9a 758{
4de84ad9 759 int ret;
6533b585 760 struct lttng_kconsumerd_local_data *ctx;
4de84ad9 761
6533b585 762 ctx = malloc(sizeof(struct lttng_kconsumerd_local_data));
cb040cc1
JD
763 if (ctx == NULL) {
764 perror("allocating context");
765 goto end;
766 }
767
768 ctx->on_buffer_ready = buffer_ready;
4de84ad9 769
cb040cc1 770 ret = pipe(ctx->kconsumerd_poll_pipe);
4de84ad9
JD
771 if (ret < 0) {
772 perror("Error creating poll pipe");
cb040cc1 773 ctx = NULL;
4de84ad9
JD
774 goto end;
775 }
776
cb040cc1 777 ret = pipe(ctx->kconsumerd_should_quit);
4de84ad9
JD
778 if (ret < 0) {
779 perror("Error creating recv pipe");
cb040cc1
JD
780 ctx = NULL;
781 goto end;
782 }
783
784 ret = pipe(ctx->kconsumerd_thread_pipe);
785 if (ret < 0) {
786 perror("Error creating thread pipe");
787 ctx = NULL;
4de84ad9
JD
788 goto end;
789 }
790
791end:
cb040cc1
JD
792 return ctx;
793}
794
795/*
6533b585 796 * Close all fds associated with the instance and free the context.
cb040cc1 797 */
6533b585 798void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data *ctx)
cb040cc1
JD
799{
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);
808 free(ctx);
809 ctx = NULL;
1ce86c9a
JD
810}
811
812/*
6533b585
DG
813 * This thread listens on the consumerd socket and receives the file
814 * descriptors from the session daemon.
1ce86c9a 815 */
6533b585 816void *lttng_kconsumerd_thread_receive_fds(void *data)
1ce86c9a
JD
817{
818 int sock, client_socket, ret;
819 struct lttcomm_kconsumerd_header tmp;
4de84ad9 820 /*
6533b585
DG
821 * structure to poll for incoming data on communication socket avoids
822 * making blocking sockets.
4de84ad9
JD
823 */
824 struct pollfd kconsumerd_sockpoll[2];
6533b585 825 struct lttng_kconsumerd_local_data *ctx = data;
4de84ad9 826
1ce86c9a 827
cb040cc1
JD
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);
1ce86c9a
JD
831 if (client_socket < 0) {
832 ERR("Cannot create command socket");
833 goto end;
834 }
835
836 ret = lttcomm_listen_unix_sock(client_socket);
837 if (ret < 0) {
838 goto end;
839 }
840
841 DBG("Sending ready command to ltt-sessiond");
6533b585 842 ret = lttng_kconsumerd_send_error(ctx, KCONSUMERD_COMMAND_SOCK_READY);
1ce86c9a
JD
843 if (ret < 0) {
844 ERR("Error sending ready command to ltt-sessiond");
845 goto end;
846 }
847
4de84ad9
JD
848 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
849 if (ret < 0) {
850 perror("fcntl O_NONBLOCK");
851 goto end;
852 }
853
854 /* prepare the FDs to poll : to client socket and the should_quit pipe */
cb040cc1 855 kconsumerd_sockpoll[0].fd = ctx->kconsumerd_should_quit[0];
4de84ad9
JD
856 kconsumerd_sockpoll[0].events = POLLIN | POLLPRI;
857 kconsumerd_sockpoll[1].fd = client_socket;
858 kconsumerd_sockpoll[1].events = POLLIN | POLLPRI;
859
6533b585 860 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
4de84ad9
JD
861 goto end;
862 }
863 DBG("Connection on client_socket");
864
1ce86c9a
JD
865 /* Blocking call, waiting for transmission */
866 sock = lttcomm_accept_unix_sock(client_socket);
867 if (sock <= 0) {
868 WARN("On accept");
869 goto end;
870 }
4de84ad9
JD
871 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
872 if (ret < 0) {
873 perror("fcntl O_NONBLOCK");
874 goto end;
875 }
876
877 /* update the polling structure to poll on the established socket */
878 kconsumerd_sockpoll[1].fd = sock;
879 kconsumerd_sockpoll[1].events = POLLIN | POLLPRI;
880
1ce86c9a 881 while (1) {
6533b585 882 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
4de84ad9
JD
883 goto end;
884 }
885 DBG("Incoming fds on sock");
886
1ce86c9a
JD
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));
890 if (ret <= 0) {
891 ERR("Communication interrupted on command socket");
892 goto end;
893 }
894 if (tmp.cmd_type == STOP) {
895 DBG("Received STOP command");
896 goto end;
897 }
3dcd2721
MD
898 if (kconsumerd_quit) {
899 DBG("kconsumerd_thread_receive_fds received quit from signal");
900 goto end;
901 }
4de84ad9 902
1ce86c9a 903 /* we received a command to add or update fds */
cb040cc1 904 ret = kconsumerd_consumerd_recv_fd(ctx, sock, kconsumerd_sockpoll,
4de84ad9 905 tmp.payload_size, tmp.cmd_type);
8b270bdb 906 if (ret < 0) {
1ce86c9a
JD
907 ERR("Receiving the FD, exiting");
908 goto end;
909 }
4de84ad9 910 DBG("received fds on sock");
1ce86c9a
JD
911 }
912
913end:
914 DBG("kconsumerd_thread_receive_fds exiting");
915
916 /*
917 * when all fds have hung up, the polling thread
918 * can exit cleanly
919 */
920 kconsumerd_quit = 1;
921
922 /*
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).
926 */
6533b585 927 kconsumerd_poll_timeout = LTTNG_KCONSUMERD_POLL_GRACE_PERIOD;
1ce86c9a
JD
928
929 /* wake up the polling thread */
cb040cc1 930 ret = write(ctx->kconsumerd_poll_pipe[1], "4", 1);
1ce86c9a
JD
931 if (ret < 0) {
932 perror("poll pipe write");
933 }
934 return NULL;
935}
936
937/*
6533b585
DG
938 * Close all the tracefiles and stream fds, should be called when all instances
939 * are destroyed.
1ce86c9a 940 */
6533b585 941void lttng_kconsumerd_cleanup(void)
1ce86c9a 942{
6533b585 943 struct lttng_kconsumerd_fd *iter, *tmp;
1ce86c9a 944
3dcd2721
MD
945 /*
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.
949 */
6533b585
DG
950 cds_list_for_each_entry_safe(iter, tmp,
951 &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
952 kconsumerd_del_fd(iter);
953 }
954}
955
3dcd2721
MD
956/*
957 * Called from signal handler.
958 */
6533b585 959void lttng_kconsumerd_should_exit(struct lttng_kconsumerd_local_data *ctx)
3dcd2721 960{
4de84ad9 961 int ret;
3dcd2721 962 kconsumerd_quit = 1;
cb040cc1 963 ret = write(ctx->kconsumerd_should_quit[1], "4", 1);
f40799e8
DG
964 if (ret < 0) {
965 perror("write kconsumerd quit");
966 }
3dcd2721
MD
967}
968
1ce86c9a 969/*
6533b585 970 * Send return code to the session daemon.
1ce86c9a 971 */
6533b585
DG
972int lttng_kconsumerd_send_error(
973 struct lttng_kconsumerd_local_data *ctx, int cmd)
1ce86c9a 974{
cb040cc1
JD
975 if (ctx->kconsumerd_error_socket > 0) {
976 return lttcomm_send_unix_sock(ctx->kconsumerd_error_socket, &cmd,
1ce86c9a
JD
977 sizeof(enum lttcomm_sessiond_command));
978 }
979
980 return 0;
981}
This page took 0.113236 seconds and 4 git commands to generate.