AU: Ensure update_engine_client get proxy retries are spaced out in time.

Hopefully, this will reduce test flakyness. It seems that last time the test
failed all 4 tries happened within a one-second timeframe.

Also, fix GError object memory leaks throughout the code.

BUG=chromium-os:21351
TEST=unit tests, tested on VM

Change-Id: If0bc5d5767d12f3396d0fcb46f3e04ed6d7dfd5c
Reviewed-on: http://gerrit.chromium.org/gerrit/8862
Commit-Ready: Darin Petkov <petkov@chromium.org>
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Darin Petkov <petkov@chromium.org>
diff --git a/update_engine_client.cc b/update_engine_client.cc
index fb2d418..db14fb6 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -19,7 +19,7 @@
 using chromeos_update_engine::kUpdateEngineServiceName;
 using chromeos_update_engine::kUpdateEngineServicePath;
 using chromeos_update_engine::kUpdateEngineServiceInterface;
-using chromeos_update_engine::utils::GetGErrorMessage;
+using chromeos_update_engine::utils::GetAndFreeGError;
 using std::string;
 
 DEFINE_string(app_version, "", "Force the current app version.");
@@ -41,24 +41,27 @@
   DBusGProxy* proxy = NULL;
   GError* error = NULL;
   const int kTries = 4;
+  const int kRetrySeconds = 10;
 
   bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
-  if (!bus) {
-    LOG(FATAL) << "Failed to get bus";
-  }
+  LOG_IF(FATAL, !bus) << "Failed to get bus: " << GetAndFreeGError(&error);
   for (int i = 0; !proxy && i < kTries; ++i) {
-    LOG_IF(INFO, i) << "Trying to get dbus proxy. Try "
-                    << (i + 1) << "/" << kTries;
+    if (i > 0) {
+      LOG(INFO) << "Retrying to get dbus proxy. Try "
+                << (i + 1) << "/" << kTries;
+      sleep(kRetrySeconds);
+    }
     proxy = dbus_g_proxy_new_for_name_owner(bus,
                                             kUpdateEngineServiceName,
                                             kUpdateEngineServicePath,
                                             kUpdateEngineServiceInterface,
                                             &error);
+    LOG_IF(WARNING, !proxy) << "Error getting dbus proxy for "
+                            << kUpdateEngineServiceName << ": "
+                            << GetAndFreeGError(&error);
   }
-  if (!proxy) {
-    LOG(FATAL) << "Error getting dbus proxy for "
-               << kUpdateEngineServiceName << ": " << GetGErrorMessage(error);
-  }
+  LOG_IF(FATAL, !proxy) << "Giving up -- unable to get dbus proxy for "
+                        << kUpdateEngineServiceName;
   *out_proxy = proxy;
   return true;
 }
@@ -101,7 +104,7 @@
       &new_size,
       &error);
   if (rc == FALSE) {
-    LOG(INFO) << "Error getting status: " << GetGErrorMessage(error);
+    LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error);
   }
   printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
          "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
@@ -164,7 +167,7 @@
                                                         omaha_url.c_str(),
                                                         &error);
   CHECK_EQ(rc, TRUE) << "Error checking for update: "
-                     << GetGErrorMessage(error);
+                     << GetAndFreeGError(&error);
   return true;
 }
 
@@ -179,7 +182,7 @@
   // Reboot error code doesn't necessarily mean that a reboot
   // failed. For example, D-Bus may be shutdown before we receive the
   // result.
-  LOG_IF(INFO, !rc) << "Reboot error message: " << GetGErrorMessage(error);
+  LOG_IF(INFO, !rc) << "Reboot error message: " << GetAndFreeGError(&error);
   return true;
 }
 
@@ -194,7 +197,7 @@
                                                    track.c_str(),
                                                    &error);
   CHECK_EQ(rc, true) << "Error setting the track: "
-                     << GetGErrorMessage(error);
+                     << GetAndFreeGError(&error);
   LOG(INFO) << "Track permanently set to: " << track;
 }
 
@@ -209,8 +212,7 @@
       org_chromium_UpdateEngineInterface_get_track(proxy,
                                                    &track,
                                                    &error);
-  CHECK_EQ(rc, true) << "Error getting the track: "
-                     << GetGErrorMessage(error);
+  CHECK_EQ(rc, true) << "Error getting the track: " << GetAndFreeGError(&error);
   string output = track;
   g_free(track);
   return output;