1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <inttypes.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include "sysdeps.h"
25
26 #include "adb.h"
27 #include "fdevent.h"
28 #include "fuse_adb_provider.h"
29
30 typedef struct stinfo stinfo;
31
32 struct stinfo {
33 void (*func)(int fd, void *cookie);
34 int fd;
35 void *cookie;
36 };
37
service_bootstrap_func(void * x)38 void service_bootstrap_func(void* x) {
39 stinfo* sti = reinterpret_cast<stinfo*>(x);
40 sti->func(sti->fd, sti->cookie);
41 free(sti);
42 }
43
sideload_host_service(int sfd,void * data)44 static void sideload_host_service(int sfd, void* data) {
45 char* args = reinterpret_cast<char*>(data);
46 int file_size;
47 int block_size;
48 if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) {
49 printf("bad sideload-host arguments: %s\n", args);
50 exit(1);
51 }
52 free(args);
53
54 printf("sideload-host file size %d block size %d\n", file_size, block_size);
55
56 int result = run_adb_fuse(sfd, file_size, block_size);
57
58 printf("sideload_host finished\n");
59 sleep(1);
60 exit(result == 0 ? 0 : 1);
61 }
62
create_service_thread(void (* func)(int,void *),void * cookie)63 static int create_service_thread(void (*func)(int, void *), void *cookie) {
64 int s[2];
65 if(adb_socketpair(s)) {
66 printf("cannot create service socket pair\n");
67 return -1;
68 }
69
70 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
71 if(sti == 0) fatal("cannot allocate stinfo");
72 sti->func = func;
73 sti->cookie = cookie;
74 sti->fd = s[1];
75
76 if (!adb_thread_create(service_bootstrap_func, sti)) {
77 free(sti);
78 adb_close(s[0]);
79 adb_close(s[1]);
80 printf("cannot create service thread\n");
81 return -1;
82 }
83
84 VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
85 return s[0];
86 }
87
service_to_fd(const char * name,const atransport * transport)88 int service_to_fd(const char* name, const atransport* transport) {
89 int ret = -1;
90
91 if (!strncmp(name, "sideload:", 9)) {
92 // this exit status causes recovery to print a special error
93 // message saying to use a newer adb (that supports
94 // sideload-host).
95 exit(3);
96 } else if (!strncmp(name, "sideload-host:", 14)) {
97 char* arg = strdup(name + 14);
98 ret = create_service_thread(sideload_host_service, arg);
99 }
100 if (ret >= 0) {
101 close_on_exec(ret);
102 }
103 return ret;
104 }
105