int bus_open_user_systemd(sd_bus **_bus) { _cleanup_bus_unref_ sd_bus *bus = NULL; _cleanup_free_ char *ee = NULL; const char *e; int r; /* Try via kdbus first, and then directly */ assert(_bus); #ifdef ENABLE_KDBUS r = sd_bus_new(&bus); if (r < 0) return r; if (asprintf(&bus->address, KERNEL_USER_BUS_FMT, getuid()) < 0) return -ENOMEM; bus->bus_client = true; r = sd_bus_start(bus); if (r >= 0) { *_bus = bus; bus = NULL; return 0; } bus = sd_bus_unref(bus); #endif e = secure_getenv("XDG_RUNTIME_DIR"); if (!e) return sd_bus_open_user(_bus); ee = bus_address_escape(e); if (!ee) return -ENOMEM; r = sd_bus_new(&bus); if (r < 0) return r; bus->address = strjoin("unix:path=", ee, "/systemd/private", NULL); if (!bus->address) return -ENOMEM; r = sd_bus_start(bus); if (r < 0) return sd_bus_open_user(_bus); r = bus_check_peercred(bus); if (r < 0) return r; *_bus = bus; bus = NULL; return 0; }
static void test_one( const char *path, const char *interface, const char *member, const char *arg0, const char *match, bool good) { _cleanup_close_ int bus_ref = -1; _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL; _cleanup_bus_message_unref_ sd_bus_message *m = NULL; sd_bus *a, *b; int r; assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0); bus_ref = bus_kernel_create_bus(name, false, &bus_name); if (bus_ref == -ENOENT) exit(EXIT_TEST_SKIP); assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); r = sd_bus_new(&a); assert_se(r >= 0); r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(a, address); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_start(a); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); log_debug("match"); r = sd_bus_add_match(b, NULL, match, NULL, NULL); assert_se(r >= 0); log_debug("signal"); r = sd_bus_emit_signal(a, path, interface, member, "s", arg0); assert_se(r >= 0); r = sd_bus_process(b, &m); assert_se(r >= 0 && (good == !!m)); sd_bus_unref(a); sd_bus_unref(b); }
int bus_open_system_systemd(sd_bus **_bus) { _cleanup_bus_unref_ sd_bus *bus = NULL; int r; assert(_bus); if (geteuid() != 0) return sd_bus_open_system(_bus); /* If we are root and kdbus is not available, then let's talk * directly to the system instance, instead of going via the * bus */ #ifdef ENABLE_KDBUS r = sd_bus_new(&bus); if (r < 0) return r; r = sd_bus_set_address(bus, KERNEL_SYSTEM_BUS_PATH); if (r < 0) return r; bus->bus_client = true; r = sd_bus_start(bus); if (r >= 0) { *_bus = bus; bus = NULL; return 0; } bus = sd_bus_unref(bus); #endif r = sd_bus_new(&bus); if (r < 0) return r; r = sd_bus_set_address(bus, "unix:path=/run/systemd/private"); if (r < 0) return r; r = sd_bus_start(bus); if (r < 0) return sd_bus_open_system(_bus); r = bus_check_peercred(bus); if (r < 0) return r; *_bus = bus; bus = NULL; return 0; }
int bus_open_system_systemd(sd_bus **_bus) { _cleanup_bus_unref_ sd_bus *bus = NULL; int r; assert(_bus); if (geteuid() != 0) return sd_bus_open_system(_bus); /* If we are root, then let's talk directly to the system * instance, instead of going via the bus */ r = sd_bus_new(&bus); if (r < 0) return r; r = sd_bus_set_address(bus, "unix:path=/run/systemd/private"); if (r < 0) return r; r = sd_bus_start(bus); if (r < 0) return r; r = bus_check_peercred(bus); if (r < 0) return r; *_bus = bus; bus = NULL; return 0; }
static int client(struct context *c) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; _cleanup_bus_unref_ sd_bus *bus = NULL; sd_bus_error error = SD_BUS_ERROR_NULL; int r; assert_se(sd_bus_new(&bus) >= 0); assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0); assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0); assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0); assert_se(sd_bus_start(bus) >= 0); r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.systemd.test", "/", "org.freedesktop.systemd.test", "Exit"); if (r < 0) { log_error("Failed to allocate method call: %s", strerror(-r)); return r; } r = sd_bus_call(bus, m, 0, &error, &reply); if (r < 0) { log_error("Failed to issue method call: %s", bus_error_message(&error, -r)); return r; } return 0; }
static void *server(void *p) { struct context *c = p; sd_bus *bus = NULL; sd_id128_t id; int r; c->quit = false; assert_se(sd_id128_randomize(&id) >= 0); assert_se(sd_bus_new(&bus) >= 0); assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0); assert_se(sd_bus_set_server(bus, 1, id) >= 0); assert_se(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.test", vtable, c) >= 0); assert_se(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.test2", vtable, c) >= 0); assert_se(sd_bus_add_fallback_vtable(bus, NULL, "/value", "org.freedesktop.systemd.ValueTest", vtable2, NULL, UINT_TO_PTR(20)) >= 0); assert_se(sd_bus_add_node_enumerator(bus, NULL, "/value", enumerator_callback, NULL) >= 0); assert_se(sd_bus_add_node_enumerator(bus, NULL, "/value/a", enumerator2_callback, NULL) >= 0); assert_se(sd_bus_add_object_manager(bus, NULL, "/value") >= 0); assert_se(sd_bus_add_object_manager(bus, NULL, "/value/a") >= 0); assert_se(sd_bus_start(bus) >= 0); log_error("Entering event loop on server"); while (!c->quit) { log_error("Loop!"); r = sd_bus_process(bus, NULL); if (r < 0) { log_error_errno(r, "Failed to process requests: %m"); goto fail; } if (r == 0) { r = sd_bus_wait(bus, (uint64_t) -1); if (r < 0) { log_error_errno(r, "Failed to wait: %m"); goto fail; } continue; } } r = 0; fail: if (bus) { sd_bus_flush(bus); sd_bus_unref(bus); } return INT_TO_PTR(r); }
static void client_chart(const char *address) { _cleanup_bus_message_unref_ sd_bus_message *x = NULL; size_t csize; sd_bus *b; int r; r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); assert_se(sd_bus_call_method(b, ":1.1", "/", "benchmark.server", "Ping", NULL, NULL, NULL) >= 0); printf("SIZE\tCOPY\tMEMFD\n"); for (csize = 1; csize <= MAX_SIZE; csize *= 2) { usec_t t; unsigned n_copying, n_memfd; printf("%zu\t", csize); b->use_memfd = 0; t = now(CLOCK_MONOTONIC); for (n_copying = 0;; n_copying++) { transaction(b, csize); if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) break; } printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec)); b->use_memfd = -1; t = now(CLOCK_MONOTONIC); for (n_memfd = 0;; n_memfd++) { transaction(b, csize); if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) break; } printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec)); } b->use_memfd = 1; assert_se(sd_bus_message_new_method_call(b, ":1.1", "/", "benchmark.server", "Exit", &x) >= 0); assert_se(sd_bus_message_append(x, "t", csize) >= 0); assert_se(sd_bus_send(b, x, NULL) >= 0); sd_bus_unref(b); }
static void test_proxy_matched(void) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL; _cleanup_free_ char *matchstr = NULL; TestProxyMatch match = {}; const char *me; int r; /* open bus 'a' */ r = sd_bus_new(&a); assert_se(r >= 0); r = sd_bus_set_address(a, "unix:path=/var/run/dbus/system_bus_socket"); assert_se(r >= 0); r = sd_bus_set_bus_client(a, true); assert_se(r >= 0); r = sd_bus_start(a); assert_se(r >= 0); r = sd_bus_get_unique_name(a, &me); assert_se(r >= 0); matchstr = strjoin("type='signal'," "member='NameAcquired'," "destination='", me, "'", NULL); assert_se(matchstr); r = sd_bus_add_match(a, NULL, matchstr, test_proxy_acquired, &match); assert_se(r >= 0); r = sd_bus_get_unique_name(a, &match.sender); assert_se(r >= 0); /* barrier to guarantee proxy/dbus-daemon handled the previous data */ r = sd_bus_call_method(a, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetId", NULL, NULL, NULL); assert_se(r >= 0); /* now we can be sure the Name* signals were sent */ do { r = sd_bus_process(a, NULL); } while (r > 0); assert_se(r == 0); assert_se(match.matched_acquired == 1); }
int main(int argc, char *argv[]) { _cleanup_close_ int bus_ref = -1; _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *bname = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; const char *ua = NULL, *ub = NULL, *the_string = NULL; sd_bus *a, *b; int r, pipe_fds[2]; const char *nn; log_set_max_level(LOG_DEBUG); assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0); bus_ref = bus_kernel_create_bus(name, false, &bus_name); if (bus_ref == -ENOENT) return EXIT_TEST_SKIP; assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); r = sd_bus_new(&a); assert_se(r >= 0); r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_description(a, "a"); assert_se(r >= 0); r = sd_bus_set_address(a, address); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); assert_se(sd_bus_negotiate_timestamp(a, 1) >= 0); assert_se(sd_bus_negotiate_creds(a, true, _SD_BUS_CREDS_ALL) >= 0); assert_se(sd_bus_negotiate_timestamp(b, 0) >= 0); assert_se(sd_bus_negotiate_creds(b, true, 0) >= 0); r = sd_bus_start(a); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); assert_se(sd_bus_negotiate_timestamp(b, 1) >= 0); assert_se(sd_bus_negotiate_creds(b, true, _SD_BUS_CREDS_ALL) >= 0); r = sd_bus_get_unique_name(a, &ua); assert_se(r >= 0); printf("unique a: %s\n", ua); r = sd_bus_get_description(a, &nn); assert_se(r >= 0); printf("name of a: %s\n", nn); r = sd_bus_get_unique_name(b, &ub); assert_se(r >= 0); printf("unique b: %s\n", ub); r = sd_bus_get_description(b, &nn); assert_se(r >= 0); printf("name of b: %s\n", nn); assert_se(bus_kernel_get_bus_name(b, &bname) >= 0); assert_se(endswith(bname, name)); r = sd_bus_call_method(a, "this.doesnt.exist", "/foo", "meh.mah", "muh", &error, NULL, "s", "yayayay"); assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN)); assert_se(r == -EHOSTUNREACH); r = sd_bus_add_match(b, NULL, "interface='waldo.com',member='Piep'", NULL, NULL); assert_se(r >= 0); r = sd_bus_emit_signal(a, "/foo/bar/waldo", "waldo.com", "Piep", "sss", "I am a string", "/this/is/a/path", "and.this.a.domain.name"); assert_se(r >= 0); r = sd_bus_try_close(b); assert_se(r == -EBUSY); r = sd_bus_process_priority(b, -10, &m); assert_se(r == 0); r = sd_bus_process(b, &m); assert_se(r > 0); assert_se(m); bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); assert_se(sd_bus_message_rewind(m, true) >= 0); r = sd_bus_message_read(m, "s", &the_string); assert_se(r >= 0); assert_se(streq(the_string, "I am a string")); sd_bus_message_unref(m); m = NULL; r = sd_bus_request_name(a, "net.x0pointer.foobar", 0); assert_se(r >= 0); r = sd_bus_message_new_method_call(b, &m, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod"); assert_se(r >= 0); assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0); assert_se(write(pipe_fds[1], "x", 1) == 1); pipe_fds[1] = safe_close(pipe_fds[1]); r = sd_bus_message_append(m, "h", pipe_fds[0]); assert_se(r >= 0); pipe_fds[0] = safe_close(pipe_fds[0]); r = sd_bus_send(b, m, NULL); assert_se(r >= 0); for (;;) { sd_bus_message_unref(m); m = NULL; r = sd_bus_process(a, &m); assert_se(r > 0); assert_se(m); bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); assert_se(sd_bus_message_rewind(m, true) >= 0); if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) { int fd; char x; r = sd_bus_message_read(m, "h", &fd); assert_se(r >= 0); assert_se(read(fd, &x, 1) == 1); assert_se(x == 'x'); break; } } r = sd_bus_release_name(a, "net.x0pointer.foobar"); assert_se(r >= 0); r = sd_bus_release_name(a, "net.x0pointer.foobar"); assert_se(r == -ESRCH); r = sd_bus_try_close(a); assert_se(r >= 0); sd_bus_unref(a); sd_bus_unref(b); return 0; }
static int client(struct context *c) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_bus_unref_ sd_bus *bus = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; const char *s; int r; assert_se(sd_bus_new(&bus) >= 0); assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0); assert_se(sd_bus_start(bus) >= 0); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "NoOperation", &error, NULL, NULL); assert_se(r >= 0); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "s", "hallo"); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); assert_se(streq(s, "<<<hallo>>>")); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Doesntexist", &error, &reply, ""); assert_se(r < 0); assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)); sd_bus_error_free(&error); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "as", 1, "hallo"); assert_se(r < 0); assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)); sd_bus_error_free(&error); r = sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, &reply, "s"); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); assert_se(streq(s, "<<<hallo>>>")); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, "s", "test"); assert_se(r >= 0); r = sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, &reply, "s"); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); assert_se(streq(s, "test")); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticIntegerProperty", &error, "u", 815); assert_se(r >= 0); assert_se(c->automatic_integer_property == 815); r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticStringProperty", &error, "s", "Du Dödel, Du!"); assert_se(r >= 0); assert_se(streq(c->automatic_string_property, "Du Dödel, Du!")); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, ""); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); fputs(s, stdout); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/value/xuzz", "org.freedesktop.systemd.ValueTest", "Value", &error, &reply, "s"); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); log_info("read %s", s); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, ""); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); fputs(s, stdout); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, ""); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); fputs(s, stdout); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, ""); assert_se(r >= 0); r = sd_bus_message_read(reply, "s", &s); assert_se(r >= 0); fputs(s, stdout); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", ""); assert_se(r >= 0); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", "org.freedesktop.systemd.ValueTest2"); assert_se(r < 0); assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_INTERFACE)); sd_bus_error_free(&error); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, ""); assert_se(r < 0); assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)); sd_bus_error_free(&error); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, ""); assert_se(r >= 0); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.systemd.ValueTest", "NotifyTest", &error, NULL, ""); assert_se(r >= 0); r = sd_bus_process(bus, &reply); assert_se(r > 0); assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.Properties", "PropertiesChanged")); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.systemd.ValueTest", "NotifyTest2", &error, NULL, ""); assert_se(r >= 0); r = sd_bus_process(bus, &reply); assert_se(r > 0); assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.Properties", "PropertiesChanged")); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitInterfacesAdded", &error, NULL, ""); assert_se(r >= 0); r = sd_bus_process(bus, &reply); assert_se(r > 0); assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded")); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitInterfacesRemoved", &error, NULL, ""); assert_se(r >= 0); r = sd_bus_process(bus, &reply); assert_se(r > 0); assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved")); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitObjectAdded", &error, NULL, ""); assert_se(r >= 0); r = sd_bus_process(bus, &reply); assert_se(r > 0); assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded")); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitObjectRemoved", &error, NULL, ""); assert_se(r >= 0); r = sd_bus_process(bus, &reply); assert_se(r > 0); assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved")); bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_unref(reply); reply = NULL; r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Exit", &error, NULL, ""); assert_se(r >= 0); sd_bus_flush(bus); return 0; }
int main(int argc, char *argv[]) { _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL; const char *unique; uint8_t *p; sd_bus *a, *b; int r, bus_ref; sd_bus_message *m; int f; uint64_t sz; uint32_t u32; size_t i, l; char *s; _cleanup_close_ int sfd = -1; log_set_max_level(LOG_DEBUG); assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0); bus_ref = bus_kernel_create_bus(name, false, &bus_name); if (bus_ref == -ENOENT) return EXIT_TEST_SKIP; assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); r = sd_bus_new(&a); assert_se(r >= 0); r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(a, address); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_start(a); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); r = sd_bus_get_unique_name(a, &unique); assert_se(r >= 0); r = sd_bus_message_new_method_call(b, &m, unique, "/a/path", "an.inter.face", "AMethod"); assert_se(r >= 0); r = sd_bus_message_open_container(m, 'r', "aysay"); assert_se(r >= 0); r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p); assert_se(r >= 0); p[0] = '<'; memset(p+1, 'L', FIRST_ARRAY-2); p[FIRST_ARRAY-1] = '>'; f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s); assert_se(f >= 0); s[0] = '<'; for (i = 1; i < STRING_SIZE-2; i++) s[i] = '0' + (i % 10); s[STRING_SIZE-2] = '>'; s[STRING_SIZE-1] = 0; munmap(s, STRING_SIZE); r = memfd_get_size(f, &sz); assert_se(r >= 0); assert_se(sz == STRING_SIZE); r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1); assert_se(r >= 0); close(f); f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p); assert_se(f >= 0); p[0] = '<'; memset(p+1, 'P', SECOND_ARRAY-2); p[SECOND_ARRAY-1] = '>'; munmap(p, SECOND_ARRAY); r = memfd_get_size(f, &sz); assert_se(r >= 0); assert_se(sz == SECOND_ARRAY); r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1); assert_se(r >= 0); close(f); r = sd_bus_message_close_container(m); assert_se(r >= 0); r = sd_bus_message_append(m, "u", 4711); assert_se(r >= 0); assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0); memcpy(p, "abcd\0", 6); munmap(p, 6); assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0); r = bus_message_seal(m, 55, 99*USEC_PER_SEC); assert_se(r >= 0); bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); r = sd_bus_send(b, m, NULL); assert_se(r >= 0); sd_bus_message_unref(m); r = sd_bus_process(a, &m); assert_se(r > 0); bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER); sd_bus_message_rewind(m, true); r = sd_bus_message_enter_container(m, 'r', "aysay"); assert_se(r > 0); r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l); assert_se(r > 0); assert_se(l == FIRST_ARRAY); assert_se(p[0] == '<'); for (i = 1; i < l-1; i++) assert_se(p[i] == 'L'); assert_se(p[l-1] == '>'); r = sd_bus_message_read(m, "s", &s); assert_se(r > 0); assert_se(s[0] == '<'); for (i = 1; i < STRING_SIZE-2; i++) assert_se(s[i] == (char) ('0' + (i % 10))); assert_se(s[STRING_SIZE-2] == '>'); assert_se(s[STRING_SIZE-1] == 0); r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l); assert_se(r > 0); assert_se(l == SECOND_ARRAY); assert_se(p[0] == '<'); for (i = 1; i < l-1; i++) assert_se(p[i] == 'P'); assert_se(p[l-1] == '>'); r = sd_bus_message_exit_container(m); assert_se(r > 0); r = sd_bus_message_read(m, "u", &u32); assert_se(r > 0); assert_se(u32 == 4711); r = sd_bus_message_read(m, "s", &s); assert_se(r > 0); assert_se(streq_ptr(s, "bcd")); sd_bus_message_unref(m); sd_bus_unref(a); sd_bus_unref(b); return 0; }
static void *server(void *p) { struct context *c = p; sd_bus *bus = NULL; sd_id128_t id; bool quit = false; int r; assert_se(sd_id128_randomize(&id) >= 0); assert_se(sd_bus_new(&bus) >= 0); assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0); assert_se(sd_bus_set_server(bus, 1, id) >= 0); assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0); assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0); assert_se(sd_bus_start(bus) >= 0); while (!quit) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; r = sd_bus_process(bus, &m); if (r < 0) { log_error("Failed to process requests: %s", strerror(-r)); goto fail; } if (r == 0) { r = sd_bus_wait(bus, (uint64_t) -1); if (r < 0) { log_error("Failed to wait: %s", strerror(-r)); goto fail; } continue; } if (!m) continue; log_info("Got message! member=%s", strna(sd_bus_message_get_member(m))); if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) { assert_se((sd_bus_can_send(bus, 'h') >= 1) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds)); r = sd_bus_message_new_method_return(m, &reply); if (r < 0) { log_error("Failed to allocate return: %s", strerror(-r)); goto fail; } quit = true; } else if (sd_bus_message_is_method_call(m, NULL, NULL)) { r = sd_bus_message_new_method_error( m, &reply, &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method.")); if (r < 0) { log_error("Failed to allocate return: %s", strerror(-r)); goto fail; } } if (reply) { r = sd_bus_send(bus, reply, NULL); if (r < 0) { log_error("Failed to send reply: %s", strerror(-r)); goto fail; } } } r = 0; fail: if (bus) { sd_bus_flush(bus); sd_bus_unref(bus); } return INT_TO_PTR(r); }
static void client_bisect(const char *address, const char *server_name) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *x = NULL; size_t lsize, rsize, csize; sd_bus *b; int r; r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL); assert_se(r >= 0); lsize = 1; rsize = MAX_SIZE; printf("SIZE\tCOPY\tMEMFD\n"); for (;;) { usec_t t; unsigned n_copying, n_memfd; csize = (lsize + rsize) / 2; if (csize <= lsize) break; if (csize <= 0) break; printf("%zu\t", csize); b->use_memfd = 0; t = now(CLOCK_MONOTONIC); for (n_copying = 0;; n_copying++) { transaction(b, csize, server_name); if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) break; } printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec)); b->use_memfd = -1; t = now(CLOCK_MONOTONIC); for (n_memfd = 0;; n_memfd++) { transaction(b, csize, server_name); if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) break; } printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec)); if (n_copying == n_memfd) break; if (n_copying > n_memfd) lsize = csize; else rsize = csize; } b->use_memfd = 1; assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0); assert_se(sd_bus_message_append(x, "t", csize) >= 0); assert_se(sd_bus_send(b, x, NULL) >= 0); sd_bus_unref(b); }
int main(int argc, char *argv[]) { enum { MODE_BISECT, MODE_CHART, } mode = MODE_BISECT; Type type = TYPE_KDBUS; int i, pair[2] = { -1, -1 }; _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *server_name = NULL; _cleanup_close_ int bus_ref = -1; const char *unique; cpu_set_t cpuset; size_t result; sd_bus *b; pid_t pid; int r; for (i = 1; i < argc; i++) { if (streq(argv[i], "chart")) { mode = MODE_CHART; continue; } else if (streq(argv[i], "legacy")) { type = TYPE_LEGACY; continue; } else if (streq(argv[i], "direct")) { type = TYPE_DIRECT; continue; } assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0); } assert_se(!MODE_BISECT || TYPE_KDBUS); assert_se(arg_loop_usec > 0); if (type == TYPE_KDBUS) { assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0); bus_ref = bus_kernel_create_bus(name, false, &bus_name); if (bus_ref == -ENOENT) exit(EXIT_TEST_SKIP); assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); } else if (type == TYPE_LEGACY) { const char *e; e = secure_getenv("DBUS_SESSION_BUS_ADDRESS"); assert_se(e); address = strdup(e); assert_se(address); } r = sd_bus_new(&b); assert_se(r >= 0); if (type == TYPE_DIRECT) { assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0); r = sd_bus_set_fd(b, pair[0], pair[0]); assert_se(r >= 0); r = sd_bus_set_server(b, true, SD_ID128_NULL); assert_se(r >= 0); } else { r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_set_bus_client(b, true); assert_se(r >= 0); } r = sd_bus_start(b); assert_se(r >= 0); if (type != TYPE_DIRECT) { r = sd_bus_get_unique_name(b, &unique); assert_se(r >= 0); server_name = strdup(unique); assert_se(server_name); } sync(); setpriority(PRIO_PROCESS, 0, -19); pid = fork(); assert_se(pid >= 0); if (pid == 0) { CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); safe_close(bus_ref); sd_bus_unref(b); switch (mode) { case MODE_BISECT: client_bisect(address, server_name); break; case MODE_CHART: client_chart(type, address, server_name, pair[1]); break; } _exit(0); } CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); server(b, &result); if (mode == MODE_BISECT) printf("Copying/memfd are equally fast at %zu bytes\n", result); assert_se(waitpid(pid, NULL, 0) == pid); safe_close(pair[1]); sd_bus_unref(b); return 0; }
int main(int argc, char *argv[]) { _cleanup_close_ int bus_ref = -1; _cleanup_free_ char *bus_name = NULL, *address = NULL; _cleanup_bus_message_unref_ sd_bus_message *m = NULL; const char *ua = NULL, *ub = NULL, *the_string = NULL; sd_bus *a, *b; int r, pipe_fds[2]; bus_ref = bus_kernel_create("deine-mutter", &bus_name); if (bus_ref == -ENOENT) return EXIT_TEST_SKIP; assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); r = sd_bus_new(&a); assert_se(r >= 0); r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(a, address); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_start(a); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); r = sd_bus_get_unique_name(a, &ua); assert_se(r >= 0); printf("unique a: %s\n", ua); r = sd_bus_get_unique_name(b, &ub); assert_se(r >= 0); printf("unique b: %s\n", ub); { //FIXME: struct kdbus_cmd_match cmd_match; cmd_match.size = sizeof(cmd_match); cmd_match.src_id = KDBUS_MATCH_SRC_ID_ANY; r = ioctl(sd_bus_get_fd(a), KDBUS_CMD_MATCH_ADD, &cmd_match); assert_se(r >= 0); r = ioctl(sd_bus_get_fd(b), KDBUS_CMD_MATCH_ADD, &cmd_match); assert_se(r >= 0); } r = sd_bus_emit_signal(a, "/foo/bar/waldo", "waldo.com", "Piep", "sss", "I am a string", "/this/is/a/path", "and.this.a.domain.name"); assert_se(r >= 0); r = sd_bus_process(b, &m); assert_se(r > 0); assert_se(m); bus_message_dump(m); assert_se(sd_bus_message_rewind(m, true) >= 0); r = sd_bus_message_read(m, "s", &the_string); assert_se(r >= 0); assert_se(streq(the_string, "I am a string")); sd_bus_message_unref(m); m = NULL; r = sd_bus_request_name(a, "net.x0pointer.foobar", 0); assert_se(r >= 0); r = sd_bus_message_new_method_call(b, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod", &m); assert_se(r >= 0); assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0); assert_se(write(pipe_fds[1], "x", 1) == 1); close_nointr_nofail(pipe_fds[1]); pipe_fds[1] = -1; r = sd_bus_message_append(m, "h", pipe_fds[0]); assert_se(r >= 0); close_nointr_nofail(pipe_fds[0]); pipe_fds[0] = -1; r = sd_bus_send(b, m, NULL); assert_se(r >= 0); for (;;) { sd_bus_message_unref(m); m = NULL; r = sd_bus_process(a, &m); assert_se(r > 0); assert_se(m); bus_message_dump(m); assert_se(sd_bus_message_rewind(m, true) >= 0); if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) { int fd; char x; r = sd_bus_message_read(m, "h", &fd); assert_se(r >= 0); assert_se(read(fd, &x, 1) == 1); assert_se(x == 'x'); break; } } r = sd_bus_release_name(a, "net.x0pointer.foobar"); assert_se(r >= 0); r = sd_bus_release_name(a, "net.x0pointer.foobar"); assert_se(r == -ESRCH); sd_bus_unref(a); sd_bus_unref(b); return 0; }
int main(int argc, char *argv[]) { _cleanup_close_ int bus_ref = -1; _cleanup_free_ char *bus_name = NULL, *address = NULL; _cleanup_bus_message_unref_ sd_bus_message *m = NULL; const char *ua = NULL, *ub = NULL, *the_string = NULL; sd_bus *a, *b; int r, pipe_fds[2]; log_set_max_level(LOG_DEBUG); bus_ref = bus_kernel_create("deine-mutter", &bus_name); if (bus_ref == -ENOENT) return EXIT_TEST_SKIP; assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); r = sd_bus_new(&a); assert_se(r >= 0); r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(a, address); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); assert_se(sd_bus_negotiate_attach_comm(a, 1) >= 0); assert_se(sd_bus_negotiate_attach_exe(a, 1) >= 0); assert_se(sd_bus_negotiate_attach_cmdline(a, 1) >= 0); assert_se(sd_bus_negotiate_attach_cgroup(a, 1) >= 0); assert_se(sd_bus_negotiate_attach_caps(a, 1) >= 0); assert_se(sd_bus_negotiate_attach_selinux_context(a, 1) >= 0); assert_se(sd_bus_negotiate_attach_audit(a, 1) >= 0); assert_se(sd_bus_negotiate_attach_comm(b, 1) >= 0); assert_se(sd_bus_negotiate_attach_exe(b, 1) >= 0); assert_se(sd_bus_negotiate_attach_cmdline(b, 1) >= 0); assert_se(sd_bus_negotiate_attach_cgroup(b, 1) >= 0); assert_se(sd_bus_negotiate_attach_caps(b, 1) >= 0); assert_se(sd_bus_negotiate_attach_selinux_context(b, 1) >= 0); assert_se(sd_bus_negotiate_attach_audit(b, 1) >= 0); r = sd_bus_start(a); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); r = sd_bus_get_unique_name(a, &ua); assert_se(r >= 0); printf("unique a: %s\n", ua); r = sd_bus_get_unique_name(b, &ub); assert_se(r >= 0); printf("unique b: %s\n", ub); r = sd_bus_add_match(b, "interface='waldo.com',member='Piep'", NULL, NULL); assert_se(r >= 0); r = sd_bus_emit_signal(a, "/foo/bar/waldo", "waldo.com", "Piep", "sss", "I am a string", "/this/is/a/path", "and.this.a.domain.name"); assert_se(r >= 0); r = sd_bus_process(b, &m); assert_se(r > 0); assert_se(m); bus_message_dump(m); assert_se(sd_bus_message_rewind(m, true) >= 0); r = sd_bus_message_read(m, "s", &the_string); assert_se(r >= 0); assert_se(streq(the_string, "I am a string")); sd_bus_message_unref(m); m = NULL; r = sd_bus_request_name(a, "net.x0pointer.foobar", 0); assert_se(r >= 0); r = sd_bus_message_new_method_call(b, "net.x0pointer.foobar", "/a/path", "an.inter.face", "AMethod", &m); assert_se(r >= 0); assert_se(pipe2(pipe_fds, O_CLOEXEC) >= 0); assert_se(write(pipe_fds[1], "x", 1) == 1); close_nointr_nofail(pipe_fds[1]); pipe_fds[1] = -1; r = sd_bus_message_append(m, "h", pipe_fds[0]); assert_se(r >= 0); close_nointr_nofail(pipe_fds[0]); pipe_fds[0] = -1; r = sd_bus_send(b, m, NULL); assert_se(r >= 0); for (;;) { sd_bus_message_unref(m); m = NULL; r = sd_bus_process(a, &m); assert_se(r > 0); assert_se(m); bus_message_dump(m); assert_se(sd_bus_message_rewind(m, true) >= 0); if (sd_bus_message_is_method_call(m, "an.inter.face", "AMethod")) { int fd; char x; r = sd_bus_message_read(m, "h", &fd); assert_se(r >= 0); assert_se(read(fd, &x, 1) == 1); assert_se(x == 'x'); break; } } r = sd_bus_release_name(a, "net.x0pointer.foobar"); assert_se(r >= 0); r = sd_bus_release_name(a, "net.x0pointer.foobar"); assert_se(r == -ESRCH); sd_bus_unref(a); sd_bus_unref(b); return 0; }
int main(int argc, char *argv[]) { enum { MODE_BISECT, MODE_CHART, } mode = MODE_BISECT; int i; _cleanup_free_ char *bus_name = NULL, *address = NULL; _cleanup_close_ int bus_ref = -1; cpu_set_t cpuset; size_t result; sd_bus *b; pid_t pid; int r; log_set_max_level(LOG_DEBUG); for (i = 1; i < argc; i++) { if (streq(argv[i], "chart")) { mode = MODE_CHART; continue; } assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0); } assert_se(arg_loop_usec > 0); bus_ref = bus_kernel_create("deine-mutter", &bus_name); if (bus_ref == -ENOENT) exit(EXIT_TEST_SKIP); assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); sync(); setpriority(PRIO_PROCESS, 0, -19); pid = fork(); assert_se(pid >= 0); if (pid == 0) { CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); close_nointr_nofail(bus_ref); sd_bus_unref(b); switch (mode) { case MODE_BISECT: client_bisect(address); break; case MODE_CHART: client_chart(address); break; } _exit(0); } CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); server(b, &result); if (mode == MODE_BISECT) printf("Copying/memfd are equally fast at %zu bytes\n", result); assert_se(waitpid(pid, NULL, 0) == pid); sd_bus_unref(b); return 0; }
int main(int argc, char *argv[]) { _cleanup_free_ char *bus_name = NULL, *address = NULL; uint8_t *p; sd_bus *a, *b; int r, bus_ref; sd_bus_message *m; sd_memfd *f; uint64_t sz; uint32_t u32; size_t i, l; char *s; log_set_max_level(LOG_DEBUG); bus_ref = bus_kernel_create("deine-mutter", &bus_name); if (bus_ref == -ENOENT) return EXIT_TEST_SKIP; assert_se(bus_ref >= 0); address = strappend("kernel:path=", bus_name); assert_se(address); r = sd_bus_new(&a); assert_se(r >= 0); r = sd_bus_new(&b); assert_se(r >= 0); r = sd_bus_set_address(a, address); assert_se(r >= 0); r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_start(a); assert_se(r >= 0); r = sd_bus_start(b); assert_se(r >= 0); r = sd_bus_message_new_method_call(b, ":1.1", "/a/path", "an.inter.face", "AMethod", &m); assert_se(r >= 0); r = sd_bus_message_open_container(m, 'r', "aysay"); assert_se(r >= 0); r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p); assert_se(r >= 0); memset(p, 'L', FIRST_ARRAY); r = sd_memfd_new_and_map(&f, STRING_SIZE, (void**) &s); assert_se(r >= 0); for (i = 0; i < STRING_SIZE-1; i++) s[i] = '0' + (i % 10); s[STRING_SIZE-1] = 0; munmap(s, STRING_SIZE); r = sd_memfd_get_size(f, &sz); assert_se(r >= 0); assert_se(sz == STRING_SIZE); r = sd_bus_message_append_string_memfd(m, f); assert_se(r >= 0); sd_memfd_free(f); r = sd_memfd_new_and_map(&f, SECOND_ARRAY, (void**) &p); assert_se(r >= 0); memset(p, 'P', SECOND_ARRAY); munmap(p, SECOND_ARRAY); r = sd_memfd_get_size(f, &sz); assert_se(r >= 0); assert_se(sz == SECOND_ARRAY); r = sd_bus_message_append_array_memfd(m, 'y', f); assert_se(r >= 0); sd_memfd_free(f); r = sd_bus_message_close_container(m); assert_se(r >= 0); r = sd_bus_message_append(m, "u", 4711); assert_se(r >= 0); r = bus_message_seal(m, 55); assert_se(r >= 0); bus_message_dump(m, stdout, true); r = sd_bus_send(b, m, NULL); assert_se(r >= 0); sd_bus_message_unref(m); r = sd_bus_process(a, &m); assert_se(r > 0); bus_message_dump(m, stdout, true); sd_bus_message_rewind(m, true); r = sd_bus_message_enter_container(m, 'r', "aysay"); assert_se(r > 0); r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l); assert_se(r > 0); assert_se(l == FIRST_ARRAY); for (i = 0; i < l; i++) assert_se(p[i] == 'L'); r = sd_bus_message_read(m, "s", &s); assert_se(r > 0); for (i = 0; i < STRING_SIZE-1; i++) assert_se(s[i] == (char) ('0' + (i % 10))); assert_se(s[STRING_SIZE-1] == 0); r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l); assert_se(r > 0); assert_se(l == SECOND_ARRAY); for (i = 0; i < l; i++) assert_se(p[i] == 'P'); r = sd_bus_message_exit_container(m); assert_se(r > 0); r = sd_bus_message_read(m, "u", &u32); assert_se(r > 0); assert_se(u32 == 4711); sd_bus_message_unref(m); sd_bus_unref(a); sd_bus_unref(b); return 0; }
int main(int argc, char *argv[]) { _cleanup_bus_unref_ sd_bus *a = NULL, *b = NULL; sd_id128_t server_id; bool is_unix; int r; if (argc > 1) { log_error("This program takes no argument."); return EXIT_FAILURE; } log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); log_parse_environment(); log_open(); is_unix = sd_is_socket(STDIN_FILENO, AF_UNIX, 0, 0) > 0 && sd_is_socket(STDOUT_FILENO, AF_UNIX, 0, 0) > 0; r = sd_bus_new(&a); if (r < 0) { log_error("Failed to allocate bus: %s", strerror(-r)); goto finish; } r = sd_bus_set_address(a, "unix:path=/run/dbus/system_bus_socket"); if (r < 0) { log_error("Failed to set address to connect to: %s", strerror(-r)); goto finish; } r = sd_bus_set_negotiate_fds(a, is_unix); if (r < 0) { log_error("Failed to set FD negotiation: %s", strerror(-r)); goto finish; } r = sd_bus_start(a); if (r < 0) { log_error("Failed to start bus client: %s", strerror(-r)); goto finish; } r = sd_bus_get_server_id(a, &server_id); if (r < 0) { log_error("Failed to get server ID: %s", strerror(-r)); goto finish; } r = sd_bus_new(&b); if (r < 0) { log_error("Failed to allocate bus: %s", strerror(-r)); goto finish; } r = sd_bus_set_fd(b, STDIN_FILENO, STDOUT_FILENO); if (r < 0) { log_error("Failed to set fds: %s", strerror(-r)); goto finish; } r = sd_bus_set_server(b, 1, server_id); if (r < 0) { log_error("Failed to set server mode: %s", strerror(-r)); goto finish; } r = sd_bus_set_negotiate_fds(b, is_unix); if (r < 0) { log_error("Failed to set FD negotiation: %s", strerror(-r)); goto finish; } r = sd_bus_set_anonymous(b, true); if (r < 0) { log_error("Failed to set anonymous authentication: %s", strerror(-r)); goto finish; } r = sd_bus_start(b); if (r < 0) { log_error("Failed to start bus client: %s", strerror(-r)); goto finish; } for (;;) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; int events_a, events_b, fd; uint64_t timeout_a, timeout_b, t; struct timespec _ts, *ts; r = sd_bus_process(a, &m); if (r < 0) { log_error("Failed to process bus: %s", strerror(-r)); goto finish; } if (m) { r = sd_bus_send(b, m, NULL); if (r < 0) { log_error("Failed to send message: %s", strerror(-r)); goto finish; } } if (r > 0) continue; r = sd_bus_process(b, &m); if (r < 0) { log_error("Failed to process bus: %s", strerror(-r)); goto finish; } if (m) { r = sd_bus_send(a, m, NULL); if (r < 0) { log_error("Failed to send message: %s", strerror(-r)); goto finish; } } if (r > 0) continue; fd = sd_bus_get_fd(a); if (fd < 0) { log_error("Failed to get fd: %s", strerror(-r)); goto finish; } events_a = sd_bus_get_events(a); if (events_a < 0) { log_error("Failed to get events mask: %s", strerror(-r)); goto finish; } r = sd_bus_get_timeout(a, &timeout_a); if (r < 0) { log_error("Failed to get timeout: %s", strerror(-r)); goto finish; } events_b = sd_bus_get_events(b); if (events_b < 0) { log_error("Failed to get events mask: %s", strerror(-r)); goto finish; } r = sd_bus_get_timeout(b, &timeout_b); if (r < 0) { log_error("Failed to get timeout: %s", strerror(-r)); goto finish; } t = timeout_a; if (t == (uint64_t) -1 || (timeout_b != (uint64_t) -1 && timeout_b < timeout_a)) t = timeout_b; if (t == (uint64_t) -1) ts = NULL; else { usec_t nw; nw = now(CLOCK_MONOTONIC); if (t > nw) t -= nw; else t = 0; ts = timespec_store(&_ts, t); } { struct pollfd p[3] = { {.fd = fd, .events = events_a, }, {.fd = STDIN_FILENO, .events = events_b & POLLIN, }, {.fd = STDOUT_FILENO, .events = events_b & POLLOUT, }};
int main(int argc, char *argv[]) { _cleanup_(sd_bus_unrefp) sd_bus *a = NULL, *b = NULL; sd_id128_t server_id; bool is_unix; int r, in_fd, out_fd; log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); log_parse_environment(); log_open(); r = parse_argv(argc, argv); if (r <= 0) goto finish; r = sd_listen_fds(0); if (r == 0) { in_fd = STDIN_FILENO; out_fd = STDOUT_FILENO; } else if (r == 1) { in_fd = SD_LISTEN_FDS_START; out_fd = SD_LISTEN_FDS_START; } else { log_error("Illegal number of file descriptors passed\n"); goto finish; } is_unix = sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 && sd_is_socket(out_fd, AF_UNIX, 0, 0) > 0; r = sd_bus_new(&a); if (r < 0) { log_error_errno(r, "Failed to allocate bus: %m"); goto finish; } r = sd_bus_set_address(a, arg_bus_path); if (r < 0) { log_error_errno(r, "Failed to set address to connect to: %m"); goto finish; } r = sd_bus_negotiate_fds(a, is_unix); if (r < 0) { log_error_errno(r, "Failed to set FD negotiation: %m"); goto finish; } r = sd_bus_start(a); if (r < 0) { log_error_errno(r, "Failed to start bus client: %m"); goto finish; } r = sd_bus_get_bus_id(a, &server_id); if (r < 0) { log_error_errno(r, "Failed to get server ID: %m"); goto finish; } r = sd_bus_new(&b); if (r < 0) { log_error_errno(r, "Failed to allocate bus: %m"); goto finish; } r = sd_bus_set_fd(b, in_fd, out_fd); if (r < 0) { log_error_errno(r, "Failed to set fds: %m"); goto finish; } r = sd_bus_set_server(b, 1, server_id); if (r < 0) { log_error_errno(r, "Failed to set server mode: %m"); goto finish; } r = sd_bus_negotiate_fds(b, is_unix); if (r < 0) { log_error_errno(r, "Failed to set FD negotiation: %m"); goto finish; } r = sd_bus_set_anonymous(b, true); if (r < 0) { log_error_errno(r, "Failed to set anonymous authentication: %m"); goto finish; } r = sd_bus_start(b); if (r < 0) { log_error_errno(r, "Failed to start bus client: %m"); goto finish; } for (;;) { _cleanup_(sd_bus_message_unrefp)sd_bus_message *m = NULL; int events_a, events_b, fd; uint64_t timeout_a, timeout_b, t; struct timespec _ts, *ts; r = sd_bus_process(a, &m); if (r < 0) { log_error_errno(r, "Failed to process bus a: %m"); goto finish; } if (m) { r = sd_bus_send(b, m, NULL); if (r < 0) { log_error_errno(r, "Failed to send message: %m"); goto finish; } } if (r > 0) continue; r = sd_bus_process(b, &m); if (r < 0) { /* treat 'connection reset by peer' as clean exit condition */ if (r == -ECONNRESET) r = 0; goto finish; } if (m) { r = sd_bus_send(a, m, NULL); if (r < 0) { log_error_errno(r, "Failed to send message: %m"); goto finish; } } if (r > 0) continue; fd = sd_bus_get_fd(a); if (fd < 0) { log_error_errno(r, "Failed to get fd: %m"); goto finish; } events_a = sd_bus_get_events(a); if (events_a < 0) { log_error_errno(r, "Failed to get events mask: %m"); goto finish; } r = sd_bus_get_timeout(a, &timeout_a); if (r < 0) { log_error_errno(r, "Failed to get timeout: %m"); goto finish; } events_b = sd_bus_get_events(b); if (events_b < 0) { log_error_errno(r, "Failed to get events mask: %m"); goto finish; } r = sd_bus_get_timeout(b, &timeout_b); if (r < 0) { log_error_errno(r, "Failed to get timeout: %m"); goto finish; } t = timeout_a; if (t == (uint64_t) -1 || (timeout_b != (uint64_t) -1 && timeout_b < timeout_a)) t = timeout_b; if (t == (uint64_t) -1) ts = NULL; else { usec_t nw; nw = now(CLOCK_MONOTONIC); if (t > nw) t -= nw; else t = 0; ts = timespec_store(&_ts, t); } { struct pollfd p[3] = { {.fd = fd, .events = events_a, }, {.fd = STDIN_FILENO, .events = events_b & POLLIN, }, {.fd = STDOUT_FILENO, .events = events_b & POLLOUT, }};
static void client_chart(Type type, const char *address, const char *server_name, int fd) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *x = NULL; size_t csize; sd_bus *b; int r; r = sd_bus_new(&b); assert_se(r >= 0); if (type == TYPE_DIRECT) { r = sd_bus_set_fd(b, fd, fd); assert_se(r >= 0); } else { r = sd_bus_set_address(b, address); assert_se(r >= 0); r = sd_bus_set_bus_client(b, true); assert_se(r >= 0); } r = sd_bus_start(b); assert_se(r >= 0); r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL); assert_se(r >= 0); switch (type) { case TYPE_KDBUS: printf("SIZE\tCOPY\tMEMFD\n"); break; case TYPE_LEGACY: printf("SIZE\tLEGACY\n"); break; case TYPE_DIRECT: printf("SIZE\tDIRECT\n"); break; } for (csize = 1; csize <= MAX_SIZE; csize *= 2) { usec_t t; unsigned n_copying, n_memfd; printf("%zu\t", csize); if (type == TYPE_KDBUS) { b->use_memfd = 0; t = now(CLOCK_MONOTONIC); for (n_copying = 0;; n_copying++) { transaction(b, csize, server_name); if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) break; } printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec)); b->use_memfd = -1; } t = now(CLOCK_MONOTONIC); for (n_memfd = 0;; n_memfd++) { transaction(b, csize, server_name); if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) break; } printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec)); } b->use_memfd = 1; assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0); assert_se(sd_bus_message_append(x, "t", csize) >= 0); assert_se(sd_bus_send(b, x, NULL) >= 0); sd_bus_unref(b); }