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 #define PROC_SYS_PREFIX "/proc/sys/"
40 static char **arg_prefixes;
41 static Hashmap *sysctl_options;
43 static int apply_sysctl(const char *property, const char *value) {
47 log_debug("Setting '%s' to '%s'", property, value);
49 p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property));
53 n = stpcpy(p, PROC_SYS_PREFIX);
60 if (!strv_isempty(arg_prefixes)) {
64 STRV_FOREACH(i, arg_prefixes)
65 if (path_startswith(p, *i)) {
71 log_debug("Skipping %s", p);
77 k = write_one_line_file(p, value);
80 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
81 "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
83 if (k != -ENOENT && r == 0)
92 static int apply_all(void) {
94 char *property, *value;
97 HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) {
100 k = apply_sysctl(property, value);
107 static int parse_file(const char *path, bool ignore_enoent) {
113 f = fopen(path, "re");
115 if (ignore_enoent && errno == ENOENT)
118 log_error("Failed to open file '%s', ignoring: %m", path);
122 log_debug("parse: %s\n", path);
124 char l[LINE_MAX], *p, *value, *new_value, *property;
126 if (!fgets(l, sizeof(l), f)) {
130 log_error("Failed to read file '%s', ignoring: %m", path);
140 if (strchr(COMMENTS, *p))
143 value = strchr(p, '=');
145 log_error("Line is not an assignment in file '%s': %s", path, value);
155 property = strdup(strstrip(p));
161 new_value = strdup(strstrip(value));
168 r = hashmap_put(sysctl_options, property, new_value);
171 /* ignore this "error" to avoid returning it
172 * for the function when this is the last key
173 * in the file being parsed. */
175 log_debug("Skipping previously assigned sysctl variable %s", property);
177 log_error("Failed to add sysctl variable %s to hashmap: %s", property, strerror(-r));
192 static int help(void) {
194 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
195 "Applies kernel sysctl settings.\n\n"
196 " -h --help Show this help\n"
197 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
198 program_invocation_short_name);
203 static int parse_argv(int argc, char *argv[]) {
209 static const struct option options[] = {
210 { "help", no_argument, NULL, 'h' },
211 { "prefix", required_argument, NULL, ARG_PREFIX },
220 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
232 for (p = optarg; *p; p++)
236 l = strv_append(arg_prefixes, optarg);
240 strv_free(arg_prefixes);
250 log_error("Unknown option code %c", c);
258 int main(int argc, char *argv[]) {
260 char *property, *value;
263 r = parse_argv(argc, argv);
265 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
267 log_set_target(LOG_TARGET_AUTO);
268 log_parse_environment();
273 sysctl_options = hashmap_new(string_hash_func, string_compare_func);
274 if (!sysctl_options) {
284 for (i = optind; i < argc; i++) {
285 k = parse_file(argv[i], false);
292 r = conf_files_list(&files, ".conf",
295 "/usr/local/lib/sysctl.d",
297 #ifdef HAVE_SPLIT_USR
302 log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
306 /* We parse the files in decreasing order of precedence.
307 * parse_file() will skip keys that were already assigned. */
309 r = parse_file("/etc/sysctl.conf", true);
311 f = files + strv_length(files) - 1;
312 STRV_FOREACH_BACKWARDS(f, files) {
313 k = parse_file(*f, true);
326 HASHMAP_FOREACH_KEY(value, property, sysctl_options, it) {
327 hashmap_remove(sysctl_options, property);
331 hashmap_free(sysctl_options);
333 strv_free(arg_prefixes);
335 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;