1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
35 #include "path-util.h"
36 #include "conf-files.h"
38 static char **arg_prefixes = NULL;
40 static const char conf_file_dirs[] =
43 "/usr/local/lib/sysctl.d\0"
50 static char *normalize_sysctl(char *s) {
60 static int apply_sysctl(const char *property, const char *value) {
61 _cleanup_free_ char *p = NULL;
65 log_debug("Setting '%s' to '%s'", property, value);
67 p = new(char, sizeof("/proc/sys/") + strlen(property));
71 n = stpcpy(p, "/proc/sys/");
74 if (!strv_isempty(arg_prefixes)) {
78 STRV_FOREACH(i, arg_prefixes)
79 if (path_startswith(p, *i)) {
85 log_debug("Skipping %s", p);
90 k = write_one_line_file(p, value);
92 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
93 "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
95 if (k != -ENOENT && r == 0)
102 static int apply_all(Hashmap *sysctl_options) {
104 char *property, *value;
107 assert(sysctl_options);
109 HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) {
112 k = apply_sysctl(property, value);
119 static int parse_file(Hashmap *sysctl_options, const char *path, bool ignore_enoent) {
120 _cleanup_fclose_ FILE *f = NULL;
125 r = search_and_fopen_nulstr(path, "re", conf_file_dirs, &f);
127 if (ignore_enoent && errno == -ENOENT)
130 log_error("Failed to open file '%s', ignoring: %s", path, strerror(-r));
134 log_debug("parse: %s\n", path);
136 char l[LINE_MAX], *p, *value, *new_value, *property, *existing;
139 if (!fgets(l, sizeof(l), f)) {
143 log_error("Failed to read file '%s', ignoring: %m", path);
151 if (strchr(COMMENTS, *p))
154 value = strchr(p, '=');
156 log_error("Line is not an assignment in file '%s': %s", path, value);
166 p = normalize_sysctl(strstrip(p));
167 value = strstrip(value);
169 existing = hashmap_get(sysctl_options, p);
171 if (!streq(value, existing))
172 log_warning("Duplicate assignment of %s in file '%s', ignoring.",
178 property = strdup(p);
182 new_value = strdup(value);
188 k = hashmap_put(sysctl_options, property, new_value);
190 log_error("Failed to add sysctl variable %s to hashmap: %s", property, strerror(-r));
200 static int help(void) {
202 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
203 "Applies kernel sysctl settings.\n\n"
204 " -h --help Show this help\n"
205 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
206 program_invocation_short_name);
211 static int parse_argv(int argc, char *argv[]) {
217 static const struct option options[] = {
218 { "help", no_argument, NULL, 'h' },
219 { "prefix", required_argument, NULL, ARG_PREFIX },
228 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
240 for (p = optarg; *p; p++)
244 l = strv_append(arg_prefixes, optarg);
248 strv_free(arg_prefixes);
258 log_error("Unknown option code %c", c);
266 int main(int argc, char *argv[]) {
268 Hashmap *sysctl_options;
270 r = parse_argv(argc, argv);
272 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
274 log_set_target(LOG_TARGET_AUTO);
275 log_parse_environment();
280 sysctl_options = hashmap_new(string_hash_func, string_compare_func);
281 if (!sysctl_options) {
291 for (i = optind; i < argc; i++) {
292 k = parse_file(sysctl_options, argv[i], false);
297 _cleanup_strv_free_ char **files = NULL;
300 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
302 log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
306 r = parse_file(sysctl_options, "/etc/sysctl.conf", true);
308 STRV_FOREACH(f, files) {
309 k = parse_file(sysctl_options, *f, true);
315 k = apply_all(sysctl_options);
320 hashmap_free_free_free(sysctl_options);
321 strv_free(arg_prefixes);
323 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;