@@ -252,8 +252,7 @@ mod impls {
252252 use core:: { alloc:: Layout , ptr:: NonNull } ;
253253 use spin:: Mutex ;
254254 use tg_console:: log;
255- use tg_easy_fs:: UserBuffer ;
256- use tg_easy_fs:: { FSManager , OpenFlags } ;
255+ use tg_easy_fs:: { make_pipe, FSManager , OpenFlags , UserBuffer } ;
257256 use tg_kernel_vm:: {
258257 page_table:: { MmuMeta , Pte , Sv39 , VAddr , VmFlags , VmMeta , PPN , VPN } ,
259258 PageManager ,
@@ -354,7 +353,7 @@ mod impls {
354353 } ) ;
355354 count as _
356355 } else if let Some ( file) = & current. fd_table [ fd] {
357- let mut file = file. lock ( ) ;
356+ let file = file. lock ( ) ;
358357 if file. writable ( ) {
359358 let mut v: Vec < & ' static mut [ u8 ] > = Vec :: new ( ) ;
360359 unsafe { v. push ( core:: slice:: from_raw_parts_mut ( ptr. as_ptr ( ) , count) ) } ;
@@ -386,7 +385,7 @@ mod impls {
386385 }
387386 count as _
388387 } else if let Some ( file) = & current. fd_table [ fd] {
389- let mut file = file. lock ( ) ;
388+ let file = file. lock ( ) ;
390389 if file. readable ( ) {
391390 let mut v: Vec < & ' static mut [ u8 ] > = Vec :: new ( ) ;
392391 unsafe { v. push ( core:: slice:: from_raw_parts_mut ( ptr. as_ptr ( ) , count) ) } ;
@@ -426,7 +425,7 @@ mod impls {
426425 FS . open ( string. as_str ( ) , OpenFlags :: from_bits ( flags as u32 ) . unwrap ( ) )
427426 {
428427 let new_fd = current. fd_table . len ( ) ;
429- current. fd_table . push ( Some ( Mutex :: new ( fd. as_ref ( ) . clone ( ) ) ) ) ;
428+ current. fd_table . push ( Some ( Mutex :: new ( fd) ) ) ;
430429 new_fd as isize
431430 } else {
432431 -1
@@ -446,6 +445,34 @@ mod impls {
446445 current. fd_table [ fd] . take ( ) ;
447446 0
448447 }
448+
449+ fn pipe ( & self , _caller : Caller , pipe : usize ) -> isize {
450+ let current = PROCESSOR . get_mut ( ) . get_current_proc ( ) . unwrap ( ) ;
451+ let ( read_end, write_end) = make_pipe ( ) ;
452+ let read_fd = current. fd_table . len ( ) ;
453+ current. fd_table . push ( Some ( Mutex :: new ( read_end) ) ) ;
454+ let write_fd = current. fd_table . len ( ) ;
455+ current. fd_table . push ( Some ( Mutex :: new ( write_end) ) ) ;
456+ // 将 read_fd 写入 pipe[0]
457+ if let Some ( mut ptr) = current
458+ . address_space
459+ . translate :: < usize > ( VAddr :: new ( pipe) , WRITEABLE )
460+ {
461+ unsafe { * ptr. as_mut ( ) = read_fd } ;
462+ } else {
463+ return -1 ;
464+ }
465+ // 将 write_fd 写入 pipe[1]
466+ if let Some ( mut ptr) = current
467+ . address_space
468+ . translate :: < usize > ( VAddr :: new ( pipe + core:: mem:: size_of :: < usize > ( ) ) , WRITEABLE )
469+ {
470+ unsafe { * ptr. as_mut ( ) = write_fd } ;
471+ } else {
472+ return -1 ;
473+ }
474+ 0
475+ }
449476 }
450477
451478 impl Process for SyscallContext {
0 commit comments