From 62fab28b4e632802e0a99458bda442d427a14f73 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:23:44 +0100 Subject: [PATCH] Sys: spawn finds its program first, keepIgnored, fds with tty, waitExit --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 6e4bd5d..466c406 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -229,6 +229,7 @@ foreground*: BOOL; (* its group takes the terminal on standard input *) tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) more*: []INT; (* fds above 2 passed on to it, at the same numbers *) + keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *) end; Status* = (running, exited, killed, stopped); Mode* = record ... end; (* a terminal's modes; its fields are the target's *) @@ -306,6 +307,7 @@ proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *) proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) +proc waitExit*(pid: INT): (INT, Status, INT); (* as wait, but only for a program to end *) proc kill*(pid, signal: INT): INT; proc pid*: INT; (* this program's process id *) proc group*: INT; (* the process group this program is in *) @@ -438,7 +440,12 @@ looked for as a shell looks for a command, and as C's `execvp` does: in each directory of this program's `PATH` in turn, an empty one being the working directory, or of `/bin:/usr/bin` if `PATH` is unset or -empty; the first that runs is the program. Its environment is this +empty; the first that runs is the program. If there is none, `spawn` +says so, -2 (ENOENT) or -13 (EACCES), before it makes a child: no +program is started, and no process id given for a program that was +never there. A `p.path` with a `/` is not looked at first, and a +program that is there but that the kernel then refuses to run (not a +program, a bad interpreter) is a child that ends with status 127. Its environment is this program's with each `NAME=value` of `p.env` added or put in place of the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in `p.dir`. Its standard input, output and error are `p.fds`; a 0 gives @@ -446,10 +453,13 @@ `ls` as a shell runs it, with every other field zero. It gets no other open file but those of `p.more`, each at its own number, as a shell passes `3 0 then return SYSCALL(80, ADR(dir[0])); end; (* chdir *) return 0; end; +(* Whether this program ignores the signal sig: SIG_IGN is 1. *) +proc ignored(sig: INT): BOOL; +var act: [4]U64; (* struct sigaction *) +begin + _ := SYSCALL(13, sig, 0, ADR(act), 8); (* rt_sigaction *) + return act[0] = 1; +end; + (* Starts the program p describes, found as run says: its process id, - or minus an errno. A program that can't be started ends with status - 127. Every signal is blocked from the fork until the program has its - own: its group, its terminal's foreground, and signals as they are - at first. *) + or minus an errno: -2 (ENOENT) or -13 (EACCES) as exec says, before + the fork, if there is no program to run. A program found that + execve then refuses ends with status 127. Every signal is blocked + from the fork until the program has its own: its group, its + terminal's foreground, and signals as they are at first. *) proc spawn*(p: Program): INT; var a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; (* struct sigaction: SIG_DFL *) - argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, i, g: INT; search: []U8; + argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, i, g, r: INT; search: []U8; begin if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *) @@ -1715,13 +1725,17 @@ if argv = nil then return -12; end; (* ENOMEM *) search := env("PATH"); (* read here, before the fork *) if LEN(search) = 0 then search := defaultPath; end; + r := find(p.path, search, a); + if r < 0 then munmap(^U8(argv), size); return r; end; all := 0FFFFFFFFFFFFFFFFH; _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) pid := SYSCALL(57); (* fork *) if pid = 0 then if takeOn(p, name, dir) # 0 then _ := SYSCALL(231, 127); end; (* exit *) closeAllBut(p.more); - for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *) + for i := 1 to 64 do + if not (p.keepIgnored and ignored(i)) then _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *) + end; _ := SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *) run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); _ := SYSCALL(231, 127); (* exit *) @@ -1773,7 +1787,10 @@ gives them, each kept to give back if execve fails *) all := 0FFFFFFFFFFFFFFFFH; _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) - for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), ADR(acts[i]), 8); end; (* rt_sigaction *) + for i := 1 to 64 do + _ := SYSCALL(13, i, 0, ADR(acts[i]), 8); (* rt_sigaction: as it is *) + if not (p.keepIgnored and (acts[i][0] = 1)) then _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; + end; _ := SYSCALL(14, 2, ADR(empty), 0, 8); r := SYSCALL(59, ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *) _ := SYSCALL(14, 2, ADR(all), 0, 8); @@ -1801,23 +1818,38 @@ return r; end; +(* wait4 with options, its status read as C's macros read it. *) +proc waitFor(pid, options: INT): (INT, Status, INT); +var status: I32; r: INT; +begin + r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *) + if r <= 0 then return r, running, 0; end; + if status & 7FH = 0 then return r, exited, INT(status) shr 8 & 0FFH; end; + if status & 0FFH = 7FH then return r, stopped, INT(status) shr 8 & 0FFH; end; + return r, killed, INT(status) & 7FH; +end; + (* Waits for the program pid, any of this program's if pid is -1, or any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with its exit status; killed or stopped, with the signal's number. Without block it doesn't wait: 0 and running if none has changed. Minus an errno, and running, if there is none to wait for. *) proc wait*(pid: INT; block: BOOL): (INT, Status, INT); -var status: I32; r, options: INT; +var options: INT; begin options := 3; (* WNOHANG, WUNTRACED *) if block then options := 2; end; - r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *) - if r <= 0 then return r, running, 0; end; - if status & 7FH = 0 then return r, exited, INT(status) shr 8 & 0FFH; end; - if status & 0FFH = 7FH then return r, stopped, INT(status) shr 8 & 0FFH; end; - return r, killed, INT(status) & 7FH; + return waitFor(pid, options); end; +(* Waits for the program pid as wait does, but only for one to end: + a stopped program is not waited for, so the status is exited or + killed, or running if there is none to wait for. *) +proc waitExit*(pid: INT): (INT, Status, INT); +begin + return waitFor(pid, 0); +end; + (* Sends the program pid, or the group -pid, the signal: 0, or minus an errno. *) proc kill*(pid, signal: INT): INT; @@ -1934,7 +1966,9 @@ terminal's foreground until it ends with status 3; one in a directory and an environment of its own writes to a pipe; one in a group of its own is stopped and killed through it; one that isn't - there ends with 127. Then signals: caught as a program ends and as + there is no child, and one found that execve refuses ends with 127; + ignored signals stay so only with keepIgnored; fds replace a + terminal's files. Then signals: caught as a program ends and as one is sent; and a program starts with them neither caught nor ignored. *) proc testPrograms; @@ -2002,9 +2036,33 @@ child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]}); r, st, c := wait(child, true); if (r # child) or (st # exited) or (c # 4) then exit(2); end; - child := spawn({path: "no-such-program-on-path", args: ["x"]}); - r, st, c := wait(child, true); - if (r # child) or (st # exited) or (c # 127) then exit(2); end; + (* one not on PATH is no child at all *) + if spawn({path: "no-such-program-on-path", args: ["x"]}) # -2 then exit(2); end; + r, st, c := wait(-1, false); + if r # -10 then exit(2); end; (* ECHILD: none was made *) + (* an ignored signal stays so only with keepIgnored *) + if ignore(sigInt, true) # 0 then exit(2); end; + child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"], keepIgnored: true}); + r, st, c := waitExit(child); + if (r # child) or (st # exited) or (c # 6) then exit(2); end; + child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"]}); + r, st, c := waitExit(child); + if (r # child) or (st # killed) or (c # sigInt) then exit(2); end; + if ignore(sigInt, false) # 0 then exit(2); end; + (* fds replace the terminal's files, with tty *) + pty := openPty(); + r, w := pipe(); + child := spawn({path: "sh", args: ["sh", "-c", "echo out; echo err >&2"], tty: pty, fds: [0, w, 0]}); + close(w); + n := 0; + repeat + m := read(r, buf[n..LEN(buf)]); + if m > 0 then INC(n, m); end; + until m <= 0; + close(r); + r, st, c := waitExit(child); + close(pty); + if (buf[0..n] # "out\n") or (r # child) or (st # exited) or (c # 0) then exit(2); end; sigs := catch([sigChld, sigHup]); if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([11]) # -22) or (catch([sigKill]) # -22) then exit(2); end; diff --git a/lib/Sys.raz b/lib/Sys.raz index c1be9dc..07ed15f 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -97,6 +97,7 @@ foreground*: BOOL; (* its group gets the terminal on standard input *) tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) more*: []INT; (* fds above 2 passed on to it at the same numbers *) + keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *) end; (* How a program that wait waited for changed. *) Status* = (running, exited, killed, stopped); @@ -1554,9 +1555,9 @@ (* What p asks of the program before it runs, done to this one: a session on the terminal name, if p.tty, or else its group, its - terminal's foreground, and its standard input, output and error; - then its working directory, the C string dir. 0, or minus an - errno. *) + terminal's foreground; then its standard input, output and error, + each p.fds names over the terminal's; then its working directory, + the C string dir. 0, or minus an errno. *) proc takeOn(p: Program; name, dir: []U8): INT; var fd: I32; i: INT; begin @@ -1571,19 +1572,36 @@ elsif p.group # 0 then _ := C.setpgid(0, I32(p.group)); end; if p.foreground then _ := C.tcsetpgrp(0, C.getpgrp()); end; - for i := 0 to 2 do - if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end; - end; + end; + for i := 0 to 2 do + if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end; end; if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then return failed(); end; return 0; +end; + +(* Whether the struct sigaction a says SIG_IGN. *) +proc ignoredIn(var a: Sigaction): BOOL; +var h: ^UINT; +begin + h := ^UINT(^U8(ADR(a)) + CAbi.saHandlerAt); + return h^ = 1; +end; + +(* Whether this program ignores the signal sig. *) +proc ignored(sig: INT): BOOL; +var a: Sigaction; +begin + _ := C.sigaction(I32(sig), nil, ^U8(ADR(a))); + return ignoredIn(a); end; (* Starts the program p describes, found as run says: its process id, - or minus an errno. A program that can't be started ends with status - 127. Every signal is blocked from the fork until the program has its - own: its group, its terminal's foreground, and signals as they are - at first. *) + or minus an errno: -2 (ENOENT) or -13 (EACCES) as exec says, before + the fork, if there is no program to run. A program found that + execve then refuses ends with status 127. Every signal is blocked + from the fork until the program has its own: its group, its + terminal's foreground, and signals as they are at first. *) proc spawn*(p: Program): INT; var a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, err, i, g: INT; search: []U8; @@ -1603,13 +1621,17 @@ if argv = nil then return -12; end; (* ENOMEM *) search := env("PATH"); (* read here, before the fork *) if LEN(search) = 0 then search := defaultPath; end; + err := find(p.path, search, a); + if err < 0 then munmap(^U8(argv), size); return err; end; for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), ^U8(ADR(old))); pid := C.fork(); if pid = 0 then if takeOn(p, name, dir) # 0 then C._exit(127); end; closeAllBut(p.more); - for i := 1 to CAbi.sigMax do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end; + for i := 1 to CAbi.sigMax do + if not (p.keepIgnored and ignored(i)) then _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end; + end; _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil); run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); C._exit(127); @@ -1666,7 +1688,12 @@ gives them, each kept to give back if execve fails *) for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), ^U8(ADR(old))); - for i := 1 to CAbi.sigMax do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), ^U8(ADR(acts[i]))); end; + for i := 1 to CAbi.sigMax do + _ := C.sigaction(I32(i), nil, ^U8(ADR(acts[i]))); (* as it is *) + if not (p.keepIgnored and ignoredIn(acts[i])) then + _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); + end; + end; _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil); _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1)); r := failed(); @@ -1696,6 +1723,18 @@ return r; end; +(* waitpid with options, its status read as C's macros read it. *) +proc waitFor(pid: INT; options: I32): (INT, Status, INT); +var status, r: I32; +begin + r := C.waitpid(I32(pid), ADR(status), options); + if r < 0 then return failed(), running, 0; end; + if r = 0 then return 0, running, 0; end; + if status & 7FH = 0 then return INT(r), exited, INT(status) shr 8 & 0FFH; end; + if status & 0FFH = 7FH then return INT(r), stopped, INT(status) shr 8 & 0FFH; end; + return INT(r), killed, INT(status) & 7FH; +end; + (* Waits for the program pid, any of this program's if pid is -1, or any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with its exit status; killed or stopped, with the signal's number. Without @@ -1703,16 +1742,19 @@ errno, and running, if there is none to wait for. The status is read as C's macros read it, which every Unix encodes alike. *) proc wait*(pid: INT; block: BOOL): (INT, Status, INT); -var status, options, r: I32; +var options: I32; begin options := CAbi.wNohang | CAbi.wUntraced; if block then options := CAbi.wUntraced; end; - r := C.waitpid(I32(pid), ADR(status), options); - if r < 0 then return failed(), running, 0; end; - if r = 0 then return 0, running, 0; end; - if status & 7FH = 0 then return INT(r), exited, INT(status) shr 8 & 0FFH; end; - if status & 0FFH = 7FH then return INT(r), stopped, INT(status) shr 8 & 0FFH; end; - return INT(r), killed, INT(status) & 7FH; + return waitFor(pid, options); +end; + +(* Waits for the program pid as wait does, but only for one to end: + a stopped program is not waited for, so the status is exited or + killed, or running if there is none to wait for. *) +proc waitExit*(pid: INT): (INT, Status, INT); +begin + return waitFor(pid, 0); end; (* Sends the program pid, or the group -pid, the signal: 0, or minus an @@ -1860,7 +1902,9 @@ terminal's foreground until it ends with status 3; one in a directory and an environment of its own writes to a pipe; one in a group of its own is stopped and killed through it; one that isn't - there ends with 127. Then signals: caught as a program ends and as + there is no child, and one found that execve refuses ends with 127; + ignored signals stay so only with keepIgnored; fds replace a + terminal's files. Then signals: caught as a program ends and as one is sent; and a program starts with them neither caught nor ignored. *) proc testPrograms; @@ -1928,9 +1972,33 @@ child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]}); r, st, c := wait(child, true); if (r # child) or (st # exited) or (c # 4) then exit(2); end; - child := spawn({path: "no-such-program-on-path", args: ["x"]}); - r, st, c := wait(child, true); - if (r # child) or (st # exited) or (c # 127) then exit(2); end; + (* one not on PATH is no child at all *) + if spawn({path: "no-such-program-on-path", args: ["x"]}) # -2 then exit(2); end; + r, st, c := wait(-1, false); + if r # -10 then exit(2); end; (* ECHILD: none was made *) + (* an ignored signal stays so only with keepIgnored *) + if ignore(sigInt, true) # 0 then exit(2); end; + child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"], keepIgnored: true}); + r, st, c := waitExit(child); + if (r # child) or (st # exited) or (c # 6) then exit(2); end; + child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"]}); + r, st, c := waitExit(child); + if (r # child) or (st # killed) or (c # sigInt) then exit(2); end; + if ignore(sigInt, false) # 0 then exit(2); end; + (* fds replace the terminal's files, with tty *) + pty := openPty(); + r, w := pipe(); + child := spawn({path: "sh", args: ["sh", "-c", "echo out; echo err >&2"], tty: pty, fds: [0, w, 0]}); + close(w); + n := 0; + repeat + m := read(r, buf[n..LEN(buf)]); + if m > 0 then INC(n, m); end; + until m <= 0; + close(r); + r, st, c := waitExit(child); + close(pty); + if (buf[0..n] # "out\n") or (r # child) or (st # exited) or (c # 0) then exit(2); end; sigs := catch([sigChld, sigHup]); if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([CAbi.sigSegv]) # -22) or (catch([sigKill]) # -22) then exit(2); end;