]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/sysctl-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / sysctl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "fileio.h"
25 #include "log.h"
26 #include "macro.h"
27 #include "string-util.h"
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) {
55 char *p;
56
57 assert(property);
58 assert(value);
59
60 log_debug("Setting '%s' to '%s'", property, value);
61
62 p = strjoina("/proc/sys/", property);
63 return write_string_file(p, value, 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 }