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