summaryrefslogtreecommitdiff
path: root/src/basic/sysctl-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-09-15 09:22:06 +0200
committerLennart Poettering <lennart@poettering.net>2021-09-15 16:32:40 +0200
commitd1469b70959660b400a2868867c564342605c74d (patch)
tree454cffe0e5d656b088154539ab91699c3bac6c1b /src/basic/sysctl-util.c
parentf9755203b972966d545bba7f3d257cf4c6a4f8a0 (diff)
downloadsystemd-d1469b70959660b400a2868867c564342605c74d.tar.gz
sysctl-util: make sysctl_read_ip_property() a wrapper around sysctl_read()
let's do what we did for sysctl_write()/sysctl_write_ip_property() also for the read paths: i.e. make one a wrapper of the other, and add more careful input validation.
Diffstat (limited to 'src/basic/sysctl-util.c')
-rw-r--r--src/basic/sysctl-util.c22
1 files changed, 9 insertions, 13 deletions
diff --git a/src/basic/sysctl-util.c b/src/basic/sysctl-util.c
index 9c81001e69..4e168dd48a 100644
--- a/src/basic/sysctl-util.c
+++ b/src/basic/sysctl-util.c
@@ -118,24 +118,20 @@ int sysctl_read(const char *property, char **ret) {
}
int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret) {
- _cleanup_free_ char *value = NULL;
const char *p;
- int r;
- assert(IN_SET(af, AF_INET, AF_INET6));
assert(property);
- p = strjoina("/proc/sys/net/ipv", af == AF_INET ? "4" : "6",
- ifname ? "/conf/" : "", strempty(ifname),
- property[0] == '/' ? "" : "/", property);
+ if (!IN_SET(af, AF_INET, AF_INET6))
+ return -EAFNOSUPPORT;
- r = read_full_virtual_file(p, &value, NULL);
- if (r < 0)
- return r;
+ if (ifname) {
+ if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
+ return -EINVAL;
- truncate_nl(value);
- if (ret)
- *ret = TAKE_PTR(value);
+ p = strjoina("net/", af_to_ipv4_ipv6(af), "/conf/", ifname, "/", property);
+ } else
+ p = strjoina("net/", af_to_ipv4_ipv6(af), "/", property);
- return r;
+ return sysctl_read(p, ret);
}