Commit 1f62a784 authored by Akshat Kumar's avatar Akshat Kumar Committed by Ron Minnich

syscall: Plan 9: keep a consistent environment array

Map order is non-deterministic. Introduce a new
environment string array that tracks the env map.
This allows us to produce identical results for
Environ() upon successive calls, as expected by the
TestConsistentEnviron test in package os.

R=rsc, ality, rminnich, bradfitz, r
CC=golang-dev
https://golang.org/cl/7411047
parent 65983695
...@@ -15,12 +15,15 @@ var ( ...@@ -15,12 +15,15 @@ var (
// envOnce guards copyenv, which populates env. // envOnce guards copyenv, which populates env.
envOnce sync.Once envOnce sync.Once
// envLock guards env. // envLock guards env and envs.
envLock sync.RWMutex envLock sync.RWMutex
// env maps from an environment variable to its value. // env maps from an environment variable to its value.
env = make(map[string]string) env = make(map[string]string)
// envs contains elements of env in the form "key=value".
envs []string
errZeroLengthKey = errors.New("zero length key") errZeroLengthKey = errors.New("zero length key")
errShortWrite = errors.New("i/o count too small") errShortWrite = errors.New("i/o count too small")
) )
...@@ -71,12 +74,16 @@ func copyenv() { ...@@ -71,12 +74,16 @@ func copyenv() {
if err != nil { if err != nil {
return return
} }
envs = make([]string, len(files))
i := 0
for _, key := range files { for _, key := range files {
v, err := readenv(key) v, err := readenv(key)
if err != nil { if err != nil {
continue continue
} }
env[key] = v env[key] = v
envs[i] = key + "=" + v
i++
} }
} }
...@@ -96,6 +103,7 @@ func Getenv(key string) (value string, found bool) { ...@@ -96,6 +103,7 @@ func Getenv(key string) (value string, found bool) {
return "", false return "", false
} }
env[key] = v env[key] = v
envs = append(envs, key+"="+v)
return v, true return v, true
} }
...@@ -112,6 +120,7 @@ func Setenv(key, value string) error { ...@@ -112,6 +120,7 @@ func Setenv(key, value string) error {
return err return err
} }
env[key] = value env[key] = value
envs = append(envs, key+"="+value)
return nil return nil
} }
...@@ -120,6 +129,7 @@ func Clearenv() { ...@@ -120,6 +129,7 @@ func Clearenv() {
defer envLock.Unlock() defer envLock.Unlock()
env = make(map[string]string) env = make(map[string]string)
envs = []string{}
RawSyscall(SYS_RFORK, RFCENVG, 0, 0) RawSyscall(SYS_RFORK, RFCENVG, 0, 0)
} }
...@@ -128,11 +138,5 @@ func Environ() []string { ...@@ -128,11 +138,5 @@ func Environ() []string {
defer envLock.RUnlock() defer envLock.RUnlock()
envOnce.Do(copyenv) envOnce.Do(copyenv)
a := make([]string, len(env)) return append([]string(nil), envs...)
i := 0
for k, v := range env {
a[i] = k + "=" + v
i++
}
return a
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment