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

Commit cd6e93b1 authored by Steven Moreland's avatar Steven Moreland Committed by Automerger Merge Worker
Browse files

Merge "ScopedFileDescriptor: better error on close" am: a77a5524 am: 2ced6c72

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1529198

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I7c1657e3e3c07aa1f47ef6aa5b06badf69b9d2d7
parents 26e76496 2ced6c72
Loading
Loading
Loading
Loading
+26 −6
Original line number Diff line number Diff line
@@ -32,6 +32,14 @@

#include <assert.h>

// defined differently by liblog
#pragma push_macro("LOG_PRI")
#ifdef LOG_PRI
#undef LOG_PRI
#endif
#include <syslog.h>
#pragma pop_macro("LOG_PRI")

#include <unistd.h>
#include <cstddef>
#include <string>
@@ -130,7 +138,7 @@ namespace impl {
/**
 * This baseclass owns a single object, used to make various classes RAII.
 */
template <typename T, typename R, R (*Destroy)(T), T DEFAULT>
template <typename T, void (*Destroy)(T), T DEFAULT>
class ScopedAResource {
   public:
    /**
@@ -198,7 +206,7 @@ class ScopedAResource {
/**
 * Convenience wrapper. See AParcel.
 */
class ScopedAParcel : public impl::ScopedAResource<AParcel*, void, AParcel_delete, nullptr> {
class ScopedAParcel : public impl::ScopedAResource<AParcel*, AParcel_delete, nullptr> {
   public:
    /**
     * Takes ownership of a.
@@ -219,7 +227,7 @@ class ScopedAParcel : public impl::ScopedAResource<AParcel*, void, AParcel_delet
/**
 * Convenience wrapper. See AStatus.
 */
class ScopedAStatus : public impl::ScopedAResource<AStatus*, void, AStatus_delete, nullptr> {
class ScopedAStatus : public impl::ScopedAResource<AStatus*, AStatus_delete, nullptr> {
   public:
    /**
     * Takes ownership of a.
@@ -291,7 +299,7 @@ class ScopedAStatus : public impl::ScopedAResource<AStatus*, void, AStatus_delet
 * Convenience wrapper. See AIBinder_DeathRecipient.
 */
class ScopedAIBinder_DeathRecipient
    : public impl::ScopedAResource<AIBinder_DeathRecipient*, void, AIBinder_DeathRecipient_delete,
    : public impl::ScopedAResource<AIBinder_DeathRecipient*, AIBinder_DeathRecipient_delete,
                                   nullptr> {
   public:
    /**
@@ -308,7 +316,7 @@ class ScopedAIBinder_DeathRecipient
 * Convenience wrapper. See AIBinder_Weak.
 */
class ScopedAIBinder_Weak
    : public impl::ScopedAResource<AIBinder_Weak*, void, AIBinder_Weak_delete, nullptr> {
    : public impl::ScopedAResource<AIBinder_Weak*, AIBinder_Weak_delete, nullptr> {
   public:
    /**
     * Takes ownership of a.
@@ -324,10 +332,22 @@ class ScopedAIBinder_Weak
    SpAIBinder promote() { return SpAIBinder(AIBinder_Weak_promote(get())); }
};

namespace internal {

static void closeWithError(int fd) {
    if (fd == -1) return;
    int ret = close(fd);
    if (ret != 0) {
        syslog(LOG_ERR, "Could not close FD %d: %s", fd, strerror(errno));
    }
}

}  // namespace internal

/**
 * Convenience wrapper for a file descriptor.
 */
class ScopedFileDescriptor : public impl::ScopedAResource<int, int, close, -1> {
class ScopedFileDescriptor : public impl::ScopedAResource<int, internal::closeWithError, -1> {
   public:
    /**
     * Takes ownership of a.