bus-util: add flags for bus_map_all_properties() (#8546)

This adds flags BUS_MAP_STRDUP and BUS_MAP_BOOLEAN_AS_BOOL.
If BUS_MAP_STRDUP is set, then each "s" message is duplicated.
If BUS_MAP_BOOLEAN_AS_BOOL is set, then each "b" message is
written to a bool pointer.

Follow-up for #8488.
See https://github.com/systemd/systemd/pull/8488#discussion_r175816270.
This commit is contained in:
Yu Watanabe 2018-03-28 20:37:27 +09:00 committed by Lennart Poettering
parent 6f7729c176
commit a7e4861c74
11 changed files with 60 additions and 19 deletions

View file

@ -473,6 +473,7 @@ static int acquire_host_info(sd_bus *bus, struct host_info **hi) {
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
hostname_map,
BUS_MAP_STRDUP,
&error,
NULL,
host);
@ -483,6 +484,7 @@ static int acquire_host_info(sd_bus *bus, struct host_info **hi) {
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
manager_map,
BUS_MAP_STRDUP,
&error,
NULL,
host);

View file

@ -170,6 +170,7 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) {
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
hostname_map,
0,
error,
&host_message,
&info);
@ -180,6 +181,7 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) {
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
manager_map,
0,
error,
&manager_message,
&info);

View file

@ -162,6 +162,7 @@ static int show_status(int argc, char **argv, void *userdata) {
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
map,
0,
&error,
&m,
&info);

View file

@ -439,7 +439,7 @@ static int print_session_status_info(sd_bus *bus, const char *path, bool *new_li
SessionStatusInfo i = {};
int r;
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &error, &m, &i);
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
if (r < 0)
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
@ -570,7 +570,7 @@ static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line)
_cleanup_(user_status_info_clear) UserStatusInfo i = {};
int r;
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &error, &m, &i);
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
if (r < 0)
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
@ -644,7 +644,7 @@ static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line)
_cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
int r;
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &error, &m, &i);
r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
if (r < 0)
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));

View file

@ -811,6 +811,7 @@ static int show_machine_info(const char *verb, sd_bus *bus, const char *path, bo
"org.freedesktop.machine1",
path,
map,
0,
&error,
&m,
&info);
@ -1092,6 +1093,7 @@ static int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
"org.freedesktop.machine1",
path,
map,
BUS_MAP_BOOLEAN_AS_BOOL,
&error,
&m,
&info);
@ -1152,6 +1154,7 @@ static int show_pool_info(sd_bus *bus) {
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
map,
0,
&error,
&m,
&info);

View file

@ -1235,6 +1235,7 @@ static int status_ifindex(sd_bus *bus, int ifindex, const char *name, bool *empt
"org.freedesktop.resolve1",
p,
property_map,
BUS_MAP_BOOLEAN_AS_BOOL,
&error,
&m,
&link_info);
@ -1446,6 +1447,7 @@ static int status_global(sd_bus *bus, bool *empty_line) {
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
property_map,
BUS_MAP_BOOLEAN_AS_BOOL,
&error,
&m,
&global_info);

View file

@ -875,6 +875,7 @@ static int run_context_update(RunContext *c, const char *path) {
"org.freedesktop.systemd1",
path,
map,
BUS_MAP_STRDUP,
&error,
NULL,
c);

View file

