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