diff options
author | 2015-11-23 22:22:45 +0000 | |
---|---|---|
committer | 2015-11-23 22:22:45 +0000 | |
commit | 8f237272e0630b8f279db7f90aebfff73a1a9c00 (patch) | |
tree | efc8ed327ecc60b28d5c94a86f275863c3d9e0ea | |
parent | 49b5443a49d737d13c8d05dfd9b827b2c4198edc (diff) | |
parent | 06673e38983baad380e818c5493b7faccf3b2393 (diff) |
Merge "Add support for file descriptors and vectors thereof"
-rw-r--r-- | include/binder/Parcel.h | 21 | ||||
-rw-r--r-- | libs/binder/Parcel.cpp | 42 |
2 files changed, 56 insertions, 7 deletions
diff --git a/include/binder/Parcel.h b/include/binder/Parcel.h index a12d68d0e9..8dd9b651f1 100644 --- a/include/binder/Parcel.h +++ b/include/binder/Parcel.h @@ -20,6 +20,7 @@ #include <vector> #include <cutils/native_handle.h> +#include <nativehelper/ScopedFd.h> #include <utils/Errors.h> #include <utils/RefBase.h> #include <utils/String16.h> @@ -154,6 +155,17 @@ public: // will be closed once the parcel is destroyed. status_t writeDupFileDescriptor(int fd); + // Place a file descriptor into the parcel. This will not affect the + // semantics of the smart file descriptor. A new descriptor will be + // created, and will be closed when the parcel is destroyed. + status_t writeUniqueFileDescriptor( + const ScopedFd& fd); + + // Place a vector of file desciptors into the parcel. Each descriptor is + // dup'd as in writeDupFileDescriptor + status_t writeUniqueFileDescriptorVector( + const std::vector<ScopedFd>& val); + // Writes a blob to the parcel. // If the blob is small, then it is stored in-place, otherwise it is // transferred by way of an anonymous shared memory region. Prefer sending @@ -250,6 +262,15 @@ public: // in the parcel, which you do not own -- use dup() to get your own copy. int readFileDescriptor() const; + // Retrieve a smart file descriptor from the parcel. + status_t readUniqueFileDescriptor( + ScopedFd* val) const; + + + // Retrieve a vector of smart file descriptors from the parcel. + status_t readUniqueFileDescriptorVector( + std::vector<ScopedFd>* val) const; + // Reads a blob from the parcel. // The caller should call release() on the blob after reading its contents. status_t readBlob(size_t len, ReadableBlob* outBlob) const; diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index 03348da773..94968218e0 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -980,12 +980,20 @@ status_t Parcel::writeDupFileDescriptor(int fd) return -errno; } status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/); - if (err) { + if (err != OK) { close(dupFd); } return err; } +status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) { + return writeDupFileDescriptor(fd.get()); +} + +status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) { + return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor); +} + status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob) { if (len > INT32_MAX) { @@ -1601,16 +1609,36 @@ native_handle* Parcel::readNativeHandle() const int Parcel::readFileDescriptor() const { const flat_binder_object* flat = readObject(true); - if (flat) { - switch (flat->type) { - case BINDER_TYPE_FD: - //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this); - return flat->handle; - } + + if (flat && flat->type == BINDER_TYPE_FD) { + return flat->handle; } + return BAD_TYPE; } +status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const +{ + int got = readFileDescriptor(); + + if (got == BAD_TYPE) { + return BAD_TYPE; + } + + val->reset(dup(got)); + + if (val->get() < 0) { + return BAD_VALUE; + } + + return OK; +} + + +status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const { + return readTypedVector(val, &Parcel::readUniqueFileDescriptor); +} + status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const { int32_t blobType; |