xref: /DragonOS/kernel/src/process/syscall.rs (revision ab5c8ca46db8e7d4793a9791292122b0b9684274)
1 use core::ffi::{c_int, c_void};
2 
3 use crate::{
4     arch::asm::current::current_pcb,
5     include::bindings::bindings::{pid_t, process_do_exit},
6     syscall::{Syscall, SystemError},
7 };
8 
9 extern "C" {
10     fn c_sys_wait4(pid: pid_t, wstatus: *mut c_int, options: c_int, rusage: *mut c_void) -> c_int;
11 }
12 
13 impl Syscall {
14     #[allow(dead_code)]
15     pub fn fork(&self) -> Result<usize, SystemError> {
16         // 由于进程管理未完成重构,fork调用暂时在arch/x86_64/syscall.rs中调用,以后会移动到这里。
17         todo!()
18     }
19 
20     #[allow(dead_code)]
21     pub fn vfork(&self) -> Result<usize, SystemError> {
22         // 由于进程管理未完成重构,vfork调用暂时在arch/x86_64/syscall.rs中调用,以后会移动到这里。
23         todo!()
24     }
25 
26     #[allow(dead_code)]
27     pub fn execve(
28         _path: *const c_void,
29         _argv: *const *const c_void,
30         _envp: *const *const c_void,
31     ) -> Result<usize, SystemError> {
32         // 由于进程管理未完成重构,execve调用暂时在arch/x86_64/syscall.rs中调用,以后会移动到这里。
33         todo!()
34     }
35 
36     pub fn wait4(
37         pid: pid_t,
38         wstatus: *mut c_int,
39         options: c_int,
40         rusage: *mut c_void,
41     ) -> Result<usize, SystemError> {
42         let ret = unsafe { c_sys_wait4(pid, wstatus, options, rusage) };
43         if (ret as isize) < 0 {
44             return Err(SystemError::from_posix_errno(-(ret as isize) as i32)
45                 .expect("wait4: Invalid errno"));
46         }
47         return Ok(ret as usize);
48     }
49 
50     /// # 退出进程
51     ///
52     /// ## 参数
53     ///
54     /// - status: 退出状态
55     pub fn exit(status: usize) -> ! {
56         unsafe { process_do_exit(status as u64) };
57         loop {}
58     }
59 
60     /// # 获取进程ID
61     pub fn getpid() -> Result<usize, SystemError> {
62         return Ok(current_pcb().pid as usize);
63     }
64 }
65