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