New example of a service: examples/var_service/zcip_if
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 24 Oct 2015 03:30:58 +0000 (05:30 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 24 Oct 2015 03:30:58 +0000 (05:30 +0200)
Zeroconf for IPv4

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
examples/var_service/zcip_if/README [new file with mode: 0644]
examples/var_service/zcip_if/convert2ipconf [new file with mode: 0755]
examples/var_service/zcip_if/log/run [new file with mode: 0755]
examples/var_service/zcip_if/p_log [new file with mode: 0755]
examples/var_service/zcip_if/run [new file with mode: 0755]
examples/var_service/zcip_if/w_log [new file with mode: 0755]
examples/var_service/zcip_if/zcip_handler [new file with mode: 0755]

diff --git a/examples/var_service/zcip_if/README b/examples/var_service/zcip_if/README
new file mode 100644 (file)
index 0000000..4ddccb2
--- /dev/null
@@ -0,0 +1,5 @@
+The real README file is one directory up.
+
+This directory's run script can have useful comments.
+If it doesn't but you feel it should, please send a patch
+to busybox's mailing list.
diff --git a/examples/var_service/zcip_if/convert2ipconf b/examples/var_service/zcip_if/convert2ipconf
new file mode 100755 (executable)
index 0000000..aa133d8
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/sh
+# convert:
+
+#interface=eth1
+#ip=169.254.x.y
+
+#let cfg=cfg+1
+#if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=...
+
+exec >/dev/null
+#exec >"$0.out"  # debug
+exec 2>&1
+
+test "$interface" || exit 1
+test "$ip" || exit 1
+
+{
+echo "let cfg=cfg+1"
+test "$interface"      && echo "if[\$cfg]='$interface'"
+test "$ip"             && echo "ip[\$cfg]='$ip'"
+test "$ip"             && echo "ipmask[\$cfg]='$ip/16'"
+} >"$1"
diff --git a/examples/var_service/zcip_if/log/run b/examples/var_service/zcip_if/log/run
new file mode 100755 (executable)
index 0000000..560d1b1
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+user=logger
+
+logdir="/var/log/service/`(cd ..;basename $PWD)`"
+mkdir -p "$logdir" 2>/dev/null
+chown -R "$user": "$logdir"
+chmod -R go-rwxst,u+rwX "$logdir"
+rm logdir
+ln -s "$logdir" logdir
+
+# make this dir accessible to logger
+chmod a+rX .
+
+exec >/dev/null
+exec 2>&1
+exec \
+env - PATH="$PATH" \
+softlimit \
+setuidgid "$user" \
+svlogd -tt "$logdir"
diff --git a/examples/var_service/zcip_if/p_log b/examples/var_service/zcip_if/p_log
new file mode 100755 (executable)
index 0000000..a2521be
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+cd log/logdir || exit 1
+cat @* current | $PAGER
diff --git a/examples/var_service/zcip_if/run b/examples/var_service/zcip_if/run
new file mode 100755 (executable)
index 0000000..94a8754
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+exec 2>&1
+exec </dev/null
+
+pwd="$PWD"
+
+if="${PWD##*/zcip_}"
+
+echo "* Upping iface $if"
+ip link set dev "$if" up
+
+echo "* Starting zcip"
+exec \
+env - PATH="$PATH" \
+softlimit \
+setuidgid root \
+zcip -fvv \
+    "$if" \
+    "$pwd/zcip_handler"
diff --git a/examples/var_service/zcip_if/w_log b/examples/var_service/zcip_if/w_log
new file mode 100755 (executable)
index 0000000..aa36ef1
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+cd log/logdir || exit 1
+watch -n2 'w=`ttysize w`; h=`ttysize h`; tail -$((h-3)) current 2>&1 | cut -b1-$((w-2))'
diff --git a/examples/var_service/zcip_if/zcip_handler b/examples/var_service/zcip_if/zcip_handler
new file mode 100755 (executable)
index 0000000..13010db
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/sh
+# executed by zcip
+# parameters: $1 and environment
+# $1 is:
+#
+# init: zcip starts. Environment:
+# interface=eth0
+#
+# config: Address is obtained.
+# interface=eth0
+# ip=169.254.a.b
+#
+# deconfig: Conflict or link went down.
+# interface=eth0
+
+service=${PWD##*/}
+file_ipconf="$service.ipconf"
+dir_ipconf="/var/run/service/fw"
+
+exec >/dev/null
+#exec >>"$0.out"  #debug
+exec 2>&1
+
+echo "`date`: Params: $*"
+
+if test x"$1" != x"config"; then
+       # Reconfigure network with this interface disabled
+       echo "Deconfiguring"
+       rm "$file_ipconf"
+       rm "$dir_ipconf/$file_ipconf"
+       sv u /var/service/fw
+       exit
+fi
+
+# "config": we've got the address
+#env # debug
+
+./convert2ipconf "$file_ipconf"
+# Reconfigure routing and firewall if needed
+diff --brief "$file_ipconf" "$dir_ipconf/$file_ipconf" >/dev/null 2>&1
+if test $? != 0; then
+       echo "Reconfiguring fw"
+       mkdir -p "$dir_ipconf" 2>/dev/null
+       cp "$file_ipconf" "$dir_ipconf/$file_ipconf"
+       sv u /var/service/fw
+fi