forked from facebookarchive/fb-adb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.c
More file actions
107 lines (94 loc) · 3 KB
/
peer.c
File metadata and controls
107 lines (94 loc) · 3 KB
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
/*
* 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 <assert.h>
#include "peer.h"
struct child*
start_peer(
const struct start_peer_info* spi,
struct strlist* stub_arguments)
{
SCOPED_RESLIST(rl);
struct cmd_rcmd_self_info rcmdi;
memset(&rcmdi, 0, sizeof (rcmdi));
rcmdi.adb = spi->adb;
rcmdi.transport = spi->transport;
rcmdi.user = spi->user;
rcmdi.cwd = spi->cwd;
rcmdi.args = strlist_to_argv(stub_arguments);
struct strlist* local_self_args = strlist_new();
strlist_append(local_self_args, orig_argv0);
strlist_xfer(local_self_args,
make_args_cmd_rcmd_self(
CMD_ARG_ALL | CMD_ARG_NAME,
&rcmdi));
struct child_start_info csi = {
.io[STDIN_FILENO] = ( spi->specified_io
? spi->io[STDIN_FILENO]
: CHILD_IO_PIPE ),
.io[STDOUT_FILENO] = ( spi->specified_io
? spi->io[STDOUT_FILENO]
: CHILD_IO_PIPE ),
.io[STDERR_FILENO] = CHILD_IO_RECORD,
.exename = my_exe(),
.argv = strlist_to_argv(local_self_args),
};
WITH_CURRENT_RESLIST(rl->parent);
struct child* c = child_start(&csi);
install_child_error_converter(c);
return c;
}
static void
send_file_to_device_pre_exec(void* data)
{
int fd = (intptr_t) data;
if (dup2(fd, STDIN_FILENO) == -1)
die_errno("dup2");
}
void
send_file_to_device(
const struct adb_opts* adb,
const struct transport_opts* transport,
const struct user_opts* user,
int fd,
const char* device_file_name,
mode_t mode)
{
SCOPED_RESLIST(rl);
dbg("sending file to device: remote name [%s]",
device_file_name);
struct strlist* args = strlist_new();
strlist_append(args, orig_argv0);
strlist_append(args, "fput");
struct cmd_fput_info fpi = {
.adb = *adb,
.transport = *transport,
.user = *user,
.xfer.write_mode = "atomic",
.xfer.mode = xaprintf("%o", mode),
.local = "-",
.remote = device_file_name,
};
strlist_xfer(args, make_args_cmd_fput(CMD_ARG_ALL, &fpi));
struct child_start_info csi = {
.io[STDIN_FILENO] = CHILD_IO_DEV_NULL,
.io[STDOUT_FILENO] = CHILD_IO_DEV_NULL,
.io[STDERR_FILENO] = CHILD_IO_RECORD,
.pre_exec = send_file_to_device_pre_exec,
.pre_exec_data = (void*) (intptr_t) fd,
.exename = my_exe(),
.argv = strlist_to_argv(args),
};
struct child* child = child_start(&csi);
install_child_error_converter(child);
int exit_code = child_status_to_exit_code(child_wait(child));
if (exit_code != 0)
die(ECOMM, "failed sending file to device");
}