mod.rs 11 KB

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