1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 #include "string-util.h"
14 #include "sysctl-util.h"
16 char *sysctl_normalize(char *s
) {
20 /* If the first separator is a slash, the path is
21 * assumed to be normalized and slashes remain slashes
22 * and dots remains dots. */
26 /* Otherwise, dots become slashes and slashes become
34 n
= strpbrk(n
+ 1, "/.");
40 int sysctl_write(const char *property
, const char *value
) {
42 _cleanup_close_
int fd
= -1;
47 log_debug("Setting '%s' to '%.*s'.", property
, (int) strcspn(value
, NEWLINE
), value
);
49 p
= strjoina("/proc/sys/", property
);
50 fd
= open(p
, O_WRONLY
|O_CLOEXEC
);
54 if (!endswith(value
, "\n"))
55 value
= strjoina(value
, "\n");
57 if (write(fd
, value
, strlen(value
)) < 0)
63 int sysctl_write_ip_property(int af
, const char *ifname
, const char *property
, const char *value
) {
66 assert(IN_SET(af
, AF_INET
, AF_INET6
));
70 p
= strjoina("/proc/sys/net/ipv", af
== AF_INET
? "4" : "6",
71 ifname
? "/conf/" : "", strempty(ifname
),
72 property
[0] == '/' ? "" : "/", property
);
74 log_debug("Setting '%s' to '%s'", p
, value
);
76 return write_string_file(p
, value
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
79 int sysctl_read(const char *property
, char **content
) {
85 p
= strjoina("/proc/sys/", property
);
86 return read_full_file(p
, content
, NULL
);
89 int sysctl_read_ip_property(int af
, const char *ifname
, const char *property
, char **ret
) {
90 _cleanup_free_
char *value
= NULL
;
94 assert(IN_SET(af
, AF_INET
, AF_INET6
));
97 p
= strjoina("/proc/sys/net/ipv", af
== AF_INET
? "4" : "6",
98 ifname
? "/conf/" : "", strempty(ifname
),
99 property
[0] == '/' ? "" : "/", property
);
101 r
= read_one_line_file(p
, &value
);
106 *ret
= TAKE_PTR(value
);