Skip to content

Commit fcad29c

Browse files
committed
core/overreach: impl set/get runtime's internal env
1 parent f17060e commit fcad29c

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

intra/core/overreach.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
package core
88

99
import (
10+
"os"
1011
"runtime/debug"
1112
"syscall"
1213
_ "unsafe" // for go:linkname
1314
)
1415

15-
// RuntimeEnviron returns the Go runtime's cached environment slice.
16-
func RuntimeEnviron() []string {
17-
return syscall.Environ()
18-
}
19-
2016
// github.com/golang/go/issues/69868
2117
// Unfortunately, Android apps have AT_SECURE set
2218
// (read bytes in /proc/self/auxv on non-rooted Androids).
@@ -60,6 +56,46 @@ func RuntimeFinishDebugVarsSetup() {
6056
runtime_finishDebugVarsSetup()
6157
}
6258

59+
// RuntimeEnviron returns the Go runtime's cached environment vars.
60+
// github.com/golang/go/blob/e2fef50def98/src/runtime/runtime1.go#L98
61+
func RuntimeEnviron() []string {
62+
return runtime_environ()
63+
}
64+
65+
// SetRuntimeEnviron sets / adds a key-value pair in the Go runtime's
66+
// cached environment vars.
67+
func SetRuntimeEnviron(key, val string) (found bool, err error) {
68+
envs := runtime_environ()
69+
kv := key + "="
70+
for i, e := range envs {
71+
if len(e) >= len(kv) && e[:len(kv)] == kv {
72+
envs[i] = kv + val
73+
found = true
74+
break
75+
}
76+
}
77+
if !found {
78+
envs = append(envs, kv+val)
79+
}
80+
return found, os.Setenv(key, val)
81+
}
82+
83+
// GetRuntimeEnviron gets a value from the Go runtime's cached
84+
// environment vars.
85+
func GetRuntimeEnviron(key string) (val string, found bool) {
86+
envs := runtime_environ()
87+
kv := key + "="
88+
for _, e := range envs {
89+
if len(e) >= len(kv) && e[:len(kv)] == kv {
90+
return e[len(kv):], true
91+
}
92+
}
93+
return
94+
}
95+
96+
//go:linkname runtime_environ runtime.environ
97+
func runtime_environ() []string
98+
6399
//go:linkname runtime_finishDebugVarsSetup runtime.finishDebugVarsSetup
64100
func runtime_finishDebugVarsSetup()
65101

intra/tun2socks.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ func LogLevel(gologLevel, consolelogLevel int32) {
120120
// github.com/golang/go/blob/fed3b0a298/src/runtime/runtime1.go#L586
121121
// gomobile builds a c-shared gojnilib:
122122
// github.com/golang/mobile/blob/2553ed8ce2/cmd/gomobile/bind_androidapp.go#L393
123-
prevtraceback := os.Getenv("GOTRACEBACK")
123+
prevtraceback, _ := core.GetRuntimeEnviron("GOTRACEBACK")
124124
if settings.Debug {
125-
os.Setenv("GOTRACEBACK", sys.s())
125+
core.SetRuntimeEnviron("GOTRACEBACK", sys.s())
126126
debug.SetTraceback(sys.s())
127127
} else {
128-
os.Setenv("GOTRACEBACK", usr.s())
128+
core.SetRuntimeEnviron("GOTRACEBACK", usr.s())
129129
debug.SetTraceback(usr.s())
130130
}
131-
curtraceback := os.Getenv("GOTRACEBACK")
131+
curtraceback, _ := core.GetRuntimeEnviron("GOTRACEBACK")
132132
core.RuntimeFinishDebugVarsSetup()
133133
gotracelevel, gotraceall, gotracecrash := core.RuntimeGotraceback()
134134

0 commit comments

Comments
 (0)