Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 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 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 23 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <thread> |
| 26 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 27 | #include "adb.h" |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 28 | #include "fdevent.h" |
| 29 | #include "fuse_adb_provider.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 30 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 31 | |
| 32 | typedef struct stinfo stinfo; |
| 33 | |
| 34 | struct stinfo { |
| 35 | void (*func)(int fd, void *cookie); |
| 36 | int fd; |
| 37 | void *cookie; |
| 38 | }; |
| 39 | |
Josh Gao | cd32476 | 2016-02-12 15:00:52 -0800 | [diff] [blame] | 40 | void service_bootstrap_func(void* x) { |
Elliott Hughes | 20531ef | 2015-04-10 13:59:19 -0700 | [diff] [blame] | 41 | stinfo* sti = reinterpret_cast<stinfo*>(x); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 42 | sti->func(sti->fd, sti->cookie); |
| 43 | free(sti); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 46 | #if PLATFORM_SDK_VERSION < 26 |
Elliott Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 47 | static void sideload_host_service(int sfd, void* data) { |
Yabin Cui | c8a3c80 | 2015-09-29 18:05:30 -0700 | [diff] [blame] | 48 | char* args = reinterpret_cast<char*>(data); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 49 | #else |
| 50 | static void sideload_host_service(int sfd, const std::string& args) { |
| 51 | #endif |
Elliott Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 52 | int file_size; |
| 53 | int block_size; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 54 | #if PLATFORM_SDK_VERSION < 26 |
Elliott Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 55 | if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) { |
| 56 | printf("bad sideload-host arguments: %s\n", args); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 57 | #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 Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 61 | exit(1); |
| 62 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 63 | #if PLATFORM_SDK_VERSION < 26 |
Yabin Cui | c8a3c80 | 2015-09-29 18:05:30 -0700 | [diff] [blame] | 64 | free(args); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 65 | #endif |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 66 | |
Elliott Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 67 | printf("sideload-host file size %d block size %d\n", file_size, block_size); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 68 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 69 | int result = run_adb_fuse(sfd, file_size, block_size); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 70 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 71 | printf("sideload_host finished\n"); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 72 | exit(result == 0 ? 0 : 1); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 75 | #if PLATFORM_SDK_VERSION < 26 |
Elliott Hughes | 921431f | 2015-05-05 14:37:53 -0700 | [diff] [blame] | 76 | static int create_service_thread(void (*func)(int, void *), void *cookie) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 77 | int s[2]; |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 78 | if (adb_socketpair(s)) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 79 | printf("cannot create service socket pair\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
Rahul Chaudhry | 4f7faac | 2016-11-08 16:06:13 -0800 | [diff] [blame] | 83 | stinfo* sti = static_cast<stinfo*>(malloc(sizeof(stinfo))); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 84 | if(sti == 0) fatal("cannot allocate stinfo"); |
| 85 | sti->func = func; |
| 86 | sti->cookie = cookie; |
| 87 | sti->fd = s[1]; |
| 88 | |
Ethan Yonker | 534d4e0 | 2016-08-26 10:05:03 -0500 | [diff] [blame] | 89 | #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 Hughes | 921431f | 2015-05-05 14:37:53 -0700 | [diff] [blame] | 93 | if (!adb_thread_create(service_bootstrap_func, sti)) { |
Ethan Yonker | 534d4e0 | 2016-08-26 10:05:03 -0500 | [diff] [blame] | 94 | #endif |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 95 | 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 Yonker | 534d4e0 | 2016-08-26 10:05:03 -0500 | [diff] [blame] | 102 | //VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1]; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 103 | return s[0]; |
| 104 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 105 | #else |
| 106 | static 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 Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 119 | |
David Pursell | c3d4d53 | 2015-08-25 12:50:47 -0700 | [diff] [blame] | 120 | int service_to_fd(const char* name, const atransport* transport) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 121 | int ret = -1; |
| 122 | |
| 123 | if (!strncmp(name, "sideload:", 9)) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 124 | // 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 Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 129 | #if PLATFORM_SDK_VERSION < 26 |
Yabin Cui | c8a3c80 | 2015-09-29 18:05:30 -0700 | [diff] [blame] | 130 | char* arg = strdup(name + 14); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 131 | #else |
| 132 | std::string arg(name + 14); |
| 133 | #endif |
Yabin Cui | c8a3c80 | 2015-09-29 18:05:30 -0700 | [diff] [blame] | 134 | ret = create_service_thread(sideload_host_service, arg); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 135 | } |
| 136 | if (ret >= 0) { |
| 137 | close_on_exec(ret); |
| 138 | } |
| 139 | return ret; |
| 140 | } |
Ethan Yonker | 534d4e0 | 2016-08-26 10:05:03 -0500 | [diff] [blame] | 141 | |
| 142 | #if PLATFORM_SDK_VERSION == 23 |
| 143 | int service_to_fd(const char* name) { |
| 144 | atransport transport; |
| 145 | return service_to_fd(name, &transport); |
| 146 | } |
| 147 | #endif |