blob: eaf88ecc20040b4c147e3748f4227fa3f9cff80f [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
xunchang34690ce2019-04-05 16:16:07 -070017#include "minadbd_services.h"
18
Doug Zongker9270a202012-01-09 15:16:13 -080019#include <errno.h>
Dan Albert8f6eb5c2015-02-24 22:07:18 -080020#include <inttypes.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080025
Josh Gao038c4a12018-07-27 11:18:30 -070026#include <functional>
xunchang5e6832a2019-03-15 16:04:32 -070027#include <memory>
Tianjie Xu6af51a02017-04-19 14:00:52 -070028#include <string>
Tao Bao5de19e22019-01-02 09:35:59 -080029#include <string_view>
Josh Gao2aa0d3a2017-04-13 12:45:55 -070030#include <thread>
31
xunchang34690ce2019-04-05 16:16:07 -070032#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 Zongker9270a202012-01-09 15:16:13 -080038#include "adb.h"
Josh Gao038c4a12018-07-27 11:18:30 -070039#include "adb_unique_fd.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080040#include "fdevent.h"
41#include "fuse_adb_provider.h"
xunchang5e6832a2019-03-15 16:04:32 -070042#include "fuse_sideload.h"
xunchang34690ce2019-04-05 16:16:07 -070043#include "minadbd_types.h"
Josh Gao038c4a12018-07-27 11:18:30 -070044#include "services.h"
Elliott Hughes24eb8a02016-06-15 15:12:17 -070045#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080046
xunchang34690ce2019-04-05 16:16:07 -070047static int minadbd_socket = -1;
Tao Baoc6dc3252019-04-16 14:22:25 -070048static bool rescue_mode = false;
49
xunchang34690ce2019-04-05 16:16:07 -070050void SetMinadbdSocketFd(int socket_fd) {
51 minadbd_socket = socket_fd;
52}
53
Tao Baoc6dc3252019-04-16 14:22:25 -070054void SetMinadbdRescueMode(bool rescue) {
55 rescue_mode = rescue;
56}
57
xunchang34690ce2019-04-05 16:16:07 -070058static 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.
72static 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 Gao038c4a12018-07-27 11:18:30 -070090static void sideload_host_service(unique_fd sfd, const std::string& args) {
katao77d61732018-09-20 20:34:05 +080091 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) {
xunchang34690ce2019-04-05 16:16:07 -070095 LOG(ERROR) << "bad sideload-host arguments: " << args;
96 exit(kMinadbdPackageSizeError);
katao77d61732018-09-20 20:34:05 +080097 }
Doug Zongker9270a202012-01-09 15:16:13 -080098
xunchang34690ce2019-04-05 16:16:07 -070099 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 Zongker9270a202012-01-09 15:16:13 -0800104
Tao Bao2be97372019-04-15 12:45:50 -0700105 auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size);
xunchang34690ce2019-04-05 16:16:07 -0700106 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 Zongker9270a202012-01-09 15:16:13 -0800110
xunchang34690ce2019-04-05 16:16:07 -0700111 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 Zongker9270a202012-01-09 15:16:13 -0800119}
120
Tao Bao5de19e22019-01-02 09:35:59 -0800121unique_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).
xunchang34690ce2019-04-05 16:16:07 -0700125 exit(kMinadbdAdbVersionError);
Tao Bao5de19e22019-01-02 09:35:59 -0800126 } else if (name.starts_with("sideload-host:")) {
127 std::string arg(name.substr(strlen("sideload-host:")));
Josh Gao038c4a12018-07-27 11:18:30 -0700128 return create_service_thread("sideload-host",
129 std::bind(sideload_host_service, std::placeholders::_1, arg));
Tao Bao99f0d9e2016-10-13 12:46:38 -0700130 }
Josh Gao038c4a12018-07-27 11:18:30 -0700131 return unique_fd{};
Doug Zongker9270a202012-01-09 15:16:13 -0800132}