---------------------------------------------------------------------------
UNSAFE MODULE---------------------------------------------------------------------------SystemPosix EXPORTSSystem ; IMPORT Text, Ctypes, (*Uexec,*) Process, Fmt, Uerror; IMPORT (*SchedulerPosix*) Word;
PROCEDUREHostname () : TEXT = VAR buf : ARRAY [0..1024] OF CHAR; len := 1024; BEGIN IF gethostname(LOOPHOLE(ADR(buf), Ctypes.char_star), len) = 0 THEN buf[1024] := '\000'; len := 0; WHILE len < 1024 AND buf[len] # '\000' DO INC(len); END; RETURN Text.FromChars(SUBARRAY(buf, 0, len)); END; RETURN "amnesiac"; END Hostname; PROCEDUREWait (p: Process.T): Process.ExitCode RAISES {Error} = VAR result, status: Ctypes.int; pid := Process.GetID(p); e : Ctypes.int; err : TEXT; BEGIN
Use this once m3core is new enough. result := SchedulerPosix.WaitProcess (pid, status);
(* 0 should be WNOHANG on user threads platforms, which there are presently none of *) result := (*Uexec.*)waitpid (pid, ADR(status), 0); IF result < 0 THEN e := GetErrno(); IF (e = Uerror.ECHILD) THEN err := "The process specified in pid does not exist or is not a child of the calling process."; ELSIF (e = Uerror.EINTR) THEN err := "WNOHANG was not set and an unblocked signal or a SIGCHLD was caught."; ELSIF (e = Uerror.EINVAL) THEN err := "The options argument was invalid."; ELSE err := "Unexpected return value " & Fmt.Int(e); END; RAISE Error("Could not wait: " & err); END;
Use this once m3core is new enough. Uexec.RepackStatus(status);
(* ensure non-zero implies lower bits non-zero *) IF (status # 0) AND (Word.And(status, 16_FF) = 0) THEN status := 1; END; RETURN MIN(LAST(Process.ExitCode), status); END Wait; BEGIN END SystemPosix.