1*33f37583SAndroid Build Coastguard Worker /*
2*33f37583SAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*33f37583SAndroid Build Coastguard Worker *
4*33f37583SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*33f37583SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*33f37583SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*33f37583SAndroid Build Coastguard Worker *
8*33f37583SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*33f37583SAndroid Build Coastguard Worker *
10*33f37583SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*33f37583SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*33f37583SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*33f37583SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*33f37583SAndroid Build Coastguard Worker * limitations under the License.
15*33f37583SAndroid Build Coastguard Worker */
16*33f37583SAndroid Build Coastguard Worker
17*33f37583SAndroid Build Coastguard Worker #include <chrono>
18*33f37583SAndroid Build Coastguard Worker #include <thread>
19*33f37583SAndroid Build Coastguard Worker
20*33f37583SAndroid Build Coastguard Worker #include "apexd.h"
21*33f37583SAndroid Build Coastguard Worker #include "apexd_lifecycle.h"
22*33f37583SAndroid Build Coastguard Worker
23*33f37583SAndroid Build Coastguard Worker #include <android-base/logging.h>
24*33f37583SAndroid Build Coastguard Worker #include <android-base/properties.h>
25*33f37583SAndroid Build Coastguard Worker
26*33f37583SAndroid Build Coastguard Worker #include "apexd_utils.h"
27*33f37583SAndroid Build Coastguard Worker
28*33f37583SAndroid Build Coastguard Worker using android::base::GetProperty;
29*33f37583SAndroid Build Coastguard Worker using android::base::Result;
30*33f37583SAndroid Build Coastguard Worker using android::base::WaitForProperty;
31*33f37583SAndroid Build Coastguard Worker
32*33f37583SAndroid Build Coastguard Worker constexpr int MAX_WAIT_COUNT = 60;
33*33f37583SAndroid Build Coastguard Worker constexpr int WAIT_DURATION_SECONDS = 10;
34*33f37583SAndroid Build Coastguard Worker static const char* BOOT_TIMEOUT = "BootTimeout"; // NOLINT
35*33f37583SAndroid Build Coastguard Worker
36*33f37583SAndroid Build Coastguard Worker namespace android {
37*33f37583SAndroid Build Coastguard Worker namespace apex {
38*33f37583SAndroid Build Coastguard Worker
IsBooting()39*33f37583SAndroid Build Coastguard Worker bool ApexdLifecycle::IsBooting() {
40*33f37583SAndroid Build Coastguard Worker auto status = GetProperty(kApexStatusSysprop, "");
41*33f37583SAndroid Build Coastguard Worker return status != kApexStatusReady && status != kApexStatusActivated;
42*33f37583SAndroid Build Coastguard Worker }
43*33f37583SAndroid Build Coastguard Worker
RevertActiveSessions(const std::string & process,const std::string & error)44*33f37583SAndroid Build Coastguard Worker void ApexdLifecycle::RevertActiveSessions(const std::string& process,
45*33f37583SAndroid Build Coastguard Worker const std::string& error) {
46*33f37583SAndroid Build Coastguard Worker auto result = RevertActiveSessionsAndReboot(process, error);
47*33f37583SAndroid Build Coastguard Worker if (!result.ok()) {
48*33f37583SAndroid Build Coastguard Worker if (error != BOOT_TIMEOUT) {
49*33f37583SAndroid Build Coastguard Worker LOG(ERROR) << "Revert failed : " << result.error();
50*33f37583SAndroid Build Coastguard Worker // Can not anything more but loop until boot successfully
51*33f37583SAndroid Build Coastguard Worker while (!boot_completed_) {
52*33f37583SAndroid Build Coastguard Worker std::this_thread::sleep_for(std::chrono::seconds(1));
53*33f37583SAndroid Build Coastguard Worker }
54*33f37583SAndroid Build Coastguard Worker return;
55*33f37583SAndroid Build Coastguard Worker }
56*33f37583SAndroid Build Coastguard Worker }
57*33f37583SAndroid Build Coastguard Worker // This should never be reached
58*33f37583SAndroid Build Coastguard Worker LOG(FATAL) << "Active sessions were reverted, but reboot wasn't triggered.";
59*33f37583SAndroid Build Coastguard Worker }
60*33f37583SAndroid Build Coastguard Worker
WaitForBootStatus(const bool has_active_session)61*33f37583SAndroid Build Coastguard Worker void ApexdLifecycle::WaitForBootStatus(const bool has_active_session) {
62*33f37583SAndroid Build Coastguard Worker int wait_count = 0;
63*33f37583SAndroid Build Coastguard Worker while (!boot_completed_) {
64*33f37583SAndroid Build Coastguard Worker // Check for change in either crashing property or sys.boot_completed
65*33f37583SAndroid Build Coastguard Worker // Wait for updatable_crashing property change for most of the time
66*33f37583SAndroid Build Coastguard Worker // (arbitrary 10s), briefly check if boot has completed successfully,
67*33f37583SAndroid Build Coastguard Worker // if not continue waiting for updatable_crashing.
68*33f37583SAndroid Build Coastguard Worker // We use this strategy so that we can quickly detect if an updatable
69*33f37583SAndroid Build Coastguard Worker // process is crashing.
70*33f37583SAndroid Build Coastguard Worker if (WaitForProperty("sys.init.updatable_crashing", "1",
71*33f37583SAndroid Build Coastguard Worker std::chrono::seconds(WAIT_DURATION_SECONDS))) {
72*33f37583SAndroid Build Coastguard Worker auto name = GetProperty("sys.init.updatable_crashing_process_name", "");
73*33f37583SAndroid Build Coastguard Worker LOG(ERROR) << "Native process '" << (name.empty() ? "[unknown]" : name)
74*33f37583SAndroid Build Coastguard Worker << "' is crashing. Attempting a revert";
75*33f37583SAndroid Build Coastguard Worker RevertActiveSessions(name, "");
76*33f37583SAndroid Build Coastguard Worker }
77*33f37583SAndroid Build Coastguard Worker // Check if system stuck in boot screen and revert the staging apex once
78*33f37583SAndroid Build Coastguard Worker if (has_active_session && ++wait_count == MAX_WAIT_COUNT) {
79*33f37583SAndroid Build Coastguard Worker LOG(ERROR) << "System didn't finish boot in "
80*33f37583SAndroid Build Coastguard Worker << (WAIT_DURATION_SECONDS * MAX_WAIT_COUNT)
81*33f37583SAndroid Build Coastguard Worker << " seconds. Attempting a revert";
82*33f37583SAndroid Build Coastguard Worker RevertActiveSessions("", BOOT_TIMEOUT);
83*33f37583SAndroid Build Coastguard Worker }
84*33f37583SAndroid Build Coastguard Worker }
85*33f37583SAndroid Build Coastguard Worker }
86*33f37583SAndroid Build Coastguard Worker
MarkBootCompleted()87*33f37583SAndroid Build Coastguard Worker void ApexdLifecycle::MarkBootCompleted() { boot_completed_ = true; }
88*33f37583SAndroid Build Coastguard Worker
89*33f37583SAndroid Build Coastguard Worker } // namespace apex
90*33f37583SAndroid Build Coastguard Worker } // namespace android
91