]>
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" |
39f0d1d2 | 13 | #include "creds-util.h" |
a0f29c76 | 14 | #include "def.h" |
32458cc9 | 15 | #include "errno-util.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
a5c32cff | 17 | #include "fileio.h" |
e0f42479 | 18 | #include "glob-util.h" |
3f6fd1ba LP |
19 | #include "hashmap.h" |
20 | #include "log.h" | |
8eb42d90 | 21 | #include "main-func.h" |
dcd5c891 | 22 | #include "pager.h" |
3f6fd1ba | 23 | #include "path-util.h" |
294bf0c3 | 24 | #include "pretty-print.h" |
07630cea | 25 | #include "string-util.h" |
3f6fd1ba | 26 | #include "strv.h" |
88a60da0 | 27 | #include "sysctl-util.h" |
8e1bd70d | 28 | |
fabe5c0e | 29 | static char **arg_prefixes = NULL; |
3c51c626 | 30 | static bool arg_cat_config = false; |
e88748c1 | 31 | static bool arg_strict = false; |
0221d68a | 32 | static PagerFlags arg_pager_flags = 0; |
8e1bd70d | 33 | |
fd8bdbc7 LP |
34 | STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep); |
35 | ||
dec02d6e LP |
36 | typedef struct Option { |
37 | char *key; | |
38 | char *value; | |
39 | bool ignore_failure; | |
40 | } Option; | |
41 | ||
42 | static Option *option_free(Option *o) { | |
43 | if (!o) | |
44 | return NULL; | |
45 | ||
46 | free(o->key); | |
47 | free(o->value); | |
48 | ||
49 | return mfree(o); | |
50 | } | |
51 | ||
52 | DEFINE_TRIVIAL_CLEANUP_FUNC(Option*, option_free); | |
53 | DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(option_hash_ops, char, string_hash_func, string_compare_func, Option, option_free); | |
54 | ||
e0f42479 | 55 | static bool test_prefix(const char *p) { |
e0f42479 ZJS |
56 | if (strv_isempty(arg_prefixes)) |
57 | return true; | |
58 | ||
c01404fd | 59 | return path_startswith_strv(p, arg_prefixes); |
e0f42479 ZJS |
60 | } |
61 | ||
dec02d6e LP |
62 | static Option *option_new( |
63 | const char *key, | |
64 | const char *value, | |
65 | bool ignore_failure) { | |
66 | ||
67 | _cleanup_(option_freep) Option *o = NULL; | |
68 | ||
69 | assert(key); | |
dec02d6e LP |
70 | |
71 | o = new(Option, 1); | |
72 | if (!o) | |
73 | return NULL; | |
74 | ||
75 | *o = (Option) { | |
76 | .key = strdup(key), | |
e0f42479 | 77 | .value = value ? strdup(value) : NULL, |
dec02d6e LP |
78 | .ignore_failure = ignore_failure, |
79 | }; | |
80 | ||
e0f42479 ZJS |
81 | if (!o->key) |
82 | return NULL; | |
83 | if (value && !o->value) | |
dec02d6e LP |
84 | return NULL; |
85 | ||
86 | return TAKE_PTR(o); | |
87 | } | |
88 | ||
e0f42479 ZJS |
89 | static int sysctl_write_or_warn(const char *key, const char *value, bool ignore_failure) { |
90 | int r; | |
91 | ||
92 | r = sysctl_write(key, value); | |
93 | if (r < 0) { | |
e88748c1 QD |
94 | /* Proceed without failing if ignore_failure is true. |
95 | * If the sysctl is not available in the kernel or we are running with reduced privileges and | |
96 | * cannot write it, then log about the issue, and proceed without failing. Unless strict mode | |
97 | * (arg_strict = true) is enabled, in which case we should fail. (EROFS is treated as a | |
98 | * permission problem here, since that's how container managers usually protected their | |
99 | * sysctls.) | |
100 | * In all other cases log an error and make the tool fail. */ | |
101 | if (ignore_failure || (!arg_strict && (r == -EROFS || ERRNO_IS_PRIVILEGE(r)))) | |
e0f42479 | 102 | log_debug_errno(r, "Couldn't write '%s' to '%s', ignoring: %m", value, key); |
e88748c1 | 103 | else if (!arg_strict && r == -ENOENT) |
1805fbcf | 104 | log_warning_errno(r, "Couldn't write '%s' to '%s', ignoring: %m", value, key); |
e0f42479 ZJS |
105 | else |
106 | return log_error_errno(r, "Couldn't write '%s' to '%s': %m", value, key); | |
107 | } | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
7177ac45 YW |
112 | static int apply_glob_option(OrderedHashmap *sysctl_options, Option *option) { |
113 | _cleanup_strv_free_ char **paths = NULL; | |
114 | _cleanup_free_ char *pattern = NULL; | |
115 | int r, k; | |
fabe5c0e | 116 | |
7177ac45 YW |
117 | assert(sysctl_options); |
118 | assert(option); | |
86fc77c4 | 119 | |
7177ac45 YW |
120 | pattern = path_join("/proc/sys", option->key); |
121 | if (!pattern) | |
122 | return log_oom(); | |
e50b33be | 123 | |
7177ac45 YW |
124 | r = glob_extend(&paths, pattern, GLOB_NOCHECK); |
125 | if (r < 0) { | |
126 | if (r == -ENOENT) { | |
127 | log_debug("No match for glob: %s", option->key); | |
128 | return 0; | |
129 | } | |
130 | if (option->ignore_failure || ERRNO_IS_PRIVILEGE(r)) { | |
131 | log_debug_errno(r, "Failed to resolve glob '%s', ignoring: %m", option->key); | |
132 | return 0; | |
133 | } else | |
134 | return log_error_errno(r, "Couldn't resolve glob '%s': %m", option->key); | |
135 | } | |
136 | ||
137 | STRV_FOREACH(s, paths) { | |
138 | const char *key; | |
86fc77c4 | 139 | |
7177ac45 | 140 | assert_se(key = path_startswith(*s, "/proc/sys")); |
9c37b41c | 141 | |
7177ac45 YW |
142 | if (!test_prefix(key)) |
143 | continue; | |
9c37b41c | 144 | |
7177ac45 YW |
145 | if (ordered_hashmap_contains(sysctl_options, key)) { |
146 | log_debug("Not setting %s (explicit setting exists).", key); | |
147 | continue; | |
148 | } | |
9c37b41c | 149 | |
7177ac45 YW |
150 | k = sysctl_write_or_warn(key, option->value, option->ignore_failure); |
151 | if (k < 0 && r >= 0) | |
152 | r = k; | |
153 | } | |
e0f42479 | 154 | |
7177ac45 YW |
155 | return r; |
156 | } | |
e0f42479 | 157 | |
7177ac45 YW |
158 | static int apply_all(OrderedHashmap *sysctl_options) { |
159 | Option *option; | |
160 | int r = 0; | |
e0f42479 | 161 | |
7177ac45 YW |
162 | ORDERED_HASHMAP_FOREACH(option, sysctl_options) { |
163 | int k; | |
e0f42479 | 164 | |
7177ac45 YW |
165 | /* Ignore "negative match" options, they are there only to exclude stuff from globs. */ |
166 | if (!option->value) | |
167 | continue; | |
e0f42479 | 168 | |
7177ac45 YW |
169 | if (string_is_glob(option->key)) |
170 | k = apply_glob_option(sysctl_options, option); | |
171 | else | |
e0f42479 | 172 | k = sysctl_write_or_warn(option->key, option->value, option->ignore_failure); |
7177ac45 YW |
173 | if (k < 0 && r >= 0) |
174 | r = k; | |
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 | 252 | existing = ordered_hashmap_get(*sysctl_options, p); |
fabe5c0e | 253 | if (existing) { |
db99904b | 254 | if (streq_ptr(value, existing->value)) { |
dec02d6e | 255 | existing->ignore_failure = existing->ignore_failure || ignore_failure; |
04bf3c1a | 256 | continue; |
dec02d6e | 257 | } |
fabe5c0e | 258 | |
2708160c | 259 | log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, pp, c); |
b2ae4d9e | 260 | option_free(ordered_hashmap_remove(*sysctl_options, p)); |
86fc77c4 MS |
261 | } |
262 | ||
dec02d6e LP |
263 | new_option = option_new(p, value, ignore_failure); |
264 | if (!new_option) | |
fabe5c0e LP |
265 | return log_oom(); |
266 | ||
350ffa97 | 267 | k = ordered_hashmap_ensure_put(sysctl_options, &option_hash_ops, new_option->key, new_option); |
dec02d6e LP |
268 | if (k < 0) |
269 | return log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", p); | |
86fc77c4 | 270 | |
dec02d6e | 271 | TAKE_PTR(new_option); |
8e1bd70d LP |
272 | } |
273 | ||
c1b664d0 | 274 | return r; |
8e1bd70d LP |
275 | } |
276 | ||
39f0d1d2 LP |
277 | static int read_credential_lines(OrderedHashmap **sysctl_options) { |
278 | _cleanup_free_ char *j = NULL; | |
279 | const char *d; | |
280 | int r; | |
281 | ||
282 | r = get_credentials_dir(&d); | |
283 | if (r == -ENXIO) | |
284 | return 0; | |
285 | if (r < 0) | |
286 | return log_error_errno(r, "Failed to get credentials directory: %m"); | |
287 | ||
288 | j = path_join(d, "sysctl.extra"); | |
289 | if (!j) | |
290 | return log_oom(); | |
291 | ||
292 | (void) parse_file(sysctl_options, j, /* ignore_enoent= */ true); | |
293 | return 0; | |
294 | } | |
295 | ||
37ec0fdd LP |
296 | static int help(void) { |
297 | _cleanup_free_ char *link = NULL; | |
298 | int r; | |
299 | ||
300 | r = terminal_urlify_man("systemd-sysctl.service", "8", &link); | |
301 | if (r < 0) | |
302 | return log_oom(); | |
303 | ||
7a2a0b90 LP |
304 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
305 | "Applies kernel sysctl settings.\n\n" | |
306 | " -h --help Show this help\n" | |
eb9da376 | 307 | " --version Show package version\n" |
3c51c626 | 308 | " --cat-config Show configuration files\n" |
0e1f5792 | 309 | " --prefix=PATH Only apply rules with the specified prefix\n" |
dcd5c891 | 310 | " --no-pager Do not pipe output into a pager\n" |
bc556335 DDM |
311 | "\nSee the %s for details.\n", |
312 | program_invocation_short_name, | |
313 | link); | |
37ec0fdd LP |
314 | |
315 | return 0; | |
7a2a0b90 LP |
316 | } |
317 | ||
318 | static int parse_argv(int argc, char *argv[]) { | |
319 | ||
320 | enum { | |
eb9da376 | 321 | ARG_VERSION = 0x100, |
3c51c626 ZJS |
322 | ARG_CAT_CONFIG, |
323 | ARG_PREFIX, | |
dcd5c891 | 324 | ARG_NO_PAGER, |
e88748c1 | 325 | ARG_STRICT, |
7a2a0b90 LP |
326 | }; |
327 | ||
328 | static const struct option options[] = { | |
3c51c626 ZJS |
329 | { "help", no_argument, NULL, 'h' }, |
330 | { "version", no_argument, NULL, ARG_VERSION }, | |
331 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, | |
332 | { "prefix", required_argument, NULL, ARG_PREFIX }, | |
dcd5c891 | 333 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
e88748c1 | 334 | { "strict", no_argument, NULL, ARG_STRICT }, |
eb9da376 | 335 | {} |
7a2a0b90 LP |
336 | }; |
337 | ||
338 | int c; | |
339 | ||
340 | assert(argc >= 0); | |
341 | assert(argv); | |
342 | ||
601185b4 | 343 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
7a2a0b90 LP |
344 | |
345 | switch (c) { | |
346 | ||
347 | case 'h': | |
37ec0fdd | 348 | return help(); |
eb9da376 LP |
349 | |
350 | case ARG_VERSION: | |
3f6fd1ba | 351 | return version(); |
7a2a0b90 | 352 | |
3c51c626 ZJS |
353 | case ARG_CAT_CONFIG: |
354 | arg_cat_config = true; | |
355 | break; | |
356 | ||
7a2a0b90 | 357 | case ARG_PREFIX: { |
c01404fd | 358 | const char *s; |
7a2a0b90 LP |
359 | char *p; |
360 | ||
0e1f5792 DH |
361 | /* We used to require people to specify absolute paths |
362 | * in /proc/sys in the past. This is kinda useless, but | |
363 | * we need to keep compatibility. We now support any | |
364 | * sysctl name available. */ | |
88a60da0 | 365 | sysctl_normalize(optarg); |
e50b33be | 366 | |
c01404fd YW |
367 | s = path_startswith(optarg, "/proc/sys"); |
368 | p = strdup(s ?: optarg); | |
0e1f5792 DH |
369 | if (!p) |
370 | return log_oom(); | |
e50b33be | 371 | |
0e1f5792 | 372 | if (strv_consume(&arg_prefixes, p) < 0) |
0d0f0c50 | 373 | return log_oom(); |
f68c5a70 | 374 | |
7a2a0b90 LP |
375 | break; |
376 | } | |
377 | ||
dcd5c891 | 378 | case ARG_NO_PAGER: |
0221d68a | 379 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
380 | break; |
381 | ||
e88748c1 QD |
382 | case ARG_STRICT: |
383 | arg_strict = true; | |
384 | break; | |
385 | ||
7a2a0b90 LP |
386 | case '?': |
387 | return -EINVAL; | |
388 | ||
389 | default: | |
04499a70 | 390 | assert_not_reached(); |
7a2a0b90 | 391 | } |
7a2a0b90 | 392 | |
baaa35ad ZJS |
393 | if (arg_cat_config && argc > optind) |
394 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
395 | "Positional arguments are not allowed with --cat-config"); | |
3c51c626 | 396 | |
7a2a0b90 LP |
397 | return 1; |
398 | } | |
399 | ||
8eb42d90 | 400 | static int run(int argc, char *argv[]) { |
dec02d6e | 401 | _cleanup_(ordered_hashmap_freep) OrderedHashmap *sysctl_options = NULL; |
a34c79d0 | 402 | int r, k; |
8e1bd70d | 403 | |
7a2a0b90 LP |
404 | r = parse_argv(argc, argv); |
405 | if (r <= 0) | |
a34c79d0 | 406 | return r; |
7a2a0b90 | 407 | |
d2acb93d | 408 | log_setup(); |
8e1bd70d | 409 | |
4c12626c LP |
410 | umask(0022); |
411 | ||
de19ece7 LP |
412 | if (argc > optind) { |
413 | int i; | |
414 | ||
2de30233 LP |
415 | r = 0; |
416 | ||
de19ece7 | 417 | for (i = optind; i < argc; i++) { |
b2ae4d9e | 418 | k = parse_file(&sysctl_options, argv[i], false); |
fabe5c0e | 419 | if (k < 0 && r == 0) |
de19ece7 LP |
420 | r = k; |
421 | } | |
422 | } else { | |
fabe5c0e | 423 | _cleanup_strv_free_ char **files = NULL; |
c1b664d0 | 424 | |
a826d4f7 | 425 | r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d")); |
a34c79d0 ZJS |
426 | if (r < 0) |
427 | return log_error_errno(r, "Failed to enumerate sysctl.d files: %m"); | |
db1413d7 | 428 | |
3c51c626 | 429 | if (arg_cat_config) { |
384c2c32 | 430 | pager_open(arg_pager_flags); |
dcd5c891 | 431 | |
a34c79d0 | 432 | return cat_files(NULL, files, 0); |
3c51c626 ZJS |
433 | } |
434 | ||
fabe5c0e | 435 | STRV_FOREACH(f, files) { |
b2ae4d9e | 436 | k = parse_file(&sysctl_options, *f, true); |
fabe5c0e | 437 | if (k < 0 && r == 0) |
db1413d7 KS |
438 | r = k; |
439 | } | |
39f0d1d2 LP |
440 | |
441 | k = read_credential_lines(&sysctl_options); | |
442 | if (k < 0 && r == 0) | |
443 | r = k; | |
8e1bd70d | 444 | } |
86fc77c4 | 445 | |
fabe5c0e LP |
446 | k = apply_all(sysctl_options); |
447 | if (k < 0 && r == 0) | |
0187f62b LP |
448 | r = k; |
449 | ||
8eb42d90 | 450 | return r; |
8e1bd70d | 451 | } |
8eb42d90 LP |
452 | |
453 | DEFINE_MAIN_FUNCTION(run); |