1 // Copyright (C) 2020 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <poll.h> 18 #include <sys/eventfd.h> 19 20 #include <cstdio> 21 #include <cstring> 22 #include <functional> 23 #include <future> 24 #include <iostream> 25 #include <mutex> 26 #include <optional> 27 #include <queue> 28 #include <sstream> 29 #include <string> 30 #include <thread> 31 #include <vector> 32 33 #include <android-base/unique_fd.h> 34 #include <snapuserd/block_server.h> 35 #include "handler_manager.h" 36 #include "snapuserd_core.h" 37 38 namespace android { 39 namespace snapshot { 40 41 static constexpr uint32_t kMaxPacketSize = 512; 42 43 static constexpr char kBootSnapshotsWithoutSlotSwitch[] = 44 "/metadata/ota/snapshot-boot-without-slot-switch"; 45 46 class UserSnapshotServer { 47 private: 48 android::base::unique_fd sockfd_; 49 bool terminating_; 50 volatile bool received_socket_signal_ = false; 51 std::vector<struct pollfd> watched_fds_; 52 bool is_socket_present_ = false; 53 bool is_server_running_ = false; 54 bool io_uring_enabled_ = false; 55 std::unique_ptr<ISnapshotHandlerManager> handlers_; 56 std::unique_ptr<IBlockServerFactory> block_server_factory_; 57 58 std::mutex lock_; 59 60 void AddWatchedFd(android::base::borrowed_fd fd, int events); 61 void AcceptClient(); 62 bool HandleClient(android::base::borrowed_fd fd, int revents); 63 bool Recv(android::base::borrowed_fd fd, std::string* data); 64 bool Sendmsg(android::base::borrowed_fd fd, const std::string& msg); 65 bool Receivemsg(android::base::borrowed_fd fd, const std::string& str); 66 67 void ShutdownThreads(); 68 std::string GetDaemonStatus(); 69 void Parsemsg(std::string const& msg, const char delim, std::vector<std::string>& out); 70 IsTerminating()71 bool IsTerminating() { return terminating_; } 72 73 void JoinAllThreads(); 74 bool StartWithSocket(bool start_listening); 75 76 public: 77 UserSnapshotServer(); 78 ~UserSnapshotServer(); 79 80 bool Start(const std::string& socketname); 81 bool Run(); 82 void Interrupt(); 83 bool RunForSocketHandoff(); 84 bool WaitForSocket(); 85 86 std::shared_ptr<HandlerThread> AddHandler(const std::string& misc_name, 87 const std::string& cow_device_path, 88 const std::string& backing_device, 89 const std::string& base_path_merge, 90 std::optional<uint32_t> num_worker_threads, 91 bool o_direct = false, 92 uint32_t cow_op_merge_size = 0); 93 bool StartHandler(const std::string& misc_name); 94 SetTerminating()95 void SetTerminating() { terminating_ = true; } ReceivedSocketSignal()96 void ReceivedSocketSignal() { received_socket_signal_ = true; } SetServerRunning()97 void SetServerRunning() { is_server_running_ = true; } IsServerRunning()98 bool IsServerRunning() { return is_server_running_; } SetIouringEnabled()99 void SetIouringEnabled() { io_uring_enabled_ = true; } IsIouringEnabled()100 bool IsIouringEnabled() { return io_uring_enabled_; } 101 }; 102 103 } // namespace snapshot 104 } // namespace android 105