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