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

Commit 6c746778 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Don't depend on libbase result.h" into main

parents 940e127e 88aa8c3c
Loading
Loading
Loading
Loading
+0 −1
Original line number Original line Diff line number Diff line
@@ -17,7 +17,6 @@


#include <memory>
#include <memory>


#include <android-base/result.h>
#include <android-base/unique_fd.h>
#include <android-base/unique_fd.h>
#include <utils/Errors.h>
#include <utils/Errors.h>


+1 −2
Original line number Original line Diff line number Diff line
@@ -18,14 +18,13 @@
#include <stddef.h>
#include <stddef.h>
#include <cstdint>
#include <cstdint>


#include <android-base/result.h>
#include <android-base/unique_fd.h>
#include <android-base/unique_fd.h>
#include <binder/RpcTransport.h>
#include <binder/RpcTransport.h>
#include <utils/Errors.h>
#include <utils/Errors.h>


namespace android::binder::os {
namespace android::binder::os {


android::base::Result<void> setNonBlocking(android::base::borrowed_fd fd);
status_t setNonBlocking(android::base::borrowed_fd fd);


status_t getRandomBytes(uint8_t* data, size_t size);
status_t getRandomBytes(uint8_t* data, size_t size);


+7 −7
Original line number Original line Diff line number Diff line
@@ -15,29 +15,29 @@
 */
 */


#include "OS.h"
#include "OS.h"
#include "Utils.h"


#include <android-base/file.h>
#include <android-base/file.h>
#include <binder/RpcTransportRaw.h>
#include <binder/RpcTransportRaw.h>
#include <log/log.h>
#include <log/log.h>
#include <string.h>
#include <string.h>


using android::base::ErrnoError;
using android::base::Result;

namespace android::binder::os {
namespace android::binder::os {


// Linux kernel supports up to 253 (from SCM_MAX_FD) for unix sockets.
// Linux kernel supports up to 253 (from SCM_MAX_FD) for unix sockets.
constexpr size_t kMaxFdsPerMsg = 253;
constexpr size_t kMaxFdsPerMsg = 253;


Result<void> setNonBlocking(android::base::borrowed_fd fd) {
status_t setNonBlocking(android::base::borrowed_fd fd) {
    int flags = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_GETFL));
    int flags = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_GETFL));
    if (flags == -1) {
    if (flags == -1) {
        return ErrnoError() << "Could not get flags for fd";
        PLOGE("Failed setNonBlocking: Could not get flags for fd");
        return -errno;
    }
    }
    if (int ret = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_SETFL, flags | O_NONBLOCK)); ret == -1) {
    if (int ret = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_SETFL, flags | O_NONBLOCK)); ret == -1) {
        return ErrnoError() << "Could not set non-blocking flag for fd";
        PLOGE("Failed setNonBlocking: Could not set non-blocking flag for fd");
        return -errno;
    }
    }
    return {};
    return OK;
}
}


