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.
| @@ -336,11 +336,15 @@ | ||
| 336 | 336 | terminal write it reads. |
| 337 | 337 | |
| 338 | 338 | `spawn` starts the program at `p.path`, with `p.args` as its |
| 339 | -arguments, the first being its name. Its environment is this | |
| 339 | +arguments, the first being its name. A `p.path` without a `/` is | |
| 340 | +looked for as a shell looks for a command, and as C's `execvp` does: | |
| 341 | +in each directory of this program's `PATH` in turn, an empty one being | |
| 342 | +the working directory, or of `/bin:/usr/bin` if `PATH` is unset or | |
| 343 | +empty; the first that runs is the program. Its environment is this | |
| 340 | 344 | program's with each `NAME=value` of `p.env` added or put in place of |
| 341 | 345 | the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in |
| 342 | 346 | `p.dir`. Its standard input, output and error are `p.fds`; a 0 gives |
| 343 | -it this program's own, so `{path: "/bin/ls", args: ["ls"]}` runs | |
| 347 | +it this program's own, so `{path: "ls", args: ["ls"]}` runs | |
| 344 | 348 | `ls` as a shell runs it, with every other field zero. It gets no |
| 345 | 349 | other open file, every signal does what it does at first, and none |
| 346 | 350 | is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a |
| @@ -1060,14 +1060,51 @@ | ||
| 1060 | 1060 | return INT(g); |
| 1061 | 1061 | end; |
| 1062 | 1062 | |
| 1063 | -(* Starts the program p describes: its process id, or minus an errno. | |
| 1064 | - A program that can't be started ends with status 127. Every signal | |
| 1065 | - is blocked from the fork until the program has its own: its group, | |
| 1066 | - its terminal's foreground, and signals as they are at first. *) | |
| 1063 | +(* Where spawn looks for a program named without a /, if PATH is unset | |
| 1064 | + or empty: glibc's execvp looks in these. *) | |
| 1065 | +const defaultPath = "/bin:/usr/bin"; | |
| 1066 | + | |
| 1067 | +(* Runs the program at name, as execve does; or, for a name without a | |
| 1068 | + /, the first program of that name in the directories of search, as | |
| 1069 | + execvp does: each in turn, an empty one being the working directory. | |
| 1070 | + It returns only if none could be run. It runs in spawn's child, after | |
| 1071 | + the fork, so it takes no memory: each path is made in buf. *) | |
| 1072 | +proc exec(name, search, buf: []U8; argv, envp: ^^U8); | |
| 1073 | +var dir: []U8; i, j, n: INT; | |
| 1074 | +begin | |
| 1075 | + i := 0; | |
| 1076 | + while (i < LEN(name)) and (name[i] # '/') do INC(i); end; | |
| 1077 | + if (i < LEN(name)) or (LEN(name) = 0) then | |
| 1078 | + if cstr(name, buf) then _ := SYSCALL(59, ADR(buf[0]), argv, envp); end; (* execve *) | |
| 1079 | + return; | |
| 1080 | + end; | |
| 1081 | + i := 0; | |
| 1082 | + while i <= LEN(search) do | |
| 1083 | + j := i; | |
| 1084 | + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; | |
| 1085 | + dir := search[i..j]; | |
| 1086 | + if LEN(dir) = 0 then dir := "."; end; | |
| 1087 | + n := LEN(dir) + 1 + LEN(name); | |
| 1088 | + if n < LEN(buf) then | |
| 1089 | + COPY(buf, dir); | |
| 1090 | + buf[LEN(dir)] := '/'; | |
| 1091 | + COPY(buf[LEN(dir) + 1..n], name); | |
| 1092 | + buf[n] := 0; | |
| 1093 | + _ := SYSCALL(59, ADR(buf[0]), argv, envp); (* execve *) | |
| 1094 | + end; | |
| 1095 | + i := j + 1; | |
| 1096 | + end; | |
| 1097 | +end; | |
| 1098 | + | |
| 1099 | +(* Starts the program p describes, found as exec says: its process id, | |
| 1100 | + or minus an errno. A program that can't be started ends with status | |
| 1101 | + 127. Every signal is blocked from the fork until the program has its | |
| 1102 | + own: its group, its terminal's foreground, and signals as they are | |
| 1103 | + at first. *) | |
| 1067 | 1104 | proc spawn*(p: Program): INT; |
| 1068 | 1105 | var |
| 1069 | 1106 | a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; (* struct sigaction: SIG_DFL *) |
| 1070 | - argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, n, i, fd, g: INT; | |
| 1107 | + argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, n, i, fd, g: INT; search: []U8; | |
| 1071 | 1108 | begin |
| 1072 | 1109 | if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1073 | 1110 | if p.tty # 0 then |
| @@ -1087,6 +1124,8 @@ | ||
| 1087 | 1124 | argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); |
| 1088 | 1125 | end; |
| 1089 | 1126 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1127 | + search := env("PATH"); (* read here, before the fork *) | |
| 1128 | + if LEN(search) = 0 then search := defaultPath; end; | |
| 1090 | 1129 | all := 0FFFFFFFFFFFFFFFFH; |
| 1091 | 1130 | _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) |
| 1092 | 1131 | pid := SYSCALL(57); (* fork *) |
| @@ -1109,7 +1148,7 @@ | ||
| 1109 | 1148 | _ := SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *) |
| 1110 | 1149 | for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *) |
| 1111 | 1150 | _ := SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *) |
| 1112 | - _ := SYSCALL(59, ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *) | |
| 1151 | + exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); | |
| 1113 | 1152 | _ := SYSCALL(231, 127); (* exit *) |
| 1114 | 1153 | end; |
| 1115 | 1154 | if (pid > 0) and (p.tty = 0) then |
| @@ -1268,6 +1307,13 @@ | ||
| 1268 | 1307 | r, st, c := wait(-1, true); |
| 1269 | 1308 | if r # -10 then exit(2); end; (* ECHILD: none left *) |
| 1270 | 1309 | child := spawn({path: "/no/such/program", args: ["x"]}); |
| 1310 | + r, st, c := wait(child, true); | |
| 1311 | + if (r # child) or (st # exited) or (c # 127) then exit(2); end; | |
| 1312 | + (* a name without a / is looked for along PATH *) | |
| 1313 | + child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]}); | |
| 1314 | + r, st, c := wait(child, true); | |
| 1315 | + if (r # child) or (st # exited) or (c # 4) then exit(2); end; | |
| 1316 | + child := spawn({path: "no-such-program-on-path", args: ["x"]}); | |
| 1271 | 1317 | r, st, c := wait(child, true); |
| 1272 | 1318 | if (r # child) or (st # exited) or (c # 127) then exit(2); end; |
| 1273 | 1319 |
| @@ -1022,13 +1022,50 @@ | ||
| 1022 | 1022 | return INT(g); |
| 1023 | 1023 | end; |
| 1024 | 1024 | |
| 1025 | -(* Starts the program p describes: its process id, or minus an errno. | |
| 1026 | - A program that can't be started ends with status 127. Every signal | |
| 1027 | - is blocked from the fork until the program has its own: its group, | |
| 1028 | - its terminal's foreground, and signals as they are at first. *) | |
| 1025 | +(* Where spawn looks for a program named without a /, if PATH is unset | |
| 1026 | + or empty: glibc's execvp looks in these. *) | |
| 1027 | +const defaultPath = "/bin:/usr/bin"; | |
| 1028 | + | |
| 1029 | +(* Runs the program at name, as execve does; or, for a name without a | |
| 1030 | + /, the first program of that name in the directories of search, as | |
| 1031 | + execvp does: each in turn, an empty one being the working directory. | |
| 1032 | + It returns only if none could be run. It runs in spawn's child, after | |
| 1033 | + the fork, so it takes no memory: each path is made in buf. *) | |
| 1034 | +proc exec(name, search, buf: []U8; argv, envp: ^^U8); | |
| 1035 | +var dir: []U8; i, j, n: INT; | |
| 1036 | +begin | |
| 1037 | + i := 0; | |
| 1038 | + while (i < LEN(name)) and (name[i] # '/') do INC(i); end; | |
| 1039 | + if (i < LEN(name)) or (LEN(name) = 0) then | |
| 1040 | + if cstr(name, buf) then _ := C.execve(ADR(buf[0]), argv, envp); end; | |
| 1041 | + return; | |
| 1042 | + end; | |
| 1043 | + i := 0; | |
| 1044 | + while i <= LEN(search) do | |
| 1045 | + j := i; | |
| 1046 | + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; | |
| 1047 | + dir := search[i..j]; | |
| 1048 | + if LEN(dir) = 0 then dir := "."; end; | |
| 1049 | + n := LEN(dir) + 1 + LEN(name); | |
| 1050 | + if n < LEN(buf) then | |
| 1051 | + COPY(buf, dir); | |
| 1052 | + buf[LEN(dir)] := '/'; | |
| 1053 | + COPY(buf[LEN(dir) + 1..n], name); | |
| 1054 | + buf[n] := 0; | |
| 1055 | + _ := C.execve(ADR(buf[0]), argv, envp); | |
| 1056 | + end; | |
| 1057 | + i := j + 1; | |
| 1058 | + end; | |
| 1059 | +end; | |
| 1060 | + | |
| 1061 | +(* Starts the program p describes, found as exec says: its process id, | |
| 1062 | + or minus an errno. A program that can't be started ends with status | |
| 1063 | + 127. Every signal is blocked from the fork until the program has its | |
| 1064 | + own: its group, its terminal's foreground, and signals as they are | |
| 1065 | + at first. *) | |
| 1029 | 1066 | proc spawn*(p: Program): INT; |
| 1030 | 1067 | var |
| 1031 | - a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: INT; | |
| 1068 | + a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: INT; search: []U8; | |
| 1032 | 1069 | all, old, empty: [16]U64; dfl: Sigaction; pid, fd: I32; (* sigset_t: 128 bytes *) |
| 1033 | 1070 | begin |
| 1034 | 1071 | if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| @@ -1046,6 +1083,8 @@ | ||
| 1046 | 1083 | argv, size := argsAndEnv(p.args, p.env, C.environ); |
| 1047 | 1084 | end; |
| 1048 | 1085 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1086 | + search := env("PATH"); (* read here, before the fork *) | |
| 1087 | + if LEN(search) = 0 then search := defaultPath; end; | |
| 1049 | 1088 | for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; |
| 1050 | 1089 | _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *) |
| 1051 | 1090 | pid := C.fork(); |
| @@ -1068,7 +1107,7 @@ | ||
| 1068 | 1107 | _ := C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *) |
| 1069 | 1108 | for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end; |
| 1070 | 1109 | _ := C.sigprocmask(2, ^U8(ADR(empty)), nil); |
| 1071 | - _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1)); | |
| 1110 | + exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); | |
| 1072 | 1111 | C._exit(127); |
| 1073 | 1112 | end; |
| 1074 | 1113 | err := failed(); |
| @@ -1251,6 +1290,13 @@ | ||
| 1251 | 1290 | r, st, c := wait(-1, true); |
| 1252 | 1291 | if r # -10 then exit(2); end; (* ECHILD: none left *) |
| 1253 | 1292 | child := spawn({path: "/no/such/program", args: ["x"]}); |
| 1293 | + r, st, c := wait(child, true); | |
| 1294 | + if (r # child) or (st # exited) or (c # 127) then exit(2); end; | |
| 1295 | + (* a name without a / is looked for along PATH *) | |
| 1296 | + child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]}); | |
| 1297 | + r, st, c := wait(child, true); | |
| 1298 | + if (r # child) or (st # exited) or (c # 4) then exit(2); end; | |
| 1299 | + child := spawn({path: "no-such-program-on-path", args: ["x"]}); | |
| 1254 | 1300 | r, st, c := wait(child, true); |
| 1255 | 1301 | if (r # child) or (st # exited) or (c # 127) then exit(2); end; |
| 1256 | 1302 |