2009-06-01 23:14:57 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2011-12-19 09:57:58 -07:00
|
|
|
// +build darwin freebsd linux netbsd openbsd
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
package syscall
|
|
|
|
|
2011-06-14 09:29:51 -06:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2010-04-02 02:11:17 -06:00
|
|
|
var (
|
|
|
|
Stdin = 0
|
|
|
|
Stdout = 1
|
|
|
|
Stderr = 2
|
|
|
|
)
|
|
|
|
|
2011-06-20 16:40:20 -06:00
|
|
|
const darwinAMD64 = OS == "darwin" && ARCH == "amd64"
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
|
|
|
|
func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
|
|
|
|
func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
|
|
|
|
func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
|
2011-06-14 09:29:51 -06:00
|
|
|
|
|
|
|
// Mmap manager, for use by operating system-specific implementations.
|
|
|
|
|
|
|
|
type mmapper struct {
|
|
|
|
sync.Mutex
|
|
|
|
active map[*byte][]byte // active mappings; key is last byte in mapping
|
2011-11-13 20:44:52 -07:00
|
|
|
mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
|
|
|
|
munmap func(addr uintptr, length uintptr) error
|
2011-06-14 09:29:51 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
|
2011-06-14 09:29:51 -06:00
|
|
|
if length <= 0 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map the requested memory.
|
|
|
|
addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
|
2011-11-13 20:44:52 -07:00
|
|
|
if errno != nil {
|
2011-06-14 09:29:51 -06:00
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice memory layout
|
|
|
|
var sl = struct {
|
|
|
|
addr uintptr
|
|
|
|
len int
|
|
|
|
cap int
|
|
|
|
}{addr, length, length}
|
|
|
|
|
|
|
|
// Use unsafe to turn sl into a []byte.
|
|
|
|
b := *(*[]byte)(unsafe.Pointer(&sl))
|
|
|
|
|
|
|
|
// Register mapping in m and return it.
|
|
|
|
p := &b[cap(b)-1]
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
m.active[p] = b
|
2011-11-13 20:44:52 -07:00
|
|
|
return b, nil
|
2011-06-14 09:29:51 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func (m *mmapper) Munmap(data []byte) (err error) {
|
2011-06-14 09:29:51 -06:00
|
|
|
if len(data) == 0 || len(data) != cap(data) {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the base of the mapping.
|
|
|
|
p := &data[cap(data)-1]
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
b := m.active[p]
|
|
|
|
if b == nil || &b[0] != &data[0] {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap the memory and update m.
|
2011-11-13 20:44:52 -07:00
|
|
|
if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
|
2011-06-14 09:29:51 -06:00
|
|
|
return errno
|
|
|
|
}
|
2011-10-18 07:56:34 -06:00
|
|
|
delete(m.active, p)
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// An Errno is an unsigned number describing an error condition.
|
|
|
|
// It implements the error interface. The zero Errno is by convention
|
|
|
|
// a non-error, so code to convert from Errno to error should use:
|
|
|
|
// err = nil
|
|
|
|
// if errno != 0 {
|
|
|
|
// err = errno
|
|
|
|
// }
|
|
|
|
type Errno uintptr
|
|
|
|
|
|
|
|
func (e Errno) Error() string {
|
|
|
|
if 0 <= e && int(e) < len(errors) {
|
|
|
|
s := errors[e]
|
|
|
|
if s != "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "errno " + itoa(int(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Errno) Temporary() bool {
|
|
|
|
return e == EINTR || e == EMFILE || e.Timeout()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Errno) Timeout() bool {
|
|
|
|
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
|
2011-06-14 09:29:51 -06:00
|
|
|
}
|