From ccb29cfa2cfefa067817bbff3af74f4e9383f3a7 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Sun, 11 Mar 2018 16:50:46 -0700 Subject: [PATCH 1/7] wait and waitpid --- src/platform/src/linux/mod.rs | 4 ++++ src/platform/src/redox/mod.rs | 4 ++++ src/wait/src/lib.rs | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index 8915e6fea7..3f0ef34ede 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -159,6 +159,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) }) 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 e663a1b2da..4d98965beb 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -202,6 +202,10 @@ 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 { + e(syscall::waitpid(pid as usize, stat_loc as &mut usize, options as usize)) +} + pub fn write(fd: c_int, buf: &[u8]) -> ssize_t { e(syscall::write(fd as usize, buf)) as ssize_t } diff --git a/src/wait/src/lib.rs b/src/wait/src/lib.rs index 51fdd28569..c64108a58a 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(-1, 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) } From b35abd1065146ce8f161cfe3601b72f3322082a9 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Sun, 11 Mar 2018 21:20:59 -0700 Subject: [PATCH 2/7] test and fixes --- src/lib.rs | 1 + src/platform/src/linux/mod.rs | 2 +- src/platform/src/redox/mod.rs | 7 ++++++- src/resource/cbindgen.toml | 4 ++-- src/wait/src/lib.rs | 2 +- tests/.gitignore | 1 + tests/Makefile | 1 + tests/waitpid.c | 16 ++++++++++++++++ 8 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 tests/waitpid.c diff --git a/src/lib.rs b/src/lib.rs index 8becd7bd0d..a45c9d7115 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,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 3f0ef34ede..f26076e6c1 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -160,7 +160,7 @@ pub fn unlink(path: *const c_char) -> 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) }) as pid_t + e(unsafe { syscall!(WAIT4, pid, stat_loc, options, 0) }) as pid_t } pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t { diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 4d98965beb..a0b2c1d137 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -203,7 +203,12 @@ pub fn unlink(path: *const c_char) -> c_int { } pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { - e(syscall::waitpid(pid as usize, stat_loc as &mut usize, options as usize)) + unsafe { + let mut temp: usize = *stat_loc as usize; + let res = e(syscall::waitpid(pid as usize, &mut temp, options as usize)); + *stat_loc = temp as c_int; + res + } } pub fn write(fd: c_int, buf: &[u8]) -> ssize_t { diff --git a/src/resource/cbindgen.toml b/src/resource/cbindgen.toml index 788d105537..1b33a4e438 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 c64108a58a..2563e41d02 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 { - waitpid(-1, stat_loc, 0) + waitpid(0-1, stat_loc, 0) } #[no_mangle] diff --git a/tests/.gitignore b/tests/.gitignore index 6cfb8396be..0edf72a5dc 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -33,5 +33,6 @@ /string/strrchr /string/strspn /unlink +/waitpid /write diff --git a/tests/Makefile b/tests/Makefile index a252889605..e0bbb3fffb 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -28,6 +28,7 @@ EXPECT_BINS=\ string/strrchr \ string/strspn \ unlink \ + waitpid \ write # Binaries that may generate varied output diff --git a/tests/waitpid.c b/tests/waitpid.c new file mode 100644 index 0000000000..dc8fe52a61 --- /dev/null +++ b/tests/waitpid.c @@ -0,0 +1,16 @@ +#include +#include +#include + +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); + } +} From 211f95155a3aa4840b2b978a09a3f49100885ee9 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Sun, 11 Mar 2018 21:24:06 -0700 Subject: [PATCH 3/7] fmt --- src/wait/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wait/src/lib.rs b/src/wait/src/lib.rs index 2563e41d02..06d410cdd9 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 { - waitpid(0-1, stat_loc, 0) + waitpid(0 - 1, stat_loc, 0) } #[no_mangle] From d177b8e9747e92be6851b021f6a91d363ae578ad Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Tue, 13 Mar 2018 16:55:16 -0700 Subject: [PATCH 4/7] requested change --- src/wait/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wait/src/lib.rs b/src/wait/src/lib.rs index 06d410cdd9..88b2a56e81 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 { - waitpid(0 - 1, stat_loc, 0) + waitpid(!0, stat_loc, 0) } #[no_mangle] From 52acce0d346256f1ad3d0562a9a3a85531a9da75 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Wed, 14 Mar 2018 12:37:24 -0700 Subject: [PATCH 5/7] handle the null case --- src/platform/src/redox/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index a0b2c1d137..1fb185bb51 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -204,9 +204,11 @@ pub fn unlink(path: *const c_char) -> c_int { pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { unsafe { - let mut temp: usize = *stat_loc as usize; - let res = e(syscall::waitpid(pid as usize, &mut temp, options as usize)); - *stat_loc = temp as c_int; + let mut temp: usize = 0; + let mut res = e(syscall::waitpid(pid as usize, &mut temp, options as usize)); + if !stat_loc.is_null() { + *stat_loc = temp as c_int; + } res } } From cdfde8c0d43f5b1e4231be5ae03e0a85e4f3d538 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Thu, 15 Mar 2018 11:55:37 -0700 Subject: [PATCH 6/7] more requested changes --- src/platform/src/redox/mod.rs | 4 ++-- tests/waitpid.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 1fb185bb51..f63870866c 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -205,8 +205,8 @@ pub fn unlink(path: *const c_char) -> 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 mut res = e(syscall::waitpid(pid as usize, &mut temp, options as usize)); - if !stat_loc.is_null() { + 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 diff --git a/tests/waitpid.c b/tests/waitpid.c index dc8fe52a61..79f9829207 100644 --- a/tests/waitpid.c +++ b/tests/waitpid.c @@ -10,7 +10,7 @@ int main(int argc, char** argv) { exit(0); } else { // parent - int* stat_loc; - waitpid(pid, stat_loc, 0); + int stat_loc; + waitpid(pid, &stat_loc, 0); } } From c568ca2932c0bd8a395fec516c49781b515b4776 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Thu, 15 Mar 2018 16:18:35 -0700 Subject: [PATCH 7/7] test cleanup --- tests/waitpid.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/waitpid.c b/tests/waitpid.c index 79f9829207..c350e33c6f 100644 --- a/tests/waitpid.c +++ b/tests/waitpid.c @@ -5,12 +5,13 @@ 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); + // child + sleep(1); + exit(0); + } else { + // parent + int stat_loc; + waitpid(pid, &stat_loc, 0); } + return 0; }