1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

bus-util: add generic parser for extracting id128 values from bus messages

This commit is contained in:
Lennart Poettering 2024-03-01 14:47:52 +01:00
parent 120f4a4451
commit 8157cc0e3e
6 changed files with 37 additions and 40 deletions

View File

@ -570,23 +570,14 @@ static int method_get_unit_by_invocation_id(sd_bus_message *message, void *userd
_cleanup_free_ char *path = NULL;
Manager *m = ASSERT_PTR(userdata);
sd_id128_t id;
const void *a;
Unit *u;
size_t sz;
int r;
assert(message);
/* Anyone can call this method */
r = sd_bus_message_read_array(message, 'y', &a, &sz);
if (r < 0)
return r;
if (sz == 0)
id = SD_ID128_NULL;
else if (sz == 16)
memcpy(&id, a, sz);
else
if (bus_message_read_id128(message, &id) < 0)
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid invocation ID");
if (sd_id128_is_null(id)) {

View File

@ -767,22 +767,17 @@ static int print_image_hostname(sd_bus *bus, const char *name) {
static int print_image_machine_id(sd_bus *bus, const char *name) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
sd_id128_t id = SD_ID128_NULL;
const void *p;
size_t size;
sd_id128_t id;
int r;
r = bus_call_method(bus, bus_machine_mgr, "GetImageMachineID", NULL, &reply, "s", name);
if (r < 0)
return r;
r = sd_bus_message_read_array(reply, 'y', &p, &size);
r = bus_message_read_id128(reply, &id);
if (r < 0)
return r;
if (size == sizeof(sd_id128_t))
memcpy(&id, p, size);
if (!sd_id128_is_null(id))
printf(" Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id));

View File

@ -1552,8 +1552,6 @@ static int acquire_invocation_id(sd_bus *bus, const char *unit, sd_id128_t *ret)
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ char *object = NULL;
const void *p;
size_t l;
int r;
assert(bus);
@ -1576,20 +1574,11 @@ static int acquire_invocation_id(sd_bus *bus, const char *unit, sd_id128_t *ret)
if (r < 0)
return log_error_errno(r, "Failed to request invocation ID for unit: %s", bus_error_message(&error, r));
r = sd_bus_message_read_array(reply, 'y', &p, &l);
r = bus_message_read_id128(reply, ret);
if (r < 0)
return bus_log_parse_error(r);
if (l == 0) {
*ret = SD_ID128_NULL;
return 0; /* no uuid set */
}
if (l != sizeof(sd_id128_t))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UUID size, %zu != %zu.", l, sizeof(sd_id128_t));
memcpy(ret, p, l);
return !sd_id128_is_null(*ret);
return 0;
}
static void set_window_title(PTYForward *f) {

View File

@ -8,21 +8,12 @@
int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
sd_id128_t *p = userdata;
const void *v;
size_t n;
int r;
r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
r = bus_message_read_id128(m, p);
if (r < 0)
return bus_log_parse_error_debug(r);
if (n == 0)
*p = SD_ID128_NULL;
else if (n == 16)
memcpy((*p).bytes, v, n);
else
return -EINVAL;
return 0;
}

View File

@ -754,3 +754,32 @@ int bus_query_sender_pidref(
return bus_creds_get_pidref(creds, ret);
}
int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret) {
const void *a;
size_t sz;
int r;
assert(m);
r = sd_bus_message_read_array(m, 'y', &a, &sz);
if (r < 0)
return r;
switch (sz) {
case 0:
if (ret)
*ret = SD_ID128_NULL;
break;
case sizeof(sd_id128_t):
if (ret)
memcpy(ret, a, sz);
break;
default:
return -EINVAL;
}
return 0;
}

View File

@ -77,3 +77,5 @@ int bus_property_get_string_set(sd_bus *bus, const char *path, const char *inter
int bus_creds_get_pidref(sd_bus_creds *c, PidRef *ret);
int bus_query_sender_pidref(sd_bus_message *m, PidRef *ret);
int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret);