check_ethernet: fix ${paused_time} for first-run
Example error scenario:
## When first run at bootup, the lock does not exist:
rm /run/autotest_pause_ethernet_hook
## So first-time run, sees errors:
/usr/local/libexec/recover-duts/hooks/check_ethernet.hook
recover_duts.log:
2020-07-21 23:24:15,087 - DEBUG - Running of /usr/local/libexec/recover-duts/hooks/check_ethernet.hook succeeded with output: stat: cannot stat '/run/autotest_pause_ethernet_hook': No such file or directory
2020-07-21 23:24:15-07:00 Last locked 0 seconds ago; skipping
Problems and fixes:
paused_time is both global and local. I didn't notice that [ -n
"${paused_time}" ] doesn't work properly, because it's already
initialized to 0.
Drop the global initialization and fix the subsequent bugs:
* the '-a' operator is apparently not recommended in POSIX shell, and
it isn't short-circuiting the way I'd like, even if it still
functions decently:
$ bash -c 'a=""; [ -n "${a}" -a "${a}" -lt 30 ] && echo true'
bash: line 0: [: : integer expression expected
$ dash -c 'a=""; [ -n "${a}" -a "${a}" -lt 30 ] && echo true'
dash: 1: [: Illegal number:
$ bash -c 'a="1"; [ -n "${a}" -a "${a}" -lt 30 ] && echo true'
true
$ dash -c 'a="1"; [ -n "${a}" -a "${a}" -lt 30 ] && echo true'
true
* It's technically possible to fall through without grabbing the lock
but still leaving ${paused_time} empty. Let's handle that to be extra
safe.
BUG=none
TEST=`rm /run/autotest_pause_ethernet_hook;
/usr/local/libexec/recover-duts/hooks/check_ethernet.hook`
--> Still see this error:
stat: cannot stat '/run/autotest_pause_ethernet_hook': No such file or directory
---> But not this:
Last locked 0 seconds ago; skipping
Change-Id: Ic39c93c4769f9b055a863bea5aa4ebe747622554
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crostestutils/+/2323710
Tested-by: Brian Norris <briannorris@chromium.org>
Reviewed-by: Grant Grundler <grundler@chromium.org>
Commit-Queue: Brian Norris <briannorris@chromium.org>
diff --git a/recover_duts/hooks/check_ethernet.hook b/recover_duts/hooks/check_ethernet.hook
index ebf78a1..0e03fd7 100755
--- a/recover_duts/hooks/check_ethernet.hook
+++ b/recover_duts/hooks/check_ethernet.hook
@@ -11,7 +11,6 @@
# See do_suspend() in src/third_party/autotest/files/client/cros/power/sys_power.py
PAUSE_FILE=/run/autotest_pause_ethernet_hook
-paused_time=0
# Critical messages should be sent to /var/log/messages. Other messages should
@@ -257,7 +256,7 @@
# Acquire the lock and hold it until exit, if possible.
if try_pause_lock; then
- if [ -n "${paused_time}" -a "${paused_time}" -lt 30 ]; then
+ if [ -n "${paused_time}" ] && [ "${paused_time}" -lt 30 ]; then
# We were recently paused. Skip this iteration, in case the Ethernet link
# is still coming up.
info_msg "Last locked ${paused_time} seconds ago; skipping"
@@ -266,6 +265,9 @@
# File wasn't locked - no need to pause.
return 1
fi
+ # Couldn't grab the lock, but also couldn't get a timestamp for it? Just
+ # assume it was recently locked.
+ [ -z "${paused_time}" ] && paused_time=0
if [ -z "${start_time}" ]; then
# Couldn't figure out lock time - just clobber it.