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 "path-util.h"
35 #include "conf-files.h"
37 #define PROC_SYS_PREFIX "/proc/sys/"
39 static char **arg_prefixes = NULL;
41 static int apply_sysctl(const char *property, const char *value) {
45 log_debug("Setting '%s' to '%s'", property, value);
47 p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property));
49 log_error("Out of memory");
53 n = stpcpy(p, PROC_SYS_PREFIX);
60 if (!strv_isempty(arg_prefixes)) {
64 STRV_FOREACH(i, arg_prefixes)
65 if (path_startswith(p, *i)) {
71 log_debug("Skipping %s", p);
77 k = write_one_line_file(p, value);
80 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
81 "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
83 if (k != -ENOENT && r == 0)
92 static int apply_file(const char *path, bool ignore_enoent) {
98 if (!(f = fopen(path, "re"))) {
99 if (ignore_enoent && errno == ENOENT)
102 log_error("Failed to open file '%s', ignoring: %m", path);
106 log_debug("apply: %s\n", path);
108 char l[LINE_MAX], *p, *value;
111 if (!fgets(l, sizeof(l), f)) {
115 log_error("Failed to read file '%s', ignoring: %m", path);
125 if (strchr(COMMENTS, *p))
128 if (!(value = strchr(p, '='))) {
129 log_error("Line is not an assignment in file '%s': %s", path, value);
139 if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0)
149 static int help(void) {
151 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
152 "Applies kernel sysctl settings.\n\n"
153 " -h --help Show this help\n"
154 " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n",
155 program_invocation_short_name);
160 static int parse_argv(int argc, char *argv[]) {
166 static const struct option options[] = {
167 { "help", no_argument, NULL, 'h' },
168 { "prefix", required_argument, NULL, ARG_PREFIX },
177 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
189 for (p = optarg; *p; p++)
193 l = strv_append(arg_prefixes, optarg);
195 log_error("Out of memory");
199 strv_free(arg_prefixes);
209 log_error("Unknown option code %c", c);
217 int main(int argc, char *argv[]) {
220 r = parse_argv(argc, argv);
222 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
224 log_set_target(LOG_TARGET_AUTO);
225 log_parse_environment();
233 for (i = optind; i < argc; i++) {
236 k = apply_file(argv[i], false);
244 r = conf_files_list(&files, ".conf",
247 "/usr/local/lib/sysctl.d",
249 #ifdef HAVE_SPLIT_USR
254 log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
258 STRV_FOREACH(f, files) {
259 k = apply_file(*f, true);
264 k = apply_file("/etc/sysctl.conf", true);
271 strv_free(arg_prefixes);
273 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;