| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
#include "sd-event.h" |
| 5 |
#include "sd-future.h" |
| 6 |
#include "sd-id128.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "bus-internal.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "fs-util.h" |
| 12 |
#include "mkdir.h" |
| 13 |
#include "path-util.h" |
| 14 |
#include "random-util.h" |
| 15 |
#include "rm-rf.h" |
| 16 |
#include "socket-util.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "tests.h" |
| 19 |
#include "time-util.h" |
| 20 |
#include "tmpfile-util.h" |
| 21 |
|
| 22 |
static int method_foobar(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 23 |
log_info("Got Foobar() call."); |
| 24 |
|
| 25 |
ASSERT_OK(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0)); |
| 26 |
return sd_bus_reply_method_return(m, NULL); |
| 27 |
} |
| 28 |
|
| 29 |
static int method_exit(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 30 |
log_info("Got Exit() call"); |
| 31 |
|
| 32 |
ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 33 |
/* Simulate D-Bus going away to test the bus_exit_now() path with exit_on_disconnect set */ |
| 34 |
bus_enter_closing(sd_bus_message_get_bus(m), EXIT_FAILURE); |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
static const sd_bus_vtable vtable[] = { |
| 39 |
SD_BUS_VTABLE_START(0), |
| 40 |
SD_BUS_METHOD("Foobar", NULL, NULL, method_foobar, SD_BUS_VTABLE_UNPRIVILEGED), |
| 41 |
SD_BUS_METHOD("Exit", NULL, NULL, method_exit, SD_BUS_VTABLE_UNPRIVILEGED), |
| 42 |
SD_BUS_VTABLE_END, |
| 43 |
}; |
| 44 |
|
| 45 |
static int server(void *userdata) { |
| 46 |
_cleanup_free_ char *suffixed = NULL, *suffixed_basename = NULL, *suffixed2 = NULL, *d = NULL; |
| 47 |
_cleanup_close_ int fd = -EBADF; |
| 48 |
union sockaddr_union u; |
| 49 |
const char *path = ASSERT_PTR(userdata); |
| 50 |
int r; |
| 51 |
|
| 52 |
log_debug("Initializing server"); |
| 53 |
|
| 54 |
/* Let's play some games, by slowly creating the socket directory, and renaming it in the middle */ |
| 55 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 56 |
|
| 57 |
ASSERT_OK(mkdir_parents(path, 0755)); |
| 58 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 59 |
|
| 60 |
ASSERT_OK(path_extract_directory(path, &d)); |
| 61 |
ASSERT_OK(asprintf(&suffixed, "%s.%" PRIx64, d, random_u64())); |
| 62 |
ASSERT_OK_ERRNO(rename(d, suffixed)); |
| 63 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 64 |
|
| 65 |
ASSERT_OK(asprintf(&suffixed2, "%s.%" PRIx64, d, random_u64())); |
| 66 |
ASSERT_OK_ERRNO(symlink(suffixed2, d)); |
| 67 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 68 |
|
| 69 |
ASSERT_OK(path_extract_filename(suffixed, &suffixed_basename)); |
| 70 |
ASSERT_OK_ERRNO(symlink(suffixed_basename, suffixed2)); |
| 71 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 72 |
|
| 73 |
socklen_t sa_len; |
| 74 |
r = sockaddr_un_set_path(&u.un, path); |
| 75 |
ASSERT_OK(r); |
| 76 |
sa_len = r; |
| 77 |
|
| 78 |
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); |
| 79 |
ASSERT_OK_ERRNO(fd); |
| 80 |
|
| 81 |
ASSERT_OK_ERRNO(bind(fd, &u.sa, sa_len)); |
| 82 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 83 |
|
| 84 |
ASSERT_OK_ERRNO(listen(fd, SOMAXCONN_DELUXE)); |
| 85 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 86 |
|
| 87 |
ASSERT_OK(touch(path)); |
| 88 |
ASSERT_OK(sd_fiber_sleep(100 * USEC_PER_MSEC)); |
| 89 |
|
| 90 |
log_debug("Initialized server"); |
| 91 |
|
| 92 |
for (;;) { |
| 93 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 94 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 95 |
sd_id128_t id; |
| 96 |
int bus_fd, code; |
| 97 |
|
| 98 |
ASSERT_OK(sd_id128_randomize(&id)); |
| 99 |
|
| 100 |
ASSERT_OK(sd_event_new(&event)); |
| 101 |
|
| 102 |
ASSERT_OK(bus_fd = sd_fiber_accept(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC)); |
| 103 |
|
| 104 |
log_debug("Accepted server connection"); |
| 105 |
|
| 106 |
ASSERT_OK(sd_bus_new(&bus)); |
| 107 |
ASSERT_OK(sd_bus_set_exit_on_disconnect(bus, true)); |
| 108 |
ASSERT_OK(sd_bus_set_description(bus, "server")); |
| 109 |
ASSERT_OK(sd_bus_set_fd(bus, bus_fd, bus_fd)); |
| 110 |
ASSERT_OK(sd_bus_set_server(bus, true, id)); |
| 111 |
/* ASSERT_OK(sd_bus_set_anonymous(bus, true)); */ |
| 112 |
|
| 113 |
ASSERT_OK(sd_bus_attach_event(bus, event, 0)); |
| 114 |
|
| 115 |
ASSERT_OK(sd_bus_add_object_vtable(bus, NULL, "/foo", "foo.TestInterface", vtable, NULL)); |
| 116 |
|
| 117 |
ASSERT_OK(sd_bus_start(bus)); |
| 118 |
|
| 119 |
ASSERT_OK(sd_event_loop(event)); |
| 120 |
|
| 121 |
ASSERT_OK(sd_event_get_exit_code(event, &code)); |
| 122 |
|
| 123 |
if (code > 0) |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
log_debug("Server done"); |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int client1(void *userdata) { |
| 133 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 134 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 135 |
const char *path = ASSERT_PTR(userdata), *t; |
| 136 |
|
| 137 |
log_debug("Initializing client1"); |
| 138 |
|
| 139 |
ASSERT_OK(sd_bus_new(&bus)); |
| 140 |
ASSERT_OK(sd_bus_set_description(bus, "client1")); |
| 141 |
|
| 142 |
t = strjoina("unix:path=", path); |
| 143 |
ASSERT_OK(sd_bus_set_address(bus, t)); |
| 144 |
ASSERT_OK(sd_bus_set_watch_bind(bus, true)); |
| 145 |
ASSERT_OK(sd_bus_start(bus)); |
| 146 |
|
| 147 |
ASSERT_OK(sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Foobar", &error, NULL, NULL)); |
| 148 |
|
| 149 |
log_debug("Client1 done"); |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
static int client2(void *userdata) { |
| 155 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 156 |
const char *path = ASSERT_PTR(userdata), *t; |
| 157 |
|
| 158 |
log_debug("Initializing client2"); |
| 159 |
|
| 160 |
ASSERT_OK(sd_bus_new(&bus)); |
| 161 |
ASSERT_OK(sd_bus_set_description(bus, "client2")); |
| 162 |
|
| 163 |
t = strjoina("unix:path=", path); |
| 164 |
ASSERT_OK(sd_bus_set_address(bus, t)); |
| 165 |
ASSERT_OK(sd_bus_set_watch_bind(bus, true)); |
| 166 |
ASSERT_OK(sd_bus_attach_event(bus, sd_fiber_get_event(), 0)); |
| 167 |
ASSERT_OK(sd_bus_start(bus)); |
| 168 |
|
| 169 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 170 |
ASSERT_OK(sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Foobar", NULL, &m, NULL)); |
| 171 |
|
| 172 |
ASSERT_OK_ZERO(sd_bus_message_is_method_error(m, NULL)); |
| 173 |
log_debug("Client2 done"); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
typedef struct RequestExitArgs { |
| 179 |
const char *path; |
| 180 |
sd_future *client1; |
| 181 |
sd_future *client2; |
| 182 |
} RequestExitArgs; |
| 183 |
|
| 184 |
static int request_exit(void *userdata) { |
| 185 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 186 |
RequestExitArgs *args = ASSERT_PTR(userdata); |
| 187 |
const char *t; |
| 188 |
|
| 189 |
/* Wait for all client fibers to complete before requesting exit */ |
| 190 |
ASSERT_OK(sd_fiber_await(args->client1)); |
| 191 |
ASSERT_OK(sd_fiber_await(args->client2)); |
| 192 |
|
| 193 |
ASSERT_OK(sd_bus_new(&bus)); |
| 194 |
|
| 195 |
t = strjoina("unix:path=", args->path); |
| 196 |
ASSERT_OK(sd_bus_set_address(bus, t)); |
| 197 |
ASSERT_OK(sd_bus_set_watch_bind(bus, true)); |
| 198 |
ASSERT_OK(sd_bus_set_description(bus, "request-exit")); |
| 199 |
ASSERT_OK(sd_bus_start(bus)); |
| 200 |
|
| 201 |
ASSERT_OK(sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Exit", NULL, NULL, NULL)); |
| 202 |
|
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
int main(int argc, char *argv[]) { |
| 207 |
_cleanup_(rm_rf_physical_and_freep) char *d = NULL; |
| 208 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 209 |
_cleanup_(sd_future_unrefp) sd_future *f_server = NULL, *f_client1 = NULL, *f_client2 = NULL, *f_exit = NULL; |
| 210 |
char *path; |
| 211 |
|
| 212 |
test_setup_logging(LOG_DEBUG); |
| 213 |
|
| 214 |
/* We use /dev/shm here rather than /tmp, since some weird distros might set up /tmp as some weird fs that |
| 215 |
* doesn't support inotify properly. */ |
| 216 |
ASSERT_OK(mkdtemp_malloc("/dev/shm/systemd-watch-bind-XXXXXX", &d)); |
| 217 |
|
| 218 |
path = strjoina(d, "/this/is/a/socket"); |
| 219 |
|
| 220 |
ASSERT_OK(sd_event_new(&e)); |
| 221 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 222 |
|
| 223 |
ASSERT_OK(sd_fiber_new(e, "server", server, path, /* destroy= */ NULL, &f_server)); |
| 224 |
|
| 225 |
ASSERT_OK(sd_fiber_new(e, "client-1", client1, path, /* destroy= */ NULL, &f_client1)); |
| 226 |
ASSERT_OK(sd_fiber_new(e, "client-2", client2, path, /* destroy= */ NULL, &f_client2)); |
| 227 |
|
| 228 |
RequestExitArgs args = { |
| 229 |
.path = path, |
| 230 |
.client1 = f_client1, |
| 231 |
.client2 = f_client2, |
| 232 |
}; |
| 233 |
ASSERT_OK(sd_fiber_new(e, "request-exit", request_exit, &args, /* destroy= */ NULL, &f_exit)); |
| 234 |
|
| 235 |
ASSERT_OK(sd_event_loop(e)); |
| 236 |
|
| 237 |
ASSERT_OK(sd_future_result(f_client1)); |
| 238 |
ASSERT_OK(sd_future_result(f_client2)); |
| 239 |
ASSERT_OK(sd_future_result(f_exit)); |
| 240 |
ASSERT_OK(sd_future_result(f_server)); |
| 241 |
|
| 242 |
return EXIT_SUCCESS; |
| 243 |
} |