Debian Codegraph @

src/basic/sysctl-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "af-list.h"
6 #include "alloc-util.h"
7 #include "fileio.h"
8 #include "hashmap.h"
9 #include "log.h"
10 #include "parse-util.h"
11 #include "path-util.h"
12 #include "socket-util.h"
13 #include "string-util.h"
14 #include "sysctl-util.h"
15
16 char* sysctl_normalize(char *s) {
17 char *n;
18
19 n = strpbrk(s, "/.");
20
21 /* If the first separator is a slash, the path is
22 * assumed to be normalized and slashes remain slashes
23 * and dots remains dots. */
24
25 if (n && *n == '.')
26 /* Dots become slashes and slashes become dots. Fun. */
27 do {
28 if (*n == '.')
29 *n = '/';
30 else
31 *n = '.';
32
33 n = strpbrk(n + 1, "/.");
34 } while (n);
35
36 path_simplify(s);
37
38 /* Kill the leading slash, but keep the first character of the string in the same place. */
39 if (s[0] == '/' && s[1] != 0)
40 memmove(s, s+1, strlen(s));
41
42 return s;
43 }
44
45 static int shadow_update(Hashmap **shadow, const char *property, const char *value) {
46 _cleanup_free_ char *k = NULL, *v = NULL, *cur_k = NULL, *cur_v = NULL;
47 int r;
48
49 assert(property);
50 assert(value);
51
52 if (!shadow)
53 return 0;
54
55 k = strdup(property);
56 if (!k)
57 return -ENOMEM;
58
59 v = strdup(value);
60 if (!v)
61 return -ENOMEM;
62
63 cur_v = hashmap_remove2(*shadow, k, (void**)&cur_k);
64
65 r = hashmap_ensure_put(shadow, &path_hash_ops_free_free, k, v);
66 if (r < 0) {
67 assert(r != -EEXIST);
68
69 return r;
70 }
71
72 TAKE_PTR(k);
73 TAKE_PTR(v);
74
75 return 0;
76 }
77
78 int sysctl_write_full(const char *property, const char *value, Hashmap **shadow) {
79 char *p;
80 int r;
81
82 assert(property);
83 assert(value);
84
85 p = strjoina("/proc/sys/", property);
86
87 path_simplify(p);
88 if (!path_is_normalized(p))
89 return -EINVAL;
90
91 log_debug("Setting '%s' to '%s'", p, value);
92
93 r = shadow_update(shadow, p, value);
94 if (r < 0)
95 return r;
96
97 return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL);
98 }
99
100 int sysctl_writef(const char *property, const char *format, ...) {
101 _cleanup_free_ char *v = NULL;
102 va_list ap;
103 int r;
104
105 va_start(ap, format);
106 r = vasprintf(&v, format, ap);
107 va_end(ap);
108
109 if (r < 0)
110 return -ENOMEM;
111
112 return sysctl_write(property, v);
113 }
114
115 static const char* af_to_sysctl_dir(int af) {
116 if (af == AF_MPLS)
117 return "mpls";
118
119 return af_to_ipv4_ipv6(af);
120 }
121
122 int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow) {
123 const char *p;
124
125 assert(property);
126 assert(value);
127
128 if (!IN_SET(af, AF_INET, AF_INET6, AF_MPLS))
129 return -EAFNOSUPPORT;
130
131 if (ifname) {
132 if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
133 return -EINVAL;
134
135 p = strjoina("net/", af_to_sysctl_dir(af), "/conf/", ifname, "/", property);
136 } else
137 p = strjoina("net/", af_to_sysctl_dir(af), "/", property);
138
139 return sysctl_write_full(p, value, shadow);
140 }
141
142 int sysctl_write_ip_property_boolean(int af, const char *ifname, const char *property, bool value, Hashmap **shadow) {
143 return sysctl_write_ip_property(af, ifname, property, one_zero(value), shadow);
144 }
145
146 int sysctl_write_ip_neighbor_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow) {
147 const char *p;
148
149 assert(property);
150 assert(value);
151 assert(ifname);
152
153 if (!IN_SET(af, AF_INET, AF_INET6))
154 return -EAFNOSUPPORT;
155
156 if (ifname) {
157 if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
158 return -EINVAL;
159 p = strjoina("net/", af_to_ipv4_ipv6(af), "/neigh/", ifname, "/", property);
160 } else
161 p = strjoina("net/", af_to_ipv4_ipv6(af), "/neigh/default/", property);
162
163 return sysctl_write_full(p, value, shadow);
164 }
165
166 int sysctl_write_ip_neighbor_property_uint32(int af, const char *ifname, const char *property, uint32_t value, Hashmap **shadow) {
167 char buf[DECIMAL_STR_MAX(uint32_t)];
168 xsprintf(buf, "%u", value);
169 return sysctl_write_ip_neighbor_property(af, ifname, property, buf, shadow);
170 }
171
172 int sysctl_read(const char *property, char **ret) {
173 char *p;
174 int r;
175
176 assert(property);
177
178 p = strjoina("/proc/sys/", property);
179
180 path_simplify(p);
181 if (!path_is_normalized(p)) /* Filter out attempts to write to /proc/sys/../../…, just in case */
182 return -EINVAL;
183
184 r = read_full_virtual_file(p, ret, NULL);
185 if (r < 0)
186 return r;
187 if (ret)
188 delete_trailing_chars(*ret, NEWLINE);
189
190 return r;
191 }
192
193 int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret) {
194 const char *p;
195
196 assert(property);
197
198 if (!IN_SET(af, AF_INET, AF_INET6, AF_MPLS))
199 return -EAFNOSUPPORT;
200
201 if (ifname) {
202 if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
203 return -EINVAL;
204
205 p = strjoina("net/", af_to_sysctl_dir(af), "/conf/", ifname, "/", property);
206 } else
207 p = strjoina("net/", af_to_sysctl_dir(af), "/", property);
208
209 return sysctl_read(p, ret);
210 }
211
212 int sysctl_read_ip_property_int(int af, const char *ifname, const char *property, int *ret) {
213 _cleanup_free_ char *s = NULL;
214 int r;
215
216 assert(ret);
217
218 r = sysctl_read_ip_property(af, ifname, property, &s);
219 if (r < 0)
220 return r;
221
222 return safe_atoi(s, ret);
223 }
224
225 int sysctl_read_ip_property_uint32(int af, const char *ifname, const char *property, uint32_t *ret) {
226 _cleanup_free_ char *s = NULL;
227 int r;
228
229 assert(ret);
230
231 r = sysctl_read_ip_property(af, ifname, property, &s);
232 if (r < 0)
233 return r;
234
235 return safe_atou32(s, ret);
236 }