mod.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. use core::fmt::Write;
  2. use core::{mem, ptr};
  3. use super::types::*;
  4. use super::{errno, FileWriter, Pal};
  5. use c_str::CStr;
  6. use header::dirent::dirent;
  7. use header::errno::{EINVAL, ENOSYS};
  8. use header::signal::SIGCHLD;
  9. use header::sys_ioctl::{winsize, TCGETS, TCSETS, TIOCGWINSZ};
  10. use header::sys_resource::rusage;
  11. use header::sys_select::fd_set;
  12. use header::sys_stat::stat;
  13. use header::sys_time::{itimerval, timeval, timezone};
  14. use header::sys_times::tms;
  15. use header::sys_utsname::utsname;
  16. use header::termios::termios;
  17. use header::time::timespec;
  18. mod signal;
  19. mod socket;
  20. const AT_FDCWD: c_int = -100;
  21. const AT_EMPTY_PATH: c_int = 0x1000;
  22. const AT_REMOVEDIR: c_int = 0x200;
  23. fn e(sys: usize) -> usize {
  24. if (sys as isize) < 0 && (sys as isize) >= -256 {
  25. unsafe {
  26. errno = -(sys as isize) as c_int;
  27. }
  28. !0
  29. } else {
  30. sys
  31. }
  32. }
  33. pub struct Sys;
  34. impl Pal for Sys {
  35. fn no_pal(name: &str) -> c_int {
  36. let _ = writeln!(FileWriter(2), "relibc: no_pal: {}", name);
  37. unsafe {
  38. errno = ENOSYS;
  39. }
  40. -1
  41. }
  42. fn access(path: &CStr, mode: c_int) -> c_int {
  43. e(unsafe { syscall!(ACCESS, path.as_ptr(), mode) }) as c_int
  44. }
  45. fn brk(addr: *mut c_void) -> *mut c_void {
  46. unsafe { syscall!(BRK, addr) as *mut c_void }
  47. }
  48. fn chdir(path: &CStr) -> c_int {
  49. e(unsafe { syscall!(CHDIR, path.as_ptr()) }) as c_int
  50. }
  51. fn chmod(path: &CStr, mode: mode_t) -> c_int {
  52. e(unsafe { syscall!(FCHMODAT, AT_FDCWD, path.as_ptr(), mode, 0) }) as c_int
  53. }
  54. fn chown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
  55. e(unsafe {
  56. syscall!(
  57. FCHOWNAT,
  58. AT_FDCWD,
  59. path.as_ptr(),
  60. owner as u32,
  61. group as u32
  62. )
  63. }) as c_int
  64. }
  65. fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
  66. e(unsafe { syscall!(CLOCK_GETTIME, clk_id, tp) }) as c_int
  67. }
  68. fn close(fildes: c_int) -> c_int {
  69. e(unsafe { syscall!(CLOSE, fildes) }) as c_int
  70. }
  71. fn dup(fildes: c_int) -> c_int {
  72. e(unsafe { syscall!(DUP, fildes) }) as c_int
  73. }
  74. fn dup2(fildes: c_int, fildes2: c_int) -> c_int {
  75. e(unsafe { syscall!(DUP3, fildes, fildes2, 0) }) as c_int
  76. }
  77. unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
  78. e(syscall!(EXECVE, path.as_ptr(), argv, envp)) as c_int
  79. }
  80. fn exit(status: c_int) -> ! {
  81. unsafe {
  82. syscall!(EXIT, status);
  83. }
  84. loop {}
  85. }
  86. fn fchdir(fildes: c_int) -> c_int {
  87. e(unsafe { syscall!(FCHDIR, fildes) }) as c_int
  88. }
  89. fn fchmod(fildes: c_int, mode: mode_t) -> c_int {
  90. e(unsafe { syscall!(FCHMOD, fildes, mode) }) as c_int
  91. }
  92. fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
  93. e(unsafe { syscall!(FCHOWN, fildes, owner, group) }) as c_int
  94. }
  95. fn flock(fd: c_int, operation: c_int) -> c_int {
  96. e(unsafe { syscall!(FLOCK, fd, operation) }) as c_int
  97. }
  98. fn fstat(fildes: c_int, buf: *mut stat) -> c_int {
  99. let empty_cstr: *const c_char = unsafe { super::cstr_from_bytes_with_nul_unchecked(b"\0") };
  100. e(unsafe { syscall!(NEWFSTATAT, fildes, empty_cstr, buf, AT_EMPTY_PATH) }) as c_int
  101. }
  102. fn fcntl(fildes: c_int, cmd: c_int, arg: c_int) -> c_int {
  103. e(unsafe { syscall!(FCNTL, fildes, cmd, arg) }) as c_int
  104. }
  105. fn fork() -> pid_t {
  106. e(unsafe { syscall!(CLONE, SIGCHLD, 0) }) as pid_t
  107. }
  108. fn fsync(fildes: c_int) -> c_int {
  109. e(unsafe { syscall!(FSYNC, fildes) }) as c_int
  110. }
  111. fn ftruncate(fildes: c_int, length: off_t) -> c_int {
  112. e(unsafe { syscall!(FTRUNCATE, fildes, length) }) as c_int
  113. }
  114. fn futimens(fd: c_int, times: *const timespec) -> c_int {
  115. e(unsafe { syscall!(UTIMENSAT, fd, ptr::null::<c_char>(), times, 0) }) as c_int
  116. }
  117. fn utimens(path: &CStr, times: *const timespec) -> c_int {
  118. e(unsafe { syscall!(UTIMENSAT, AT_FDCWD, path.as_ptr(), times, 0) }) as c_int
  119. }
  120. fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
  121. if e(unsafe { syscall!(GETCWD, buf, size) }) == !0 {
  122. ptr::null_mut()
  123. } else {
  124. buf
  125. }
  126. }
  127. fn getdents(fd: c_int, dirents: *mut dirent, bytes: usize) -> c_int {
  128. unsafe { syscall!(GETDENTS64, fd, dirents, bytes) as c_int }
  129. }
  130. fn getegid() -> gid_t {
  131. e(unsafe { syscall!(GETEGID) }) as gid_t
  132. }
  133. fn geteuid() -> uid_t {
  134. e(unsafe { syscall!(GETEUID) }) as uid_t
  135. }
  136. fn getgid() -> gid_t {
  137. e(unsafe { syscall!(GETGID) }) as gid_t
  138. }
  139. fn getrusage(who: c_int, r_usage: *mut rusage) -> c_int {
  140. e(unsafe { syscall!(GETRUSAGE, who, r_usage) }) as c_int
  141. }
  142. unsafe fn gethostname(mut name: *mut c_char, len: size_t) -> c_int {
  143. // len only needs to be mutable on linux
  144. let mut len = len;
  145. let mut uts = mem::uninitialized();
  146. let err = Sys::uname(&mut uts);
  147. if err < 0 {
  148. mem::forget(uts);
  149. return err;
  150. }
  151. for c in uts.nodename.iter() {
  152. if len == 0 {
  153. break;
  154. }
  155. len -= 1;
  156. *name = *c;
  157. if *name == 0 {
  158. // We do want to copy the zero also, so we check this after the copying.
  159. break;
  160. }
  161. name = name.offset(1);
  162. }
  163. 0
  164. }
  165. fn getitimer(which: c_int, out: *mut itimerval) -> c_int {
  166. e(unsafe { syscall!(GETITIMER, which, out) }) as c_int
  167. }
  168. fn getpgid(pid: pid_t) -> pid_t {
  169. e(unsafe { syscall!(GETPGID, pid) }) as pid_t
  170. }
  171. fn getpid() -> pid_t {
  172. e(unsafe { syscall!(GETPID) }) as pid_t
  173. }
  174. fn getppid() -> pid_t {
  175. e(unsafe { syscall!(GETPPID) }) as pid_t
  176. }
  177. fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int {
  178. e(unsafe { syscall!(GETTIMEOFDAY, tp, tzp) }) as c_int
  179. }
  180. fn getuid() -> uid_t {
  181. e(unsafe { syscall!(GETUID) }) as uid_t
  182. }
  183. fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) -> c_int {
  184. // TODO: Somehow support varargs to syscall??
  185. e(unsafe { syscall!(IOCTL, fd, request, out) }) as c_int
  186. }
  187. fn isatty(fd: c_int) -> c_int {
  188. let mut winsize = winsize::default();
  189. (Self::ioctl(fd, TIOCGWINSZ, &mut winsize as *mut _ as *mut c_void) == 0) as c_int
  190. }
  191. fn link(path1: &CStr, path2: &CStr) -> c_int {
  192. e(unsafe {
  193. syscall!(
  194. LINKAT,
  195. AT_FDCWD,
  196. path1.as_ptr(),
  197. AT_FDCWD,
  198. path2.as_ptr(),
  199. 0
  200. )
  201. }) as c_int
  202. }
  203. fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
  204. e(unsafe { syscall!(LSEEK, fildes, offset, whence) }) as off_t
  205. }
  206. fn mkdir(path: &CStr, mode: mode_t) -> c_int {
  207. e(unsafe { syscall!(MKDIRAT, AT_FDCWD, path.as_ptr(), mode) }) as c_int
  208. }
  209. fn mkfifo(path: &CStr, mode: mode_t) -> c_int {
  210. e(unsafe { syscall!(MKNODAT, AT_FDCWD, path.as_ptr(), mode, 0) }) as c_int
  211. }
  212. unsafe fn mmap(
  213. addr: *mut c_void,
  214. len: usize,
  215. prot: c_int,
  216. flags: c_int,
  217. fildes: c_int,
  218. off: off_t,
  219. ) -> *mut c_void {
  220. e(syscall!(MMAP, addr, len, prot, flags, fildes, off)) as *mut c_void
  221. }
  222. unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int {
  223. e(syscall!(MUNMAP, addr, len)) as c_int
  224. }
  225. fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
  226. e(unsafe { syscall!(NANOSLEEP, rqtp, rmtp) }) as c_int
  227. }
  228. fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int {
  229. e(unsafe { syscall!(OPENAT, AT_FDCWD, path.as_ptr(), oflag, mode) }) as c_int
  230. }
  231. fn pipe(fildes: &mut [c_int]) -> c_int {
  232. e(unsafe { syscall!(PIPE2, fildes.as_mut_ptr(), 0) }) as c_int
  233. }
  234. fn read(fildes: c_int, buf: &mut [u8]) -> ssize_t {
  235. e(unsafe { syscall!(READ, fildes, buf.as_mut_ptr(), buf.len()) }) as ssize_t
  236. }
  237. fn rename(old: &CStr, new: &CStr) -> c_int {
  238. e(unsafe { syscall!(RENAMEAT, AT_FDCWD, old.as_ptr(), AT_FDCWD, new.as_ptr()) }) as c_int
  239. }
  240. fn rmdir(path: &CStr) -> c_int {
  241. e(unsafe { syscall!(UNLINKAT, AT_FDCWD, path.as_ptr(), AT_REMOVEDIR) }) as c_int
  242. }
  243. fn select(
  244. nfds: c_int,
  245. readfds: *mut fd_set,
  246. writefds: *mut fd_set,
  247. exceptfds: *mut fd_set,
  248. timeout: *mut timeval,
  249. ) -> c_int {
  250. e(unsafe { syscall!(SELECT, nfds, readfds, writefds, exceptfds, timeout) }) as c_int
  251. }
  252. fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> c_int {
  253. e(unsafe { syscall!(SETITIMER, which, new, old) }) as c_int
  254. }
  255. fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
  256. e(unsafe { syscall!(SETPGID, pid, pgid) }) as c_int
  257. }
  258. fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
  259. e(unsafe { syscall!(SETREGID, rgid, egid) }) as c_int
  260. }
  261. fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
  262. e(unsafe { syscall!(SETREUID, ruid, euid) }) as c_int
  263. }
  264. fn tcgetattr(fd: c_int, out: *mut termios) -> c_int {
  265. Self::ioctl(fd, TCGETS, out as *mut c_void)
  266. }
  267. fn tcsetattr(fd: c_int, act: c_int, value: *const termios) -> c_int {
  268. if act < 0 || act > 2 {
  269. unsafe {
  270. errno = EINVAL;
  271. }
  272. return -1;
  273. }
  274. // This is safe because ioctl shouldn't modify the value
  275. Self::ioctl(fd, TCSETS + act as c_ulong, value as *mut c_void)
  276. }
  277. fn times(out: *mut tms) -> clock_t {
  278. unsafe { syscall!(TIMES, out) as clock_t }
  279. }
  280. fn umask(mask: mode_t) -> mode_t {
  281. unsafe { syscall!(UMASK, mask) as mode_t }
  282. }
  283. fn uname(utsname: *mut utsname) -> c_int {
  284. e(unsafe { syscall!(UNAME, utsname, 0) }) as c_int
  285. }
  286. fn unlink(path: &CStr) -> c_int {
  287. e(unsafe { syscall!(UNLINKAT, AT_FDCWD, path.as_ptr(), 0) }) as c_int
  288. }
  289. fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
  290. e(unsafe { syscall!(WAIT4, pid, stat_loc, options, 0) }) as pid_t
  291. }
  292. fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
  293. e(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
  294. }
  295. }