]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sysctl/sysctl.c
4572cba7230690199e1f7e919da50a5c00829b83
[thirdparty/systemd.git] / src / sysctl / sysctl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <getopt.h>
8 #include <limits.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "conf-files.h"
15 #include "def.h"
16 #include "fd-util.h"
17 #include "fileio.h"
18 #include "hashmap.h"
19 #include "log.h"
20 #include "pager.h"
21 #include "path-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "sysctl-util.h"
25 #include "terminal-util.h"
26 #include "util.h"
27
28 static char **arg_prefixes = NULL;
29 static bool arg_cat_config = false;
30 static bool arg_no_pager = false;
31
32 static int apply_all(OrderedHashmap *sysctl_options) {
33 char *property, *value;
34 Iterator i;
35 int r = 0;
36
37 ORDERED_HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) {
38 int k;
39
40 k = sysctl_write(property, value);
41 if (k < 0) {
42 /* If the sysctl is not available in the kernel or we are running with reduced privileges and
43 * cannot write it, then log about the issue at LOG_NOTICE level, and proceed without
44 * failing. (EROFS is treated as a permission problem here, since that's how container managers
45 * usually protected their sysctls.) In all other cases log an error and make the tool fail. */
46
47 if (IN_SET(k, -EPERM, -EACCES, -EROFS, -ENOENT))
48 log_notice_errno(k, "Couldn't write '%s' to '%s', ignoring: %m", value, property);
49 else {
50 log_error_errno(k, "Couldn't write '%s' to '%s': %m", value, property);
51 if (r == 0)
52 r = k;
53 }
54 }
55 }
56
57 return r;
58 }
59
60 static bool test_prefix(const char *p) {
61 char **i;
62
63 if (strv_isempty(arg_prefixes))
64 return true;
65
66 STRV_FOREACH(i, arg_prefixes) {
67 const char *t;
68
69 t = path_startswith(*i, "/proc/sys/");
70 if (!t)
71 t = *i;
72 if (path_startswith(p, t))
73 return true;
74 }
75
76 return false;
77 }
78
79 static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ignore_enoent) {
80 _cleanup_fclose_ FILE *f = NULL;
81 unsigned c = 0;
82 int r;
83
84 assert(path);
85
86 r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("sysctl.d"), &f);
87 if (r < 0) {
88 if (ignore_enoent && r == -ENOENT)
89 return 0;
90
91 return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path);
92 }
93
94 log_debug("Parsing %s", path);
95 for (;;) {
96 char *p, *value, *new_value, *property, *existing;
97 _cleanup_free_ char *l = NULL;
98 void *v;
99 int k;
100
101 k = read_line(f, LONG_LINE_MAX, &l);
102 if (k == 0)
103 break;
104 if (k < 0)
105 return log_error_errno(k, "Failed to read file '%s', ignoring: %m", path);
106
107 c++;
108
109 p = strstrip(l);
110
111 if (isempty(p))
112 continue;
113 if (strchr(COMMENTS "\n", *p))
114 continue;
115
116 value = strchr(p, '=');
117 if (!value) {
118 log_error("Line is not an assignment at '%s:%u': %s", path, c, value);
119
120 if (r == 0)
121 r = -EINVAL;
122 continue;
123 }
124
125 *value = 0;
126 value++;
127
128 p = sysctl_normalize(strstrip(p));
129 value = strstrip(value);
130
131 if (!test_prefix(p))
132 continue;
133
134 existing = ordered_hashmap_get2(sysctl_options, p, &v);
135 if (existing) {
136 if (streq(value, existing))
137 continue;
138
139 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, path, c);
140 free(ordered_hashmap_remove(sysctl_options, p));
141 free(v);
142 }
143
144 property = strdup(p);
145 if (!property)
146 return log_oom();
147
148 new_value = strdup(value);
149 if (!new_value) {
150 free(property);
151 return log_oom();
152 }
153
154 k = ordered_hashmap_put(sysctl_options, property, new_value);
155 if (k < 0) {
156 log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", property);
157 free(property);
158 free(new_value);
159 return k;
160 }
161 }
162
163 return r;
164 }
165
166 static void help(void) {
167 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
168 "Applies kernel sysctl settings.\n\n"
169 " -h --help Show this help\n"
170 " --version Show package version\n"
171 " --cat-config Show configuration files\n"
172 " --prefix=PATH Only apply rules with the specified prefix\n"
173 " --no-pager Do not pipe output into a pager\n"
174 , program_invocation_short_name);
175 }
176
177 static int parse_argv(int argc, char *argv[]) {
178
179 enum {
180 ARG_VERSION = 0x100,
181 ARG_CAT_CONFIG,
182 ARG_PREFIX,
183 ARG_NO_PAGER,
184 };
185
186 static const struct option options[] = {
187 { "help", no_argument, NULL, 'h' },
188 { "version", no_argument, NULL, ARG_VERSION },
189 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
190 { "prefix", required_argument, NULL, ARG_PREFIX },
191 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
192 {}
193 };
194
195 int c;
196
197 assert(argc >= 0);
198 assert(argv);
199
200 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
201
202 switch (c) {
203
204 case 'h':
205 help();
206 return 0;
207
208 case ARG_VERSION:
209 return version();
210
211 case ARG_CAT_CONFIG:
212 arg_cat_config = true;
213 break;
214
215 case ARG_PREFIX: {
216 char *p;
217
218 /* We used to require people to specify absolute paths
219 * in /proc/sys in the past. This is kinda useless, but
220 * we need to keep compatibility. We now support any
221 * sysctl name available. */
222 sysctl_normalize(optarg);
223
224 if (path_startswith(optarg, "/proc/sys"))
225 p = strdup(optarg);
226 else
227 p = strappend("/proc/sys/", optarg);
228 if (!p)
229 return log_oom();
230
231 if (strv_consume(&arg_prefixes, p) < 0)
232 return log_oom();
233
234 break;
235 }
236
237 case ARG_NO_PAGER:
238 arg_no_pager = true;
239 break;
240
241 case '?':
242 return -EINVAL;
243
244 default:
245 assert_not_reached("Unhandled option");
246 }
247
248 if (arg_cat_config && argc > optind) {
249 log_error("Positional arguments are not allowed with --cat-config");
250 return -EINVAL;
251 }
252
253 return 1;
254 }
255
256 int main(int argc, char *argv[]) {
257 OrderedHashmap *sysctl_options = NULL;
258 int r = 0, k;
259
260 r = parse_argv(argc, argv);
261 if (r <= 0)
262 goto finish;
263
264 log_set_target(LOG_TARGET_AUTO);
265 log_parse_environment();
266 log_open();
267
268 umask(0022);
269
270 sysctl_options = ordered_hashmap_new(&path_hash_ops);
271 if (!sysctl_options) {
272 r = log_oom();
273 goto finish;
274 }
275
276 r = 0;
277
278 if (argc > optind) {
279 int i;
280
281 for (i = optind; i < argc; i++) {
282 k = parse_file(sysctl_options, argv[i], false);
283 if (k < 0 && r == 0)
284 r = k;
285 }
286 } else {
287 _cleanup_strv_free_ char **files = NULL;
288 char **f;
289
290 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
291 if (r < 0) {
292 log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
293 goto finish;
294 }
295
296 if (arg_cat_config) {
297 (void) pager_open(arg_no_pager, false);
298
299 r = cat_files(NULL, files, 0);
300 goto finish;
301 }
302
303 STRV_FOREACH(f, files) {
304 k = parse_file(sysctl_options, *f, true);
305 if (k < 0 && r == 0)
306 r = k;
307 }
308 }
309
310 k = apply_all(sysctl_options);
311 if (k < 0 && r == 0)
312 r = k;
313
314 finish:
315 pager_close();
316
317 ordered_hashmap_free_free_free(sysctl_options);
318 strv_free(arg_prefixes);
319
320 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
321 }