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 PagerFlags arg_pager_flags
= 0;
33 STATIC_DESTRUCTOR_REGISTER(arg_prefixes
, strv_freep
);
35 typedef struct Option
{
41 static Option
*option_free(Option
*o
) {
51 DEFINE_TRIVIAL_CLEANUP_FUNC(Option
*, option_free
);
52 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(option_hash_ops
, char, string_hash_func
, string_compare_func
, Option
, option_free
);
54 static bool test_prefix(const char *p
) {
55 if (strv_isempty(arg_prefixes
))
58 STRV_FOREACH(i
, arg_prefixes
) {
61 t
= path_startswith(*i
, "/proc/sys/");
65 if (path_startswith(p
, t
))
72 static Option
*option_new(
75 bool ignore_failure
) {
77 _cleanup_(option_freep
) Option
*o
= NULL
;
87 .value
= value
? strdup(value
) : NULL
,
88 .ignore_failure
= ignore_failure
,
93 if (value
&& !o
->value
)
99 static int sysctl_write_or_warn(const char *key
, const char *value
, bool ignore_failure
) {
102 r
= sysctl_write(key
, value
);
104 /* If the sysctl is not available in the kernel or we are running with reduced privileges and
105 * cannot write it, then log about the issue, and proceed without failing. (EROFS is treated
106 * as a permission problem here, since that's how container managers usually protected their
107 * sysctls.) In all other cases log an error and make the tool fail. */
108 if (ignore_failure
|| r
== -EROFS
|| ERRNO_IS_PRIVILEGE(r
))
109 log_debug_errno(r
, "Couldn't write '%s' to '%s', ignoring: %m", value
, key
);
110 else if (r
== -ENOENT
)
111 log_warning_errno(r
, "Couldn't write '%s' to '%s', ignoring: %m", value
, key
);
113 return log_error_errno(r
, "Couldn't write '%s' to '%s': %m", value
, key
);
119 static int apply_all(OrderedHashmap
*sysctl_options
) {
123 ORDERED_HASHMAP_FOREACH(option
, sysctl_options
) {
126 /* Ignore "negative match" options, they are there only to exclude stuff from globs. */
130 if (string_is_glob(option
->key
)) {
131 _cleanup_strv_free_
char **paths
= NULL
;
132 _cleanup_free_
char *pattern
= NULL
;
134 pattern
= path_join("/proc/sys", option
->key
);
138 k
= glob_extend(&paths
, pattern
, GLOB_NOCHECK
);
140 if (option
->ignore_failure
|| ERRNO_IS_PRIVILEGE(k
))
141 log_debug_errno(k
, "Failed to resolve glob '%s', ignoring: %m",
144 log_error_errno(k
, "Couldn't resolve glob '%s': %m",
150 } else if (strv_isempty(paths
))
151 log_debug("No match for glob: %s", option
->key
);
153 STRV_FOREACH(s
, paths
) {
156 assert_se(key
= path_startswith(*s
, "/proc/sys"));
158 if (!test_prefix(key
))
161 if (ordered_hashmap_contains(sysctl_options
, key
)) {
162 log_debug("Not setting %s (explicit setting exists).", key
);
166 k
= sysctl_write_or_warn(key
, option
->value
, option
->ignore_failure
);
172 k
= sysctl_write_or_warn(option
->key
, option
->value
, option
->ignore_failure
);
181 static int parse_file(OrderedHashmap
**sysctl_options
, const char *path
, bool ignore_enoent
) {
182 _cleanup_fclose_
FILE *f
= NULL
;
183 _cleanup_free_
char *pp
= NULL
;
189 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
, &pp
);
191 if (ignore_enoent
&& r
== -ENOENT
)
194 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
197 log_debug("Parsing %s", pp
);
199 _cleanup_(option_freep
) Option
*new_option
= NULL
;
200 _cleanup_free_
char *l
= NULL
;
201 bool ignore_failure
= false;
206 k
= read_line(f
, LONG_LINE_MAX
, &l
);
210 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", pp
);
218 if (strchr(COMMENTS
"\n", *p
))
221 value
= strchr(p
, '=');
224 ignore_failure
= true;
230 value
= strstrip(value
);
234 /* We have a "negative match" option. Let's continue with value==NULL. */
237 log_syntax(NULL
, LOG_WARNING
, pp
, c
, 0,
238 "Line is not an assignment, ignoring: %s", p
);
246 p
= sysctl_normalize(p
);
248 /* We can't filter out globs at this point, we'll need to do that later. */
249 if (!string_is_glob(p
) &&
253 if (ordered_hashmap_ensure_allocated(sysctl_options
, &option_hash_ops
) < 0)
256 existing
= ordered_hashmap_get(*sysctl_options
, p
);
258 if (streq_ptr(value
, existing
->value
)) {
259 existing
->ignore_failure
= existing
->ignore_failure
|| ignore_failure
;
263 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, pp
, c
);
264 option_free(ordered_hashmap_remove(*sysctl_options
, p
));
267 new_option
= option_new(p
, value
, ignore_failure
);
271 k
= ordered_hashmap_put(*sysctl_options
, new_option
->key
, new_option
);
273 return log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", p
);
275 TAKE_PTR(new_option
);
281 static int read_credential_lines(OrderedHashmap
**sysctl_options
) {
282 _cleanup_free_
char *j
= NULL
;
286 r
= get_credentials_dir(&d
);
290 return log_error_errno(r
, "Failed to get credentials directory: %m");
292 j
= path_join(d
, "sysctl.extra");
296 (void) parse_file(sysctl_options
, j
, /* ignore_enoent= */ true);
300 static int help(void) {
301 _cleanup_free_
char *link
= NULL
;
304 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
308 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
309 "Applies kernel sysctl settings.\n\n"
310 " -h --help Show this help\n"
311 " --version Show package version\n"
312 " --cat-config Show configuration files\n"
313 " --prefix=PATH Only apply rules with the specified prefix\n"
314 " --no-pager Do not pipe output into a pager\n"
315 "\nSee the %s for details.\n",
316 program_invocation_short_name
,
322 static int parse_argv(int argc
, char *argv
[]) {
331 static const struct option options
[] = {
332 { "help", no_argument
, NULL
, 'h' },
333 { "version", no_argument
, NULL
, ARG_VERSION
},
334 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
335 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
336 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
345 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
356 arg_cat_config
= true;
362 /* We used to require people to specify absolute paths
363 * in /proc/sys in the past. This is kinda useless, but
364 * we need to keep compatibility. We now support any
365 * sysctl name available. */
366 sysctl_normalize(optarg
);
368 if (path_startswith(optarg
, "/proc/sys"))
371 p
= path_join("/proc/sys", optarg
);
375 if (strv_consume(&arg_prefixes
, p
) < 0)
382 arg_pager_flags
|= PAGER_DISABLE
;
389 assert_not_reached();
392 if (arg_cat_config
&& argc
> optind
)
393 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
394 "Positional arguments are not allowed with --cat-config");
399 static int run(int argc
, char *argv
[]) {
400 _cleanup_(ordered_hashmap_freep
) OrderedHashmap
*sysctl_options
= NULL
;
403 r
= parse_argv(argc
, argv
);
416 for (i
= optind
; i
< argc
; i
++) {
417 k
= parse_file(&sysctl_options
, argv
[i
], false);
422 _cleanup_strv_free_
char **files
= NULL
;
424 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
426 return log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
428 if (arg_cat_config
) {
429 pager_open(arg_pager_flags
);
431 return cat_files(NULL
, files
, 0);
434 STRV_FOREACH(f
, files
) {
435 k
= parse_file(&sysctl_options
, *f
, true);
440 k
= read_credential_lines(&sysctl_options
);
445 k
= apply_all(sysctl_options
);
452 DEFINE_MAIN_FUNCTION(run
);