aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'adb_install.cpp')
-rw-r--r--adb_install.cpp189
1 files changed, 93 insertions, 96 deletions
diff --git a/adb_install.cpp b/adb_install.cpp
index 79b8df91..e7d7758f 100644
--- a/adb_install.cpp
+++ b/adb_install.cpp
@@ -14,124 +14,121 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#include <unistd.h> 17#include "adb_install.h"
18#include <dirent.h> 18
19#include <errno.h> 19#include <errno.h>
20#include <fcntl.h>
21#include <signal.h>
20#include <stdlib.h> 22#include <stdlib.h>
21#include <string.h> 23#include <string.h>
24#include <sys/stat.h>
22#include <sys/types.h> 25#include <sys/types.h>
23#include <sys/wait.h> 26#include <sys/wait.h>
24#include <sys/stat.h> 27#include <unistd.h>
25#include <signal.h> 28
26#include <fcntl.h> 29#include <android-base/properties.h>
27 30
28#include "ui.h"
29#include "install.h"
30#include "common.h" 31#include "common.h"
31#include "adb_install.h"
32#include "minadbd/fuse_adb_provider.h"
33#include "fuse_sideload.h" 32#include "fuse_sideload.h"
33#include "install.h"
34#include "ui.h"
34 35
35#include <android-base/properties.h> 36static void set_usb_driver(bool enabled) {
36 37 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
37static void set_usb_driver(RecoveryUI* ui, bool enabled) { 38 if (fd < 0) {
38 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY); 39 ui->Print("failed to open driver control: %s\n", strerror(errno));
39 if (fd < 0) { 40 return;
40 ui->Print("failed to open driver control: %s\n", strerror(errno)); 41 }
41 return; 42 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
42 } 43 ui->Print("failed to set driver control: %s\n", strerror(errno));
43 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) { 44 }
44 ui->Print("failed to set driver control: %s\n", strerror(errno)); 45 if (close(fd) < 0) {
45 } 46 ui->Print("failed to close driver control: %s\n", strerror(errno));
46 if (close(fd) < 0) { 47 }
47 ui->Print("failed to close driver control: %s\n", strerror(errno));
48 }
49} 48}
50 49
51static void stop_adbd(RecoveryUI* ui) { 50static void stop_adbd() {
52 ui->Print("Stopping adbd...\n"); 51 ui->Print("Stopping adbd...\n");
53 android::base::SetProperty("ctl.stop", "adbd"); 52 android::base::SetProperty("ctl.stop", "adbd");
54 set_usb_driver(ui, false); 53 set_usb_driver(false);
55} 54}
56 55
57static void maybe_restart_adbd(RecoveryUI* ui) { 56static void maybe_restart_adbd() {
58 if (is_ro_debuggable()) { 57 if (is_ro_debuggable()) {
59 ui->Print("Restarting adbd...\n"); 58 ui->Print("Restarting adbd...\n");
60 set_usb_driver(ui, true); 59 set_usb_driver(true);
61 android::base::SetProperty("ctl.start", "adbd"); 60 android::base::SetProperty("ctl.start", "adbd");
62 } 61 }
63} 62}
64 63
65// How long (in seconds) we wait for the host to start sending us a 64int apply_from_adb(bool* wipe_cache, const char* install_file) {
66// package, before timing out. 65 modified_flash = true;
67#define ADB_INSTALL_TIMEOUT 300 66
68 67 stop_adbd();
69int apply_from_adb(RecoveryUI* ui, bool* wipe_cache, const char* install_file) { 68 set_usb_driver(true);
70 modified_flash = true; 69
71 70 ui->Print(
72 stop_adbd(ui); 71 "\n\nNow send the package you want to apply\n"
73 set_usb_driver(ui, true); 72 "to the device with \"adb sideload <filename>\"...\n");
74 73
75 ui->Print("\n\nNow send the package you want to apply\n" 74 pid_t child;
76 "to the device with \"adb sideload <filename>\"...\n"); 75 if ((child = fork()) == 0) {
77 76 execl("/sbin/recovery", "recovery", "--adbd", nullptr);
78 pid_t child; 77 _exit(EXIT_FAILURE);
79 if ((child = fork()) == 0) { 78 }
80 execl("/sbin/recovery", "recovery", "--adbd", NULL); 79
81 _exit(EXIT_FAILURE); 80 // How long (in seconds) we wait for the host to start sending us a package, before timing out.
81 static constexpr int ADB_INSTALL_TIMEOUT = 300;
82
83 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host connects and starts serving a
84 // package. Poll for its appearance. (Note that inotify doesn't work with FUSE.)
85 int result = INSTALL_ERROR;
86 int status;
87 bool waited = false;
88 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
89 if (waitpid(child, &status, WNOHANG) != 0) {
90 result = INSTALL_ERROR;
91 waited = true;
92 break;
82 } 93 }
83 94
84 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
85 // connects and starts serving a package. Poll for its
86 // appearance. (Note that inotify doesn't work with FUSE.)
87 int result = INSTALL_ERROR;
88 int status;
89 bool waited = false;
90 struct stat st; 95 struct stat st;
91 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) { 96 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
92 if (waitpid(child, &status, WNOHANG) != 0) { 97 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) {
93 result = INSTALL_ERROR; 98 sleep(1);
94 waited = true; 99 continue;
95 break; 100 } else {
96 } 101 ui->Print("\nTimed out waiting for package.\n\n");
97 102 result = INSTALL_ERROR;
98 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { 103 kill(child, SIGKILL);
99 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
100 sleep(1);
101 continue;
102 } else {
103 ui->Print("\nTimed out waiting for package.\n\n");
104 result = INSTALL_ERROR;
105 kill(child, SIGKILL);
106 break;
107 }
108 }
109 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0);
110 break; 104 break;
105 }
111 } 106 }
107 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0);
108 break;
109 }
112 110
113 if (!waited) { 111 if (!waited) {
114 // Calling stat() on this magic filename signals the minadbd 112 // Calling stat() on this magic filename signals the minadbd subprocess to shut down.
115 // subprocess to shut down. 113 struct stat st;
116 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); 114 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
117 115
118 // TODO(dougz): there should be a way to cancel waiting for a 116 // TODO: there should be a way to cancel waiting for a package (by pushing some button combo on
119 // package (by pushing some button combo on the device). For now 117 // the device). For now you just have to 'adb sideload' a file that's not a valid package, like
120 // you just have to 'adb sideload' a file that's not a valid 118 // "/dev/null".
121 // package, like "/dev/null". 119 waitpid(child, &status, 0);
122 waitpid(child, &status, 0); 120 }
123 } 121
124 122 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
125 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { 123 if (WEXITSTATUS(status) == 3) {
126 if (WEXITSTATUS(status) == 3) { 124 ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
127 ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n"); 125 } else if (!WIFSIGNALED(status)) {
128 } else if (!WIFSIGNALED(status)) { 126 ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
129 ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
130 }
131 } 127 }
128 }
132 129
133 set_usb_driver(ui, false); 130 set_usb_driver(false);
134 maybe_restart_adbd(ui); 131 maybe_restart_adbd();
135 132
136 return result; 133 return result;
137} 134}