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

Commit 8e0af5f1 authored by Josh Gao's avatar Josh Gao
Browse files

adb: switch to base::{Send,Receive}FileDescriptors.

Test: test_device.py
Test: adb abb package list packages
Change-Id: I080dc8620d77b0e6144c895fd2a0cbf7b8063c53
parent 5f87bbdb
Loading
Loading
Loading
Loading
+0 −76
Original line number Diff line number Diff line
@@ -187,79 +187,3 @@ bool ReadOrderlyShutdown(int fd) {
        return false;
    }
}

#if defined(__linux__)
bool SendFileDescriptor(int socket_fd, int fd) {
    struct msghdr msg;
    struct iovec iov;
    char dummy = '!';
    union {
        cmsghdr cm;
        char buffer[CMSG_SPACE(sizeof(int))];
    } cm_un;

    iov.iov_base = &dummy;
    iov.iov_len = 1;
    msg.msg_name = nullptr;
    msg.msg_namelen = 0;
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_flags = 0;
    msg.msg_control = cm_un.buffer;
    msg.msg_controllen = sizeof(cm_un.buffer);

    cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    ((int*)CMSG_DATA(cmsg))[0] = fd;

    int ret = TEMP_FAILURE_RETRY(sendmsg(socket_fd, &msg, 0));
    if (ret < 0) {
        D("sending file descriptor via socket %d failed: %s", socket_fd, strerror(errno));
        return false;
    }

    D("sent file descriptor %d to via socket %d", fd, socket_fd);
    return true;
}

bool ReceiveFileDescriptor(int socket_fd, unique_fd* fd, std::string* error) {
    char dummy = '!';
    union {
        cmsghdr cm;
        char buffer[CMSG_SPACE(sizeof(int))];
    } cm_un;

    iovec iov;
    iov.iov_base = &dummy;
    iov.iov_len = 1;

    msghdr msg;
    msg.msg_name = nullptr;
    msg.msg_namelen = 0;
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_flags = 0;
    msg.msg_control = cm_un.buffer;
    msg.msg_controllen = sizeof(cm_un.buffer);

    cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    ((int*)(CMSG_DATA(cmsg)))[0] = -1;

    int rc = TEMP_FAILURE_RETRY(recvmsg(socket_fd, &msg, 0));
    if (rc <= 0) {
        *error = perror_str("receiving file descriptor via socket failed");
        D("receiving file descriptor via socket %d failed: %s", socket_fd, strerror(errno));
        return false;
    }

    fd->reset(((int*)(CMSG_DATA(cmsg)))[0]);
    D("received file descriptor %d to via socket %d", fd->get(), socket_fd);

    return true;
}
#endif
+0 −9
Original line number Diff line number Diff line
@@ -74,13 +74,4 @@ bool WriteFdExactly(int 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)));

#if !ADB_HOST
// Sends an FD via Unix domain socket.
bool SendFileDescriptor(int socket_fd, int fd);

// Receives an FD via Unix domain socket.
bool ReceiveFileDescriptor(int socket_fd, unique_fd* fd, std::string* error);
#endif

#endif /* ADB_IO_H */
+4 −2
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@

#include <sys/wait.h>

#include <android-base/cmsg.h>

namespace {

class AdbFdTextOutput : public android::TextOutput {
@@ -83,8 +85,8 @@ int main(int argc, char* const argv[]) {
            break;
        }

        auto result = StartCommandInProcess(std::move(data), &execCmd);
        if (!SendFileDescriptor(fd, result)) {
        unique_fd result = StartCommandInProcess(std::move(data), &execCmd);
        if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) {
            PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data;
            break;
        }
+5 −2
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@
#include "adb_utils.h"
#include "shell_service.h"

#include <android-base/cmsg.h>

namespace {

struct AbbProcess;
@@ -59,8 +61,9 @@ unique_fd AbbProcess::sendCommand(std::string_view command) {

        unique_fd fd;
        std::string error;
        if (!ReceiveFileDescriptor(socket_fd_, &fd, &error)) {
            LOG(ERROR) << "failed to receive FD from abb: " << error;
        char buf;
        if (android::base::ReceiveFileDescriptors(socket_fd_, &buf, 1, &fd) != 1) {
            PLOG(ERROR) << "failed to receive FD from abb";
            socket_fd_.reset();
            continue;
        }
+3 −1
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@
#include <memory>
#include <vector>

#include <android-base/cmsg.h>

#include "adb.h"
#include "adb_io.h"
#include "adb_unique_fd.h"
@@ -237,7 +239,7 @@ static void jdwp_process_event(int socket, unsigned events, void* _proc) {
        CHECK(!proc->out_fds.empty());

        int fd = proc->out_fds.back().get();
        if (!SendFileDescriptor(socket, fd)) {
        if (android::base::SendFileDescriptors(socket, "", 1, fd) != 1) {
            D("sending new file descriptor to JDWP %d failed: %s", proc->pid, strerror(errno));
            goto CloseProcess;
        }