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

Commit bf551893 authored by Ryo Hashimoto's avatar Ryo Hashimoto
Browse files

Add C++ implementation of ParcelFileDescriptor

Bug: 80377815
Test: make binderLibTest && adb sync && adb shell /data/nativetest64/binderLibTest/binderLibTest
Change-Id: I1c54a80b7bf1cf1a51987502e4d844765c20531d
parent 14906cd8
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -61,6 +61,7 @@ cc_library {
        "MemoryDealer.cpp",
        "MemoryDealer.cpp",
        "MemoryHeapBase.cpp",
        "MemoryHeapBase.cpp",
        "Parcel.cpp",
        "Parcel.cpp",
        "ParcelFileDescriptor.cpp",
        "PermissionCache.cpp",
        "PermissionCache.cpp",
        "PersistableBundle.cpp",
        "PersistableBundle.cpp",
        "ProcessInfoService.cpp",
        "ProcessInfoService.cpp",
+29 −0
Original line number Original line Diff line number Diff line
@@ -1179,6 +1179,19 @@ status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
    return writeFileDescriptor(fd, 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) {
status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
    return writeDupFileDescriptor(fd.get());
    return writeDupFileDescriptor(fd.get());
}
}
@@ -2167,6 +2180,22 @@ status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
    return OK;
    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 {
status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
    return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
    return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
+37 −0
Original line number Original line 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
+7 −0
Original line number Original line Diff line number Diff line
@@ -205,6 +205,10 @@ public:
    // The Parcel does not take ownership of the given fd unless you ask it to.
    // The Parcel does not take ownership of the given fd unless you ask it to.
    status_t            writeParcelFileDescriptor(int fd, bool takeOwnership = false);
    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
    // Place a file descriptor into the parcel.  This will not affect the
    // semantics of the smart file descriptor. A new descriptor will be
    // semantics of the smart file descriptor. A new descriptor will be
    // created, and will be closed when the parcel is destroyed.
    // created, and will be closed when the parcel is destroyed.
@@ -364,6 +368,9 @@ public:
    status_t            readUniqueFileDescriptor(
    status_t            readUniqueFileDescriptor(
                            base::unique_fd* val) const;
                            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.
    // Retrieve a vector of smart file descriptors from the parcel.
    status_t            readUniqueFileDescriptorVector(
    status_t            readUniqueFileDescriptorVector(
+51 −0
Original line number Original line 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