]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/sysctl-util.c
Merge pull request #2056 from evverx/expose-soft-limits-on-the-bus
[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 <stdio.h>
23 #include <string.h>
24
25 #include "fileio.h"
26 #include "log.h"
27 #include "macro.h"
28 #include "string-util.h"
29 #include "sysctl-util.h"
30
31 char *sysctl_normalize(char *s) {
32 char *n;
33
34 n = strpbrk(s, "/.");
35 /* If the first separator is a slash, the path is
36 * assumed to be normalized and slashes remain slashes
37 * and dots remains dots. */
38 if (!n || *n == '/')
39 return s;
40
41 /* Otherwise, dots become slashes and slashes become
42 * dots. Fun. */
43 while (n) {
44 if (*n == '.')
45 *n = '/';
46 else
47 *n = '.';
48
49 n = strpbrk(n + 1, "/.");
50 }
51
52 return s;
53 }
54
55 int sysctl_write(const char *property, const char *value) {
56 char *p;
57
58 assert(property);
59 assert(value);
60
61 log_debug("Setting '%s' to '%s'", property, value);
62
63 p = strjoina("/proc/sys/", property);
64 return write_string_file(p, value, 0);
65 }
66
67 int sysctl_read(const char *property, char **content) {
68 char *p;
69
70 assert(property);
71 assert(content);
72
73 p = strjoina("/proc/sys/", property);
74 return read_full_file(p, content, NULL);
75 }