]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sysctl/sysctl.c
test-fileio: do not use variable before checking return value
[thirdparty/systemd.git] / src / sysctl / sysctl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
8e1bd70d 2
8e1bd70d 3#include <errno.h>
7a2a0b90 4#include <getopt.h>
3f6fd1ba
LP
5#include <limits.h>
6#include <stdbool.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
8e1bd70d 10
2c21044f 11#include "conf-files.h"
a0f29c76 12#include "def.h"
3ffd4af2 13#include "fd-util.h"
a5c32cff 14#include "fileio.h"
3f6fd1ba
LP
15#include "hashmap.h"
16#include "log.h"
8eb42d90 17#include "main-func.h"
dcd5c891 18#include "pager.h"
3f6fd1ba 19#include "path-util.h"
294bf0c3 20#include "pretty-print.h"
07630cea 21#include "string-util.h"
3f6fd1ba 22#include "strv.h"
88a60da0 23#include "sysctl-util.h"
3f6fd1ba 24#include "util.h"
8e1bd70d 25
fabe5c0e 26static char **arg_prefixes = NULL;
3c51c626 27static bool arg_cat_config = false;
0221d68a 28static PagerFlags arg_pager_flags = 0;
8e1bd70d 29
fd8bdbc7
LP
30STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep);
31
886cf982 32static int apply_all(OrderedHashmap *sysctl_options) {
86fc77c4
MS
33 char *property, *value;
34 Iterator i;
e50b33be 35 int r = 0;
fabe5c0e 36
886cf982 37 ORDERED_HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) {
86fc77c4
MS
38 int k;
39
88a60da0
KS
40 k = sysctl_write(property, value);
41 if (k < 0) {
39540de8
LP
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 }
88a60da0 54 }
86fc77c4 55 }
e50b33be 56
86fc77c4
MS
57 return r;
58}
59
9c37b41c
LP
60static 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
886cf982 79static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ignore_enoent) {
fabe5c0e 80 _cleanup_fclose_ FILE *f = NULL;
98bf5011 81 unsigned c = 0;
fabe5c0e 82 int r;
8e1bd70d
LP
83
84 assert(path);
85
a826d4f7 86 r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("sysctl.d"), &f);
fabe5c0e 87 if (r < 0) {
6f6fad96 88 if (ignore_enoent && r == -ENOENT)
c1b664d0
LP
89 return 0;
90
8d3d7072 91 return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path);
8e1bd70d
LP
92 }
93
924bc14f 94 log_debug("Parsing %s", path);
4f14f2bb 95 for (;;) {
a668bfe8
TSH
96 char *p, *value, *new_value, *property, *existing;
97 _cleanup_free_ char *l = NULL;
04bf3c1a 98 void *v;
fabe5c0e 99 int k;
548f6937 100
a668bfe8
TSH
101 k = read_line(f, LONG_LINE_MAX, &l);
102 if (k == 0)
103 break;
a668bfe8
TSH
104 if (k < 0)
105 return log_error_errno(k, "Failed to read file '%s', ignoring: %m", path);
8e1bd70d 106
98bf5011
LP
107 c++;
108
8e1bd70d 109 p = strstrip(l);
8e1bd70d 110
548f6937
LP
111 if (isempty(p))
112 continue;
d3b6d0c2 113 if (strchr(COMMENTS "\n", *p))
8e1bd70d
LP
114 continue;
115
86fc77c4
MS
116 value = strchr(p, '=');
117 if (!value) {
bfc4183e 118 log_error("Line is not an assignment at '%s:%u': %s", path, c, p);
c1b664d0
LP
119
120 if (r == 0)
121 r = -EINVAL;
8e1bd70d
LP
122 continue;
123 }
124
125 *value = 0;
126 value++;
127
88a60da0 128 p = sysctl_normalize(strstrip(p));
fabe5c0e
LP
129 value = strstrip(value);
130
9c37b41c 131 if (!test_prefix(p))
b99802f7 132 continue;
b99802f7 133
886cf982 134 existing = ordered_hashmap_get2(sysctl_options, p, &v);
fabe5c0e 135 if (existing) {
04bf3c1a
KS
136 if (streq(value, existing))
137 continue;
fabe5c0e 138
98bf5011 139 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, path, c);
886cf982 140 free(ordered_hashmap_remove(sysctl_options, p));
04bf3c1a 141 free(v);
86fc77c4
MS
142 }
143
fabe5c0e
LP
144 property = strdup(p);
145 if (!property)
146 return log_oom();
147
148 new_value = strdup(value);
86fc77c4
MS
149 if (!new_value) {
150 free(property);
fabe5c0e 151 return log_oom();
86fc77c4
MS
152 }
153
886cf982 154 k = ordered_hashmap_put(sysctl_options, property, new_value);
fabe5c0e 155 if (k < 0) {
da927ba9 156 log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", property);
86fc77c4
MS
157 free(property);
158 free(new_value);
fabe5c0e 159 return k;
86fc77c4 160 }
8e1bd70d
LP
161 }
162
c1b664d0 163 return r;
8e1bd70d
LP
164}
165
37ec0fdd
LP
166static 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
7a2a0b90
LP
174 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
175 "Applies kernel sysctl settings.\n\n"
176 " -h --help Show this help\n"
eb9da376 177 " --version Show package version\n"
3c51c626 178 " --cat-config Show configuration files\n"
0e1f5792 179 " --prefix=PATH Only apply rules with the specified prefix\n"
dcd5c891 180 " --no-pager Do not pipe output into a pager\n"
37ec0fdd
LP
181 "\nSee the %s for details.\n"
182 , program_invocation_short_name
183 , link
184 );
185
186 return 0;
7a2a0b90
LP
187}
188
189static int parse_argv(int argc, char *argv[]) {
190
191 enum {
eb9da376 192 ARG_VERSION = 0x100,
3c51c626
ZJS
193 ARG_CAT_CONFIG,
194 ARG_PREFIX,
dcd5c891 195 ARG_NO_PAGER,
7a2a0b90
LP
196 };
197
198 static const struct option options[] = {
3c51c626
ZJS
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 },
dcd5c891 203 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
eb9da376 204 {}
7a2a0b90
LP
205 };
206
207 int c;
208
209 assert(argc >= 0);
210 assert(argv);
211
601185b4 212 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
7a2a0b90
LP
213
214 switch (c) {
215
216 case 'h':
37ec0fdd 217 return help();
eb9da376
LP
218
219 case ARG_VERSION:
3f6fd1ba 220 return version();
7a2a0b90 221
3c51c626
ZJS
222 case ARG_CAT_CONFIG:
223 arg_cat_config = true;
224 break;
225
7a2a0b90
LP
226 case ARG_PREFIX: {
227 char *p;
228
0e1f5792
DH
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. */
88a60da0 233 sysctl_normalize(optarg);
e50b33be 234
27458ed6 235 if (path_startswith(optarg, "/proc/sys"))
0e1f5792
DH
236 p = strdup(optarg);
237 else
238 p = strappend("/proc/sys/", optarg);
0e1f5792
DH
239 if (!p)
240 return log_oom();
e50b33be 241
0e1f5792 242 if (strv_consume(&arg_prefixes, p) < 0)
0d0f0c50 243 return log_oom();
f68c5a70 244
7a2a0b90
LP
245 break;
246 }
247
dcd5c891 248 case ARG_NO_PAGER:
0221d68a 249 arg_pager_flags |= PAGER_DISABLE;
dcd5c891
LP
250 break;
251
7a2a0b90
LP
252 case '?':
253 return -EINVAL;
254
255 default:
eb9da376 256 assert_not_reached("Unhandled option");
7a2a0b90 257 }
7a2a0b90 258
baaa35ad
ZJS
259 if (arg_cat_config && argc > optind)
260 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
261 "Positional arguments are not allowed with --cat-config");
3c51c626 262
7a2a0b90
LP
263 return 1;
264}
265
8eb42d90 266static int run(int argc, char *argv[]) {
07a91a45 267 _cleanup_(ordered_hashmap_free_free_freep) OrderedHashmap *sysctl_options = NULL;
a34c79d0 268 int r, k;
8e1bd70d 269
7a2a0b90
LP
270 r = parse_argv(argc, argv);
271 if (r <= 0)
a34c79d0 272 return r;
7a2a0b90 273
6bf3c61c 274 log_setup_service();
8e1bd70d 275
4c12626c
LP
276 umask(0022);
277
548f6937 278 sysctl_options = ordered_hashmap_new(&path_hash_ops);
a34c79d0
ZJS
279 if (!sysctl_options)
280 return log_oom();
86fc77c4 281
0187f62b
LP
282 r = 0;
283
de19ece7
LP
284 if (argc > optind) {
285 int i;
286
287 for (i = optind; i < argc; i++) {
fabe5c0e
LP
288 k = parse_file(sysctl_options, argv[i], false);
289 if (k < 0 && r == 0)
de19ece7
LP
290 r = k;
291 }
292 } else {
fabe5c0e
LP
293 _cleanup_strv_free_ char **files = NULL;
294 char **f;
c1b664d0 295
a826d4f7 296 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
a34c79d0
ZJS
297 if (r < 0)
298 return log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
db1413d7 299
3c51c626 300 if (arg_cat_config) {
0221d68a 301 (void) pager_open(arg_pager_flags);
dcd5c891 302
a34c79d0 303 return cat_files(NULL, files, 0);
3c51c626
ZJS
304 }
305
fabe5c0e
LP
306 STRV_FOREACH(f, files) {
307 k = parse_file(sysctl_options, *f, true);
308 if (k < 0 && r == 0)
db1413d7
KS
309 r = k;
310 }
8e1bd70d 311 }
86fc77c4 312
fabe5c0e
LP
313 k = apply_all(sysctl_options);
314 if (k < 0 && r == 0)
0187f62b
LP
315 r = k;
316
8eb42d90 317 return r;
8e1bd70d 318}
8eb42d90
LP
319
320DEFINE_MAIN_FUNCTION(run);