From: Vadim Kochan <[email protected]> Make easy to set/get sysctl params via helpers. They can be used in other modules by refactoring.
Signed-off-by: Vadim Kochan <[email protected]> --- sysctl.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sysctl.h | 7 +++++++ 2 files changed, 76 insertions(+) create mode 100644 sysctl.c create mode 100644 sysctl.h diff --git a/sysctl.c b/sysctl.c new file mode 100644 index 0000000..11cb1da --- /dev/null +++ b/sysctl.c @@ -0,0 +1,69 @@ +/* + * sysctl - helpers to /proc/sys interface + * Subject to the GPL, version 2. + */ + +#include <fcntl.h> +#include <stdio.h> +#include <limits.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> + +#include "built_in.h" + +#define SYS_PATH "/proc/sys/" + +int sysctl_set_int(char *file, int value) +{ + char path[PATH_MAX]; + char str[64]; + ssize_t ret; + int fd; + + path[0] = '\0'; + strcat(path, SYS_PATH); + 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(char *file, int *value) +{ + char path[PATH_MAX]; + char str[64]; + ssize_t ret; + int fd; + + path[0] = '\0'; + strcat(path, SYS_PATH); + 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..ee5fdc4 --- /dev/null +++ b/sysctl.h @@ -0,0 +1,7 @@ +#ifndef SYSCTL_I_H +#define SYSCTL_I_H + +int sysctl_set_int(char *file, int value); +int sysctl_get_int(char *file, int *value); + +#endif -- 2.4.2 -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
