blob: 03692d00c9a232daa26197ac707620bd75682dec [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
Ethan Yonker58f21322018-08-24 11:17:36 -0500120#if PLATFORM_SDK_VERSION >= 28
Josh Gao84c82a82018-04-13 16:08:01 -0700121int service_to_fd(const char* name, atransport* /* transport */) {
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500122#else
Ethan Yonker58f21322018-08-24 11:17:36 -0500123int service_to_fd(const char* name, const atransport* transport __unused) {
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500124#endif
Tao Bao99f0d9e2016-10-13 12:46:38 -0700125 int ret = -1;
Doug Zongker9270a202012-01-09 15:16:13 -0800126
Tao Bao99f0d9e2016-10-13 12:46:38 -0700127 if (!strncmp(name, "sideload:", 9)) {
128 // this exit status causes recovery to print a special error
129 // message saying to use a newer adb (that supports
130 // sideload-host).
131 exit(3);
132 } else if (!strncmp(name, "sideload-host:", 14)) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500133#if PLATFORM_SDK_VERSION < 26
134 char* arg = strdup(name + 14);
135#else
Tao Bao99f0d9e2016-10-13 12:46:38 -0700136 std::string arg(name + 14);
Ethan Yonker58f21322018-08-24 11:17:36 -0500137#endif
Tao Bao99f0d9e2016-10-13 12:46:38 -0700138 ret = create_service_thread(sideload_host_service, arg);
139 }
140 if (ret >= 0) {
141 close_on_exec(ret);
142 }
143 return ret;
Doug Zongker9270a202012-01-09 15:16:13 -0800144}
Ethan Yonker534d4e02016-08-26 10:05:03 -0500145
146#if PLATFORM_SDK_VERSION == 23
147int service_to_fd(const char* name) {
148 atransport transport;
149 return service_to_fd(name, &transport);
150}
151#endif