1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/types.h>
12 #include "conf-files.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 bool arg_cat_config
= false;
28 static PagerFlags arg_pager_flags
= 0;
30 STATIC_DESTRUCTOR_REGISTER(arg_prefixes
, strv_freep
);
32 typedef struct Option
{
38 static Option
*option_free(Option
*o
) {
48 DEFINE_TRIVIAL_CLEANUP_FUNC(Option
*, option_free
);
49 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(option_hash_ops
, char, string_hash_func
, string_compare_func
, Option
, option_free
);
51 static Option
*option_new(
54 bool ignore_failure
) {
56 _cleanup_(option_freep
) Option
*o
= NULL
;
67 .value
= strdup(value
),
68 .ignore_failure
= ignore_failure
,
71 if (!o
->key
|| !o
->value
)
77 static int apply_all(OrderedHashmap
*sysctl_options
) {
82 ORDERED_HASHMAP_FOREACH(option
, sysctl_options
, i
) {
85 k
= sysctl_write(option
->key
, option
->value
);
87 /* If the sysctl is not available in the kernel or we are running with reduced
88 * privileges and cannot write it, then log about the issue at LOG_NOTICE level, and
89 * proceed without failing. (EROFS is treated as a permission problem here, since
90 * that's how container managers usually protected their sysctls.) In all other cases
91 * log an error and make the tool fail. */
93 if (IN_SET(k
, -EPERM
, -EACCES
, -EROFS
, -ENOENT
) || option
->ignore_failure
)
94 log_notice_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", option
->value
, option
->key
);
96 log_error_errno(k
, "Couldn't write '%s' to '%s': %m", option
->value
, option
->key
);
106 static bool test_prefix(const char *p
) {
109 if (strv_isempty(arg_prefixes
))
112 STRV_FOREACH(i
, arg_prefixes
) {
115 t
= path_startswith(*i
, "/proc/sys/");
118 if (path_startswith(p
, t
))
125 static int parse_file(OrderedHashmap
*sysctl_options
, const char *path
, bool ignore_enoent
) {
126 _cleanup_fclose_
FILE *f
= NULL
;
132 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
);
134 if (ignore_enoent
&& r
== -ENOENT
)
137 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
140 log_debug("Parsing %s", path
);
142 _cleanup_(option_freep
) Option
*new_option
= NULL
;
143 _cleanup_free_
char *l
= NULL
;
149 k
= read_line(f
, LONG_LINE_MAX
, &l
);
153 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", path
);
161 if (strchr(COMMENTS
"\n", *p
))
164 value
= strchr(p
, '=');
166 log_syntax(NULL
, LOG_WARNING
, path
, c
, 0, "Line is not an assignment, ignoring: %s", p
);
176 ignore_failure
= p
[0] == '-';
180 p
= sysctl_normalize(p
);
181 value
= strstrip(value
);
186 existing
= ordered_hashmap_get(sysctl_options
, p
);
188 if (streq(value
, existing
->value
)) {
189 existing
->ignore_failure
= existing
->ignore_failure
|| ignore_failure
;
193 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, path
, c
);
194 option_free(ordered_hashmap_remove(sysctl_options
, p
));
197 new_option
= option_new(p
, value
, ignore_failure
);
201 k
= ordered_hashmap_put(sysctl_options
, new_option
->key
, new_option
);
203 return log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", p
);
205 TAKE_PTR(new_option
);
211 static int help(void) {
212 _cleanup_free_
char *link
= NULL
;
215 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
219 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
220 "Applies kernel sysctl settings.\n\n"
221 " -h --help Show this help\n"
222 " --version Show package version\n"
223 " --cat-config Show configuration files\n"
224 " --prefix=PATH Only apply rules with the specified prefix\n"
225 " --no-pager Do not pipe output into a pager\n"
226 "\nSee the %s for details.\n"
227 , program_invocation_short_name
234 static int parse_argv(int argc
, char *argv
[]) {
243 static const struct option options
[] = {
244 { "help", no_argument
, NULL
, 'h' },
245 { "version", no_argument
, NULL
, ARG_VERSION
},
246 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
247 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
248 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
257 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
268 arg_cat_config
= true;
274 /* We used to require people to specify absolute paths
275 * in /proc/sys in the past. This is kinda useless, but
276 * we need to keep compatibility. We now support any
277 * sysctl name available. */
278 sysctl_normalize(optarg
);
280 if (path_startswith(optarg
, "/proc/sys"))
283 p
= path_join("/proc/sys", optarg
);
287 if (strv_consume(&arg_prefixes
, p
) < 0)
294 arg_pager_flags
|= PAGER_DISABLE
;
301 assert_not_reached("Unhandled option");
304 if (arg_cat_config
&& argc
> optind
)
305 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
306 "Positional arguments are not allowed with --cat-config");
311 static int run(int argc
, char *argv
[]) {
312 _cleanup_(ordered_hashmap_freep
) OrderedHashmap
*sysctl_options
= NULL
;
315 r
= parse_argv(argc
, argv
);
323 sysctl_options
= ordered_hashmap_new(&option_hash_ops
);
332 for (i
= optind
; i
< argc
; i
++) {
333 k
= parse_file(sysctl_options
, argv
[i
], false);
338 _cleanup_strv_free_
char **files
= NULL
;
341 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
343 return log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
345 if (arg_cat_config
) {
346 (void) pager_open(arg_pager_flags
);
348 return cat_files(NULL
, files
, 0);
351 STRV_FOREACH(f
, files
) {
352 k
= parse_file(sysctl_options
, *f
, true);
358 k
= apply_all(sysctl_options
);
365 DEFINE_MAIN_FUNCTION(run
);