diff --git a/src/platform/Cargo.toml b/src/platform/Cargo.toml index e25b8251c0..a00eb79be6 100644 --- a/src/platform/Cargo.toml +++ b/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" diff --git a/src/platform/src/lib.rs b/src/platform/src/lib.rs index 5f57298db7..6359118c71 100644 --- a/src/platform/src/lib.rs +++ b/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] diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index a8d720939d..66fafabbb4 100644 --- a/src/platform/src/linux/mod.rs +++ b/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 } diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 057ad80ba0..e9fe6ddaff 100644 --- a/src/platform/src/redox/mod.rs +++ b/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::>(); + 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 } diff --git a/src/unistd/src/lib.rs b/src/unistd/src/lib.rs index 90811a6c7d..ea4609ba50 100644 --- a/src/unistd/src/lib.rs +++ b/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] diff --git a/tests/.gitignore b/tests/.gitignore index 76cf8f3e05..e5819ec545 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -17,5 +17,6 @@ /link /link.out /math +/pipe /printf /write diff --git a/tests/Makefile b/tests/Makefile index d8abbea971..b371961d99 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -14,6 +14,7 @@ BINS=\ getid \ link \ math \ + pipe \ printf \ write diff --git a/tests/pipe.c b/tests/pipe.c new file mode 100644 index 0000000000..0ad78bc3e5 --- /dev/null +++ b/tests/pipe.c @@ -0,0 +1,23 @@ +//http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html +#include + +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); + } +}