chiark / gitweb /
bus: don't make use of the private bus socket if kdbus is available
[elogind.git] / src / libsystemd-bus / bus-message.h
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
24 #include <stdbool.h>
25 #include <byteswap.h>
26 #include <sys/socket.h>
27
28 #include "macro.h"
29 #include "sd-bus.h"
30 #include "kdbus.h"
31 #include "time-util.h"
32 #include "bus-creds.h"
33 #include "bus-protocol.h"
34
35 struct bus_container {
36         char enclosing;
37
38         unsigned index, saved_index;
39
40         char *signature;
41
42         uint32_t *array_size;
43         size_t before, begin;
44 };
45
46 struct bus_header {
47         uint8_t endian;
48         uint8_t type;
49         uint8_t flags;
50         uint8_t version;
51         uint32_t body_size;
52         uint32_t serial;
53         uint32_t fields_size;
54 } _packed_;
55
56 struct bus_body_part {
57         struct bus_body_part *next;
58         void *data;
59         size_t size;
60         size_t mapped;
61         int memfd;
62         bool free_this:1;
63         bool munmap_this:1;
64         bool sealed:1;
65         bool is_zero:1;
66 };
67
68 struct sd_bus_message {
69         unsigned n_ref;
70
71         sd_bus *bus;
72
73         uint32_t reply_serial;
74
75         const char *path;
76         const char *interface;
77         const char *member;
78         const char *destination;
79         const char *sender;
80
81         sd_bus_error error;
82
83         sd_bus_creds creds;
84
85         usec_t monotonic;
86         usec_t realtime;
87
88         bool sealed:1;
89         bool dont_send:1;
90         bool allow_fds:1;
91         bool free_header:1;
92         bool free_kdbus:1;
93         bool free_fds:1;
94         bool release_kdbus:1;
95         bool poisoned:1;
96
97         struct bus_header *header;
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         unsigned n_containers;
111
112         struct iovec *iovec;
113         struct iovec iovec_fixed[2];
114         unsigned n_iovec;
115
116         struct kdbus_msg *kdbus;
117
118         char *peeked_signature;
119
120         /* If set replies to this message must carry the signature
121          * specified here to successfully seal. This is initialized
122          * from the vtable data */
123         const char *enforced_reply_signature;
124
125         usec_t timeout;
126
127         char sender_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
128         char destination_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
129 };
130
131 #define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != BUS_NATIVE_ENDIAN)
132
133 static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
134         return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
135 }
136
137 static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
138         return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
139 }
140
141 static inline uint64_t BUS_MESSAGE_BSWAP64(sd_bus_message *m, uint64_t u) {
142         return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_64(u) : u;
143 }
144
145 static inline uint32_t BUS_MESSAGE_SERIAL(sd_bus_message *m) {
146         return BUS_MESSAGE_BSWAP32(m, m->header->serial);
147 }
148
149 static inline uint32_t BUS_MESSAGE_BODY_SIZE(sd_bus_message *m) {
150         return BUS_MESSAGE_BSWAP32(m, m->header->body_size);
151 }
152
153 static inline uint32_t BUS_MESSAGE_FIELDS_SIZE(sd_bus_message *m) {
154         return BUS_MESSAGE_BSWAP32(m, m->header->fields_size);
155 }
156
157 static inline uint32_t BUS_MESSAGE_SIZE(sd_bus_message *m) {
158         return
159                 sizeof(struct bus_header) +
160                 ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)) +
161                 BUS_MESSAGE_BODY_SIZE(m);
162 }
163
164 static inline uint32_t BUS_MESSAGE_BODY_BEGIN(sd_bus_message *m) {
165         return
166                 sizeof(struct bus_header) +
167                 ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
168 }
169
170 static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
171         return (uint8_t*) m->header + sizeof(struct bus_header);
172 }
173
174 int bus_message_seal(sd_bus_message *m, uint64_t serial);
175 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
176 int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
177
178 int bus_message_from_header(
179                 sd_bus *bus,
180                 void *header,
181                 size_t length,
182                 int *fds,
183                 unsigned n_fds,
184                 const struct ucred *ucred,
185                 const char *label,
186                 size_t extra,
187                 sd_bus_message **ret);
188
189 int bus_message_from_malloc(
190                 sd_bus *bus,
191                 void *buffer,
192                 size_t length,
193                 int *fds,
194                 unsigned n_fds,
195                 const struct ucred *ucred,
196                 const char *label,
197                 sd_bus_message **ret);
198
199 const char* bus_message_get_arg(sd_bus_message *m, unsigned i);
200
201 int bus_message_append_ap(sd_bus_message *m, const char *types, va_list ap);
202
203 int bus_message_parse_fields(sd_bus_message *m);
204
205 bool bus_header_is_complete(struct bus_header *h, size_t size);
206 int bus_header_message_size(struct bus_header *h, size_t *sum);
207
208 struct bus_body_part *message_append_part(sd_bus_message *m);
209
210 #define MESSAGE_FOREACH_PART(part, i, m) \
211         for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts; (i)++, (part) = (part)->next)
212
213 int bus_body_part_map(struct bus_body_part *part);
214 void bus_body_part_unmap(struct bus_body_part *part);
215
216 int bus_message_to_errno(sd_bus_message *m);
217
218 int bus_message_new_synthetic_error(sd_bus *bus, uint64_t serial, const sd_bus_error *e, sd_bus_message **m);