]> git.proxmox.com Git - wasi-libc.git/commitdiff
Add a function to de-initialize the environment-variable state.
authorDan Gohman <dev@sunfishcode.online>
Thu, 11 Mar 2021 15:26:31 +0000 (07:26 -0800)
committerDan Gohman <dev@sunfishcode.online>
Tue, 25 May 2021 22:25:05 +0000 (15:25 -0700)
Add a `__wasilibc_deinit_environ` function which clears the current
environment variable state to the state where next time the environment
variable functions are called, they'll reinitialize the environment.

And add a `__wasilibc_maybe_reinitialize_environ_eagerly` function to
reinitialize the environment variable state if `environ` or `_environ`
are needed.

These functions are needed by wizer to be able to suspend and resume
a program and have it read new environment variables from the host
environment; see bytecodealliance/wizer#8 for background.

expected/wasm32-wasi/defined-symbols.txt
libc-bottom-half/headers/private/stdlib.h
libc-bottom-half/headers/public/wasi/libc-environ.h
libc-bottom-half/sources/__wasilibc_initialize_environ.c
libc-bottom-half/sources/environ.c

index 7a22631ae221c6a74d1168405d4d19de04f2a256..c97b3268b708ab1aafea684c88cfcbe835e669a0 100644 (file)
@@ -305,6 +305,7 @@ __wasi_sock_send
 __wasi_sock_shutdown
 __wasilibc_access
 __wasilibc_cwd
+__wasilibc_deinitialize_environ
 __wasilibc_ensure_environ
 __wasilibc_environ
 __wasilibc_environ
@@ -316,6 +317,8 @@ __wasilibc_initialize_environ
 __wasilibc_link
 __wasilibc_link_newat
 __wasilibc_link_oldat
+__wasilibc_maybe_reinitialize_environ_eagerly
+__wasilibc_maybe_reinitialize_environ_eagerly
 __wasilibc_nocwd___wasilibc_rmdirat
 __wasilibc_nocwd___wasilibc_unlinkat
 __wasilibc_nocwd_faccessat
index f1e8d6f2262cd0572ea493f5274f5baa1c0ad0a1..8c2b395a6b2c3181e0e07a41bdef0ba736843afe 100644 (file)
@@ -4,3 +4,5 @@
 #include <stddef.h>
 
 #include_next <stdlib.h>
+
+int clearenv(void);
index b404adda1b8b1a13ac5e1dfa2a0045fc5553c070..ffc962e7b5daadafa62c4af3b31d71d29a31b3ad 100644 (file)
@@ -1,6 +1,10 @@
 #ifndef __wasi_libc_environ_h
 #define __wasi_libc_environ_h
 
+/// This header file is a WASI-libc-specific interface, and is not needed by
+/// most programs. Most programs should just use the standard `getenv` and
+/// related APIs, which take care of all of the details automatically.
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -12,6 +16,14 @@ void __wasilibc_initialize_environ(void);
 /// If `__wasilibc_initialize_environ` has not yet been called, call it.
 void __wasilibc_ensure_environ(void);
 
+/// De-initialize the global environment variable state, so that subsequent
+/// calls to `__wasilibc_ensure_environ` call `__wasilibc_initialize_environ`.
+void __wasilibc_deinitialize_environ(void);
+
+/// Call `__wasilibc_initialize_environ` only if `environ` and `_environ` are
+/// referenced in the program.
+void __wasilibc_maybe_reinitialize_environ_eagerly(void);
+
 #ifdef __cplusplus
 }
 #endif
index fe6001a19005cbb6c57e3e16e24df8a81ab21c1c..4ec4d30a6fa71182c2030d1ded134b55e2239f5c 100644 (file)
@@ -75,3 +75,20 @@ oserr:
 software:
     _Exit(EX_SOFTWARE);
 }
+
+// See the comments in libc-environ.h.
+void __wasilibc_deinitialize_environ(void) {
+    if (__wasilibc_environ != (char **)-1) {
+        // Let libc-top-half clear the old environment-variable strings.
+        clearenv();
+        // Set the pointer to the special init value.
+        __wasilibc_environ = (char **)-1;
+    }
+}
+
+// See the comments in libc-environ.h.
+__attribute__((weak))
+void __wasilibc_maybe_reinitialize_environ_eagerly(void) {
+    // This version does nothing. It may be overridden by a version which does
+    // something if `environ` is used.
+}
index bc5a078724cddb93194fb2a7965f0ec5e32bd7df..94d82ee9d66f6cb54fadb299cc7fc9f77add7d14 100644 (file)
@@ -24,3 +24,10 @@ __attribute__((constructor(50)))
 static void __wasilibc_initialize_environ_eagerly(void) {
     __wasilibc_initialize_environ();
 }
+
+// See the comments in libc-environ.h.
+void __wasilibc_maybe_reinitialize_environ_eagerly(void) {
+    // This translation unit is linked in if `environ` is used, meaning we need
+    // to eagerly reinitialize the environment variables.
+    __wasilibc_initialize_environ();
+}