1 use system_error::SystemError; 2 3 use crate::{ 4 filesystem::vfs::{ 5 file::{File, FileMode}, 6 ROOT_INODE, 7 }, 8 process::{Pid, ProcessManager}, 9 }; 10 11 /// @brief 初始化pid=1的进程的stdio 12 pub fn stdio_init() -> Result<(), SystemError> { 13 if ProcessManager::current_pcb().pid() != Pid(1) { 14 return Err(SystemError::EPERM); 15 } 16 let tty_inode = ROOT_INODE() 17 .lookup("/dev/tty0") 18 .expect("Init stdio: can't find tty0"); 19 let stdin = 20 File::new(tty_inode.clone(), FileMode::O_RDONLY).expect("Init stdio: can't create stdin"); 21 let stdout = 22 File::new(tty_inode.clone(), FileMode::O_WRONLY).expect("Init stdio: can't create stdout"); 23 let stderr = File::new(tty_inode.clone(), FileMode::O_WRONLY | FileMode::O_SYNC) 24 .expect("Init stdio: can't create stderr"); 25 26 /* 27 按照规定,进程的文件描述符数组的前三个位置,分别是stdin, stdout, stderr 28 */ 29 assert_eq!( 30 ProcessManager::current_pcb() 31 .fd_table() 32 .write() 33 .alloc_fd(stdin, None) 34 .unwrap(), 35 0 36 ); 37 assert_eq!( 38 ProcessManager::current_pcb() 39 .fd_table() 40 .write() 41 .alloc_fd(stdout, None) 42 .unwrap(), 43 1 44 ); 45 assert_eq!( 46 ProcessManager::current_pcb() 47 .fd_table() 48 .write() 49 .alloc_fd(stderr, None) 50 .unwrap(), 51 2 52 ); 53 return Ok(()); 54 } 55