Bladeren bron

Merge remote-tracking branch 'sajattack/unistd'

Jeremy Soller 7 jaren geleden
bovenliggende
commit
0bcabc2163
8 gewijzigde bestanden met toevoegingen van 66 en 3 verwijderingen
  1. 3 0
      src/platform/Cargo.toml
  2. 2 0
      src/platform/src/lib.rs
  3. 12 0
      src/platform/src/linux/mod.rs
  4. 19 0
      src/platform/src/redox/mod.rs
  5. 5 3
      src/unistd/src/lib.rs
  6. 1 0
      tests/.gitignore
  7. 1 0
      tests/Makefile
  8. 23 0
      tests/pipe.c

+ 3 - 0
src/platform/Cargo.toml

@@ -8,3 +8,6 @@ sc = "0.2"
 
 [target.'cfg(target_os = "redox")'.dependencies]
 redox_syscall = "0.1"
+
+[dependencies]
+alloc-no-stdlib = "1.2"

+ 2 - 0
src/platform/src/lib.rs

@@ -3,6 +3,8 @@
 #![no_std]
 #![allow(non_camel_case_types)]
 //TODO #![feature(thread_local)]
+#![feature(alloc)]
+extern crate alloc;
 
 #[cfg(all(not(feature = "no_std"), target_os = "linux"))]
 #[macro_use]

+ 12 - 0
src/platform/src/linux/mod.rs

@@ -62,6 +62,10 @@ pub fn fchdir(fildes: c_int) -> c_int {
     e(unsafe { syscall!(FCHDIR, fildes) }) as c_int
 }
 
+pub fn fork() -> pid_t {
+    e(unsafe {syscall!(FORK) }) as pid_t
+}
+
 pub fn fsync(fildes: c_int) -> c_int {
     e(unsafe { syscall!(FSYNC, fildes) }) as c_int
 }
@@ -114,6 +118,14 @@ pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
     e(unsafe { syscall!(OPENAT, AT_FDCWD, path, oflag, mode) }) as c_int
 }
 
+pub fn pipe(fildes: [c_int; 2]) -> c_int {
+    e(unsafe { syscall!(PIPE2, fildes.as_ptr(), 0) }) as c_int
+}
+
+pub fn read(fildes: c_int, buf: &[u8]) -> ssize_t {
+    e(unsafe { syscall!(READ, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
+}
+
 pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
     e(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
 }

+ 19 - 0
src/platform/src/redox/mod.rs

@@ -1,5 +1,8 @@
+extern crate alloc;
+
 use core::ptr;
 use core::slice;
+use alloc::Vec;
 use syscall;
 
 use c_str;
@@ -63,6 +66,10 @@ pub fn fchdir(fd: c_int) -> c_int {
     }
 }
 
+pub fn fork() -> pid_t {
+   e(unsafe { syscall::clone(0) }) as pid_t
+}
+
 pub fn fsync(fd: c_int) -> c_int {
     e(syscall::fsync(fd as usize)) as c_int
 }
@@ -119,6 +126,18 @@ pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
     e(syscall::open(path, (oflag as usize) | (mode as usize))) as c_int
 }
 
+pub fn pipe(fds: [c_int; 2]) -> c_int {
+    let usize_vec = fds.iter().map(|x| *x as usize).collect::<Vec<usize>>();
+    let usize_slice = usize_vec.as_slice();
+    let mut usize_arr: [usize; 2] = Default::default();
+    usize_arr.copy_from_slice(usize_slice);
+    e(syscall::pipe2(&mut usize_arr, 0)) as c_int
+}
+
+pub fn read(fd: c_int, buf: &mut [u8]) -> ssize_t {
+    e(syscall::read(fd as usize, buf)) as ssize_t
+}
+
 pub fn write(fd: c_int, buf: &[u8]) -> ssize_t {
     e(syscall::write(fd as usize, buf)) as ssize_t
 }

+ 5 - 3
src/unistd/src/lib.rs

@@ -140,7 +140,7 @@ pub extern "C" fn fdatasync(fildes: c_int) -> c_int {
 
 #[no_mangle]
 pub extern "C" fn fork() -> pid_t {
-    unimplemented!();
+    platform::fork()
 }
 
 #[no_mangle]
@@ -295,7 +295,7 @@ pub extern "C" fn pause() -> c_int {
 
 #[no_mangle]
 pub extern "C" fn pipe(fildes: [c_int; 2]) -> c_int {
-    unimplemented!();
+    platform::pipe(fildes)
 }
 
 #[no_mangle]
@@ -324,7 +324,9 @@ pub extern "C" fn pwrite(
 
 #[no_mangle]
 pub extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
-    unimplemented!();
+    use core::slice;
+    let buf = unsafe { slice::from_raw_parts_mut(buf as *mut u8, nbyte as usize) };
+    platform::read(fildes, buf)
 }
 
 #[no_mangle]

+ 1 - 0
tests/.gitignore

@@ -17,5 +17,6 @@
 /link
 /link.out
 /math
+/pipe
 /printf
 /write

+ 1 - 0
tests/Makefile

@@ -14,6 +14,7 @@ BINS=\
 	getid \
 	link \
 	math \
+	pipe \
 	printf \
 	write
 

+ 23 - 0
tests/pipe.c

@@ -0,0 +1,23 @@
+//http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html
+#include <unistd.h>
+
+int main()
+{
+
+    int pid, pip[2];
+    char instring[20];
+
+    pipe(pip); 
+
+    pid = fork();
+    if (pid == 0)           /* child : sends message to parent*/
+    {
+        /* send 7 characters in the string, including end-of-string */
+        write(pip[1], "Hi Mom!", 7);  
+    }
+    else			/* parent : receives message from child */
+    {
+        /* read from the pipe */
+        read(pip[0], instring, 7);
+    }
+}