Sys: execve, the kernel's, for a shell exec runs a program as spawn would: every signal back to what it does at first, ignored ones too, and a search along the process's PATH. A shell wants neither: trap '' HUP must keep the command alive, and it searches its own PATH. execve(path, args, env) is the kernel's: no search, fds and signals as they are, but that a handled signal goes back to its default. concha's Ckern goes with it.
| @@ -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`, `dup`, `dupTo`, `dupAbove`, `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`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; what an errno means, `errorText`; 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`, `dupAbove`, `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`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; what an errno means, `errorText`; 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 | |
| @@ -263,6 +263,7 @@ | ||
| 263 | 263 | proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) |
| 264 | 264 | proc spawn*(p: Program): INT; (* a program started: its process id *) |
| 265 | 265 | proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) |
| 266 | +proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *) | |
| 266 | 267 | proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) |
| 267 | 268 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) |
| 268 | 269 | proc kill*(pid, signal: INT): INT; |
| @@ -404,6 +405,15 @@ | ||
| 404 | 405 | run, with nothing done; or, if the program is found but won't start, |
| 405 | 406 | minus the errno, with what `p` asks of the terminal, the group, the |
| 406 | 407 | files and the directory done, and this program's signals as they were. |
| 408 | + | |
| 409 | +`execve(path, args, env)` is the kernel's execve, for a shell, which | |
| 410 | +has looked for the program along its own `PATH` and set up the child | |
| 411 | +already: the program at `path` is run with `args` and with `env`, | |
| 412 | +`NAME=value`, for its whole environment. Nothing is searched for, and | |
| 413 | +every fd and signal stays as it is, but that a handled signal does what | |
| 414 | +it does at first: an ignored one stays ignored, as `trap '' HUP` wants | |
| 415 | +of the command it comes before. It returns only if it can't: minus an | |
| 416 | +errno, with nothing changed. | |
| 407 | 417 | |
| 408 | 418 | A shell moves its own files with `dup` and `dupTo`, for `2>&1` and |
| 409 | 419 | `exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the |
| @@ -1719,6 +1719,23 @@ | ||
| 1719 | 1719 | return r; |
| 1720 | 1720 | end; |
| 1721 | 1721 | |
| 1722 | +(* Runs the program at path in this one's place, with args, the first | |
| 1723 | + its name, and env, NAME=value, its whole environment, as the | |
| 1724 | + kernel's execve does and a shell wants: no search, and every fd and | |
| 1725 | + signal as it is, but that a handled signal does what it does at | |
| 1726 | + first. It returns only if it can't: minus an errno. *) | |
| 1727 | +proc execve*(path: []U8; args, env: [][]U8): INT; | |
| 1728 | +var a: [4096]U8; argv: ^^U8; none: ^U8; size, r: INT; | |
| 1729 | +begin | |
| 1730 | + if not cstr(path, a) then return -36; end; (* ENAMETOOLONG *) | |
| 1731 | + none := nil; | |
| 1732 | + argv, size := argsAndEnv(args, env, ADR(none)); | |
| 1733 | + if argv = nil then return -12; end; (* ENOMEM *) | |
| 1734 | + r := SYSCALL(59, ADR(a), argv, argv + (LEN(args) + 1)); (* execve *) | |
| 1735 | + munmap(^U8(argv), size); | |
| 1736 | + return r; | |
| 1737 | +end; | |
| 1738 | + | |
| 1722 | 1739 | (* Waits for the program pid, any of this program's if pid is -1, or |
| 1723 | 1740 | any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with |
| 1724 | 1741 | its exit status; killed or stopped, with the signal's number. Without |
| @@ -1522,6 +1522,24 @@ | ||
| 1522 | 1522 | return r; |
| 1523 | 1523 | end; |
| 1524 | 1524 | |
| 1525 | +(* Runs the program at path in this one's place, with args, the first | |
| 1526 | + its name, and env, NAME=value, its whole environment, as the | |
| 1527 | + kernel's execve does and a shell wants: no search, and every fd and | |
| 1528 | + signal as it is, but that a handled signal does what it does at | |
| 1529 | + first. It returns only if it can't: minus an errno. *) | |
| 1530 | +proc execve*(path: []U8; args, env: [][]U8): INT; | |
| 1531 | +var a: [4096]U8; argv: ^^U8; none: ^U8; size, r: INT; | |
| 1532 | +begin | |
| 1533 | + if not cstr(path, a) then return -36; end; (* ENAMETOOLONG *) | |
| 1534 | + none := nil; | |
| 1535 | + argv, size := argsAndEnv(args, env, ADR(none)); | |
| 1536 | + if argv = nil then return -12; end; (* ENOMEM *) | |
| 1537 | + _ := C.execve(ADR(a[0]), argv, argv + (LEN(args) + 1)); | |
| 1538 | + r := failed(); | |
| 1539 | + munmap(^U8(argv), size); | |
| 1540 | + return r; | |
| 1541 | +end; | |
| 1542 | + | |
| 1525 | 1543 | (* Waits for the program pid, any of this program's if pid is -1, or |
| 1526 | 1544 | any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with |
| 1527 | 1545 | its exit status; killed or stopped, with the signal's number. Without |
| @@ -0,0 +1,3 @@ | ||
| 1 | +only none | |
| 2 | +ignored 1 | |
| 3 | +caught 0 |
| @@ -0,0 +1,15 @@ | ||
| 1 | +(* Sys.execve over the C library: this program replaced by sh, found by no search, | |
| 2 | + with the environment it is given and no other, a signal ignored | |
| 3 | + still ignored and one handled back to what it does at first. *) | |
| 4 | +module ExecveC; | |
| 5 | +import Sys; | |
| 6 | + | |
| 7 | +proc main*: INT; | |
| 8 | +begin | |
| 9 | + if (Sys.ignore(1, true) # 0) or (Sys.handle(10) # 0) then return 1; end; (* SIGHUP, SIGUSR1 *) | |
| 10 | + return Sys.execve("/bin/sh", ["sh", "-c", | |
| 11 | + "echo $X ${HOME-none}; while read k v; do case $k in SigIgn:) echo ignored $((0x$v & 1));; SigCgt:) echo caught $((0x$v >> 9 & 1));; esac; done </proc/self/status; exit 5"], | |
| 12 | + ["X=only"]); | |
| 13 | +end; | |
| 14 | + | |
| 15 | +end ExecveC. |
| @@ -0,0 +1 @@ | ||
| 1 | +5 |
| @@ -0,0 +1,3 @@ | ||
| 1 | +only none | |
| 2 | +ignored 1 | |
| 3 | +caught 0 |
| @@ -0,0 +1,15 @@ | ||
| 1 | +(* Sys.execve: this program replaced by sh, found by no search, | |
| 2 | + with the environment it is given and no other, a signal ignored | |
| 3 | + still ignored and one handled back to what it does at first. *) | |
| 4 | +module Execve; | |
| 5 | +import Sys; | |
| 6 | + | |
| 7 | +proc main*: INT; | |
| 8 | +begin | |
| 9 | + if (Sys.ignore(1, true) # 0) or (Sys.handle(10) # 0) then return 1; end; (* SIGHUP, SIGUSR1 *) | |
| 10 | + return Sys.execve("/bin/sh", ["sh", "-c", | |
| 11 | + "echo $X ${HOME-none}; while read k v; do case $k in SigIgn:) echo ignored $((0x$v & 1));; SigCgt:) echo caught $((0x$v >> 9 & 1));; esac; done </proc/self/status; exit 5"], | |
| 12 | + ["X=only"]); | |
| 13 | +end; | |
| 14 | + | |
| 15 | +end Execve. |
| @@ -0,0 +1 @@ | ||
| 1 | +5 |