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 #ifndef ANDROID_APEXD_APEXD_SESSION_H_ 18 #define ANDROID_APEXD_APEXD_SESSION_H_ 19 20 #include <android-base/result.h> 21 22 #include "apex_constants.h" 23 24 #include "session_state.pb.h" 25 26 #include <optional> 27 28 namespace android { 29 namespace apex { 30 31 // Starting from R, apexd prefers /metadata partition (kNewApexSessionsDir) as 32 // location for sessions-related information. For devices that don't have 33 // /metadata partition, apexd will fallback to the /data one 34 // (kOldApexSessionsDir). 35 static constexpr const char* kOldApexSessionsDir = "/data/apex/sessions"; 36 static constexpr const char* kNewApexSessionsDir = "/metadata/apex/sessions"; 37 38 // Returns top-level directory to store sessions metadata in. 39 // If device has /metadata partition, this will return 40 // /metadata/apex/sessions, on all other devices it will return 41 // /data/apex/sessions. 42 std::string GetSessionsDir(); 43 44 // TODO(b/288309411): remove static functions in this class. 45 class ApexSession { 46 public: 47 ApexSession() = delete; 48 49 const google::protobuf::RepeatedField<int> GetChildSessionIds() const; 50 ::apex::proto::SessionState::State GetState() const; 51 int GetId() const; 52 const std::string& GetBuildFingerprint() const; 53 const std::string& GetCrashingNativeProcess() const; 54 const std::string& GetErrorMessage() const; 55 bool IsFinalized() const; 56 bool HasRollbackEnabled() const; 57 bool IsRollback() const; 58 int GetRollbackId() const; 59 const google::protobuf::RepeatedPtrField<std::string> GetApexNames() const; 60 const google::protobuf::RepeatedPtrField<std::string> GetApexFileHashes() 61 const; 62 const std::string& GetSessionDir() const; 63 64 void SetChildSessionIds(const std::vector<int>& child_session_ids); 65 void SetBuildFingerprint(const std::string& fingerprint); 66 void SetHasRollbackEnabled(const bool enabled); 67 void SetIsRollback(const bool is_rollback); 68 void SetRollbackId(const int rollback_id); 69 void SetCrashingNativeProcess(const std::string& crashing_process); 70 void SetErrorMessage(const std::string& error_message); 71 void AddApexName(const std::string& apex_name); 72 void SetApexFileHashes(const std::vector<std::string>& hashes); 73 74 android::base::Result<void> UpdateStateAndCommit( 75 const ::apex::proto::SessionState::State& state); 76 77 android::base::Result<void> DeleteSession() const; 78 79 // Returns the directories containing the apexes staged for this session. 80 std::vector<std::string> GetStagedApexDirs( 81 const std::string& staged_session_dir) const; 82 83 friend class ApexSessionManager; 84 85 private: 86 ApexSession(::apex::proto::SessionState state, std::string session_dir); 87 ::apex::proto::SessionState state_; 88 std::string session_dir_; 89 }; 90 91 class ApexSessionManager { 92 public: 93 ApexSessionManager(ApexSessionManager&&) noexcept; 94 ApexSessionManager& operator=(ApexSessionManager&&) noexcept; 95 96 static std::unique_ptr<ApexSessionManager> Create( 97 std::string sessions_base_dir); 98 99 android::base::Result<ApexSession> CreateSession(int session_id); 100 android::base::Result<ApexSession> GetSession(int session_id) const; 101 std::vector<ApexSession> GetSessions() const; 102 std::vector<ApexSession> GetSessionsInState( 103 const ::apex::proto::SessionState::State& state) const; 104 105 android::base::Result<void> MigrateFromOldSessionsDir( 106 const std::string& old_sessions_base_dir); 107 108 bool HasActiveSession(); 109 void DeleteFinalizedSessions(); 110 111 private: 112 explicit ApexSessionManager(std::string sessions_base_dir); 113 ApexSessionManager(const ApexSessionManager&) = delete; 114 ApexSessionManager& operator=(const ApexSessionManager&) = delete; 115 116 std::string sessions_base_dir_; 117 }; 118 119 std::ostream& operator<<(std::ostream& out, const ApexSession& session); 120 121 } // namespace apex 122 } // namespace android 123 124 #endif // ANDROID_APEXD_APEXD_SESSION_H 125