1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "conf-files.h"
18 #include "path-util.h"
19 #include "string-util.h"
21 #include "sysctl-util.h"
22 #include "terminal-util.h"
25 static char **arg_prefixes
= NULL
;
26 static bool arg_cat_config
= false;
27 static PagerFlags arg_pager_flags
= 0;
29 static int apply_all(OrderedHashmap
*sysctl_options
) {
30 char *property
, *value
;
34 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, sysctl_options
, i
) {
37 k
= sysctl_write(property
, value
);
39 /* If the sysctl is not available in the kernel or we are running with reduced privileges and
40 * cannot write it, then log about the issue at LOG_NOTICE level, and proceed without
41 * failing. (EROFS is treated as a permission problem here, since that's how container managers
42 * usually protected their sysctls.) In all other cases log an error and make the tool fail. */
44 if (IN_SET(k
, -EPERM
, -EACCES
, -EROFS
, -ENOENT
))
45 log_notice_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", value
, property
);
47 log_error_errno(k
, "Couldn't write '%s' to '%s': %m", value
, property
);
57 static bool test_prefix(const char *p
) {
60 if (strv_isempty(arg_prefixes
))
63 STRV_FOREACH(i
, arg_prefixes
) {
66 t
= path_startswith(*i
, "/proc/sys/");
69 if (path_startswith(p
, t
))
76 static int parse_file(OrderedHashmap
*sysctl_options
, const char *path
, bool ignore_enoent
) {
77 _cleanup_fclose_
FILE *f
= NULL
;
83 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
);
85 if (ignore_enoent
&& r
== -ENOENT
)
88 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
91 log_debug("Parsing %s", path
);
93 char *p
, *value
, *new_value
, *property
, *existing
;
94 _cleanup_free_
char *l
= NULL
;
98 k
= read_line(f
, LONG_LINE_MAX
, &l
);
102 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", path
);
110 if (strchr(COMMENTS
"\n", *p
))
113 value
= strchr(p
, '=');
115 log_error("Line is not an assignment at '%s:%u': %s", path
, c
, value
);
125 p
= sysctl_normalize(strstrip(p
));
126 value
= strstrip(value
);
131 existing
= ordered_hashmap_get2(sysctl_options
, p
, &v
);
133 if (streq(value
, existing
))
136 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, path
, c
);
137 free(ordered_hashmap_remove(sysctl_options
, p
));
141 property
= strdup(p
);
145 new_value
= strdup(value
);
151 k
= ordered_hashmap_put(sysctl_options
, property
, new_value
);
153 log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", property
);
163 static int help(void) {
164 _cleanup_free_
char *link
= NULL
;
167 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
171 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
172 "Applies kernel sysctl settings.\n\n"
173 " -h --help Show this help\n"
174 " --version Show package version\n"
175 " --cat-config Show configuration files\n"
176 " --prefix=PATH Only apply rules with the specified prefix\n"
177 " --no-pager Do not pipe output into a pager\n"
178 "\nSee the %s for details.\n"
179 , program_invocation_short_name
186 static int parse_argv(int argc
, char *argv
[]) {
195 static const struct option options
[] = {
196 { "help", no_argument
, NULL
, 'h' },
197 { "version", no_argument
, NULL
, ARG_VERSION
},
198 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
199 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
200 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
209 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
220 arg_cat_config
= true;
226 /* We used to require people to specify absolute paths
227 * in /proc/sys in the past. This is kinda useless, but
228 * we need to keep compatibility. We now support any
229 * sysctl name available. */
230 sysctl_normalize(optarg
);
232 if (path_startswith(optarg
, "/proc/sys"))
235 p
= strappend("/proc/sys/", optarg
);
239 if (strv_consume(&arg_prefixes
, p
) < 0)
246 arg_pager_flags
|= PAGER_DISABLE
;
253 assert_not_reached("Unhandled option");
256 if (arg_cat_config
&& argc
> optind
) {
257 log_error("Positional arguments are not allowed with --cat-config");
264 int main(int argc
, char *argv
[]) {
265 OrderedHashmap
*sysctl_options
= NULL
;
268 r
= parse_argv(argc
, argv
);
276 sysctl_options
= ordered_hashmap_new(&path_hash_ops
);
277 if (!sysctl_options
) {
287 for (i
= optind
; i
< argc
; i
++) {
288 k
= parse_file(sysctl_options
, argv
[i
], false);
293 _cleanup_strv_free_
char **files
= NULL
;
296 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
298 log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
302 if (arg_cat_config
) {
303 (void) pager_open(arg_pager_flags
);
305 r
= cat_files(NULL
, files
, 0);
309 STRV_FOREACH(f
, files
) {
310 k
= parse_file(sysctl_options
, *f
, true);
316 k
= apply_all(sysctl_options
);
323 ordered_hashmap_free_free_free(sysctl_options
);
324 strv_free(arg_prefixes
);
326 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;