2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "conf-files.h"
34 #include "path-util.h"
35 #include "string-util.h"
37 #include "sysctl-util.h"
40 static char **arg_prefixes
= NULL
;
42 static const char conf_file_dirs
[] = CONF_PATHS_NULSTR("sysctl.d");
44 static int apply_all(OrderedHashmap
*sysctl_options
) {
45 char *property
, *value
;
49 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, sysctl_options
, i
) {
52 k
= sysctl_write(property
, value
);
54 /* If the sysctl is not available in the kernel or we are running with reduced privileges and
55 * cannot write it, then log about the issue at LOG_NOTICE level, and proceed without
56 * failing. (EROFS is treated as a permission problem here, since that's how container managers
57 * usually protected their sysctls.) In all other cases log an error and make the tool fail. */
59 if (IN_SET(k
, -EPERM
, -EACCES
, -EROFS
, -ENOENT
))
60 log_notice_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", value
, property
);
62 log_error_errno(k
, "Couldn't write '%s' to '%s': %m", value
, property
);
72 static bool test_prefix(const char *p
) {
75 if (strv_isempty(arg_prefixes
))
78 STRV_FOREACH(i
, arg_prefixes
) {
81 t
= path_startswith(*i
, "/proc/sys/");
84 if (path_startswith(p
, t
))
91 static int parse_file(OrderedHashmap
*sysctl_options
, const char *path
, bool ignore_enoent
) {
92 _cleanup_fclose_
FILE *f
= NULL
;
98 r
= search_and_fopen_nulstr(path
, "re", NULL
, conf_file_dirs
, &f
);
100 if (ignore_enoent
&& r
== -ENOENT
)
103 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
106 log_debug("Parsing %s", path
);
108 char l
[LINE_MAX
], *p
, *value
, *new_value
, *property
, *existing
;
112 if (!fgets(l
, sizeof(l
), f
)) {
116 return log_error_errno(errno
, "Failed to read file '%s', ignoring: %m", path
);
125 if (strchr(COMMENTS
"\n", *p
))
128 value
= strchr(p
, '=');
130 log_error("Line is not an assignment at '%s:%u': %s", path
, c
, value
);
140 p
= sysctl_normalize(strstrip(p
));
141 value
= strstrip(value
);
146 existing
= ordered_hashmap_get2(sysctl_options
, p
, &v
);
148 if (streq(value
, existing
))
151 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, path
, c
);
152 free(ordered_hashmap_remove(sysctl_options
, p
));
156 property
= strdup(p
);
160 new_value
= strdup(value
);
166 k
= ordered_hashmap_put(sysctl_options
, property
, new_value
);
168 log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", property
);
178 static void help(void) {
179 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
180 "Applies kernel sysctl settings.\n\n"
181 " -h --help Show this help\n"
182 " --version Show package version\n"
183 " --prefix=PATH Only apply rules with the specified prefix\n"
184 , program_invocation_short_name
);
187 static int parse_argv(int argc
, char *argv
[]) {
194 static const struct option options
[] = {
195 { "help", no_argument
, NULL
, 'h' },
196 { "version", no_argument
, NULL
, ARG_VERSION
},
197 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
206 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
220 /* We used to require people to specify absolute paths
221 * in /proc/sys in the past. This is kinda useless, but
222 * we need to keep compatibility. We now support any
223 * sysctl name available. */
224 sysctl_normalize(optarg
);
226 if (path_startswith(optarg
, "/proc/sys"))
229 p
= strappend("/proc/sys/", optarg
);
233 if (strv_consume(&arg_prefixes
, p
) < 0)
243 assert_not_reached("Unhandled option");
249 int main(int argc
, char *argv
[]) {
250 OrderedHashmap
*sysctl_options
= NULL
;
253 r
= parse_argv(argc
, argv
);
257 log_set_target(LOG_TARGET_AUTO
);
258 log_parse_environment();
263 sysctl_options
= ordered_hashmap_new(&string_hash_ops
);
264 if (!sysctl_options
) {
274 for (i
= optind
; i
< argc
; i
++) {
275 k
= parse_file(sysctl_options
, argv
[i
], false);
280 _cleanup_strv_free_
char **files
= NULL
;
283 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
285 log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
289 STRV_FOREACH(f
, files
) {
290 k
= parse_file(sysctl_options
, *f
, true);
296 k
= apply_all(sysctl_options
);
301 ordered_hashmap_free_free_free(sysctl_options
);
302 strv_free(arg_prefixes
);
304 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;