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
|
|
|
|
2007-11-29 06:40:28 +00:00
|
|
|
static int sysctl_read_setting(const char *setting);
|
|
|
|
static int sysctl_write_setting(const char *setting);
|
|
|
|
static int sysctl_display_all(const char *path);
|
|
|
|
static int sysctl_preload_file_and_exit(const char *filename);
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
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 06:02:11 +00:00
|
|
|
|
|
|
|
/* error messages */
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ERR_MALFORMED_SETTING[] ALIGN1 =
|
2007-10-29 19:22:13 +00:00
|
|
|
"error: malformed setting '%s'";
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ERR_NO_EQUALS[] ALIGN1 =
|
2007-10-29 19:22:13 +00:00
|
|
|
"error: '%s' must be of the form name=value";
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ERR_INVALID_KEY[] ALIGN1 =
|
2007-10-29 19:22:13 +00:00
|
|
|
"error: '%s' is an unknown key";
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ERR_UNKNOWN_WRITING[] ALIGN1 =
|
2007-10-29 19:22:13 +00:00
|
|
|
"error setting key '%s'";
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ERR_UNKNOWN_READING[] ALIGN1 =
|
2007-10-29 19:22:13 +00:00
|
|
|
"error reading key '%s'";
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ERR_PERMISSION_DENIED[] ALIGN1 =
|
2007-10-29 19:22:13 +00:00
|
|
|
"error: permission denied on key '%s'";
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char WARN_BAD_LINE[] ALIGN1 =
|
2007-10-29 19:22:13 +00:00
|
|
|
"warning: %s(%d): invalid syntax, continuing";
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
|
2003-12-26 02:19:34 +00:00
|
|
|
static void dwrite_str(int fd, const char *buf)
|
|
|
|
{
|
|
|
|
write(fd, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
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;
|
2003-12-24 06:02:11 +00:00
|
|
|
int sysctl_main(int argc, char **argv)
|
|
|
|
{
|
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);
|
|
|
|
option_mask32 ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
|
|
|
|
|
|
|
|
if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL))
|
|
|
|
return sysctl_display_all(PROC_SYS);
|
|
|
|
if (opt & FLAG_PRELOAD_FILE)
|
|
|
|
return sysctl_preload_file_and_exit(*argv ? *argv : ETC_SYSCTL_CONF);
|
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
while (*argv) {
|
|
|
|
if (opt & FLAG_WRITE)
|
|
|
|
retval |= sysctl_write_setting(*argv);
|
|
|
|
else
|
|
|
|
retval |= sysctl_read_setting(*argv);
|
|
|
|
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;
|
2007-10-29 19:22:13 +00:00
|
|
|
} /* end sysctl_main() */
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
/*
|
2007-10-29 19:22:13 +00:00
|
|
|
* preload the sysctl's from a conf file
|
|
|
|
* - we parse the file and then reform it (strip out whitespace)
|
2003-12-24 06:02:11 +00:00
|
|
|
*/
|
2003-12-26 02:19:34 +00:00
|
|
|
#define PRELOAD_BUF 256
|
|
|
|
|
2007-11-29 06:40:28 +00:00
|
|
|
static int sysctl_preload_file_and_exit(const char *filename)
|
2003-12-24 06:02:11 +00:00
|
|
|
{
|
2007-10-29 19:22:13 +00:00
|
|
|
int lineno;
|
2003-12-26 02:19:34 +00:00
|
|
|
char oneline[PRELOAD_BUF];
|
|
|
|
char buffer[PRELOAD_BUF];
|
2007-10-29 19:22:13 +00:00
|
|
|
char *name, *value;
|
|
|
|
FILE *fp;
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-11-29 06:40:28 +00:00
|
|
|
fp = xfopen(filename, "r");
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
lineno = 0;
|
2003-12-24 06:02:11 +00:00
|
|
|
while (fgets(oneline, sizeof(oneline) - 1, fp)) {
|
|
|
|
lineno++;
|
|
|
|
trim(oneline);
|
2007-10-29 19:22:13 +00:00
|
|
|
if (oneline[0] == '#' || oneline[0] == ';')
|
2003-12-24 06:02:11 +00:00
|
|
|
continue;
|
2007-10-29 19:22:13 +00:00
|
|
|
if (!oneline[0] || !oneline[1])
|
2003-12-24 06:02:11 +00:00
|
|
|
continue;
|
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
name = strtok(oneline, "=");
|
|
|
|
if (!name) {
|
2003-12-24 06:02:11 +00:00
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
trim(name);
|
2007-10-29 19:22:13 +00:00
|
|
|
if (!*name) {
|
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
value = strtok(NULL, "\n\r");
|
2007-10-29 19:22:13 +00:00
|
|
|
if (!value) {
|
2003-12-24 06:02:11 +00:00
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-29 19:22:13 +00:00
|
|
|
while (*value == ' ' || *value == '\t')
|
2003-12-24 06:02:11 +00:00
|
|
|
value++;
|
2007-10-29 19:22:13 +00:00
|
|
|
if (!*value) {
|
|
|
|
bb_error_msg(WARN_BAD_LINE, filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-09-09 12:24:19 +00:00
|
|
|
/* safe because sizeof(oneline) == sizeof(buffer) */
|
|
|
|
sprintf(buffer, "%s=%s", name, value);
|
2007-11-29 06:40:28 +00:00
|
|
|
sysctl_write_setting(buffer);
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
fclose(fp);
|
2003-12-24 06:02:11 +00:00
|
|
|
return 0;
|
2007-11-29 06:40:28 +00:00
|
|
|
} /* end sysctl_preload_file_and_exit() */
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a single sysctl setting
|
|
|
|
*/
|
2007-11-29 06:40:28 +00:00
|
|
|
static int sysctl_write_setting(const char *setting)
|
2003-12-24 06:02:11 +00:00
|
|
|
{
|
2007-11-29 06:40:28 +00:00
|
|
|
int retval;
|
2007-10-29 19:22:13 +00:00
|
|
|
const char *name;
|
2003-12-24 06:02:11 +00:00
|
|
|
const char *value;
|
|
|
|
const char *equals;
|
|
|
|
char *tmpname, *outname, *cptr;
|
2007-11-29 06:40:28 +00:00
|
|
|
int fd;
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
name = setting;
|
|
|
|
equals = strchr(setting, '=');
|
|
|
|
if (!equals) {
|
2003-12-24 06:02:11 +00:00
|
|
|
bb_error_msg(ERR_NO_EQUALS, setting);
|
2007-11-29 06:40:28 +00:00
|
|
|
return EXIT_FAILURE;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
value = equals + 1; /* point to the value in name=value */
|
|
|
|
if (name == equals || !*value) {
|
2003-12-24 06:02:11 +00:00
|
|
|
bb_error_msg(ERR_MALFORMED_SETTING, setting);
|
2007-11-29 06:40:28 +00:00
|
|
|
return EXIT_FAILURE;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name);
|
|
|
|
outname = xstrdup(tmpname + strlen_PROC_SYS);
|
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:
|
2007-11-29 06:40:28 +00:00
|
|
|
if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
|
|
|
|
bb_error_msg(ERR_INVALID_KEY, outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
bb_perror_msg(ERR_PERMISSION_DENIED, outname);
|
|
|
|
break;
|
|
|
|
default:
|
2007-10-29 19:22:13 +00:00
|
|
|
bb_perror_msg(ERR_UNKNOWN_WRITING, outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
retval = EXIT_FAILURE;
|
2003-12-24 06:02:11 +00:00
|
|
|
} else {
|
2003-12-26 02:19:34 +00:00
|
|
|
dwrite_str(fd, value);
|
2003-12-24 06:02:11 +00:00
|
|
|
close(fd);
|
2007-11-29 06:40:28 +00:00
|
|
|
if (option_mask32 & FLAG_SHOW_KEYS) {
|
|
|
|
printf("%s = ", outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
puts(value);
|
|
|
|
retval = EXIT_SUCCESS;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(tmpname);
|
|
|
|
free(outname);
|
|
|
|
return retval;
|
2007-10-29 19:22:13 +00:00
|
|
|
} /* end sysctl_write_setting() */
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
/*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Read a sysctl setting
|
2003-12-24 06:02:11 +00:00
|
|
|
*/
|
2007-11-29 06:40:28 +00:00
|
|
|
static int sysctl_read_setting(const char *name)
|
2003-12-24 06:02:11 +00:00
|
|
|
{
|
2007-11-29 06:40:28 +00:00
|
|
|
int retval;
|
2003-12-24 06:02:11 +00:00
|
|
|
char *tmpname, *outname, *cptr;
|
|
|
|
char inbuf[1025];
|
|
|
|
FILE *fp;
|
|
|
|
|
2007-11-29 06:40:28 +00:00
|
|
|
if (!*name) {
|
|
|
|
if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
|
|
|
|
bb_error_msg(ERR_INVALID_KEY, name);
|
|
|
|
return -1;
|
|
|
|
}
|
2003-12-24 06:02:11 +00:00
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
tmpname = concat_path_file(PROC_SYS, name);
|
|
|
|
outname = xstrdup(tmpname + strlen_PROC_SYS);
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
while ((cptr = strchr(tmpname, '.')) != NULL)
|
|
|
|
*cptr = '/';
|
|
|
|
while ((cptr = strchr(outname, '/')) != NULL)
|
|
|
|
*cptr = '.';
|
|
|
|
|
2007-10-29 19:22:13 +00:00
|
|
|
fp = fopen(tmpname, "r");
|
|
|
|
if (fp == NULL) {
|
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)
|
|
|
|
bb_error_msg(ERR_INVALID_KEY, outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
bb_error_msg(ERR_PERMISSION_DENIED, outname);
|
|
|
|
break;
|
|
|
|
default:
|
2007-10-29 19:22:13 +00:00
|
|
|
bb_perror_msg(ERR_UNKNOWN_READING, outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
retval = EXIT_FAILURE;
|
2003-12-24 06:02:11 +00:00
|
|
|
} else {
|
|
|
|
while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
|
2007-11-29 06:40:28 +00:00
|
|
|
if (option_mask32 & FLAG_SHOW_KEYS) {
|
|
|
|
printf("%s = ", outname);
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2007-11-29 06:40:28 +00:00
|
|
|
fputs(inbuf, stdout);
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
|
|
|
fclose(fp);
|
2007-11-29 06:40:28 +00:00
|
|
|
retval = EXIT_SUCCESS;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(tmpname);
|
|
|
|
free(outname);
|
|
|
|
return retval;
|
2007-10-29 19:22:13 +00:00
|
|
|
} /* end sysctl_read_setting() */
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
/*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Display all the sysctl settings
|
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
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *de;
|
|
|
|
char *tmpdir;
|
|
|
|
struct stat ts;
|
|
|
|
|
2007-04-12 00:32:05 +00:00
|
|
|
dp = opendir(path);
|
|
|
|
if (!dp) {
|
2007-11-29 06:40:28 +00:00
|
|
|
return EXIT_FAILURE;
|
2003-12-24 06:02:11 +00:00
|
|
|
}
|
2007-10-29 19:22:13 +00:00
|
|
|
while ((de = readdir(dp)) != NULL) {
|
|
|
|
tmpdir = concat_subpath_file(path, de->d_name);
|
|
|
|
if (tmpdir == NULL)
|
2007-11-29 06:40:28 +00:00
|
|
|
continue; /* . or .. */
|
2007-10-29 19:22:13 +00:00
|
|
|
if (stat(tmpdir, &ts) != 0) {
|
|
|
|
bb_perror_msg(tmpdir);
|
|
|
|
} else if (S_ISDIR(ts.st_mode)) {
|
2007-11-29 06:40:28 +00:00
|
|
|
retval |= sysctl_display_all(tmpdir);
|
2007-10-29 19:22:13 +00:00
|
|
|
} else {
|
2007-11-29 06:40:28 +00:00
|
|
|
retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS);
|
2007-10-29 19:22:13 +00:00
|
|
|
}
|
|
|
|
free(tmpdir);
|
|
|
|
} /* end while */
|
|
|
|
closedir(dp);
|
2003-12-24 06:02:11 +00:00
|
|
|
|
|
|
|
return retval;
|
2007-10-29 19:22:13 +00:00
|
|
|
} /* end sysctl_display_all() */
|