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