| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
#include "sd-future.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "bus-future.h" |
| 8 |
#include "bus-internal.h" |
| 9 |
#include "bus-message.h" |
| 10 |
|
| 11 |
typedef struct BusFuture { |
| 12 |
sd_bus_slot *slot; |
| 13 |
sd_bus_message *reply; |
| 14 |
} BusFuture; |
| 15 |
|
| 16 |
static void* bus_future_alloc(void) { |
| 17 |
return new0(BusFuture, 1); |
| 18 |
} |
| 19 |
|
| 20 |
static void bus_future_free(sd_future *f) { |
| 21 |
BusFuture *bf = ASSERT_PTR(sd_future_get_private(f)); |
| 22 |
sd_bus_slot_unref(bf->slot); |
| 23 |
sd_bus_message_unref(bf->reply); |
| 24 |
free(bf); |
| 25 |
} |
| 26 |
|
| 27 |
static int bus_future_cancel(sd_future *f) { |
| 28 |
BusFuture *bf = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 29 |
|
| 30 |
bf->slot = sd_bus_slot_unref(bf->slot); |
| 31 |
return sd_future_resolve(f, -ECANCELED); |
| 32 |
} |
| 33 |
|
| 34 |
static const sd_future_ops bus_future_ops = { |
| 35 |
.size = sizeof(sd_future_ops), |
| 36 |
.alloc = bus_future_alloc, |
| 37 |
.free = bus_future_free, |
| 38 |
.cancel = bus_future_cancel, |
| 39 |
}; |
| 40 |
|
| 41 |
static int bus_future_handler(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 42 |
sd_future *f = ASSERT_PTR(userdata); |
| 43 |
BusFuture *bf = ASSERT_PTR(sd_future_get_private(f)); |
| 44 |
int r; |
| 45 |
|
| 46 |
/* Resolve with 0 on a success reply and -errno (derived from the D-Bus error name) on a |
| 47 |
* method error reply, so a caller awaiting the future learns about call failures from the |
| 48 |
* resolution value alone. The reply itself is always stashed in bf->reply so |
| 49 |
* future_get_bus_reply() can hand back the detailed sd_bus_error (name + message) on |
| 50 |
* top of the bare errno. Cancellation surfaces as -ECANCELED via bus_future_cancel(), |
| 51 |
* with bf->reply left NULL — callers can distinguish "got an error reply" from "no reply |
| 52 |
* will arrive" by whether future_get_bus_reply() can produce a message. */ |
| 53 |
bf->slot = sd_bus_slot_unref(bf->slot); |
| 54 |
bf->reply = sd_bus_message_ref(m); |
| 55 |
|
| 56 |
r = sd_bus_message_is_method_error(m, NULL) ? -sd_bus_message_get_errno(m) : 0; |
| 57 |
return sd_future_resolve(f, r); |
| 58 |
} |
| 59 |
|
| 60 |
int bus_call_future(sd_bus *bus, sd_bus_message *m, uint64_t usec, sd_future **ret) { |
| 61 |
int r; |
| 62 |
|
| 63 |
assert(bus); |
| 64 |
assert(m); |
| 65 |
assert(ret); |
| 66 |
|
| 67 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 68 |
r = sd_future_new(&bus_future_ops, &f); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
|
| 72 |
BusFuture *bf = sd_future_get_private(f); |
| 73 |
|
| 74 |
r = sd_bus_call_async(bus, &bf->slot, m, bus_future_handler, f, usec); |
| 75 |
if (r < 0) |
| 76 |
return r; |
| 77 |
|
| 78 |
*ret = TAKE_PTR(f); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int future_get_bus_reply(sd_future *f, sd_bus_error *reterr_error, sd_bus_message **ret_reply) { |
| 83 |
BusFuture *bf = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 84 |
sd_bus_message *reply = ASSERT_PTR(bf->reply); |
| 85 |
|
| 86 |
assert(sd_future_get_ops(f) == &bus_future_ops); |
| 87 |
assert(sd_future_state(f) == SD_FUTURE_RESOLVED); |
| 88 |
|
| 89 |
if (sd_bus_message_is_method_error(reply, NULL)) { |
| 90 |
if (reterr_error) |
| 91 |
return sd_bus_error_copy(reterr_error, sd_bus_message_get_error(reply)); |
| 92 |
return -sd_bus_message_get_errno(reply); |
| 93 |
} |
| 94 |
|
| 95 |
if (reply->n_fds > 0 && !sd_bus_message_get_bus(reply)->accept_fd) |
| 96 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, |
| 97 |
"Reply message contained file descriptors which I couldn't accept. Sorry."); |
| 98 |
|
| 99 |
if (reterr_error) |
| 100 |
*reterr_error = SD_BUS_ERROR_NULL; |
| 101 |
if (ret_reply) |
| 102 |
*ret_reply = sd_bus_message_ref(reply); |
| 103 |
|
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
int bus_call_suspend( |
| 108 |
sd_bus *bus, |
| 109 |
sd_bus_message *m, |
| 110 |
uint64_t usec, |
| 111 |
sd_bus_error *reterr_error, |
| 112 |
sd_bus_message **ret_reply) { |
| 113 |
|
| 114 |
int r; |
| 115 |
|
| 116 |
assert(bus); |
| 117 |
assert(m); |
| 118 |
assert(sd_fiber_is_running()); |
| 119 |
|
| 120 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *f = NULL; |
| 121 |
r = bus_call_future(bus, m, usec, &f); |
| 122 |
if (r < 0) |
| 123 |
return sd_bus_error_set_errno(reterr_error, r); |
| 124 |
|
| 125 |
r = sd_fiber_suspend(); |
| 126 |
|
| 127 |
/* If the future isn't resolved, the suspend was interrupted before a reply arrived (fiber |
| 128 |
* cancelled, fiber-wide SD_FIBER_TIMEOUT scope expired, …). There's no reply to extract, |
| 129 |
* so surface the resume error directly. When the future is resolved, future_get_bus_reply() |
| 130 |
* recovers either the reply or the detailed sd_bus_error from the error reply. */ |
| 131 |
if (sd_future_state(f) != SD_FUTURE_RESOLVED) |
| 132 |
return sd_bus_error_set_errno(reterr_error, r); |
| 133 |
|
| 134 |
return future_get_bus_reply(f, reterr_error, ret_reply); |
| 135 |
} |