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 <ostream> 21 #include <string> 22 #include <vector> 23 24 #include <android-base/macros.h> 25 #include <android-base/result.h> 26 27 #include "apex_constants.h" 28 #include "apex_database.h" 29 #include "apex_file.h" 30 #include "apex_file_repository.h" 31 #include "apexd_session.h" 32 33 namespace android { 34 namespace apex { 35 36 // A structure containing all the values that might need to be injected for 37 // testing (e.g. apexd status property, etc.) 38 // 39 // Ideally we want to introduce Apexd class and use dependency injection for 40 // such values, but that will require a sizeable refactoring. For the time being 41 // this config should do the trick. 42 struct ApexdConfig { 43 const char* apex_status_sysprop; 44 std::vector<std::string> apex_built_in_dirs; 45 const char* active_apex_data_dir; 46 const char* decompression_dir; 47 const char* ota_reserved_dir; 48 const char* apex_hash_tree_dir; 49 const char* staged_session_dir; 50 }; 51 52 static const ApexdConfig kDefaultConfig = { 53 kApexStatusSysprop, kApexPackageBuiltinDirs, kActiveApexPackagesDataDir, 54 kApexDecompressedDir, kOtaReservedDir, kApexHashTreeDir, 55 kStagedSessionsDir, 56 }; 57 58 class CheckpointInterface; 59 60 void SetConfig(const ApexdConfig& config); 61 62 // Exposed only for testing. 63 android::base::Result<void> Unmount( 64 const MountedApexDatabase::MountedApexData& data, bool deferred); 65 66 android::base::Result<void> ResumeRevertIfNeeded(); 67 68 android::base::Result<void> PreinstallPackages( 69 const std::vector<std::string>& paths) WARN_UNUSED; 70 android::base::Result<void> PostinstallPackages( 71 const std::vector<std::string>& paths) WARN_UNUSED; 72 73 android::base::Result<void> StagePackages( 74 const std::vector<std::string>& tmpPaths) WARN_UNUSED; 75 android::base::Result<void> UnstagePackages( 76 const std::vector<std::string>& paths) WARN_UNUSED; 77 78 android::base::Result<std::vector<ApexFile>> SubmitStagedSession( 79 const int session_id, const std::vector<int>& child_session_ids, 80 const bool has_rollback_enabled, const bool is_rollback, 81 const int rollback_id) WARN_UNUSED; 82 android::base::Result<void> MarkStagedSessionReady(const int session_id) 83 WARN_UNUSED; 84 android::base::Result<void> MarkStagedSessionSuccessful(const int session_id) 85 WARN_UNUSED; 86 // Only only of the parameters should be passed during revert 87 android::base::Result<void> RevertActiveSessions( 88 const std::string& crashing_native_process, 89 const std::string& error_message); 90 // Only only of the parameters should be passed during revert 91 android::base::Result<void> RevertActiveSessionsAndReboot( 92 const std::string& crashing_native_process, 93 const std::string& error_message); 94 95 android::base::Result<void> ActivatePackage(const std::string& full_path) 96 WARN_UNUSED; 97 android::base::Result<void> DeactivatePackage(const std::string& full_path) 98 WARN_UNUSED; 99 100 std::vector<ApexFile> GetActivePackages(); 101 android::base::Result<ApexFile> GetActivePackage( 102 const std::string& package_name); 103 104 std::vector<ApexFile> GetFactoryPackages(); 105 106 android::base::Result<void> AbortStagedSession(const int session_id); 107 108 android::base::Result<void> SnapshotCeData(const int user_id, 109 const int rollback_id, 110 const std::string& apex_name); 111 android::base::Result<void> RestoreCeData(const int user_id, 112 const int rollback_id, 113 const std::string& apex_name); 114 115 android::base::Result<void> DestroyDeSnapshots(const int rollback_id); 116 android::base::Result<void> DestroyCeSnapshots(const int user_id, 117 const int rollback_id); 118 android::base::Result<void> DestroyCeSnapshotsNotSpecified( 119 int user_id, const std::vector<int>& retain_rollback_ids); 120 121 int OnBootstrap(); 122 // Sets the values of gVoldService and gInFsCheckpointMode. 123 void InitializeVold(CheckpointInterface* checkpoint_service); 124 // Initializes in-memory state (e.g. pre-installed data, activated apexes). 125 // Must be called first before calling any other boot sequence related function. 126 void Initialize(CheckpointInterface* checkpoint_service); 127 // Initializes data apex as in-memory state. Should be called only if we are 128 // not booting, since initialization timing is different when booting 129 void InitializeDataApex(); 130 // Migrates sessions from /data/apex/session to /metadata/session.i 131 // Must only be called during boot (i.e apexd.status is not "ready" or 132 // "activated"). 133 android::base::Result<void> MigrateSessionsDirIfNeeded(); 134 // Apex activation logic. Scans staged apex sessions and activates apexes. 135 // Must only be called during boot (i.e apexd.status is not "ready" or 136 // "activated"). 137 void OnStart(); 138 // For every package X, there can be at most two APEX, pre-installed vs 139 // installed on data. We decide which ones should be activated and return them 140 // as a list 141 std::vector<ApexFileRef> SelectApexForActivation( 142 const std::unordered_map<std::string, std::vector<ApexFileRef>>& all_apex, 143 const ApexFileRepository& instance); 144 std::vector<ApexFile> ProcessCompressedApex( 145 const std::vector<ApexFileRef>& compressed_apex, bool is_ota_chroot); 146 // Validate |apex| is same as |capex| 147 android::base::Result<void> ValidateDecompressedApex(const ApexFile& capex, 148 const ApexFile& apex); 149 // Notifies system that apexes are activated by setting apexd.status property to 150 // "activated". 151 // Must only be called during boot (i.e. apexd.status is not "ready" or 152 // "activated"). 153 void OnAllPackagesActivated(bool is_bootstrap); 154 // Notifies system that apexes are ready by setting apexd.status property to 155 // "ready". 156 // Must only be called during boot (i.e. apexd.status is not "ready" or 157 // "activated"). 158 void OnAllPackagesReady(); 159 void OnBootCompleted(); 160 // Exposed for testing 161 void RemoveInactiveDataApex(); 162 void BootCompletedCleanup(); 163 int SnapshotOrRestoreDeUserData(); 164 165 int UnmountAll(); 166 167 android::base::Result<MountedApexDatabase::MountedApexData> 168 GetTempMountedApexData(const std::string& package); 169 170 // Optimistically tries to remount as many APEX packages as possible. 171 // For more documentation see corresponding binder call in IApexService.aidl. 172 android::base::Result<void> RemountPackages(); 173 174 // Exposed for unit tests 175 android::base::Result<bool> ShouldAllocateSpaceForDecompression( 176 const std::string& new_apex_name, int64_t new_apex_version, 177 const ApexFileRepository& instance); 178 179 void CollectApexInfoList(std::ostream& os, 180 const std::vector<ApexFile>& active_apexs, 181 const std::vector<ApexFile>& inactive_apexs); 182 183 // Reserve |size| bytes in |dest_dir| by creating a zero-filled file 184 android::base::Result<void> ReserveSpaceForCompressedApex( 185 int64_t size, const std::string& dest_dir); 186 187 // Activates apexes in otapreot_chroot environment. 188 // TODO(b/172911822): support compressed apexes. 189 int OnOtaChrootBootstrap(); 190 191 // Activates flattened apexes in otapreopt_chroot environment. 192 int OnOtaChrootBootstrapFlattenedApex(); 193 194 android::apex::MountedApexDatabase& GetApexDatabaseForTesting(); 195 196 // Performs a non-staged install of an APEX specified by |package_path|. 197 // TODO(ioffe): add more documentation. 198 android::base::Result<ApexFile> InstallPackage(const std::string& package_path); 199 200 } // namespace apex 201 } // namespace android 202 203 #endif // ANDROID_APEXD_APEXD_H_ 204