| 1 | /* |
| 2 | * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "payload.h" |
| 9 | #include <common/dynamic-array.h> |
| 10 | #include <common/dynamic-buffer.h> |
| 11 | #include <common/error.h> |
| 12 | |
| 13 | static |
| 14 | void release_fd_handle_ref(void *ptr) |
| 15 | { |
| 16 | struct fd_handle *fd_handle = ptr; |
| 17 | |
| 18 | fd_handle_put(fd_handle); |
| 19 | } |
| 20 | |
| 21 | LTTNG_HIDDEN |
| 22 | void lttng_payload_init(struct lttng_payload *payload) |
| 23 | { |
| 24 | assert(payload); |
| 25 | lttng_dynamic_buffer_init(&payload->buffer); |
| 26 | lttng_dynamic_pointer_array_init(&payload->_fd_handles, |
| 27 | release_fd_handle_ref); |
| 28 | } |
| 29 | |
| 30 | LTTNG_HIDDEN |
| 31 | int lttng_payload_copy(const struct lttng_payload *src_payload, |
| 32 | struct lttng_payload *dst_payload) |
| 33 | { |
| 34 | int ret; |
| 35 | size_t i; |
| 36 | |
| 37 | ret = lttng_dynamic_buffer_append_buffer( |
| 38 | &dst_payload->buffer, &src_payload->buffer); |
| 39 | if (ret) { |
| 40 | goto end; |
| 41 | } |
| 42 | |
| 43 | for (i = 0; i < lttng_dynamic_pointer_array_get_count( |
| 44 | &src_payload->_fd_handles); |
| 45 | i++) { |
| 46 | struct fd_handle *new_fd_handle; |
| 47 | const struct fd_handle *src_fd_handle = |
| 48 | lttng_dynamic_pointer_array_get_pointer( |
| 49 | &src_payload->_fd_handles, i); |
| 50 | |
| 51 | new_fd_handle = fd_handle_copy(src_fd_handle); |
| 52 | if (!new_fd_handle) { |
| 53 | PERROR("Failed to copy fd_handle while copying a payload"); |
| 54 | ret = -1; |
| 55 | goto end; |
| 56 | } |
| 57 | |
| 58 | ret = lttng_payload_push_fd_handle(dst_payload, new_fd_handle); |
| 59 | fd_handle_put(new_fd_handle); |
| 60 | if (ret) { |
| 61 | goto end; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | end: |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | LTTNG_HIDDEN |
| 70 | void lttng_payload_reset(struct lttng_payload *payload) |
| 71 | { |
| 72 | if (!payload) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | lttng_dynamic_buffer_reset(&payload->buffer); |
| 77 | lttng_dynamic_pointer_array_reset(&payload->_fd_handles); |
| 78 | } |
| 79 | |
| 80 | LTTNG_HIDDEN |
| 81 | void lttng_payload_clear(struct lttng_payload *payload) |
| 82 | { |
| 83 | lttng_dynamic_buffer_set_size(&payload->buffer, 0); |
| 84 | lttng_dynamic_pointer_array_clear(&payload->_fd_handles); |
| 85 | } |
| 86 | |
| 87 | LTTNG_HIDDEN |
| 88 | int lttng_payload_push_fd_handle(struct lttng_payload *payload, |
| 89 | struct fd_handle *fd_handle) |
| 90 | { |
| 91 | int ret; |
| 92 | |
| 93 | if (!payload) { |
| 94 | ret = -1; |
| 95 | goto end; |
| 96 | } |
| 97 | |
| 98 | ret = lttng_dynamic_pointer_array_add_pointer( |
| 99 | &payload->_fd_handles, fd_handle); |
| 100 | if (ret) { |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | fd_handle_get(fd_handle); |
| 105 | end: |
| 106 | return ret; |
| 107 | } |