1
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
6
// This file is compiled as ordinary Go code,
7
// but it is also input to mksyscall,
8
// which parses the //sys lines and generates system call stubs.
9
// Note that sometimes we use a lowercase //sys name and
10
// wrap it in our own nicer implementation.
20
// A Note is a string describing a process note.
21
// It implements the os.Signal interface.
24
func (n Note) Signal() {}
26
func (n Note) String() string {
36
// For testing: clients can set this flag to force
37
// creation of IPv6 sockets to return EAFNOSUPPORT.
38
var SocketDisableIPv6 bool
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)
45
func atoi(b []byte) (n uint) {
47
for i := 0; i < len(b); i++ {
48
n = n*10 + uint(b[i]-'0')
53
func cstring(s []byte) string {
54
i := bytes.IndexByte(s, 0)
64
RawSyscall(SYS_ERRSTR, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), 0)
67
return cstring(buf[:])
70
// Implemented in assembly to import from runtime.
73
func Exit(code int) { exit(code) }
75
func readnum(path string) (uint, error) {
78
fd, e := Open(path, O_RDONLY)
84
n, e := Pread(fd, b[:], 0)
91
for ; m < n && b[m] == ' '; m++ {
94
return atoi(b[m : n-1]), nil
97
func Getpid() (pid int) {
98
n, _ := readnum("#c/pid")
102
func Getppid() (ppid int) {
103
n, _ := readnum("#c/ppid")
107
func Read(fd int, p []byte) (n int, err error) {
108
return Pread(fd, p, -1)
111
func Write(fd int, p []byte) (n int, err error) {
112
return Pwrite(fd, p, -1)
117
//sys fd2path(fd int, buf []byte) (err error)
119
func Fd2path(fd int) (path string, err error) {
122
e := fd2path(fd, buf[:])
126
return cstring(buf[:]), nil
129
//sys pipe(p *[2]int32) (err error)
131
func Pipe(p []int) (err error) {
133
return syscall.ErrorString("bad arg in system call")
144
// Underlying system call writes to newoffset via pointer.
145
// Implemented in assembly to avoid allocation.
146
func seek(placeholder uintptr, fd int, offset int64, whence int) (newoffset int64, err string)
148
func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
149
newoffset, e := seek(0, fd, offset, whence)
152
err = syscall.ErrorString(e)
157
func Mkdir(path string, mode uint32) (err error) {
158
fd, err := Create(path, O_RDONLY, DMDIR|mode)
173
func (w Waitmsg) Exited() bool { return true }
174
func (w Waitmsg) Signaled() bool { return false }
176
func (w Waitmsg) ExitStatus() int {
178
// a normal exit returns no message
184
//sys await(s []byte) (n int, err error)
186
func Await(w *Waitmsg) (err error) {
190
n, err := await(buf[:])
192
if err != nil || w == nil {
198
for i := 0; i < n && nf < len(f)-1; i++ {
209
return syscall.ErrorString("invalid wait message")
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])
217
// await() returns '' for no error
223
func Unmount(name, old string) (err error) {
225
oldp, err := BytePtrFromString(old)
229
oldptr := uintptr(unsafe.Pointer(oldp))
232
var e syscall.ErrorString
234
// bind(2) man page: If name is zero, everything bound or mounted upon old is unbound or unmounted.
236
r0, _, e = Syscall(SYS_UNMOUNT, _zero, oldptr, 0)
238
namep, err := BytePtrFromString(name)
242
r0, _, e = Syscall(SYS_UNMOUNT, uintptr(unsafe.Pointer(namep)), oldptr, 0)
251
func Fchdir(fd int) (err error) {
252
path, err := Fd2path(fd)
261
type Timespec struct {
271
func NsecToTimeval(nsec int64) (tv Timeval) {
272
nsec += 999 // round up to microsecond
273
tv.Usec = int32(nsec % 1e9 / 1e3)
274
tv.Sec = int32(nsec / 1e9)
281
r0, _, _ := Syscall(SYS_NSEC, uintptr(unsafe.Pointer(&scratch)), 0, 0)
282
// TODO(aram): remove hack after I fix _nsec in the pc64 kernel.
289
func Gettimeofday(tv *Timeval) error {
291
*tv = NsecToTimeval(nsec)
295
func Getpagesize() int { return 0x1000 }
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 }
302
func Getgroups() (gids []int, err error) {
303
return make([]int, 0), nil
306
//sys open(path string, mode int) (fd int, err error)
308
func Open(path string, mode int) (fd int, err error) {
310
return open(path, mode)
313
//sys create(path string, mode int, perm uint32) (fd int, err error)
315
func Create(path string, mode int, perm uint32) (fd int, err error) {
317
return create(path, mode, perm)
320
//sys remove(path string) (err error)
322
func Remove(path string) error {
327
//sys stat(path string, edir []byte) (n int, err error)
329
func Stat(path string, edir []byte) (n int, err error) {
331
return stat(path, edir)
334
//sys bind(name string, old string, flag int) (err error)
336
func Bind(name string, old string, flag int) (err error) {
338
return bind(name, old, flag)
341
//sys mount(fd int, afd int, old string, flag int, aname string) (err error)
343
func Mount(fd int, afd int, old string, flag int, aname string) (err error) {
345
return mount(fd, afd, old, flag, aname)
348
//sys wstat(path string, edir []byte) (err error)
350
func Wstat(path string, edir []byte) (err error) {
352
return wstat(path, edir)
355
//sys chdir(path string) (err error)
356
//sys Dup(oldfd int, newfd int) (fd int, err error)
357
//sys Pread(fd int, p []byte, offset int64) (n int, err error)
358
//sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
359
//sys Close(fd int) (err error)
360
//sys Fstat(fd int, edir []byte) (n int, err error)
361
//sys Fwstat(fd int, edir []byte) (err error)