raz / commit 35fa926d

commit
35fa926d9ecff7fb6dedcd914ded379faf98088f
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6dfa68f1
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.

patch · log

9 files changed, +84 -1

doc/raz-lib.md (history) modified +11 -1

@@ -11,7 +11,7 @@
1111 | Module | What |
1212 |---|---|
1313 | `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 |
1515 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
1616 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
1717 | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike |
@@ -263,6 +263,7 @@
263263 proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *)
264264 proc spawn*(p: Program): INT; (* a program started: its process id *)
265265 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 *)
266267 proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *)
267268 proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *)
268269 proc kill*(pid, signal: INT): INT;
@@ -404,6 +405,15 @@
404405 run, with nothing done; or, if the program is found but won't start,
405406 minus the errno, with what `p` asks of the terminal, the group, the
406407 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.
407417
408418 A shell moves its own files with `dup` and `dupTo`, for `2>&1` and
409419 `exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the

lib/Sys.linux-x86_64.raz (history) modified +17 -0

@@ -1719,6 +1719,23 @@
17191719 return r;
17201720 end;
17211721
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+
17221739 (* Waits for the program pid, any of this program's if pid is -1, or
17231740 any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with
17241741 its exit status; killed or stopped, with the signal's number. Without

lib/Sys.raz (history) modified +18 -0

@@ -1522,6 +1522,24 @@
15221522 return r;
15231523 end;
15241524
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+
15251543 (* Waits for the program pid, any of this program's if pid is -1, or
15261544 any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with
15271545 its exit status; killed or stopped, with the signal's number. Without

test/libc/ExecveC.expect (history) added +3 -0

@@ -0,0 +1,3 @@
1+only none
2+ignored 1
3+caught 0

test/libc/ExecveC.raz (history) added +15 -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.

test/libc/ExecveC.status (history) added +1 -0

@@ -0,0 +1 @@
1+5

test/run/Execve.expect (history) added +3 -0

@@ -0,0 +1,3 @@
1+only none
2+ignored 1
3+caught 0

test/run/Execve.raz (history) added +15 -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.

test/run/Execve.status (history) added +1 -0

@@ -0,0 +1 @@
1+5