1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/types.h>
12 #include "conf-files.h"
14 #include "errno-util.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, and proceed without
90 * failing. (EROFS is treated as a permission problem here, since that's how
91 * container managers usually protected their sysctls.) In all other cases log an
92 * error and make the tool fail. */
94 if (option
->ignore_failure
|| k
== -EROFS
|| ERRNO_IS_PRIVILEGE(k
))
95 log_debug_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", option
->value
, option
->key
);
96 else if (k
== -ENOENT
)
97 log_info_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", option
->value
, option
->key
);
99 log_error_errno(k
, "Couldn't write '%s' to '%s': %m", option
->value
, option
->key
);
109 static bool test_prefix(const char *p
) {
112 if (strv_isempty(arg_prefixes
))
115 STRV_FOREACH(i
, arg_prefixes
) {
118 t
= path_startswith(*i
, "/proc/sys/");
121 if (path_startswith(p
, t
))
128 static int parse_file(OrderedHashmap
**sysctl_options
, const char *path
, bool ignore_enoent
) {
129 _cleanup_fclose_
FILE *f
= NULL
;
135 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
);
137 if (ignore_enoent
&& r
== -ENOENT
)
140 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
143 log_debug("Parsing %s", path
);
145 _cleanup_(option_freep
) Option
*new_option
= NULL
;
146 _cleanup_free_
char *l
= NULL
;
152 k
= read_line(f
, LONG_LINE_MAX
, &l
);
156 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", path
);
164 if (strchr(COMMENTS
"\n", *p
))
167 value
= strchr(p
, '=');
169 log_syntax(NULL
, LOG_WARNING
, path
, c
, 0, "Line is not an assignment, ignoring: %s", p
);
179 ignore_failure
= p
[0] == '-';
183 p
= sysctl_normalize(p
);
184 value
= strstrip(value
);
189 if (ordered_hashmap_ensure_allocated(sysctl_options
, &option_hash_ops
) < 0)
192 existing
= ordered_hashmap_get(*sysctl_options
, p
);
194 if (streq(value
, existing
->value
)) {
195 existing
->ignore_failure
= existing
->ignore_failure
|| ignore_failure
;
199 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, path
, c
);
200 option_free(ordered_hashmap_remove(*sysctl_options
, p
));
203 new_option
= option_new(p
, value
, ignore_failure
);
207 k
= ordered_hashmap_put(*sysctl_options
, new_option
->key
, new_option
);
209 return log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", p
);
211 TAKE_PTR(new_option
);
217 static int help(void) {
218 _cleanup_free_
char *link
= NULL
;
221 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
225 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
226 "Applies kernel sysctl settings.\n\n"
227 " -h --help Show this help\n"
228 " --version Show package version\n"
229 " --cat-config Show configuration files\n"
230 " --prefix=PATH Only apply rules with the specified prefix\n"
231 " --no-pager Do not pipe output into a pager\n"
232 "\nSee the %s for details.\n"
233 , program_invocation_short_name
240 static int parse_argv(int argc
, char *argv
[]) {
249 static const struct option options
[] = {
250 { "help", no_argument
, NULL
, 'h' },
251 { "version", no_argument
, NULL
, ARG_VERSION
},
252 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
253 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
254 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
263 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
274 arg_cat_config
= true;
280 /* We used to require people to specify absolute paths
281 * in /proc/sys in the past. This is kinda useless, but
282 * we need to keep compatibility. We now support any
283 * sysctl name available. */
284 sysctl_normalize(optarg
);
286 if (path_startswith(optarg
, "/proc/sys"))
289 p
= path_join("/proc/sys", optarg
);
293 if (strv_consume(&arg_prefixes
, p
) < 0)
300 arg_pager_flags
|= PAGER_DISABLE
;
307 assert_not_reached("Unhandled option");
310 if (arg_cat_config
&& argc
> optind
)
311 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
312 "Positional arguments are not allowed with --cat-config");
317 static int run(int argc
, char *argv
[]) {
318 _cleanup_(ordered_hashmap_freep
) OrderedHashmap
*sysctl_options
= NULL
;
321 r
= parse_argv(argc
, argv
);
334 for (i
= optind
; i
< argc
; i
++) {
335 k
= parse_file(&sysctl_options
, argv
[i
], false);
340 _cleanup_strv_free_
char **files
= NULL
;
343 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
345 return log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
347 if (arg_cat_config
) {
348 (void) pager_open(arg_pager_flags
);
350 return cat_files(NULL
, files
, 0);
353 STRV_FOREACH(f
, files
) {
354 k
= parse_file(&sysctl_options
, *f
, true);
360 k
= apply_all(sysctl_options
);
367 DEFINE_MAIN_FUNCTION(run
);