]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/sysctl-util.c
Merge pull request #1989 from keszybz/filetriggers-v2
[thirdparty/systemd.git] / src / shared / sysctl-util.c
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
22 #include <errno.h>
23 #include <getopt.h>
24 #include <limits.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "fileio.h"
31 #include "log.h"
32 #include "string-util.h"
33 #include "sysctl-util.h"
34 #include "util.h"
35
36 char *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
60 int sysctl_write(const char *property, const char *value) {
61 char *p;
62
63 assert(property);
64 assert(value);
65
66 log_debug("Setting '%s' to '%s'", property, value);
67
68 p = strjoina("/proc/sys/", property);
69 return write_string_file(p, value, 0);
70 }
71
72 int sysctl_read(const char *property, char **content) {
73 char *p;
74
75 assert(property);
76 assert(content);
77
78 p = strjoina("/proc/sys/", property);
79 return read_full_file(p, content, NULL);
80 }