1
0
mirror of https://github.com/golang/go synced 2024-10-05 22:31:22 -06:00
go/src/syscall/env_plan9.go
Brad Fitzpatrick bc9748ee6b syscall: make pwd process-wide on Plan 9
On Plan 9, the pwd is apparently per-thread not per process. That
means different goroutines saw different current directories, even
changing within a goroutine as they were scheduled.

Instead, track the the process-wide pwd protected by a mutex in the
syscall package and set the current goroutine thread's pwd to the
correct once at critical points.

Fixes #9428

Change-Id: I928e90886355be4a95c2be834f5883e2b50fc0cf
Reviewed-on: https://go-review.googlesource.com/6350
Reviewed-by: David du Colombier <0intro@gmail.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-02-28 18:17:35 +00:00

109 lines
1.8 KiB
Go

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Plan 9 environment variables.
package syscall
import (
"errors"
)
var (
errZeroLengthKey = errors.New("zero length key")
errShortWrite = errors.New("i/o count too small")
)
func readenv(key string) (string, error) {
fd, err := open("/env/"+key, O_RDONLY)
if err != nil {
return "", err
}
defer Close(fd)
l, _ := Seek(fd, 0, 2)
Seek(fd, 0, 0)
buf := make([]byte, l)
n, err := Read(fd, buf)
if err != nil {
return "", err
}
if n > 0 && buf[n-1] == 0 {
buf = buf[:n-1]
}
return string(buf), nil
}
func writeenv(key, value string) error {
fd, err := create("/env/"+key, O_RDWR, 0666)
if err != nil {
return err
}
defer Close(fd)
b := []byte(value)
n, err := Write(fd, b)
if err != nil {
return err
}
if n != len(b) {
return errShortWrite
}
return nil
}
func Getenv(key string) (value string, found bool) {
if len(key) == 0 {
return "", false
}
v, err := readenv(key)
if err != nil {
return "", false
}
return v, true
}
func Setenv(key, value string) error {
if len(key) == 0 {
return errZeroLengthKey
}
err := writeenv(key, value)
if err != nil {
return err
}
return nil
}
func Clearenv() {
RawSyscall(SYS_RFORK, RFCENVG, 0, 0)
}
func Unsetenv(key string) error {
if len(key) == 0 {
return errZeroLengthKey
}
Remove("/env/" + key)
return nil
}
func Environ() []string {
fd, err := open("/env", O_RDONLY)
if err != nil {
return nil
}
defer Close(fd)
files, err := readdirnames(fd)
if err != nil {
return nil
}
ret := make([]string, 0, len(files))
for _, key := range files {
v, err := readenv(key)
if err != nil {
continue
}
ret = append(ret, key+"="+v)
}
return ret
}