wasi-libc/libc-bottom-half/sources/environ.c
Dan Gohman 82fc2c4f44 Add a function to de-initialize the environment-variable state.
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.
2021-05-25 15:25:05 -07:00

34 lines
1.3 KiB
C

#include <unistd.h>
#include <stdlib.h>
#include <sysexits.h>
#include <wasi/api.h>
#include <wasi/libc.h>
#include <wasi/libc-environ.h>
// If the program does use `environ`, it'll get this version of
// `__wasilibc_environ`, which is initialized with a constructor function, so
// that it's initialized whenever user code might want to access it.
char **__wasilibc_environ;
extern __typeof(__wasilibc_environ) _environ
__attribute__((weak, alias("__wasilibc_environ")));
extern __typeof(__wasilibc_environ) environ
__attribute__((weak, alias("__wasilibc_environ")));
// We define this function here in the same source file as
// `__wasilibc_environ`, so that this function is called in iff environment
// variable support is used.
// Concerning the 50 -- levels up to 100 are reserved for the implementation,
// so we an arbitrary number in the middle of the range to allow other
// reserved things to go before or after.
__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();
}