No Description

adb_install.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "adb_install.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <signal.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. #include <android-base/file.h>
  27. #include <android-base/logging.h>
  28. #include <android-base/properties.h>
  29. #include <android-base/unique_fd.h>
  30. #include "common.h"
  31. #include "fuse_sideload.h"
  32. #include "install.h"
  33. #include "ui.h"
  34. static void set_usb_driver(bool enabled) {
  35. // USB configfs doesn't use /s/c/a/a/enable.
  36. if (android::base::GetBoolProperty("sys.usb.configfs", false)) {
  37. return;
  38. }
  39. static constexpr const char* USB_DRIVER_CONTROL = "/sys/class/android_usb/android0/enable";
  40. android::base::unique_fd fd(open(USB_DRIVER_CONTROL, O_WRONLY));
  41. if (fd == -1) {
  42. PLOG(ERROR) << "Failed to open driver control";
  43. return;
  44. }
  45. // Not using android::base::WriteStringToFile since that will open with O_CREAT and give EPERM
  46. // when USB_DRIVER_CONTROL doesn't exist. When it gives EPERM, we don't know whether that's due
  47. // to non-existent USB_DRIVER_CONTROL or indeed a permission issue.
  48. if (!android::base::WriteStringToFd(enabled ? "1" : "0", fd)) {
  49. PLOG(ERROR) << "Failed to set driver control";
  50. }
  51. }
  52. static void stop_adbd() {
  53. ui->Print("Stopping adbd...\n");
  54. android::base::SetProperty("ctl.stop", "adbd");
  55. set_usb_driver(false);
  56. }
  57. static void maybe_restart_adbd() {
  58. if (is_ro_debuggable()) {
  59. ui->Print("Restarting adbd...\n");
  60. set_usb_driver(true);
  61. android::base::SetProperty("ctl.start", "adbd");
  62. }
  63. }
  64. static pthread_t sideload_thread;
  65. static pid_t sideload_adb_pid;
  66. static bool sideload_cancelled;
  67. static bool sideload_started;
  68. static void *adb_sideload_thread(void*) {
  69. time_t start_time = time(nullptr);
  70. time_t now = start_time;
  71. // How long (in seconds) we wait for the host to start sending us a package, before timing out.
  72. static constexpr int ADB_INSTALL_TIMEOUT = 300;
  73. // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host connects and starts serving a
  74. // package. Poll for its appearance. (Note that inotify doesn't work with FUSE.)
  75. int status = -1;
  76. while (now - start_time < ADB_INSTALL_TIMEOUT) {
  77. // Exit if either:
  78. // - The adb child process dies, or
  79. // - The ui tells us to cancel
  80. if (kill(sideload_adb_pid, 0) != 0) {
  81. break;
  82. }
  83. if (sideload_cancelled) {
  84. break;
  85. }
  86. struct stat st;
  87. status = stat(FUSE_SIDELOAD_HOST_PATHNAME, &st);
  88. if (status == 0) {
  89. break;
  90. }
  91. if (errno != ENOENT && errno != ENOTCONN) {
  92. ui->Print("\nError %s waiting for package\n\n", strerror(errno));
  93. break;
  94. }
  95. sleep(1);
  96. now = time(nullptr);
  97. }
  98. if (status == 0) {
  99. sideload_started = true;
  100. // Signal UI thread that sideload has started
  101. ui->CancelWaitKey();
  102. }
  103. return nullptr;
  104. }
  105. void sideload_start() {
  106. stop_adbd();
  107. set_usb_driver(true);
  108. if ((sideload_adb_pid = fork()) == 0) {
  109. execl("/sbin/recovery", "recovery", "--adbd", nullptr);
  110. _exit(EXIT_FAILURE);
  111. }
  112. ui->Print("\n\nNow send the package you want to apply\n"
  113. "to the device with \"adb sideload <filename>\"...\n");
  114. sideload_cancelled = false;
  115. sideload_started = false;
  116. pthread_create(&sideload_thread, nullptr, &adb_sideload_thread, nullptr);
  117. }
  118. void sideload_wait(bool cancel) {
  119. if (cancel) {
  120. sideload_cancelled = true;
  121. }
  122. pthread_join(sideload_thread, nullptr);
  123. }
  124. int sideload_install(bool* wipe_cache, const char* install_file, bool verify) {
  125. int result = INSTALL_ERROR;
  126. if (sideload_started) {
  127. modified_flash = true;
  128. set_perf_mode(true);
  129. result = install_package(FUSE_SIDELOAD_HOST_PATHNAME,
  130. wipe_cache,
  131. install_file,
  132. false, 0, verify);
  133. set_perf_mode(false);
  134. }
  135. return result;
  136. }
  137. void sideload_stop() {
  138. // Ensure adb exits
  139. int status;
  140. kill(sideload_adb_pid, SIGTERM);
  141. waitpid(sideload_adb_pid, &status, 0);
  142. sideload_started = false;
  143. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  144. if (WEXITSTATUS(status) == 3) {
  145. ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
  146. } else if (!WIFSIGNALED(status)) {
  147. ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
  148. }
  149. }
  150. ui->FlushKeys();
  151. maybe_restart_adbd();
  152. }