]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
88a60da0 KS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
88a60da0 KS |
6 | ***/ |
7 | ||
521251d2 MG |
8 | #include <errno.h> |
9 | #include <fcntl.h> | |
3f6fd1ba | 10 | #include <stdio.h> |
3f6fd1ba | 11 | #include <string.h> |
521251d2 | 12 | #include <unistd.h> |
88a60da0 | 13 | |
521251d2 | 14 | #include "fd-util.h" |
3f6fd1ba | 15 | #include "fileio.h" |
88a60da0 | 16 | #include "log.h" |
a8fbdf54 | 17 | #include "macro.h" |
07630cea | 18 | #include "string-util.h" |
88a60da0 KS |
19 | #include "sysctl-util.h" |
20 | ||
21 | char *sysctl_normalize(char *s) { | |
22 | char *n; | |
23 | ||
24 | n = strpbrk(s, "/."); | |
25 | /* If the first separator is a slash, the path is | |
26 | * assumed to be normalized and slashes remain slashes | |
27 | * and dots remains dots. */ | |
28 | if (!n || *n == '/') | |
29 | return s; | |
30 | ||
31 | /* Otherwise, dots become slashes and slashes become | |
32 | * dots. Fun. */ | |
33 | while (n) { | |
34 | if (*n == '.') | |
35 | *n = '/'; | |
36 | else | |
37 | *n = '.'; | |
38 | ||
39 | n = strpbrk(n + 1, "/."); | |
40 | } | |
41 | ||
42 | return s; | |
43 | } | |
44 | ||
45 | int sysctl_write(const char *property, const char *value) { | |
0f5a8be5 | 46 | char *p; |
521251d2 | 47 | _cleanup_close_ int fd = -1; |
88a60da0 | 48 | |
0f5a8be5 LP |
49 | assert(property); |
50 | assert(value); | |
88a60da0 | 51 | |
0f5a8be5 | 52 | log_debug("Setting '%s' to '%s'", property, value); |
88a60da0 | 53 | |
0f5a8be5 | 54 | p = strjoina("/proc/sys/", property); |
521251d2 MG |
55 | fd = open(p, O_WRONLY|O_CLOEXEC); |
56 | if (fd < 0) | |
57 | return -errno; | |
58 | ||
59 | if (!endswith(value, "\n")) | |
60 | value = strjoina(value, "\n"); | |
61 | ||
62 | if (write(fd, value, strlen(value)) < 0) | |
63 | return -errno; | |
64 | ||
65 | return 0; | |
88a60da0 KS |
66 | } |
67 | ||
68 | int sysctl_read(const char *property, char **content) { | |
0f5a8be5 | 69 | char *p; |
88a60da0 | 70 | |
0f5a8be5 LP |
71 | assert(property); |
72 | assert(content); | |
88a60da0 | 73 | |
0f5a8be5 | 74 | p = strjoina("/proc/sys/", property); |
88a60da0 KS |
75 | return read_full_file(p, content, NULL); |
76 | } |