]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
88a60da0 KS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
3f6fd1ba | 21 | #include <stdio.h> |
3f6fd1ba | 22 | #include <string.h> |
88a60da0 | 23 | |
3f6fd1ba | 24 | #include "fileio.h" |
88a60da0 | 25 | #include "log.h" |
a8fbdf54 | 26 | #include "macro.h" |
07630cea | 27 | #include "string-util.h" |
88a60da0 KS |
28 | #include "sysctl-util.h" |
29 | ||
30 | char *sysctl_normalize(char *s) { | |
31 | char *n; | |
32 | ||
33 | n = strpbrk(s, "/."); | |
34 | /* If the first separator is a slash, the path is | |
35 | * assumed to be normalized and slashes remain slashes | |
36 | * and dots remains dots. */ | |
37 | if (!n || *n == '/') | |
38 | return s; | |
39 | ||
40 | /* Otherwise, dots become slashes and slashes become | |
41 | * dots. Fun. */ | |
42 | while (n) { | |
43 | if (*n == '.') | |
44 | *n = '/'; | |
45 | else | |
46 | *n = '.'; | |
47 | ||
48 | n = strpbrk(n + 1, "/."); | |
49 | } | |
50 | ||
51 | return s; | |
52 | } | |
53 | ||
54 | int sysctl_write(const char *property, const char *value) { | |
0f5a8be5 | 55 | char *p; |
88a60da0 | 56 | |
0f5a8be5 LP |
57 | assert(property); |
58 | assert(value); | |
88a60da0 | 59 | |
0f5a8be5 | 60 | log_debug("Setting '%s' to '%s'", property, value); |
88a60da0 | 61 | |
0f5a8be5 | 62 | p = strjoina("/proc/sys/", property); |
ad118bda | 63 | return write_string_file(p, value, 0); |
88a60da0 KS |
64 | } |
65 | ||
66 | int sysctl_read(const char *property, char **content) { | |
0f5a8be5 | 67 | char *p; |
88a60da0 | 68 | |
0f5a8be5 LP |
69 | assert(property); |
70 | assert(content); | |
88a60da0 | 71 | |
0f5a8be5 | 72 | p = strjoina("/proc/sys/", property); |
88a60da0 KS |
73 | return read_full_file(p, content, NULL); |
74 | } |