]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sysctl.c
sysctl: implement native sysctl tool to support Debian-style /etc/sysctl.d
[thirdparty/systemd.git] / src / sysctl.c
CommitLineData
8e1bd70d
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include <stdlib.h>
23#include <stdbool.h>
24#include <errno.h>
25#include <string.h>
26#include <ftw.h>
27#include <stdio.h>
28#include <limits.h>
29
30#include "log.h"
31#include "util.h"
32
33#define PROC_SYS_PREFIX "/proc/sys/"
34
35static int exit_code = 0;
36
37static void apply_sysctl(const char *property, const char *value) {
38 char *p, *n;
39 int r;
40
41 log_debug("Setting '%s' to '%s'", property, value);
42
43 if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) {
44 log_error("Out of memory");
45 exit_code = -ENOMEM;
46 }
47
48 n = stpcpy(p, PROC_SYS_PREFIX);
49 strcpy(n, property);
50
51 for (; *n; n++)
52 if (*n == '.')
53 *n = '/';
54
55 if ((r = write_one_line_file(p, value)) < 0) {
56 log_warning("Failed to write '%s' to '%s': %s", value, p, strerror(-r));
57 exit_code = r;
58 }
59
60 free(p);
61}
62
63static void apply_file(const char *path) {
64 FILE *f;
65
66 assert(path);
67
68 if (!(f = fopen(path, "re"))) {
69 log_error("Failed to open file '%s', ignoring: %m", path);
70 exit_code = -errno;
71 return;
72 }
73
74 while (!feof(f)) {
75 char l[LINE_MAX], *p, *value;
76
77 if (!fgets(l, sizeof(l), f)) {
78 if (feof(f))
79 break;
80
81 log_error("Failed to read file '%s', ignoring: %m", path);
82 exit_code = -errno;
83 goto finish;
84 }
85
86 p = strstrip(l);
87
88 if (!*p)
89 continue;
90
91 if (strchr(COMMENTS, *p))
92 continue;
93
94 if (!(value = strchr(p, '='))) {
95 log_error("Line is not an assignment in file '%s': %s", path, value);
96 exit_code = -EINVAL;
97 continue;
98 }
99
100 *value = 0;
101 value++;
102
103 apply_sysctl(strstrip(p), strstrip(value));
104 }
105
106finish:
107 fclose(f);
108}
109
110static int nftw_cb(
111 const char *fpath,
112 const struct stat *sb,
113 int tflag,
114 struct FTW *ftwbuf) {
115
116 if (tflag != FTW_F)
117 return 0;
118
119 if (ignore_file(fpath + ftwbuf->base))
120 return 0;
121
122 if (!endswith(fpath, ".conf"))
123 return 0;
124
125 apply_file(fpath);
126 return 0;
127};
128
129int main(int argc, char *argv[]) {
130
131 if (argc > 2) {
132 log_error("This program expects one or no arguments.");
133 return EXIT_FAILURE;
134 }
135
136 log_set_target(LOG_TARGET_AUTO);
137 log_parse_environment();
138 log_open();
139
140 if (argc > 1)
141 nftw(argv[1], nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
142 else {
143 nftw("/etc/sysctl.conf", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
144 nftw("/etc/sysctl.d", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
145 }
146
147 return exit_code < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
148}