init: halt service if initial check fails

If the pre-startup phase fails, then there's a problem with the watchdog.
Stop upstart from trying to respawn us all the time in that case.  This
fixes the restart storm/spam seen when we build images for real devices,
but then boot them in a VM where there is no watchdog service.

BUG=chromium:667075
TEST=startup still works on a real device
TEST=startup halts in a VM

Change-Id: Icbd22f04dd434ad1e1fe8ae42a2385232b419be0
Reviewed-on: https://chromium-review.googlesource.com/419215
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Mattias Nissler <mnissler@chromium.org>
diff --git a/daisydog.conf b/daisydog.conf
index 76d015f..df1640c 100644
--- a/daisydog.conf
+++ b/daisydog.conf
@@ -23,9 +23,19 @@
 
   # Run in "check" mode to determine if the last reset was because
   # of a watchdog timeout.
-  if check_cmd | grep -Fq "watchdog-timeout"; then
-    metrics_client -v HwWatchdogReboot
+  if ! output="$(check_cmd 2>&1)"; then
+    # If we get a failure here, assume the watchdog is broken (or doesn't
+    # exist as in a VM), and exit (which halts the init script entirely).
+    logger -t "${UPSTART_JOB}" -p daemon.error \
+      "error: watchdog startup failed: ${output}"
+    exit 1
   fi
+
+  case "${output}" in
+  *watchdog-timeout*)
+    metrics_client -v HwWatchdogReboot
+    ;;
+  esac
 end script
 
 exec chrt -r 99 /sbin/minijail0 -i ${minijail_common_args}