summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2015-07-19 14:41:18 +0300
committerDaniel Borkmann <daniel@iogearbox.net>2015-07-20 10:03:54 +0200
commita1e7c1dc527ca3c37349cebc6fbeaa12a594fcf3 (patch)
tree6f9eefe9f5b3dfaf9ecfd4c1964e0ec0c6bf2c5d
parentbcc10fdc239d2fe7073637df4febfe60463b4b46 (diff)
sysctl: Add sysctl module with /proc/sys helpers
Moved sysctl get/set funcs from flowtop to separated sysctl module. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
-rw-r--r--flowtop.c55
-rw-r--r--flowtop/Makefile1
-rw-r--r--sysctl.c67
-rw-r--r--sysctl.h7
4 files changed, 76 insertions, 54 deletions
diff --git a/flowtop.c b/flowtop.c
index 8cf2fd3..bf2e585 100644
--- a/flowtop.c
+++ b/flowtop.c
@@ -39,6 +39,7 @@
#include "pkt_buff.h"
#include "screen.h"
#include "proc.h"
+#include "sysctl.h"
struct flow_entry {
uint32_t flow_id, use, status;
@@ -222,60 +223,6 @@ static const struct nfct_filter_ipv6 filter_ipv6 = {
.mask = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
};
-#define SYS_PATH "/proc/sys/"
-
-static int sysctl_set_int(char *file, int value)
-{
- char path[PATH_MAX];
- char str[64];
- ssize_t ret;
- int fd;
-
- strncpy(path, SYS_PATH, PATH_MAX);
- strncat(path, file, PATH_MAX - sizeof(SYS_PATH) - 1);
-
- fd = open(path, O_WRONLY);
- if (unlikely(fd < 0))
- return -1;
-
- ret = snprintf(str, 63, "%d", value);
- if (ret < 0) {
- close(fd);
- return -1;
- }
-
- ret = write(fd, str, strlen(str));
-
- close(fd);
- return ret <= 0 ? -1 : 0;
-}
-
-static int sysctl_get_int(char *file, int *value)
-{
- char path[PATH_MAX];
- char str[64];
- ssize_t ret;
- int fd;
-
- strncpy(path, SYS_PATH, PATH_MAX);
- strncat(path, file, PATH_MAX - sizeof(SYS_PATH) - 1);
-
- fd = open(path, O_RDONLY);
- if (fd < 0)
- return -1;
-
- ret = read(fd, str, sizeof(str));
- if (ret > 0) {
- *value = atoi(str);
- ret = 0;
- } else {
- ret = -1;
- }
-
- close(fd);
- return ret;
-}
-
static void signal_handler(int number)
{
switch (number) {
diff --git a/flowtop/Makefile b/flowtop/Makefile
index 85cb0b6..edb5829 100644
--- a/flowtop/Makefile
+++ b/flowtop/Makefile
@@ -23,6 +23,7 @@ flowtop-objs = xmalloc.o \
tprintf.o \
screen.o \
die.o \
+ sysctl.o \
flowtop.o
ifeq ($(CONFIG_GEOIP), 1)
diff --git a/sysctl.c b/sysctl.c
new file mode 100644
index 0000000..283d403
--- /dev/null
+++ b/sysctl.c
@@ -0,0 +1,67 @@
+/*
+ * sysctl - sysctl set/get helpers
+ * Subject to the GPL, version 2.
+ */
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/limits.h>
+
+#include "built_in.h"
+
+#define SYS_PATH "/proc/sys/"
+
+int sysctl_set_int(const char *file, int value)
+{
+ char path[PATH_MAX];
+ char str[64];
+ ssize_t ret;
+ int fd;
+
+ strncpy(path, SYS_PATH, PATH_MAX);
+ strncat(path, file, PATH_MAX - sizeof(SYS_PATH) - 1);
+
+ fd = open(path, O_WRONLY);
+ if (unlikely(fd < 0))
+ return -1;
+
+ ret = snprintf(str, 63, "%d", value);
+ if (ret < 0) {
+ close(fd);
+ return -1;
+ }
+
+ ret = write(fd, str, strlen(str));
+
+ close(fd);
+ return ret <= 0 ? -1 : 0;
+}
+
+int sysctl_get_int(const char *file, int *value)
+{
+ char path[PATH_MAX];
+ char str[64];
+ ssize_t ret;
+ int fd;
+
+ strncpy(path, SYS_PATH, PATH_MAX);
+ strncat(path, file, PATH_MAX - sizeof(SYS_PATH) - 1);
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ ret = read(fd, str, sizeof(str));
+ if (ret > 0) {
+ *value = atoi(str);
+ ret = 0;
+ } else {
+ ret = -1;
+ }
+
+ close(fd);
+ return ret;
+}
diff --git a/sysctl.h b/sysctl.h
new file mode 100644
index 0000000..2f88f0a
--- /dev/null
+++ b/sysctl.h
@@ -0,0 +1,7 @@
+#ifndef SYSCTL_H
+#define SYSCTL_H
+
+int sysctl_set_int(const char *file, int value);
+int sysctl_get_int(const char *file, int *value);
+
+#endif