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 <unistd.h> 18 19 #include <chrono> 20 #include <cstring> 21 #include <iostream> 22 #include <string> 23 #include <thread> 24 #include <vector> 25 26 #include <android-base/unique_fd.h> 27 28 namespace android { 29 namespace snapshot { 30 31 static constexpr uint32_t PACKET_SIZE = 512; 32 33 static constexpr char kSnapuserdSocket[] = "snapuserd"; 34 static constexpr char kSnapuserdSocketProxy[] = "snapuserd_proxy"; 35 36 // Ensure that the second-stage daemon for snapuserd is running. 37 bool EnsureSnapuserdStarted(); 38 39 class SnapuserdClient { 40 private: 41 android::base::unique_fd sockfd_; 42 43 bool Sendmsg(const std::string& msg); 44 std::string Receivemsg(); 45 46 bool ValidateConnection(); 47 48 public: 49 explicit SnapuserdClient(android::base::unique_fd&& sockfd); 50 51 static std::unique_ptr<SnapuserdClient> Connect(const std::string& socket_name, 52 std::chrono::milliseconds timeout_ms); 53 54 bool StopSnapuserd(); 55 56 // Initializing a snapuserd handler is a three-step process: 57 // 58 // 1. Client invokes InitDmUserCow. This creates the snapuserd handler and validates the 59 // COW. The number of sectors required for the dm-user target is returned. 60 // 2. Client creates the device-mapper device with the dm-user target. 61 // 3. Client calls AttachControlDevice. 62 // 63 // The misc_name must be the "misc_name" given to dm-user in step 2. 64 // 65 uint64_t InitDmUserCow(const std::string& misc_name, const std::string& cow_device, 66 const std::string& backing_device, 67 const std::string& base_path_merge = ""); 68 bool AttachDmUser(const std::string& misc_name); 69 70 // Wait for snapuserd to disassociate with a dm-user control device. This 71 // must ONLY be called if the control device has already been deleted. 72 bool WaitForDeviceDelete(const std::string& control_device); 73 CloseConnection()74 void CloseConnection() { sockfd_ = {}; } 75 76 // Detach snapuserd. This shuts down the listener socket, and will cause 77 // snapuserd to gracefully exit once all handler threads have terminated. 78 // This should only be used on first-stage instances of snapuserd. 79 bool DetachSnapuserd(); 80 81 // Returns true if the snapuserd instance supports bridging a socket to second-stage init. 82 bool SupportsSecondStageSocketHandoff(); 83 84 // Returns true if the merge is started(or resumed from crash). 85 bool InitiateMerge(const std::string& misc_name); 86 87 // Returns Merge completion percentage 88 double GetMergePercent(); 89 90 // Return the status of the snapshot 91 std::string QuerySnapshotStatus(const std::string& misc_name); 92 93 // Check the update verification status - invoked by update_verifier during 94 // boot 95 bool QueryUpdateVerification(); 96 }; 97 98 } // namespace snapshot 99 } // namespace android 100