blob: e0679fb0c5802491bd9eda145c31a7d17b9ce306 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 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
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/updater.h"
Tao Bao59dcb9c2016-10-03 18:06:46 -070018
Elliott Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
Tao Bao641fa972018-04-25 18:59:40 -070020#include <unistd.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070022#include <string>
23
Tao Bao039f2da2016-11-22 16:29:50 -080024#include <android-base/logging.h>
Tao Bao59dcb9c2016-10-03 18:06:46 -070025#include <android-base/strings.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070026
Tianjie Xu58d59122019-05-03 01:05:04 -070027Updater::~Updater() {
28 if (package_handle_) {
29 CloseArchive(package_handle_);
30 }
Tao Bao039f2da2016-11-22 16:29:50 -080031}
32
Tianjie Xu58d59122019-05-03 01:05:04 -070033bool Updater::Init(int fd, const std::string& package_filename, bool is_retry,
34 struct selabel_handle* sehandle) {
Tao Bao039f2da2016-11-22 16:29:50 -080035 // Set up the pipe for sending commands back to the parent process.
Tianjie Xu58d59122019-05-03 01:05:04 -070036 cmd_pipe_.reset(fdopen(fd, "wb"));
37 if (!cmd_pipe_) {
38 LOG(ERROR) << "Failed to open the command pipe";
39 return false;
Tao Bao039f2da2016-11-22 16:29:50 -080040 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070041
Tianjie Xu58d59122019-05-03 01:05:04 -070042 setlinebuf(cmd_pipe_.get());
43
44 if (!mapped_package_.MapFile(package_filename)) {
45 LOG(ERROR) << "failed to map package " << package_filename;
46 return false;
47 }
48 if (int open_err = OpenArchiveFromMemory(mapped_package_.addr, mapped_package_.length,
49 package_filename.c_str(), &package_handle_);
50 open_err != 0) {
51 LOG(ERROR) << "failed to open package " << package_filename << ": "
52 << ErrorCodeString(open_err);
53 return false;
54 }
55 if (!ReadEntryToString(package_handle_, SCRIPT_NAME, &updater_script_)) {
56 return false;
Tao Bao039f2da2016-11-22 16:29:50 -080057 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070058
Tianjie Xu58d59122019-05-03 01:05:04 -070059 is_retry_ = is_retry;
60
61 sehandle_ = sehandle;
62 if (!sehandle_) {
63 fprintf(cmd_pipe_.get(), "ui_print Warning: No file_contexts\n");
Tao Bao039f2da2016-11-22 16:29:50 -080064 }
Tianjie Xu58d59122019-05-03 01:05:04 -070065 return true;
66}
Doug Zongker9931f7f2009-06-10 14:11:53 -070067
Tianjie Xu58d59122019-05-03 01:05:04 -070068bool Updater::RunUpdate() {
Tao Bao039f2da2016-11-22 16:29:50 -080069 // Parse the script.
Tianjie Xuc4447322017-03-06 14:44:59 -080070 std::unique_ptr<Expr> root;
Tao Bao039f2da2016-11-22 16:29:50 -080071 int error_count = 0;
Tianjie Xu58d59122019-05-03 01:05:04 -070072 int error = ParseString(updater_script_, &root, &error_count);
Tao Bao039f2da2016-11-22 16:29:50 -080073 if (error != 0 || error_count > 0) {
74 LOG(ERROR) << error_count << " parse errors";
Tianjie Xu58d59122019-05-03 01:05:04 -070075 return false;
Tao Bao039f2da2016-11-22 16:29:50 -080076 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070077
Tao Bao039f2da2016-11-22 16:29:50 -080078 // Evaluate the parsed script.
Tianjie Xu58d59122019-05-03 01:05:04 -070079 State state(updater_script_, this);
80 state.is_retry = is_retry_;
Doug Zongker9931f7f2009-06-10 14:11:53 -070081
Tianjie Xu58d59122019-05-03 01:05:04 -070082 bool status = Evaluate(&state, root, &result_);
83 if (status) {
84 fprintf(cmd_pipe_.get(), "ui_print script succeeded: result was [%s]\n", result_.c_str());
85 // Even though the script doesn't abort, still log the cause code if result is empty.
86 if (result_.empty() && state.cause_code != kNoCause) {
87 fprintf(cmd_pipe_.get(), "log cause: %d\n", state.cause_code);
88 }
89 return true;
90 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -070091
Tianjie Xu58d59122019-05-03 01:05:04 -070092 ParseAndReportErrorCode(&state);
93 return false;
94}
Tianjie Xu7ce287d2016-05-31 09:29:49 -070095
Tianjie Xu58d59122019-05-03 01:05:04 -070096void Updater::WriteToCommandPipe(const std::string& message, bool flush) const {
97 fprintf(cmd_pipe_.get(), "%s\n", message.c_str());
98 if (flush) {
99 fflush(cmd_pipe_.get());
100 }
101}
102
103void Updater::UiPrint(const std::string& message) const {
104 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
105 // so skip sending empty strings to ui.
106 std::vector<std::string> lines = android::base::Split(message, "\n");
107 for (const auto& line : lines) {
108 if (!line.empty()) {
109 fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800110 }
111 }
112
Tianjie Xu58d59122019-05-03 01:05:04 -0700113 // on the updater side, we need to dump the contents to stderr (which has
114 // been redirected to the log file). because the recovery will only print
115 // the contents to screen when processing pipe command ui_print.
116 LOG(INFO) << message;
117}
Tao Bao039f2da2016-11-22 16:29:50 -0800118
Tianjie Xu58d59122019-05-03 01:05:04 -0700119void Updater::ParseAndReportErrorCode(State* state) {
120 CHECK(state);
121 if (state->errmsg.empty()) {
122 LOG(ERROR) << "script aborted (no error message)";
123 fprintf(cmd_pipe_.get(), "ui_print script aborted (no error message)\n");
Tao Bao039f2da2016-11-22 16:29:50 -0800124 } else {
Tianjie Xu58d59122019-05-03 01:05:04 -0700125 LOG(ERROR) << "script aborted: " << state->errmsg;
126 const std::vector<std::string> lines = android::base::Split(state->errmsg, "\n");
127 for (const std::string& line : lines) {
128 // Parse the error code in abort message.
129 // Example: "E30: This package is for bullhead devices."
130 if (!line.empty() && line[0] == 'E') {
131 if (sscanf(line.c_str(), "E%d: ", &state->error_code) != 1) {
132 LOG(ERROR) << "Failed to parse error code: [" << line << "]";
133 }
134 }
135 fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str());
136 }
Tao Bao039f2da2016-11-22 16:29:50 -0800137 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138
Tianjie Xu58d59122019-05-03 01:05:04 -0700139 // Installation has been aborted. Set the error code to kScriptExecutionFailure unless
140 // a more specific code has been set in errmsg.
141 if (state->error_code == kNoError) {
142 state->error_code = kScriptExecutionFailure;
143 }
144 fprintf(cmd_pipe_.get(), "log error: %d\n", state->error_code);
145 // Cause code should provide additional information about the abort.
146 if (state->cause_code != kNoCause) {
147 fprintf(cmd_pipe_.get(), "log cause: %d\n", state->cause_code);
148 if (state->cause_code == kPatchApplicationFailure) {
149 LOG(INFO) << "Patch application failed, retry update.";
150 fprintf(cmd_pipe_.get(), "retry_update\n");
151 } else if (state->cause_code == kEioFailure) {
152 LOG(INFO) << "Update failed due to EIO, retry update.";
153 fprintf(cmd_pipe_.get(), "retry_update\n");
154 }
155 }
156}
157
158bool Updater::ReadEntryToString(ZipArchiveHandle za, const std::string& entry_name,
159 std::string* content) {
160 ZipEntry entry;
161 int find_err = FindEntry(za, entry_name, &entry);
162 if (find_err != 0) {
163 LOG(ERROR) << "failed to find " << entry_name
164 << " in the package: " << ErrorCodeString(find_err);
165 return false;
Tao Bao039f2da2016-11-22 16:29:50 -0800166 }
Tao Bao039f2da2016-11-22 16:29:50 -0800167
Tianjie Xu58d59122019-05-03 01:05:04 -0700168 content->resize(entry.uncompressed_length);
169 int extract_err = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&content->at(0)),
170 entry.uncompressed_length);
171 if (extract_err != 0) {
172 LOG(ERROR) << "failed to read script from package: " << ErrorCodeString(extract_err);
173 return false;
174 }
175
176 return true;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177}