bus-util.c revision 21586b77de9bad401f245dd55b75cedb0c2e9b7c
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/***
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2013 Lennart Poettering
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen***/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include <sys/socket.h>
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen#include "util.h"
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#include "strv.h"
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#include "macro.h"
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include "def.h"
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include "missing.h"
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include "sd-event.h"
d122f9ee3a0e3c02ff8100a3dcd1866e90a6537aTom Gundersen#include "sd-bus.h"
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen#include "bus-error.h"
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen#include "bus-message.h"
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen#include "bus-util.h"
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen#include "bus-internal.h"
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersenstatic int quit_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen sd_event *e = userdata;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert(bus);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert(m);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert(e);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen sd_event_request_quit(e);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return 1;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen}
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersenint bus_async_unregister_and_quit(sd_event *e, sd_bus *bus, const char *name) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen _cleanup_free_ char *match = NULL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen int r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert(e);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert(bus);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen assert(name);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen r = asprintf(&match, "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameLost',arg0='%s'", name);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen if (r < 0)
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen return r;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen r = sd_bus_add_match(bus, match, quit_callback, e);
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen if (r < 0)
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen return r;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen r = sd_bus_release_name(bus, name);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen if (r < 0)
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen return r;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen if (r != SD_BUS_NAME_RELEASED)
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersen return -EIO;
756775814cf69471f74ce853745bba69f2ba94efThomas Hindoe Paaboel Andersen
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersen return 0;
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersen}
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersen
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersenint bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen bool exiting = false;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen int r;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert(e);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert(bus);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert(name);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen for (;;) {
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen r = sd_event_get_state(e);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r == SD_EVENT_FINISHED)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen break;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_event_run(e, exiting ? (uint64_t) -1 : timeout);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r == 0 && !exiting) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = bus_async_unregister_and_quit(e, bus, name);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen exiting = true;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen }
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen }
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return 0;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen}
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersenint bus_verify_polkit(
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen sd_bus *bus,
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen sd_bus_message *m,
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen const char *action,
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen bool interactive,
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt bool *_challenge,
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt sd_bus_error *e) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt const char *sender;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt uid_t uid;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen int r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert(bus);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert(m);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert(action);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen sender = sd_bus_message_get_sender(m);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (!sender)
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return -EBADMSG;
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_bus_get_owner_uid(bus, sender, &uid);
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering if (r < 0)
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering return r;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering if (uid == 0)
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering return 1;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering#ifdef ENABLE_POLKIT
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen else {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen int authorized = false, challenge = false;
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering r = sd_bus_call_method(
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering bus,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering "org.freedesktop.PolicyKit1",
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering "/org/freedesktop/PolicyKit1/Authority",
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering "org.freedesktop.PolicyKit1.Authority",
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering "CheckAuthorization",
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering e,
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering &reply,
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering "(sa{sv})sa{ss}us",
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen "system-bus-name", 1, "name", "s", sender,
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen action,
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen 0,
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen interactive ? 1 : 0,
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen "");
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (r < 0) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen /* Treat no PK available as access denied */
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen sd_bus_error_free(e);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return -EACCES;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen }
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return r;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen }
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen if (r >= 0)
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (authorized)
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return 1;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (_challenge) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen *_challenge = challenge;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return 0;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen }
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen }
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen#endif
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return -EACCES;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen}
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#ifdef ENABLE_POLKIT
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersentypedef struct AsyncPolkitQuery {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen sd_bus_message *request, *reply;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen sd_bus_message_handler_t callback;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen void *userdata;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen uint64_t serial;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Hashmap *registry;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen} AsyncPolkitQuery;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenstatic void async_polkit_query_free(sd_bus *b, AsyncPolkitQuery *q) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (!q)
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (q->serial > 0 && b)
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen sd_bus_call_async_cancel(b, q->serial);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (q->registry && q->request)
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen hashmap_remove(q->registry, q->request);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen sd_bus_message_unref(q->request);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen sd_bus_message_unref(q->reply);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen free(q);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen}
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenstatic int async_polkit_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen AsyncPolkitQuery *q = userdata;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen int r;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert(bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert(reply);
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen assert(q);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen q->reply = sd_bus_message_ref(reply);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen q->serial = 0;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_message_rewind(q->request, true);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (r < 0) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_reply_method_errno(q->request, r, NULL);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen goto finish;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen }
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = q->callback(bus, q->request, q->userdata, &error_buffer);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = bus_maybe_reply_error(q->request, r, &error_buffer);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenfinish:
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen async_polkit_query_free(bus, q);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen}
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#endif
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenint bus_verify_polkit_async(
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen sd_bus *bus,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen Hashmap **registry,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen sd_bus_message *m,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen const char *action,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen bool interactive,
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen sd_bus_error *error,
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen sd_bus_message_handler_t callback,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen void *userdata) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#ifdef ENABLE_POLKIT
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_bus_message_unref_ sd_bus_message *pk = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen AsyncPolkitQuery *q;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#endif
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen const char *sender;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen uid_t uid;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen int r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert(bus);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert(registry);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert(m);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert(action);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#ifdef ENABLE_POLKIT
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen q = hashmap_get(*registry, m);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (q) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen int authorized, challenge;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* This is the second invocation of this function, and
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen * there's already a response from polkit, let's
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen * process it */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert(q->reply);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (sd_bus_message_is_method_error(q->reply, NULL)) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen const sd_bus_error *e;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Copy error from polkit reply */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen e = sd_bus_message_get_error(q->reply);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen sd_bus_error_copy(error, e);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Treat no PK available as access denied */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return -EACCES;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return sd_bus_error_get_errno(e);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen }
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r >= 0)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r < 0)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (authorized)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return 1;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return -EACCES;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen }
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#endif
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen sender = sd_bus_message_get_sender(m);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (!sender)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return -EBADMSG;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_bus_get_owner_uid(bus, sender, &uid);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r < 0)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (uid == 0)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return 1;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#ifdef ENABLE_POLKIT
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = hashmap_ensure_allocated(registry, trivial_hash_func, trivial_compare_func);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r < 0)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_bus_message_new_method_call(
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen bus,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen "org.freedesktop.PolicyKit1",
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen "/org/freedesktop/PolicyKit1/Authority",
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen "org.freedesktop.PolicyKit1.Authority",
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen "CheckAuthorization",
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen &pk);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r < 0)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_bus_message_append(
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen pk,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen "(sa{sv})sa{ss}us",
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen "system-bus-name", 1, "name", "s", sender,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen action,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen 0,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen interactive ? 1 : 0,
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen NULL);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r < 0)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen q = new0(AsyncPolkitQuery, 1);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (!q)
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return -ENOMEM;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen q->request = sd_bus_message_ref(m);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen q->callback = callback;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen q->userdata = userdata;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = hashmap_put(*registry, m, q);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r < 0) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen async_polkit_query_free(bus, q);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen }
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen q->registry = *registry;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_bus_call_async(bus, pk, async_polkit_callback, q, 0, &q->serial);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (r < 0) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen async_polkit_query_free(bus, q);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return r;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen }
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return 0;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#endif
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return -EACCES;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen}
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenvoid bus_verify_polkit_async_registry_free(sd_bus *bus, Hashmap *registry) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen#ifdef ENABLE_POLKIT
d6bd972d061af306ede2affd2c9340a1660f7996Tom Gundersen AsyncPolkitQuery *q;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen while ((q = hashmap_steal_first(registry)))
d6bd972d061af306ede2affd2c9340a1660f7996Tom Gundersen async_polkit_query_free(bus, q);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen hashmap_free(registry);
d6bd972d061af306ede2affd2c9340a1660f7996Tom Gundersen#endif
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen}
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersenint bus_check_peercred(sd_bus *c) {
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen struct ucred ucred;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen socklen_t l;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen int fd;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert(c);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen fd = sd_bus_get_fd(c);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen if (fd < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return fd;
1231c4d238844e77018caf5b5852f01d96373c47Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen l = sizeof(struct ucred);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return -errno;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (l != sizeof(struct ucred))
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen return -E2BIG;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (ucred.uid != 0 && ucred.uid != geteuid())
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return -EPERM;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return 1;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen}
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williamsint bus_open_system_systemd(sd_bus **_bus) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen _cleanup_bus_unref_ sd_bus *bus = NULL;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen int r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert(_bus);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (geteuid() != 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return sd_bus_open_system(_bus);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen /* If we are root, then let's talk directly to the system
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen * instance, instead of going via the bus */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = sd_bus_new(&bus);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (r < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return r;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (r < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = sd_bus_start(bus);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (r < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = bus_check_peercred(bus);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen *_bus = bus;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen bus = NULL;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen return 0;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen}
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersenint bus_open_user_systemd(sd_bus **_bus) {
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen _cleanup_bus_unref_ sd_bus *bus = NULL;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen _cleanup_free_ char *p = NULL;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen const char *e;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen int r;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen /* If we are supposed to talk to the instance, try via
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen * XDG_RUNTIME_DIR first, then fallback to normal bus
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen * access */
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen assert(_bus);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen e = secure_getenv("XDG_RUNTIME_DIR");
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (e) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return -ENOMEM;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen }
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen r = sd_bus_new(&bus);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = sd_bus_set_address(bus, p);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = sd_bus_start(bus);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = bus_check_peercred(bus);
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen *_bus = bus;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen bus = NULL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return 0;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen}
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersenint bus_print_property(const char *name, sd_bus_message *property, bool all) {
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen char type;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen const char *contents;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen int r;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen assert(name);
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen assert(property);
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering r = sd_bus_message_peek_type(property, &type, &contents);
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering if (r < 0)
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering return r;
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering switch (type) {
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering case SD_BUS_TYPE_STRING: {
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering const char *s;
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = sd_bus_message_read_basic(property, type, &s);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (all || !isempty(s))
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen printf("%s=%s\n", name, s);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen return 1;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen }
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen case SD_BUS_TYPE_BOOLEAN: {
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen bool b;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen r = sd_bus_message_read_basic(property, type, &b);
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen if (r < 0)
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen return r;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen printf("%s=%s\n", name, yes_no(b));
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen return 1;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen }
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen case SD_BUS_TYPE_UINT64: {
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen uint64_t u;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen r = sd_bus_message_read_basic(property, type, &u);
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (r < 0)
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen return r;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen /* Yes, heuristics! But we can change this check
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen * should it turn out to not be sufficient */
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (endswith(name, "Timestamp")) {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen char timestamp[FORMAT_TIMESTAMP_MAX], *t;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen t = format_timestamp(timestamp, sizeof(timestamp), u);
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (t || all)
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen printf("%s=%s\n", name, strempty(t));
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams } else if (strstr(name, "USec")) {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen char timespan[FORMAT_TIMESPAN_MAX];
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u, 0));
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen } else
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen printf("%s=%llu\n", name, (unsigned long long) u);
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen return 1;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen }
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen case SD_BUS_TYPE_UINT32: {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen uint32_t u;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
c15fb62a731f1a457af94e60ac6a4d23f219a8f6Thomas Hindoe Paaboel Andersen r = sd_bus_message_read_basic(property, type, &u);
c15fb62a731f1a457af94e60ac6a4d23f219a8f6Thomas Hindoe Paaboel Andersen if (r < 0)
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen return r;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (strstr(name, "UMask") || strstr(name, "Mode"))
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen printf("%s=%04o\n", name, u);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen else
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen printf("%s=%u\n", name, (unsigned) u);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return 1;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen }
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen case SD_BUS_TYPE_INT32: {
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen int32_t i;
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen r = sd_bus_message_read_basic(property, type, &i);
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen if (r < 0)
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen printf("%s=%i\n", name, (int) i);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return 1;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen }
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen case SD_BUS_TYPE_DOUBLE: {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen double d;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen r = sd_bus_message_read_basic(property, type, &d);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (r < 0)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return r;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen printf("%s=%g\n", name, d);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return 1;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen }
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen case SD_BUS_TYPE_ARRAY:
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (streq(contents, "s")) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen bool first = true;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen const char *str;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (r < 0)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return r;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen while((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (first)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen printf("%s=", name);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen printf("%s%s", first ? "" : " ", str);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen first = false;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen }
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (r < 0)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return r;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (first && all)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen printf("%s=", name);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (!first || all)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen puts("");
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen r = sd_bus_message_exit_container(property);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (r < 0)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return r;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return 1;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen } else if (streq(contents, "y")) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen const uint8_t *u;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen size_t n;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
e2acdb6b0f68d9b4152708a9f21bf9e11f8b9e7eTorstein Husebø if (r < 0)
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen return r;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (all || n > 0) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen unsigned int i;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen printf("%s=", name);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen for (i = 0; i < n; i++)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen printf("%02x", u[i]);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen puts("");
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen }
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return 1;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen } else if (streq(contents, "u")) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen uint32_t *u;
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen size_t n;
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (r < 0)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return r;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (all || n > 0) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen unsigned int i;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen printf("%s=", name);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen for (i = 0; i < n; i++)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen printf("%08x", u[i]);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen puts("");
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen }
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return 1;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen }
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen break;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen }
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return 0;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen}
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersenint bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool all) {
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen int r;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert(bus);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert(path);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen r = sd_bus_call_method(bus,
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen dest,
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen path,
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen "org.freedesktop.DBus.Properties",
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen "GetAll",
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen &error,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen &reply,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen "s", "");
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (r < 0)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen return r;
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (r < 0)
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen return r;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen const char *name;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen const char *contents;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (!filter || strv_find(filter, name)) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = sd_bus_message_peek_type(reply, NULL, &contents);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = bus_print_property(name, reply, all);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r == 0) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (all)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen printf("%s=[unprintable]\n", name);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* skip what we didn't read */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_bus_message_skip(reply, contents);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen }
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_bus_message_exit_container(reply);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen } else {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = sd_bus_message_skip(reply, "v");
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (r < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen }
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = sd_bus_message_exit_container(reply);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (r < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen }
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen if (r < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen r = sd_bus_message_exit_container(reply);
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen if (r < 0)
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen return r;
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen return 0;
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen}
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersenint bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen sd_id128_t *p = userdata;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen const void *v;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen size_t n;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen int r;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (n == 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen *p = SD_ID128_NULL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen else if (n == 16)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen memcpy((*p).bytes, v, n);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen else
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return -EINVAL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return 0;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen}
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersenstatic int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen char type;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen int r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = sd_bus_message_peek_type(m, &type, NULL);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen switch (type) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen case SD_BUS_TYPE_STRING: {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen const char *s;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen char *str;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen char **p = userdata;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = sd_bus_message_read_basic(m, type, &s);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen break;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen if (isempty(s))
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen break;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen str = strdup(s);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (!str) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = -ENOMEM;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen break;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen }
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen free(*p);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen *p = str;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen break;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen }
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen case SD_BUS_TYPE_ARRAY: {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen _cleanup_strv_free_ char **l = NULL;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen char ***p = userdata;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = bus_message_read_strv_extend(m, &l);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen break;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
a7f7d1bde43fc825c49afea3f946f5b4b3d563e0Harald Hoyer strv_free(*p);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen *p = l;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen l = NULL;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen break;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen }
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
02557f973aed0fed7154fefe53d67e2935f918dcThomas Hindoe Paaboel Andersen case SD_BUS_TYPE_BOOLEAN: {
02557f973aed0fed7154fefe53d67e2935f918dcThomas Hindoe Paaboel Andersen unsigned b;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen bool *p = userdata;
02557f973aed0fed7154fefe53d67e2935f918dcThomas Hindoe Paaboel Andersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_bus_message_read_basic(m, type, &b);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen if (r < 0)
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen break;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen *p = b;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen break;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen }
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen case SD_BUS_TYPE_UINT32: {
bff92d2c2f913c368d80066ee89855665818edc8Susant Sahani uint64_t u;
bff92d2c2f913c368d80066ee89855665818edc8Susant Sahani uint32_t *p = userdata;
bff92d2c2f913c368d80066ee89855665818edc8Susant Sahani
38a03f06a7393d2721c23f23f0589d2f6d0904afLennart Poettering r = sd_bus_message_read_basic(m, type, &u);
bff92d2c2f913c368d80066ee89855665818edc8Susant Sahani if (r < 0)
bff92d2c2f913c368d80066ee89855665818edc8Susant Sahani break;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen *p = u;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen break;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen }
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen case SD_BUS_TYPE_UINT64: {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen uint64_t t;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen uint64_t *p = userdata;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = sd_bus_message_read_basic(m, type, &t);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen break;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen *p = t;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen break;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen }
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen default:
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen break;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen }
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen return r;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen}
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersenint bus_map_all_properties(sd_bus *bus,
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen const char *destination,
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen const char *path,
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen const struct bus_properties_map *map,
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen void *userdata) {
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen int r;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert(bus);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert(destination);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert(path);
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen assert(map);
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen r = sd_bus_call_method( bus,
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen destination,
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen path,
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen "org.freedesktop.DBus.Properties",
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen "GetAll",
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen &error,
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen &m,
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen "s", "");
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen if (r < 0)
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen return r;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen if (r < 0)
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen return r;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen const struct bus_properties_map *prop;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen const char *member;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen const char *contents;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen void *v;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen unsigned i;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen return r;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen for (i = 0, prop = NULL; map[i].member; i++)
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen if (streq(map[i].member, member)) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen prop = &map[i];
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen break;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen }
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (prop) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_message_peek_type(m, NULL, &contents);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (r < 0)
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen return r;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen if (r < 0)
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen return r;
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen v = (uint8_t *)userdata + prop->offset;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (map[i].set)
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = prop->set(bus, member, m, &error, v);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen else
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = map_basic(bus, member, m, &error, v);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_message_exit_container(m);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (r < 0)
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen return r;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen } else {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_message_skip(m, "v");
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (r < 0)
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen return r;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen }
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_message_exit_container(m);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (r < 0)
a38d99451f2bf8026ec51aee91662292e823c6a8Lennart Poettering return r;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen }
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen return r;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen}
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
2a1288ff89322a2f49c79f6d1832c8164c14a05cLennart Poetteringint bus_open_transport(BusTransport transport, const char *host, bool user, sd_bus **bus) {
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen int r;
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen assert(transport >= 0);
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen assert(transport < _BUS_TRANSPORT_MAX);
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen assert(bus);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -ENOTSUP);
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen switch (transport) {
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen case BUS_TRANSPORT_LOCAL:
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen if (user)
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen r = sd_bus_default_user(bus);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen else
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_default_system(bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen break;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen case BUS_TRANSPORT_REMOTE:
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_open_system_remote(host, bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen break;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen case BUS_TRANSPORT_CONTAINER:
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_open_system_container(host, bus);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen break;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen default:
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen assert_not_reached("Hmm, unknown transport type.");
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen }
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen return r;
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen}
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersenint bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen int r;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert(transport >= 0);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert(transport < _BUS_TRANSPORT_MAX);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert(bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -ENOTSUP);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen switch (transport) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen case BUS_TRANSPORT_LOCAL:
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (user)
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = bus_open_user_systemd(bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen else
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = bus_open_system_systemd(bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen break;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen case BUS_TRANSPORT_REMOTE:
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_open_system_remote(host, bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen break;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen case BUS_TRANSPORT_CONTAINER:
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = sd_bus_open_system_container(host, bus);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen break;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen default:
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen assert_not_reached("Hmm, unknown transport type.");
621ac3d2cc8f37169166df9c7f379b0cb6b17e36Thomas Hindoe Paaboel Andersen }
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen return r;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen}
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersenint bus_property_get_tristate(
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen sd_bus *bus,
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen const char *path,
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen const char *interface,
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen const char *property,
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen sd_bus_message *reply,
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen void *userdata,
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen sd_bus_error *error) {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen int *tristate = userdata;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen return sd_bus_message_append(reply, "b", *tristate > 0);
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen}
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersenint bus_property_get_bool(
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen sd_bus *bus,
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen const char *path,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering const char *interface,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering const char *property,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering sd_bus_message *reply,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering void *userdata,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering sd_bus_error *error) {
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering int b = *(bool*) userdata;
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering return sd_bus_message_append_basic(reply, 'b', &b);
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering}
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering#if __SIZEOF_SIZE_T__ != 8
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poetteringint bus_property_get_size(
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering sd_bus *bus,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering const char *path,
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
uint64_t sz = *(size_t*) userdata;
return sd_bus_message_append_basic(reply, 't', &sz);
}
#endif
#if __SIZEOF_LONG__ != 8
int bus_property_get_long(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
int64_t l = *(long*) userdata;
return sd_bus_message_append_basic(reply, 'x', &l);
}
int bus_property_get_ulong(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
uint64_t ul = *(unsigned long*) userdata;
return sd_bus_message_append_basic(reply, 't', &ul);
}
#endif
int bus_log_parse_error(int r) {
log_error("Failed to parse message: %s", strerror(-r));
return r;
}
int bus_log_create_error(int r) {
log_error("Failed to create message: %s", strerror(-r));
return r;
}
int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
assert(message);
assert(u);
return sd_bus_message_read(
message,
"(ssssssouso)",
&u->id,
&u->description,
&u->load_state,
&u->active_state,
&u->sub_state,
&u->following,
&u->unit_path,
&u->job_id,
&u->job_type,
&u->job_path);
}
int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error) {
assert(m);
if (r < 0) {
if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
sd_bus_reply_method_errno(m, r, error);
} else if (sd_bus_error_is_set(error)) {
if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
sd_bus_reply_method_error(m, error);
} else
return r;
log_debug("Failed to process message [type=%s sender=%s path=%s interface=%s member=%s signature=%s]: %s",
bus_message_type_to_string(m->header->type),
strna(m->sender),
strna(m->path),
strna(m->interface),
strna(m->member),
strna(m->root_container.signature),
bus_error_message(error, r));
return 1;
}