|
7 | 7 | package core |
8 | 8 |
|
9 | 9 | import ( |
| 10 | + "os" |
10 | 11 | "runtime/debug" |
11 | 12 | "syscall" |
12 | 13 | _ "unsafe" // for go:linkname |
13 | 14 | ) |
14 | 15 |
|
15 | | -// RuntimeEnviron returns the Go runtime's cached environment slice. |
16 | | -func RuntimeEnviron() []string { |
17 | | - return syscall.Environ() |
18 | | -} |
19 | | - |
20 | 16 | // github.com/golang/go/issues/69868 |
21 | 17 | // Unfortunately, Android apps have AT_SECURE set |
22 | 18 | // (read bytes in /proc/self/auxv on non-rooted Androids). |
@@ -60,6 +56,46 @@ func RuntimeFinishDebugVarsSetup() { |
60 | 56 | runtime_finishDebugVarsSetup() |
61 | 57 | } |
62 | 58 |
|
| 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 | + |
63 | 99 | //go:linkname runtime_finishDebugVarsSetup runtime.finishDebugVarsSetup |
64 | 100 | func runtime_finishDebugVarsSetup() |
65 | 101 |
|
|
0 commit comments