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 log_debug("Skipping previously assigned sysctl variable %s", property);
173 log_error("Failed to add sysctl variable %s to hashmap: %s", property, strerror(-r));
188 static int help(void) {
190 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
191 "Applies kernel sysctl settings.\n\n"
192 " -h --help Show this help\n"
193 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
194 program_invocation_short_name);
199 static int parse_argv(int argc, char *argv[]) {
205 static const struct option options[] = {
206 { "help", no_argument, NULL, 'h' },
207 { "prefix", required_argument, NULL, ARG_PREFIX },
216 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
228 for (p = optarg; *p; p++)
232 l = strv_append(arg_prefixes, optarg);
236 strv_free(arg_prefixes);
246 log_error("Unknown option code %c", c);
254 int main(int argc, char *argv[]) {
256 char *property, *value;
259 r = parse_argv(argc, argv);
261 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
263 log_set_target(LOG_TARGET_AUTO);
264 log_parse_environment();
269 sysctl_options = hashmap_new(string_hash_func, string_compare_func);
270 if (!sysctl_options) {
278 for (i = optind; i < argc; i++) {
281 k = parse_file(argv[i], false);
289 r = conf_files_list(&files, ".conf",
292 "/usr/local/lib/sysctl.d",
294 #ifdef HAVE_SPLIT_USR
299 log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
303 /* We parse the files in decreasing order of precedence.
304 * parse_file() will skip keys that were already assigned. */
306 r = parse_file("/etc/sysctl.conf", true);
308 f = files + strv_length(files) - 1;
309 STRV_FOREACH_BACKWARDS(f, files) {
310 k = parse_file(*f, true);
320 HASHMAP_FOREACH_KEY(value, property, sysctl_options, it) {
321 hashmap_remove(sysctl_options, property);
325 hashmap_free(sysctl_options);
327 strv_free(arg_prefixes);
329 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;