#include "log.h"
#include "dbus-common.h"
#include "util.h"
+#include "missing.h"
#include "def.h"
#include "strv.h"
* try via XDG_RUNTIME_DIR first, then
* fallback to normal bus access */
- e = getenv("XDG_RUNTIME_DIR");
+ e = secure_getenv("XDG_RUNTIME_DIR");
if (e) {
char *p;
assert(user || host);
if (user && host)
- asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
+ asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s%%40%s,argv3=systemd-stdio-bridge", user, host);
else if (user)
- asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
+ asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s%%40localhost,argv3=systemd-stdio-bridge", user);
else if (host)
asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
dbus_connection_set_exit_on_disconnect(bus, FALSE);
- if ((r = sync_auth(bus, error)) < 0) {
+ r = sync_auth(bus, error);
+ if (r < 0) {
dbus_connection_close(bus);
dbus_connection_unref(bus);
return r;
}
const char *bus_error_message(const DBusError *error) {
- assert(error);
+ if (!error)
+ return NULL;
/* Sometimes the D-Bus server is a little bit too verbose with
* its error messages, so let's override them here */
return error->message;
}
+const char *bus_error(const DBusError *error, int err) {
+ if (error && dbus_error_is_set(error))
+ return bus_error_message(error);
+
+ return strerror(err < 0 ? -err : err);
+}
+
DBusHandlerResult bus_default_message_handler(
DBusConnection *c,
DBusMessage *message,
const BusBoundProperties *bound_properties) {
DBusError error;
- DBusMessage *reply = NULL;
+ _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
int r;
assert(c);
if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
- if (!(reply = dbus_message_new_method_return(message)))
+ reply = dbus_message_new_method_return(message);
+ if (!reply)
goto oom;
if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
data = (char*)bp->base + p->offset;
if (p->indirect)
data = *(void**)data;
- r = p->append(&sub, property, data);
- if (r < 0) {
- if (r == -ENOMEM)
- goto oom;
- dbus_message_unref(reply);
+ r = p->append(&sub, property, data);
+ if (r == -ENOMEM)
+ goto oom;
+ if (r < 0)
return bus_send_error_reply(c, message, NULL, r);
- }
if (!dbus_message_iter_close_container(&iter, &sub))
goto oom;
return bus_send_error_reply(c, message, &error, -EINVAL);
}
- if (!(reply = dbus_message_new_method_return(message)))
+ reply = dbus_message_new_method_return(message);
+ if (!reply)
goto oom;
dbus_message_iter_init_append(reply, &iter);
if (p->indirect)
data = *(void**)data;
r = p->append(&sub3, p->property, data);
- if (r < 0) {
- if (r == -ENOMEM)
- goto oom;
-
- dbus_message_unref(reply);
+ if (r == -ENOMEM)
+ goto oom;
+ if (r < 0)
return bus_send_error_reply(c, message, NULL, r);
- }
if (!dbus_message_iter_close_container(&sub2, &sub3) ||
!dbus_message_iter_close_container(&sub, &sub2))
}
if (reply) {
- if (!dbus_connection_send(c, reply, NULL))
+ if (!bus_maybe_send_reply(c, message, reply))
goto oom;
- dbus_message_unref(reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
oom:
- if (reply)
- dbus_message_unref(reply);
-
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_NEED_MEMORY;
return 0;
}
+int bus_property_set_uint64(DBusMessageIter *i, const char *property, void *data) {
+ uint64_t *t = data;
+
+ assert(i);
+ assert(property);
+
+ dbus_message_iter_get_basic(i, t);
+ return 0;
+}
+
const char *bus_errno_to_dbus(int error) {
switch(error) {
return DBUS_ERROR_FAILED;
}
+dbus_bool_t bus_maybe_send_reply (DBusConnection *c,
+ DBusMessage *message,
+ DBusMessage *reply)
+{
+ /* Some parts of systemd "reply" to signals, which of course
+ * have the no-reply flag set. We will be defensive here and
+ * still send out a reply if we're passed a signal.
+ */
+ if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL &&
+ dbus_message_get_no_reply(message))
+ return TRUE;
+ return dbus_connection_send(c, reply, NULL);
+}
+
DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
- DBusMessage *reply = NULL;
+ _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
const char *name, *text;
if (berror && dbus_error_is_set(berror)) {
text = strerror(-error);
}
- if (!(reply = dbus_message_new_error(message, name, text)))
+ reply = dbus_message_new_error(message, name, text);
+ if (!reply)
goto oom;
- if (!dbus_connection_send(c, reply, NULL))
+ if (!bus_maybe_send_reply(c, message, reply))
goto oom;
- dbus_message_unref(reply);
-
if (berror)
dbus_error_free(berror);
dbus_message_iter_next(&sub);
}
- if (!(l = new(char*, n+1)))
+ l = new(char*, n+1);
+ if (!l)
return -ENOMEM;
dbus_message_iter_recurse(iter, &sub);
return 0;
}
+int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
+ DBusMessageIter sub, sub2;
+ unsigned n = 0, i = 0;
+ _cleanup_strv_free_ char **l = NULL;
+
+ assert(iter);
+ assert(_l);
+
+ if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
+ dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRUCT)
+ return -EINVAL;
+
+ dbus_message_iter_recurse(iter, &sub);
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ n++;
+ dbus_message_iter_next(&sub);
+ }
+
+ l = new(char*, n*2+1);
+ if (!l)
+ return -ENOMEM;
+ l[0] = NULL; /* make sure that l is properly terminated at all times */
+
+ dbus_message_iter_recurse(iter, &sub);
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ const char *a, *b;
+
+ assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT);
+
+ dbus_message_iter_recurse(&sub, &sub2);
+
+ if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &a, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &b, false) < 0)
+ return -EINVAL;
+
+ l[i] = strdup(a);
+ if (!l[i])
+ return -ENOMEM;
+ i++;
+
+ l[i] = strdup(b);
+ if (!l[i])
+ return -ENOMEM;
+ i++;
+
+ dbus_message_iter_next(&sub);
+ }
+
+ assert(i == n*2);
+ l[i] = NULL;
+
+ if (_l) {
+ *_l = l;
+ l = NULL; /* avoid freeing */
+ }
+
+ return 0;
+}
+
+int bus_parse_unit_info(DBusMessageIter *iter, struct unit_info *u) {
+ DBusMessageIter sub;
+
+ assert(iter);
+ assert(u);
+
+ if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRUCT)
+ return -EINVAL;
+
+ dbus_message_iter_recurse(iter, &sub);
+
+ if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &u->id, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &u->description, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &u->following, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
+ log_error("Failed to parse reply.");
+ return -EIO;
+ }
+
+ return 0;
+}
+
int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
DBusMessageIter sub;
} else if (strstr(name, "USec")) {
char timespan[FORMAT_TIMESPAN_MAX];
- printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
+ printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u, 0));
} else
printf("%s=%llu\n", name, (unsigned long long) u);
puts("");
}
+ return 1;
+
+ } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_UINT32) {
+ DBusMessageIter sub;
+
+ dbus_message_iter_recurse(iter, &sub);
+ if (all ||
+ dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ printf("%s=", name);
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ uint32_t u;
+
+ assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32);
+ dbus_message_iter_get_basic(&sub, &u);
+ printf("%08x", u);
+
+ dbus_message_iter_next(&sub);
+ }
+
+ puts("");
+ }
+
return 1;
}
}
void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) {
- DBusMessage *m = NULL;
+ _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
DBusPendingCall *pending = NULL;
assert(bus);
if (!dbus_pending_call_set_notify(pending, release_name_pending_cb, bus, NULL))
goto oom;
- dbus_message_unref(m);
dbus_pending_call_unref(pending);
return;
oom:
- log_error("Out of memory");
+ log_oom();
if (pending) {
dbus_pending_call_cancel(pending);
dbus_pending_call_unref(pending);
}
-
- if (m)
- dbus_message_unref(m);
}
DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata) {
assert(m);
assert(remain_until);
- /* Everytime we get a new message we reset out timeout */
+ /* Every time we get a new message we reset out timeout */
*remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected"))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
+
+/* This mimics dbus_bus_get_unix_user() */
+pid_t bus_get_unix_process_id(
+ DBusConnection *connection,
+ const char *name,
+ DBusError *error) {
+
+ _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL;
+ uint32_t pid = 0;
+
+ m = dbus_message_new_method_call(
+ DBUS_SERVICE_DBUS,
+ DBUS_PATH_DBUS,
+ DBUS_INTERFACE_DBUS,
+ "GetConnectionUnixProcessID");
+ if (!m) {
+ dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
+ return 0;
+ }
+
+ if (!dbus_message_append_args(
+ m,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID)) {
+ dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
+ return 0;
+ }
+
+ reply = dbus_connection_send_with_reply_and_block(connection, m, -1, error);
+ if (!reply)
+ return 0;
+
+ if (dbus_set_error_from_message(error, reply))
+ return 0;
+
+ if (!dbus_message_get_args(
+ reply, error,
+ DBUS_TYPE_UINT32, &pid,
+ DBUS_TYPE_INVALID))
+ return 0;
+
+ return (pid_t) pid;
+}
+
+bool bus_error_is_no_service(const DBusError *error) {
+ assert(error);
+
+ if (!dbus_error_is_set(error))
+ return false;
+
+ if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
+ return true;
+
+ if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
+ return true;
+
+ return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
+}
+
+int bus_method_call_with_reply(
+ DBusConnection *bus,
+ const char *destination,
+ const char *path,
+ const char *interface,
+ const char *method,
+ DBusMessage **return_reply,
+ DBusError *return_error,
+ int first_arg_type, ...) {
+
+ DBusError error;
+ _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
+ DBusMessage *reply;
+ va_list ap;
+ int r = 0;
+
+ dbus_error_init(&error);
+ assert(bus);
+
+ m = dbus_message_new_method_call(destination, path, interface, method);
+ if (!m) {
+ r = log_oom();
+ goto finish;
+ }
+
+ va_start(ap, first_arg_type);
+ if (!dbus_message_append_args_valist(m, first_arg_type, ap)) {
+ va_end(ap);
+ r = log_oom();
+ goto finish;
+ }
+ va_end(ap);
+
+ reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
+ if (!reply) {
+ if (!return_error)
+ log_error("Failed to issue method call: %s", bus_error_message(&error));
+
+ if (bus_error_is_no_service(&error))
+ r = -ENOENT;
+ else if (dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED))
+ r = -EACCES;
+ else if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY))
+ r = -ETIMEDOUT;
+ else if (dbus_error_has_name(&error, DBUS_ERROR_DISCONNECTED))
+ r = -ECONNRESET;
+ else
+ r = -EIO;
+ goto finish;
+ }
+
+ if (return_reply)
+ *return_reply = reply;
+ else
+ dbus_message_unref(reply);
+
+finish:
+ if (return_error)
+ *return_error = error;
+ else
+ dbus_error_free(&error);
+
+ return r;
+}
+
+void bus_message_unrefp(DBusMessage **reply) {
+ if (!reply)
+ return;
+
+ if (!*reply)
+ return;
+
+ dbus_message_unref(*reply);
+}
+
+const char *bus_message_get_sender_with_fallback(DBusMessage *m) {
+ const char *s;
+
+ assert(m);
+
+ s = dbus_message_get_sender(m);
+ if (s)
+ return s;
+
+ /* When the message came in from a direct connection the
+ * message will have no sender. We fix that here. */
+
+ return ":no-sender";
+}