commit 7e8a3466c36d1d53856f85e59d5f503eec1a6943
parent 4380d79c06d7a596d762b121b0d329c791569466
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date: Fri, 25 Feb 2022 01:46:13 +0100
implement os::exec::peek{,any}
Signed-off-by: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Diffstat:
2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/os/exec/process+freebsd.ha b/os/exec/process+freebsd.ha
@@ -111,7 +111,7 @@ export fn waitall() (uint | error | !(process, exit_status)) = {
export fn peek(proc: *process) (status | void | error) = {
let ru: rt::rusage = rt::rusage { ... };
let st: status = status { ... };
- match (rt::wait4(*proc, &st.status, 0, &ru)) {
+ match (rt::wait4(*proc, &st.status, rt::WNOHANG, &ru)) {
case let err: rt::errno =>
return errors::errno(err);
case let pid: int =>
@@ -126,6 +126,24 @@ export fn peek(proc: *process) (status | void | error) = {
return st;
};
+// Waits for the first child process to complete, then returns its process info
+// and status
+export fn peekany() ((process, status) | void | error) = {
+ let ru: rt::rusage = rt::rusage { ... };
+ let st: status = status { ... };
+ match (rt::wait4(-1, &st.status, rt::WNOHANG, &ru)) {
+ case let err: rt::errno =>
+ return errors::errno(err);
+ case let pid: int =>
+ switch (pid) {
+ case 0 =>
+ return;
+ case =>
+ return (pid, st);
+ };
+ };
+};
+
// The exit status code of a process.
export type exited = int;
diff --git a/os/exec/process+linux.ha b/os/exec/process+linux.ha
@@ -97,7 +97,7 @@ export fn waitall() (uint | error | !(process, exit_status)) = {
export fn peek(proc: *process) (status | void | error) = {
let ru: rt::rusage = rt::rusage { ... };
let st: status = status { ... };
- match (rt::wait4(*proc, &st.status, 0, &ru)) {
+ match (rt::wait4(*proc, &st.status, rt::WNOHANG, &ru)) {
case let err: rt::errno =>
return errors::errno(err);
case let pid: int =>
@@ -112,6 +112,24 @@ export fn peek(proc: *process) (status | void | error) = {
return st;
};
+// Waits for the first child process to complete, then returns its process info
+// and status
+export fn peekany() ((process, status) | void | error) = {
+ let ru: rt::rusage = rt::rusage { ... };
+ let st: status = status { ... };
+ match (rt::wait4(-1, &st.status, rt::WNOHANG, &ru)) {
+ case let err: rt::errno =>
+ return errors::errno(err);
+ case let pid: int =>
+ switch (pid) {
+ case 0 =>
+ return;
+ case =>
+ return (pid, st);
+ };
+ };
+};
+
// The exit status code of a process.
export type exited = int;