| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/socket.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-future.h" |
| 8 |
|
| 9 |
#include "bus-internal.h" |
| 10 |
#include "tests.h" |
| 11 |
#include "time-util.h" |
| 12 |
|
| 13 |
typedef struct Context { |
| 14 |
/* Counters for the concurrency check: every Concurrent invocation bumps in_flight on entry |
| 15 |
* and drops it on exit, and tracks the maximum observed concurrency. If fiber dispatch |
| 16 |
* works, two overlapping client calls must both be inside the handler at the same time, |
| 17 |
* giving a max of at least 2. */ |
| 18 |
int in_flight; |
| 19 |
int max_in_flight; |
| 20 |
sd_future *waiter; |
| 21 |
} Context; |
| 22 |
|
| 23 |
static int method_concurrent(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 24 |
Context *c = ASSERT_PTR(userdata); |
| 25 |
|
| 26 |
ASSERT_OK_POSITIVE(sd_fiber_is_running()); |
| 27 |
|
| 28 |
c->in_flight++; |
| 29 |
if (c->in_flight > c->max_in_flight) |
| 30 |
c->max_in_flight = c->in_flight; |
| 31 |
|
| 32 |
/* Synchronize on the peer instead of sleeping: the first handler to enter stashes its |
| 33 |
* fiber and suspends; the second resumes it and then yields, so the first runs to |
| 34 |
* completion (sending its reply) before the second proceeds. Both are therefore in |
| 35 |
* flight at the same time regardless of how long any individual dispatch takes, and |
| 36 |
* the reply order matches the request order. */ |
| 37 |
if (c->in_flight < 2) { |
| 38 |
assert(!c->waiter); |
| 39 |
c->waiter = sd_fiber_get_current(); |
| 40 |
ASSERT_OK(sd_fiber_suspend()); |
| 41 |
} else { |
| 42 |
ASSERT_OK(sd_fiber_resume(TAKE_PTR(c->waiter), 0)); |
| 43 |
ASSERT_OK(sd_fiber_yield()); |
| 44 |
} |
| 45 |
|
| 46 |
c->in_flight--; |
| 47 |
|
| 48 |
return sd_bus_reply_method_return(m, NULL); |
| 49 |
} |
| 50 |
|
| 51 |
static int method_fail_errno(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 52 |
ASSERT_OK_POSITIVE(sd_fiber_is_running()); |
| 53 |
|
| 54 |
/* Yielding first exercises the deferred-error path in the fiber entry: the handler returns |
| 55 |
* a negative errno after suspending, and bus_maybe_reply_error() must still turn that into |
| 56 |
* a matching sd_bus error reply. */ |
| 57 |
ASSERT_OK(sd_fiber_sleep(1 * USEC_PER_MSEC)); |
| 58 |
|
| 59 |
return -EACCES; |
| 60 |
} |
| 61 |
|
| 62 |
static int method_fail_error(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 63 |
ASSERT_OK_POSITIVE(sd_fiber_is_running()); |
| 64 |
|
| 65 |
ASSERT_OK(sd_fiber_sleep(1 * USEC_PER_MSEC)); |
| 66 |
|
| 67 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "bad arguments from fiber"); |
| 68 |
} |
| 69 |
|
| 70 |
static const sd_bus_vtable vtable[] = { |
| 71 |
SD_BUS_VTABLE_START(0), |
| 72 |
SD_BUS_METHOD("Concurrent", NULL, NULL, method_concurrent, |
| 73 |
SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_METHOD_FIBER), |
| 74 |
SD_BUS_METHOD("FailErrno", NULL, NULL, method_fail_errno, |
| 75 |
SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_METHOD_FIBER), |
| 76 |
SD_BUS_METHOD("FailError", NULL, NULL, method_fail_error, |
| 77 |
SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_METHOD_FIBER), |
| 78 |
SD_BUS_VTABLE_END, |
| 79 |
}; |
| 80 |
|
| 81 |
typedef struct Setup { |
| 82 |
int fds[2]; |
| 83 |
Context *c; |
| 84 |
} Setup; |
| 85 |
|
| 86 |
static int attach_pair(Setup *s, sd_bus **ret_server, sd_bus **ret_client) { |
| 87 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *server = NULL, *client = NULL; |
| 88 |
sd_id128_t id; |
| 89 |
|
| 90 |
assert(ret_server); |
| 91 |
assert(ret_client); |
| 92 |
|
| 93 |
ASSERT_OK(sd_id128_randomize(&id)); |
| 94 |
ASSERT_OK(sd_bus_new(&server)); |
| 95 |
ASSERT_OK(sd_bus_set_description(server, "server")); |
| 96 |
ASSERT_OK(sd_bus_set_fd(server, s->fds[0], s->fds[0])); |
| 97 |
ASSERT_OK(sd_bus_set_server(server, true, id)); |
| 98 |
ASSERT_OK(sd_bus_attach_event(server, sd_fiber_get_event(), 0)); |
| 99 |
ASSERT_OK(sd_bus_add_object_vtable(server, NULL, "/test", "test.Fiber", vtable, s->c)); |
| 100 |
ASSERT_OK(sd_bus_start(server)); |
| 101 |
|
| 102 |
ASSERT_OK(sd_bus_new(&client)); |
| 103 |
ASSERT_OK(sd_bus_set_description(client, "client")); |
| 104 |
ASSERT_OK(sd_bus_set_fd(client, s->fds[1], s->fds[1])); |
| 105 |
ASSERT_OK(sd_bus_attach_event(client, sd_fiber_get_event(), 0)); |
| 106 |
ASSERT_OK(sd_bus_start(client)); |
| 107 |
|
| 108 |
*ret_server = TAKE_PTR(server); |
| 109 |
*ret_client = TAKE_PTR(client); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static int call_concurrent_fiber(void *userdata) { |
| 114 |
sd_bus *client = ASSERT_PTR(userdata); |
| 115 |
|
| 116 |
/* A plain suspending sd_bus_call() — on a fiber this goes through sd_bus_call_suspend() |
| 117 |
* which multiplexes onto the single client connection, so multiple caller fibers can have |
| 118 |
* calls in flight at the same time. */ |
| 119 |
return sd_bus_call_method(client, NULL, "/test", "test.Fiber", "Concurrent", |
| 120 |
NULL, NULL, NULL); |
| 121 |
} |
| 122 |
|
| 123 |
static int concurrency_fiber(void *userdata) { |
| 124 |
Setup *s = ASSERT_PTR(userdata); |
| 125 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *server = NULL, *client = NULL; |
| 126 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *f_a = NULL, *f_b = NULL; |
| 127 |
|
| 128 |
ASSERT_OK(attach_pair(s, &server, &client)); |
| 129 |
|
| 130 |
/* Two concurrent calls on the shared client bus. Each lands in method_concurrent which |
| 131 |
* blocks on the peer; if fiber dispatch works the second is entered while the first is |
| 132 |
* suspended, so max_in_flight on the context reaches 2. */ |
| 133 |
ASSERT_OK(sd_fiber_new(sd_fiber_get_event(), "call-a", call_concurrent_fiber, client, |
| 134 |
/* destroy= */ NULL, &f_a)); |
| 135 |
ASSERT_OK(sd_fiber_new(sd_fiber_get_event(), "call-b", call_concurrent_fiber, client, |
| 136 |
/* destroy= */ NULL, &f_b)); |
| 137 |
|
| 138 |
ASSERT_OK(sd_fiber_await(f_a)); |
| 139 |
ASSERT_OK(sd_fiber_await(f_b)); |
| 140 |
|
| 141 |
ASSERT_OK(sd_future_result(f_a)); |
| 142 |
ASSERT_OK(sd_future_result(f_b)); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
TEST(fiber_method_concurrency) { |
| 147 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 148 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 149 |
Context c = {}; |
| 150 |
Setup s = { .c = &c }; |
| 151 |
|
| 152 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM, 0, s.fds)); |
| 153 |
|
| 154 |
ASSERT_OK(sd_event_new(&e)); |
| 155 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 156 |
|
| 157 |
ASSERT_OK(sd_fiber_new(e, "concurrency", concurrency_fiber, &s, /* destroy= */ NULL, &f)); |
| 158 |
|
| 159 |
ASSERT_OK(sd_event_loop(e)); |
| 160 |
|
| 161 |
ASSERT_OK(sd_future_result(f)); |
| 162 |
ASSERT_GE(c.max_in_flight, 2); |
| 163 |
} |
| 164 |
|
| 165 |
static int errors_fiber(void *userdata) { |
| 166 |
Setup *s = ASSERT_PTR(userdata); |
| 167 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *server = NULL, *client = NULL; |
| 168 |
|
| 169 |
ASSERT_OK(attach_pair(s, &server, &client)); |
| 170 |
|
| 171 |
/* A fiber handler that returns a negative errno gets turned into a matching sd_bus error |
| 172 |
* reply (bus_maybe_reply_error → sd_bus_reply_method_errno). */ |
| 173 |
_cleanup_(sd_bus_error_free) sd_bus_error e1 = SD_BUS_ERROR_NULL; |
| 174 |
ASSERT_ERROR(sd_bus_call_method(client, NULL, "/test", "test.Fiber", "FailErrno", |
| 175 |
&e1, NULL, NULL), |
| 176 |
EACCES); |
| 177 |
ASSERT_TRUE(sd_bus_error_has_name(&e1, SD_BUS_ERROR_ACCESS_DENIED)); |
| 178 |
|
| 179 |
/* A fiber handler that populates sd_bus_error directly propagates both name and message. */ |
| 180 |
_cleanup_(sd_bus_error_free) sd_bus_error e2 = SD_BUS_ERROR_NULL; |
| 181 |
ASSERT_FAIL(sd_bus_call_method(client, NULL, "/test", "test.Fiber", "FailError", |
| 182 |
&e2, NULL, NULL)); |
| 183 |
ASSERT_TRUE(sd_bus_error_has_name(&e2, SD_BUS_ERROR_INVALID_ARGS)); |
| 184 |
ASSERT_STREQ(e2.message, "bad arguments from fiber"); |
| 185 |
|
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
TEST(fiber_method_errors) { |
| 190 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 191 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 192 |
Context c = {}; |
| 193 |
Setup s = { .c = &c }; |
| 194 |
|
| 195 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM, 0, s.fds)); |
| 196 |
|
| 197 |
ASSERT_OK(sd_event_new(&e)); |
| 198 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 199 |
|
| 200 |
ASSERT_OK(sd_fiber_new(e, "errors", errors_fiber, &s, /* destroy= */ NULL, &f)); |
| 201 |
|
| 202 |
ASSERT_OK(sd_event_loop(e)); |
| 203 |
|
| 204 |
ASSERT_OK(sd_future_result(f)); |
| 205 |
} |
| 206 |
|
| 207 |
DEFINE_TEST_MAIN(LOG_DEBUG); |