This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
adb.c
235 lines (205 loc) · 6.62 KB
/
adb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/wait.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include "adb.h"
#include "child.h"
#include "util.h"
#include "argv.h"
#include "strutil.h"
#include "fs.h"
struct adb_communication {
char* output;
int status;
};
static struct adb_communication
run_adb(const char* const* adb_args,
const char* args[])
{
struct child_start_info csi = {
.io[STDIN_FILENO] = CHILD_IO_DEV_NULL,
.io[STDOUT_FILENO] = CHILD_IO_PIPE,
.io[STDERR_FILENO] = CHILD_IO_DUP_TO_STDOUT,
.exename = "adb",
.argv = ARGV_CONCAT(ARGV("adb"),
adb_args ?: empty_argv,
args ?: empty_argv)
};
struct child* adb = child_start(&csi);
return (struct adb_communication) {
.output = massage_output_buf(slurp_fd_buf(adb->fd[1]->fd)),
.status = child_wait(adb),
};
}
void
adb_send_file(const char* local,
const char* remote,
const char* const* adb_args)
{
SCOPED_RESLIST(rl);
struct adb_communication com =
run_adb(adb_args, ARGV("push", local, remote));
if (!child_status_success_p(com.status))
die(ECOMM, "adb error: %s", com.output);
}
void
adb_rename_file(const char* old_name,
const char* new_name,
unsigned api_level,
unsigned rename_flags,
const char* const* adb_args)
{
SCOPED_RESLIST(rl);
if (!shell_safe_word_p(old_name))
die(EINVAL, "invalid shell word: [%s]", old_name);
if (!shell_safe_word_p(new_name))
die(EINVAL, "invalid shell word: [%s]", new_name);
// Old versions of Android don't support mv -f, but treat stdin of
// /dev/null as a clue to not prompt on overwrite. Google somehow
// managed to screw up mv(1) in API 23 and above so that the
// </dev/null trick doesn't work for suppressing the overwrite
// prompt --- instead, it makes mv delete(!) the source file.
// Let's just make the move version-dependent. Split on version
// 19, because that version does support mv -f and we want to
// catch all possible breakage.
struct adb_communication com =
run_adb(adb_args,
api_level > 19
? ARGV("shell",
"mv",
"-f",
old_name,
new_name,
"&&",
"echo",
"yes")
: ARGV("shell",
"</dev/null",
"mv",
old_name,
new_name,
"&&",
"echo",
"yes"));
const char* output = com.output;
if (!child_status_success_p(com.status) || strcmp(output, "yes") != 0) {
if (string_ends_with_p(output, ": Permission denied") &&
(rename_flags & ADB_RENAME_FALL_BACK_TO_CAT))
{
dbg("device has screwed up SELinux policy; falling back to copying");
char* cmd = xaprintf("cat %s > %s && chmod 755 %s && rm -f %s",
old_name,
new_name,
new_name,
old_name);
struct adb_communication catcom = run_adb(adb_args, ARGV("shell", cmd));
if (child_status_success_p(catcom.status)) {
return;
}
}
die(ECOMM, "moving fb-adb to final location failed: %s", output);
}
}
void
adb_add_forward(const char* local,
const char* remote,
const char* const* adb_args)
{
struct adb_communication com =
run_adb(adb_args, ARGV("forward", local, remote));
if (!child_status_success_p(com.status))
die(ECOMM, "adb_add_forward failed: %s", com.output);
}
void
adb_remove_forward(const char* local,
const char* const* adb_args)
{
struct adb_communication com =
run_adb(adb_args, ARGV("forward", "--remove", local));
if (!child_status_success_p(com.status))
die(ECOMM, "adb_remove_forward failed: %s", com.output);
}
struct remove_forward_cleanup {
struct cleanup* cl;
const char* local;
const char* const* adb_args;
};
static void
remove_forward_cleanup_1(void* data)
{
struct remove_forward_cleanup* crf = data;
adb_remove_forward(crf->local, crf->adb_args);
}
static void
remove_forward_cleanup(void* data)
{
SCOPED_RESLIST(rl);
(void) catch_error(remove_forward_cleanup_1, data, NULL);
}
struct remove_forward_cleanup*
remove_forward_cleanup_allocate(
const char* local,
const char* const* adb_args)
{
struct remove_forward_cleanup* crf = xcalloc(sizeof (*crf));
crf->cl = cleanup_allocate();
crf->local = xstrdup(local);
crf->adb_args = argv_concat_deepcopy(adb_args, NULL);
return crf;
}
void
remove_forward_cleanup_commit(struct remove_forward_cleanup* rfc)
{
cleanup_commit(rfc->cl, remove_forward_cleanup, rfc);
}
char*
adb_getprop(const char* property, const char* const* adb_args)
{
SCOPED_RESLIST(rl);
struct adb_communication com = run_adb(
adb_args,
ARGV("shell", "getprop", property));
char* output = com.output;
if (!child_status_success_p(com.status))
die(ECOMM, "adb error: %s", com.output);
const char* ws = " \t\r\n";
while (strchr(ws, *output))
++output;
size_t output_length = strlen(output);
while (output_length > 0 && strchr(ws, output[output_length-1]))
output_length -= 1;
if (output_length == 0)
die(EINVAL, "property %s missing or empty", property);
WITH_CURRENT_RESLIST(rl->parent);
char* value = xstrndup(output, output_length);
dbg("property [%s] = [%s]", property, value);
return value;
}
unsigned
adb_api_level(const char* const* adb_args)
{
SCOPED_RESLIST(rl);
char* api_level_string = adb_getprop("ro.build.version.sdk", adb_args);
char* endptr;
errno = 0;
unsigned long api_level = strtoul(api_level_string, &endptr, 10);
if (*endptr != '\0' || errno != 0 || api_level > UINT_MAX)
die(EINVAL,
"bogus api level property value: [%s]",
api_level_string);
return (unsigned) api_level;
}