Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 76537239 authored by Josh Gao's avatar Josh Gao Committed by android-build-merger
Browse files

Merge "Add a way to turn off unique_fd's operator int."

am: 971bae0b

Change-Id: I8940d68e1a60ee800df6512da8c269edec7018fe
parents 4a749cc3 971bae0b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ cc_defaults {
        "-Wvla",
        "-DADB_HOST=1",         // overridden by adbd_defaults
        "-DALLOW_ADBD_ROOT=0",  // overridden by adbd_defaults
        "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION=1",
    ],
    cpp_std: "experimental",

+20 −20
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@
#include "adb_utils.h"
#include "sysdeps.h"

bool SendProtocolString(int fd, std::string_view s) {
bool SendProtocolString(borrowed_fd fd, std::string_view s) {
    unsigned int length = s.size();
    if (length > MAX_PAYLOAD - 4) {
        errno = EMSGSIZE;
@@ -47,7 +47,7 @@ bool SendProtocolString(int fd, std::string_view s) {
    return WriteFdExactly(fd, str);
}

bool ReadProtocolString(int fd, std::string* s, std::string* error) {
bool ReadProtocolString(borrowed_fd fd, std::string* s, std::string* error) {
    char buf[5];
    if (!ReadFdExactly(fd, buf, 4)) {
        *error = perror_str("protocol fault (couldn't read status length)");
@@ -65,57 +65,57 @@ bool ReadProtocolString(int fd, std::string* s, std::string* error) {
    return true;
}

bool SendOkay(int fd) {
bool SendOkay(borrowed_fd fd) {
    return WriteFdExactly(fd, "OKAY", 4);
}

bool SendFail(int fd, std::string_view reason) {
bool SendFail(borrowed_fd fd, std::string_view reason) {
    return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason);
}

bool ReadFdExactly(int fd, void* buf, size_t len) {
bool ReadFdExactly(borrowed_fd fd, void* buf, size_t len) {
    char* p = reinterpret_cast<char*>(buf);

    size_t len0 = len;

    D("readx: fd=%d wanted=%zu", fd, len);
    D("readx: fd=%d wanted=%zu", fd.get(), len);
    while (len > 0) {
        int r = adb_read(fd, p, len);
        if (r > 0) {
            len -= r;
            p += r;
        } else if (r == -1) {
            D("readx: fd=%d error %d: %s", fd, errno, strerror(errno));
            D("readx: fd=%d error %d: %s", fd.get(), errno, strerror(errno));
            return false;
        } else {
            D("readx: fd=%d disconnected", fd);
            D("readx: fd=%d disconnected", fd.get());
            errno = 0;
            return false;
        }
    }

    VLOG(RWX) << "readx: fd=" << fd << " wanted=" << len0 << " got=" << (len0 - len)
              << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
    VLOG(RWX) << "readx: fd=" << fd.get() << " wanted=" << len0 << " got=" << (len0 - len) << " "
              << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);

    return true;
}

bool WriteFdExactly(int fd, const void* buf, size_t len) {
bool WriteFdExactly(borrowed_fd fd, const void* buf, size_t len) {
    const char* p = reinterpret_cast<const char*>(buf);
    int r;

    VLOG(RWX) << "writex: fd=" << fd << " len=" << len
              << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
    VLOG(RWX) << "writex: fd=" << fd.get() << " len=" << len << " "
              << dump_hex(reinterpret_cast<const unsigned char*>(buf), len);

    while (len > 0) {
        r = adb_write(fd, p, len);
        if (r == -1) {
            D("writex: fd=%d error %d: %s", fd, errno, strerror(errno));
            D("writex: fd=%d error %d: %s", fd.get(), errno, strerror(errno));
            if (errno == EAGAIN) {
                std::this_thread::yield();
                continue;
            } else if (errno == EPIPE) {
                D("writex: fd=%d disconnected", fd);
                D("writex: fd=%d disconnected", fd.get());
                errno = 0;
                return false;
            } else {
@@ -129,15 +129,15 @@ bool WriteFdExactly(int fd, const void* buf, size_t len) {
    return true;
}

bool WriteFdExactly(int fd, const char* str) {
bool WriteFdExactly(borrowed_fd fd, const char* str) {
    return WriteFdExactly(fd, str, strlen(str));
}

bool WriteFdExactly(int fd, const std::string& str) {
bool WriteFdExactly(borrowed_fd fd, const std::string& str) {
    return WriteFdExactly(fd, str.c_str(), str.size());
}

bool WriteFdFmt(int fd, const char* fmt, ...) {
bool WriteFdFmt(borrowed_fd fd, const char* fmt, ...) {
    std::string str;

    va_list ap;
@@ -148,7 +148,7 @@ bool WriteFdFmt(int fd, const char* fmt, ...) {
    return WriteFdExactly(fd, str);
}

bool ReadOrderlyShutdown(int fd) {
bool ReadOrderlyShutdown(borrowed_fd fd) {
    char buf[16];

    // Only call this function if you're sure that the peer does
@@ -178,7 +178,7 @@ bool ReadOrderlyShutdown(int fd) {
        // data. We don't repeatedly call adb_read() until we get zero because
        // we don't know how long that would take, but we do know that the
        // caller wants to close the socket soon.
        VLOG(RWX) << "ReadOrderlyShutdown(" << fd << ") unexpectedly read "
        VLOG(RWX) << "ReadOrderlyShutdown(" << fd.get() << ") unexpectedly read "
                  << dump_hex(buf, result);
        // Shutdown the socket to prevent the caller from reading or writing to
        // it which doesn't make sense if we just read and discarded some data.
+10 −10
Original line number Diff line number Diff line
@@ -25,16 +25,16 @@
#include "adb_unique_fd.h"

// Sends the protocol "OKAY" message.
bool SendOkay(int fd);
bool SendOkay(borrowed_fd fd);

// Sends the protocol "FAIL" message, with the given failure reason.
bool SendFail(int fd, std::string_view reason);
bool SendFail(borrowed_fd fd, std::string_view reason);

// Writes a protocol-format string; a four hex digit length followed by the string data.
bool SendProtocolString(int fd, std::string_view s);
bool SendProtocolString(borrowed_fd fd, std::string_view s);

// Reads a protocol-format string; a four hex digit length followed by the string data.
bool ReadProtocolString(int fd, std::string* s, std::string* error);
bool ReadProtocolString(borrowed_fd fd, std::string* s, std::string* error);

// Reads exactly len bytes from fd into buf.
//
@@ -42,7 +42,7 @@ bool ReadProtocolString(int fd, std::string* s, std::string* error);
// were read. If EOF was found, errno will be set to 0.
//
// If this function fails, the contents of buf are undefined.
bool ReadFdExactly(int fd, void* buf, size_t len);
bool ReadFdExactly(borrowed_fd fd, void* buf, size_t len);

// Given a client socket, wait for orderly/graceful shutdown. Call this:
//
@@ -60,19 +60,19 @@ bool ReadFdExactly(int fd, void* buf, size_t len);
// connect()s from the client to fail with WSAEADDRINUSE on Windows.
// Returns true if it is sure that orderly/graceful shutdown has occurred with
// no additional data read from the server.
bool ReadOrderlyShutdown(int fd);
bool ReadOrderlyShutdown(borrowed_fd fd);

// Writes exactly len bytes from buf to fd.
//
// Returns false if there is an error or if the fd was closed before the write
// completed. If the other end of the fd (such as in a socket, pipe, or fifo),
// is closed, errno will be set to 0.
bool WriteFdExactly(int fd, const void* buf, size_t len);
bool WriteFdExactly(borrowed_fd fd, const void* buf, size_t len);

// Same as above, but for strings.
bool WriteFdExactly(int fd, const char* s);
bool WriteFdExactly(int fd, const std::string& s);
bool WriteFdExactly(borrowed_fd fd, const char* s);
bool WriteFdExactly(borrowed_fd fd, const std::string& s);

// Same as above, but formats the string to send.
bool WriteFdFmt(int fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
bool WriteFdFmt(borrowed_fd fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
#endif /* ADB_IO_H */
+2 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ using unique_fd = android::base::unique_fd_impl<AdbCloser>;
using unique_fd = android::base::unique_fd;
#endif

using android::base::borrowed_fd;

template <typename T>
int adb_close(const android::base::unique_fd_impl<T>&)
        __attribute__((__unavailable__("adb_close called on unique_fd")));
+5 −5
Original line number Diff line number Diff line
@@ -234,15 +234,15 @@ std::string perror_str(const char* msg) {

#if !defined(_WIN32)
// Windows version provided in sysdeps_win32.cpp
bool set_file_block_mode(int fd, bool block) {
    int flags = fcntl(fd, F_GETFL, 0);
bool set_file_block_mode(borrowed_fd fd, bool block) {
    int flags = fcntl(fd.get(), F_GETFL, 0);
    if (flags == -1) {
        PLOG(ERROR) << "failed to fcntl(F_GETFL) for fd " << fd;
        PLOG(ERROR) << "failed to fcntl(F_GETFL) for fd " << fd.get();
        return false;
    }
    flags = block ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
    if (fcntl(fd, F_SETFL, flags) != 0) {
        PLOG(ERROR) << "failed to fcntl(F_SETFL) for fd " << fd << ", flags " << flags;
    if (fcntl(fd.get(), F_SETFL, flags) != 0) {
        PLOG(ERROR) << "failed to fcntl(F_SETFL) for fd " << fd.get() << ", flags " << flags;
        return false;
    }
    return true;
Loading