]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | /*** | |
3 | Copyright 2010 Lennart Poettering | |
4 | ***/ | |
5 | ||
6 | #include <errno.h> | |
7 | #include <fcntl.h> | |
8 | #include <stdio.h> | |
9 | #include <string.h> | |
10 | #include <unistd.h> | |
11 | ||
12 | #include "fd-util.h" | |
13 | #include "fileio.h" | |
14 | #include "log.h" | |
15 | #include "macro.h" | |
16 | #include "string-util.h" | |
17 | #include "sysctl-util.h" | |
18 | ||
19 | char *sysctl_normalize(char *s) { | |
20 | char *n; | |
21 | ||
22 | n = strpbrk(s, "/."); | |
23 | /* If the first separator is a slash, the path is | |
24 | * assumed to be normalized and slashes remain slashes | |
25 | * and dots remains dots. */ | |
26 | if (!n || *n == '/') | |
27 | return s; | |
28 | ||
29 | /* Otherwise, dots become slashes and slashes become | |
30 | * dots. Fun. */ | |
31 | while (n) { | |
32 | if (*n == '.') | |
33 | *n = '/'; | |
34 | else | |
35 | *n = '.'; | |
36 | ||
37 | n = strpbrk(n + 1, "/."); | |
38 | } | |
39 | ||
40 | return s; | |
41 | } | |
42 | ||
43 | int sysctl_write(const char *property, const char *value) { | |
44 | char *p; | |
45 | _cleanup_close_ int fd = -1; | |
46 | ||
47 | assert(property); | |
48 | assert(value); | |
49 | ||
50 | log_debug("Setting '%s' to '%s'", property, value); | |
51 | ||
52 | p = strjoina("/proc/sys/", property); | |
53 | fd = open(p, O_WRONLY|O_CLOEXEC); | |
54 | if (fd < 0) | |
55 | return -errno; | |
56 | ||
57 | if (!endswith(value, "\n")) | |
58 | value = strjoina(value, "\n"); | |
59 | ||
60 | if (write(fd, value, strlen(value)) < 0) | |
61 | return -errno; | |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
66 | int sysctl_read(const char *property, char **content) { | |
67 | char *p; | |
68 | ||
69 | assert(property); | |
70 | assert(content); | |
71 | ||
72 | p = strjoina("/proc/sys/", property); | |
73 | return read_full_file(p, content, NULL); | |
74 | } |