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<file and <(...) need. - exec: the program in this one's place. It finds the program first (find: on PATH, no directory, executable), so that -2 or -13 leave everything as it was; past that, what p asks of the terminal, group, fds and directory is done (takeOn, shared with spawn's child), and if execve still fails the signals are given back as they were. - access, with Access's new executing, which open refuses; umask; envAt; getMode and setMode (TCSADRAIN), a terminal's modes kept for each job; cpuTime; limit and setLimit, with the limit constants; uid, euid and hostName; sigUsr1, sigUsr2 and sigAlrm. The numbers of the signals and limits are each target's; the C Sys's header now says it has Linux's, as it says what else is glibc's. Each Sys's test gains testShell; test/run/Exec and test/libc/ExecC run sh by exec with an fd kept and one not. raz-lib.md says it all.
| @@ -369,128 +369,6 @@ | ||
| 369 | 369 | procedure values) take years; the narrow form here leaves record |
| 370 | 370 | lifetimes out, and so the first false alarm above, on purpose. |
| 371 | 371 | |
| 372 | -## What a shell needs of `Sys` | |
| 373 | - | |
| 374 | -For concha, the shell written in raz. `examples/Sh.raz` shows how far | |
| 375 | -`Sys` goes today: pipelines, `<`, `>`, `>>` and `2>`, and job control | |
| 376 | -through `spawn`'s `group` and `foreground`. A shell people use every | |
| 377 | -day needs more. Each item below is library growth that breaks no | |
| 378 | -program, and each has a C call behind it, for `Sys.raz` over the C | |
| 379 | -library. | |
| 380 | - | |
| 381 | -**Moving file descriptors.** `2>&1`, `3>log`, `exec >out` and `{ a; | |
| 382 | -b; } >f` move or copy the shell's own descriptors, around a builtin | |
| 383 | -or for good; `Program.fds` covers only a child's first three. | |
| 384 | - | |
| 385 | -``` | |
| 386 | -proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) | |
| 387 | -proc dupTo*(fd, to: INT): INT; (* to made the same file as fd, closing what it was *) | |
| 388 | -proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd passed to programs spawn starts, or not *) | |
| 389 | -``` | |
| 390 | - | |
| 391 | -`Program` gains `more*: []INT`, descriptors passed on to the program | |
| 392 | -at the same numbers, for `3<file cmd` and for `<(...)`, which hands a | |
| 393 | -program `/dev/fd/N`. `spawn` closes every other one, as it does now. | |
| 394 | -The C calls: `dup`, `dup2`, `fcntl` with `FD_CLOEXEC`. | |
| 395 | - | |
| 396 | -**Replacing the shell.** `exec cmd` runs a program in the shell's | |
| 397 | -place, as does the last command of `sh -c`, saving a process. | |
| 398 | - | |
| 399 | -``` | |
| 400 | -proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't, with minus an errno *) | |
| 401 | -``` | |
| 402 | - | |
| 403 | -`p.group`, `p.foreground` and `p.tty` are as in `spawn`. The C call | |
| 404 | -is `execve`. | |
| 405 | - | |
| 406 | -**Finding programs.** A shell looks a name up along `PATH`, and `test | |
| 407 | --x`, `-r` and `-w` ask whether this user may run, read or write a | |
| 408 | -file, which `Info.mode` alone can't say: it depends on the user, the | |
| 409 | -file's owner and group, and the file system (read-only, `noexec`). | |
| 410 | - | |
| 411 | -``` | |
| 412 | -proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may, by its real ids; reading, writing, and running *) | |
| 413 | -``` | |
| 414 | - | |
| 415 | -`Access` gains `running`, which `open` refuses. The C call is | |
| 416 | -`access`. | |
| 417 | - | |
| 418 | -**The environment.** `env` reads one variable. A shell starts from | |
| 419 | -its whole environment, to list it (`env`, `export -p`) and pass it on | |
| 420 | -with what was exported: | |
| 421 | - | |
| 422 | -``` | |
| 423 | -proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) | |
| 424 | -``` | |
| 425 | - | |
| 426 | -Nothing to set it: the shell keeps its own table and passes it whole | |
| 427 | -with `onlyEnv`. | |
| 428 | - | |
| 429 | -**The umask.** `umask` shows and sets the bits taken from every file | |
| 430 | -the shell, and what it starts, makes. | |
| 431 | - | |
| 432 | -``` | |
| 433 | -proc umask*(mask: INT): INT; (* the mask set to mask; the one it was *) | |
| 434 | -``` | |
| 435 | - | |
| 436 | -**A terminal's modes, kept for each job.** A job stopped by Ctrl-Z | |
| 437 | -in vi leaves the terminal raw; `fg` must give it back the modes it | |
| 438 | -had, and the shell its own between. `rawMode` and `restoreMode` keep | |
| 439 | -one saved state; a shell needs as many as it has jobs. | |
| 440 | - | |
| 441 | -``` | |
| 442 | -type Mode* = record ... end; (* a terminal's modes; the target's *) | |
| 443 | -proc getMode*(fd: INT): (Mode, INT); | |
| 444 | -proc setMode*(fd: INT; m: Mode): INT; (* once the output written is sent, as TCSADRAIN *) | |
| 445 | -``` | |
| 446 | - | |
| 447 | -The C calls: `tcgetattr` and `tcsetattr`. | |
| 448 | - | |
| 449 | -**Time used.** `time pipeline` and `times` report CPU time used, the | |
| 450 | -shell's and its children's, which `clock` can't tell. | |
| 451 | - | |
| 452 | -``` | |
| 453 | -proc cpuTime*(children: BOOL): (I64, I64); (* user and system time, in ns: this program's, or its waited-for children's *) | |
| 454 | -``` | |
| 455 | - | |
| 456 | -The C call is `getrusage`. | |
| 457 | - | |
| 458 | -**Limits.** `ulimit -n`, `-s`, `-c`: | |
| 459 | - | |
| 460 | -``` | |
| 461 | -const limitFiles* = 7; limitStack* = 3; limitCore* = 4; ... (* each target's numbers *) | |
| 462 | -proc limit*(which: INT): (I64, I64, INT); (* soft and hard; -1 for none *) | |
| 463 | -proc setLimit*(which: INT; soft, hard: I64): INT; | |
| 464 | -``` | |
| 465 | - | |
| 466 | -The C calls: `getrlimit` and `setrlimit`. | |
| 467 | - | |
| 468 | -**Who and where.** A prompt shows the user, the host, and `#` for | |
| 469 | -root; `~` is the home directory, and `~name` someone else's. | |
| 470 | - | |
| 471 | -``` | |
| 472 | -proc uid*: INT; (* the real user id *) | |
| 473 | -proc euid*: INT; (* the effective one: 0 is root *) | |
| 474 | -proc hostName*: ([]U8, INT); (* good until the next call *) | |
| 475 | -``` | |
| 476 | - | |
| 477 | -`~name` is the sixth field of `name`'s line in `/etc/passwd`, which a | |
| 478 | -shell reads with `Files.read`; `HOME` is `~`. The C calls: `getuid`, | |
| 479 | -`geteuid`, `gethostname`. | |
| 480 | - | |
| 481 | -**More signals.** `trap` and `kill -USR1` name them: | |
| 482 | - | |
| 483 | -``` | |
| 484 | -const sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; | |
| 485 | -``` | |
| 486 | - | |
| 487 | -**Not needed.** Globs are rwalk's `Glob` over `openDir` and | |
| 488 | -`entryKind`. `read -t` is `ready`. Here-documents are a `pipe`, or a | |
| 489 | -`memFile` when long. `wait` already says `stopped`; a job continued | |
| 490 | -by another program (`WCONTINUED`) would need a fifth `Status`, which | |
| 491 | -breaks a `match` on `Status` that names all four, so it is left out: | |
| 492 | -the shell knows when it continues a job itself. | |
| 493 | - | |
| 494 | 372 | ## Enumerations' names, each its own |
| 495 | 373 | |
| 496 | 374 | An enumeration's names are declared in the scope around its type |
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | | Module | What | |
| 12 | 12 | |---|---| |
| 13 | 13 | | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | |
| 14 | -| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | | |
| 14 | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | | |
| 15 | 15 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 16 | 16 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 17 | 17 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| @@ -161,9 +161,13 @@ | ||
| 161 | 161 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 162 | 162 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| 163 | 163 | sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; |
| 164 | + sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; | |
| 165 | + (* the limits limit and setLimit take, by their numbers on Linux; each target has its own *) | |
| 166 | + limitCpu* = 0; limitFileSize* = 1; limitData* = 2; limitStack* = 3; | |
| 167 | + limitCore* = 4; limitProcs* = 6; limitFiles* = 7; limitMemory* = 9; | |
| 164 | 168 | |
| 165 | 169 | type |
| 166 | - Access* = (reading, writing, creating, exclusive, truncating, appending); | |
| 170 | + Access* = (reading, writing, creating, exclusive, truncating, appending, executing); | |
| 167 | 171 | Accesses* = set of Access; |
| 168 | 172 | Info* = record |
| 169 | 173 | size*: I64; (* in bytes *) |
| @@ -187,8 +191,10 @@ | ||
| 187 | 191 | group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) |
| 188 | 192 | foreground*: BOOL; (* its group takes the terminal on standard input *) |
| 189 | 193 | tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) |
| 194 | + more*: []INT; (* fds above 2 passed on to it, at the same numbers *) | |
| 190 | 195 | end; |
| 191 | 196 | Status* = (running, exited, killed, stopped); |
| 197 | + Mode* = record ... end; (* a terminal's modes; its fields are the target's *) | |
| 192 | 198 | Peer* = record ... end; (* whom a datagram came from *) |
| 193 | 199 | |
| 194 | 200 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| @@ -204,6 +210,9 @@ | ||
| 204 | 210 | proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *) |
| 205 | 211 | proc lock*(fd: INT; wait: BOOL): INT; (* the file's lock, held until fd is closed *) |
| 206 | 212 | proc close*(fd: INT); |
| 213 | +proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) | |
| 214 | +proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) | |
| 215 | +proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *) | |
| 207 | 216 | proc isTerminal*(fd: INT): BOOL; |
| 208 | 217 | proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| 209 | 218 | proc munmap*(p: ^U8; size: INT); |
| @@ -215,6 +224,8 @@ | ||
| 215 | 224 | proc removeDir*(path: []U8): INT; (* an empty directory *) |
| 216 | 225 | proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) |
| 217 | 226 | proc chmod*(path: []U8; mode: INT): INT; |
| 227 | +proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) | |
| 228 | +proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *) | |
| 218 | 229 | proc symlink*(target, path: []U8): INT; (* path made a link to target *) |
| 219 | 230 | proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) |
| 220 | 231 | proc info*(path: []U8): (Info, INT); (* following a symbolic link *) |
| @@ -229,6 +240,8 @@ | ||
| 229 | 240 | |
| 230 | 241 | proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) |
| 231 | 242 | proc restoreMode*(fd: INT); (* as rawMode found it *) |
| 243 | +proc getMode*(fd: INT): (Mode, INT); (* the terminal's modes, to set again *) | |
| 244 | +proc setMode*(fd: INT; m: Mode): INT; (* once what was written to it is sent *) | |
| 232 | 245 | proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) |
| 233 | 246 | proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 234 | 247 | proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) |
| @@ -236,6 +249,7 @@ | ||
| 236 | 249 | proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) |
| 237 | 250 | |
| 238 | 251 | proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) |
| 252 | +proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) | |
| 239 | 253 | proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) |
| 240 | 254 | proc setWorkDir*(path: []U8): INT; |
| 241 | 255 | |
| @@ -243,12 +257,19 @@ | ||
| 243 | 257 | proc openPty*: INT; (* a pseudo-terminal: the fd of its controlling side *) |
| 244 | 258 | proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) |
| 245 | 259 | proc spawn*(p: Program): INT; (* a program started: its process id *) |
| 260 | +proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) | |
| 246 | 261 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) |
| 247 | 262 | proc kill*(pid, signal: INT): INT; |
| 248 | 263 | proc pid*: INT; (* this program's process id *) |
| 249 | 264 | proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *) |
| 250 | 265 | proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *) |
| 251 | 266 | proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *) |
| 267 | +proc uid*: INT; (* the user this program runs as *) | |
| 268 | +proc euid*: INT; (* the user whose rights it has: 0 is root *) | |
| 269 | +proc hostName*: ([]U8, INT); (* this machine's name, good until the next call *) | |
| 270 | +proc cpuTime*(children: BOOL): (I64, I64); (* ns used in the program and in the system; or by its children *) | |
| 271 | +proc limit*(which: INT): (I64, I64, INT); (* the soft limit and the hard; -1 for none *) | |
| 272 | +proc setLimit*(which: INT; soft, hard: I64): INT; | |
| 252 | 273 | |
| 253 | 274 | proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *) |
| 254 | 275 | proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *) |
| @@ -346,8 +367,9 @@ | ||
| 346 | 367 | `p.dir`. Its standard input, output and error are `p.fds`; a 0 gives |
| 347 | 368 | it this program's own, so `{path: "ls", args: ["ls"]}` runs |
| 348 | 369 | `ls` as a shell runs it, with every other field zero. It gets no |
| 349 | -other open file, every signal does what it does at first, and none | |
| 350 | -is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a | |
| 370 | +other open file but those of `p.more`, each at its own number, as a | |
| 371 | +shell passes `3<file` on, every signal does what it does at first, and | |
| 372 | +none is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a | |
| 351 | 373 | session on that terminal: it is its controlling terminal, and its |
| 352 | 374 | standard input, output and error, and `p.fds` and `p.group` are not |
| 353 | 375 | used. |
| @@ -362,6 +384,41 @@ | ||
| 362 | 384 | moves a program to a group, as the shell does for each program it |
| 363 | 385 | starts, for whichever of the two runs first; a shell runs in a group |
| 364 | 386 | of its own, `setGroup(0, 0)`. |
| 387 | + | |
| 388 | +`exec` runs the program `p` describes in this program's place, as a | |
| 389 | +shell's `exec cmd` does, found and started as `spawn` would start it, | |
| 390 | +`p.tty`, `p.group`, `p.foreground`, `p.fds` and `p.dir` included, | |
| 391 | +except that it keeps this program's other open files: each stays open | |
| 392 | +in the program unless `keepOnExec(fd, false)` was said of it, as it is | |
| 393 | +of `pipe`'s, and those of `p.more` are kept. It returns only if the | |
| 394 | +program can't run: -2 (ENOENT) or -13 (EACCES) if there is none to | |
| 395 | +run, with nothing done; or, if the program is found but won't start, | |
| 396 | +minus the errno, with what `p` asks of the terminal, the group, the | |
| 397 | +files and the directory done, and this program's signals as they were. | |
| 398 | + | |
| 399 | +A shell moves its own files with `dup` and `dupTo`, for `2>&1` and | |
| 400 | +`exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the | |
| 401 | +same open file as `fd`, closing what `into` was, and `dup` gives a new | |
| 402 | +fd for it, the lowest free, as a shell saves a file to give it back. | |
| 403 | +`access` says whether this user, by its real ids, may read, write or | |
| 404 | +execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` | |
| 405 | +alone can't say; an empty `how` asks only whether it is there. | |
| 406 | +`open` refuses `executing`, with -22 (EINVAL). `umask` sets the bits | |
| 407 | +taken away from the mode of every file this program, and those it | |
| 408 | +starts, make, and says what they were. `envAt` gives the environment | |
| 409 | +one variable at a time, from 0, for a shell to list it and to pass it | |
| 410 | +on whole with `onlyEnv`. `getMode` reads a terminal's modes, and | |
| 411 | +`setMode` sets them again once what was written is sent, so that a | |
| 412 | +shell keeps them for each job: a program stopped by Ctrl-Z with its | |
| 413 | +terminal raw gets it back raw, and the shell its own between. | |
| 414 | +`cpuTime` says how long the processor ran this program, in its own | |
| 415 | +code and in the system for it, or, if `children`, the programs it | |
| 416 | +started and has waited for, as `time` and `times` say. `limit` gives a | |
| 417 | +limit, `limitFiles` say, soft and hard, and `setLimit` sets them, as | |
| 418 | +`ulimit` does: a program may raise its soft limit up to the hard one, | |
| 419 | +and lower either. `uid` is the user this program runs as, `euid` the | |
| 420 | +one whose rights it has, 0 for root, as a prompt shows `#`; `hostName` | |
| 421 | +is the machine's name. | |
| 365 | 422 | |
| 366 | 423 | `wait` waits for the program `pid` to change, or for any of this |
| 367 | 424 | program's if `pid` is -1, and says which one, and how: `exited`, with |
| @@ -72,11 +72,20 @@ | ||
| 72 | 72 | extern proc fork*: I32; |
| 73 | 73 | extern proc setsid*: I32; |
| 74 | 74 | extern proc dup2*(fd, fd2: I32): I32; |
| 75 | +extern proc dup*(fd: I32): I32; | |
| 75 | 76 | extern proc close_range*(first, last: U32; flags: I32): I32; |
| 76 | 77 | extern proc execve*(path: ^U8; argv, envp: ^^U8): I32; |
| 77 | 78 | extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32; |
| 78 | 79 | extern proc kill*(pid, signal: I32): I32; |
| 79 | 80 | extern proc getpid*: I32; |
| 81 | +extern proc getuid*: U32; | |
| 82 | +extern proc geteuid*: U32; | |
| 83 | +extern proc gethostname*(name: ^U8; n: UINT): I32; | |
| 84 | +extern proc access*(path: ^U8; how: I32): I32; | |
| 85 | +extern proc umask*(mask: U32): U32; | |
| 86 | +extern proc getrusage*(who: I32; usage: ^U8): I32; | |
| 87 | +extern proc getrlimit*(which: I32; limit: ^U8): I32; | |
| 88 | +extern proc setrlimit*(which: I32; limit: ^U8): I32; | |
| 80 | 89 | extern proc getpgrp*: I32; |
| 81 | 90 | extern proc setpgid*(pid, group: I32): I32; |
| 82 | 91 | extern proc tcsetpgrp*(fd, group: I32): I32; |
| @@ -13,12 +13,22 @@ | ||
| 13 | 13 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 14 | 14 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| 15 | 15 | sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; |
| 16 | + sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; | |
| 17 | + (* the limits limit and setLimit take, by their numbers on Linux *) | |
| 18 | + limitCpu* = 0; (* seconds of the processor *) | |
| 19 | + limitFileSize* = 1; (* bytes in a file it writes *) | |
| 20 | + limitData* = 2; (* bytes of its data *) | |
| 21 | + limitStack* = 3; (* bytes of its stack *) | |
| 22 | + limitCore* = 4; (* bytes of a core dump *) | |
| 23 | + limitProcs* = 6; (* processes of its user *) | |
| 24 | + limitFiles* = 7; (* one more than its highest fd *) | |
| 25 | + limitMemory* = 9; (* bytes of its address space *) | |
| 16 | 26 | |
| 17 | 27 | type |
| 18 | 28 | (* What open is asked for: reading, writing or both, and what else |
| 19 | 29 | it does to the file. *) |
| 20 | 30 | Access* = (reading, writing, creating, exclusive, truncating, |
| 21 | - appending); | |
| 31 | + appending, executing); | |
| 22 | 32 | Accesses* = set of Access; (* what open is given *) |
| 23 | 33 | |
| 24 | 34 | (* What info says of a file. *) |
| @@ -57,6 +67,7 @@ | ||
| 57 | 67 | group*: INT; (* its group: 0 for this program's, -1 a new one *) |
| 58 | 68 | foreground*: BOOL; (* its group gets the terminal on standard input *) |
| 59 | 69 | tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) |
| 70 | + more*: []INT; (* fds above 2 passed on to it at the same numbers *) | |
| 60 | 71 | end; |
| 61 | 72 | (* How a program that wait waited for changed. *) |
| 62 | 73 | Status* = (running, exited, killed, stopped); |
| @@ -67,6 +78,11 @@ | ||
| 67 | 78 | line: U8; |
| 68 | 79 | cc: [19]U8; |
| 69 | 80 | end; |
| 81 | + (* A terminal's modes, as getMode reads them and setMode sets them. *) | |
| 82 | + Mode* = record | |
| 83 | + t: Termios; | |
| 84 | + end; | |
| 85 | + | |
| 70 | 86 | Winsize = record rows, cols, xpixel, ypixel: U16; end; |
| 71 | 87 | Pollfd = record fd: I32; events, revents: I16; end; |
| 72 | 88 | |
| @@ -113,6 +129,7 @@ | ||
| 113 | 129 | saved: Termios; (* the terminal as rawMode found it *) |
| 114 | 130 | caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *) |
| 115 | 131 | sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) |
| 132 | + uts: [390]U8; (* struct utsname, which hostName gives the nodename of *) | |
| 116 | 133 | |
| 117 | 134 | (* Writes s to the file descriptor fd; the number of bytes written, or |
| 118 | 135 | minus an errno. *) |
| @@ -161,6 +178,7 @@ | ||
| 161 | 178 | proc openMode*(path: []U8; how: Accesses; mode: INT): INT; |
| 162 | 179 | var buf: [4096]U8; |
| 163 | 180 | begin |
| 181 | + if executing in how then return -22; end; (* EINVAL: access asks it, open can't *) | |
| 164 | 182 | if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) |
| 165 | 183 | return SYSCALL(2, ADR(buf), flags(how), mode); |
| 166 | 184 | end; |
| @@ -237,6 +255,32 @@ | ||
| 237 | 255 | _ := SYSCALL(3, fd); |
| 238 | 256 | end; |
| 239 | 257 | |
| 258 | +(* A new file descriptor for the file fd is open on, the lowest free: | |
| 259 | + it, or minus an errno. *) | |
| 260 | +proc dup*(fd: INT): INT; | |
| 261 | +begin | |
| 262 | + return SYSCALL(32, fd); (* dup *) | |
| 263 | +end; | |
| 264 | + | |
| 265 | +(* Makes the file descriptor into stand for the file fd is open on, | |
| 266 | + closing what into was first: into, or minus an errno. *) | |
| 267 | +proc dupTo*(fd, into: INT): INT; | |
| 268 | +begin | |
| 269 | + return SYSCALL(33, fd, into); (* dup2 *) | |
| 270 | +end; | |
| 271 | + | |
| 272 | +(* Whether fd stays open in the program exec runs in this one's place: | |
| 273 | + yes, or no, closed then, as pipe's fds are at first. 0, or minus an | |
| 274 | + errno. *) | |
| 275 | +proc keepOnExec*(fd: INT; yes: BOOL): INT; | |
| 276 | +var f: INT; | |
| 277 | +begin | |
| 278 | + f := SYSCALL(72, fd, 1); (* fcntl: F_GETFD *) | |
| 279 | + if f < 0 then return f; end; | |
| 280 | + if yes then f := f & ~1; else f := f | 1; end; (* FD_CLOEXEC *) | |
| 281 | + return SYSCALL(72, fd, 2, f); (* F_SETFD *) | |
| 282 | +end; | |
| 283 | + | |
| 240 | 284 | (* size bytes of fresh, zeroed memory from the system, a multiple of |
| 241 | 285 | pages, or nil. *) |
| 242 | 286 | proc mmap*(size: INT): ^U8; |
| @@ -325,6 +369,27 @@ | ||
| 325 | 369 | return SYSCALL(90, ADR(a), mode); |
| 326 | 370 | end; |
| 327 | 371 | |
| 372 | +(* 0 if this user, by its real ids, may do with path what how asks: | |
| 373 | + reading, writing and executing; minus an errno, -13 (EACCES) if not. | |
| 374 | + An empty how asks only whether path is there. *) | |
| 375 | +proc access*(path: []U8; how: Accesses): INT; | |
| 376 | +var a: [4096]U8; m: INT; | |
| 377 | +begin | |
| 378 | + if not cstr(path, a) then return -36; end; | |
| 379 | + m := 0; (* F_OK *) | |
| 380 | + if reading in how then m := m | 4; end; (* R_OK *) | |
| 381 | + if writing in how then m := m | 2; end; (* W_OK *) | |
| 382 | + if executing in how then m := m | 1; end; (* X_OK *) | |
| 383 | + return SYSCALL(21, ADR(a), m); (* access *) | |
| 384 | +end; | |
| 385 | + | |
| 386 | +(* Sets the bits taken from every file this program, and the programs | |
| 387 | + it starts, make: the mask it was. *) | |
| 388 | +proc umask*(mask: INT): INT; | |
| 389 | +begin | |
| 390 | + return SYSCALL(95, mask & 1FFH); (* umask *) | |
| 391 | +end; | |
| 392 | + | |
| 328 | 393 | (* Makes path a symbolic link to target. *) |
| 329 | 394 | proc symlink*(target, path: []U8): INT; |
| 330 | 395 | var a, b: [4096]U8; |
| @@ -498,8 +563,24 @@ | ||
| 498 | 563 | proc restoreMode*(fd: INT); |
| 499 | 564 | begin |
| 500 | 565 | _ := SYSCALL(16, fd, 5402H, ADR(saved)); |
| 566 | +end; | |
| 567 | + | |
| 568 | +(* The modes of the terminal on fd, as a shell keeps them for each job; | |
| 569 | + and 0, or minus an errno, -25 (ENOTTY) if it isn't a terminal. *) | |
| 570 | +proc getMode*(fd: INT): (Mode, INT); | |
| 571 | +var m: Mode; r: INT; | |
| 572 | +begin | |
| 573 | + r := SYSCALL(16, fd, 5401H, ADR(m.t)); (* TCGETS *) | |
| 574 | + return m, r; | |
| 501 | 575 | end; |
| 502 | 576 | |
| 577 | +(* Gives the terminal on fd the modes m, once what was written to it | |
| 578 | + is sent: 0, or minus an errno. *) | |
| 579 | +proc setMode*(fd: INT; m: Mode): INT; | |
| 580 | +begin | |
| 581 | + return SYSCALL(16, fd, 5403H, ADR(m.t)); (* TCSETSW *) | |
| 582 | +end; | |
| 583 | + | |
| 503 | 584 | (* The columns and rows of the terminal on fd; 80 by 24 if it doesn't |
| 504 | 585 | say. *) |
| 505 | 586 | proc termSize*(fd: INT): (INT, INT); |
| @@ -926,6 +1007,22 @@ | ||
| 926 | 1007 | return ""; |
| 927 | 1008 | end; |
| 928 | 1009 | |
| 1010 | +(* The environment's ith NAME=value, from 0, and true; "" and false | |
| 1011 | + past the last. *) | |
| 1012 | +proc envAt*(i: INT): ([]U8, BOOL); | |
| 1013 | +var e: ^^U8; s: ^U8; j, n: INT; | |
| 1014 | +begin | |
| 1015 | + if i < 0 then return "", false; end; | |
| 1016 | + e := ARGV() + (ARGC() + 1); | |
| 1017 | + for j := 0 to i do | |
| 1018 | + if e[j] = nil then return "", false; end; | |
| 1019 | + end; | |
| 1020 | + s := e[i]; | |
| 1021 | + n := 0; | |
| 1022 | + while s[n] # 0 do INC(n); end; | |
| 1023 | + return s[0..n], true; | |
| 1024 | +end; | |
| 1025 | + | |
| 929 | 1026 | (* The working directory, good until the next call; and 0, or minus an |
| 930 | 1027 | errno. *) |
| 931 | 1028 | proc workDir*: ([]U8, INT); |
| @@ -1034,6 +1131,62 @@ | ||
| 1034 | 1131 | return SYSCALL(39); (* getpid *) |
| 1035 | 1132 | end; |
| 1036 | 1133 | |
| 1134 | +(* The user this program runs as: its real id. *) | |
| 1135 | +proc uid*: INT; | |
| 1136 | +begin | |
| 1137 | + return SYSCALL(102); (* getuid *) | |
| 1138 | +end; | |
| 1139 | + | |
| 1140 | +(* The user whose rights this program has, its effective id: 0 is | |
| 1141 | + root. *) | |
| 1142 | +proc euid*: INT; | |
| 1143 | +begin | |
| 1144 | + return SYSCALL(107); (* geteuid *) | |
| 1145 | +end; | |
| 1146 | + | |
| 1147 | +(* The name of this machine, good until the next call; and 0, or minus | |
| 1148 | + an errno. *) | |
| 1149 | +proc hostName*: ([]U8, INT); | |
| 1150 | +var r, n: INT; | |
| 1151 | +begin | |
| 1152 | + r := SYSCALL(63, ADR(uts)); (* uname *) | |
| 1153 | + if r < 0 then return "", r; end; | |
| 1154 | + n := 0; | |
| 1155 | + while (n < 64) and (uts[65 + n] # 0) do INC(n); end; (* the nodename, at 65 *) | |
| 1156 | + return uts[65..65 + n], 0; | |
| 1157 | +end; | |
| 1158 | + | |
| 1159 | +(* The time on the processor, in ns, that this program has used, or, | |
| 1160 | + if children, the programs it started and waited for: in the program | |
| 1161 | + itself, and in the system for it. *) | |
| 1162 | +proc cpuTime*(children: BOOL): (I64, I64); | |
| 1163 | +var ru: [18]I64; (* struct rusage: two struct timeval first *) | |
| 1164 | +begin | |
| 1165 | + if children then _ := SYSCALL(98, -1, ADR(ru)); (* getrusage: RUSAGE_CHILDREN *) | |
| 1166 | + else _ := SYSCALL(98, 0, ADR(ru)); (* RUSAGE_SELF *) | |
| 1167 | + end; | |
| 1168 | + return ru[0] * 1000000000 + ru[1] * 1000, ru[2] * 1000000000 + ru[3] * 1000; | |
| 1169 | +end; | |
| 1170 | + | |
| 1171 | +(* The limit which, one of the limit constants: the soft one, which | |
| 1172 | + this program may raise up to the hard one; -1 for none; and 0, or | |
| 1173 | + minus an errno. *) | |
| 1174 | +proc limit*(which: INT): (I64, I64, INT); | |
| 1175 | +var r: [2]I64; err: INT; | |
| 1176 | +begin | |
| 1177 | + err := SYSCALL(97, which, ADR(r)); (* getrlimit; RLIM_INFINITY is -1 *) | |
| 1178 | + return r[0], r[1], err; | |
| 1179 | +end; | |
| 1180 | + | |
| 1181 | +(* Sets the limit which to soft and hard, -1 for none: 0, or minus an | |
| 1182 | + errno. *) | |
| 1183 | +proc setLimit*(which: INT; soft, hard: I64): INT; | |
| 1184 | +var r: [2]I64; | |
| 1185 | +begin | |
| 1186 | + r := [soft, hard]; | |
| 1187 | + return SYSCALL(160, which, ADR(r)); (* setrlimit *) | |
| 1188 | +end; | |
| 1189 | + | |
| 1037 | 1190 | (* Moves the program pid, or this one if pid is 0, to the process group |
| 1038 | 1191 | group, or to a new one it leads if group is 0. *) |
| 1039 | 1192 | proc setGroup*(pid, group: INT): INT; |
| @@ -1064,39 +1217,156 @@ | ||
| 1064 | 1217 | or empty: glibc's execvp looks in these. *) |
| 1065 | 1218 | const defaultPath = "/bin:/usr/bin"; |
| 1066 | 1219 | |
| 1220 | +(* The ith directory of search, a list of them with : between, as PATH | |
| 1221 | + is: dir, "." if it is empty, and where the next starts; -1 past the | |
| 1222 | + last. *) | |
| 1223 | +proc pathDir(search: []U8; i: INT): ([]U8, INT); | |
| 1224 | +var j: INT; | |
| 1225 | +begin | |
| 1226 | + if i > LEN(search) then return "", -1; end; | |
| 1227 | + j := i; | |
| 1228 | + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; | |
| 1229 | + if j = i then return ".", j + 1; end; | |
| 1230 | + return search[i..j], j + 1; | |
| 1231 | +end; | |
| 1232 | + | |
| 1233 | +(* dir, a /, and name, a C string in buf; false if it doesn't fit. *) | |
| 1234 | +proc joinPath(dir, name, buf: []U8): BOOL; | |
| 1235 | +var n: INT; | |
| 1236 | +begin | |
| 1237 | + n := LEN(dir) + 1 + LEN(name); | |
| 1238 | + if n >= LEN(buf) then return false; end; | |
| 1239 | + COPY(buf, dir); | |
| 1240 | + buf[LEN(dir)] := '/'; | |
| 1241 | + COPY(buf[LEN(dir) + 1..n], name); | |
| 1242 | + buf[n] := 0; | |
| 1243 | + return true; | |
| 1244 | +end; | |
| 1245 | + | |
| 1246 | +(* Whether name holds a /, and so is a path, not a name to look for. *) | |
| 1247 | +proc isPath(name: []U8): BOOL; | |
| 1248 | +var i: INT; | |
| 1249 | +begin | |
| 1250 | + for i := 0 to LEN(name) - 1 do | |
| 1251 | + if name[i] = '/' then return true; end; | |
| 1252 | + end; | |
| 1253 | + return false; | |
| 1254 | +end; | |
| 1255 | + | |
| 1067 | 1256 | (* Runs the program at name, as execve does; or, for a name without a |
| 1068 | 1257 | /, the first program of that name in the directories of search, as |
| 1069 | 1258 | execvp does: each in turn, an empty one being the working directory. |
| 1070 | 1259 | It returns only if none could be run. It runs in spawn's child, after |
| 1071 | 1260 | 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; | |
| 1261 | +proc run(name, search, buf: []U8; argv, envp: ^^U8); | |
| 1262 | +var dir: []U8; i: INT; | |
| 1074 | 1263 | 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 | |
| 1264 | + if isPath(name) or (LEN(name) = 0) then | |
| 1078 | 1265 | if cstr(name, buf) then _ := SYSCALL(59, ADR(buf[0]), argv, envp); end; (* execve *) |
| 1079 | 1266 | return; |
| 1080 | 1267 | 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 *) | |
| 1268 | + dir, i := pathDir(search, 0); | |
| 1269 | + while i >= 0 do | |
| 1270 | + if joinPath(dir, name, buf) then _ := SYSCALL(59, ADR(buf[0]), argv, envp); end; (* execve *) | |
| 1271 | + dir, i := pathDir(search, i); | |
| 1272 | + end; | |
| 1273 | +end; | |
| 1274 | + | |
| 1275 | +(* The program exec runs for name, as a C string in buf: name itself | |
| 1276 | + if it has a /, or else the first file of that name in the | |
| 1277 | + directories of search that is no directory and that this user may | |
| 1278 | + run. 0; or -2 (ENOENT) if there is none, or -13 (EACCES) if one is | |
| 1279 | + there and can't be run. *) | |
| 1280 | +proc find(name, search, buf: []U8): INT; | |
| 1281 | +var dir: []U8; i, err, r: INT; f: Info; | |
| 1282 | +begin | |
| 1283 | + if isPath(name) then | |
| 1284 | + if not cstr(name, buf) then return -36; end; | |
| 1285 | + return 0; | |
| 1286 | + end; | |
| 1287 | + r := -2; (* ENOENT *) | |
| 1288 | + dir, i := pathDir(search, 0); | |
| 1289 | + while (LEN(name) > 0) and (i >= 0) do | |
| 1290 | + if joinPath(dir, name, buf) then | |
| 1291 | + f, err := info(buf[0..LEN(dir) + 1 + LEN(name)]); | |
| 1292 | + if (err = 0) and not f.dir then | |
| 1293 | + if SYSCALL(21, ADR(buf[0]), 1) = 0 then return 0; end; (* access, X_OK *) | |
| 1294 | + r := -13; (* EACCES *) | |
| 1295 | + end; | |
| 1094 | 1296 | end; |
| 1095 | - i := j + 1; | |
| 1297 | + dir, i := pathDir(search, i); | |
| 1096 | 1298 | end; |
| 1299 | + return r; | |
| 1097 | 1300 | end; |
| 1098 | 1301 | |
| 1099 | -(* Starts the program p describes, found as exec says: its process id, | |
| 1302 | +(* The name of the terminal of the pseudo-terminal fd, as a C string | |
| 1303 | + in name: /dev/pts/ and the number TIOCGPTN gives. 0, or -25 | |
| 1304 | + (ENOTTY). *) | |
| 1305 | +proc ptyName(fd: INT; name: []U8): INT; | |
| 1306 | +var n, i, size: INT; | |
| 1307 | +begin | |
| 1308 | + n := 0; | |
| 1309 | + if SYSCALL(16, fd, 80045430H, ADR(n)) # 0 then return -25; end; (* TIOCGPTN; ENOTTY *) | |
| 1310 | + i := put("/dev/pts/", ADR(name[0])) - ADR(name[0]) - 1; (* at its zero *) | |
| 1311 | + size := n; | |
| 1312 | + repeat INC(i); size := size div 10; until size = 0; | |
| 1313 | + name[i] := 0; | |
| 1314 | + repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0; | |
| 1315 | + return 0; | |
| 1316 | +end; | |
| 1317 | + | |
| 1318 | +(* Closes every fd from 3 on but those of keep, which stay open in the | |
| 1319 | + program exec starts. It runs in spawn's child, after the fork, so it | |
| 1320 | + takes no memory: it closes the runs between the fds kept, the lowest | |
| 1321 | + first, each with one close_range. *) | |
| 1322 | +proc closeAllBut(keep: []INT); | |
| 1323 | +var lo, next, i: INT; | |
| 1324 | +begin | |
| 1325 | + lo := 3; | |
| 1326 | + repeat | |
| 1327 | + next := -1; | |
| 1328 | + for i := 0 to LEN(keep) - 1 do | |
| 1329 | + if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end; | |
| 1330 | + end; | |
| 1331 | + if next < 0 then | |
| 1332 | + _ := SYSCALL(436, lo, 0FFFFFFFFH, 0); (* close_range: the rest *) | |
| 1333 | + else | |
| 1334 | + if next > lo then _ := SYSCALL(436, lo, next - 1, 0); end; | |
| 1335 | + _ := SYSCALL(72, next, 2, 0); (* fcntl: F_SETFD, kept on exec *) | |
| 1336 | + lo := next + 1; | |
| 1337 | + end; | |
| 1338 | + until next < 0; | |
| 1339 | +end; | |
| 1340 | + | |
| 1341 | +(* What p asks of the program before it runs, done to this one: a | |
| 1342 | + session on the terminal name, if p.tty, or else its group, its | |
| 1343 | + terminal's foreground, and its standard input, output and error; | |
| 1344 | + then its working directory, the C string dir. 0, or minus an | |
| 1345 | + errno. *) | |
| 1346 | +proc takeOn(p: Program; name, dir: []U8): INT; | |
| 1347 | +var fd, i, r: INT; | |
| 1348 | +begin | |
| 1349 | + if p.tty # 0 then | |
| 1350 | + _ := SYSCALL(112); (* setsid *) | |
| 1351 | + fd := SYSCALL(2, ADR(name[0]), 2); (* open, O_RDWR *) | |
| 1352 | + if fd < 0 then return fd; end; | |
| 1353 | + r := SYSCALL(16, fd, 540EH, 0); (* TIOCSCTTY *) | |
| 1354 | + if r # 0 then return r; end; | |
| 1355 | + for i := 0 to 2 do _ := SYSCALL(33, fd, i); end; (* dup2 *) | |
| 1356 | + else | |
| 1357 | + if p.group = -1 then _ := SYSCALL(109, 0, 0); (* setpgid *) | |
| 1358 | + elsif p.group # 0 then _ := SYSCALL(109, 0, p.group); | |
| 1359 | + end; | |
| 1360 | + if p.foreground then _ := foreground(0, SYSCALL(111)); end; (* getpgrp *) | |
| 1361 | + for i := 0 to 2 do | |
| 1362 | + if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *) | |
| 1363 | + end; | |
| 1364 | + end; | |
| 1365 | + if LEN(p.dir) > 0 then return SYSCALL(80, ADR(dir[0])); end; (* chdir *) | |
| 1366 | + return 0; | |
| 1367 | +end; | |
| 1368 | + | |
| 1369 | +(* Starts the program p describes, found as run says: its process id, | |
| 1100 | 1370 | or minus an errno. A program that can't be started ends with status |
| 1101 | 1371 | 127. Every signal is blocked from the fork until the program has its |
| 1102 | 1372 | own: its group, its terminal's foreground, and signals as they are |
| @@ -1104,19 +1374,10 @@ | ||
| 1104 | 1374 | proc spawn*(p: Program): INT; |
| 1105 | 1375 | var |
| 1106 | 1376 | a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; (* struct sigaction: SIG_DFL *) |
| 1107 | - argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, n, i, fd, g: INT; search: []U8; | |
| 1377 | + argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, i, g: INT; search: []U8; | |
| 1108 | 1378 | begin |
| 1109 | 1379 | if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1110 | - if p.tty # 0 then | |
| 1111 | - (* its terminal: /dev/pts/ and the number TIOCGPTN gives *) | |
| 1112 | - n := 0; | |
| 1113 | - if SYSCALL(16, p.tty, 80045430H, ADR(n)) # 0 then return -25; end; (* ENOTTY *) | |
| 1114 | - i := put("/dev/pts/", ADR(name[0])) - ADR(name[0]) - 1; (* at its zero *) | |
| 1115 | - size := n; | |
| 1116 | - repeat INC(i); size := size div 10; until size = 0; | |
| 1117 | - name[i] := 0; | |
| 1118 | - repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0; | |
| 1119 | - end; | |
| 1380 | + if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *) | |
| 1120 | 1381 | none := nil; |
| 1121 | 1382 | if p.onlyEnv then |
| 1122 | 1383 | argv, size := argsAndEnv(p.args, p.env, ADR(none)); |
| @@ -1130,25 +1391,11 @@ | ||
| 1130 | 1391 | _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) |
| 1131 | 1392 | pid := SYSCALL(57); (* fork *) |
| 1132 | 1393 | if pid = 0 then |
| 1133 | - if p.tty # 0 then | |
| 1134 | - _ := SYSCALL(112); (* setsid *) | |
| 1135 | - fd := SYSCALL(2, ADR(name), 2); (* open, O_RDWR *) | |
| 1136 | - if (fd < 0) or (SYSCALL(16, fd, 540EH, 0) # 0) then _ := SYSCALL(231, 127); end; (* TIOCSCTTY; exit *) | |
| 1137 | - for i := 0 to 2 do _ := SYSCALL(33, fd, i); end; (* dup2 *) | |
| 1138 | - else | |
| 1139 | - if p.group = -1 then _ := SYSCALL(109, 0, 0); (* setpgid *) | |
| 1140 | - elsif p.group # 0 then _ := SYSCALL(109, 0, p.group); | |
| 1141 | - end; | |
| 1142 | - if p.foreground then _ := foreground(0, SYSCALL(111)); end; (* getpgrp *) | |
| 1143 | - for i := 0 to 2 do | |
| 1144 | - if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *) | |
| 1145 | - end; | |
| 1146 | - end; | |
| 1147 | - if (LEN(p.dir) > 0) and (SYSCALL(80, ADR(dir)) # 0) then _ := SYSCALL(231, 127); end; (* chdir *) | |
| 1148 | - _ := SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *) | |
| 1394 | + if takeOn(p, name, dir) # 0 then _ := SYSCALL(231, 127); end; (* exit *) | |
| 1395 | + closeAllBut(p.more); | |
| 1149 | 1396 | for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *) |
| 1150 | 1397 | _ := SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *) |
| 1151 | - exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); | |
| 1398 | + run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); | |
| 1152 | 1399 | _ := SYSCALL(231, 127); (* exit *) |
| 1153 | 1400 | end; |
| 1154 | 1401 | if (pid > 0) and (p.tty = 0) then |
| @@ -1162,6 +1409,53 @@ | ||
| 1162 | 1409 | munmap(^U8(argv), size); |
| 1163 | 1410 | return pid; |
| 1164 | 1411 | end; |
| 1412 | + | |
| 1413 | +(* Runs the program p describes in this one's place, as spawn would | |
| 1414 | + start it, but keeping this program's other fds as they are, open | |
| 1415 | + unless keepOnExec said no; the fds of p.more are kept. It returns | |
| 1416 | + only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no | |
| 1417 | + program to run, before anything is done; or, once the program's | |
| 1418 | + terminal, group, fds and working directory are as p asks, which | |
| 1419 | + stay so, minus the errno that stopped it, with this program's | |
| 1420 | + signals given back as they were. *) | |
| 1421 | +proc exec*(p: Program): INT; | |
| 1422 | +var | |
| 1423 | + a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; acts: [65][4]U64; (* struct sigaction *) | |
| 1424 | + argv: ^^U8; none: ^U8; all, old, empty: U64; size, r, i: INT; search: []U8; | |
| 1425 | +begin | |
| 1426 | + if not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) | |
| 1427 | + if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *) | |
| 1428 | + search := env("PATH"); | |
| 1429 | + if LEN(search) = 0 then search := defaultPath; end; | |
| 1430 | + r := find(p.path, search, a); | |
| 1431 | + if r < 0 then return r; end; | |
| 1432 | + none := nil; | |
| 1433 | + if p.onlyEnv then | |
| 1434 | + argv, size := argsAndEnv(p.args, p.env, ADR(none)); | |
| 1435 | + else | |
| 1436 | + argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); | |
| 1437 | + end; | |
| 1438 | + if argv = nil then return -12; end; (* ENOMEM *) | |
| 1439 | + r := takeOn(p, name, dir); | |
| 1440 | + if r = 0 then | |
| 1441 | + for i := 0 to LEN(p.more) - 1 do | |
| 1442 | + if p.more[i] > 2 then _ := keepOnExec(p.more[i], true); end; | |
| 1443 | + end; | |
| 1444 | + (* every signal as it is at first and none blocked, as spawn | |
| 1445 | + gives them, each kept to give back if execve fails *) | |
| 1446 | + all := 0FFFFFFFFFFFFFFFFH; | |
| 1447 | + _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) | |
| 1448 | + for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), ADR(acts[i]), 8); end; (* rt_sigaction *) | |
| 1449 | + _ := SYSCALL(14, 2, ADR(empty), 0, 8); | |
| 1450 | + r := SYSCALL(59, ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *) | |
| 1451 | + _ := SYSCALL(14, 2, ADR(all), 0, 8); | |
| 1452 | + for i := 1 to 64 do _ := SYSCALL(13, i, ADR(acts[i]), 0, 8); end; | |
| 1453 | + _ := SYSCALL(14, 2, ADR(old), 0, 8); | |
| 1454 | + end; | |
| 1455 | + munmap(^U8(argv), size); | |
| 1456 | + return r; | |
| 1457 | +end; | |
| 1458 | + | |
| 1165 | 1459 | (* Waits for the program pid, or any of this program's if pid is -1, to |
| 1166 | 1460 | change, if block, and says which one changed and how: exited, with |
| 1167 | 1461 | its exit status; killed or stopped, with the signal's number. Without |
| @@ -1332,6 +1626,97 @@ | ||
| 1332 | 1626 | if ignore(sigInt, false) # 0 then exit(2); end; |
| 1333 | 1627 | end; |
| 1334 | 1628 | |
| 1629 | +(* The test of what a shell needs: fds copied, moved and kept on | |
| 1630 | + exec; a program given an fd at its own number, and one not; access, | |
| 1631 | + the umask and the environment by number; a terminal's modes; time | |
| 1632 | + used; limits; who and where; and exec, with nothing to run, and | |
| 1633 | + with a program that can't be run, after which the signals caught | |
| 1634 | + are caught still. *) | |
| 1635 | +proc testShell; | |
| 1636 | +var | |
| 1637 | + r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; | |
| 1638 | + m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; | |
| 1639 | +begin | |
| 1640 | + r, w := pipe(); | |
| 1641 | + if w < 0 then exit(2); end; | |
| 1642 | + fd := dup(w); | |
| 1643 | + if (fd < 0) or (write(fd, "ab") # 2) or (read(r, buf) # 2) then exit(2); end; | |
| 1644 | + close(fd); | |
| 1645 | + if (dupTo(w, 9) # 9) or (write(9, "c") # 1) or (read(r, buf) # 1) or (buf[0] # 'c') then exit(2); end; | |
| 1646 | + if (keepOnExec(9, false) # 0) or (keepOnExec(9, true) # 0) or (keepOnExec(-1, true) # -9) then exit(2); end; | |
| 1647 | + child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo more >&9"], more: [9]}); | |
| 1648 | + close(9); | |
| 1649 | + close(w); | |
| 1650 | + n := 0; | |
| 1651 | + repeat | |
| 1652 | + k := read(r, buf[n..LEN(buf)]); | |
| 1653 | + if k > 0 then INC(n, k); end; | |
| 1654 | + until k <= 0; | |
| 1655 | + close(r); | |
| 1656 | + if buf[0..n] # "more\n" then exit(2); end; | |
| 1657 | + k, st, c := wait(child, true); | |
| 1658 | + if (st # exited) or (c # 0) then exit(2); end; | |
| 1659 | + r, w := pipe(); | |
| 1660 | + if (w < 0) or (dupTo(w, 9) # 9) then exit(2); end; | |
| 1661 | + child := spawn({path: "/bin/sh", args: ["sh", "-c", "{ echo none >&9; } 2>/dev/null"]}); (* 9 is closed in it *) | |
| 1662 | + close(9); | |
| 1663 | + close(w); | |
| 1664 | + if read(r, buf) # 0 then exit(2); end; | |
| 1665 | + close(r); | |
| 1666 | + k, st, c := wait(child, true); | |
| 1667 | + if (st # exited) or (c = 0) then exit(2); end; | |
| 1668 | + | |
| 1669 | + if (access("/bin/sh", {executing}) # 0) or (access("/", {reading}) # 0) | |
| 1670 | + or (access("/no/such/file", {}) # -2) or (open("/bin/sh", {executing}) # -22) then exit(2); end; | |
| 1671 | + mask := umask(3FH); (* 077 *) | |
| 1672 | + if umask(mask) # 3FH then exit(2); end; | |
| 1673 | + path := false; | |
| 1674 | + n := 0; | |
| 1675 | + repeat | |
| 1676 | + e, ok := envAt(n); | |
| 1677 | + if ok and (LEN(e) > 5) and (e[0..5] = "PATH=") then path := true; end; | |
| 1678 | + INC(n); | |
| 1679 | + until not ok; | |
| 1680 | + e, ok := envAt(-1); | |
| 1681 | + if not path or ok or (LEN(e) # 0) then exit(2); end; | |
| 1682 | + | |
| 1683 | + (* a terminal's modes, kept and given back: a new one has lines *) | |
| 1684 | + pty := openPty(); | |
| 1685 | + m, err := getMode(pty); | |
| 1686 | + if (err # 0) or (m.t.lflag & 2 = 0) or not rawMode(pty) then exit(2); end; (* ICANON *) | |
| 1687 | + raw, err := getMode(pty); | |
| 1688 | + if (err # 0) or (raw.t.lflag & 2 # 0) or (setMode(pty, m) # 0) then exit(2); end; | |
| 1689 | + raw, err := getMode(pty); | |
| 1690 | + if (err # 0) or (raw.t.lflag & 2 = 0) then exit(2); end; | |
| 1691 | + m, err := getMode(-1); | |
| 1692 | + if err # -9 then exit(2); end; (* EBADF *) | |
| 1693 | + r, w := pipe(); | |
| 1694 | + m, err := getMode(r); | |
| 1695 | + if err # -25 then exit(2); end; (* ENOTTY *) | |
| 1696 | + close(r); | |
| 1697 | + close(w); | |
| 1698 | + close(pty); | |
| 1699 | + | |
| 1700 | + user, system := cpuTime(false); | |
| 1701 | + if (user < 0) or (system < 0) or (user + system = 0) then exit(2); end; | |
| 1702 | + user, system := cpuTime(true); | |
| 1703 | + if (user < 0) or (system < 0) then exit(2); end; | |
| 1704 | + soft, hard, err := limit(limitFiles); | |
| 1705 | + if (err # 0) or (soft < 16) or (setLimit(limitFiles, soft - 1, hard) # 0) then exit(2); end; | |
| 1706 | + s2, h2, err := limit(limitFiles); | |
| 1707 | + if (err # 0) or (s2 # soft - 1) or (h2 # hard) or (setLimit(limitFiles, soft, hard) # 0) then exit(2); end; | |
| 1708 | + soft, hard, err := limit(10000); | |
| 1709 | + if err # -22 then exit(2); end; (* EINVAL *) | |
| 1710 | + e, err := hostName(); | |
| 1711 | + if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; | |
| 1712 | + | |
| 1713 | + sigs := catch([sigUsr1]); | |
| 1714 | + while caught(sigs) # 0 do end; (* the sigChld of the programs above *) | |
| 1715 | + if (sigs < 0) or (exec({path: "no-such-program-on-path", args: ["x"]}) # -2) then exit(2); end; | |
| 1716 | + if exec({path: "/etc/passwd", args: ["x"]}) # -13 then exit(2); end; (* not executable *) | |
| 1717 | + if (kill(pid(), sigUsr1) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigUsr1) then exit(2); end; | |
| 1718 | +end; | |
| 1719 | + | |
| 1335 | 1720 | (* The test of time, random bytes and the network: a sleep the clock |
| 1336 | 1721 | sees; addresses read and refused; a TCP connection on the loopback; |
| 1337 | 1722 | a datagram to a port where no one listens, refused; servers on |
| @@ -1562,6 +1947,7 @@ | ||
| 1562 | 1947 | close(l); |
| 1563 | 1948 | _ := remove("/tmp/raz-sys-socket"); |
| 1564 | 1949 | testPrograms; |
| 1950 | + testShell; | |
| 1565 | 1951 | testNet; |
| 1566 | 1952 | end; |
| 1567 | 1953 |
| @@ -2,12 +2,16 @@ | ||
| 2 | 2 | file of their own, with the same exports. A program that imports it |
| 3 | 3 | uses C. |
| 4 | 4 | |
| 5 | - It keeps to POSIX's names, and raw mode is cfmakeraw's. What is | |
| 6 | - glibc's, on x86-64: where a name lies in struct dirent; struct stat; | |
| 7 | - open's flags; the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and | |
| 8 | - TIOCSCTTY; close_range; SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; and | |
| 9 | - struct sockaddr_un and sockaddr_in, which on the BSDs start with a | |
| 10 | - byte of length. A C library that differs needs a Sys of its own. *) | |
| 5 | + It keeps to POSIX's names, and raw mode is cfmakeraw's. A terminal's | |
| 6 | + modes, Mode, are its struct termios whole, of whatever layout, and | |
| 7 | + only the two struct timeval that begin struct rusage are read. What | |
| 8 | + is glibc's, on x86-64: where a name lies in struct dirent; struct | |
| 9 | + stat; open's flags; the numbers of the requests TIOCGWINSZ, | |
| 10 | + TIOCSWINSZ and TIOCSCTTY; close_range; SOCK_CLOEXEC, AF_INET6 and | |
| 11 | + SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs | |
| 12 | + start with a byte of length; the numbers of the signals and of the | |
| 13 | + limits, and RLIM_INFINITY, all ones. A C library that differs needs | |
| 14 | + a Sys of its own. *) | |
| 11 | 15 | module Sys; |
| 12 | 16 | import C; |
| 13 | 17 | |
| @@ -22,6 +26,17 @@ | ||
| 22 | 26 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 23 | 27 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| 24 | 28 | sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; |
| 29 | + sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; | |
| 30 | + (* the limits limit and setLimit take, by their numbers on Linux; | |
| 31 | + another target has its own *) | |
| 32 | + limitCpu* = 0; (* seconds of the processor *) | |
| 33 | + limitFileSize* = 1; (* bytes in a file it writes *) | |
| 34 | + limitData* = 2; (* bytes of its data *) | |
| 35 | + limitStack* = 3; (* bytes of its stack *) | |
| 36 | + limitCore* = 4; (* bytes of a core dump *) | |
| 37 | + limitProcs* = 6; (* processes of its user *) | |
| 38 | + limitFiles* = 7; (* one more than its highest fd *) | |
| 39 | + limitMemory* = 9; (* bytes of its address space *) | |
| 25 | 40 | DirentName = 19; (* the offset of d_name in glibc's struct dirent *) |
| 26 | 41 | DirentType = 18; (* and of d_type *) |
| 27 | 42 | TIOCGWINSZ = 5413H; (* glibc's, as the two below *) |
| @@ -32,7 +47,7 @@ | ||
| 32 | 47 | (* What open is asked for: reading, writing or both, and what else |
| 33 | 48 | it does to the file. *) |
| 34 | 49 | Access* = (reading, writing, creating, exclusive, truncating, |
| 35 | - appending); | |
| 50 | + appending, executing); | |
| 36 | 51 | Accesses* = set of Access; (* what open is given *) |
| 37 | 52 | |
| 38 | 53 | (* What info says of a file. *) |
| @@ -70,6 +85,7 @@ | ||
| 70 | 85 | group*: INT; (* its group: 0 for this program's, -1 a new one *) |
| 71 | 86 | foreground*: BOOL; (* its group gets the terminal on standard input *) |
| 72 | 87 | tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) |
| 88 | + more*: []INT; (* fds above 2 passed on to it at the same numbers *) | |
| 73 | 89 | end; |
| 74 | 90 | (* How a program that wait waited for changed. *) |
| 75 | 91 | Status* = (running, exited, killed, stopped); |
| @@ -83,6 +99,18 @@ | ||
| 83 | 99 | restorer: ^U8; |
| 84 | 100 | end; |
| 85 | 101 | |
| 102 | + (* A terminal's modes, as getMode reads them and setMode sets them: | |
| 103 | + a struct termios, of the C library's size, 256 bytes at most. *) | |
| 104 | + Mode* = record | |
| 105 | + t: [256]U8; | |
| 106 | + end; | |
| 107 | + | |
| 108 | + (* struct rusage: what is read of it, the time used in the program | |
| 109 | + and in the system, each a struct timeval, its microseconds at | |
| 110 | + most an int and little-endian, then room for the rest *) | |
| 111 | + Timeval = record sec: I64; usec: I32; pad: I32; end; | |
| 112 | + Rusage = record user, system: Timeval; rest: [32]I64; end; | |
| 113 | + | |
| 86 | 114 | Winsize = record rows, cols, xpixel, ypixel: U16; end; |
| 87 | 115 | Pollfd = record fd: I32; events, revents: I16; end; |
| 88 | 116 | |
| @@ -127,6 +155,7 @@ | ||
| 127 | 155 | cwd: [4096]U8; (* what workDir gives *) |
| 128 | 156 | linked: [4096]U8; (* what readLink gives *) |
| 129 | 157 | saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) |
| 158 | + host: [256]U8; (* what hostName gives *) | |
| 130 | 159 | sigRead, sigWrite: I32; (* the pipe the handler of caught signals writes their numbers to, once open *) |
| 131 | 160 | |
| 132 | 161 | (* minus errno, as a system call returns it *) |
| @@ -190,6 +219,7 @@ | ||
| 190 | 219 | proc openMode*(path: []U8; how: Accesses; mode: INT): INT; |
| 191 | 220 | var buf: [4096]U8; r: INT; |
| 192 | 221 | begin |
| 222 | + if executing in how then return -22; end; (* EINVAL: access asks it, open can't *) | |
| 193 | 223 | if not cstr(path, buf) then return -36; end; |
| 194 | 224 | r := INT(C.open(ADR(buf[0]), flags(how), mode)); |
| 195 | 225 | if r < 0 then return failed(); end; |
| @@ -283,6 +313,39 @@ | ||
| 283 | 313 | _ := C.close(I32(fd)); |
| 284 | 314 | end; |
| 285 | 315 | |
| 316 | +(* A new file descriptor for the file fd is open on, the lowest free: | |
| 317 | + it, or minus an errno. *) | |
| 318 | +proc dup*(fd: INT): INT; | |
| 319 | +var r: I32; | |
| 320 | +begin | |
| 321 | + r := C.dup(I32(fd)); | |
| 322 | + if r < 0 then return failed(); end; | |
| 323 | + return INT(r); | |
| 324 | +end; | |
| 325 | + | |
| 326 | +(* Makes the file descriptor into stand for the file fd is open on, | |
| 327 | + closing what into was first: into, or minus an errno. *) | |
| 328 | +proc dupTo*(fd, into: INT): INT; | |
| 329 | +var r: I32; | |
| 330 | +begin | |
| 331 | + r := C.dup2(I32(fd), I32(into)); | |
| 332 | + if r < 0 then return failed(); end; | |
| 333 | + return INT(r); | |
| 334 | +end; | |
| 335 | + | |
| 336 | +(* Whether fd stays open in the program exec runs in this one's place: | |
| 337 | + yes, or no, closed then, as pipe's fds are at first. 0, or minus an | |
| 338 | + errno. *) | |
| 339 | +proc keepOnExec*(fd: INT; yes: BOOL): INT; | |
| 340 | +var f: I32; | |
| 341 | +begin | |
| 342 | + f := C.fcntl(I32(fd), 1, 0); (* F_GETFD *) | |
| 343 | + if f < 0 then return failed(); end; | |
| 344 | + if yes then f := f & ~1; else f := f | 1; end; (* FD_CLOEXEC *) | |
| 345 | + if C.fcntl(I32(fd), 2, f) < 0 then return failed(); end; (* F_SETFD *) | |
| 346 | + return 0; | |
| 347 | +end; | |
| 348 | + | |
| 286 | 349 | (* size bytes of fresh, zeroed memory from the system, a multiple of |
| 287 | 350 | pages, or nil. *) |
| 288 | 351 | proc mmap*(size: INT): ^U8; |
| @@ -371,6 +434,28 @@ | ||
| 371 | 434 | return 0; |
| 372 | 435 | end; |
| 373 | 436 | |
| 437 | +(* 0 if this user, by its real ids, may do with path what how asks: | |
| 438 | + reading, writing and executing; minus an errno, -13 (EACCES) if not. | |
| 439 | + An empty how asks only whether path is there. *) | |
| 440 | +proc access*(path: []U8; how: Accesses): INT; | |
| 441 | +var a: [4096]U8; m: I32; | |
| 442 | +begin | |
| 443 | + if not cstr(path, a) then return -36; end; | |
| 444 | + m := 0; (* F_OK *) | |
| 445 | + if reading in how then m := m | 4; end; (* R_OK *) | |
| 446 | + if writing in how then m := m | 2; end; (* W_OK *) | |
| 447 | + if executing in how then m := m | 1; end; (* X_OK *) | |
| 448 | + if C.access(ADR(a[0]), m) # 0 then return failed(); end; | |
| 449 | + return 0; | |
| 450 | +end; | |
| 451 | + | |
| 452 | +(* Sets the bits taken from every file this program, and the programs | |
| 453 | + it starts, make: the mask it was. *) | |
| 454 | +proc umask*(mask: INT): INT; | |
| 455 | +begin | |
| 456 | + return INT(C.umask(U32(mask & 1FFH)) & 1FFH); | |
| 457 | +end; | |
| 458 | + | |
| 374 | 459 | (* Makes path a symbolic link to target. *) |
| 375 | 460 | proc symlink*(target, path: []U8): INT; |
| 376 | 461 | var a, b: [4096]U8; |
| @@ -525,6 +610,23 @@ | ||
| 525 | 610 | _ := C.tcsetattr(I32(fd), 0, ADR(saved[0])); |
| 526 | 611 | end; |
| 527 | 612 | |
| 613 | +(* The modes of the terminal on fd, as a shell keeps them for each job; | |
| 614 | + and 0, or minus an errno, -25 (ENOTTY) if it isn't a terminal. *) | |
| 615 | +proc getMode*(fd: INT): (Mode, INT); | |
| 616 | +var m: Mode; | |
| 617 | +begin | |
| 618 | + if C.tcgetattr(I32(fd), ADR(m.t[0])) # 0 then return m, failed(); end; | |
| 619 | + return m, 0; | |
| 620 | +end; | |
| 621 | + | |
| 622 | +(* Gives the terminal on fd the modes m, once what was written to it | |
| 623 | + is sent: 0, or minus an errno. *) | |
| 624 | +proc setMode*(fd: INT; m: Mode): INT; | |
| 625 | +begin | |
| 626 | + if C.tcsetattr(I32(fd), 1, ADR(m.t[0])) # 0 then return failed(); end; (* TCSADRAIN *) | |
| 627 | + return 0; | |
| 628 | +end; | |
| 629 | + | |
| 528 | 630 | (* The columns and rows of the terminal on fd; 80 by 24 if it doesn't |
| 529 | 631 | say. *) |
| 530 | 632 | proc termSize*(fd: INT): (INT, INT); |
| @@ -890,6 +992,22 @@ | ||
| 890 | 992 | return s[0..n]; |
| 891 | 993 | end; |
| 892 | 994 | |
| 995 | +(* The environment's ith NAME=value, from 0, and true; "" and false | |
| 996 | + past the last. *) | |
| 997 | +proc envAt*(i: INT): ([]U8, BOOL); | |
| 998 | +var e: ^^U8; s: ^U8; j, n: INT; | |
| 999 | +begin | |
| 1000 | + if i < 0 then return "", false; end; | |
| 1001 | + e := C.environ; | |
| 1002 | + for j := 0 to i do | |
| 1003 | + if e[j] = nil then return "", false; end; | |
| 1004 | + end; | |
| 1005 | + s := e[i]; | |
| 1006 | + n := 0; | |
| 1007 | + while s[n] # 0 do INC(n); end; | |
| 1008 | + return s[0..n], true; | |
| 1009 | +end; | |
| 1010 | + | |
| 893 | 1011 | (* The working directory, good until the next call; and 0, or minus an |
| 894 | 1012 | errno. *) |
| 895 | 1013 | proc workDir*: ([]U8, INT); |
| @@ -999,6 +1117,64 @@ | ||
| 999 | 1117 | return INT(C.getpid()); |
| 1000 | 1118 | end; |
| 1001 | 1119 | |
| 1120 | +(* The user this program runs as: its real id. *) | |
| 1121 | +proc uid*: INT; | |
| 1122 | +begin | |
| 1123 | + return INT(C.getuid()); | |
| 1124 | +end; | |
| 1125 | + | |
| 1126 | +(* The user whose rights this program has, its effective id: 0 is | |
| 1127 | + root. *) | |
| 1128 | +proc euid*: INT; | |
| 1129 | +begin | |
| 1130 | + return INT(C.geteuid()); | |
| 1131 | +end; | |
| 1132 | + | |
| 1133 | +(* The name of this machine, good until the next call; and 0, or minus | |
| 1134 | + an errno. *) | |
| 1135 | +proc hostName*: ([]U8, INT); | |
| 1136 | +var n: INT; | |
| 1137 | +begin | |
| 1138 | + if C.gethostname(ADR(host[0]), LEN(host) - 1) # 0 then return "", failed(); end; | |
| 1139 | + host[LEN(host) - 1] := 0; (* POSIX doesn't promise one if it is cut *) | |
| 1140 | + n := 0; | |
| 1141 | + while host[n] # 0 do INC(n); end; | |
| 1142 | + return host[0..n], 0; | |
| 1143 | +end; | |
| 1144 | + | |
| 1145 | +(* The time on the processor, in ns, that this program has used, or, | |
| 1146 | + if children, the programs it started and waited for: in the program | |
| 1147 | + itself, and in the system for it. *) | |
| 1148 | +proc cpuTime*(children: BOOL): (I64, I64); | |
| 1149 | +var ru: Rusage; | |
| 1150 | +begin | |
| 1151 | + if children then _ := C.getrusage(-1, ^U8(ADR(ru))); (* RUSAGE_CHILDREN *) | |
| 1152 | + else _ := C.getrusage(0, ^U8(ADR(ru))); (* RUSAGE_SELF *) | |
| 1153 | + end; | |
| 1154 | + return ru.user.sec * 1000000000 + I64(ru.user.usec) * 1000, | |
| 1155 | + ru.system.sec * 1000000000 + I64(ru.system.usec) * 1000; | |
| 1156 | +end; | |
| 1157 | + | |
| 1158 | +(* The limit which, one of the limit constants: the soft one, which | |
| 1159 | + this program may raise up to the hard one; -1 for none; and 0, or | |
| 1160 | + minus an errno. *) | |
| 1161 | +proc limit*(which: INT): (I64, I64, INT); | |
| 1162 | +var r: [2]I64; (* struct rlimit; RLIM_INFINITY is -1 *) | |
| 1163 | +begin | |
| 1164 | + if C.getrlimit(I32(which), ^U8(ADR(r))) # 0 then return 0, 0, failed(); end; | |
| 1165 | + return r[0], r[1], 0; | |
| 1166 | +end; | |
| 1167 | + | |
| 1168 | +(* Sets the limit which to soft and hard, -1 for none: 0, or minus an | |
| 1169 | + errno. *) | |
| 1170 | +proc setLimit*(which: INT; soft, hard: I64): INT; | |
| 1171 | +var r: [2]I64; | |
| 1172 | +begin | |
| 1173 | + r := [soft, hard]; | |
| 1174 | + if C.setrlimit(I32(which), ^U8(ADR(r))) # 0 then return failed(); end; | |
| 1175 | + return 0; | |
| 1176 | +end; | |
| 1177 | + | |
| 1002 | 1178 | (* Moves the program pid, or this one if pid is 0, to the process group |
| 1003 | 1179 | group, or to a new one it leads if group is 0. *) |
| 1004 | 1180 | proc setGroup*(pid, group: INT): INT; |
| @@ -1029,55 +1205,166 @@ | ||
| 1029 | 1205 | or empty: glibc's execvp looks in these. *) |
| 1030 | 1206 | const defaultPath = "/bin:/usr/bin"; |
| 1031 | 1207 | |
| 1208 | +(* The ith directory of search, a list of them with : between, as PATH | |
| 1209 | + is: dir, "." if it is empty, and where the next starts; -1 past the | |
| 1210 | + last. *) | |
| 1211 | +proc pathDir(search: []U8; i: INT): ([]U8, INT); | |
| 1212 | +var j: INT; | |
| 1213 | +begin | |
| 1214 | + if i > LEN(search) then return "", -1; end; | |
| 1215 | + j := i; | |
| 1216 | + while (j < LEN(search)) and (search[j] # ':') do INC(j); end; | |
| 1217 | + if j = i then return ".", j + 1; end; | |
| 1218 | + return search[i..j], j + 1; | |
| 1219 | +end; | |
| 1220 | + | |
| 1221 | +(* dir, a /, and name, a C string in buf; false if it doesn't fit. *) | |
| 1222 | +proc joinPath(dir, name, buf: []U8): BOOL; | |
| 1223 | +var n: INT; | |
| 1224 | +begin | |
| 1225 | + n := LEN(dir) + 1 + LEN(name); | |
| 1226 | + if n >= LEN(buf) then return false; end; | |
| 1227 | + COPY(buf, dir); | |
| 1228 | + buf[LEN(dir)] := '/'; | |
| 1229 | + COPY(buf[LEN(dir) + 1..n], name); | |
| 1230 | + buf[n] := 0; | |
| 1231 | + return true; | |
| 1232 | +end; | |
| 1233 | + | |
| 1234 | +(* Whether name holds a /, and so is a path, not a name to look for. *) | |
| 1235 | +proc isPath(name: []U8): BOOL; | |
| 1236 | +var i: INT; | |
| 1237 | +begin | |
| 1238 | + for i := 0 to LEN(name) - 1 do | |
| 1239 | + if name[i] = '/' then return true; end; | |
| 1240 | + end; | |
| 1241 | + return false; | |
| 1242 | +end; | |
| 1243 | + | |
| 1032 | 1244 | (* Runs the program at name, as execve does; or, for a name without a |
| 1033 | 1245 | /, the first program of that name in the directories of search, as |
| 1034 | 1246 | execvp does: each in turn, an empty one being the working directory. |
| 1035 | 1247 | It returns only if none could be run. It runs in spawn's child, after |
| 1036 | 1248 | the fork, so it takes no memory: each path is made in buf. *) |
| 1037 | -proc exec(name, search, buf: []U8; argv, envp: ^^U8); | |
| 1038 | -var dir: []U8; i, j, n: INT; | |
| 1249 | +proc run(name, search, buf: []U8; argv, envp: ^^U8); | |
| 1250 | +var dir: []U8; i: INT; | |
| 1039 | 1251 | begin |
| 1040 | - i := 0; | |
| 1041 | - while (i < LEN(name)) and (name[i] # '/') do INC(i); end; | |
| 1042 | - if (i < LEN(name)) or (LEN(name) = 0) then | |
| 1252 | + if isPath(name) or (LEN(name) = 0) then | |
| 1043 | 1253 | if cstr(name, buf) then _ := C.execve(ADR(buf[0]), argv, envp); end; |
| 1044 | 1254 | return; |
| 1045 | 1255 | end; |
| 1046 | - i := 0; | |
| 1047 | - while i <= LEN(search) do | |
| 1048 | - j := i; | |
| 1049 | - while (j < LEN(search)) and (search[j] # ':') do INC(j); end; | |
| 1050 | - dir := search[i..j]; | |
| 1051 | - if LEN(dir) = 0 then dir := "."; end; | |
| 1052 | - n := LEN(dir) + 1 + LEN(name); | |
| 1053 | - if n < LEN(buf) then | |
| 1054 | - COPY(buf, dir); | |
| 1055 | - buf[LEN(dir)] := '/'; | |
| 1056 | - COPY(buf[LEN(dir) + 1..n], name); | |
| 1057 | - buf[n] := 0; | |
| 1058 | - _ := C.execve(ADR(buf[0]), argv, envp); | |
| 1256 | + dir, i := pathDir(search, 0); | |
| 1257 | + while i >= 0 do | |
| 1258 | + if joinPath(dir, name, buf) then _ := C.execve(ADR(buf[0]), argv, envp); end; | |
| 1259 | + dir, i := pathDir(search, i); | |
| 1260 | + end; | |
| 1261 | +end; | |
| 1262 | + | |
| 1263 | +(* The program exec runs for name, as a C string in buf: name itself | |
| 1264 | + if it has a /, or else the first file of that name in the | |
| 1265 | + directories of search that is no directory and that this user may | |
| 1266 | + run. 0; or -2 (ENOENT) if there is none, or -13 (EACCES) if one is | |
| 1267 | + there and can't be run. *) | |
| 1268 | +proc find(name, search, buf: []U8): INT; | |
| 1269 | +var dir: []U8; i, err, r: INT; f: Info; | |
| 1270 | +begin | |
| 1271 | + if isPath(name) then | |
| 1272 | + if not cstr(name, buf) then return -36; end; | |
| 1273 | + return 0; | |
| 1274 | + end; | |
| 1275 | + r := -2; (* ENOENT *) | |
| 1276 | + dir, i := pathDir(search, 0); | |
| 1277 | + while (LEN(name) > 0) and (i >= 0) do | |
| 1278 | + if joinPath(dir, name, buf) then | |
| 1279 | + f, err := info(buf[0..LEN(dir) + 1 + LEN(name)]); | |
| 1280 | + if (err = 0) and not f.dir then | |
| 1281 | + if C.access(ADR(buf[0]), 1) = 0 then return 0; end; (* X_OK *) | |
| 1282 | + r := -13; (* EACCES *) | |
| 1283 | + end; | |
| 1284 | + end; | |
| 1285 | + dir, i := pathDir(search, i); | |
| 1286 | + end; | |
| 1287 | + return r; | |
| 1288 | +end; | |
| 1289 | + | |
| 1290 | +(* The name of the terminal of the pseudo-terminal fd, as a C string | |
| 1291 | + in name, which isn't safe to ask after a fork: 0, or minus an | |
| 1292 | + errno. *) | |
| 1293 | +proc ptyName(fd: INT; name: []U8): INT; | |
| 1294 | +var s: ^U8; n: INT; | |
| 1295 | +begin | |
| 1296 | + s := C.ptsname(I32(fd)); | |
| 1297 | + if s = nil then return failed(); end; | |
| 1298 | + n := 0; | |
| 1299 | + while s[n] # 0 do INC(n); end; | |
| 1300 | + if not cstr(s[0..n], name) then return -36; end; | |
| 1301 | + return 0; | |
| 1302 | +end; | |
| 1303 | + | |
| 1304 | +(* Closes every fd from 3 on but those of keep, which stay open in the | |
| 1305 | + program exec starts. It runs in spawn's child, after the fork, so it | |
| 1306 | + takes no memory: it closes the runs between the fds kept, the lowest | |
| 1307 | + first, each with one close_range. *) | |
| 1308 | +proc closeAllBut(keep: []INT); | |
| 1309 | +var lo, next, i: INT; | |
| 1310 | +begin | |
| 1311 | + lo := 3; | |
| 1312 | + repeat | |
| 1313 | + next := -1; | |
| 1314 | + for i := 0 to LEN(keep) - 1 do | |
| 1315 | + if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end; | |
| 1316 | + end; | |
| 1317 | + if next < 0 then | |
| 1318 | + _ := C.close_range(U32(lo), 0FFFFFFFFH, 0); (* the rest *) | |
| 1319 | + else | |
| 1320 | + if next > lo then _ := C.close_range(U32(lo), U32(next - 1), 0); end; | |
| 1321 | + _ := C.fcntl(I32(next), 2, 0); (* F_SETFD: kept on exec *) | |
| 1322 | + lo := next + 1; | |
| 1323 | + end; | |
| 1324 | + until next < 0; | |
| 1325 | +end; | |
| 1326 | + | |
| 1327 | +(* What p asks of the program before it runs, done to this one: a | |
| 1328 | + session on the terminal name, if p.tty, or else its group, its | |
| 1329 | + terminal's foreground, and its standard input, output and error; | |
| 1330 | + then its working directory, the C string dir. 0, or minus an | |
| 1331 | + errno. *) | |
| 1332 | +proc takeOn(p: Program; name, dir: []U8): INT; | |
| 1333 | +var fd: I32; i: INT; | |
| 1334 | +begin | |
| 1335 | + if p.tty # 0 then | |
| 1336 | + _ := C.setsid(); | |
| 1337 | + fd := C.open(ADR(name[0]), 2); (* O_RDWR *) | |
| 1338 | + if fd < 0 then return failed(); end; | |
| 1339 | + if C.ioctl(fd, TIOCSCTTY, 0) # 0 then return failed(); end; | |
| 1340 | + for i := 0 to 2 do _ := C.dup2(fd, I32(i)); end; | |
| 1341 | + else | |
| 1342 | + if p.group = -1 then _ := C.setpgid(0, 0); | |
| 1343 | + elsif p.group # 0 then _ := C.setpgid(0, I32(p.group)); | |
| 1344 | + end; | |
| 1345 | + if p.foreground then _ := C.tcsetpgrp(0, C.getpgrp()); end; | |
| 1346 | + for i := 0 to 2 do | |
| 1347 | + if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end; | |
| 1059 | 1348 | end; |
| 1060 | - i := j + 1; | |
| 1061 | 1349 | end; |
| 1350 | + if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then return failed(); end; | |
| 1351 | + return 0; | |
| 1062 | 1352 | end; |
| 1063 | 1353 | |
| 1064 | -(* Starts the program p describes, found as exec says: its process id, | |
| 1354 | +(* Starts the program p describes, found as run says: its process id, | |
| 1065 | 1355 | or minus an errno. A program that can't be started ends with status |
| 1066 | 1356 | 127. Every signal is blocked from the fork until the program has its |
| 1067 | 1357 | own: its group, its terminal's foreground, and signals as they are |
| 1068 | 1358 | at first. *) |
| 1069 | 1359 | proc spawn*(p: Program): INT; |
| 1070 | 1360 | var |
| 1071 | - a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: INT; search: []U8; | |
| 1072 | - all, old, empty: [16]U64; dfl: Sigaction; pid, fd: I32; (* sigset_t: 128 bytes *) | |
| 1361 | + a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, err, i, g: INT; search: []U8; | |
| 1362 | + all, old, empty: [16]U64; dfl: Sigaction; pid: I32; (* sigset_t: 128 bytes *) | |
| 1073 | 1363 | begin |
| 1074 | 1364 | if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1075 | 1365 | if p.tty # 0 then |
| 1076 | - s := C.ptsname(I32(p.tty)); (* here, since it isn't safe after fork *) | |
| 1077 | - if s = nil then return failed(); end; | |
| 1078 | - n := 0; | |
| 1079 | - while s[n] # 0 do INC(n); end; | |
| 1080 | - if not cstr(s[0..n], name) then return -36; end; | |
| 1366 | + err := ptyName(p.tty, name); (* here, since it isn't safe after fork *) | |
| 1367 | + if err # 0 then return err; end; | |
| 1081 | 1368 | end; |
| 1082 | 1369 | none := nil; |
| 1083 | 1370 | if p.onlyEnv then |
| @@ -1092,25 +1379,11 @@ | ||
| 1092 | 1379 | _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *) |
| 1093 | 1380 | pid := C.fork(); |
| 1094 | 1381 | if pid = 0 then |
| 1095 | - if p.tty # 0 then | |
| 1096 | - _ := C.setsid(); | |
| 1097 | - fd := C.open(ADR(name[0]), 2); (* O_RDWR *) | |
| 1098 | - if (fd < 0) or (C.ioctl(fd, TIOCSCTTY, 0) # 0) then C._exit(127); end; | |
| 1099 | - for i := 0 to 2 do _ := C.dup2(fd, I32(i)); end; | |
| 1100 | - else | |
| 1101 | - if p.group = -1 then _ := C.setpgid(0, 0); | |
| 1102 | - elsif p.group # 0 then _ := C.setpgid(0, I32(p.group)); | |
| 1103 | - end; | |
| 1104 | - if p.foreground then _ := C.tcsetpgrp(0, C.getpgrp()); end; | |
| 1105 | - for i := 0 to 2 do | |
| 1106 | - if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end; | |
| 1107 | - end; | |
| 1108 | - end; | |
| 1109 | - if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then C._exit(127); end; | |
| 1110 | - _ := C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *) | |
| 1382 | + if takeOn(p, name, dir) # 0 then C._exit(127); end; | |
| 1383 | + closeAllBut(p.more); | |
| 1111 | 1384 | for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end; |
| 1112 | 1385 | _ := C.sigprocmask(2, ^U8(ADR(empty)), nil); |
| 1113 | - exec(p.path, search, a, argv, argv + (LEN(p.args) + 1)); | |
| 1386 | + run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); | |
| 1114 | 1387 | C._exit(127); |
| 1115 | 1388 | end; |
| 1116 | 1389 | err := failed(); |
| @@ -1127,6 +1400,56 @@ | ||
| 1127 | 1400 | return INT(pid); |
| 1128 | 1401 | end; |
| 1129 | 1402 | |
| 1403 | +(* Runs the program p describes in this one's place, as spawn would | |
| 1404 | + start it, but keeping this program's other fds as they are, open | |
| 1405 | + unless keepOnExec said no; the fds of p.more are kept. It returns | |
| 1406 | + only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no | |
| 1407 | + program to run, before anything is done; or, once the program's | |
| 1408 | + terminal, group, fds and working directory are as p asks, which | |
| 1409 | + stay so, minus the errno that stopped it, with this program's | |
| 1410 | + signals given back as they were. *) | |
| 1411 | +proc exec*(p: Program): INT; | |
| 1412 | +var | |
| 1413 | + a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, r, i: INT; search: []U8; | |
| 1414 | + all, old, empty: [16]U64; dfl: Sigaction; acts: [65]Sigaction; | |
| 1415 | +begin | |
| 1416 | + if not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) | |
| 1417 | + if p.tty # 0 then | |
| 1418 | + r := ptyName(p.tty, name); | |
| 1419 | + if r # 0 then return r; end; | |
| 1420 | + end; | |
| 1421 | + search := env("PATH"); | |
| 1422 | + if LEN(search) = 0 then search := defaultPath; end; | |
| 1423 | + r := find(p.path, search, a); | |
| 1424 | + if r < 0 then return r; end; | |
| 1425 | + none := nil; | |
| 1426 | + if p.onlyEnv then | |
| 1427 | + argv, size := argsAndEnv(p.args, p.env, ADR(none)); | |
| 1428 | + else | |
| 1429 | + argv, size := argsAndEnv(p.args, p.env, C.environ); | |
| 1430 | + end; | |
| 1431 | + if argv = nil then return -12; end; (* ENOMEM *) | |
| 1432 | + r := takeOn(p, name, dir); | |
| 1433 | + if r = 0 then | |
| 1434 | + for i := 0 to LEN(p.more) - 1 do | |
| 1435 | + if p.more[i] > 2 then _ := keepOnExec(p.more[i], true); end; | |
| 1436 | + end; | |
| 1437 | + (* every signal as it is at first and none blocked, as spawn | |
| 1438 | + gives them, each kept to give back if execve fails *) | |
| 1439 | + for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; | |
| 1440 | + _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *) | |
| 1441 | + for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), ^U8(ADR(acts[i]))); end; | |
| 1442 | + _ := C.sigprocmask(2, ^U8(ADR(empty)), nil); | |
| 1443 | + _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1)); | |
| 1444 | + r := failed(); | |
| 1445 | + _ := C.sigprocmask(2, ^U8(ADR(all)), nil); | |
| 1446 | + for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(acts[i])), nil); end; | |
| 1447 | + _ := C.sigprocmask(2, ^U8(ADR(old)), nil); | |
| 1448 | + end; | |
| 1449 | + munmap(^U8(argv), size); | |
| 1450 | + return r; | |
| 1451 | +end; | |
| 1452 | + | |
| 1130 | 1453 | (* Waits for the program pid, or any of this program's if pid is -1, to |
| 1131 | 1454 | change, if block, and says which one changed and how: exited, with |
| 1132 | 1455 | its exit status; killed or stopped, with the signal's number. Without |
| @@ -1318,6 +1641,97 @@ | ||
| 1318 | 1641 | if ignore(sigInt, false) # 0 then exit(2); end; |
| 1319 | 1642 | end; |
| 1320 | 1643 | |
| 1644 | +(* The test of what a shell needs: fds copied, moved and kept on | |
| 1645 | + exec; a program given an fd at its own number, and one not; access, | |
| 1646 | + the umask and the environment by number; a terminal's modes; time | |
| 1647 | + used; limits; who and where; and exec, with nothing to run, and | |
| 1648 | + with a program that can't be run, after which the signals caught | |
| 1649 | + are caught still. *) | |
| 1650 | +proc testShell; | |
| 1651 | +var | |
| 1652 | + r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; | |
| 1653 | + m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; | |
| 1654 | +begin | |
| 1655 | + r, w := pipe(); | |
| 1656 | + if w < 0 then exit(2); end; | |
| 1657 | + fd := dup(w); | |
| 1658 | + if (fd < 0) or (write(fd, "ab") # 2) or (read(r, buf) # 2) then exit(2); end; | |
| 1659 | + close(fd); | |
| 1660 | + if (dupTo(w, 9) # 9) or (write(9, "c") # 1) or (read(r, buf) # 1) or (buf[0] # 'c') then exit(2); end; | |
| 1661 | + if (keepOnExec(9, false) # 0) or (keepOnExec(9, true) # 0) or (keepOnExec(-1, true) # -9) then exit(2); end; | |
| 1662 | + child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo more >&9"], more: [9]}); | |
| 1663 | + close(9); | |
| 1664 | + close(w); | |
| 1665 | + n := 0; | |
| 1666 | + repeat | |
| 1667 | + k := read(r, buf[n..LEN(buf)]); | |
| 1668 | + if k > 0 then INC(n, k); end; | |
| 1669 | + until k <= 0; | |
| 1670 | + close(r); | |
| 1671 | + if buf[0..n] # "more\n" then exit(2); end; | |
| 1672 | + k, st, c := wait(child, true); | |
| 1673 | + if (st # exited) or (c # 0) then exit(2); end; | |
| 1674 | + r, w := pipe(); | |
| 1675 | + if (w < 0) or (dupTo(w, 9) # 9) then exit(2); end; | |
| 1676 | + child := spawn({path: "/bin/sh", args: ["sh", "-c", "{ echo none >&9; } 2>/dev/null"]}); (* 9 is closed in it *) | |
| 1677 | + close(9); | |
| 1678 | + close(w); | |
| 1679 | + if read(r, buf) # 0 then exit(2); end; | |
| 1680 | + close(r); | |
| 1681 | + k, st, c := wait(child, true); | |
| 1682 | + if (st # exited) or (c = 0) then exit(2); end; | |
| 1683 | + | |
| 1684 | + if (access("/bin/sh", {executing}) # 0) or (access("/", {reading}) # 0) | |
| 1685 | + or (access("/no/such/file", {}) # -2) or (open("/bin/sh", {executing}) # -22) then exit(2); end; | |
| 1686 | + mask := umask(3FH); (* 077 *) | |
| 1687 | + if umask(mask) # 3FH then exit(2); end; | |
| 1688 | + path := false; | |
| 1689 | + n := 0; | |
| 1690 | + repeat | |
| 1691 | + e, ok := envAt(n); | |
| 1692 | + if ok and (LEN(e) > 5) and (e[0..5] = "PATH=") then path := true; end; | |
| 1693 | + INC(n); | |
| 1694 | + until not ok; | |
| 1695 | + e, ok := envAt(-1); | |
| 1696 | + if not path or ok or (LEN(e) # 0) then exit(2); end; | |
| 1697 | + | |
| 1698 | + (* a terminal's modes, kept and given back *) | |
| 1699 | + pty := openPty(); | |
| 1700 | + m, err := getMode(pty); | |
| 1701 | + if (err # 0) or not rawMode(pty) then exit(2); end; | |
| 1702 | + raw, err := getMode(pty); | |
| 1703 | + if (err # 0) or (raw.t = m.t) or (setMode(pty, m) # 0) then exit(2); end; | |
| 1704 | + raw, err := getMode(pty); | |
| 1705 | + if (err # 0) or (raw.t # m.t) then exit(2); end; | |
| 1706 | + m, err := getMode(-1); | |
| 1707 | + if err # -9 then exit(2); end; (* EBADF *) | |
| 1708 | + r, w := pipe(); | |
| 1709 | + m, err := getMode(r); | |
| 1710 | + if err # -25 then exit(2); end; (* ENOTTY *) | |
| 1711 | + close(r); | |
| 1712 | + close(w); | |
| 1713 | + close(pty); | |
| 1714 | + | |
| 1715 | + user, system := cpuTime(false); | |
| 1716 | + if (user < 0) or (system < 0) or (user + system = 0) then exit(2); end; | |
| 1717 | + user, system := cpuTime(true); | |
| 1718 | + if (user < 0) or (system < 0) then exit(2); end; | |
| 1719 | + soft, hard, err := limit(limitFiles); | |
| 1720 | + if (err # 0) or (soft < 16) or (setLimit(limitFiles, soft - 1, hard) # 0) then exit(2); end; | |
| 1721 | + s2, h2, err := limit(limitFiles); | |
| 1722 | + if (err # 0) or (s2 # soft - 1) or (h2 # hard) or (setLimit(limitFiles, soft, hard) # 0) then exit(2); end; | |
| 1723 | + soft, hard, err := limit(10000); | |
| 1724 | + if err # -22 then exit(2); end; (* EINVAL *) | |
| 1725 | + e, err := hostName(); | |
| 1726 | + if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; | |
| 1727 | + | |
| 1728 | + sigs := catch([sigUsr1]); | |
| 1729 | + while caught(sigs) # 0 do end; (* the sigChld of the programs above *) | |
| 1730 | + if (sigs < 0) or (exec({path: "no-such-program-on-path", args: ["x"]}) # -2) then exit(2); end; | |
| 1731 | + if exec({path: "/etc/passwd", args: ["x"]}) # -13 then exit(2); end; (* not executable *) | |
| 1732 | + if (kill(pid(), sigUsr1) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigUsr1) then exit(2); end; | |
| 1733 | +end; | |
| 1734 | + | |
| 1321 | 1735 | (* The test of time, random bytes and the network: a sleep the clock |
| 1322 | 1736 | sees; addresses read and refused; a TCP connection on the loopback; |
| 1323 | 1737 | a datagram to a port where no one listens, refused; servers on |
| @@ -1548,6 +1962,7 @@ | ||
| 1548 | 1962 | close(l); |
| 1549 | 1963 | _ := remove("/tmp/raz-sys-socket"); |
| 1550 | 1964 | testPrograms; |
| 1965 | + testShell; | |
| 1551 | 1966 | testNet; |
| 1552 | 1967 | end; |
| 1553 | 1968 |
| @@ -1,5 +1,5 @@ | ||
| 1 | -lib/Sys.raz:12:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz | |
| 2 | - 12 | import C; | |
| 1 | +lib/Sys.raz:16:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz | |
| 2 | + 16 | import C; | |
| 3 | 3 | | ^ |
| 4 | 4 | help: a program has one module of each name; a symbolic link to one of these files chooses it |
| 5 | 5 | raz: 1 error |
| @@ -0,0 +1,2 @@ | ||
| 1 | +replaced | |
| 2 | +closed |
| @@ -0,0 +1,15 @@ | ||
| 1 | +(* Sys.exec over the C library: this program replaced by sh, with an | |
| 2 | + fd it keeps, at its own number, and one it doesn't; sh's output and | |
| 3 | + status are its. *) | |
| 4 | +module ExecC; | |
| 5 | +import Sys; | |
| 6 | + | |
| 7 | +proc main*: INT; | |
| 8 | +begin | |
| 9 | + if (Sys.dupTo(Sys.stdout, 7) # 7) or (Sys.dupTo(Sys.stdout, 8) # 8) then return 1; end; | |
| 10 | + if (Sys.keepOnExec(7, false) # 0) or (Sys.keepOnExec(8, false) # 0) then return 1; end; | |
| 11 | + return Sys.exec({path: "sh", env: ["X=replaced"], more: [7], | |
| 12 | + args: ["sh", "-c", "echo $X >&7; { echo kept >&8; } 2>/dev/null || echo closed; exit 5"]}); | |
| 13 | +end; | |
| 14 | + | |
| 15 | +end ExecC. |
| @@ -0,0 +1 @@ | ||
| 1 | +5 |
| @@ -0,0 +1,2 @@ | ||
| 1 | +replaced | |
| 2 | +closed |
| @@ -0,0 +1,14 @@ | ||
| 1 | +(* Sys.exec: this program replaced by sh, with an fd it keeps, at its | |
| 2 | + own number, and one it doesn't; sh's output and status are its. *) | |
| 3 | +module Exec; | |
| 4 | +import Sys; | |
| 5 | + | |
| 6 | +proc main*: INT; | |
| 7 | +begin | |
| 8 | + if (Sys.dupTo(Sys.stdout, 7) # 7) or (Sys.dupTo(Sys.stdout, 8) # 8) then return 1; end; | |
| 9 | + if (Sys.keepOnExec(7, false) # 0) or (Sys.keepOnExec(8, false) # 0) then return 1; end; | |
| 10 | + return Sys.exec({path: "sh", env: ["X=replaced"], more: [7], | |
| 11 | + args: ["sh", "-c", "echo $X >&7; { echo kept >&8; } 2>/dev/null || echo closed; exit 5"]}); | |
| 12 | +end; | |
| 13 | + | |
| 14 | +end Exec. |
| @@ -0,0 +1 @@ | ||
| 1 | +5 |