1
2
3
4
5
6
7
8
9
10
11
12 package plan9
13
14 import (
15 "bytes"
16 "syscall"
17 "unsafe"
18 )
19
20
21
22 type Note string
23
24 func (n Note) Signal() {}
25
26 func (n Note) String() string {
27 return string(n)
28 }
29
30 var (
31 Stdin = 0
32 Stdout = 1
33 Stderr = 2
34 )
35
36
37
38 var SocketDisableIPv6 bool
39
40 func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.ErrorString)
41 func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.ErrorString)
42 func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
43 func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr)
44
45 func atoi(b []byte) (n uint) {
46 n = 0
47 for i := 0; i < len(b); i++ {
48 n = n*10 + uint(b[i]-'0')
49 }
50 return
51 }
52
53 func cstring(s []byte) string {
54 i := bytes.IndexByte(s, 0)
55 if i == -1 {
56 i = len(s)
57 }
58 return string(s[:i])
59 }
60
61 func errstr() string {
62 var buf [ERRMAX]byte
63
64 RawSyscall(SYS_ERRSTR, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), 0)
65
66 buf[len(buf)-1] = 0
67 return cstring(buf[:])
68 }
69
70
71 func exit(code int)
72
73 func Exit(code int) { exit(code) }
74
75 func readnum(path string) (uint, error) {
76 var b [12]byte
77
78 fd, e := Open(path, O_RDONLY)
79 if e != nil {
80 return 0, e
81 }
82 defer Close(fd)
83
84 n, e := Pread(fd, b[:], 0)
85
86 if e != nil {
87 return 0, e
88 }
89
90 m := 0
91 for ; m < n && b[m] == ' '; m++ {
92 }
93
94 return atoi(b[m : n-1]), nil
95 }
96
97 func Getpid() (pid int) {
98 n, _ := readnum("#c/pid")
99 return int(n)
100 }
101
102 func Getppid() (ppid int) {
103 n, _ := readnum("#c/ppid")
104 return int(n)
105 }
106
107 func Read(fd int, p []byte) (n int, err error) {
108 return Pread(fd, p, -1)
109 }
110
111 func Write(fd int, p []byte) (n int, err error) {
112 return Pwrite(fd, p, -1)
113 }
114
115 var ioSync int64
116
117
118
119 func Fd2path(fd int) (path string, err error) {
120 var buf [512]byte
121
122 e := fd2path(fd, buf[:])
123 if e != nil {
124 return "", e
125 }
126 return cstring(buf[:]), nil
127 }
128
129
130
131 func Pipe(p []int) (err error) {
132 if len(p) != 2 {
133 return syscall.ErrorString("bad arg in system call")
134 }
135 var pp [2]int32
136 err = pipe(&pp)
137 if err == nil {
138 p[0] = int(pp[0])
139 p[1] = int(pp[1])
140 }
141 return
142 }
143
144
145
146 func seek(placeholder uintptr, fd int, offset int64, whence int) (newoffset int64, err string)
147
148 func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
149 newoffset, e := seek(0, fd, offset, whence)
150
151 if newoffset == -1 {
152 err = syscall.ErrorString(e)
153 }
154 return
155 }
156
157 func Mkdir(path string, mode uint32) (err error) {
158 fd, err := Create(path, O_RDONLY, DMDIR|mode)
159
160 if fd != -1 {
161 Close(fd)
162 }
163
164 return
165 }
166
167 type Waitmsg struct {
168 Pid int
169 Time [3]uint32
170 Msg string
171 }
172
173 func (w Waitmsg) Exited() bool { return true }
174 func (w Waitmsg) Signaled() bool { return false }
175
176 func (w Waitmsg) ExitStatus() int {
177 if len(w.Msg) == 0 {
178
179 return 0
180 }
181 return 1
182 }
183
184
185
186 func Await(w *Waitmsg) (err error) {
187 var buf [512]byte
188 var f [5][]byte
189
190 n, err := await(buf[:])
191
192 if err != nil || w == nil {
193 return
194 }
195
196 nf := 0
197 p := 0
198 for i := 0; i < n && nf < len(f)-1; i++ {
199 if buf[i] == ' ' {
200 f[nf] = buf[p:i]
201 p = i + 1
202 nf++
203 }
204 }
205 f[nf] = buf[p:]
206 nf++
207
208 if nf != len(f) {
209 return syscall.ErrorString("invalid wait message")
210 }
211 w.Pid = int(atoi(f[0]))
212 w.Time[0] = uint32(atoi(f[1]))
213 w.Time[1] = uint32(atoi(f[2]))
214 w.Time[2] = uint32(atoi(f[3]))
215 w.Msg = cstring(f[4])
216 if w.Msg == "''" {
217
218 w.Msg = ""
219 }
220 return
221 }
222
223 func Unmount(name, old string) (err error) {
224 fixwd()
225 oldp, err := BytePtrFromString(old)
226 if err != nil {
227 return err
228 }
229 oldptr := uintptr(unsafe.Pointer(oldp))
230
231 var r0 uintptr
232 var e syscall.ErrorString
233
234
235 if name == "" {
236 r0, _, e = Syscall(SYS_UNMOUNT, _zero, oldptr, 0)
237 } else {
238 namep, err := BytePtrFromString(name)
239 if err != nil {
240 return err
241 }
242 r0, _, e = Syscall(SYS_UNMOUNT, uintptr(unsafe.Pointer(namep)), oldptr, 0)
243 }
244
245 if int32(r0) == -1 {
246 err = e
247 }
248 return
249 }
250
251 func Fchdir(fd int) (err error) {
252 path, err := Fd2path(fd)
253
254 if err != nil {
255 return
256 }
257
258 return Chdir(path)
259 }
260
261 type Timespec struct {
262 Sec int32
263 Nsec int32
264 }
265
266 type Timeval struct {
267 Sec int32
268 Usec int32
269 }
270
271 func NsecToTimeval(nsec int64) (tv Timeval) {
272 nsec += 999
273 tv.Usec = int32(nsec % 1e9 / 1e3)
274 tv.Sec = int32(nsec / 1e9)
275 return
276 }
277
278 func nsec() int64 {
279 var scratch int64
280
281 r0, _, _ := Syscall(SYS_NSEC, uintptr(unsafe.Pointer(&scratch)), 0, 0)
282
283 if r0 == 0 {
284 return scratch
285 }
286 return int64(r0)
287 }
288
289 func Gettimeofday(tv *Timeval) error {
290 nsec := nsec()
291 *tv = NsecToTimeval(nsec)
292 return nil
293 }
294
295 func Getpagesize() int { return 0x1000 }
296
297 func Getegid() (egid int) { return -1 }
298 func Geteuid() (euid int) { return -1 }
299 func Getgid() (gid int) { return -1 }
300 func Getuid() (uid int) { return -1 }
301
302 func Getgroups() (gids []int, err error) {
303 return make([]int, 0), nil
304 }
305
306
307
308 func Open(path string, mode int) (fd int, err error) {
309 fixwd()
310 return open(path, mode)
311 }
312
313
314
315 func Create(path string, mode int, perm uint32) (fd int, err error) {
316 fixwd()
317 return create(path, mode, perm)
318 }
319
320
321
322 func Remove(path string) error {
323 fixwd()
324 return remove(path)
325 }
326
327
328
329 func Stat(path string, edir []byte) (n int, err error) {
330 fixwd()
331 return stat(path, edir)
332 }
333
334
335
336 func Bind(name string, old string, flag int) (err error) {
337 fixwd()
338 return bind(name, old, flag)
339 }
340
341
342
343 func Mount(fd int, afd int, old string, flag int, aname string) (err error) {
344 fixwd()
345 return mount(fd, afd, old, flag, aname)
346 }
347
348
349
350 func Wstat(path string, edir []byte) (err error) {
351 fixwd()
352 return wstat(path, edir)
353 }
354
355
356
357
358
359
360
361
362
View as plain text