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