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

Commit 6dfef255 authored by Yabin Cui's avatar Yabin Cui
Browse files

adb: keep file flags in fdevent_install.

Bug: 24615098
Change-Id: Ia791ecbe612f09aca3bbd5787513f121fae54da5
parent d1f8e4dc
Loading
Loading
Loading
Loading
+16 −0
Original line number Original line Diff line number Diff line
@@ -229,3 +229,19 @@ bool parse_host_and_port(const std::string& address,
std::string perror_str(const char* msg) {
std::string perror_str(const char* msg) {
    return android::base::StringPrintf("%s: %s", msg, strerror(errno));
    return android::base::StringPrintf("%s: %s", msg, strerror(errno));
}
}

#if !defined(_WIN32)
bool set_file_block_mode(int fd, bool block) {
    int flags = fcntl(fd, F_GETFL, 0);
    if (flags == -1) {
        PLOG(ERROR) << "failed to fcntl(F_GETFL) for fd " << fd;
        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;
        return false;
    }
    return true;
}
#endif
+2 −0
Original line number Original line Diff line number Diff line
@@ -46,4 +46,6 @@ bool parse_host_and_port(const std::string& address,


std::string perror_str(const char* msg);
std::string perror_str(const char* msg);


bool set_file_block_mode(int fd, bool block);

#endif
#endif
+16 −0
Original line number Original line Diff line number Diff line
@@ -202,3 +202,19 @@ TEST(adb_utils, mkdirs) {
  ASSERT_EQ(0, chdir(td.path)) << strerror(errno);
  ASSERT_EQ(0, chdir(td.path)) << strerror(errno);
  test_mkdirs(std::string("relative/subrel/file"));
  test_mkdirs(std::string("relative/subrel/file"));
}
}

#if !defined(_WIN32)
TEST(adb_utils, set_file_block_mode) {
  int fd = adb_open("/dev/null", O_RDWR | O_APPEND);
  ASSERT_GE(fd, 0);
  int flags = fcntl(fd, F_GETFL, 0);
  ASSERT_EQ(O_RDWR | O_APPEND, (flags & (O_RDWR | O_APPEND)));
  ASSERT_TRUE(set_file_block_mode(fd, false));
  int new_flags = fcntl(fd, F_GETFL, 0);
  ASSERT_EQ(flags | O_NONBLOCK, new_flags);
  ASSERT_TRUE(set_file_block_mode(fd, true));
  new_flags = fcntl(fd, F_GETFL, 0);
  ASSERT_EQ(flags, new_flags);
  ASSERT_EQ(0, adb_close(fd));
}
#endif
+3 −2
Original line number Original line Diff line number Diff line
@@ -36,6 +36,7 @@


#include "adb_io.h"
#include "adb_io.h"
#include "adb_trace.h"
#include "adb_trace.h"
#include "adb_utils.h"


#if !ADB_HOST
#if !ADB_HOST
// This socket is used when a subproc shell service exists.
// This socket is used when a subproc shell service exists.
@@ -124,11 +125,11 @@ void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
    fde->fd = fd;
    fde->fd = fd;
    fde->func = func;
    fde->func = func;
    fde->arg = arg;
    fde->arg = arg;
    if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
    if (!set_file_block_mode(fd, false)) {
        // Here is not proper to handle the error. If it fails here, some error is
        // Here is not proper to handle the error. If it fails here, some error is
        // likely to be detected by poll(), then we can let the callback function
        // likely to be detected by poll(), then we can let the callback function
        // to handle it.
        // to handle it.
        LOG(ERROR) << "failed to fcntl(" << fd << ") to be nonblock";
        LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
    }
    }
    auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
    auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
    CHECK(pair.second) << "install existing fd " << fd;
    CHECK(pair.second) << "install existing fd " << fd;
+3 −3
Original line number Original line Diff line number Diff line
@@ -18,14 +18,14 @@


#include <gtest/gtest.h>
#include <gtest/gtest.h>


#include <pthread.h>
#include <signal.h>

#include <limits>
#include <limits>
#include <queue>
#include <queue>
#include <string>
#include <string>
#include <vector>
#include <vector>


#include <pthread.h>
#include <signal.h>

#include "adb_io.h"
#include "adb_io.h"


class FdHandler {
class FdHandler {
Loading