Merge "fs_mgr: overlay drop redundant check"
diff --git a/adb/Android.bp b/adb/Android.bp
index a9ccc84..53f4404 100644
--- a/adb/Android.bp
+++ b/adb/Android.bp
@@ -24,7 +24,6 @@
         "-Wno-missing-field-initializers",
         "-Wvla",
     ],
-    cpp_std: "gnu++17",
     rtti: true,
 
     use_version_lib: true,
diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp
index 22f8e8d..07b5747 100644
--- a/adb/client/commandline.cpp
+++ b/adb/client/commandline.cpp
@@ -1579,15 +1579,20 @@
         }
         return adb_connect_command(android::base::StringPrintf("tcpip:%d", port));
     }
+    // clang-format off
     else if (!strcmp(argv[0], "remount") ||
              !strcmp(argv[0], "reboot") ||
              !strcmp(argv[0], "reboot-bootloader") ||
+             !strcmp(argv[0], "reboot-fastboot") ||
              !strcmp(argv[0], "usb") ||
              !strcmp(argv[0], "disable-verity") ||
              !strcmp(argv[0], "enable-verity")) {
+        // clang-format on
         std::string command;
         if (!strcmp(argv[0], "reboot-bootloader")) {
             command = "reboot:bootloader";
+        } else if (!strcmp(argv[0], "reboot-fastboot")) {
+            command = "reboot:fastboot";
         } else if (argc > 1) {
             command = android::base::StringPrintf("%s:%s", argv[0], argv[1]);
         } else {
diff --git a/adb/sockets.cpp b/adb/sockets.cpp
index dfd9a0a..1534792 100644
--- a/adb/sockets.cpp
+++ b/adb/sockets.cpp
@@ -26,14 +26,10 @@
 #include <unistd.h>
 
 #include <algorithm>
-#include <map>
 #include <mutex>
 #include <string>
-#include <thread>
 #include <vector>
 
-#include <android-base/thread_annotations.h>
-
 #if !ADB_HOST
 #include <android-base/properties.h>
 #include <log/log_properties.h>
@@ -41,150 +37,9 @@
 
 #include "adb.h"
 #include "adb_io.h"
-#include "adb_utils.h"
-#include "sysdeps/chrono.h"
 #include "transport.h"
 #include "types.h"
 
-// The standard (RFC 1122 - 4.2.2.13) says that if we call close on a
-// socket while we have pending data, a TCP RST should be sent to the
-// other end to notify it that we didn't read all of its data. However,
-// this can result in data that we've successfully written out to be dropped
-// on the other end. To avoid this, instead of immediately closing a
-// socket, call shutdown on it instead, and then read from the file
-// descriptor until we hit EOF or an error before closing.
-struct LingeringSocketCloser {
-    LingeringSocketCloser() = default;
-    ~LingeringSocketCloser() = delete;
-
-    // Defer thread creation until it's needed, because we need for there to
-    // only be one thread when dropping privileges in adbd.
-    void Start() {
-        CHECK(!thread_.joinable());
-
-        int fds[2];
-        if (adb_socketpair(fds) != 0) {
-            PLOG(FATAL) << "adb_socketpair failed";
-        }
-
-        set_file_block_mode(fds[0], false);
-        set_file_block_mode(fds[1], false);
-
-        notify_fd_read_.reset(fds[0]);
-        notify_fd_write_.reset(fds[1]);
-
-        thread_ = std::thread([this]() { Run(); });
-    }
-
-    void EnqueueSocket(unique_fd socket) {
-        // Shutdown the socket in the outgoing direction only, so that
-        // we don't have the same problem on the opposite end.
-        adb_shutdown(socket.get(), SHUT_WR);
-        set_file_block_mode(socket.get(), false);
-
-        std::lock_guard<std::mutex> lock(mutex_);
-        int fd = socket.get();
-        SocketInfo info = {
-                .fd = std::move(socket),
-                .deadline = std::chrono::steady_clock::now() + 1s,
-        };
-
-        D("LingeringSocketCloser received fd %d", fd);
-
-        fds_.emplace(fd, std::move(info));
-        if (adb_write(notify_fd_write_, "", 1) == -1 && errno != EAGAIN) {
-            PLOG(FATAL) << "failed to write to LingeringSocketCloser notify fd";
-        }
-    }
-
-  private:
-    std::vector<adb_pollfd> GeneratePollFds() {
-        std::lock_guard<std::mutex> lock(mutex_);
-        std::vector<adb_pollfd> result;
-        result.push_back(adb_pollfd{.fd = notify_fd_read_, .events = POLLIN});
-        for (auto& [fd, _] : fds_) {
-            result.push_back(adb_pollfd{.fd = fd, .events = POLLIN});
-        }
-        return result;
-    }
-
-    void Run() {
-        while (true) {
-            std::vector<adb_pollfd> pfds = GeneratePollFds();
-            int rc = adb_poll(pfds.data(), pfds.size(), 1000);
-            if (rc == -1) {
-                PLOG(FATAL) << "poll failed in LingeringSocketCloser";
-            }
-
-            std::lock_guard<std::mutex> lock(mutex_);
-            if (rc == 0) {
-                // Check deadlines.
-                auto now = std::chrono::steady_clock::now();
-                for (auto it = fds_.begin(); it != fds_.end();) {
-                    if (now > it->second.deadline) {
-                        D("LingeringSocketCloser closing fd %d due to deadline", it->first);
-                        it = fds_.erase(it);
-                    } else {
-                        D("deadline still not expired for fd %d", it->first);
-                        ++it;
-                    }
-                }
-                continue;
-            }
-
-            for (auto& pfd : pfds) {
-                if ((pfd.revents & POLLIN) == 0) {
-                    continue;
-                }
-
-                // Empty the fd.
-                ssize_t rc;
-                char buf[32768];
-                while ((rc = adb_read(pfd.fd, buf, sizeof(buf))) > 0) {
-                    continue;
-                }
-
-                if (pfd.fd == notify_fd_read_) {
-                    continue;
-                }
-
-                auto it = fds_.find(pfd.fd);
-                if (it == fds_.end()) {
-                    LOG(FATAL) << "fd is missing";
-                }
-
-                if (rc == -1 && errno == EAGAIN) {
-                    if (std::chrono::steady_clock::now() > it->second.deadline) {
-                        D("LingeringSocketCloser closing fd %d due to deadline", pfd.fd);
-                    } else {
-                        continue;
-                    }
-                } else if (rc == -1) {
-                    D("LingeringSocketCloser closing fd %d due to error %d", pfd.fd, errno);
-                } else {
-                    D("LingeringSocketCloser closing fd %d due to EOF", pfd.fd);
-                }
-
-                fds_.erase(it);
-            }
-        }
-    }
-
-    std::thread thread_;
-    unique_fd notify_fd_read_;
-    unique_fd notify_fd_write_;
-
-    struct SocketInfo {
-        unique_fd fd;
-        std::chrono::steady_clock::time_point deadline;
-    };
-
-    std::mutex mutex_;
-    std::map<int, SocketInfo> fds_ GUARDED_BY(mutex_);
-};
-
-static auto& socket_closer = *new LingeringSocketCloser();
-
 static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
 static unsigned local_socket_next_id = 1;
 
@@ -388,12 +243,10 @@
 
     D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
 
-    // Defer thread creation until it's needed, because we need for there to
-    // only be one thread when dropping privileges in adbd.
-    static std::once_flag once;
-    std::call_once(once, []() { socket_closer.Start(); });
-
-    socket_closer.EnqueueSocket(fdevent_release(s->fde));
+    /* IMPORTANT: the remove closes the fd
+    ** that belongs to this socket
+    */
+    fdevent_destroy(s->fde);
 
     remove_socket(s);
     delete s;
diff --git a/adb/test_device.py b/adb/test_device.py
index 4c45a73..c3166ff 100755
--- a/adb/test_device.py
+++ b/adb/test_device.py
@@ -35,8 +35,6 @@
 import time
 import unittest
 
-from datetime import datetime
-
 import adb
 
 def requires_root(func):
@@ -1337,63 +1335,6 @@
             self.device.forward_remove("tcp:{}".format(local_port))
 
 
-class SocketTest(DeviceTest):
-    def test_socket_flush(self):
-        """Test that we handle socket closure properly.
-
-        If we're done writing to a socket, closing before the other end has
-        closed will send a TCP_RST if we have incoming data queued up, which
-        may result in data that we've written being discarded.
-
-        Bug: http://b/74616284
-        """
-        s = socket.create_connection(("localhost", 5037))
-
-        def adb_length_prefixed(string):
-            encoded = string.encode("utf8")
-            result = b"%04x%s" % (len(encoded), encoded)
-            return result
-
-        if "ANDROID_SERIAL" in os.environ:
-            transport_string = "host:transport:" + os.environ["ANDROID_SERIAL"]
-        else:
-            transport_string = "host:transport-any"
-
-        s.sendall(adb_length_prefixed(transport_string))
-        response = s.recv(4)
-        self.assertEquals(b"OKAY", response)
-
-        shell_string = "shell:sleep 0.5; dd if=/dev/zero bs=1m count=1 status=none; echo foo"
-        s.sendall(adb_length_prefixed(shell_string))
-
-        response = s.recv(4)
-        self.assertEquals(b"OKAY", response)
-
-        # Spawn a thread that dumps garbage into the socket until failure.
-        def spam():
-            buf = b"\0" * 16384
-            try:
-                while True:
-                    s.sendall(buf)
-            except Exception as ex:
-                print(ex)
-
-        thread = threading.Thread(target=spam)
-        thread.start()
-
-        time.sleep(1)
-
-        received = b""
-        while True:
-            read = s.recv(512)
-            if len(read) == 0:
-                break
-            received += read
-
-        self.assertEquals(1024 * 1024 + len("foo\n"), len(received))
-        thread.join()
-
-
 if sys.platform == "win32":
     # From https://stackoverflow.com/a/38749458
     import os
diff --git a/fs_mgr/liblp/Android.bp b/fs_mgr/liblp/Android.bp
index 69dc065..bbdec5b 100644
--- a/fs_mgr/liblp/Android.bp
+++ b/fs_mgr/liblp/Android.bp
@@ -51,6 +51,7 @@
         "liblp",
         "libbase",
         "libfs_mgr",
+        "libsparse",
     ],
     srcs: [
         "builder_test.cpp",
diff --git a/fs_mgr/liblp/images.cpp b/fs_mgr/liblp/images.cpp
index 742b1d0..511f7be 100644
--- a/fs_mgr/liblp/images.cpp
+++ b/fs_mgr/liblp/images.cpp
@@ -19,8 +19,6 @@
 #include <limits.h>
 
 #include <android-base/file.h>
-#include <android-base/unique_fd.h>
-#include <sparse/sparse.h>
 
 #include "reader.h"
 #include "utility.h"
@@ -89,41 +87,36 @@
     return WriteToImageFile(fd, input);
 }
 
-// We use an object to build the sparse file since it requires that data
-// pointers be held alive until the sparse file is destroyed. It's easier
-// to do this when the data pointers are all in one place.
-class SparseBuilder {
-  public:
-    SparseBuilder(const LpMetadata& metadata, uint32_t block_size,
-                  const std::map<std::string, std::string>& images);
-
-    bool Build();
-    bool Export(const char* file);
-    bool IsValid() const { return file_ != nullptr; }
-
-  private:
-    bool AddData(const std::string& blob, uint64_t sector);
-    bool AddPartitionImage(const LpMetadataPartition& partition, const std::string& file);
-    int OpenImageFile(const std::string& file);
-    bool SectorToBlock(uint64_t sector, uint32_t* block);
-
-    const LpMetadata& metadata_;
-    const LpMetadataGeometry& geometry_;
-    uint32_t block_size_;
-    std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)> file_;
-    std::string primary_blob_;
-    std::string backup_blob_;
-    std::map<std::string, std::string> images_;
-    std::vector<android::base::unique_fd> temp_fds_;
-};
-
 SparseBuilder::SparseBuilder(const LpMetadata& metadata, uint32_t block_size,
                              const std::map<std::string, std::string>& images)
     : metadata_(metadata),
       geometry_(metadata.geometry),
       block_size_(block_size),
