svc/tripe-ifup.in: Better error handling.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 14 Mar 2015 14:39:42 +0000 (14:39 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 14 Mar 2015 14:48:41 +0000 (14:48 +0000)
Don't give up as soon as a network configuration command fails.  That
tends to leave the device's routing in a hopelessly broken state.
Instead, catch errors, report them via the server, and issue a slightly
different notification on completion.

svc/tripe-ifup.8.in
svc/tripe-ifup.in

index 2b50041..f9a783c 100644 (file)
@@ -125,6 +125,16 @@ the link, over the tunnel interface.  The
 may be IPv4 or IPv6 addresses.  If the interface has only an IPv4
 address then IPv6 routes will be ignored, and
 .IR "vice versa" .
+.RS
+.PP
+If any configuration commands fail, a warning
+.IP
+.B USER tripe-ifup command-failed
+.BI rc= rc
+.I command
+.PP
+is issued.
+.RE
 .hP 4.
 Configure the interface MTU and bring it up.  The
 interface MTU is configured based on the path MTU to the peer's external
@@ -143,8 +153,11 @@ Notify services.  A notification
 .IP
 .B USER tripe-ifup configured
 .I peer
+.RB [ failed ]
 .PP
-is issued.
+is issued: the
+.B failed
+token is included if any of the configuration commands failed.
 .RE
 .
 .\"--------------------------------------------------------------------------
index ae447db..e92c0fd 100644 (file)
@@ -25,6 +25,18 @@ export PATH TRIPEDIR
 if [ -d /proc/sys/net/ipv6 ]; then have6=t; else have6=nil; fi
 
 ###--------------------------------------------------------------------------
+### Error handling.
+
+win=t
+try () {
+  if "$@"; then :; else
+    rc=$?
+    tripectl warn tripe-ifup command-failed rc=$rc "$*"
+    win=nil
+  fi
+}
+
+###--------------------------------------------------------------------------
 ### Collect arguments.
 
 ## Collect the simple arguments.
@@ -46,7 +58,7 @@ esac
 
 case "${P_IFNAME+set}" in
   set)
-    ip link set "$ifname" name "$P_IFNAME"
+    try ip link set "$ifname" name "$P_IFNAME"
     ifname=$P_IFNAME
     $tripectl setifname "$peer" "$ifname"
     ;;
@@ -80,13 +92,13 @@ haveaddr4=nil
 set -- $l4addr
 case $#,${r4addr+set} in
   [1-9]*,set)
-    ip addr add "$1" peer "$r4addr" dev "$ifname"
+    try ip addr add "$1" peer "$r4addr" dev "$ifname"
     haveaddr4=t
     shift
     ;;
 esac
 for a in "$@"; do
-  ip addr add "$a" dev "$ifname"
+  try ip addr add "$a" dev "$ifname"
   haveaddr4=t
 done
 
@@ -97,11 +109,11 @@ set -- $l6addr
 case $have6,$# in
   t,[1-9]*)
     for a in "$@"; do
-      ip addr add "$a" dev "$ifname"
+      try ip addr add "$a" dev "$ifname"
       haveaddr6=t
     done
     case ${r6addr+set} in
-      set) ip route add $r6addr proto static dev "$ifname" ;;
+      set) try ip route add $r6addr proto static dev "$ifname" ;;
     esac
     ;;
 esac
@@ -121,7 +133,7 @@ case $haveaddr4,$haveaddr6 in
        mtu=$(expr "$pathmtu" - 29 - $A_BULK_OVERHEAD)
        ;;
     esac
-    ip link set dev "$ifname" up mtu "$mtu"
+    try ip link set dev "$ifname" up mtu "$mtu"
     ;;
 esac
 
@@ -142,7 +154,7 @@ set -- $route4
 case $haveaddr4,$# in
   t,[1-9]*)
     for p in "$@"; do
-      ip route add $p proto static via "$r4addr"
+      try ip route add $p proto static via "$r4addr"
     done
     ;;
 esac
@@ -152,7 +164,7 @@ set -- $route6
 case $haveaddr6,$# in
   t,[1-9]*)
     for p in "$@"; do
-      ip route add $p proto static via "${r6addr%/*}"
+      try ip route add $p proto static via "${r6addr%/*}"
     done
     ;;
 esac
@@ -169,6 +181,9 @@ esac
 ###--------------------------------------------------------------------------
 ### Issue a notification that we've won.
 
-$tripectl notify tripe-ifup configured "$peer"
+case $win in
+  t) $tripectl notify tripe-ifup configured "$peer" ;;
+  nil) $tripectl notify tripe-ifup configured "$peer" failed ;;
+esac
 
 ###----- That's all, folks --------------------------------------------------