mod.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use types::*;
  2. const AT_FDCWD: c_int = -100;
  3. pub fn brk(addr: *const c_void) -> c_int {
  4. unsafe {
  5. let newbrk = syscall!(BRK, addr);
  6. if newbrk < addr as usize {
  7. -1
  8. } else {
  9. 0
  10. }
  11. }
  12. }
  13. pub fn chdir(path: *const c_char) -> c_int {
  14. unsafe { syscall!(CHDIR, path) as c_int }
  15. }
  16. pub fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
  17. unsafe { syscall!(CHOWN, owner as u32, group as u32) as c_int }
  18. }
  19. pub fn close(fildes: c_int) -> c_int {
  20. unsafe { syscall!(CLOSE, fildes) as c_int }
  21. }
  22. pub fn dup(fildes: c_int) -> c_int {
  23. unsafe { syscall!(DUP, fildes) as c_int }
  24. }
  25. pub fn dup2(fildes: c_int, fildes2: c_int) -> c_int {
  26. unsafe { syscall!(DUP2, fildes, fildes2) as c_int }
  27. }
  28. pub fn exit(status: c_int) -> ! {
  29. unsafe {
  30. syscall!(EXIT, status);
  31. }
  32. loop {}
  33. }
  34. pub fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
  35. unsafe { syscall!(FCHOWN, fildes, owner, group) as c_int }
  36. }
  37. pub fn fchdir(fildes: c_int) -> c_int {
  38. unsafe { syscall!(FCHDIR, fildes) as c_int }
  39. }
  40. pub fn fsync(fildes: c_int) -> c_int {
  41. unsafe { syscall!(FSYNC, fildes) as c_int }
  42. }
  43. pub fn ftruncate(fildes: c_int, length: off_t) -> c_int {
  44. unsafe { syscall!(FTRUNCATE, fildes, length) as c_int }
  45. }
  46. pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
  47. unsafe {
  48. syscall!(GETCWD, buf, size);
  49. buf as *mut c_char
  50. }
  51. }
  52. pub fn getegid() -> gid_t {
  53. unsafe { syscall!(GETEGID) }
  54. }
  55. pub fn geteuid() -> uid_t {
  56. unsafe { syscall!(GETEUID) }
  57. }
  58. pub fn getgid() -> gid_t {
  59. unsafe { syscall!(GETGID) }
  60. }
  61. pub fn getpgid(pid: pid_t) -> pid_t {
  62. unsafe { syscall!(GETPGID, pid) }
  63. }
  64. pub fn getpid() -> pid_t {
  65. unsafe { syscall!(GETPID) }
  66. }
  67. pub fn getppid() -> pid_t {
  68. unsafe { syscall!(GETPPID) }
  69. }
  70. pub fn getuid() -> uid_t {
  71. unsafe { syscall!(GETUID) }
  72. }
  73. pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
  74. unsafe { syscall!(LINK, path1, path2) as c_int }
  75. }
  76. #[cfg(target_arch = "x86_64")]
  77. pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
  78. unsafe { syscall!(OPEN, path, oflag, mode) as c_int }
  79. }
  80. #[cfg(target_arch = "aarch64")]
  81. pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
  82. unsafe { syscall!(OPENAT, AT_FDCWD, path, oflag, mode) as c_int }
  83. }
  84. pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
  85. unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) as ssize_t }
  86. }