Merge "Moving the JAVA_BINARY variable to be initialized properly"
diff --git a/drivers/shell/Android.bp b/drivers/shell/Android.bp
deleted file mode 100644
index 990b3d7..0000000
--- a/drivers/shell/Android.bp
+++ /dev/null
@@ -1,87 +0,0 @@
-//
-// Copyright (C) 2016 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-package {
-    default_applicable_licenses: ["Android-Apache-2.0"],
-}
-
-cc_binary {
-    name: "vts_shell_driver",
-    multilib: {
-        lib64: {
-            stem: "vts_shell_driver64",
-        },
-        lib32: {
-            stem: "vts_shell_driver32",
-        },
-    },
-
-    srcs: [
-        "ShellDriver.cpp",
-        "ShellDriverMain.cpp",
-    ],
-
-    shared_libs: [
-        "libbase",
-        "libutils",
-        "libcutils",
-        "libvts_multidevice_proto",
-        "libprotobuf-cpp-full",
-        "libvts_drivercomm",
-    ],
-
-    include_dirs: [
-        "test/vts/proto",
-        "test/vts/drivers/libdrivercomm",
-        "external/protobuf/src",
-    ],
-
-    cflags: [
-        "-Werror",
-        "-Wall",
-    ],
-
-    compile_multilib: "both",
-}
-
-cc_test {
-    name: "vts_shell_driver_test",
-
-    srcs: [
-        "ShellDriver.cpp",
-        "ShellDriverTest.cpp",
-    ],
-
-    shared_libs: [
-        "libbase",
-        "libutils",
-        "libcutils",
-        "libvts_multidevice_proto",
-        "libprotobuf-cpp-full",
-        "libvts_drivercomm",
-    ],
-
-    include_dirs: [
-        "test/vts/proto",
-        "test/vts/drivers/libdrivercomm",
-        "external/protobuf/src",
-    ],
-
-    cflags: [
-        "-Werror",
-        "-Wall",
-    ],
-}
diff --git a/drivers/shell/ShellDriver.cpp b/drivers/shell/ShellDriver.cpp
deleted file mode 100644
index 3bac44e..0000000
--- a/drivers/shell/ShellDriver.cpp
+++ /dev/null
@@ -1,277 +0,0 @@
-/*
- * Copyright 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#define LOG_TAG "VtsShellDriver"
-
-#include "ShellDriver.h"
-
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/un.h>
-#include <sstream>
-
-#include <VtsDriverCommUtil.h>
-#include <VtsDriverFileUtil.h>
-#include <android-base/logging.h>
-
-#include "test/vts/proto/VtsDriverControlMessage.pb.h"
-
-using namespace std;
-
-// Threshold of serialized proto msg size sent over socket.
-static constexpr long kProtoSizeThreshold = 1024 * 1024;  // 1MB
-
-namespace android {
-namespace vts {
-
-int VtsShellDriver::Close() {
-  int result = 0;
-
-  if (!this->socket_address_.empty()) {
-    result = unlink(this->socket_address_.c_str());
-    if (result != 0) {
-      LOG(ERROR) << " ERROR closing socket (errno = " << errno << ")";
-    }
-    this->socket_address_.clear();
-  }
-
-  return result;
-}
-
-CommandResult* VtsShellDriver::ExecShellCommandPopen(const string& command) {
-  CommandResult* result = new CommandResult();
-
-  // TODO: handle no output case.
-  FILE* output_fp;
-
-  // execute the command.
-  output_fp = popen(command.c_str(), "r");
-  if (output_fp == NULL) {
-    LOG(ERROR) << "Failed to run command: " << command;
-    result->exit_code = errno;
-    return result;
-  }
-
-  char buff[4096];
-  stringstream ss;
-
-  int bytes_read;
-  while (!feof(output_fp)) {
-    bytes_read = fread(buff, 1, sizeof(buff) - 1, output_fp);
-    // TODO: catch stderr
-    if (ferror(output_fp)) {
-      LOG(ERROR) << "ERROR reading shell output";
-      result->exit_code = -1;
-      return result;
-    }
-
-    buff[bytes_read] = '\0';
-    ss << buff;
-  }
-
-  LOG(DEBUG) << " Returning output: " << ss.str();
-  result->stdout = ss.str();
-
-  result->exit_code = pclose(output_fp) / 256;
-  return result;
-}
-
-CommandResult* VtsShellDriver::ExecShellCommandNohup(const string& command) {
-  CommandResult* result = new CommandResult();
-
-  string temp_dir = GetDirFromFilePath(this->socket_address_);
-  string temp_file_name_pattern = temp_dir + "/nohupXXXXXX";
-  int temp_file_name_len = temp_file_name_pattern.length() + 1;
-  char stdout_file_name[temp_file_name_len];
-  char stderr_file_name[temp_file_name_len];
-  strcpy(stdout_file_name, temp_file_name_pattern.c_str());
-  strcpy(stderr_file_name, temp_file_name_pattern.c_str());
-  int stdout_file = mkstemp(stdout_file_name);
-  int stderr_file = mkstemp(stderr_file_name);
-  close(stdout_file);
-  close(stderr_file);
-
-  stringstream ss;
-  ss << "nohup sh -c '" << command << "' >" << stdout_file_name << " 2>"
-     << stderr_file_name;
-
-  // execute the command.
-  int exit_code = system(ss.str().c_str()) / 256;
-  result->exit_code = exit_code;
-
-  // If stdout size larger than threshold, send back the temp file path.
-  // Otherwise, send back the context directly.
-  long stdout_size = GetFileSize(stdout_file_name);
-  if (stdout_size > kProtoSizeThreshold) {
-    result->stdout = string(stdout_file_name);
-  } else {
-    result->stdout = ReadFile(stdout_file_name);
-    remove(stdout_file_name);
-  }
-
-  // If stderr size larger than threshold, send back the temp file path.
-  // Otherwise, send back the context directly.
-  long stderr_size = GetFileSize(stderr_file_name);
-  if (stderr_size > kProtoSizeThreshold) {
-    result->stderr = string(stderr_file_name);
-  } else {
-    result->stderr = ReadFile(stderr_file_name);
-    remove(stderr_file_name);
-  }
-
-  return result;
-}
-
-int VtsShellDriver::ExecShellCommand(
-    const string& command, VtsDriverControlResponseMessage* responseMessage) {
-  CommandResult* result = this->ExecShellCommandNohup(command);
-
-  responseMessage->add_stdout(result->stdout);
-  responseMessage->add_stderr(result->stderr);
-
-  int exit_code = result->exit_code;
-  responseMessage->add_exit_code(result->exit_code);
-
-  delete result;
-  return exit_code;
-}
-
-int VtsShellDriver::HandleShellCommandConnection(int connection_fd) {
-  VtsDriverCommUtil driverUtil(connection_fd);
-  VtsDriverControlCommandMessage cmd_msg;
-  int numberOfFailure = 0;
-
-  while (1) {
-    if (!driverUtil.VtsSocketRecvMessage(
-            static_cast<google::protobuf::Message*>(&cmd_msg))) {
-      LOG(ERROR) << "Receiving message failure.";
-      return -1;
-    }
-
-    if (cmd_msg.command_type() == EXIT) {
-      LOG(ERROR) << "Received exit command.";
-      break;
-    } else if (cmd_msg.command_type() != EXECUTE_COMMAND) {
-      LOG(ERROR) << "Unknown command type " << cmd_msg.command_type();
-      continue;
-    }
-    LOG(INFO) << "Received " << cmd_msg.shell_command_size()
-              << " command(s). Processing...";
-
-    // execute command and write back output
-    VtsDriverControlResponseMessage responseMessage;
-
-    for (const auto& command : cmd_msg.shell_command()) {
-      if (ExecShellCommand(command, &responseMessage) != 0) {
-        LOG(ERROR) << "Error during executing command [" << command << "]";
-        --numberOfFailure;
-      }
-    }
-
-    // TODO: other response code conditions
-    responseMessage.set_response_code(VTS_DRIVER_RESPONSE_SUCCESS);
-    if (!driverUtil.VtsSocketSendMessage(responseMessage)) {
-      LOG(ERROR) << "Write output to socket error.";
-      --numberOfFailure;
-    }
-    LOG(DEBUG) << "Finished processing commands.";
-  }
-
-  if (driverUtil.Close() != 0) {
-    LOG(ERROR) << "Failed to close connection. errno: " << errno;
-    --numberOfFailure;
-  }
-
-  return numberOfFailure;
-}
-
-int VtsShellDriver::StartListen() {
-  if (this->socket_address_.empty()) {
-    LOG(ERROR) << "NULL socket address.";
-    return -1;
-  }
-
-  LOG(INFO) << "Start listening on " << this->socket_address_;
-
-  struct sockaddr_un address;
-  int socket_fd, connection_fd;
-  socklen_t address_length;
-  pid_t child;
-
-  socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
-  if (socket_fd < 0) {
-    PLOG(ERROR) << "socket() failed";
-    return socket_fd;
-  }
-
-  unlink(this->socket_address_.c_str());
-  memset(&address, 0, sizeof(struct sockaddr_un));
-  address.sun_family = AF_UNIX;
-  strncpy(address.sun_path, this->socket_address_.c_str(),
-          sizeof(address.sun_path) - 1);
-
-  if (::bind(socket_fd, (struct sockaddr*)&address,
-             sizeof(struct sockaddr_un)) != 0) {
-    PLOG(ERROR) << "bind() failed";
-    return 1;
-  }
-
-  if (listen(socket_fd, 5) != 0) {
-    PLOG(ERROR) << "listen() failed";
-    return errno;
-  }
-
-  while (1) {
-    address_length = sizeof(address);
-
-    // TODO(yuexima) exit message to break loop
-    connection_fd =
-        accept(socket_fd, (struct sockaddr*)&address, &address_length);
-    if (connection_fd == -1) {
-      PLOG(ERROR) << "accept failed";
-      break;
-    }
-
-    child = fork();
-    if (child == 0) {
-      close(socket_fd);
-      // now inside newly created connection handling process
-      if (HandleShellCommandConnection(connection_fd) != 0) {
-        LOG(ERROR) << "Failed to handle connection.";
-        close(connection_fd);
-        exit(1);
-      }
-      close(connection_fd);
-      exit(0);
-    } else if (child > 0) {
-      close(connection_fd);
-    } else {
-      LOG(ERROR) << "Create child process failed. Exiting...";
-      return (errno);
-    }
-  }
-  close(socket_fd);
-
-  return 0;
-}
-
-long VtsShellDriver::GetFileSize(const char* filename) {
-  struct stat stat_buf;
-  int rc = stat(filename, &stat_buf);
-  return rc == 0 ? stat_buf.st_size : -1;
-}
-
-}  // namespace vts
-}  // namespace android
diff --git a/drivers/shell/ShellDriver.h b/drivers/shell/ShellDriver.h
deleted file mode 100644
index 717d22c..0000000
--- a/drivers/shell/ShellDriver.h
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __VTS_SHELL_DRIVER_H_
-#define __VTS_SHELL_DRIVER_H_
-
-#include <string>
-
-#include "test/vts/proto/VtsDriverControlMessage.pb.h"
-
-using namespace std;
-
-namespace android {
-namespace vts {
-
-struct CommandResult {
-  string stdout;
-  string stderr;
-  int exit_code;
-};
-
-class VtsShellDriver {
- public:
-  VtsShellDriver() { socket_address_.clear(); }
-
-  explicit VtsShellDriver(const char* socket_address)
-      : socket_address_(socket_address) {}
-
-  ~VtsShellDriver() {
-    if (!this->socket_address_.empty()) {
-      Close();
-    }
-  }
-
-  // closes the sockets.
-  int Close();
-
-  // start shell driver server on unix socket
-  int StartListen();
-
- private:
-  // socket address
-  string socket_address_;
-
-  /*
-   * execute a given shell command and return the output file descriptor
-   * Please remember to call close_output after usage.
-   */
-  int ExecShellCommand(const string& command,
-                       VtsDriverControlResponseMessage* respond_message);
-
-  /*
-   * Handles a socket connection. Will execute a received shell command
-   * and send back the output text.
-   */
-  int HandleShellCommandConnection(int connection_fd);
-
-  /*
-   * Execute a shell command using popen and return a CommandResult object.
-   */
-  CommandResult* ExecShellCommandPopen(const string& command);
-
-  /*
-   * Execute a shell command using nohup and return a CommandResult object.
-   */
-  CommandResult* ExecShellCommandNohup(const string& command);
-
-  /*
-   * Helper method to get the size of the given file.
-   */
-  long GetFileSize(const char* filename);
-};
-
-}  // namespace vts
-}  // namespace android
-
-#endif  // __VTS_SHELL_DRIVER_H_
diff --git a/drivers/shell/ShellDriverMain.cpp b/drivers/shell/ShellDriverMain.cpp
deleted file mode 100644
index 6ddc9d4..0000000
--- a/drivers/shell/ShellDriverMain.cpp
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <getopt.h>
-#include <string>
-
-#include <android-base/logging.h>
-
-#include "ShellDriver.h"
-
-using namespace std;
-
-static constexpr const char* DEFAULT_SOCKET_PATH =
-    "/data/local/tmp/tmp_socket_shell_driver.tmp";
-
-// Dumps usage on stderr.
-static void usage() {
-  fprintf(
-      stderr,
-      "Usage: vts_shell_driver --server_socket_path=<UNIX_domain_socket_path>\n"
-      "\n"
-      "Android Vts Shell Driver v0.1.  To run shell commands on Android system."
-      "\n"
-      "Options:\n"
-      "--help\n"
-      "    Show this message.\n"
-      "--socket_path=<Unix_socket_path>\n"
-      "    Show this message.\n"
-      "\n"
-      "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
-      "\n");
-}
-
-// Parses command args and kicks things off.
-int main(int argc, char** argv) {
-  android::base::InitLogging(argv, android::base::StderrLogger);
-
-  static const struct option longOptions[] = {
-      {"help", no_argument, NULL, 'h'},
-      {"server_socket_path", required_argument, NULL, 's'},
-      {NULL, 0, NULL, 0}};
-
-  string socket_path = DEFAULT_SOCKET_PATH;
-
-  while (true) {
-    int optionIndex = 0;
-    int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
-    if (ic == -1) {
-      break;
-    }
-
-    switch (ic) {
-      case 'h':
-        usage();
-        return 0;
-      case 's':
-        socket_path = string(optarg);
-        break;
-      default:
-        if (ic != '?') {
-          fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
-        }
-        return 2;
-    }
-  }
-
-  android::vts::VtsShellDriver shellDriver(socket_path.c_str());
-  return shellDriver.StartListen();
-}
diff --git a/drivers/shell/ShellDriverTest.cpp b/drivers/shell/ShellDriverTest.cpp
deleted file mode 100644
index 8ceb614..0000000
--- a/drivers/shell/ShellDriverTest.cpp
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- * Copyright 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "ShellDriverTest.h"
-
-#include <errno.h>
-#include <gtest/gtest.h>
-#include <limits.h>
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <unistd.h>
-
-#include <iostream>
-#include <sstream>
-
-#include <VtsDriverCommUtil.h>
-#include "test/vts/proto/VtsDriverControlMessage.pb.h"
-
-#include "ShellDriver.h"
-
-using namespace std;
-
-namespace android {
-namespace vts {
-
-static int kMaxRetry = 3;
-
-/*
- * send a command to the driver on specified UNIX domain socket and print out
- * the outputs from driver.
- */
-static string vts_shell_driver_test_client_start(const string& command,
-                                                 const string& socket_address) {
-  struct sockaddr_un address;
-  int socket_fd;
-
-  socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
-  if (socket_fd < 0) {
-    fprintf(stderr, "socket() failed\n");
-    return "";
-  }
-
-  VtsDriverCommUtil driverUtil(socket_fd);
-
-  memset(&address, 0, sizeof(struct sockaddr_un));
-
-  address.sun_family = AF_UNIX;
-  strncpy(address.sun_path, socket_address.c_str(),
-          sizeof(address.sun_path) - 1);
-
-  int conn_success;
-  int retry_count = 0;
-
-  conn_success = connect(socket_fd, (struct sockaddr*)&address,
-                         sizeof(struct sockaddr_un));
-  for (retry_count = 0; retry_count < kMaxRetry && conn_success != 0;
-       retry_count++) {  // retry if server not ready
-    printf("Client: connection failed, retrying...\n");
-    retry_count++;
-    if (usleep(50 * pow(retry_count, 3)) != 0) {
-      fprintf(stderr, "shell driver unit test: sleep intrupted.");
-    }
-
-    conn_success = connect(socket_fd, (struct sockaddr*)&address,
-                           sizeof(struct sockaddr_un));
-  }
-
-  if (conn_success != 0) {
-    fprintf(stderr, "connect() failed\n");
-    return "";
-  }
-
-  VtsDriverControlCommandMessage cmd_msg;
-
-  cmd_msg.add_shell_command(command);
-  cmd_msg.set_command_type(EXECUTE_COMMAND);
-
-  if (!driverUtil.VtsSocketSendMessage(cmd_msg)) {
-    return NULL;
-  }
-
-  // read driver output
-  VtsDriverControlResponseMessage out_msg;
-
-  if (!driverUtil.VtsSocketRecvMessage(
-          static_cast<google::protobuf::Message*>(&out_msg))) {
-    return "";
-  }
-
-  // TODO(yuexima) use vector for output messages
-  stringstream ss;
-  for (int i = 0; i < out_msg.stdout_size(); i++) {
-    string out_str = out_msg.stdout(i);
-    cout << "[Shell driver] output for command " << i << ": " << out_str
-         << endl;
-    ss << out_str;
-  }
-  close(socket_fd);
-
-  cout << "[Client] receiving output: " << ss.str() << endl;
-  return ss.str();
-}
-
-/*
- * Prototype unit test helper. It first forks a vts_shell_driver process
- * and then call a client function to execute a command.
- */
-static string test_shell_command_output(const string& command,
-                                        const string& socket_address) {
-  pid_t p_driver;
-  string res_client;
-
-  VtsShellDriver shellDriver(socket_address.c_str());
-
-  p_driver = fork();
-  if (p_driver == 0) {  // child
-    int res_driver = shellDriver.StartListen();
-
-    if (res_driver != 0) {
-      fprintf(stderr, "Driver reported error. The error code is: %d.\n",
-              res_driver);
-      exit(res_driver);
-    }
-
-    exit(0);
-  } else if (p_driver > 0) {  // parent
-    res_client = vts_shell_driver_test_client_start(command, socket_address);
-    if (res_client.empty()) {
-      fprintf(stderr, "Client reported error.\n");
-      exit(1);
-    }
-    cout << "Client receiving: " << res_client << endl;
-  } else {
-    fprintf(stderr,
-            "shell_driver_test.cpp: create child process failed for driver.");
-    exit(-1);
-  }
-
-  // send kill signal to insure the process would not block
-  kill(p_driver, SIGKILL);
-
-  return res_client;
-}
-
-/*
- * This test tests whether the output of "uname" is "Linux\n"
- */
-TEST(vts_shell_driver_start, vts_shell_driver_unit_test_uname) {
-  string expected = "Linux\n";
-  string output =
-      test_shell_command_output("uname", "/data/local/tmp/test1_1.tmp");
-  ASSERT_EQ(output.compare(expected), 0);
-}
-
-/*
- * This test tests whether the output of "which ls" is "/system/bin/ls\n"
- */
-TEST(vts_shell_driver_start, vts_shell_driver_unit_test_which_ls) {
-  string expected = "/system/bin/ls\n";
-  string output =
-      test_shell_command_output("which ls", "/data/local/tmp/test1_2.tmp");
-  ASSERT_EQ(output.compare(expected), 0);
-}
-
-}  // namespace vts
-}  // namespace android
diff --git a/drivers/shell/ShellDriverTest.h b/drivers/shell/ShellDriverTest.h
deleted file mode 100644
index c3b3aba..0000000
--- a/drivers/shell/ShellDriverTest.h
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __VTS_SHELL_DRIVER_TEST_H_
-#define __VTS_SHELL_DRIVER_TEST_H_
-
-#endif  // __VTS_SHELL_DRIVER_TEST_H_
diff --git a/testcases/host/kernel_proc_file_api_test/proc_utils.py b/testcases/host/kernel_proc_file_api_test/proc_utils.py
index e40cc70..4361f9b 100644
--- a/testcases/host/kernel_proc_file_api_test/proc_utils.py
+++ b/testcases/host/kernel_proc_file_api_test/proc_utils.py
@@ -126,11 +126,18 @@
             time.sleep(1)
         return self.isBootCompleted()
 
-    def Root(self, timeout=None):
-        cmd = ["root"]
+    def Root(self):
         try:
-            self.adb.Execute(cmd, timeout=timeout)
-            return True
+            self.adb.Execute(["root"])
+            RETRIES = 3
+            for i in range(RETRIES):
+                self.adb.Execute(["wait-for-device"])
+                # Verify that we haven't raced with the exit of the old,
+                # non-root adbd
+                out, err, r_code = self.shell.Execute("id -un")
+                if r_code == 0 and not err.strip() and out.strip() == "root":
+                    return True
+                time.sleep(1)
         except subprocess.CalledProcessError as e:
             logging.exception(e)
         return False