status_t getRandomBytes(uint8_t* data, size_t size) {
status_t getRandomBytes(uint8_t* data, size_t size) {
+22 −23
Original line number Original line Diff line number Diff line
@@ -18,7 +18,6 @@


#include <binder/ProcessState.h>
#include <binder/ProcessState.h>


#include <android-base/result.h>
#include <android-base/scopeguard.h>
#include <android-base/scopeguard.h>
#include <android-base/strings.h>
#include <android-base/strings.h>
#include <binder/BpBinder.h>
#include <binder/BpBinder.h>
@@ -32,6 +31,7 @@
#include <utils/Thread.h>
#include <utils/Thread.h>


#include "Static.h"
#include "Static.h"
#include "Utils.h"
#include "binder_module.h"
#include "binder_module.h"


#include <errno.h>
#include <errno.h>
@@ -512,31 +512,31 @@ String8 ProcessState::getDriverName() {
    return mDriverName;
    return mDriverName;
}
}


static base::Result<int> open_driver(const char* driver) {
static base::unique_fd open_driver(const char* driver) {
    int fd = open(driver, O_RDWR | O_CLOEXEC);
    auto fd = base::unique_fd(open(driver, O_RDWR | O_CLOEXEC));
    if (fd < 0) {
    if (!fd.ok()) {
        return base::ErrnoError() << "Opening '" << driver << "' failed";
        PLOGE("Opening '%s' failed", driver);
        return {};
    }
    }
    int vers = 0;
    int vers = 0;
    status_t result = ioctl(fd, BINDER_VERSION, &vers);
    int result = ioctl(fd.get(), BINDER_VERSION, &vers);
    if (result == -1) {
    if (result == -1) {
        close(fd);
        PLOGE("Binder ioctl to obtain version failed");
        return base::ErrnoError() << "Binder ioctl to obtain version failed";
        return {};
    }
    }
    if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
    if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
        close(fd);
        ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! "
        return base::Error() << "Binder driver protocol(" << vers
              "ioctl() return value: %d",
                             << ") does not match user space protocol("
              vers, BINDER_CURRENT_PROTOCOL_VERSION, result);
                             << BINDER_CURRENT_PROTOCOL_VERSION
        return {};
                             << ")! ioctl() return value: " << result;
    }
    }
    size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
    size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
    result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
    result = ioctl(fd.get(), BINDER_SET_MAX_THREADS, &maxThreads);
    if (result == -1) {
    if (result == -1) {
        ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
        ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
    }
    }
    uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION;
    uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION;
    result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable);
    result = ioctl(fd.get(), BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable);
    if (result == -1) {
    if (result == -1) {
        ALOGE_IF(ProcessState::isDriverFeatureEnabled(
        ALOGE_IF(ProcessState::isDriverFeatureEnabled(
                     ProcessState::DriverFeature::ONEWAY_SPAM_DETECTION),
                     ProcessState::DriverFeature::ONEWAY_SPAM_DETECTION),
@@ -561,28 +561,27 @@ ProcessState::ProcessState(const char* driver)
        mThreadPoolStarted(false),
        mThreadPoolStarted(false),
        mThreadPoolSeq(1),
        mThreadPoolSeq(1),
        mCallRestriction(CallRestriction::NONE) {
        mCallRestriction(CallRestriction::NONE) {
    base::Result<int> opened = open_driver(driver);
    base::unique_fd opened = open_driver(driver);


    if (opened.ok()) {
    if (opened.ok()) {
        // mmap the binder, providing a chunk of virtual address space to receive transactions.
        // mmap the binder, providing a chunk of virtual address space to receive transactions.
        mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE,
        mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE,
                        opened.value(), 0);
                        opened.get(), 0);
        if (mVMStart == MAP_FAILED) {
        if (mVMStart == MAP_FAILED) {
            close(opened.value());
            // *sigh*
            // *sigh*
            opened = base::Error()
            ALOGE("Using %s failed: unable to mmap transaction memory.", driver);
                    << "Using " << driver << " failed: unable to mmap transaction memory.";
            opened.reset();
            mDriverName.clear();
            mDriverName.clear();
        }
        }
    }
    }


#ifdef __ANDROID__
#ifdef __ANDROID__
    LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating: %s",
    LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating.",
                        driver, opened.error().message().c_str());
                        driver);
#endif
#endif


    if (opened.ok()) {
    if (opened.ok()) {
        mDriverFD = opened.value();
        mDriverFD = opened.release();
    }
    }
}
}


+1 −4
Original line number Original line Diff line number Diff line
@@ -230,10 +230,7 @@ status_t RpcServer::recvmsgSocketConnection(const RpcServer& server, RpcTranspor
    }
    }


    unique_fd fd(std::move(std::get<unique_fd>(fds.back())));
    unique_fd fd(std::move(std::get<unique_fd>(fds.back())));
    if (auto res = binder::os::setNonBlocking(fd); !res.ok()) {
    if (status_t res = binder::os::setNonBlocking(fd); res != OK) return res;
        ALOGE("Failed setNonBlocking: %s", res.error().message().c_str());
        return res.error().code() == 0 ? UNKNOWN_ERROR : -res.error().code();
    }


    *out = RpcTransportFd(std::move(fd));
    *out = RpcTransportFd(std::move(fd));
    return OK;
    return OK;
Loading