summaryrefslogtreecommitdiff
path: root/sysctl.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysctl.c')
-rw-r--r--sysctl.c67
1 files changed, 67 insertions, 0 deletions
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;
+}