From 6cf7f01256c39677a0a5561ebca60e8def9d6d7e Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 6 Nov 2009 04:04:19 +0100 Subject: adding example runit-style service directory Signed-off-by: Denys Vlasenko --- examples/var_service/dhcp_if/convert2ipconf | 53 +++++++++++++++++++ examples/var_service/dhcp_if/dhcp_handler | 82 +++++++++++++++++++++++++++++ examples/var_service/dhcp_if/log/run | 21 ++++++++ examples/var_service/dhcp_if/p_log | 4 ++ examples/var_service/dhcp_if/run | 23 ++++++++ examples/var_service/dhcp_if/w_log | 4 ++ 6 files changed, 187 insertions(+) create mode 100755 examples/var_service/dhcp_if/convert2ipconf create mode 100755 examples/var_service/dhcp_if/dhcp_handler create mode 100755 examples/var_service/dhcp_if/log/run create mode 100755 examples/var_service/dhcp_if/p_log create mode 100755 examples/var_service/dhcp_if/run create mode 100755 examples/var_service/dhcp_if/w_log (limited to 'examples/var_service/dhcp_if') diff --git a/examples/var_service/dhcp_if/convert2ipconf b/examples/var_service/dhcp_if/convert2ipconf new file mode 100755 index 0000000..cee0854 --- /dev/null +++ b/examples/var_service/dhcp_if/convert2ipconf @@ -0,0 +1,53 @@ +#!/bin/sh +# convert: + +# dhcptype=5 +# serverid=172.16.42.102 +# lease=97200 +# interface=eth0 +# ip=172.16.42.177 +# subnet=255.255.255.0 +# mask=24 +# broadcast=172.16.22.255 +# router=172.16.42.98 +# dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27 +# domain=lab.example.com example.com +# ntpsrv=10.34.32.125 10.34.255.7 + +# into: + +#let cfg=cfg+1 +#if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=... + +exec >"$0.out" 2>&1 + +test "$interface" || exit 1 +test -f "$1" || exit 1 + +# Unsafe, and does not handle values with spaces: +#. "./$1" || exit 1 +# Safe(r) parsing: +sq="'" +while read line; do + #echo "line: $line" + # Skip empty lines and lines with single quotes + test "${line##*$sq*}" || continue + var="${line%%=*}" + val="${line#*=}" + #echo "var:$var val:'$val'" + eval "$var='$val'" +done <"$1" + +{ +echo "let cfg=cfg+1" +test "$interface" && echo "if[\$cfg]='$interface'" +test "$ip" && echo "ip[\$cfg]='$ip'" +test "$ip" && test "$mask" \ + && echo "ipmask[\$cfg]='$ip/$mask'" +test "$router" && echo "gw[\$cfg]='$router'" +test "$dns" && echo "dns[\$cfg]='$dns'" +# TODO: I never saw a dhcp server which correctly announces +# which subnet(s) is/are available thru advertised router +# Assume 0/0 +echo "net[\$cfg]='0/0'" +} >"$2" diff --git a/examples/var_service/dhcp_if/dhcp_handler b/examples/var_service/dhcp_if/dhcp_handler new file mode 100755 index 0000000..9ed3e7a --- /dev/null +++ b/examples/var_service/dhcp_if/dhcp_handler @@ -0,0 +1,82 @@ +#!/bin/sh +# executed by udhcpc +# parameters: $1 and environment +# +# $1 is: +# +# deconfig: This argument is used when udhcpc starts, and +# when a lease is lost. The script should put the interface in an +# up, but deconfigured state, ie: ifconfig $interface 0.0.0.0. +# Environment: interface=ethN +# +# bound: This argument is used when udhcpc moves from an +# unbound, to a bound state. All of the paramaters are set in +# enviromental variables, The script should configure the interface, +# and set any other relavent parameters (default gateway, dns server, etc). +# Environment: +# dhcptype=5 +# serverid=172.16.42.102 +# lease=97200 +# interface=eth0 +# ip=172.16.42.177 +# subnet=255.255.255.0 +# mask=24 +# broadcast=172.16.22.255 +# router=172.16.42.98 +# dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27 +# domain=lab.example.com example.com +# ntpsrv=10.34.32.125 10.34.255.7 +# +# renew: This argument is used when a DHCP lease is renewed. All of +# the paramaters are set in enviromental variables. This argument is +# used when the interface is already configured, so the IP address, +# will not change, however, the other DHCP paramaters, such as the +# default gateway, subnet mask, and dns server may change. +# Environment: same as for "bound". +# +# nak: This argument is used with udhcpc receives a NAK message. +# The script with the deconfig argument will be called directly +# afterwards, so no changes to the network interface are neccessary. +# This hook is provided for purely informational purposes (the +# message option may contain a reason for the NAK). +# Environment: interface=ethN, serverid=IP_ADDR +# +# leasefail: called when lease cannot be obtained +# (for example, when DHCP server is down). +# Environment: interface=ethN + +# TODO: put domain into /etc/resolv.conf (thru /var/service/fw) +# TODO: feed ntp IPs to /var/service/ntp + +service=`basename $PWD` +outfile="$service.ipconf" +dir="/var/run/service/fw" + +exec >>"$0.out" 2>&1 + +echo "`date`: Params: $*" + +if test x"$1" != x"bound" && test x"$1" != x"renew" ; then + # Reconfigure network with this interface disabled + echo "Deconfiguring" + rm "$service.out" + rm "$outfile" + rm "$dir/$outfile" + sv u /var/service/fw + exit +fi + +# Bound: we've got the lease + +# Process params +env >"$service.out" +./convert2ipconf "$service.out" "$outfile" + +# Reconfigure routing and firewall if needed +diff --brief "$outfile" "$dir/$outfile" >/dev/null 2>&1 +if test "$?" != "0"; then + echo "Reconfiguring" + mkdir -p "$dir" 2>/dev/null + cp "$outfile" "$dir/$outfile" + sv u /var/service/fw +fi diff --git a/examples/var_service/dhcp_if/log/run b/examples/var_service/dhcp_if/log/run new file mode 100755 index 0000000..560d1b1 --- /dev/null +++ b/examples/var_service/dhcp_if/log/run @@ -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/dhcp_if/p_log b/examples/var_service/dhcp_if/p_log new file mode 100755 index 0000000..a2521be --- /dev/null +++ b/examples/var_service/dhcp_if/p_log @@ -0,0 +1,4 @@ +#!/bin/sh + +cd log/logdir || exit 1 +cat @* current | $PAGER diff --git a/examples/var_service/dhcp_if/run b/examples/var_service/dhcp_if/run new file mode 100755 index 0000000..aec79e0 --- /dev/null +++ b/examples/var_service/dhcp_if/run @@ -0,0 +1,23 @@ +#!/bin/sh + +exec 2>&1 +exec &1 | cut -b0-$((w-2))' -- cgit v1.1