sysctl.c revision 44143309dd0b37d61d7d842ca58f01a65646ec71
220N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2362N/A
220N/A/***
220N/A This file is part of systemd.
220N/A
220N/A Copyright 2010 Lennart Poettering
220N/A
220N/A systemd is free software; you can redistribute it and/or modify it
220N/A under the terms of the GNU General Public License as published by
220N/A the Free Software Foundation; either version 2 of the License, or
220N/A (at your option) any later version.
220N/A
220N/A systemd is distributed in the hope that it will be useful, but
220N/A WITHOUT ANY WARRANTY; without even the implied warranty of
220N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
220N/A General Public License for more details.
220N/A
220N/A You should have received a copy of the GNU General Public License
2362N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
2362N/A***/
2362N/A
220N/A#include <stdlib.h>
220N/A#include <stdbool.h>
220N/A#include <errno.h>
220N/A#include <string.h>
220N/A#include <stdio.h>
220N/A#include <limits.h>
220N/A
220N/A#include "log.h"
220N/A#include "strv.h"
220N/A#include "util.h"
220N/A
220N/A#define PROC_SYS_PREFIX "/proc/sys/"
220N/A
220N/Astatic int apply_sysctl(const char *property, const char *value) {
220N/A char *p, *n;
220N/A int r = 0, k;
220N/A
220N/A log_debug("Setting '%s' to '%s'", property, value);
220N/A
220N/A if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) {
220N/A log_error("Out of memory");
220N/A return -ENOMEM;
220N/A }
220N/A
220N/A n = stpcpy(p, PROC_SYS_PREFIX);
220N/A strcpy(n, property);
220N/A
220N/A for (; *n; n++)
220N/A if (*n == '.')
220N/A *n = '/';
220N/A
220N/A if ((k = write_one_line_file(p, value)) < 0) {
220N/A
220N/A log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
220N/A "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
220N/A
220N/A if (k != -ENOENT && r == 0)
220N/A r = k;
220N/A }
220N/A
220N/A free(p);
220N/A
220N/A return r;
220N/A}
220N/A
220N/Astatic int apply_file(const char *path, bool ignore_enoent) {
220N/A FILE *f;
220N/A int r = 0;
220N/A
220N/A assert(path);
220N/A
220N/A if (!(f = fopen(path, "re"))) {
220N/A if (ignore_enoent && errno == ENOENT)
220N/A return 0;
220N/A
220N/A log_error("Failed to open file '%s', ignoring: %m", path);
220N/A return -errno;
220N/A }
220N/A
220N/A log_debug("apply: %s\n", path);
220N/A while (!feof(f)) {
220N/A char l[LINE_MAX], *p, *value;
220N/A int k;
220N/A
220N/A if (!fgets(l, sizeof(l), f)) {
220N/A if (feof(f))
220N/A break;
220N/A
220N/A log_error("Failed to read file '%s', ignoring: %m", path);
220N/A r = -errno;
220N/A goto finish;
220N/A }
220N/A
220N/A p = strstrip(l);
220N/A
220N/A if (!*p)
220N/A continue;
220N/A
220N/A if (strchr(COMMENTS, *p))
220N/A continue;
220N/A
220N/A if (!(value = strchr(p, '='))) {
220N/A log_error("Line is not an assignment in file '%s': %s", path, value);
220N/A
220N/A if (r == 0)
220N/A r = -EINVAL;
220N/A continue;
220N/A }
220N/A
220N/A *value = 0;
220N/A value++;
220N/A
220N/A if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0)
220N/A r = k;
220N/A }
220N/A
220N/Afinish:
220N/A fclose(f);
220N/A
220N/A return r;
220N/A}
220N/A
220N/Aint main(int argc, char *argv[]) {
220N/A int r = 0;
220N/A
220N/A if (argc > 2) {
220N/A log_error("This program expects one or no arguments.");
220N/A return EXIT_FAILURE;
220N/A }
220N/A
220N/A log_set_target(LOG_TARGET_AUTO);
220N/A log_parse_environment();
220N/A log_open();
220N/A
220N/A if (argc > 1)
220N/A r = apply_file(argv[1], false);
220N/A else {
220N/A char **files, **f;
220N/A
220N/A apply_file("/etc/sysctl.conf", true);
220N/A
220N/A r = conf_files_list(&files, ".conf",
220N/A "/run/sysctl.d",
220N/A "/etc/sysctl.d",
220N/A "/usr/lib/sysctl.d",
220N/A NULL);
220N/A if (r < 0) {
220N/A log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
220N/A goto finish;
220N/A }
220N/A
220N/A STRV_FOREACH(f, files) {
220N/A int k;
220N/A
220N/A k = apply_file(*f, true);
220N/A if (k < 0 && r == 0)
220N/A r = k;
220N/A }
220N/A
220N/A strv_free(files);
220N/A }
220N/Afinish:
220N/A return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
220N/A}
220N/A