static int adapter_set_powered(struct context *ctx, const char *path, bool powered) { sd_bus *bus = sol_bus_client_get_bus(ctx->bluez); const char *service = sol_bus_client_get_service(ctx->bluez); sd_bus_message *m; int r; m = create_property_set(bus, service, path, "org.bluez.Adapter1", "Powered"); SOL_NULL_CHECK(m, -ENOMEM); r = sd_bus_message_open_container(m, 'v', "b"); SOL_INT_CHECK_GOTO(r, < 0, done); r = sd_bus_message_append(m, "b", &powered); SOL_INT_CHECK_GOTO(r, < 0, done); r = sd_bus_message_close_container(m); SOL_INT_CHECK_GOTO(r, < 0, done); r = sd_bus_call_async(bus, NULL, m, sol_bus_log_callback, NULL, 0); SOL_INT_CHECK_GOTO(r, < 0, done); done: sd_bus_message_unref(m); return r; }
static int kbdctx_query_locale(kbdctx *kc) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; int r; kc->slot_locale_get_all = sd_bus_slot_unref(kc->slot_locale_get_all); r = sd_bus_message_new_method_call(kc->context->sysbus, &m, "org.freedesktop.locale1", "/org/freedesktop/locale1", "org.freedesktop.DBus.Properties", "GetAll"); if (r < 0) goto error; r = sd_bus_message_append(m, "s", "org.freedesktop.locale1"); if (r < 0) goto error; r = sd_bus_call_async(kc->context->sysbus, &kc->slot_locale_get_all, m, kbdctx_locale_get_all_fn, kc, 0); if (r < 0) goto error; return 0; error: return log_debug_errno(r, "idev-keyboard: cannot send GetAll to locale1: %m"); }
int sysview_session_take_control(sysview_session *session) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; int r; assert_return(session, -EINVAL); assert_return(!session->custom, -EINVAL); if (session->wants_control) return 0; r = sd_bus_message_new_method_call(session->seat->context->sysbus, &m, "org.freedesktop.login1", session->path, "org.freedesktop.login1.Session", "TakeControl"); if (r < 0) return r; r = sd_bus_message_append(m, "b", 0); if (r < 0) return r; r = sd_bus_call_async(session->seat->context->sysbus, &session->slot_take_control, m, session_take_control_fn, session, 0); if (r < 0) return r; session->wants_control = true; return 0; }
int cc_Calculator_split_async( struct cc_client_Calculator *instance, double value, cc_Calculator_split_reply_t callback) { int result = 0; struct cc_instance *i; sd_bus_message *message = NULL; CC_LOG_DEBUG("invoked cc_Calculator_split_async()\n"); assert(instance); assert(callback); i = instance->instance; assert(i && i->backend && i->backend->bus); assert(i->service && i->path && i->interface); if (instance->split_reply_slot) { CC_LOG_ERROR("unable to call method with already pending reply\n"); return -EBUSY; } assert(!instance->split_reply_callback); result = sd_bus_message_new_method_call( i->backend->bus, &message, i->service, i->path, i->interface, "split"); if (result < 0) { CC_LOG_ERROR("unable to create message: %s\n", strerror(-result)); goto fail; } result = sd_bus_message_append(message, "d", value); if (result < 0) { CC_LOG_ERROR("unable to append message method arguments: %s\n", strerror(-result)); goto fail; } result = sd_bus_call_async( i->backend->bus, &instance->split_reply_slot, message, &cc_Calculator_split_reply_thunk, instance, CC_DBUS_ASYNC_CALL_TIMEOUT_USEC); if (result < 0) { CC_LOG_ERROR("unable to issue method call: %s\n", strerror(-result)); goto fail; } instance->split_reply_callback = callback; fail: message = sd_bus_message_unref(message); return result; }
_public_ int sd_bus_call_method_async( sd_bus *bus, sd_bus_slot **slot, const char *destination, const char *path, const char *interface, const char *member, sd_bus_message_handler_t callback, void *userdata, const char *types, ...) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; int r; assert_return(bus, -EINVAL); assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(!bus_pid_changed(bus), -ECHILD); if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; r = sd_bus_message_new_method_call(bus, &m, destination, path, interface, member); if (r < 0) return r; if (!isempty(types)) { va_list ap; va_start(ap, types); r = sd_bus_message_appendv(m, types, ap); va_end(ap); if (r < 0) return r; } return sd_bus_call_async(bus, slot, m, callback, userdata, 0); }
static void _bus_initialized(sd_bus *bus) { sd_bus_message *m = NULL; int r; _ctx.properties.system_state = SOL_PLATFORM_STATE_UNKNOWN; r = sd_bus_message_new_method_call(bus, &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Subscribe"); SOL_INT_CHECK_GOTO(r, < 0, fail_new_method); r = sd_bus_call_async(bus, NULL, m, sol_bus_log_callback, NULL, 0); SOL_INT_CHECK_GOTO(r, < 0, fail_call); sd_bus_message_unref(m); r = sol_bus_map_cached_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", _manager_properties, _manager_properties_changed, &_ctx); SOL_INT_CHECK_GOTO(r, < 0, fail_map_properties); return; fail_call: sd_bus_message_unref(m); fail_map_properties: fail_new_method: sol_bus_close(); }
int bus_verify_polkit_async( sd_bus_message *call, int capability, const char *action, bool interactive, Hashmap **registry, sd_bus_error *error) { #ifdef ENABLE_POLKIT _cleanup_bus_message_unref_ sd_bus_message *pk = NULL; AsyncPolkitQuery *q; const char *sender; sd_bus_message_handler_t callback; void *userdata; #endif int r; assert(call); assert(action); assert(registry); #ifdef ENABLE_POLKIT q = hashmap_get(*registry, call); if (q) { int authorized, challenge; /* This is the second invocation of this function, and * there's already a response from polkit, let's * process it */ assert(q->reply); if (sd_bus_message_is_method_error(q->reply, NULL)) { const sd_bus_error *e; /* Copy error from polkit reply */ e = sd_bus_message_get_error(q->reply); sd_bus_error_copy(error, e); /* Treat no PK available as access denied */ if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) return -EACCES; return -sd_bus_error_get_errno(e); } r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}"); if (r >= 0) r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge); if (r < 0) return r; if (authorized) return 1; return -EACCES; } #endif r = sd_bus_query_sender_privilege(call, capability); if (r < 0) return r; else if (r > 0) return 1; #ifdef ENABLE_POLKIT if (sd_bus_get_current_message(call->bus) != call) return -EINVAL; callback = sd_bus_get_current_handler(call->bus); if (!callback) return -EINVAL; userdata = sd_bus_get_current_userdata(call->bus); sender = sd_bus_message_get_sender(call); if (!sender) return -EBADMSG; r = hashmap_ensure_allocated(registry, trivial_hash_func, trivial_compare_func); if (r < 0) return r; r = sd_bus_message_new_method_call( call->bus, &pk, "org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority", "org.freedesktop.PolicyKit1.Authority", "CheckAuthorization"); if (r < 0) return r; r = sd_bus_message_append( pk, "(sa{sv})sa{ss}us", "system-bus-name", 1, "name", "s", sender, action, 0, interactive ? 1 : 0, NULL); if (r < 0) return r; q = new0(AsyncPolkitQuery, 1); if (!q) return -ENOMEM; q->request = sd_bus_message_ref(call); q->callback = callback; q->userdata = userdata; r = hashmap_put(*registry, call, q); if (r < 0) { async_polkit_query_free(q); return r; } q->registry = *registry; r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0); if (r < 0) { async_polkit_query_free(q); return r; } return 0; #endif return -EACCES; }
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); }