2014-04-17 10:18:38 -04: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 11:38:31 -04:00
|
|
|
static void
|
|
|
|
streplace(char *s, int a, int b)
|
|
|
|
{
|
2014-04-17 12:15:36 -04:00
|
|
|
for (; *s; s++)
|
|
|
|
if (*s == a)
|
|
|
|
*s = b;
|
2014-04-17 11:38:31 -04:00
|
|
|
}
|
|
|
|
|
2014-04-17 10:18:38 -04:00
|
|
|
static int
|
|
|
|
getsysctl(char *variable, char **value)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char *p;
|
2014-04-17 11:22:58 -04:00
|
|
|
char *buf, *tmp, c;
|
2014-04-17 10:18:38 -04:00
|
|
|
int fd;
|
|
|
|
ssize_t n;
|
2014-04-17 11:13:34 -04:00
|
|
|
size_t sz, i;
|
2014-04-17 10:18:38 -04:00
|
|
|
|
2014-04-17 11:38:31 -04:00
|
|
|
streplace(variable, '.', '/');
|
2014-04-17 10:18:38 -04:00
|
|
|
|
|
|
|
strlcpy(path, "/proc/sys/", sizeof(path));
|
2014-04-17 11:38:31 -04:00
|
|
|
if (strlcat(path, variable, sizeof(path)) >= sizeof(path)) {
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 10:18:38 -04:00
|
|
|
return -1;
|
2014-04-17 11:38:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 10:18:38 -04:00
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-04-17 11:13:34 -04: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 11:22:58 -04:00
|
|
|
tmp = realloc(buf, sz);
|
|
|
|
if (!tmp) {
|
2014-04-17 11:13:34 -04:00
|
|
|
close(fd);
|
2014-04-17 11:22:58 -04:00
|
|
|
free(buf);
|
2014-04-17 11:13:34 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-04-17 11:22:58 -04:00
|
|
|
buf = tmp;
|
2014-04-17 11:13:34 -04:00
|
|
|
}
|
|
|
|
buf[i++] = c;
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
2014-04-17 11:13:34 -04:00
|
|
|
buf[i] = '\0';
|
2014-04-17 10:18:38 -04: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 14:05:25 -04:00
|
|
|
streplace(variable, '.', '/');
|
2014-04-17 10:18:38 -04:00
|
|
|
|
|
|
|
strlcpy(path, "/proc/sys/", sizeof(path));
|
2014-04-17 14:05:25 -04:00
|
|
|
if (strlcat(path, variable, sizeof(path)) >= sizeof(path)) {
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 10:18:38 -04:00
|
|
|
return -1;
|
2014-04-17 14:05:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
streplace(variable, '/', '.');
|
2014-04-17 10:18:38 -04:00
|
|
|
|
|
|
|
fd = open(path, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n = write(fd, value, strlen(value));
|
2014-04-18 04:39:19 -04:00
|
|
|
if ((size_t)n != strlen(value)) {
|
2014-04-17 10:18:38 -04:00
|
|
|
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 12:05:14 -04:00
|
|
|
int r = EXIT_SUCCESS;
|
2014-04-17 10:18:38 -04:00
|
|
|
|
|
|
|
argv0 = argv[0];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
2014-04-17 12:01:57 -04:00
|
|
|
for (p = argv[i]; *p; p++) {
|
|
|
|
if (p[0] == '.' && p[1] == '.') {
|
2014-04-17 12:05:14 -04:00
|
|
|
r = EXIT_FAILURE;
|
2014-04-17 12:01:57 -04:00
|
|
|
weprintf("malformed input: %s\n", argv[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p != '\0')
|
|
|
|
continue;
|
|
|
|
p = strchr(argv[i], '=');
|
2014-04-17 10:18:38 -04:00
|
|
|
if (p) {
|
|
|
|
if (p[1] == '\0') {
|
2014-04-17 12:05:14 -04:00
|
|
|
r = EXIT_FAILURE;
|
2014-04-17 12:01:57 -04:00
|
|
|
weprintf("malformed input: %s\n", argv[i]);
|
2014-04-17 10:18:38 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
value = &p[1];
|
2014-04-17 12:01:57 -04:00
|
|
|
} else {
|
|
|
|
value = NULL;
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
2014-04-17 12:01:57 -04:00
|
|
|
variable = argv[i];
|
|
|
|
|
2014-04-18 04:30:48 -04:00
|
|
|
if (value) {
|
|
|
|
if (setsysctl(variable, value) < 0) {
|
|
|
|
r = EXIT_FAILURE;
|
|
|
|
weprintf("failed to set sysctl for %s\n", variable);
|
|
|
|
continue;
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
2014-04-18 04:30:48 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (getsysctl(variable, &value) < 0) {
|
|
|
|
r = EXIT_FAILURE;
|
|
|
|
weprintf("failed to get sysctl for %s\n", variable);
|
|
|
|
continue;
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
2014-04-18 04:30:48 -04:00
|
|
|
printf("%s = %s\n", variable, value);
|
|
|
|
free(value);
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 12:05:14 -04:00
|
|
|
return r;
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|