| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/resource.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-bus.h" |
| 8 |
#include "sd-future.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "bus-error.h" |
| 12 |
#include "bus-internal.h" |
| 13 |
#include "bus-match.h" |
| 14 |
#include "bus-message.h" |
| 15 |
#include "errno-util.h" |
| 16 |
#include "fd-util.h" |
| 17 |
#include "format-util.h" |
| 18 |
#include "log.h" |
| 19 |
#include "memfd-util.h" |
| 20 |
#include "stat-util.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "tests.h" |
| 23 |
#include "time-util.h" |
| 24 |
|
| 25 |
static int match_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 26 |
log_info("Match triggered! destination=%s interface=%s member=%s", |
| 27 |
strna(sd_bus_message_get_destination(m)), |
| 28 |
strna(sd_bus_message_get_interface(m)), |
| 29 |
strna(sd_bus_message_get_member(m))); |
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
static int object_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 34 |
int r; |
| 35 |
|
| 36 |
if (sd_bus_message_is_method_error(m, NULL)) |
| 37 |
return 0; |
| 38 |
|
| 39 |
if (sd_bus_message_is_method_call(m, "org.object.test", "Foobar")) { |
| 40 |
log_info("Invoked Foobar() on %s", sd_bus_message_get_path(m)); |
| 41 |
|
| 42 |
r = sd_bus_reply_method_return(m, NULL); |
| 43 |
if (r < 0) |
| 44 |
return log_error_errno(r, "Failed to send reply: %m"); |
| 45 |
|
| 46 |
return 1; |
| 47 |
} |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
static int server_init(sd_bus **ret) { |
| 53 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 54 |
const char *unique, *desc; |
| 55 |
sd_id128_t id; |
| 56 |
int r; |
| 57 |
|
| 58 |
assert(ret); |
| 59 |
|
| 60 |
r = sd_bus_open_user_with_description(&bus, "my bus!"); |
| 61 |
if (r < 0) |
| 62 |
return log_error_errno(r, "Failed to connect to user bus: %m"); |
| 63 |
|
| 64 |
r = sd_bus_get_bus_id(bus, &id); |
| 65 |
if (r < 0) |
| 66 |
return log_error_errno(r, "Failed to get server ID: %m"); |
| 67 |
|
| 68 |
r = sd_bus_get_unique_name(bus, &unique); |
| 69 |
if (r < 0) |
| 70 |
return log_error_errno(r, "Failed to get unique name: %m"); |
| 71 |
|
| 72 |
ASSERT_OK(sd_bus_get_description(bus, &desc)); |
| 73 |
ASSERT_STREQ(desc, "my bus!"); |
| 74 |
|
| 75 |
log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id)); |
| 76 |
log_info("Unique ID: %s", unique); |
| 77 |
log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h')); |
| 78 |
|
| 79 |
r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0); |
| 80 |
if (r < 0) |
| 81 |
return log_error_errno(r, "Failed to acquire name: %m"); |
| 82 |
|
| 83 |
r = sd_bus_add_fallback(bus, NULL, "/foo/bar", object_callback, NULL); |
| 84 |
if (r < 0) |
| 85 |
return log_error_errno(r, "Failed to add object: %m"); |
| 86 |
|
| 87 |
r = sd_bus_match_signal(bus, NULL, NULL, NULL, "foo.bar", "Notify", match_callback, NULL); |
| 88 |
if (r < 0) |
| 89 |
return log_error_errno(r, "Failed to request match: %m"); |
| 90 |
|
| 91 |
r = sd_bus_match_signal(bus, NULL, NULL, NULL, "foo.bar", "NotifyTo", match_callback, NULL); |
| 92 |
if (r < 0) |
| 93 |
return log_error_errno(r, "Failed to request match: %m"); |
| 94 |
|
| 95 |
r = sd_bus_add_match(bus, NULL, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL); |
| 96 |
if (r < 0) |
| 97 |
return log_error_errno(r, "Failed to add match: %m"); |
| 98 |
|
| 99 |
bus_match_dump(stdout, &bus->match_callbacks, 0); |
| 100 |
|
| 101 |
*ret = TAKE_PTR(bus); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static int server(void *userdata) { |
| 106 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 107 |
bool client1_gone = false, client2_gone = false; |
| 108 |
int r; |
| 109 |
|
| 110 |
while (!client1_gone || !client2_gone) { |
| 111 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 112 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 113 |
pid_t pid = 0; |
| 114 |
const char *label = NULL; |
| 115 |
|
| 116 |
r = sd_bus_process(bus, &m); |
| 117 |
if (r < 0) |
| 118 |
return log_error_errno(r, "Failed to process requests: %m"); |
| 119 |
if (r == 0) { |
| 120 |
r = sd_bus_wait(bus, UINT64_MAX); |
| 121 |
if (r < 0) |
| 122 |
return log_error_errno(r, "Failed to wait: %m"); |
| 123 |
|
| 124 |
continue; |
| 125 |
} |
| 126 |
if (!m) |
| 127 |
continue; |
| 128 |
|
| 129 |
r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_PID | SD_BUS_CREDS_SELINUX_CONTEXT, &creds); |
| 130 |
if (r < 0) |
| 131 |
log_debug_errno(r, "Failed to query sender credentials, ignoring: %m"); |
| 132 |
else { |
| 133 |
r = sd_bus_creds_get_pid(creds, &pid); |
| 134 |
if (r < 0) |
| 135 |
return log_error_errno(r, "Failed to get sender pid: %m"); |
| 136 |
|
| 137 |
(void) sd_bus_creds_get_selinux_context(creds, &label); |
| 138 |
} |
| 139 |
|
| 140 |
log_info("Got message! member=%s pid="PID_FMT" label=%s", |
| 141 |
strna(sd_bus_message_get_member(m)), |
| 142 |
pid, |
| 143 |
strna(label)); |
| 144 |
|
| 145 |
/* sd_bus_message_dump(m); */ |
| 146 |
/* sd_bus_message_rewind(m, true); */ |
| 147 |
|
| 148 |
if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "LowerCase")) { |
| 149 |
const char *hello; |
| 150 |
_cleanup_free_ char *lowercase = NULL; |
| 151 |
|
| 152 |
r = sd_bus_message_read(m, "s", &hello); |
| 153 |
if (r < 0) |
| 154 |
return log_error_errno(r, "Failed to get parameter: %m"); |
| 155 |
|
| 156 |
lowercase = strdup(hello); |
| 157 |
if (!lowercase) |
| 158 |
return log_oom(); |
| 159 |
|
| 160 |
ascii_strlower(lowercase); |
| 161 |
|
| 162 |
r = sd_bus_reply_method_return(m, "s", lowercase); |
| 163 |
if (r < 0) |
| 164 |
return log_error_errno(r, "Failed to send reply: %m"); |
| 165 |
|
| 166 |
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient1")) { |
| 167 |
|
| 168 |
r = sd_bus_reply_method_return(m, NULL); |
| 169 |
if (r < 0) |
| 170 |
return log_error_errno(r, "Failed to send reply: %m"); |
| 171 |
|
| 172 |
client1_gone = true; |
| 173 |
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient2")) { |
| 174 |
|
| 175 |
r = sd_bus_reply_method_return(m, NULL); |
| 176 |
if (r < 0) |
| 177 |
return log_error_errno(r, "Failed to send reply: %m"); |
| 178 |
|
| 179 |
client2_gone = true; |
| 180 |
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Slow")) { |
| 181 |
|
| 182 |
r = sd_fiber_sleep(1 * USEC_PER_SEC); |
| 183 |
if (r < 0) |
| 184 |
return r; |
| 185 |
|
| 186 |
r = sd_bus_reply_method_return(m, NULL); |
| 187 |
if (r < 0) |
| 188 |
return log_error_errno(r, "Failed to send reply: %m"); |
| 189 |
|
| 190 |
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "FileDescriptor")) { |
| 191 |
int fd; |
| 192 |
static const char x = 'X'; |
| 193 |
|
| 194 |
r = sd_bus_message_read(m, "h", &fd); |
| 195 |
if (r < 0) |
| 196 |
return log_error_errno(r, "Failed to get parameter: %m"); |
| 197 |
|
| 198 |
log_info("Received fd=%d", fd); |
| 199 |
|
| 200 |
ssize_t n = sd_fiber_write(fd, &x, 1); |
| 201 |
if (n < 0) { |
| 202 |
safe_close(fd); |
| 203 |
return log_error_errno(n, "Failed to write to fd: %m"); |
| 204 |
} |
| 205 |
|
| 206 |
r = sd_bus_reply_method_return(m, NULL); |
| 207 |
if (r < 0) |
| 208 |
return log_error_errno(r, "Failed to send reply: %m"); |
| 209 |
|
| 210 |
} else if (sd_bus_message_is_method_call(m, NULL, NULL)) { |
| 211 |
|
| 212 |
r = sd_bus_reply_method_error( |
| 213 |
m, |
| 214 |
&SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method.")); |
| 215 |
if (r < 0) |
| 216 |
return log_error_errno(r, "Failed to send reply: %m"); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
static int client1(void *userdata) { |
| 224 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 225 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 226 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 227 |
const char *hello; |
| 228 |
int r; |
| 229 |
_cleanup_close_pair_ int pp[2] = EBADF_PAIR; |
| 230 |
char x; |
| 231 |
|
| 232 |
r = sd_bus_open_user(&bus); |
| 233 |
if (r < 0) { |
| 234 |
log_error_errno(r, "Failed to connect to user bus: %m"); |
| 235 |
goto finish; |
| 236 |
} |
| 237 |
|
| 238 |
r = sd_bus_call_method( |
| 239 |
bus, |
| 240 |
"org.freedesktop.systemd.test", |
| 241 |
"/", |
| 242 |
"org.freedesktop.systemd.test", |
| 243 |
"LowerCase", |
| 244 |
&error, |
| 245 |
&reply, |
| 246 |
"s", |
| 247 |
"HELLO"); |
| 248 |
if (r < 0) { |
| 249 |
log_error_errno(r, "Failed to issue method call: %m"); |
| 250 |
goto finish; |
| 251 |
} |
| 252 |
|
| 253 |
r = sd_bus_message_read(reply, "s", &hello); |
| 254 |
if (r < 0) { |
| 255 |
log_error_errno(r, "Failed to get string: %m"); |
| 256 |
goto finish; |
| 257 |
} |
| 258 |
|
| 259 |
ASSERT_STREQ(hello, "hello"); |
| 260 |
|
| 261 |
if (pipe2(pp, O_CLOEXEC|O_NONBLOCK) < 0) { |
| 262 |
r = log_error_errno(errno, "Failed to allocate pipe: %m"); |
| 263 |
goto finish; |
| 264 |
} |
| 265 |
|
| 266 |
log_info("Sending fd=%d", pp[1]); |
| 267 |
|
| 268 |
r = sd_bus_call_method( |
| 269 |
bus, |
| 270 |
"org.freedesktop.systemd.test", |
| 271 |
"/", |
| 272 |
"org.freedesktop.systemd.test", |
| 273 |
"FileDescriptor", |
| 274 |
&error, |
| 275 |
NULL, |
| 276 |
"h", |
| 277 |
pp[1]); |
| 278 |
if (r < 0) { |
| 279 |
log_error_errno(r, "Failed to issue method call: %m"); |
| 280 |
goto finish; |
| 281 |
} |
| 282 |
|
| 283 |
ssize_t n = sd_fiber_read(pp[0], &x, 1); |
| 284 |
if (n <= 0) { |
| 285 |
log_error("Failed to read from pipe: %s", STRERROR_OR_EOF(n)); |
| 286 |
goto finish; |
| 287 |
} |
| 288 |
|
| 289 |
r = 0; |
| 290 |
|
| 291 |
finish: |
| 292 |
if (bus) { |
| 293 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *q = NULL; |
| 294 |
|
| 295 |
r = sd_bus_message_new_method_call( |
| 296 |
bus, |
| 297 |
&q, |
| 298 |
"org.freedesktop.systemd.test", |
| 299 |
"/", |
| 300 |
"org.freedesktop.systemd.test", |
| 301 |
"ExitClient1"); |
| 302 |
if (r < 0) |
| 303 |
log_error_errno(r, "Failed to allocate method call: %m"); |
| 304 |
else |
| 305 |
sd_bus_send(bus, q, NULL); |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
return r; |
| 310 |
} |
| 311 |
|
| 312 |
static int quit_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 313 |
bool *x = userdata; |
| 314 |
|
| 315 |
log_error_errno(sd_bus_message_get_errno(m), "Quit callback: %m"); |
| 316 |
|
| 317 |
*x = 1; |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
static int client2(void *userdata) { |
| 322 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 323 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 324 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 325 |
bool quit = false; |
| 326 |
const char *mid; |
| 327 |
int r; |
| 328 |
|
| 329 |
r = sd_bus_open_user(&bus); |
| 330 |
if (r < 0) { |
| 331 |
log_error_errno(r, "Failed to connect to user bus: %m"); |
| 332 |
goto finish; |
| 333 |
} |
| 334 |
|
| 335 |
r = sd_bus_message_new_method_call( |
| 336 |
bus, |
| 337 |
&m, |
| 338 |
"org.freedesktop.systemd.test", |
| 339 |
"/foo/bar/waldo/piep", |
| 340 |
"org.object.test", |
| 341 |
"Foobar"); |
| 342 |
if (r < 0) { |
| 343 |
log_error_errno(r, "Failed to allocate method call: %m"); |
| 344 |
goto finish; |
| 345 |
} |
| 346 |
|
| 347 |
r = sd_bus_send(bus, m, NULL); |
| 348 |
if (r < 0) { |
| 349 |
log_error("Failed to issue method call: %s", bus_error_message(&error, r)); |
| 350 |
goto finish; |
| 351 |
} |
| 352 |
|
| 353 |
m = sd_bus_message_unref(m); |
| 354 |
|
| 355 |
r = sd_bus_message_new_signal( |
| 356 |
bus, |
| 357 |
&m, |
| 358 |
"/foobar", |
| 359 |
"foo.bar", |
| 360 |
"Notify"); |
| 361 |
if (r < 0) { |
| 362 |
log_error_errno(r, "Failed to allocate signal: %m"); |
| 363 |
goto finish; |
| 364 |
} |
| 365 |
|
| 366 |
r = sd_bus_send(bus, m, NULL); |
| 367 |
if (r < 0) { |
| 368 |
log_error("Failed to issue signal: %s", bus_error_message(&error, r)); |
| 369 |
goto finish; |
| 370 |
} |
| 371 |
|
| 372 |
m = sd_bus_message_unref(m); |
| 373 |
|
| 374 |
r = sd_bus_message_new_signal_to( |
| 375 |
bus, |
| 376 |
&m, |
| 377 |
"org.freedesktop.systemd.test", |
| 378 |
"/foobar", |
| 379 |
"foo.bar", |
| 380 |
"NotifyTo"); |
| 381 |
if (r < 0) { |
| 382 |
log_error_errno(r, "Failed to allocate signal to: %m"); |
| 383 |
goto finish; |
| 384 |
} |
| 385 |
|
| 386 |
r = sd_bus_send(bus, m, NULL); |
| 387 |
if (r < 0) { |
| 388 |
log_error("Failed to issue signal to: %s", bus_error_message(&error, r)); |
| 389 |
goto finish; |
| 390 |
} |
| 391 |
|
| 392 |
m = sd_bus_message_unref(m); |
| 393 |
|
| 394 |
r = sd_bus_message_new_method_call( |
| 395 |
bus, |
| 396 |
&m, |
| 397 |
"org.freedesktop.systemd.test", |
| 398 |
"/", |
| 399 |
"org.freedesktop.DBus.Peer", |
| 400 |
"GetMachineId"); |
| 401 |
if (r < 0) { |
| 402 |
log_error_errno(r, "Failed to allocate method call: %m"); |
| 403 |
goto finish; |
| 404 |
} |
| 405 |
|
| 406 |
r = sd_bus_call(bus, m, 0, &error, &reply); |
| 407 |
if (r < 0) { |
| 408 |
log_error("Failed to issue method call: %s", bus_error_message(&error, r)); |
| 409 |
goto finish; |
| 410 |
} |
| 411 |
|
| 412 |
r = sd_bus_message_read(reply, "s", &mid); |
| 413 |
if (r < 0) { |
| 414 |
log_error_errno(r, "Failed to parse machine ID: %m"); |
| 415 |
goto finish; |
| 416 |
} |
| 417 |
|
| 418 |
log_info("Machine ID is %s.", mid); |
| 419 |
|
| 420 |
m = sd_bus_message_unref(m); |
| 421 |
|
| 422 |
r = sd_bus_message_new_method_call( |
| 423 |
bus, |
| 424 |
&m, |
| 425 |
"org.freedesktop.systemd.test", |
| 426 |
"/", |
| 427 |
"org.freedesktop.systemd.test", |
| 428 |
"Slow"); |
| 429 |
if (r < 0) { |
| 430 |
log_error_errno(r, "Failed to allocate method call: %m"); |
| 431 |
goto finish; |
| 432 |
} |
| 433 |
|
| 434 |
reply = sd_bus_message_unref(reply); |
| 435 |
|
| 436 |
r = sd_bus_call(bus, m, 200 * USEC_PER_MSEC, &error, &reply); |
| 437 |
if (r < 0) |
| 438 |
log_debug("Failed to issue method call: %s", bus_error_message(&error, r)); |
| 439 |
else { |
| 440 |
r = log_error_errno(SYNTHETIC_ERRNO(ENOANO), "Slow call unexpectedly succeeded."); |
| 441 |
goto finish; |
| 442 |
} |
| 443 |
|
| 444 |
m = sd_bus_message_unref(m); |
| 445 |
|
| 446 |
r = sd_bus_message_new_method_call( |
| 447 |
bus, |
| 448 |
&m, |
| 449 |
"org.freedesktop.systemd.test", |
| 450 |
"/", |
| 451 |
"org.freedesktop.systemd.test", |
| 452 |
"Slow"); |
| 453 |
if (r < 0) { |
| 454 |
log_error_errno(r, "Failed to allocate method call: %m"); |
| 455 |
goto finish; |
| 456 |
} |
| 457 |
|
| 458 |
r = sd_bus_call_async(bus, NULL, m, quit_callback, &quit, 200 * USEC_PER_MSEC); |
| 459 |
if (r < 0) { |
| 460 |
log_info("Failed to issue method call: %s", bus_error_message(&error, r)); |
| 461 |
goto finish; |
| 462 |
} |
| 463 |
|
| 464 |
while (!quit) { |
| 465 |
r = sd_bus_process(bus, NULL); |
| 466 |
if (r < 0) { |
| 467 |
log_error_errno(r, "Failed to process requests: %m"); |
| 468 |
goto finish; |
| 469 |
} |
| 470 |
if (r == 0) { |
| 471 |
r = sd_bus_wait(bus, UINT64_MAX); |
| 472 |
if (r < 0) { |
| 473 |
log_error_errno(r, "Failed to wait: %m"); |
| 474 |
goto finish; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
r = 0; |
| 480 |
|
| 481 |
finish: |
| 482 |
if (bus) { |
| 483 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *q = NULL; |
| 484 |
|
| 485 |
r = sd_bus_message_new_method_call( |
| 486 |
bus, |
| 487 |
&q, |
| 488 |
"org.freedesktop.systemd.test", |
| 489 |
"/", |
| 490 |
"org.freedesktop.systemd.test", |
| 491 |
"ExitClient2"); |
| 492 |
if (r < 0) { |
| 493 |
log_error_errno(r, "Failed to allocate method call: %m"); |
| 494 |
goto finish; |
| 495 |
} |
| 496 |
|
| 497 |
(void) sd_bus_send(bus, q, NULL); |
| 498 |
} |
| 499 |
|
| 500 |
return r; |
| 501 |
} |
| 502 |
|
| 503 |
static ino_t get_inode(int fd) { |
| 504 |
struct stat st; |
| 505 |
assert_se(fstat(fd, &st) >= 0); |
| 506 |
return st.st_ino; |
| 507 |
} |
| 508 |
|
| 509 |
static int get_one_message(sd_bus *bus, sd_bus_message **m) { |
| 510 |
int r; |
| 511 |
|
| 512 |
assert (m); |
| 513 |
|
| 514 |
while (!*m) { |
| 515 |
r = sd_bus_wait(bus, UINT64_MAX); |
| 516 |
if (r < 0) |
| 517 |
return log_error_errno(r, "Failed to wait: %m"); |
| 518 |
r = sd_bus_process(bus, m); |
| 519 |
if (r < 0) |
| 520 |
return log_error_errno(r, "Failed to process requests: %m"); |
| 521 |
} |
| 522 |
|
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
TEST(ctrunc) { |
| 527 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 528 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *recvd = NULL, *sent = NULL; |
| 529 |
struct rlimit orig_rl, new_rl; |
| 530 |
const char *unique; |
| 531 |
const int n_fds_to_send = 64; |
| 532 |
ino_t memfd_st_ino[n_fds_to_send]; |
| 533 |
int r; |
| 534 |
|
| 535 |
/* Connect to the session bus and eat the NamedAcquired message */ |
| 536 |
r = sd_bus_open_user(&bus); |
| 537 |
if (r < 0) |
| 538 |
return (void) log_error_errno(r, "Cannot connect to bus: %m"); |
| 539 |
ASSERT_OK(get_one_message(bus, &recvd)); |
| 540 |
recvd = sd_bus_message_unref(recvd); |
| 541 |
|
| 542 |
if (!sd_bus_can_send(bus, 'h')) |
| 543 |
return (void) log_error("Bus does not support fd passing: %m"); |
| 544 |
|
| 545 |
/* We will create a message with 64 fds in it and set a fd limit of 128 and try to receive it. We'll |
| 546 |
* hold on to that message after we send it and then attempt to receive it back. Since various other |
| 547 |
* fds will be open, with both copies of the message, we'll definitely hit the limit of 128. |
| 548 |
*/ |
| 549 |
ASSERT_OK(sd_bus_get_unique_name(bus, &unique)); |
| 550 |
ASSERT_OK(sd_bus_message_new_method_call(bus, &sent, unique, "/", "org.freedesktop.systemd.test", "SendFds")); |
| 551 |
ASSERT_OK(sd_bus_message_open_container(sent, SD_BUS_TYPE_ARRAY, "h")); |
| 552 |
|
| 553 |
/* Create a series of memfds, appending each to the message */ |
| 554 |
for (int i = 0; i < n_fds_to_send; i++) { |
| 555 |
_cleanup_close_ int memfd = memfd_create_wrapper("ctrunc-test", 0); |
| 556 |
ASSERT_OK(memfd); |
| 557 |
memfd_st_ino[i] = get_inode(memfd); |
| 558 |
ASSERT_OK(sd_bus_message_append(sent, "h", memfd)); |
| 559 |
} |
| 560 |
ASSERT_OK(sd_bus_message_close_container(sent)); |
| 561 |
|
| 562 |
/* Send the message - keep 'sent' alive to hold the duplicated fd references */ |
| 563 |
ASSERT_OK(sd_bus_send(bus, sent, NULL)); |
| 564 |
|
| 565 |
/* Now turn down the fd limit, receive the message, and turn it back up again */ |
| 566 |
ASSERT_OK_ERRNO(getrlimit(RLIMIT_NOFILE, &orig_rl)); |
| 567 |
new_rl.rlim_cur = n_fds_to_send * 2; |
| 568 |
new_rl.rlim_max = orig_rl.rlim_max; |
| 569 |
ASSERT_OK_ERRNO(setrlimit(RLIMIT_NOFILE, &new_rl)); |
| 570 |
|
| 571 |
/* The very first message should be the one we expect */ |
| 572 |
ASSERT_OK(get_one_message(bus, &recvd)); |
| 573 |
|
| 574 |
/* This needs to succeed or the following tests are going to be unhappy... */ |
| 575 |
ASSERT_EQ(setrlimit(RLIMIT_NOFILE, &orig_rl), 0); |
| 576 |
|
| 577 |
/* dbus-daemon disconnects peers when FDs get truncated |
| 578 |
* https://github.com/systemd/systemd/issues/41150 */ |
| 579 |
if (sd_bus_message_is_signal(recvd, "org.freedesktop.DBus.Local", "Disconnected") > 0) |
| 580 |
return (void) log_tests_skipped("Running with dbus-daemon, which doesn't support fd passing with truncation"); |
| 581 |
|
| 582 |
ASSERT_TRUE(sd_bus_message_is_method_call(recvd, "org.freedesktop.systemd.test", "SendFds")); |
| 583 |
|
| 584 |
/* Try to read all the fds. We expect at least one to fail with -EBADMSG due to |
| 585 |
* truncation, and all subsequent reads must also fail with -EBADMSG. */ |
| 586 |
int i; |
| 587 |
ASSERT_OK(sd_bus_message_enter_container(recvd, SD_BUS_TYPE_ARRAY, "h")); |
| 588 |
for (i = 0; i < n_fds_to_send; i++) { |
| 589 |
int fd; /* weakly owned: the fd belongs to the message */ |
| 590 |
r = sd_bus_message_read_basic(recvd, 'h', &fd); |
| 591 |
if (r == -EBADMSG) |
| 592 |
/* Good! We were expecting this! */ |
| 593 |
break; |
| 594 |
ASSERT_OK(r); |
| 595 |
ASSERT_EQ(get_inode(fd), memfd_st_ino[i]); |
| 596 |
} |
| 597 |
|
| 598 |
/* Make sure we successfully sent at least one fd but not all of them */ |
| 599 |
ASSERT_GT(i, 0); |
| 600 |
ASSERT_LT(i, n_fds_to_send); |
| 601 |
log_info("fds truncated at %i", i); |
| 602 |
|
| 603 |
/* At this point we're stuck. We can call sd_bus_message_read_basic() as often as we want, but we |
| 604 |
* won't be able to make progress and won't be able to close the array or read anything else in the |
| 605 |
* message. |
| 606 |
*/ |
| 607 |
for (i = 0; i < 2 * n_fds_to_send; i++) { |
| 608 |
int fd; /* weakly owned: the fd belongs to the message */ |
| 609 |
ASSERT_ERROR(sd_bus_message_read_basic(recvd, 'h', &fd), EBADMSG); |
| 610 |
} |
| 611 |
ASSERT_ERROR(sd_bus_message_exit_container(recvd), EBUSY); |
| 612 |
recvd = sd_bus_message_unref(recvd); |
| 613 |
|
| 614 |
/* Send the message again without the fd limits to make sure the connection still works */ |
| 615 |
ASSERT_OK(sd_bus_send(bus, sent, NULL)); |
| 616 |
ASSERT_OK(get_one_message(bus, &recvd)); |
| 617 |
ASSERT_TRUE(sd_bus_message_is_method_call(recvd, "org.freedesktop.systemd.test", "SendFds")); |
| 618 |
|
| 619 |
/* Read all the fds. */ |
| 620 |
ASSERT_EQ(sd_bus_message_enter_container(recvd, SD_BUS_TYPE_ARRAY, "h"), 1); |
| 621 |
for (i = 0; i < n_fds_to_send; i++) { |
| 622 |
int fd; /* weakly owned: the fd belongs to the message */ |
| 623 |
ASSERT_OK(sd_bus_message_read_basic(recvd, 'h', &fd)); |
| 624 |
ASSERT_EQ(get_inode(fd), memfd_st_ino[i]); |
| 625 |
} |
| 626 |
ASSERT_OK(sd_bus_message_exit_container(recvd)); |
| 627 |
|
| 628 |
log_info("MSG_CTRUNC test passed"); |
| 629 |
} |
| 630 |
|
| 631 |
TEST(chat) { |
| 632 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 633 |
_cleanup_(sd_future_unrefp) sd_future *f_server = NULL, *f_client1 = NULL, *f_client2 = NULL; |
| 634 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 635 |
int r; |
| 636 |
|
| 637 |
test_setup_logging(LOG_INFO); |
| 638 |
|
| 639 |
r = server_init(&bus); |
| 640 |
if (r < 0) |
| 641 |
return (void) log_tests_skipped_errno(r, "Failed to connect to bus: %m"); |
| 642 |
|
| 643 |
log_info("Initialized..."); |
| 644 |
|
| 645 |
ASSERT_OK(sd_event_new(&e)); |
| 646 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 647 |
|
| 648 |
ASSERT_OK(sd_fiber_new(e, "client-1", client1, NULL, /* destroy= */ NULL, &f_client1)); |
| 649 |
ASSERT_OK(sd_fiber_new(e, "client-2", client2, NULL, /* destroy= */ NULL, &f_client2)); |
| 650 |
ASSERT_OK(sd_fiber_new(e, "server", server, bus, /* destroy= */ NULL, &f_server)); |
| 651 |
|
| 652 |
ASSERT_OK(sd_event_loop(e)); |
| 653 |
|
| 654 |
ASSERT_OK(sd_future_result(f_client1)); |
| 655 |
ASSERT_OK(sd_future_result(f_client2)); |
| 656 |
ASSERT_OK(sd_future_result(f_server)); |
| 657 |
} |
| 658 |
|
| 659 |
DEFINE_TEST_MAIN(LOG_INFO); |