1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
// Copyright 2012 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.
package runtime
import "unsafe"
const (
// Plan 9 environment device
envDir = "/env/"
// size of buffer to read from a directory
dirBufSize = 4096
// size of buffer to read an environment variable (may grow)
envBufSize = 128
// offset of the name field in a 9P directory entry - see syscall.UnmarshalDir()
nameOffset = 39
)
// Goenvs caches the Plan 9 environment variables at start of execution into
// string array envs, to supply the initial contents for os.Environ.
// Subsequent calls to os.Setenv will change this cache, without writing back
// to the (possibly shared) Plan 9 environment, so that Setenv and Getenv
// conform to the same Posix semantics as on other operating systems.
// For Plan 9 shared environment semantics, instead of Getenv(key) and
// Setenv(key, value), one can use os.ReadFile("/env/" + key) and
// os.WriteFile("/env/" + key, value, 0666) respectively.
//
//go:nosplit
func goenvs() {
buf := make([]byte, envBufSize)
copy(buf, envDir)
dirfd := open(&buf[0], _OREAD, 0)
if dirfd < 0 {
return
}
defer closefd(dirfd)
dofiles(dirfd, func(name []byte) {
name = append(name, 0)
buf = buf[:len(envDir)]
copy(buf, envDir)
buf = append(buf, name...)
fd := open(&buf[0], _OREAD, 0)
if fd < 0 {
return
}
defer closefd(fd)
n := len(buf)
r := 0
for {
r = int(pread(fd, unsafe.Pointer(&buf[0]), int32(n), 0))
if r < n {
break
}
n = int(seek(fd, 0, 2)) + 1
if len(buf) < n {
buf = make([]byte, n)
}
}
if r <= 0 {
r = 0
} else if buf[r-1] == 0 {
r--
}
name[len(name)-1] = '='
env := make([]byte, len(name)+r)
copy(env, name)
copy(env[len(name):], buf[:r])
envs = append(envs, string(env))
})
}
// Dofiles reads the directory opened with file descriptor fd, applying function f
// to each filename in it.
//
//go:nosplit
func dofiles(dirfd int32, f func([]byte)) {
dirbuf := new([dirBufSize]byte)
var off int64 = 0
for {
n := pread(dirfd, unsafe.Pointer(&dirbuf[0]), int32(dirBufSize), off)
if n <= 0 {
return
}
for b := dirbuf[:n]; len(b) > 0; {
var name []byte
name, b = gdirname(b)
if name == nil {
return
}
f(name)
}
off += int64(n)
}
}
// Gdirname returns the first filename from a buffer of directory entries,
// and a slice containing the remaining directory entries.
// If the buffer doesn't start with a valid directory entry, the returned name is nil.
//
//go:nosplit
func gdirname(buf []byte) (name []byte, rest []byte) {
if 2+nameOffset+2 > len(buf) {
return
}
entryLen, buf := gbit16(buf)
if entryLen > len(buf) {
return
}
n, b := gbit16(buf[nameOffset:])
if n > len(b) {
return
}
name = b[:n]
rest = buf[entryLen:]
return
}
// Gbit16 reads a 16-bit little-endian binary number from b and returns it
// with the remaining slice of b.
//
//go:nosplit
func gbit16(b []byte) (int, []byte) {
return int(b[0]) | int(b[1])<<8, b[2:]
}
|