recover_duts: spruce up reload_network_device

There's a lot of problems in this file

 * it's no longer used in a udev rule [1] -- it's only used in test images,
   for the 'check_ethernet' hook used in the lab; so remove some of the
   udev cruft
 * it isn't very nice for interactive use (most of its errors go only to
   syslog; it doesn't give clear expectations about its args)
 * it tries to control wlan power, if used on a wlan device; this will
   fail on a lot of devices, which means you'll be stuck (we already
   removed the driver module at this point, and 'set -e' means we fail
   hard). That's not even verified or recommended even on those devices
   where it may work. So stop doing it.
 * not all drivers are modules

This cleans up most of that.

Currently, check_ethernet.hook only calls this for ethernet devices. Now
it is safe(r) to use on wlan devices too.

[1] https://chromium-review.googlesource.com/493888

BUG=b:112780859
TEST=`reload_network_device`, with {lo,eth0,wlan0} args, and without
     args

Change-Id: I61dcff44e3293b1d4fc9ff29ed21fe42cd3d344d
Signed-off-by: Brian Norris <briannorris@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1565198
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Reviewed-by: Grant Grundler <grundler@chromium.org>
diff --git a/recover_duts/reload_network_device b/recover_duts/reload_network_device
index e5285ef..fd60d79 100755
--- a/recover_duts/reload_network_device
+++ b/recover_duts/reload_network_device
@@ -4,47 +4,41 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-# UDEV event helper script that restarts a hung network device.
+# Helper script that restarts a hung network device.
 
+SCRIPT_NAME="$(basename $0)"
 POWER_DOWN_TIME_SECONDS=1
-WLAN_POWER_UP_TIME_SECONDS=1
-WLAN_REINITIALIZE_TIME_SECONDS=5
 
 set -e
 
-error () {
-  logger -t $0 "Error: $1"
+error() {
+  logger -t "${SCRIPT_NAME}" -s "Error: $1"
   exit 1
 }
 
-if [ -n "${1}" ] ; then
-  eval $(udevadm info --query=property --path="/sys/class/net/${1}" |
-         egrep '^(DEVPATH|DEVTYPE|ID_BUS|INTERFACE|SUBSYSTEM)')
-  EVENT=device_hang
+if [ $# -ne 1 ] ; then
+  error "Missing network interface name"
 fi
 
-if [ "${SUBSYSTEM}" != "net" -o \
-     "${EVENT}" != "device_hang" -o \
-     -z "${DEVPATH}" ] ; then
-  exit 0
+eval $(udevadm info --query=property --path="/sys/class/net/${1}" |
+       egrep '^(DEVPATH|DEVTYPE|ID_BUS|INTERFACE|SUBSYSTEM)')
+
+if [ "${SUBSYSTEM}" != "net" -o -z "${DEVPATH}" ] ; then
+  error "Could not retreive info about device ${1}"
 fi
 
-logger -t $0 "Restarting network interface ${INTERFACE}"
+logger -t "${SCRIPT_NAME}" "Restarting network interface ${INTERFACE}"
 
-# The DEVPATH for a network interface should look something like:
-#   /devices/pci0000:00/0000:00:1c.0/0000:01:00.0/net/wlan0
-if [ $(basename $(dirname "${DEVPATH}")) != 'net' ] ; then
-  error "Device path for ${INTERFACE} (${DEVPATH}) is invalid."
+device_path="$(readlink -f "/sys${DEVPATH}/../..")"
+
+if [ -e "/sys${DEVPATH}/device/driver/module" ]; then
+  module_name="$(basename "$(readlink -f "/sys${DEVPATH}/device/driver/module")")"
+  rmmod "${module_name}" || error "rmmod '${module_name}' failed ($?)"
 fi
 
-device_path=$(readlink -f "/sys${DEVPATH}/../..")
-module_name=$(basename $(readlink -f "/sys${DEVPATH}/device/driver/module"))
-
-rmmod "${module_name}"
-
 if [ "${ID_BUS}" = "pci" ] ; then
   device_remove_path="${device_path}/remove"
-  bus_rescan_path=$(readlink -f  "${device_path}/../rescan")
+  bus_rescan_path="$(readlink -f  "${device_path}/../rescan")"
 
   if [ ! -e "${device_remove_path}" ] ; then
     error "Device remove path ${device_remove_path} does not exist"
@@ -55,27 +49,13 @@
   fi
 
   echo 1 > "${device_remove_path}"
-  if [ "${DEVTYPE}" = "wlan" ] ; then
-    ectool wireless 0xe  # Disable wireless lan.
-    ectool wireless 0x6  # Power down wireless lan.
-  fi
-
   sleep ${POWER_DOWN_TIME_SECONDS}
-
-  if [ "${DEVTYPE}" = "wlan" ] ; then
-    ectool wireless 0xe  # Power up wireless lan.
-    sleep ${WLAN_POWER_UP_TIME_SECONDS}
-    ectool wireless 0xf  # Enable wireless lan.
-
-    sleep ${WLAN_REINITIALIZE_TIME_SECONDS}
-  fi
-
   echo 1 > "${bus_rescan_path}"
 elif [ "${ID_BUS}" = "usb" ] ; then
-  device_authorize_path=$(readlink -f "${device_path}/../authorized")
+  device_authorize_path="$(readlink -f "${device_path}/../authorized")"
   echo 0 > "${device_authorize_path}"
   sleep ${POWER_DOWN_TIME_SECONDS}
   echo 1 > "${device_authorize_path}"
 else
-  error "Bus type ${ID_BUS} is unknown"
+  error "Bus type '${ID_BUS}' is unknown"
 fi