int main(int argc, char *argv[]) { sd_bus_slot *slot = NULL; int r; char *mode = NULL; /* Connect to system bus */ // r = sd_bus_open_user(&bus); r = sd_bus_open_system(&bus); if (r < 0) { fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); goto finish; } r = sd_bus_add_match(bus, &slot, FILTER, bus_signal_cb, NULL); if (r < 0) { fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), FILTER); goto finish; } r = sd_bus_add_match(bus, &slot, FILTER2, greeting, NULL); if (r < 0) { fprintf(stderr, "Failed: %d sd_bus_add_match: %s : %s\n", __LINE__, strerror(-r), FILTER); goto finish; } for (;;) { /* Process requests */ r = sd_bus_process(bus, NULL); if (r < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); goto finish; } if (r > 0) { continue; } r = sd_bus_wait(bus, (uint64_t) - 1); if (r < 0) { fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); goto finish; } } finish: sd_bus_slot_unref(slot); sd_bus_unref(bus); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
static void server(sd_bus *b, size_t *result) { int r; for (;;) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; r = sd_bus_process(b, &m); assert_se(r >= 0); if (r == 0) assert_se(sd_bus_wait(b, (usec_t) -1) >= 0); if (!m) continue; if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping")) assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0); else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) { const void *p; size_t sz; /* Make sure the mmap is mapped */ assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0); assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0); } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) { uint64_t res; assert_se(sd_bus_message_read(m, "t", &res) > 0); *result = res; return; } else assert_not_reached("Unknown method"); } }
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); }
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 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); }
static struct ipmi_rs *ipmi_dbus_sendrecv(struct ipmi_intf *intf, struct ipmi_rq *req) { sd_bus_message *msg; int rc; (void)intf; rsp.ccode = 0xff; rc = sd_bus_message_new_signal(bus, &msg, object_path, interface, "ReceivedMessage"); if (rc < 0) { lprintf(LOG_ERR, "%s: failed to create message: %s\n", __func__, strerror(-rc)); goto out; } rc = sd_bus_message_append(msg, "yyyy", ++seq, req->msg.netfn, req->msg.lun, req->msg.cmd); if (rc < 0) { lprintf(LOG_ERR, "%s: failed to init bytes\n", __func__); goto out_free; } rc = sd_bus_message_append_array(msg, 'y', req->msg.data, req->msg.data_len); if (rc < 0) { lprintf(LOG_ERR, "%s: failed to init body\n", __func__); goto out_free; } rc = sd_bus_send(bus, msg, NULL); if (rc < 0) { lprintf(LOG_ERR, "%s: failed to send dbus message\n", __func__); goto out_free; } for (reply_received = false; !reply_received;) { rc = sd_bus_wait(bus, -1); sd_bus_process(bus, NULL); } out_free: sd_bus_message_unref(msg); out: return &rsp; }
void kirstu_dbus_process(void) { printf("D-BUS Loop\n"); int r = sd_bus_process(bus, NULL); if (r < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); exit(EXIT_FAILURE); } if (r > 0) { return; } }
/* * This is the main loop of the d-bus service. It won't exit until * quit_dbus_main_loop() is called. * * It is should be invoked as the startup function of a thread or the caller * should not expect it to return. */ void * multipath_main_loop(void * ap) { sd_bus_slot *slot = NULL; sd_bus *bus = NULL; int r; /* Connect to the user bus this time */ r = sd_bus_open_user(&bus); if (r < 0) { fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); goto finish; } sync_maps(bus, slot); /* Take a well-known service name so that clients can find us */ r = sd_bus_request_name(bus, MULTIPATH_BASE_INTERFACE, 0); if (r < 0) { fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r)); goto finish; } for (;;) { /* Process requests */ r = sd_bus_process(bus, NULL); if (r < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); goto finish; } if (r > 0) /* we processed a request, try to process another one, right-away */ continue; /* Wait for the next request to process */ r = sd_bus_wait(bus, (uint64_t) - 1); if (r < 0) { fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); goto finish; } } finish: sd_bus_slot_unref(slot); sd_bus_unref(bus); return NULL; }
int main(int argc, char *argv[]) { sd_bus_slot *slot = NULL; int r; char *mode = NULL; if (argc != 2) { fprintf(stderr, "syntax: %s [server|client]\n", argv[0]); return 1; } mode = argv[1]; /* Connect to system bus */ r = sd_bus_open_system(&bus); if (r < 0) { fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); goto finish; } if (!strcmp("server", mode)) { r = sd_bus_add_object_vtable(bus, &slot, OBJ, /* object path */ INT, /* interface name */ signal_vtable, NULL); if (r < 0) { fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r)); goto finish; } /* Take a well-known service name so that clients can find us */ r = sd_bus_request_name(bus, INT, 0); if (r < 0) { fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r)); goto finish; } } else if (!strcmp("client", mode)) { r = sd_bus_add_match(bus, &slot, FILTER, bus_signal_cb, NULL); if (r < 0) { fprintf(stderr, "Failed: sd_bus_add_match: %s\n", strerror(-r)); goto finish; } } else { fprintf(stderr, "Invalid operating mode %s", mode); return 1; } for (;;) { /* Process requests */ r = sd_bus_process(bus, NULL); if (r < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); goto finish; } if (r > 0) { continue; } r = sd_bus_wait(bus, (uint64_t) - 1); if (r < 0) { fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); goto finish; } } finish: sd_bus_slot_unref(slot); sd_bus_unref(bus); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
int main(int argc, char *argv[]) { sd_bus_slot *slot = NULL; int r; char *mode = NULL; // Register all the handlers that provider implementation to IPMI commands. ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH); #ifdef __IPMI_DEBUG__ printf("Registered Function handlers:\n"); // Print the registered handlers and their arguments. for(auto& iter : g_ipmid_router_map) { ipmi_fn_cmd_t fn_and_cmd = iter.first; printf("NETFN:[0x%X], cmd[0x%X]\n", fn_and_cmd.first, fn_and_cmd.second); } #endif /* Connect to system bus */ r = sd_bus_open_system(&bus); if (r < 0) { fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); goto finish; } r = sd_bus_add_match(bus, &slot, FILTER, handle_ipmi_command, NULL); if (r < 0) { fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), FILTER); goto finish; } for (;;) { /* Process requests */ r = sd_bus_process(bus, NULL); if (r < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); goto finish; } if (r > 0) { continue; } r = sd_bus_wait(bus, (uint64_t) - 1); if (r < 0) { fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); goto finish; } } finish: sd_bus_slot_unref(slot); sd_bus_unref(bus); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
static void* client2(void*p) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; bool quit = false; const char *mid; int r; r = sd_bus_open_user(&bus); if (r < 0) { log_error_errno(r, "Failed to connect to user bus: %m"); goto finish; } r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.systemd.test", "/foo/bar/waldo/piep", "org.object.test", "Foobar"); if (r < 0) { log_error_errno(r, "Failed to allocate method call: %m"); goto finish; } r = sd_bus_send(bus, m, NULL); if (r < 0) { log_error("Failed to issue method call: %s", bus_error_message(&error, -r)); goto finish; } m = sd_bus_message_unref(m); r = sd_bus_message_new_signal( bus, &m, "/foobar", "foo.bar", "Notify"); if (r < 0) { log_error_errno(r, "Failed to allocate signal: %m"); goto finish; } r = sd_bus_send(bus, m, NULL); if (r < 0) { log_error("Failed to issue signal: %s", bus_error_message(&error, -r)); goto finish; } m = sd_bus_message_unref(m); r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.systemd.test", "/", "org.freedesktop.DBus.Peer", "GetMachineId"); if (r < 0) { log_error_errno(r, "Failed to allocate method call: %m"); goto finish; } 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)); goto finish; } r = sd_bus_message_read(reply, "s", &mid); if (r < 0) { log_error_errno(r, "Failed to parse machine ID: %m"); goto finish; } log_info("Machine ID is %s.", mid); m = sd_bus_message_unref(m); r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.systemd.test", "/", "org.freedesktop.systemd.test", "Slow"); if (r < 0) { log_error_errno(r, "Failed to allocate method call: %m"); goto finish; } reply = sd_bus_message_unref(reply); r = sd_bus_call(bus, m, 200 * USEC_PER_MSEC, &error, &reply); if (r < 0) log_info("Failed to issue method call: %s", bus_error_message(&error, -r)); else log_info("Slow call succeed."); m = sd_bus_message_unref(m); r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.systemd.test", "/", "org.freedesktop.systemd.test", "Slow"); if (r < 0) { log_error_errno(r, "Failed to allocate method call: %m"); goto finish; } r = sd_bus_call_async(bus, NULL, m, quit_callback, &quit, 200 * USEC_PER_MSEC); if (r < 0) { log_info("Failed to issue method call: %s", bus_error_message(&error, -r)); goto finish; } while (!quit) { r = sd_bus_process(bus, NULL); if (r < 0) { log_error_errno(r, "Failed to process requests: %m"); goto finish; } if (r == 0) { r = sd_bus_wait(bus, (uint64_t) -1); if (r < 0) { log_error_errno(r, "Failed to wait: %m"); goto finish; } } } r = 0; finish: if (bus) { _cleanup_bus_message_unref_ sd_bus_message *q; r = sd_bus_message_new_method_call( bus, &q, "org.freedesktop.systemd.test", "/", "org.freedesktop.systemd.test", "ExitClient2"); if (r < 0) { log_error_errno(r, "Failed to allocate method call: %m"); goto finish; } (void) sd_bus_send(bus, q, NULL); } return INT_TO_PTR(r); }
static int server(sd_bus *bus) { int r; bool client1_gone = false, client2_gone = false; while (!client1_gone || !client2_gone) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; pid_t pid = 0; const char *label = NULL; r = sd_bus_process(bus, &m); 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; } if (!m) continue; sd_bus_creds_get_pid(sd_bus_message_get_creds(m), &pid); sd_bus_creds_get_selinux_context(sd_bus_message_get_creds(m), &label); log_info("Got message! member=%s pid="PID_FMT" label=%s", strna(sd_bus_message_get_member(m)), pid, strna(label)); /* bus_message_dump(m); */ /* sd_bus_message_rewind(m, true); */ if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "LowerCase")) { const char *hello; _cleanup_free_ char *lowercase = NULL; r = sd_bus_message_read(m, "s", &hello); if (r < 0) { log_error_errno(r, "Failed to get parameter: %m"); goto fail; } lowercase = strdup(hello); if (!lowercase) { r = log_oom(); goto fail; } ascii_strlower(lowercase); r = sd_bus_reply_method_return(m, "s", lowercase); if (r < 0) { log_error_errno(r, "Failed to send reply: %m"); goto fail; } } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient1")) { r = sd_bus_reply_method_return(m, NULL); if (r < 0) { log_error_errno(r, "Failed to send reply: %m"); goto fail; } client1_gone = true; } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient2")) { r = sd_bus_reply_method_return(m, NULL); if (r < 0) { log_error_errno(r, "Failed to send reply: %m"); goto fail; } client2_gone = true; } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Slow")) { sleep(1); r = sd_bus_reply_method_return(m, NULL); if (r < 0) { log_error_errno(r, "Failed to send reply: %m"); goto fail; } } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "FileDescriptor")) { int fd; static const char x = 'X'; r = sd_bus_message_read(m, "h", &fd); if (r < 0) { log_error_errno(r, "Failed to get parameter: %m"); goto fail; } log_info("Received fd=%d", fd); if (write(fd, &x, 1) < 0) { log_error_errno(errno, "Failed to write to fd: %m"); safe_close(fd); goto fail; } r = sd_bus_reply_method_return(m, NULL); if (r < 0) { log_error_errno(r, "Failed to send reply: %m"); goto fail; } } else if (sd_bus_message_is_method_call(m, NULL, NULL)) { r = sd_bus_reply_method_error( m, &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method.")); if (r < 0) { log_error_errno(r, "Failed to send reply: %m"); goto fail; } } } r = 0; fail: if (bus) { sd_bus_flush(bus); sd_bus_unref(bus); } return r; }
int main(int argc, char *argv[]) { sd_bus_slot *slot = NULL; sd_bus *bus = NULL; int r; char **acquired = NULL, **activatable = NULL, **i; /* Connect to the user bus this time */ r = sd_bus_open_system(&bus); if (r < 0) { fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); goto finish; } /* Install an object */ r = sd_bus_add_object_vtable(bus, &slot, "/org/openbmc/examples/path0/SDBusObj", /* object path */ "org.openbmc.examples.Echo", /* interface name */ echo_vtable, NULL); if (r < 0) { fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r)); goto finish; } /* Install an object */ r = sd_bus_add_object_vtable(bus, &slot, "/org/openbmc/examples/path1/SDBusObj", /* object path */ "org.openbmc.examples.Echo", /* interface name */ echo_vtable, NULL); if (r < 0) { fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r)); goto finish; } /* Take a well-known service name so that clients can find us */ r = sd_bus_request_name(bus, "org.openbmc.examples.SDBusService", 0); if (r < 0) { fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r)); goto finish; } for (;;) { /* Process requests */ r = sd_bus_process(bus, NULL); if (r < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); goto finish; } if (r > 0) /* we processed a request, try to process another one, right-away */ continue; /* Wait for the next request to process */ r = sd_bus_wait(bus, (uint64_t) -1); if (r < 0) { fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); goto finish; } } finish: sd_bus_slot_unref(slot); sd_bus_unref(bus); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
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); }
/* * ------------------------------------------------ * Called as part of setting up skeleton services. * ----------------------------------------------- */ int start_led_services() { /* Generic error reporter. */ int rc = -1; int num_leds = 0; int count_leds = 0; /* Bus and slot where we are offering the LED dbus service. */ sd_bus *bus_type = NULL; sd_bus_slot *led_slot = NULL; /* For walking '/sys/class/leds/' looking for names of LED.*/ struct dirent **led_list; /* Get a hook onto system bus. */ rc = sd_bus_open_system(&bus_type); if(rc < 0) { fprintf(stderr,"Error opening system bus.\n"); return rc; } count_leds = num_leds = scandir("/sys/class/leds/", &led_list, led_select, alphasort); if(num_leds <= 0) { fprintf(stderr,"No LEDs present in the system\n"); sd_bus_slot_unref(led_slot); sd_bus_unref(bus_type); return rc; } /* Fully qualified Dbus object for a particular LED */ char led_object[128] = {0}; int len = 0; /* For each led present, announce the service on dbus. */ while(num_leds--) { memset(led_object, 0x0, sizeof(led_object)); len = snprintf(led_object, sizeof(led_object), "%s%s", "/org/openbmc/controller/led/", led_list[num_leds]->d_name); if(len >= sizeof(led_object)) { fprintf(stderr, "Error. LED object is too long:[%d]\n",len); rc = -1; break; } /* Install the object */ rc = sd_bus_add_object_vtable(bus_type, &led_slot, led_object, /* object path */ "org.openbmc.controller.led", /* interface name */ led_control_vtable, NULL); if (rc < 0) { fprintf(stderr, "Failed to add object to dbus: %s\n", strerror(-rc)); break; } } /* Done with all registration. */ while (count_leds > 0) { free(led_list[--count_leds]); if(count_leds == 0) { free(led_list); } } /* If we had success in adding the providers, request for a bus name. */ if(rc == 0) { /* Take one in OpenBmc */ rc = sd_bus_request_name(bus_type, "org.openbmc.controller.led", 0); if (rc < 0) { fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-rc)); } else { for (;;) { /* Process requests */ rc = sd_bus_process(bus_type, NULL); if (rc < 0) { fprintf(stderr, "Failed to process bus: %s\n", strerror(-rc)); break; } if (rc > 0) { continue; } rc = sd_bus_wait(bus_type, (uint64_t) - 1); if (rc < 0) { fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-rc)); break; } } } } sd_bus_slot_unref(led_slot); sd_bus_unref(bus_type); return rc; }
/* * First of all, creates a transaction; * then reads the newly created bus path, * adds a match to the bus to wait until installation is really finished before notifying the user. * finally, calls InstallFiles method, and processes the bus while finished == 0. */ void *install_package(void *str) { sd_bus_error error = SD_BUS_ERROR_NULL; sd_bus_message *mess = NULL; sd_bus *install_bus = NULL; const char *path; int r, inhibit_fd = -1, finished = 0; r = sd_bus_open_system(&install_bus); if (r < 0) { print_and_warn(strerror(-r), ERR_LINE); goto finish; } INFO("calling CreateTransaction on bus."); r = sd_bus_call_method(install_bus, "org.freedesktop.PackageKit", "/org/freedesktop/PackageKit", "org.freedesktop.PackageKit", "CreateTransaction", &error, &mess, NULL); if (r < 0) { print_and_warn(error.message, ERR_LINE); goto finish; } if (config.inhibit) { inhibit_fd = inhibit_suspend("Package installation..."); } sd_bus_message_read(mess, "o", &path); r = sd_bus_add_match(install_bus, NULL, "type='signal',interface='org.freedesktop.PackageKit.Transaction',member='Finished'", match_callback, &finished); if (r < 0) { print_and_warn(strerror(-r), ERR_LINE); goto finish; } sd_bus_flush(install_bus); INFO("calling InstallFiles on bus."); r = sd_bus_call_method(install_bus, "org.freedesktop.PackageKit", path, "org.freedesktop.PackageKit.Transaction", "InstallFiles", &error, NULL, "tas", 0, 1, (char *)str); if (r < 0) { print_and_warn(error.message, ERR_LINE); goto finish; } while (!finished) { r = sd_bus_process(install_bus, NULL); if (r > 0) { continue; } r = sd_bus_wait(install_bus, (uint64_t) -1); if (r < 0) { break; } } finish: stop_inhibition(inhibit_fd); close_bus(&error, mess, install_bus); pthread_detach(pthread_self()); pthread_exit(NULL); }
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_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; }
static int BusHandler(sd_event_source *es, int fd, uint32_t revents, void *userdata) { sd_bus_process(bus, NULL); return 1; }
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_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_(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, }};
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; }