2014-04-17 14:18:38 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-17 15:38:31 +00:00
|
|
|
static void
|
|
|
|
streplace(char *s, int a, int b)
|
|
|
|
{
|
2014-04-17 16:15:36 +00:00
|
|
|
for (; *s; s++)
|
|
|
|
if (*s == a)
|
|
|
|
*s = b;
|
2014-04-17 15:38:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 14:18:38 +00:00
|
|
|
static int
|
|
|
|
getsysctl(char *variable, char **value)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char *p;
|
2014-04-17 15:22:58 +00:00
|
|
|
char *buf, *tmp, c;
|
2014-04-17 14:18:38 +00:00
|
|
|
int fd;
|
|
|
|
ssize_t n;
|
2014-04-17 15:13:34 +00:00
|
|
|
size_t sz, i;
|
2014-04-17 14:18:38 +00:00
|
|
|
|
2014-04-17 15:38:31 +00:00
|
|
|
streplace(variable, '.', '/');
|
2014-04-17 14:18:38 +00:00
|
|
|
|
|
|
|
strlcpy(path, "/proc/sys/", sizeof(path));
|
2014-04-17 15:38:31 +00:00
|
|
|
if (strlcat(path, variable, sizeof(path)) >= sizeof(path)) {
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 14:18:38 +00:00
|
|
|
return -1;
|
2014-04-17 15:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 14:18:38 +00:00
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-04-17 15:13:34 +00:00
|
|
|
i = 0;
|
|
|
|
sz = 1;
|
|
|
|
buf = NULL;
|
|
|
|
while (1) {
|
|
|
|
n = read(fd, &c, 1);
|
|
|
|
if (n < 0) {
|
|
|
|
close(fd);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
if (i == sz - 1) {
|
|
|
|
sz *= 2;
|
2014-04-17 15:22:58 +00:00
|
|
|
tmp = realloc(buf, sz);
|
|
|
|
if (!tmp) {
|
2014-04-17 15:13:34 +00:00
|
|
|
close(fd);
|
2014-04-17 15:22:58 +00:00
|
|
|
free(buf);
|
2014-04-17 15:13:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-04-17 15:22:58 +00:00
|
|
|
buf = tmp;
|
2014-04-17 15:13:34 +00:00
|
|
|
}
|
|
|
|
buf[i++] = c;
|
2014-04-17 14:18:38 +00:00
|
|
|
}
|
2014-04-17 15:13:34 +00:00
|
|
|
buf[i] = '\0';
|
2014-04-17 14:18:38 +00:00
|
|
|
|
|
|
|
p = strrchr(buf, '\n');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
*value = buf;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
setsysctl(char *variable, char *value)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
int fd;
|
|
|
|
ssize_t n;
|
|
|
|
|
2014-04-17 18:05:25 +00:00
|
|
|
streplace(variable, '.', '/');
|
2014-04-17 14:18:38 +00:00
|
|
|
|
|
|
|
strlcpy(path, "/proc/sys/", sizeof(path));
|
2014-04-17 18:05:25 +00:00
|
|
|
if (strlcat(path, variable, sizeof(path)) >= sizeof(path)) {
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 14:18:38 +00:00
|
|
|
return -1;
|
2014-04-17 18:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 14:18:38 +00:00
|
|
|
|
|
|
|
fd = open(path, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n = write(fd, value, strlen(value));
|
|
|
|
if (n != (ssize_t)strlen(value)) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s variable[=value]...\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *variable;
|
|
|
|
char *value;
|
|
|
|
char *p;
|
|
|
|
int i;
|
2014-04-17 16:05:14 +00:00
|
|
|
int r = EXIT_SUCCESS;
|
2014-04-17 14:18:38 +00:00
|
|
|
|
|
|
|
argv0 = argv[0];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
2014-04-17 16:01:57 +00:00
|
|
|
for (p = argv[i]; *p; p++) {
|
|
|
|
if (p[0] == '.' && p[1] == '.') {
|
2014-04-17 16:05:14 +00:00
|
|
|
r = EXIT_FAILURE;
|
2014-04-17 16:01:57 +00:00
|
|
|
weprintf("malformed input: %s\n", argv[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p != '\0')
|
|
|
|
continue;
|
|
|
|
p = strchr(argv[i], '=');
|
2014-04-17 14:18:38 +00:00
|
|
|
if (p) {
|
|
|
|
if (p[1] == '\0') {
|
2014-04-17 16:05:14 +00:00
|
|
|
r = EXIT_FAILURE;
|
2014-04-17 16:01:57 +00:00
|
|
|
weprintf("malformed input: %s\n", argv[i]);
|
2014-04-17 14:18:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
value = &p[1];
|
2014-04-17 16:01:57 +00:00
|
|
|
} else {
|
|
|
|
value = NULL;
|
2014-04-17 14:18:38 +00:00
|
|
|
}
|
2014-04-17 16:01:57 +00:00
|
|
|
variable = argv[i];
|
|
|
|
|
2014-04-17 14:18:38 +00:00
|
|
|
if (variable) {
|
|
|
|
if (value) {
|
|
|
|
if (setsysctl(variable, value) < 0) {
|
2014-04-17 16:05:14 +00:00
|
|
|
r = EXIT_FAILURE;
|
2014-04-17 14:18:38 +00:00
|
|
|
weprintf("failed to set sysctl for %s\n", variable);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (getsysctl(variable, &value) < 0) {
|
2014-04-17 16:05:14 +00:00
|
|
|
r = EXIT_FAILURE;
|
2014-04-17 14:18:38 +00:00
|
|
|
weprintf("failed to get sysctl for %s\n", variable);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
printf("%s = %s\n", variable, value);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 16:05:14 +00:00
|
|
|
return r;
|
2014-04-17 14:18:38 +00:00
|
|
|
}
|