diff --git a/src/lib.rs b/src/lib.rs index b51ef10c08ec9ef92f1b16609d2648fe8f60b369..a2589ae7954e34050a55f4d29a63d6e4135dc36f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ extern crate string; extern crate sys_time; extern crate time; extern crate unistd; +extern crate wait; extern crate wctype; #[lang = "eh_personality"] diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index 6868ac3af25baeb815fcd1cd7d92cf6583544008..44d7ed2cfb8dcecfe9d5f972e53409aa3f9a9799 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -163,6 +163,10 @@ pub fn unlink(path: *const c_char) -> c_int { e(unsafe { syscall!(UNLINKAT, AT_FDCWD, path, 0) }) as c_int } +pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { + e(unsafe { syscall!(WAIT4, pid, stat_loc, options, 0) }) as pid_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 869e52b42ed94cdeff3d17ca50d3de00e8e7bc5a..dc671ef3767084f68948b78139e43b7c7c6a1699 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -206,6 +206,17 @@ pub fn unlink(path: *const c_char) -> c_int { e(syscall::unlink(path)) as c_int } +pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { + unsafe { + let mut temp: usize = 0; + let res = e(syscall::waitpid(pid as usize, &mut temp, options as usize)); + if !stat_loc.is_null() { + *stat_loc = temp as c_int; + } + res + } +} + pub fn write(fd: c_int, buf: &[u8]) -> ssize_t { e(syscall::write(fd as usize, buf)) as ssize_t } diff --git a/src/resource/cbindgen.toml b/src/resource/cbindgen.toml index 788d105537cead2894f82a9ca6f85657dc1065da..1b33a4e4383d52da0b59effa2dc33a7ff4b46b05 100644 --- a/src/resource/cbindgen.toml +++ b/src/resource/cbindgen.toml @@ -1,7 +1,7 @@ -sys_includes = ["sys/types.h", "sys/time.h"] +sys_includes = ["sys/types.h", "stdint.h", "sys/time.h"] include_guard = "_SYS_RESOURCE_H" language = "C" -style = "Tag" +style = "Both" [enum] prefix_with_name = true diff --git a/src/wait/src/lib.rs b/src/wait/src/lib.rs index 51fdd28569e516767de612a1201b98fd5132eb90..88b2a56e81deff5f98d89c84034237bd15a39773 100644 --- a/src/wait/src/lib.rs +++ b/src/wait/src/lib.rs @@ -11,7 +11,7 @@ use resource::rusage; #[no_mangle] pub unsafe extern "C" fn wait(stat_loc: *mut c_int) -> pid_t { - unimplemented!(); + waitpid(!0, stat_loc, 0) } #[no_mangle] @@ -39,5 +39,5 @@ pub unsafe extern "C" fn wait3( #[no_mangle] pub unsafe extern "C" fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { - unimplemented!(); + platform::waitpid(pid, stat_loc, options) } diff --git a/tests/.gitignore b/tests/.gitignore index 64317b6cbec44deb707b355235417717a4511f1b..685d52e548000dea037e8637e02dbb1a634eb524 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -37,5 +37,6 @@ /string/strrchr /string/strspn /unlink +/waitpid /write diff --git a/tests/Makefile b/tests/Makefile index 92e3f70f46b2fa7a4859512b8eba477c0c9b2d78..120530f22ee9541172f25a62f0d705ca9b1feaeb 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -35,6 +35,7 @@ EXPECT_BINS=\ string/strstr \ string/strpbrk \ unlink \ + waitpid \ write # Binaries that may generate varied output diff --git a/tests/waitpid.c b/tests/waitpid.c new file mode 100644 index 0000000000000000000000000000000000000000..c350e33c6f9a65a834445c1107aa7befa69398a0 --- /dev/null +++ b/tests/waitpid.c @@ -0,0 +1,17 @@ +#include <sys/wait.h> +#include <unistd.h> +#include <stdlib.h> + +int main(int argc, char** argv) { + pid_t pid = fork(); + if (pid == 0) { + // child + sleep(1); + exit(0); + } else { + // parent + int stat_loc; + waitpid(pid, &stat_loc, 0); + } + return 0; +}