1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/types.h>
13 #include "conf-files.h"
19 #include "main-func.h"
21 #include "path-util.h"
22 #include "pretty-print.h"
23 #include "string-util.h"
25 #include "sysctl-util.h"
27 static char **arg_prefixes
= NULL
;
28 static bool arg_cat_config
= 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 Option
*option_new(
55 bool ignore_failure
) {
57 _cleanup_(option_freep
) Option
*o
= NULL
;
68 .value
= strdup(value
),
69 .ignore_failure
= ignore_failure
,
72 if (!o
->key
|| !o
->value
)
78 static int apply_all(OrderedHashmap
*sysctl_options
) {
83 ORDERED_HASHMAP_FOREACH(option
, sysctl_options
, i
) {
86 k
= sysctl_write(option
->key
, option
->value
);
88 /* If the sysctl is not available in the kernel or we are running with reduced
89 * privileges and cannot write it, then log about the issue at LOG_NOTICE level, and
90 * proceed without failing. (EROFS is treated as a permission problem here, since
91 * that's how container managers usually protected their sysctls.) In all other cases
92 * log an error and make the tool fail. */
94 if (IN_SET(k
, -EPERM
, -EACCES
, -EROFS
, -ENOENT
) || option
->ignore_failure
)
95 log_notice_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", option
->value
, option
->key
);
97 log_error_errno(k
, "Couldn't write '%s' to '%s': %m", option
->value
, option
->key
);
107 static bool test_prefix(const char *p
) {
110 if (strv_isempty(arg_prefixes
))
113 STRV_FOREACH(i
, arg_prefixes
) {
116 t
= path_startswith(*i
, "/proc/sys/");
119 if (path_startswith(p
, t
))
126 static int parse_file(OrderedHashmap
*sysctl_options
, const char *path
, bool ignore_enoent
) {
127 _cleanup_fclose_
FILE *f
= NULL
;
133 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
);
135 if (ignore_enoent
&& r
== -ENOENT
)
138 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
141 log_debug("Parsing %s", path
);
143 _cleanup_(option_freep
) Option
*new_option
= NULL
;
144 _cleanup_free_
char *l
= NULL
;
150 k
= read_line(f
, LONG_LINE_MAX
, &l
);
154 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", path
);
162 if (strchr(COMMENTS
"\n", *p
))
165 value
= strchr(p
, '=');
167 log_syntax(NULL
, LOG_WARNING
, path
, c
, 0, "Line is not an assignment, ignoring: %s", p
);
177 ignore_failure
= p
[0] == '-';
181 p
= sysctl_normalize(p
);
182 value
= strstrip(value
);
187 existing
= ordered_hashmap_get(sysctl_options
, p
);
189 if (streq(value
, existing
->value
)) {
190 existing
->ignore_failure
= existing
->ignore_failure
|| ignore_failure
;
194 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, path
, c
);
195 option_free(ordered_hashmap_remove(sysctl_options
, p
));
198 new_option
= option_new(p
, value
, ignore_failure
);
202 k
= ordered_hashmap_put(sysctl_options
, new_option
->key
, new_option
);
204 return log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", p
);
206 TAKE_PTR(new_option
);
212 static int help(void) {
213 _cleanup_free_
char *link
= NULL
;
216 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
220 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
221 "Applies kernel sysctl settings.\n\n"
222 " -h --help Show this help\n"
223 " --version Show package version\n"
224 " --cat-config Show configuration files\n"
225 " --prefix=PATH Only apply rules with the specified prefix\n"
226 " --no-pager Do not pipe output into a pager\n"
227 "\nSee the %s for details.\n"
228 , program_invocation_short_name
235 static int parse_argv(int argc
, char *argv
[]) {
244 static const struct option options
[] = {
245 { "help", no_argument
, NULL
, 'h' },
246 { "version", no_argument
, NULL
, ARG_VERSION
},
247 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
248 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
249 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
258 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
269 arg_cat_config
= true;
275 /* We used to require people to specify absolute paths
276 * in /proc/sys in the past. This is kinda useless, but
277 * we need to keep compatibility. We now support any
278 * sysctl name available. */
279 sysctl_normalize(optarg
);
281 if (path_startswith(optarg
, "/proc/sys"))
284 p
= path_join("/proc/sys", optarg
);
288 if (strv_consume(&arg_prefixes
, p
) < 0)
295 arg_pager_flags
|= PAGER_DISABLE
;
302 assert_not_reached("Unhandled option");
305 if (arg_cat_config
&& argc
> optind
)
306 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
307 "Positional arguments are not allowed with --cat-config");
312 static int run(int argc
, char *argv
[]) {
313 _cleanup_(ordered_hashmap_freep
) OrderedHashmap
*sysctl_options
= NULL
;
316 r
= parse_argv(argc
, argv
);
324 sysctl_options
= ordered_hashmap_new(&option_hash_ops
);
333 for (i
= optind
; i
< argc
; i
++) {
334 k
= parse_file(sysctl_options
, argv
[i
], false);
339 _cleanup_strv_free_
char **files
= NULL
;
342 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
344 return log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
346 if (arg_cat_config
) {
347 (void) pager_open(arg_pager_flags
);
349 return cat_files(NULL
, files
, 0);
352 STRV_FOREACH(f
, files
) {
353 k
= parse_file(sysctl_options
, *f
, true);
359 k
= apply_all(sysctl_options
);
366 DEFINE_MAIN_FUNCTION(run
);