sys_open.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //! System call handler for opening files.
  2. use system_error::SystemError;
  3. use crate::arch::syscall::nr::SYS_OPEN;
  4. use crate::syscall::table::FormattedSyscallParam;
  5. use crate::syscall::table::Syscall;
  6. use alloc::string::ToString;
  7. use alloc::vec::Vec;
  8. /// Handler for the `open` system call.
  9. pub struct SysOpenHandle;
  10. impl Syscall for SysOpenHandle {
  11. /// Returns the number of arguments this syscall takes (3).
  12. fn num_args(&self) -> usize {
  13. 3
  14. }
  15. /// Handles the open syscall by extracting arguments and calling `do_open`.
  16. fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
  17. let path = Self::path(args);
  18. let flags = Self::flags(args);
  19. let mode = Self::mode(args);
  20. super::open_utils::do_open(path, flags, mode, true)
  21. }
  22. /// Formats the syscall arguments for display/debugging purposes.
  23. fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {
  24. vec![
  25. FormattedSyscallParam::new("path", format!("{:#x}", Self::path(args) as usize)),
  26. FormattedSyscallParam::new("flags", Self::flags(args).to_string()),
  27. FormattedSyscallParam::new("mode", Self::mode(args).to_string()),
  28. ]
  29. }
  30. }
  31. impl SysOpenHandle {
  32. /// Extracts the path argument from syscall parameters.
  33. fn path(args: &[usize]) -> *const u8 {
  34. args[0] as *const u8
  35. }
  36. /// Extracts the flags argument from syscall parameters.
  37. fn flags(args: &[usize]) -> u32 {
  38. args[1] as u32
  39. }
  40. /// Extracts the mode argument from syscall parameters.
  41. fn mode(args: &[usize]) -> u32 {
  42. args[2] as u32
  43. }
  44. }
  45. syscall_table_macros::declare_syscall!(SYS_OPEN, SysOpenHandle);