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_lifecycle.h"
21
22 #include <android-base/logging.h>
23 #include <android-base/properties.h>
24
25 #include "apexd_utils.h"
26
27 #define LOG_TAG "apexd"
28
29 using android::base::GetProperty;
30 using android::base::Result;
31 using android::base::WaitForProperty;
32
33 namespace android {
34 namespace apex {
35
IsBooting()36 bool ApexdLifecycle::IsBooting() {
37 auto status = GetProperty(kApexStatusSysprop, "");
38 return status != kApexStatusReady && status != kApexStatusActivated;
39 }
40
WaitForBootStatus(Result<void> (& revert_fn)(const std::string &,const std::string &))41 void ApexdLifecycle::WaitForBootStatus(
42 Result<void> (&revert_fn)(const std::string&, const std::string&)) {
43 while (!boot_completed_) {
44 // Check for change in either crashing property or sys.boot_completed
45 // Wait for updatable_crashing property change for most of the time
46 // (arbitrary 10s), briefly check if boot has completed successfully,
47 // if not continue waiting for updatable_crashing.
48 // We use this strategy so that we can quickly detect if an updatable
49 // process is crashing.
50 if (WaitForProperty("sys.init.updatable_crashing", "1",
51 std::chrono::seconds(10))) {
52 auto name = GetProperty("sys.init.updatable_crashing_process_name", "");
53 LOG(ERROR) << "Native process '" << (name.empty() ? "[unknown]" : name)
54 << "' is crashing. Attempting a revert";
55 auto result = revert_fn(name, "");
56 if (!result.ok()) {
57 LOG(ERROR) << "Revert failed : " << result.error();
58 return WaitForBootStatus();
59 } else {
60 // This should never be reached, since revert_fn should've rebooted
61 // the device.
62 LOG(FATAL) << "Active sessions were reverted, but reboot wasn't "
63 "triggered.";
64 }
65 }
66 }
67 }
68
WaitForBootStatus()69 void ApexdLifecycle::WaitForBootStatus() {
70 while (!boot_completed_) {
71 std::this_thread::sleep_for(std::chrono::seconds(1));
72 }
73 }
74
MarkBootCompleted()75 void ApexdLifecycle::MarkBootCompleted() { boot_completed_ = true; }
76
77 } // namespace apex
78 } // namespace android
79