1 // Copyright (C) 2024 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 use std::fs::File; 15 use std::os::fd::AsRawFd; 16 use std::os::unix::io::FromRawFd; 17 18 use ipc::parcel::MsgParcel; 19 use ipc::{IpcResult, IpcStatusCode}; 20 21 use crate::error::ErrorCode; 22 use crate::service::RequestServiceStub; 23 24 impl RequestServiceStub { open_channel(&self, reply: &mut MsgParcel) -> IpcResult<()>25 pub(crate) fn open_channel(&self, reply: &mut MsgParcel) -> IpcResult<()> { 26 let pid = ipc::Skeleton::calling_pid(); 27 info!("Service open_channel pid {}", pid); 28 match self.client_manager.open_channel(pid) { 29 Ok(ud_fd) => { 30 // `as_raw_fd` does not track the ownership or life cycle of this fd. 31 let fd = ud_fd.as_raw_fd(); 32 let file = unsafe { File::from_raw_fd(fd) }; 33 info!("End open_channel fd {}", fd); 34 reply.write(&(ErrorCode::ErrOk as i32))?; 35 reply.write_file(file)?; 36 Ok(()) 37 } 38 Err(err) => { 39 error!("End Service open_channel, failed: {:?}", err); 40 sys_event!( 41 ExecError, 42 DfxCode::INVALID_IPC_MESSAGE_A26, 43 &format!("End Service open_channel, failed: {:?}", err) 44 ); 45 reply.write(&(ErrorCode::ParameterCheck as i32))?; 46 Err(IpcStatusCode::Failed) 47 } 48 } 49 } 50 } 51