blob: 40f2cea7b48cffc3b59c45524d341af41e01ac10 [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");
72 sleep(1);
73 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080074}
75
Ethan Yonkerfefe5912017-09-30 22:22:13 -050076#if PLATFORM_SDK_VERSION < 26
Elliott Hughes921431f2015-05-05 14:37:53 -070077static int create_service_thread(void (*func)(int, void *), void *cookie) {
Doug Zongker9270a202012-01-09 15:16:13 -080078 int s[2];
Elliott Hughes24eb8a02016-06-15 15:12:17 -070079 if (adb_socketpair(s)) {
Doug Zongker9270a202012-01-09 15:16:13 -080080 printf("cannot create service socket pair\n");
81 return -1;
82 }
83
Rahul Chaudhry4f7faac2016-11-08 16:06:13 -080084 stinfo* sti = static_cast<stinfo*>(malloc(sizeof(stinfo)));
Doug Zongker9270a202012-01-09 15:16:13 -080085 if(sti == 0) fatal("cannot allocate stinfo");
86 sti->func = func;
87 sti->cookie = cookie;
88 sti->fd = s[1];
89
Ethan Yonker534d4e02016-08-26 10:05:03 -050090#if PLATFORM_SDK_VERSION == 23
91 adb_thread_t t;
92 if (adb_thread_create( &t, (adb_thread_func_t)service_bootstrap_func, sti)){
93#else
Elliott Hughes921431f2015-05-05 14:37:53 -070094 if (!adb_thread_create(service_bootstrap_func, sti)) {
Ethan Yonker534d4e02016-08-26 10:05:03 -050095#endif
Doug Zongker9270a202012-01-09 15:16:13 -080096 free(sti);
97 adb_close(s[0]);
98 adb_close(s[1]);
99 printf("cannot create service thread\n");
100 return -1;
101 }
102
Ethan Yonker534d4e02016-08-26 10:05:03 -0500103 //VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
Doug Zongker9270a202012-01-09 15:16:13 -0800104 return s[0];
105}
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500106#else
107static int create_service_thread(void (*func)(int, const std::string&), const std::string& args) {
108 int s[2];
109 if (adb_socketpair(s)) {
110 printf("cannot create service socket pair\n");
111 return -1;
112 }
113
114 std::thread([s, func, args]() { func(s[1], args); }).detach();
115
116 //VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
117 return s[0];
118}
119#endif
Doug Zongker9270a202012-01-09 15:16:13 -0800120
David Pursellc3d4d532015-08-25 12:50:47 -0700121int service_to_fd(const char* name, const atransport* transport) {
Doug Zongker9270a202012-01-09 15:16:13 -0800122 int ret = -1;
123
124 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -0700125 // this exit status causes recovery to print a special error
126 // message saying to use a newer adb (that supports
127 // sideload-host).
128 exit(3);
129 } else if (!strncmp(name, "sideload-host:", 14)) {
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500130#if PLATFORM_SDK_VERSION < 26
Yabin Cuic8a3c802015-09-29 18:05:30 -0700131 char* arg = strdup(name + 14);
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500132#else
133 std::string arg(name + 14);
134#endif
Yabin Cuic8a3c802015-09-29 18:05:30 -0700135 ret = create_service_thread(sideload_host_service, arg);
Doug Zongker9270a202012-01-09 15:16:13 -0800136 }
137 if (ret >= 0) {
138 close_on_exec(ret);
139 }
140 return ret;
141}
Ethan Yonker534d4e02016-08-26 10:05:03 -0500142
143#if PLATFORM_SDK_VERSION == 23
144int service_to_fd(const char* name) {
145 atransport transport;
146 return service_to_fd(name, &transport);
147}
148#endif