diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2010-05-21 14:17:36 -0700 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2010-05-21 14:17:36 -0700 |
commit | 7d70cc23db917a04561e390ca039142ec1a0a6ff (patch) | |
tree | 771acf1d3023d69c97439e9b245a9f0a359cee58 /src | |
parent | 0639cdb20357c3c1201d5a7012700373729c834a (diff) | |
download | vyatta-cfg-7d70cc23db917a04561e390ca039142ec1a0a6ff.tar.gz vyatta-cfg-7d70cc23db917a04561e390ca039142ec1a0a6ff.zip |
Add command to get/set sysfs files
By using a command (instead of echo), it is possible to set capability
and avoid sudo.
Diffstat (limited to 'src')
-rw-r--r-- | src/sysfs.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/sysfs.c b/src/sysfs.c new file mode 100644 index 0000000..a84cea4 --- /dev/null +++ b/src/sysfs.c @@ -0,0 +1,79 @@ +/* + * Program to set sysfs value - similar to sysctl commmand + */ + +#include <stdio.h> +#include <limits.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> + +#define SYS "/sys" + +static void get(const char *name) +{ + char path[PATH_MAX]; + char buf[BUFSIZ]; + FILE *f; + + snprintf(path, PATH_MAX, SYS "/%s", name); + f = fopen(path, "r"); + if (f == NULL) { + fprintf(stderr, "%s : %s\n", path, strerror(errno)); + exit(1); + } + + while (fgets(buf, BUFSIZ, f) != NULL) + fputs(buf, stdout); + + if (ferror(f)) { + fprintf(stderr, "%s : read %s\n", path, strerror(errno)); + exit(1); + } + fclose(f); +} + +static void set(const char *name, const char *val) +{ + FILE *f; + char path[PATH_MAX]; + + snprintf(path, PATH_MAX, SYS "/%s", name); + f = fopen(path, "w"); + if (f == NULL) { + fprintf(stderr, "%s : %s\n", path, strerror(errno)); + exit(1); + } + + fprintf(f, "%s\n", val); + fflush(f); + + if (ferror(f)) { + fprintf(stderr, "%s : read %s\n", path, strerror(errno)); + exit(1); + } + fclose(f); +} + +int main(int argc, char **argv) +{ + if (argc == 1) { + fprintf(stderr, "Usage: %s variable\n", argv[0]); + fprintf(stderr, " %s variable=value\n", argv[0]); + return 1; + } + + while (--argc) { + char *ep, *arg = *++argv; + + ep = strchr(arg, '='); + if (!ep) + get(arg); + else { + *ep++ = '\0'; + set(arg, ep); + } + } + + return 0; +} |