Update version to 2.0-pre12
[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
1e307fab 21#include <assert.h>
1ce86c9a
JD
22#include <fcntl.h>
23#include <poll.h>
24#include <pthread.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/mman.h>
28#include <sys/socket.h>
29#include <sys/types.h>
30#include <unistd.h>
6533b585 31
1e307fab
DG
32#include <lttng-kernel-ctl.h>
33#include <lttng-sessiond-comm.h>
6533b585 34#include <lttng/lttng-kconsumerd.h>
1e307fab 35#include <lttngerr.h>
1ce86c9a 36
6533b585 37static struct lttng_kconsumerd_global_data {
242cd187
MD
38 /*
39 * kconsumerd_data.lock protects kconsumerd_data.fd_list,
6533b585
DG
40 * kconsumerd_data.fds_count, and kconsumerd_data.need_update. It ensures
41 * the count matches the number of items in the fd_list. It ensures the
42 * list updates *always* trigger an fd_array update (therefore need to make
43 * list update vs kconsumerd_data.need_update flag update atomic, and also
44 * flag read, fd array and flag clear atomic).
242cd187
MD
45 */
46 pthread_mutex_t lock;
47 /*
6533b585 48 * Number of element for the list below. Protected by kconsumerd_data.lock.
242cd187
MD
49 */
50 unsigned int fds_count;
51 /*
6533b585 52 * List of FDs. Protected by kconsumerd_data.lock.
242cd187 53 */
6533b585 54 struct lttng_kconsumerd_fd_list fd_list;
242cd187 55 /*
6533b585
DG
56 * Flag specifying if the local array of FDs needs update in the poll
57 * function. Protected by kconsumerd_data.lock.
242cd187
MD
58 */
59 unsigned int need_update;
60} kconsumerd_data = {
61 .fd_list.head = CDS_LIST_HEAD_INIT(kconsumerd_data.fd_list.head),
cb040cc1
JD
62 .fds_count = 0,
63 .need_update = 1,
1ce86c9a
JD
64};
65
6533b585 66/* timeout parameter, to control the polling thread grace period. */
1ce86c9a
JD
67static int kconsumerd_poll_timeout = -1;
68
3dcd2721 69/*
6533b585
DG
70 * Flag to inform the polling thread to quit when all fd hung up. Updated by
71 * the kconsumerd_thread_receive_fds when it notices that all fds has hung up.
72 * Also updated by the signal handler (kconsumerd_should_exit()). Read by the
73 * polling threads.
3dcd2721
MD
74 */
75static volatile int kconsumerd_quit = 0;
1ce86c9a
JD
76
77/*
6533b585
DG
78 * Find a session fd in the global list. The kconsumerd_data.lock must be
79 * locked during this call.
38079a1b 80 *
6533b585 81 * Return 1 if found else 0.
38079a1b
DG
82 */
83static int kconsumerd_find_session_fd(int fd)
84{
6533b585 85 struct lttng_kconsumerd_fd *iter;
38079a1b 86
242cd187 87 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
38079a1b
DG
88 if (iter->sessiond_fd == fd) {
89 DBG("Duplicate session fd %d", fd);
38079a1b
DG
90 return 1;
91 }
92 }
38079a1b
DG
93
94 return 0;
95}
96
1ce86c9a 97/*
6533b585 98 * Remove a fd from the global list protected by a mutex.
1ce86c9a 99 */
6533b585 100static void kconsumerd_del_fd(struct lttng_kconsumerd_fd *lcf)
1ce86c9a 101{
8b270bdb 102 int ret;
242cd187 103 pthread_mutex_lock(&kconsumerd_data.lock);
1ce86c9a 104 cds_list_del(&lcf->list);
242cd187
MD
105 if (kconsumerd_data.fds_count > 0) {
106 kconsumerd_data.fds_count--;
1ce86c9a 107 if (lcf != NULL) {
8b270bdb
JD
108 if (lcf->mmap_base != NULL) {
109 ret = munmap(lcf->mmap_base, lcf->mmap_len);
110 if (ret != 0) {
111 perror("munmap");
112 }
113 }
cb040cc1
JD
114 if (lcf->out_fd != 0) {
115 close(lcf->out_fd);
116 }
1ce86c9a
JD
117 close(lcf->consumerd_fd);
118 free(lcf);
119 lcf = NULL;
120 }
121 }
242cd187
MD
122 kconsumerd_data.need_update = 1;
123 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
124}
125
126/*
5348b470
JD
127 * Create a struct lttcomm_kconsumerd_msg from the
128 * information received on the receiving socket
1ce86c9a 129 */
5348b470
JD
130struct lttng_kconsumerd_fd *kconsumerd_allocate_fd(
131 struct lttcomm_kconsumerd_msg *buf,
6533b585 132 int consumerd_fd)
1ce86c9a 133{
6533b585 134 struct lttng_kconsumerd_fd *tmp_fd;
38079a1b 135
5348b470
JD
136 tmp_fd = malloc(sizeof(struct lttng_kconsumerd_fd));
137 if (tmp_fd == NULL) {
138 perror("malloc struct lttng_kconsumerd_fd");
38079a1b
DG
139 goto end;
140 }
1ce86c9a 141
1ce86c9a
JD
142 tmp_fd->sessiond_fd = buf->fd;
143 tmp_fd->consumerd_fd = consumerd_fd;
144 tmp_fd->state = buf->state;
145 tmp_fd->max_sb_size = buf->max_sb_size;
cb040cc1
JD
146 tmp_fd->out_fd = 0;
147 tmp_fd->out_fd_offset = 0;
8b270bdb
JD
148 tmp_fd->mmap_len = 0;
149 tmp_fd->mmap_base = NULL;
150 tmp_fd->output = buf->output;
1ce86c9a 151 strncpy(tmp_fd->path_name, buf->path_name, PATH_MAX);
99497cd0 152 tmp_fd->path_name[PATH_MAX - 1] = '\0';
5348b470
JD
153 DBG("Allocated %s (sessiond_fd %d, consumerd_fd %d, out_fd %d)",
154 tmp_fd->path_name, tmp_fd->sessiond_fd,
155 tmp_fd->consumerd_fd, tmp_fd->out_fd);
1ce86c9a 156
5348b470
JD
157end:
158 return tmp_fd;
159}
1ce86c9a 160
5348b470
JD
161/*
162 * Add a fd to the global list protected by a mutex.
163 */
164static int kconsumerd_add_fd(struct lttng_kconsumerd_fd *tmp_fd)
165{
166 int ret;
8b270bdb 167
5348b470
JD
168 pthread_mutex_lock(&kconsumerd_data.lock);
169 /* Check if already exist */
170 ret = kconsumerd_find_session_fd(tmp_fd->sessiond_fd);
171 if (ret == 1) {
172 goto end;
8b270bdb 173 }
242cd187
MD
174 cds_list_add(&tmp_fd->list, &kconsumerd_data.fd_list.head);
175 kconsumerd_data.fds_count++;
176 kconsumerd_data.need_update = 1;
5348b470 177
1ce86c9a 178end:
242cd187 179 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
180 return ret;
181}
182
183/*
6533b585 184 * Update a fd according to what we just received.
1ce86c9a
JD
185 */
186static void kconsumerd_change_fd_state(int sessiond_fd,
6533b585 187 enum lttng_kconsumerd_fd_state state)
1ce86c9a 188{
6533b585 189 struct lttng_kconsumerd_fd *iter;
0237248c 190
242cd187
MD
191 pthread_mutex_lock(&kconsumerd_data.lock);
192 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
193 if (iter->sessiond_fd == sessiond_fd) {
194 iter->state = state;
195 break;
196 }
197 }
242cd187
MD
198 kconsumerd_data.need_update = 1;
199 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
200}
201
202/*
6533b585
DG
203 * Allocate the pollfd structure and the local view of the out fds to avoid
204 * doing a lookup in the linked list and concurrency issues when writing is
205 * needed. Called with kconsumerd_data.lock held.
1ce86c9a 206 *
6533b585 207 * Returns the number of fds in the structures.
1ce86c9a 208 */
6533b585
DG
209static int kconsumerd_update_poll_array(
210 struct lttng_kconsumerd_local_data *ctx, struct pollfd **pollfd,
211 struct lttng_kconsumerd_fd **local_kconsumerd_fd)
1ce86c9a 212{
6533b585 213 struct lttng_kconsumerd_fd *iter;
1ce86c9a
JD
214 int i = 0;
215
216 DBG("Updating poll fd array");
242cd187 217 cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
218 if (iter->state == ACTIVE_FD) {
219 DBG("Active FD %d", iter->consumerd_fd);
220 (*pollfd)[i].fd = iter->consumerd_fd;
221 (*pollfd)[i].events = POLLIN | POLLPRI;
222 local_kconsumerd_fd[i] = iter;
223 i++;
224 }
225 }
226
227 /*
6533b585
DG
228 * Insert the kconsumerd_poll_pipe at the end of the array and don't
229 * increment i so nb_fd is the number of real FD.
1ce86c9a 230 */
cb040cc1 231 (*pollfd)[i].fd = ctx->kconsumerd_poll_pipe[0];
1ce86c9a 232 (*pollfd)[i].events = POLLIN;
1ce86c9a
JD
233 return i;
234}
235
6533b585
DG
236/*
237 * Receives an array of file descriptors and the associated structures
238 * describing each fd (path name).
239 *
240 * Returns the size of received data
241 */
242static int kconsumerd_consumerd_recv_fd(
243 struct lttng_kconsumerd_local_data *ctx, int sfd,
244 struct pollfd *kconsumerd_sockpoll, int size,
245 enum lttng_kconsumerd_command cmd_type)
246{
247 struct iovec iov[1];
248 int ret = 0, i, tmp2;
249 struct cmsghdr *cmsg;
250 int nb_fd;
251 char recv_fd[CMSG_SPACE(sizeof(int))];
252 struct lttcomm_kconsumerd_msg lkm;
5348b470 253 struct lttng_kconsumerd_fd *new_fd;
6533b585
DG
254
255 /* the number of fds we are about to receive */
256 nb_fd = size / sizeof(struct lttcomm_kconsumerd_msg);
257
258 /*
259 * nb_fd is the number of fds we receive. One fd per recvmsg.
260 */
261 for (i = 0; i < nb_fd; i++) {
262 struct msghdr msg = { 0 };
263
264 /* Prepare to receive the structures */
265 iov[0].iov_base = &lkm;
266 iov[0].iov_len = sizeof(lkm);
267 msg.msg_iov = iov;
268 msg.msg_iovlen = 1;
269
270 msg.msg_control = recv_fd;
271 msg.msg_controllen = sizeof(recv_fd);
272
273 DBG("Waiting to receive fd");
274 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
275 goto end;
276 }
277
278 if ((ret = recvmsg(sfd, &msg, 0)) < 0) {
279 perror("recvmsg");
280 continue;
281 }
282
283 if (ret != (size / nb_fd)) {
284 ERR("Received only %d, expected %d", ret, size);
285 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
286 goto end;
287 }
288
289 cmsg = CMSG_FIRSTHDR(&msg);
290 if (!cmsg) {
291 ERR("Invalid control message header");
292 ret = -1;
293 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
294 goto end;
295 }
296
297 /* if we received fds */
298 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
299 switch (cmd_type) {
300 case ADD_STREAM:
301 DBG("kconsumerd_add_fd %s (%d)", lkm.path_name,
302 ((int *) CMSG_DATA(cmsg))[0]);
303
5348b470
JD
304 new_fd = kconsumerd_allocate_fd(&lkm, ((int *) CMSG_DATA(cmsg))[0]);
305 if (new_fd == NULL) {
6533b585
DG
306 lttng_kconsumerd_send_error(ctx, KCONSUMERD_OUTFD_ERROR);
307 goto end;
308 }
5348b470
JD
309
310 if (ctx->on_recv_fd != NULL) {
311 ret = ctx->on_recv_fd(new_fd);
312 if (ret == 0) {
313 kconsumerd_add_fd(new_fd);
314 } else if (ret < 0) {
315 goto end;
316 }
317 } else {
318 kconsumerd_add_fd(new_fd);
319 }
6533b585
DG
320 break;
321 case UPDATE_STREAM:
5348b470
JD
322 if (ctx->on_update_fd != NULL) {
323 ret = ctx->on_update_fd(lkm.fd, lkm.state);
324 if (ret == 0) {
325 kconsumerd_change_fd_state(lkm.fd, lkm.state);
326 } else if (ret < 0) {
327 goto end;
328 }
329 } else {
330 kconsumerd_change_fd_state(lkm.fd, lkm.state);
331 }
6533b585
DG
332 break;
333 default:
334 break;
335 }
336 /* signal the poll thread */
337 tmp2 = write(ctx->kconsumerd_poll_pipe[1], "4", 1);
338 if (tmp2 < 0) {
339 perror("write kconsumerd poll");
340 }
341 } else {
342 ERR("Didn't received any fd");
343 lttng_kconsumerd_send_error(ctx, KCONSUMERD_ERROR_RECV_FD);
344 ret = -1;
345 goto end;
346 }
347 }
348
349end:
350 return ret;
351}
352
353/*
354 * Set the error socket.
355 */
356void lttng_kconsumerd_set_error_sock(
357 struct lttng_kconsumerd_local_data *ctx, int sock)
358{
359 ctx->kconsumerd_error_socket = sock;
360}
361
362/*
363 * Set the command socket path.
364 */
365
366void lttng_kconsumerd_set_command_sock_path(
367 struct lttng_kconsumerd_local_data *ctx, char *sock)
368{
369 ctx->kconsumerd_command_sock_path = sock;
370}
1ce86c9a 371
92ab9ab6
JD
372static void lttng_kconsumerd_sync_trace_file(
373 struct lttng_kconsumerd_fd *kconsumerd_fd, off_t orig_offset)
374{
375 int outfd = kconsumerd_fd->out_fd;
376 /*
377 * This does a blocking write-and-wait on any page that belongs to the
378 * subbuffer prior to the one we just wrote.
379 * Don't care about error values, as these are just hints and ways to
380 * limit the amount of page cache used.
381 */
382 if (orig_offset >= kconsumerd_fd->max_sb_size) {
383 sync_file_range(outfd, orig_offset - kconsumerd_fd->max_sb_size,
384 kconsumerd_fd->max_sb_size,
385 SYNC_FILE_RANGE_WAIT_BEFORE
386 | SYNC_FILE_RANGE_WRITE
387 | SYNC_FILE_RANGE_WAIT_AFTER);
388 /*
389 * Give hints to the kernel about how we access the file:
390 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
391 * we write it.
392 *
393 * We need to call fadvise again after the file grows because the
394 * kernel does not seem to apply fadvise to non-existing parts of the
395 * file.
396 *
397 * Call fadvise _after_ having waited for the page writeback to
398 * complete because the dirty page writeback semantic is not well
399 * defined. So it can be expected to lead to lower throughput in
400 * streaming.
401 */
402 posix_fadvise(outfd, orig_offset - kconsumerd_fd->max_sb_size,
403 kconsumerd_fd->max_sb_size, POSIX_FADV_DONTNEED);
404 }
405}
406
407
1ce86c9a 408/*
6533b585 409 * Mmap the ring buffer, read it and write the data to the tracefile.
1ce86c9a 410 *
1ce86c9a
JD
411 * Returns the number of bytes written
412 */
6533b585
DG
413int lttng_kconsumerd_on_read_subbuffer_mmap(
414 struct lttng_kconsumerd_local_data *ctx,
415 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len)
1ce86c9a 416{
8b270bdb 417 unsigned long mmap_offset;
1ce86c9a
JD
418 long ret = 0;
419 off_t orig_offset = kconsumerd_fd->out_fd_offset;
420 int fd = kconsumerd_fd->consumerd_fd;
421 int outfd = kconsumerd_fd->out_fd;
422
1ce86c9a
JD
423 /* get the offset inside the fd to mmap */
424 ret = kernctl_get_mmap_read_offset(fd, &mmap_offset);
425 if (ret != 0) {
426 ret = errno;
427 perror("kernctl_get_mmap_read_offset");
428 goto end;
429 }
430
1ce86c9a 431 while (len > 0) {
8b270bdb 432 ret = write(outfd, kconsumerd_fd->mmap_base + mmap_offset, len);
1ce86c9a
JD
433 if (ret >= len) {
434 len = 0;
435 } else if (ret < 0) {
436 ret = errno;
437 perror("Error in file write");
438 goto end;
439 }
440 /* This won't block, but will start writeout asynchronously */
441 sync_file_range(outfd, kconsumerd_fd->out_fd_offset, ret,
442 SYNC_FILE_RANGE_WRITE);
443 kconsumerd_fd->out_fd_offset += ret;
444 }
445
92ab9ab6 446 lttng_kconsumerd_sync_trace_file(kconsumerd_fd, orig_offset);
1ce86c9a 447
1ce86c9a
JD
448 goto end;
449
450end:
1ce86c9a
JD
451 return ret;
452}
453
454/*
1ce86c9a 455 * Splice the data from the ring buffer to the tracefile.
6533b585
DG
456 *
457 * Returns the number of bytes spliced.
1ce86c9a 458 */
6533b585
DG
459int lttng_kconsumerd_on_read_subbuffer_splice(
460 struct lttng_kconsumerd_local_data *ctx,
461 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len)
1ce86c9a
JD
462{
463 long ret = 0;
464 loff_t offset = 0;
465 off_t orig_offset = kconsumerd_fd->out_fd_offset;
466 int fd = kconsumerd_fd->consumerd_fd;
467 int outfd = kconsumerd_fd->out_fd;
468
469 while (len > 0) {
470 DBG("splice chan to pipe offset %lu (fd : %d)",
471 (unsigned long)offset, fd);
cb040cc1 472 ret = splice(fd, &offset, ctx->kconsumerd_thread_pipe[1], NULL, len,
1ce86c9a
JD
473 SPLICE_F_MOVE | SPLICE_F_MORE);
474 DBG("splice chan to pipe ret %ld", ret);
475 if (ret < 0) {
476 ret = errno;
477 perror("Error in relay splice");
478 goto splice_error;
479 }
480
cb040cc1 481 ret = splice(ctx->kconsumerd_thread_pipe[0], NULL, outfd, NULL, ret,
1ce86c9a
JD
482 SPLICE_F_MOVE | SPLICE_F_MORE);
483 DBG("splice pipe to file %ld", ret);
484 if (ret < 0) {
485 ret = errno;
486 perror("Error in file splice");
487 goto splice_error;
488 }
751667bd 489 len -= ret;
1ce86c9a
JD
490 /* This won't block, but will start writeout asynchronously */
491 sync_file_range(outfd, kconsumerd_fd->out_fd_offset, ret,
492 SYNC_FILE_RANGE_WRITE);
493 kconsumerd_fd->out_fd_offset += ret;
494 }
92ab9ab6 495 lttng_kconsumerd_sync_trace_file(kconsumerd_fd, orig_offset);
1ce86c9a 496
1ce86c9a
JD
497 goto end;
498
499splice_error:
500 /* send the appropriate error description to sessiond */
501 switch(ret) {
502 case EBADF:
6533b585 503 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_EBADF);
1ce86c9a
JD
504 break;
505 case EINVAL:
6533b585 506 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_EINVAL);
1ce86c9a
JD
507 break;
508 case ENOMEM:
6533b585 509 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_ENOMEM);
1ce86c9a
JD
510 break;
511 case ESPIPE:
6533b585 512 lttng_kconsumerd_send_error(ctx, KCONSUMERD_SPLICE_ESPIPE);
1ce86c9a 513 break;
1ce86c9a
JD
514 }
515
516end:
517 return ret;
518}
519
92ab9ab6
JD
520/*
521 * Take a snapshot for a specific fd
522 *
523 * Returns 0 on success, < 0 on error
524 */
525int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data *ctx,
526 struct lttng_kconsumerd_fd *kconsumerd_fd)
527{
528 int ret = 0;
529 int infd = kconsumerd_fd->consumerd_fd;
530
531 ret = kernctl_snapshot(infd);
532 if (ret != 0) {
533 ret = errno;
534 perror("Getting sub-buffer snapshot.");
535 }
536
537 return ret;
538}
539
540/*
541 * Get the produced position
542 *
543 * Returns 0 on success, < 0 on error
544 */
545int lttng_kconsumerd_get_produced_snapshot(
546 struct lttng_kconsumerd_local_data *ctx,
547 struct lttng_kconsumerd_fd *kconsumerd_fd,
548 unsigned long *pos)
549{
550 int ret;
551 int infd = kconsumerd_fd->consumerd_fd;
552
553 ret = kernctl_snapshot_get_produced(infd, pos);
554 if (ret != 0) {
555 ret = errno;
556 perror("kernctl_snapshot_get_produced");
557 }
558
559 return ret;
560}
561
4de84ad9 562/*
6533b585
DG
563 * Poll on the should_quit pipe and the command socket return -1 on error and
564 * should exit, 0 if data is available on the command socket
4de84ad9 565 */
6533b585 566int lttng_kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll)
4de84ad9
JD
567{
568 int num_rdy;
569
570 num_rdy = poll(kconsumerd_sockpoll, 2, -1);
571 if (num_rdy == -1) {
572 perror("Poll error");
573 goto exit;
574 }
575 if (kconsumerd_sockpoll[0].revents == POLLIN) {
576 DBG("kconsumerd_should_quit wake up");
577 goto exit;
578 }
579 return 0;
580
581exit:
582 return -1;
583}
584
1ce86c9a 585/*
6533b585
DG
586 * This thread polls the fds in the ltt_fd_list to consume the data and write
587 * it to tracefile if necessary.
1ce86c9a 588 */
6533b585 589void *lttng_kconsumerd_thread_poll_fds(void *data)
1ce86c9a
JD
590{
591 int num_rdy, num_hup, high_prio, ret, i;
592 struct pollfd *pollfd = NULL;
593 /* local view of the fds */
6533b585 594 struct lttng_kconsumerd_fd **local_kconsumerd_fd = NULL;
242cd187 595 /* local view of kconsumerd_data.fds_count */
1ce86c9a
JD
596 int nb_fd = 0;
597 char tmp;
598 int tmp2;
6533b585 599 struct lttng_kconsumerd_local_data *ctx = data;
1ce86c9a 600
1ce86c9a 601
6533b585 602 local_kconsumerd_fd = malloc(sizeof(struct lttng_kconsumerd_fd));
1ce86c9a
JD
603
604 while (1) {
605 high_prio = 0;
606 num_hup = 0;
607
608 /*
609 * the ltt_fd_list has been updated, we need to update our
610 * local array as well
611 */
242cd187
MD
612 pthread_mutex_lock(&kconsumerd_data.lock);
613 if (kconsumerd_data.need_update) {
1ce86c9a
JD
614 if (pollfd != NULL) {
615 free(pollfd);
616 pollfd = NULL;
617 }
618 if (local_kconsumerd_fd != NULL) {
619 free(local_kconsumerd_fd);
620 local_kconsumerd_fd = NULL;
621 }
0237248c 622
1ce86c9a 623 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
242cd187 624 pollfd = malloc((kconsumerd_data.fds_count + 1) * sizeof(struct pollfd));
1ce86c9a
JD
625 if (pollfd == NULL) {
626 perror("pollfd malloc");
242cd187 627 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
628 goto end;
629 }
0237248c 630
1ce86c9a 631 /* allocate for all fds + 1 for the kconsumerd_poll_pipe */
242cd187 632 local_kconsumerd_fd = malloc((kconsumerd_data.fds_count + 1) *
6533b585 633 sizeof(struct lttng_kconsumerd_fd));
1ce86c9a
JD
634 if (local_kconsumerd_fd == NULL) {
635 perror("local_kconsumerd_fd malloc");
242cd187 636 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
637 goto end;
638 }
cb040cc1 639 ret = kconsumerd_update_poll_array(ctx, &pollfd, local_kconsumerd_fd);
1ce86c9a
JD
640 if (ret < 0) {
641 ERR("Error in allocating pollfd or local_outfds");
6533b585 642 lttng_kconsumerd_send_error(ctx, KCONSUMERD_POLL_ERROR);
242cd187 643 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
644 goto end;
645 }
646 nb_fd = ret;
242cd187 647 kconsumerd_data.need_update = 0;
1ce86c9a 648 }
242cd187 649 pthread_mutex_unlock(&kconsumerd_data.lock);
1ce86c9a
JD
650
651 /* poll on the array of fds */
652 DBG("polling on %d fd", nb_fd + 1);
653 num_rdy = poll(pollfd, nb_fd + 1, kconsumerd_poll_timeout);
654 DBG("poll num_rdy : %d", num_rdy);
655 if (num_rdy == -1) {
656 perror("Poll error");
6533b585 657 lttng_kconsumerd_send_error(ctx, KCONSUMERD_POLL_ERROR);
1ce86c9a
JD
658 goto end;
659 } else if (num_rdy == 0) {
660 DBG("Polling thread timed out");
661 goto end;
662 }
663
664 /* No FDs and kconsumerd_quit, kconsumerd_cleanup the thread */
665 if (nb_fd == 0 && kconsumerd_quit == 1) {
666 goto end;
667 }
668
669 /*
242cd187
MD
670 * If the kconsumerd_poll_pipe triggered poll go
671 * directly to the beginning of the loop to update the
672 * array. We want to prioritize array update over
673 * low-priority reads.
1ce86c9a 674 */
242cd187 675 if (pollfd[nb_fd].revents == POLLIN) {
1ce86c9a 676 DBG("kconsumerd_poll_pipe wake up");
cb040cc1 677 tmp2 = read(ctx->kconsumerd_poll_pipe[0], &tmp, 1);
f40799e8
DG
678 if (tmp2 < 0) {
679 perror("read kconsumerd poll");
680 }
1ce86c9a
JD
681 continue;
682 }
683
684 /* Take care of high priority channels first. */
685 for (i = 0; i < nb_fd; i++) {
686 switch(pollfd[i].revents) {
687 case POLLERR:
688 ERR("Error returned in polling fd %d.", pollfd[i].fd);
689 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
690 num_hup++;
691 break;
692 case POLLHUP:
693 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
694 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
695 num_hup++;
696 break;
697 case POLLNVAL:
698 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
699 kconsumerd_del_fd(local_kconsumerd_fd[i]);
1ce86c9a
JD
700 num_hup++;
701 break;
702 case POLLPRI:
703 DBG("Urgent read on fd %d", pollfd[i].fd);
704 high_prio = 1;
cb040cc1 705 ret = ctx->on_buffer_ready(local_kconsumerd_fd[i]);
1ce86c9a
JD
706 /* it's ok to have an unavailable sub-buffer */
707 if (ret == EAGAIN) {
708 ret = 0;
709 }
710 break;
711 }
712 }
713
714 /* If every buffer FD has hung up, we end the read loop here */
715 if (nb_fd > 0 && num_hup == nb_fd) {
716 DBG("every buffer FD has hung up\n");
717 if (kconsumerd_quit == 1) {
718 goto end;
719 }
720 continue;
721 }
722
723 /* Take care of low priority channels. */
724 if (high_prio == 0) {
725 for (i = 0; i < nb_fd; i++) {
726 if (pollfd[i].revents == POLLIN) {
727 DBG("Normal read on fd %d", pollfd[i].fd);
cb040cc1 728 ret = ctx->on_buffer_ready(local_kconsumerd_fd[i]);
1ce86c9a
JD
729 /* it's ok to have an unavailable subbuffer */
730 if (ret == EAGAIN) {
731 ret = 0;
732 }
733 }
734 }
735 }
736 }
737end:
738 DBG("polling thread exiting");
739 if (pollfd != NULL) {
740 free(pollfd);
741 pollfd = NULL;
742 }
743 if (local_kconsumerd_fd != NULL) {
744 free(local_kconsumerd_fd);
745 local_kconsumerd_fd = NULL;
746 }
1ce86c9a
JD
747 return NULL;
748}
749
750/*
6533b585 751 * Initialise the necessary environnement :
cb040cc1 752 * - create a new context
4de84ad9
JD
753 * - create the poll_pipe
754 * - create the should_quit pipe (for signal handler)
cb040cc1 755 * - create the thread pipe (for splice)
6533b585 756 *
cb040cc1
JD
757 * Takes a function pointer as argument, this function is called when data is
758 * available on a buffer. This function is responsible to do the
759 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
760 * buffer configuration and then kernctl_put_next_subbuf at the end.
6533b585 761 *
cb040cc1 762 * Returns a pointer to the new context or NULL on error.
1ce86c9a 763 */
6533b585 764struct lttng_kconsumerd_local_data *lttng_kconsumerd_create(
5348b470
JD
765 int (*buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd),
766 int (*recv_fd)(struct lttng_kconsumerd_fd *kconsumerd_fd),
767 int (*update_fd)(int sessiond_fd, uint32_t state))
1ce86c9a 768{
5348b470 769 int ret, i;
6533b585 770 struct lttng_kconsumerd_local_data *ctx;
4de84ad9 771
6533b585 772 ctx = malloc(sizeof(struct lttng_kconsumerd_local_data));
cb040cc1
JD
773 if (ctx == NULL) {
774 perror("allocating context");
5348b470 775 goto error;
cb040cc1
JD
776 }
777
5348b470 778 /* assign the callbacks */
cb040cc1 779 ctx->on_buffer_ready = buffer_ready;
5348b470
JD
780 ctx->on_recv_fd = recv_fd;
781 ctx->on_update_fd = update_fd;
4de84ad9 782
cb040cc1 783 ret = pipe(ctx->kconsumerd_poll_pipe);
4de84ad9
JD
784 if (ret < 0) {
785 perror("Error creating poll pipe");
5348b470 786 goto error_poll_pipe;
4de84ad9
JD
787 }
788
cb040cc1 789 ret = pipe(ctx->kconsumerd_should_quit);
4de84ad9
JD
790 if (ret < 0) {
791 perror("Error creating recv pipe");
5348b470 792 goto error_quit_pipe;
cb040cc1
JD
793 }
794
795 ret = pipe(ctx->kconsumerd_thread_pipe);
796 if (ret < 0) {
797 perror("Error creating thread pipe");
5348b470 798 goto error_thread_pipe;
4de84ad9
JD
799 }
800
cb040cc1 801 return ctx;
5348b470
JD
802
803
804error_thread_pipe:
805 for (i = 0; i < 2; i++) {
806 int err;
807
808 err = close(ctx->kconsumerd_should_quit[i]);
809 assert(!err);
810 }
811error_quit_pipe:
812 for (i = 0; i < 2; i++) {
813 int err;
814
815 err = close(ctx->kconsumerd_poll_pipe[i]);
816 assert(!err);
817 }
818error_poll_pipe:
819 free(ctx);
820error:
821 return NULL;
cb040cc1
JD
822}
823
824/*
6533b585 825 * Close all fds associated with the instance and free the context.
cb040cc1 826 */
6533b585 827void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data *ctx)
cb040cc1
JD
828{
829 close(ctx->kconsumerd_error_socket);
830 close(ctx->kconsumerd_thread_pipe[0]);
831 close(ctx->kconsumerd_thread_pipe[1]);
832 close(ctx->kconsumerd_poll_pipe[0]);
833 close(ctx->kconsumerd_poll_pipe[1]);
834 close(ctx->kconsumerd_should_quit[0]);
835 close(ctx->kconsumerd_should_quit[1]);
836 unlink(ctx->kconsumerd_command_sock_path);
837 free(ctx);
838 ctx = NULL;
1ce86c9a
JD
839}
840
841/*
6533b585
DG
842 * This thread listens on the consumerd socket and receives the file
843 * descriptors from the session daemon.
1ce86c9a 844 */
6533b585 845void *lttng_kconsumerd_thread_receive_fds(void *data)
1ce86c9a
JD
846{
847 int sock, client_socket, ret;
848 struct lttcomm_kconsumerd_header tmp;
4de84ad9 849 /*
6533b585
DG
850 * structure to poll for incoming data on communication socket avoids
851 * making blocking sockets.
4de84ad9
JD
852 */
853 struct pollfd kconsumerd_sockpoll[2];
6533b585 854 struct lttng_kconsumerd_local_data *ctx = data;
4de84ad9 855
1ce86c9a 856
cb040cc1
JD
857 DBG("Creating command socket %s", ctx->kconsumerd_command_sock_path);
858 unlink(ctx->kconsumerd_command_sock_path);
859 client_socket = lttcomm_create_unix_sock(ctx->kconsumerd_command_sock_path);
1ce86c9a
JD
860 if (client_socket < 0) {
861 ERR("Cannot create command socket");
862 goto end;
863 }
864
865 ret = lttcomm_listen_unix_sock(client_socket);
866 if (ret < 0) {
867 goto end;
868 }
869
870 DBG("Sending ready command to ltt-sessiond");
6533b585 871 ret = lttng_kconsumerd_send_error(ctx, KCONSUMERD_COMMAND_SOCK_READY);
1ce86c9a
JD
872 if (ret < 0) {
873 ERR("Error sending ready command to ltt-sessiond");
874 goto end;
875 }
876
4de84ad9
JD
877 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
878 if (ret < 0) {
879 perror("fcntl O_NONBLOCK");
880 goto end;
881 }
882
883 /* prepare the FDs to poll : to client socket and the should_quit pipe */
cb040cc1 884 kconsumerd_sockpoll[0].fd = ctx->kconsumerd_should_quit[0];
4de84ad9
JD
885 kconsumerd_sockpoll[0].events = POLLIN | POLLPRI;
886 kconsumerd_sockpoll[1].fd = client_socket;
887 kconsumerd_sockpoll[1].events = POLLIN | POLLPRI;
888
6533b585 889 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
4de84ad9
JD
890 goto end;
891 }
892 DBG("Connection on client_socket");
893
1ce86c9a
JD
894 /* Blocking call, waiting for transmission */
895 sock = lttcomm_accept_unix_sock(client_socket);
896 if (sock <= 0) {
897 WARN("On accept");
898 goto end;
899 }
4de84ad9
JD
900 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
901 if (ret < 0) {
902 perror("fcntl O_NONBLOCK");
903 goto end;
904 }
905
906 /* update the polling structure to poll on the established socket */
907 kconsumerd_sockpoll[1].fd = sock;
908 kconsumerd_sockpoll[1].events = POLLIN | POLLPRI;
909
1ce86c9a 910 while (1) {
6533b585 911 if (lttng_kconsumerd_poll_socket(kconsumerd_sockpoll) < 0) {
4de84ad9
JD
912 goto end;
913 }
914 DBG("Incoming fds on sock");
915
1ce86c9a
JD
916 /* We first get the number of fd we are about to receive */
917 ret = lttcomm_recv_unix_sock(sock, &tmp,
918 sizeof(struct lttcomm_kconsumerd_header));
919 if (ret <= 0) {
920 ERR("Communication interrupted on command socket");
921 goto end;
922 }
923 if (tmp.cmd_type == STOP) {
924 DBG("Received STOP command");
925 goto end;
926 }
3dcd2721
MD
927 if (kconsumerd_quit) {
928 DBG("kconsumerd_thread_receive_fds received quit from signal");
929 goto end;
930 }
4de84ad9 931
1ce86c9a 932 /* we received a command to add or update fds */
cb040cc1 933 ret = kconsumerd_consumerd_recv_fd(ctx, sock, kconsumerd_sockpoll,
4de84ad9 934 tmp.payload_size, tmp.cmd_type);
8b270bdb 935 if (ret < 0) {
1ce86c9a
JD
936 ERR("Receiving the FD, exiting");
937 goto end;
938 }
4de84ad9 939 DBG("received fds on sock");
1ce86c9a
JD
940 }
941
942end:
943 DBG("kconsumerd_thread_receive_fds exiting");
944
945 /*
946 * when all fds have hung up, the polling thread
947 * can exit cleanly
948 */
949 kconsumerd_quit = 1;
950
951 /*
952 * 2s of grace period, if no polling events occur during
953 * this period, the polling thread will exit even if there
954 * are still open FDs (should not happen, but safety mechanism).
955 */
6533b585 956 kconsumerd_poll_timeout = LTTNG_KCONSUMERD_POLL_GRACE_PERIOD;
1ce86c9a
JD
957
958 /* wake up the polling thread */
cb040cc1 959 ret = write(ctx->kconsumerd_poll_pipe[1], "4", 1);
1ce86c9a
JD
960 if (ret < 0) {
961 perror("poll pipe write");
962 }
963 return NULL;
964}
965
966/*
6533b585
DG
967 * Close all the tracefiles and stream fds, should be called when all instances
968 * are destroyed.
1ce86c9a 969 */
6533b585 970void lttng_kconsumerd_cleanup(void)
1ce86c9a 971{
6533b585 972 struct lttng_kconsumerd_fd *iter, *tmp;
1ce86c9a 973
3dcd2721
MD
974 /*
975 * close all outfd. Called when there are no more threads
976 * running (after joining on the threads), no need to protect
977 * list iteration with mutex.
978 */
6533b585
DG
979 cds_list_for_each_entry_safe(iter, tmp,
980 &kconsumerd_data.fd_list.head, list) {
1ce86c9a
JD
981 kconsumerd_del_fd(iter);
982 }
983}
984
3dcd2721
MD
985/*
986 * Called from signal handler.
987 */
6533b585 988void lttng_kconsumerd_should_exit(struct lttng_kconsumerd_local_data *ctx)
3dcd2721 989{
4de84ad9 990 int ret;
3dcd2721 991 kconsumerd_quit = 1;
cb040cc1 992 ret = write(ctx->kconsumerd_should_quit[1], "4", 1);
f40799e8
DG
993 if (ret < 0) {
994 perror("write kconsumerd quit");
995 }
3dcd2721
MD
996}
997
1ce86c9a 998/*
6533b585 999 * Send return code to the session daemon.
1ce86c9a 1000 */
6533b585
DG
1001int lttng_kconsumerd_send_error(
1002 struct lttng_kconsumerd_local_data *ctx, int cmd)
1ce86c9a 1003{
cb040cc1
JD
1004 if (ctx->kconsumerd_error_socket > 0) {
1005 return lttcomm_send_unix_sock(ctx->kconsumerd_error_socket, &cmd,
1ce86c9a
JD
1006 sizeof(enum lttcomm_sessiond_command));
1007 }
1008
1009 return 0;
1010}
This page took 0.068614 seconds and 4 git commands to generate.