mod.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. return -1
  8. }
  9. 0
  10. }
  11. }
  12. pub fn chdir(path: *const c_char) -> c_int {
  13. unsafe {
  14. syscall!(CHDIR, path) as c_int
  15. }
  16. }
  17. pub fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
  18. unsafe {
  19. syscall!(CHOWN, owner as u32, group as u32) as c_int
  20. }
  21. }
  22. pub fn close(fildes: c_int) -> c_int {
  23. unsafe {
  24. syscall!(CLOSE, fildes) as c_int
  25. }
  26. }
  27. pub fn dup(fildes: c_int) -> c_int {
  28. unsafe {
  29. syscall!(DUP, fildes) as c_int
  30. }
  31. }
  32. pub fn dup2(fildes: c_int, fildes2:c_int) -> c_int {
  33. unsafe {
  34. syscall!(DUP2, fildes, fildes2) as c_int
  35. }
  36. }
  37. pub fn exit(status: c_int) -> ! {
  38. unsafe {
  39. syscall!(EXIT, status);
  40. }
  41. loop {}
  42. }
  43. pub fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
  44. unsafe {
  45. syscall!(FCHOWN, owner, group) as c_int
  46. }
  47. }
  48. pub fn fchdir(fildes: c_int) -> c_int {
  49. unsafe {
  50. syscall!(FCHDIR, fildes) as c_int
  51. }
  52. }
  53. pub fn fsync(fildes: c_int) -> c_int {
  54. unsafe {
  55. syscall!(FSYNC, fildes) as c_int
  56. }
  57. }
  58. pub fn ftruncate(fildes: c_int, length: off_t) -> c_int {
  59. unsafe {
  60. syscall!(FTRUNCATE, fildes, length) as c_int
  61. }
  62. }
  63. pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
  64. unsafe {
  65. syscall!(GETCWD, buf, size);
  66. buf as *mut c_char
  67. }
  68. }
  69. pub fn getegid() -> gid_t {
  70. unsafe {
  71. syscall!(GETEGID)
  72. }
  73. }
  74. pub fn geteuid() -> uid_t {
  75. unsafe {
  76. syscall!(GETEUID)
  77. }
  78. }
  79. pub fn getgid() -> gid_t {
  80. unsafe {
  81. syscall!(GETGID)
  82. }
  83. }
  84. pub fn getpgid(pid: pid_t) -> pid_t {
  85. unsafe {
  86. syscall!(GETPGID, pid)
  87. }
  88. }
  89. pub fn getpid() -> pid_t {
  90. unsafe {
  91. syscall!(GETPID)
  92. }
  93. }
  94. pub fn getppid() -> pid_t {
  95. unsafe {
  96. syscall!(GETPPID)
  97. }
  98. }
  99. pub fn getuid() -> uid_t {
  100. unsafe {
  101. syscall!(GETUID)
  102. }
  103. }
  104. pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
  105. unsafe {
  106. syscall!(LINK, path1, path2) as c_int
  107. }
  108. }
  109. #[cfg(target_arch = "x86_64")]
  110. pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
  111. unsafe {
  112. syscall!(OPEN, path, oflag, mode) as c_int
  113. }
  114. }
  115. #[cfg(target_arch = "aarch64")]
  116. pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
  117. unsafe {
  118. syscall!(OPENAT, AT_FDCWD, path, oflag, mode) as c_int
  119. }
  120. }
  121. pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
  122. unsafe {
  123. syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) as ssize_t
  124. }
  125. }