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 #ifndef ANDROID_APEXD_APEXD_H_ 18 #define ANDROID_APEXD_APEXD_H_ 19 20 #include <android-base/macros.h> 21 #include <android-base/result.h> 22 23 #include <ostream> 24 #include <string> 25 #include <vector> 26 27 #include "apex_classpath.h" 28 #include "apex_constants.h" 29 #include "apex_database.h" 30 #include "apex_file.h" 31 #include "apex_file_repository.h" 32 #include "apexd_session.h" 33 34 namespace android { 35 namespace apex { 36 37 // A structure containing all the values that might need to be injected for 38 // testing (e.g. apexd status property, etc.) 39 // 40 // Ideally we want to introduce Apexd class and use dependency injection for 41 // such values, but that will require a sizeable refactoring. For the time being 42 // this config should do the trick. 43 struct ApexdConfig { 44 const char* apex_status_sysprop; 45 std::unordered_map<ApexPartition, std::string> builtin_dirs; 46 const char* active_apex_data_dir; 47 const char* decompression_dir; 48 const char* ota_reserved_dir; 49 const char* staged_session_dir; 50 // Overrides the path to the "metadata" partition which is by default 51 // /dev/block/by-name/payload-metadata It should be a path pointing the first 52 // partition of the VM payload disk. So, realpath() of this path is checked if 53 // it has the suffix "1". For example, /test-dir/test-metadata-1 can be valid 54 // and the subsequent numbers should point APEX files. 55 const char* vm_payload_metadata_partition_prop; 56 const char* active_apex_selinux_ctx; 57 58 // TODO(b/381173074) True in tests for now. Will be configured as true if 59 // - new device (ro.vendor.api_level >= 202504 (TBD)) 60 // - or, upgrading device with migration done (e.g. flag in /metadata/apex) 61 bool mount_before_data; 62 }; 63 64 static const ApexdConfig kDefaultConfig = { 65 kApexStatusSysprop, 66 kBuiltinApexPackageDirs, 67 kActiveApexPackagesDataDir, 68 kApexDecompressedDir, 69 kOtaReservedDir, 70 kStagedSessionsDir, 71 kVmPayloadMetadataPartitionProp, 72 "u:object_r:staging_data_file", 73 false, /* mount_before_data */ 74 }; 75 76 class CheckpointInterface; 77 78 void SetConfig(const ApexdConfig& config); 79 80 // Exposed only for testing. 81 android::base::Result<void> Unmount( 82 const MountedApexDatabase::MountedApexData& data, bool deferred); 83 84 android::base::Result<void> ResumeRevertIfNeeded(); 85 86 android::base::Result<void> PreinstallPackages( 87 const std::vector<std::string>& paths) WARN_UNUSED; 88 89 android::base::Result<void> StagePackages( 90 const std::vector<std::string>& tmpPaths) WARN_UNUSED; 91 android::base::Result<void> UnstagePackages( 92 const std::vector<std::string>& paths) WARN_UNUSED; 93 94 android::base::Result<std::vector<ApexFile>> SubmitStagedSession( 95 const int session_id, const std::vector<int>& child_session_ids, 96 const bool has_rollback_enabled, const bool is_rollback, 97 const int rollback_id) WARN_UNUSED; 98 android::base::Result<std::vector<ApexFile>> GetStagedApexFiles( 99 const int session_id, 100 const std::vector<int>& child_session_ids) WARN_UNUSED; 101 android::base::Result<ClassPath> MountAndDeriveClassPath( 102 const std::vector<ApexFile>&) WARN_UNUSED; 103 android::base::Result<void> MarkStagedSessionReady(const int session_id) 104 WARN_UNUSED; 105 android::base::Result<void> MarkStagedSessionSuccessful(const int session_id) 106 WARN_UNUSED; 107 // Only only of the parameters should be passed during revert 108 android::base::Result<void> RevertActiveSessions( 109 const std::string& crashing_native_process, 110 const std::string& error_message); 111 // Only only of the parameters should be passed during revert 112 android::base::Result<void> RevertActiveSessionsAndReboot( 113 const std::string& crashing_native_process, 114 const std::string& error_message); 115 116 android::base::Result<void> ActivatePackage(const std::string& full_path) 117 WARN_UNUSED; 118 android::base::Result<void> DeactivatePackage(const std::string& full_path) 119 WARN_UNUSED; 120 121 std::vector<ApexFile> GetActivePackages(); 122 123 std::vector<ApexFile> GetFactoryPackages(); 124 125 android::base::Result<void> AbortStagedSession(const int session_id); 126 127 android::base::Result<void> SnapshotCeData(const int user_id, 128 const int rollback_id, 129 const std::string& apex_name); 130 android::base::Result<void> RestoreCeData(const int user_id, 131 const int rollback_id, 132 const std::string& apex_name); 133 134 android::base::Result<void> DestroyDeSnapshots(const int rollback_id); 135 android::base::Result<void> DestroyCeSnapshots(const int user_id, 136 const int rollback_id); 137 android::base::Result<void> DestroyCeSnapshotsNotSpecified( 138 int user_id, const std::vector<int>& retain_rollback_ids); 139 140 int OnBootstrap(); 141 // Sets the values of gVoldService and gInFsCheckpointMode. 142 void InitializeVold(CheckpointInterface* checkpoint_service); 143 // Sets the value of gSessionManager. 144 void InitializeSessionManager(ApexSessionManager* session_manager); 145 // Initializes in-memory state (e.g. pre-installed data, activated apexes). 146 // Must be called first before calling any other boot sequence related function. 147 void Initialize(CheckpointInterface* checkpoint_service); 148 // Initializes data apex as in-memory state. Should be called only if we are 149 // not booting, since initialization timing is different when booting 150 void InitializeDataApex(); 151 // Apex activation logic. Scans staged apex sessions and activates apexes. 152 // Must only be called during boot (i.e apexd.status is not "ready" or 153 // "activated"). 154 void OnStart(); 155 // For every package X, there can be at most two APEX, pre-installed vs 156 // installed on data. We decide which ones should be activated and return them 157 // as a list 158 std::vector<ApexFileRef> SelectApexForActivation(); 159 std::vector<ApexFile> ProcessCompressedApex( 160 const std::vector<ApexFileRef>& compressed_apex, bool is_ota_chroot); 161 // Validate |apex| is same as |capex| 162 android::base::Result<void> ValidateDecompressedApex(const ApexFile& capex, 163 const ApexFile& apex); 164 // Notifies system that apexes are activated by setting apexd.status property to 165 // "activated". 166 // Must only be called during boot (i.e. apexd.status is not "ready" or 167 // "activated"). 168 void OnAllPackagesActivated(bool is_bootstrap); 169 // Notifies system that apexes are ready by setting apexd.status property to 170 // "ready". 171 // Must only be called during boot (i.e. apexd.status is not "ready" or 172 // "activated"). 173 void OnAllPackagesReady(); 174 void OnBootCompleted(); 175 176 // Removes inactivate apexes on /data after activation. 177 // This can happen when prebuilt APEXes are newer than /data apexes with OTA. 178 // Exposed for testing. 179 void RemoveInactiveDataApex(); 180 181 void BootCompletedCleanup(); 182 int SnapshotOrRestoreDeUserData(); 183 184 // Unmounts all apexes. 185 // If `also_include_staged_apexes` is true, it's for Pre-reboot Dexopt. 186 int UnmountAll(bool also_include_staged_apexes); 187 188 android::base::Result<MountedApexDatabase::MountedApexData> 189 GetTempMountedApexData(const std::string& package); 190 191 // Exposed for unit tests 192 bool ShouldAllocateSpaceForDecompression(const std::string& new_apex_name, 193 int64_t new_apex_version, 194 const ApexFileRepository& instance, 195 const MountedApexDatabase& db); 196 197 int64_t CalculateSizeForCompressedApex( 198 const std::vector<std::tuple<std::string, int64_t, int64_t>>& 199 compressed_apexes); 200 201 // Casts |ApexPartition| to partition string used in XSD. 202 std::string CastPartition(ApexPartition partition); 203 void CollectApexInfoList(std::ostream& os, 204 const std::vector<ApexFile>& active_apexs, 205 const std::vector<ApexFile>& inactive_apexs); 206 207 // Reserve |size| bytes in |dest_dir| by creating a zero-filled file 208 android::base::Result<void> ReserveSpaceForCompressedApex( 209 int64_t size, const std::string& dest_dir); 210 211 // Entry point when running in the VM mode (with --vm arg) 212 int OnStartInVmMode(); 213 214 // Activates apexes in otapreot_chroot environment. 215 // If `also_include_staged_apexes` is true, it's for Pre-reboot Dexopt. 216 int OnOtaChrootBootstrap(bool also_include_staged_apexes); 217 218 android::apex::MountedApexDatabase& GetApexDatabaseForTesting(); 219 220 // Performs a non-staged install of an APEX specified by |package_path|. 221 // TODO(ioffe): add more documentation. 222 android::base::Result<ApexFile> InstallPackage(const std::string& package_path, 223 bool force); 224 225 bool IsActiveApexChanged(const ApexFile& apex); 226 227 // Shouldn't be used outside of apexd_test.cpp 228 std::set<std::string>& GetChangedActiveApexesForTesting(); 229 230 ApexSessionManager* GetSessionManager(); 231 232 } // namespace apex 233 } // namespace android 234 235 #endif // ANDROID_APEXD_APEXD_H_ 236