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/>.
34 #include "conf-files.h"
36 #define PROC_SYS_PREFIX "/proc/sys/"
38 static char **arg_prefixes = NULL;
40 static int apply_sysctl(const char *property, const char *value) {
44 log_debug("Setting '%s' to '%s'", property, value);
46 p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property));
48 log_error("Out of memory");
52 n = stpcpy(p, PROC_SYS_PREFIX);
59 if (!strv_isempty(arg_prefixes)) {
63 STRV_FOREACH(i, arg_prefixes)
64 if (path_startswith(p, *i)) {
70 log_debug("Skipping %s", p);
76 k = write_one_line_file(p, value);
79 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
80 "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
82 if (k != -ENOENT && r == 0)
91 static int apply_file(const char *path, bool ignore_enoent) {
97 if (!(f = fopen(path, "re"))) {
98 if (ignore_enoent && errno == ENOENT)
101 log_error("Failed to open file '%s', ignoring: %m", path);
105 log_debug("apply: %s\n", path);
107 char l[LINE_MAX], *p, *value;
110 if (!fgets(l, sizeof(l), f)) {
114 log_error("Failed to read file '%s', ignoring: %m", path);
124 if (strchr(COMMENTS, *p))
127 if (!(value = strchr(p, '='))) {
128 log_error("Line is not an assignment in file '%s': %s", path, value);
138 if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0)
148 static int help(void) {
150 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
151 "Applies kernel sysctl settings.\n\n"
152 " -h --help Show this help\n"
153 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
154 program_invocation_short_name);
159 static int parse_argv(int argc, char *argv[]) {
165 static const struct option options[] = {
166 { "help", no_argument, NULL, 'h' },
167 { "prefix", required_argument, NULL, ARG_PREFIX },
176 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
188 for (p = optarg; *p; p++)
192 l = strv_append(arg_prefixes, optarg);
194 log_error("Out of memory");
198 strv_free(arg_prefixes);
208 log_error("Unknown option code %c", c);
216 int main(int argc, char *argv[]) {
219 r = parse_argv(argc, argv);
221 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
223 log_set_target(LOG_TARGET_AUTO);
224 log_parse_environment();
232 for (i = optind; i < argc; i++) {
235 k = apply_file(argv[i], false);
243 r = conf_files_list(&files, ".conf",
246 "/usr/local/lib/sysctl.d",
248 #ifdef HAVE_SPLIT_USR
253 log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
257 STRV_FOREACH(f, files) {
258 k = apply_file(*f, true);
263 k = apply_file("/etc/sysctl.conf", true);
270 strv_free(arg_prefixes);
272 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;