sysctl-util: add sysctl_read_ip_property()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 18 Feb 2019 05:41:43 +0000 (14:41 +0900)
committerMichael Biebl <biebl@debian.org>
Thu, 18 Jul 2019 17:38:23 +0000 (18:38 +0100)
(cherry picked from commit a6b3b0aace152b77682d68d99b3e41580c955efb)

Gbp-Pq: Name sysctl-util-add-sysctl_read_ip_property.patch

src/shared/sysctl-util.c
src/shared/sysctl-util.h

index 480e6c38a112975a9fe0b49c709fea45c06bb78a..ba894899964f7e76f7a7b7d2cd1ee39d014cbfe9 100644 (file)
@@ -69,3 +69,25 @@ int sysctl_read(const char *property, char **content) {
         p = strjoina("/proc/sys/", property);
         return read_full_file(p, content, NULL);
 }
+
+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);
+
+        r = read_one_line_file(p, &value);
+        if (r < 0)
+                return r;
+
+        if (ret)
+                *ret = TAKE_PTR(value);
+
+        return r;
+}
index fd7c78b2b8d5869866bb7a8c6469a4688c653ad6..22f52f8421e8d921b7d13923a5116ce4c9dcb7b7 100644 (file)
@@ -5,3 +5,4 @@ char *sysctl_normalize(char *s);
 int sysctl_read(const char *property, char **value);
 int sysctl_write(const char *property, const char *value);
 
+int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret);