]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
88a60da0 | 2 | |
521251d2 MG |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
3f6fd1ba | 5 | #include <stdio.h> |
3f6fd1ba | 6 | #include <string.h> |
521251d2 | 7 | #include <unistd.h> |
88a60da0 | 8 | |
521251d2 | 9 | #include "fd-util.h" |
3f6fd1ba | 10 | #include "fileio.h" |
88a60da0 | 11 | #include "log.h" |
a8fbdf54 | 12 | #include "macro.h" |
07630cea | 13 | #include "string-util.h" |
88a60da0 KS |
14 | #include "sysctl-util.h" |
15 | ||
16 | char *sysctl_normalize(char *s) { | |
17 | char *n; | |
18 | ||
19 | n = strpbrk(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. */ | |
23 | if (!n || *n == '/') | |
24 | return s; | |
25 | ||
26 | /* Otherwise, dots become slashes and slashes become | |
27 | * dots. Fun. */ | |
28 | while (n) { | |
29 | if (*n == '.') | |
30 | *n = '/'; | |
31 | else | |
32 | *n = '.'; | |
33 | ||
34 | n = strpbrk(n + 1, "/."); | |
35 | } | |
36 | ||
37 | return s; | |
38 | } | |
39 | ||
40 | int sysctl_write(const char *property, const char *value) { | |
0f5a8be5 | 41 | char *p; |
521251d2 | 42 | _cleanup_close_ int fd = -1; |
88a60da0 | 43 | |
0f5a8be5 LP |
44 | assert(property); |
45 | assert(value); | |
88a60da0 | 46 | |
0f5a8be5 | 47 | log_debug("Setting '%s' to '%s'", property, value); |
88a60da0 | 48 | |
0f5a8be5 | 49 | p = strjoina("/proc/sys/", property); |
521251d2 MG |
50 | fd = open(p, O_WRONLY|O_CLOEXEC); |
51 | if (fd < 0) | |
52 | return -errno; | |
53 | ||
54 | if (!endswith(value, "\n")) | |
55 | value = strjoina(value, "\n"); | |
56 | ||
57 | if (write(fd, value, strlen(value)) < 0) | |
58 | return -errno; | |
59 | ||
60 | return 0; | |
88a60da0 KS |
61 | } |
62 | ||
63 | int sysctl_read(const char *property, char **content) { | |
0f5a8be5 | 64 | char *p; |
88a60da0 | 65 | |
0f5a8be5 LP |
66 | assert(property); |
67 | assert(content); | |
88a60da0 | 68 | |
0f5a8be5 | 69 | p = strjoina("/proc/sys/", property); |
88a60da0 KS |
70 | return read_full_file(p, content, NULL); |
71 | } |