1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-06 16:59:03 +03:00

resolvectl: simplify map_{link,global}_domains()

This commit is contained in:
Yu Watanabe 2018-05-03 17:10:02 +09:00
parent 5bc53feeac
commit c513bb6ea4

View File

@ -1273,6 +1273,39 @@ static int map_link_current_dns_server(sd_bus *bus, const char *member, sd_bus_m
return read_dns_server_one(m, false, userdata); return read_dns_server_one(m, false, userdata);
} }
static int read_domain_one(sd_bus_message *m, bool with_ifindex, char **ret) {
_cleanup_free_ char *str = NULL;
int ifindex, route_only, r;
const char *domain;
assert(m);
assert(ret);
if (with_ifindex)
r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only);
else
r = sd_bus_message_read(m, "(sb)", &domain, &route_only);
if (r <= 0)
return r;
if (with_ifindex && ifindex != 0) {
/* only show the global ones here */
*ret = NULL;
return 1;
}
if (route_only)
str = strappend("~", domain);
else
str = strdup(domain);
if (!str)
return -ENOMEM;
*ret = TAKE_PTR(str);
return 1;
}
static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
char ***l = userdata; char ***l = userdata;
int r; int r;
@ -1287,22 +1320,16 @@ static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m,
return r; return r;
for (;;) { for (;;) {
const char *domain; char *pretty = NULL;
int route_only;
char *pretty;
r = sd_bus_message_read(m, "(sb)", &domain, &route_only); r = read_domain_one(m, false, &pretty);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
break; break;
if (route_only) if (isempty(pretty))
pretty = strappend("~", domain); continue;
else
pretty = strdup(domain);
if (!pretty)
return -ENOMEM;
r = strv_consume(l, pretty); r = strv_consume(l, pretty);
if (r < 0) if (r < 0)
@ -1554,26 +1581,17 @@ static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m
return r; return r;
for (;;) { for (;;) {
const char *domain; char *pretty = NULL;
int route_only, ifindex;
char *pretty;
r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only); r = read_domain_one(m, true, &pretty);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
break; break;
if (ifindex != 0) /* only show the global ones here */ if (isempty(pretty))
continue; continue;
if (route_only)
pretty = strappend("~", domain);
else
pretty = strdup(domain);
if (!pretty)
return -ENOMEM;
r = strv_consume(l, pretty); r = strv_consume(l, pretty);
if (r < 0) if (r < 0)
return r; return r;