summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2018-04-26 01:47:04 +0200
committerTao Bao <tbao@google.com>2018-05-01 18:48:00 +0200
commit6d99d4b4571b95ff221439c819826381f8bb96f9 (patch)
tree3fedcc8cfb126580ef1e7fa771f97ba10ec8dacb
parentMerge "Drop '#include "ui.h"' from device.h." (diff)
downloadandroid_bootable_recovery-6d99d4b4571b95ff221439c819826381f8bb96f9.tar
android_bootable_recovery-6d99d4b4571b95ff221439c819826381f8bb96f9.tar.gz
android_bootable_recovery-6d99d4b4571b95ff221439c819826381f8bb96f9.tar.bz2
android_bootable_recovery-6d99d4b4571b95ff221439c819826381f8bb96f9.tar.lz
android_bootable_recovery-6d99d4b4571b95ff221439c819826381f8bb96f9.tar.xz
android_bootable_recovery-6d99d4b4571b95ff221439c819826381f8bb96f9.tar.zst
android_bootable_recovery-6d99d4b4571b95ff221439c819826381f8bb96f9.zip
-rw-r--r--Android.mk1
-rw-r--r--common.h6
-rw-r--r--private/recovery.h19
-rw-r--r--recovery.cpp139
-rw-r--r--recovery_main.cpp162
5 files changed, 192 insertions, 135 deletions
diff --git a/Android.mk b/Android.mk
index f2fa6fb3c..acff24a73 100644
--- a/Android.mk
+++ b/Android.mk
@@ -126,6 +126,7 @@ LOCAL_SRC_FILES := \
device.cpp \
fuse_sdcard_provider.cpp \
recovery.cpp \
+ recovery_main.cpp \
roots.cpp \
rotate_logs.cpp \
diff --git a/common.h b/common.h
index 4228e71de..33c5ba08f 100644
--- a/common.h
+++ b/common.h
@@ -37,9 +37,13 @@ extern std::string stage;
// The reason argument provided in "--reason=".
extern const char* reason;
-// fopen a file, mounting volumes and making parent dirs as necessary.
+// fopen(3)'s the given file, by mounting volumes and making parent dirs as necessary. Returns the
+// file pointer, or nullptr on error.
FILE* fopen_path(const std::string& path, const char* mode);
+// In turn fflush(3)'s, fsync(3)'s and fclose(3)'s the given stream.
+void check_and_fclose(FILE* fp, const std::string& name);
+
void ui_print(const char* format, ...) __printflike(1, 2);
bool is_ro_debuggable();
diff --git a/private/recovery.h b/private/recovery.h
new file mode 100644
index 000000000..5b2ca4b3f
--- /dev/null
+++ b/private/recovery.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+int start_recovery(int argc, char** argv);
diff --git a/recovery.cpp b/recovery.cpp
index d2b3eb516..bd176da5a 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#include "private/recovery.h"
+
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
@@ -35,7 +37,6 @@
#include <unistd.h>
#include <algorithm>
-#include <chrono>
#include <memory>
#include <string>
#include <vector>
@@ -51,8 +52,8 @@
#include <cutils/android_reboot.h>
#include <cutils/properties.h> /* for property_list */
#include <healthd/BatteryMonitor.h>
-#include <private/android_logger.h> /* private pmsg functions */
#include <private/android_filesystem_config.h> /* for AID_SYSTEM */
+#include <private/android_logger.h> /* private pmsg functions */
#include <selinux/android.h>
#include <selinux/label.h>
#include <selinux/selinux.h>
@@ -64,7 +65,6 @@
#include "fuse_sdcard_provider.h"
#include "fuse_sideload.h"
#include "install.h"
-#include "minadbd/minadbd.h"
#include "minui/minui.h"
#include "otautil/DirUtil.h"
#include "otautil/error_code.h"
@@ -147,7 +147,6 @@ struct selabel_handle* sehandle;
* 7b. the user reboots (pulling the battery, etc) into the main system
*/
-// Open a given path, mounting partitions as necessary.
FILE* fopen_path(const std::string& path, const char* mode) {
if (ensure_path_mounted(path.c_str()) != 0) {
LOG(ERROR) << "Can't mount " << path;
@@ -162,8 +161,7 @@ FILE* fopen_path(const std::string& path, const char* mode) {
return fopen(path.c_str(), mode);
}
-// close a file, log an error if the error indicator is set
-static void check_and_fclose(FILE* fp, const std::string& name) {
+void check_and_fclose(FILE* fp, const std::string& name) {
fflush(fp);
if (fsync(fileno(fp)) == -1) {
PLOG(ERROR) << "Failed to fsync " << name;
@@ -186,92 +184,6 @@ bool reboot(const std::string& command) {
return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd);
}
-static void redirect_stdio(const char* filename) {
- int pipefd[2];
- if (pipe(pipefd) == -1) {
- PLOG(ERROR) << "pipe failed";
-
- // Fall back to traditional logging mode without timestamps.
- // If these fail, there's not really anywhere to complain...
- freopen(filename, "a", stdout); setbuf(stdout, NULL);
- freopen(filename, "a", stderr); setbuf(stderr, NULL);
-
- return;
- }
-
- pid_t pid = fork();
- if (pid == -1) {
- PLOG(ERROR) << "fork failed";
-
- // Fall back to traditional logging mode without timestamps.
- // If these fail, there's not really anywhere to complain...
- freopen(filename, "a", stdout); setbuf(stdout, NULL);
- freopen(filename, "a", stderr); setbuf(stderr, NULL);
-
- return;
- }
-
- if (pid == 0) {
- /// Close the unused write end.
- close(pipefd[1]);
-
- auto start = std::chrono::steady_clock::now();
-
- // Child logger to actually write to the log file.
- FILE* log_fp = fopen(filename, "ae");
- if (log_fp == nullptr) {
- PLOG(ERROR) << "fopen \"" << filename << "\" failed";
- close(pipefd[0]);
- _exit(EXIT_FAILURE);
- }
-
- FILE* pipe_fp = fdopen(pipefd[0], "r");
- if (pipe_fp == nullptr) {
- PLOG(ERROR) << "fdopen failed";
- check_and_fclose(log_fp, filename);
- close(pipefd[0]);
- _exit(EXIT_FAILURE);
- }
-
- char* line = nullptr;
- size_t len = 0;
- while (getline(&line, &len, pipe_fp) != -1) {
- auto now = std::chrono::steady_clock::now();
- double duration = std::chrono::duration_cast<std::chrono::duration<double>>(
- now - start).count();
- if (line[0] == '\n') {
- fprintf(log_fp, "[%12.6lf]\n", duration);
- } else {
- fprintf(log_fp, "[%12.6lf] %s", duration, line);
- }
- fflush(log_fp);
- }
-
- PLOG(ERROR) << "getline failed";
-
- free(line);
- check_and_fclose(log_fp, filename);
- close(pipefd[0]);
- _exit(EXIT_FAILURE);
- } else {
- // Redirect stdout/stderr to the logger process.
- // Close the unused read end.
- close(pipefd[0]);
-
- setbuf(stdout, nullptr);
- setbuf(stderr, nullptr);
-
- if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
- PLOG(ERROR) << "dup2 stdout failed";
- }
- if (dup2(pipefd[1], STDERR_FILENO) == -1) {
- PLOG(ERROR) << "dup2 stderr failed";
- }
-
- close(pipefd[1]);
- }
-}
-
// command line args come from, in decreasing precedence:
// - the actual command line
// - the bootloader control block (one per line, after "recovery")
@@ -1218,18 +1130,6 @@ void ui_print(const char* format, ...) {
}
}
-static constexpr char log_characters[] = "VDIWEF";
-
-void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity,
- const char* /* tag */, const char* /* file */, unsigned int /* line */,
- const char* message) {
- if (severity >= android::base::ERROR && ui != nullptr) {
- ui->Print("E:%s\n", message);
- } else {
- fprintf(stdout, "%c:%s\n", log_characters[severity], message);
- }
-}
-
static bool is_battery_ok(int* required_battery_level) {
struct healthd_config healthd_config = {
.batteryStatusPath = android::String8(android::String8::kEmptyString),
@@ -1339,38 +1239,9 @@ static void log_failure_code(ErrorCode code, const std::string& update_package)
LOG(INFO) << log_content;
}
-int main(int argc, char **argv) {
- // We don't have logcat yet under recovery; so we'll print error on screen and
- // log to stdout (which is redirected to recovery.log) as we used to do.
- android::base::InitLogging(argv, &UiLogger);
-
- // Take last pmsg contents and rewrite it to the current pmsg session.
- static const char filter[] = "recovery/";
- // Do we need to rotate?
- bool doRotate = false;
-
- __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &doRotate);
- // Take action to refresh pmsg contents
- __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &doRotate);
-
- // If this binary is started with the single argument "--adbd",
- // instead of being the normal recovery binary, it turns into kind
- // of a stripped-down version of adbd that only supports the
- // 'sideload' command. Note this must be a real argument, not
- // anything in the command file or bootloader control block; the
- // only way recovery should be run with this argument is when it
- // starts a copy of itself from the apply_from_adb() function.
- if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
- minadbd_main();
- return 0;
- }
-
+int start_recovery(int argc, char** argv) {
time_t start = time(nullptr);
- // redirect_stdio should be called only in non-sideload mode. Otherwise
- // we may have two logger instances with different timestamps.
- redirect_stdio(Paths::Get().temporary_log_file().c_str());
-
printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
load_volume_table();
diff --git a/recovery_main.cpp b/recovery_main.cpp
new file mode 100644
index 000000000..9f579f7cd
--- /dev/null
+++ b/recovery_main.cpp
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <chrono>
+
+#include <android-base/logging.h>
+#include <private/android_logger.h> /* private pmsg functions */
+
+#include "common.h"
+#include "minadbd/minadbd.h"
+#include "otautil/paths.h"
+#include "private/recovery.h"
+#include "rotate_logs.h"
+#include "ui.h"
+
+static void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity,
+ const char* /* tag */, const char* /* file */, unsigned int /* line */,
+ const char* message) {
+ static constexpr char log_characters[] = "VDIWEF";
+ if (severity >= android::base::ERROR && ui != nullptr) {
+ ui->Print("E:%s\n", message);
+ } else {
+ fprintf(stdout, "%c:%s\n", log_characters[severity], message);
+ }
+}
+
+static void redirect_stdio(const char* filename) {
+ int pipefd[2];
+ if (pipe(pipefd) == -1) {
+ PLOG(ERROR) << "pipe failed";
+
+ // Fall back to traditional logging mode without timestamps. If these fail, there's not really
+ // anywhere to complain...
+ freopen(filename, "a", stdout);
+ setbuf(stdout, nullptr);
+ freopen(filename, "a", stderr);
+ setbuf(stderr, nullptr);
+
+ return;
+ }
+
+ pid_t pid = fork();
+ if (pid == -1) {
+ PLOG(ERROR) << "fork failed";
+
+ // Fall back to traditional logging mode without timestamps. If these fail, there's not really
+ // anywhere to complain...
+ freopen(filename, "a", stdout);
+ setbuf(stdout, nullptr);
+ freopen(filename, "a", stderr);
+ setbuf(stderr, nullptr);
+
+ return;
+ }
+
+ if (pid == 0) {
+ /// Close the unused write end.
+ close(pipefd[1]);
+
+ auto start = std::chrono::steady_clock::now();
+
+ // Child logger to actually write to the log file.
+ FILE* log_fp = fopen(filename, "ae");
+ if (log_fp == nullptr) {
+ PLOG(ERROR) << "fopen \"" << filename << "\" failed";
+ close(pipefd[0]);
+ _exit(EXIT_FAILURE);
+ }
+
+ FILE* pipe_fp = fdopen(pipefd[0], "r");
+ if (pipe_fp == nullptr) {
+ PLOG(ERROR) << "fdopen failed";
+ check_and_fclose(log_fp, filename);
+ close(pipefd[0]);
+ _exit(EXIT_FAILURE);
+ }
+
+ char* line = nullptr;
+ size_t len = 0;
+ while (getline(&line, &len, pipe_fp) != -1) {
+ auto now = std::chrono::steady_clock::now();
+ double duration =
+ std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count();
+ if (line[0] == '\n') {
+ fprintf(log_fp, "[%12.6lf]\n", duration);
+ } else {
+ fprintf(log_fp, "[%12.6lf] %s", duration, line);
+ }
+ fflush(log_fp);
+ }
+
+ PLOG(ERROR) << "getline failed";
+
+ free(line);
+ check_and_fclose(log_fp, filename);
+ close(pipefd[0]);
+ _exit(EXIT_FAILURE);
+ } else {
+ // Redirect stdout/stderr to the logger process. Close the unused read end.
+ close(pipefd[0]);
+
+ setbuf(stdout, nullptr);
+ setbuf(stderr, nullptr);
+
+ if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
+ PLOG(ERROR) << "dup2 stdout failed";
+ }
+ if (dup2(pipefd[1], STDERR_FILENO) == -1) {
+ PLOG(ERROR) << "dup2 stderr failed";
+ }
+
+ close(pipefd[1]);
+ }
+}
+
+int main(int argc, char** argv) {
+ // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
+ // (which is redirected to recovery.log) as we used to do.
+ android::base::InitLogging(argv, &UiLogger);
+
+ // Take last pmsg contents and rewrite it to the current pmsg session.
+ static constexpr const char filter[] = "recovery/";
+ // Do we need to rotate?
+ bool do_rotate = false;
+
+ __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
+ // Take action to refresh pmsg contents
+ __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
+
+ // If this binary is started with the single argument "--adbd", instead of being the normal
+ // recovery binary, it turns into kind of a stripped-down version of adbd that only supports the
+ // 'sideload' command. Note this must be a real argument, not anything in the command file or
+ // bootloader control block; the only way recovery should be run with this argument is when it
+ // starts a copy of itself from the apply_from_adb() function.
+ if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
+ minadbd_main();
+ return 0;
+ }
+
+ // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
+ // instances with different timestamps.
+ redirect_stdio(Paths::Get().temporary_log_file().c_str());
+
+ return start_recovery(argc, argv);
+}