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