xref: /DragonOS/kernel/src/ipc/syscall.rs (revision 7285c927d95bb4b5c692c51a8f86c47009d07667)
1 use crate::{
2     arch::asm::current::current_pcb,
3     filesystem::vfs::file::{File, FileMode},
4     include::bindings::bindings::pt_regs,
5     syscall::SystemError,
6 };
7 
8 use super::pipe::LockedPipeInode;
9 
10 #[no_mangle]
11 /// @brief 调用匿名管道
12 pub extern "C" fn sys_pipe(regs: &pt_regs) -> u64 {
13     let fd: *mut i32 = regs.r8 as *mut i32;
14     return do_pipe(fd)
15         .map(|x| x as u64)
16         .unwrap_or_else(|e| e.to_posix_errno() as u64);
17 }
18 
19 pub fn do_pipe(fd: *mut i32) -> Result<i64, SystemError> {
20     let pipe_ptr = LockedPipeInode::new();
21     let read_file = File::new(pipe_ptr.clone(), FileMode::O_RDONLY);
22     let write_file = File::new(pipe_ptr.clone(), FileMode::O_WRONLY);
23 
24     let read_fd = current_pcb().alloc_fd(read_file.unwrap(), None);
25     if !read_fd.is_ok() {
26         return Err(read_fd.unwrap_err());
27     }
28 
29     let write_fd = current_pcb().alloc_fd(write_file.unwrap(), None);
30     if !write_fd.is_ok() {
31         return Err(write_fd.unwrap_err());
32     }
33     unsafe {
34         *fd.offset(0) = read_fd.unwrap();
35         *fd.offset(1) = write_fd.unwrap();
36     }
37     return Ok(0);
38 }
39