]>
Commit | Line | Data |
---|---|---|
de1c301e LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
2c93b4ef | 23 | #include <fcntl.h> |
bc7fd8cd | 24 | #include <sys/mman.h> |
de1c301e LP |
25 | |
26 | #include "util.h" | |
9a17484d | 27 | #include "utf8.h" |
89ffcd2a | 28 | #include "strv.h" |
6629161f | 29 | #include "time-util.h" |
d8d3d8a7 | 30 | #include "cgroup-util.h" |
de1c301e | 31 | |
de1c301e | 32 | #include "sd-bus.h" |
9a17484d | 33 | #include "bus-message.h" |
de1c301e LP |
34 | #include "bus-internal.h" |
35 | #include "bus-type.h" | |
36 | #include "bus-signature.h" | |
37 | ||
80a46c73 | 38 | static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored); |
de1c301e | 39 | |
bc7fd8cd LP |
40 | static void *adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) { |
41 | ||
42 | if (p == NULL) | |
43 | return NULL; | |
44 | ||
45 | if (old_base == new_base) | |
46 | return (void*) p; | |
47 | ||
48 | if ((uint8_t*) p < (uint8_t*) old_base) | |
49 | return (void*) p; | |
50 | ||
51 | if ((uint8_t*) p >= (uint8_t*) old_base + sz) | |
52 | return (void*) p; | |
53 | ||
54 | return (uint8_t*) new_base + ((uint8_t*) p - (uint8_t*) old_base); | |
55 | } | |
56 | ||
57 | static void message_free_part(sd_bus_message *m, struct bus_body_part *part) { | |
58 | assert(m); | |
59 | assert(part); | |
60 | ||
61 | if (part->memfd >= 0) { | |
66b26c5c LP |
62 | /* If we can reuse the memfd, try that. For that it |
63 | * can't be sealed yet. */ | |
bc7fd8cd LP |
64 | |
65 | if (!part->sealed) | |
66 | bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped); | |
67 | else { | |
89b4fc46 LP |
68 | if (part->mapped > 0) |
69 | assert_se(munmap(part->data, part->mapped) == 0); | |
bc7fd8cd LP |
70 | |
71 | close_nointr_nofail(part->memfd); | |
72 | } | |
73 | ||
453a0c29 LP |
74 | } else if (part->munmap_this) |
75 | munmap(part->data, part->mapped); | |
76 | else if (part->free_this) | |
bc7fd8cd LP |
77 | free(part->data); |
78 | ||
79 | if (part != &m->body) | |
80 | free(part); | |
81 | } | |
82 | ||
83 | static void message_reset_parts(sd_bus_message *m) { | |
84 | struct bus_body_part *part; | |
85 | ||
86 | assert(m); | |
87 | ||
88 | part = &m->body; | |
89 | while (m->n_body_parts > 0) { | |
90 | struct bus_body_part *next = part->next; | |
91 | message_free_part(m, part); | |
92 | part = next; | |
93 | m->n_body_parts--; | |
94 | } | |
95 | ||
96 | m->body_end = NULL; | |
97 | ||
98 | m->cached_rindex_part = NULL; | |
99 | m->cached_rindex_part_begin = 0; | |
100 | } | |
101 | ||
102 | static void message_reset_containers(sd_bus_message *m) { | |
9a17484d LP |
103 | unsigned i; |
104 | ||
105 | assert(m); | |
106 | ||
107 | for (i = 0; i < m->n_containers; i++) | |
108 | free(m->containers[i].signature); | |
109 | ||
110 | free(m->containers); | |
111 | m->containers = NULL; | |
112 | ||
113 | m->n_containers = 0; | |
114 | m->root_container.index = 0; | |
115 | } | |
116 | ||
de1c301e | 117 | static void message_free(sd_bus_message *m) { |
de1c301e LP |
118 | assert(m); |
119 | ||
120 | if (m->free_header) | |
121 | free(m->header); | |
122 | ||
bc7fd8cd | 123 | message_reset_parts(m); |
de1c301e | 124 | |
6629161f LP |
125 | if (m->free_kdbus) |
126 | free(m->kdbus); | |
127 | ||
62b3e928 KS |
128 | if (m->release_kdbus) { |
129 | uint64_t off; | |
130 | ||
131 | off = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer; | |
132 | ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, &off); | |
133 | } | |
fd8d62d9 | 134 | |
f54514f3 LP |
135 | if (m->bus) |
136 | sd_bus_unref(m->bus); | |
137 | ||
2c93b4ef LP |
138 | if (m->free_fds) { |
139 | close_many(m->fds, m->n_fds); | |
140 | free(m->fds); | |
141 | } | |
de1c301e | 142 | |
bc7fd8cd LP |
143 | if (m->iovec != m->iovec_fixed) |
144 | free(m->iovec); | |
145 | ||
77930f11 LP |
146 | free(m->cmdline_array); |
147 | ||
bc7fd8cd | 148 | message_reset_containers(m); |
9a17484d | 149 | free(m->root_container.signature); |
de1c301e | 150 | |
9a17484d | 151 | free(m->peeked_signature); |
d8d3d8a7 LP |
152 | |
153 | free(m->unit); | |
154 | free(m->user_unit); | |
155 | free(m->session); | |
de1c301e LP |
156 | free(m); |
157 | } | |
158 | ||
c91cb83c LP |
159 | static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) { |
160 | void *op, *np; | |
161 | size_t old_size, new_size, start; | |
de1c301e | 162 | |
c91cb83c | 163 | assert(m); |
de1c301e | 164 | |
c91cb83c | 165 | if (m->poisoned) |
de1c301e LP |
166 | return NULL; |
167 | ||
c91cb83c LP |
168 | old_size = sizeof(struct bus_header) + m->header->fields_size; |
169 | start = ALIGN_TO(old_size, align); | |
170 | new_size = start + sz; | |
de1c301e | 171 | |
c91cb83c LP |
172 | if (old_size == new_size) |
173 | return (uint8_t*) m->header + old_size; | |
de1c301e | 174 | |
c91cb83c LP |
175 | if (new_size > (size_t) ((uint32_t) -1)) |
176 | goto poison; | |
de1c301e | 177 | |
c91cb83c LP |
178 | if (m->free_header) { |
179 | np = realloc(m->header, ALIGN8(new_size)); | |
180 | if (!np) | |
181 | goto poison; | |
182 | } else { | |
183 | /* Initially, the header is allocated as part of of | |
184 | * the sd_bus_message itself, let's replace it by | |
185 | * dynamic data */ | |
de1c301e | 186 | |
c91cb83c LP |
187 | np = malloc(ALIGN8(new_size)); |
188 | if (!np) | |
189 | goto poison; | |
de1c301e | 190 | |
c91cb83c LP |
191 | memcpy(np, m->header, sizeof(struct bus_header)); |
192 | } | |
de1c301e | 193 | |
c91cb83c LP |
194 | /* Zero out padding */ |
195 | if (start > old_size) | |
196 | memset((uint8_t*) np + old_size, 0, start - old_size); | |
bc7fd8cd | 197 | |
c91cb83c LP |
198 | op = m->header; |
199 | m->header = np; | |
200 | m->header->fields_size = new_size - sizeof(struct bus_header); | |
de1c301e | 201 | |
bc7fd8cd | 202 | /* Adjust quick access pointers */ |
c91cb83c LP |
203 | m->path = adjust_pointer(m->path, op, old_size, m->header); |
204 | m->interface = adjust_pointer(m->interface, op, old_size, m->header); | |
205 | m->member = adjust_pointer(m->member, op, old_size, m->header); | |
206 | m->destination = adjust_pointer(m->destination, op, old_size, m->header); | |
207 | m->sender = adjust_pointer(m->sender, op, old_size, m->header); | |
208 | m->error.name = adjust_pointer(m->error.name, op, old_size, m->header); | |
bc7fd8cd | 209 | |
c91cb83c | 210 | m->free_header = true; |
de1c301e | 211 | |
c91cb83c LP |
212 | return (uint8_t*) np + start; |
213 | ||
214 | poison: | |
215 | m->poisoned = true; | |
216 | return NULL; | |
de1c301e LP |
217 | } |
218 | ||
219 | static int message_append_field_string( | |
220 | sd_bus_message *m, | |
221 | uint8_t h, | |
222 | char type, | |
223 | const char *s, | |
224 | const char **ret) { | |
225 | ||
226 | size_t l; | |
227 | uint8_t *p; | |
228 | ||
229 | assert(m); | |
230 | ||
231 | l = strlen(s); | |
232 | if (l > (size_t) (uint32_t) -1) | |
233 | return -EINVAL; | |
234 | ||
235 | /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */ | |
236 | p = message_extend_fields(m, 8, 4 + 4 + l + 1); | |
237 | if (!p) | |
238 | return -ENOMEM; | |
239 | ||
240 | p[0] = h; | |
241 | p[1] = 1; | |
242 | p[2] = type; | |
243 | p[3] = 0; | |
244 | ||
245 | ((uint32_t*) p)[1] = l; | |
246 | memcpy(p + 8, s, l + 1); | |
247 | ||
248 | if (ret) | |
bc7fd8cd | 249 | *ret = (char*) p + 8; |
de1c301e LP |
250 | |
251 | return 0; | |
252 | } | |
253 | ||
254 | static int message_append_field_signature( | |
255 | sd_bus_message *m, | |
256 | uint8_t h, | |
257 | const char *s, | |
258 | const char **ret) { | |
259 | ||
260 | size_t l; | |
261 | uint8_t *p; | |
262 | ||
263 | assert(m); | |
264 | ||
265 | l = strlen(s); | |
266 | if (l > 255) | |
267 | return -EINVAL; | |
268 | ||
269 | /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */ | |
270 | p = message_extend_fields(m, 8, 4 + 1 + l + 1); | |
271 | if (!p) | |
272 | return -ENOMEM; | |
273 | ||
274 | p[0] = h; | |
275 | p[1] = 1; | |
276 | p[2] = SD_BUS_TYPE_SIGNATURE; | |
277 | p[3] = 0; | |
278 | p[4] = l; | |
279 | memcpy(p + 5, s, l + 1); | |
280 | ||
281 | if (ret) | |
282 | *ret = (const char*) p + 5; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) { | |
288 | uint8_t *p; | |
289 | ||
290 | assert(m); | |
291 | ||
292 | /* field id byte + signature length + signature 'u' + NUL + value */ | |
293 | p = message_extend_fields(m, 8, 4 + 4); | |
294 | if (!p) | |
295 | return -ENOMEM; | |
296 | ||
297 | p[0] = h; | |
298 | p[1] = 1; | |
299 | p[2] = SD_BUS_TYPE_UINT32; | |
300 | p[3] = 0; | |
301 | ||
302 | ((uint32_t*) p)[1] = x; | |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
6629161f | 307 | int bus_message_from_header( |
2571ead1 LP |
308 | void *buffer, |
309 | size_t length, | |
2c93b4ef LP |
310 | int *fds, |
311 | unsigned n_fds, | |
312 | const struct ucred *ucred, | |
2571ead1 | 313 | const char *label, |
6629161f | 314 | size_t extra, |
2571ead1 LP |
315 | sd_bus_message **ret) { |
316 | ||
80a46c73 LP |
317 | sd_bus_message *m; |
318 | struct bus_header *h; | |
6629161f | 319 | size_t a, label_sz; |
80a46c73 LP |
320 | |
321 | assert(buffer || length <= 0); | |
2c93b4ef | 322 | assert(fds || n_fds <= 0); |
80a46c73 LP |
323 | assert(ret); |
324 | ||
325 | if (length < sizeof(struct bus_header)) | |
326 | return -EBADMSG; | |
327 | ||
328 | h = buffer; | |
329 | if (h->version != 1) | |
330 | return -EBADMSG; | |
331 | ||
332 | if (h->serial == 0) | |
333 | return -EBADMSG; | |
334 | ||
335 | if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID) | |
336 | return -EBADMSG; | |
337 | ||
6629161f LP |
338 | if (h->endian != SD_BUS_LITTLE_ENDIAN && |
339 | h->endian != SD_BUS_BIG_ENDIAN) | |
80a46c73 LP |
340 | return -EBADMSG; |
341 | ||
6629161f | 342 | a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra); |
80a46c73 | 343 | |
2571ead1 LP |
344 | if (label) { |
345 | label_sz = strlen(label); | |
6629161f LP |
346 | a += label_sz + 1; |
347 | } | |
2571ead1 LP |
348 | |
349 | m = malloc0(a); | |
80a46c73 LP |
350 | if (!m) |
351 | return -ENOMEM; | |
352 | ||
353 | m->n_ref = 1; | |
2c93b4ef | 354 | m->sealed = true; |
80a46c73 | 355 | m->header = h; |
2c93b4ef LP |
356 | m->fds = fds; |
357 | m->n_fds = n_fds; | |
80a46c73 | 358 | |
2571ead1 LP |
359 | if (ucred) { |
360 | m->uid = ucred->uid; | |
361 | m->pid = ucred->pid; | |
362 | m->gid = ucred->gid; | |
363 | m->uid_valid = m->gid_valid = true; | |
364 | } | |
365 | ||
366 | if (label) { | |
6629161f | 367 | m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra); |
2571ead1 LP |
368 | memcpy(m->label, label, label_sz + 1); |
369 | } | |
370 | ||
6629161f LP |
371 | *ret = m; |
372 | return 0; | |
373 | } | |
374 | ||
375 | int bus_message_from_malloc( | |
376 | void *buffer, | |
377 | size_t length, | |
378 | int *fds, | |
379 | unsigned n_fds, | |
380 | const struct ucred *ucred, | |
381 | const char *label, | |
382 | sd_bus_message **ret) { | |
383 | ||
384 | sd_bus_message *m; | |
385 | int r; | |
386 | ||
387 | r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m); | |
388 | if (r < 0) | |
389 | return r; | |
390 | ||
391 | if (length != BUS_MESSAGE_SIZE(m)) { | |
392 | r = -EBADMSG; | |
393 | goto fail; | |
394 | } | |
395 | ||
bc7fd8cd LP |
396 | m->n_body_parts = 1; |
397 | m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)); | |
398 | m->body.size = length - sizeof(struct bus_header) - ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)); | |
399 | m->body.sealed = true; | |
400 | m->body.memfd = -1; | |
6629161f | 401 | |
80a46c73 | 402 | m->n_iovec = 1; |
bc7fd8cd | 403 | m->iovec = m->iovec_fixed; |
80a46c73 LP |
404 | m->iovec[0].iov_base = buffer; |
405 | m->iovec[0].iov_len = length; | |
406 | ||
6629161f | 407 | r = bus_message_parse_fields(m); |
2c93b4ef LP |
408 | if (r < 0) |
409 | goto fail; | |
410 | ||
411 | /* We take possession of the memory and fds now */ | |
412 | m->free_header = true; | |
413 | m->free_fds = true; | |
80a46c73 LP |
414 | |
415 | *ret = m; | |
416 | return 0; | |
2c93b4ef LP |
417 | |
418 | fail: | |
419 | message_free(m); | |
420 | return r; | |
80a46c73 LP |
421 | } |
422 | ||
de1c301e LP |
423 | static sd_bus_message *message_new(sd_bus *bus, uint8_t type) { |
424 | sd_bus_message *m; | |
425 | ||
80a46c73 | 426 | m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header)); |
de1c301e LP |
427 | if (!m) |
428 | return NULL; | |
429 | ||
430 | m->n_ref = 1; | |
431 | m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message))); | |
80a46c73 | 432 | m->header->endian = SD_BUS_NATIVE_ENDIAN; |
de1c301e LP |
433 | m->header->type = type; |
434 | m->header->version = bus ? bus->message_version : 1; | |
021a1e78 | 435 | m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING); |
de1c301e | 436 | |
9b05bc48 LP |
437 | if (bus) |
438 | m->bus = sd_bus_ref(bus); | |
439 | ||
de1c301e LP |
440 | return m; |
441 | } | |
442 | ||
443 | int sd_bus_message_new_signal( | |
444 | sd_bus *bus, | |
445 | const char *path, | |
446 | const char *interface, | |
447 | const char *member, | |
448 | sd_bus_message **m) { | |
449 | ||
450 | sd_bus_message *t; | |
451 | int r; | |
452 | ||
9d6c7c82 LP |
453 | assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN); |
454 | assert_return(object_path_is_valid(path), -EINVAL); | |
455 | assert_return(interface_name_is_valid(interface), -EINVAL); | |
456 | assert_return(member_name_is_valid(member), -EINVAL); | |
457 | assert_return(m, -EINVAL); | |
de1c301e | 458 | |
40ca29a1 | 459 | t = message_new(bus, SD_BUS_MESSAGE_SIGNAL); |
de1c301e LP |
460 | if (!t) |
461 | return -ENOMEM; | |
462 | ||
89ffcd2a LP |
463 | t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED; |
464 | ||
de1c301e LP |
465 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path); |
466 | if (r < 0) | |
467 | goto fail; | |
468 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface); | |
469 | if (r < 0) | |
470 | goto fail; | |
471 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member); | |
472 | if (r < 0) | |
473 | goto fail; | |
474 | ||
475 | *m = t; | |
476 | return 0; | |
477 | ||
478 | fail: | |
479 | sd_bus_message_unref(t); | |
480 | return r; | |
481 | } | |
482 | ||
483 | int sd_bus_message_new_method_call( | |
484 | sd_bus *bus, | |
485 | const char *destination, | |
486 | const char *path, | |
487 | const char *interface, | |
488 | const char *member, | |
489 | sd_bus_message **m) { | |
490 | ||
491 | sd_bus_message *t; | |
492 | int r; | |
493 | ||
9d6c7c82 LP |
494 | assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN); |
495 | assert_return(!destination || service_name_is_valid(destination), -EINVAL); | |
496 | assert_return(object_path_is_valid(path), -EINVAL); | |
497 | assert_return(!interface || interface_name_is_valid(interface), -EINVAL); | |
498 | assert_return(member_name_is_valid(member), -EINVAL); | |
499 | assert_return(m, -EINVAL); | |
de1c301e | 500 | |
40ca29a1 | 501 | t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL); |
de1c301e LP |
502 | if (!t) |
503 | return -ENOMEM; | |
504 | ||
505 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path); | |
506 | if (r < 0) | |
507 | goto fail; | |
508 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member); | |
509 | if (r < 0) | |
510 | goto fail; | |
511 | ||
512 | if (interface) { | |
513 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface); | |
514 | if (r < 0) | |
515 | goto fail; | |
516 | } | |
517 | ||
518 | if (destination) { | |
519 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination); | |
520 | if (r < 0) | |
521 | goto fail; | |
522 | } | |
523 | ||
524 | *m = t; | |
525 | return 0; | |
526 | ||
527 | fail: | |
528 | message_free(t); | |
529 | return r; | |
530 | } | |
531 | ||
5407f2de | 532 | static int message_new_reply( |
de1c301e LP |
533 | sd_bus *bus, |
534 | sd_bus_message *call, | |
5407f2de | 535 | uint8_t type, |
de1c301e LP |
536 | sd_bus_message **m) { |
537 | ||
538 | sd_bus_message *t; | |
539 | int r; | |
540 | ||
9d6c7c82 LP |
541 | assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN); |
542 | assert_return(call, -EINVAL); | |
543 | assert_return(call->sealed, -EPERM); | |
544 | assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); | |
545 | assert_return(m, -EINVAL); | |
de1c301e | 546 | |
5407f2de | 547 | t = message_new(bus, type); |
de1c301e LP |
548 | if (!t) |
549 | return -ENOMEM; | |
550 | ||
89ffcd2a | 551 | t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED; |
de1c301e | 552 | t->reply_serial = BUS_MESSAGE_SERIAL(call); |
5407f2de | 553 | |
de1c301e LP |
554 | r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial); |
555 | if (r < 0) | |
556 | goto fail; | |
557 | ||
558 | if (call->sender) { | |
8f155917 | 559 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination); |
de1c301e LP |
560 | if (r < 0) |
561 | goto fail; | |
562 | } | |
563 | ||
5407f2de LP |
564 | t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED); |
565 | ||
de1c301e | 566 | *m = t; |
89ffcd2a | 567 | return 0; |
de1c301e LP |
568 | |
569 | fail: | |
570 | message_free(t); | |
571 | return r; | |
572 | } | |
573 | ||
5407f2de LP |
574 | int sd_bus_message_new_method_return( |
575 | sd_bus *bus, | |
576 | sd_bus_message *call, | |
577 | sd_bus_message **m) { | |
578 | ||
40ca29a1 | 579 | return message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_RETURN, m); |
5407f2de LP |
580 | } |
581 | ||
de1c301e LP |
582 | int sd_bus_message_new_method_error( |
583 | sd_bus *bus, | |
584 | sd_bus_message *call, | |
585 | const sd_bus_error *e, | |
586 | sd_bus_message **m) { | |
587 | ||
588 | sd_bus_message *t; | |
589 | int r; | |
590 | ||
9d6c7c82 LP |
591 | assert_return(sd_bus_error_is_set(e), -EINVAL); |
592 | assert_return(m, -EINVAL); | |
de1c301e | 593 | |
40ca29a1 | 594 | r = message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_ERROR, &t); |
de1c301e | 595 | if (r < 0) |
5407f2de | 596 | return r; |
de1c301e LP |
597 | |
598 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name); | |
599 | if (r < 0) | |
600 | goto fail; | |
601 | ||
602 | if (e->message) { | |
603 | r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message); | |
604 | if (r < 0) | |
605 | goto fail; | |
606 | } | |
607 | ||
608 | *m = t; | |
609 | return 0; | |
610 | ||
611 | fail: | |
612 | message_free(t); | |
613 | return r; | |
614 | } | |
615 | ||
29ddb38f LP |
616 | int sd_bus_message_new_method_errorf( |
617 | sd_bus *bus, | |
618 | sd_bus_message *call, | |
619 | sd_bus_message **m, | |
620 | const char *name, | |
621 | const char *format, | |
622 | ...) { | |
623 | ||
40ca29a1 | 624 | _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
29ddb38f LP |
625 | va_list ap; |
626 | int r; | |
627 | ||
40ca29a1 LP |
628 | assert_return(name, -EINVAL); |
629 | assert_return(m, -EINVAL); | |
630 | ||
631 | va_start(ap, format); | |
632 | r = bus_error_setfv(&error, name, format, ap); | |
633 | va_end(ap); | |
29ddb38f | 634 | |
29ddb38f LP |
635 | if (r < 0) |
636 | return r; | |
637 | ||
40ca29a1 LP |
638 | return sd_bus_message_new_method_error(bus, call, &error, m); |
639 | } | |
29ddb38f | 640 | |
40ca29a1 LP |
641 | int sd_bus_message_new_method_errno( |
642 | sd_bus *bus, | |
643 | sd_bus_message *call, | |
644 | int error, | |
645 | const sd_bus_error *p, | |
646 | sd_bus_message **m) { | |
29ddb38f | 647 | |
40ca29a1 | 648 | _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; |
29ddb38f | 649 | |
40ca29a1 LP |
650 | if (sd_bus_error_is_set(p)) |
651 | return sd_bus_message_new_method_error(bus, call, p, m); | |
29ddb38f | 652 | |
40ca29a1 | 653 | sd_bus_error_set_errno(&berror, error); |
29ddb38f | 654 | |
40ca29a1 LP |
655 | return sd_bus_message_new_method_error(bus, call, &berror, m); |
656 | } | |
29ddb38f | 657 | |
40ca29a1 LP |
658 | int sd_bus_message_new_method_errnof( |
659 | sd_bus *bus, | |
660 | sd_bus_message *call, | |
661 | sd_bus_message **m, | |
662 | int error, | |
663 | const char *format, | |
664 | ...) { | |
665 | ||
666 | _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; | |
667 | va_list ap; | |
668 | int r; | |
669 | ||
670 | va_start(ap, format); | |
671 | r = bus_error_set_errnofv(&berror, error, format, ap); | |
672 | va_end(ap); | |
673 | ||
674 | if (r < 0) | |
675 | return r; | |
676 | ||
677 | return sd_bus_message_new_method_error(bus, call, &berror, m); | |
29ddb38f LP |
678 | } |
679 | ||
eb01ba5d LP |
680 | int bus_message_new_synthetic_error( |
681 | sd_bus *bus, | |
682 | uint64_t serial, | |
683 | const sd_bus_error *e, | |
684 | sd_bus_message **m) { | |
685 | ||
686 | sd_bus_message *t; | |
687 | int r; | |
688 | ||
689 | assert(sd_bus_error_is_set(e)); | |
690 | assert(m); | |
691 | ||
40ca29a1 | 692 | t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR); |
eb01ba5d LP |
693 | if (!t) |
694 | return -ENOMEM; | |
695 | ||
696 | t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED; | |
697 | t->reply_serial = serial; | |
698 | ||
699 | r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial); | |
700 | if (r < 0) | |
701 | goto fail; | |
702 | ||
703 | if (bus && bus->unique_name) { | |
704 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination); | |
705 | if (r < 0) | |
706 | goto fail; | |
707 | } | |
708 | ||
3a7d4f1b LP |
709 | r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name); |
710 | if (r < 0) | |
711 | goto fail; | |
712 | ||
713 | if (e->message) { | |
714 | r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message); | |
715 | if (r < 0) | |
716 | goto fail; | |
717 | } | |
718 | ||
eb01ba5d LP |
719 | *m = t; |
720 | return 0; | |
721 | ||
722 | fail: | |
723 | message_free(t); | |
724 | return r; | |
725 | } | |
726 | ||
de1c301e | 727 | sd_bus_message* sd_bus_message_ref(sd_bus_message *m) { |
9d6c7c82 | 728 | assert_return(m, NULL); |
de1c301e LP |
729 | |
730 | assert(m->n_ref > 0); | |
731 | m->n_ref++; | |
732 | ||
733 | return m; | |
734 | } | |
735 | ||
736 | sd_bus_message* sd_bus_message_unref(sd_bus_message *m) { | |
9d6c7c82 | 737 | assert_return(m, NULL); |
de1c301e LP |
738 | |
739 | assert(m->n_ref > 0); | |
740 | m->n_ref--; | |
741 | ||
742 | if (m->n_ref <= 0) | |
743 | message_free(m); | |
744 | ||
745 | return NULL; | |
746 | } | |
747 | ||
748 | int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) { | |
9d6c7c82 LP |
749 | assert_return(m, -EINVAL); |
750 | assert_return(type, -EINVAL); | |
de1c301e LP |
751 | |
752 | *type = m->header->type; | |
753 | return 0; | |
754 | } | |
755 | ||
756 | int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) { | |
9d6c7c82 LP |
757 | assert_return(m, -EINVAL); |
758 | assert_return(serial, -EINVAL); | |
759 | assert_return(m->header->serial != 0, -ENOENT); | |
de1c301e LP |
760 | |
761 | *serial = BUS_MESSAGE_SERIAL(m); | |
762 | return 0; | |
763 | } | |
764 | ||
765 | int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) { | |
9d6c7c82 LP |
766 | assert_return(m, -EINVAL); |
767 | assert_return(serial, -EINVAL); | |
768 | assert_return(m->reply_serial != 0, -ENOENT); | |
de1c301e LP |
769 | |
770 | *serial = m->reply_serial; | |
771 | return 0; | |
772 | } | |
773 | ||
774 | int sd_bus_message_get_no_reply(sd_bus_message *m) { | |
9d6c7c82 | 775 | assert_return(m, -EINVAL); |
de1c301e | 776 | |
40ca29a1 | 777 | return m->header->type == SD_BUS_MESSAGE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0; |
de1c301e LP |
778 | } |
779 | ||
780 | const char *sd_bus_message_get_path(sd_bus_message *m) { | |
9d6c7c82 | 781 | assert_return(m, NULL); |
de1c301e LP |
782 | |
783 | return m->path; | |
784 | } | |
785 | ||
786 | const char *sd_bus_message_get_interface(sd_bus_message *m) { | |
9d6c7c82 | 787 | assert_return(m, NULL); |
de1c301e LP |
788 | |
789 | return m->interface; | |
790 | } | |
791 | ||
792 | const char *sd_bus_message_get_member(sd_bus_message *m) { | |
9d6c7c82 | 793 | assert_return(m, NULL); |
de1c301e LP |
794 | |
795 | return m->member; | |
796 | } | |
797 | const char *sd_bus_message_get_destination(sd_bus_message *m) { | |
9d6c7c82 | 798 | assert_return(m, NULL); |
de1c301e LP |
799 | |
800 | return m->destination; | |
801 | } | |
802 | ||
803 | const char *sd_bus_message_get_sender(sd_bus_message *m) { | |
9d6c7c82 | 804 | assert_return(m, NULL); |
de1c301e LP |
805 | |
806 | return m->sender; | |
807 | } | |
808 | ||
809 | const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) { | |
9d6c7c82 LP |
810 | assert_return(m, NULL); |
811 | assert_return(sd_bus_error_is_set(&m->error), NULL); | |
de1c301e LP |
812 | |
813 | return &m->error; | |
814 | } | |
815 | ||
816 | int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) { | |
9d6c7c82 LP |
817 | assert_return(m, -EINVAL); |
818 | assert_return(uid, -EINVAL); | |
819 | assert_return(m->uid_valid, -ESRCH); | |
de1c301e LP |
820 | |
821 | *uid = m->uid; | |
822 | return 0; | |
823 | } | |
824 | ||
825 | int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) { | |
9d6c7c82 LP |
826 | assert_return(m, -EINVAL); |
827 | assert_return(gid, -EINVAL); | |
828 | assert_return(m->gid_valid, -ESRCH); | |
de1c301e LP |
829 | |
830 | *gid = m->gid; | |
831 | return 0; | |
832 | } | |
833 | ||
834 | int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) { | |
9d6c7c82 LP |
835 | assert_return(m, -EINVAL); |
836 | assert_return(pid, -EINVAL); | |
837 | assert_return(m->pid > 0, -ESRCH); | |
de1c301e LP |
838 | |
839 | *pid = m->pid; | |
840 | return 0; | |
841 | } | |
842 | ||
843 | int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) { | |
9d6c7c82 LP |
844 | assert_return(m, -EINVAL); |
845 | assert_return(tid, -EINVAL); | |
846 | assert_return(m->tid > 0, -ESRCH); | |
de1c301e LP |
847 | |
848 | *tid = m->tid; | |
849 | return 0; | |
850 | } | |
851 | ||
8323bc1f | 852 | int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) { |
9d6c7c82 LP |
853 | assert_return(m, -EINVAL); |
854 | assert_return(usec, -EINVAL); | |
855 | assert_return(m->pid_starttime > 0, -ESRCH); | |
8323bc1f LP |
856 | |
857 | *usec = m->pid_starttime; | |
858 | return 0; | |
859 | } | |
860 | ||
4a875b61 | 861 | int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) { |
9d6c7c82 LP |
862 | assert_return(m, -EINVAL); |
863 | assert_return(m->label, -ESRCH); | |
2571ead1 | 864 | |
4a875b61 LP |
865 | *ret = m->label; |
866 | return 0; | |
2571ead1 LP |
867 | } |
868 | ||
69aec65c | 869 | int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) { |
9d6c7c82 LP |
870 | assert_return(m, -EINVAL); |
871 | assert_return(usec, -EINVAL); | |
872 | assert_return(m->monotonic > 0, -ESRCH); | |
acb5a3cb | 873 | |
69aec65c | 874 | *usec = m->monotonic; |
acb5a3cb LP |
875 | return 0; |
876 | } | |
877 | ||
69aec65c | 878 | int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) { |
9d6c7c82 LP |
879 | assert_return(m, -EINVAL); |
880 | assert_return(usec, -EINVAL); | |
881 | assert_return(m->realtime > 0, -ESRCH); | |
69aec65c LP |
882 | |
883 | *usec = m->realtime; | |
884 | return 0; | |
885 | } | |
886 | ||
4a875b61 | 887 | int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) { |
9d6c7c82 LP |
888 | assert_return(m, -EINVAL); |
889 | assert_return(ret, -EINVAL); | |
890 | assert_return(m->comm, -ESRCH); | |
69aec65c | 891 | |
4a875b61 LP |
892 | *ret = m->comm; |
893 | return 0; | |
69aec65c LP |
894 | } |
895 | ||
4a875b61 | 896 | int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) { |
9d6c7c82 LP |
897 | assert_return(m, -EINVAL); |
898 | assert_return(ret, -EINVAL); | |
899 | assert_return(m->tid_comm, -ESRCH); | |
69aec65c | 900 | |
4a875b61 LP |
901 | *ret = m->tid_comm; |
902 | return 0; | |
69aec65c LP |
903 | } |
904 | ||
4a875b61 | 905 | int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) { |
9d6c7c82 LP |
906 | assert_return(m, -EINVAL); |
907 | assert_return(ret, -EINVAL); | |
908 | assert_return(m->exe, -ESRCH); | |
69aec65c | 909 | |
4a875b61 LP |
910 | *ret = m->exe; |
911 | return 0; | |
912 | } | |
913 | ||
914 | int sd_bus_message_get_cgroup(sd_bus_message *m, const char **ret) { | |
9d6c7c82 LP |
915 | assert_return(m, -EINVAL); |
916 | assert_return(ret, -EINVAL); | |
917 | assert_return(m->cgroup, -ESRCH); | |
4a875b61 LP |
918 | |
919 | *ret = m->cgroup; | |
920 | return 0; | |
69aec65c LP |
921 | } |
922 | ||
d8d3d8a7 LP |
923 | int sd_bus_message_get_unit(sd_bus_message *m, const char **ret) { |
924 | int r; | |
925 | ||
9d6c7c82 LP |
926 | assert_return(m, -EINVAL); |
927 | assert_return(ret, -EINVAL); | |
928 | assert_return(m->cgroup, -ESRCH); | |
d8d3d8a7 LP |
929 | |
930 | if (!m->unit) { | |
931 | r = cg_path_get_unit(m->cgroup, &m->unit); | |
932 | if (r < 0) | |
933 | return r; | |
934 | } | |
935 | ||
936 | *ret = m->unit; | |
937 | return 0; | |
938 | } | |
939 | ||
940 | int sd_bus_message_get_user_unit(sd_bus_message *m, const char **ret) { | |
941 | int r; | |
942 | ||
9d6c7c82 LP |
943 | assert_return(m, -EINVAL); |
944 | assert_return(ret, -EINVAL); | |
945 | assert_return(m->cgroup, -ESRCH); | |
d8d3d8a7 LP |
946 | |
947 | if (!m->user_unit) { | |
948 | r = cg_path_get_user_unit(m->cgroup, &m->user_unit); | |
949 | if (r < 0) | |
950 | return r; | |
951 | } | |
952 | ||
953 | *ret = m->user_unit; | |
954 | return 0; | |
955 | } | |
956 | ||
957 | int sd_bus_message_get_session(sd_bus_message *m, const char **ret) { | |
958 | int r; | |
959 | ||
9d6c7c82 LP |
960 | assert_return(m, -EINVAL); |
961 | assert_return(ret, -EINVAL); | |
962 | assert_return(m->cgroup, -ESRCH); | |
d8d3d8a7 LP |
963 | |
964 | if (!m->session) { | |
965 | r = cg_path_get_session(m->cgroup, &m->session); | |
966 | if (r < 0) | |
967 | return r; | |
968 | } | |
969 | ||
970 | *ret = m->session; | |
971 | return 0; | |
972 | } | |
973 | ||
bc7f3beb | 974 | int sd_bus_message_get_owner_uid(sd_bus_message *m, uid_t *uid) { |
9d6c7c82 LP |
975 | assert_return(m, -EINVAL); |
976 | assert_return(uid, -EINVAL); | |
977 | assert_return(m->cgroup, -ESRCH); | |
bc7f3beb LP |
978 | |
979 | return cg_path_get_owner_uid(m->cgroup, uid); | |
980 | } | |
981 | ||
77930f11 LP |
982 | int sd_bus_message_get_cmdline(sd_bus_message *m, char ***cmdline) { |
983 | size_t n, i; | |
984 | const char *p; | |
985 | bool first; | |
986 | ||
9d6c7c82 LP |
987 | assert_return(m, -EINVAL); |
988 | assert_return(m->cmdline, -ESRCH); | |
77930f11 LP |
989 | |
990 | for (p = m->cmdline, n = 0; p < m->cmdline + m->cmdline_length; p++) | |
991 | if (*p == 0) | |
992 | n++; | |
993 | ||
994 | m->cmdline_array = new(char*, n + 1); | |
995 | if (!m->cmdline_array) | |
996 | return -ENOMEM; | |
997 | ||
998 | for (p = m->cmdline, i = 0, first = true; p < m->cmdline + m->cmdline_length; p++) { | |
999 | if (first) | |
1000 | m->cmdline_array[i++] = (char*) p; | |
1001 | ||
1002 | first = *p == 0; | |
1003 | } | |
1004 | ||
1005 | m->cmdline_array[i] = NULL; | |
1006 | *cmdline = m->cmdline_array; | |
1007 | ||
1008 | return 0; | |
1009 | } | |
1010 | ||
120f919e | 1011 | int sd_bus_message_get_audit_sessionid(sd_bus_message *m, uint32_t *sessionid) { |
9d6c7c82 LP |
1012 | assert_return(m, -EINVAL); |
1013 | assert_return(sessionid, -EINVAL); | |
1014 | assert_return(m->audit, -ESRCH); | |
120f919e LP |
1015 | |
1016 | *sessionid = m->audit->sessionid; | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | int sd_bus_message_get_audit_loginuid(sd_bus_message *m, uid_t *uid) { | |
9d6c7c82 LP |
1021 | assert_return(m, -EINVAL); |
1022 | assert_return(uid, -EINVAL); | |
1023 | assert_return(m->audit, -ESRCH); | |
120f919e LP |
1024 | |
1025 | *uid = m->audit->loginuid; | |
1026 | return 0; | |
1027 | } | |
1028 | ||
102ea8e4 LP |
1029 | int sd_bus_message_has_effective_cap(sd_bus_message *m, int capability) { |
1030 | unsigned sz; | |
1031 | ||
9d6c7c82 LP |
1032 | assert_return(m, -EINVAL); |
1033 | assert_return(capability < 0, -EINVAL); | |
1034 | assert_return(!m->capability, -ESRCH); | |
102ea8e4 LP |
1035 | |
1036 | sz = m->capability_size / 4; | |
1037 | if ((unsigned) capability >= sz*8) | |
1038 | return 0; | |
1039 | ||
1040 | return !!(m->capability[2 * sz + (capability / 8)] & (1 << (capability % 8))); | |
1041 | } | |
1042 | ||
de1c301e | 1043 | int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) { |
9d6c7c82 | 1044 | assert_return(m, -EINVAL); |
de1c301e | 1045 | |
40ca29a1 | 1046 | if (m->header->type != SD_BUS_MESSAGE_SIGNAL) |
de1c301e LP |
1047 | return 0; |
1048 | ||
1049 | if (interface && (!m->interface || !streq(m->interface, interface))) | |
1050 | return 0; | |
1051 | ||
1052 | if (member && (!m->member || !streq(m->member, member))) | |
1053 | return 0; | |
1054 | ||
1055 | return 1; | |
1056 | } | |
1057 | ||
1058 | int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member) { | |
9d6c7c82 | 1059 | assert_return(m, -EINVAL); |
de1c301e | 1060 | |
40ca29a1 | 1061 | if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) |
de1c301e LP |
1062 | return 0; |
1063 | ||
1064 | if (interface && (!m->interface || !streq(m->interface, interface))) | |
1065 | return 0; | |
1066 | ||
1067 | if (member && (!m->member || !streq(m->member, member))) | |
1068 | return 0; | |
1069 | ||
1070 | return 1; | |
1071 | } | |
1072 | ||
1073 | int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) { | |
9d6c7c82 | 1074 | assert_return(m, -EINVAL); |
de1c301e | 1075 | |
40ca29a1 | 1076 | if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) |
de1c301e LP |
1077 | return 0; |
1078 | ||
1079 | if (name && (!m->error.name || !streq(m->error.name, name))) | |
1080 | return 0; | |
1081 | ||
1082 | return 1; | |
1083 | } | |
1084 | ||
1085 | int sd_bus_message_set_no_reply(sd_bus_message *m, int b) { | |
9d6c7c82 LP |
1086 | assert_return(m, -EINVAL); |
1087 | assert_return(!m->sealed, -EPERM); | |
1088 | assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM); | |
de1c301e LP |
1089 | |
1090 | if (b) | |
1091 | m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED; | |
1092 | else | |
1093 | m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED; | |
1094 | ||
1095 | return 0; | |
1096 | } | |
1097 | ||
1098 | static struct bus_container *message_get_container(sd_bus_message *m) { | |
1099 | assert(m); | |
1100 | ||
1101 | if (m->n_containers == 0) | |
1102 | return &m->root_container; | |
1103 | ||
9a17484d LP |
1104 | assert(m->containers); |
1105 | return m->containers + m->n_containers - 1; | |
de1c301e LP |
1106 | } |
1107 | ||
bc7fd8cd LP |
1108 | struct bus_body_part *message_append_part(sd_bus_message *m) { |
1109 | struct bus_body_part *part; | |
1110 | ||
1111 | assert(m); | |
1112 | ||
1113 | if (m->poisoned) | |
1114 | return NULL; | |
1115 | ||
1116 | if (m->n_body_parts <= 0) { | |
1117 | part = &m->body; | |
1118 | zero(*part); | |
1119 | } else { | |
1120 | assert(m->body_end); | |
1121 | ||
1122 | part = new0(struct bus_body_part, 1); | |
1123 | if (!part) { | |
1124 | m->poisoned = true; | |
1125 | return NULL; | |
1126 | } | |
1127 | ||
1128 | m->body_end->next = part; | |
1129 | } | |
1130 | ||
1131 | part->memfd = -1; | |
1132 | m->body_end = part; | |
1133 | m->n_body_parts ++; | |
1134 | ||
1135 | return part; | |
1136 | } | |
1137 | ||
1138 | static void part_zero(struct bus_body_part *part, size_t sz) { | |
1139 | assert(part); | |
1140 | assert(sz > 0); | |
1141 | assert(sz < 8); | |
1142 | ||
453a0c29 LP |
1143 | /* All other fields can be left in their defaults */ |
1144 | assert(!part->data); | |
1145 | assert(part->memfd < 0); | |
1146 | ||
bc7fd8cd | 1147 | part->size = sz; |
453a0c29 LP |
1148 | part->is_zero = true; |
1149 | part->sealed = true; | |
bc7fd8cd LP |
1150 | } |
1151 | ||
1152 | static int part_make_space( | |
1153 | struct sd_bus_message *m, | |
1154 | struct bus_body_part *part, | |
1155 | size_t sz, | |
1156 | void **q) { | |
1157 | ||
1158 | void *n; | |
1159 | int r; | |
1160 | ||
1161 | assert(m); | |
1162 | assert(part); | |
1163 | assert(!part->sealed); | |
1164 | ||
1165 | if (m->poisoned) | |
1166 | return -ENOMEM; | |
1167 | ||
1168 | if (!part->data && part->memfd < 0) | |
1169 | part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped); | |
1170 | ||
1171 | if (part->memfd >= 0) { | |
1172 | uint64_t u = sz; | |
1173 | ||
1174 | r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &u); | |
1175 | if (r < 0) { | |
1176 | m->poisoned = true; | |
1177 | return -errno; | |
1178 | } | |
1179 | ||
453a0c29 LP |
1180 | if (!part->data || sz > part->mapped) { |
1181 | size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1); | |
bc7fd8cd LP |
1182 | |
1183 | if (part->mapped <= 0) | |
1184 | n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0); | |
1185 | else | |
1186 | n = mremap(part->data, part->mapped, psz, MREMAP_MAYMOVE); | |
1187 | ||
1188 | if (n == MAP_FAILED) { | |
1189 | m->poisoned = true; | |
1190 | return -errno; | |
1191 | } | |
1192 | ||
1193 | part->mapped = psz; | |
1194 | part->data = n; | |
1195 | } | |
bf30e48f KS |
1196 | |
1197 | part->munmap_this = true; | |
bc7fd8cd | 1198 | } else { |
23c6f770 | 1199 | n = realloc(part->data, MAX(sz, 1u)); |
bc7fd8cd LP |
1200 | if (!n) { |
1201 | m->poisoned = true; | |
1202 | return -ENOMEM; | |
1203 | } | |
1204 | ||
1205 | part->data = n; | |
1206 | part->free_this = true; | |
1207 | } | |
1208 | ||
1209 | if (q) | |
1210 | *q = part->data ? (uint8_t*) part->data + part->size : NULL; | |
1211 | ||
1212 | part->size = sz; | |
1213 | return 0; | |
1214 | } | |
1215 | ||
453a0c29 | 1216 | static void message_extend_containers(sd_bus_message *m, size_t expand) { |
de1c301e | 1217 | struct bus_container *c; |
453a0c29 LP |
1218 | |
1219 | assert(m); | |
1220 | ||
1221 | if (expand <= 0) | |
1222 | return; | |
1223 | ||
1224 | /* Update counters */ | |
1225 | for (c = m->containers; c < m->containers + m->n_containers; c++) | |
1226 | if (c->array_size) | |
1227 | *c->array_size += expand; | |
453a0c29 LP |
1228 | } |
1229 | ||
1230 | static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) { | |
bc7fd8cd LP |
1231 | struct bus_body_part *part = NULL; |
1232 | size_t start_body, end_body, padding, start_part, end_part, added; | |
1233 | bool add_new_part; | |
1234 | void *p; | |
1235 | int r; | |
de1c301e LP |
1236 | |
1237 | assert(m); | |
9a17484d | 1238 | assert(align > 0); |
bc7fd8cd | 1239 | assert(!m->sealed); |
de1c301e | 1240 | |
bc7fd8cd LP |
1241 | if (m->poisoned) |
1242 | return NULL; | |
de1c301e | 1243 | |
bc7fd8cd LP |
1244 | start_body = ALIGN_TO((size_t) m->header->body_size, align); |
1245 | end_body = start_body + sz; | |
1246 | ||
1247 | padding = start_body - m->header->body_size; | |
1248 | added = padding + sz; | |
1249 | ||
1250 | /* Check for 32bit overflows */ | |
1251 | if (end_body > (size_t) ((uint32_t) -1)) { | |
1252 | m->poisoned = true; | |
de1c301e | 1253 | return NULL; |
bc7fd8cd | 1254 | } |
de1c301e | 1255 | |
bc7fd8cd LP |
1256 | add_new_part = |
1257 | m->n_body_parts <= 0 || | |
1258 | m->body_end->sealed || | |
1259 | padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size; | |
de1c301e | 1260 | |
bc7fd8cd LP |
1261 | if (add_new_part) { |
1262 | if (padding > 0) { | |
1263 | part = message_append_part(m); | |
1264 | if (!part) | |
1265 | return NULL; | |
1266 | ||
1267 | part_zero(part, padding); | |
1268 | } | |
1269 | ||
1270 | part = message_append_part(m); | |
1271 | if (!part) | |
1272 | return NULL; | |
1273 | ||
1274 | r = part_make_space(m, part, sz, &p); | |
1275 | if (r < 0) | |
1276 | return NULL; | |
1277 | } else { | |
453a0c29 | 1278 | struct bus_container *c; |
bc7fd8cd LP |
1279 | void *op; |
1280 | size_t os; | |
1281 | ||
1282 | part = m->body_end; | |
1283 | op = part->data; | |
1284 | os = part->size; | |
1285 | ||
1286 | start_part = ALIGN_TO(part->size, align); | |
1287 | end_part = start_part + sz; | |
1288 | ||
1289 | r = part_make_space(m, part, end_part, &p); | |
1290 | if (r < 0) | |
1291 | return NULL; | |
1292 | ||
1293 | if (padding > 0) { | |
1294 | memset(p, 0, padding); | |
1295 | p = (uint8_t*) p + padding; | |
de1c301e LP |
1296 | } |
1297 | ||
bc7fd8cd LP |
1298 | /* Readjust pointers */ |
1299 | for (c = m->containers; c < m->containers + m->n_containers; c++) | |
1300 | c->array_size = adjust_pointer(c->array_size, op, os, part->data); | |
1301 | ||
1302 | m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data); | |
de1c301e LP |
1303 | } |
1304 | ||
bc7fd8cd | 1305 | m->header->body_size = end_body; |
453a0c29 | 1306 | message_extend_containers(m, added); |
de1c301e LP |
1307 | |
1308 | return p; | |
1309 | } | |
1310 | ||
1311 | int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) { | |
1312 | struct bus_container *c; | |
27f6e5c7 | 1313 | ssize_t align, sz; |
de1c301e LP |
1314 | uint32_t k; |
1315 | void *a; | |
2c93b4ef | 1316 | int fd = -1; |
62cfa9da | 1317 | uint32_t fdi = 0; |
2c93b4ef | 1318 | int r; |
de1c301e | 1319 | |
9d6c7c82 LP |
1320 | assert_return(m, -EINVAL); |
1321 | assert_return(p, -EINVAL); | |
1322 | assert_return(!m->sealed, -EPERM); | |
1323 | assert_return(bus_type_is_basic(type), -EINVAL); | |
1324 | assert_return(!m->poisoned, -ESTALE); | |
de1c301e LP |
1325 | |
1326 | c = message_get_container(m); | |
1327 | ||
1328 | if (c->signature && c->signature[c->index]) { | |
1329 | /* Container signature is already set */ | |
1330 | ||
1331 | if (c->signature[c->index] != type) | |
9a17484d | 1332 | return -ENXIO; |
de1c301e | 1333 | } else { |
5cbe7492 LP |
1334 | char *e; |
1335 | ||
de1c301e LP |
1336 | /* Maybe we can append to the signature? But only if this is the top-level container*/ |
1337 | if (c->enclosing != 0) | |
9a17484d | 1338 | return -ENXIO; |
de1c301e LP |
1339 | |
1340 | e = strextend(&c->signature, CHAR_TO_STR(type), NULL); | |
bc7fd8cd LP |
1341 | if (!e) { |
1342 | m->poisoned = true; | |
de1c301e | 1343 | return -ENOMEM; |
bc7fd8cd | 1344 | } |
de1c301e LP |
1345 | } |
1346 | ||
de1c301e LP |
1347 | switch (type) { |
1348 | ||
1349 | case SD_BUS_TYPE_STRING: | |
1350 | case SD_BUS_TYPE_OBJECT_PATH: | |
b8beb278 | 1351 | |
de1c301e LP |
1352 | align = 4; |
1353 | sz = 4 + strlen(p) + 1; | |
1354 | break; | |
1355 | ||
1356 | case SD_BUS_TYPE_SIGNATURE: | |
b8beb278 | 1357 | |
de1c301e LP |
1358 | align = 1; |
1359 | sz = 1 + strlen(p) + 1; | |
1360 | break; | |
1361 | ||
1362 | case SD_BUS_TYPE_BOOLEAN: | |
1363 | align = sz = 4; | |
1364 | ||
1365 | assert_cc(sizeof(int) == sizeof(uint32_t)); | |
1366 | memcpy(&k, p, 4); | |
1367 | k = !!k; | |
1368 | p = &k; | |
1369 | break; | |
1370 | ||
2c93b4ef LP |
1371 | case SD_BUS_TYPE_UNIX_FD: { |
1372 | int z, *f; | |
1373 | ||
1374 | if (!m->allow_fds) { | |
1375 | r = -ENOTSUP; | |
1376 | goto fail; | |
1377 | } | |
1378 | ||
1379 | align = sz = 4; | |
1380 | ||
1381 | z = *(int*) p; | |
1382 | if (z < 0) { | |
1383 | r = -EINVAL; | |
1384 | goto fail; | |
1385 | } | |
1386 | ||
1387 | fd = fcntl(z, F_DUPFD_CLOEXEC, 3); | |
1388 | if (fd < 0) { | |
1389 | r = -errno; | |
1390 | goto fail; | |
1391 | } | |
1392 | ||
1393 | f = realloc(m->fds, sizeof(int) * (m->n_fds + 1)); | |
1394 | if (!f) { | |
bc7fd8cd | 1395 | m->poisoned = true; |
2c93b4ef LP |
1396 | r = -ENOMEM; |
1397 | goto fail; | |
1398 | } | |
1399 | ||
1400 | fdi = m->n_fds; | |
1401 | f[fdi] = fd; | |
1402 | m->fds = f; | |
1403 | m->free_fds = true; | |
1404 | break; | |
1405 | } | |
1406 | ||
de1c301e LP |
1407 | default: |
1408 | align = bus_type_get_alignment(type); | |
1409 | sz = bus_type_get_size(type); | |
1410 | break; | |
1411 | } | |
1412 | ||
1413 | assert(align > 0); | |
1414 | assert(sz > 0); | |
1415 | ||
1416 | a = message_extend_body(m, align, sz); | |
1417 | if (!a) { | |
2c93b4ef LP |
1418 | r = -ENOMEM; |
1419 | goto fail; | |
de1c301e LP |
1420 | } |
1421 | ||
1422 | if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) { | |
1423 | *(uint32_t*) a = sz - 5; | |
1424 | memcpy((uint8_t*) a + 4, p, sz - 4); | |
1425 | ||
1426 | if (stored) | |
1427 | *stored = (const uint8_t*) a + 4; | |
1428 | ||
1429 | } else if (type == SD_BUS_TYPE_SIGNATURE) { | |
1430 | *(uint8_t*) a = sz - 1; | |
1431 | memcpy((uint8_t*) a + 1, p, sz - 1); | |
1432 | ||
1433 | if (stored) | |
1434 | *stored = (const uint8_t*) a + 1; | |
2c93b4ef LP |
1435 | } else if (type == SD_BUS_TYPE_UNIX_FD) { |
1436 | *(uint32_t*) a = fdi; | |
1437 | ||
1438 | if (stored) | |
1439 | *stored = a; | |
1440 | ||
1441 | m->n_fds ++; | |
de1c301e LP |
1442 | |
1443 | } else { | |
1444 | memcpy(a, p, sz); | |
1445 | ||
1446 | if (stored) | |
1447 | *stored = a; | |
1448 | } | |
1449 | ||
1450 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
9a17484d | 1451 | c->index++; |
de1c301e LP |
1452 | |
1453 | return 0; | |
2c93b4ef LP |
1454 | |
1455 | fail: | |
2c93b4ef LP |
1456 | if (fd >= 0) |
1457 | close_nointr_nofail(fd); | |
1458 | ||
1459 | return r; | |
de1c301e LP |
1460 | } |
1461 | ||
1462 | int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) { | |
1463 | return message_append_basic(m, type, p, NULL); | |
1464 | } | |
1465 | ||
f8e013f8 LP |
1466 | int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s) { |
1467 | struct bus_container *c; | |
f8e013f8 | 1468 | void *a; |
f8e013f8 | 1469 | |
9d6c7c82 LP |
1470 | assert_return(m, -EINVAL); |
1471 | assert_return(s, -EINVAL); | |
1472 | assert_return(!m->sealed, -EPERM); | |
1473 | assert_return(!m->poisoned, -ESTALE); | |
f8e013f8 LP |
1474 | |
1475 | c = message_get_container(m); | |
1476 | ||
1477 | if (c->signature && c->signature[c->index]) { | |
1478 | /* Container signature is already set */ | |
1479 | ||
1480 | if (c->signature[c->index] != SD_BUS_TYPE_STRING) | |
1481 | return -ENXIO; | |
1482 | } else { | |
5cbe7492 LP |
1483 | char *e; |
1484 | ||
f8e013f8 LP |
1485 | /* Maybe we can append to the signature? But only if this is the top-level container*/ |
1486 | if (c->enclosing != 0) | |
1487 | return -ENXIO; | |
1488 | ||
1489 | e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL); | |
bc7fd8cd LP |
1490 | if (!e) { |
1491 | m->poisoned = true; | |
f8e013f8 | 1492 | return -ENOMEM; |
bc7fd8cd | 1493 | } |
f8e013f8 LP |
1494 | } |
1495 | ||
f8e013f8 | 1496 | a = message_extend_body(m, 4, 4 + size + 1); |
bc7fd8cd LP |
1497 | if (!a) |
1498 | return -ENOMEM; | |
f8e013f8 LP |
1499 | |
1500 | *(uint32_t*) a = size; | |
1501 | *s = (char*) a + 4; | |
1502 | ||
1503 | (*s)[size] = 0; | |
1504 | ||
1505 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
1506 | c->index++; | |
1507 | ||
1508 | return 0; | |
f8e013f8 LP |
1509 | } |
1510 | ||
de1c301e LP |
1511 | static int bus_message_open_array( |
1512 | sd_bus_message *m, | |
1513 | struct bus_container *c, | |
1514 | const char *contents, | |
1515 | uint32_t **array_size) { | |
1516 | ||
9a17484d | 1517 | unsigned nindex; |
bc7fd8cd | 1518 | void *a, *op; |
de1c301e | 1519 | int alignment; |
bc7fd8cd LP |
1520 | size_t os; |
1521 | struct bus_body_part *o; | |
de1c301e LP |
1522 | |
1523 | assert(m); | |
1524 | assert(c); | |
1525 | assert(contents); | |
1526 | assert(array_size); | |
1527 | ||
29ddb38f | 1528 | if (!signature_is_single(contents, true)) |
de1c301e LP |
1529 | return -EINVAL; |
1530 | ||
1531 | alignment = bus_type_get_alignment(contents[0]); | |
1532 | if (alignment < 0) | |
1533 | return alignment; | |
1534 | ||
1535 | if (c->signature && c->signature[c->index]) { | |
1536 | ||
1537 | /* Verify the existing signature */ | |
1538 | ||
1539 | if (c->signature[c->index] != SD_BUS_TYPE_ARRAY) | |
9a17484d | 1540 | return -ENXIO; |
de1c301e LP |
1541 | |
1542 | if (!startswith(c->signature + c->index + 1, contents)) | |
9a17484d | 1543 | return -ENXIO; |
de1c301e LP |
1544 | |
1545 | nindex = c->index + 1 + strlen(contents); | |
1546 | } else { | |
5cbe7492 LP |
1547 | char *e; |
1548 | ||
de1c301e | 1549 | if (c->enclosing != 0) |
9a17484d | 1550 | return -ENXIO; |
de1c301e LP |
1551 | |
1552 | /* Extend the existing signature */ | |
1553 | ||
1554 | e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL); | |
bc7fd8cd LP |
1555 | if (!e) { |
1556 | m->poisoned = true; | |
de1c301e | 1557 | return -ENOMEM; |
bc7fd8cd | 1558 | } |
de1c301e LP |
1559 | |
1560 | nindex = e - c->signature; | |
1561 | } | |
1562 | ||
de1c301e | 1563 | a = message_extend_body(m, 4, 4); |
bc7fd8cd | 1564 | if (!a) |
de1c301e | 1565 | return -ENOMEM; |
de1c301e | 1566 | |
bc7fd8cd LP |
1567 | o = m->body_end; |
1568 | op = m->body_end->data; | |
1569 | os = m->body_end->size; | |
de1c301e | 1570 | |
bc7fd8cd LP |
1571 | /* Add alignment between size and first element */ |
1572 | if (!message_extend_body(m, alignment, 0)) | |
de1c301e | 1573 | return -ENOMEM; |
de1c301e LP |
1574 | |
1575 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
1576 | c->index = nindex; | |
1577 | ||
bc7fd8cd LP |
1578 | /* location of array size might have changed so let's readjust a */ |
1579 | if (o == m->body_end) | |
1580 | a = adjust_pointer(a, op, os, m->body_end->data); | |
de1c301e | 1581 | |
bc7fd8cd | 1582 | *(uint32_t*) a = 0; |
de1c301e LP |
1583 | *array_size = a; |
1584 | return 0; | |
1585 | } | |
1586 | ||
1587 | static int bus_message_open_variant( | |
1588 | sd_bus_message *m, | |
1589 | struct bus_container *c, | |
1590 | const char *contents) { | |
1591 | ||
9a17484d | 1592 | size_t l; |
de1c301e LP |
1593 | void *a; |
1594 | ||
1595 | assert(m); | |
1596 | assert(c); | |
1597 | assert(contents); | |
1598 | ||
29ddb38f | 1599 | if (!signature_is_single(contents, false)) |
de1c301e LP |
1600 | return -EINVAL; |
1601 | ||
1602 | if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN) | |
1603 | return -EINVAL; | |
1604 | ||
1605 | if (c->signature && c->signature[c->index]) { | |
1606 | ||
1607 | if (c->signature[c->index] != SD_BUS_TYPE_VARIANT) | |
9a17484d | 1608 | return -ENXIO; |
de1c301e LP |
1609 | |
1610 | } else { | |
5cbe7492 LP |
1611 | char *e; |
1612 | ||
de1c301e | 1613 | if (c->enclosing != 0) |
9a17484d | 1614 | return -ENXIO; |
de1c301e LP |
1615 | |
1616 | e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL); | |
bc7fd8cd LP |
1617 | if (!e) { |
1618 | m->poisoned = true; | |
de1c301e | 1619 | return -ENOMEM; |
bc7fd8cd | 1620 | } |
de1c301e LP |
1621 | } |
1622 | ||
de1c301e LP |
1623 | l = strlen(contents); |
1624 | a = message_extend_body(m, 1, 1 + l + 1); | |
bc7fd8cd | 1625 | if (!a) |
de1c301e | 1626 | return -ENOMEM; |
de1c301e LP |
1627 | |
1628 | *(uint8_t*) a = l; | |
1629 | memcpy((uint8_t*) a + 1, contents, l + 1); | |
1630 | ||
1631 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
9a17484d | 1632 | c->index++; |
de1c301e LP |
1633 | |
1634 | return 0; | |
1635 | } | |
1636 | ||
1637 | static int bus_message_open_struct( | |
1638 | sd_bus_message *m, | |
1639 | struct bus_container *c, | |
1640 | const char *contents) { | |
1641 | ||
1642 | size_t nindex; | |
de1c301e LP |
1643 | |
1644 | assert(m); | |
1645 | assert(c); | |
1646 | assert(contents); | |
1647 | ||
1648 | if (!signature_is_valid(contents, false)) | |
1649 | return -EINVAL; | |
1650 | ||
1651 | if (c->signature && c->signature[c->index]) { | |
1652 | size_t l; | |
1653 | ||
1654 | l = strlen(contents); | |
1655 | ||
1656 | if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN || | |
1657 | !startswith(c->signature + c->index + 1, contents) || | |
1658 | c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END) | |
9a17484d | 1659 | return -ENXIO; |
de1c301e LP |
1660 | |
1661 | nindex = c->index + 1 + l + 1; | |
1662 | } else { | |
5cbe7492 LP |
1663 | char *e; |
1664 | ||
de1c301e | 1665 | if (c->enclosing != 0) |
9a17484d | 1666 | return -ENXIO; |
de1c301e LP |
1667 | |
1668 | e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL); | |
bc7fd8cd LP |
1669 | if (!e) { |
1670 | m->poisoned = true; | |
de1c301e | 1671 | return -ENOMEM; |
bc7fd8cd | 1672 | } |
de1c301e LP |
1673 | |
1674 | nindex = e - c->signature; | |
1675 | } | |
1676 | ||
1677 | /* Align contents to 8 byte boundary */ | |
bc7fd8cd | 1678 | if (!message_extend_body(m, 8, 0)) |
de1c301e | 1679 | return -ENOMEM; |
de1c301e LP |
1680 | |
1681 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
1682 | c->index = nindex; | |
1683 | ||
1684 | return 0; | |
1685 | } | |
1686 | ||
1687 | static int bus_message_open_dict_entry( | |
1688 | sd_bus_message *m, | |
1689 | struct bus_container *c, | |
1690 | const char *contents) { | |
1691 | ||
1692 | size_t nindex; | |
1693 | ||
1694 | assert(m); | |
1695 | assert(c); | |
1696 | assert(contents); | |
1697 | ||
1698 | if (!signature_is_pair(contents)) | |
1699 | return -EINVAL; | |
1700 | ||
1701 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
9a17484d | 1702 | return -ENXIO; |
de1c301e LP |
1703 | |
1704 | if (c->signature && c->signature[c->index]) { | |
1705 | size_t l; | |
1706 | ||
1707 | l = strlen(contents); | |
1708 | ||
1709 | if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN || | |
1710 | !startswith(c->signature + c->index + 1, contents) || | |
1711 | c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END) | |
9a17484d | 1712 | return -ENXIO; |
de1c301e LP |
1713 | |
1714 | nindex = c->index + 1 + l + 1; | |
1715 | } else | |
9a17484d | 1716 | return -ENXIO; |
de1c301e LP |
1717 | |
1718 | /* Align contents to 8 byte boundary */ | |
1719 | if (!message_extend_body(m, 8, 0)) | |
1720 | return -ENOMEM; | |
1721 | ||
1722 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
1723 | c->index = nindex; | |
1724 | ||
1725 | return 0; | |
1726 | } | |
1727 | ||
1728 | int sd_bus_message_open_container( | |
1729 | sd_bus_message *m, | |
1730 | char type, | |
1731 | const char *contents) { | |
1732 | ||
9a17484d | 1733 | struct bus_container *c, *w; |
de1c301e | 1734 | uint32_t *array_size = NULL; |
9a17484d | 1735 | char *signature; |
b3af9646 | 1736 | size_t before; |
de1c301e LP |
1737 | int r; |
1738 | ||
9d6c7c82 LP |
1739 | assert_return(m, -EINVAL); |
1740 | assert_return(!m->sealed, -EPERM); | |
1741 | assert_return(contents, -EINVAL); | |
1742 | assert_return(!m->poisoned, -ESTALE); | |
de1c301e LP |
1743 | |
1744 | /* Make sure we have space for one more container */ | |
9a17484d | 1745 | w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1)); |
bc7fd8cd LP |
1746 | if (!w) { |
1747 | m->poisoned = true; | |
de1c301e | 1748 | return -ENOMEM; |
bc7fd8cd LP |
1749 | } |
1750 | ||
9a17484d | 1751 | m->containers = w; |
de1c301e LP |
1752 | |
1753 | c = message_get_container(m); | |
1754 | ||
1755 | signature = strdup(contents); | |
bc7fd8cd LP |
1756 | if (!signature) { |
1757 | m->poisoned = true; | |
de1c301e | 1758 | return -ENOMEM; |
bc7fd8cd | 1759 | } |
de1c301e | 1760 | |
b3af9646 LP |
1761 | /* Save old index in the parent container, in case we have to |
1762 | * abort this container */ | |
1763 | c->saved_index = c->index; | |
1764 | before = m->header->body_size; | |
1765 | ||
de1c301e LP |
1766 | if (type == SD_BUS_TYPE_ARRAY) |
1767 | r = bus_message_open_array(m, c, contents, &array_size); | |
1768 | else if (type == SD_BUS_TYPE_VARIANT) | |
1769 | r = bus_message_open_variant(m, c, contents); | |
1770 | else if (type == SD_BUS_TYPE_STRUCT) | |
1771 | r = bus_message_open_struct(m, c, contents); | |
1772 | else if (type == SD_BUS_TYPE_DICT_ENTRY) | |
1773 | r = bus_message_open_dict_entry(m, c, contents); | |
1774 | else | |
1775 | r = -EINVAL; | |
1776 | ||
1777 | if (r < 0) { | |
1778 | free(signature); | |
1779 | return r; | |
1780 | } | |
1781 | ||
1782 | /* OK, let's fill it in */ | |
9a17484d LP |
1783 | w += m->n_containers++; |
1784 | w->enclosing = type; | |
1785 | w->signature = signature; | |
1786 | w->index = 0; | |
1787 | w->array_size = array_size; | |
b3af9646 LP |
1788 | w->before = before; |
1789 | w->begin = m->rindex; | |
de1c301e LP |
1790 | |
1791 | return 0; | |
1792 | } | |
1793 | ||
1794 | int sd_bus_message_close_container(sd_bus_message *m) { | |
1795 | struct bus_container *c; | |
1796 | ||
9d6c7c82 LP |
1797 | assert_return(m, -EINVAL); |
1798 | assert_return(!m->sealed, -EPERM); | |
1799 | assert_return(m->n_containers > 0, -EINVAL); | |
1800 | assert_return(!m->poisoned, -ESTALE); | |
de1c301e LP |
1801 | |
1802 | c = message_get_container(m); | |
de1c301e | 1803 | if (c->enclosing != SD_BUS_TYPE_ARRAY) |
9a17484d | 1804 | if (c->signature && c->signature[c->index] != 0) |
de1c301e LP |
1805 | return -EINVAL; |
1806 | ||
1807 | free(c->signature); | |
1808 | m->n_containers--; | |
1809 | ||
1810 | return 0; | |
1811 | } | |
1812 | ||
1b492614 LP |
1813 | typedef struct { |
1814 | const char *types; | |
1815 | unsigned n_struct; | |
1816 | unsigned n_array; | |
1817 | } TypeStack; | |
1818 | ||
1819 | static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) { | |
1820 | assert(stack); | |
1821 | assert(max > 0); | |
1822 | ||
1823 | if (*i >= max) | |
1824 | return -EINVAL; | |
1825 | ||
1826 | stack[*i].types = types; | |
1827 | stack[*i].n_struct = n_struct; | |
1828 | stack[*i].n_array = n_array; | |
1829 | (*i)++; | |
1830 | ||
1831 | return 0; | |
1832 | } | |
1833 | ||
1834 | static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) { | |
1835 | assert(stack); | |
1836 | assert(max > 0); | |
1837 | assert(types); | |
1838 | assert(n_struct); | |
1839 | assert(n_array); | |
1840 | ||
1841 | if (*i <= 0) | |
1842 | return 0; | |
1843 | ||
1844 | (*i)--; | |
1845 | *types = stack[*i].types; | |
1846 | *n_struct = stack[*i].n_struct; | |
1847 | *n_array = stack[*i].n_array; | |
1848 | ||
1849 | return 1; | |
1850 | } | |
1851 | ||
917b5dc7 | 1852 | int bus_message_append_ap( |
de1c301e LP |
1853 | sd_bus_message *m, |
1854 | const char *types, | |
1855 | va_list ap) { | |
1856 | ||
1b492614 LP |
1857 | unsigned n_array, n_struct; |
1858 | TypeStack stack[BUS_CONTAINER_DEPTH]; | |
1859 | unsigned stack_ptr = 0; | |
de1c301e LP |
1860 | int r; |
1861 | ||
1862 | assert(m); | |
88d331d5 LP |
1863 | |
1864 | if (!types) | |
1865 | return 0; | |
de1c301e | 1866 | |
1b492614 LP |
1867 | n_array = (unsigned) -1; |
1868 | n_struct = strlen(types); | |
1869 | ||
1870 | for (;;) { | |
1871 | const char *t; | |
1872 | ||
1873 | if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) { | |
1874 | r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array); | |
1875 | if (r < 0) | |
1876 | return r; | |
1877 | if (r == 0) | |
1878 | break; | |
1879 | ||
1880 | r = sd_bus_message_close_container(m); | |
1881 | if (r < 0) | |
1882 | return r; | |
1883 | ||
1884 | continue; | |
1885 | } | |
1886 | ||
1887 | t = types; | |
1888 | if (n_array != (unsigned) -1) | |
1889 | n_array --; | |
1890 | else { | |
1891 | types ++; | |
1892 | n_struct--; | |
1893 | } | |
1894 | ||
de1c301e LP |
1895 | switch (*t) { |
1896 | ||
1897 | case SD_BUS_TYPE_BYTE: { | |
1898 | uint8_t x; | |
1899 | ||
1900 | x = (uint8_t) va_arg(ap, int); | |
1901 | r = sd_bus_message_append_basic(m, *t, &x); | |
1902 | break; | |
1903 | } | |
1904 | ||
1905 | case SD_BUS_TYPE_BOOLEAN: | |
1906 | case SD_BUS_TYPE_INT32: | |
9a17484d LP |
1907 | case SD_BUS_TYPE_UINT32: |
1908 | case SD_BUS_TYPE_UNIX_FD: { | |
de1c301e LP |
1909 | uint32_t x; |
1910 | ||
9a17484d LP |
1911 | /* We assume a boolean is the same as int32_t */ |
1912 | assert_cc(sizeof(int32_t) == sizeof(int)); | |
1913 | ||
de1c301e LP |
1914 | x = va_arg(ap, uint32_t); |
1915 | r = sd_bus_message_append_basic(m, *t, &x); | |
1916 | break; | |
1917 | } | |
1918 | ||
1919 | case SD_BUS_TYPE_INT16: | |
1920 | case SD_BUS_TYPE_UINT16: { | |
1921 | uint16_t x; | |
1922 | ||
1923 | x = (uint16_t) va_arg(ap, int); | |
1924 | r = sd_bus_message_append_basic(m, *t, &x); | |
1925 | break; | |
1926 | } | |
1927 | ||
1928 | case SD_BUS_TYPE_INT64: | |
1929 | case SD_BUS_TYPE_UINT64: | |
1930 | case SD_BUS_TYPE_DOUBLE: { | |
1931 | uint64_t x; | |
1932 | ||
1933 | x = va_arg(ap, uint64_t); | |
1934 | r = sd_bus_message_append_basic(m, *t, &x); | |
1935 | break; | |
1936 | } | |
1937 | ||
1938 | case SD_BUS_TYPE_STRING: | |
1939 | case SD_BUS_TYPE_OBJECT_PATH: | |
1940 | case SD_BUS_TYPE_SIGNATURE: { | |
1941 | const char *x; | |
1942 | ||
1943 | x = va_arg(ap, const char*); | |
1944 | r = sd_bus_message_append_basic(m, *t, x); | |
1945 | break; | |
1946 | } | |
1947 | ||
de1c301e | 1948 | case SD_BUS_TYPE_ARRAY: { |
de1c301e LP |
1949 | size_t k; |
1950 | ||
1951 | r = signature_element_length(t + 1, &k); | |
1952 | if (r < 0) | |
1953 | return r; | |
1954 | ||
1955 | { | |
1956 | char s[k + 1]; | |
de1c301e LP |
1957 | memcpy(s, t + 1, k); |
1958 | s[k] = 0; | |
de1c301e LP |
1959 | |
1960 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s); | |
1961 | if (r < 0) | |
1962 | return r; | |
1b492614 | 1963 | } |
de1c301e | 1964 | |
1b492614 LP |
1965 | if (n_array == (unsigned) -1) { |
1966 | types += k; | |
1967 | n_struct -= k; | |
de1c301e LP |
1968 | } |
1969 | ||
1b492614 LP |
1970 | r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
1971 | if (r < 0) | |
1972 | return r; | |
1973 | ||
1974 | types = t + 1; | |
1975 | n_struct = k; | |
1976 | n_array = va_arg(ap, unsigned); | |
1977 | ||
de1c301e LP |
1978 | break; |
1979 | } | |
1980 | ||
1981 | case SD_BUS_TYPE_VARIANT: { | |
1982 | const char *s; | |
1983 | ||
1984 | s = va_arg(ap, const char*); | |
1985 | if (!s) | |
1986 | return -EINVAL; | |
1987 | ||
1988 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s); | |
1989 | if (r < 0) | |
1990 | return r; | |
1991 | ||
1b492614 | 1992 | r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
de1c301e LP |
1993 | if (r < 0) |
1994 | return r; | |
1995 | ||
1b492614 LP |
1996 | types = s; |
1997 | n_struct = strlen(s); | |
1998 | n_array = (unsigned) -1; | |
1999 | ||
de1c301e LP |
2000 | break; |
2001 | } | |
2002 | ||
2003 | case SD_BUS_TYPE_STRUCT_BEGIN: | |
2004 | case SD_BUS_TYPE_DICT_ENTRY_BEGIN: { | |
2005 | size_t k; | |
2006 | ||
2007 | r = signature_element_length(t, &k); | |
2008 | if (r < 0) | |
2009 | return r; | |
2010 | ||
2011 | { | |
2012 | char s[k - 1]; | |
2013 | ||
2014 | memcpy(s, t + 1, k - 2); | |
2015 | s[k - 2] = 0; | |
2016 | ||
2017 | r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s); | |
2018 | if (r < 0) | |
2019 | return r; | |
1b492614 | 2020 | } |
de1c301e | 2021 | |
1b492614 LP |
2022 | if (n_array == (unsigned) -1) { |
2023 | types += k - 1; | |
2024 | n_struct -= k - 1; | |
2025 | } | |
de1c301e | 2026 | |
1b492614 LP |
2027 | r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
2028 | if (r < 0) | |
2029 | return r; | |
de1c301e | 2030 | |
1b492614 LP |
2031 | types = t + 1; |
2032 | n_struct = k - 2; | |
2033 | n_array = (unsigned) -1; | |
de1c301e LP |
2034 | |
2035 | break; | |
2036 | } | |
2037 | ||
2038 | default: | |
2039 | r = -EINVAL; | |
2040 | } | |
2041 | ||
2042 | if (r < 0) | |
2043 | return r; | |
2044 | } | |
2045 | ||
2046 | return 0; | |
2047 | } | |
2048 | ||
2049 | int sd_bus_message_append(sd_bus_message *m, const char *types, ...) { | |
2050 | va_list ap; | |
2051 | int r; | |
2052 | ||
9d6c7c82 LP |
2053 | assert_return(m, -EINVAL); |
2054 | assert_return(types, -EINVAL); | |
2055 | assert_return(!m->sealed, -EPERM); | |
2056 | assert_return(!m->poisoned, -ESTALE); | |
de1c301e LP |
2057 | |
2058 | va_start(ap, types); | |
917b5dc7 | 2059 | r = bus_message_append_ap(m, types, ap); |
de1c301e LP |
2060 | va_end(ap); |
2061 | ||
2062 | return r; | |
2063 | } | |
2064 | ||
f8e013f8 | 2065 | int sd_bus_message_append_array_space(sd_bus_message *m, char type, size_t size, void **ptr) { |
b3af9646 LP |
2066 | ssize_t align, sz; |
2067 | void *a; | |
2068 | int r; | |
2069 | ||
9d6c7c82 LP |
2070 | assert_return(m, -EINVAL); |
2071 | assert_return(!m->sealed, -EPERM); | |
2072 | assert_return(bus_type_is_trivial(type), -EINVAL); | |
2073 | assert_return(ptr || size == 0, -EINVAL); | |
2074 | assert_return(!m->poisoned, -ESTALE); | |
b3af9646 LP |
2075 | |
2076 | align = bus_type_get_alignment(type); | |
2077 | sz = bus_type_get_size(type); | |
2078 | ||
2079 | assert_se(align > 0); | |
2080 | assert_se(sz > 0); | |
2081 | ||
2082 | if (size % sz != 0) | |
2083 | return -EINVAL; | |
2084 | ||
2085 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type)); | |
2086 | if (r < 0) | |
2087 | return r; | |
2088 | ||
2089 | a = message_extend_body(m, align, size); | |
bc7fd8cd LP |
2090 | if (!a) |
2091 | return -ENOMEM; | |
b3af9646 LP |
2092 | |
2093 | r = sd_bus_message_close_container(m); | |
2094 | if (r < 0) | |
bc7fd8cd | 2095 | return r; |
b3af9646 LP |
2096 | |
2097 | *ptr = a; | |
2098 | return 0; | |
b3af9646 LP |
2099 | } |
2100 | ||
2101 | int sd_bus_message_append_array(sd_bus_message *m, char type, const void *ptr, size_t size) { | |
2102 | int r; | |
2103 | void *p; | |
2104 | ||
9d6c7c82 LP |
2105 | assert_return(m, -EINVAL); |
2106 | assert_return(!m->sealed, -EPERM); | |
2107 | assert_return(bus_type_is_trivial(type), -EINVAL); | |
2108 | assert_return(ptr || size == 0, -EINVAL); | |
2109 | assert_return(!m->poisoned, -ESTALE); | |
b3af9646 | 2110 | |
f8e013f8 | 2111 | r = sd_bus_message_append_array_space(m, type, size, &p); |
b3af9646 LP |
2112 | if (r < 0) |
2113 | return r; | |
2114 | ||
2115 | if (size > 0) | |
2116 | memcpy(p, ptr, size); | |
2117 | ||
2118 | return 0; | |
2119 | } | |
2120 | ||
453a0c29 LP |
2121 | int sd_bus_message_append_array_memfd(sd_bus_message *m, char type, sd_memfd *memfd) { |
2122 | _cleanup_close_ int copy_fd = -1; | |
2123 | struct bus_body_part *part; | |
2124 | ssize_t align, sz; | |
2125 | uint64_t size; | |
2126 | void *a; | |
2127 | int r; | |
2128 | ||
2129 | if (!m) | |
2130 | return -EINVAL; | |
2131 | if (!memfd) | |
2132 | return -EINVAL; | |
2133 | if (m->sealed) | |
2134 | return -EPERM; | |
2135 | if (!bus_type_is_trivial(type)) | |
2136 | return -EINVAL; | |
2137 | if (m->poisoned) | |
2138 | return -ESTALE; | |
2139 | ||
2140 | r = sd_memfd_set_sealed(memfd, true); | |
2141 | if (r < 0) | |
2142 | return r; | |
2143 | ||
2144 | copy_fd = sd_memfd_dup_fd(memfd); | |
2145 | if (copy_fd < 0) | |
2146 | return copy_fd; | |
2147 | ||
2148 | r = sd_memfd_get_size(memfd, &size); | |
2149 | if (r < 0) | |
2150 | return r; | |
2151 | ||
2152 | align = bus_type_get_alignment(type); | |
2153 | sz = bus_type_get_size(type); | |
2154 | ||
2155 | assert_se(align > 0); | |
2156 | assert_se(sz > 0); | |
2157 | ||
2158 | if (size % sz != 0) | |
2159 | return -EINVAL; | |
2160 | ||
5cbe7492 | 2161 | if (size > (uint64_t) (uint32_t) -1) |
453a0c29 LP |
2162 | return -EINVAL; |
2163 | ||
2164 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type)); | |
2165 | if (r < 0) | |
2166 | return r; | |
2167 | ||
2168 | a = message_extend_body(m, align, 0); | |
2169 | if (!a) | |
2170 | return -ENOMEM; | |
2171 | ||
2172 | part = message_append_part(m); | |
2173 | if (!part) | |
2174 | return -ENOMEM; | |
2175 | ||
2176 | part->memfd = copy_fd; | |
2177 | part->sealed = true; | |
2178 | part->size = size; | |
2179 | copy_fd = -1; | |
2180 | ||
2181 | message_extend_containers(m, size); | |
2182 | m->header->body_size += size; | |
2183 | ||
2184 | return sd_bus_message_close_container(m); | |
2185 | } | |
2186 | ||
5cbe7492 LP |
2187 | int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) { |
2188 | _cleanup_close_ int copy_fd = -1; | |
2189 | struct bus_body_part *part; | |
2190 | struct bus_container *c; | |
2191 | uint64_t size; | |
2192 | void *a; | |
2193 | int r; | |
2194 | ||
55736ed0 LP |
2195 | assert_return(m, -EINVAL); |
2196 | assert_return(memfd, -EINVAL); | |
2197 | assert_return(!m->sealed, -EPERM); | |
2198 | assert_return(!m->poisoned, -ESTALE); | |
5cbe7492 LP |
2199 | |
2200 | r = sd_memfd_set_sealed(memfd, true); | |
2201 | if (r < 0) | |
2202 | return r; | |
2203 | ||
2204 | copy_fd = sd_memfd_dup_fd(memfd); | |
2205 | if (copy_fd < 0) | |
2206 | return copy_fd; | |
2207 | ||
2208 | r = sd_memfd_get_size(memfd, &size); | |
2209 | if (r < 0) | |
2210 | return r; | |
2211 | ||
2212 | /* We require this to be NUL terminated */ | |
2213 | if (size == 0) | |
2214 | return -EINVAL; | |
2215 | ||
2216 | if (size > (uint64_t) (uint32_t) -1) | |
2217 | return -EINVAL; | |
2218 | ||
2219 | c = message_get_container(m); | |
2220 | if (c->signature && c->signature[c->index]) { | |
2221 | /* Container signature is already set */ | |
2222 | ||
2223 | if (c->signature[c->index] != SD_BUS_TYPE_STRING) | |
2224 | return -ENXIO; | |
2225 | } else { | |
2226 | char *e; | |
2227 | ||
2228 | /* Maybe we can append to the signature? But only if this is the top-level container*/ | |
2229 | if (c->enclosing != 0) | |
2230 | return -ENXIO; | |
2231 | ||
2232 | e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL); | |
2233 | if (!e) { | |
2234 | m->poisoned = true; | |
2235 | return -ENOMEM; | |
2236 | } | |
2237 | } | |
2238 | ||
2239 | a = message_extend_body(m, 4, 4); | |
2240 | if (!a) | |
2241 | return -ENOMEM; | |
2242 | ||
2243 | *(uint32_t*) a = size - 1; | |
2244 | ||
2245 | part = message_append_part(m); | |
2246 | if (!part) | |
2247 | return -ENOMEM; | |
2248 | ||
2249 | part->memfd = copy_fd; | |
2250 | part->sealed = true; | |
2251 | part->size = size; | |
2252 | copy_fd = -1; | |
2253 | ||
2254 | message_extend_containers(m, size); | |
2255 | m->header->body_size += size; | |
2256 | ||
2257 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
2258 | c->index++; | |
2259 | ||
2260 | return 0; | |
2261 | } | |
2262 | ||
55736ed0 LP |
2263 | int sd_bus_message_append_strv(sd_bus_message *m, char **l) { |
2264 | char **i; | |
2265 | int r; | |
2266 | ||
2267 | assert_return(m, -EINVAL); | |
2268 | assert_return(!m->sealed, -EPERM); | |
2269 | assert_return(!m->poisoned, -ESTALE); | |
2270 | ||
2271 | r = sd_bus_message_open_container(m, 'a', "s"); | |
2272 | if (r < 0) | |
2273 | return r; | |
2274 | ||
2275 | STRV_FOREACH(i, l) { | |
2276 | r = sd_bus_message_append_basic(m, 's', *i); | |
2277 | if (r < 0) | |
2278 | return r; | |
2279 | } | |
2280 | ||
2281 | return sd_bus_message_close_container(m); | |
2282 | } | |
2283 | ||
a392d361 | 2284 | int bus_body_part_map(struct bus_body_part *part) { |
453a0c29 LP |
2285 | void *p; |
2286 | size_t psz; | |
2287 | ||
2288 | assert_se(part); | |
2289 | ||
2290 | if (part->data) | |
2291 | return 0; | |
2292 | ||
2293 | if (part->size <= 0) | |
2294 | return 0; | |
2295 | ||
1307c3ff LP |
2296 | /* For smaller zero parts (as used for padding) we don't need to map anything... */ |
2297 | if (part->memfd < 0 && part->is_zero && part->size < 8) { | |
2298 | static const uint8_t zeroes[7] = { }; | |
2299 | part->data = (void*) zeroes; | |
2300 | return 0; | |
2301 | } | |
2302 | ||
453a0c29 LP |
2303 | psz = PAGE_ALIGN(part->size); |
2304 | ||
2305 | if (part->memfd >= 0) | |
2306 | p = mmap(NULL, psz, PROT_READ, MAP_SHARED, part->memfd, 0); | |
2307 | else if (part->is_zero) | |
2308 | p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | |
2309 | else | |
2310 | return -EINVAL; | |
2311 | ||
2312 | if (p == MAP_FAILED) | |
2313 | return -errno; | |
2314 | ||
2315 | part->mapped = psz; | |
2316 | part->data = p; | |
a392d361 LP |
2317 | part->munmap_this = true; |
2318 | ||
453a0c29 LP |
2319 | return 0; |
2320 | } | |
2321 | ||
a392d361 LP |
2322 | void bus_body_part_unmap(struct bus_body_part *part) { |
2323 | ||
2324 | assert_se(part); | |
2325 | ||
2326 | if (part->memfd < 0) | |
2327 | return; | |
2328 | ||
a392d361 LP |
2329 | if (!part->data) |
2330 | return; | |
2331 | ||
bf30e48f KS |
2332 | if (!part->munmap_this) |
2333 | return; | |
a392d361 LP |
2334 | |
2335 | assert_se(munmap(part->data, part->mapped) == 0); | |
2336 | ||
2337 | part->data = NULL; | |
2338 | part->mapped = 0; | |
2339 | part->munmap_this = false; | |
2340 | ||
2341 | return; | |
2342 | } | |
2343 | ||
9a17484d | 2344 | static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) { |
bc7fd8cd | 2345 | size_t k, start, end; |
de1c301e | 2346 | |
9a17484d LP |
2347 | assert(rindex); |
2348 | assert(align > 0); | |
de1c301e | 2349 | |
9a17484d | 2350 | start = ALIGN_TO((size_t) *rindex, align); |
bc7fd8cd | 2351 | end = start + nbytes; |
9a17484d | 2352 | |
bc7fd8cd | 2353 | if (end > sz) |
9a17484d LP |
2354 | return -EBADMSG; |
2355 | ||
2356 | /* Verify that padding is 0 */ | |
2357 | for (k = *rindex; k < start; k++) | |
2358 | if (((const uint8_t*) p)[k] != 0) | |
2359 | return -EBADMSG; | |
2360 | ||
2361 | if (r) | |
2362 | *r = (uint8_t*) p + start; | |
2363 | ||
bc7fd8cd | 2364 | *rindex = end; |
9a17484d LP |
2365 | |
2366 | return 1; | |
de1c301e LP |
2367 | } |
2368 | ||
7b058942 LP |
2369 | static bool message_end_of_signature(sd_bus_message *m) { |
2370 | struct bus_container *c; | |
2371 | ||
2372 | assert(m); | |
2373 | ||
2374 | c = message_get_container(m); | |
2375 | return !c->signature || c->signature[c->index] == 0; | |
2376 | } | |
2377 | ||
9a17484d LP |
2378 | static bool message_end_of_array(sd_bus_message *m, size_t index) { |
2379 | struct bus_container *c; | |
de1c301e | 2380 | |
9a17484d | 2381 | assert(m); |
de1c301e | 2382 | |
9a17484d LP |
2383 | c = message_get_container(m); |
2384 | if (!c->array_size) | |
2385 | return false; | |
de1c301e | 2386 | |
9a17484d | 2387 | return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size); |
de1c301e LP |
2388 | } |
2389 | ||
7b058942 LP |
2390 | int sd_bus_message_at_end(sd_bus_message *m, int complete) { |
2391 | assert_return(m, -EINVAL); | |
2392 | assert_return(m->sealed, -EPERM); | |
2393 | ||
2394 | if (complete && m->n_containers > 0) | |
2395 | return false; | |
2396 | ||
2397 | if (message_end_of_signature(m)) | |
2398 | return true; | |
2399 | ||
2400 | if (message_end_of_array(m, m->rindex)) | |
2401 | return true; | |
2402 | ||
2403 | return false; | |
2404 | } | |
2405 | ||
bc7fd8cd LP |
2406 | static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) { |
2407 | struct bus_body_part *part; | |
2408 | size_t begin; | |
453a0c29 LP |
2409 | int r; |
2410 | ||
bc7fd8cd LP |
2411 | assert(m); |
2412 | ||
2413 | if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) { | |
2414 | part = m->cached_rindex_part; | |
2415 | begin = m->cached_rindex_part_begin; | |
2416 | } else { | |
2417 | part = &m->body; | |
2418 | begin = 0; | |
2419 | } | |
2420 | ||
2421 | while (part) { | |
2422 | if (index < begin) | |
2423 | return NULL; | |
2424 | ||
2425 | if (index + sz <= begin + part->size) { | |
453a0c29 | 2426 | |
a392d361 | 2427 | r = bus_body_part_map(part); |
453a0c29 LP |
2428 | if (r < 0) |
2429 | return NULL; | |
2430 | ||
bc7fd8cd | 2431 | if (p) |
453a0c29 | 2432 | *p = (uint8_t*) part->data + index - begin; |
bc7fd8cd LP |
2433 | |
2434 | m->cached_rindex_part = part; | |
2435 | m->cached_rindex_part_begin = begin; | |
2436 | ||
2437 | return part; | |
2438 | } | |
2439 | ||
453a0c29 | 2440 | begin += part->size; |
bc7fd8cd LP |
2441 | part = part->next; |
2442 | } | |
2443 | ||
2444 | return NULL; | |
2445 | } | |
2446 | ||
2447 | static int message_peek_body( | |
2448 | sd_bus_message *m, | |
2449 | size_t *rindex, | |
2450 | size_t align, | |
2451 | size_t nbytes, | |
2452 | void **ret) { | |
2453 | ||
2454 | size_t k, start, end, padding; | |
2455 | struct bus_body_part *part; | |
2456 | uint8_t *q; | |
2457 | ||
de1c301e | 2458 | assert(m); |
9a17484d LP |
2459 | assert(rindex); |
2460 | assert(align > 0); | |
de1c301e | 2461 | |
9a17484d LP |
2462 | if (message_end_of_array(m, *rindex)) |
2463 | return 0; | |
de1c301e | 2464 | |
bc7fd8cd LP |
2465 | start = ALIGN_TO((size_t) *rindex, align); |
2466 | padding = start - *rindex; | |
2467 | end = start + nbytes; | |
2468 | ||
2469 | if (end > BUS_MESSAGE_BODY_SIZE(m)) | |
2470 | return -EBADMSG; | |
2471 | ||
2472 | part = find_part(m, *rindex, padding, (void**) &q); | |
2473 | if (!part) | |
2474 | return -EBADMSG; | |
2475 | ||
2476 | if (q) { | |
2477 | /* Verify padding */ | |
2478 | for (k = 0; k < padding; k++) | |
2479 | if (q[k] != 0) | |
2480 | return -EBADMSG; | |
2481 | } | |
2482 | ||
2483 | part = find_part(m, start, nbytes, (void**) &q); | |
2484 | if (!part || !q) | |
2485 | return -EBADMSG; | |
2486 | ||
2487 | *rindex = end; | |
2488 | ||
2489 | if (ret) | |
2490 | *ret = q; | |
2491 | ||
2492 | return 1; | |
9a17484d | 2493 | } |
de1c301e | 2494 | |
ac89bf1d | 2495 | static bool validate_nul(const char *s, size_t l) { |
de1c301e | 2496 | |
9a17484d LP |
2497 | /* Check for NUL chars in the string */ |
2498 | if (memchr(s, 0, l)) | |
2499 | return false; | |
de1c301e | 2500 | |
9a17484d LP |
2501 | /* Check for NUL termination */ |
2502 | if (s[l] != 0) | |
2503 | return false; | |
de1c301e | 2504 | |
ac89bf1d LP |
2505 | return true; |
2506 | } | |
2507 | ||
2508 | static bool validate_string(const char *s, size_t l) { | |
2509 | ||
2510 | if (!validate_nul(s, l)) | |
2511 | return false; | |
2512 | ||
9a17484d LP |
2513 | /* Check if valid UTF8 */ |
2514 | if (!utf8_is_valid(s)) | |
2515 | return false; | |
2516 | ||
2517 | return true; | |
de1c301e LP |
2518 | } |
2519 | ||
9a17484d | 2520 | static bool validate_signature(const char *s, size_t l) { |
de1c301e | 2521 | |
ac89bf1d | 2522 | if (!validate_nul(s, l)) |
9a17484d | 2523 | return false; |
de1c301e | 2524 | |
9a17484d LP |
2525 | /* Check if valid signature */ |
2526 | if (!signature_is_valid(s, true)) | |
2527 | return false; | |
2528 | ||
2529 | return true; | |
2530 | } | |
2531 | ||
ac89bf1d LP |
2532 | static bool validate_object_path(const char *s, size_t l) { |
2533 | ||
2534 | if (!validate_nul(s, l)) | |
2535 | return false; | |
2536 | ||
2537 | if (!object_path_is_valid(s)) | |
2538 | return false; | |
2539 | ||
2540 | return true; | |
2541 | } | |
2542 | ||
9a17484d LP |
2543 | int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) { |
2544 | struct bus_container *c; | |
9a17484d | 2545 | void *q; |
0dcd14b9 | 2546 | int r; |
9a17484d | 2547 | |
9d6c7c82 LP |
2548 | assert_return(m, -EINVAL); |
2549 | assert_return(m->sealed, -EPERM); | |
2550 | assert_return(bus_type_is_basic(type), -EINVAL); | |
de1c301e | 2551 | |
7b058942 | 2552 | if (message_end_of_signature(m)) |
430fb8fa | 2553 | return -ENXIO; |
9a17484d | 2554 | |
1daf8121 LP |
2555 | if (message_end_of_array(m, m->rindex)) |
2556 | return 0; | |
2557 | ||
7b058942 | 2558 | c = message_get_container(m); |
9a17484d LP |
2559 | if (c->signature[c->index] != type) |
2560 | return -ENXIO; | |
2561 | ||
2562 | switch (type) { | |
2563 | ||
2564 | case SD_BUS_TYPE_STRING: | |
2565 | case SD_BUS_TYPE_OBJECT_PATH: { | |
2566 | uint32_t l; | |
2567 | size_t rindex; | |
2568 | ||
2569 | rindex = m->rindex; | |
2570 | r = message_peek_body(m, &rindex, 4, 4, &q); | |
2571 | if (r <= 0) | |
2572 | return r; | |
2573 | ||
2574 | l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); | |
2575 | r = message_peek_body(m, &rindex, 1, l+1, &q); | |
de1c301e LP |
2576 | if (r < 0) |
2577 | return r; | |
9a17484d LP |
2578 | if (r == 0) |
2579 | return -EBADMSG; | |
2580 | ||
ac89bf1d LP |
2581 | if (type == SD_BUS_TYPE_OBJECT_PATH) { |
2582 | if (!validate_object_path(q, l)) | |
2583 | return -EBADMSG; | |
2584 | } else { | |
2585 | if (!validate_string(q, l)) | |
2586 | return -EBADMSG; | |
2587 | } | |
9a17484d LP |
2588 | |
2589 | m->rindex = rindex; | |
0dcd14b9 LP |
2590 | if (p) |
2591 | *(const char**) p = q; | |
2592 | ||
9a17484d | 2593 | break; |
de1c301e LP |
2594 | } |
2595 | ||
9a17484d LP |
2596 | case SD_BUS_TYPE_SIGNATURE: { |
2597 | uint8_t l; | |
2598 | size_t rindex; | |
2599 | ||
2600 | rindex = m->rindex; | |
2601 | r = message_peek_body(m, &rindex, 1, 1, &q); | |
2602 | if (r <= 0) | |
2603 | return r; | |
2604 | ||
2605 | l = *(uint8_t*) q; | |
2606 | r = message_peek_body(m, &rindex, 1, l+1, &q); | |
de1c301e LP |
2607 | if (r < 0) |
2608 | return r; | |
9a17484d LP |
2609 | if (r == 0) |
2610 | return -EBADMSG; | |
2611 | ||
2612 | if (!validate_signature(q, l)) | |
2613 | return -EBADMSG; | |
2614 | ||
2615 | m->rindex = rindex; | |
0dcd14b9 LP |
2616 | |
2617 | if (p) | |
2618 | *(const char**) p = q; | |
9a17484d | 2619 | break; |
de1c301e LP |
2620 | } |
2621 | ||
9a17484d | 2622 | default: { |
27f6e5c7 ZJS |
2623 | ssize_t sz, align; |
2624 | size_t rindex; | |
de1c301e | 2625 | |
9a17484d LP |
2626 | align = bus_type_get_alignment(type); |
2627 | sz = bus_type_get_size(type); | |
27f6e5c7 | 2628 | assert(align > 0 && sz > 0); |
de1c301e | 2629 | |
2c93b4ef LP |
2630 | rindex = m->rindex; |
2631 | r = message_peek_body(m, &rindex, align, sz, &q); | |
9a17484d LP |
2632 | if (r <= 0) |
2633 | return r; | |
2634 | ||
2635 | switch (type) { | |
2636 | ||
2637 | case SD_BUS_TYPE_BYTE: | |
0dcd14b9 LP |
2638 | if (p) |
2639 | *(uint8_t*) p = *(uint8_t*) q; | |
9a17484d LP |
2640 | break; |
2641 | ||
2642 | case SD_BUS_TYPE_BOOLEAN: | |
0dcd14b9 LP |
2643 | if (p) |
2644 | *(unsigned*) p = !!*(uint32_t*) q; | |
9a17484d LP |
2645 | break; |
2646 | ||
2647 | case SD_BUS_TYPE_INT16: | |
2648 | case SD_BUS_TYPE_UINT16: | |
0dcd14b9 LP |
2649 | if (p) |
2650 | *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q); | |
9a17484d LP |
2651 | break; |
2652 | ||
2653 | case SD_BUS_TYPE_INT32: | |
2654 | case SD_BUS_TYPE_UINT32: | |
0dcd14b9 LP |
2655 | if (p) |
2656 | *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); | |
9a17484d LP |
2657 | break; |
2658 | ||
2659 | case SD_BUS_TYPE_INT64: | |
2660 | case SD_BUS_TYPE_UINT64: | |
2661 | case SD_BUS_TYPE_DOUBLE: | |
0dcd14b9 LP |
2662 | if (p) |
2663 | *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q); | |
9a17484d LP |
2664 | break; |
2665 | ||
2c93b4ef | 2666 | case SD_BUS_TYPE_UNIX_FD: { |
2c93b4ef LP |
2667 | uint32_t j; |
2668 | ||
2669 | j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); | |
2670 | if (j >= m->n_fds) | |
2671 | return -EBADMSG; | |
2672 | ||
0dcd14b9 LP |
2673 | if (p) |
2674 | *(int*) p = m->fds[j]; | |
2c93b4ef LP |
2675 | break; |
2676 | } | |
2677 | ||
9a17484d LP |
2678 | default: |
2679 | assert_not_reached("Unknown basic type..."); | |
2680 | } | |
2681 | ||
b3af9646 | 2682 | m->rindex = rindex; |
2c93b4ef | 2683 | |
9a17484d LP |
2684 | break; |
2685 | } | |
2686 | } | |
2687 | ||
2688 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
2689 | c->index++; | |
2690 | ||
2691 | return 1; | |
de1c301e LP |
2692 | } |
2693 | ||
9a17484d LP |
2694 | static int bus_message_enter_array( |
2695 | sd_bus_message *m, | |
2696 | struct bus_container *c, | |
2697 | const char *contents, | |
2698 | uint32_t **array_size) { | |
2699 | ||
2700 | size_t rindex; | |
2701 | void *q; | |
2702 | int r, alignment; | |
2703 | ||
2704 | assert(m); | |
2705 | assert(c); | |
2706 | assert(contents); | |
2707 | assert(array_size); | |
2708 | ||
29ddb38f | 2709 | if (!signature_is_single(contents, true)) |
de1c301e | 2710 | return -EINVAL; |
9a17484d LP |
2711 | |
2712 | alignment = bus_type_get_alignment(contents[0]); | |
2713 | if (alignment < 0) | |
2714 | return alignment; | |
2715 | ||
2716 | if (!c->signature || c->signature[c->index] == 0) | |
2717 | return 0; | |
2718 | ||
2719 | if (c->signature[c->index] != SD_BUS_TYPE_ARRAY) | |
2720 | return -ENXIO; | |
2721 | ||
2722 | if (!startswith(c->signature + c->index + 1, contents)) | |
2723 | return -ENXIO; | |
2724 | ||
2725 | rindex = m->rindex; | |
2726 | r = message_peek_body(m, &rindex, 4, 4, &q); | |
2727 | if (r <= 0) | |
2728 | return r; | |
2729 | ||
ac89bf1d | 2730 | if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE) |
80a46c73 | 2731 | return -EBADMSG; |
9a17484d LP |
2732 | |
2733 | r = message_peek_body(m, &rindex, alignment, 0, NULL); | |
2734 | if (r < 0) | |
2735 | return r; | |
2736 | if (r == 0) | |
2737 | return -EBADMSG; | |
2738 | ||
2739 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
2740 | c->index += 1 + strlen(contents); | |
2741 | ||
2742 | m->rindex = rindex; | |
2743 | ||
2744 | *array_size = (uint32_t*) q; | |
2745 | ||
2746 | return 1; | |
2747 | } | |
2748 | ||
2749 | static int bus_message_enter_variant( | |
2750 | sd_bus_message *m, | |
2751 | struct bus_container *c, | |
2752 | const char *contents) { | |
2753 | ||
2754 | size_t rindex; | |
2755 | uint8_t l; | |
2756 | void *q; | |
2757 | int r; | |
2758 | ||
2759 | assert(m); | |
2760 | assert(c); | |
2761 | assert(contents); | |
2762 | ||
29ddb38f | 2763 | if (!signature_is_single(contents, false)) |
de1c301e | 2764 | return -EINVAL; |
de1c301e | 2765 | |
9a17484d LP |
2766 | if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN) |
2767 | return -EINVAL; | |
2768 | ||
2769 | if (!c->signature || c->signature[c->index] == 0) | |
2770 | return 0; | |
2771 | ||
2772 | if (c->signature[c->index] != SD_BUS_TYPE_VARIANT) | |
2773 | return -ENXIO; | |
2774 | ||
2775 | rindex = m->rindex; | |
2776 | r = message_peek_body(m, &rindex, 1, 1, &q); | |
2777 | if (r <= 0) | |
2778 | return r; | |
2779 | ||
2780 | l = *(uint8_t*) q; | |
2781 | r = message_peek_body(m, &rindex, 1, l+1, &q); | |
2782 | if (r < 0) | |
2783 | return r; | |
2784 | if (r == 0) | |
2785 | return -EBADMSG; | |
2786 | ||
2787 | if (!validate_signature(q, l)) | |
2788 | return -EBADMSG; | |
2789 | ||
2790 | if (!streq(q, contents)) | |
2791 | return -ENXIO; | |
2792 | ||
2793 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
2794 | c->index++; | |
2795 | ||
2796 | m->rindex = rindex; | |
2797 | ||
2798 | return 1; | |
de1c301e LP |
2799 | } |
2800 | ||
9a17484d LP |
2801 | static int bus_message_enter_struct( |
2802 | sd_bus_message *m, | |
2803 | struct bus_container *c, | |
2804 | const char *contents) { | |
2805 | ||
2806 | size_t l; | |
2807 | int r; | |
2808 | ||
2809 | assert(m); | |
2810 | assert(c); | |
2811 | assert(contents); | |
2812 | ||
2813 | if (!signature_is_valid(contents, false)) | |
2814 | return -EINVAL; | |
2815 | ||
2816 | if (!c->signature || c->signature[c->index] == 0) | |
2817 | return 0; | |
2818 | ||
2819 | l = strlen(contents); | |
2820 | ||
2821 | if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN || | |
2822 | !startswith(c->signature + c->index + 1, contents) || | |
2823 | c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END) | |
2824 | return -ENXIO; | |
2825 | ||
2826 | r = message_peek_body(m, &m->rindex, 8, 0, NULL); | |
2827 | if (r <= 0) | |
2828 | return r; | |
2829 | ||
2830 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
2831 | c->index += 1 + l + 1; | |
2832 | ||
2833 | return 1; | |
2834 | } | |
2835 | ||
2836 | static int bus_message_enter_dict_entry( | |
2837 | sd_bus_message *m, | |
2838 | struct bus_container *c, | |
2839 | const char *contents) { | |
2840 | ||
2841 | size_t l; | |
2842 | int r; | |
2843 | ||
2844 | assert(m); | |
2845 | assert(c); | |
2846 | assert(contents); | |
2847 | ||
2848 | if (!signature_is_pair(contents)) | |
2849 | return -EINVAL; | |
2850 | ||
2851 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
2852 | return -ENXIO; | |
2853 | ||
2854 | if (!c->signature || c->signature[c->index] == 0) | |
2855 | return 0; | |
2856 | ||
2857 | l = strlen(contents); | |
2858 | ||
2859 | if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN || | |
2860 | !startswith(c->signature + c->index + 1, contents) || | |
2861 | c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END) | |
2862 | return -ENXIO; | |
2863 | ||
2864 | r = message_peek_body(m, &m->rindex, 8, 0, NULL); | |
2865 | if (r <= 0) | |
2866 | return r; | |
2867 | ||
2868 | if (c->enclosing != SD_BUS_TYPE_ARRAY) | |
2869 | c->index += 1 + l + 1; | |
2870 | ||
2871 | return 1; | |
2872 | } | |
2873 | ||
2874 | int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents) { | |
2875 | struct bus_container *c, *w; | |
2876 | uint32_t *array_size = NULL; | |
2877 | char *signature; | |
b3af9646 | 2878 | size_t before; |
9a17484d LP |
2879 | int r; |
2880 | ||
275b39fe LP |
2881 | assert_return(m, -EINVAL); |
2882 | assert_return(m->sealed, -EPERM); | |
2883 | assert_return(type != 0 || !contents, -EINVAL); | |
2884 | ||
2885 | if (type == 0 || !contents) { | |
2886 | const char *cc; | |
2887 | char tt; | |
2888 | ||
2889 | /* Allow entering into anonymous containers */ | |
2890 | r = sd_bus_message_peek_type(m, &tt, &cc); | |
2891 | if (r <= 0) | |
2892 | return r; | |
2893 | ||
2894 | if (type != 0 && type != tt) | |
2895 | return -ENXIO; | |
2896 | ||
2897 | if (contents && !streq(contents, cc)) | |
2898 | return -ENXIO; | |
2899 | ||
2900 | type = tt; | |
2901 | contents = cc; | |
2902 | } | |
9a17484d | 2903 | |
ed205a6b LP |
2904 | /* |
2905 | * We enforce a global limit on container depth, that is much | |
2906 | * higher than the 32 structs and 32 arrays the specification | |
2907 | * mandates. This is simpler to implement for us, and we need | |
2908 | * this only to ensure our container array doesn't grow | |
2909 | * without bounds. We are happy to return any data from a | |
2910 | * message as long as the data itself is valid, even if the | |
2911 | * overall message might be not. | |
2912 | * | |
2913 | * Note that the message signature is validated when | |
2914 | * parsing the headers, and that validation does check the | |
2915 | * 32/32 limit. | |
2916 | * | |
2917 | * Note that the specification defines no limits on the depth | |
2918 | * of stacked variants, but we do. | |
2919 | */ | |
2920 | if (m->n_containers >= BUS_CONTAINER_DEPTH) | |
2921 | return -EBADMSG; | |
2922 | ||
9a17484d LP |
2923 | w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1)); |
2924 | if (!w) | |
2925 | return -ENOMEM; | |
2926 | m->containers = w; | |
2927 | ||
7b058942 | 2928 | if (message_end_of_signature(m)) |
430fb8fa | 2929 | return -ENXIO; |
9a17484d | 2930 | |
1daf8121 LP |
2931 | if (message_end_of_array(m, m->rindex)) |
2932 | return 0; | |
2933 | ||
7b058942 LP |
2934 | c = message_get_container(m); |
2935 | ||
9a17484d LP |
2936 | signature = strdup(contents); |
2937 | if (!signature) | |
2938 | return -ENOMEM; | |
2939 | ||
b3af9646 LP |
2940 | c->saved_index = c->index; |
2941 | before = m->rindex; | |
2942 | ||
9a17484d LP |
2943 | if (type == SD_BUS_TYPE_ARRAY) |
2944 | r = bus_message_enter_array(m, c, contents, &array_size); | |
2945 | else if (type == SD_BUS_TYPE_VARIANT) | |
2946 | r = bus_message_enter_variant(m, c, contents); | |
2947 | else if (type == SD_BUS_TYPE_STRUCT) | |
2948 | r = bus_message_enter_struct(m, c, contents); | |
2949 | else if (type == SD_BUS_TYPE_DICT_ENTRY) | |
2950 | r = bus_message_enter_dict_entry(m, c, contents); | |
2951 | else | |
2952 | r = -EINVAL; | |
2953 | ||
2954 | if (r <= 0) { | |
2955 | free(signature); | |
2956 | return r; | |
2957 | } | |
2958 | ||
2959 | /* OK, let's fill it in */ | |
2960 | w += m->n_containers++; | |
2961 | w->enclosing = type; | |
2962 | w->signature = signature; | |
2963 | w->index = 0; | |
2964 | w->array_size = array_size; | |
b3af9646 | 2965 | w->before = before; |
9a17484d LP |
2966 | w->begin = m->rindex; |
2967 | ||
2968 | return 1; | |
2969 | } | |
2970 | ||
2971 | int sd_bus_message_exit_container(sd_bus_message *m) { | |
2972 | struct bus_container *c; | |
2973 | ||
9d6c7c82 LP |
2974 | assert_return(m, -EINVAL); |
2975 | assert_return(m->sealed, -EPERM); | |
2976 | assert_return(m->n_containers > 0, -EINVAL); | |
9a17484d LP |
2977 | |
2978 | c = message_get_container(m); | |
2979 | if (c->enclosing == SD_BUS_TYPE_ARRAY) { | |
2980 | uint32_t l; | |
2981 | ||
2982 | l = BUS_MESSAGE_BSWAP32(m, *c->array_size); | |
2983 | if (c->begin + l != m->rindex) | |
2984 | return -EBUSY; | |
2985 | ||
2986 | } else { | |
2987 | if (c->signature && c->signature[c->index] != 0) | |
2988 | return -EINVAL; | |
2989 | } | |
2990 | ||
2991 | free(c->signature); | |
2992 | m->n_containers--; | |
2993 | ||
2994 | return 1; | |
2995 | } | |
2996 | ||
b3af9646 LP |
2997 | static void message_quit_container(sd_bus_message *m) { |
2998 | struct bus_container *c; | |
2999 | ||
3000 | assert(m); | |
3001 | assert(m->sealed); | |
3002 | assert(m->n_containers > 0); | |
3003 | ||
3004 | c = message_get_container(m); | |
3005 | ||
3006 | /* Undo seeks */ | |
3007 | assert(m->rindex >= c->before); | |
3008 | m->rindex = c->before; | |
3009 | ||
3010 | /* Free container */ | |
3011 | free(c->signature); | |
3012 | m->n_containers--; | |
3013 | ||
3014 | /* Correct index of new top-level container */ | |
3015 | c = message_get_container(m); | |
3016 | c->index = c->saved_index; | |
3017 | } | |
3018 | ||
9a17484d LP |
3019 | int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) { |
3020 | struct bus_container *c; | |
3021 | int r; | |
3022 | ||
c430fee6 LP |
3023 | assert_return(m, -EINVAL); |
3024 | assert_return(m->sealed, -EPERM); | |
9a17484d | 3025 | |
7b058942 | 3026 | if (message_end_of_signature(m)) |
9a17484d LP |
3027 | goto eof; |
3028 | ||
3029 | if (message_end_of_array(m, m->rindex)) | |
3030 | goto eof; | |
3031 | ||
7b058942 LP |
3032 | c = message_get_container(m); |
3033 | ||
9a17484d LP |
3034 | if (bus_type_is_basic(c->signature[c->index])) { |
3035 | if (contents) | |
3036 | *contents = NULL; | |
3037 | if (type) | |
3038 | *type = c->signature[c->index]; | |
3039 | return 1; | |
3040 | } | |
3041 | ||
3042 | if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) { | |
3043 | ||
3044 | if (contents) { | |
3045 | size_t l; | |
3046 | char *sig; | |
3047 | ||
3048 | r = signature_element_length(c->signature+c->index+1, &l); | |
3049 | if (r < 0) | |
3050 | return r; | |
3051 | ||
80a46c73 LP |
3052 | assert(l >= 1); |
3053 | ||
9a17484d LP |
3054 | sig = strndup(c->signature + c->index + 1, l); |
3055 | if (!sig) | |
3056 | return -ENOMEM; | |
3057 | ||
3058 | free(m->peeked_signature); | |
3059 | m->peeked_signature = sig; | |
3060 | ||
3061 | *contents = sig; | |
3062 | } | |
3063 | ||
3064 | if (type) | |
3065 | *type = SD_BUS_TYPE_ARRAY; | |
3066 | ||
3067 | return 1; | |
3068 | } | |
3069 | ||
3070 | if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN || | |
3071 | c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) { | |
3072 | ||
3073 | if (contents) { | |
3074 | size_t l; | |
3075 | char *sig; | |
3076 | ||
3077 | r = signature_element_length(c->signature+c->index, &l); | |
3078 | if (r < 0) | |
3079 | return r; | |
3080 | ||
3081 | assert(l >= 2); | |
3082 | sig = strndup(c->signature + c->index + 1, l - 2); | |
3083 | if (!sig) | |
3084 | return -ENOMEM; | |
3085 | ||
3086 | free(m->peeked_signature); | |
3087 | m->peeked_signature = sig; | |
3088 | ||
3089 | *contents = sig; | |
3090 | } | |
3091 | ||
3092 | if (type) | |
3093 | *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY; | |
3094 | ||
3095 | return 1; | |
3096 | } | |
3097 | ||
3098 | if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) { | |
3099 | if (contents) { | |
3100 | size_t rindex, l; | |
3101 | void *q; | |
3102 | ||
3103 | rindex = m->rindex; | |
3104 | r = message_peek_body(m, &rindex, 1, 1, &q); | |
3105 | if (r < 0) | |
3106 | return r; | |
3107 | if (r == 0) | |
3108 | goto eof; | |
3109 | ||
3110 | l = *(uint8_t*) q; | |
3111 | r = message_peek_body(m, &rindex, 1, l+1, &q); | |
3112 | if (r < 0) | |
3113 | return r; | |
3114 | if (r == 0) | |
3115 | return -EBADMSG; | |
3116 | ||
3117 | if (!validate_signature(q, l)) | |
3118 | return -EBADMSG; | |
3119 | ||
3120 | *contents = q; | |
3121 | } | |
3122 | ||
3123 | if (type) | |
3124 | *type = SD_BUS_TYPE_VARIANT; | |
3125 | ||
3126 | return 1; | |
3127 | } | |
3128 | ||
3129 | return -EINVAL; | |
3130 | ||
3131 | eof: | |
3132 | if (type) | |
7b058942 | 3133 | *type = 0; |
9a17484d LP |
3134 | if (contents) |
3135 | *contents = NULL; | |
3136 | return 0; | |
3137 | } | |
3138 | ||
89ffcd2a | 3139 | int sd_bus_message_rewind(sd_bus_message *m, int complete) { |
9a17484d LP |
3140 | struct bus_container *c; |
3141 | ||
9d6c7c82 LP |
3142 | assert_return(m, -EINVAL); |
3143 | assert_return(m->sealed, -EPERM); | |
9a17484d LP |
3144 | |
3145 | if (complete) { | |
bc7fd8cd | 3146 | message_reset_containers(m); |
9a17484d LP |
3147 | m->rindex = 0; |
3148 | m->root_container.index = 0; | |
3149 | ||
3150 | c = message_get_container(m); | |
3151 | } else { | |
3152 | c = message_get_container(m); | |
3153 | ||
3154 | c->index = 0; | |
3155 | m->rindex = c->begin; | |
3156 | } | |
3157 | ||
3158 | return !isempty(c->signature); | |
3159 | } | |
1b492614 LP |
3160 | static int message_read_ap( |
3161 | sd_bus_message *m, | |
3162 | const char *types, | |
3163 | va_list ap) { | |
9a17484d | 3164 | |
fe1d424d LP |
3165 | unsigned n_array, n_struct; |
3166 | TypeStack stack[BUS_CONTAINER_DEPTH]; | |
3167 | unsigned stack_ptr = 0; | |
430fb8fa | 3168 | unsigned n_loop = 0; |
9a17484d LP |
3169 | int r; |
3170 | ||
3171 | assert(m); | |
1b492614 | 3172 | |
430fb8fa | 3173 | if (isempty(types)) |
1b492614 | 3174 | return 0; |
9a17484d | 3175 | |
fe1d424d LP |
3176 | /* Ideally, we'd just call ourselves recursively on every |
3177 | * complex type. However, the state of a va_list that is | |
3178 | * passed to a function is undefined after that function | |
3179 | * returns. This means we need to docode the va_list linearly | |
3180 | * in a single stackframe. We hence implement our own | |
3181 | * home-grown stack in an array. */ | |
3182 | ||
430fb8fa LP |
3183 | n_array = (unsigned) -1; /* lenght of current array entries */ |
3184 | n_struct = strlen(types); /* length of current struct contents signature */ | |
fe1d424d LP |
3185 | |
3186 | for (;;) { | |
3187 | const char *t; | |
3188 | ||
430fb8fa LP |
3189 | n_loop++; |
3190 | ||
1b492614 | 3191 | if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) { |
fe1d424d LP |
3192 | r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array); |
3193 | if (r < 0) | |
3194 | return r; | |
3195 | if (r == 0) | |
3196 | break; | |
3197 | ||
3198 | r = sd_bus_message_exit_container(m); | |
3199 | if (r < 0) | |
3200 | return r; | |
3201 | ||
3202 | continue; | |
3203 | } | |
3204 | ||
3205 | t = types; | |
3206 | if (n_array != (unsigned) -1) | |
3207 | n_array --; | |
3208 | else { | |
3209 | types ++; | |
3210 | n_struct--; | |
3211 | } | |
3212 | ||
9a17484d LP |
3213 | switch (*t) { |
3214 | ||
3215 | case SD_BUS_TYPE_BYTE: | |
3216 | case SD_BUS_TYPE_BOOLEAN: | |
3217 | case SD_BUS_TYPE_INT16: | |
3218 | case SD_BUS_TYPE_UINT16: | |
3219 | case SD_BUS_TYPE_INT32: | |
3220 | case SD_BUS_TYPE_UINT32: | |
3221 | case SD_BUS_TYPE_INT64: | |
3222 | case SD_BUS_TYPE_UINT64: | |
3223 | case SD_BUS_TYPE_DOUBLE: | |
3224 | case SD_BUS_TYPE_STRING: | |
3225 | case SD_BUS_TYPE_OBJECT_PATH: | |
2c93b4ef LP |
3226 | case SD_BUS_TYPE_SIGNATURE: |
3227 | case SD_BUS_TYPE_UNIX_FD: { | |
9a17484d LP |
3228 | void *p; |
3229 | ||
3230 | p = va_arg(ap, void*); | |
3231 | r = sd_bus_message_read_basic(m, *t, p); | |
fe1d424d LP |
3232 | if (r < 0) |
3233 | return r; | |
430fb8fa LP |
3234 | if (r == 0) { |
3235 | if (n_loop <= 1) | |
3236 | return 0; | |
3237 | ||
fe1d424d | 3238 | return -ENXIO; |
430fb8fa | 3239 | } |
fe1d424d | 3240 | |
9a17484d LP |
3241 | break; |
3242 | } | |
3243 | ||
3244 | case SD_BUS_TYPE_ARRAY: { | |
3245 | size_t k; | |
3246 | ||
3247 | r = signature_element_length(t + 1, &k); | |
3248 | if (r < 0) | |
3249 | return r; | |
3250 | ||
3251 | { | |
9a17484d | 3252 | char s[k + 1]; |
9a17484d LP |
3253 | memcpy(s, t + 1, k); |
3254 | s[k] = 0; | |
9a17484d LP |
3255 | |
3256 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s); | |
3257 | if (r < 0) | |
3258 | return r; | |
430fb8fa LP |
3259 | if (r == 0) { |
3260 | if (n_loop <= 1) | |
3261 | return 0; | |
3262 | ||
9a17484d | 3263 | return -ENXIO; |
430fb8fa | 3264 | } |
fe1d424d | 3265 | } |
9a17484d | 3266 | |
fe1d424d LP |
3267 | if (n_array == (unsigned) -1) { |
3268 | types += k; | |
3269 | n_struct -= k; | |
9a17484d LP |
3270 | } |
3271 | ||
fe1d424d LP |
3272 | r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
3273 | if (r < 0) | |
3274 | return r; | |
3275 | ||
3276 | types = t + 1; | |
3277 | n_struct = k; | |
3278 | n_array = va_arg(ap, unsigned); | |
3279 | ||
9a17484d LP |
3280 | break; |
3281 | } | |
3282 | ||
3283 | case SD_BUS_TYPE_VARIANT: { | |
3284 | const char *s; | |
3285 | ||
3286 | s = va_arg(ap, const char *); | |
3287 | if (!s) | |
3288 | return -EINVAL; | |
3289 | ||
3290 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s); | |
3291 | if (r < 0) | |
3292 | return r; | |
430fb8fa LP |
3293 | if (r == 0) { |
3294 | if (n_loop <= 1) | |
3295 | return 0; | |
3296 | ||
9a17484d | 3297 | return -ENXIO; |
430fb8fa | 3298 | } |
9a17484d | 3299 | |
fe1d424d | 3300 | r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
9a17484d LP |
3301 | if (r < 0) |
3302 | return r; | |
9a17484d | 3303 | |
fe1d424d LP |
3304 | types = s; |
3305 | n_struct = strlen(s); | |
3306 | n_array = (unsigned) -1; | |
3307 | ||
9a17484d LP |
3308 | break; |
3309 | } | |
3310 | ||
3311 | case SD_BUS_TYPE_STRUCT_BEGIN: | |
3312 | case SD_BUS_TYPE_DICT_ENTRY_BEGIN: { | |
3313 | size_t k; | |
3314 | ||
3315 | r = signature_element_length(t, &k); | |
3316 | if (r < 0) | |
3317 | return r; | |
3318 | ||
3319 | { | |
3320 | char s[k - 1]; | |
3321 | memcpy(s, t + 1, k - 2); | |
3322 | s[k - 2] = 0; | |
3323 | ||
3324 | r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s); | |
3325 | if (r < 0) | |
3326 | return r; | |
430fb8fa LP |
3327 | if (r == 0) { |
3328 | if (n_loop <= 1) | |
3329 | return 0; | |
9a17484d | 3330 | return -ENXIO; |
430fb8fa | 3331 | } |
fe1d424d | 3332 | } |
9a17484d | 3333 | |
fe1d424d LP |
3334 | if (n_array == (unsigned) -1) { |
3335 | types += k - 1; | |
3336 | n_struct -= k - 1; | |
3337 | } | |
9a17484d | 3338 | |
fe1d424d LP |
3339 | r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array); |
3340 | if (r < 0) | |
3341 | return r; | |
9a17484d | 3342 | |
fe1d424d LP |
3343 | types = t + 1; |
3344 | n_struct = k - 2; | |
3345 | n_array = (unsigned) -1; | |
9a17484d LP |
3346 | |
3347 | break; | |
3348 | } | |
3349 | ||
3350 | default: | |
fe1d424d | 3351 | return -EINVAL; |
9a17484d | 3352 | } |
9a17484d LP |
3353 | } |
3354 | ||
3355 | return 1; | |
3356 | } | |
3357 | ||
3358 | int sd_bus_message_read(sd_bus_message *m, const char *types, ...) { | |
3359 | va_list ap; | |
3360 | int r; | |
3361 | ||
9b07511d LP |
3362 | assert_return(m, -EINVAL); |
3363 | assert_return(m->sealed, -EPERM); | |
3364 | assert_return(types, -EINVAL); | |
9a17484d LP |
3365 | |
3366 | va_start(ap, types); | |
3367 | r = message_read_ap(m, types, ap); | |
3368 | va_end(ap); | |
3369 | ||
3370 | return r; | |
3371 | } | |
3372 | ||
9b07511d LP |
3373 | int sd_bus_message_skip(sd_bus_message *m, const char *types) { |
3374 | int r; | |
3375 | ||
3376 | assert_return(m, -EINVAL); | |
3377 | assert_return(m->sealed, -EPERM); | |
3378 | assert_return(types, -EINVAL); | |
3379 | ||
3380 | if (isempty(types)) | |
3381 | return 0; | |
3382 | ||
3383 | switch (*types) { | |
3384 | ||
3385 | case SD_BUS_TYPE_BYTE: | |
3386 | case SD_BUS_TYPE_BOOLEAN: | |
3387 | case SD_BUS_TYPE_INT16: | |
3388 | case SD_BUS_TYPE_UINT16: | |
3389 | case SD_BUS_TYPE_INT32: | |
3390 | case SD_BUS_TYPE_UINT32: | |
3391 | case SD_BUS_TYPE_INT64: | |
3392 | case SD_BUS_TYPE_UINT64: | |
3393 | case SD_BUS_TYPE_DOUBLE: | |
3394 | case SD_BUS_TYPE_STRING: | |
3395 | case SD_BUS_TYPE_OBJECT_PATH: | |
3396 | case SD_BUS_TYPE_SIGNATURE: | |
3397 | case SD_BUS_TYPE_UNIX_FD: | |
3398 | ||
3399 | r = sd_bus_message_read_basic(m, *types, NULL); | |
3400 | if (r <= 0) | |
3401 | return r; | |
3402 | ||
3403 | r = sd_bus_message_skip(m, types + 1); | |
3404 | if (r < 0) | |
3405 | return r; | |
3406 | ||
3407 | return 1; | |
3408 | ||
3409 | case SD_BUS_TYPE_ARRAY: { | |
3410 | size_t k; | |
3411 | ||
3412 | r = signature_element_length(types + 1, &k); | |
3413 | if (r < 0) | |
3414 | return r; | |
3415 | ||
3416 | { | |
3417 | char s[k+1]; | |
3418 | memcpy(s, types+1, k); | |
3419 | s[k] = 0; | |
3420 | ||
3421 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s); | |
3422 | if (r <= 0) | |
3423 | return r; | |
3424 | ||
3425 | for (;;) { | |
3426 | r = sd_bus_message_skip(m, s); | |
3427 | if (r < 0) | |
3428 | return r; | |
3429 | if (r == 0) | |
3430 | break; | |
3431 | } | |
3432 | ||
3433 | r = sd_bus_message_exit_container(m); | |
3434 | if (r < 0) | |
3435 | return r; | |
3436 | } | |
3437 | ||
3438 | r = sd_bus_message_skip(m, types + 1 + k); | |
3439 | if (r < 0) | |
3440 | return r; | |
3441 | ||
3442 | return 1; | |
3443 | } | |
3444 | ||
3445 | case SD_BUS_TYPE_VARIANT: { | |
3446 | const char *contents; | |
3447 | char x; | |
3448 | ||
3449 | r = sd_bus_message_peek_type(m, &x, &contents); | |
3450 | if (r <= 0) | |
3451 | return r; | |
3452 | ||
3453 | if (x != SD_BUS_TYPE_VARIANT) | |
3454 | return -ENXIO; | |
3455 | ||
3456 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); | |
3457 | if (r <= 0) | |
3458 | return r; | |
3459 | ||
3460 | r = sd_bus_message_skip(m, contents); | |
3461 | if (r < 0) | |
3462 | return r; | |
3463 | assert(r != 0); | |
3464 | ||
3465 | r = sd_bus_message_exit_container(m); | |
3466 | if (r < 0) | |
3467 | return r; | |
3468 | ||
3469 | r = sd_bus_message_skip(m, types + 1); | |
3470 | if (r < 0) | |
3471 | return r; | |
3472 | ||
3473 | return 1; | |
3474 | } | |
3475 | ||
3476 | case SD_BUS_TYPE_STRUCT_BEGIN: | |
3477 | case SD_BUS_TYPE_DICT_ENTRY_BEGIN: { | |
3478 | size_t k; | |
3479 | ||
3480 | r = signature_element_length(types, &k); | |
3481 | if (r < 0) | |
3482 | return r; | |
3483 | ||
3484 | { | |
3485 | char s[k-1]; | |
3486 | memcpy(s, types+1, k-2); | |
3487 | s[k-2] = 0; | |
3488 | ||
3489 | r = sd_bus_message_enter_container(m, *types == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s); | |
3490 | if (r <= 0) | |
3491 | return r; | |
3492 | ||
3493 | r = sd_bus_message_skip(m, s); | |
3494 | if (r < 0) | |
3495 | return r; | |
3496 | assert(r != 0); | |
3497 | ||
3498 | r = sd_bus_message_exit_container(m); | |
3499 | if (r < 0) | |
3500 | return r; | |
3501 | } | |
3502 | ||
3503 | r = sd_bus_message_skip(m, types + k); | |
3504 | if (r < 0) | |
3505 | return r; | |
3506 | ||
3507 | return 1; | |
3508 | } | |
3509 | ||
3510 | default: | |
3511 | return -EINVAL; | |
3512 | } | |
3513 | } | |
3514 | ||
b3af9646 LP |
3515 | int sd_bus_message_read_array(sd_bus_message *m, char type, const void **ptr, size_t *size) { |
3516 | struct bus_container *c; | |
3517 | void *p; | |
3518 | size_t sz; | |
3519 | ssize_t align; | |
3520 | int r; | |
3521 | ||
9d6c7c82 LP |
3522 | assert_return(m, -EINVAL); |
3523 | assert_return(m->sealed, -EPERM); | |
3524 | assert_return(bus_type_is_trivial(type), -EINVAL); | |
3525 | assert_return(ptr, -EINVAL); | |
3526 | assert_return(size, -EINVAL); | |
3527 | assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -ENOTSUP); | |
b3af9646 LP |
3528 | |
3529 | align = bus_type_get_alignment(type); | |
3530 | if (align < 0) | |
3531 | return align; | |
3532 | ||
3533 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type)); | |
66b26c5c | 3534 | if (r <= 0) |
b3af9646 LP |
3535 | return r; |
3536 | ||
3537 | c = message_get_container(m); | |
3538 | sz = BUS_MESSAGE_BSWAP32(m, *c->array_size); | |
3539 | ||
3540 | r = message_peek_body(m, &m->rindex, align, sz, &p); | |
3541 | if (r < 0) | |
3542 | goto fail; | |
3543 | if (r == 0) { | |
3544 | r = -EBADMSG; | |
3545 | goto fail; | |
3546 | } | |
3547 | ||
3548 | r = sd_bus_message_exit_container(m); | |
3549 | if (r < 0) | |
3550 | goto fail; | |
3551 | ||
3552 | *ptr = (const void*) p; | |
3553 | *size = sz; | |
3554 | ||
3555 | return 1; | |
3556 | ||
3557 | fail: | |
3558 | message_quit_container(m); | |
3559 | return r; | |
3560 | } | |
3561 | ||
80a46c73 LP |
3562 | static int message_peek_fields( |
3563 | sd_bus_message *m, | |
3564 | size_t *rindex, | |
3565 | size_t align, | |
3566 | size_t nbytes, | |
3567 | void **ret) { | |
3568 | ||
3569 | assert(m); | |
3570 | assert(rindex); | |
3571 | assert(align > 0); | |
3572 | ||
c91cb83c | 3573 | return buffer_peek(BUS_MESSAGE_FIELDS(m), BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret); |
80a46c73 LP |
3574 | } |
3575 | ||
9f26c90c LP |
3576 | static int message_peek_field_uint32( |
3577 | sd_bus_message *m, | |
3578 | size_t *ri, | |
3579 | uint32_t *ret) { | |
3580 | ||
3581 | int r; | |
3582 | void *q; | |
3583 | ||
3584 | assert(m); | |
3585 | assert(ri); | |
3586 | ||
3587 | r = message_peek_fields(m, ri, 4, 4, &q); | |
3588 | if (r < 0) | |
3589 | return r; | |
3590 | ||
3591 | if (ret) | |
3592 | *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); | |
3593 | ||
3594 | return 0; | |
3595 | } | |
3596 | ||
80a46c73 LP |
3597 | static int message_peek_field_string( |
3598 | sd_bus_message *m, | |
6693860f | 3599 | bool (*validate)(const char *p), |
80a46c73 LP |
3600 | size_t *ri, |
3601 | const char **ret) { | |
3602 | ||
9f26c90c | 3603 | uint32_t l; |
80a46c73 LP |
3604 | int r; |
3605 | void *q; | |
3606 | ||
9a17484d | 3607 | assert(m); |
80a46c73 LP |
3608 | assert(ri); |
3609 | ||
9f26c90c | 3610 | r = message_peek_field_uint32(m, ri, &l); |
80a46c73 LP |
3611 | if (r < 0) |
3612 | return r; | |
3613 | ||
80a46c73 LP |
3614 | r = message_peek_fields(m, ri, 1, l+1, &q); |
3615 | if (r < 0) | |
3616 | return r; | |
9a17484d | 3617 | |
6693860f LP |
3618 | if (validate) { |
3619 | if (!validate_nul(q, l)) | |
3620 | return -EBADMSG; | |
3621 | ||
3622 | if (!validate(q)) | |
ac89bf1d LP |
3623 | return -EBADMSG; |
3624 | } else { | |
3625 | if (!validate_string(q, l)) | |
3626 | return -EBADMSG; | |
3627 | } | |
80a46c73 LP |
3628 | |
3629 | if (ret) | |
3630 | *ret = q; | |
3631 | ||
3632 | return 0; | |
3633 | } | |
3634 | ||
3635 | static int message_peek_field_signature( | |
3636 | sd_bus_message *m, | |
3637 | size_t *ri, | |
3638 | const char **ret) { | |
3639 | ||
3640 | size_t l; | |
3641 | int r; | |
3642 | void *q; | |
3643 | ||
3644 | assert(m); | |
3645 | assert(ri); | |
3646 | ||
3647 | r = message_peek_fields(m, ri, 1, 1, &q); | |
3648 | if (r < 0) | |
3649 | return r; | |
3650 | ||
3651 | l = *(uint8_t*) q; | |
3652 | r = message_peek_fields(m, ri, 1, l+1, &q); | |
3653 | if (r < 0) | |
3654 | return r; | |
3655 | ||
3656 | if (!validate_signature(q, l)) | |
3657 | return -EBADMSG; | |
3658 | ||
3659 | if (ret) | |
3660 | *ret = q; | |
3661 | ||
3662 | return 0; | |
3663 | } | |
3664 | ||
80a46c73 LP |
3665 | static int message_skip_fields( |
3666 | sd_bus_message *m, | |
3667 | size_t *ri, | |
3668 | uint32_t array_size, | |
3669 | const char **signature) { | |
3670 | ||
3671 | size_t original_index; | |
3672 | int r; | |
3673 | ||
3674 | assert(m); | |
3675 | assert(ri); | |
3676 | assert(signature); | |
3677 | ||
3678 | original_index = *ri; | |
3679 | ||
3680 | for (;;) { | |
3681 | char t; | |
80a46c73 LP |
3682 | size_t l; |
3683 | ||
3684 | if (array_size != (uint32_t) -1 && | |
3685 | array_size <= *ri - original_index) | |
3686 | return 0; | |
3687 | ||
3688 | t = **signature; | |
3689 | if (!t) | |
3690 | return 0; | |
3691 | ||
6693860f LP |
3692 | if (t == SD_BUS_TYPE_STRING) { |
3693 | ||
3694 | r = message_peek_field_string(m, NULL, ri, NULL); | |
3695 | if (r < 0) | |
3696 | return r; | |
3697 | ||
3698 | (*signature)++; | |
3699 | ||
3700 | } else if (t == SD_BUS_TYPE_OBJECT_PATH) { | |
80a46c73 | 3701 | |
6693860f | 3702 | r = message_peek_field_string(m, object_path_is_valid, ri, NULL); |
80a46c73 LP |
3703 | if (r < 0) |
3704 | return r; | |
3705 | ||
3706 | (*signature)++; | |
3707 | ||
3708 | } else if (t == SD_BUS_TYPE_SIGNATURE) { | |
3709 | ||
3710 | r = message_peek_field_signature(m, ri, NULL); | |
3711 | if (r < 0) | |
3712 | return r; | |
3713 | ||
3714 | (*signature)++; | |
3715 | ||
3716 | } else if (bus_type_is_basic(t)) { | |
27f6e5c7 | 3717 | ssize_t align, k; |
80a46c73 | 3718 | |
c66a2e0c TG |
3719 | align = bus_type_get_alignment(t); |
3720 | k = bus_type_get_size(t); | |
27f6e5c7 | 3721 | assert(align > 0 && k > 0); |
80a46c73 LP |
3722 | |
3723 | r = message_peek_fields(m, ri, align, k, NULL); | |
3724 | if (r < 0) | |
3725 | return r; | |
9a17484d | 3726 | |
80a46c73 LP |
3727 | (*signature)++; |
3728 | ||
3729 | } else if (t == SD_BUS_TYPE_ARRAY) { | |
3730 | ||
3731 | r = signature_element_length(*signature+1, &l); | |
3732 | if (r < 0) | |
3733 | return r; | |
3734 | ||
3735 | assert(l >= 1); | |
3736 | { | |
3737 | char sig[l-1], *s; | |
9f26c90c | 3738 | uint32_t nas; |
80a46c73 LP |
3739 | int alignment; |
3740 | ||
3741 | strncpy(sig, *signature + 1, l-1); | |
3742 | s = sig; | |
3743 | ||
3744 | alignment = bus_type_get_alignment(sig[0]); | |
3745 | if (alignment < 0) | |
3746 | return alignment; | |
3747 | ||
9f26c90c | 3748 | r = message_peek_field_uint32(m, ri, &nas); |
80a46c73 LP |
3749 | if (r < 0) |
3750 | return r; | |
ac89bf1d | 3751 | if (nas > BUS_ARRAY_MAX_SIZE) |
80a46c73 LP |
3752 | return -EBADMSG; |
3753 | ||
3754 | r = message_peek_fields(m, ri, alignment, 0, NULL); | |
3755 | if (r < 0) | |
3756 | return r; | |
3757 | ||
3758 | r = message_skip_fields(m, ri, nas, (const char**) &s); | |
3759 | if (r < 0) | |
3760 | return r; | |
3761 | } | |
3762 | ||
3763 | (*signature) += 1 + l; | |
3764 | ||
3765 | } else if (t == SD_BUS_TYPE_VARIANT) { | |
3766 | const char *s; | |
3767 | ||
3768 | r = message_peek_field_signature(m, ri, &s); | |
3769 | if (r < 0) | |
3770 | return r; | |
3771 | ||
3772 | r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s); | |
3773 | if (r < 0) | |
3774 | return r; | |
3775 | ||
3776 | (*signature)++; | |
3777 | ||
3778 | } else if (t == SD_BUS_TYPE_STRUCT || | |
3779 | t == SD_BUS_TYPE_DICT_ENTRY) { | |
3780 | ||
3781 | r = signature_element_length(*signature, &l); | |
3782 | if (r < 0) | |
3783 | return r; | |
3784 | ||
3785 | assert(l >= 2); | |
3786 | { | |
3787 | char sig[l-1], *s; | |
3788 | strncpy(sig, *signature + 1, l-1); | |
3789 | s = sig; | |
3790 | ||
3791 | r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s); | |
3792 | if (r < 0) | |
3793 | return r; | |
3794 | } | |
3795 | ||
3796 | *signature += l; | |
3797 | } else | |
3798 | return -EINVAL; | |
3799 | } | |
3800 | } | |
3801 | ||
6629161f | 3802 | int bus_message_parse_fields(sd_bus_message *m) { |
80a46c73 LP |
3803 | size_t ri; |
3804 | int r; | |
2c93b4ef | 3805 | uint32_t unix_fds = 0; |
80a46c73 LP |
3806 | |
3807 | assert(m); | |
3808 | ||
3809 | for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) { | |
3810 | const char *signature; | |
3811 | uint8_t *header; | |
3812 | ||
3813 | r = message_peek_fields(m, &ri, 8, 1, (void**) &header); | |
3814 | if (r < 0) | |
3815 | return r; | |
3816 | ||
3817 | r = message_peek_field_signature(m, &ri, &signature); | |
3818 | if (r < 0) | |
3819 | return r; | |
3820 | ||
3821 | switch (*header) { | |
3822 | case _SD_BUS_MESSAGE_HEADER_INVALID: | |
3823 | return -EBADMSG; | |
3824 | ||
3825 | case SD_BUS_MESSAGE_HEADER_PATH: | |
2c93b4ef LP |
3826 | |
3827 | if (m->path) | |
3828 | return -EBADMSG; | |
3829 | ||
80a46c73 LP |
3830 | if (!streq(signature, "o")) |
3831 | return -EBADMSG; | |
3832 | ||
6693860f | 3833 | r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path); |
80a46c73 LP |
3834 | break; |
3835 | ||
3836 | case SD_BUS_MESSAGE_HEADER_INTERFACE: | |
2c93b4ef LP |
3837 | |
3838 | if (m->interface) | |
3839 | return -EBADMSG; | |
3840 | ||
80a46c73 LP |
3841 | if (!streq(signature, "s")) |
3842 | return -EBADMSG; | |
3843 | ||
6693860f | 3844 | r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface); |
80a46c73 LP |
3845 | break; |
3846 | ||
3847 | case SD_BUS_MESSAGE_HEADER_MEMBER: | |
2c93b4ef LP |
3848 | |
3849 | if (m->member) | |
3850 | return -EBADMSG; | |
3851 | ||
80a46c73 LP |
3852 | if (!streq(signature, "s")) |
3853 | return -EBADMSG; | |
3854 | ||
6693860f | 3855 | r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member); |
80a46c73 LP |
3856 | break; |
3857 | ||
3858 | case SD_BUS_MESSAGE_HEADER_ERROR_NAME: | |
2c93b4ef LP |
3859 | |
3860 | if (m->error.name) | |
3861 | return -EBADMSG; | |
3862 | ||
80a46c73 LP |
3863 | if (!streq(signature, "s")) |
3864 | return -EBADMSG; | |
3865 | ||
6693860f | 3866 | r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name); |
80a46c73 LP |
3867 | break; |
3868 | ||
3869 | case SD_BUS_MESSAGE_HEADER_DESTINATION: | |
2c93b4ef LP |
3870 | |
3871 | if (m->destination) | |
3872 | return -EBADMSG; | |
3873 | ||
80a46c73 LP |
3874 | if (!streq(signature, "s")) |
3875 | return -EBADMSG; | |
3876 | ||
6693860f | 3877 | r = message_peek_field_string(m, service_name_is_valid, &ri, &m->destination); |
80a46c73 LP |
3878 | break; |
3879 | ||
3880 | case SD_BUS_MESSAGE_HEADER_SENDER: | |
2c93b4ef LP |
3881 | |
3882 | if (m->sender) | |
3883 | return -EBADMSG; | |
3884 | ||
80a46c73 LP |
3885 | if (!streq(signature, "s")) |
3886 | return -EBADMSG; | |
3887 | ||
6693860f | 3888 | r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender); |
80a46c73 LP |
3889 | break; |
3890 | ||
3891 | ||
3892 | case SD_BUS_MESSAGE_HEADER_SIGNATURE: { | |
3893 | const char *s; | |
3894 | char *c; | |
3895 | ||
2c93b4ef LP |
3896 | if (m->root_container.signature) |
3897 | return -EBADMSG; | |
3898 | ||
80a46c73 LP |
3899 | if (!streq(signature, "g")) |
3900 | return -EBADMSG; | |
3901 | ||
3902 | r = message_peek_field_signature(m, &ri, &s); | |
3903 | if (r < 0) | |
3904 | return r; | |
3905 | ||
3906 | c = strdup(s); | |
3907 | if (!c) | |
3908 | return -ENOMEM; | |
3909 | ||
3910 | free(m->root_container.signature); | |
3911 | m->root_container.signature = c; | |
80a46c73 LP |
3912 | break; |
3913 | } | |
3914 | ||
3915 | case SD_BUS_MESSAGE_HEADER_REPLY_SERIAL: | |
2c93b4ef LP |
3916 | if (m->reply_serial != 0) |
3917 | return -EBADMSG; | |
3918 | ||
80a46c73 LP |
3919 | if (!streq(signature, "u")) |
3920 | return -EBADMSG; | |
3921 | ||
3922 | r = message_peek_field_uint32(m, &ri, &m->reply_serial); | |
6693860f LP |
3923 | if (r < 0) |
3924 | return r; | |
3925 | ||
3926 | if (m->reply_serial == 0) | |
3927 | return -EBADMSG; | |
3928 | ||
80a46c73 LP |
3929 | break; |
3930 | ||
2c93b4ef LP |
3931 | case SD_BUS_MESSAGE_HEADER_UNIX_FDS: |
3932 | if (unix_fds != 0) | |
3933 | return -EBADMSG; | |
3934 | ||
3935 | if (!streq(signature, "u")) | |
3936 | return -EBADMSG; | |
3937 | ||
3938 | r = message_peek_field_uint32(m, &ri, &unix_fds); | |
3939 | if (r < 0) | |
3940 | return -EBADMSG; | |
3941 | ||
3942 | if (unix_fds == 0) | |
3943 | return -EBADMSG; | |
3944 | ||
3945 | break; | |
3946 | ||
80a46c73 LP |
3947 | default: |
3948 | r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature); | |
3949 | } | |
3950 | ||
3951 | if (r < 0) | |
3952 | return r; | |
3953 | } | |
3954 | ||
2c93b4ef LP |
3955 | if (m->n_fds != unix_fds) |
3956 | return -EBADMSG; | |
3957 | ||
80a46c73 LP |
3958 | if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0)) |
3959 | return -EBADMSG; | |
3960 | ||
3961 | switch (m->header->type) { | |
3962 | ||
40ca29a1 | 3963 | case SD_BUS_MESSAGE_SIGNAL: |
80a46c73 LP |
3964 | if (!m->path || !m->interface || !m->member) |
3965 | return -EBADMSG; | |
3966 | break; | |
3967 | ||
40ca29a1 | 3968 | case SD_BUS_MESSAGE_METHOD_CALL: |
80a46c73 LP |
3969 | |
3970 | if (!m->path || !m->member) | |
3971 | return -EBADMSG; | |
3972 | ||
3973 | break; | |
3974 | ||
40ca29a1 | 3975 | case SD_BUS_MESSAGE_METHOD_RETURN: |
80a46c73 LP |
3976 | |
3977 | if (m->reply_serial == 0) | |
3978 | return -EBADMSG; | |
3979 | break; | |
3980 | ||
40ca29a1 | 3981 | case SD_BUS_MESSAGE_METHOD_ERROR: |
80a46c73 LP |
3982 | |
3983 | if (m->reply_serial == 0 || !m->error.name) | |
3984 | return -EBADMSG; | |
3985 | break; | |
3986 | } | |
9a17484d | 3987 | |
89ffcd2a | 3988 | /* Try to read the error message, but if we can't it's a non-issue */ |
40ca29a1 | 3989 | if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR) |
89ffcd2a LP |
3990 | sd_bus_message_read(m, "s", &m->error.message); |
3991 | ||
9a17484d LP |
3992 | return 0; |
3993 | } | |
3994 | ||
9a17484d | 3995 | int bus_message_seal(sd_bus_message *m, uint64_t serial) { |
47e6ce32 | 3996 | struct bus_body_part *part; |
69aec65c | 3997 | size_t l, a; |
47e6ce32 LP |
3998 | unsigned i; |
3999 | int r; | |
9a17484d LP |
4000 | |
4001 | assert(m); | |
4002 | ||
4003 | if (m->sealed) | |
4004 | return -EPERM; | |
4005 | ||
4006 | if (m->n_containers > 0) | |
4007 | return -EBADMSG; | |
4008 | ||
13c299d3 LP |
4009 | if (m->poisoned) |
4010 | return -ESTALE; | |
4011 | ||
9a17484d LP |
4012 | /* If there's a non-trivial signature set, then add it in here */ |
4013 | if (!isempty(m->root_container.signature)) { | |
80a46c73 | 4014 | r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL); |
9a17484d LP |
4015 | if (r < 0) |
4016 | return r; | |
4017 | } | |
4018 | ||
4019 | if (m->n_fds > 0) { | |
4020 | r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds); | |
4021 | if (r < 0) | |
4022 | return r; | |
4023 | } | |
4024 | ||
66b26c5c LP |
4025 | /* Add padding at the end of the fields part, since we know |
4026 | * the body needs to start at an 8 byte alignment. We made | |
4027 | * sure we allocated enough space for this, so all we need to | |
4028 | * do here is to zero it out. */ | |
69aec65c LP |
4029 | l = BUS_MESSAGE_FIELDS_SIZE(m); |
4030 | a = ALIGN8(l) - l; | |
c91cb83c LP |
4031 | if (a > 0) |
4032 | memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a); | |
69aec65c | 4033 | |
66b26c5c LP |
4034 | /* If this is something we can send as memfd, then let's seal |
4035 | the memfd now. Note that we can send memfds as payload only | |
4036 | for directed messages, and not for broadcasts. */ | |
8f155917 | 4037 | if (m->destination && m->bus && m->bus->use_memfd) { |
66b26c5c | 4038 | MESSAGE_FOREACH_PART(part, i, m) |
8f155917 | 4039 | if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) { |
66b26c5c | 4040 | bus_body_part_unmap(part); |
a392d361 | 4041 | |
66b26c5c LP |
4042 | if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0) |
4043 | part->sealed = true; | |
4044 | } | |
4045 | } | |
47e6ce32 | 4046 | |
9a17484d LP |
4047 | m->header->serial = serial; |
4048 | m->sealed = true; | |
4049 | ||
9a17484d LP |
4050 | return 0; |
4051 | } | |
4052 | ||
4053 | int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) { | |
9d6c7c82 LP |
4054 | assert_return(m, -EINVAL); |
4055 | assert_return(destination, -EINVAL); | |
4056 | assert_return(!m->sealed, -EPERM); | |
4057 | assert_return(!m->destination, -EEXIST); | |
9a17484d LP |
4058 | |
4059 | return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination); | |
4060 | } | |
4061 | ||
c430fee6 | 4062 | int bus_message_dump(sd_bus_message *m, FILE *f, bool with_header) { |
d8d3d8a7 | 4063 | const char *u = NULL, *uu = NULL, *s = NULL; |
77930f11 | 4064 | char **cmdline = NULL; |
9a17484d LP |
4065 | unsigned level = 1; |
4066 | int r; | |
120f919e LP |
4067 | uid_t owner, audit_loginuid; |
4068 | uint32_t audit_sessionid; | |
9a17484d LP |
4069 | |
4070 | assert(m); | |
4071 | ||
c430fee6 LP |
4072 | if (!f) |
4073 | f = stdout; | |
4074 | ||
4075 | if (with_header) { | |
4076 | fprintf(f, | |
4077 | "Message %p\n" | |
4078 | "\tn_ref=%u\n" | |
4079 | "\tendian=%c\n" | |
4080 | "\ttype=%i\n" | |
4081 | "\tflags=%u\n" | |
4082 | "\tversion=%u\n" | |
4083 | "\tserial=%u\n" | |
4084 | "\tfields_size=%u\n" | |
4085 | "\tbody_size=%u\n" | |
4086 | "\tpath=%s\n" | |
4087 | "\tinterface=%s\n" | |
4088 | "\tmember=%s\n" | |
4089 | "\tdestination=%s\n" | |
4090 | "\tsender=%s\n" | |
4091 | "\tsignature=%s\n" | |
4092 | "\treply_serial=%u\n" | |
c430fee6 LP |
4093 | "\tsealed=%s\n" |
4094 | "\tn_body_parts=%u\n", | |
4095 | m, | |
4096 | m->n_ref, | |
4097 | m->header->endian, | |
4098 | m->header->type, | |
4099 | m->header->flags, | |
4100 | m->header->version, | |
4101 | BUS_MESSAGE_SERIAL(m), | |
4102 | BUS_MESSAGE_FIELDS_SIZE(m), | |
4103 | BUS_MESSAGE_BODY_SIZE(m), | |
4104 | strna(m->path), | |
4105 | strna(m->interface), | |
4106 | strna(m->member), | |
4107 | strna(m->destination), | |
4108 | strna(m->sender), | |
4109 | strna(m->root_container.signature), | |
4110 | m->reply_serial, | |
c430fee6 LP |
4111 | yes_no(m->sealed), |
4112 | m->n_body_parts); | |
4113 | ||
3fc047f4 LP |
4114 | if (sd_bus_error_is_set(&m->error)) |
4115 | fprintf(f, | |
4116 | "\terror.name=%s\n" | |
4117 | "\terror.message=%s\n", | |
4118 | strna(m->error.name), | |
4119 | strna(m->error.message)); | |
4120 | ||
c430fee6 LP |
4121 | if (m->pid != 0) |
4122 | fprintf(f, "\tpid=%lu\n", (unsigned long) m->pid); | |
4123 | if (m->tid != 0) | |
4124 | fprintf(f, "\ttid=%lu\n", (unsigned long) m->tid); | |
4125 | if (m->uid_valid) | |
4126 | fprintf(f, "\tuid=%lu\n", (unsigned long) m->uid); | |
4127 | if (m->gid_valid) | |
4128 | fprintf(f, "\tgid=%lu\n", (unsigned long) m->gid); | |
4129 | if (m->pid_starttime != 0) | |
4130 | fprintf(f, "\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime); | |
4131 | if (m->monotonic != 0) | |
4132 | fprintf(f, "\tmonotonic=%llu\n", (unsigned long long) m->monotonic); | |
4133 | if (m->realtime != 0) | |
4134 | fprintf(f, "\trealtime=%llu\n", (unsigned long long) m->realtime); | |
4135 | if (m->exe) | |
4136 | fprintf(f, "\texe=[%s]\n", m->exe); | |
4137 | if (m->comm) | |
4138 | fprintf(f, "\tcomm=[%s]\n", m->comm); | |
4139 | if (m->tid_comm) | |
4140 | fprintf(f, "\ttid_comm=[%s]\n", m->tid_comm); | |
4141 | if (m->label) | |
4142 | fprintf(f, "\tlabel=[%s]\n", m->label); | |
4143 | if (m->cgroup) | |
4144 | fprintf(f, "\tcgroup=[%s]\n", m->cgroup); | |
4145 | ||
4146 | sd_bus_message_get_unit(m, &u); | |
4147 | if (u) | |
4148 | fprintf(f, "\tunit=[%s]\n", u); | |
4149 | sd_bus_message_get_user_unit(m, &uu); | |
4150 | if (uu) | |
4151 | fprintf(f, "\tuser_unit=[%s]\n", uu); | |
4152 | sd_bus_message_get_session(m, &s); | |
4153 | if (s) | |
4154 | fprintf(f, "\tsession=[%s]\n", s); | |
4155 | if (sd_bus_message_get_owner_uid(m, &owner) >= 0) | |
4156 | fprintf(f, "\towner_uid=%lu\n", (unsigned long) owner); | |
4157 | if (sd_bus_message_get_audit_loginuid(m, &audit_loginuid) >= 0) | |
4158 | fprintf(f, "\taudit_loginuid=%lu\n", (unsigned long) audit_loginuid); | |
4159 | if (sd_bus_message_get_audit_sessionid(m, &audit_sessionid) >= 0) | |
4160 | fprintf(f, "\taudit_sessionid=%lu\n", (unsigned long) audit_sessionid); | |
4161 | ||
3fc047f4 LP |
4162 | r = sd_bus_message_has_effective_cap(m, 5); |
4163 | if (r >= 0) | |
4164 | fprintf(f, "\tCAP_KILL=%s\n", yes_no(r)); | |
c430fee6 LP |
4165 | |
4166 | if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) { | |
4167 | char **c; | |
4168 | ||
4169 | fputs("\tcmdline=[", f); | |
4170 | STRV_FOREACH(c, cmdline) { | |
4171 | if (c != cmdline) | |
4172 | fputc(' ', f); | |
4173 | ||
4174 | fputs(*c, f); | |
4175 | } | |
77930f11 | 4176 | |
c430fee6 LP |
4177 | fputs("]\n", f); |
4178 | } | |
77930f11 LP |
4179 | } |
4180 | ||
9a17484d LP |
4181 | r = sd_bus_message_rewind(m, true); |
4182 | if (r < 0) { | |
4183 | log_error("Failed to rewind: %s", strerror(-r)); | |
4184 | return r; | |
4185 | } | |
4186 | ||
c430fee6 | 4187 | fprintf(f, "BEGIN_MESSAGE \"%s\" {\n", strempty(m->root_container.signature)); |
9a17484d LP |
4188 | |
4189 | for(;;) { | |
4190 | _cleanup_free_ char *prefix = NULL; | |
4191 | const char *contents = NULL; | |
4192 | char type; | |
4193 | union { | |
4194 | uint8_t u8; | |
4195 | uint16_t u16; | |
4196 | int16_t s16; | |
4197 | uint32_t u32; | |
4198 | int32_t s32; | |
4199 | uint64_t u64; | |
4200 | int64_t s64; | |
4201 | double d64; | |
4202 | const char *string; | |
4203 | int i; | |
4204 | } basic; | |
4205 | ||
4206 | r = sd_bus_message_peek_type(m, &type, &contents); | |
4207 | if (r < 0) { | |
4208 | log_error("Failed to peek type: %s", strerror(-r)); | |
4209 | return r; | |
4210 | } | |
4211 | if (r == 0) { | |
4212 | if (level <= 1) | |
4213 | break; | |
4214 | ||
4215 | r = sd_bus_message_exit_container(m); | |
4216 | if (r < 0) { | |
4217 | log_error("Failed to exit container: %s", strerror(-r)); | |
4218 | return r; | |
4219 | } | |
4220 | ||
4221 | level--; | |
4222 | ||
4223 | prefix = strrep("\t", level); | |
4224 | if (!prefix) | |
4225 | return log_oom(); | |
4226 | ||
4227 | if (type == SD_BUS_TYPE_ARRAY) | |
c430fee6 | 4228 | fprintf(f, "%s} END_ARRAY \n", prefix); |
9a17484d | 4229 | else if (type == SD_BUS_TYPE_VARIANT) |
c430fee6 | 4230 | fprintf(f, "%s} END_VARIANT\n", prefix); |
9a17484d | 4231 | else if (type == SD_BUS_TYPE_STRUCT) |
c430fee6 | 4232 | fprintf(f, "%s} END_STRUCT\n", prefix); |
9a17484d | 4233 | else if (type == SD_BUS_TYPE_DICT_ENTRY) |
c430fee6 | 4234 | fprintf(f, "%s} END_DICT_ENTRY\n", prefix); |
9a17484d LP |
4235 | |
4236 | continue; | |
4237 | } | |
4238 | ||
4239 | prefix = strrep("\t", level); | |
4240 | if (!prefix) | |
4241 | return log_oom(); | |
4242 | ||
4243 | if (bus_type_is_container(type) > 0) { | |
4244 | r = sd_bus_message_enter_container(m, type, contents); | |
4245 | if (r < 0) { | |
4246 | log_error("Failed to enter container: %s", strerror(-r)); | |
4247 | return r; | |
4248 | } | |
4249 | ||
4250 | if (type == SD_BUS_TYPE_ARRAY) | |
c430fee6 | 4251 | fprintf(f, "%sBEGIN_ARRAY \"%s\" {\n", prefix, contents); |
9a17484d | 4252 | else if (type == SD_BUS_TYPE_VARIANT) |
c430fee6 | 4253 | fprintf(f, "%sBEGIN_VARIANT \"%s\" {\n", prefix, contents); |
9a17484d | 4254 | else if (type == SD_BUS_TYPE_STRUCT) |
c430fee6 | 4255 | fprintf(f, "%sBEGIN_STRUCT \"%s\" {\n", prefix, contents); |
9a17484d | 4256 | else if (type == SD_BUS_TYPE_DICT_ENTRY) |
c430fee6 | 4257 | fprintf(f, "%sBEGIN_DICT_ENTRY \"%s\" {\n", prefix, contents); |
9a17484d LP |
4258 | |
4259 | level ++; | |
4260 | ||
4261 | continue; | |
4262 | } | |
4263 | ||
4264 | r = sd_bus_message_read_basic(m, type, &basic); | |
4265 | if (r < 0) { | |
4266 | log_error("Failed to get basic: %s", strerror(-r)); | |
4267 | return r; | |
4268 | } | |
4269 | ||
c430fee6 LP |
4270 | assert(r > 0); |
4271 | ||
9a17484d LP |
4272 | switch (type) { |
4273 | ||
4274 | case SD_BUS_TYPE_BYTE: | |
c430fee6 | 4275 | fprintf(f, "%sBYTE: %u\n", prefix, basic.u8); |
9a17484d LP |
4276 | break; |
4277 | ||
4278 | case SD_BUS_TYPE_BOOLEAN: | |
c430fee6 | 4279 | fprintf(f, "%sBOOLEAN: %s\n", prefix, yes_no(basic.i)); |
9a17484d LP |
4280 | break; |
4281 | ||
4282 | case SD_BUS_TYPE_INT16: | |
c430fee6 | 4283 | fprintf(f, "%sINT16: %i\n", prefix, basic.s16); |
9a17484d LP |
4284 | break; |
4285 | ||
4286 | case SD_BUS_TYPE_UINT16: | |
c430fee6 | 4287 | fprintf(f, "%sUINT16: %u\n", prefix, basic.u16); |
9a17484d LP |
4288 | break; |
4289 | ||
4290 | case SD_BUS_TYPE_INT32: | |
c430fee6 | 4291 | fprintf(f, "%sINT32: %i\n", prefix, basic.s32); |
9a17484d LP |
4292 | break; |
4293 | ||
4294 | case SD_BUS_TYPE_UINT32: | |
c430fee6 | 4295 | fprintf(f, "%sUINT32: %u\n", prefix, basic.u32); |
9a17484d LP |
4296 | break; |
4297 | ||
4298 | case SD_BUS_TYPE_INT64: | |
c430fee6 | 4299 | fprintf(f, "%sINT64: %lli\n", prefix, (long long) basic.s64); |
9a17484d LP |
4300 | break; |
4301 | ||
4302 | case SD_BUS_TYPE_UINT64: | |
c430fee6 | 4303 | fprintf(f, "%sUINT64: %llu\n", prefix, (unsigned long long) basic.u64); |
9a17484d LP |
4304 | break; |
4305 | ||
4306 | case SD_BUS_TYPE_DOUBLE: | |
c430fee6 | 4307 | fprintf(f, "%sDOUBLE: %g\n", prefix, basic.d64); |
9a17484d LP |
4308 | break; |
4309 | ||
4310 | case SD_BUS_TYPE_STRING: | |
c430fee6 | 4311 | fprintf(f, "%sSTRING: \"%s\"\n", prefix, basic.string); |
9a17484d LP |
4312 | break; |
4313 | ||
4314 | case SD_BUS_TYPE_OBJECT_PATH: | |
c430fee6 | 4315 | fprintf(f, "%sOBJECT_PATH: \"%s\"\n", prefix, basic.string); |
9a17484d LP |
4316 | break; |
4317 | ||
4318 | case SD_BUS_TYPE_SIGNATURE: | |
c430fee6 | 4319 | fprintf(f, "%sSIGNATURE: \"%s\"\n", prefix, basic.string); |
9a17484d LP |
4320 | break; |
4321 | ||
4322 | case SD_BUS_TYPE_UNIX_FD: | |
c430fee6 | 4323 | fprintf(f, "%sUNIX_FD: %i\n", prefix, basic.i); |
9a17484d LP |
4324 | break; |
4325 | ||
4326 | default: | |
4327 | assert_not_reached("Unknown basic type."); | |
4328 | } | |
4329 | } | |
4330 | ||
c430fee6 | 4331 | fprintf(f, "} END_MESSAGE\n"); |
9a17484d | 4332 | return 0; |
de1c301e LP |
4333 | } |
4334 | ||
4335 | int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) { | |
4336 | size_t total; | |
de1c301e | 4337 | void *p, *e; |
bc7fd8cd LP |
4338 | unsigned i; |
4339 | struct bus_body_part *part; | |
de1c301e LP |
4340 | |
4341 | assert(m); | |
4342 | assert(buffer); | |
4343 | assert(sz); | |
4344 | ||
6629161f | 4345 | total = BUS_MESSAGE_SIZE(m); |
de1c301e LP |
4346 | |
4347 | p = malloc(total); | |
4348 | if (!p) | |
4349 | return -ENOMEM; | |
4350 | ||
c91cb83c | 4351 | e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m)); |
9b29bb68 | 4352 | MESSAGE_FOREACH_PART(part, i, m) |
bc7fd8cd | 4353 | e = mempcpy(e, part->data, part->size); |
2100fa10 LP |
4354 | |
4355 | assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p)); | |
de1c301e LP |
4356 | |
4357 | *buffer = p; | |
4358 | *sz = total; | |
4359 | ||
4360 | return 0; | |
4361 | } | |
89ffcd2a LP |
4362 | |
4363 | int bus_message_read_strv_extend(sd_bus_message *m, char ***l) { | |
4364 | int r; | |
4365 | ||
4366 | assert(m); | |
4367 | assert(l); | |
4368 | ||
4369 | r = sd_bus_message_enter_container(m, 'a', "s"); | |
4686d1b6 | 4370 | if (r <= 0) |
89ffcd2a LP |
4371 | return r; |
4372 | ||
4373 | for (;;) { | |
4374 | const char *s; | |
4375 | ||
4376 | r = sd_bus_message_read_basic(m, 's', &s); | |
4377 | if (r < 0) | |
4378 | return r; | |
4379 | if (r == 0) | |
4380 | break; | |
4381 | ||
4382 | r = strv_extend(l, s); | |
4383 | if (r < 0) | |
4384 | return r; | |
4385 | } | |
4386 | ||
4387 | r = sd_bus_message_exit_container(m); | |
4388 | if (r < 0) | |
4389 | return r; | |
4390 | ||
4391 | return 0; | |
4392 | } | |
392d5b37 | 4393 | |
4686d1b6 MAP |
4394 | int sd_bus_message_read_strv(sd_bus_message *m, char ***l) { |
4395 | char **strv = NULL; | |
4396 | int r; | |
4397 | ||
4398 | assert_return(m, -EINVAL); | |
4399 | assert_return(m->sealed, -EPERM); | |
4400 | assert_return(l, -EINVAL); | |
4401 | ||
4402 | r = bus_message_read_strv_extend(m, &strv); | |
4403 | if (r <= 0) { | |
4404 | strv_free(strv); | |
4405 | return r; | |
4406 | } | |
4407 | ||
4408 | *l = strv; | |
4409 | return 1; | |
4410 | } | |
4411 | ||
392d5b37 LP |
4412 | const char* bus_message_get_arg(sd_bus_message *m, unsigned i) { |
4413 | int r; | |
42c5aaf3 LP |
4414 | const char *t = NULL; |
4415 | unsigned j; | |
392d5b37 LP |
4416 | |
4417 | assert(m); | |
4418 | ||
4419 | r = sd_bus_message_rewind(m, true); | |
4420 | if (r < 0) | |
4421 | return NULL; | |
4422 | ||
42c5aaf3 LP |
4423 | for (j = 0; j <= i; j++) { |
4424 | char type; | |
4425 | ||
392d5b37 LP |
4426 | r = sd_bus_message_peek_type(m, &type, NULL); |
4427 | if (r < 0) | |
4428 | return NULL; | |
4429 | ||
4430 | if (type != SD_BUS_TYPE_STRING && | |
4431 | type != SD_BUS_TYPE_OBJECT_PATH && | |
4432 | type != SD_BUS_TYPE_SIGNATURE) | |
4433 | return NULL; | |
4434 | ||
4435 | r = sd_bus_message_read_basic(m, type, &t); | |
4436 | if (r < 0) | |
4437 | return NULL; | |
392d5b37 LP |
4438 | } |
4439 | ||
392d5b37 LP |
4440 | return t; |
4441 | } | |
2100fa10 | 4442 | |
c91cb83c LP |
4443 | bool bus_header_is_complete(struct bus_header *h, size_t size) { |
4444 | size_t full; | |
4445 | ||
4446 | assert(h); | |
4447 | assert(size); | |
4448 | ||
4449 | if (size < sizeof(struct bus_header)) | |
4450 | return false; | |
4451 | ||
4452 | full = sizeof(struct bus_header) + | |
4453 | (h->endian == SD_BUS_NATIVE_ENDIAN ? h->fields_size : bswap_32(h->fields_size)); | |
4454 | ||
4455 | return size >= full; | |
4456 | } | |
4457 | ||
4458 | int bus_header_message_size(struct bus_header *h, size_t *sum) { | |
6629161f LP |
4459 | size_t fs, bs; |
4460 | ||
4461 | assert(h); | |
4462 | assert(sum); | |
4463 | ||
4464 | if (h->endian == SD_BUS_NATIVE_ENDIAN) { | |
4465 | fs = h->fields_size; | |
4466 | bs = h->body_size; | |
4467 | } else if (h->endian == SD_BUS_REVERSE_ENDIAN) { | |
4468 | fs = bswap_32(h->fields_size); | |
4469 | bs = bswap_32(h->body_size); | |
4470 | } else | |
4471 | return -EBADMSG; | |
2100fa10 | 4472 | |
6629161f LP |
4473 | *sum = sizeof(struct bus_header) + ALIGN8(fs) + bs; |
4474 | return 0; | |
2100fa10 | 4475 | } |
eb01ba5d | 4476 | |
40ca29a1 LP |
4477 | int sd_bus_message_get_errno(sd_bus_message *m) { |
4478 | assert_return(m, -EINVAL); | |
eb01ba5d | 4479 | |
40ca29a1 | 4480 | if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) |
eb01ba5d LP |
4481 | return 0; |
4482 | ||
40ca29a1 | 4483 | return sd_bus_error_get_errno(&m->error); |
eb01ba5d | 4484 | } |
29ddb38f | 4485 | |
40ca29a1 | 4486 | const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) { |
29ddb38f LP |
4487 | struct bus_container *c; |
4488 | ||
9d6c7c82 | 4489 | assert_return(m, NULL); |
29ddb38f LP |
4490 | |
4491 | c = complete ? &m->root_container : message_get_container(m); | |
40ca29a1 | 4492 | return c->signature ?: ""; |
29ddb38f | 4493 | } |
c430fee6 LP |
4494 | |
4495 | int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) { | |
4496 | bool done_something = false; | |
4497 | int r; | |
4498 | ||
80ba3b84 LP |
4499 | assert_return(m, -EINVAL); |
4500 | assert_return(source, -EINVAL); | |
4501 | assert_return(!m->sealed, -EPERM); | |
4502 | assert_return(source->sealed, -EPERM); | |
4503 | ||
c430fee6 LP |
4504 | do { |
4505 | const char *contents; | |
4506 | char type; | |
4507 | union { | |
4508 | uint8_t u8; | |
4509 | uint16_t u16; | |
4510 | int16_t s16; | |
4511 | uint32_t u32; | |
4512 | int32_t s32; | |
4513 | uint64_t u64; | |
4514 | int64_t s64; | |
4515 | double d64; | |
4516 | const char *string; | |
4517 | int i; | |
4518 | } basic; | |
4519 | ||
4520 | r = sd_bus_message_peek_type(source, &type, &contents); | |
4521 | if (r < 0) | |
4522 | return r; | |
4523 | if (r == 0) | |
4524 | break; | |
4525 | ||
4526 | done_something = true; | |
4527 | ||
4528 | if (bus_type_is_container(type) > 0) { | |
4529 | ||
4530 | r = sd_bus_message_enter_container(source, type, contents); | |
4531 | if (r < 0) | |
4532 | return r; | |
4533 | ||
4534 | r = sd_bus_message_open_container(m, type, contents); | |
4535 | if (r < 0) | |
4536 | return r; | |
4537 | ||
4538 | r = sd_bus_message_copy(m, source, true); | |
4539 | if (r < 0) | |
4540 | return r; | |
4541 | ||
4542 | r = sd_bus_message_close_container(m); | |
4543 | if (r < 0) | |
4544 | return r; | |
4545 | ||
4546 | r = sd_bus_message_exit_container(source); | |
4547 | if (r < 0) | |
4548 | return r; | |
4549 | ||
4550 | continue; | |
4551 | } | |
4552 | ||
4553 | r = sd_bus_message_read_basic(source, type, &basic); | |
4554 | if (r < 0) | |
4555 | return r; | |
4556 | ||
4557 | assert(r > 0); | |
4558 | ||
4559 | if (type == SD_BUS_TYPE_OBJECT_PATH || | |
4560 | type == SD_BUS_TYPE_SIGNATURE || | |
4561 | type == SD_BUS_TYPE_STRING) | |
4562 | r = sd_bus_message_append_basic(m, type, basic.string); | |
4563 | else | |
4564 | r = sd_bus_message_append_basic(m, type, &basic); | |
4565 | ||
4566 | if (r < 0) | |
4567 | return r; | |
4568 | ||
4569 | } while (all); | |
4570 | ||
4571 | return done_something; | |
4572 | } | |
4573 | ||
4574 | int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) { | |
4575 | const char *c; | |
4576 | char t; | |
4577 | int r; | |
4578 | ||
4579 | assert_return(m, -EINVAL); | |
4580 | assert_return(m->sealed, -EPERM); | |
4581 | assert_return(!type || bus_type_is_valid(type), -EINVAL); | |
4582 | assert_return(!contents || signature_is_valid(contents, true), -EINVAL); | |
4583 | assert_return(type || contents, -EINVAL); | |
4584 | assert_return(!contents || !type || bus_type_is_container(type), -EINVAL); | |
4585 | ||
4586 | r = sd_bus_message_peek_type(m, &t, &c); | |
4587 | if (r <= 0) | |
4588 | return r; | |
4589 | ||
4590 | if (type != 0 && type != t) | |
4591 | return 0; | |
4592 | ||
4593 | if (contents && !streq_ptr(contents, c)) | |
4594 | return 0; | |
4595 | ||
4596 | return 1; | |
4597 | } |