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