-      file_(sparse_file_new(block_size_, geometry_.block_device_size), sparse_file_destroy),
-      images_(images) {}
+      file_(nullptr, sparse_file_destroy),
+      images_(images) {
+    if (block_size % LP_SECTOR_SIZE != 0) {
+        LERROR << "Block size must be a multiple of the sector size, " << LP_SECTOR_SIZE;
+        return;
+    }
+    if (metadata.geometry.block_device_size % block_size != 0) {
+        LERROR << "Device size must be a multiple of the block size, " << block_size;
+        return;
+    }
+    if (metadata.geometry.metadata_max_size % block_size != 0) {
+        LERROR << "Metadata max size must be a multiple of the block size, " << block_size;
+        return;
+    }
+
+    uint64_t num_blocks = metadata.geometry.block_device_size % block_size;
+    if (num_blocks >= UINT_MAX) {
+        // libsparse counts blocks in unsigned 32-bit integers, so we check to
+        // make sure we're not going to overflow.
+        LERROR << "Block device is too large to encode with libsparse.";
+        return;
+    }
+
+    file_.reset(sparse_file_new(block_size_, geometry_.block_device_size));
+}
 
 bool SparseBuilder::Export(const char* file) {
     android::base::unique_fd fd(open(file, O_CREAT | O_RDWR | O_TRUNC, 0644));
@@ -206,8 +199,8 @@
     // The backup area contains all metadata slots, and then geometry. Similar
     // to before we write the metadata to every slot.
     int64_t backup_offset = GetBackupMetadataOffset(geometry_, 0);
-    uint64_t backups_start = geometry_.block_device_size + backup_offset;
-    uint64_t backup_sector = backups_start / LP_SECTOR_SIZE;
+    int64_t backups_start = static_cast<int64_t>(geometry_.block_device_size) + backup_offset;
+    int64_t backup_sector = backups_start / LP_SECTOR_SIZE;
 
     backup_blob_ = all_metadata + geometry_blob;
     if (!AddData(backup_blob_, backup_sector)) {
@@ -336,22 +329,6 @@
 
 bool WriteToSparseFile(const char* file, const LpMetadata& metadata, uint32_t block_size,
                        const std::map<std::string, std::string>& images) {
-    if (block_size % LP_SECTOR_SIZE != 0) {
-        LERROR << "Block size must be a multiple of the sector size, " << LP_SECTOR_SIZE;
-        return false;
-    }
-    if (metadata.geometry.block_device_size % block_size != 0) {
-        LERROR << "Device size must be a multiple of the block size, " << block_size;
-        return false;
-    }
-    uint64_t num_blocks = metadata.geometry.block_device_size % block_size;
-    if (num_blocks >= UINT_MAX) {
-        // libsparse counts blocks in unsigned 32-bit integers, so we check to
-        // make sure we're not going to overflow.
-        LERROR << "Block device is too large to encode with libsparse.";
-        return false;
-    }
-
     SparseBuilder builder(metadata, block_size, images);
     if (!builder.IsValid()) {
         LERROR << "Could not allocate sparse file of size " << metadata.geometry.block_device_size;
diff --git a/fs_mgr/liblp/images.h b/fs_mgr/liblp/images.h
index 3a999b8..2031e33 100644
--- a/fs_mgr/liblp/images.h
+++ b/fs_mgr/liblp/images.h
@@ -14,7 +14,14 @@
  * limitations under the License.
  */
 
+#include <stdint.h>
+#include <map>
+#include <memory>
+#include <string>
+
+#include <android-base/unique_fd.h>
 #include <liblp/liblp.h>
+#include <sparse/sparse.h>
 
 namespace android {
 namespace fs_mgr {
@@ -25,5 +32,35 @@
 bool WriteToImageFile(const char* file, const LpMetadata& metadata);
 bool WriteToImageFile(int fd, const LpMetadata& metadata);
 
+// We use an object to build the sparse file since it requires that data
+// pointers be held alive until the sparse file is destroyed. It's easier
+// to do this when the data pointers are all in one place.
+class SparseBuilder {
+  public:
+    SparseBuilder(const LpMetadata& metadata, uint32_t block_size,
+                  const std::map<std::string, std::string>& images);
+
+    bool Build();
+    bool Export(const char* file);
+    bool IsValid() const { return file_ != nullptr; }
+
+    sparse_file* file() const { return file_.get(); }
+
+  private:
+    bool AddData(const std::string& blob, uint64_t sector);
+    bool AddPartitionImage(const LpMetadataPartition& partition, const std::string& file);
+    int OpenImageFile(const std::string& file);
+    bool SectorToBlock(uint64_t sector, uint32_t* block);
+
+    const LpMetadata& metadata_;
+    const LpMetadataGeometry& geometry_;
+    uint32_t block_size_;
+    std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)> file_;
+    std::string primary_blob_;
+    std::string backup_blob_;
+    std::map<std::string, std::string> images_;
+    std::vector<android::base::unique_fd> temp_fds_;
+};
+
 }  // namespace fs_mgr
 }  // namespace android
diff --git a/fs_mgr/liblp/io_test.cpp b/fs_mgr/liblp/io_test.cpp
index 01de3ac..322219b 100644
--- a/fs_mgr/liblp/io_test.cpp
+++ b/fs_mgr/liblp/io_test.cpp
@@ -535,3 +535,36 @@
     ASSERT_GE(new_table->partitions.size(), 1);
     ASSERT_EQ(GetPartitionName(new_table->partitions[0]), GetPartitionName(imported->partitions[0]));
 }
+
+// Test that writing a sparse image can be read back.
+TEST(liblp, FlashSparseImage) {
+    unique_fd fd = CreateFakeDisk();
+    ASSERT_GE(fd, 0);
+
+    BlockDeviceInfo device_info(kDiskSize, 0, 0, 512);
+    unique_ptr<MetadataBuilder> builder =
+            MetadataBuilder::New(device_info, kMetadataSize, kMetadataSlots);
+    ASSERT_NE(builder, nullptr);
+    ASSERT_TRUE(AddDefaultPartitions(builder.get()));
+
+    unique_ptr<LpMetadata> exported = builder->Export();
+    ASSERT_NE(exported, nullptr);
+
+    // Build the sparse file.
+    SparseBuilder sparse(*exported.get(), 512, {});
+    ASSERT_TRUE(sparse.IsValid());
+    sparse_file_verbose(sparse.file());
+    ASSERT_TRUE(sparse.Build());
+
+    // Write it to the fake disk.
+    ASSERT_NE(lseek(fd.get(), 0, SEEK_SET), -1);
+    int ret = sparse_file_write(sparse.file(), fd.get(), false, false, false);
+    ASSERT_EQ(ret, 0);
+
+    // Verify that we can read both sets of metadata.
+    LpMetadataGeometry geometry;
+    ASSERT_TRUE(ReadPrimaryGeometry(fd.get(), &geometry));
+    ASSERT_TRUE(ReadBackupGeometry(fd.get(), &geometry));
+    ASSERT_NE(ReadPrimaryMetadata(fd.get(), geometry, 0), nullptr);
+    ASSERT_NE(ReadBackupMetadata(fd.get(), geometry, 0), nullptr);
+}
diff --git a/fs_mgr/liblp/reader.cpp b/fs_mgr/liblp/reader.cpp
index 005d493..87411cb 100644
--- a/fs_mgr/liblp/reader.cpp
+++ b/fs_mgr/liblp/reader.cpp
@@ -120,10 +120,7 @@
     return true;
 }
 
-// Read and validate geometry information from a block device that holds
-// logical partitions. If the information is corrupted, this will attempt
-// to read it from a secondary backup location.
-bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry) {
+bool ReadPrimaryGeometry(int fd, LpMetadataGeometry* geometry) {
     // Read the first 4096 bytes.
     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(LP_METADATA_GEOMETRY_SIZE);
     if (SeekFile64(fd, 0, SEEK_SET) < 0) {
@@ -134,11 +131,12 @@
         PERROR << __PRETTY_FUNCTION__ << "read " << LP_METADATA_GEOMETRY_SIZE << " bytes failed";
         return false;
     }
-    if (ParseGeometry(buffer.get(), geometry)) {
-        return true;
-    }
+    return ParseGeometry(buffer.get(), geometry);
+}
 
+bool ReadBackupGeometry(int fd, LpMetadataGeometry* geometry) {
     // Try the backup copy in the last 4096 bytes.
+    std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(LP_METADATA_GEOMETRY_SIZE);
     if (SeekFile64(fd, -LP_METADATA_GEOMETRY_SIZE, SEEK_END) < 0) {
         PERROR << __PRETTY_FUNCTION__ << "lseek failed, offset " << -LP_METADATA_GEOMETRY_SIZE;
         return false;
@@ -151,6 +149,16 @@
     return ParseGeometry(buffer.get(), geometry);
 }
 
+// Read and validate geometry information from a block device that holds
+// logical partitions. If the information is corrupted, this will attempt
+// to read it from a secondary backup location.
+bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry) {
+    if (ReadPrimaryGeometry(fd, geometry)) {
+        return true;
+    }
+    return ReadBackupGeometry(fd, geometry);
+}
+
 static bool ValidateTableBounds(const LpMetadataHeader& header,
                                 const LpMetadataTableDescriptor& table) {
     if (table.offset > header.tables_size) {
diff --git a/fs_mgr/liblp/reader.h b/fs_mgr/liblp/reader.h
index 9f6ca6e..24b2611 100644
--- a/fs_mgr/liblp/reader.h
+++ b/fs_mgr/liblp/reader.h
@@ -36,6 +36,8 @@
 std::unique_ptr<LpMetadata> ParseMetadata(const LpMetadataGeometry& geometry, const void* buffer,
                                           size_t size);
 bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry);
+bool ReadPrimaryGeometry(int fd, LpMetadataGeometry* geometry);
+bool ReadBackupGeometry(int fd, LpMetadataGeometry* geometry);
 
 // These functions assume a valid geometry and slot number.
 std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry& geometry,
diff --git a/init/stable_properties.h b/init/stable_properties.h
index 4972d10..baef833 100644
--- a/init/stable_properties.h
+++ b/init/stable_properties.h
@@ -31,6 +31,7 @@
 static const std::set<std::string> kExportedActionableProperties = {
     "dev.bootcomplete",
     "init.svc.console",
+    "init.svc.dumpstatez",
     "init.svc.mediadrm",
     "init.svc.surfaceflinger",
     "init.svc.zygote",