Bidirectional test mode signaling over GPIO

* Implements a bidirectional synchronous handshake protocol over
  dut_flaga/b GPIOs. This protocol is designed to return true (test
  mode) only if the DUT is connected to a servo board which implements
  the remote end.

* Includes unit tests for the test mode signaling routine, complete with
  mock/fake implementation of the remote end.

Note that we still do not deploy GpioHandler in actual update
processing, which will be done later.

BUG=chromium-os:25397,chromium-os:27109,chromium-os:27672
TEST=Builds and passes unit tests (including new ones)

Change-Id: I265407ed735c3e1354e10782ac30566b16caeb20
Reviewed-on: https://gerrit.chromium.org/gerrit/23330
Reviewed-by: Gaurav Shah <gauravsh@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Commit-Ready: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
diff --git a/file_descriptor.cc b/file_descriptor.cc
index 275aa70..a064925 100644
--- a/file_descriptor.cc
+++ b/file_descriptor.cc
@@ -48,10 +48,28 @@
 
 bool EintrSafeFileDescriptor::Close() {
   CHECK_GE(fd_, 0);
-  int ret = HANDLE_EINTR(close(fd_));
-  if (!ret)
-    fd_ = -1;
-  return !ret;
+  if (HANDLE_EINTR(close(fd_)))
+    return false;
+  Reset();
+  return true;
+}
+
+void EintrSafeFileDescriptor::Reset() {
+  fd_ = -1;
+}
+
+
+ScopedFileDescriptorCloser::~ScopedFileDescriptorCloser() {
+  if (descriptor_ && descriptor_->IsOpen() && !descriptor_->Close()) {
+    const char* err_str = "file closing failed";
+    if (descriptor_->IsSettingErrno()) {
+      PLOG(ERROR) << err_str;
+    } else {
+      LOG(ERROR) << err_str;
+    }
+    // Abandon the current descriptor, forcing it back to a closed state.
+    descriptor_->Reset();
+  }
 }
 
 }  // namespace chromeos_update_engine