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 // apexd uses the /metadata partition (kApexSessionsDir) as 32 // location for sessions-related information. 33 static constexpr const char* kApexSessionsDir = "/metadata/apex/sessions"; 34 35 // Returns top-level directory to store sessions metadata in. 36 std::string GetSessionsDir(); 37 38 // TODO(b/288309411): remove static functions in this class. 39 class ApexSession { 40 public: 41 ApexSession() = delete; 42 43 const google::protobuf::RepeatedField<int> GetChildSessionIds() const; 44 ::apex::proto::SessionState::State GetState() const; 45 int GetId() const; 46 const std::string& GetBuildFingerprint() const; 47 const std::string& GetCrashingNativeProcess() const; 48 const std::string& GetErrorMessage() const; 49 bool IsFinalized() const; 50 bool HasRollbackEnabled() const; 51 bool IsRollback() const; 52 int GetRollbackId() const; 53 const google::protobuf::RepeatedPtrField<std::string> GetApexNames() const; 54 const google::protobuf::RepeatedPtrField<std::string> GetApexFileHashes() 55 const; 56 const google::protobuf::RepeatedPtrField<std::string> GetApexImages() const; 57 const std::string& GetSessionDir() const; 58 59 void SetChildSessionIds(const std::vector<int>& child_session_ids); 60 void SetBuildFingerprint(const std::string& fingerprint); 61 void SetHasRollbackEnabled(const bool enabled); 62 void SetIsRollback(const bool is_rollback); 63 void SetRollbackId(const int rollback_id); 64 void SetCrashingNativeProcess(const std::string& crashing_process); 65 void SetErrorMessage(const std::string& error_message); 66 void AddApexName(const std::string& apex_name); 67 void SetApexFileHashes(const std::vector<std::string>& hashes); 68 void SetApexImages(const std::vector<std::string>& images); 69 70 android::base::Result<void> UpdateStateAndCommit( 71 const ::apex::proto::SessionState::State& state); 72 73 android::base::Result<void> DeleteSession() const; 74 75 // Returns the directories containing the apexes staged for this session. 76 std::vector<std::string> GetStagedApexDirs( 77 const std::string& staged_session_dir) const; 78 79 friend class ApexSessionManager; 80 81 private: 82 ApexSession(::apex::proto::SessionState state, std::string session_dir); 83 ::apex::proto::SessionState state_; 84 std::string session_dir_; 85 }; 86 87 class ApexSessionManager { 88 public: 89 static std::unique_ptr<ApexSessionManager> Create( 90 std::string sessions_base_dir); 91 92 android::base::Result<ApexSession> CreateSession(int session_id); 93 android::base::Result<ApexSession> GetSession(int session_id) const; 94 std::vector<ApexSession> GetSessions() const; 95 std::vector<ApexSession> GetSessionsInState( 96 const ::apex::proto::SessionState::State& state) const; 97 98 bool HasActiveSession(); 99 void DeleteFinalizedSessions(); 100 101 private: 102 explicit ApexSessionManager(std::string sessions_base_dir); 103 ApexSessionManager(const ApexSessionManager&) = delete; 104 ApexSessionManager& operator=(const ApexSessionManager&) = delete; 105 ApexSessionManager(ApexSessionManager&&) = delete; 106 ApexSessionManager& operator=(ApexSessionManager&&) = delete; 107 108 std::string sessions_base_dir_; 109 }; 110 111 std::ostream& operator<<(std::ostream& out, const ApexSession& session); 112 113 } // namespace apex 114 } // namespace android 115 116 #endif // ANDROID_APEXD_APEXD_SESSION_H 117