2
0

lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //! unistd implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html
  2. #![no_std]
  3. #![cfg_attr(target_os = "redox", feature(alloc))]
  4. #[cfg(target_os = "redox")]
  5. extern crate alloc;
  6. extern crate errno;
  7. extern crate platform;
  8. extern crate stdio;
  9. extern crate string;
  10. extern crate sys_utsname;
  11. use core::ptr;
  12. use platform::types::*;
  13. pub use brk::*;
  14. pub use getopt::*;
  15. pub use pathconf::*;
  16. mod brk;
  17. mod getopt;
  18. mod pathconf;
  19. pub const R_OK: c_int = 1;
  20. pub const W_OK: c_int = 2;
  21. pub const X_OK: c_int = 4;
  22. pub const F_OK: c_int = 8;
  23. pub const SEEK_SET: c_int = 0;
  24. pub const SEEK_CUR: c_int = 1;
  25. pub const SEEK_END: c_int = 2;
  26. pub const F_ULOCK: c_int = 0;
  27. pub const F_LOCK: c_int = 1;
  28. pub const F_TLOCK: c_int = 2;
  29. pub const F_TEST: c_int = 3;
  30. pub const STDIN_FILENO: c_int = 0;
  31. pub const STDOUT_FILENO: c_int = 1;
  32. pub const STDERR_FILENO: c_int = 2;
  33. #[no_mangle]
  34. pub static mut environ: *mut *mut c_char = ptr::null_mut();
  35. #[no_mangle]
  36. pub extern "C" fn _exit(status: c_int) {
  37. platform::exit(status)
  38. }
  39. #[no_mangle]
  40. pub extern "C" fn access(path: *const c_char, amode: c_int) -> c_int {
  41. unimplemented!();
  42. }
  43. #[no_mangle]
  44. pub extern "C" fn alarm(seconds: c_uint) -> c_uint {
  45. unimplemented!();
  46. }
  47. #[no_mangle]
  48. pub extern "C" fn chdir(path: *const c_char) -> c_int {
  49. platform::chdir(path)
  50. }
  51. #[no_mangle]
  52. pub extern "C" fn chroot(path: *const c_char) -> c_int {
  53. unimplemented!();
  54. }
  55. #[no_mangle]
  56. pub extern "C" fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
  57. platform::chown(path, owner, group)
  58. }
  59. #[no_mangle]
  60. pub extern "C" fn close(fildes: c_int) -> c_int {
  61. platform::close(fildes)
  62. }
  63. #[no_mangle]
  64. pub extern "C" fn confstr(name: c_int, buf: *mut c_char, len: size_t) -> size_t {
  65. unimplemented!();
  66. }
  67. #[no_mangle]
  68. pub extern "C" fn crypt(key: *const c_char, salt: *const c_char) -> *mut c_char {
  69. unimplemented!();
  70. }
  71. #[no_mangle]
  72. pub extern "C" fn dup(fildes: c_int) -> c_int {
  73. platform::dup(fildes)
  74. }
  75. #[no_mangle]
  76. pub extern "C" fn dup2(fildes: c_int, fildes2: c_int) -> c_int {
  77. platform::dup2(fildes, fildes2)
  78. }
  79. #[no_mangle]
  80. pub extern "C" fn encrypt(block: [c_char; 64], edflag: c_int) {
  81. unimplemented!();
  82. }
  83. // #[no_mangle]
  84. // pub extern "C" fn execl(path: *const c_char, args: *const *mut c_char) -> c_int {
  85. // unimplemented!();
  86. // }
  87. // #[no_mangle]
  88. // pub extern "C" fn execle(
  89. // path: *const c_char,
  90. // args: *const *mut c_char,
  91. // envp: *const *mut c_char,
  92. // ) -> c_int {
  93. // unimplemented!();
  94. // }
  95. // #[no_mangle]
  96. // pub extern "C" fn execlp(file: *const c_char, args: *const *mut c_char) -> c_int {
  97. // unimplemented!();
  98. // }
  99. #[no_mangle]
  100. pub unsafe extern "C" fn execv(path: *const c_char, argv: *const *mut c_char) -> c_int {
  101. execve(path, argv, environ)
  102. }
  103. #[no_mangle]
  104. pub unsafe extern "C" fn execve(
  105. path: *const c_char,
  106. argv: *const *mut c_char,
  107. envp: *const *mut c_char,
  108. ) -> c_int {
  109. platform::execve(path, argv, envp)
  110. }
  111. #[no_mangle]
  112. pub extern "C" fn execvp(file: *const c_char, argv: *const *mut c_char) -> c_int {
  113. unimplemented!();
  114. }
  115. #[no_mangle]
  116. pub extern "C" fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
  117. platform::fchown(fildes, owner, group)
  118. }
  119. #[no_mangle]
  120. pub extern "C" fn fchdir(fildes: c_int) -> c_int {
  121. platform::fchdir(fildes)
  122. }
  123. #[no_mangle]
  124. pub extern "C" fn fdatasync(fildes: c_int) -> c_int {
  125. unimplemented!();
  126. }
  127. #[no_mangle]
  128. pub extern "C" fn fork() -> pid_t {
  129. platform::fork()
  130. }
  131. #[no_mangle]
  132. pub extern "C" fn fsync(fildes: c_int) -> c_int {
  133. platform::fsync(fildes)
  134. }
  135. #[no_mangle]
  136. pub extern "C" fn ftruncate(fildes: c_int, length: off_t) -> c_int {
  137. platform::ftruncate(fildes, length)
  138. }
  139. #[no_mangle]
  140. pub extern "C" fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
  141. platform::getcwd(buf, size)
  142. }
  143. #[no_mangle]
  144. pub extern "C" fn getdtablesize() -> c_int {
  145. unimplemented!();
  146. }
  147. #[no_mangle]
  148. pub extern "C" fn getegid() -> gid_t {
  149. platform::getegid()
  150. }
  151. #[no_mangle]
  152. pub extern "C" fn geteuid() -> uid_t {
  153. platform::geteuid()
  154. }
  155. #[no_mangle]
  156. pub extern "C" fn getgid() -> gid_t {
  157. platform::getgid()
  158. }
  159. #[no_mangle]
  160. pub extern "C" fn getgroups(gidsetsize: c_int, grouplist: *mut gid_t) -> c_int {
  161. unimplemented!();
  162. }
  163. #[no_mangle]
  164. pub extern "C" fn gethostid() -> c_long {
  165. unimplemented!();
  166. }
  167. #[no_mangle]
  168. pub unsafe extern "C" fn gethostname(mut name: *mut c_char, len: size_t) -> c_int {
  169. #[cfg(target_os = "linux")]
  170. {
  171. use core::mem;
  172. // len only needs to be mutable on linux
  173. let mut len = len;
  174. let mut uts: sys_utsname::utsname = mem::uninitialized();
  175. let err = sys_utsname::uname(&mut uts);
  176. if err < 0 {
  177. mem::forget(uts);
  178. return err;
  179. }
  180. for c in uts.nodename.iter() {
  181. if len == 0 {
  182. break;
  183. }
  184. len -= 1;
  185. *name = *c;
  186. if *name == 0 {
  187. // We do want to copy the zero also, so we check this after the copying.
  188. break;
  189. }
  190. name = name.offset(1);
  191. }
  192. }
  193. #[cfg(target_os = "redox")]
  194. {
  195. use platform::syscall::flag::*;
  196. use platform::{e, FileReader, Read};
  197. let fd = e(platform::syscall::open("/etc/hostname", O_RDONLY)) as i32;
  198. if fd < 0 {
  199. return fd;
  200. }
  201. let mut reader = FileReader(fd);
  202. for _ in 0..len {
  203. if !reader.read_u8(&mut *(name as *mut u8)) {
  204. *name = 0;
  205. break;
  206. }
  207. name = name.offset(1);
  208. }
  209. }
  210. 0
  211. }
  212. #[no_mangle]
  213. pub extern "C" fn getlogin() -> *mut c_char {
  214. unimplemented!();
  215. }
  216. #[no_mangle]
  217. pub extern "C" fn getlogin_r(name: *mut c_char, namesize: size_t) -> c_int {
  218. unimplemented!();
  219. }
  220. #[no_mangle]
  221. pub extern "C" fn getpagesize() -> c_int {
  222. unimplemented!();
  223. }
  224. #[no_mangle]
  225. pub extern "C" fn getpass(prompt: *const c_char) -> *mut c_char {
  226. unimplemented!();
  227. }
  228. #[no_mangle]
  229. pub extern "C" fn getpgid(pid: pid_t) -> pid_t {
  230. platform::getpgid(pid)
  231. }
  232. #[no_mangle]
  233. pub extern "C" fn getpgrp() -> pid_t {
  234. platform::getpgid(platform::getpid())
  235. }
  236. #[no_mangle]
  237. pub extern "C" fn getpid() -> pid_t {
  238. platform::getpid()
  239. }
  240. #[no_mangle]
  241. pub extern "C" fn getppid() -> pid_t {
  242. platform::getppid()
  243. }
  244. #[no_mangle]
  245. pub extern "C" fn getsid(pid: pid_t) -> pid_t {
  246. unimplemented!();
  247. }
  248. #[no_mangle]
  249. pub extern "C" fn getuid() -> uid_t {
  250. platform::getuid()
  251. }
  252. #[no_mangle]
  253. pub extern "C" fn getwd(path_name: *mut c_char) -> *mut c_char {
  254. unimplemented!();
  255. }
  256. #[no_mangle]
  257. pub extern "C" fn isatty(fildes: c_int) -> c_int {
  258. unimplemented!();
  259. }
  260. #[no_mangle]
  261. pub extern "C" fn lchown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
  262. unimplemented!();
  263. }
  264. #[no_mangle]
  265. pub extern "C" fn link(path1: *const c_char, path2: *const c_char) -> c_int {
  266. platform::link(path1, path2)
  267. }
  268. #[no_mangle]
  269. pub extern "C" fn lockf(fildes: c_int, function: c_int, size: off_t) -> c_int {
  270. unimplemented!();
  271. }
  272. #[no_mangle]
  273. pub extern "C" fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
  274. unimplemented!();
  275. }
  276. #[no_mangle]
  277. pub extern "C" fn nice(incr: c_int) -> c_int {
  278. unimplemented!();
  279. }
  280. #[no_mangle]
  281. pub extern "C" fn pause() -> c_int {
  282. unimplemented!();
  283. }
  284. #[no_mangle]
  285. pub extern "C" fn pipe(fildes: [c_int; 2]) -> c_int {
  286. platform::pipe(fildes)
  287. }
  288. #[no_mangle]
  289. pub extern "C" fn pread(fildes: c_int, buf: *mut c_void, nbyte: size_t, offset: off_t) -> ssize_t {
  290. unimplemented!();
  291. }
  292. #[no_mangle]
  293. pub extern "C" fn pthread_atfork(
  294. prepare: Option<extern "C" fn()>,
  295. parent: Option<extern "C" fn()>,
  296. child: Option<extern "C" fn()>,
  297. ) -> c_int {
  298. unimplemented!();
  299. }
  300. #[no_mangle]
  301. pub extern "C" fn pwrite(
  302. fildes: c_int,
  303. buf: *const c_void,
  304. nbyte: size_t,
  305. offset: off_t,
  306. ) -> ssize_t {
  307. unimplemented!();
  308. }
  309. #[no_mangle]
  310. pub extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
  311. use core::slice;
  312. let buf = unsafe { slice::from_raw_parts_mut(buf as *mut u8, nbyte as usize) };
  313. platform::read(fildes, buf)
  314. }
  315. #[no_mangle]
  316. pub extern "C" fn readlink(path: *const c_char, buf: *mut c_char, bufsize: size_t) -> c_int {
  317. unimplemented!();
  318. }
  319. #[no_mangle]
  320. pub extern "C" fn rmdir(path: *const c_char) -> c_int {
  321. platform::rmdir(path)
  322. }
  323. #[no_mangle]
  324. pub extern "C" fn setgid(gid: gid_t) -> c_int {
  325. platform::setregid(gid, gid)
  326. }
  327. #[no_mangle]
  328. pub extern "C" fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
  329. platform::setpgid(pid, pgid)
  330. }
  331. #[no_mangle]
  332. pub extern "C" fn setpgrp() -> pid_t {
  333. unimplemented!();
  334. }
  335. #[no_mangle]
  336. pub extern "C" fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
  337. platform::setregid(rgid, egid)
  338. }
  339. #[no_mangle]
  340. pub extern "C" fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
  341. platform::setreuid(ruid, euid)
  342. }
  343. #[no_mangle]
  344. pub extern "C" fn setsid() -> pid_t {
  345. unimplemented!();
  346. }
  347. #[no_mangle]
  348. pub extern "C" fn setuid(uid: uid_t) -> c_int {
  349. platform::setreuid(uid, uid)
  350. }
  351. #[no_mangle]
  352. pub extern "C" fn sleep(seconds: c_uint) -> c_uint {
  353. let rqtp = timespec {
  354. tv_sec: seconds as i64,
  355. tv_nsec: 0,
  356. };
  357. let rmtp = ptr::null_mut();
  358. platform::nanosleep(&rqtp, rmtp);
  359. 0
  360. }
  361. #[no_mangle]
  362. pub extern "C" fn swab(src: *const c_void, dest: *mut c_void, nbytes: ssize_t) {
  363. unimplemented!();
  364. }
  365. #[no_mangle]
  366. pub extern "C" fn symlink(path1: *const c_char, path2: *const c_char) -> c_int {
  367. unimplemented!();
  368. }
  369. #[no_mangle]
  370. pub extern "C" fn sync() {
  371. unimplemented!();
  372. }
  373. #[no_mangle]
  374. pub extern "C" fn sysconf(name: c_int) -> c_long {
  375. unimplemented!();
  376. }
  377. #[no_mangle]
  378. pub extern "C" fn tcgetpgrp() -> pid_t {
  379. unimplemented!();
  380. }
  381. #[no_mangle]
  382. pub extern "C" fn tcsetpgrp(fildes: c_int, pgid_id: pid_t) -> c_int {
  383. unimplemented!();
  384. }
  385. #[no_mangle]
  386. pub extern "C" fn truncate(path: *const c_char, length: off_t) -> c_int {
  387. unimplemented!();
  388. }
  389. #[no_mangle]
  390. pub extern "C" fn ttyname(fildes: c_int) -> *mut c_char {
  391. unimplemented!();
  392. }
  393. #[no_mangle]
  394. pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t) -> c_int {
  395. unimplemented!();
  396. }
  397. #[no_mangle]
  398. pub extern "C" fn ualarm(useconds: useconds_t, interval: useconds_t) -> useconds_t {
  399. unimplemented!();
  400. }
  401. #[no_mangle]
  402. pub extern "C" fn unlink(path: *const c_char) -> c_int {
  403. platform::unlink(path)
  404. }
  405. #[no_mangle]
  406. pub extern "C" fn usleep(useconds: useconds_t) -> c_int {
  407. let rqtp = timespec {
  408. tv_sec: 0,
  409. tv_nsec: (useconds * 1000).into(),
  410. };
  411. let rmtp = ptr::null_mut();
  412. platform::nanosleep(&rqtp, rmtp)
  413. }
  414. #[no_mangle]
  415. pub extern "C" fn vfork() -> pid_t {
  416. unimplemented!();
  417. }
  418. #[no_mangle]
  419. pub extern "C" fn write(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
  420. use core::slice;
  421. let buf = unsafe { slice::from_raw_parts(buf as *const u8, nbyte as usize) };
  422. platform::write(fildes, buf)
  423. }
  424. /*
  425. #[no_mangle]
  426. pub extern "C" fn func(args) -> c_int {
  427. unimplemented!();
  428. }
  429. */