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 exit_code = 0;
37 static void apply_sysctl(const char *property, const char *value) {
41 log_debug("Setting '%s' to '%s'", property, value);
43 if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) {
44 log_error("Out of memory");
49 n = stpcpy(p, PROC_SYS_PREFIX);
56 if ((r = write_one_line_file(p, value)) < 0) {
58 log_full(r == -ENOENT ? LOG_DEBUG : LOG_WARNING,
59 "Failed to write '%s' to '%s': %s", value, p, strerror(-r));
68 static void apply_file(const char *path) {
73 if (!(f = fopen(path, "re"))) {
74 log_error("Failed to open file '%s', ignoring: %m", path);
80 char l[LINE_MAX], *p, *value;
82 if (!fgets(l, sizeof(l), f)) {
86 log_error("Failed to read file '%s', ignoring: %m", path);
96 if (strchr(COMMENTS, *p))
99 if (!(value = strchr(p, '='))) {
100 log_error("Line is not an assignment in file '%s': %s", path, value);
108 apply_sysctl(strstrip(p), strstrip(value));
117 const struct stat *sb,
119 struct FTW *ftwbuf) {
124 if (ignore_file(fpath + ftwbuf->base))
127 if (!endswith(fpath, ".conf"))
134 int main(int argc, char *argv[]) {
137 log_error("This program expects one or no arguments.");
141 log_set_target(LOG_TARGET_AUTO);
142 log_parse_environment();
146 nftw(argv[1], nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
148 nftw("/etc/sysctl.conf", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
149 nftw("/etc/sysctl.d", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
152 return exit_code < 0 ? EXIT_FAILURE : EXIT_SUCCESS;