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(Hashmap
*sysctl_options
) {
45 char *property
, *value
;
49 HASHMAP_FOREACH_KEY(value
, property
, sysctl_options
, i
) {
52 k
= sysctl_write(property
, value
);
54 log_full_errno(k
== -ENOENT
? LOG_INFO
: LOG_WARNING
, k
,
55 "Couldn't write '%s' to '%s', ignoring: %m", value
, property
);
57 if (r
== 0 && k
!= -ENOENT
)
65 static int parse_file(Hashmap
*sysctl_options
, const char *path
, bool ignore_enoent
) {
66 _cleanup_fclose_
FILE *f
= NULL
;
71 r
= search_and_fopen_nulstr(path
, "re", NULL
, conf_file_dirs
, &f
);
73 if (ignore_enoent
&& r
== -ENOENT
)
76 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
79 log_debug("Parsing %s", path
);
81 char l
[LINE_MAX
], *p
, *value
, *new_value
, *property
, *existing
;
85 if (!fgets(l
, sizeof(l
), f
)) {
89 return log_error_errno(errno
, "Failed to read file '%s', ignoring: %m", path
);
96 if (strchr(COMMENTS
"\n", *p
))
99 value
= strchr(p
, '=');
101 log_error("Line is not an assignment in file '%s': %s", path
, value
);
111 p
= sysctl_normalize(strstrip(p
));
112 value
= strstrip(value
);
114 if (!strv_isempty(arg_prefixes
)) {
116 STRV_FOREACH(i
, arg_prefixes
) {
117 t
= path_startswith(*i
, "/proc/sys/");
120 if (path_startswith(p
, t
))
128 existing
= hashmap_get2(sysctl_options
, p
, &v
);
130 if (streq(value
, existing
))
133 log_debug("Overwriting earlier assignment of %s in file '%s'.", p
, path
);
134 free(hashmap_remove(sysctl_options
, p
));
138 property
= strdup(p
);
142 new_value
= strdup(value
);
148 k
= hashmap_put(sysctl_options
, property
, new_value
);
150 log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", property
);
160 static void help(void) {
161 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
162 "Applies kernel sysctl settings.\n\n"
163 " -h --help Show this help\n"
164 " --version Show package version\n"
165 " --prefix=PATH Only apply rules with the specified prefix\n"
166 , program_invocation_short_name
);
169 static int parse_argv(int argc
, char *argv
[]) {
176 static const struct option options
[] = {
177 { "help", no_argument
, NULL
, 'h' },
178 { "version", no_argument
, NULL
, ARG_VERSION
},
179 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
188 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
202 /* We used to require people to specify absolute paths
203 * in /proc/sys in the past. This is kinda useless, but
204 * we need to keep compatibility. We now support any
205 * sysctl name available. */
206 sysctl_normalize(optarg
);
208 if (startswith(optarg
, "/proc/sys"))
211 p
= strappend("/proc/sys/", optarg
);
215 if (strv_consume(&arg_prefixes
, p
) < 0)
225 assert_not_reached("Unhandled option");
231 int main(int argc
, char *argv
[]) {
233 Hashmap
*sysctl_options
;
235 r
= parse_argv(argc
, argv
);
237 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
239 log_set_target(LOG_TARGET_AUTO
);
240 log_parse_environment();
245 sysctl_options
= hashmap_new(&string_hash_ops
);
246 if (!sysctl_options
) {
256 for (i
= optind
; i
< argc
; i
++) {
257 k
= parse_file(sysctl_options
, argv
[i
], false);
262 _cleanup_strv_free_
char **files
= NULL
;
265 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, conf_file_dirs
);
267 log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
271 STRV_FOREACH(f
, files
) {
272 k
= parse_file(sysctl_options
, *f
, true);
278 k
= apply_all(sysctl_options
);
283 hashmap_free_free_free(sysctl_options
);
284 strv_free(arg_prefixes
);
286 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;