From 35fa926d9ecff7fb6dedcd914ded379faf98088f Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:53:58 +0100 Subject: [PATCH] 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. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 40172d9..c2f6bee 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -11,7 +11,7 @@ | Module | What | |---|---| | `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 | -| `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 | +| `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 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | @@ -263,6 +263,7 @@ proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) proc spawn*(p: Program): INT; (* a program started: its process id *) proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) +proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *) proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) proc kill*(pid, signal: INT): INT; @@ -404,6 +405,15 @@ run, with nothing done; or, if the program is found but won't start, minus the errno, with what `p` asks of the terminal, the group, the files and the directory done, and this program's signals as they were. + +`execve(path, args, env)` is the kernel's execve, for a shell, which +has looked for the program along its own `PATH` and set up the child +already: the program at `path` is run with `args` and with `env`, +`NAME=value`, for its whole environment. Nothing is searched for, and +every fd and signal stays as it is, but that a handled signal does what +it does at first: an ignored one stays ignored, as `trap '' HUP` wants +of the command it comes before. It returns only if it can't: minus an +errno, with nothing changed. A shell moves its own files with `dup` and `dupTo`, for `2>&1` and `exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index d081d15..9618e90 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -1719,6 +1719,23 @@ return r; end; +(* Runs the program at path in this one's place, with args, the first + its name, and env, NAME=value, its whole environment, as the + kernel's execve does and a shell wants: no search, and every fd and + signal as it is, but that a handled signal does what it does at + first. It returns only if it can't: minus an errno. *) +proc execve*(path: []U8; args, env: [][]U8): INT; +var a: [4096]U8; argv: ^^U8; none: ^U8; size, r: INT; +begin + if not cstr(path, a) then return -36; end; (* ENAMETOOLONG *) + none := nil; + argv, size := argsAndEnv(args, env, ADR(none)); + if argv = nil then return -12; end; (* ENOMEM *) + r := SYSCALL(59, ADR(a), argv, argv + (LEN(args) + 1)); (* execve *) + munmap(^U8(argv), size); + return r; +end; + (* Waits for the program pid, any of this program's if pid is -1, or any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with its exit status; killed or stopped, with the signal's number. Without diff --git a/lib/Sys.raz b/lib/Sys.raz index 50af531..e7b4680 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -1522,6 +1522,24 @@ return r; end; +(* Runs the program at path in this one's place, with args, the first + its name, and env, NAME=value, its whole environment, as the + kernel's execve does and a shell wants: no search, and every fd and + signal as it is, but that a handled signal does what it does at + first. It returns only if it can't: minus an errno. *) +proc execve*(path: []U8; args, env: [][]U8): INT; +var a: [4096]U8; argv: ^^U8; none: ^U8; size, r: INT; +begin + if not cstr(path, a) then return -36; end; (* ENAMETOOLONG *) + none := nil; + argv, size := argsAndEnv(args, env, ADR(none)); + if argv = nil then return -12; end; (* ENOMEM *) + _ := C.execve(ADR(a[0]), argv, argv + (LEN(args) + 1)); + r := failed(); + munmap(^U8(argv), size); + return r; +end; + (* Waits for the program pid, any of this program's if pid is -1, or any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with its exit status; killed or stopped, with the signal's number. Without diff --git a/test/libc/ExecveC.expect b/test/libc/ExecveC.expect new file mode 100644 index 0000000..dc13a50 --- /dev/null +++ b/test/libc/ExecveC.expect @@ -0,0 +1,3 @@ +only none +ignored 1 +caught 0 diff --git a/test/libc/ExecveC.raz b/test/libc/ExecveC.raz new file mode 100644 index 0000000..136279f --- /dev/null +++ b/test/libc/ExecveC.raz @@ -0,0 +1,15 @@ +(* Sys.execve over the C library: this program replaced by sh, found by no search, + with the environment it is given and no other, a signal ignored + still ignored and one handled back to what it does at first. *) +module ExecveC; +import Sys; + +proc main*: INT; +begin + if (Sys.ignore(1, true) # 0) or (Sys.handle(10) # 0) then return 1; end; (* SIGHUP, SIGUSR1 *) + return Sys.execve("/bin/sh", ["sh", "-c", + "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 > 9 & 1));; esac; done