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

Commit 531f7334 authored by Bernie Innocenti's avatar Bernie Innocenti Committed by Mike Yu
Browse files

Simplify socket configuration in DNSResponder::startServer()

Since Linux 2.6.27, sockets can be direclty created in non-blocking
mode.

Bug: 130686826
Test: atest resolv_integration_test
Merged-In: I574e3287df8915218ded60f010821eb9a75ae727
Merged-In: I04e890f2249f00cc74d2311711e291efa73a9a49
Change-Id: I82856f849154efafe1b88c4c94f4475c536afaab
(cherry picked from commit 1c54a438d35ebd4cb6eda530f717d53c4fef0104)
parent 0b67a11a
Loading
Loading
Loading
Loading
+6 −14
Original line number Diff line number Diff line
@@ -604,30 +604,22 @@ bool DNSResponder::startServer() {
        return false;
    }
    for (const addrinfo* ai = ai_res ; ai ; ai = ai->ai_next) {
        android::base::unique_fd s(socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol));
        if (s.get() < 0) {
            APLOGI("ignore creating socket %d failed", s.get());
        socket_.reset(socket(ai->ai_family, ai->ai_socktype | SOCK_NONBLOCK, ai->ai_protocol));
        if (socket_.get() < 0) {
            APLOGI("ignore creating socket %d failed", socket_.get());
            continue;
        }
        enableSockopt(s.get(), SOL_SOCKET, SO_REUSEPORT).ignoreError();
        enableSockopt(s.get(), SOL_SOCKET, SO_REUSEADDR).ignoreError();
        enableSockopt(socket_.get(), SOL_SOCKET, SO_REUSEPORT).ignoreError();
        enableSockopt(socket_.get(), SOL_SOCKET, SO_REUSEADDR).ignoreError();
        std::string host_str = addr2str(ai->ai_addr, ai->ai_addrlen);
        if (bind(s.get(), ai->ai_addr, ai->ai_addrlen)) {
        if (bind(socket_.get(), ai->ai_addr, ai->ai_addrlen)) {
            APLOGI("failed to bind UDP %s:%s", host_str.c_str(), listen_service_.c_str());
            continue;
        }
        ALOGI("bound to UDP %s:%s", host_str.c_str(), listen_service_.c_str());
        socket_ = std::move(s);
        break;
    }

    int flags = fcntl(socket_.get(), F_GETFL, 0);
    if (flags < 0) flags = 0;
    if (fcntl(socket_.get(), F_SETFL, flags | O_NONBLOCK) < 0) {
        APLOGI("fcntl(F_SETFL) failed for socket %d", socket_.get());
        return false;
    }

    // Set up eventfd socket.
    event_fd_.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
    if (event_fd_.get() == -1) {