return 0;
}
+int bus_open_user_systemd(sd_bus **_bus) {
+ _cleanup_bus_unref_ sd_bus *bus = NULL;
+ _cleanup_free_ char *p = NULL;
+ const char *e;
+ int r;
+
+ /* If we are supposed to talk to the instance, try via
+ * XDG_RUNTIME_DIR first, then fallback to normal bus
+ * access */
+
+ assert(_bus);
+
+ e = secure_getenv("XDG_RUNTIME_DIR");
+ if (e) {
+ if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0)
+ return -ENOMEM;
+ }
+
+ r = sd_bus_new(&bus);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_set_address(bus, p);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_start(bus);
+ if (r < 0)
+ return r;
+
+ r = bus_check_peercred(bus);
+ if (r < 0)
+ return r;
+
+ *_bus = bus;
+ bus = NULL;
+
+ return 0;
+}
+
int bus_print_property(const char *name, sd_bus_message *property, bool all) {
char type;
const char *contents;
case SD_BUS_TYPE_ARRAY:
if (streq(contents, "s")) {
- bool space = false;
- char tp;
- const char *cnt;
+ bool first = true;
+ const char *str;
r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
if (r < 0)
return r;
- r = sd_bus_message_peek_type(property, &tp, &cnt);
+ while((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
+ if (first)
+ printf("%s=", name);
+
+ printf("%s%s", first ? "" : " ", str);
+
+ first = false;
+ }
if (r < 0)
return r;
- if (all || cnt) {
- const char *str;
-
+ if (first && all)
printf("%s=", name);
-
- while((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) >= 0) {
- printf("%s%s", space ? " " : "", str);
-
- space = true;
- }
- if (r < 0)
- return r;
-
+ if (!first || all)
puts("");
- }
r = sd_bus_message_exit_container(property);
if (r < 0)
return 0;
}
-int bus_print_all_properties(sd_bus *bus, const char *path, char **filter, bool all) {
+int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool all) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(path);
r = sd_bus_call_method(bus,
- "org.freedesktop.machine1",
+ dest,
path,
"org.freedesktop.DBus.Properties",
"GetAll",
r = bus_print_property(name, reply, all);
if (r < 0)
return r;
- if (r == 0 && all)
- printf("%s=[unprintable]\n", name);
+ if (r == 0) {
+ if (all)
+ printf("%s=[unprintable]\n", name);
+ /* skip what we didn't read */
+ r = sd_bus_message_skip(reply, contents);
+ if (r < 0)
+ return r;
+ }
r = sd_bus_message_exit_container(reply);
if (r < 0)
return r;
v = (uint8_t *)userdata + prop->offset;
- if (map[i].set)
+ if (map[i].set)
r = prop->set(bus, member, m, &error, v);
else
r = map_basic(bus, member, m, &error, v);
return r;
}
+int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
+ int r;
+
+ assert(transport >= 0);
+ assert(transport < _BUS_TRANSPORT_MAX);
+ assert(bus);
+
+ assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
+ assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -ENOTSUP);
+
+ switch (transport) {
+
+ case BUS_TRANSPORT_LOCAL:
+ if (user)
+ r = bus_open_user_systemd(bus);
+ else
+ r = bus_open_system_systemd(bus);
+
+ break;
+
+ case BUS_TRANSPORT_REMOTE:
+ r = sd_bus_open_system_remote(host, bus);
+ break;
+
+ case BUS_TRANSPORT_CONTAINER:
+ r = sd_bus_open_system_container(host, bus);
+ break;
+
+ default:
+ assert_not_reached("Hmm, unknown transport type.");
+ }
+
+ return r;
+}
+
int bus_property_get_bool(
sd_bus *bus,
const char *path,
return sd_bus_message_append_basic(reply, 'u', userdata);
}
+
+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);
+}