From 26203eae4d7b7d7e5e37d4232a838648d744800d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 15 Dec 2015 04:56:03 +0100 Subject: [PATCH] Add package batman-adv-visdata --- net/batman-adv-visdata/Makefile | 40 ++++++++ .../files/batman-adv-visdata.init | 22 +++++ net/batman-adv-visdata/src/Makefile | 4 + .../src/batman-adv-visdata.c | 99 +++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 net/batman-adv-visdata/Makefile create mode 100755 net/batman-adv-visdata/files/batman-adv-visdata.init create mode 100644 net/batman-adv-visdata/src/Makefile create mode 100644 net/batman-adv-visdata/src/batman-adv-visdata.c diff --git a/net/batman-adv-visdata/Makefile b/net/batman-adv-visdata/Makefile new file mode 100644 index 0000000..bda1982 --- /dev/null +++ b/net/batman-adv-visdata/Makefile @@ -0,0 +1,40 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=batman-adv-visdata +PKG_VERSION:=1 + +PKG_LICENSE:=BSD-2-Clause + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk + +define Package/batman-adv-visdata + SECTION:=net + CATEGORY:=Network + DEPENDS:= + TITLE:=Pulls the batman-adv originators and transtable_global tables out of the kernel periodically +endef + + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Build/Configure +endef + +define Build/Compile + CFLAGS="$(TARGET_CFLAGS)" CPPFLAGS="$(TARGET_CPPFLAGS)" $(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS) +endef + +define Package/batman-adv-visdata/install + $(INSTALL_DIR) $(1)/usr/sbin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/batman-adv-visdata $(1)/usr/sbin/ + + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/batman-adv-visdata.init $(1)/etc/init.d/batman-adv-visdata +endef + +$(eval $(call BuildPackage,batman-adv-visdata)) diff --git a/net/batman-adv-visdata/files/batman-adv-visdata.init b/net/batman-adv-visdata/files/batman-adv-visdata.init new file mode 100755 index 0000000..36a3156 --- /dev/null +++ b/net/batman-adv-visdata/files/batman-adv-visdata.init @@ -0,0 +1,22 @@ +#!/bin/sh /etc/rc.common + +START=98 +USE_PROCD=1 + +start_service() { + include /lib/functions + + local meshes + + add_mesh() { + meshes="$meshes $1" + } + + config_load batman-adv + config_foreach add_mesh 'mesh' + + procd_open_instance + procd_set_param command /usr/sbin/batman-adv-visdata $meshes + procd_set_param respawn ${threshold:-20} ${timeout:-5} ${retry:-3} + procd_close_instance +} diff --git a/net/batman-adv-visdata/src/Makefile b/net/batman-adv-visdata/src/Makefile new file mode 100644 index 0000000..3f939ae --- /dev/null +++ b/net/batman-adv-visdata/src/Makefile @@ -0,0 +1,4 @@ +all: batman-adv-visdata + +batman-adv-visdata: batman-adv-visdata.c + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS) diff --git a/net/batman-adv-visdata/src/batman-adv-visdata.c b/net/batman-adv-visdata/src/batman-adv-visdata.c new file mode 100644 index 0000000..f536bbc --- /dev/null +++ b/net/batman-adv-visdata/src/batman-adv-visdata.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include + +#include +#include + + +/* + * batadv-vis updates its originators every 10s + * We choose a bigger interval to make reboots even more unlikely, + * and make our interval coprime with batadv-vis's + */ +#define UPDATE_INTERVAL 19 + +#define INPUT_DIR "/sys/kernel/debug/batman_adv" +#define OUTPUT_DIR "/tmp/batman-adv-visdata" + +#define ORIGINATORS "originators" +#define TRANSTABLE_GLOBAL "transtable_global" + + +static bool copy_file(const char *src, const char *dst) { + FILE *input = NULL, *output = NULL; + + input = fopen(src, "r"); + if (!input) + goto error; + + output = fopen(dst, "w"); + if (!output) + goto error; + + while (!feof(input)) { + if (ferror(input)) + goto error; + + char buf[1024]; + size_t r = fread(buf, 1, sizeof(buf), input); + if (!r) + continue; + + if (!fwrite(buf, r, 1, output)) + goto error; + } + + fclose(input); + fclose(output); + + return true; + + error: + if (input) + fclose(input); + if (output) + fclose(output); + + unlink(dst); + return false; +} + +static void handle_file(const char *mesh, const char *file) { + char src[strlen(INPUT_DIR) + 1 + strlen(mesh) + 1 + strlen(file) + 1]; + char dst[strlen(OUTPUT_DIR) + 1 + strlen(mesh) + 1 + strlen(file) + 1]; + char tmp[sizeof(dst) + 4]; + + snprintf(src, sizeof(src), "%s/%s/%s", INPUT_DIR, mesh, file); + snprintf(dst, sizeof(dst), "%s/%s/%s", OUTPUT_DIR, mesh, file); + snprintf(tmp, sizeof(tmp), "%s.new", dst); + + if (copy_file(src, tmp)) + rename(tmp, dst); + else + unlink(dst); +} + +static void handle_mesh(const char *mesh) { + char dir[strlen(OUTPUT_DIR) + 1 + strlen(mesh) + 1]; + + snprintf(dir, sizeof(dir), "%s/%s", OUTPUT_DIR, mesh); + mkdir(dir, 0777); + + handle_file(mesh, ORIGINATORS); + handle_file(mesh, TRANSTABLE_GLOBAL); +} + +int main(int argc, char *argv[]) { + system("rm -rf " OUTPUT_DIR "; mkdir -p " OUTPUT_DIR); + + while (true) { + int i; + for (i = 1; i < argc; i++) + handle_mesh(argv[i]); + + sleep(UPDATE_INTERVAL); + } +}