diff options
author | Matteo Croce <teknoraver@meta.com> | 2024-07-01 21:58:30 +0200 |
---|---|---|
committer | Matteo Croce <teknoraver@meta.com> | 2024-09-11 23:01:25 +0200 |
commit | 766bcf302aa6a863e4a3e5ee26f2f2742077181d (patch) | |
tree | efdb579e8535e24d19b3c2ba72681b7082704944 /src/basic | |
parent | resolvectl: rework StatusMode handling into a switch/case statement (diff) | |
download | systemd-766bcf302aa6a863e4a3e5ee26f2f2742077181d.tar.xz systemd-766bcf302aa6a863e4a3e5ee26f2f2742077181d.zip |
extend sysctl functions to shadow values
Pass to all the sysctl_* functions a hashmap which can be used to
optionally save the value written in the sysctl.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/sysctl-util.c | 45 | ||||
-rw-r--r-- | src/basic/sysctl-util.h | 21 |
2 files changed, 52 insertions, 14 deletions
diff --git a/src/basic/sysctl-util.c b/src/basic/sysctl-util.c index b284c9ccd2..dfb99e1896 100644 --- a/src/basic/sysctl-util.c +++ b/src/basic/sysctl-util.c @@ -44,8 +44,39 @@ char* sysctl_normalize(char *s) { return s; } -int sysctl_write(const char *property, const char *value) { +static int shadow_update(Hashmap **shadow, const char *property, const char *value) { + _cleanup_free_ char *k = NULL, *v = NULL, *cur_k = NULL, *cur_v = NULL; + int r; + + assert(property); + assert(value); + + if (!shadow) + return 0; + + k = strdup(property); + if (!k) + return -ENOMEM; + + v = strdup(value); + if (!v) + return -ENOMEM; + + cur_v = hashmap_remove2(*shadow, k, (void**)&cur_k); + + r = hashmap_ensure_put(shadow, &path_hash_ops_free_free, k, v); + if (r < 0) + return r; + + TAKE_PTR(k); + TAKE_PTR(v); + + return 0; +} + +int sysctl_write_full(const char *property, const char *value, Hashmap **shadow) { char *p; + int r; assert(property); assert(value); @@ -58,6 +89,10 @@ int sysctl_write(const char *property, const char *value) { log_debug("Setting '%s' to '%s'", p, value); + r = shadow_update(shadow, p, value); + if (r < 0) + return r; + return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL); } @@ -76,7 +111,7 @@ int sysctl_writef(const char *property, const char *format, ...) { return sysctl_write(property, v); } -int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value) { +int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow) { const char *p; assert(property); @@ -93,10 +128,10 @@ int sysctl_write_ip_property(int af, const char *ifname, const char *property, c } else p = strjoina("net/", af_to_ipv4_ipv6(af), "/", property); - return sysctl_write(p, value); + return sysctl_write_full(p, value, shadow); } -int sysctl_write_ip_neighbor_property(int af, const char *ifname, const char *property, const char *value) { +int sysctl_write_ip_neighbor_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow) { const char *p; assert(property); @@ -113,7 +148,7 @@ int sysctl_write_ip_neighbor_property(int af, const char *ifname, const char *pr } else p = strjoina("net/", af_to_ipv4_ipv6(af), "/neigh/default/", property); - return sysctl_write(p, value); + return sysctl_write_full(p, value, shadow); } int sysctl_read(const char *property, char **ret) { diff --git a/src/basic/sysctl-util.h b/src/basic/sysctl-util.h index 2bf5491703..041292f693 100644 --- a/src/basic/sysctl-util.h +++ b/src/basic/sysctl-util.h @@ -10,27 +10,30 @@ 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_full(const char *property, const char *value, Hashmap **shadow); int sysctl_writef(const char *property, const char *format, ...) _printf_(2, 3); +static inline int sysctl_write(const char *property, const char *value) { + return sysctl_write_full(property, value, NULL); +} int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret); -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)); +int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow); +static inline int sysctl_write_ip_property_boolean(int af, const char *ifname, const char *property, bool value, Hashmap **shadow) { + return sysctl_write_ip_property(af, ifname, property, one_zero(value), shadow); } -int sysctl_write_ip_neighbor_property(int af, const char *ifname, const char *property, const char *value); -static inline int sysctl_write_ip_neighbor_property_uint32(int af, const char *ifname, const char *property, uint32_t value) { +int sysctl_write_ip_neighbor_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow); +static inline int sysctl_write_ip_neighbor_property_uint32(int af, const char *ifname, const char *property, uint32_t value, Hashmap **shadow) { char buf[DECIMAL_STR_MAX(uint32_t)]; xsprintf(buf, "%u", value); - return sysctl_write_ip_neighbor_property(af, ifname, property, buf); + return sysctl_write_ip_neighbor_property(af, ifname, property, buf, shadow); } #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) { \ + static inline int sysctl_write_ip_property_##name(int af, const char *ifname, const char *property, type value, Hashmap **shadow) { \ char buf[DECIMAL_STR_MAX(type)]; \ xsprintf(buf, format, value); \ - return sysctl_write_ip_property(af, ifname, property, buf); \ + return sysctl_write_ip_property(af, ifname, property, buf, shadow); \ } DEFINE_SYSCTL_WRITE_IP_PROPERTY(int, int, "%i"); |