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 #define TRACE_TAG TRACE_SERVICES
27 #include "adb.h"
28 #include "fdevent.h"
29 #include "fuse_adb_provider.h"
30
31 typedef struct stinfo stinfo;
32
33 struct stinfo {
34 void (*func)(int fd, void *cookie);
35 int fd;
36 void *cookie;
37 };
38
service_bootstrap_func(void * x)39 void* service_bootstrap_func(void* x) {
40 stinfo* sti = reinterpret_cast<stinfo*>(x);
41 sti->func(sti->fd, sti->cookie);
42 free(sti);
43 return 0;
44 }
45
sideload_host_service(int sfd,void * data)46 static void sideload_host_service(int sfd, void* data) {
47 const char* args = reinterpret_cast<const char*>(data);
48 int file_size;
49 int block_size;
50 if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) {
51 printf("bad sideload-host arguments: %s\n", args);
52 exit(1);
53 }
54
55 printf("sideload-host file size %d block size %d\n", file_size, block_size);
56
57 int result = run_adb_fuse(sfd, file_size, block_size);
58
59 printf("sideload_host finished\n");
60 sleep(1);
61 exit(result == 0 ? 0 : 1);
62 }
63
create_service_thread(void (* func)(int,void *),void * cookie)64 static int create_service_thread(void (*func)(int, void *), void *cookie)
65 {
66 int s[2];
67 if(adb_socketpair(s)) {
68 printf("cannot create service socket pair\n");
69 return -1;
70 }
71
72 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
73 if(sti == 0) fatal("cannot allocate stinfo");
74 sti->func = func;
75 sti->cookie = cookie;
76 sti->fd = s[1];
77
78 adb_thread_t t;
79 if (adb_thread_create( &t, service_bootstrap_func, sti)){
80 free(sti);
81 adb_close(s[0]);
82 adb_close(s[1]);
83 printf("cannot create service thread\n");
84 return -1;
85 }
86
87 D("service thread started, %d:%d\n",s[0], s[1]);
88 return s[0];
89 }
90
service_to_fd(const char * name)91 int service_to_fd(const char* name) {
92 int ret = -1;
93
94 if (!strncmp(name, "sideload:", 9)) {
95 // this exit status causes recovery to print a special error
96 // message saying to use a newer adb (that supports
97 // sideload-host).
98 exit(3);
99 } else if (!strncmp(name, "sideload-host:", 14)) {
100 ret = create_service_thread(sideload_host_service, (void*)(name + 14));
101 }
102 if (ret >= 0) {
103 close_on_exec(ret);
104 }
105 return ret;
106 }
107