Loading libs/binder/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -61,6 +61,7 @@ cc_library { "MemoryDealer.cpp", "MemoryHeapBase.cpp", "Parcel.cpp", "ParcelFileDescriptor.cpp", "PermissionCache.cpp", "PersistableBundle.cpp", "ProcessInfoService.cpp", Loading libs/binder/Parcel.cpp +29 −0 Original line number Diff line number Diff line Loading @@ -1179,6 +1179,19 @@ status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership) return writeFileDescriptor(fd, takeOwnership); } status_t Parcel::writeDupParcelFileDescriptor(int fd) { int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0); if (dupFd < 0) { return -errno; } status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/); if (err != OK) { close(dupFd); } return err; } status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) { return writeDupFileDescriptor(fd.get()); } Loading Loading @@ -2167,6 +2180,22 @@ status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const return OK; } status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const { int got = readParcelFileDescriptor(); if (got == BAD_TYPE) { return BAD_TYPE; } val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0)); if (val->get() < 0) { return BAD_VALUE; } return OK; } status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor); Loading libs/binder/ParcelFileDescriptor.cpp 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <binder/ParcelFileDescriptor.h> namespace android { namespace os { ParcelFileDescriptor::ParcelFileDescriptor() = default; ParcelFileDescriptor::ParcelFileDescriptor(android::base::unique_fd fd) : mFd(std::move(fd)) {} ParcelFileDescriptor::~ParcelFileDescriptor() = default; status_t ParcelFileDescriptor::writeToParcel(Parcel* parcel) const { return parcel->writeDupParcelFileDescriptor(mFd.get()); } status_t ParcelFileDescriptor::readFromParcel(const Parcel* parcel) { return parcel->readUniqueParcelFileDescriptor(&mFd); } } // namespace os } // namespace android libs/binder/include/binder/Parcel.h +7 −0 Original line number Diff line number Diff line Loading @@ -205,6 +205,10 @@ public: // The Parcel does not take ownership of the given fd unless you ask it to. status_t writeParcelFileDescriptor(int fd, bool takeOwnership = false); // Place a Java "parcel file descriptor" into the parcel. A dup of the fd is made, which will // be closed once the parcel is destroyed. status_t writeDupParcelFileDescriptor(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. Loading Loading @@ -364,6 +368,9 @@ public: status_t readUniqueFileDescriptor( base::unique_fd* val) const; // Retrieve a Java "parcel file descriptor" from the parcel. status_t readUniqueParcelFileDescriptor(base::unique_fd* val) const; // Retrieve a vector of smart file descriptors from the parcel. status_t readUniqueFileDescriptorVector( Loading libs/binder/include/binder/ParcelFileDescriptor.h 0 → 100644 +51 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_PARCEL_FILE_DESCRIPTOR_H_ #define ANDROID_PARCEL_FILE_DESCRIPTOR_H_ #include <android-base/unique_fd.h> #include <binder/Parcel.h> #include <binder/Parcelable.h> namespace android { namespace os { /* * C++ implementation of the Java class android.os.ParcelFileDescriptor */ class ParcelFileDescriptor : public android::Parcelable { public: ParcelFileDescriptor(); explicit ParcelFileDescriptor(android::base::unique_fd fd); ~ParcelFileDescriptor() override; int get() const { return mFd.get(); } android::base::unique_fd release() { return std::move(mFd); } void reset(android::base::unique_fd fd = android::base::unique_fd()) { mFd = std::move(fd); } // android::Parcelable override: android::status_t writeToParcel(android::Parcel* parcel) const override; android::status_t readFromParcel(const android::Parcel* parcel) override; private: android::base::unique_fd mFd; }; } // namespace os } // namespace android #endif // ANDROID_OS_PARCEL_FILE_DESCRIPTOR_H_ Loading
libs/binder/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -61,6 +61,7 @@ cc_library { "MemoryDealer.cpp", "MemoryHeapBase.cpp", "Parcel.cpp", "ParcelFileDescriptor.cpp", "PermissionCache.cpp", "PersistableBundle.cpp", "ProcessInfoService.cpp", Loading
libs/binder/Parcel.cpp +29 −0 Original line number Diff line number Diff line Loading @@ -1179,6 +1179,19 @@ status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership) return writeFileDescriptor(fd, takeOwnership); } status_t Parcel::writeDupParcelFileDescriptor(int fd) { int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0); if (dupFd < 0) { return -errno; } status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/); if (err != OK) { close(dupFd); } return err; } status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) { return writeDupFileDescriptor(fd.get()); } Loading Loading @@ -2167,6 +2180,22 @@ status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const return OK; } status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const { int got = readParcelFileDescriptor(); if (got == BAD_TYPE) { return BAD_TYPE; } val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0)); if (val->get() < 0) { return BAD_VALUE; } return OK; } status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor); Loading
libs/binder/ParcelFileDescriptor.cpp 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <binder/ParcelFileDescriptor.h> namespace android { namespace os { ParcelFileDescriptor::ParcelFileDescriptor() = default; ParcelFileDescriptor::ParcelFileDescriptor(android::base::unique_fd fd) : mFd(std::move(fd)) {} ParcelFileDescriptor::~ParcelFileDescriptor() = default; status_t ParcelFileDescriptor::writeToParcel(Parcel* parcel) const { return parcel->writeDupParcelFileDescriptor(mFd.get()); } status_t ParcelFileDescriptor::readFromParcel(const Parcel* parcel) { return parcel->readUniqueParcelFileDescriptor(&mFd); } } // namespace os } // namespace android
libs/binder/include/binder/Parcel.h +7 −0 Original line number Diff line number Diff line Loading @@ -205,6 +205,10 @@ public: // The Parcel does not take ownership of the given fd unless you ask it to. status_t writeParcelFileDescriptor(int fd, bool takeOwnership = false); // Place a Java "parcel file descriptor" into the parcel. A dup of the fd is made, which will // be closed once the parcel is destroyed. status_t writeDupParcelFileDescriptor(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. Loading Loading @@ -364,6 +368,9 @@ public: status_t readUniqueFileDescriptor( base::unique_fd* val) const; // Retrieve a Java "parcel file descriptor" from the parcel. status_t readUniqueParcelFileDescriptor(base::unique_fd* val) const; // Retrieve a vector of smart file descriptors from the parcel. status_t readUniqueFileDescriptorVector( Loading
libs/binder/include/binder/ParcelFileDescriptor.h 0 → 100644 +51 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_PARCEL_FILE_DESCRIPTOR_H_ #define ANDROID_PARCEL_FILE_DESCRIPTOR_H_ #include <android-base/unique_fd.h> #include <binder/Parcel.h> #include <binder/Parcelable.h> namespace android { namespace os { /* * C++ implementation of the Java class android.os.ParcelFileDescriptor */ class ParcelFileDescriptor : public android::Parcelable { public: ParcelFileDescriptor(); explicit ParcelFileDescriptor(android::base::unique_fd fd); ~ParcelFileDescriptor() override; int get() const { return mFd.get(); } android::base::unique_fd release() { return std::move(mFd); } void reset(android::base::unique_fd fd = android::base::unique_fd()) { mFd = std::move(fd); } // android::Parcelable override: android::status_t writeToParcel(android::Parcel* parcel) const override; android::status_t readFromParcel(const android::Parcel* parcel) override; private: android::base::unique_fd mFd; }; } // namespace os } // namespace android #endif // ANDROID_OS_PARCEL_FILE_DESCRIPTOR_H_