1 /*
2 * Copyright (C) 2019 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 #include "apexd_session.h"
18
19 #include "apexd_utils.h"
20 #include "string_log.h"
21
22 #include "session_state.pb.h"
23
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include <filesystem>
30 #include <fstream>
31 #include <optional>
32 #include <utility>
33
34 using android::base::Error;
35 using android::base::Result;
36 using android::base::StringPrintf;
37 using apex::proto::SessionState;
38
39 namespace android {
40 namespace apex {
41
42 namespace {
43
44 // Starting from R, apexd prefers /metadata partition (kNewApexSessionsDir) as
45 // location for sessions-related information. For devices that don't have
46 // /metadata partition, apexd will fallback to the /data one
47 // (kOldApexSessionsDir).
48 static constexpr const char* kOldApexSessionsDir = "/data/apex/sessions";
49 static constexpr const char* kNewApexSessionsDir = "/metadata/apex/sessions";
50
51 static constexpr const char* kStateFileName = "state";
52
53 } // namespace
54
ApexSession(SessionState state)55 ApexSession::ApexSession(SessionState state) : state_(std::move(state)) {}
56
GetSessionsDir()57 std::string ApexSession::GetSessionsDir() {
58 static std::string result;
59 static std::once_flag once_flag;
60 std::call_once(once_flag, [&]() {
61 auto status =
62 FindFirstExistingDirectory(kNewApexSessionsDir, kOldApexSessionsDir);
63 if (!status.ok()) {
64 LOG(FATAL) << status.error();
65 }
66 result = std::move(*status);
67 });
68 return result;
69 }
70
MigrateToMetadataSessionsDir()71 Result<void> ApexSession::MigrateToMetadataSessionsDir() {
72 return MoveDir(kOldApexSessionsDir, kNewApexSessionsDir);
73 }
74
CreateSession(int session_id)75 Result<ApexSession> ApexSession::CreateSession(int session_id) {
76 SessionState state;
77 // Create session directory
78 std::string session_dir = GetSessionsDir() + "/" + std::to_string(session_id);
79 if (auto status = createDirIfNeeded(session_dir, 0700); !status.ok()) {
80 return status.error();
81 }
82 state.set_id(session_id);
83
84 return ApexSession(state);
85 }
86
GetSessionFromFile(const std::string & path)87 Result<ApexSession> ApexSession::GetSessionFromFile(const std::string& path) {
88 SessionState state;
89 std::fstream stateFile(path, std::ios::in | std::ios::binary);
90 if (!stateFile) {
91 return Error() << "Failed to open " << path;
92 }
93
94 if (!state.ParseFromIstream(&stateFile)) {
95 return Error() << "Failed to parse " << path;
96 }
97
98 return ApexSession(state);
99 }
100
GetSession(int session_id)101 Result<ApexSession> ApexSession::GetSession(int session_id) {
102 auto path = StringPrintf("%s/%d/%s", GetSessionsDir().c_str(), session_id,
103 kStateFileName);
104
105 return GetSessionFromFile(path);
106 }
107
GetSessions()108 std::vector<ApexSession> ApexSession::GetSessions() {
109 std::vector<ApexSession> sessions;
110
111 Result<std::vector<std::string>> sessionPaths = ReadDir(
112 GetSessionsDir(), [](const std::filesystem::directory_entry& entry) {
113 std::error_code ec;
114 return entry.is_directory(ec);
115 });
116
117 if (!sessionPaths.ok()) {
118 return sessions;
119 }
120
121 for (const std::string& sessionDirPath : *sessionPaths) {
122 // Try to read session state
123 auto session = GetSessionFromFile(sessionDirPath + "/" + kStateFileName);
124 if (!session.ok()) {
125 LOG(WARNING) << session.error();
126 continue;
127 }
128 sessions.push_back(std::move(*session));
129 }
130
131 return sessions;
132 }
133
GetSessionsInState(SessionState::State state)134 std::vector<ApexSession> ApexSession::GetSessionsInState(
135 SessionState::State state) {
136 auto sessions = GetSessions();
137 sessions.erase(
138 std::remove_if(sessions.begin(), sessions.end(),
139 [&](const ApexSession &s) { return s.GetState() != state; }),
140 sessions.end());
141
142 return sessions;
143 }
144
GetActiveSessions()145 std::vector<ApexSession> ApexSession::GetActiveSessions() {
146 auto sessions = GetSessions();
147 std::vector<ApexSession> activeSessions;
148 for (const ApexSession& session : sessions) {
149 if (!session.IsFinalized() && session.GetState() != SessionState::UNKNOWN) {
150 activeSessions.push_back(session);
151 }
152 }
153 return activeSessions;
154 }
155
GetState() const156 SessionState::State ApexSession::GetState() const { return state_.state(); }
157
GetId() const158 int ApexSession::GetId() const { return state_.id(); }
159
GetBuildFingerprint() const160 std::string ApexSession::GetBuildFingerprint() const {
161 return state_.expected_build_fingerprint();
162 }
163
IsFinalized() const164 bool ApexSession::IsFinalized() const {
165 switch (GetState()) {
166 case SessionState::SUCCESS:
167 case SessionState::ACTIVATION_FAILED:
168 case SessionState::REVERTED:
169 case SessionState::REVERT_FAILED:
170 return true;
171 default:
172 return false;
173 }
174 }
175
HasRollbackEnabled() const176 bool ApexSession::HasRollbackEnabled() const {
177 return state_.rollback_enabled();
178 }
179
IsRollback() const180 bool ApexSession::IsRollback() const { return state_.is_rollback(); }
181
GetRollbackId() const182 int ApexSession::GetRollbackId() const { return state_.rollback_id(); }
183
GetCrashingNativeProcess() const184 std::string ApexSession::GetCrashingNativeProcess() const {
185 return state_.crashing_native_process();
186 }
187
GetChildSessionIds() const188 const google::protobuf::RepeatedField<int> ApexSession::GetChildSessionIds()
189 const {
190 return state_.child_session_ids();
191 }
192
SetChildSessionIds(const std::vector<int> & child_session_ids)193 void ApexSession::SetChildSessionIds(
194 const std::vector<int>& child_session_ids) {
195 *(state_.mutable_child_session_ids()) = {child_session_ids.begin(),
196 child_session_ids.end()};
197 }
198
199 const google::protobuf::RepeatedPtrField<std::string>
GetApexNames() const200 ApexSession::GetApexNames() const {
201 return state_.apex_names();
202 }
203
SetBuildFingerprint(const std::string & fingerprint)204 void ApexSession::SetBuildFingerprint(const std::string& fingerprint) {
205 *(state_.mutable_expected_build_fingerprint()) = fingerprint;
206 }
207
SetHasRollbackEnabled(const bool enabled)208 void ApexSession::SetHasRollbackEnabled(const bool enabled) {
209 state_.set_rollback_enabled(enabled);
210 }
211
SetIsRollback(const bool is_rollback)212 void ApexSession::SetIsRollback(const bool is_rollback) {
213 state_.set_is_rollback(is_rollback);
214 }
215
SetRollbackId(const int rollback_id)216 void ApexSession::SetRollbackId(const int rollback_id) {
217 state_.set_rollback_id(rollback_id);
218 }
219
SetCrashingNativeProcess(const std::string & crashing_process)220 void ApexSession::SetCrashingNativeProcess(
221 const std::string& crashing_process) {
222 state_.set_crashing_native_process(crashing_process);
223 }
224
AddApexName(const std::string & apex_name)225 void ApexSession::AddApexName(const std::string& apex_name) {
226 state_.add_apex_names(apex_name);
227 }
228
UpdateStateAndCommit(const SessionState::State & session_state)229 Result<void> ApexSession::UpdateStateAndCommit(
230 const SessionState::State& session_state) {
231 state_.set_state(session_state);
232
233 auto state_file_path = StringPrintf("%s/%d/%s", GetSessionsDir().c_str(),
234 state_.id(), kStateFileName);
235
236 std::fstream state_file(state_file_path,
237 std::ios::out | std::ios::trunc | std::ios::binary);
238 if (!state_.SerializeToOstream(&state_file)) {
239 return Error() << "Failed to write state file " << state_file_path;
240 }
241
242 return {};
243 }
244
DeleteSession() const245 Result<void> ApexSession::DeleteSession() const {
246 std::string session_dir = GetSessionsDir() + "/" + std::to_string(GetId());
247 LOG(INFO) << "Deleting " << session_dir;
248 auto path = std::filesystem::path(session_dir);
249 std::error_code error_code;
250 std::filesystem::remove_all(path, error_code);
251 if (error_code) {
252 return Error() << "Failed to delete " << session_dir << " : "
253 << error_code.message();
254 }
255 return {};
256 }
257
operator <<(std::ostream & out,const ApexSession & session)258 std::ostream& operator<<(std::ostream& out, const ApexSession& session) {
259 return out << "[id = " << session.GetId()
260 << "; state = " << SessionState::State_Name(session.GetState())
261 << "]";
262 }
263
264 } // namespace apex
265 } // namespace android
266