@ -1057,7 +1057,7 @@ int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_err
return 0;
}
static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata, bool copy_string) {
static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigned flags, sd_bus_error *error, void *userdata) {
char type;
int r;
@ -1078,7 +1078,7 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_
if (isempty(s))
s = NULL;
if (copy_string)
if (flags & BUS_MAP_STRDUP)
return free_and_strdup((char **) userdata, s);
*p = s;
@ -1099,14 +1099,17 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_
}
case SD_BUS_TYPE_BOOLEAN: {
unsigned b;
bool *p = userdata;
int b;
r = sd_bus_message_read_basic(m, type, &b);
if (r < 0)
return r;
*p = !!b;
if (flags & BUS_MAP_BOOLEAN_AS_BOOL)
* (bool*) userdata = !!b;
else
* (int*) userdata = b;
return 0;
}
@ -1151,7 +1154,7 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_
int bus_message_map_all_properties(
sd_bus_message *m,
const struct bus_properties_map *map,
bool copy_string,
unsigned flags,
sd_bus_error *error,
void *userdata) {
@ -1194,7 +1197,7 @@ int bus_message_map_all_properties(
if (map[i].set)
r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
else
r = map_basic(sd_bus_message_get_bus(m), member, m, error, v, copy_string);
r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v);
if (r < 0)
return r;
@ -1220,7 +1223,7 @@ int bus_message_map_all_properties(
int bus_message_map_properties_changed(
sd_bus_message *m,
const struct bus_properties_map *map,
bool copy_string,
unsigned flags,
sd_bus_error *error,
void *userdata) {
@ -1230,7 +1233,7 @@ int bus_message_map_properties_changed(
assert(m);
assert(map);
r = bus_message_map_all_properties(m, map, copy_string, error, userdata);
r = bus_message_map_all_properties(m, map, flags, error, userdata);
if (r < 0)
return r;
@ -1260,6 +1263,7 @@ int bus_map_all_properties(
const char *destination,
const char *path,
const struct bus_properties_map *map,
unsigned flags,
sd_bus_error *error,
sd_bus_message **reply,
void *userdata) {
@ -1271,6 +1275,7 @@ int bus_map_all_properties(
assert(destination);
assert(path);
assert(map);
assert(reply || (flags & BUS_MAP_STRDUP));
r = sd_bus_call_method(
bus,
@ -1284,7 +1289,7 @@ int bus_map_all_properties(
if (r < 0)
return r;
r = bus_message_map_all_properties(m, map, !reply, error, userdata);
r = bus_message_map_all_properties(m, map, flags, error, userdata);
if (r < 0)
return r;

View file

@ -49,11 +49,17 @@ struct bus_properties_map {
size_t offset;
};
enum {
BUS_MAP_STRDUP = 1U << 0, /* If set, each "s" message is duplicated. Thus, each pointer needs to be freed. */
BUS_MAP_BOOLEAN_AS_BOOL = 1U << 1, /* If set, each "b" message is written to a bool pointer. If not set, "b" is written to a int pointer. */
};
int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata);
int bus_message_map_all_properties(sd_bus_message *m, const struct bus_properties_map *map, bool copy_string, sd_bus_error *error, void *userdata);
int bus_message_map_properties_changed(sd_bus_message *m, const struct bus_properties_map *map, bool copy_string, sd_bus_error *error, void *userdata);
int bus_map_all_properties(sd_bus *bus, const char *destination, const char *path, const struct bus_properties_map *map, sd_bus_error *error, sd_bus_message **reply, void *userdata);
int bus_message_map_all_properties(sd_bus_message *m, const struct bus_properties_map *map, unsigned flags, sd_bus_error *error, void *userdata);
int bus_message_map_properties_changed(sd_bus_message *m, const struct bus_properties_map *map, unsigned flags, sd_bus_error *error, void *userdata);
int bus_map_all_properties(sd_bus *bus, const char *destination, const char *path, const struct bus_properties_map *map,
unsigned flags, sd_bus_error *error, sd_bus_message **reply, void *userdata);
int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name);

View file

@ -1686,6 +1686,7 @@ static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, cha
"org.freedesktop.systemd1",
path,
map[arg_dependency],
0,
&error,
NULL,
&info);
@ -1892,7 +1893,15 @@ static int get_machine_properties(sd_bus *bus, struct machine_info *mi) {
bus = container;
}
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, NULL, NULL, mi);
r = bus_map_all_properties(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
machine_info_property_map,
BUS_MAP_STRDUP,
NULL,
NULL,
mi);
if (r < 0)
return r;
@ -5018,6 +5027,7 @@ static int show_one(
"org.freedesktop.systemd1",
path,
show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map,
BUS_MAP_BOOLEAN_AS_BOOL,
&error,
&reply,
&info);
@ -5151,7 +5161,15 @@ static int show_system_status(sd_bus *bus) {
if (!hn)
return log_oom();
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, &error, NULL, &mi);
r = bus_map_all_properties(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
machine_info_property_map,
BUS_MAP_STRDUP,
&error,
NULL,
&mi);
if (r < 0)
return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r));
@ -6090,7 +6108,7 @@ static int unit_exists(LookupPaths *lp, const char *unit) {
if (r < 0)
return r;
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, &error, &m, &info);
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info);
if (r < 0)
return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));

View file

@ -161,6 +161,7 @@ static int show_status(int argc, char **argv, void *userdata) {
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
map,
BUS_MAP_BOOLEAN_AS_BOOL,
&error,
&m,
&info);