xref: /aosp_15_r20/system/apex/apexd/apexd.h (revision 33f3758387333dbd2962d7edbd98681940d895da)
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 
59 static const ApexdConfig kDefaultConfig = {
60     kApexStatusSysprop,
61     kBuiltinApexPackageDirs,
62     kActiveApexPackagesDataDir,
63     kApexDecompressedDir,
64     kOtaReservedDir,
65     kStagedSessionsDir,
66     kVmPayloadMetadataPartitionProp,
67     "u:object_r:staging_data_file",
68 };
69 
70 class CheckpointInterface;
71 
72 void SetConfig(const ApexdConfig& config);
73 
74 // Exposed only for testing.
75 android::base::Result<void> Unmount(
76     const MountedApexDatabase::MountedApexData& data, bool deferred);
77 
78 android::base::Result<void> ResumeRevertIfNeeded();
79 
80 android::base::Result<void> PreinstallPackages(
81     const std::vector<std::string>& paths) WARN_UNUSED;
82 
83 android::base::Result<void> StagePackages(
84     const std::vector<std::string>& tmpPaths) WARN_UNUSED;
85 android::base::Result<void> UnstagePackages(
86     const std::vector<std::string>& paths) WARN_UNUSED;
87 
88 android::base::Result<std::vector<ApexFile>> SubmitStagedSession(
89     const int session_id, const std::vector<int>& child_session_ids,
90     const bool has_rollback_enabled, const bool is_rollback,
91     const int rollback_id) WARN_UNUSED;
92 android::base::Result<std::vector<ApexFile>> GetStagedApexFiles(
93     const int session_id,
94     const std::vector<int>& child_session_ids) WARN_UNUSED;
95 android::base::Result<ClassPath> MountAndDeriveClassPath(
96     const std::vector<ApexFile>&) WARN_UNUSED;
97 android::base::Result<void> MarkStagedSessionReady(const int session_id)
98     WARN_UNUSED;
99 android::base::Result<void> MarkStagedSessionSuccessful(const int session_id)
100     WARN_UNUSED;
101 // Only only of the parameters should be passed during revert
102 android::base::Result<void> RevertActiveSessions(
103     const std::string& crashing_native_process,
104     const std::string& error_message);
105 // Only only of the parameters should be passed during revert
106 android::base::Result<void> RevertActiveSessionsAndReboot(
107     const std::string& crashing_native_process,
108     const std::string& error_message);
109 
110 android::base::Result<void> ActivatePackage(const std::string& full_path)
111     WARN_UNUSED;
112 android::base::Result<void> DeactivatePackage(const std::string& full_path)
113     WARN_UNUSED;
114 
115 std::vector<ApexFile> GetActivePackages();
116 android::base::Result<ApexFile> GetActivePackage(
117     const std::string& package_name);
118 
119 std::vector<ApexFile> GetFactoryPackages();
120 
121 android::base::Result<void> AbortStagedSession(const int session_id);
122 
123 android::base::Result<void> SnapshotCeData(const int user_id,
124                                            const int rollback_id,
125                                            const std::string& apex_name);
126 android::base::Result<void> RestoreCeData(const int user_id,
127                                           const int rollback_id,
128                                           const std::string& apex_name);
129 
130 android::base::Result<void> DestroyDeSnapshots(const int rollback_id);
131 android::base::Result<void> DestroyCeSnapshots(const int user_id,
132                                                const int rollback_id);
133 android::base::Result<void> DestroyCeSnapshotsNotSpecified(
134     int user_id, const std::vector<int>& retain_rollback_ids);
135 
136 int OnBootstrap();
137 // Sets the values of gVoldService and gInFsCheckpointMode.
138 void InitializeVold(CheckpointInterface* checkpoint_service);
139 // Sets the value of gSessionManager.
140 void InitializeSessionManager(ApexSessionManager* session_manager);
141 // Initializes in-memory state (e.g. pre-installed data, activated apexes).
142 // Must be called first before calling any other boot sequence related function.
143 void Initialize(CheckpointInterface* checkpoint_service);
144 // Initializes data apex as in-memory state. Should be called only if we are
145 // not booting, since initialization timing is different when booting
146 void InitializeDataApex();
147 // Apex activation logic. Scans staged apex sessions and activates apexes.
148 // Must only be called during boot (i.e apexd.status is not "ready" or
149 // "activated").
150 void OnStart();
151 // For every package X, there can be at most two APEX, pre-installed vs
152 // installed on data. We decide which ones should be activated and return them
153 // as a list
154 std::vector<ApexFileRef> SelectApexForActivation(
155     const std::unordered_map<std::string, std::vector<ApexFileRef>>& all_apex,
156     const ApexFileRepository& instance);
157 std::vector<ApexFile> ProcessCompressedApex(
158     const std::vector<ApexFileRef>& compressed_apex, bool is_ota_chroot);
159 // Validate |apex| is same as |capex|
160 android::base::Result<void> ValidateDecompressedApex(const ApexFile& capex,
161                                                      const ApexFile& apex);
162 // Notifies system that apexes are activated by setting apexd.status property to
163 // "activated".
164 // Must only be called during boot (i.e. apexd.status is not "ready" or
165 // "activated").
166 void OnAllPackagesActivated(bool is_bootstrap);
167 // Notifies system that apexes are ready by setting apexd.status property to
168 // "ready".
169 // Must only be called during boot (i.e. apexd.status is not "ready" or
170 // "activated").
171 void OnAllPackagesReady();
172 void OnBootCompleted();
173 
174 // Removes inactivate apexes on /data after activation.
175 // This can happen when prebuilt APEXes are newer than /data apexes with OTA.
176 // Exposed for testing.
177 void RemoveInactiveDataApex();
178 
179 void BootCompletedCleanup();
180 int SnapshotOrRestoreDeUserData();
181 
182 // Unmounts all apexes.
183 // If `also_include_staged_apexes` is true, it's for Pre-reboot Dexopt.
184 int UnmountAll(bool also_include_staged_apexes);
185 
186 android::base::Result<MountedApexDatabase::MountedApexData>
187 GetTempMountedApexData(const std::string& package);
188 
189 // Exposed for unit tests
190 bool ShouldAllocateSpaceForDecompression(const std::string& new_apex_name,
191                                          int64_t new_apex_version,
192                                          const ApexFileRepository& instance);
193 
194 int64_t CalculateSizeForCompressedApex(
195     const std::vector<std::tuple<std::string, int64_t, int64_t>>&
196         compressed_apexes,
197     const ApexFileRepository& instance);
198 
199 // Casts |ApexPartition| to partition string used in XSD.
200 std::string CastPartition(ApexPartition partition);
201 void CollectApexInfoList(std::ostream& os,
202                          const std::vector<ApexFile>& active_apexs,
203                          const std::vector<ApexFile>& inactive_apexs);
204 
205 // Reserve |size| bytes in |dest_dir| by creating a zero-filled file
206 android::base::Result<void> ReserveSpaceForCompressedApex(
207     int64_t size, const std::string& dest_dir);
208 
209 // Entry point when running in the VM mode (with --vm arg)
210 int OnStartInVmMode();
211 
212 // Activates apexes in otapreot_chroot environment.
213 // If `also_include_staged_apexes` is true, it's for Pre-reboot Dexopt.
214 int OnOtaChrootBootstrap(bool also_include_staged_apexes);
215 
216 android::apex::MountedApexDatabase& GetApexDatabaseForTesting();
217 
218 // Performs a non-staged install of an APEX specified by |package_path|.
219 // TODO(ioffe): add more documentation.
220 android::base::Result<ApexFile> InstallPackage(const std::string& package_path,
221                                                bool force);
222 
223 bool IsActiveApexChanged(const ApexFile& apex);
224 
225 // Shouldn't be used outside of apexd_test.cpp
226 std::set<std::string>& GetChangedActiveApexesForTesting();
227 
228 ApexSessionManager* GetSessionManager();
229 
230 }  // namespace apex
231 }  // namespace android
232 
233 #endif  // ANDROID_APEXD_APEXD_H_
234