proto-shell: move script handler dump code to handler.c
authorFelix Fietkau <nbd@openwrt.org>
Tue, 24 Sep 2013 08:47:47 +0000 (10:47 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 22 Oct 2013 12:10:33 +0000 (14:10 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
CMakeLists.txt
handler.c [new file with mode: 0644]
handler.h [new file with mode: 0644]
proto-shell.c

index 777a8e8fa576e896ac834926522bb1f4595f1cc6..632d1b9480b734bee420c92c8938164a862fd402 100644 (file)
@@ -11,7 +11,7 @@ IF(APPLE)
 ENDIF()
 
 SET(SOURCES
-       main.c utils.c system.c tunnel.c
+       main.c utils.c system.c tunnel.c handler.c
        interface.c interface-ip.c interface-event.c
        iprule.c proto.c proto-static.c proto-shell.c
        config.c device.c bridge.c vlan.c alias.c
diff --git a/handler.c b/handler.c
new file mode 100644 (file)
index 0000000..4d0a57a
--- /dev/null
+++ b/handler.c
@@ -0,0 +1,119 @@
+/*
+ * netifd - network interface daemon
+ * Copyright (C) 2012-2013 Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#define _GNU_SOURCE
+#include <glob.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "netifd.h"
+#include "system.h"
+#include "handler.h"
+
+static int
+netifd_dir_push(int fd)
+{
+       int prev_fd = open(".", O_RDONLY | O_DIRECTORY);
+       system_fd_set_cloexec(prev_fd);
+       if (fd >= 0)
+               fchdir(fd);
+       return prev_fd;
+}
+
+static void
+netifd_dir_pop(int prev_fd)
+{
+       fchdir(prev_fd);
+       close(prev_fd);
+}
+
+int netifd_open_subdir(const char *name)
+{
+       int prev_dir;
+       int ret = -1;
+
+       prev_dir = netifd_dir_push(-1);
+       if (chdir(main_path)) {
+               perror("chdir(main path)");
+               goto out;
+       }
+
+       ret = open(name, O_RDONLY | O_DIRECTORY);
+       if (ret >= 0)
+               system_fd_set_cloexec(ret);
+
+out:
+       netifd_dir_pop(prev_dir);
+       return ret;
+}
+
+static void
+netifd_init_script_handler(const char *name, script_dump_cb cb)
+{
+       struct json_tokener *tok = NULL;
+       json_object *obj;
+       static char buf[512];
+       char *start, *cmd;
+       FILE *f;
+       int len;
+
+#define DUMP_SUFFIX    " '' dump"
+
+       cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
+       sprintf(cmd, "%s" DUMP_SUFFIX, name);
+
+       f = popen(cmd, "r");
+       if (!f)
+               return;
+
+       do {
+               start = fgets(buf, sizeof(buf), f);
+               if (!start)
+                       continue;
+
+               len = strlen(start);
+
+               if (!tok)
+                       tok = json_tokener_new();
+
+               obj = json_tokener_parse_ex(tok, start, len);
+               if (!is_error(obj)) {
+                       cb(name, obj);
+                       json_object_put(obj);
+                       json_tokener_free(tok);
+                       tok = NULL;
+               } else if (start[len - 1] == '\n') {
+                       json_tokener_free(tok);
+                       tok = NULL;
+               }
+       } while (!feof(f) && !ferror(f));
+
+       if (tok)
+               json_tokener_free(tok);
+
+       pclose(f);
+}
+
+void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
+{
+       glob_t g;
+       int i, prev_fd;
+
+       prev_fd = netifd_dir_push(dir_fd);
+       glob("./*.sh", 0, NULL, &g);
+       for (i = 0; i < g.gl_pathc; i++)
+               netifd_init_script_handler(g.gl_pathv[i], cb);
+       netifd_dir_pop(prev_fd);
+}
diff --git a/handler.h b/handler.h
new file mode 100644 (file)
index 0000000..02438f2
--- /dev/null
+++ b/handler.h
@@ -0,0 +1,24 @@
+/*
+ * netifd - network interface daemon
+ * Copyright (C) 2012-2013 Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#ifndef __NETIFD_HANDLER_H
+#define __NETIFD_HANDLER_H
+
+#include <libubox/blobmsg_json.h>
+
+typedef void (*script_dump_cb)(const char *name, json_object *obj);
+
+int netifd_open_subdir(const char *name);
+void netifd_init_script_handlers(int dir_fd, script_dump_cb cb);
+
+#endif
index 9c242e7b478b197e1c1f4a7c166c3d31a794b87f..6ac8dab2f7dd79502630c5b5425bcd9cc7ff2ab2 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <glob.h>
 #include <unistd.h>
-#include <fcntl.h>
 #include <signal.h>
 
 #include <arpa/inet.h>
 #include <netinet/in.h>
 
-#include <libubox/blobmsg_json.h>
 
 #include "netifd.h"
 #include "interface.h"
 #include "interface-ip.h"
 #include "proto.h"
 #include "system.h"
+#include "handler.h"
 
 static int proto_fd = -1;
 
@@ -858,106 +856,6 @@ proto_shell_add_handler(const char *script, json_object *obj)
        add_proto_handler(proto);
 }
 
-typedef void (*script_dump_cb)(const char *name, json_object *obj);
-
-static int
-netifd_dir_push(int fd)
-{
-       int prev_fd = open(".", O_RDONLY | O_DIRECTORY);
-       system_fd_set_cloexec(prev_fd);
-       if (fd >= 0)
-               fchdir(fd);
-       return prev_fd;
-}
-
-static void
-netifd_dir_pop(int prev_fd)
-{
-       fchdir(prev_fd);
-       close(prev_fd);
-}
-
-static int
-netifd_open_subdir(const char *name)
-{
-       int prev_dir;
-       int ret = -1;
-
-       prev_dir = netifd_dir_push(-1);
-       if (chdir(main_path)) {
-               perror("chdir(main path)");
-               goto out;
-       }
-
-       ret = open(name, O_RDONLY | O_DIRECTORY);
-       if (ret >= 0)
-               system_fd_set_cloexec(ret);
-
-out:
-       netifd_dir_pop(prev_dir);
-       return ret;
-}
-
-static void
-netifd_init_script_handler(const char *name, script_dump_cb cb)
-{
-       struct json_tokener *tok = NULL;
-       json_object *obj;
-       static char buf[512];
-       char *start, *cmd;
-       FILE *f;
-       int len;
-
-#define DUMP_SUFFIX    " '' dump"
-
-       cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
-       sprintf(cmd, "%s" DUMP_SUFFIX, name);
-
-       f = popen(cmd, "r");
-       if (!f)
-               return;
-
-       do {
-               start = fgets(buf, sizeof(buf), f);
-               if (!start)
-                       continue;
-
-               len = strlen(start);
-
-               if (!tok)
-                       tok = json_tokener_new();
-
-               obj = json_tokener_parse_ex(tok, start, len);
-               if (!is_error(obj)) {
-                       cb(name, obj);
-                       json_object_put(obj);
-                       json_tokener_free(tok);
-                       tok = NULL;
-               } else if (start[len - 1] == '\n') {
-                       json_tokener_free(tok);
-                       tok = NULL;
-               }
-       } while (!feof(f) && !ferror(f));
-
-       if (tok)
-               json_tokener_free(tok);
-
-       pclose(f);
-}
-
-static void
-netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
-{
-       glob_t g;
-       int i, prev_fd;
-
-       prev_fd = netifd_dir_push(dir_fd);
-       glob("./*.sh", 0, NULL, &g);
-       for (i = 0; i < g.gl_pathc; i++)
-               netifd_init_script_handler(g.gl_pathv[i], cb);
-       netifd_dir_pop(prev_fd);
-}
-
 static void __init proto_shell_init(void)
 {
        proto_fd = netifd_open_subdir("proto");