]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/sysctl-util.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / shared / sysctl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "log.h"
12 #include "macro.h"
13 #include "string-util.h"
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) {
41 char *p;
42 _cleanup_close_ int fd = -1;
43
44 assert(property);
45 assert(value);
46
47 log_debug("Setting '%s' to '%s'", property, value);
48
49 p = strjoina("/proc/sys/", property);
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;
61 }
62
63 int sysctl_read(const char *property, char **content) {
64 char *p;
65
66 assert(property);
67 assert(content);
68
69 p = strjoina("/proc/sys/", property);
70 return read_full_file(p, content, NULL);
71 }