1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <string>
20 #include <android-base/result.h>
21 #include <aconfigd.pb.h>
22 
23 #include "storage_files_manager.h"
24 
25 namespace android {
26   namespace aconfigd {
27 
28     /// Aconfigd socket name
29     static constexpr char kAconfigdSocket[] = "aconfigd";
30 
31     /// Aconfigd root dir
32     static constexpr char kAconfigdRootDir[] = "/metadata/aconfig";
33 
34     /// Persistent storage records pb file full path
35     static constexpr char kPersistentStorageRecordsFileName[] =
36         "/metadata/aconfig/storage_records.pb";
37 
38   class Aconfigd {
39     public:
40 
41     /// constructor
Aconfigd(const std::string & root_dir,const std::string & persist_storage_records)42     Aconfigd(const std::string& root_dir,
43              const std::string& persist_storage_records)
44         : root_dir_(root_dir)
45         , persist_storage_records_(persist_storage_records)
46         , storage_files_manager_(nullptr) {
47       storage_files_manager_.reset(new StorageFilesManager(root_dir_));
48     }
49 
50     /// destructor
51     ~Aconfigd() = default;
52 
53     /// no copy
54     Aconfigd(const Aconfigd&) = delete;
55     Aconfigd& operator=(const Aconfigd&) = delete;
56 
57     /// move constructor and assignment
Aconfigd(Aconfigd && rhs)58     Aconfigd(Aconfigd&& rhs)
59         : root_dir_(rhs.root_dir_)
60         , persist_storage_records_(rhs.persist_storage_records_)
61         , storage_files_manager_(std::move(rhs.storage_files_manager_))
62     {}
63     Aconfigd& operator=(Aconfigd&& rhs) = delete;
64 
65     public:
66 
67     /// Initialize in memory aconfig storage records
68     base::Result<void> InitializeInMemoryStorageRecords();
69 
70     /// Initialize platform RO partition flag storage
71     base::Result<void> InitializePlatformStorage();
72 
73     /// Initialize mainline flag storage
74     base::Result<void> InitializeMainlineStorage();
75 
76     /// Handle incoming messages to aconfigd socket
77     base::Result<void> HandleSocketRequest(const StorageRequestMessage& message,
78                                      StorageReturnMessage& return_message);
79 
80     private:
81 
82     /// Handle a flag override request
83     base::Result<void> HandleFlagOverride(
84         const StorageRequestMessage::FlagOverrideMessage& msg,
85         StorageReturnMessage& return_msg);
86 
87     /// Handle OTA flag staging request
88     base::Result<void> HandleOTAStaging(
89         const StorageRequestMessage::OTAFlagStagingMessage& msg,
90         StorageReturnMessage& return_msg);
91 
92     /// Handle new storage request
93     base::Result<void> HandleNewStorage(
94         const StorageRequestMessage::NewStorageMessage& msg,
95         StorageReturnMessage& return_msg);
96 
97     /// Handle a flag query request
98     base::Result<void> HandleFlagQuery(
99         const StorageRequestMessage::FlagQueryMessage& msg,
100         StorageReturnMessage& return_msg);
101 
102     /// Handle override removal request
103     base::Result<void> HandleLocalOverrideRemoval(
104         const StorageRequestMessage::RemoveLocalOverrideMessage& msg,
105         StorageReturnMessage& return_msg);
106 
107     /// Handle storage reset
108     base::Result<void> HandleStorageReset(StorageReturnMessage& return_msg);
109 
110     /// Handle list storage
111     base::Result<void> HandleListStorage(
112         const StorageRequestMessage::ListStorageMessage& msg,
113         StorageReturnMessage& return_message);
114 
115     /// Read OTA flag overrides to be applied for current build
116     base::Result<std::vector<FlagOverride>> ReadOTAFlagOverridesToApply();
117 
118     /// Write remaining OTA flag overrides back to pb file
119     base::Result<void> WriteRemainingOTAOverrides(
120         const std::vector<FlagOverride>& ota_flags);
121 
122     private:
123 
124     /// root storage dir
125     const std::string root_dir_;
126 
127     /// persist storage records pb file
128     const std::string persist_storage_records_;
129 
130     /// storage files manager
131     std::unique_ptr<StorageFilesManager> storage_files_manager_;
132   };
133 
134   } // namespace aconfigd
135 } // namespace android
136