xref: /aosp_15_r20/system/apex/apexd/apexd_lifecycle.cpp (revision 33f3758387333dbd2962d7edbd98681940d895da)
1 /*
2  * Copyright (C) 2020 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 
17 #include <chrono>
18 #include <thread>
19 
20 #include "apexd.h"
21 #include "apexd_lifecycle.h"
22 
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 
26 #include "apexd_utils.h"
27 
28 using android::base::GetProperty;
29 using android::base::Result;
30 using android::base::WaitForProperty;
31 
32 constexpr int MAX_WAIT_COUNT = 60;
33 constexpr int WAIT_DURATION_SECONDS = 10;
34 static const char* BOOT_TIMEOUT = "BootTimeout"; // NOLINT
35 
36 namespace android {
37 namespace apex {
38 
IsBooting()39 bool ApexdLifecycle::IsBooting() {
40   auto status = GetProperty(kApexStatusSysprop, "");
41   return status != kApexStatusReady && status != kApexStatusActivated;
42 }
43 
RevertActiveSessions(const std::string & process,const std::string & error)44 void ApexdLifecycle::RevertActiveSessions(const std::string& process,
45                                           const std::string& error) {
46   auto result = RevertActiveSessionsAndReboot(process, error);
47   if (!result.ok()) {
48     if (error != BOOT_TIMEOUT) {
49       LOG(ERROR) << "Revert failed : " << result.error();
50       // Can not anything more but loop until boot successfully
51       while (!boot_completed_) {
52         std::this_thread::sleep_for(std::chrono::seconds(1));
53       }
54       return;
55     }
56   }
57   // This should never be reached
58   LOG(FATAL) << "Active sessions were reverted, but reboot wasn't triggered.";
59 }
60 
WaitForBootStatus(const bool has_active_session)61 void ApexdLifecycle::WaitForBootStatus(const bool has_active_session) {
62   int wait_count = 0;
63   while (!boot_completed_) {
64     // Check for change in either crashing property or sys.boot_completed
65     // Wait for updatable_crashing property change for most of the time
66     // (arbitrary 10s), briefly check if boot has completed successfully,
67     // if not continue waiting for updatable_crashing.
68     // We use this strategy so that we can quickly detect if an updatable
69     // process is crashing.
70     if (WaitForProperty("sys.init.updatable_crashing", "1",
71                         std::chrono::seconds(WAIT_DURATION_SECONDS))) {
72       auto name = GetProperty("sys.init.updatable_crashing_process_name", "");
73       LOG(ERROR) << "Native process '" << (name.empty() ? "[unknown]" : name)
74                  << "' is crashing. Attempting a revert";
75       RevertActiveSessions(name, "");
76     }
77     // Check if system stuck in boot screen and revert the staging apex once
78     if (has_active_session && ++wait_count == MAX_WAIT_COUNT) {
79       LOG(ERROR) << "System didn't finish boot in "
80                  << (WAIT_DURATION_SECONDS * MAX_WAIT_COUNT)
81                  << " seconds. Attempting a revert";
82       RevertActiveSessions("", BOOT_TIMEOUT);
83     }
84   }
85 }
86 
MarkBootCompleted()87 void ApexdLifecycle::MarkBootCompleted() { boot_completed_ = true; }
88 
89 }  // namespace apex
90 }  // namespace android
91