]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-message.h
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / libsystemd / 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 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
173 int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
174
175 int bus_message_from_header(
176 sd_bus *bus,
177 void *header,
178 size_t header_accessible,
179 void *footer,
180 size_t footer_accessible,
181 size_t message_size,
182 int *fds,
183 size_t n_fds,
184 const char *label,
185 size_t extra,
186 sd_bus_message **ret);
187
188 int bus_message_from_malloc(
189 sd_bus *bus,
190 void *buffer,
191 size_t length,
192 int *fds,
193 size_t n_fds,
194 const char *label,
195 sd_bus_message **ret);
196
197 int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str);
198 int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv);
199
200 int bus_message_parse_fields(sd_bus_message *m);
201
202 struct bus_body_part *message_append_part(sd_bus_message *m);
203
204 #define MESSAGE_FOREACH_PART(part, i, m) \
205 for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts; (i)++, (part) = (part)->next)
206
207 int bus_body_part_map(struct bus_body_part *part);
208 void bus_body_part_unmap(struct bus_body_part *part);
209
210 int bus_message_to_errno(sd_bus_message *m);
211
212 int bus_message_new_synthetic_error(sd_bus *bus, uint64_t serial, const sd_bus_error *e, sd_bus_message **m);
213
214 int bus_message_remarshal(sd_bus *bus, sd_bus_message **m);
215
216 int bus_message_append_sender(sd_bus_message *m, const char *sender);
217
218 void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m);
219 void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);