blob: 6919cdd4008110a91e082a1b93b6069e22e48e8f [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
Doug Zongker9270a202012-01-09 15:16:13 -080017#include <errno.h>
Dan Albert8f6eb5c2015-02-24 22:07:18 -080018#include <inttypes.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080023
Ethan Yonkerfefe5912017-09-30 22:22:13 -050024#include <string>
25#include <thread>
26
Doug Zongker9270a202012-01-09 15:16:13 -080027#include "adb.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080028#include "fdevent.h"
29#include "fuse_adb_provider.h"
Elliott Hughes24eb8a02016-06-15 15:12:17 -070030#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080031
32typedef struct stinfo stinfo;
33
34struct stinfo {
35 void (*func)(int fd, void *cookie);
36 int fd;
37 void *cookie;
38};
39
Josh Gaocd324762016-02-12 15:00:52 -080040void service_bootstrap_func(void* x) {
Elliott Hughes20531ef2015-04-10 13:59:19 -070041 stinfo* sti = reinterpret_cast<stinfo*>(x);
Doug Zongker9270a202012-01-09 15:16:13 -080042 sti->func(sti->fd, sti->cookie);
43 free(sti);
Doug Zongker9270a202012-01-09 15:16:13 -080044}
45
Ethan Yonkerfefe5912017-09-30 22:22:13 -050046#if PLATFORM_SDK_VERSION < 26
Elliott Hughesba45ddf2015-04-27 18:39:27 -070047static void sideload_host_service(int sfd, void* data) {
Yabin Cuic8a3c802015-09-29 18:05:30 -070048 char* args = reinterpret_cast<char*>(data);
Ethan Yonkerfefe5912017-09-30 22:22:13 -050049#else
50static void sideload_host_service(int sfd, const std::string& args) {
51#endif
Elliott Hughesba45ddf2015-04-27 18:39:27 -070052 int file_size;
53 int block_size;
Ethan Yonkerfefe5912017-09-30 22:22:13 -050054#if PLATFORM_SDK_VERSION < 26
Elliott Hughesba45ddf2015-04-27 18:39:27 -070055 if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) {
56 printf("bad sideload-host arguments: %s\n", args);
Ethan Yonkerfefe5912017-09-30 22:22:13 -050057#else
58 if (sscanf(args.c_str(), "%d:%d", &file_size, &block_size) != 2) {
59 printf("bad sideload-host arguments: %s\n", args.c_str());
60#endif
Elliott Hughesba45ddf2015-04-27 18:39:27 -070061 exit(1);
62 }
Ethan Yonkerfefe5912017-09-30 22:22:13 -050063#if PLATFORM_SDK_VERSION < 26
Yabin Cuic8a3c802015-09-29 18:05:30 -070064 free(args);
Ethan Yonkerfefe5912017-09-30 22:22:13 -050065#endif
Doug Zongker9270a202012-01-09 15:16:13 -080066
Elliott Hughesba45ddf2015-04-27 18:39:27 -070067 printf("sideload-host file size %d block size %d\n", file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080068
Doug Zongker18a78e02014-07-10 07:31:46 -070069 int result = run_adb_fuse(sfd, file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080070
Doug Zongker075ad802014-06-26 15:35:51 -070071 printf("sideload_host finished\n");
Doug Zongker075ad802014-06-26 15:35:51 -070072 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080073}
74
Ethan Yonkerfefe5912017-09-30 22:22:13 -050075#if PLATFORM_SDK_VERSION < 26
Elliott Hughes921431f2015-05-05 14:37:53 -070076static int create_service_thread(void (*func)(int, void *), void *cookie) {
Doug Zongker9270a202012-01-09 15:16:13 -080077 int s[2];
Elliott Hughes24eb8a02016-06-15 15:12:17 -070078 if (adb_socketpair(s)) {
Doug Zongker9270a202012-01-09 15:16:13 -080079 printf("cannot create service socket pair\n");
80 return -1;
81 }
82
Rahul Chaudhry4f7faac2016-11-08 16:06:13 -080083 stinfo* sti = static_cast<stinfo*>(malloc(sizeof(stinfo)));
Doug Zongker9270a202012-01-09 15:16:13 -080084 if(sti == 0) fatal("cannot allocate stinfo");
85 sti->func = func;
86 sti->cookie = cookie;
87 sti->fd = s[1];
88
Ethan Yonker534d4e02016-08-26 10:05:03 -050089#if PLATFORM_SDK_VERSION == 23
90 adb_thread_t t;
91 if (adb_thread_create( &t, (adb_thread_func_t)service_bootstrap_func, sti)){
92#else
Elliott Hughes921431f2015-05-05 14:37:53 -070093 if (!adb_thread_create(service_bootstrap_func, sti)) {
Ethan Yonker534d4e02016-08-26 10:05:03 -050094#endif
Doug Zongker9270a202012-01-09 15:16:13 -080095 free(sti);
96 adb_close(s[0]);
97 adb_close(s[1]);
98 printf("cannot create service thread\n");
99 return -1;
100 }
101
Ethan Yonker534d4e02016-08-26 10:05:03 -0500102 //VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
Doug Zongker9270a202012-01-09 15:16:13 -0800103 return s[0];
104}
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500105#else
106static int create_service_thread(void (*func)(int, const std::string&), const std::string& args) {
107 int s[2];
108 if (adb_socketpair(s)) {
109 printf("cannot create service socket pair\n");
110 return -1;
111 }
112
113 std::thread([s, func, args]() { func(s[1], args); }).detach();
114
115 //VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
116 return s[0];
117}
118#endif
Doug Zongker9270a202012-01-09 15:16:13 -0800119
David Pursellc3d4d532015-08-25 12:50:47 -0700120int service_to_fd(const char* name, const atransport* transport) {
Doug Zongker9270a202012-01-09 15:16:13 -0800121 int ret = -1;
122
123 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -0700124 // this exit status causes recovery to print a special error
125 // message saying to use a newer adb (that supports
126 // sideload-host).
127 exit(3);
128 } else if (!strncmp(name, "sideload-host:", 14)) {
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500129#if PLATFORM_SDK_VERSION < 26
Yabin Cuic8a3c802015-09-29 18:05:30 -0700130 char* arg = strdup(name + 14);
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500131#else
132 std::string arg(name + 14);
133#endif
Yabin Cuic8a3c802015-09-29 18:05:30 -0700134 ret = create_service_thread(sideload_host_service, arg);
Doug Zongker9270a202012-01-09 15:16:13 -0800135 }
136 if (ret >= 0) {
137 close_on_exec(ret);
138 }
139 return ret;
140}
Ethan Yonker534d4e02016-08-26 10:05:03 -0500141
142#if PLATFORM_SDK_VERSION == 23
143int service_to_fd(const char* name) {
144 atransport transport;
145 return service_to_fd(name, &transport);
146}
147#endif