1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/types.h>
13 #include "conf-files.h"
19 #include "main-func.h"
21 #include "path-util.h"
22 #include "pretty-print.h"
23 #include "string-util.h"
25 #include "sysctl-util.h"
27 static char **arg_prefixes
= NULL
;
28 static bool arg_cat_config
= false;
29 static PagerFlags arg_pager_flags
= 0;
31 STATIC_DESTRUCTOR_REGISTER(arg_prefixes
, strv_freep
);
33 static int apply_all(OrderedHashmap
*sysctl_options
) {
34 char *property
, *value
;
38 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, sysctl_options
, i
) {
41 k
= sysctl_write(property
, value
);
43 /* If the sysctl is not available in the kernel or we are running with reduced privileges and
44 * cannot write it, then log about the issue at LOG_NOTICE level, and proceed without
45 * failing. (EROFS is treated as a permission problem here, since that's how container managers
46 * usually protected their sysctls.) In all other cases log an error and make the tool fail. */
48 if (IN_SET(k
, -EPERM
, -EACCES
, -EROFS
, -ENOENT
))
49 log_notice_errno(k
, "Couldn't write '%s' to '%s', ignoring: %m", value
, property
);
51 log_error_errno(k
, "Couldn't write '%s' to '%s': %m", value
, property
);
61 static bool test_prefix(const char *p
) {
64 if (strv_isempty(arg_prefixes
))
67 STRV_FOREACH(i
, arg_prefixes
) {
70 t
= path_startswith(*i
, "/proc/sys/");
73 if (path_startswith(p
, t
))
80 static int parse_file(OrderedHashmap
*sysctl_options
, const char *path
, bool ignore_enoent
) {
81 _cleanup_fclose_
FILE *f
= NULL
;
87 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("sysctl.d"), &f
);
89 if (ignore_enoent
&& r
== -ENOENT
)
92 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
95 log_debug("Parsing %s", path
);
97 char *p
, *value
, *new_value
, *property
, *existing
;
98 _cleanup_free_
char *l
= NULL
;
102 k
= read_line(f
, LONG_LINE_MAX
, &l
);
106 return log_error_errno(k
, "Failed to read file '%s', ignoring: %m", path
);
114 if (strchr(COMMENTS
"\n", *p
))
117 value
= strchr(p
, '=');
119 log_error("Line is not an assignment at '%s:%u': %s", path
, c
, p
);
129 p
= sysctl_normalize(strstrip(p
));
130 value
= strstrip(value
);
135 existing
= ordered_hashmap_get2(sysctl_options
, p
, &v
);
137 if (streq(value
, existing
))
140 log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p
, path
, c
);
141 free(ordered_hashmap_remove(sysctl_options
, p
));
145 property
= strdup(p
);
149 new_value
= strdup(value
);
155 k
= ordered_hashmap_put(sysctl_options
, property
, new_value
);
157 log_error_errno(k
, "Failed to add sysctl variable %s to hashmap: %m", property
);
167 static int help(void) {
168 _cleanup_free_
char *link
= NULL
;
171 r
= terminal_urlify_man("systemd-sysctl.service", "8", &link
);
175 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
176 "Applies kernel sysctl settings.\n\n"
177 " -h --help Show this help\n"
178 " --version Show package version\n"
179 " --cat-config Show configuration files\n"
180 " --prefix=PATH Only apply rules with the specified prefix\n"
181 " --no-pager Do not pipe output into a pager\n"
182 "\nSee the %s for details.\n"
183 , program_invocation_short_name
190 static int parse_argv(int argc
, char *argv
[]) {
199 static const struct option options
[] = {
200 { "help", no_argument
, NULL
, 'h' },
201 { "version", no_argument
, NULL
, ARG_VERSION
},
202 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
203 { "prefix", required_argument
, NULL
, ARG_PREFIX
},
204 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
213 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
224 arg_cat_config
= true;
230 /* We used to require people to specify absolute paths
231 * in /proc/sys in the past. This is kinda useless, but
232 * we need to keep compatibility. We now support any
233 * sysctl name available. */
234 sysctl_normalize(optarg
);
236 if (path_startswith(optarg
, "/proc/sys"))
239 p
= strappend("/proc/sys/", optarg
);
243 if (strv_consume(&arg_prefixes
, p
) < 0)
250 arg_pager_flags
|= PAGER_DISABLE
;
257 assert_not_reached("Unhandled option");
260 if (arg_cat_config
&& argc
> optind
)
261 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
262 "Positional arguments are not allowed with --cat-config");
267 static int run(int argc
, char *argv
[]) {
268 _cleanup_(ordered_hashmap_free_free_freep
) OrderedHashmap
*sysctl_options
= NULL
;
271 r
= parse_argv(argc
, argv
);
279 sysctl_options
= ordered_hashmap_new(&path_hash_ops
);
288 for (i
= optind
; i
< argc
; i
++) {
289 k
= parse_file(sysctl_options
, argv
[i
], false);
294 _cleanup_strv_free_
char **files
= NULL
;
297 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
299 return log_error_errno(r
, "Failed to enumerate sysctl.d files: %m");
301 if (arg_cat_config
) {
302 (void) pager_open(arg_pager_flags
);
304 return cat_files(NULL
, files
, 0);
307 STRV_FOREACH(f
, files
) {
308 k
= parse_file(sysctl_options
, *f
, true);
314 k
= apply_all(sysctl_options
);
321 DEFINE_MAIN_FUNCTION(run
);