raz / commit 0fc0d46e

commit
0fc0d46e4dcd418861692b7e4a3ae3f633011177
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e57d9c7e
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.

patch · log

3 files changed, +110 -14

doc/raz-lib.md (history) modified +6 -2

@@ -336,11 +336,15 @@
336336 terminal write it reads.
337337
338338 `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
340344 program's with each `NAME=value` of `p.env` added or put in place of
341345 the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in
342346 `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
344348 `ls` as a shell runs it, with every other field zero. It gets no
345349 other open file, every signal does what it does at first, and none
346350 is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a

lib/Sys.linux-x86_64.raz (history) modified +52 -6

@@ -1060,14 +1060,51 @@
10601060 return INT(g);
10611061 end;
10621062
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. *)
10671104 proc spawn*(p: Program): INT;
10681105 var
10691106 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;
10711108 begin
10721109 if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *)
10731110 if p.tty # 0 then
@@ -1087,6 +1124,8 @@
10871124 argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1));
10881125 end;
10891126 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;
10901129 all := 0FFFFFFFFFFFFFFFFH;
10911130 _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *)
10921131 pid := SYSCALL(57); (* fork *)
@@ -1109,7 +1148,7 @@
11091148 _ := SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *)
11101149 for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *)
11111150 _ := 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));
11131152 _ := SYSCALL(231, 127); (* exit *)
11141153 end;
11151154 if (pid > 0) and (p.tty = 0) then
@@ -1268,6 +1307,13 @@
12681307 r, st, c := wait(-1, true);
12691308 if r # -10 then exit(2); end; (* ECHILD: none left *)
12701309 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"]});
12711317 r, st, c := wait(child, true);
12721318 if (r # child) or (st # exited) or (c # 127) then exit(2); end;
12731319

lib/Sys.raz (history) modified +52 -6

@@ -1022,13 +1022,50 @@
10221022 return INT(g);
10231023 end;
10241024
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. *)
10291066 proc spawn*(p: Program): INT;
10301067 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;
10321069 all, old, empty: [16]U64; dfl: Sigaction; pid, fd: I32; (* sigset_t: 128 bytes *)
10331070 begin
10341071 if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *)
@@ -1046,6 +1083,8 @@
10461083 argv, size := argsAndEnv(p.args, p.env, C.environ);
10471084 end;
10481085 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;
10491088 for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end;
10501089 _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *)
10511090 pid := C.fork();
@@ -1068,7 +1107,7 @@
10681107 _ := C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *)
10691108 for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end;
10701109 _ := 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));
10721111 C._exit(127);
10731112 end;
10741113 err := failed();
@@ -1251,6 +1290,13 @@
12511290 r, st, c := wait(-1, true);
12521291 if r # -10 then exit(2); end; (* ECHILD: none left *)
12531292 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"]});
12541300 r, st, c := wait(child, true);
12551301 if (r # child) or (st # exited) or (c # 127) then exit(2); end;
12561302