From ac9d3e891edb702c37bc5d29391d21e73f88092c Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 07:35:31 +0000 Subject: [PATCH] Sys: what a shell needs, for concha, over Linux and over C By the owner's word, in both Sys files, each over its own calls: the Linux one by system calls, the C one by POSIX's, with no new layout of glibc's (a terminal's modes are its struct termios whole, and only the two struct timeval that begin struct rusage are read). - dup, dupTo and keepOnExec: a shell's own fds copied and moved, and whether each stays open in the program exec runs. - Program.more: fds above 2 passed on at the same numbers; spawn's child closes the runs between them (closeAllBut) and keeps them on exec, as 3`, `>>` and `2>`, and job control -through `spawn`'s `group` and `foreground`. A shell people use every -day needs more. Each item below is library growth that breaks no -program, and each has a C call behind it, for `Sys.raz` over the C -library. - -**Moving file descriptors.** `2>&1`, `3>log`, `exec >out` and `{ a; -b; } >f` move or copy the shell's own descriptors, around a builtin -or for good; `Program.fds` covers only a child's first three. - -``` -proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) -proc dupTo*(fd, to: INT): INT; (* to made the same file as fd, closing what it was *) -proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd passed to programs spawn starts, or not *) -``` - -`Program` gains `more*: []INT`, descriptors passed on to the program -at the same numbers, for `3&1` and +`exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the +same open file as `fd`, closing what `into` was, and `dup` gives a new +fd for it, the lowest free, as a shell saves a file to give it back. +`access` says whether this user, by its real ids, may read, write or +execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` +alone can't say; an empty `how` asks only whether it is there. +`open` refuses `executing`, with -22 (EINVAL). `umask` sets the bits +taken away from the mode of every file this program, and those it +starts, make, and says what they were. `envAt` gives the environment +one variable at a time, from 0, for a shell to list it and to pass it +on whole with `onlyEnv`. `getMode` reads a terminal's modes, and +`setMode` sets them again once what was written is sent, so that a +shell keeps them for each job: a program stopped by Ctrl-Z with its +terminal raw gets it back raw, and the shell its own between. +`cpuTime` says how long the processor ran this program, in its own +code and in the system for it, or, if `children`, the programs it +started and has waited for, as `time` and `times` say. `limit` gives a +limit, `limitFiles` say, soft and hard, and `setLimit` sets them, as +`ulimit` does: a program may raise its soft limit up to the hard one, +and lower either. `uid` is the user this program runs as, `euid` the +one whose rights it has, 0 for root, as a prompt shows `#`; `hostName` +is the machine's name. `wait` waits for the program `pid` to change, or for any of this program's if `pid` is -1, and says which one, and how: `exited`, with diff --git a/lib/C.raz b/lib/C.raz index 9755fba..3da5977 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -72,11 +72,20 @@ extern proc fork*: I32; extern proc setsid*: I32; extern proc dup2*(fd, fd2: I32): I32; +extern proc dup*(fd: I32): I32; extern proc close_range*(first, last: U32; flags: I32): I32; extern proc execve*(path: ^U8; argv, envp: ^^U8): I32; extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32; extern proc kill*(pid, signal: I32): I32; extern proc getpid*: I32; +extern proc getuid*: U32; +extern proc geteuid*: U32; +extern proc gethostname*(name: ^U8; n: UINT): I32; +extern proc access*(path: ^U8; how: I32): I32; +extern proc umask*(mask: U32): U32; +extern proc getrusage*(who: I32; usage: ^U8): I32; +extern proc getrlimit*(which: I32; limit: ^U8): I32; +extern proc setrlimit*(which: I32; limit: ^U8): I32; extern proc getpgrp*: I32; extern proc setpgid*(pid, group: I32): I32; extern proc tcsetpgrp*(fd, group: I32): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 8133e81..cedfc49 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -13,12 +13,22 @@ sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; + sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; + (* the limits limit and setLimit take, by their numbers on Linux *) + limitCpu* = 0; (* seconds of the processor *) + limitFileSize* = 1; (* bytes in a file it writes *) + limitData* = 2; (* bytes of its data *) + limitStack* = 3; (* bytes of its stack *) + limitCore* = 4; (* bytes of a core dump *) + limitProcs* = 6; (* processes of its user *) + limitFiles* = 7; (* one more than its highest fd *) + limitMemory* = 9; (* bytes of its address space *) type (* What open is asked for: reading, writing or both, and what else it does to the file. *) Access* = (reading, writing, creating, exclusive, truncating, - appending); + appending, executing); Accesses* = set of Access; (* what open is given *) (* What info says of a file. *) @@ -57,6 +67,7 @@ group*: INT; (* its group: 0 for this program's, -1 a new one *) 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 *) end; (* How a program that wait waited for changed. *) Status* = (running, exited, killed, stopped); @@ -67,6 +78,11 @@ line: U8; cc: [19]U8; end; + (* A terminal's modes, as getMode reads them and setMode sets them. *) + Mode* = record + t: Termios; + end; + Winsize = record rows, cols, xpixel, ypixel: U16; end; Pollfd = record fd: I32; events, revents: I16; end; @@ -113,6 +129,7 @@ saved: Termios; (* the terminal as rawMode found it *) caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *) sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) + uts: [390]U8; (* struct utsname, which hostName gives the nodename of *) (* Writes s to the file descriptor fd; the number of bytes written, or minus an errno. *) @@ -161,6 +178,7 @@ proc openMode*(path: []U8; how: Accesses; mode: INT): INT; var buf: [4096]U8; begin + if executing in how then return -22; end; (* EINVAL: access asks it, open can't *) if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) return SYSCALL(2, ADR(buf), flags(how), mode); end; @@ -237,6 +255,32 @@ _ := SYSCALL(3, fd); end; +(* A new file descriptor for the file fd is open on, the lowest free: + it, or minus an errno. *) +proc dup*(fd: INT): INT; +begin + return SYSCALL(32, fd); (* dup *) +end; + +(* Makes the file descriptor into stand for the file fd is open on, + closing what into was first: into, or minus an errno. *) +proc dupTo*(fd, into: INT): INT; +begin + return SYSCALL(33, fd, into); (* dup2 *) +end; + +(* Whether fd stays open in the program exec runs in this one's place: + yes, or no, closed then, as pipe's fds are at first. 0, or minus an + errno. *) +proc keepOnExec*(fd: INT; yes: BOOL): INT; +var f: INT; +begin + f := SYSCALL(72, fd, 1); (* fcntl: F_GETFD *) + if f < 0 then return f; end; + if yes then f := f & ~1; else f := f | 1; end; (* FD_CLOEXEC *) + return SYSCALL(72, fd, 2, f); (* F_SETFD *) +end; + (* size bytes of fresh, zeroed memory from the system, a multiple of pages, or nil. *) proc mmap*(size: INT): ^U8; @@ -325,6 +369,27 @@ return SYSCALL(90, ADR(a), mode); end; +(* 0 if this user, by its real ids, may do with path what how asks: + reading, writing and executing; minus an errno, -13 (EACCES) if not. + An empty how asks only whether path is there. *) +proc access*(path: []U8; how: Accesses): INT; +var a: [4096]U8; m: INT; +begin + if not cstr(path, a) then return -36; end; + m := 0; (* F_OK *) + if reading in how then m := m | 4; end; (* R_OK *) + if writing in how then m := m | 2; end; (* W_OK *) + if executing in how then m := m | 1; end; (* X_OK *) + return SYSCALL(21, ADR(a), m); (* access *) +end; + +(* Sets the bits taken from every file this program, and the programs + it starts, make: the mask it was. *) +proc umask*(mask: INT): INT; +begin + return SYSCALL(95, mask & 1FFH); (* umask *) +end; + (* Makes path a symbolic link to target. *) proc symlink*(target, path: []U8): INT; var a, b: [4096]U8; @@ -498,8 +563,24 @@ proc restoreMode*(fd: INT); begin _ := SYSCALL(16, fd, 5402H, ADR(saved)); +end; + +(* The modes of the terminal on fd, as a shell keeps them for each job; + and 0, or minus an errno, -25 (ENOTTY) if it isn't a terminal. *) +proc getMode*(fd: INT): (Mode, INT); +var m: Mode; r: INT; +begin + r := SYSCALL(16, fd, 5401H, ADR(m.t)); (* TCGETS *) + return m, r; end; +(* Gives the terminal on fd the modes m, once what was written to it + is sent: 0, or minus an errno. *) +proc setMode*(fd: INT; m: Mode): INT; +begin + return SYSCALL(16, fd, 5403H, ADR(m.t)); (* TCSETSW *) +end; + (* The columns and rows of the terminal on fd; 80 by 24 if it doesn't say. *) proc termSize*(fd: INT): (INT, INT); @@ -926,6 +1007,22 @@ return ""; end; +(* The environment's ith NAME=value, from 0, and true; "" and false + past the last. *) +proc envAt*(i: INT): ([]U8, BOOL); +var e: ^^U8; s: ^U8; j, n: INT; +begin + if i < 0 then return "", false; end; + e := ARGV() + (ARGC() + 1); + for j := 0 to i do + if e[j] = nil then return "", false; end; + end; + s := e[i]; + n := 0; + while s[n] # 0 do INC(n); end; + return s[0..n], true; +end; + (* The working directory, good until the next call; and 0, or minus an errno. *) proc workDir*: ([]U8, INT); @@ -1034,6 +1131,62 @@ return SYSCALL(39); (* getpid *) end; +(* The user this program runs as: its real id. *) +proc uid*: INT; +begin + return SYSCALL(102); (* getuid *) +end; + +(* The user whose rights this program has, its effective id: 0 is + root. *) +proc euid*: INT; +begin + return SYSCALL(107); (* geteuid *) +end; + +(* The name of this machine, good until the next call; and 0, or minus + an errno. *) +proc hostName*: ([]U8, INT); +var r, n: INT; +begin + r := SYSCALL(63, ADR(uts)); (* uname *) + if r < 0 then return "", r; end; + n := 0; + while (n < 64) and (uts[65 + n] # 0) do INC(n); end; (* the nodename, at 65 *) + return uts[65..65 + n], 0; +end; + +(* The time on the processor, in ns, that this program has used, or, + if children, the programs it started and waited for: in the program + itself, and in the system for it. *) +proc cpuTime*(children: BOOL): (I64, I64); +var ru: [18]I64; (* struct rusage: two struct timeval first *) +begin + if children then _ := SYSCALL(98, -1, ADR(ru)); (* getrusage: RUSAGE_CHILDREN *) + else _ := SYSCALL(98, 0, ADR(ru)); (* RUSAGE_SELF *) + end; + return ru[0] * 1000000000 + ru[1] * 1000, ru[2] * 1000000000 + ru[3] * 1000; +end; + +(* The limit which, one of the limit constants: the soft one, which + this program may raise up to the hard one; -1 for none; and 0, or + minus an errno. *) +proc limit*(which: INT): (I64, I64, INT); +var r: [2]I64; err: INT; +begin + err := SYSCALL(97, which, ADR(r)); (* getrlimit; RLIM_INFINITY is -1 *) + return r[0], r[1], err; +end; + +(* Sets the limit which to soft and hard, -1 for none: 0, or minus an + errno. *) +proc setLimit*(which: INT; soft, hard: I64): INT; +var r: [2]I64; +begin + r := [soft, hard]; + return SYSCALL(160, which, ADR(r)); (* setrlimit *) +end; + (* Moves the program pid, or this one if pid is 0, to the process group group, or to a new one it leads if group is 0. *) proc setGroup*(pid, group: INT): INT; @@ -1064,39 +1217,156 @@ or empty: glibc's execvp looks in these. *) const defaultPath = "/bin:/usr/bin"; +(* The ith directory of search, a list of them with : between, as PATH + is: dir, "." if it is empty, and where the next starts; -1 past the + last. *) +proc pathDir(search: []U8; i: INT): ([]U8, INT); +var j: INT; +begin + if i > LEN(search) then return "", -1; end; + j := i; + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; + if j = i then return ".", j + 1; end; + return search[i..j], j + 1; +end; + +(* dir, a /, and name, a C string in buf; false if it doesn't fit. *) +proc joinPath(dir, name, buf: []U8): BOOL; +var n: INT; +begin + n := LEN(dir) + 1 + LEN(name); + if n >= LEN(buf) then return false; end; + COPY(buf, dir); + buf[LEN(dir)] := '/'; + COPY(buf[LEN(dir) + 1..n], name); + buf[n] := 0; + return true; +end; + +(* Whether name holds a /, and so is a path, not a name to look for. *) +proc isPath(name: []U8): BOOL; +var i: INT; +begin + for i := 0 to LEN(name) - 1 do + if name[i] = '/' then return true; end; + end; + return false; +end; + (* 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; +proc run(name, search, buf: []U8; argv, envp: ^^U8); +var dir: []U8; i: 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 isPath(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 *) + dir, i := pathDir(search, 0); + while i >= 0 do + if joinPath(dir, name, buf) then _ := SYSCALL(59, ADR(buf[0]), argv, envp); end; (* execve *) + dir, i := pathDir(search, i); + end; +end; + +(* The program exec runs for name, as a C string in buf: name itself + if it has a /, or else the first file of that name in the + directories of search that is no directory and that this user may + run. 0; or -2 (ENOENT) if there is none, or -13 (EACCES) if one is + there and can't be run. *) +proc find(name, search, buf: []U8): INT; +var dir: []U8; i, err, r: INT; f: Info; +begin + if isPath(name) then + if not cstr(name, buf) then return -36; end; + return 0; + end; + r := -2; (* ENOENT *) + dir, i := pathDir(search, 0); + while (LEN(name) > 0) and (i >= 0) do + if joinPath(dir, name, buf) then + f, err := info(buf[0..LEN(dir) + 1 + LEN(name)]); + if (err = 0) and not f.dir then + if SYSCALL(21, ADR(buf[0]), 1) = 0 then return 0; end; (* access, X_OK *) + r := -13; (* EACCES *) + end; end; - i := j + 1; + dir, i := pathDir(search, i); end; + return r; end; -(* Starts the program p describes, found as exec says: its process id, +(* The name of the terminal of the pseudo-terminal fd, as a C string + in name: /dev/pts/ and the number TIOCGPTN gives. 0, or -25 + (ENOTTY). *) +proc ptyName(fd: INT; name: []U8): INT; +var n, i, size: INT; +begin + n := 0; + if SYSCALL(16, fd, 80045430H, ADR(n)) # 0 then return -25; end; (* TIOCGPTN; ENOTTY *) + i := put("/dev/pts/", ADR(name[0])) - ADR(name[0]) - 1; (* at its zero *) + size := n; + repeat INC(i); size := size div 10; until size = 0; + name[i] := 0; + repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0; + return 0; +end; + +(* Closes every fd from 3 on but those of keep, which stay open in the + program exec starts. It runs in spawn's child, after the fork, so it + takes no memory: it closes the runs between the fds kept, the lowest + first, each with one close_range. *) +proc closeAllBut(keep: []INT); +var lo, next, i: INT; +begin + lo := 3; + repeat + next := -1; + for i := 0 to LEN(keep) - 1 do + if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end; + end; + if next < 0 then + _ := SYSCALL(436, lo, 0FFFFFFFFH, 0); (* close_range: the rest *) + else + if next > lo then _ := SYSCALL(436, lo, next - 1, 0); end; + _ := SYSCALL(72, next, 2, 0); (* fcntl: F_SETFD, kept on exec *) + lo := next + 1; + end; + until next < 0; +end; + +(* 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. *) +proc takeOn(p: Program; name, dir: []U8): INT; +var fd, i, r: INT; +begin + if p.tty # 0 then + _ := SYSCALL(112); (* setsid *) + fd := SYSCALL(2, ADR(name[0]), 2); (* open, O_RDWR *) + if fd < 0 then return fd; end; + r := SYSCALL(16, fd, 540EH, 0); (* TIOCSCTTY *) + if r # 0 then return r; end; + for i := 0 to 2 do _ := SYSCALL(33, fd, i); end; (* dup2 *) + else + if p.group = -1 then _ := SYSCALL(109, 0, 0); (* setpgid *) + elsif p.group # 0 then _ := SYSCALL(109, 0, p.group); + end; + if p.foreground then _ := foreground(0, SYSCALL(111)); end; (* getpgrp *) + for i := 0 to 2 do + if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *) + end; + end; + if LEN(p.dir) > 0 then return SYSCALL(80, ADR(dir[0])); end; (* chdir *) + return 0; +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 @@ -1104,19 +1374,10 @@ 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; search: []U8; + argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, i, 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 - (* its terminal: /dev/pts/ and the number TIOCGPTN gives *) - n := 0; - if SYSCALL(16, p.tty, 80045430H, ADR(n)) # 0 then return -25; end; (* ENOTTY *) - i := put("/dev/pts/", ADR(name[0])) - ADR(name[0]) - 1; (* at its zero *) - size := n; - repeat INC(i); size := size div 10; until size = 0; - name[i] := 0; - repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0; - end; + if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *) none := nil; if p.onlyEnv then argv, size := argsAndEnv(p.args, p.env, ADR(none)); @@ -1130,25 +1391,11 @@ _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) pid := SYSCALL(57); (* fork *) if pid = 0 then - if p.tty # 0 then - _ := SYSCALL(112); (* setsid *) - fd := SYSCALL(2, ADR(name), 2); (* open, O_RDWR *) - if (fd < 0) or (SYSCALL(16, fd, 540EH, 0) # 0) then _ := SYSCALL(231, 127); end; (* TIOCSCTTY; exit *) - for i := 0 to 2 do _ := SYSCALL(33, fd, i); end; (* dup2 *) - else - if p.group = -1 then _ := SYSCALL(109, 0, 0); (* setpgid *) - elsif p.group # 0 then _ := SYSCALL(109, 0, p.group); - end; - if p.foreground then _ := foreground(0, SYSCALL(111)); end; (* getpgrp *) - for i := 0 to 2 do - if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *) - end; - end; - if (LEN(p.dir) > 0) and (SYSCALL(80, ADR(dir)) # 0) then _ := SYSCALL(231, 127); end; (* chdir *) - _ := SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *) + 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 *) _ := SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *) - exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); + run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); _ := SYSCALL(231, 127); (* exit *) end; if (pid > 0) and (p.tty = 0) then @@ -1162,6 +1409,53 @@ munmap(^U8(argv), size); return pid; end; + +(* Runs the program p describes in this one's place, as spawn would + start it, but keeping this program's other fds as they are, open + unless keepOnExec said no; the fds of p.more are kept. It returns + only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no + program to run, before anything is done; or, once the program's + terminal, group, fds and working directory are as p asks, which + stay so, minus the errno that stopped it, with this program's + signals given back as they were. *) +proc exec*(p: Program): INT; +var + a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; acts: [65][4]U64; (* struct sigaction *) + argv: ^^U8; none: ^U8; all, old, empty: U64; size, r, i: INT; search: []U8; +begin + if 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 *) + search := env("PATH"); + if LEN(search) = 0 then search := defaultPath; end; + r := find(p.path, search, a); + if r < 0 then return r; end; + none := nil; + if p.onlyEnv then + argv, size := argsAndEnv(p.args, p.env, ADR(none)); + else + argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); + end; + if argv = nil then return -12; end; (* ENOMEM *) + r := takeOn(p, name, dir); + if r = 0 then + for i := 0 to LEN(p.more) - 1 do + if p.more[i] > 2 then _ := keepOnExec(p.more[i], true); end; + end; + (* every signal as it is at first and none blocked, as spawn + 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 *) + _ := 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); + for i := 1 to 64 do _ := SYSCALL(13, i, ADR(acts[i]), 0, 8); end; + _ := SYSCALL(14, 2, ADR(old), 0, 8); + end; + munmap(^U8(argv), size); + return r; +end; + (* Waits for the program pid, or any of this program's if pid is -1, 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 @@ -1332,6 +1626,97 @@ if ignore(sigInt, false) # 0 then exit(2); end; end; +(* The test of what a shell needs: fds copied, moved and kept on + exec; a program given an fd at its own number, and one not; access, + the umask and the environment by number; a terminal's modes; time + used; limits; who and where; and exec, with nothing to run, and + with a program that can't be run, after which the signals caught + are caught still. *) +proc testShell; +var + r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; + m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; +begin + r, w := pipe(); + if w < 0 then exit(2); end; + fd := dup(w); + if (fd < 0) or (write(fd, "ab") # 2) or (read(r, buf) # 2) then exit(2); end; + close(fd); + if (dupTo(w, 9) # 9) or (write(9, "c") # 1) or (read(r, buf) # 1) or (buf[0] # 'c') then exit(2); end; + if (keepOnExec(9, false) # 0) or (keepOnExec(9, true) # 0) or (keepOnExec(-1, true) # -9) then exit(2); end; + child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo more >&9"], more: [9]}); + close(9); + close(w); + n := 0; + repeat + k := read(r, buf[n..LEN(buf)]); + if k > 0 then INC(n, k); end; + until k <= 0; + close(r); + if buf[0..n] # "more\n" then exit(2); end; + k, st, c := wait(child, true); + if (st # exited) or (c # 0) then exit(2); end; + r, w := pipe(); + if (w < 0) or (dupTo(w, 9) # 9) then exit(2); end; + child := spawn({path: "/bin/sh", args: ["sh", "-c", "{ echo none >&9; } 2>/dev/null"]}); (* 9 is closed in it *) + close(9); + close(w); + if read(r, buf) # 0 then exit(2); end; + close(r); + k, st, c := wait(child, true); + if (st # exited) or (c = 0) then exit(2); end; + + if (access("/bin/sh", {executing}) # 0) or (access("/", {reading}) # 0) + or (access("/no/such/file", {}) # -2) or (open("/bin/sh", {executing}) # -22) then exit(2); end; + mask := umask(3FH); (* 077 *) + if umask(mask) # 3FH then exit(2); end; + path := false; + n := 0; + repeat + e, ok := envAt(n); + if ok and (LEN(e) > 5) and (e[0..5] = "PATH=") then path := true; end; + INC(n); + until not ok; + e, ok := envAt(-1); + if not path or ok or (LEN(e) # 0) then exit(2); end; + + (* a terminal's modes, kept and given back: a new one has lines *) + pty := openPty(); + m, err := getMode(pty); + if (err # 0) or (m.t.lflag & 2 = 0) or not rawMode(pty) then exit(2); end; (* ICANON *) + raw, err := getMode(pty); + if (err # 0) or (raw.t.lflag & 2 # 0) or (setMode(pty, m) # 0) then exit(2); end; + raw, err := getMode(pty); + if (err # 0) or (raw.t.lflag & 2 = 0) then exit(2); end; + m, err := getMode(-1); + if err # -9 then exit(2); end; (* EBADF *) + r, w := pipe(); + m, err := getMode(r); + if err # -25 then exit(2); end; (* ENOTTY *) + close(r); + close(w); + close(pty); + + user, system := cpuTime(false); + if (user < 0) or (system < 0) or (user + system = 0) then exit(2); end; + user, system := cpuTime(true); + if (user < 0) or (system < 0) then exit(2); end; + soft, hard, err := limit(limitFiles); + if (err # 0) or (soft < 16) or (setLimit(limitFiles, soft - 1, hard) # 0) then exit(2); end; + s2, h2, err := limit(limitFiles); + if (err # 0) or (s2 # soft - 1) or (h2 # hard) or (setLimit(limitFiles, soft, hard) # 0) then exit(2); end; + soft, hard, err := limit(10000); + if err # -22 then exit(2); end; (* EINVAL *) + e, err := hostName(); + if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; + + sigs := catch([sigUsr1]); + while caught(sigs) # 0 do end; (* the sigChld of the programs above *) + if (sigs < 0) or (exec({path: "no-such-program-on-path", args: ["x"]}) # -2) then exit(2); end; + if exec({path: "/etc/passwd", args: ["x"]}) # -13 then exit(2); end; (* not executable *) + if (kill(pid(), sigUsr1) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigUsr1) then exit(2); end; +end; + (* The test of time, random bytes and the network: a sleep the clock sees; addresses read and refused; a TCP connection on the loopback; a datagram to a port where no one listens, refused; servers on @@ -1562,6 +1947,7 @@ close(l); _ := remove("/tmp/raz-sys-socket"); testPrograms; + testShell; testNet; end; diff --git a/lib/Sys.raz b/lib/Sys.raz index 12147e6..1c8645f 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -2,12 +2,16 @@ file of their own, with the same exports. A program that imports it uses C. - It keeps to POSIX's names, and raw mode is cfmakeraw's. What is - glibc's, on x86-64: where a name lies in struct dirent; struct stat; - open's flags; the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and - TIOCSCTTY; close_range; SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; and - struct sockaddr_un and sockaddr_in, which on the BSDs start with a - byte of length. A C library that differs needs a Sys of its own. *) + It keeps to POSIX's names, and raw mode is cfmakeraw's. A terminal's + modes, Mode, are its struct termios whole, of whatever layout, and + only the two struct timeval that begin struct rusage are read. What + is glibc's, on x86-64: where a name lies in struct dirent; struct + stat; open's flags; the numbers of the requests TIOCGWINSZ, + TIOCSWINSZ and TIOCSCTTY; close_range; SOCK_CLOEXEC, AF_INET6 and + SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs + start with a byte of length; the numbers of the signals and of the + limits, and RLIM_INFINITY, all ones. A C library that differs needs + a Sys of its own. *) module Sys; import C; @@ -22,6 +26,17 @@ sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; + sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; + (* the limits limit and setLimit take, by their numbers on Linux; + another target has its own *) + limitCpu* = 0; (* seconds of the processor *) + limitFileSize* = 1; (* bytes in a file it writes *) + limitData* = 2; (* bytes of its data *) + limitStack* = 3; (* bytes of its stack *) + limitCore* = 4; (* bytes of a core dump *) + limitProcs* = 6; (* processes of its user *) + limitFiles* = 7; (* one more than its highest fd *) + limitMemory* = 9; (* bytes of its address space *) DirentName = 19; (* the offset of d_name in glibc's struct dirent *) DirentType = 18; (* and of d_type *) TIOCGWINSZ = 5413H; (* glibc's, as the two below *) @@ -32,7 +47,7 @@ (* What open is asked for: reading, writing or both, and what else it does to the file. *) Access* = (reading, writing, creating, exclusive, truncating, - appending); + appending, executing); Accesses* = set of Access; (* what open is given *) (* What info says of a file. *) @@ -70,6 +85,7 @@ group*: INT; (* its group: 0 for this program's, -1 a new one *) 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 *) end; (* How a program that wait waited for changed. *) Status* = (running, exited, killed, stopped); @@ -83,6 +99,18 @@ restorer: ^U8; end; + (* A terminal's modes, as getMode reads them and setMode sets them: + a struct termios, of the C library's size, 256 bytes at most. *) + Mode* = record + t: [256]U8; + end; + + (* struct rusage: what is read of it, the time used in the program + and in the system, each a struct timeval, its microseconds at + most an int and little-endian, then room for the rest *) + Timeval = record sec: I64; usec: I32; pad: I32; end; + Rusage = record user, system: Timeval; rest: [32]I64; end; + Winsize = record rows, cols, xpixel, ypixel: U16; end; Pollfd = record fd: I32; events, revents: I16; end; @@ -127,6 +155,7 @@ cwd: [4096]U8; (* what workDir gives *) linked: [4096]U8; (* what readLink gives *) saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) + host: [256]U8; (* what hostName gives *) sigRead, sigWrite: I32; (* the pipe the handler of caught signals writes their numbers to, once open *) (* minus errno, as a system call returns it *) @@ -190,6 +219,7 @@ proc openMode*(path: []U8; how: Accesses; mode: INT): INT; var buf: [4096]U8; r: INT; begin + if executing in how then return -22; end; (* EINVAL: access asks it, open can't *) if not cstr(path, buf) then return -36; end; r := INT(C.open(ADR(buf[0]), flags(how), mode)); if r < 0 then return failed(); end; @@ -283,6 +313,39 @@ _ := C.close(I32(fd)); end; +(* A new file descriptor for the file fd is open on, the lowest free: + it, or minus an errno. *) +proc dup*(fd: INT): INT; +var r: I32; +begin + r := C.dup(I32(fd)); + if r < 0 then return failed(); end; + return INT(r); +end; + +(* Makes the file descriptor into stand for the file fd is open on, + closing what into was first: into, or minus an errno. *) +proc dupTo*(fd, into: INT): INT; +var r: I32; +begin + r := C.dup2(I32(fd), I32(into)); + if r < 0 then return failed(); end; + return INT(r); +end; + +(* Whether fd stays open in the program exec runs in this one's place: + yes, or no, closed then, as pipe's fds are at first. 0, or minus an + errno. *) +proc keepOnExec*(fd: INT; yes: BOOL): INT; +var f: I32; +begin + f := C.fcntl(I32(fd), 1, 0); (* F_GETFD *) + if f < 0 then return failed(); end; + if yes then f := f & ~1; else f := f | 1; end; (* FD_CLOEXEC *) + if C.fcntl(I32(fd), 2, f) < 0 then return failed(); end; (* F_SETFD *) + return 0; +end; + (* size bytes of fresh, zeroed memory from the system, a multiple of pages, or nil. *) proc mmap*(size: INT): ^U8; @@ -371,6 +434,28 @@ return 0; end; +(* 0 if this user, by its real ids, may do with path what how asks: + reading, writing and executing; minus an errno, -13 (EACCES) if not. + An empty how asks only whether path is there. *) +proc access*(path: []U8; how: Accesses): INT; +var a: [4096]U8; m: I32; +begin + if not cstr(path, a) then return -36; end; + m := 0; (* F_OK *) + if reading in how then m := m | 4; end; (* R_OK *) + if writing in how then m := m | 2; end; (* W_OK *) + if executing in how then m := m | 1; end; (* X_OK *) + if C.access(ADR(a[0]), m) # 0 then return failed(); end; + return 0; +end; + +(* Sets the bits taken from every file this program, and the programs + it starts, make: the mask it was. *) +proc umask*(mask: INT): INT; +begin + return INT(C.umask(U32(mask & 1FFH)) & 1FFH); +end; + (* Makes path a symbolic link to target. *) proc symlink*(target, path: []U8): INT; var a, b: [4096]U8; @@ -525,6 +610,23 @@ _ := C.tcsetattr(I32(fd), 0, ADR(saved[0])); end; +(* The modes of the terminal on fd, as a shell keeps them for each job; + and 0, or minus an errno, -25 (ENOTTY) if it isn't a terminal. *) +proc getMode*(fd: INT): (Mode, INT); +var m: Mode; +begin + if C.tcgetattr(I32(fd), ADR(m.t[0])) # 0 then return m, failed(); end; + return m, 0; +end; + +(* Gives the terminal on fd the modes m, once what was written to it + is sent: 0, or minus an errno. *) +proc setMode*(fd: INT; m: Mode): INT; +begin + if C.tcsetattr(I32(fd), 1, ADR(m.t[0])) # 0 then return failed(); end; (* TCSADRAIN *) + return 0; +end; + (* The columns and rows of the terminal on fd; 80 by 24 if it doesn't say. *) proc termSize*(fd: INT): (INT, INT); @@ -890,6 +992,22 @@ return s[0..n]; end; +(* The environment's ith NAME=value, from 0, and true; "" and false + past the last. *) +proc envAt*(i: INT): ([]U8, BOOL); +var e: ^^U8; s: ^U8; j, n: INT; +begin + if i < 0 then return "", false; end; + e := C.environ; + for j := 0 to i do + if e[j] = nil then return "", false; end; + end; + s := e[i]; + n := 0; + while s[n] # 0 do INC(n); end; + return s[0..n], true; +end; + (* The working directory, good until the next call; and 0, or minus an errno. *) proc workDir*: ([]U8, INT); @@ -999,6 +1117,64 @@ return INT(C.getpid()); end; +(* The user this program runs as: its real id. *) +proc uid*: INT; +begin + return INT(C.getuid()); +end; + +(* The user whose rights this program has, its effective id: 0 is + root. *) +proc euid*: INT; +begin + return INT(C.geteuid()); +end; + +(* The name of this machine, good until the next call; and 0, or minus + an errno. *) +proc hostName*: ([]U8, INT); +var n: INT; +begin + if C.gethostname(ADR(host[0]), LEN(host) - 1) # 0 then return "", failed(); end; + host[LEN(host) - 1] := 0; (* POSIX doesn't promise one if it is cut *) + n := 0; + while host[n] # 0 do INC(n); end; + return host[0..n], 0; +end; + +(* The time on the processor, in ns, that this program has used, or, + if children, the programs it started and waited for: in the program + itself, and in the system for it. *) +proc cpuTime*(children: BOOL): (I64, I64); +var ru: Rusage; +begin + if children then _ := C.getrusage(-1, ^U8(ADR(ru))); (* RUSAGE_CHILDREN *) + else _ := C.getrusage(0, ^U8(ADR(ru))); (* RUSAGE_SELF *) + end; + return ru.user.sec * 1000000000 + I64(ru.user.usec) * 1000, + ru.system.sec * 1000000000 + I64(ru.system.usec) * 1000; +end; + +(* The limit which, one of the limit constants: the soft one, which + this program may raise up to the hard one; -1 for none; and 0, or + minus an errno. *) +proc limit*(which: INT): (I64, I64, INT); +var r: [2]I64; (* struct rlimit; RLIM_INFINITY is -1 *) +begin + if C.getrlimit(I32(which), ^U8(ADR(r))) # 0 then return 0, 0, failed(); end; + return r[0], r[1], 0; +end; + +(* Sets the limit which to soft and hard, -1 for none: 0, or minus an + errno. *) +proc setLimit*(which: INT; soft, hard: I64): INT; +var r: [2]I64; +begin + r := [soft, hard]; + if C.setrlimit(I32(which), ^U8(ADR(r))) # 0 then return failed(); end; + return 0; +end; + (* Moves the program pid, or this one if pid is 0, to the process group group, or to a new one it leads if group is 0. *) proc setGroup*(pid, group: INT): INT; @@ -1029,55 +1205,166 @@ or empty: glibc's execvp looks in these. *) const defaultPath = "/bin:/usr/bin"; +(* The ith directory of search, a list of them with : between, as PATH + is: dir, "." if it is empty, and where the next starts; -1 past the + last. *) +proc pathDir(search: []U8; i: INT): ([]U8, INT); +var j: INT; +begin + if i > LEN(search) then return "", -1; end; + j := i; + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; + if j = i then return ".", j + 1; end; + return search[i..j], j + 1; +end; + +(* dir, a /, and name, a C string in buf; false if it doesn't fit. *) +proc joinPath(dir, name, buf: []U8): BOOL; +var n: INT; +begin + n := LEN(dir) + 1 + LEN(name); + if n >= LEN(buf) then return false; end; + COPY(buf, dir); + buf[LEN(dir)] := '/'; + COPY(buf[LEN(dir) + 1..n], name); + buf[n] := 0; + return true; +end; + +(* Whether name holds a /, and so is a path, not a name to look for. *) +proc isPath(name: []U8): BOOL; +var i: INT; +begin + for i := 0 to LEN(name) - 1 do + if name[i] = '/' then return true; end; + end; + return false; +end; + (* 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; +proc run(name, search, buf: []U8; argv, envp: ^^U8); +var dir: []U8; i: 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 isPath(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); + dir, i := pathDir(search, 0); + while i >= 0 do + if joinPath(dir, name, buf) then _ := C.execve(ADR(buf[0]), argv, envp); end; + dir, i := pathDir(search, i); + end; +end; + +(* The program exec runs for name, as a C string in buf: name itself + if it has a /, or else the first file of that name in the + directories of search that is no directory and that this user may + run. 0; or -2 (ENOENT) if there is none, or -13 (EACCES) if one is + there and can't be run. *) +proc find(name, search, buf: []U8): INT; +var dir: []U8; i, err, r: INT; f: Info; +begin + if isPath(name) then + if not cstr(name, buf) then return -36; end; + return 0; + end; + r := -2; (* ENOENT *) + dir, i := pathDir(search, 0); + while (LEN(name) > 0) and (i >= 0) do + if joinPath(dir, name, buf) then + f, err := info(buf[0..LEN(dir) + 1 + LEN(name)]); + if (err = 0) and not f.dir then + if C.access(ADR(buf[0]), 1) = 0 then return 0; end; (* X_OK *) + r := -13; (* EACCES *) + end; + end; + dir, i := pathDir(search, i); + end; + return r; +end; + +(* The name of the terminal of the pseudo-terminal fd, as a C string + in name, which isn't safe to ask after a fork: 0, or minus an + errno. *) +proc ptyName(fd: INT; name: []U8): INT; +var s: ^U8; n: INT; +begin + s := C.ptsname(I32(fd)); + if s = nil then return failed(); end; + n := 0; + while s[n] # 0 do INC(n); end; + if not cstr(s[0..n], name) then return -36; end; + return 0; +end; + +(* Closes every fd from 3 on but those of keep, which stay open in the + program exec starts. It runs in spawn's child, after the fork, so it + takes no memory: it closes the runs between the fds kept, the lowest + first, each with one close_range. *) +proc closeAllBut(keep: []INT); +var lo, next, i: INT; +begin + lo := 3; + repeat + next := -1; + for i := 0 to LEN(keep) - 1 do + if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end; + end; + if next < 0 then + _ := C.close_range(U32(lo), 0FFFFFFFFH, 0); (* the rest *) + else + if next > lo then _ := C.close_range(U32(lo), U32(next - 1), 0); end; + _ := C.fcntl(I32(next), 2, 0); (* F_SETFD: kept on exec *) + lo := next + 1; + end; + until next < 0; +end; + +(* 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. *) +proc takeOn(p: Program; name, dir: []U8): INT; +var fd: I32; i: INT; +begin + if p.tty # 0 then + _ := C.setsid(); + fd := C.open(ADR(name[0]), 2); (* O_RDWR *) + if fd < 0 then return failed(); end; + if C.ioctl(fd, TIOCSCTTY, 0) # 0 then return failed(); end; + for i := 0 to 2 do _ := C.dup2(fd, I32(i)); end; + else + if p.group = -1 then _ := C.setpgid(0, 0); + 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; - i := j + 1; end; + if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then return failed(); end; + return 0; end; -(* Starts the program p describes, found as exec says: its process id, +(* 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. *) proc spawn*(p: Program): INT; var - 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 *) + a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, err, i, g: INT; search: []U8; + all, old, empty: [16]U64; dfl: Sigaction; pid: I32; (* sigset_t: 128 bytes *) begin if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) if p.tty # 0 then - s := C.ptsname(I32(p.tty)); (* here, since it isn't safe after fork *) - if s = nil then return failed(); end; - n := 0; - while s[n] # 0 do INC(n); end; - if not cstr(s[0..n], name) then return -36; end; + err := ptyName(p.tty, name); (* here, since it isn't safe after fork *) + if err # 0 then return err; end; end; none := nil; if p.onlyEnv then @@ -1092,25 +1379,11 @@ _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *) pid := C.fork(); if pid = 0 then - if p.tty # 0 then - _ := C.setsid(); - fd := C.open(ADR(name[0]), 2); (* O_RDWR *) - if (fd < 0) or (C.ioctl(fd, TIOCSCTTY, 0) # 0) then C._exit(127); end; - for i := 0 to 2 do _ := C.dup2(fd, I32(i)); end; - else - if p.group = -1 then _ := C.setpgid(0, 0); - 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; - if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then C._exit(127); end; - _ := C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *) + if takeOn(p, name, dir) # 0 then C._exit(127); end; + closeAllBut(p.more); for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end; _ := C.sigprocmask(2, ^U8(ADR(empty)), nil); - exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); + run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); C._exit(127); end; err := failed(); @@ -1127,6 +1400,56 @@ return INT(pid); end; +(* Runs the program p describes in this one's place, as spawn would + start it, but keeping this program's other fds as they are, open + unless keepOnExec said no; the fds of p.more are kept. It returns + only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no + program to run, before anything is done; or, once the program's + terminal, group, fds and working directory are as p asks, which + stay so, minus the errno that stopped it, with this program's + signals given back as they were. *) +proc exec*(p: Program): INT; +var + a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, r, i: INT; search: []U8; + all, old, empty: [16]U64; dfl: Sigaction; acts: [65]Sigaction; +begin + if not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) + if p.tty # 0 then + r := ptyName(p.tty, name); + if r # 0 then return r; end; + end; + search := env("PATH"); + if LEN(search) = 0 then search := defaultPath; end; + r := find(p.path, search, a); + if r < 0 then return r; end; + none := nil; + if p.onlyEnv then + argv, size := argsAndEnv(p.args, p.env, ADR(none)); + else + argv, size := argsAndEnv(p.args, p.env, C.environ); + end; + if argv = nil then return -12; end; (* ENOMEM *) + r := takeOn(p, name, dir); + if r = 0 then + for i := 0 to LEN(p.more) - 1 do + if p.more[i] > 2 then _ := keepOnExec(p.more[i], true); end; + end; + (* every signal as it is at first and none blocked, as spawn + gives them, each kept to give back if execve fails *) + for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; + _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *) + for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), ^U8(ADR(acts[i]))); end; + _ := C.sigprocmask(2, ^U8(ADR(empty)), nil); + _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1)); + r := failed(); + _ := C.sigprocmask(2, ^U8(ADR(all)), nil); + for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(acts[i])), nil); end; + _ := C.sigprocmask(2, ^U8(ADR(old)), nil); + end; + munmap(^U8(argv), size); + return r; +end; + (* Waits for the program pid, or any of this program's if pid is -1, 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 @@ -1318,6 +1641,97 @@ if ignore(sigInt, false) # 0 then exit(2); end; end; +(* The test of what a shell needs: fds copied, moved and kept on + exec; a program given an fd at its own number, and one not; access, + the umask and the environment by number; a terminal's modes; time + used; limits; who and where; and exec, with nothing to run, and + with a program that can't be run, after which the signals caught + are caught still. *) +proc testShell; +var + r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; + m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; +begin + r, w := pipe(); + if w < 0 then exit(2); end; + fd := dup(w); + if (fd < 0) or (write(fd, "ab") # 2) or (read(r, buf) # 2) then exit(2); end; + close(fd); + if (dupTo(w, 9) # 9) or (write(9, "c") # 1) or (read(r, buf) # 1) or (buf[0] # 'c') then exit(2); end; + if (keepOnExec(9, false) # 0) or (keepOnExec(9, true) # 0) or (keepOnExec(-1, true) # -9) then exit(2); end; + child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo more >&9"], more: [9]}); + close(9); + close(w); + n := 0; + repeat + k := read(r, buf[n..LEN(buf)]); + if k > 0 then INC(n, k); end; + until k <= 0; + close(r); + if buf[0..n] # "more\n" then exit(2); end; + k, st, c := wait(child, true); + if (st # exited) or (c # 0) then exit(2); end; + r, w := pipe(); + if (w < 0) or (dupTo(w, 9) # 9) then exit(2); end; + child := spawn({path: "/bin/sh", args: ["sh", "-c", "{ echo none >&9; } 2>/dev/null"]}); (* 9 is closed in it *) + close(9); + close(w); + if read(r, buf) # 0 then exit(2); end; + close(r); + k, st, c := wait(child, true); + if (st # exited) or (c = 0) then exit(2); end; + + if (access("/bin/sh", {executing}) # 0) or (access("/", {reading}) # 0) + or (access("/no/such/file", {}) # -2) or (open("/bin/sh", {executing}) # -22) then exit(2); end; + mask := umask(3FH); (* 077 *) + if umask(mask) # 3FH then exit(2); end; + path := false; + n := 0; + repeat + e, ok := envAt(n); + if ok and (LEN(e) > 5) and (e[0..5] = "PATH=") then path := true; end; + INC(n); + until not ok; + e, ok := envAt(-1); + if not path or ok or (LEN(e) # 0) then exit(2); end; + + (* a terminal's modes, kept and given back *) + pty := openPty(); + m, err := getMode(pty); + if (err # 0) or not rawMode(pty) then exit(2); end; + raw, err := getMode(pty); + if (err # 0) or (raw.t = m.t) or (setMode(pty, m) # 0) then exit(2); end; + raw, err := getMode(pty); + if (err # 0) or (raw.t # m.t) then exit(2); end; + m, err := getMode(-1); + if err # -9 then exit(2); end; (* EBADF *) + r, w := pipe(); + m, err := getMode(r); + if err # -25 then exit(2); end; (* ENOTTY *) + close(r); + close(w); + close(pty); + + user, system := cpuTime(false); + if (user < 0) or (system < 0) or (user + system = 0) then exit(2); end; + user, system := cpuTime(true); + if (user < 0) or (system < 0) then exit(2); end; + soft, hard, err := limit(limitFiles); + if (err # 0) or (soft < 16) or (setLimit(limitFiles, soft - 1, hard) # 0) then exit(2); end; + s2, h2, err := limit(limitFiles); + if (err # 0) or (s2 # soft - 1) or (h2 # hard) or (setLimit(limitFiles, soft, hard) # 0) then exit(2); end; + soft, hard, err := limit(10000); + if err # -22 then exit(2); end; (* EINVAL *) + e, err := hostName(); + if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; + + sigs := catch([sigUsr1]); + while caught(sigs) # 0 do end; (* the sigChld of the programs above *) + if (sigs < 0) or (exec({path: "no-such-program-on-path", args: ["x"]}) # -2) then exit(2); end; + if exec({path: "/etc/passwd", args: ["x"]}) # -13 then exit(2); end; (* not executable *) + if (kill(pid(), sigUsr1) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigUsr1) then exit(2); end; +end; + (* The test of time, random bytes and the network: a sleep the clock sees; addresses read and refused; a TCP connection on the loopback; a datagram to a port where no one listens, refused; servers on @@ -1548,6 +1962,7 @@ close(l); _ := remove("/tmp/raz-sys-socket"); testPrograms; + testShell; testNet; end; diff --git a/test/err/Clash.err b/test/err/Clash.err index ce01459..71cb9e1 100644 --- a/test/err/Clash.err +++ b/test/err/Clash.err @@ -1,5 +1,5 @@ -lib/Sys.raz:12:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz - 12 | import C; +lib/Sys.raz:16:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz + 16 | import C; | ^ help: a program has one module of each name; a symbolic link to one of these files chooses it raz: 1 error diff --git a/test/libc/ExecC.expect b/test/libc/ExecC.expect new file mode 100644 index 0000000..5f1e3f6 --- /dev/null +++ b/test/libc/ExecC.expect @@ -0,0 +1,2 @@ +replaced +closed diff --git a/test/libc/ExecC.raz b/test/libc/ExecC.raz new file mode 100644 index 0000000..5958a5b --- /dev/null +++ b/test/libc/ExecC.raz @@ -0,0 +1,15 @@ +(* Sys.exec over the C library: this program replaced by sh, with an + fd it keeps, at its own number, and one it doesn't; sh's output and + status are its. *) +module ExecC; +import Sys; + +proc main*: INT; +begin + if (Sys.dupTo(Sys.stdout, 7) # 7) or (Sys.dupTo(Sys.stdout, 8) # 8) then return 1; end; + if (Sys.keepOnExec(7, false) # 0) or (Sys.keepOnExec(8, false) # 0) then return 1; end; + return Sys.exec({path: "sh", env: ["X=replaced"], more: [7], + args: ["sh", "-c", "echo $X >&7; { echo kept >&8; } 2>/dev/null || echo closed; exit 5"]}); +end; + +end ExecC. diff --git a/test/libc/ExecC.status b/test/libc/ExecC.status new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/test/libc/ExecC.status @@ -0,0 +1 @@ +5 diff --git a/test/run/Exec.expect b/test/run/Exec.expect new file mode 100644 index 0000000..5f1e3f6 --- /dev/null +++ b/test/run/Exec.expect @@ -0,0 +1,2 @@ +replaced +closed diff --git a/test/run/Exec.raz b/test/run/Exec.raz new file mode 100644 index 0000000..2e4348b --- /dev/null +++ b/test/run/Exec.raz @@ -0,0 +1,14 @@ +(* Sys.exec: this program replaced by sh, with an fd it keeps, at its + own number, and one it doesn't; sh's output and status are its. *) +module Exec; +import Sys; + +proc main*: INT; +begin + if (Sys.dupTo(Sys.stdout, 7) # 7) or (Sys.dupTo(Sys.stdout, 8) # 8) then return 1; end; + if (Sys.keepOnExec(7, false) # 0) or (Sys.keepOnExec(8, false) # 0) then return 1; end; + return Sys.exec({path: "sh", env: ["X=replaced"], more: [7], + args: ["sh", "-c", "echo $X >&7; { echo kept >&8; } 2>/dev/null || echo closed; exit 5"]}); +end; + +end Exec. diff --git a/test/run/Exec.status b/test/run/Exec.status new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/test/run/Exec.status @@ -0,0 +1 @@ +5