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");
48 n = stpcpy(p, PROC_SYS_PREFIX);
55 if ((r = write_one_line_file(p, value)) < 0) {
57 log_full(r == -ENOENT ? LOG_DEBUG : LOG_WARNING,
58 "Failed to write '%s' to '%s': %s", value, p, strerror(-r));
67 static void apply_file(const char *path) {
72 if (!(f = fopen(path, "re"))) {
73 log_error("Failed to open file '%s', ignoring: %m", path);
79 char l[LINE_MAX], *p, *value;
81 if (!fgets(l, sizeof(l), f)) {
85 log_error("Failed to read file '%s', ignoring: %m", path);
95 if (strchr(COMMENTS, *p))
98 if (!(value = strchr(p, '='))) {
99 log_error("Line is not an assignment in file '%s': %s", path, value);
107 apply_sysctl(strstrip(p), strstrip(value));
116 const struct stat *sb,
118 struct FTW *ftwbuf) {
123 if (ignore_file(fpath + ftwbuf->base))
126 if (!endswith(fpath, ".conf"))
133 int main(int argc, char *argv[]) {
136 log_error("This program expects one or no arguments.");
140 log_set_target(LOG_TARGET_AUTO);
141 log_parse_environment();
145 nftw(argv[1], nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
147 nftw("/etc/sysctl.conf", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
148 nftw("/etc/sysctl.d", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
151 return exit_code < 0 ? EXIT_FAILURE : EXIT_SUCCESS;