syscall: refactor environment handling

* Move environment functions to their own files.
  * Rewrite the WASIp2 version of environment variables to be much
    simpler (don't go through C functions).
This commit is contained in:
Ayke van Laethem
2024-11-18 11:43:50 +01:00
committed by Ayke
parent e12da15f7d
commit 289fceb3ea
6 changed files with 142 additions and 146 deletions
-52
View File
@@ -1227,58 +1227,6 @@ func p2fileTypeToStatType(t types.DescriptorType) uint32 {
return 0
}
var libc_envs map[string]string
func populateEnvironment() {
libc_envs = make(map[string]string)
for _, kv := range environment.GetEnvironment().Slice() {
libc_envs[kv[0]] = kv[1]
}
}
// char * getenv(const char *name);
//
//export getenv
func getenv(key *byte) *byte {
k := goString(key)
v, ok := libc_envs[k]
if !ok {
return nil
}
// The new allocation is zero-filled; allocating an extra byte and then
// copying the data over will leave the last byte untouched,
// null-terminating the string.
vbytes := make([]byte, len(v)+1)
copy(vbytes, v)
return unsafe.SliceData(vbytes)
}
// int setenv(const char *name, const char *value, int overwrite);
//
//export setenv
func setenv(key, value *byte, overwrite int) int {
k := goString(key)
if _, ok := libc_envs[k]; ok && overwrite == 0 {
return 0
}
v := goString(value)
libc_envs[k] = v
return 0
}
// int unsetenv(const char *name);
//
//export unsetenv
func unsetenv(key *byte) int {
k := goString(key)
delete(libc_envs, k)
return 0
}
// void arc4random_buf (void *, size_t);
//
//export arc4random_buf