]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/bus-message.h
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-message.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
de1c301e
LP
2#pragma once
3
4/***
5 This file is part of systemd.
6
7 Copyright 2013 Lennart Poettering
de1c301e
LP
8***/
9
de1c301e 10#include <byteswap.h>
07630cea 11#include <stdbool.h>
2c35d880 12#include <sys/socket.h>
de1c301e 13
de1c301e 14#include "sd-bus.h"
07630cea 15
5b12334d 16#include "bus-creds.h"
0461f8cd 17#include "bus-protocol.h"
07630cea
LP
18#include "macro.h"
19#include "time-util.h"
de1c301e
LP
20
21struct bus_container {
22 char enclosing;
6647dc66 23 bool need_offsets:1;
de1c301e 24
6647dc66 25 /* Indexes into the signature string */
b3af9646 26 unsigned index, saved_index;
453a0c29
LP
27 char *signature;
28
6647dc66 29 size_t before, begin, end;
c1b9d935 30
6647dc66
LP
31 /* dbus1: pointer to the array size value, if this is a value */
32 uint32_t *array_size;
c1b9d935 33
6647dc66 34 /* gvariant: list of offsets to end of children if this is struct/dict entry/array */
306f07be 35 size_t *offsets, n_offsets, offsets_allocated, offset_index;
6647dc66 36 size_t item_size;
3798fd4c
LP
37
38 char *peeked_signature;
de1c301e
LP
39};
40
bc7fd8cd 41struct bus_body_part {
453a0c29 42 struct bus_body_part *next;
bc7fd8cd 43 void *data;
7dcd79c2 44 void *mmap_begin;
bc7fd8cd
LP
45 size_t size;
46 size_t mapped;
8e959fbf 47 size_t allocated;
7dcd79c2 48 uint64_t memfd_offset;
bc7fd8cd
LP
49 int memfd;
50 bool free_this:1;
453a0c29 51 bool munmap_this:1;
bc7fd8cd 52 bool sealed:1;
453a0c29 53 bool is_zero:1;
bc7fd8cd
LP
54};
55
de1c301e
LP
56struct sd_bus_message {
57 unsigned n_ref;
58
fd8d62d9
LP
59 sd_bus *bus;
60
42c4ebcb 61 uint64_t reply_cookie;
de1c301e
LP
62
63 const char *path;
64 const char *interface;
65 const char *member;
66 const char *destination;
67 const char *sender;
de1c301e
LP
68
69 sd_bus_error error;
70
5b12334d
LP
71 sd_bus_creds creds;
72
69aec65c
LP
73 usec_t monotonic;
74 usec_t realtime;
6a0e376c 75 uint64_t seqnum;
ca7b42c8 76 int64_t priority;
022fb855 77 uint64_t verify_destination_id;
de1c301e
LP
78
79 bool sealed:1;
2c93b4ef
LP
80 bool dont_send:1;
81 bool allow_fds:1;
de1c301e 82 bool free_header:1;
2c93b4ef 83 bool free_fds:1;
bc7fd8cd 84 bool poisoned:1;
de1c301e 85
2ac7c17f 86 /* The first and last bytes of the message */
de1c301e 87 struct bus_header *header;
2ac7c17f
LP
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
bc7fd8cd
LP
98 struct bus_body_part body;
99 struct bus_body_part *body_end;
100 unsigned n_body_parts;
de1c301e 101
9a17484d 102 size_t rindex;
bc7fd8cd
LP
103 struct bus_body_part *cached_rindex_part;
104 size_t cached_rindex_part_begin;
9a17484d 105
de1c301e
LP
106 uint32_t n_fds;
107 int *fds;
108
9a17484d 109 struct bus_container root_container, *containers;
2ac7c17f 110 size_t n_containers;
306f07be 111 size_t containers_allocated;
de1c301e 112
bc7fd8cd 113 struct iovec *iovec;
c91cb83c 114 struct iovec iovec_fixed[2];
de1c301e 115 unsigned n_iovec;
9a17484d
LP
116
117 char *peeked_signature;
6629161f 118
6717d473
LP
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
6629161f 124 usec_t timeout;
51038c03 125
c1b9d935
LP
126 size_t header_offsets[_BUS_MESSAGE_HEADER_MAX];
127 unsigned n_header_offsets;
de1c301e
LP
128};
129
2ac7c17f
LP
130static inline bool BUS_MESSAGE_NEED_BSWAP(sd_bus_message *m) {
131 return m->header->endian != BUS_NATIVE_ENDIAN;
132}
de1c301e 133
9a17484d
LP
134static 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
138static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
de1c301e
LP
139 return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
140}
141
9a17484d
LP
142static 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
42c4ebcb 146static inline uint64_t BUS_MESSAGE_COOKIE(sd_bus_message *m) {
2ac7c17f
LP
147 if (m->header->version == 2)
148 return BUS_MESSAGE_BSWAP64(m, m->header->dbus2.cookie);
de1c301e 149
2ac7c17f 150 return BUS_MESSAGE_BSWAP32(m, m->header->dbus1.serial);
de1c301e
LP
151}
152
2ac7c17f 153static inline size_t BUS_MESSAGE_SIZE(sd_bus_message *m) {
6629161f
LP
154 return
155 sizeof(struct bus_header) +
2ac7c17f
LP
156 ALIGN8(m->fields_size) +
157 m->body_size;
6629161f
LP
158}
159
2ac7c17f 160static inline size_t BUS_MESSAGE_BODY_BEGIN(sd_bus_message *m) {
c91cb83c
LP
161 return
162 sizeof(struct bus_header) +
2ac7c17f 163 ALIGN8(m->fields_size);
c91cb83c
LP
164}
165
166static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
167 return (uint8_t*) m->header + sizeof(struct bus_header);
168}
169
6647dc66
LP
170static inline bool BUS_MESSAGE_IS_GVARIANT(sd_bus_message *m) {
171 return m->header->version == 2;
172}
173
de1c301e 174int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
89ffcd2a 175int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
2c93b4ef 176
6629161f 177int bus_message_from_header(
df2d202e 178 sd_bus *bus,
6629161f 179 void *header,
2ac7c17f
LP
180 size_t header_accessible,
181 void *footer,
182 size_t footer_accessible,
183 size_t message_size,
6629161f
LP
184 int *fds,
185 unsigned n_fds,
6629161f
LP
186 const char *label,
187 size_t extra,
188 sd_bus_message **ret);
189
2c93b4ef 190int bus_message_from_malloc(
df2d202e 191 sd_bus *bus,
2c93b4ef
LP
192 void *buffer,
193 size_t length,
194 int *fds,
195 unsigned n_fds,
2c93b4ef
LP
196 const char *label,
197 sd_bus_message **ret);
392d5b37 198
eccd47c5
LP
199int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str);
200int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv);
917b5dc7 201
6629161f
LP
202int bus_message_parse_fields(sd_bus_message *m);
203
bc7fd8cd 204struct bus_body_part *message_append_part(sd_bus_message *m);
9b29bb68
LP
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)
a392d361
LP
208
209int bus_body_part_map(struct bus_body_part *part);
210void bus_body_part_unmap(struct bus_body_part *part);
eb01ba5d
LP
211
212int bus_message_to_errno(sd_bus_message *m);
213
214int bus_message_new_synthetic_error(sd_bus *bus, uint64_t serial, const sd_bus_error *e, sd_bus_message **m);
e1c433c6
LP
215
216int bus_message_remarshal(sd_bus *bus, sd_bus_message **m);
a7639e37
LP
217
218int bus_message_append_sender(sd_bus_message *m, const char *sender);
d29ae291
LP
219
220void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m);
221void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);