trafgen: fix -t 0 option to use sendto
[netsniff-ng-new.git] / sysctl.c
blobda1ad866a0edd569031b21295a691cfd7932e19e
1 /*
2 * sysctl - sysctl set/get helpers
3 * Subject to the GPL, version 2.
4 */
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <limits.h>
13 #include "built_in.h"
14 #include "sysctl.h"
16 int sysctl_set_int(const char *file, int value)
18 char path[PATH_MAX];
19 char str[64];
20 ssize_t ret;
21 int fd;
23 strncpy(path, SYSCTL_PROC_PATH, PATH_MAX);
24 strncat(path, file, PATH_MAX - sizeof(SYSCTL_PROC_PATH) - 1);
26 fd = open(path, O_WRONLY);
27 if (unlikely(fd < 0))
28 return -1;
30 ret = snprintf(str, 63, "%d", value);
31 if (ret < 0) {
32 close(fd);
33 return -1;
36 ret = write(fd, str, strlen(str));
38 close(fd);
39 return ret <= 0 ? -1 : 0;
42 int sysctl_get_int(const char *file, int *value)
44 char path[PATH_MAX];
45 char str[64];
46 ssize_t ret;
47 int fd;
49 strncpy(path, SYSCTL_PROC_PATH, PATH_MAX);
50 strncat(path, file, PATH_MAX - sizeof(SYSCTL_PROC_PATH) - 1);
52 fd = open(path, O_RDONLY);
53 if (fd < 0)
54 return -1;
56 ret = read(fd, str, sizeof(str));
57 if (ret > 0) {
58 *value = atoi(str);
59 ret = 0;
60 } else {
61 ret = -1;
64 close(fd);
65 return ret;