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
/
cmd_xdex.c
278 lines (253 loc) · 7.55 KB
/
cmd_xdex.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include "util.h"
#include "autocmd.h"
#include "constants.h"
#include "fs.h"
#include "xfer.h"
#include "child.h"
#include "peer.h"
#include "dex.h"
#if FBADB_MAIN
#include "agent.h"
static const char* const xdex_path_suffixes[] = {"", ".dex", ".dex.jar" };
static int
search_xdex_path(
const char* base,
const char** found_program,
const char* path)
{
SCOPED_RESLIST(rl);
char* saveptr = NULL;
for (const char* path_ent = strtok_r(xstrdup(path), ":", &saveptr);
path_ent != NULL;
path_ent = strtok_r(NULL, ":", &saveptr))
{
SCOPED_RESLIST(rl_each_path);
for (size_t i = 0; i < ARRAYSIZE(xdex_path_suffixes); ++i) {
const char* suffix = xdex_path_suffixes[i];
const char* candidate =
xaprintf("%s/%s%s", path_ent, base, suffix);
int fd = try_xopen(candidate, O_RDONLY, 0);
if (fd != -1) {
WITH_CURRENT_RESLIST(rl->parent);
*found_program = xstrdup(candidate);
return xdup(fd);
}
}
}
die(ENOENT, "could not find dex program `%s' on path", base);
}
enum dex_format {
DEX_FORMAT_UNKNOWN,
DEX_FORMAT_DEX,
DEX_FORMAT_DEX_JAR,
};
static enum dex_format
check_dex_format(int fd)
{
char hdr[4];
size_t hdrsz = read_all(fd, hdr, sizeof (hdr));
if (hdrsz == 4) {
if (memcmp(hdr, "PK\x03\x04", 4) == 0)
return DEX_FORMAT_DEX_JAR; // Any zip .dex.jar? Close enough.
if (memcmp(hdr, "\x64\x65\x78\0a", 4) == 0)
return DEX_FORMAT_DEX;
}
return DEX_FORMAT_UNKNOWN;
}
static char*
run_xdex_stub(const struct adb_opts* adb,
const struct transport_opts* transport,
const struct user_opts* user,
const char* name)
{
SCOPED_RESLIST(rl);
struct start_peer_info spi = {
// Not cwd: we want default in case we have a package
.adb = *adb,
.transport = *transport,
.user = *user,
.specified_io = true,
.io[STDIN_FILENO] = CHILD_IO_DEV_NULL,
.io[STDOUT_FILENO] = CHILD_IO_PIPE,
};
struct cmd_xdex_stub_info stub_args = {
.name = name,
};
struct child* peer = start_peer(
&spi,
make_args_cmd_xdex_stub(
CMD_ARG_NAME | CMD_ARG_ALL,
&stub_args));
char* resp = slurp_fd(peer->fd[STDOUT_FILENO]->fd, NULL);
child_wait_die_on_error(peer);
WITH_CURRENT_RESLIST(rl->parent);
return xstrdup(resp);
}
int
xdex_main(const struct cmd_xdex_info* info)
{
if (info->program[0] == '\0')
usage_error("PROGRAM option to xdex cannot be empty");
if (info->shex.exename)
usage_error("--exename makes no sense with xdex");
int program_fd;
const char* found_program;
if (strchr(info->program, '/') != NULL) {
program_fd = xopen(info->program, O_RDONLY, 0);
found_program = info->program;
} else {
program_fd = search_xdex_path(
info->program,
&found_program,
getenv("FB_ADB_XDEX_PATH") ?: ".");
}
switch (check_dex_format(program_fd)) {
default:
abort();
case DEX_FORMAT_UNKNOWN:
die(EINVAL,
"dex program `%s' has unknown format",
found_program);
case DEX_FORMAT_DEX:
die(EINVAL,
"dex program `%s' must be a jar file, not raw dex",
found_program);
case DEX_FORMAT_DEX_JAR:
break;
}
xrewindfd(program_fd);
struct sha256_hash program_hash = sha256_fd(program_fd);
char* found_program_basename = xbasename(found_program);
char* dot = strchr(found_program_basename, '.');
if (dot != NULL)
*dot = '\0';
if (found_program_basename[0] == '\0')
die(EINVAL, "empty xdex basename");
char* name = xaprintf(
"%s-%s",
found_program_basename,
hex_encode_bytes(
program_hash.digest,
sizeof (program_hash.digest) / 2));
char* resp = run_xdex_stub(
&info->adb,
&info->transport,
&info->user,
name);
dbg("resp from xdex stub: [%s]", resp);
char status = *resp++;
if (status != 'Y' && status != 'N')
die(ECOMM, "invalid stub response code %d", (int) status);
char* full_dex_jar_path = resp;
if (status == 'N') {
xrewindfd(program_fd);
send_file_to_device(
&info->adb,
&info->transport,
&info->user,
program_fd,
full_dex_jar_path,
0600);
resp = run_xdex_stub(
&info->adb,
&info->transport,
&info->user,
name);
if (*resp++ != 'Y')
die(EINVAL, "dex state not acceptable after upload");
full_dex_jar_path = resp;
}
set_prgname("rdex");
struct cmd_rdex_info rdex_info = {
.rdex.no_compile = 1,
.adb = info->adb,
.transport = info->transport,
.user = info->user,
.cwd = info->cwd,
.shex = info->shex,
.dexfile = full_dex_jar_path,
.classname = info->classname,
.args = info->args,
};
return rdex_main(&rdex_info);
}
int
agent_main(const struct cmd_agent_info* info)
{
char* resp = run_xdex_stub(
&info->adb,
&info->transport,
&info->user,
agent_name);
char status = *resp++;
char* full_dex_jar_path = resp;
if (status != 'Y' && status != 'N')
die(ECOMM, "invalid stub response code %d", (int) status);
if (status == 'N') {
SCOPED_RESLIST(send_rl);
const char* program_tmpname;
int program_fd = xnamed_tempfile(&program_tmpname);
write_all(program_fd, agent_dex_jar, agent_dex_jar_size);
xrewindfd(program_fd);
send_file_to_device(
&info->adb,
&info->transport,
&info->user,
program_fd,
full_dex_jar_path,
0600);
resp = run_xdex_stub(
&info->adb,
&info->transport,
&info->user,
agent_name);
if (*resp++ != 'Y')
die(EINVAL, "dex state not acceptable after upload");
WITH_CURRENT_RESLIST(send_rl->parent);
full_dex_jar_path = xstrdup(resp);
}
set_prgname("rdex");
struct cmd_rdex_info rdex_info = {
.rdex.no_compile = 1,
.adb = info->adb,
.transport = info->transport,
.user = info->user,
.dexfile = full_dex_jar_path,
.classname = "com.facebook.fbadb.agent.Agent",
.args = info->args,
};
return rdex_main(&rdex_info);
}
#else // !FBADB_MAIN below
int
xdex_stub_main(const struct cmd_xdex_stub_info* info) {
set_prgname("");
const char* name = info->name;
const char* dex_jar_name = xaprintf("xdex-%s.dex.jar", name);
const char* mydir = my_fb_adb_directory();
const char* dex_jar_fullname = xaprintf("%s/%s", mydir, dex_jar_name);
char status = 'N';
if (access(dex_jar_fullname, F_OK) == 0) {
compile_dex(dex_jar_fullname);
status = 'Y';
}
xprintf(xstdout, "%c", status);
xprintf(xstdout, "%s", dex_jar_fullname);
xflush(xstdout);
return 0;
}
#endif