uprobe: transmit binary file descritptor through lttng_payload
[lttng-tools.git] / src / common / payload-view.c
CommitLineData
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
5a2f5f00 8#include <common/dynamic-array.h>
c0a66c84
JG
9#include <common/buffer-view.h>
10#include "payload-view.h"
11#include "payload.h"
12#include <stddef.h>
13
14LTTNG_HIDDEN
15struct lttng_payload_view lttng_payload_view_from_payload(
16 const struct lttng_payload *payload, size_t offset,
17 ptrdiff_t len)
18{
19 return (struct lttng_payload_view) {
20 .buffer = lttng_buffer_view_from_dynamic_buffer(
21 &payload->buffer, offset, len),
22 ._fds = payload->_fds,
23 };
24}
25
26LTTNG_HIDDEN
27struct lttng_payload_view lttng_payload_view_from_view(
28 struct lttng_payload_view *view, size_t offset,
29 ptrdiff_t len)
30{
31 return (struct lttng_payload_view) {
32 .buffer = lttng_buffer_view_from_view(
ddab38d1 33 &view->buffer, offset, len),
c0a66c84 34 ._fds = view->_fds,
ddab38d1
JG
35 ._iterator.p_fds_position = view->_iterator.p_fds_position ?:
36 &view->_iterator.fds_position,
c0a66c84
JG
37 };
38}
39
40LTTNG_HIDDEN
41struct lttng_payload_view lttng_payload_view_from_dynamic_buffer(
42 const struct lttng_dynamic_buffer *buffer, size_t offset,
43 ptrdiff_t len)
44{
45 return (struct lttng_payload_view) {
46 .buffer = lttng_buffer_view_from_dynamic_buffer(
47 buffer, offset, len)
48 };
49}
50
5a2f5f00
JG
51LTTNG_HIDDEN
52struct lttng_payload_view lttng_payload_view_from_buffer_view(
53 const struct lttng_buffer_view *view, size_t offset,
54 ptrdiff_t len)
55{
56 return (struct lttng_payload_view) {
57 .buffer = lttng_buffer_view_from_view(
58 view, offset, len)
59 };
60}
61
e368fb43
JG
62LTTNG_HIDDEN
63struct lttng_payload_view lttng_payload_view_init_from_buffer(
64 const char *src, size_t offset, ptrdiff_t len)
65{
66 return (struct lttng_payload_view) {
67 .buffer = lttng_buffer_view_init(
68 src, offset, len)
69 };
70}
71
5a2f5f00
JG
72LTTNG_HIDDEN
73int lttng_payload_view_get_fd_count(struct lttng_payload_view *payload_view)
74{
75 int ret;
76 size_t position;
77
78 if (!payload_view) {
79 ret = -1;
80 goto end;
81 }
82
83 ret = lttng_dynamic_array_get_count(&payload_view->_fds);
84 if (ret < 0) {
85 goto end;
86 }
87
88 position = payload_view->_iterator.p_fds_position ?
89 *payload_view->_iterator.p_fds_position :
90 payload_view->_iterator.fds_position;
91 ret = ret - (int) position;
92end:
93 return ret;
94}
95
c0a66c84
JG
96LTTNG_HIDDEN
97int lttng_payload_view_pop_fd(struct lttng_payload_view *view)
98{
99 int ret = 0;
100 size_t fd_count;
101 size_t *pos;
102
103 if (!view) {
104 ret = -1;
105 goto end;
106 }
107
108 fd_count = lttng_dynamic_array_get_count(&view->_fds);
109 pos = view->_iterator.p_fds_position ? view->_iterator.p_fds_position :
110 &view->_iterator.fds_position;
111
112 if (*pos >= fd_count) {
113 ret = -1;
114 goto end;
115 }
116
117 ret = *((int *) lttng_dynamic_array_get_element(
118 &view->_fds, *pos));
119 (*pos)++;
120end:
121 return ret;
122}
This page took 0.042424 seconds and 4 git commands to generate.