]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/bus-message.h
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-message.h
CommitLineData
de1c301e
LP
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
de1c301e 24#include <byteswap.h>
07630cea 25#include <stdbool.h>
2c35d880 26#include <sys/socket.h>
de1c301e 27
de1c301e 28#include "sd-bus.h"
07630cea 29
5b12334d 30#include "bus-creds.h"
0461f8cd 31#include "bus-protocol.h"
07630cea
LP
32#include "macro.h"
33#include "time-util.h"
de1c301e
LP
34
35struct bus_container {
36 char enclosing;
6647dc66 37 bool need_offsets:1;
de1c301e 38
6647dc66 39 /* Indexes into the signature string */
b3af9646 40 unsigned index, saved_index;
453a0c29
LP
41 char *signature;
42
6647dc66 43 size_t before, begin, end;
c1b9d935 44
6647dc66
LP
45 /* dbus1: pointer to the array size value, if this is a value */
46 uint32_t *array_size;
c1b9d935 47
6647dc66 48 /* gvariant: list of offsets to end of children if this is struct/dict entry/array */
306f07be 49 size_t *offsets, n_offsets, offsets_allocated, offset_index;
6647dc66 50 size_t item_size;
3798fd4c
LP
51
52 char *peeked_signature;
de1c301e
LP
53};
54
bc7fd8cd 55struct bus_body_part {
453a0c29 56 struct bus_body_part *next;
bc7fd8cd 57 void *data;
7dcd79c2 58 void *mmap_begin;
bc7fd8cd
LP
59 size_t size;
60 size_t mapped;
8e959fbf 61 size_t allocated;
7dcd79c2 62 uint64_t memfd_offset;
bc7fd8cd
LP
63 int memfd;
64 bool free_this:1;
453a0c29 65 bool munmap_this:1;
bc7fd8cd 66 bool sealed:1;
453a0c29 67 bool is_zero:1;
bc7fd8cd
LP
68};
69
de1c301e
LP
70struct sd_bus_message {
71 unsigned n_ref;
72
fd8d62d9
LP
73 sd_bus *bus;
74
42c4ebcb 75 uint64_t reply_cookie;
de1c301e
LP
76
77 const char *path;
78 const char *interface;
79 const char *member;
80 const char *destination;
81 const char *sender;
de1c301e
LP
82
83 sd_bus_error error;
84
5b12334d
LP
85 sd_bus_creds creds;
86
69aec65c
LP
87 usec_t monotonic;
88 usec_t realtime;
6a0e376c 89 uint64_t seqnum;
ca7b42c8 90 int64_t priority;
022fb855 91 uint64_t verify_destination_id;
de1c301e
LP
92
93 bool sealed:1;
2c93b4ef
LP
94 bool dont_send:1;
95 bool allow_fds:1;
de1c301e 96 bool free_header:1;
6629161f 97 bool free_kdbus:1;
2c93b4ef 98 bool free_fds:1;
fd8d62d9 99 bool release_kdbus:1;
bc7fd8cd 100 bool poisoned:1;
de1c301e 101
2ac7c17f 102 /* The first and last bytes of the message */
de1c301e 103 struct bus_header *header;
2ac7c17f
LP
104 void *footer;
105
106 /* How many bytes are accessible in the above pointers */
107 size_t header_accessible;
108 size_t footer_accessible;
109
110 size_t fields_size;
111 size_t body_size;
112 size_t user_body_size;
113
bc7fd8cd
LP
114 struct bus_body_part body;
115 struct bus_body_part *body_end;
116 unsigned n_body_parts;
de1c301e 117
9a17484d 118 size_t rindex;
bc7fd8cd
LP
119 struct bus_body_part *cached_rindex_part;
120 size_t cached_rindex_part_begin;
9a17484d 121
de1c301e
LP
122 uint32_t n_fds;
123 int *fds;
124
9a17484d 125 struct bus_container root_container, *containers;
2ac7c17f 126 size_t n_containers;
306f07be 127 size_t containers_allocated;
de1c301e 128
bc7fd8cd 129 struct iovec *iovec;
c91cb83c 130 struct iovec iovec_fixed[2];
de1c301e 131 unsigned n_iovec;
9a17484d 132
bc7fd8cd
LP
133 struct kdbus_msg *kdbus;
134
9a17484d 135 char *peeked_signature;
6629161f 136
6717d473
LP
137 /* If set replies to this message must carry the signature
138 * specified here to successfully seal. This is initialized
139 * from the vtable data */
140 const char *enforced_reply_signature;
141
6629161f 142 usec_t timeout;
51038c03
LP
143
144 char sender_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
145 char destination_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
022fb855 146 char *destination_ptr;
c1b9d935
LP
147
148 size_t header_offsets[_BUS_MESSAGE_HEADER_MAX];
149 unsigned n_header_offsets;
de1c301e
LP
150};
151
2ac7c17f
LP
152static inline bool BUS_MESSAGE_NEED_BSWAP(sd_bus_message *m) {
153 return m->header->endian != BUS_NATIVE_ENDIAN;
154}
de1c301e 155
9a17484d
LP
156static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
157 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
158}
159
160static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
de1c301e
LP
161 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
162}
163
9a17484d
LP
164static inline uint64_t BUS_MESSAGE_BSWAP64(sd_bus_message *m, uint64_t u) {
165 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_64(u) : u;
166}
167
42c4ebcb 168static inline uint64_t BUS_MESSAGE_COOKIE(sd_bus_message *m) {
2ac7c17f
LP
169 if (m->header->version == 2)
170 return BUS_MESSAGE_BSWAP64(m, m->header->dbus2.cookie);
de1c301e 171
2ac7c17f 172 return BUS_MESSAGE_BSWAP32(m, m->header->dbus1.serial);
de1c301e
LP
173}
174
2ac7c17f 175static inline size_t BUS_MESSAGE_SIZE(sd_bus_message *m) {
6629161f
LP
176 return
177 sizeof(struct bus_header) +
2ac7c17f
LP
178 ALIGN8(m->fields_size) +
179 m->body_size;
6629161f
LP
180}
181
2ac7c17f 182static inline size_t BUS_MESSAGE_BODY_BEGIN(sd_bus_message *m) {
c91cb83c
LP
183 return
184 sizeof(struct bus_header) +
2ac7c17f 185 ALIGN8(m->fields_size);
c91cb83c
LP
186}
187
188static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
189 return (uint8_t*) m->header + sizeof(struct bus_header);
190}
191
6647dc66
LP
192static inline bool BUS_MESSAGE_IS_GVARIANT(sd_bus_message *m) {
193 return m->header->version == 2;
194}
195
3df7a7e6 196int bus_message_seal(sd_bus_message *m, uint64_t serial, usec_t timeout);
de1c301e 197int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
89ffcd2a 198int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
2c93b4ef 199
6629161f 200int bus_message_from_header(
df2d202e 201 sd_bus *bus,
6629161f 202 void *header,
2ac7c17f
LP
203 size_t header_accessible,
204 void *footer,
205 size_t footer_accessible,
206 size_t message_size,
6629161f
LP
207 int *fds,
208 unsigned n_fds,
6629161f
LP
209 const char *label,
210 size_t extra,
211 sd_bus_message **ret);
212
2c93b4ef 213int bus_message_from_malloc(
df2d202e 214 sd_bus *bus,
2c93b4ef
LP
215 void *buffer,
216 size_t length,
217 int *fds,
218 unsigned n_fds,
2c93b4ef
LP
219 const char *label,
220 sd_bus_message **ret);
392d5b37 221
eccd47c5
LP
222int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str);
223int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv);
917b5dc7
LP
224
225int bus_message_append_ap(sd_bus_message *m, const char *types, va_list ap);
2100fa10 226
6629161f
LP
227int bus_message_parse_fields(sd_bus_message *m);
228
bc7fd8cd 229struct bus_body_part *message_append_part(sd_bus_message *m);
9b29bb68
LP
230
231#define MESSAGE_FOREACH_PART(part, i, m) \
232 for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts; (i)++, (part) = (part)->next)
a392d361
LP
233
234int bus_body_part_map(struct bus_body_part *part);
235void bus_body_part_unmap(struct bus_body_part *part);
eb01ba5d
LP
236
237int bus_message_to_errno(sd_bus_message *m);
238
239int bus_message_new_synthetic_error(sd_bus *bus, uint64_t serial, const sd_bus_error *e, sd_bus_message **m);
e1c433c6
LP
240
241int bus_message_remarshal(sd_bus *bus, sd_bus_message **m);
a7639e37
LP
242
243int bus_message_append_sender(sd_bus_message *m, const char *sender);
d29ae291
LP
244
245void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m);
246void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);