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 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); }
void manager_free(Manager *m) { Network *network; Bridge *bridge; Link *link; udev_monitor_unref(m->udev_monitor); udev_unref(m->udev); sd_bus_unref(m->bus); sd_event_source_unref(m->udev_event_source); sd_event_unref(m->event); while ((network = m->networks)) network_free(network); while ((link = hashmap_first(m->links))) link_free(link); hashmap_free(m->links); while ((bridge = hashmap_first(m->bridges))) bridge_free(bridge); hashmap_free(m->bridges); sd_rtnl_unref(m->rtnl); free(m); }
static int connect_bus(void) { int r; sd_bus *bus = NULL; struct source_ctx *s; r = sd_bus_default_system(&bus); SOL_INT_CHECK(r, < 0, r); s = sol_mainloop_source_get_data(_ctx.mainloop_source); r = sd_bus_attach_event(bus, s->event, SD_EVENT_PRIORITY_NORMAL); SOL_INT_CHECK_GOTO(r, < 0, fail); r = sd_bus_add_match(bus, NULL, "type='signal'," "sender='org.freedesktop.DBus.Local'," "interface='org.freedesktop.DBus.Local'," "member='Disconnected'", _match_disconnected, &_ctx); SOL_INT_CHECK_GOTO(r, < 0, fail); _ctx.bus = bus; return 0; fail: sd_bus_unref(bus); return r; }
static void manager_free(struct manager *m) { unsigned int i; struct link *l; if (!m) return; while ((l = MANAGER_FIRST_LINK(m))) link_free(l); manager_dbus_disconnect(m); shl_htable_clear_uint(&m->links, NULL, NULL); sd_event_source_unref(m->udev_mon_source); udev_monitor_unref(m->udev_mon); udev_unref(m->udev); for (i = 0; m->sigs[i]; ++i) sd_event_source_unref(m->sigs[i]); sd_bus_unref(m->bus); sd_event_unref(m->event); free(m->friendly_name); free(m); }
void manager_free(Manager *m) { Network *network; NetDev *netdev; Link *link; if (!m) return; udev_monitor_unref(m->udev_monitor); udev_unref(m->udev); sd_bus_unref(m->bus); sd_event_source_unref(m->udev_event_source); sd_event_source_unref(m->sigterm_event_source); sd_event_source_unref(m->sigint_event_source); sd_event_unref(m->event); while ((network = m->networks)) network_free(network); while ((link = hashmap_first(m->links))) link_free(link); hashmap_free(m->links); while ((netdev = hashmap_first(m->netdevs))) netdev_free(netdev); hashmap_free(m->netdevs); sd_rtnl_unref(m->rtnl); free(m); }
void manager_free(Manager *m) { Machine *machine; assert(m); while (m->operations) operation_free(m->operations); assert(m->n_operations == 0); while ((machine = hashmap_first(m->machines))) machine_free(machine); hashmap_free(m->machines); hashmap_free(m->machine_units); hashmap_free(m->machine_leaders); hashmap_free_with_destructor(m->image_cache, image_unref); sd_event_source_unref(m->image_cache_defer_event); bus_verify_polkit_async_registry_free(m->polkit_registry); sd_bus_unref(m->bus); sd_event_unref(m->event); free(m); }
void manager_free(Manager *m) { Machine *machine; Image *i; assert(m); while ((machine = hashmap_first(m->machines))) machine_free(machine); hashmap_free(m->machines); hashmap_free(m->machine_units); hashmap_free(m->machine_leaders); while ((i = hashmap_steal_first(m->image_cache))) image_unref(i); hashmap_free(m->image_cache); sd_event_source_unref(m->image_cache_defer_event); bus_verify_polkit_async_registry_free(m->polkit_registry); sd_bus_unref(m->bus); sd_event_unref(m->event); free(m); }
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 struct ratbagctl *ratbagctl_free(struct ratbagctl *ctl) { if (!ctl) return NULL; ctl->bus = sd_bus_unref(ctl->bus); return mfree(ctl); }
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); }
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; }
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 run_context_free(RunContext *c) { assert(c); c->forward = pty_forward_free(c->forward); c->match = sd_bus_slot_unref(c->match); c->bus = sd_bus_unref(c->bus); c->event = sd_event_unref(c->event); free(c->active_state); free(c->result); }
static void server_done(Server *s) { assert(s); while (s->fifos) fifo_free(s->fifos); safe_close(s->epoll_fd); if (s->bus) { sd_bus_flush(s->bus); sd_bus_unref(s->bus); } }
static Modeset *modeset_free(Modeset *m) { if (!m) return NULL; m->grdev_session = grdev_session_free(m->grdev_session); m->grdev = grdev_context_unref(m->grdev); m->sysview = sysview_context_free(m->sysview); m->exit_src = sd_event_source_unref(m->exit_src); m->bus = sd_bus_unref(m->bus); m->event = sd_event_unref(m->event); free(m->seat); free(m->session); free(m); return NULL; }
Manager *manager_free(Manager *m) { if (!m) return NULL; assert(!m->workspace_list); m->idev = idev_context_unref(m->idev); m->grdev = grdev_context_unref(m->grdev); m->sysview = sysview_context_free(m->sysview); m->uf = unifont_unref(m->uf); m->sysbus = sd_bus_unref(m->sysbus); m->event = sd_event_unref(m->event); free(m); return NULL; }
void manager_free(Manager *m) { Machine *machine; assert(m); while ((machine = hashmap_first(m->machines))) machine_free(machine); hashmap_free(m->machines); hashmap_free(m->machine_units); hashmap_free(m->machine_leaders); sd_bus_unref(m->bus); sd_event_unref(m->event); free(m); }
/* * 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; }
static Evcat *evcat_free(Evcat *e) { if (!e) return NULL; e->idev_session = idev_session_free(e->idev_session); e->idev = idev_context_unref(e->idev); e->sysview = sysview_context_free(e->sysview); e->bus = sd_bus_unref(e->bus); e->event = sd_event_unref(e->event); free(e->seat); free(e->session); free(e); tcflush(0, TCIOFLUSH); return NULL; }
_public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) { if (!track) return NULL; assert(track->n_ref > 0); if (track->n_ref > 1) { track->n_ref--; return NULL; } if (track->in_list) LIST_REMOVE(tracks, track->bus->tracks, track); bus_track_remove_from_queue(track); hashmap_free_with_destructor(track->names, track_item_free); sd_bus_unref(track->bus); return mfree(track); }
void dispd_dbus_free(struct dispd_dbus *dispd_dbus) { if(!dispd_dbus) { return; } if(dispd_dbus->exposed) { sd_bus_release_name(dispd_dbus->bus, "org.freedesktop.miracle.wfd"); } if(dispd_dbus->bus) { sd_bus_unref(dispd_dbus->bus); } if(dispd_dbus->loop) { sd_event_unref(dispd_dbus->loop); } free(dispd_dbus); }
_public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) { const char *n; if (!track) return NULL; assert(track->n_ref > 0); if (track->n_ref > 1) { track->n_ref --; return NULL; } while ((n = hashmap_first_key(track->names))) sd_bus_track_remove_name(track, n); bus_track_remove_from_queue(track); hashmap_free(track->names); sd_bus_unref(track->bus); free(track); return NULL; }
_public_ int sd_bus_slot_set_floating(sd_bus_slot *slot, int b) { assert_return(slot, -EINVAL); if (slot->floating == !!b) return 0; if (!slot->bus) /* already disconnected slots can't be reconnected */ return -ESTALE; slot->floating = b; /* When a slot is "floating" then the bus references the slot. Otherwise the slot references the bus. Hence, * when we move from one to the other, let's increase one reference and decrease the other. */ if (b) { sd_bus_slot_ref(slot); sd_bus_unref(slot->bus); } else { sd_bus_ref(slot->bus); sd_bus_slot_unref(slot); } return 1; }
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 int server_init(sd_bus **_bus) { sd_bus *bus = NULL; sd_id128_t id; int r; const char *unique; assert_se(_bus); r = sd_bus_open_user(&bus); if (r < 0) { log_error_errno(r, "Failed to connect to user bus: %m"); goto fail; } r = sd_bus_get_bus_id(bus, &id); if (r < 0) { log_error_errno(r, "Failed to get server ID: %m"); goto fail; } r = sd_bus_get_unique_name(bus, &unique); if (r < 0) { log_error_errno(r, "Failed to get unique name: %m"); goto fail; } log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id)); log_info("Unique ID: %s", unique); log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h')); r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0); if (r < 0) { log_error_errno(r, "Failed to acquire name: %m"); goto fail; } r = sd_bus_add_fallback(bus, NULL, "/foo/bar", object_callback, NULL); if (r < 0) { log_error_errno(r, "Failed to add object: %m"); goto fail; } r = sd_bus_add_match(bus, NULL, "type='signal',interface='foo.bar',member='Notify'", match_callback, NULL); if (r < 0) { log_error_errno(r, "Failed to add match: %m"); goto fail; } r = sd_bus_add_match(bus, NULL, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL); if (r < 0) { log_error_errno(r, "Failed to add match: %m"); goto fail; } bus_match_dump(&bus->match_callbacks, 0); *_bus = bus; return 0; fail: if (bus) sd_bus_unref(bus); return 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; }
static void manager_free(Manager *m) { Session *session; User *u; Device *d; Seat *s; Inhibitor *i; Button *b; assert(m); while ((session = hashmap_first(m->sessions))) session_free(session); while ((u = hashmap_first(m->users))) user_free(u); while ((d = hashmap_first(m->devices))) device_free(d); while ((s = hashmap_first(m->seats))) seat_free(s); while ((i = hashmap_first(m->inhibitors))) inhibitor_free(i); while ((b = hashmap_first(m->buttons))) button_free(b); hashmap_free(m->devices); hashmap_free(m->seats); hashmap_free(m->sessions); hashmap_free(m->users); hashmap_free(m->inhibitors); hashmap_free(m->buttons); hashmap_free(m->user_units); hashmap_free(m->session_units); sd_event_source_unref(m->idle_action_event_source); sd_event_source_unref(m->inhibit_timeout_source); sd_event_source_unref(m->scheduled_shutdown_timeout_source); sd_event_source_unref(m->nologin_timeout_source); sd_event_source_unref(m->wall_message_timeout_source); sd_event_source_unref(m->console_active_event_source); sd_event_source_unref(m->udev_seat_event_source); sd_event_source_unref(m->udev_device_event_source); sd_event_source_unref(m->udev_vcsa_event_source); sd_event_source_unref(m->udev_button_event_source); sd_event_source_unref(m->lid_switch_ignore_event_source); safe_close(m->console_active_fd); udev_monitor_unref(m->udev_seat_monitor); udev_monitor_unref(m->udev_device_monitor); udev_monitor_unref(m->udev_vcsa_monitor); udev_monitor_unref(m->udev_button_monitor); udev_unref(m->udev); if (m->unlink_nologin) (void) unlink("/run/nologin"); bus_verify_polkit_async_registry_free(m->polkit_registry); sd_bus_unref(m->bus); sd_event_unref(m->event); safe_close(m->reserve_vt_fd); strv_free(m->kill_only_users); strv_free(m->kill_exclude_users); free(m->scheduled_shutdown_type); free(m->scheduled_shutdown_tty); free(m->wall_message); free(m->action_job); free(m); }
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; }