1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
14 #include "conf-files.h"
21 #include "path-util.h"
22 #include "string-util.h"
24 #include "sysctl-util.h"
25 #include "terminal-util.h"
28 static char **arg_prefixes
= NULL
;
29 static bool arg_cat_config
= false;
30 static bool arg_no_pager
= false;
32 static int apply_all(OrderedHashmap
*sysctl_options
) {
33 char *property
, *value
;
37 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, sysctl_options
, i
) {
40 k
= sysctl_write(property
, value
);
42 /* If the sysctl is not available in the kernel or we are running with reduced privileges and
43 * cannot write it, then log about the issue at LOG_NOTICE level, and proceed without
44 * failing. (EROFS is treated as a permission problem here, since that's how container managers
45 * usually protected their sysctls.) In all other cases log an error and make the tool fail. */
47 if (IN_SET(k
, -EPERM
, -EACCES
, -EROFS
, -ENOENT
))
48 log_notice_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", value
, property
);
50 log_error_errno(k
, "Couldn't write '%s' to '%s': %m", value
, property
);
60 static bool test_prefix(const char *p
) {
63 if (strv_isempty(arg_prefixes
))
66 STRV_FOREACH(i
, arg_prefixes
) {
69 t
= path_startswith(*i
, "/proc/sys/");
72 if (path_startswith(p
, t
))
79 static int parse_file(OrderedHashmap
*sysctl_options
, const char *path
, bool ignore_enoent
) {
80 _cleanup_fclose_
FILE *f
= NULL
;
86 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
);
88 if (ignore_enoent
&& r
== -ENOENT
)
91 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
94 log_debug("Parsing %s", path
);
96 char *p
, *value
, *new_value
, *property
, *existing
;
97 _cleanup_free_
char *l
= NULL
;
101 k
= read_line(f
, LONG_LINE_MAX
, &l
);
105 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", path
);
113 if (strchr(COMMENTS
"\n", *p
))
116 value
= strchr(p
, '=');
118 log_error("Line is not an assignment at '%s:%u': %s", path
, c
, value
);
128 p
= sysctl_normalize(strstrip(p
));
129 value
= strstrip(value
);
134 existing
= ordered_hashmap_get2(sysctl_options
, p
, &v
);
136 if (streq(value
, existing
))
139 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, path
, c
);
140 free(ordered_hashmap_remove(sysctl_options
, p
));
144 property
= strdup(p
);
148 new_value
= strdup(value
);
154 k
= ordered_hashmap_put(sysctl_options
, property
, new_value
);
156 log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", property
);
166 static void help(void) {
167 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
168 "Applies kernel sysctl settings.\n\n"
169 " -h --help Show this help\n"
170 " --version Show package version\n"
171 " --cat-config Show configuration files\n"
172 " --prefix=PATH Only apply rules with the specified prefix\n"
173 " --no-pager Do not pipe output into a pager\n"
174 , program_invocation_short_name
);
177 static int parse_argv(int argc
, char *argv
[]) {
186 static const struct option options
[] = {
187 { "help", no_argument
, NULL
, 'h' },
188 { "version", no_argument
, NULL
, ARG_VERSION
},
189 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
190 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
191 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
200 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
212 arg_cat_config
= true;
218 /* We used to require people to specify absolute paths
219 * in /proc/sys in the past. This is kinda useless, but
220 * we need to keep compatibility. We now support any
221 * sysctl name available. */
222 sysctl_normalize(optarg
);
224 if (path_startswith(optarg
, "/proc/sys"))
227 p
= strappend("/proc/sys/", optarg
);
231 if (strv_consume(&arg_prefixes
, p
) < 0)
245 assert_not_reached("Unhandled option");
248 if (arg_cat_config
&& argc
> optind
) {
249 log_error("Positional arguments are not allowed with --cat-config");
256 int main(int argc
, char *argv
[]) {
257 OrderedHashmap
*sysctl_options
= NULL
;
260 r
= parse_argv(argc
, argv
);
264 log_set_target(LOG_TARGET_AUTO
);
265 log_parse_environment();
270 sysctl_options
= ordered_hashmap_new(&path_hash_ops
);
271 if (!sysctl_options
) {
281 for (i
= optind
; i
< argc
; i
++) {
282 k
= parse_file(sysctl_options
, argv
[i
], false);
287 _cleanup_strv_free_
char **files
= NULL
;
290 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
292 log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
296 if (arg_cat_config
) {
297 (void) pager_open(arg_no_pager
, false);
299 r
= cat_files(NULL
, files
, 0);
303 STRV_FOREACH(f
, files
) {
304 k
= parse_file(sysctl_options
, *f
, true);
310 k
= apply_all(sysctl_options
);
317 ordered_hashmap_free_free_free(sysctl_options
);
318 strv_free(arg_prefixes
);
320 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;