2008-10-15 15:14:37 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2003-12-24 11:32:11 +05:30
|
|
|
/*
|
|
|
|
* Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
|
|
|
|
*
|
2006-04-18 03:19:34 +05:30
|
|
|
* Copyright 1999 George Staikos
|
2006-04-12 13:05:12 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2003-12-24 11:32:11 +05:30
|
|
|
*
|
|
|
|
* Changelog:
|
2009-03-29 07:53:16 +05:30
|
|
|
* v1.01 - added -p <preload> to preload values from a file
|
|
|
|
* v1.01.1 - busybox applet aware by <solar@gentoo.org>
|
2003-12-24 11:32:11 +05:30
|
|
|
*/
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define sysctl_trivial_usage
|
|
|
|
//usage: "[OPTIONS] [VALUE]..."
|
|
|
|
//usage:#define sysctl_full_usage "\n\n"
|
|
|
|
//usage: "Configure kernel parameters at runtime\n"
|
|
|
|
//usage: "\n -n Don't print key names"
|
|
|
|
//usage: "\n -e Don't warn about unknown keys"
|
|
|
|
//usage: "\n -w Change sysctl setting"
|
|
|
|
//usage: "\n -p FILE Load sysctl settings from FILE (default /etc/sysctl.conf)"
|
|
|
|
//usage: "\n -a Display all values"
|
|
|
|
//usage: "\n -A Display all values in table form"
|
|
|
|
//usage:
|
|
|
|
//usage:#define sysctl_example_usage
|
|
|
|
//usage: "sysctl [-n] [-e] variable...\n"
|
|
|
|
//usage: "sysctl [-n] [-e] -w variable=value...\n"
|
|
|
|
//usage: "sysctl [-n] [-e] -a\n"
|
|
|
|
//usage: "sysctl [-n] [-e] -p file (default /etc/sysctl.conf)\n"
|
|
|
|
//usage: "sysctl [-n] [-e] -A\n"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2007-11-29 12:10:28 +05:30
|
|
|
enum {
|
|
|
|
FLAG_SHOW_KEYS = 1 << 0,
|
|
|
|
FLAG_SHOW_KEY_ERRORS = 1 << 1,
|
|
|
|
FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
|
|
|
|
FLAG_SHOW_ALL = 1 << 3,
|
|
|
|
FLAG_PRELOAD_FILE = 1 << 4,
|
|
|
|
FLAG_WRITE = 1 << 5,
|
|
|
|
};
|
2009-03-29 07:53:16 +05:30
|
|
|
#define OPTION_STR "neAapw"
|
2007-11-29 12:10:28 +05:30
|
|
|
|
2009-03-29 07:53:16 +05:30
|
|
|
static void sysctl_dots_to_slashes(char *name)
|
2003-12-24 11:32:11 +05:30
|
|
|
{
|
2009-03-29 07:53:16 +05:30
|
|
|
char *cptr, *last_good, *end;
|
2008-07-20 18:31:56 +05:30
|
|
|
|
2009-03-29 07:53:16 +05:30
|
|
|
/* Convert minimum number of '.' to '/' so that
|
|
|
|
* we end up with existing file's name.
|
|
|
|
*
|
|
|
|
* Example from bug 3894:
|
|
|
|
* net.ipv4.conf.eth0.100.mc_forwarding ->
|
|
|
|
* net/ipv4/conf/eth0.100/mc_forwarding
|
|
|
|
* NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
|
|
|
|
* therefore we must start from the end, and if
|
|
|
|
* we replaced even one . -> /, start over again,
|
|
|
|
* but never replace dots before the position
|
|
|
|
* where last replacement occurred.
|
|
|
|
*
|
|
|
|
* Another bug we later had is that
|
|
|
|
* net.ipv4.conf.eth0.100
|
|
|
|
* (without .mc_forwarding) was mishandled.
|
|
|
|
*
|
|
|
|
* To set up testing: modprobe 8021q; vconfig add eth0 100
|
|
|
|
*/
|
|
|
|
end = name + strlen(name);
|
|
|
|
last_good = name - 1;
|
|
|
|
*end = '.'; /* trick the loop into trying full name too */
|
2008-12-24 08:41:43 +05:30
|
|
|
|
2009-03-29 07:53:16 +05:30
|
|
|
again:
|
|
|
|
cptr = end;
|
|
|
|
while (cptr > last_good) {
|
|
|
|
if (*cptr == '.') {
|
|
|
|
*cptr = '\0';
|
|
|
|
//bb_error_msg("trying:'%s'", name);
|
|
|
|
if (access(name, F_OK) == 0) {
|
2010-03-27 09:32:00 +05:30
|
|
|
*cptr = '/';
|
2009-03-29 07:53:16 +05:30
|
|
|
//bb_error_msg("replaced:'%s'", name);
|
|
|
|
last_good = cptr;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
*cptr = '.';
|
|
|
|
}
|
|
|
|
cptr--;
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
2009-03-29 07:53:16 +05:30
|
|
|
*end = '\0';
|
2008-12-24 08:41:43 +05:30
|
|
|
}
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2008-12-24 08:41:43 +05:30
|
|
|
static int sysctl_act_on_setting(char *setting)
|
2003-12-24 11:32:11 +05:30
|
|
|
{
|
2008-12-24 08:41:43 +05:30
|
|
|
int fd, retval = EXIT_SUCCESS;
|
2008-12-29 06:33:17 +05:30
|
|
|
char *cptr, *outname;
|
|
|
|
char *value = value; /* for compiler */
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2008-12-24 08:41:43 +05:30
|
|
|
outname = xstrdup(setting);
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2008-12-24 08:41:43 +05:30
|
|
|
cptr = outname;
|
|
|
|
while (*cptr) {
|
|
|
|
if (*cptr == '/')
|
|
|
|
*cptr = '.';
|
|
|
|
cptr++;
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
|
|
|
|
2008-12-24 08:41:43 +05:30
|
|
|
if (option_mask32 & FLAG_WRITE) {
|
|
|
|
cptr = strchr(setting, '=');
|
|
|
|
if (cptr == NULL) {
|
|
|
|
bb_error_msg("error: '%s' must be of the form name=value",
|
|
|
|
outname);
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
2010-10-29 15:16:52 +05:30
|
|
|
value = cptr + 1; /* point to the value in name=value */
|
2008-12-24 08:41:43 +05:30
|
|
|
if (setting == cptr || !*value) {
|
|
|
|
bb_error_msg("error: malformed setting '%s'", outname);
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
*cptr = '\0';
|
2009-03-29 07:52:19 +05:30
|
|
|
outname[cptr - setting] = '\0';
|
|
|
|
/* procps 3.2.7 actually uses these flags */
|
2008-12-24 08:41:43 +05:30
|
|
|
fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
|
|
|
} else {
|
|
|
|
fd = open(setting, O_RDONLY);
|
|
|
|
}
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2007-04-12 04:50:53 +05:30
|
|
|
if (fd < 0) {
|
2003-12-24 11:32:11 +05:30
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2007-11-29 12:10:28 +05:30
|
|
|
if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
|
2008-12-24 08:41:43 +05:30
|
|
|
bb_error_msg("error: '%s' is an unknown key", outname);
|
2003-12-24 11:32:11 +05:30
|
|
|
break;
|
|
|
|
default:
|
2008-12-24 08:41:43 +05:30
|
|
|
bb_perror_msg("error %sing key '%s'",
|
|
|
|
option_mask32 & FLAG_WRITE ?
|
|
|
|
"sett" : "read",
|
|
|
|
outname);
|
2003-12-24 11:32:11 +05:30
|
|
|
break;
|
|
|
|
}
|
2007-11-29 12:10:28 +05:30
|
|
|
retval = EXIT_FAILURE;
|
2008-12-24 08:41:43 +05:30
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option_mask32 & FLAG_WRITE) {
|
2009-03-29 07:52:19 +05:30
|
|
|
//TODO: procps 3.2.7 writes "value\n", note trailing "\n"
|
2009-03-09 05:42:37 +05:30
|
|
|
xwrite_str(fd, value);
|
2003-12-24 11:32:11 +05:30
|
|
|
close(fd);
|
2008-12-24 08:41:43 +05:30
|
|
|
if (option_mask32 & FLAG_SHOW_KEYS)
|
2007-11-29 12:10:28 +05:30
|
|
|
printf("%s = ", outname);
|
|
|
|
puts(value);
|
2008-12-24 08:41:43 +05:30
|
|
|
} else {
|
|
|
|
char c;
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2008-12-24 08:41:43 +05:30
|
|
|
value = cptr = xmalloc_read(fd, NULL);
|
|
|
|
close(fd);
|
|
|
|
if (value == NULL) {
|
2008-10-15 13:59:17 +05:30
|
|
|
bb_perror_msg("error reading key '%s'", outname);
|
2008-12-24 08:41:43 +05:30
|
|
|
goto end;
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
2008-12-24 08:41:43 +05:30
|
|
|
|
|
|
|
/* dev.cdrom.info and sunrpc.transports, for example,
|
|
|
|
* are multi-line. Try "sysctl sunrpc.transports"
|
|
|
|
*/
|
|
|
|
while ((c = *cptr) != '\0') {
|
|
|
|
if (option_mask32 & FLAG_SHOW_KEYS)
|
2007-11-29 12:10:28 +05:30
|
|
|
printf("%s = ", outname);
|
2008-12-24 08:41:43 +05:30
|
|
|
while (1) {
|
|
|
|
fputc(c, stdout);
|
|
|
|
cptr++;
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
c = *cptr;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
|
|
|
}
|
2008-12-24 08:41:43 +05:30
|
|
|
free(value);
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
2008-12-24 08:41:43 +05:30
|
|
|
end:
|
2003-12-24 11:32:11 +05:30
|
|
|
free(outname);
|
|
|
|
return retval;
|
2008-12-24 08:41:43 +05:30
|
|
|
}
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2009-03-29 07:53:16 +05:30
|
|
|
static int sysctl_act_recursive(const char *path)
|
2003-12-24 11:32:11 +05:30
|
|
|
{
|
2008-12-24 08:41:43 +05:30
|
|
|
DIR *dirp;
|
|
|
|
struct stat buf;
|
|
|
|
struct dirent *entry;
|
|
|
|
char *next;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
stat(path, &buf);
|
|
|
|
if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
|
|
|
|
dirp = opendir(path);
|
|
|
|
if (dirp == NULL)
|
|
|
|
return -1;
|
|
|
|
while ((entry = readdir(dirp)) != NULL) {
|
2009-03-29 07:52:19 +05:30
|
|
|
next = concat_subpath_file(path, entry->d_name);
|
2008-12-24 08:41:43 +05:30
|
|
|
if (next == NULL)
|
|
|
|
continue; /* d_name is "." or ".." */
|
|
|
|
/* if path was ".", drop "./" prefix: */
|
2009-03-29 07:53:16 +05:30
|
|
|
retval |= sysctl_act_recursive((next[0] == '.' && next[1] == '/') ?
|
2008-12-24 08:41:43 +05:30
|
|
|
next + 2 : next);
|
|
|
|
free(next);
|
2007-10-30 00:52:13 +05:30
|
|
|
}
|
2008-12-24 08:41:43 +05:30
|
|
|
closedir(dirp);
|
|
|
|
} else {
|
|
|
|
char *name = xstrdup(path);
|
|
|
|
retval |= sysctl_act_on_setting(name);
|
|
|
|
free(name);
|
|
|
|
}
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
return retval;
|
2008-12-24 08:41:43 +05:30
|
|
|
}
|
2008-10-15 13:52:55 +05:30
|
|
|
|
2009-03-29 07:53:16 +05:30
|
|
|
/* Set sysctl's from a conf file. Format example:
|
|
|
|
* # Controls IP packet forwarding
|
|
|
|
* net.ipv4.ip_forward = 0
|
|
|
|
*/
|
|
|
|
static int sysctl_handle_preload_file(const char *filename)
|
2008-10-15 13:52:55 +05:30
|
|
|
{
|
2009-03-29 07:53:16 +05:30
|
|
|
char *token[2];
|
|
|
|
parser_t *parser;
|
2008-10-15 19:20:24 +05:30
|
|
|
|
2009-03-29 07:53:16 +05:30
|
|
|
parser = config_open(filename);
|
|
|
|
/* Must do it _after_ config_open(): */
|
|
|
|
xchdir("/proc/sys");
|
2012-03-08 04:58:24 +05:30
|
|
|
/* xchroot("/proc/sys") - if you are paranoid */
|
2008-12-24 08:41:43 +05:30
|
|
|
|
2009-03-29 07:53:16 +05:30
|
|
|
//TODO: ';' is comment char too
|
|
|
|
//TODO: comment may be only at line start. "var=1 #abc" - "1 #abc" is the value
|
|
|
|
// (but _whitespace_ from ends should be trimmed first (and we do it right))
|
|
|
|
//TODO: "var==1" is mishandled (must use "=1" as a value, but uses "1")
|
2010-06-21 10:47:23 +05:30
|
|
|
// can it be fixed by removing PARSE_COLLAPSE bit?
|
2009-03-29 07:53:16 +05:30
|
|
|
while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
|
2009-05-11 03:11:29 +05:30
|
|
|
char *tp;
|
2009-03-29 07:53:16 +05:30
|
|
|
sysctl_dots_to_slashes(token[0]);
|
2009-05-11 03:11:29 +05:30
|
|
|
tp = xasprintf("%s=%s", token[0], token[1]);
|
|
|
|
sysctl_act_recursive(tp);
|
|
|
|
free(tp);
|
2008-10-15 13:52:55 +05:30
|
|
|
}
|
2009-03-29 07:53:16 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
config_close(parser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int sysctl_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
|
|
|
|
argv += optind;
|
|
|
|
opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
|
|
|
|
option_mask32 = opt;
|
|
|
|
|
|
|
|
if (opt & FLAG_PRELOAD_FILE) {
|
|
|
|
option_mask32 |= FLAG_WRITE;
|
|
|
|
/* xchdir("/proc/sys") is inside */
|
|
|
|
return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
|
|
|
|
}
|
|
|
|
xchdir("/proc/sys");
|
2012-03-08 04:58:24 +05:30
|
|
|
/* xchroot("/proc/sys") - if you are paranoid */
|
2009-03-29 07:53:16 +05:30
|
|
|
if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
|
|
|
|
return sysctl_act_recursive(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
while (*argv) {
|
|
|
|
sysctl_dots_to_slashes(*argv);
|
|
|
|
retval |= sysctl_act_recursive(*argv);
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
2008-12-24 08:41:43 +05:30
|
|
|
}
|