2010-11-18 21:52:26 +01:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2010 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2010-11-18 21:52:26 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-11-18 21:52:26 +01:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "path-util.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;
|
2010-11-18 21:52:26 +01:00
|
|
|
|
2015-11-10 15:57:21 +01:00
|
|
|
static const char conf_file_dirs[] = CONF_PATHS_NULSTR("sysctl.d");
|
2013-02-11 23:48:36 +01:00
|
|
|
|
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);
|
|
|
|
|
2014-03-14 05:32:12 +01:00
|
|
|
r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &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 (;;) {
|
2013-02-11 23:48:36 +01:00
|
|
|
char l[LINE_MAX], *p, *value, *new_value, *property, *existing;
|
2013-08-15 18:35:03 +02:00
|
|
|
void *v;
|
2013-02-11 23:48:36 +01:00
|
|
|
int k;
|
2010-11-18 21:52:26 +01:00
|
|
|
|
|
|
|
if (!fgets(l, sizeof(l), f)) {
|
|
|
|
if (feof(f))
|
|
|
|
break;
|
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "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);
|
|
|
|
if (!*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
|
|
|
}
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
static void help(void) {
|
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"
|
2014-09-17 09:06:49 +02:00
|
|
|
" --prefix=PATH Only apply rules with the specified prefix\n"
|
2014-08-02 17:12:21 +02:00
|
|
|
, program_invocation_short_name);
|
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,
|
2011-07-12 03:36:17 +02:00
|
|
|
ARG_PREFIX
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2013-11-06 18:28:39 +01:00
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
2011-07-12 03:36:17 +02:00
|
|
|
{ "prefix", required_argument, NULL, ARG_PREFIX },
|
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':
|
2014-08-02 17:12:21 +02:00
|
|
|
help();
|
|
|
|
return 0;
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-11-18 21:52:26 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
2016-10-25 09:27:39 +02:00
|
|
|
OrderedHashmap *sysctl_options = NULL;
|
2012-09-21 17:01:39 +02:00
|
|
|
int r = 0, 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)
|
2016-10-25 09:27:39 +02:00
|
|
|
goto finish;
|
2011-07-12 03:36:17 +02:00
|
|
|
|
2010-11-18 21:52:26 +01:00
|
|
|
log_set_target(LOG_TARGET_AUTO);
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
2011-08-01 20:52:18 +02:00
|
|
|
umask(0022);
|
|
|
|
|
2016-09-24 14:56:07 +02:00
|
|
|
sysctl_options = ordered_hashmap_new(&string_hash_ops);
|
2012-08-03 16:20:31 +02:00
|
|
|
if (!sysctl_options) {
|
|
|
|
r = log_oom();
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
|
2011-04-28 23:51:24 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
|
2011-04-28 23:51:24 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
2011-04-25 20:41:47 +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;
|
|
|
|
|
2011-04-28 23:51:24 +02:00
|
|
|
finish:
|
2016-09-24 14:56:07 +02:00
|
|
|
ordered_hashmap_free_free_free(sysctl_options);
|
2011-07-15 02:01:31 +02:00
|
|
|
strv_free(arg_prefixes);
|
|
|
|
|
2011-03-03 22:17:09 +01:00
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
2010-11-18 21:52:26 +01:00
|
|
|
}
|