syscall: implement setenv/unsetenv in the runtime
This is expected starting with Go 1.20. I've also applied the same modification to syscall_libc.go so that setenv is only called in a single place.
Этот коммит содержится в:
родитель
19db0144ae
коммит
3177591b31
2 изменённых файлов: 28 добавлений и 31 удалений
|
@ -1,28 +1,40 @@
|
||||||
//go:build linux
|
//go:build linux || darwin
|
||||||
|
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
// Update the C environment if cgo is loaded.
|
// Update the C environment if cgo is loaded.
|
||||||
// Called from syscall.Setenv.
|
// Called from Go 1.20 and above.
|
||||||
//
|
//
|
||||||
//go:linkname syscall_setenv_c syscall.setenv_c
|
//go:linkname syscallSetenv syscall.runtimeSetenv
|
||||||
func syscall_setenv_c(key string, val string) {
|
func syscallSetenv(key, value string) {
|
||||||
keydata := cstring(key)
|
keydata := cstring(key)
|
||||||
valdata := cstring(val)
|
valdata := cstring(value)
|
||||||
// ignore any errors
|
// ignore any errors
|
||||||
libc_setenv(&keydata[0], &valdata[0], 1)
|
libc_setenv(&keydata[0], &valdata[0], 1)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the C environment if cgo is loaded.
|
// Update the C environment if cgo is loaded.
|
||||||
// Called from syscall.Unsetenv.
|
// Called from Go 1.20 and above.
|
||||||
//
|
//
|
||||||
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
|
//go:linkname syscallUnsetenv syscall.runtimeUnsetenv
|
||||||
func syscall_unsetenv_c(key string) {
|
func syscallUnsetenv(key string) {
|
||||||
keydata := cstring(key)
|
keydata := cstring(key)
|
||||||
// ignore any errors
|
// ignore any errors
|
||||||
libc_unsetenv(&keydata[0])
|
libc_unsetenv(&keydata[0])
|
||||||
return
|
}
|
||||||
|
|
||||||
|
// Compatibility with Go 1.19 and below.
|
||||||
|
//
|
||||||
|
//go:linkname syscall_setenv_c syscall.setenv_c
|
||||||
|
func syscall_setenv_c(key string, val string) {
|
||||||
|
syscallSetenv(key, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compatibility with Go 1.19 and below.
|
||||||
|
//
|
||||||
|
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
|
||||||
|
func syscall_unsetenv_c(key string) {
|
||||||
|
syscallUnsetenv(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// cstring converts a Go string to a C string.
|
// cstring converts a Go string to a C string.
|
|
@ -197,21 +197,12 @@ func Setenv(key, val string) (err error) {
|
||||||
return EINVAL
|
return EINVAL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keydata := cstring(key)
|
runtimeSetenv(key, val)
|
||||||
valdata := cstring(val)
|
|
||||||
errCode := libc_setenv(&keydata[0], &valdata[0], 1)
|
|
||||||
if errCode != 0 {
|
|
||||||
err = getErrno()
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Unsetenv(key string) (err error) {
|
func Unsetenv(key string) (err error) {
|
||||||
keydata := cstring(key)
|
runtimeUnsetenv(key)
|
||||||
errCode := libc_unsetenv(&keydata[0])
|
|
||||||
if errCode != 0 {
|
|
||||||
err = getErrno()
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,6 +303,10 @@ func splitSlice(p []byte) (buf *byte, len uintptr) {
|
||||||
return slice.buf, slice.len
|
return slice.buf, slice.len
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// These two functions are provided by the runtime.
|
||||||
|
func runtimeSetenv(key, value string)
|
||||||
|
func runtimeUnsetenv(key string)
|
||||||
|
|
||||||
//export strlen
|
//export strlen
|
||||||
func libc_strlen(ptr unsafe.Pointer) uintptr
|
func libc_strlen(ptr unsafe.Pointer) uintptr
|
||||||
|
|
||||||
|
@ -325,16 +320,6 @@ func libc_write(fd int32, buf *byte, count uint) int
|
||||||
//export getenv
|
//export getenv
|
||||||
func libc_getenv(name *byte) *byte
|
func libc_getenv(name *byte) *byte
|
||||||
|
|
||||||
// int setenv(const char *name, const char *val, int replace);
|
|
||||||
//
|
|
||||||
//export setenv
|
|
||||||
func libc_setenv(name *byte, val *byte, replace int32) int32
|
|
||||||
|
|
||||||
// int unsetenv(const char *name);
|
|
||||||
//
|
|
||||||
//export unsetenv
|
|
||||||
func libc_unsetenv(name *byte) int32
|
|
||||||
|
|
||||||
// ssize_t read(int fd, void *buf, size_t count);
|
// ssize_t read(int fd, void *buf, size_t count);
|
||||||
//
|
//
|
||||||
//export read
|
//export read
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче