]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sysctl/sysctl.c
Merge pull request #8947 from yuwata/meson-0.44
[thirdparty/systemd.git] / src / sysctl / sysctl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <getopt.h>
10 #include <limits.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "conf-files.h"
17 #include "def.h"
18 #include "fd-util.h"
19 #include "fileio.h"
20 #include "hashmap.h"
21 #include "log.h"
22 #include "path-util.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "sysctl-util.h"
26 #include "terminal-util.h"
27 #include "util.h"
28
29 static char **arg_prefixes = NULL;
30 static bool arg_cat_config = 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 , program_invocation_short_name);
174 }
175
176 static int parse_argv(int argc, char *argv[]) {
177
178 enum {
179 ARG_VERSION = 0x100,
180 ARG_CAT_CONFIG,
181 ARG_PREFIX,
182 };
183
184 static const struct option options[] = {
185 { "help", no_argument, NULL, 'h' },
186 { "version", no_argument, NULL, ARG_VERSION },
187 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
188 { "prefix", required_argument, NULL, ARG_PREFIX },
189 {}
190 };
191
192 int c;
193
194 assert(argc >= 0);
195 assert(argv);
196
197 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
198
199 switch (c) {
200
201 case 'h':
202 help();
203 return 0;
204
205 case ARG_VERSION:
206 return version();
207
208 case ARG_CAT_CONFIG:
209 arg_cat_config = true;
210 break;
211
212 case ARG_PREFIX: {
213 char *p;
214
215 /* We used to require people to specify absolute paths
216 * in /proc/sys in the past. This is kinda useless, but
217 * we need to keep compatibility. We now support any
218 * sysctl name available. */
219 sysctl_normalize(optarg);
220
221 if (path_startswith(optarg, "/proc/sys"))
222 p = strdup(optarg);
223 else
224 p = strappend("/proc/sys/", optarg);
225 if (!p)
226 return log_oom();
227
228 if (strv_consume(&arg_prefixes, p) < 0)
229 return log_oom();
230
231 break;
232 }
233
234 case '?':
235 return -EINVAL;
236
237 default:
238 assert_not_reached("Unhandled option");
239 }
240
241 if (arg_cat_config && argc > optind) {
242 log_error("Positional arguments are not allowed with --cat-config");
243 return -EINVAL;
244 }
245
246 return 1;
247 }
248
249 int main(int argc, char *argv[]) {
250 OrderedHashmap *sysctl_options = NULL;
251 int r = 0, k;
252
253 r = parse_argv(argc, argv);
254 if (r <= 0)
255 goto finish;
256
257 log_set_target(LOG_TARGET_AUTO);
258 log_parse_environment();
259 log_open();
260
261 umask(0022);
262
263 sysctl_options = ordered_hashmap_new(&path_hash_ops);
264 if (!sysctl_options) {
265 r = log_oom();
266 goto finish;
267 }
268
269 r = 0;
270
271 if (argc > optind) {
272 int i;
273
274 for (i = optind; i < argc; i++) {
275 k = parse_file(sysctl_options, argv[i], false);
276 if (k < 0 && r == 0)
277 r = k;
278 }
279 } else {
280 _cleanup_strv_free_ char **files = NULL;
281 char **f;
282
283 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
284 if (r < 0) {
285 log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
286 goto finish;
287 }
288
289 if (arg_cat_config) {
290 r = cat_files(NULL, files, 0);
291 goto finish;
292 }
293
294 STRV_FOREACH(f, files) {
295 k = parse_file(sysctl_options, *f, true);
296 if (k < 0 && r == 0)
297 r = k;
298 }
299 }
300
301 k = apply_all(sysctl_options);
302 if (k < 0 && r == 0)
303 r = k;
304
305 finish:
306 ordered_hashmap_free_free_free(sysctl_options);
307 strv_free(arg_prefixes);
308
309 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
310 }