From: Yu Watanabe Date: Mon, 18 Feb 2019 04:34:01 +0000 (+0900) Subject: sysctl-util: introduce sysctl_write_ip_property() and friends X-Git-Tag: v242-rc1~269^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3decde0226f5aabece893a413d9b5fb825da7cb4;p=thirdparty%2Fsystemd.git sysctl-util: introduce sysctl_write_ip_property() and friends --- diff --git a/src/shared/sysctl-util.c b/src/shared/sysctl-util.c index 480e6c38a11..82f557eb31e 100644 --- a/src/shared/sysctl-util.c +++ b/src/shared/sysctl-util.c @@ -60,6 +60,22 @@ int sysctl_write(const char *property, const char *value) { return 0; } +int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value) { + const char *p; + + assert(IN_SET(af, AF_INET, AF_INET6)); + assert(property); + assert(value); + + p = strjoina("/proc/sys/net/ipv", af == AF_INET ? "4" : "6", + ifname ? "/conf/" : "", strempty(ifname), + property[0] == '/' ? "" : "/", property); + + log_debug("Setting '%s' to '%s'", p, value); + + return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); +} + int sysctl_read(const char *property, char **content) { char *p; diff --git a/src/shared/sysctl-util.h b/src/shared/sysctl-util.h index fd7c78b2b8d..940118c2589 100644 --- a/src/shared/sysctl-util.h +++ b/src/shared/sysctl-util.h @@ -1,7 +1,28 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once +#include +#include + +#include "macro.h" +#include "stdio-util.h" +#include "util.h" + char *sysctl_normalize(char *s); int sysctl_read(const char *property, char **value); int sysctl_write(const char *property, const char *value); +int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value); +static inline int sysctl_write_ip_property_boolean(int af, const char *ifname, const char *property, bool value) { + return sysctl_write_ip_property(af, ifname, property, one_zero(value)); +} + +#define DEFINE_SYSCTL_WRITE_IP_PROPERTY(name, type, format) \ + static inline int sysctl_write_ip_property_##name(int af, const char *ifname, const char *property, type value) { \ + char buf[DECIMAL_STR_MAX(type)]; \ + xsprintf(buf, format, value); \ + return sysctl_write_ip_property(af, ifname, property, buf); \ + } + +DEFINE_SYSCTL_WRITE_IP_PROPERTY(int, int, "%i"); +DEFINE_SYSCTL_WRITE_IP_PROPERTY(uint32, uint32_t, "%" PRIu32);