forked from facebookarchive/fb-adb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_shex.c
2470 lines (2113 loc) · 70.5 KB
/
cmd_shex.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <sys/types.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <sys/socket.h>
#include "util.h"
#include "child.h"
#include "ringbuf.h"
#include "proto.h"
#include "core.h"
#include "channel.h"
#include "xmkraw.h"
#include "termbits.h"
#include "adbenc.h"
#include "constants.h"
#include "adb.h"
#include "chat.h"
#include "stubs.h"
#include "timestamp.h"
#include "argv.h"
#include "autocmd.h"
#include "strutil.h"
#include "net.h"
#include "xmkraw.h"
#include "fs.h"
#include "elfid.h"
#include "errcodes.h"
#include "peer.h"
#include "fdrecorder.h"
#define ARG_DEFAULT_SH ((const char*)MSG_CMDLINE_DEFAULT_SH)
#define ARG_DEFAULT_SH_LOGIN ((const char*)MSG_CMDLINE_DEFAULT_SH_LOGIN)
#define ARG_EXEC_FILE ((const char*)MSG_CMDLINE_EXEC_FILE)
#define ARG_CMDLINE_SELF_ARGV0 ((const char*)MSG_CMDLINE_SELF_ARGV0)
enum transport_type {
transport_shell,
transport_unix,
transport_tcp,
#ifdef HAVE_LOCAL_STUB
transport_local,
#endif
};
struct transport {
enum transport_type type;
const char* tcp_addr;
};
typedef void (*writer_function)(int, const void*, size_t);
struct childcom {
struct fdh* to_child;
struct fdh* from_child;
writer_function writer;
};
struct adb_info {
const char* const* adb_args;
const struct adb_opts* adb_opts;
};
static void
tc_write(const struct childcom* tc,
const void* buf,
size_t sz)
{
tc->writer(tc->to_child->fd, buf, sz);
}
static void
tc_sendmsg(const struct childcom* tc,
const struct msg* m)
{
dbgmsg(m, "tc_sendmsg");
tc_write(tc, m, m->size);
}
static struct msg*
tc_recvmsg(const struct childcom* tc)
{
return read_msg(tc->from_child->fd, read_all);
}
static struct chat*
tc_chat_new(const struct childcom* tc)
{
return chat_new(tc->to_child->fd, tc->from_child->fd);
}
struct child_hello {
char ver[FB_ADB_FINGERPRINT_LENGTH+1];
unsigned abi_mask;
unsigned uid;
unsigned api_level;
};
struct fb_adb_shex {
struct fb_adb_sh sh;
int child_exit_status;
bool child_exited;
};
static bool
parse_child_hello(const char* line, struct child_hello* chello)
{
int n = -1;
sscanf(line, FB_ADB_PROTO_START_LINE "%n",
&chello->ver[0],
&chello->abi_mask,
&chello->uid,
&chello->api_level,
&n);
return n != -1;
}
#ifdef HAVE_LOCAL_STUB
static struct child*
start_stub_local(struct child_hello* chello)
{
SCOPED_RESLIST(rl);
// First, if we're being run from our build directory, try just
// running the unstripped stub binary directly.
const char* stub_exe_name = NULL;
int exefd = -1;
stub_exe_name =
xaprintf(
"%s/stub-local/fb-adb",
xdirname(orig_argv0));
exefd = try_xopen(stub_exe_name, O_RDONLY, 0);
dbg("trying build dir local stub [%s]: %d", stub_exe_name, exefd);
if (exefd != -1 && xfstat(exefd).st_uid != getuid()) {
dbg("not using build directory exe: uid mismatch");
exefd = -1;
}
if (exefd == -1) {
stub_exe_name = xaprintf("%s/local-stub", my_fb_adb_directory());
dbg("extracting local stub to private file [%s]", stub_exe_name);
{
SCOPED_RESLIST(rl_tempwrite);
const char* stub_exe_tmp_name = xaprintf(
"%s.tmp.%s",
stub_exe_name,
gen_hex_random(ENOUGH_ENTROPY));
int tmpfd = xopen(stub_exe_tmp_name,
O_CREAT | O_EXCL | O_WRONLY,
0700);
write_all(tmpfd, stubs[0].data, stubs[0].size);
WITH_CURRENT_RESLIST(rl);
exefd = xopen(stub_exe_tmp_name, O_RDONLY, 0);
xrename(stub_exe_tmp_name, stub_exe_name);
}
}
allow_inherit(exefd);
const struct child_start_info csi = {
.io[STDIN_FILENO] = CHILD_IO_PIPE,
.io[STDOUT_FILENO] = CHILD_IO_PIPE,
.io[STDERR_FILENO] = CHILD_IO_RECORD,
.exename = xaprintf("/proc/self/fd/%d", exefd),
.argv = ARGV(stub_exe_name, "stub"),
};
SCOPED_RESLIST(rl_child);
struct child* child = child_start(&csi);
install_child_error_converter(child);
WITH_CURRENT_RESLIST(rl);
char* resp;
struct chat* cc = chat_new(
child->fd[STDIN_FILENO]->fd,
child->fd[STDOUT_FILENO]->fd);
do {
resp = chat_read_line(cc);
} while (clowny_output_line_p(resp));
if (!parse_child_hello(resp, chello))
die(ECOMM, "trouble with local adb stub: %s", resp);
if (strcmp(chello->ver, build_fingerprint) != 0)
die(ERR_FINGERPRINT_MISMATCH, "stale stub?!");
reslist_xfer(rl->parent, rl_child);
return child;
}
#endif
static void
send_stub(const void* data,
size_t datasz,
const char* const* adb_args,
const char* adb_name)
{
SCOPED_RESLIST(rl);
const char* tmpfilename;
int tmpfile = xnamed_tempfile(&tmpfilename);
write_all(tmpfile, data, datasz);
// N.B. The device-side adb server helpfully copies the user
// permission bits to group and world, so if we were to make this
// file writable for us locally, we'd actually be making it
// world-writable on device!
if (fchmod(tmpfile, 0555 /* -r-xr-xr-x */) == -1)
die_errno("fchmod");
adb_send_file(tmpfilename, adb_name, adb_args);
}
static struct child*
try_adb_stub(const struct child_start_info* csi,
const char* adb_name,
struct child_hello* chello,
char** err)
{
SCOPED_RESLIST(rl);
struct child* child = child_start(csi);
install_child_error_converter(child);
struct chat* cc = chat_new(
child->fd[STDIN_FILENO]->fd,
child->fd[STDOUT_FILENO]->fd);
chat_swallow_prompt(cc);
*err = NULL;
// We choose adb_name such that it doesn't need to be quoted
char* cmd = NULL;
#ifndef NDEBUG
const char* remote_wrapper = getenv("FB_ADB_REMOTE_WRAPPER");
if (remote_wrapper)
adb_name = xaprintf("%s %s", remote_wrapper, adb_name);
const char* remote_debug = getenv("FB_ADB_REMOTE_DEBUG");
if (remote_debug) {
if (remote_debug[0] != '>')
die(EINVAL, "remote debugging must dump to file: "
"otherwise, debug output will interfere with "
"fb-adb protocol.");
cmd = xaprintf("FB_ADB_DEBUG='%s' exec %s stub",
remote_debug,
adb_name);
}
#endif
if (cmd == NULL)
cmd = xaprintf("exec %s stub", adb_name);
dbg("cmd for stub: [%s]", cmd);
unsigned promptw = 40;
if (strlen(cmd) > promptw) {
// The extra round trip sucks, but if we don't do this, mksh's
// helpful line editing will screw up our echo detection.
unsigned long total_promptw = promptw + strlen(cmd);
chat_talk_at(cc, xaprintf("COLUMNS=%lu", total_promptw));
chat_swallow_prompt(cc);
}
chat_talk_at(cc, cmd);
char* resp = chat_read_line(cc);
dbg("stub resp: [%s]", resp);
if (parse_child_hello(resp, chello) &&
!strcmp(chello->ver, build_fingerprint))
{
dbg("found good child version");
reslist_xfer(rl->parent, rl);
return child;
}
child_kill(child, SIGTERM);
(void) child_wait(child);
WITH_CURRENT_RESLIST(rl->parent);
*err = xstrdup(resp);
return NULL;
}
struct delete_device_tmpfile {
const char** adb_args;
char* device_filename;
};
static void
delete_device_tmpfile_cleanup_1(void* data)
{
struct delete_device_tmpfile* ddt = data;
const struct child_start_info csi = {
.io[STDIN_FILENO] = CHILD_IO_DEV_NULL,
.io[STDOUT_FILENO] = CHILD_IO_DEV_NULL,
.io[STDERR_FILENO] = CHILD_IO_DEV_NULL,
.exename = "adb",
.argv = ARGV_CONCAT(ARGV("adb"),
ddt->adb_args,
ARGV("shell",
"</dev/null",
"rm",
"-f",
// Chosen not to require quoting
ddt->device_filename)),
};
child_wait(child_start(&csi));
}
__attribute__((unused))
static void
delete_device_tmpfile_cleanup(void* data)
{
SCOPED_RESLIST(rl_cleanup);
(void) catch_error(delete_device_tmpfile_cleanup_1, data, NULL);
}
static void
add_cleanup_delete_device_tmpfile(const char* device_filename,
const char* const* adb_args)
{
struct cleanup* cl = cleanup_allocate();
struct delete_device_tmpfile* ddt = xcalloc(sizeof (*ddt));
ddt->device_filename = xstrdup(device_filename);
ddt->adb_args = argv_concat_deepcopy(adb_args, NULL);
cleanup_commit(cl, delete_device_tmpfile_cleanup, ddt);
}
static struct child*
start_stub_adb(const char* const* adb_args,
struct child_hello* chello)
{
const struct child_start_info csi = {
.io[STDIN_FILENO] = CHILD_IO_PIPE,
.io[STDOUT_FILENO] = CHILD_IO_PIPE,
.io[STDERR_FILENO] = CHILD_IO_RECORD,
.exename = "adb",
.argv = ARGV_CONCAT(ARGV("adb"), adb_args, ARGV("shell")),
};
struct child* child = NULL;
char* err = NULL;
child = try_adb_stub(&csi, FB_ADB_REMOTE_FILENAME, chello, &err);
if (child == NULL) {
char* tmp_adb = xaprintf(
"%s.%s",
FB_ADB_REMOTE_FILENAME,
gen_hex_random(10));
add_cleanup_delete_device_tmpfile(tmp_adb, adb_args);
size_t ns = nr_stubs;
size_t first_stub = 0;
#ifdef HAVE_LOCAL_STUB
first_stub = 1;
#endif
for (unsigned i = first_stub; i < ns && !child; ++i) {
send_stub(stubs[i].data, stubs[i].size, adb_args, tmp_adb);
child = try_adb_stub(&csi, tmp_adb, chello, &err);
}
if (!child)
die(ECOMM, "trouble starting adb stub: %s", err);
child_kill(child, SIGTERM);
child_wait(child);
unsigned api_level = adb_api_level(adb_args);
dbg("device appears to have API level %u", api_level);
adb_rename_file(tmp_adb,
FB_ADB_REMOTE_FILENAME,
api_level,
adb_args);
child = try_adb_stub(&csi, FB_ADB_REMOTE_FILENAME, chello, &err);
if (!child)
die(ECOMM, "trouble starting adb stub: %s", err);
}
return child;
}
static void
shex_process_msg(struct fb_adb_sh* sh, struct msg mhdr)
{
if (mhdr.type == MSG_CHILD_EXIT) {
struct fb_adb_shex* shex = (struct fb_adb_shex*) sh;
struct msg_child_exit m;
read_cmdmsg(sh, mhdr, &m, sizeof (m));
dbgmsg(&m.msg, "recv");
shex->child_exited = true;
shex->child_exit_status = m.exit_status;
return;
}
fb_adb_sh_process_msg(sh, mhdr);
}
static bool
fill_window_size(int fd, struct window_size* ws)
{
int ret;
struct winsize wz;
do {
ret = ioctl(fd, TIOCGWINSZ, &wz);
} while (ret == -1 && errno == EINTR);
if (ret != 0)
return false;
ws->row = wz.ws_row;
ws->col = wz.ws_col;
ws->xpixel = wz.ws_xpixel;
ws->ypixel = wz.ws_ypixel;
return ret == 0;
}
struct tty_flags {
unsigned tty_p : 1;
unsigned want_pty_p : 1;
unsigned compress : 1;
unsigned our_very_own_open_file_description : 1;
};
struct msg_shex_hello*
make_hello_msg(size_t max_cmdsz,
size_t stdio_ringbufsz,
size_t command_ringbufsz,
size_t nr_argv,
struct tty_flags tty_flags[3])
{
struct msg_shex_hello* m;
size_t sz = sizeof (*m) + nr_termbits * sizeof (m->tctl[0]);
m = xcalloc(sz);
m->msg.type = MSG_SHEX_HELLO;
m->nr_argv = nr_argv;
m->maxmsg = XMIN(max_cmdsz, MSG_MAX_SIZE);
m->stub_send_bufsz = command_ringbufsz;
m->stub_recv_bufsz = command_ringbufsz;
for (int i = 0; i < 3; ++i) {
m->si[i].bufsz = stdio_ringbufsz;
m->si[i].pty_p = tty_flags[i].want_pty_p;
m->si[i].compress = tty_flags[i].compress;
}
m->posix_vdisable_value = _POSIX_VDISABLE;
struct term_control* tc = &m->tctl[0];
struct termios in_attr;
struct termios out_attr;
int in_tty = -1;
if (m->si[STDIN_FILENO].pty_p && tty_flags[STDIN_FILENO].tty_p) {
in_tty = STDIN_FILENO;
xtcgetattr(in_tty, &in_attr);
m->ispeed = cfgetispeed(&in_attr);
}
int out_tty = -1;
if (m->si[STDOUT_FILENO].pty_p && tty_flags[STDOUT_FILENO].tty_p)
out_tty = STDOUT_FILENO;
else if (m->si[STDERR_FILENO].pty_p && tty_flags[STDERR_FILENO].tty_p)
out_tty = STDERR_FILENO;
if (out_tty != -1) {
xtcgetattr(out_tty, &out_attr);
m->ospeed = cfgetospeed(&out_attr);
m->have_ws = fill_window_size(out_tty, &m->ws);
dbg("ws %ux%u (%ux%u)",
m->ws.row,
m->ws.col,
m->ws.xpixel,
m->ws.ypixel);
}
for (unsigned i = 0; i < nr_termbits; ++i) {
const struct termbit* tb = &termbits[i];
if (in_tty != -1 && tb->thing == TERM_IFLAG)
tc->value = !!(in_attr.c_iflag & tb->value);
else if (in_tty != -1 && tb->thing == TERM_LFLAG)
tc->value = !!(in_attr.c_lflag & tb->value);
else if (in_tty != -1 && tb->thing == TERM_C_CC)
tc->value = in_attr.c_cc[tb->value];
else if (out_tty != -1 && tb->thing == TERM_OFLAG)
tc->value = !!(out_attr.c_oflag & tb->value);
else
continue;
snprintf(tc->name, sizeof (tc->name), "%s", tb->name);
tc++;
}
m->msg.size = (char*) tc - (char*) m;
return m;
}
static bool saw_sigwinch = false;
static void
handle_sigwinch(int signo)
{
saw_sigwinch = true;
}
struct environ_op {
struct environ_op* next;
const char* name;
const char* value;
};
static struct environ_op*
reverse_environ_ops(struct environ_op* environ_ops)
{
// Reverse the operations list to restore argv order
struct environ_op* new_environ_ops = NULL;
while (environ_ops != NULL) {
struct environ_op* eop = environ_ops;
environ_ops = environ_ops->next;
eop->next = new_environ_ops;
new_environ_ops = eop;
}
return new_environ_ops;
}
static void
send_environ_set(const struct childcom* tc,
const char* name,
const char* value)
{
size_t name_length = strlen(name);
size_t value_length = strlen(value);
struct msg_environment_variable_set* e;
size_t msglen = sizeof (*e) + name_length + 1 + value_length;
if (msglen <= MSG_MAX_SIZE) {
e = alloca(msglen);
memset(e, 0, sizeof (*e));
e->msg.type = MSG_ENVIRONMENT_VARIABLE_SET;
e->msg.size = msglen;
memcpy(&e->value[0], name, name_length);
e->value[name_length] = '\0';
memcpy(&e->value[name_length+1], value, value_length);
tc_sendmsg(tc, &e->msg);
} else {
if (name_length > UINT32_MAX || value_length > UINT32_MAX)
die(EINVAL, "environment variable too long");
struct msg_environment_variable_set_jumbo ej;
memset(&ej, 0, sizeof (ej));
ej.msg.type = MSG_ENVIRONMENT_VARIABLE_SET_JUMBO;
ej.msg.size = sizeof (ej);
ej.name_length = name_length;
ej.value_length = value_length;
tc_sendmsg(tc, &ej.msg);
tc_write(tc, name, name_length);
tc_write(tc, value, value_length);
}
}
static void
send_environ_unset(const struct childcom* tc,
const char* name)
{
size_t name_length = strlen(name);
struct msg_environment_variable_unset* ue;
size_t msglen = sizeof (*ue) + name_length;
if (msglen <= MSG_MAX_SIZE) {
ue = alloca(msglen);
memset(ue, 0, sizeof (*ue));
ue->msg.type = MSG_ENVIRONMENT_VARIABLE_UNSET;
ue->msg.size = msglen;
memcpy(&ue->name, name, name_length);
tc_sendmsg(tc, &ue->msg);
} else {
if (name_length > UINT32_MAX)
die(EINVAL, "environment variable too long");
struct msg_environment_variable_unset_jumbo uej;
memset(&uej, 0, sizeof (uej));
uej.msg.type = MSG_ENVIRONMENT_VARIABLE_UNSET_JUMBO;
uej.msg.size = sizeof (uej);
uej.name_length = name_length;
tc_sendmsg(tc, &uej.msg);
tc_write(tc, name, name_length);
}
}
static void
send_environ_clearenv(const struct childcom* tc)
{
struct msg cev;
memset(&cev, 0, sizeof (cev));
cev.type = MSG_CLEARENV;
cev.size = sizeof (cev);
tc_sendmsg(tc, &cev);
}
static void
send_environ_ops(const struct childcom* tc,
struct environ_op* environ_ops)
{
while (environ_ops != NULL) {
struct environ_op* eop = environ_ops;
environ_ops = environ_ops->next;
if (eop->name && eop->value)
send_environ_set(tc, eop->name, eop->value);
else if (eop->name)
send_environ_unset(tc, eop->name);
else
send_environ_clearenv(tc);
}
}
static void
send_cmdline_argument(const struct childcom* tc, const void* val)
{
struct msg m;
size_t valsz;
size_t totalsz;
uint16_t type;
if (val == ARG_DEFAULT_SH) {
val = NULL;
valsz = 0;
type = MSG_CMDLINE_DEFAULT_SH;
} else if (val == ARG_DEFAULT_SH_LOGIN) {
val = NULL;
valsz = 0;
type = MSG_CMDLINE_DEFAULT_SH_LOGIN;
} else if (val == ARG_EXEC_FILE) {
val = NULL;
valsz = 0;
type = MSG_CMDLINE_EXEC_FILE;
} else if (val == ARG_CMDLINE_SELF_ARGV0) {
val = NULL;
valsz = 0;
type = MSG_CMDLINE_SELF_ARGV0;
} else {
valsz = strlen((const char*)val);
type = MSG_CMDLINE_ARGUMENT;
}
if (type == MSG_CMDLINE_ARGUMENT) {
dbg("sending command line option [%s]", (char*) val);
} else {
dbg("sending special command line option type %d", (int) type);
}
if (SATADD(&totalsz, sizeof (m), valsz) || totalsz > UINT32_MAX)
die(EINVAL, "command line argument too long");
if (totalsz <= MSG_MAX_SIZE) {
m.type = type;
m.size = totalsz;
tc_write(tc, &m, sizeof (m));
tc_write(tc, val, valsz);
} else {
assert(type == MSG_CMDLINE_ARGUMENT);
struct msg_cmdline_argument_jumbo mj;
memset(&mj, 0, sizeof (mj));
mj.msg.type = MSG_CMDLINE_ARGUMENT_JUMBO;
mj.msg.size = sizeof (mj);
mj.actual_size = valsz;
tc_write(tc, &mj, sizeof (mj));
tc_write(tc, val, valsz);
}
}
static void
lim_format_shell_command_line(const char* command,
const char *const* argv,
size_t *pos,
char *buf,
size_t bufsz)
{
if (command != NULL) {
/* Special case: don't quote the first argument. */
lim_strcat(command, pos, buf, bufsz);
}
while (*argv != NULL) {
lim_outc(' ', pos, buf, bufsz);
lim_shellquote(*argv++, pos, buf, bufsz);
}
}
static void
send_cmdline(const struct childcom* tc,
const char* exename,
const char* command,
const char* const* argv)
{
dbg("sending command line");
send_cmdline_argument(tc, exename ?: command);
send_cmdline_argument(tc, command);
while (*argv)
send_cmdline_argument(tc, *argv++);
dbg("done sending command line");
}
static void
command_re_exec_as_root(const struct childcom* tc)
{
SCOPED_RESLIST(rl);
// Tell child to re-exec itself as root. It'll send another hello
// message, which we read below.
struct msg rmsg = {
.size = sizeof (rmsg),
.type = MSG_EXEC_AS_ROOT,
};
tc_sendmsg(tc, &rmsg);
struct chat* cc = tc_chat_new(tc);
char* resp;
do {
resp = chat_read_line(cc);
} while (clowny_output_line_p(resp));
struct child_hello chello;
if (!parse_child_hello(resp, &chello))
die(ECOMM, "trouble re-execing adb stub as root: %s", resp);
if (chello.uid != 0)
die(ECOMM, "told child to re-exec as root; gave us uid=%d",
chello.uid);
}
struct re_exec_as_user_ctx {
const struct childcom* tc;
const char* want_user;
bool shell_thunk;
};
static void
command_re_exec_as_user_1(void* arg)
{
const struct re_exec_as_user_ctx* info = arg;
SCOPED_RESLIST(rl_re_exec_as_user);
// Tell child to re-exec itself as our user. It'll send another
// hello message, which we read below.
const char* want_user = info->want_user;
const struct childcom* tc = info->tc;
struct msg_exec_as_user* m;
size_t want_user_length = strlen(want_user);
size_t alloc_size = sizeof (*m);
if (SATADD(&alloc_size, alloc_size, want_user_length) ||
alloc_size > MSG_MAX_SIZE)
{
die(EINVAL, "username too long");
}
m = xcalloc(alloc_size);
m->msg.size = alloc_size;
m->msg.type = MSG_EXEC_AS_USER;
m->shell_thunk = info->shell_thunk;
memcpy(m->username, want_user, want_user_length);
tc_sendmsg(tc, &m->msg);
struct chat* cc = tc_chat_new(tc);
char* resp;
do {
resp = chat_read_line(cc);
} while (clowny_output_line_p(resp));
struct child_hello chello;
if (!parse_child_hello(resp, &chello))
die(ECOMM, "trouble re-execing adb stub as %s: %s",
want_user, resp);
if (strcmp(chello.ver, build_fingerprint) != 0)
die(ERR_FINGERPRINT_MISMATCH, "stale child");
}
static bool
re_exec_error_permission_denied_p(const struct errinfo* ei)
{
return (ei->err == ECOMM &&
string_starts_with_p(ei->msg, "trouble re-execing adb stub as ") &&
strstr(ei->msg, ": exec failed ") &&
string_ends_with_p(ei->msg, ": Permission denied"));
}
static bool
re_exec_error_package_unknown_p(const struct errinfo* ei)
{
return (ei->err == ECOMM &&
string_ends_with_p(ei->msg, "' is unknown"));
}
static void
send_chdir(const struct childcom* tc,
const char* child_chdir)
{
size_t dirsz;
size_t totalsz;
struct msg_chdir mchd;
dirsz = strlen(child_chdir);
if (SATADD(&totalsz, dirsz, sizeof (mchd))
|| totalsz > MSG_MAX_SIZE)
{
die(EINVAL, "directory too long");
}
memset(&mchd, 0, sizeof (mchd));
mchd.msg.size = totalsz;
mchd.msg.type = MSG_CHDIR;
dbg("sending chdir to [%s] %hu", child_chdir, mchd.msg.size);
tc_write(tc, &mchd, sizeof (mchd));
tc_write(tc, child_chdir, dirsz);
}
static void
send_rebind_to_unix_socket_message(const struct childcom* tc,
const char* device_socket)
{
size_t device_socket_length = strlen(device_socket);
size_t msgsz;
struct msg_rebind_to_unix_socket rm;
if (SATADD(&msgsz, sizeof (rm), device_socket_length) ||
msgsz > MSG_MAX_SIZE)
{
die(ERANGE, "socket name too long");
}
memset(&rm, 0, sizeof (rm));
rm.msg.type = MSG_REBIND_TO_UNIX_SOCKET;
rm.msg.size = msgsz;
tc_write(tc, &rm.msg, sizeof (rm));
tc_write(tc, device_socket, device_socket_length);
}
static struct childcom*
connect_to_device_socket(
const char* const* adb_args,
const char* device_socket)
{
SCOPED_RESLIST(rl);
char* host_socket =
xaprintf("%s/fb-adb-conn-%s.sock",
system_tempdir(),
gen_hex_random(ENOUGH_ENTROPY));
char* remote = xaprintf("localabstract:%s", device_socket);
char* local = xaprintf("localfilesystem:%s", host_socket);
struct cleanup* ucl = cleanup_allocate();
struct remove_forward_cleanup* crf =
remove_forward_cleanup_allocate(local, adb_args);
adb_add_forward(local, remote, adb_args);
cleanup_commit(ucl, unlink_cleanup, host_socket);
remove_forward_cleanup_commit(crf);
int scon = xsocket(AF_UNIX, SOCK_STREAM, 0);
xconnect(scon, make_addr_unix_filesystem(host_socket));
WITH_CURRENT_RESLIST(rl->parent);
struct childcom* ntc = xcalloc(sizeof (*ntc));
ntc->from_child = fdh_dup(scon);
ntc->to_child = fdh_dup(scon);
ntc->writer = write_all;
dbg("AF_UNIX connection local:%s remote:%s", local, remote);
return ntc;
}
static struct childcom*
reconnect_over_unix_socket(
const struct childcom* tc,
const char* const* adb_args)
{
SCOPED_RESLIST(rl);
// N.B. We can't use reverse forwarding (having the stub connect
// to us) because Gingerbread doesn't support reverse forwarding.
// We need to wait for a reply from the child to ensure that we
// don't race against the stub's call to listen().
char* device_socket =
xaprintf("%s/fb-adb-conn-%s.sock",
DEVICE_TEMP_DIR,
gen_hex_random(10));
send_rebind_to_unix_socket_message(tc, device_socket);
struct msg* reply = tc_recvmsg(tc);
if (reply->type != MSG_LISTENING_ON_SOCKET)
die(ECOMM, "child sent incorrect reply %u to socket bind",
(unsigned) reply->type);
WITH_CURRENT_RESLIST(rl->parent);
return connect_to_device_socket(adb_args, device_socket);
}
static struct child* monitored_child;
static void
accept_die_on_sigchld_sigchld_action(
int signo,
siginfo_t* info,
void* context)
{
assert(signo == SIGCHLD);
if (child_poll_death(monitored_child)) {
pid_t pid = monitored_child->pid;
monitored_child = NULL;
die(ECOMM, "child %d died", (int) pid);
}
}
static int
accept_die_on_sigchld(int sock, struct child* child)
{
SCOPED_RESLIST(rl);
if (monitored_child != NULL)
die(EINVAL, "only one monitored child supported");
sigset_t all_signals;
sigfillset(&all_signals);
struct sigaction sa = {
.sa_sigaction = accept_die_on_sigchld_sigchld_action,
.sa_flags = SA_SIGINFO,
};
memcpy(&sa.sa_mask, &all_signals, sizeof (sigset_t));
sigaction_restore_as_cleanup(SIGCHLD, &sa);
save_signals_unblock_for_io();
sigaddset(&signals_unblock_for_io, SIGCHLD);
monitored_child = child;
sigset_t pending;
sigpending(&pending);
WITH_CURRENT_RESLIST(rl->parent);
int conn = xaccept(sock);
monitored_child = NULL;
return conn;
}
static struct childcom*
reconnect_over_tcp_socket(const struct childcom* tc,
struct child* adb,
const char* tcp_addr)
{
SCOPED_RESLIST(rl);
static const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_flags = AI_PASSIVE,
.ai_socktype = SOCK_STREAM,
};
char* node;
char* service;
str2gaiargs(tcp_addr, &node, &service);
struct addrinfo* ai =
xgetaddrinfo_interruptible(node, service, &hints);
while (ai && ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
ai = ai->ai_next;
if (!ai)