2006-06-03 02:26:16 +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
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2003-12-24 11:32:11 +05:30
|
|
|
*
|
|
|
|
* 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 13:59:22 +05:30
|
|
|
*
|
2003-12-24 11:32:11 +05:30
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Function Prototypes
|
|
|
|
*/
|
|
|
|
static int sysctl_read_setting(const char *setting, int output);
|
|
|
|
static int sysctl_write_setting(const char *setting, int output);
|
|
|
|
static int sysctl_preload_file(const char *filename, int output);
|
|
|
|
static int sysctl_display_all(const char *path, int output, int show_table);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals...
|
|
|
|
*/
|
2007-10-30 00:52:13 +05:30
|
|
|
static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf";
|
|
|
|
static const char PROC_SYS[] ALIGN1 = "/proc/sys/";
|
|
|
|
enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 };
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
/* error messages */
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_UNKNOWN_PARAMETER[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error: unknown parameter '%s'";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_MALFORMED_SETTING[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error: malformed setting '%s'";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_NO_EQUALS[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error: '%s' must be of the form name=value";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_INVALID_KEY[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error: '%s' is an unknown key";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_UNKNOWN_WRITING[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error setting key '%s'";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_UNKNOWN_READING[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error reading key '%s'";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_PERMISSION_DENIED[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error: permission denied on key '%s'";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ERR_PRELOAD_FILE[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"error: cannot open preload file '%s'";
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char WARN_BAD_LINE[] ALIGN1 =
|
2007-10-30 00:52:13 +05:30
|
|
|
"warning: %s(%d): invalid syntax, continuing";
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
|
2003-12-26 07:49:34 +05:30
|
|
|
static void dwrite_str(int fd, const char *buf)
|
|
|
|
{
|
|
|
|
write(fd, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
2003-12-24 11:32:11 +05:30
|
|
|
/*
|
2004-03-15 13:59:22 +05:30
|
|
|
* sysctl_main()...
|
2003-12-24 11:32:11 +05:30
|
|
|
*/
|
2007-10-11 15:35:36 +05:30
|
|
|
int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2003-12-24 11:32:11 +05:30
|
|
|
int sysctl_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
int output = 1;
|
|
|
|
int write_mode = 0;
|
|
|
|
int switches_allowed = 1;
|
|
|
|
|
|
|
|
if (argc < 2)
|
2003-12-26 07:49:34 +05:30
|
|
|
bb_show_usage();
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
argv++;
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
for (; *argv /*&& **argv*/; argv++) {
|
2003-12-24 11:32:11 +05:30
|
|
|
if (switches_allowed && **argv == '-') { /* we have a switch */
|
|
|
|
switch ((*argv)[1]) {
|
|
|
|
case 'n':
|
|
|
|
output = 0;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
write_mode = 1;
|
|
|
|
switches_allowed = 0;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
argv++;
|
2007-10-30 00:52:13 +05:30
|
|
|
return sysctl_preload_file(((*argv /*&& **argv*/) ? *argv : ETC_SYSCTL_CONF),
|
|
|
|
output);
|
2003-12-24 11:32:11 +05:30
|
|
|
case 'a':
|
|
|
|
case 'A':
|
2007-10-30 00:52:13 +05:30
|
|
|
return sysctl_display_all(PROC_SYS, output,
|
|
|
|
((*argv)[1] == 'A'));
|
2003-12-24 11:32:11 +05:30
|
|
|
default:
|
|
|
|
bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv);
|
2007-10-30 00:52:13 +05:30
|
|
|
/* fall through */
|
|
|
|
//case 'h':
|
|
|
|
//case '?':
|
2003-12-26 07:49:34 +05:30
|
|
|
bb_show_usage();
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switches_allowed = 0;
|
|
|
|
if (write_mode)
|
2007-10-30 00:52:13 +05:30
|
|
|
retval |= sysctl_write_setting(*argv, output);
|
2003-12-24 11:32:11 +05:30
|
|
|
else
|
|
|
|
sysctl_read_setting(*argv, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
2007-10-30 00:52:13 +05:30
|
|
|
} /* end sysctl_main() */
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-30 00:52:13 +05:30
|
|
|
* sysctl_preload_file
|
|
|
|
* preload the sysctl's from a conf file
|
|
|
|
* - we parse the file and then reform it (strip out whitespace)
|
2003-12-24 11:32:11 +05:30
|
|
|
*/
|
2003-12-26 07:49:34 +05:30
|
|
|
#define PRELOAD_BUF 256
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
static int sysctl_preload_file(const char *filename, int output)
|
2003-12-24 11:32:11 +05:30
|
|
|
{
|
2007-10-30 00:52:13 +05:30
|
|
|
int lineno;
|
2003-12-26 07:49:34 +05:30
|
|
|
char oneline[PRELOAD_BUF];
|
|
|
|
char buffer[PRELOAD_BUF];
|
2007-10-30 00:52:13 +05:30
|
|
|
char *name, *value;
|
|
|
|
FILE *fp;
|
2003-12-24 11:32:11 +05:30
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
fp = fopen(filename, "r");
|
|
|
|
if (fp == NULL) {
|
2003-12-26 07:49:34 +05:30
|
|
|
bb_error_msg_and_die(ERR_PRELOAD_FILE, filename);
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
lineno = 0;
|
2003-12-24 11:32:11 +05:30
|
|
|
while (fgets(oneline, sizeof(oneline) - 1, fp)) {
|
|
|
|
lineno++;
|
|
|
|
trim(oneline);
|
2007-10-30 00:52:13 +05:30
|
|
|
if (oneline[0] == '#' || oneline[0] == ';')
|
2003-12-24 11:32:11 +05:30
|
|
|
continue;
|
2007-10-30 00:52:13 +05:30
|
|
|
if (!oneline[0] || !oneline[1])
|
2003-12-24 11:32:11 +05:30
|
|
|
continue;
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
name = strtok(oneline, "=");
|
|
|
|
if (!name) {
|
2003-12-24 11:32:11 +05:30
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
trim(name);
|
2007-10-30 00:52:13 +05:30
|
|
|
if (!*name) {
|
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
value = strtok(NULL, "\n\r");
|
2007-10-30 00:52:13 +05:30
|
|
|
if (!value) {
|
2003-12-24 11:32:11 +05:30
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-30 00:52:13 +05:30
|
|
|
while (*value == ' ' || *value == '\t')
|
2003-12-24 11:32:11 +05:30
|
|
|
value++;
|
2007-10-30 00:52:13 +05:30
|
|
|
if (!*value) {
|
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-09-09 17:54:19 +05:30
|
|
|
/* safe because sizeof(oneline) == sizeof(buffer) */
|
|
|
|
sprintf(buffer, "%s=%s", name, value);
|
2003-12-24 11:32:11 +05:30
|
|
|
sysctl_write_setting(buffer, output);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2007-10-30 00:52:13 +05:30
|
|
|
} /* end sysctl_preload_file() */
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a single sysctl setting
|
|
|
|
*/
|
2007-10-30 00:52:13 +05:30
|
|
|
static int sysctl_write_setting(const char *setting, int output)
|
2003-12-24 11:32:11 +05:30
|
|
|
{
|
|
|
|
int retval = 0;
|
2007-10-30 00:52:13 +05:30
|
|
|
const char *name;
|
2003-12-24 11:32:11 +05:30
|
|
|
const char *value;
|
|
|
|
const char *equals;
|
|
|
|
char *tmpname, *outname, *cptr;
|
|
|
|
int fd = -1;
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
name = setting;
|
|
|
|
equals = strchr(setting, '=');
|
|
|
|
if (!equals) {
|
2003-12-24 11:32:11 +05:30
|
|
|
bb_error_msg(ERR_NO_EQUALS, setting);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
value = equals + 1; /* point to the value in name=value */
|
|
|
|
if (name == equals || !*value) {
|
2003-12-24 11:32:11 +05:30
|
|
|
bb_error_msg(ERR_MALFORMED_SETTING, setting);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
|
|
|
|
outname = xstrdup(tmpname + strlen_PROC_SYS);
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
while ((cptr = strchr(tmpname, '.')) != NULL)
|
|
|
|
*cptr = '/';
|
|
|
|
|
|
|
|
while ((cptr = strchr(outname, '/')) != NULL)
|
|
|
|
*cptr = '.';
|
|
|
|
|
2007-04-12 04:50:53 +05:30
|
|
|
fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
|
|
if (fd < 0) {
|
2003-12-24 11:32:11 +05:30
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
bb_error_msg(ERR_INVALID_KEY, outname);
|
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
bb_perror_msg(ERR_PERMISSION_DENIED, outname);
|
|
|
|
break;
|
|
|
|
default:
|
2007-10-30 00:52:13 +05:30
|
|
|
bb_perror_msg(ERR_UNKNOWN_WRITING, outname);
|
2003-12-24 11:32:11 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
retval = -1;
|
|
|
|
} else {
|
2003-12-26 07:49:34 +05:30
|
|
|
dwrite_str(fd, value);
|
2003-12-24 11:32:11 +05:30
|
|
|
close(fd);
|
|
|
|
if (output) {
|
2003-12-26 07:49:34 +05:30
|
|
|
dwrite_str(STDOUT_FILENO, outname);
|
|
|
|
dwrite_str(STDOUT_FILENO, " = ");
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
2003-12-26 07:49:34 +05:30
|
|
|
dwrite_str(STDOUT_FILENO, value);
|
|
|
|
dwrite_str(STDOUT_FILENO, "\n");
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
free(tmpname);
|
|
|
|
free(outname);
|
|
|
|
return retval;
|
2007-10-30 00:52:13 +05:30
|
|
|
} /* end sysctl_write_setting() */
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
/*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Read a sysctl setting
|
2003-12-24 11:32:11 +05:30
|
|
|
*/
|
2007-10-30 00:52:13 +05:30
|
|
|
static int sysctl_read_setting(const char *setting, int output)
|
2003-12-24 11:32:11 +05:30
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
char *tmpname, *outname, *cptr;
|
|
|
|
char inbuf[1025];
|
2007-10-30 00:52:13 +05:30
|
|
|
const char *name;
|
2003-12-24 11:32:11 +05:30
|
|
|
FILE *fp;
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
if (!*setting)
|
2003-12-24 11:32:11 +05:30
|
|
|
bb_error_msg(ERR_INVALID_KEY, setting);
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
name = setting;
|
|
|
|
tmpname = concat_path_file(PROC_SYS, name);
|
|
|
|
outname = xstrdup(tmpname + strlen_PROC_SYS);
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
while ((cptr = strchr(tmpname, '.')) != NULL)
|
|
|
|
*cptr = '/';
|
|
|
|
while ((cptr = strchr(outname, '/')) != NULL)
|
|
|
|
*cptr = '.';
|
|
|
|
|
2007-10-30 00:52:13 +05:30
|
|
|
fp = fopen(tmpname, "r");
|
|
|
|
if (fp == NULL) {
|
2003-12-24 11:32:11 +05:30
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
bb_error_msg(ERR_INVALID_KEY, outname);
|
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
bb_error_msg(ERR_PERMISSION_DENIED, outname);
|
|
|
|
break;
|
|
|
|
default:
|
2007-10-30 00:52:13 +05:30
|
|
|
bb_perror_msg(ERR_UNKNOWN_READING, outname);
|
2003-12-24 11:32:11 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
retval = -1;
|
|
|
|
} else {
|
|
|
|
while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
|
|
|
|
if (output) {
|
2003-12-26 07:49:34 +05:30
|
|
|
dwrite_str(STDOUT_FILENO, outname);
|
|
|
|
dwrite_str(STDOUT_FILENO, " = ");
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
2003-12-26 07:49:34 +05:30
|
|
|
dwrite_str(STDOUT_FILENO, inbuf);
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(tmpname);
|
|
|
|
free(outname);
|
|
|
|
return retval;
|
2007-10-30 00:52:13 +05:30
|
|
|
} /* end sysctl_read_setting() */
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
/*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Display all the sysctl settings
|
2003-12-24 11:32:11 +05:30
|
|
|
*/
|
2007-10-30 00:52:13 +05:30
|
|
|
static int sysctl_display_all(const char *path, int output, int show_table)
|
2003-12-24 11:32:11 +05:30
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *de;
|
|
|
|
char *tmpdir;
|
|
|
|
struct stat ts;
|
|
|
|
|
2007-04-12 06:02:05 +05:30
|
|
|
dp = opendir(path);
|
|
|
|
if (!dp) {
|
2007-10-30 00:52:13 +05:30
|
|
|
return -1;
|
2003-12-24 11:32:11 +05:30
|
|
|
}
|
2007-10-30 00:52:13 +05:30
|
|
|
while ((de = readdir(dp)) != NULL) {
|
|
|
|
tmpdir = concat_subpath_file(path, de->d_name);
|
|
|
|
if (tmpdir == NULL)
|
|
|
|
continue;
|
|
|
|
if (stat(tmpdir, &ts) != 0) {
|
|
|
|
bb_perror_msg(tmpdir);
|
|
|
|
} else if (S_ISDIR(ts.st_mode)) {
|
|
|
|
sysctl_display_all(tmpdir, output, show_table);
|
|
|
|
} else {
|
|
|
|
retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS, output);
|
|
|
|
}
|
|
|
|
free(tmpdir);
|
|
|
|
} /* end while */
|
|
|
|
closedir(dp);
|
2003-12-24 11:32:11 +05:30
|
|
|
|
|
|
|
return retval;
|
2007-10-30 00:52:13 +05:30
|
|
|
} /* end sysctl_display_all() */
|