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/>.
32 #define PROC_SYS_PREFIX "/proc/sys/"
34 static int apply_sysctl(const char *property, const char *value) {
38 log_debug("Setting '%s' to '%s'", property, value);
40 if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) {
41 log_error("Out of memory");
45 n = stpcpy(p, PROC_SYS_PREFIX);
52 if ((k = write_one_line_file(p, value)) < 0) {
54 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
55 "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
57 if (k != -ENOENT && r == 0)
66 static int apply_file(const char *path, bool ignore_enoent) {
72 if (!(f = fopen(path, "re"))) {
73 if (ignore_enoent && errno == ENOENT)
76 log_error("Failed to open file '%s', ignoring: %m", path);
81 char l[LINE_MAX], *p, *value;
84 if (!fgets(l, sizeof(l), f)) {
88 log_error("Failed to read file '%s', ignoring: %m", path);
98 if (strchr(COMMENTS, *p))
101 if (!(value = strchr(p, '='))) {
102 log_error("Line is not an assignment in file '%s': %s", path, value);
112 if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0)
122 static int scandir_filter(const struct dirent *d) {
125 if (ignore_file(d->d_name))
128 if (d->d_type != DT_REG &&
129 d->d_type != DT_LNK &&
130 d->d_type != DT_UNKNOWN)
133 return endswith(d->d_name, ".conf");
136 static int apply_tree(const char *path) {
137 struct dirent **de = NULL;
140 if ((n = scandir(path, &de, scandir_filter, alphasort)) < 0) {
145 log_error("Failed to enumerate %s files: %m", path);
149 for (i = 0; i < n; i++) {
153 k = asprintf(&fn, "%s/%s", path, de[i]->d_name);
157 log_error("Failed to allocate file name.");
164 if ((k = apply_file(fn, true)) < 0 && r == 0)
173 int main(int argc, char *argv[]) {
177 log_error("This program expects one or no arguments.");
181 log_set_target(LOG_TARGET_AUTO);
182 log_parse_environment();
186 r = apply_file(argv[1], false);
190 r = apply_file("/etc/sysctl.conf", true);
192 if ((k = apply_tree("/etc/sysctl.d")) < 0 && r == 0)
196 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;