]>
Commit | Line | Data |
---|---|---|
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 KS |
31 | #include "log.h" |
32 | #include "util.h" | |
88a60da0 KS |
33 | #include "sysctl-util.h" |
34 | ||
35 | char *sysctl_normalize(char *s) { | |
36 | char *n; | |
37 | ||
38 | n = strpbrk(s, "/."); | |
39 | /* If the first separator is a slash, the path is | |
40 | * assumed to be normalized and slashes remain slashes | |
41 | * and dots remains dots. */ | |
42 | if (!n || *n == '/') | |
43 | return s; | |
44 | ||
45 | /* Otherwise, dots become slashes and slashes become | |
46 | * dots. Fun. */ | |
47 | while (n) { | |
48 | if (*n == '.') | |
49 | *n = '/'; | |
50 | else | |
51 | *n = '.'; | |
52 | ||
53 | n = strpbrk(n + 1, "/."); | |
54 | } | |
55 | ||
56 | return s; | |
57 | } | |
58 | ||
59 | int sysctl_write(const char *property, const char *value) { | |
0f5a8be5 | 60 | char *p; |
88a60da0 | 61 | |
0f5a8be5 LP |
62 | assert(property); |
63 | assert(value); | |
88a60da0 | 64 | |
0f5a8be5 | 65 | log_debug("Setting '%s' to '%s'", property, value); |
88a60da0 | 66 | |
0f5a8be5 | 67 | p = strjoina("/proc/sys/", property); |
ad118bda | 68 | return write_string_file(p, value, 0); |
88a60da0 KS |
69 | } |
70 | ||
71 | int sysctl_read(const char *property, char **content) { | |
0f5a8be5 | 72 | char *p; |
88a60da0 | 73 | |
0f5a8be5 LP |
74 | assert(property); |
75 | assert(content); | |
88a60da0 | 76 | |
0f5a8be5 | 77 | p = strjoina("/proc/sys/", property); |
88a60da0 KS |
78 | return read_full_file(p, content, NULL); |
79 | } |