Debian Codegraph @

src/libsystemd/sd-bus/bus-message.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <byteswap.h>
5 #include <sys/uio.h>
6
7 #include "sd-bus-protocol.h"
8
9 #include "bus-creds.h"
10 #include "bus-forward.h"
11 #include "bus-protocol.h"
12 #include "memory-util.h"
13
14 typedef struct BusMessageHeader {
15 uint8_t endian;
16 uint8_t type;
17 uint8_t flags;
18 uint8_t version;
19 uint32_t body_size;
20 /* Note that what the bus spec calls "serial" we'll call "cookie" instead, because we don't
21 * want to imply that the cookie was in any way monotonically increasing. */
22 uint32_t serial;
23 uint32_t fields_size;
24 } _packed_ BusMessageHeader;
25
26 typedef struct BusMessageContainer {
27 char enclosing;
28
29 /* Indexes into the signature string */
30 unsigned index, saved_index;
31 char *signature;
32
33 size_t before, begin, end;
34
35 /* pointer to the array size value, if this is a value */
36 uint32_t *array_size;
37
38 char *peeked_signature;
39 } BusMessageContainer;
40
41 typedef struct BusMessageBodyPart {
42 BusMessageBodyPart *next;
43 void *data;
44 void *mmap_begin;
45 size_t size;
46 size_t mapped;
47 size_t allocated;
48 uint64_t memfd_offset;
49 int memfd;
50 bool free_this:1;
51 bool munmap_this:1;
52 bool sealed:1;
53 bool is_zero:1;
54 } BusMessageBodyPart;
55
56 typedef struct sd_bus_message {
57 /* Caveat: a message can be referenced in two different ways: the main (user-facing) way will also
58 * pin the bus connection object the message is associated with. The secondary way ("queued") is used
59 * when a message is in the read or write queues of the bus connection object, which will not pin the
60 * bus connection object. This is necessary so that we don't have to have a pair of cyclic references
61 * between a message that is queued and its connection: as soon as a message is only referenced by
62 * the connection (by means of being queued) and the connection itself has no other references it
63 * will be freed. */
64
65 unsigned n_ref; /* Counter of references that pin the connection */
66 unsigned n_queued; /* Counter of references that do not pin the connection */
67
68 sd_bus *bus;
69
70 uint64_t reply_cookie;
71
72 const char *path;
73 const char *interface;
74 const char *member;
75 const char *destination;
76 const char *sender;
77
78 sd_bus_error error;
79
80 sd_bus_creds creds;
81
82 usec_t monotonic;
83 usec_t realtime;
84 uint64_t seqnum;
85 uint64_t verify_destination_id;
86
87 bool sealed:1;
88 bool dont_send:1;
89 bool allow_fds:1;
90 bool free_header:1;
91 bool free_fds:1;
92 bool poisoned:1;
93 bool sensitive:1;
94
95 /* The first bytes of the message */
96 BusMessageHeader *header;
97
98 size_t fields_size;
99 size_t body_size;
100 size_t user_body_size;
101
102 BusMessageBodyPart body;
103 BusMessageBodyPart *body_end;
104 unsigned n_body_parts;
105
106 size_t rindex;
107 BusMessageBodyPart *cached_rindex_part;
108 size_t cached_rindex_part_begin;
109
110 uint32_t n_fds;
111 int *fds;
112
113 BusMessageContainer root_container, *containers;
114 size_t n_containers;
115
116 struct iovec *iovec;
117 struct iovec iovec_fixed[2];
118 unsigned n_iovec;
119
120 char *peeked_signature;
121
122 /* If set replies to this message must carry the signature
123 * specified here to successfully seal. This is initialized
124 * from the vtable data */
125 const char *enforced_reply_signature;
126
127 usec_t timeout;
128
129 size_t header_offsets[_BUS_MESSAGE_HEADER_MAX];
130 unsigned n_header_offsets;
131
132 uint64_t read_counter;
133 } sd_bus_message;
134
135 static inline bool BUS_MESSAGE_NEED_BSWAP(sd_bus_message *m) {
136 return m->header->endian != BUS_NATIVE_ENDIAN;
137 }
138
139 static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
140 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
141 }
142
143 static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
144 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
145 }
146
147 static inline uint64_t BUS_MESSAGE_BSWAP64(sd_bus_message *m, uint64_t u) {
148 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_64(u) : u;
149 }
150
151 static inline uint64_t BUS_MESSAGE_COOKIE(sd_bus_message *m) {
152 return BUS_MESSAGE_BSWAP32(m, m->header->serial);
153 }
154
155 static inline size_t BUS_MESSAGE_SIZE(sd_bus_message *m) {
156 /* Silence static analyzers, fields_size is validated at message creation */
157 assert(ALIGN8(m->fields_size) != SIZE_MAX);
158 assert(ALIGN8(m->fields_size) <= SIZE_MAX - sizeof(BusMessageHeader));
159 assert(m->body_size <= SIZE_MAX - sizeof(BusMessageHeader) - ALIGN8(m->fields_size));
160 return
161 sizeof(BusMessageHeader) +
162 ALIGN8(m->fields_size) +
163 m->body_size;
164 }
165
166 static inline size_t BUS_MESSAGE_BODY_BEGIN(sd_bus_message *m) {
167 /* Silence static analyzers, fields_size is validated at message creation */
168 assert(ALIGN8(m->fields_size) != SIZE_MAX);
169 assert(ALIGN8(m->fields_size) <= SIZE_MAX - sizeof(BusMessageHeader));
170 return
171 sizeof(BusMessageHeader) +
172 ALIGN8(m->fields_size);
173 }
174
175 static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
176 return (uint8_t*) m->header + sizeof(BusMessageHeader);
177 }
178
179 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
180
181 int bus_message_from_malloc(
182 sd_bus *bus,
183 void *buffer,
184 size_t length,
185 int *fds,
186 size_t n_fds,
187 bool got_ctrunc,
188 const char *label,
189 sd_bus_message **ret);
190
191 int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str);
192 int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv);
193
194 #define MESSAGE_FOREACH_PART(part, i, m) \
195 for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts; (i)++, (part) = (part)->next)
196
197 int bus_body_part_map(BusMessageBodyPart *part);
198 void bus_body_part_unmap(BusMessageBodyPart *part);
199
200 int bus_message_new_synthetic_error(sd_bus *bus, uint64_t cookie, const sd_bus_error *e, sd_bus_message **m);
201
202 int bus_message_remarshal(sd_bus *bus, sd_bus_message **m);
203
204 void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m);
205 void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);
206
207 sd_bus_message* bus_message_ref_queued(sd_bus_message *m, sd_bus *bus);
208 sd_bus_message* bus_message_unref_queued(sd_bus_message *m, sd_bus *bus);
209
210 char** bus_message_make_log_fields(sd_bus_message *m);