summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-03-22 11:47:29 +0100
committerLennart Poettering <lennart@poettering.net>2019-04-12 14:25:44 +0200
commit83f18c91d095a437392a0b78f1c7631dcbc0ba5d (patch)
tree84023cb6113956077e77c0ca7c17312b09f7aa5b
parent9b2559a13e5b8ccdf3b429f3c7e82159e2a733b5 (diff)
downloadsystemd-83f18c91d095a437392a0b78f1c7631dcbc0ba5d.tar.gz
core: use string_table_lookup() at more places
-rw-r--r--src/core/dbus-unit.c24
-rw-r--r--src/core/unit.c8
2 files changed, 14 insertions, 18 deletions
diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c
index 6f1a74d6b5..d34833f4ff 100644
--- a/src/core/dbus-unit.c
+++ b/src/core/dbus-unit.c
@@ -19,6 +19,7 @@
#include "selinux-access.h"
#include "signal-util.h"
#include "special.h"
+#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
@@ -1029,26 +1030,23 @@ static int property_get_ip_counter(
void *userdata,
sd_bus_error *error) {
- CGroupIPAccountingMetric metric;
- uint64_t value = (uint64_t) -1;
+ static const char *const table[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = {
+ [CGROUP_IP_INGRESS_BYTES] = "IPIngressBytes",
+ [CGROUP_IP_EGRESS_BYTES] = "IPEgressBytes",
+ [CGROUP_IP_INGRESS_PACKETS] = "IPIngressPackets",
+ [CGROUP_IP_EGRESS_PACKETS] = "IPEgressPackets",
+ };
+
+ uint64_t value = UINT64_MAX;
Unit *u = userdata;
+ ssize_t metric;
assert(bus);
assert(reply);
assert(property);
assert(u);
- if (streq(property, "IPIngressBytes"))
- metric = CGROUP_IP_INGRESS_BYTES;
- else if (streq(property, "IPIngressPackets"))
- metric = CGROUP_IP_INGRESS_PACKETS;
- else if (streq(property, "IPEgressBytes"))
- metric = CGROUP_IP_EGRESS_BYTES;
- else {
- assert(streq(property, "IPEgressPackets"));
- metric = CGROUP_IP_EGRESS_PACKETS;
- }
-
+ assert_se((metric = string_table_lookup(table, ELEMENTSOF(table), property)) >= 0);
(void) unit_get_ip_accounting(u, metric, &value);
return sd_bus_message_append(reply, "t", value);
}
diff --git a/src/core/unit.c b/src/core/unit.c
index 99b7acbef1..bbc27243a9 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -3323,8 +3323,8 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
for (;;) {
_cleanup_free_ char *line = NULL;
- CGroupIPAccountingMetric m;
char *l, *v;
+ ssize_t m;
size_t k;
r = read_line(f, LONG_LINE_MAX, &line);
@@ -3576,10 +3576,8 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
}
/* Check if this is an IP accounting metric serialization field */
- for (m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++)
- if (streq(l, ip_accounting_metric_field[m]))
- break;
- if (m < _CGROUP_IP_ACCOUNTING_METRIC_MAX) {
+ m = string_table_lookup(ip_accounting_metric_field, ELEMENTSOF(ip_accounting_metric_field), l);
+ if (m >= 0) {
uint64_t c;
r = safe_atou64(v, &c);