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