From 0fc0d46e4dcd418861692b7e4a3ae3f633011177 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 22:01:35 +0000 Subject: [PATCH] Sys.spawn: a name without a / is found along PATH What huh needs to run git and gh, by the owner's word. Library growth in both Sys files: a path with a / is run as before; a name without one is looked for in each directory of this program's PATH in turn, as execvp does, an empty one being the working directory, and /bin:/usr/bin if PATH is unset or empty. The search runs in the child, after the fork, in the buffer spawn already has, so it takes no memory there. Sys's test runs sh by its name, and a name on no directory of PATH ends with 127, as before; raz-lib.md says it. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 0351b69..6e1e9b3 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -336,11 +336,15 @@ terminal write it reads. `spawn` starts the program at `p.path`, with `p.args` as its -arguments, the first being its name. Its environment is this +arguments, the first being its name. A `p.path` without a `/` is +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 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 -it this program's own, so `{path: "/bin/ls", args: ["ls"]}` runs +it this program's own, so `{path: "ls", args: ["ls"]}` runs `ls` as a shell runs it, with every other field zero. It gets no other open file, every signal does what it does at first, and none is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 32580ea..eab234e 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -1060,14 +1060,51 @@ return INT(g); end; -(* Starts the program p describes: 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. *) +(* Where spawn looks for a program named without a /, if PATH is unset + or empty: glibc's execvp looks in these. *) +const defaultPath = "/bin:/usr/bin"; + +(* Runs the program at name, as execve does; or, for a name without a + /, the first program of that name in the directories of search, as + execvp does: each in turn, an empty one being the working directory. + It returns only if none could be run. It runs in spawn's child, after + the fork, so it takes no memory: each path is made in buf. *) +proc exec(name, search, buf: []U8; argv, envp: ^^U8); +var dir: []U8; i, j, n: INT; +begin + i := 0; + while (i < LEN(name)) and (name[i] # '/') do INC(i); end; + if (i < LEN(name)) or (LEN(name) = 0) then + if cstr(name, buf) then _ := SYSCALL(59, ADR(buf[0]), argv, envp); end; (* execve *) + return; + end; + i := 0; + while i <= LEN(search) do + j := i; + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; + dir := search[i..j]; + if LEN(dir) = 0 then dir := "."; end; + n := LEN(dir) + 1 + LEN(name); + if n < LEN(buf) then + COPY(buf, dir); + buf[LEN(dir)] := '/'; + COPY(buf[LEN(dir) + 1..n], name); + buf[n] := 0; + _ := SYSCALL(59, ADR(buf[0]), argv, envp); (* execve *) + end; + i := j + 1; + end; +end; + +(* Starts the program p describes, found as exec 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. *) 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, n, i, fd, g: INT; + argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, n, i, fd, g: 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 then @@ -1087,6 +1124,8 @@ argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); end; if argv = nil then return -12; end; (* ENOMEM *) + search := env("PATH"); (* read here, before the fork *) + if LEN(search) = 0 then search := defaultPath; end; all := 0FFFFFFFFFFFFFFFFH; _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) pid := SYSCALL(57); (* fork *) @@ -1109,7 +1148,7 @@ _ := SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *) for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *) _ := SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *) - _ := SYSCALL(59, ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *) + exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); _ := SYSCALL(231, 127); (* exit *) end; if (pid > 0) and (p.tty = 0) then @@ -1268,6 +1307,13 @@ r, st, c := wait(-1, true); if r # -10 then exit(2); end; (* ECHILD: none left *) child := spawn({path: "/no/such/program", args: ["x"]}); + r, st, c := wait(child, true); + if (r # child) or (st # exited) or (c # 127) then exit(2); end; + (* a name without a / is looked for along PATH *) + 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; diff --git a/lib/Sys.raz b/lib/Sys.raz index c778f34..5dec61d 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -1022,13 +1022,50 @@ return INT(g); end; -(* Starts the program p describes: 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. *) +(* Where spawn looks for a program named without a /, if PATH is unset + or empty: glibc's execvp looks in these. *) +const defaultPath = "/bin:/usr/bin"; + +(* Runs the program at name, as execve does; or, for a name without a + /, the first program of that name in the directories of search, as + execvp does: each in turn, an empty one being the working directory. + It returns only if none could be run. It runs in spawn's child, after + the fork, so it takes no memory: each path is made in buf. *) +proc exec(name, search, buf: []U8; argv, envp: ^^U8); +var dir: []U8; i, j, n: INT; +begin + i := 0; + while (i < LEN(name)) and (name[i] # '/') do INC(i); end; + if (i < LEN(name)) or (LEN(name) = 0) then + if cstr(name, buf) then _ := C.execve(ADR(buf[0]), argv, envp); end; + return; + end; + i := 0; + while i <= LEN(search) do + j := i; + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; + dir := search[i..j]; + if LEN(dir) = 0 then dir := "."; end; + n := LEN(dir) + 1 + LEN(name); + if n < LEN(buf) then + COPY(buf, dir); + buf[LEN(dir)] := '/'; + COPY(buf[LEN(dir) + 1..n], name); + buf[n] := 0; + _ := C.execve(ADR(buf[0]), argv, envp); + end; + i := j + 1; + end; +end; + +(* Starts the program p describes, found as exec 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. *) proc spawn*(p: Program): INT; var - a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: INT; + a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: INT; search: []U8; all, old, empty: [16]U64; dfl: Sigaction; pid, fd: I32; (* sigset_t: 128 bytes *) begin if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) @@ -1046,6 +1083,8 @@ argv, size := argsAndEnv(p.args, p.env, C.environ); end; if argv = nil then return -12; end; (* ENOMEM *) + search := env("PATH"); (* read here, before the fork *) + if LEN(search) = 0 then search := defaultPath; end; for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *) pid := C.fork(); @@ -1068,7 +1107,7 @@ _ := C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *) for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end; _ := C.sigprocmask(2, ^U8(ADR(empty)), nil); - _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1)); + exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); C._exit(127); end; err := failed(); @@ -1251,6 +1290,13 @@ r, st, c := wait(-1, true); if r # -10 then exit(2); end; (* ECHILD: none left *) child := spawn({path: "/no/such/program", args: ["x"]}); + r, st, c := wait(child, true); + if (r # child) or (st # exited) or (c # 127) then exit(2); end; + (* a name without a / is looked for along PATH *) + 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;