blob: f2b65c09b1adbaef54b55d8f272aadb3069aa6e1 [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;
48void SetMinadbdSocketFd(int socket_fd) {
49 minadbd_socket = socket_fd;
50}
51
52static bool WriteCommandToFd(MinadbdCommands cmd, int fd) {
53 char message[kMinadbdMessageSize];
54 memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix));
55 android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd);
56
57 if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) {
58 PLOG(ERROR) << "Failed to write message " << message;
59 return false;
60 }
61 return true;
62}
63
64// Blocks and reads the command status from |fd|. Returns false if the received message has a
65// format error.
66static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) {
67 char buffer[kMinadbdMessageSize];
68 if (!android::base::ReadFully(fd, buffer, kMinadbdMessageSize)) {
69 PLOG(ERROR) << "Failed to response status from socket";
70 exit(kMinadbdSocketIOError);
71 }
72
73 std::string message(buffer, buffer + kMinadbdMessageSize);
74 if (!android::base::StartsWith(message, kMinadbdStatusPrefix)) {
75 LOG(ERROR) << "Failed to parse status in " << message;
76 return false;
77 }
78
79 *status = android::base::get_unaligned<MinadbdCommandStatus>(
80 message.substr(strlen(kMinadbdStatusPrefix)).c_str());
81 return true;
82}
83
Josh Gao038c4a12018-07-27 11:18:30 -070084static void sideload_host_service(unique_fd sfd, const std::string& args) {
katao77d61732018-09-20 20:34:05 +080085 int64_t file_size;
86 int block_size;
87 if ((sscanf(args.c_str(), "%" SCNd64 ":%d", &file_size, &block_size) != 2) || file_size <= 0 ||
88 block_size <= 0) {
xunchang34690ce2019-04-05 16:16:07 -070089 LOG(ERROR) << "bad sideload-host arguments: " << args;
90 exit(kMinadbdPackageSizeError);
katao77d61732018-09-20 20:34:05 +080091 }
Doug Zongker9270a202012-01-09 15:16:13 -080092
xunchang34690ce2019-04-05 16:16:07 -070093 LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size;
94
95 if (!WriteCommandToFd(MinadbdCommands::kInstall, minadbd_socket)) {
96 exit(kMinadbdSocketIOError);
97 }
Doug Zongker9270a202012-01-09 15:16:13 -080098
Tao Bao2be97372019-04-15 12:45:50 -070099 auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size);
xunchang34690ce2019-04-05 16:16:07 -0700100 if (int result = run_fuse_sideload(std::move(adb_data_reader)); result != 0) {
101 LOG(ERROR) << "Failed to start fuse";
102 exit(kMinadbdFuseStartError);
103 }
Doug Zongker9270a202012-01-09 15:16:13 -0800104
xunchang34690ce2019-04-05 16:16:07 -0700105 MinadbdCommandStatus status;
106 if (!WaitForCommandStatus(minadbd_socket, &status)) {
107 exit(kMinadbdMessageFormatError);
108 }
109 LOG(INFO) << "Got command status: " << static_cast<unsigned int>(status);
110
111 LOG(INFO) << "sideload_host finished";
112 exit(kMinadbdSuccess);
Doug Zongker9270a202012-01-09 15:16:13 -0800113}
114
Tao Bao5de19e22019-01-02 09:35:59 -0800115unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) {
116 if (name.starts_with("sideload:")) {
117 // This exit status causes recovery to print a special error message saying to use a newer adb
118 // (that supports sideload-host).
xunchang34690ce2019-04-05 16:16:07 -0700119 exit(kMinadbdAdbVersionError);
Tao Bao5de19e22019-01-02 09:35:59 -0800120 } else if (name.starts_with("sideload-host:")) {
121 std::string arg(name.substr(strlen("sideload-host:")));
Josh Gao038c4a12018-07-27 11:18:30 -0700122 return create_service_thread("sideload-host",
123 std::bind(sideload_host_service, std::placeholders::_1, arg));
Tao Bao99f0d9e2016-10-13 12:46:38 -0700124 }
Josh Gao038c4a12018-07-27 11:18:30 -0700125 return unique_fd{};
Doug Zongker9270a202012-01-09 15:16:13 -0800126}