1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include <sys/types.h>
12 #include "conf-files.h"
13 #include "creds-util.h"
15 #include "errno-util.h"
18 #include "glob-util.h"
21 #include "main-func.h"
23 #include "path-util.h"
24 #include "pretty-print.h"
25 #include "string-util.h"
27 #include "sysctl-util.h"
29 static char **arg_prefixes
= NULL
;
30 static bool arg_cat_config
= false;
31 static bool arg_strict
= false;
32 static PagerFlags arg_pager_flags
= 0;
34 STATIC_DESTRUCTOR_REGISTER(arg_prefixes
, strv_freep
);
36 typedef struct Option
{
42 static Option
*option_free(Option
*o
) {
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
);
55 static bool test_prefix(const char *p
) {
56 if (strv_isempty(arg_prefixes
))
59 STRV_FOREACH(i
, arg_prefixes
) {
62 t
= path_startswith(*i
, "/proc/sys/");
66 if (path_startswith(p
, t
))
73 static Option
*option_new(
76 bool ignore_failure
) {
78 _cleanup_(option_freep
) Option
*o
= NULL
;
88 .value
= value
? strdup(value
) : NULL
,
89 .ignore_failure
= ignore_failure
,
94 if (value
&& !o
->value
)
100 static int sysctl_write_or_warn(const char *key
, const char *value
, bool ignore_failure
) {
103 r
= sysctl_write(key
, value
);
105 /* Proceed without failing if ignore_failure is true.
106 * If the sysctl is not available in the kernel or we are running with reduced privileges and
107 * cannot write it, then log about the issue, and proceed without failing. Unless strict mode
108 * (arg_strict = true) is enabled, in which case we should fail. (EROFS is treated as a
109 * permission problem here, since that's how container managers usually protected their
111 * In all other cases log an error and make the tool fail. */
112 if (ignore_failure
|| (!arg_strict
&& (r
== -EROFS
|| ERRNO_IS_PRIVILEGE(r
))))
113 log_debug_errno(r
, "Couldn't write '%s' to '%s', ignoring: %m", value
, key
);
114 else if (!arg_strict
&& r
== -ENOENT
)
115 log_warning_errno(r
, "Couldn't write '%s' to '%s', ignoring: %m", value
, key
);
117 return log_error_errno(r
, "Couldn't write '%s' to '%s': %m", value
, key
);
123 static int apply_all(OrderedHashmap
*sysctl_options
) {
127 ORDERED_HASHMAP_FOREACH(option
, sysctl_options
) {
130 /* Ignore "negative match" options, they are there only to exclude stuff from globs. */
134 if (string_is_glob(option
->key
)) {
135 _cleanup_strv_free_
char **paths
= NULL
;
136 _cleanup_free_
char *pattern
= NULL
;
138 pattern
= path_join("/proc/sys", option
->key
);
142 k
= glob_extend(&paths
, pattern
, GLOB_NOCHECK
);
144 if (option
->ignore_failure
|| ERRNO_IS_PRIVILEGE(k
))
145 log_debug_errno(k
, "Failed to resolve glob '%s', ignoring: %m",
148 log_error_errno(k
, "Couldn't resolve glob '%s': %m",
154 } else if (strv_isempty(paths
))
155 log_debug("No match for glob: %s", option
->key
);
157 STRV_FOREACH(s
, paths
) {
160 assert_se(key
= path_startswith(*s
, "/proc/sys"));
162 if (!test_prefix(key
))
165 if (ordered_hashmap_contains(sysctl_options
, key
)) {
166 log_debug("Not setting %s (explicit setting exists).", key
);
170 k
= sysctl_write_or_warn(key
, option
->value
, option
->ignore_failure
);
176 k
= sysctl_write_or_warn(option
->key
, option
->value
, option
->ignore_failure
);
185 static int parse_file(OrderedHashmap
**sysctl_options
, const char *path
, bool ignore_enoent
) {
186 _cleanup_fclose_
FILE *f
= NULL
;
187 _cleanup_free_
char *pp
= NULL
;
193 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
, &pp
);
195 if (ignore_enoent
&& r
== -ENOENT
)
198 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
201 log_debug("Parsing %s", pp
);
203 _cleanup_(option_freep
) Option
*new_option
= NULL
;
204 _cleanup_free_
char *l
= NULL
;
205 bool ignore_failure
= false;
210 k
= read_line(f
, LONG_LINE_MAX
, &l
);
214 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", pp
);
222 if (strchr(COMMENTS
"\n", *p
))
225 value
= strchr(p
, '=');
228 ignore_failure
= true;
234 value
= strstrip(value
);
238 /* We have a "negative match" option. Let's continue with value==NULL. */
241 log_syntax(NULL
, LOG_WARNING
, pp
, c
, 0,
242 "Line is not an assignment, ignoring: %s", p
);
250 p
= sysctl_normalize(p
);
252 /* We can't filter out globs at this point, we'll need to do that later. */
253 if (!string_is_glob(p
) &&
257 if (ordered_hashmap_ensure_allocated(sysctl_options
, &option_hash_ops
) < 0)
260 existing
= ordered_hashmap_get(*sysctl_options
, p
);
262 if (streq_ptr(value
, existing
->value
)) {
263 existing
->ignore_failure
= existing
->ignore_failure
|| ignore_failure
;
267 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, pp
, c
);
268 option_free(ordered_hashmap_remove(*sysctl_options
, p
));
271 new_option
= option_new(p
, value
, ignore_failure
);
275 k
= ordered_hashmap_put(*sysctl_options
, new_option
->key
, new_option
);
277 return log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", p
);
279 TAKE_PTR(new_option
);
285 static int read_credential_lines(OrderedHashmap
**sysctl_options
) {
286 _cleanup_free_
char *j
= NULL
;
290 r
= get_credentials_dir(&d
);
294 return log_error_errno(r
, "Failed to get credentials directory: %m");
296 j
= path_join(d
, "sysctl.extra");
300 (void) parse_file(sysctl_options
, j
, /* ignore_enoent= */ true);
304 static int help(void) {
305 _cleanup_free_
char *link
= NULL
;
308 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
312 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
313 "Applies kernel sysctl settings.\n\n"
314 " -h --help Show this help\n"
315 " --version Show package version\n"
316 " --cat-config Show configuration files\n"
317 " --prefix=PATH Only apply rules with the specified prefix\n"
318 " --no-pager Do not pipe output into a pager\n"
319 "\nSee the %s for details.\n",
320 program_invocation_short_name
,
326 static int parse_argv(int argc
, char *argv
[]) {
336 static const struct option options
[] = {
337 { "help", no_argument
, NULL
, 'h' },
338 { "version", no_argument
, NULL
, ARG_VERSION
},
339 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
340 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
341 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
342 { "strict", no_argument
, NULL
, ARG_STRICT
},
351 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
362 arg_cat_config
= true;
368 /* We used to require people to specify absolute paths
369 * in /proc/sys in the past. This is kinda useless, but
370 * we need to keep compatibility. We now support any
371 * sysctl name available. */
372 sysctl_normalize(optarg
);
374 if (path_startswith(optarg
, "/proc/sys"))
377 p
= path_join("/proc/sys", optarg
);
381 if (strv_consume(&arg_prefixes
, p
) < 0)
388 arg_pager_flags
|= PAGER_DISABLE
;
399 assert_not_reached();
402 if (arg_cat_config
&& argc
> optind
)
403 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
404 "Positional arguments are not allowed with --cat-config");
409 static int run(int argc
, char *argv
[]) {
410 _cleanup_(ordered_hashmap_freep
) OrderedHashmap
*sysctl_options
= NULL
;
413 r
= parse_argv(argc
, argv
);
426 for (i
= optind
; i
< argc
; i
++) {
427 k
= parse_file(&sysctl_options
, argv
[i
], false);
432 _cleanup_strv_free_
char **files
= NULL
;
434 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
436 return log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
438 if (arg_cat_config
) {
439 pager_open(arg_pager_flags
);
441 return cat_files(NULL
, files
, 0);
444 STRV_FOREACH(f
, files
) {
445 k
= parse_file(&sysctl_options
, *f
, true);
450 k
= read_credential_lines(&sysctl_options
);
455 k
= apply_all(sysctl_options
);
462 DEFINE_MAIN_FUNCTION(run
);