]>
Commit | Line | Data |
---|---|---|
663996b3 MS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
663996b3 | 20 | #include <errno.h> |
663996b3 | 21 | #include <getopt.h> |
6300502b MP |
22 | #include <limits.h> |
23 | #include <stdbool.h> | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
663996b3 | 27 | |
663996b3 | 28 | #include "conf-files.h" |
db2df898 MP |
29 | #include "def.h" |
30 | #include "fd-util.h" | |
663996b3 | 31 | #include "fileio.h" |
6300502b MP |
32 | #include "hashmap.h" |
33 | #include "log.h" | |
34 | #include "path-util.h" | |
db2df898 | 35 | #include "string-util.h" |
6300502b | 36 | #include "strv.h" |
e3bff60a | 37 | #include "sysctl-util.h" |
6300502b | 38 | #include "util.h" |
663996b3 MS |
39 | |
40 | static char **arg_prefixes = NULL; | |
41 | ||
db2df898 | 42 | static const char conf_file_dirs[] = CONF_PATHS_NULSTR("sysctl.d"); |
663996b3 | 43 | |
663996b3 | 44 | static int apply_all(Hashmap *sysctl_options) { |
663996b3 MS |
45 | char *property, *value; |
46 | Iterator i; | |
e3bff60a | 47 | int r = 0; |
663996b3 MS |
48 | |
49 | HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) { | |
50 | int k; | |
51 | ||
e3bff60a MP |
52 | k = sysctl_write(property, value); |
53 | if (k < 0) { | |
13d276d0 MP |
54 | log_full_errno(k == -ENOENT ? LOG_INFO : LOG_WARNING, k, |
55 | "Couldn't write '%s' to '%s', ignoring: %m", value, property); | |
e3bff60a MP |
56 | |
57 | if (r == 0 && k != -ENOENT) | |
58 | r = k; | |
59 | } | |
663996b3 | 60 | } |
e3bff60a | 61 | |
663996b3 MS |
62 | return r; |
63 | } | |
64 | ||
65 | static int parse_file(Hashmap *sysctl_options, const char *path, bool ignore_enoent) { | |
66 | _cleanup_fclose_ FILE *f = NULL; | |
67 | int r; | |
68 | ||
69 | assert(path); | |
70 | ||
60f067b4 | 71 | r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f); |
663996b3 MS |
72 | if (r < 0) { |
73 | if (ignore_enoent && r == -ENOENT) | |
74 | return 0; | |
75 | ||
f47781d8 | 76 | return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path); |
663996b3 MS |
77 | } |
78 | ||
e3bff60a | 79 | log_debug("Parsing %s", path); |
663996b3 MS |
80 | while (!feof(f)) { |
81 | char l[LINE_MAX], *p, *value, *new_value, *property, *existing; | |
14228c0d | 82 | void *v; |
663996b3 MS |
83 | int k; |
84 | ||
85 | if (!fgets(l, sizeof(l), f)) { | |
86 | if (feof(f)) | |
87 | break; | |
88 | ||
db2df898 | 89 | return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path); |
663996b3 MS |
90 | } |
91 | ||
92 | p = strstrip(l); | |
93 | if (!*p) | |
94 | continue; | |
95 | ||
96 | if (strchr(COMMENTS "\n", *p)) | |
97 | continue; | |
98 | ||
99 | value = strchr(p, '='); | |
100 | if (!value) { | |
101 | log_error("Line is not an assignment in file '%s': %s", path, value); | |
102 | ||
103 | if (r == 0) | |
104 | r = -EINVAL; | |
105 | continue; | |
106 | } | |
107 | ||
108 | *value = 0; | |
109 | value++; | |
110 | ||
e3bff60a | 111 | p = sysctl_normalize(strstrip(p)); |
663996b3 MS |
112 | value = strstrip(value); |
113 | ||
e735f4d4 MP |
114 | if (!strv_isempty(arg_prefixes)) { |
115 | char **i, *t; | |
116 | STRV_FOREACH(i, arg_prefixes) { | |
117 | t = path_startswith(*i, "/proc/sys/"); | |
118 | if (t == NULL) | |
119 | t = *i; | |
120 | if (path_startswith(p, t)) | |
121 | goto found; | |
122 | } | |
123 | /* not found */ | |
124 | continue; | |
125 | } | |
126 | ||
127 | found: | |
14228c0d | 128 | existing = hashmap_get2(sysctl_options, p, &v); |
663996b3 | 129 | if (existing) { |
14228c0d MB |
130 | if (streq(value, existing)) |
131 | continue; | |
663996b3 | 132 | |
e3bff60a | 133 | log_debug("Overwriting earlier assignment of %s in file '%s'.", p, path); |
14228c0d MB |
134 | free(hashmap_remove(sysctl_options, p)); |
135 | free(v); | |
663996b3 MS |
136 | } |
137 | ||
138 | property = strdup(p); | |
139 | if (!property) | |
140 | return log_oom(); | |
141 | ||
142 | new_value = strdup(value); | |
143 | if (!new_value) { | |
144 | free(property); | |
145 | return log_oom(); | |
146 | } | |
147 | ||
148 | k = hashmap_put(sysctl_options, property, new_value); | |
149 | if (k < 0) { | |
f47781d8 | 150 | log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", property); |
663996b3 MS |
151 | free(property); |
152 | free(new_value); | |
153 | return k; | |
154 | } | |
155 | } | |
156 | ||
157 | return r; | |
158 | } | |
159 | ||
5eef597e | 160 | static void help(void) { |
663996b3 MS |
161 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
162 | "Applies kernel sysctl settings.\n\n" | |
163 | " -h --help Show this help\n" | |
60f067b4 | 164 | " --version Show package version\n" |
5eef597e MP |
165 | " --prefix=PATH Only apply rules with the specified prefix\n" |
166 | , program_invocation_short_name); | |
663996b3 MS |
167 | } |
168 | ||
169 | static int parse_argv(int argc, char *argv[]) { | |
170 | ||
171 | enum { | |
60f067b4 | 172 | ARG_VERSION = 0x100, |
663996b3 MS |
173 | ARG_PREFIX |
174 | }; | |
175 | ||
176 | static const struct option options[] = { | |
177 | { "help", no_argument, NULL, 'h' }, | |
60f067b4 | 178 | { "version", no_argument, NULL, ARG_VERSION }, |
663996b3 | 179 | { "prefix", required_argument, NULL, ARG_PREFIX }, |
60f067b4 | 180 | {} |
663996b3 MS |
181 | }; |
182 | ||
183 | int c; | |
184 | ||
185 | assert(argc >= 0); | |
186 | assert(argv); | |
187 | ||
5eef597e | 188 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
663996b3 MS |
189 | |
190 | switch (c) { | |
191 | ||
192 | case 'h': | |
5eef597e MP |
193 | help(); |
194 | return 0; | |
60f067b4 JS |
195 | |
196 | case ARG_VERSION: | |
6300502b | 197 | return version(); |
663996b3 MS |
198 | |
199 | case ARG_PREFIX: { | |
200 | char *p; | |
663996b3 | 201 | |
5eef597e MP |
202 | /* We used to require people to specify absolute paths |
203 | * in /proc/sys in the past. This is kinda useless, but | |
204 | * we need to keep compatibility. We now support any | |
205 | * sysctl name available. */ | |
e3bff60a MP |
206 | sysctl_normalize(optarg); |
207 | ||
5eef597e MP |
208 | if (startswith(optarg, "/proc/sys")) |
209 | p = strdup(optarg); | |
210 | else | |
211 | p = strappend("/proc/sys/", optarg); | |
5eef597e MP |
212 | if (!p) |
213 | return log_oom(); | |
e3bff60a | 214 | |
5eef597e | 215 | if (strv_consume(&arg_prefixes, p) < 0) |
663996b3 MS |
216 | return log_oom(); |
217 | ||
663996b3 MS |
218 | break; |
219 | } | |
220 | ||
221 | case '?': | |
222 | return -EINVAL; | |
223 | ||
224 | default: | |
60f067b4 | 225 | assert_not_reached("Unhandled option"); |
663996b3 | 226 | } |
663996b3 MS |
227 | |
228 | return 1; | |
229 | } | |
230 | ||
231 | int main(int argc, char *argv[]) { | |
232 | int r = 0, k; | |
233 | Hashmap *sysctl_options; | |
234 | ||
235 | r = parse_argv(argc, argv); | |
236 | if (r <= 0) | |
237 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
238 | ||
239 | log_set_target(LOG_TARGET_AUTO); | |
240 | log_parse_environment(); | |
241 | log_open(); | |
242 | ||
243 | umask(0022); | |
244 | ||
5eef597e | 245 | sysctl_options = hashmap_new(&string_hash_ops); |
663996b3 MS |
246 | if (!sysctl_options) { |
247 | r = log_oom(); | |
248 | goto finish; | |
249 | } | |
250 | ||
251 | r = 0; | |
252 | ||
253 | if (argc > optind) { | |
254 | int i; | |
255 | ||
256 | for (i = optind; i < argc; i++) { | |
257 | k = parse_file(sysctl_options, argv[i], false); | |
258 | if (k < 0 && r == 0) | |
259 | r = k; | |
260 | } | |
261 | } else { | |
262 | _cleanup_strv_free_ char **files = NULL; | |
263 | char **f; | |
264 | ||
265 | r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs); | |
266 | if (r < 0) { | |
f47781d8 | 267 | log_error_errno(r, "Failed to enumerate sysctl.d files: %m"); |
663996b3 MS |
268 | goto finish; |
269 | } | |
270 | ||
663996b3 MS |
271 | STRV_FOREACH(f, files) { |
272 | k = parse_file(sysctl_options, *f, true); | |
273 | if (k < 0 && r == 0) | |
274 | r = k; | |
275 | } | |
276 | } | |
277 | ||
278 | k = apply_all(sysctl_options); | |
279 | if (k < 0 && r == 0) | |
280 | r = k; | |
281 | ||
282 | finish: | |
283 | hashmap_free_free_free(sysctl_options); | |
284 | strv_free(arg_prefixes); | |
285 | ||
286 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
287 | } |