]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-message.h
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-message.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <byteswap.h>
5 #include <stdbool.h>
6 #include <sys/socket.h>
7
8 #include "sd-bus.h"
9
10 #include "bus-creds.h"
11 #include "bus-protocol.h"
12 #include "macro.h"
13 #include "time-util.h"
14
15 struct bus_container {
16 char enclosing;
17 bool need_offsets:1;
18
19 /* Indexes into the signature string */
20 unsigned index, saved_index;
21 char *signature;
22
23 size_t before, begin, end;
24
25 /* dbus1: pointer to the array size value, if this is a value */
26 uint32_t *array_size;
27
28 /* gvariant: list of offsets to end of children if this is struct/dict entry/array */
29 size_t *offsets, n_offsets, offsets_allocated, offset_index;
30 size_t item_size;
31
32 char *peeked_signature;
33 };
34
35 struct bus_body_part {
36 struct bus_body_part *next;
37 void *data;
38 void *mmap_begin;
39 size_t size;
40 size_t mapped;
41 size_t allocated;
42 uint64_t memfd_offset;
43 int memfd;
44 bool free_this:1;
45 bool munmap_this:1;
46 bool sealed:1;
47 bool is_zero:1;
48 };
49
50 struct sd_bus_message {
51 unsigned n_ref;
52
53 sd_bus *bus;
54
55 uint64_t reply_cookie;
56
57 const char *path;
58 const char *interface;
59 const char *member;
60 const char *destination;
61 const char *sender;
62
63 sd_bus_error error;
64
65 sd_bus_creds creds;
66
67 usec_t monotonic;
68 usec_t realtime;
69 uint64_t seqnum;
70 int64_t priority;
71 uint64_t verify_destination_id;
72
73 bool sealed:1;
74 bool dont_send:1;
75 bool allow_fds:1;
76 bool free_header:1;
77 bool free_fds:1;
78 bool poisoned:1;
79
80 /* The first and last bytes of the message */
81 struct bus_header *header;
82 void *footer;
83
84 /* How many bytes are accessible in the above pointers */
85 size_t header_accessible;
86 size_t footer_accessible;
87
88 size_t fields_size;
89 size_t body_size;
90 size_t user_body_size;
91
92 struct bus_body_part body;
93 struct bus_body_part *body_end;
94 unsigned n_body_parts;
95
96 size_t rindex;
97 struct bus_body_part *cached_rindex_part;
98 size_t cached_rindex_part_begin;
99
100 uint32_t n_fds;
101 int *fds;
102
103 struct bus_container root_container, *containers;
104 size_t n_containers;
105 size_t containers_allocated;
106
107 struct iovec *iovec;
108 struct iovec iovec_fixed[2];
109 unsigned n_iovec;
110
111 char *peeked_signature;
112
113 /* If set replies to this message must carry the signature
114 * specified here to successfully seal. This is initialized
115 * from the vtable data */
116 const char *enforced_reply_signature;
117
118 usec_t timeout;
119
120 size_t header_offsets[_BUS_MESSAGE_HEADER_MAX];
121 unsigned n_header_offsets;
122 };
123
124 static inline bool BUS_MESSAGE_NEED_BSWAP(sd_bus_message *m) {
125 return m->header->endian != BUS_NATIVE_ENDIAN;
126 }
127
128 static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
129 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
130 }
131
132 static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
133 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
134 }
135
136 static inline uint64_t BUS_MESSAGE_BSWAP64(sd_bus_message *m, uint64_t u) {
137 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_64(u) : u;
138 }
139
140 static inline uint64_t BUS_MESSAGE_COOKIE(sd_bus_message *m) {
141 if (m->header->version == 2)
142 return BUS_MESSAGE_BSWAP64(m, m->header->dbus2.cookie);
143
144 return BUS_MESSAGE_BSWAP32(m, m->header->dbus1.serial);
145 }
146
147 static inline size_t BUS_MESSAGE_SIZE(sd_bus_message *m) {
148 return
149 sizeof(struct bus_header) +
150 ALIGN8(m->fields_size) +
151 m->body_size;
152 }
153
154 static inline size_t BUS_MESSAGE_BODY_BEGIN(sd_bus_message *m) {
155 return
156 sizeof(struct bus_header) +
157 ALIGN8(m->fields_size);
158 }
159
160 static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
161 return (uint8_t*) m->header + sizeof(struct bus_header);
162 }
163
164 static inline bool BUS_MESSAGE_IS_GVARIANT(sd_bus_message *m) {
165 return m->header->version == 2;
166 }
167
168 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
169 int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
170
171 int bus_message_from_header(
172 sd_bus *bus,
173 void *header,
174 size_t header_accessible,
175 void *footer,
176 size_t footer_accessible,
177 size_t message_size,
178 int *fds,
179 size_t n_fds,
180 const char *label,
181 size_t extra,
182 sd_bus_message **ret);
183
184 int bus_message_from_malloc(
185 sd_bus *bus,
186 void *buffer,
187 size_t length,
188 int *fds,
189 size_t n_fds,
190 const char *label,
191 sd_bus_message **ret);
192
193 int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str);
194 int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv);
195
196 int bus_message_parse_fields(sd_bus_message *m);
197
198 struct bus_body_part *message_append_part(sd_bus_message *m);
199
200 #define MESSAGE_FOREACH_PART(part, i, m) \
201 for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts; (i)++, (part) = (part)->next)
202
203 int bus_body_part_map(struct bus_body_part *part);
204 void bus_body_part_unmap(struct bus_body_part *part);
205
206 int bus_message_to_errno(sd_bus_message *m);
207
208 int bus_message_new_synthetic_error(sd_bus *bus, uint64_t serial, const sd_bus_error *e, sd_bus_message **m);
209
210 int bus_message_remarshal(sd_bus *bus, sd_bus_message **m);
211
212 void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m);
213 void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);