2008-10-15 09:44:37 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2003-12-24 06:02:11 +00:00
|
|
|
/*
|
|
|
|
* Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
|
|
|
|
*
|
2006-04-17 21:49:34 +00:00
|
|
|
* Copyright 1999 George Staikos
|
2006-04-12 07:35:12 +00:00
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2003-12-24 06:02:11 +00:00
|
|
|
*
|
|
|
|
* Changelog:
|
|
|
|
* v1.01:
|
|
|
|
* - added -p <preload> to preload values from a file
|
|
|
|
* v1.01.1
|
|
|
|
* - busybox applet aware by <solar@gentoo.org>
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2003-12-24 06:02:11 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
static int sysctl_act_on_setting(char *setting);
|
2007-11-29 06:40:28 +00:00
|
|
|
static int sysctl_display_all(const char *path);
|
2008-12-24 03:11:43 +00:00
|
|
|
static int sysctl_handle_preload_file(const char *filename);
|
2008-10-15 08:22:55 +00:00
|
|
|
static void sysctl_dots_to_slashes(char *name);
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-11-29 06:40:28 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int sysctl_main(int argc UNUSED_PARAM, char **argv)
|
2003-12-24 06:02:11 +00:00
|
|
|
{
|
2007-11-29 06:40:28 +00:00
|
|
|
int retval;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
|
|
|
|
argv += optind;
|
|
|
|
opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
|
2008-12-24 03:11:43 +00:00
|
|
|
option_mask32 = opt;
|
2007-11-29 06:40:28 +00:00
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
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");
|
|
|
|
/* xchroot(".") - if you are paranoid */
|
|
|
|
if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
|
|
|
|
return sysctl_display_all(".");
|
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
while (*argv) {
|
2008-12-24 03:11:43 +00:00
|
|
|
sysctl_dots_to_slashes(*argv);
|
|
|
|
retval |= sysctl_display_all(*argv);
|
2007-11-29 06:40:28 +00:00
|
|
|
argv++;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
|
2003-12-24 06:02:11 +00:00
|
|
|
return retval;
|
2008-12-24 03:11:43 +00:00
|
|
|
}
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2008-10-15 08:45:54 +00:00
|
|
|
/* Set sysctl's from a conf file. Format example:
|
|
|
|
* # Controls IP packet forwarding
|
|
|
|
* net.ipv4.ip_forward = 0
|
2003-12-24 06:02:11 +00:00
|
|
|
*/
|
2008-12-24 03:11:43 +00:00
|
|
|
static int sysctl_handle_preload_file(const char *filename)
|
2003-12-24 06:02:11 +00:00
|
|
|
{
|
2008-07-20 13:01:56 +00:00
|
|
|
char *token[2];
|
|
|
|
parser_t *parser;
|
|
|
|
|
|
|
|
parser = config_open(filename);
|
2008-12-24 03:11:43 +00:00
|
|
|
/* Must do it _after_ config_open(): */
|
|
|
|
xchdir("/proc/sys");
|
|
|
|
/* xchroot(".") - if you are paranoid */
|
|
|
|
|
2008-10-15 08:45:54 +00:00
|
|
|
// TODO: ';' is comment char too
|
|
|
|
while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
|
2008-12-24 03:11:43 +00:00
|
|
|
/* Save ~4 bytes by using parser internals */
|
|
|
|
/* parser->line is big enough for sprintf */
|
|
|
|
sprintf(parser->line, "%s=%s", token[0], token[1]);
|
|
|
|
sysctl_dots_to_slashes(parser->line);
|
|
|
|
sysctl_display_all(parser->line);
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
2008-07-20 13:01:56 +00:00
|
|
|
config_close(parser);
|
2003-12-24 06:02:11 +00:00
|
|
|
return 0;
|
2008-12-24 03:11:43 +00:00
|
|
|
}
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
static int sysctl_act_on_setting(char *setting)
|
2003-12-24 06:02:11 +00:00
|
|
|
{
|
2008-12-24 03:11:43 +00:00
|
|
|
int fd, retval = EXIT_SUCCESS;
|
2008-12-29 01:03:17 +00:00
|
|
|
char *cptr, *outname;
|
|
|
|
char *value = value; /* for compiler */
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
outname = xstrdup(setting);
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
cptr = outname;
|
|
|
|
while (*cptr) {
|
|
|
|
if (*cptr == '/')
|
|
|
|
*cptr = '.';
|
|
|
|
cptr++;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
value = cptr + 1; /* point to the value in name=value */
|
|
|
|
if (setting == cptr || !*value) {
|
|
|
|
bb_error_msg("error: malformed setting '%s'", outname);
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
*cptr = '\0';
|
|
|
|
fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
|
|
|
} else {
|
|
|
|
fd = open(setting, O_RDONLY);
|
|
|
|
}
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-04-11 23:20:53 +00:00
|
|
|
if (fd < 0) {
|
2003-12-24 06:02:11 +00:00
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2007-11-29 06:40:28 +00:00
|
|
|
if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
|
2008-12-24 03:11:43 +00:00
|
|
|
bb_error_msg("error: '%s' is an unknown key", outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
2008-12-24 03:11:43 +00:00
|
|
|
bb_perror_msg("error %sing key '%s'",
|
|
|
|
option_mask32 & FLAG_WRITE ?
|
|
|
|
"sett" : "read",
|
|
|
|
outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
retval = EXIT_FAILURE;
|
2008-12-24 03:11:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option_mask32 & FLAG_WRITE) {
|
2009-03-09 00:12:37 +00:00
|
|
|
xwrite_str(fd, value);
|
2003-12-24 06:02:11 +00:00
|
|
|
close(fd);
|
2008-12-24 03:11:43 +00:00
|
|
|
if (option_mask32 & FLAG_SHOW_KEYS)
|
2007-11-29 06:40:28 +00:00
|
|
|
printf("%s = ", outname);
|
|
|
|
puts(value);
|
2008-12-24 03:11:43 +00:00
|
|
|
} else {
|
|
|
|
char c;
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
value = cptr = xmalloc_read(fd, NULL);
|
|
|
|
close(fd);
|
|
|
|
if (value == NULL) {
|
2008-10-15 08:29:17 +00:00
|
|
|
bb_perror_msg("error reading key '%s'", outname);
|
2008-12-24 03:11:43 +00:00
|
|
|
goto end;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2008-12-24 03:11:43 +00:00
|
|
|
|
|
|
|
/* 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 06:40:28 +00:00
|
|
|
printf("%s = ", outname);
|
2008-12-24 03:11:43 +00:00
|
|
|
while (1) {
|
|
|
|
fputc(c, stdout);
|
|
|
|
cptr++;
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
c = *cptr;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-24 03:11:43 +00:00
|
|
|
free(value);
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2008-12-24 03:11:43 +00:00
|
|
|
end:
|
2003-12-24 06:02:11 +00:00
|
|
|
free(outname);
|
|
|
|
return retval;
|
2008-12-24 03:11:43 +00:00
|
|
|
}
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-11-29 06:40:28 +00:00
|
|
|
static int sysctl_display_all(const char *path)
|
2003-12-24 06:02:11 +00:00
|
|
|
{
|
2008-12-24 03:11:43 +00:00
|
|
|
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) {
|
|
|
|
next = concat_subpath_file(
|
|
|
|
path, entry->d_name);
|
|
|
|
if (next == NULL)
|
|
|
|
continue; /* d_name is "." or ".." */
|
|
|
|
/* if path was ".", drop "./" prefix: */
|
|
|
|
retval |= sysctl_display_all((next[0] == '.' && next[1] == '/') ?
|
|
|
|
next + 2 : next);
|
|
|
|
free(next);
|
2007-10-29 19:22:13 +00:00
|
|
|
}
|
2008-12-24 03:11:43 +00:00
|
|
|
closedir(dirp);
|
|
|
|
} else {
|
|
|
|
char *name = xstrdup(path);
|
|
|
|
retval |= sysctl_act_on_setting(name);
|
|
|
|
free(name);
|
|
|
|
}
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
return retval;
|
2008-12-24 03:11:43 +00:00
|
|
|
}
|
2008-10-15 08:22:55 +00:00
|
|
|
|
|
|
|
static void sysctl_dots_to_slashes(char *name)
|
|
|
|
{
|
2008-10-15 13:50:24 +00:00
|
|
|
char *cptr, *last_good, *end;
|
|
|
|
|
2008-12-24 03:11:43 +00:00
|
|
|
/* Convert minimum number of '.' to '/' so that
|
|
|
|
* we end up with existing file's name.
|
|
|
|
*
|
|
|
|
* Example from bug 3894:
|
2008-10-15 08:22:55 +00:00
|
|
|
* net.ipv4.conf.eth0.100.mc_forwarding ->
|
2008-12-24 03:11:43 +00:00
|
|
|
* net/ipv4/conf/eth0.100/mc_forwarding
|
|
|
|
* NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
|
2008-10-15 09:43:35 +00:00
|
|
|
* therefore we must start from the end, and if
|
|
|
|
* we replaced even one . -> /, start over again,
|
|
|
|
* but never replace dots before the position
|
2008-12-24 03:11:43 +00:00
|
|
|
* where last replacement occurred.
|
2009-01-08 03:35:47 +00:00
|
|
|
*
|
|
|
|
* 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
|
2008-12-24 03:11:43 +00:00
|
|
|
*/
|
|
|
|
end = name + strlen(name);
|
2008-10-15 09:43:35 +00:00
|
|
|
last_good = name - 1;
|
2008-12-29 01:03:17 +00:00
|
|
|
*end = '.'; /* trick the loop into trying full name too */
|
2008-12-24 03:11:43 +00:00
|
|
|
|
2008-10-15 09:43:35 +00:00
|
|
|
again:
|
|
|
|
cptr = end;
|
|
|
|
while (cptr > last_good) {
|
2008-10-15 08:22:55 +00:00
|
|
|
if (*cptr == '.') {
|
|
|
|
*cptr = '\0';
|
2008-12-24 03:11:43 +00:00
|
|
|
//bb_error_msg("trying:'%s'", name);
|
2008-10-15 09:43:35 +00:00
|
|
|
if (access(name, F_OK) == 0) {
|
2009-01-08 03:35:47 +00:00
|
|
|
if (cptr != end) /* prevent trailing '/' */
|
|
|
|
*cptr = '/';
|
2008-12-24 03:11:43 +00:00
|
|
|
//bb_error_msg("replaced:'%s'", name);
|
2008-10-15 09:43:35 +00:00
|
|
|
last_good = cptr;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
*cptr = '.';
|
2008-10-15 08:22:55 +00:00
|
|
|
}
|
2008-10-15 09:43:35 +00:00
|
|
|
cptr--;
|
2008-10-15 08:22:55 +00:00
|
|
|
}
|
2008-12-24 03:11:43 +00:00
|
|
|
*end = '\0';
|
|
|
|
}
|