Merge "Add support for file descriptors and vectors thereof" am: 8f237272e0
am: 0ebbd1ef19
* commit '0ebbd1ef19a4a4f6b0dfcc8d8d7c625ab5211f9d':
Add support for file descriptors and vectors thereof
diff --git a/Parcel.cpp b/Parcel.cpp
index a101d90..13ecc87 100644
--- a/Parcel.cpp
+++ b/Parcel.cpp
@@ -1006,12 +1006,20 @@
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) {
@@ -1625,16 +1633,36 @@
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;
diff --git a/include/hwbinder/Parcel.h b/include/hwbinder/Parcel.h
index b6106ba..16a4790 100644
--- a/include/hwbinder/Parcel.h
+++ b/include/hwbinder/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 @@
// 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 @@
// 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;