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 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 17 | #include "minadbd_services.h" |
| 18 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 25 | |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 26 | #include <functional> |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 27 | #include <memory> |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 28 | #include <string> |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 29 | #include <string_view> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 30 | #include <thread> |
| 31 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 32 | #include <android-base/file.h> |
| 33 | #include <android-base/logging.h> |
| 34 | #include <android-base/memory.h> |
| 35 | #include <android-base/stringprintf.h> |
| 36 | #include <android-base/strings.h> |
| 37 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 38 | #include "adb.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 39 | #include "adb_unique_fd.h" |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 40 | #include "fdevent.h" |
| 41 | #include "fuse_adb_provider.h" |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 42 | #include "fuse_sideload.h" |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 43 | #include "minadbd_types.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 44 | #include "services.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 45 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 46 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 47 | static int minadbd_socket = -1; |
Tao Bao | 378bfbf | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 48 | static bool rescue_mode = false; |
| 49 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 50 | void SetMinadbdSocketFd(int socket_fd) { |
| 51 | minadbd_socket = socket_fd; |
| 52 | } |
| 53 | |
Tao Bao | 378bfbf | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 54 | void SetMinadbdRescueMode(bool rescue) { |
| 55 | rescue_mode = rescue; |
| 56 | } |
| 57 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 58 | static bool WriteCommandToFd(MinadbdCommands cmd, int fd) { |
| 59 | char message[kMinadbdMessageSize]; |
| 60 | memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix)); |
| 61 | android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd); |
| 62 | |
| 63 | if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) { |
| 64 | PLOG(ERROR) << "Failed to write message " << message; |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | // Blocks and reads the command status from |fd|. Returns false if the received message has a |
| 71 | // format error. |
| 72 | static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) { |
| 73 | char buffer[kMinadbdMessageSize]; |
| 74 | if (!android::base::ReadFully(fd, buffer, kMinadbdMessageSize)) { |
| 75 | PLOG(ERROR) << "Failed to response status from socket"; |
| 76 | exit(kMinadbdSocketIOError); |
| 77 | } |
| 78 | |
| 79 | std::string message(buffer, buffer + kMinadbdMessageSize); |
| 80 | if (!android::base::StartsWith(message, kMinadbdStatusPrefix)) { |
| 81 | LOG(ERROR) << "Failed to parse status in " << message; |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | *status = android::base::get_unaligned<MinadbdCommandStatus>( |
| 86 | message.substr(strlen(kMinadbdStatusPrefix)).c_str()); |
| 87 | return true; |
| 88 | } |
| 89 | |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 90 | static void sideload_host_service(unique_fd sfd, const std::string& args) { |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 91 | int64_t file_size; |
| 92 | int block_size; |
| 93 | if ((sscanf(args.c_str(), "%" SCNd64 ":%d", &file_size, &block_size) != 2) || file_size <= 0 || |
| 94 | block_size <= 0) { |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 95 | LOG(ERROR) << "bad sideload-host arguments: " << args; |
| 96 | exit(kMinadbdPackageSizeError); |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 97 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 98 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 99 | LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size; |
| 100 | |
| 101 | if (!WriteCommandToFd(MinadbdCommands::kInstall, minadbd_socket)) { |
| 102 | exit(kMinadbdSocketIOError); |
| 103 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 104 | |
Tao Bao | 178cdd4 | 2019-04-15 12:45:50 -0700 | [diff] [blame^] | 105 | auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size); |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 106 | if (int result = run_fuse_sideload(std::move(adb_data_reader)); result != 0) { |
| 107 | LOG(ERROR) << "Failed to start fuse"; |
| 108 | exit(kMinadbdFuseStartError); |
| 109 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 110 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 111 | MinadbdCommandStatus status; |
| 112 | if (!WaitForCommandStatus(minadbd_socket, &status)) { |
| 113 | exit(kMinadbdMessageFormatError); |
| 114 | } |
| 115 | LOG(INFO) << "Got command status: " << static_cast<unsigned int>(status); |
| 116 | |
| 117 | LOG(INFO) << "sideload_host finished"; |
| 118 | exit(kMinadbdSuccess); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 121 | unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) { |
| 122 | if (name.starts_with("sideload:")) { |
| 123 | // This exit status causes recovery to print a special error message saying to use a newer adb |
| 124 | // (that supports sideload-host). |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 125 | exit(kMinadbdAdbVersionError); |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 126 | } else if (name.starts_with("sideload-host:")) { |
| 127 | std::string arg(name.substr(strlen("sideload-host:"))); |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 128 | return create_service_thread("sideload-host", |
| 129 | std::bind(sideload_host_service, std::placeholders::_1, arg)); |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 130 | } |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 131 | return unique_fd{}; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 132 | } |