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