2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2010-11-18 21:52:26 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2011-07-12 03:36:17 +02:00
|
|
|
#include <getopt.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2012-05-07 18:55:45 +02:00
|
|
|
#include "conf-files.h"
|
2015-11-03 12:26:12 +01:00
|
|
|
#include "def.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2013-02-14 12:26:13 +01:00
|
|
|
#include "fileio.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "hashmap.h"
|
|
|
|
#include "log.h"
|
2018-11-20 11:28:19 +01:00
|
|
|
#include "main-func.h"
|
2018-06-12 15:37:53 +02:00
|
|
|
#include "pager.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "path-util.h"
|
2018-11-20 15:42:57 +01:00
|
|
|
#include "pretty-print.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "strv.h"
|
2015-03-11 10:37:45 +01:00
|
|
|
#include "sysctl-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "util.h"
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2013-02-11 23:48:36 +01:00
|
|
|
static char **arg_prefixes = NULL;
|
2018-04-26 21:08:53 +02:00
|
|
|
static bool arg_cat_config = false;
|
2018-11-11 12:56:29 +01:00
|
|
|
static PagerFlags arg_pager_flags = 0;
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2018-11-20 11:29:54 +01:00
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep);
|
|
|
|
|
2016-09-24 14:56:07 +02:00
|
|
|
static int apply_all(OrderedHashmap *sysctl_options) {
|
2012-08-03 16:20:31 +02:00
|
|
|
char *property, *value;
|
|
|
|
Iterator i;
|
2015-04-24 19:55:16 +02:00
|
|
|
int r = 0;
|
2013-02-11 23:48:36 +01:00
|
|
|
|
2016-09-24 14:56:07 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) {
|
2012-08-03 16:20:31 +02:00
|
|
|
int k;
|
|
|
|
|
2015-03-11 10:37:45 +01:00
|
|
|
k = sysctl_write(property, value);
|
|
|
|
if (k < 0) {
|
2016-10-25 09:22:22 +02:00
|
|
|
/* If the sysctl is not available in the kernel or we are running with reduced privileges and
|
|
|
|
* cannot write it, then log about the issue at LOG_NOTICE level, and proceed without
|
|
|
|
* failing. (EROFS is treated as a permission problem here, since that's how container managers
|
|
|
|
* usually protected their sysctls.) In all other cases log an error and make the tool fail. */
|
|
|
|
|
|
|
|
if (IN_SET(k, -EPERM, -EACCES, -EROFS, -ENOENT))
|
|
|
|
log_notice_errno(k, "Couldn't write '%s' to '%s', ignoring: %m", value, property);
|
|
|
|
else {
|
|
|
|
log_error_errno(k, "Couldn't write '%s' to '%s': %m", value, property);
|
|
|
|
if (r == 0)
|
|
|
|
r = k;
|
|
|
|
}
|
2015-03-11 10:37:45 +01:00
|
|
|
}
|
2012-08-03 16:20:31 +02:00
|
|
|
}
|
2015-04-24 19:55:16 +02:00
|
|
|
|
2012-08-03 16:20:31 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-10-25 09:25:21 +02:00
|
|
|
static bool test_prefix(const char *p) {
|
|
|
|
char **i;
|
|
|
|
|
|
|
|
if (strv_isempty(arg_prefixes))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
STRV_FOREACH(i, arg_prefixes) {
|
|
|
|
const char *t;
|
|
|
|
|
|
|
|
t = path_startswith(*i, "/proc/sys/");
|
|
|
|
if (!t)
|
|
|
|
t = *i;
|
|
|
|
if (path_startswith(p, t))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-24 14:56:07 +02:00
|
|
|
static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ignore_enoent) {
|
2013-02-11 23:48:36 +01:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2016-10-25 09:26:10 +02:00
|
|
|
unsigned c = 0;
|
2013-02-11 23:48:36 +01:00
|
|
|
int r;
|
2010-11-18 21:52:26 +01:00
|
|
|
|
|
|
|
assert(path);
|
|
|
|
|
2018-04-27 18:20:17 +02:00
|
|
|
r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("sysctl.d"), &f);
|
2013-02-11 23:48:36 +01:00
|
|
|
if (r < 0) {
|
2013-03-27 13:41:59 +01:00
|
|
|
if (ignore_enoent && r == -ENOENT)
|
2011-03-03 22:17:09 +01:00
|
|
|
return 0;
|
|
|
|
|
2014-11-28 19:13:53 +01:00
|
|
|
return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path);
|
2010-11-18 21:52:26 +01:00
|
|
|
}
|
|
|
|
|
2015-03-15 03:56:01 +01:00
|
|
|
log_debug("Parsing %s", path);
|
2016-10-25 09:26:31 +02:00
|
|
|
for (;;) {
|
2017-12-12 16:43:17 +01:00
|
|
|
char *p, *value, *new_value, *property, *existing;
|
|
|
|
_cleanup_free_ char *l = NULL;
|
2013-08-15 18:35:03 +02:00
|
|
|
void *v;
|
2013-02-11 23:48:36 +01:00
|
|
|
int k;
|
2018-02-08 18:58:35 +01:00
|
|
|
|
2017-12-12 16:43:17 +01:00
|
|
|
k = read_line(f, LONG_LINE_MAX, &l);
|
|
|
|
if (k == 0)
|
|
|
|
break;
|
|
|
|
if (k < 0)
|
|
|
|
return log_error_errno(k, "Failed to read file '%s', ignoring: %m", path);
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2016-10-25 09:26:10 +02:00
|
|
|
c++;
|
|
|
|
|
2010-11-18 21:52:26 +01:00
|
|
|
p = strstrip(l);
|
|
|
|
|
2018-02-08 18:58:35 +01:00
|
|
|
if (isempty(p))
|
|
|
|
continue;
|
2013-04-14 02:22:53 +02:00
|
|
|
if (strchr(COMMENTS "\n", *p))
|
2010-11-18 21:52:26 +01:00
|
|
|
continue;
|
|
|
|
|
2012-08-03 16:20:31 +02:00
|
|
|
value = strchr(p, '=');
|
|
|
|
if (!value) {
|
2016-10-25 09:26:10 +02:00
|
|
|
log_error("Line is not an assignment at '%s:%u': %s", path, c, value);
|
2011-03-03 22:17:09 +01:00
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
r = -EINVAL;
|
2010-11-18 21:52:26 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = 0;
|
|
|
|
value++;
|
|
|
|
|
2015-03-11 10:37:45 +01:00
|
|
|
p = sysctl_normalize(strstrip(p));
|
2013-02-11 23:48:36 +01:00
|
|
|
value = strstrip(value);
|
|
|
|
|
2016-10-25 09:25:21 +02:00
|
|
|
if (!test_prefix(p))
|
2015-02-07 14:12:41 +01:00
|
|
|
continue;
|
|
|
|
|
2016-09-24 14:56:07 +02:00
|
|
|
existing = ordered_hashmap_get2(sysctl_options, p, &v);
|
2013-02-11 23:48:36 +01:00
|
|
|
if (existing) {
|
2013-08-15 18:35:03 +02:00
|
|
|
if (streq(value, existing))
|
|
|
|
continue;
|
2013-02-11 23:48:36 +01:00
|
|
|
|
2016-10-25 09:26:10 +02:00
|
|
|
log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, path, c);
|
2016-09-24 14:56:07 +02:00
|
|
|
free(ordered_hashmap_remove(sysctl_options, p));
|
2013-08-15 18:35:03 +02:00
|
|
|
free(v);
|
2012-08-03 16:20:31 +02:00
|
|
|
}
|
|
|
|
|
2013-02-11 23:48:36 +01:00
|
|
|
property = strdup(p);
|
|
|
|
if (!property)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
new_value = strdup(value);
|
2012-08-03 16:20:31 +02:00
|
|
|
if (!new_value) {
|
|
|
|
free(property);
|
2013-02-11 23:48:36 +01:00
|
|
|
return log_oom();
|
2012-08-03 16:20:31 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 14:56:07 +02:00
|
|
|
k = ordered_hashmap_put(sysctl_options, property, new_value);
|
2013-02-11 23:48:36 +01:00
|
|
|
if (k < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", property);
|
2012-08-03 16:20:31 +02:00
|
|
|
free(property);
|
|
|
|
free(new_value);
|
2013-02-11 23:48:36 +01:00
|
|
|
return k;
|
2012-08-03 16:20:31 +02:00
|
|
|
}
|
2010-11-18 21:52:26 +01:00
|
|
|
}
|
|
|
|
|
2011-03-03 22:17:09 +01:00
|
|
|
return r;
|
2010-11-18 21:52:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("systemd-sysctl.service", "8", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2011-07-12 03:36:17 +02:00
|
|
|
printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
|
|
|
|
"Applies kernel sysctl settings.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
2013-11-06 18:28:39 +01:00
|
|
|
" --version Show package version\n"
|
2018-04-26 21:08:53 +02:00
|
|
|
" --cat-config Show configuration files\n"
|
2014-09-17 09:06:49 +02:00
|
|
|
" --prefix=PATH Only apply rules with the specified prefix\n"
|
2018-06-12 15:37:53 +02:00
|
|
|
" --no-pager Do not pipe output into a pager\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2011-07-12 03:36:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
2013-11-06 18:28:39 +01:00
|
|
|
ARG_VERSION = 0x100,
|
2018-04-26 21:08:53 +02:00
|
|
|
ARG_CAT_CONFIG,
|
|
|
|
ARG_PREFIX,
|
2018-06-12 15:37:53 +02:00
|
|
|
ARG_NO_PAGER,
|
2011-07-12 03:36:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
2018-04-26 21:08:53 +02:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
|
|
|
|
{ "prefix", required_argument, NULL, ARG_PREFIX },
|
2018-06-12 15:37:53 +02:00
|
|
|
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
2013-11-06 18:28:39 +01:00
|
|
|
{}
|
2011-07-12 03:36:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
|
2011-07-12 03:36:17 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2013-11-06 18:28:39 +01:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2011-07-12 03:36:17 +02:00
|
|
|
|
2018-04-26 21:08:53 +02:00
|
|
|
case ARG_CAT_CONFIG:
|
|
|
|
arg_cat_config = true;
|
|
|
|
break;
|
|
|
|
|
2011-07-12 03:36:17 +02:00
|
|
|
case ARG_PREFIX: {
|
|
|
|
char *p;
|
|
|
|
|
2014-09-17 09:06:49 +02:00
|
|
|
/* We used to require people to specify absolute paths
|
|
|
|
* in /proc/sys in the past. This is kinda useless, but
|
|
|
|
* we need to keep compatibility. We now support any
|
|
|
|
* sysctl name available. */
|
2015-03-11 10:37:45 +01:00
|
|
|
sysctl_normalize(optarg);
|
2015-04-24 19:55:16 +02:00
|
|
|
|
2017-08-09 19:03:39 +02:00
|
|
|
if (path_startswith(optarg, "/proc/sys"))
|
2014-09-17 09:06:49 +02:00
|
|
|
p = strdup(optarg);
|
|
|
|
else
|
|
|
|
p = strappend("/proc/sys/", optarg);
|
|
|
|
if (!p)
|
|
|
|
return log_oom();
|
2015-04-24 19:55:16 +02:00
|
|
|
|
2014-09-17 09:06:49 +02:00
|
|
|
if (strv_consume(&arg_prefixes, p) < 0)
|
2012-07-25 23:55:59 +02:00
|
|
|
return log_oom();
|
2011-07-15 02:01:31 +02:00
|
|
|
|
2011-07-12 03:36:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-12 15:37:53 +02:00
|
|
|
case ARG_NO_PAGER:
|
2018-11-11 12:56:29 +01:00
|
|
|
arg_pager_flags |= PAGER_DISABLE;
|
2018-06-12 15:37:53 +02:00
|
|
|
break;
|
|
|
|
|
2011-07-12 03:36:17 +02:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
2013-11-06 18:28:39 +01:00
|
|
|
assert_not_reached("Unhandled option");
|
2011-07-12 03:36:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (arg_cat_config && argc > optind)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Positional arguments are not allowed with --cat-config");
|
2018-04-26 21:08:53 +02:00
|
|
|
|
2011-07-12 03:36:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-11-20 11:28:19 +01:00
|
|
|
static int run(int argc, char *argv[]) {
|
2018-11-20 11:29:02 +01:00
|
|
|
_cleanup_(ordered_hashmap_free_free_freep) OrderedHashmap *sysctl_options = NULL;
|
2018-11-20 14:03:46 +01:00
|
|
|
int r, k;
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2011-07-12 03:36:17 +02:00
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
2018-11-20 14:03:46 +01:00
|
|
|
return r;
|
2011-07-12 03:36:17 +02:00
|
|
|
|
2018-11-20 11:18:22 +01:00
|
|
|
log_setup_service();
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2011-08-01 20:52:18 +02:00
|
|
|
umask(0022);
|
|
|
|
|
2018-02-08 18:58:35 +01:00
|
|
|
sysctl_options = ordered_hashmap_new(&path_hash_ops);
|
2018-11-20 14:03:46 +01:00
|
|
|
if (!sysctl_options)
|
|
|
|
return log_oom();
|
2012-08-03 16:20:31 +02:00
|
|
|
|
2012-09-21 17:01:39 +02:00
|
|
|
r = 0;
|
|
|
|
|
2012-03-20 15:31:09 +01:00
|
|
|
if (argc > optind) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = optind; i < argc; i++) {
|
2013-02-11 23:48:36 +01:00
|
|
|
k = parse_file(sysctl_options, argv[i], false);
|
|
|
|
if (k < 0 && r == 0)
|
2012-03-20 15:31:09 +01:00
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-11 23:48:36 +01:00
|
|
|
_cleanup_strv_free_ char **files = NULL;
|
|
|
|
char **f;
|
2011-03-03 22:17:09 +01:00
|
|
|
|
2018-04-27 18:20:17 +02:00
|
|
|
r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
|
2018-11-20 14:03:46 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
|
2011-04-25 20:41:47 +02:00
|
|
|
|
2018-04-26 21:08:53 +02:00
|
|
|
if (arg_cat_config) {
|
2018-11-11 12:56:29 +01:00
|
|
|
(void) pager_open(arg_pager_flags);
|
2018-06-12 15:37:53 +02:00
|
|
|
|
2018-11-20 14:03:46 +01:00
|
|
|
return cat_files(NULL, files, 0);
|
2018-04-26 21:08:53 +02:00
|
|
|
}
|
|
|
|
|
2013-02-11 23:48:36 +01:00
|
|
|
STRV_FOREACH(f, files) {
|
|
|
|
k = parse_file(sysctl_options, *f, true);
|
|
|
|
if (k < 0 && r == 0)
|
2011-04-25 20:41:47 +02:00
|
|
|
r = k;
|
|
|
|
}
|
2010-11-18 21:52:26 +01:00
|
|
|
}
|
2012-08-03 16:20:31 +02:00
|
|
|
|
2013-02-11 23:48:36 +01:00
|
|
|
k = apply_all(sysctl_options);
|
|
|
|
if (k < 0 && r == 0)
|
2012-09-21 17:01:39 +02:00
|
|
|
r = k;
|
|
|
|
|
2018-11-20 11:28:19 +01:00
|
|
|
return r;
|
2010-11-18 21:52:26 +01:00
|
|
|
}
|
2018-11-20 11:28:19 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|