1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
9 #include "conf-files.h"
10 #include "constants.h"
11 #include "creds-util.h"
12 #include "errno-util.h"
15 #include "glob-util.h"
18 #include "main-func.h"
20 #include "path-util.h"
21 #include "pretty-print.h"
22 #include "string-util.h"
24 #include "sysctl-util.h"
26 static char **arg_prefixes
= NULL
;
27 static CatFlags arg_cat_flags
= CAT_CONFIG_OFF
;
28 static bool arg_strict
= false;
29 static PagerFlags arg_pager_flags
= 0;
31 STATIC_DESTRUCTOR_REGISTER(arg_prefixes
, strv_freep
);
33 typedef struct Option
{
39 static Option
*option_free(Option
*o
) {
49 DEFINE_TRIVIAL_CLEANUP_FUNC(Option
*, option_free
);
50 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(option_hash_ops
, char, string_hash_func
, string_compare_func
, Option
, option_free
);
52 static bool test_prefix(const char *p
) {
53 if (strv_isempty(arg_prefixes
))
56 return path_startswith_strv(p
, arg_prefixes
);
59 static Option
*option_new(
62 bool ignore_failure
) {
64 _cleanup_(option_freep
) Option
*o
= NULL
;
74 .value
= value
? strdup(value
) : NULL
,
75 .ignore_failure
= ignore_failure
,
80 if (value
&& !o
->value
)
86 static int sysctl_write_or_warn(const char *key
, const char *value
, bool ignore_failure
, bool ignore_enoent
) {
89 r
= sysctl_write(key
, value
);
91 /* Proceed without failing if ignore_failure is true.
92 * If the sysctl is not available in the kernel or we are running with reduced privileges and
93 * cannot write it, then log about the issue, and proceed without failing. Unless strict mode
94 * (arg_strict = true) is enabled, in which case we should fail. (EROFS is treated as a
95 * permission problem here, since that's how container managers usually protected their
97 * In all other cases log an error and make the tool fail. */
98 if (ignore_failure
|| (!arg_strict
&& (r
== -EROFS
|| ERRNO_IS_PRIVILEGE(r
))))
99 log_debug_errno(r
, "Couldn't write '%s' to '%s', ignoring: %m", value
, key
);
100 else if (ignore_enoent
&& r
== -ENOENT
)
101 log_warning_errno(r
, "Couldn't write '%s' to '%s', ignoring: %m", value
, key
);
103 return log_error_errno(r
, "Couldn't write '%s' to '%s': %m", value
, key
);
109 static int apply_glob_option_with_prefix(OrderedHashmap
*sysctl_options
, Option
*option
, const char *prefix
) {
110 _cleanup_strv_free_
char **paths
= NULL
;
111 _cleanup_free_
char *pattern
= NULL
;
114 assert(sysctl_options
);
118 _cleanup_free_
char *key
= NULL
;
120 r
= path_glob_can_match(option
->key
, prefix
, &key
);
122 return log_error_errno(r
, "Failed to check if the glob '%s' matches prefix '%s': %m",
123 option
->key
, prefix
);
125 log_debug("The glob '%s' does not match prefix '%s'.", option
->key
, prefix
);
129 log_debug("The glob '%s' is prefixed with '%s': '%s'", option
->key
, prefix
, key
);
131 if (!string_is_glob(key
)) {
132 /* The prefixed pattern is not glob anymore. Let's skip to call glob(). */
133 if (ordered_hashmap_contains(sysctl_options
, key
)) {
134 log_debug("Not setting %s (explicit setting exists).", key
);
138 return sysctl_write_or_warn(key
, option
->value
,
139 /* ignore_failure = */ option
->ignore_failure
,
140 /* ignore_enoent = */ true);
143 pattern
= path_join("/proc/sys", key
);
145 pattern
= path_join("/proc/sys", option
->key
);
149 r
= glob_extend(&paths
, pattern
, GLOB_NOCHECK
);
152 log_debug("No match for glob: %s", option
->key
);
155 if (option
->ignore_failure
|| ERRNO_IS_PRIVILEGE(r
)) {
156 log_debug_errno(r
, "Failed to resolve glob '%s', ignoring: %m", option
->key
);
159 return log_error_errno(r
, "Couldn't resolve glob '%s': %m", option
->key
);
162 STRV_FOREACH(s
, paths
) {
165 assert_se(key
= path_startswith(*s
, "/proc/sys"));
167 if (ordered_hashmap_contains(sysctl_options
, key
)) {
168 log_debug("Not setting %s (explicit setting exists).", key
);
173 sysctl_write_or_warn(key
, option
->value
,
174 /* ignore_failure = */ option
->ignore_failure
,
175 /* ignore_enoent = */ !arg_strict
));
181 static int apply_glob_option(OrderedHashmap
*sysctl_options
, Option
*option
) {
184 if (strv_isempty(arg_prefixes
))
185 return apply_glob_option_with_prefix(sysctl_options
, option
, NULL
);
187 STRV_FOREACH(i
, arg_prefixes
)
188 RET_GATHER(r
, apply_glob_option_with_prefix(sysctl_options
, option
, *i
));
192 static int apply_all(OrderedHashmap
*sysctl_options
) {
196 ORDERED_HASHMAP_FOREACH(option
, sysctl_options
) {
199 /* Ignore "negative match" options, they are there only to exclude stuff from globs. */
203 if (string_is_glob(option
->key
))
204 k
= apply_glob_option(sysctl_options
, option
);
206 k
= sysctl_write_or_warn(option
->key
, option
->value
,
207 /* ignore_failure = */ option
->ignore_failure
,
208 /* ignore_enoent = */ !arg_strict
);
215 static int parse_file(OrderedHashmap
**sysctl_options
, const char *path
, bool ignore_enoent
) {
216 _cleanup_fclose_
FILE *f
= NULL
;
217 _cleanup_free_
char *pp
= NULL
;
223 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
, &pp
);
225 if (ignore_enoent
&& r
== -ENOENT
)
228 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
231 log_debug("Parsing %s", pp
);
233 _cleanup_(option_freep
) Option
*new_option
= NULL
;
234 _cleanup_free_
char *l
= NULL
;
235 bool ignore_failure
= false;
240 k
= read_stripped_line(f
, LONG_LINE_MAX
, &l
);
244 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", pp
);
250 if (strchr(COMMENTS
, l
[0]))
254 value
= strchr(p
, '=');
257 ignore_failure
= true;
263 value
= strstrip(value
);
267 /* We have a "negative match" option. Let's continue with value==NULL. */
270 log_syntax(NULL
, LOG_WARNING
, pp
, c
, 0,
271 "Line is not an assignment, ignoring: %s", p
);
279 p
= sysctl_normalize(p
);
281 /* We can't filter out globs at this point, we'll need to do that later. */
282 if (!string_is_glob(p
) &&
286 existing
= ordered_hashmap_get(*sysctl_options
, p
);
288 if (streq_ptr(value
, existing
->value
)) {
289 existing
->ignore_failure
= existing
->ignore_failure
|| ignore_failure
;
293 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, pp
, c
);
294 option_free(ordered_hashmap_remove(*sysctl_options
, p
));
297 new_option
= option_new(p
, value
, ignore_failure
);
301 k
= ordered_hashmap_ensure_put(sysctl_options
, &option_hash_ops
, new_option
->key
, new_option
);
303 return log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", p
);
305 TAKE_PTR(new_option
);
311 static int read_credential_lines(OrderedHashmap
**sysctl_options
) {
312 _cleanup_free_
char *j
= NULL
;
316 r
= get_credentials_dir(&d
);
320 return log_error_errno(r
, "Failed to get credentials directory: %m");
322 j
= path_join(d
, "sysctl.extra");
326 (void) parse_file(sysctl_options
, j
, /* ignore_enoent= */ true);
330 static int cat_config(char **files
) {
331 pager_open(arg_pager_flags
);
333 return cat_files(NULL
, files
, arg_cat_flags
);
336 static int help(void) {
337 _cleanup_free_
char *link
= NULL
;
340 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
344 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
345 "Applies kernel sysctl settings.\n\n"
346 " -h --help Show this help\n"
347 " --version Show package version\n"
348 " --cat-config Show configuration files\n"
349 " --tldr Show non-comment parts of configuration\n"
350 " --prefix=PATH Only apply rules with the specified prefix\n"
351 " --no-pager Do not pipe output into a pager\n"
352 "\nSee the %s for details.\n",
353 program_invocation_short_name
,
359 static int parse_argv(int argc
, char *argv
[]) {
370 static const struct option options
[] = {
371 { "help", no_argument
, NULL
, 'h' },
372 { "version", no_argument
, NULL
, ARG_VERSION
},
373 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
374 { "tldr", no_argument
, NULL
, ARG_TLDR
},
375 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
376 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
377 { "strict", no_argument
, NULL
, ARG_STRICT
},
386 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
397 arg_cat_flags
= CAT_CONFIG_ON
;
401 arg_cat_flags
= CAT_TLDR
;
408 /* We used to require people to specify absolute paths
409 * in /proc/sys in the past. This is kinda useless, but
410 * we need to keep compatibility. We now support any
411 * sysctl name available. */
412 sysctl_normalize(optarg
);
414 s
= path_startswith(optarg
, "/proc/sys");
415 p
= strdup(s
?: optarg
);
419 if (strv_consume(&arg_prefixes
, p
) < 0)
426 arg_pager_flags
|= PAGER_DISABLE
;
437 assert_not_reached();
440 if (arg_cat_flags
!= CAT_CONFIG_OFF
&& argc
> optind
)
441 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
442 "Positional arguments are not allowed with --cat-config/--tldr.");
447 static int run(int argc
, char *argv
[]) {
448 _cleanup_ordered_hashmap_free_ OrderedHashmap
*sysctl_options
= NULL
;
451 r
= parse_argv(argc
, argv
);
462 for (int i
= optind
; i
< argc
; i
++)
463 RET_GATHER(r
, parse_file(&sysctl_options
, argv
[i
], false));
466 _cleanup_strv_free_
char **files
= NULL
;
468 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
470 return log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
472 if (arg_cat_flags
!= CAT_CONFIG_OFF
)
473 return cat_config(files
);
475 STRV_FOREACH(f
, files
)
476 RET_GATHER(r
, parse_file(&sysctl_options
, *f
, true));
478 RET_GATHER(r
, read_credential_lines(&sysctl_options
));
481 RET_GATHER(r
, apply_all(sysctl_options
));
486 DEFINE_MAIN_FUNCTION(run
);