blob: 218b84a38421dda1174ee5cbf6430262711eca6b [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -08001/*
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 <stdlib.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <string.h>
21#include <errno.h>
22
23#include "sysdeps.h"
24#include "fdevent.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070025#include "fuse_adb_provider.h"
Doug Zongker9270a202012-01-09 15:16:13 -080026
27#define TRACE_TAG TRACE_SERVICES
28#include "adb.h"
29
30typedef struct stinfo stinfo;
31
32struct stinfo {
33 void (*func)(int fd, void *cookie);
34 int fd;
35 void *cookie;
36};
37
38
39void *service_bootstrap_func(void *x)
40{
41 stinfo *sti = x;
42 sti->func(sti->fd, sti->cookie);
43 free(sti);
44 return 0;
45}
46
Doug Zongker075ad802014-06-26 15:35:51 -070047static void sideload_host_service(int sfd, void* cookie)
Doug Zongker9270a202012-01-09 15:16:13 -080048{
Doug Zongker075ad802014-06-26 15:35:51 -070049 char* saveptr;
50 const char* s = strtok_r(cookie, ":", &saveptr);
51 uint64_t file_size = strtoull(s, NULL, 10);
52 s = strtok_r(NULL, ":", &saveptr);
53 uint32_t block_size = strtoul(s, NULL, 10);
Doug Zongker9270a202012-01-09 15:16:13 -080054
Doug Zongker075ad802014-06-26 15:35:51 -070055 printf("sideload-host file size %llu block size %lu\n", file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080056
Doug Zongker18a78e02014-07-10 07:31:46 -070057 int result = run_adb_fuse(sfd, file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080058
Doug Zongker075ad802014-06-26 15:35:51 -070059 printf("sideload_host finished\n");
60 sleep(1);
61 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080062}
63
Doug Zongker9270a202012-01-09 15:16:13 -080064#if 0
65static void echo_service(int fd, void *cookie)
66{
67 char buf[4096];
68 int r;
69 char *p;
70 int c;
71
72 for(;;) {
73 r = read(fd, buf, 4096);
74 if(r == 0) goto done;
75 if(r < 0) {
76 if(errno == EINTR) continue;
77 else goto done;
78 }
79
80 c = r;
81 p = buf;
82 while(c > 0) {
83 r = write(fd, p, c);
84 if(r > 0) {
85 c -= r;
86 p += r;
87 continue;
88 }
89 if((r < 0) && (errno == EINTR)) continue;
90 goto done;
91 }
92 }
93done:
94 close(fd);
95}
96#endif
97
98static int create_service_thread(void (*func)(int, void *), void *cookie)
99{
100 stinfo *sti;
101 adb_thread_t t;
102 int s[2];
103
104 if(adb_socketpair(s)) {
105 printf("cannot create service socket pair\n");
106 return -1;
107 }
108
109 sti = malloc(sizeof(stinfo));
110 if(sti == 0) fatal("cannot allocate stinfo");
111 sti->func = func;
112 sti->cookie = cookie;
113 sti->fd = s[1];
114
115 if(adb_thread_create( &t, service_bootstrap_func, sti)){
116 free(sti);
117 adb_close(s[0]);
118 adb_close(s[1]);
119 printf("cannot create service thread\n");
120 return -1;
121 }
122
123 D("service thread started, %d:%d\n",s[0], s[1]);
124 return s[0];
125}
126
127int service_to_fd(const char *name)
128{
129 int ret = -1;
130
131 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -0700132 // this exit status causes recovery to print a special error
133 // message saying to use a newer adb (that supports
134 // sideload-host).
135 exit(3);
136 } else if (!strncmp(name, "sideload-host:", 14)) {
137 ret = create_service_thread(sideload_host_service, (void*)(name + 14));
Doug Zongker9270a202012-01-09 15:16:13 -0800138#if 0
139 } else if(!strncmp(name, "echo:", 5)){
140 ret = create_service_thread(echo_service, 0);
141#endif
142 }
143 if (ret >= 0) {
144 close_on_exec(ret);
145 }
146 return ret;
147}