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