]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sysctl/sysctl.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / sysctl / sysctl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <limits.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "conf-files.h"
12 #include "def.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "hashmap.h"
16 #include "log.h"
17 #include "main-func.h"
18 #include "pager.h"
19 #include "path-util.h"
20 #include "pretty-print.h"
21 #include "string-util.h"
22 #include "strv.h"
23 #include "sysctl-util.h"
24 #include "util.h"
25
26 static char **arg_prefixes = NULL;
27 static bool arg_cat_config = false;
28 static PagerFlags arg_pager_flags = 0;
29
30 STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep);
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, p);
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 int help(void) {
167 _cleanup_free_ char *link = NULL;
168 int r;
169
170 r = terminal_urlify_man("systemd-sysctl.service", "8", &link);
171 if (r < 0)
172 return log_oom();
173
174 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
175 "Applies kernel sysctl settings.\n\n"
176 " -h --help Show this help\n"
177 " --version Show package version\n"
178 " --cat-config Show configuration files\n"
179 " --prefix=PATH Only apply rules with the specified prefix\n"
180 " --no-pager Do not pipe output into a pager\n"
181 "\nSee the %s for details.\n"
182 , program_invocation_short_name
183 , link
184 );
185
186 return 0;
187 }
188
189 static int parse_argv(int argc, char *argv[]) {
190
191 enum {
192 ARG_VERSION = 0x100,
193 ARG_CAT_CONFIG,
194 ARG_PREFIX,
195 ARG_NO_PAGER,
196 };
197
198 static const struct option options[] = {
199 { "help", no_argument, NULL, 'h' },
200 { "version", no_argument, NULL, ARG_VERSION },
201 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
202 { "prefix", required_argument, NULL, ARG_PREFIX },
203 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
204 {}
205 };
206
207 int c;
208
209 assert(argc >= 0);
210 assert(argv);
211
212 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
213
214 switch (c) {
215
216 case 'h':
217 return help();
218
219 case ARG_VERSION:
220 return version();
221
222 case ARG_CAT_CONFIG:
223 arg_cat_config = true;
224 break;
225
226 case ARG_PREFIX: {
227 char *p;
228
229 /* We used to require people to specify absolute paths
230 * in /proc/sys in the past. This is kinda useless, but
231 * we need to keep compatibility. We now support any
232 * sysctl name available. */
233 sysctl_normalize(optarg);
234
235 if (path_startswith(optarg, "/proc/sys"))
236 p = strdup(optarg);
237 else
238 p = strappend("/proc/sys/", optarg);
239 if (!p)
240 return log_oom();
241
242 if (strv_consume(&arg_prefixes, p) < 0)
243 return log_oom();
244
245 break;
246 }
247
248 case ARG_NO_PAGER:
249 arg_pager_flags |= PAGER_DISABLE;
250 break;
251
252 case '?':
253 return -EINVAL;
254
255 default:
256 assert_not_reached("Unhandled option");
257 }
258
259 if (arg_cat_config && argc > optind)
260 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
261 "Positional arguments are not allowed with --cat-config");
262
263 return 1;
264 }
265
266 static int run(int argc, char *argv[]) {
267 _cleanup_(ordered_hashmap_free_free_freep) OrderedHashmap *sysctl_options = NULL;
268 int r, k;
269
270 r = parse_argv(argc, argv);
271 if (r <= 0)
272 return r;
273
274 log_setup_service();
275
276 umask(0022);
277
278 sysctl_options = ordered_hashmap_new(&path_hash_ops);
279 if (!sysctl_options)
280 return log_oom();
281
282 r = 0;
283
284 if (argc > optind) {
285 int i;
286
287 for (i = optind; i < argc; i++) {
288 k = parse_file(sysctl_options, argv[i], false);
289 if (k < 0 && r == 0)
290 r = k;
291 }
292 } else {
293 _cleanup_strv_free_ char **files = NULL;
294 char **f;
295
296 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
297 if (r < 0)
298 return log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
299
300 if (arg_cat_config) {
301 (void) pager_open(arg_pager_flags);
302
303 return cat_files(NULL, files, 0);
304 }
305
306 STRV_FOREACH(f, files) {
307 k = parse_file(sysctl_options, *f, true);
308 if (k < 0 && r == 0)
309 r = k;
310 }
311 }
312
313 k = apply_all(sysctl_options);
314 if (k < 0 && r == 0)
315 r = k;
316
317 return r;
318 }
319
320 DEFINE_MAIN_FUNCTION(run);