]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8e1bd70d | 2 | |
8e1bd70d | 3 | #include <errno.h> |
7a2a0b90 | 4 | #include <getopt.h> |
3f6fd1ba LP |
5 | #include <limits.h> |
6 | #include <stdbool.h> | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
ca78ad1d ZJS |
9 | #include <sys/stat.h> |
10 | #include <sys/types.h> | |
8e1bd70d | 11 | |
2c21044f | 12 | #include "conf-files.h" |
a0f29c76 | 13 | #include "def.h" |
32458cc9 | 14 | #include "errno-util.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
a5c32cff | 16 | #include "fileio.h" |
e0f42479 | 17 | #include "glob-util.h" |
3f6fd1ba LP |
18 | #include "hashmap.h" |
19 | #include "log.h" | |
8eb42d90 | 20 | #include "main-func.h" |
dcd5c891 | 21 | #include "pager.h" |
3f6fd1ba | 22 | #include "path-util.h" |
294bf0c3 | 23 | #include "pretty-print.h" |
07630cea | 24 | #include "string-util.h" |
3f6fd1ba | 25 | #include "strv.h" |
88a60da0 | 26 | #include "sysctl-util.h" |
8e1bd70d | 27 | |
fabe5c0e | 28 | static char **arg_prefixes = NULL; |
3c51c626 | 29 | static bool arg_cat_config = false; |
0221d68a | 30 | static PagerFlags arg_pager_flags = 0; |
8e1bd70d | 31 | |
fd8bdbc7 LP |
32 | STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep); |
33 | ||
dec02d6e LP |
34 | typedef struct Option { |
35 | char *key; | |
36 | char *value; | |
37 | bool ignore_failure; | |
38 | } Option; | |
39 | ||
40 | static Option *option_free(Option *o) { | |
41 | if (!o) | |
42 | return NULL; | |
43 | ||
44 | free(o->key); | |
45 | free(o->value); | |
46 | ||
47 | return mfree(o); | |
48 | } | |
49 | ||
50 | DEFINE_TRIVIAL_CLEANUP_FUNC(Option*, option_free); | |
51 | DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(option_hash_ops, char, string_hash_func, string_compare_func, Option, option_free); | |
52 | ||
e0f42479 | 53 | static bool test_prefix(const char *p) { |
e0f42479 ZJS |
54 | if (strv_isempty(arg_prefixes)) |
55 | return true; | |
56 | ||
57 | STRV_FOREACH(i, arg_prefixes) { | |
58 | const char *t; | |
59 | ||
60 | t = path_startswith(*i, "/proc/sys/"); | |
61 | if (!t) | |
62 | t = *i; | |
63 | ||
64 | if (path_startswith(p, t)) | |
65 | return true; | |
66 | } | |
67 | ||
68 | return false; | |
69 | } | |
70 | ||
dec02d6e LP |
71 | static Option *option_new( |
72 | const char *key, | |
73 | const char *value, | |
74 | bool ignore_failure) { | |
75 | ||
76 | _cleanup_(option_freep) Option *o = NULL; | |
77 | ||
78 | assert(key); | |
dec02d6e LP |
79 | |
80 | o = new(Option, 1); | |
81 | if (!o) | |
82 | return NULL; | |
83 | ||
84 | *o = (Option) { | |
85 | .key = strdup(key), | |
e0f42479 | 86 | .value = value ? strdup(value) : NULL, |
dec02d6e LP |
87 | .ignore_failure = ignore_failure, |
88 | }; | |
89 | ||
e0f42479 ZJS |
90 | if (!o->key) |
91 | return NULL; | |
92 | if (value && !o->value) | |
dec02d6e LP |
93 | return NULL; |
94 | ||
95 | return TAKE_PTR(o); | |
96 | } | |
97 | ||
e0f42479 ZJS |
98 | static int sysctl_write_or_warn(const char *key, const char *value, bool ignore_failure) { |
99 | int r; | |
100 | ||
101 | r = sysctl_write(key, value); | |
102 | if (r < 0) { | |
103 | /* If the sysctl is not available in the kernel or we are running with reduced privileges and | |
104 | * cannot write it, then log about the issue, and proceed without failing. (EROFS is treated | |
105 | * as a permission problem here, since that's how container managers usually protected their | |
106 | * sysctls.) In all other cases log an error and make the tool fail. */ | |
107 | if (ignore_failure || r == -EROFS || ERRNO_IS_PRIVILEGE(r)) | |
108 | log_debug_errno(r, "Couldn't write '%s' to '%s', ignoring: %m", value, key); | |
109 | else if (r == -ENOENT) | |
110 | log_info_errno(r, "Couldn't write '%s' to '%s', ignoring: %m", value, key); | |
111 | else | |
112 | return log_error_errno(r, "Couldn't write '%s' to '%s': %m", value, key); | |
113 | } | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
886cf982 | 118 | static int apply_all(OrderedHashmap *sysctl_options) { |
dec02d6e | 119 | Option *option; |
e50b33be | 120 | int r = 0; |
fabe5c0e | 121 | |
90e74a66 | 122 | ORDERED_HASHMAP_FOREACH(option, sysctl_options) { |
86fc77c4 MS |
123 | int k; |
124 | ||
e0f42479 ZJS |
125 | /* Ignore "negative match" options, they are there only to exclude stuff from globs. */ |
126 | if (!option->value) | |
127 | continue; | |
e50b33be | 128 | |
e0f42479 ZJS |
129 | if (string_is_glob(option->key)) { |
130 | _cleanup_strv_free_ char **paths = NULL; | |
131 | _cleanup_free_ char *pattern = NULL; | |
86fc77c4 | 132 | |
e0f42479 ZJS |
133 | pattern = path_join("/proc/sys", option->key); |
134 | if (!pattern) | |
135 | return log_oom(); | |
9c37b41c | 136 | |
544e146b | 137 | k = glob_extend(&paths, pattern, GLOB_NOCHECK); |
e0f42479 | 138 | if (k < 0) { |
c53ce14d | 139 | if (option->ignore_failure || ERRNO_IS_PRIVILEGE(k)) |
e0f42479 ZJS |
140 | log_debug_errno(k, "Failed to resolve glob '%s', ignoring: %m", |
141 | option->key); | |
142 | else { | |
143 | log_error_errno(k, "Couldn't resolve glob '%s': %m", | |
144 | option->key); | |
145 | if (r == 0) | |
146 | r = k; | |
147 | } | |
9c37b41c | 148 | |
e0f42479 ZJS |
149 | } else if (strv_isempty(paths)) |
150 | log_debug("No match for glob: %s", option->key); | |
9c37b41c | 151 | |
e0f42479 ZJS |
152 | STRV_FOREACH(s, paths) { |
153 | const char *key; | |
154 | ||
155 | assert_se(key = path_startswith(*s, "/proc/sys")); | |
156 | ||
157 | if (!test_prefix(key)) | |
158 | continue; | |
159 | ||
160 | if (ordered_hashmap_contains(sysctl_options, key)) { | |
42a033f7 | 161 | log_debug("Not setting %s (explicit setting exists).", key); |
e0f42479 ZJS |
162 | continue; |
163 | } | |
164 | ||
165 | k = sysctl_write_or_warn(key, option->value, option->ignore_failure); | |
166 | if (r == 0) | |
167 | r = k; | |
168 | } | |
169 | ||
170 | } else { | |
171 | k = sysctl_write_or_warn(option->key, option->value, option->ignore_failure); | |
172 | if (r == 0) | |
173 | r = k; | |
174 | } | |
9c37b41c LP |
175 | } |
176 | ||
e0f42479 | 177 | return r; |
9c37b41c LP |
178 | } |
179 | ||
b2ae4d9e | 180 | static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ignore_enoent) { |
fabe5c0e | 181 | _cleanup_fclose_ FILE *f = NULL; |
2708160c | 182 | _cleanup_free_ char *pp = NULL; |
98bf5011 | 183 | unsigned c = 0; |
fabe5c0e | 184 | int r; |
8e1bd70d LP |
185 | |
186 | assert(path); | |
187 | ||
2708160c | 188 | r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("sysctl.d"), &f, &pp); |
fabe5c0e | 189 | if (r < 0) { |
6f6fad96 | 190 | if (ignore_enoent && r == -ENOENT) |
c1b664d0 LP |
191 | return 0; |
192 | ||
8d3d7072 | 193 | return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path); |
8e1bd70d LP |
194 | } |
195 | ||
2708160c | 196 | log_debug("Parsing %s", pp); |
4f14f2bb | 197 | for (;;) { |
dec02d6e | 198 | _cleanup_(option_freep) Option *new_option = NULL; |
a668bfe8 | 199 | _cleanup_free_ char *l = NULL; |
e0f42479 | 200 | bool ignore_failure = false; |
dec02d6e LP |
201 | Option *existing; |
202 | char *p, *value; | |
fabe5c0e | 203 | int k; |
548f6937 | 204 | |
a668bfe8 TSH |
205 | k = read_line(f, LONG_LINE_MAX, &l); |
206 | if (k == 0) | |
207 | break; | |
a668bfe8 | 208 | if (k < 0) |
2708160c | 209 | return log_error_errno(k, "Failed to read file '%s', ignoring: %m", pp); |
8e1bd70d | 210 | |
98bf5011 LP |
211 | c++; |
212 | ||
8e1bd70d | 213 | p = strstrip(l); |
8e1bd70d | 214 | |
548f6937 LP |
215 | if (isempty(p)) |
216 | continue; | |
d3b6d0c2 | 217 | if (strchr(COMMENTS "\n", *p)) |
8e1bd70d LP |
218 | continue; |
219 | ||
86fc77c4 | 220 | value = strchr(p, '='); |
e0f42479 ZJS |
221 | if (value) { |
222 | if (p[0] == '-') { | |
223 | ignore_failure = true; | |
224 | p++; | |
225 | } | |
8e1bd70d | 226 | |
e0f42479 ZJS |
227 | *value = 0; |
228 | value++; | |
229 | value = strstrip(value); | |
8e1bd70d | 230 | |
e0f42479 ZJS |
231 | } else { |
232 | if (p[0] == '-') | |
233 | /* We have a "negative match" option. Let's continue with value==NULL. */ | |
234 | p++; | |
235 | else { | |
2708160c | 236 | log_syntax(NULL, LOG_WARNING, pp, c, 0, |
e0f42479 ZJS |
237 | "Line is not an assignment, ignoring: %s", p); |
238 | if (r == 0) | |
239 | r = -EINVAL; | |
240 | continue; | |
241 | } | |
242 | } | |
dec02d6e | 243 | |
e0f42479 | 244 | p = strstrip(p); |
dec02d6e | 245 | p = sysctl_normalize(p); |
fabe5c0e | 246 | |
e0f42479 ZJS |
247 | /* We can't filter out globs at this point, we'll need to do that later. */ |
248 | if (!string_is_glob(p) && | |
249 | !test_prefix(p)) | |
b99802f7 | 250 | continue; |
b99802f7 | 251 | |
b2ae4d9e ZJS |
252 | if (ordered_hashmap_ensure_allocated(sysctl_options, &option_hash_ops) < 0) |
253 | return log_oom(); | |
254 | ||
255 | existing = ordered_hashmap_get(*sysctl_options, p); | |
fabe5c0e | 256 | if (existing) { |
db99904b | 257 | if (streq_ptr(value, existing->value)) { |
dec02d6e | 258 | existing->ignore_failure = existing->ignore_failure || ignore_failure; |
04bf3c1a | 259 | continue; |
dec02d6e | 260 | } |
fabe5c0e | 261 | |
2708160c | 262 | log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, pp, c); |
b2ae4d9e | 263 | option_free(ordered_hashmap_remove(*sysctl_options, p)); |
86fc77c4 MS |
264 | } |
265 | ||
dec02d6e LP |
266 | new_option = option_new(p, value, ignore_failure); |
267 | if (!new_option) | |
fabe5c0e LP |
268 | return log_oom(); |
269 | ||
b2ae4d9e | 270 | k = ordered_hashmap_put(*sysctl_options, new_option->key, new_option); |
dec02d6e LP |
271 | if (k < 0) |
272 | return log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", p); | |
86fc77c4 | 273 | |
dec02d6e | 274 | TAKE_PTR(new_option); |
8e1bd70d LP |
275 | } |
276 | ||
c1b664d0 | 277 | return r; |
8e1bd70d LP |
278 | } |
279 | ||
37ec0fdd LP |
280 | static int help(void) { |
281 | _cleanup_free_ char *link = NULL; | |
282 | int r; | |
283 | ||
284 | r = terminal_urlify_man("systemd-sysctl.service", "8", &link); | |
285 | if (r < 0) | |
286 | return log_oom(); | |
287 | ||
7a2a0b90 LP |
288 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
289 | "Applies kernel sysctl settings.\n\n" | |
290 | " -h --help Show this help\n" | |
eb9da376 | 291 | " --version Show package version\n" |
3c51c626 | 292 | " --cat-config Show configuration files\n" |
0e1f5792 | 293 | " --prefix=PATH Only apply rules with the specified prefix\n" |
dcd5c891 | 294 | " --no-pager Do not pipe output into a pager\n" |
bc556335 DDM |
295 | "\nSee the %s for details.\n", |
296 | program_invocation_short_name, | |
297 | link); | |
37ec0fdd LP |
298 | |
299 | return 0; | |
7a2a0b90 LP |
300 | } |
301 | ||
302 | static int parse_argv(int argc, char *argv[]) { | |
303 | ||
304 | enum { | |
eb9da376 | 305 | ARG_VERSION = 0x100, |
3c51c626 ZJS |
306 | ARG_CAT_CONFIG, |
307 | ARG_PREFIX, | |
dcd5c891 | 308 | ARG_NO_PAGER, |
7a2a0b90 LP |
309 | }; |
310 | ||
311 | static const struct option options[] = { | |
3c51c626 ZJS |
312 | { "help", no_argument, NULL, 'h' }, |
313 | { "version", no_argument, NULL, ARG_VERSION }, | |
314 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, | |
315 | { "prefix", required_argument, NULL, ARG_PREFIX }, | |
dcd5c891 | 316 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
eb9da376 | 317 | {} |
7a2a0b90 LP |
318 | }; |
319 | ||
320 | int c; | |
321 | ||
322 | assert(argc >= 0); | |
323 | assert(argv); | |
324 | ||
601185b4 | 325 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
7a2a0b90 LP |
326 | |
327 | switch (c) { | |
328 | ||
329 | case 'h': | |
37ec0fdd | 330 | return help(); |
eb9da376 LP |
331 | |
332 | case ARG_VERSION: | |
3f6fd1ba | 333 | return version(); |
7a2a0b90 | 334 | |
3c51c626 ZJS |
335 | case ARG_CAT_CONFIG: |
336 | arg_cat_config = true; | |
337 | break; | |
338 | ||
7a2a0b90 LP |
339 | case ARG_PREFIX: { |
340 | char *p; | |
341 | ||
0e1f5792 DH |
342 | /* We used to require people to specify absolute paths |
343 | * in /proc/sys in the past. This is kinda useless, but | |
344 | * we need to keep compatibility. We now support any | |
345 | * sysctl name available. */ | |
88a60da0 | 346 | sysctl_normalize(optarg); |
e50b33be | 347 | |
27458ed6 | 348 | if (path_startswith(optarg, "/proc/sys")) |
0e1f5792 DH |
349 | p = strdup(optarg); |
350 | else | |
b910cc72 | 351 | p = path_join("/proc/sys", optarg); |
0e1f5792 DH |
352 | if (!p) |
353 | return log_oom(); | |
e50b33be | 354 | |
0e1f5792 | 355 | if (strv_consume(&arg_prefixes, p) < 0) |
0d0f0c50 | 356 | return log_oom(); |
f68c5a70 | 357 | |
7a2a0b90 LP |
358 | break; |
359 | } | |
360 | ||
dcd5c891 | 361 | case ARG_NO_PAGER: |
0221d68a | 362 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
363 | break; |
364 | ||
7a2a0b90 LP |
365 | case '?': |
366 | return -EINVAL; | |
367 | ||
368 | default: | |
04499a70 | 369 | assert_not_reached(); |
7a2a0b90 | 370 | } |
7a2a0b90 | 371 | |
baaa35ad ZJS |
372 | if (arg_cat_config && argc > optind) |
373 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
374 | "Positional arguments are not allowed with --cat-config"); | |
3c51c626 | 375 | |
7a2a0b90 LP |
376 | return 1; |
377 | } | |
378 | ||
8eb42d90 | 379 | static int run(int argc, char *argv[]) { |
dec02d6e | 380 | _cleanup_(ordered_hashmap_freep) OrderedHashmap *sysctl_options = NULL; |
a34c79d0 | 381 | int r, k; |
8e1bd70d | 382 | |
7a2a0b90 LP |
383 | r = parse_argv(argc, argv); |
384 | if (r <= 0) | |
a34c79d0 | 385 | return r; |
7a2a0b90 | 386 | |
d2acb93d | 387 | log_setup(); |
8e1bd70d | 388 | |
4c12626c LP |
389 | umask(0022); |
390 | ||
de19ece7 LP |
391 | if (argc > optind) { |
392 | int i; | |
393 | ||
2de30233 LP |
394 | r = 0; |
395 | ||
de19ece7 | 396 | for (i = optind; i < argc; i++) { |
b2ae4d9e | 397 | k = parse_file(&sysctl_options, argv[i], false); |
fabe5c0e | 398 | if (k < 0 && r == 0) |
de19ece7 LP |
399 | r = k; |
400 | } | |
401 | } else { | |
fabe5c0e | 402 | _cleanup_strv_free_ char **files = NULL; |
c1b664d0 | 403 | |
a826d4f7 | 404 | r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d")); |
a34c79d0 ZJS |
405 | if (r < 0) |
406 | return log_error_errno(r, "Failed to enumerate sysctl.d files: %m"); | |
db1413d7 | 407 | |
3c51c626 | 408 | if (arg_cat_config) { |
384c2c32 | 409 | pager_open(arg_pager_flags); |
dcd5c891 | 410 | |
a34c79d0 | 411 | return cat_files(NULL, files, 0); |
3c51c626 ZJS |
412 | } |
413 | ||
fabe5c0e | 414 | STRV_FOREACH(f, files) { |
b2ae4d9e | 415 | k = parse_file(&sysctl_options, *f, true); |
fabe5c0e | 416 | if (k < 0 && r == 0) |
db1413d7 KS |
417 | r = k; |
418 | } | |
8e1bd70d | 419 | } |
86fc77c4 | 420 | |
fabe5c0e LP |
421 | k = apply_all(sysctl_options); |
422 | if (k < 0 && r == 0) | |
0187f62b LP |
423 | r = k; |
424 | ||
8eb42d90 | 425 | return r; |
8e1bd70d | 426 | } |
8eb42d90 LP |
427 | |
428 | DEFINE_MAIN_FUNCTION(run); |