Skip to content

Commit b678295

Browse files
committed
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.
1 parent 75897e7 commit b678295

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

src/runtime/env_linux.go renamed to src/runtime/env_unix.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
//go:build linux
1+
//go:build linux || darwin
22

33
package runtime
44

55
// Update the C environment if cgo is loaded.
6-
// Called from syscall.Setenv.
6+
// Called from Go 1.20 and above.
77
//
8-
//go:linkname syscall_setenv_c syscall.setenv_c
9-
func syscall_setenv_c(key string, val string) {
8+
//go:linkname syscallSetenv syscall.runtimeSetenv
9+
func syscallSetenv(key, value string) {
1010
keydata := cstring(key)
11-
valdata := cstring(val)
11+
valdata := cstring(value)
1212
// ignore any errors
1313
libc_setenv(&keydata[0], &valdata[0], 1)
14-
return
1514
}
1615

1716
// Update the C environment if cgo is loaded.
18-
// Called from syscall.Unsetenv.
17+
// Called from Go 1.20 and above.
1918
//
20-
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
21-
func syscall_unsetenv_c(key string) {
19+
//go:linkname syscallUnsetenv syscall.runtimeUnsetenv
20+
func syscallUnsetenv(key string) {
2221
keydata := cstring(key)
2322
// ignore any errors
2423
libc_unsetenv(&keydata[0])
25-
return
24+
}
25+
26+
// Compatibility with Go 1.19 and below.
27+
//
28+
//go:linkname syscall_setenv_c syscall.setenv_c
29+
func syscall_setenv_c(key string, val string) {
30+
syscallSetenv(key, val)
31+
}
32+
33+
// Compatibility with Go 1.19 and below.
34+
//
35+
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
36+
func syscall_unsetenv_c(key string) {
37+
syscallUnsetenv(key)
2638
}
2739

2840
// cstring converts a Go string to a C string.

src/syscall/syscall_libc.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,12 @@ func Setenv(key, val string) (err error) {
197197
return EINVAL
198198
}
199199
}
200-
keydata := cstring(key)
201-
valdata := cstring(val)
202-
errCode := libc_setenv(&keydata[0], &valdata[0], 1)
203-
if errCode != 0 {
204-
err = getErrno()
205-
}
200+
runtimeSetenv(key, val)
206201
return
207202
}
208203

209204
func Unsetenv(key string) (err error) {
210-
keydata := cstring(key)
211-
errCode := libc_unsetenv(&keydata[0])
212-
if errCode != 0 {
213-
err = getErrno()
214-
}
205+
runtimeUnsetenv(key)
215206
return
216207
}
217208

@@ -312,6 +303,10 @@ func splitSlice(p []byte) (buf *byte, len uintptr) {
312303
return slice.buf, slice.len
313304
}
314305

306+
// These two functions are provided by the runtime.
307+
func runtimeSetenv(key, value string)
308+
func runtimeUnsetenv(key string)
309+
315310
//export strlen
316311
func libc_strlen(ptr unsafe.Pointer) uintptr
317312

@@ -325,16 +320,6 @@ func libc_write(fd int32, buf *byte, count uint) int
325320
//export getenv
326321
func libc_getenv(name *byte) *byte
327322

328-
// int setenv(const char *name, const char *val, int replace);
329-
//
330-
//export setenv
331-
func libc_setenv(name *byte, val *byte, replace int32) int32
332-
333-
// int unsetenv(const char *name);
334-
//
335-
//export unsetenv
336-
func libc_unsetenv(name *byte) int32
337-
338323
// ssize_t read(int fd, void *buf, size_t count);
339324
//
340325
//export read

0 commit comments

Comments
 (0)