Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #define LOG_TAG "apexd" |
| 18 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 19 | #include <strings.h> |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 20 | |
Jiyong Park | b1d81b8 | 2019-05-10 21:12:27 +0900 | [diff] [blame] | 21 | #include <ApexProperties.sysprop.h> |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 24 | #include "apexd.h" |
Andreas Gampe | 6aaa2fe | 2019-03-29 14:13:59 -0700 | [diff] [blame] | 25 | #include "apexd_checkpoint_vold.h" |
Andreas Gampe | f766355 | 2019-01-03 09:22:11 -0800 | [diff] [blame] | 26 | #include "apexd_prepostinstall.h" |
Martijn Coenen | 95da273 | 2019-02-13 16:32:07 +0100 | [diff] [blame] | 27 | #include "apexd_prop.h" |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 28 | #include "apexservice.h" |
| 29 | |
Jiyong Park | 4d0f832 | 2019-02-02 19:45:57 +0900 | [diff] [blame] | 30 | #include <android-base/properties.h> |
| 31 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | int HandleSubcommand(char** argv) { |
| 35 | if (strcmp("--pre-install", argv[1]) == 0) { |
| 36 | LOG(INFO) << "Preinstall subcommand detected"; |
| 37 | return android::apex::RunPreInstall(argv); |
| 38 | } |
| 39 | |
Andreas Gampe | f766355 | 2019-01-03 09:22:11 -0800 | [diff] [blame] | 40 | if (strcmp("--post-install", argv[1]) == 0) { |
| 41 | LOG(INFO) << "Postinstall subcommand detected"; |
| 42 | return android::apex::RunPostInstall(argv); |
| 43 | } |
| 44 | |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 45 | if (strcmp("--bootstrap", argv[1]) == 0) { |
| 46 | LOG(INFO) << "Bootstrap subcommand detected"; |
| 47 | return android::apex::onBootstrap(); |
| 48 | } |
| 49 | |
Nikita Ioffe | 0867c79 | 2019-11-06 21:43:13 +0000 | [diff] [blame] | 50 | if (strcmp("--unmount-all", argv[1]) == 0) { |
| 51 | LOG(INFO) << "Unmount all subcommand detected"; |
| 52 | return android::apex::unmountAll(); |
| 53 | } |
| 54 | |
Oli Lan | a18705f | 2020-01-18 14:35:46 +0000 | [diff] [blame] | 55 | if (strcmp("--snapshotde", argv[1]) == 0) { |
| 56 | LOG(INFO) << "Snapshot DE subcommand detected"; |
Oli Lan | 30e598c | 2020-05-15 17:00:26 +0100 | [diff] [blame] | 57 | // Need to know if checkpointing is enabled so that a prerestore snapshot |
| 58 | // can be taken if it's not. |
| 59 | android::base::Result<android::apex::VoldCheckpointInterface> |
| 60 | vold_service_st = android::apex::VoldCheckpointInterface::Create(); |
| 61 | if (!vold_service_st.ok()) { |
| 62 | LOG(ERROR) << "Could not retrieve vold service: " |
| 63 | << vold_service_st.error(); |
| 64 | } else { |
| 65 | android::apex::initializeVold(&*vold_service_st); |
| 66 | } |
| 67 | |
Oli Lan | 54da92a | 2020-02-06 11:40:04 +0000 | [diff] [blame] | 68 | int result = android::apex::snapshotOrRestoreDeUserData(); |
| 69 | |
| 70 | if (result == 0) { |
| 71 | // Notify other components (e.g. init) that all APEXs are ready to be used |
| 72 | // Note that it's important that the binder service is registered at this |
| 73 | // point, since other system services might depend on it. |
| 74 | android::apex::onAllPackagesReady(); |
| 75 | } |
| 76 | return result; |
Oli Lan | a18705f | 2020-01-18 14:35:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 79 | LOG(ERROR) << "Unknown subcommand: " << argv[1]; |
| 80 | return 1; |
| 81 | } |
| 82 | |
Nikita Ioffe | e9684ac | 2020-05-07 14:14:19 +0100 | [diff] [blame] | 83 | void InstallSigtermSignalHandler() { |
| 84 | struct sigaction action = {}; |
| 85 | action.sa_handler = [](int /*signal*/) { |
| 86 | // Handle SIGTERM gracefully. |
| 87 | // By default, when SIGTERM is received a process will exit with non-zero |
| 88 | // exit code, which will trigger reboot_on_failure handler if one is |
| 89 | // defined. This doesn't play well with userspace reboot which might |
| 90 | // terminate apexd with SIGTERM if apexd was running at the moment of |
| 91 | // userspace reboot, hence this custom handler to exit gracefully. |
| 92 | _exit(0); |
| 93 | }; |
| 94 | sigaction(SIGTERM, &action, nullptr); |
| 95 | } |
| 96 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 97 | } // namespace |
| 98 | |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 99 | int main(int /*argc*/, char** argv) { |
Nikita Ioffe | 9c8e262 | 2019-10-25 01:06:53 +0100 | [diff] [blame] | 100 | android::base::InitLogging(argv, &android::base::KernelLogger); |
Nikita Ioffe | 6ed9ccd | 2019-10-11 11:19:18 +0100 | [diff] [blame] | 101 | // TODO: add a -v flag or an external setting to change LogSeverity. |
| 102 | android::base::SetMinimumLogSeverity(android::base::VERBOSE); |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 103 | |
Nikita Ioffe | e9684ac | 2020-05-07 14:14:19 +0100 | [diff] [blame] | 104 | InstallSigtermSignalHandler(); |
| 105 | |
Nikita Ioffe | 6ed9ccd | 2019-10-11 11:19:18 +0100 | [diff] [blame] | 106 | const bool has_subcommand = argv[1] != nullptr; |
Jiyong Park | b1d81b8 | 2019-05-10 21:12:27 +0900 | [diff] [blame] | 107 | if (!android::sysprop::ApexProperties::updatable().value_or(false)) { |
| 108 | LOG(INFO) << "This device does not support updatable APEX. Exiting"; |
Nikita Ioffe | 6ed9ccd | 2019-10-11 11:19:18 +0100 | [diff] [blame] | 109 | if (!has_subcommand) { |
Oli Lan | 54da92a | 2020-02-06 11:40:04 +0000 | [diff] [blame] | 110 | // mark apexd as activated so that init can proceed |
| 111 | android::apex::onAllPackagesActivated(); |
Oli Lan | 54da92a | 2020-02-06 11:40:04 +0000 | [diff] [blame] | 112 | } else if (strcmp("--snapshotde", argv[1]) == 0) { |
| 113 | // mark apexd as ready |
| 114 | android::apex::onAllPackagesReady(); |
Nikita Ioffe | 6ed9ccd | 2019-10-11 11:19:18 +0100 | [diff] [blame] | 115 | } |
Jiyong Park | b1d81b8 | 2019-05-10 21:12:27 +0900 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Nikita Ioffe | 6ed9ccd | 2019-10-11 11:19:18 +0100 | [diff] [blame] | 119 | if (has_subcommand) { |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 120 | return HandleSubcommand(argv); |
| 121 | } |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 122 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 123 | android::base::Result<android::apex::VoldCheckpointInterface> |
Andreas Gampe | 6aaa2fe | 2019-03-29 14:13:59 -0700 | [diff] [blame] | 124 | vold_service_st = android::apex::VoldCheckpointInterface::Create(); |
| 125 | android::apex::VoldCheckpointInterface* vold_service = nullptr; |
Bernie Innocenti | d04d5d0 | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 126 | if (!vold_service_st.ok()) { |
Andreas Gampe | 6aaa2fe | 2019-03-29 14:13:59 -0700 | [diff] [blame] | 127 | LOG(ERROR) << "Could not retrieve vold service: " |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 128 | << vold_service_st.error(); |
Andreas Gampe | 6aaa2fe | 2019-03-29 14:13:59 -0700 | [diff] [blame] | 129 | } else { |
| 130 | vold_service = &*vold_service_st; |
| 131 | } |
Nikita Ioffe | dee4b6e | 2020-04-22 01:01:17 +0100 | [diff] [blame] | 132 | android::apex::initialize(vold_service); |
Andreas Gampe | 6aaa2fe | 2019-03-29 14:13:59 -0700 | [diff] [blame] | 133 | |
Nikita Ioffe | dee4b6e | 2020-04-22 01:01:17 +0100 | [diff] [blame] | 134 | bool booting = android::apex::isBooting(); |
| 135 | if (booting) { |
| 136 | android::apex::migrateSessionsDirIfNeeded(); |
| 137 | android::apex::onStart(); |
| 138 | } |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 139 | android::apex::binder::CreateAndRegisterService(); |
Zimuzo | 9cc0be4 | 2019-01-09 11:37:34 +0000 | [diff] [blame] | 140 | android::apex::binder::StartThreadPool(); |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 141 | |
Nikita Ioffe | dee4b6e | 2020-04-22 01:01:17 +0100 | [diff] [blame] | 142 | if (booting) { |
| 143 | // Notify other components (e.g. init) that all APEXs are correctly mounted |
| 144 | // and activated (but are not yet ready to be used). Configuration based on |
| 145 | // activated APEXs may be performed at this point, but use of APEXs |
| 146 | // themselves should wait for the ready status instead, which is set when |
| 147 | // the "--snapshotde" subcommand is received and snapshot/restore is |
| 148 | // complete. |
| 149 | android::apex::onAllPackagesActivated(); |
| 150 | android::apex::waitForBootStatus( |
| 151 | android::apex::revertActiveSessionsAndReboot, |
| 152 | android::apex::bootCompletedCleanup); |
| 153 | } |
Martijn Coenen | 95da273 | 2019-02-13 16:32:07 +0100 | [diff] [blame] | 154 | |
Jon Spivack | 1b84030 | 2020-02-06 18:34:30 -0800 | [diff] [blame] | 155 | android::apex::binder::AllowServiceShutdown(); |
| 156 | |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 157 | android::apex::binder::JoinThreadPool(); |
Andreas Gampe | 9d016d5 | 2018-10-19 18:56:50 -0700 | [diff] [blame] | 158 | return 1; |
| 159 | } |