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::vector<std::string> apex_built_in_dirs; 46 const char* active_apex_data_dir; 47 const char* decompression_dir; 48 const char* ota_reserved_dir; 49 const char* apex_hash_tree_dir; 50 const char* staged_session_dir; 51 // Overrides the path to the "metadata" partition which is by default 52 // /dev/block/by-name/payload-metadata It should be a path pointing the first 53 // partition of the VM payload disk. So, realpath() of this path is checked if 54 // it has the suffix "1". For example, /test-dir/test-metadata-1 can be valid 55 // and the subsequent numbers should point APEX files. 56 const char* vm_payload_metadata_partition_prop; 57 const char* active_apex_selinux_ctx; 58 }; 59 60 static const ApexdConfig kDefaultConfig = { 61 kApexStatusSysprop, 62 kApexPackageBuiltinDirs, 63 kActiveApexPackagesDataDir, 64 kApexDecompressedDir, 65 kOtaReservedDir, 66 kApexHashTreeDir, 67 kStagedSessionsDir, 68 kVmPayloadMetadataPartitionProp, 69 "u:object_r:staging_data_file", 70 }; 71 72 class CheckpointInterface; 73 74 void SetConfig(const ApexdConfig& config); 75 76 // Exposed only for testing. 77 android::base::Result<void> Unmount( 78 const MountedApexDatabase::MountedApexData& data, bool deferred); 79 80 android::base::Result<void> ResumeRevertIfNeeded(); 81 82 android::base::Result<void> PreinstallPackages( 83 const std::vector<std::string>& paths) WARN_UNUSED; 84 85 android::base::Result<void> StagePackages( 86 const std::vector<std::string>& tmpPaths) WARN_UNUSED; 87 android::base::Result<void> UnstagePackages( 88 const std::vector<std::string>& paths) WARN_UNUSED; 89 90 android::base::Result<std::vector<ApexFile>> SubmitStagedSession( 91 const int session_id, const std::vector<int>& child_session_ids, 92 const bool has_rollback_enabled, const bool is_rollback, 93 const int rollback_id) WARN_UNUSED; 94 android::base::Result<std::vector<ApexFile>> GetStagedApexFiles( 95 const int session_id, 96 const std::vector<int>& child_session_ids) WARN_UNUSED; 97 android::base::Result<ClassPath> MountAndDeriveClassPath( 98 const std::vector<ApexFile>&) WARN_UNUSED; 99 android::base::Result<void> MarkStagedSessionReady(const int session_id) 100 WARN_UNUSED; 101 android::base::Result<void> MarkStagedSessionSuccessful(const int session_id) 102 WARN_UNUSED; 103 // Only only of the parameters should be passed during revert 104 android::base::Result<void> RevertActiveSessions( 105 const std::string& crashing_native_process, 106 const std::string& error_message); 107 // Only only of the parameters should be passed during revert 108 android::base::Result<void> RevertActiveSessionsAndReboot( 109 const std::string& crashing_native_process, 110 const std::string& error_message); 111 112 android::base::Result<void> ActivatePackage(const std::string& full_path) 113 WARN_UNUSED; 114 android::base::Result<void> DeactivatePackage(const std::string& full_path) 115 WARN_UNUSED; 116 117 std::vector<ApexFile> GetActivePackages(); 118 android::base::Result<ApexFile> GetActivePackage( 119 const std::string& package_name); 120 121 std::vector<ApexFile> GetFactoryPackages(); 122 123 android::base::Result<void> AbortStagedSession(const int session_id); 124 125 android::base::Result<void> SnapshotCeData(const int user_id, 126 const int rollback_id, 127 const std::string& apex_name); 128 android::base::Result<void> RestoreCeData(const int user_id, 129 const int rollback_id, 130 const std::string& apex_name); 131 132 android::base::Result<void> DestroyDeSnapshots(const int rollback_id); 133 android::base::Result<void> DestroyCeSnapshots(const int user_id, 134 const int rollback_id); 135 android::base::Result<void> DestroyCeSnapshotsNotSpecified( 136 int user_id, const std::vector<int>& retain_rollback_ids); 137 138 int OnBootstrap(); 139 // Sets the values of gVoldService and gInFsCheckpointMode. 140 void InitializeVold(CheckpointInterface* checkpoint_service); 141 // Sets the value of gSessionManager. 142 void InitializeSessionManager(ApexSessionManager* session_manager); 143 // Initializes in-memory state (e.g. pre-installed data, activated apexes). 144 // Must be called first before calling any other boot sequence related function. 145 void Initialize(CheckpointInterface* checkpoint_service); 146 // Initializes data apex as in-memory state. Should be called only if we are 147 // not booting, since initialization timing is different when booting 148 void InitializeDataApex(); 149 // Apex activation logic. Scans staged apex sessions and activates apexes. 150 // Must only be called during boot (i.e apexd.status is not "ready" or 151 // "activated"). 152 void OnStart(); 153 // For every package X, there can be at most two APEX, pre-installed vs 154 // installed on data. We decide which ones should be activated and return them 155 // as a list 156 std::vector<ApexFileRef> SelectApexForActivation( 157 const std::unordered_map<std::string, std::vector<ApexFileRef>>& all_apex, 158 const ApexFileRepository& instance); 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 // Optimistically tries to remount as many APEX packages as possible. 192 // For more documentation see corresponding binder call in IApexService.aidl. 193 android::base::Result<void> RemountPackages(); 194 195 // Exposed for unit tests 196 bool ShouldAllocateSpaceForDecompression(const std::string& new_apex_name, 197 int64_t new_apex_version, 198 const ApexFileRepository& instance); 199 200 int64_t CalculateSizeForCompressedApex( 201 const std::vector<std::tuple<std::string, int64_t, int64_t>>& 202 compressed_apexes, 203 const ApexFileRepository& instance); 204 205 void CollectApexInfoList(std::ostream& os, 206 const std::vector<ApexFile>& active_apexs, 207 const std::vector<ApexFile>& inactive_apexs); 208 209 // Reserve |size| bytes in |dest_dir| by creating a zero-filled file 210 android::base::Result<void> ReserveSpaceForCompressedApex( 211 int64_t size, const std::string& dest_dir); 212 213 // Entry point when running in the VM mode (with --vm arg) 214 int OnStartInVmMode(); 215 216 // Activates apexes in otapreot_chroot environment. 217 // If `also_include_staged_apexes` is true, it's for Pre-reboot Dexopt. 218 int OnOtaChrootBootstrap(bool also_include_staged_apexes); 219 220 android::apex::MountedApexDatabase& GetApexDatabaseForTesting(); 221 222 // Performs a non-staged install of an APEX specified by |package_path|. 223 // TODO(ioffe): add more documentation. 224 android::base::Result<ApexFile> InstallPackage(const std::string& package_path, 225 bool force); 226 227 // Exposed for testing. 228 android::base::Result<int> AddBlockApex(ApexFileRepository& instance); 229 230 bool IsActiveApexChanged(const ApexFile& apex); 231 232 // Shouldn't be used outside of apexd_test.cpp 233 std::set<std::string>& GetChangedActiveApexesForTesting(); 234 235 } // namespace apex 236 } // namespace android 237 238 #endif // ANDROID_APEXD_APEXD_H_ 239