File tree Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,12 @@ impl FileSystem {
3131 content : "Hello from filesystem!" . to_string ( ) ,
3232 } ,
3333 ) ;
34+ files. insert (
35+ "program.exe" . to_string ( ) ,
36+ File {
37+ content : "Executable program content" . to_string ( ) ,
38+ } ,
39+ ) ;
3440
3541 Self { files }
3642 }
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ pub enum ProcessState {
1515pub struct Process {
1616 pub pid : PID ,
1717 pub parent_pid : Option < PID > ,
18+ pub program_name : String ,
1819 pub state : ProcessState ,
1920 pub exit_status : Option < ExitStatus > ,
2021 pub children : HashSet < PID > ,
@@ -31,6 +32,7 @@ impl Process {
3132 Self {
3233 pid,
3334 parent_pid,
35+ program_name : "init" . to_string ( ) ,
3436 state : ProcessState :: Ready ,
3537 exit_status : None ,
3638 children : HashSet :: new ( ) ,
@@ -84,11 +86,13 @@ impl ProcessControlBlock {
8486 Ok ( child_pid)
8587 }
8688
87- pub fn exec ( & mut self , pid : PID ) -> Result < ( ) , String > {
89+ pub fn exec ( & mut self , pid : PID , program_name : String ) -> Result < ( ) , String > {
8890 let process = self
8991 . processes
9092 . get_mut ( & pid)
9193 . ok_or_else ( || format ! ( "Process {} not found" , pid) ) ?;
94+
95+ process. program_name = program_name;
9296 process. state = ProcessState :: Ready ;
9397
9498 Ok ( ( ) )
Original file line number Diff line number Diff line change @@ -54,7 +54,7 @@ pub fn syscall_dispatch(
5454 }
5555 SYS_GETPID => sys_getpid ( current_pid) ,
5656 SYS_KILL => sys_kill ( & mut kernel. process_table , current_pid, args. args [ 0 ] as PID ) ,
57- SYS_EXEC => Ok ( 0 ) ,
57+ SYS_EXEC => sys_exec ( kernel , current_pid , args . args [ 0 ] as u32 ) ,
5858 SYS_READ => {
5959 let fd = FileDescriptor ( args. args [ 0 ] as u32 ) ;
6060 let count = args. args [ 1 ] as usize ;
@@ -261,3 +261,20 @@ pub fn sys_close(
261261 Err ( SyscallError :: EBADF )
262262 }
263263}
264+
265+ pub fn sys_exec (
266+ kernel : & mut super :: Kernel ,
267+ current_pid : PID ,
268+ _path_len : u32 ,
269+ ) -> SyscallResult < i32 > {
270+ let program_name = "program.exe" . to_string ( ) ;
271+
272+ if !kernel. filesystem . files . contains_key ( & program_name) {
273+ return Err ( SyscallError :: EBADF ) ;
274+ }
275+
276+ match kernel. process_table . exec ( current_pid, program_name) {
277+ Ok ( ( ) ) => Ok ( 0 ) ,
278+ Err ( _) => Err ( SyscallError :: ESRCH ) ,
279+ }
280+ }
You can’t perform that action at this time.
0 commit comments