summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2010-05-21 14:17:36 -0700
committerStephen Hemminger <stephen.hemminger@vyatta.com>2010-05-21 14:17:36 -0700
commit7d70cc23db917a04561e390ca039142ec1a0a6ff (patch)
tree771acf1d3023d69c97439e9b245a9f0a359cee58
parent0639cdb20357c3c1201d5a7012700373729c834a (diff)
downloadvyatta-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.
-rw-r--r--Makefile.am4
-rw-r--r--src/sysfs.c79
2 files changed, 82 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index 69c10c7..efe1a92 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -32,6 +32,8 @@ sbin_PROGRAMS += src/dump
sbin_PROGRAMS += src/my_delete
sbin_PROGRAMS += src/my_set
sbin_PROGRAMS += src/check_tmpl
+sbin_PROGRAMS += src/sysfs
+
src_priority_SOURCES = src/priority.c
src_my_commit2_SOURCES = src/commit2.c
src_exe_action_SOURCES = src/exe_action.c
@@ -39,6 +41,7 @@ src_dump_SOURCES = src/dump_session.c
src_my_delete_SOURCES = src/delete.c
src_my_set_SOURCES = src/set.c
src_check_tmpl_SOURCES = src/check_tmpl.c
+src_sysfs = src/sysfs.c
sbin_SCRIPTS = scripts/vyatta-cfg-cmd-wrapper
sbin_SCRIPTS += scripts/vyatta-validate-type.pl
@@ -77,4 +80,3 @@ install-exec-hook:
mkdir -p $(DESTDIR)$(etc_shell_leveldir)
cd etc/shell/level; $(cpiop) $(DESTDIR)$(etc_shell_leveldir)
cd $(DESTDIR)/opt/vyatta/sbin; ln -s my_commit2 my_commit
-
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;
+}