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>
|
2014-06-14 08:13:10 -04:00
|
|
|
#include "text.h"
|
2014-04-17 10:18:38 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-17 11:38:31 -04:00
|
|
|
static void
|
2014-05-06 08:36:49 -04:00
|
|
|
replacestr(char *s, int a, int b)
|
2014-04-17 11:38:31 -04:00
|
|
|
{
|
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-05-06 08:36:49 -04:00
|
|
|
replacestr(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)) {
|
2014-05-06 08:36:49 -04:00
|
|
|
replacestr(variable, '/', '.');
|
2014-04-17 10:18:38 -04:00
|
|
|
return -1;
|
2014-04-17 11:38:31 -04:00
|
|
|
}
|
|
|
|
|
2014-05-06 08:36:49 -04:00
|
|
|
replacestr(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-05-06 08:36:49 -04:00
|
|
|
replacestr(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)) {
|
2014-05-06 08:36:49 -04:00
|
|
|
replacestr(variable, '/', '.');
|
2014-04-17 10:18:38 -04:00
|
|
|
return -1;
|
2014-04-17 14:05:25 -04:00
|
|
|
}
|
|
|
|
|
2014-05-06 08:36:49 -04:00
|
|
|
replacestr(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;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:20:21 -04:00
|
|
|
static int
|
|
|
|
parsepair(char *pair)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *variable;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
for (p = pair; *p; p++) {
|
|
|
|
if (p[0] == '.' && p[1] == '.') {
|
|
|
|
weprintf("malformed input: %s\n", pair);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = strchr(pair, '=');
|
|
|
|
if (p) {
|
|
|
|
if (p[1] == '\0') {
|
|
|
|
weprintf("malformed input: %s\n", pair);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
value = &p[1];
|
|
|
|
} else {
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
variable = pair;
|
|
|
|
if (value) {
|
|
|
|
if (setsysctl(variable, value) < 0) {
|
|
|
|
weprintf("failed to set sysctl for %s\n", variable);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (getsysctl(variable, &value) < 0) {
|
|
|
|
weprintf("failed to get sysctl for %s\n", variable);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
printf("%s = %s\n", variable, value);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-17 10:18:38 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-06-13 10:20:21 -04:00
|
|
|
eprintf("usage: %s [-p file] variable[=value]...\n", argv0);
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-13 10:20:21 -04:00
|
|
|
FILE *fp;
|
2014-06-14 08:13:10 -04:00
|
|
|
char *buf = NULL, *p;
|
2014-06-13 10:20:21 -04:00
|
|
|
char *file = NULL;
|
2014-06-14 08:13:10 -04:00
|
|
|
size_t size = 0;
|
2014-04-17 10:18:38 -04:00
|
|
|
int i;
|
2014-04-17 12:05:14 -04:00
|
|
|
int r = EXIT_SUCCESS;
|
2014-04-17 10:18:38 -04:00
|
|
|
|
2014-06-13 10:20:21 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'p':
|
|
|
|
file = EARGF(usage());
|
|
|
|
break;
|
|
|
|
default:
|
2014-04-17 10:18:38 -04:00
|
|
|
usage();
|
2014-06-13 10:20:21 -04:00
|
|
|
} ARGEND;
|
2014-04-17 10:18:38 -04:00
|
|
|
|
2014-06-13 10:20:21 -04:00
|
|
|
if (!file && argc < 1)
|
|
|
|
usage();
|
2014-04-17 12:01:57 -04:00
|
|
|
|
2014-06-13 10:20:21 -04:00
|
|
|
if (!file) {
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
if (parsepair(argv[i]) < 0)
|
2014-04-18 04:30:48 -04:00
|
|
|
r = EXIT_FAILURE;
|
2014-06-13 10:20:21 -04:00
|
|
|
} else {
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (!fp)
|
|
|
|
eprintf("fopen %s:", file);
|
2014-06-14 08:13:10 -04:00
|
|
|
while (agetline(&buf, &size, fp) != -1) {
|
2014-06-13 10:20:21 -04:00
|
|
|
p = buf;
|
|
|
|
for (p = buf; *p == ' ' || *p == '\t'; p++)
|
|
|
|
;
|
|
|
|
if (*p == '#' || *p == '\n')
|
2014-04-18 04:30:48 -04:00
|
|
|
continue;
|
2014-06-13 10:20:21 -04:00
|
|
|
for (p = buf; *p; p++) {
|
|
|
|
if (*p == '\n') {
|
|
|
|
*p = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
2014-06-13 10:20:21 -04:00
|
|
|
p = buf;
|
|
|
|
if (parsepair(p) < 0)
|
2014-04-18 04:30:48 -04:00
|
|
|
r = EXIT_FAILURE;
|
2014-04-17 10:18:38 -04:00
|
|
|
}
|
2014-06-13 10:20:21 -04:00
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("%s: read error:", file);
|
2014-06-14 08:13:10 -04:00
|
|
|
free(buf);
|
2014-06-13 10:20:21 -04:00
|
|
|
fclose(fp);
|
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
|
|
|
}
|