chiark / gitweb /
bus: let's use GREEDY_REALLOC() when allocating space for containers
[elogind.git] / src / libsystemd-bus / bus-message.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2013 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <stdbool.h>
25 #include <byteswap.h>
26 #include <sys/socket.h>
27
28 #include "macro.h"
29 #include "sd-bus.h"
30 #include "kdbus.h"
31 #include "time-util.h"
32 #include "bus-creds.h"
33 #include "bus-protocol.h"
34
35 struct bus_container {
36         char enclosing;
37         bool need_offsets:1;
38
39         /* Indexes into the signature  string */
40         unsigned index, saved_index;
41         char *signature;
42
43         size_t before, begin, end;
44
45         /* dbus1: pointer to the array size value, if this is a value */
46         uint32_t *array_size;
47
48         /* gvariant: list of offsets to end of children if this is struct/dict entry/array */
49         size_t *offsets, n_offsets, offsets_allocated, offset_index;
50         size_t item_size;
51 };
52
53 struct bus_header {
54         uint8_t endian;
55         uint8_t type;
56         uint8_t flags;
57         uint8_t version;
58         uint32_t body_size;
59         uint32_t serial;
60         uint32_t fields_size;
61 } _packed_;
62
63 struct bus_body_part {
64         struct bus_body_part *next;
65         void *data;
66         size_t size;
67         size_t mapped;
68         int memfd;
69         bool free_this:1;
70         bool munmap_this:1;
71         bool sealed:1;
72         bool is_zero:1;
73 };
74
75 struct sd_bus_message {
76         unsigned n_ref;
77
78         sd_bus *bus;
79
80         uint32_t reply_serial;
81
82         const char *path;
83         const char *interface;
84         const char *member;
85         const char *destination;
86         const char *sender;
87
88         sd_bus_error error;
89
90         sd_bus_creds creds;
91
92         usec_t monotonic;
93         usec_t realtime;
94
95         bool sealed:1;
96         bool dont_send:1;
97         bool allow_fds:1;
98         bool free_header:1;
99         bool free_kdbus:1;
100         bool free_fds:1;
101         bool release_kdbus:1;
102         bool poisoned:1;
103
104         struct bus_header *header;
105         struct bus_body_part body;
106         struct bus_body_part *body_end;
107         unsigned n_body_parts;
108
109         size_t rindex;
110         struct bus_body_part *cached_rindex_part;
111         size_t cached_rindex_part_begin;
112
113         uint32_t n_fds;
114         int *fds;
115
116         struct bus_container root_container, *containers;
117         unsigned n_containers;
118         size_t containers_allocated;
119
120         struct iovec *iovec;
121         struct iovec iovec_fixed[2];
122         unsigned n_iovec;
123
124         struct kdbus_msg *kdbus;
125
126         char *peeked_signature;
127
128         /* If set replies to this message must carry the signature
129          * specified here to successfully seal. This is initialized
130          * from the vtable data */
131         const char *enforced_reply_signature;
132
133         usec_t timeout;
134
135         char sender_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
136         char destination_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
137
138         size_t header_offsets[_BUS_MESSAGE_HEADER_MAX];
139         unsigned n_header_offsets;
140 };
141
142 #define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != BUS_NATIVE_ENDIAN)
143
144 static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
145         return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
146 }
147
148 static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
149         return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
150 }
151
152 static inline uint64_t BUS_MESSAGE_BSWAP64(sd_bus_message *m, uint64_t u) {
153         return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_64(u) : u;
154 }
155
156 static inline uint32_t BUS_MESSAGE_SERIAL(sd_bus_message *m) {
157         return BUS_MESSAGE_BSWAP32(m, m->header->serial);
158 }
159
160 static inline uint32_t BUS_MESSAGE_BODY_SIZE(sd_bus_message *m) {
161         return BUS_MESSAGE_BSWAP32(m, m->header->body_size);
162 }
163
164 static inline uint32_t BUS_MESSAGE_FIELDS_SIZE(sd_bus_message *m) {
165         return BUS_MESSAGE_BSWAP32(m, m->header->fields_size);
166 }
167
168 static inline uint32_t BUS_MESSAGE_SIZE(sd_bus_message *m) {
169         return
170                 sizeof(struct bus_header) +
171                 ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)) +
172                 BUS_MESSAGE_BODY_SIZE(m);
173 }
174
175 static inline uint32_t BUS_MESSAGE_BODY_BEGIN(sd_bus_message *m) {
176         return
177                 sizeof(struct bus_header) +
178                 ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
179 }
180
181 static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
182         return (uint8_t*) m->header + sizeof(struct bus_header);
183 }
184
185 static inline bool BUS_MESSAGE_IS_GVARIANT(sd_bus_message *m) {
186         return m->header->version == 2;
187 }
188
189 int bus_message_seal(sd_bus_message *m, uint64_t serial, usec_t timeout);
190 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
191 int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
192
193 int bus_message_from_header(
194                 sd_bus *bus,
195                 void *header,
196                 size_t length,
197                 int *fds,
198                 unsigned n_fds,
199                 const struct ucred *ucred,
200                 const char *label,
201                 size_t extra,
202                 sd_bus_message **ret);
203
204 int bus_message_from_malloc(
205                 sd_bus *bus,
206                 void *buffer,
207                 size_t length,
208                 int *fds,
209                 unsigned n_fds,
210                 const struct ucred *ucred,
211                 const char *label,
212                 sd_bus_message **ret);
213
214 const char* bus_message_get_arg(sd_bus_message *m, unsigned i);
215
216 int bus_message_append_ap(sd_bus_message *m, const char *types, va_list ap);
217
218 int bus_message_parse_fields(sd_bus_message *m);
219
220 bool bus_header_is_complete(struct bus_header *h, size_t size);
221 int bus_header_message_size(struct bus_header *h, size_t *sum);
222
223 struct bus_body_part *message_append_part(sd_bus_message *m);
224
225 #define MESSAGE_FOREACH_PART(part, i, m) \
226         for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts; (i)++, (part) = (part)->next)
227
228 int bus_body_part_map(struct bus_body_part *part);
229 void bus_body_part_unmap(struct bus_body_part *part);
230
231 int bus_message_to_errno(sd_bus_message *m);
232
233 int bus_message_new_synthetic_error(sd_bus *bus, uint64_t serial, const sd_bus_error *e, sd_bus_message **m);