From 870ce96965f0bdbdd071bf927050e5ce41dd8701 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:14:38 +0100 Subject: [PATCH] Sys: setEnv, for this program and those it starts On Linux the environment becomes Sys's own the first time a variable is added, as C's setenv makes it: an array of pointers that doubles when full, a variable set again keeping its place, and the NAME=values from blocks of 64 KB, never given back, so that what env gave stays good. env, envAt, spawn and exec read it through environment. Over C, setenv. Library growth that breaks no program, met in rwalk's tests. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 0600265..eac5926 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -583,23 +583,6 @@ Met in rrem's parser, documenting `(?>x*)`. -## Setting an environment variable - -`Sys.env` reads a variable; nothing sets one. A test can't give the -programs it runs, or the code it tests, an environment of its own: -rwalk's tests read the user's real global ignore file, since they -can't point `XDG_CONFIG_HOME` at a directory of their own. - -The proposal, in `Sys`: - -``` -proc setEnv*(name, value: []U8): INT; (* sets name to value, for this program and those it starts; 0, or minus an errno *) -``` - -It breaks no program. - -Met in rwalk's tests. - ## A hint for a hexadecimal name `FFFDH`, meant as a number, is an identifier, and the error is "FFFDH diff --git a/doc/raz-lib.md b/doc/raz-lib.md index a57ad42..40172d9 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`, `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` 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 | | `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 | @@ -254,6 +254,7 @@ proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) +proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *) proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) proc setWorkDir*(path: []U8): INT; @@ -424,7 +425,10 @@ taken away from the mode of every file this program, and those it starts, make, and says what they were. `envAt` gives the environment one variable at a time, from 0, for a shell to list it and to pass it -on whole with `onlyEnv`. `getMode` reads a terminal's modes, and +on whole with `onlyEnv`. `setEnv` refuses a name that is empty or +holds an `=`, and a 0 in either, with -22; as C's `setenv`, it isn't +safe while another thread reads the environment, and what `env` gave +for a variable may be gone once it sets that variable again. `getMode` reads a terminal's modes, and `setMode` sets them again once what was written is sent, so that a shell keeps them for each job: a program stopped by Ctrl-Z with its terminal raw gets it back raw, and the shell its own between. diff --git a/lib/C.raz b/lib/C.raz index 2f1ba1b..7e4b986 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -55,6 +55,7 @@ extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; extern proc getrandom*(buf: ^U8; n: UINT; flags: U32): INT; extern proc getenv*(name: ^U8): ^U8; +extern proc setenv*(name, value: ^U8; overwrite: I32): I32; extern proc strerror*(err: I32): ^U8; extern proc getcwd*(buf: ^U8; n: UINT): ^U8; extern proc chdir*(path: ^U8): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index d056514..174cfbe 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -138,6 +138,12 @@ sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *) uts: [390]U8; (* struct utsname, which hostName gives the nodename of *) + (* the environment setEnv made, nil while it is the one the program + started with; its variables, and the pointers it has room for *) + envp: ^^U8; + envLen, envCap: INT; + envText: ^U8; (* where setEnv puts its next NAME=value *) + envLeft: INT; (* and the bytes left there *) (* Writes s to the file descriptor fd; the number of bytes written, or minus an errno. *) @@ -1151,21 +1157,36 @@ return errorTexts[err]; end; -(* The value of the environment variable name; "" if it isn't set. The - environment follows the arguments and their nil. *) +(* The environment, its NAME=values as C strings and a nil: the one the + program started with, after the arguments and their nil, until + setEnv makes one of its own. *) +proc environment: ^^U8; +begin + if envp # nil then return envp; end; + return ARGV() + (ARGC() + 1); +end; + +(* Whether the NAME=value s, a C string, is name's. *) +proc named(s: ^U8; name: []U8): BOOL; +var n: INT; +begin + n := 0; + while (n < LEN(name)) and (s[n] = name[n]) do INC(n); end; + return (n = LEN(name)) and (s[n] = '='); +end; + +(* The value of the environment variable name; "" if it isn't set. *) proc env*(name: []U8): []U8; -var e: ^^U8; s: ^U8; i, n, m: INT; +var e: ^^U8; s: ^U8; i, m: INT; begin - e := ARGV() + (ARGC() + 1); + e := environment(); i := 0; while e[i] # nil do - s := e[i]; - n := 0; - while (n < LEN(name)) and (s[n] = name[n]) do INC(n); end; - if (n = LEN(name)) and (s[n] = '=') then - m := n + 1; + if named(e[i], name) then + s := e[i]; + m := LEN(name) + 1; while s[m] # 0 do INC(m); end; - return s[n + 1..m]; + return s[LEN(name) + 1..m]; end; INC(i); end; @@ -1178,7 +1199,7 @@ var e: ^^U8; s: ^U8; j, n: INT; begin if i < 0 then return "", false; end; - e := ARGV() + (ARGC() + 1); + e := environment(); for j := 0 to i do if e[j] = nil then return "", false; end; end; @@ -1280,6 +1301,71 @@ return p, size; end; +(* Sets the environment variable name to value, for env and for the + programs this one starts; 0, or minus an errno: -22 if name is empty + or holds an = or a 0, or value a 0, and -12 if there is no memory. + As C's setenv does, it makes the environment the program's own the + first time it adds a variable: an array of pointers that doubles + when it is full, the old one given back. A variable set again keeps + its place. A NAME=value is never given back, so that what env gave + stays good for the rest of the run; they are taken in turn from + blocks of 64 KB. It isn't safe while another thread reads the + environment or starts a program, as C's setenv isn't. *) +proc setEnv*(name, value: []U8): INT; +var e, p: ^^U8; s: ^U8; size, cap, n, i: INT; +begin + if LEN(name) = 0 then return -22; end; (* EINVAL *) + for i := 0 to LEN(name) - 1 do + if (name[i] = '=') or (name[i] = 0) then return -22; end; + end; + for i := 0 to LEN(value) - 1 do + if value[i] = 0 then return -22; end; + end; + (* NAME=value, written where the next one goes, and taken only once + the array has room for it *) + size := LEN(name) + LEN(value) + 2; + if size > envLeft then + n := 10000H; + if size > n then n := size; end; + s := mmap(n); + if s = nil then return -12; end; (* ENOMEM *) + envText := s; + envLeft := n; + end; + s := envText; + COPY(s[0..LEN(name)], name); + s[LEN(name)] := '='; + COPY(s[LEN(name) + 1..size - 1], value); + s[size - 1] := 0; + e := environment(); + if envp = nil then + envLen := 0; + while e[envLen] # nil do INC(envLen); end; + end; + i := 0; + while (i < envLen) and not named(e[i], name) do INC(i); end; + if i = envLen then + (* a new variable: the array grows if it is the first, or full *) + if (envp = nil) or (envLen + 1 = envCap) then + cap := 2 * (envLen + 1); + if cap < 32 then cap := 32; end; + p := ^^U8(mmap(cap * SIZE(^U8))); + if p = nil then return -12; end; + if envLen > 0 then COPY(p[0..envLen], e[0..envLen]); end; + if envp # nil then munmap(^U8(envp), envCap * SIZE(^U8)); end; + envp := p; + envCap := cap; + e := p; + end; + INC(envLen); + e[envLen] := nil; + end; + e[i] := s; + envText := envText + size; + envLeft := envLeft - size; + return 0; +end; + (* A pipe: the fd to read and the fd to write, both closed in a program exec starts; or minus an errno and -1. *) proc pipe*: (INT, INT); @@ -1553,7 +1639,7 @@ if p.onlyEnv then argv, size := argsAndEnv(p.args, p.env, ADR(none)); else - argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); + argv, size := argsAndEnv(p.args, p.env, environment()); end; if argv = nil then return -12; end; (* ENOMEM *) search := env("PATH"); (* read here, before the fork *) @@ -1604,7 +1690,7 @@ if p.onlyEnv then argv, size := argsAndEnv(p.args, p.env, ADR(none)); else - argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); + argv, size := argsAndEnv(p.args, p.env, environment()); end; if argv = nil then return -12; end; (* ENOMEM *) r := takeOn(p, name, dir); @@ -2146,6 +2232,26 @@ if (w # 80) or (h # 24) then exit(2); end; (* the environment *) if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; + (* the environment set: refused, added, set again, grown past its first array *) + if (setEnv("", "x") # -22) or (setEnv("A=B", "x") # -22) or (setEnv("A", "\x00") # -22) then exit(2); end; + if (setEnv("RAZ_SYS_TEST", "one") # 0) or (env("RAZ_SYS_TEST") # "one") then exit(2); end; + name := env("RAZ_SYS_TEST"); + if (setEnv("RAZ_SYS_TEST", "") # 0) or (env("RAZ_SYS_TEST") # "") or (name # "one") then exit(2); end; + buf := "RAZ_SYS_TEST_.."; + for k := 0 to 99 do + buf[13] := U8('0' + k div 10); + buf[14] := U8('0' + k mod 10); + if setEnv(buf[0..15], buf[13..15]) # 0 then exit(2); end; + end; + if (env("RAZ_SYS_TEST_42") # "42") or (env("RAZ_SYS_TEST") # "") or (LEN(env("PATH")) = 0) then exit(2); end; + n := 0; + k := 0; + repeat + name, ok := envAt(k); + if (LEN(name) > 12) and (name[0..12] = "RAZ_SYS_TEST") then INC(n); end; + INC(k); + until not ok; + if n # 101 then exit(2); end; name, err := workDir(); if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end; if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end; diff --git a/lib/Sys.raz b/lib/Sys.raz index b0860b5..50af531 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -1141,6 +1141,33 @@ return p, size; end; +(* Sets the environment variable name to value, for env and for the + programs this one starts; 0, or minus an errno: -22 if name is empty + or holds an = or a 0, or value a 0, and -12 if there is no memory. + The C library's setenv keeps the environment. It isn't safe while + another thread reads the environment or starts a program. *) +proc setEnv*(name, value: []U8): INT; +var p: ^U8; size, i, r: INT; +begin + if LEN(name) = 0 then return -22; end; (* EINVAL *) + for i := 0 to LEN(name) - 1 do + if (name[i] = '=') or (name[i] = 0) then return -22; end; + end; + for i := 0 to LEN(value) - 1 do + if value[i] = 0 then return -22; end; + end; + (* both as C strings, in memory that comes zeroed *) + size := LEN(name) + LEN(value) + 2; + p := mmap(size); + if p = nil then return -12; end; (* ENOMEM *) + COPY(p[0..LEN(name)], name); + COPY(p[LEN(name) + 1..size - 1], value); + r := 0; + if C.setenv(p, p + (LEN(name) + 1), 1) # 0 then r := failed(); end; + munmap(p, size); + return r; +end; + (* A pipe: the fd to read and the fd to write, both closed in a program exec starts; or minus an errno and -1. *) proc pipe*: (INT, INT); @@ -2031,6 +2058,26 @@ if (w # 80) or (h # 24) then exit(2); end; (* the environment *) if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; + (* the environment set: refused, added, set again, grown past its first array *) + if (setEnv("", "x") # -22) or (setEnv("A=B", "x") # -22) or (setEnv("A", "\x00") # -22) then exit(2); end; + if (setEnv("RAZ_SYS_TEST", "one") # 0) or (env("RAZ_SYS_TEST") # "one") then exit(2); end; + name := env("RAZ_SYS_TEST"); + if (setEnv("RAZ_SYS_TEST", "") # 0) or (env("RAZ_SYS_TEST") # "") or (name # "one") then exit(2); end; + buf := "RAZ_SYS_TEST_.."; + for k := 0 to 99 do + buf[13] := U8('0' + k div 10); + buf[14] := U8('0' + k mod 10); + if setEnv(buf[0..15], buf[13..15]) # 0 then exit(2); end; + end; + if (env("RAZ_SYS_TEST_42") # "42") or (env("RAZ_SYS_TEST") # "") or (LEN(env("PATH")) = 0) then exit(2); end; + n := 0; + k := 0; + repeat + name, ok := envAt(k); + if (LEN(name) > 12) and (name[0..12] = "RAZ_SYS_TEST") then INC(n); end; + INC(k); + until not ok; + if n # 101 then exit(2); end; name, err := workDir(); if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end; if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end; diff --git a/test/libc/SetEnvC.expect b/test/libc/SetEnvC.expect new file mode 100644 index 0000000..c7cb36b --- /dev/null +++ b/test/libc/SetEnvC.expect @@ -0,0 +1,4 @@ +second +1 +given +1 diff --git a/test/libc/SetEnvC.raz b/test/libc/SetEnvC.raz new file mode 100644 index 0000000..120a78f --- /dev/null +++ b/test/libc/SetEnvC.raz @@ -0,0 +1,26 @@ +(* Sys.setEnv over C: a variable set, then set again, reaches a program this + one starts, as one variable; one it named in its own env wins. *) +module SetEnvC; +import Out, Rt, Sys; + +proc run(env: [][]U8); +var p: Sys.Program; pid: INT; +begin + p.path := "/bin/sh"; + p.args := ["sh", "-c", "echo $RAZ_SET_ENV; env | grep -c ^RAZ_SET_ENV="]; + p.env := env; + Out.flush; + pid := Sys.spawn(p); + if pid < 0 then Rt.stop("SetEnv: no sh"); end; + _, _, _ := Sys.wait(pid, true); +end; + +proc main*; +begin + if Sys.setEnv("RAZ_SET_ENV", "first") # 0 then Rt.stop("SetEnv: set"); end; + if Sys.setEnv("RAZ_SET_ENV", "second") # 0 then Rt.stop("SetEnv: set again"); end; + run([]); + run(["RAZ_SET_ENV=given"]); +end; + +end SetEnvC. diff --git a/test/run/SetEnv.expect b/test/run/SetEnv.expect new file mode 100644 index 0000000..c7cb36b --- /dev/null +++ b/test/run/SetEnv.expect @@ -0,0 +1,4 @@ +second +1 +given +1 diff --git a/test/run/SetEnv.raz b/test/run/SetEnv.raz new file mode 100644 index 0000000..9b2a8a5 --- /dev/null +++ b/test/run/SetEnv.raz @@ -0,0 +1,26 @@ +(* Sys.setEnv: a variable set, then set again, reaches a program this + one starts, as one variable; one it named in its own env wins. *) +module SetEnv; +import Out, Rt, Sys; + +proc run(env: [][]U8); +var p: Sys.Program; pid: INT; +begin + p.path := "/bin/sh"; + p.args := ["sh", "-c", "echo $RAZ_SET_ENV; env | grep -c ^RAZ_SET_ENV="]; + p.env := env; + Out.flush; + pid := Sys.spawn(p); + if pid < 0 then Rt.stop("SetEnv: no sh"); end; + _, _, _ := Sys.wait(pid, true); +end; + +proc main*; +begin + if Sys.setEnv("RAZ_SET_ENV", "first") # 0 then Rt.stop("SetEnv: set"); end; + if Sys.setEnv("RAZ_SET_ENV", "second") # 0 then Rt.stop("SetEnv: set again"); end; + run([]); + run(["RAZ_SET_ENV=given"]); +end; + +end SetEnv.