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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
33 #define PROC_SYS_PREFIX "/proc/sys/"
35 static int apply_sysctl(const char *property, const char *value) {
39 log_debug("Setting '%s' to '%s'", property, value);
41 if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) {
42 log_error("Out of memory");
46 n = stpcpy(p, PROC_SYS_PREFIX);
53 if ((k = write_one_line_file(p, value)) < 0) {
55 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
56 "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
58 if (k != -ENOENT && r == 0)
67 static int apply_file(const char *path, bool ignore_enoent) {
73 if (!(f = fopen(path, "re"))) {
74 if (ignore_enoent && errno == ENOENT)
77 log_error("Failed to open file '%s', ignoring: %m", path);
81 log_debug("apply: %s\n", path);
83 char l[LINE_MAX], *p, *value;
86 if (!fgets(l, sizeof(l), f)) {
90 log_error("Failed to read file '%s', ignoring: %m", path);
100 if (strchr(COMMENTS, *p))
103 if (!(value = strchr(p, '='))) {
104 log_error("Line is not an assignment in file '%s': %s", path, value);
114 if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0)
124 int main(int argc, char *argv[]) {
128 log_error("This program expects one or no arguments.");
132 log_set_target(LOG_TARGET_AUTO);
133 log_parse_environment();
137 r = apply_file(argv[1], false);
142 r = conf_files_list(&files, ".conf",
145 "/usr/local/lib/sysctl.d",
150 log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
154 STRV_FOREACH(f, files) {
157 k = apply_file(*f, true);
162 apply_file("/etc/sysctl.conf", true);
167 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;