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