update_engine: Added ability to retrieve old version via DBus

Now it is possible to retrieve an old OS version that was in use
before the last reboot, provided that we are now booted into
an updated version. This is to be used by crash reporter in case
the reboot was caused by a crash in kernel while reboot was pending
for an update. In such a scenario, the crash would be attributed
to the current (updated) version, which is wrong.

Also added additional parameter, --old_version, to
update_engine_client to retrieve the old version number, if any,
from the update engine.

BUG=chrome-os-partner:26518
TEST=cros deploy and tested via update_engine_client

Change-Id: I84e2322c5698b44b4c77f25c8f4211cdb367e5dc
Reviewed-on: https://chromium-review.googlesource.com/190149
Reviewed-by: Chris Sosa <sosa@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/update_engine_client.cc b/update_engine_client.cc
index 6bd3d9f..d27f027 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -57,6 +57,8 @@
               "cellular networks.");
 DEFINE_bool(watch_for_updates, false,
             "Listen for status updates and print them to the screen.");
+DEFINE_bool(prev_version, false,
+            "Show the previous OS version used before the update reboot.");
 
 namespace {
 
@@ -373,6 +375,26 @@
   g_main_loop_unref(loop);
 }
 
+void ShowPrevVersion() {
+  DBusGProxy* proxy;
+  GError* error = nullptr;
+
+  CHECK(GetProxy(&proxy));
+
+  char* prev_version = nullptr;
+
+  gboolean rc = update_engine_client_get_prev_version(proxy,
+                                                      &prev_version,
+                                                      &error);
+  if (!rc) {
+    LOG(ERROR) << "Error getting previous version: "
+               << GetAndFreeGError(&error);
+  } else {
+    LOG(INFO) << "Previous version = " << prev_version;
+    g_free(prev_version);
+  }
+}
+
 }  // namespace {}
 
 int main(int argc, char** argv) {
@@ -521,6 +543,10 @@
     return 0;
   }
 
+  if (FLAGS_prev_version) {
+    ShowPrevVersion();
+  }
+
   LOG(INFO) << "Done.";
   return 0;
 }