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