1/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
1617use super::{
18 BorrowedParcel, Deserialize, DeserializeArray, DeserializeOption, Serialize, SerializeArray,
19 SerializeOption,
20};
21use crate::binder::AsNative;
22use crate::error::{status_result, Result, StatusCode};
23use crate::sys;
2425use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
2627/// Rust version of the Java class android.os.ParcelFileDescriptor
28#[derive(Debug)]
29pub struct ParcelFileDescriptor(OwnedFd);
3031impl ParcelFileDescriptor {
32/// Create a new `ParcelFileDescriptor`
33pub fn new<F: Into<OwnedFd>>(fd: F) -> Self {
34Self(fd.into())
35 }
36}
3738impl AsRef<OwnedFd> for ParcelFileDescriptor {
39fn as_ref(&self) -> &OwnedFd {
40&self.0
41}
42}
4344impl From<ParcelFileDescriptor> for OwnedFd {
45fn from(fd: ParcelFileDescriptor) -> OwnedFd {
46 fd.0
47}
48}
4950impl AsRawFd for ParcelFileDescriptor {
51fn as_raw_fd(&self) -> RawFd {
52self.0.as_raw_fd()
53 }
54}
5556impl IntoRawFd for ParcelFileDescriptor {
57fn into_raw_fd(self) -> RawFd {
58self.0.into_raw_fd()
59 }
60}
6162impl PartialEq for ParcelFileDescriptor {
63// Since ParcelFileDescriptors own the FD, if this function ever returns true (and it is used to
64 // compare two different objects), then it would imply that an FD is double-owned.
65fn eq(&self, other: &Self) -> bool {
66self.as_raw_fd() == other.as_raw_fd()
67 }
68}
6970impl Eq for ParcelFileDescriptor {}
7172impl Serialize for ParcelFileDescriptor {
73fn serialize(&self, parcel: &mut BorrowedParcel<'_>) -> Result<()> {
74let fd = self.0.as_raw_fd();
75// Safety: `Parcel` always contains a valid pointer to an
76 // `AParcel`. Likewise, `ParcelFileDescriptor` always contains a
77 // valid file, so we can borrow a valid file
78 // descriptor. `AParcel_writeParcelFileDescriptor` does NOT take
79 // ownership of the fd, so we need not duplicate it first.
80let status = unsafe { sys::AParcel_writeParcelFileDescriptor(parcel.as_native_mut(), fd) };
81 status_result(status)
82 }
83}
8485impl SerializeArray for ParcelFileDescriptor {}
8687impl SerializeOption for ParcelFileDescriptor {
88fn serialize_option(this: Option<&Self>, parcel: &mut BorrowedParcel<'_>) -> Result<()> {
89if let Some(f) = this {
90 f.serialize(parcel)
91 } else {
92let status =
93// Safety: `Parcel` always contains a valid pointer to an
94 // `AParcel`. `AParcel_writeParcelFileDescriptor` accepts the
95 // value `-1` as the file descriptor to signify serializing a
96 // null file descriptor.
97unsafe { sys::AParcel_writeParcelFileDescriptor(parcel.as_native_mut(), -1i32) };
98 status_result(status)
99 }
100 }
101}
102103impl DeserializeOption for ParcelFileDescriptor {
104fn deserialize_option(parcel: &BorrowedParcel<'_>) -> Result<Option<Self>> {
105let mut fd = -1i32;
106// Safety: `Parcel` always contains a valid pointer to an
107 // `AParcel`. We pass a valid mutable pointer to an i32, which
108 // `AParcel_readParcelFileDescriptor` assigns the valid file
109 // descriptor into, or `-1` if deserializing a null file
110 // descriptor. The read function passes ownership of the file
111 // descriptor to its caller if it was non-null, so we must take
112 // ownership of the file and ensure that it is eventually closed.
113unsafe {
114 status_result(sys::AParcel_readParcelFileDescriptor(parcel.as_native(), &mut fd))?;
115 }
116if fd < 0 {
117Ok(None)
118 } else {
119// Safety: At this point, we know that the file descriptor was
120 // not -1, so must be a valid, owned file descriptor which we
121 // can safely turn into a `File`.
122let file = unsafe { OwnedFd::from_raw_fd(fd) };
123Ok(Some(ParcelFileDescriptor::new(file)))
124 }
125 }
126}
127128impl Deserialize for ParcelFileDescriptor {
129type UninitType = Option<Self>;
130fn uninit() -> Self::UninitType {
131Self::UninitType::default()
132 }
133fn from_init(value: Self) -> Self::UninitType {
134Some(value)
135 }
136137fn deserialize(parcel: &BorrowedParcel<'_>) -> Result<Self> {
138 Deserialize::deserialize(parcel).transpose().unwrap_or(Err(StatusCode::UNEXPECTED_NULL))
139 }
140}
141142impl DeserializeArray for ParcelFileDescriptor {}