2010-08-20 12:20:12 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2010-03-26 14:23:54 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-08-23 09:51:25 -06:00
|
|
|
// +build darwin dragonfly freebsd 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
|
|
|
|
2010-03-26 14:23:54 -06:00
|
|
|
// BSD system call wrappers shared by *BSD based systems
|
|
|
|
// including OS X (Darwin) and FreeBSD. Like the other
|
|
|
|
// syscall_*.go files it is compiled as Go code but also
|
|
|
|
// used as input to mksyscall which parses the //sys
|
|
|
|
// lines and generates system call stubs.
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
2012-01-13 14:40:55 -07:00
|
|
|
import (
|
2012-07-29 17:50:23 -06:00
|
|
|
"runtime"
|
2012-01-13 14:40:55 -07:00
|
|
|
"unsafe"
|
|
|
|
)
|
2010-03-26 14:23:54 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapped
|
|
|
|
*/
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error)
|
|
|
|
//sysnb setgroups(ngid int, gid *_Gid_t) (err error)
|
2010-03-26 14:23:54 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Getgroups() (gids []int, err error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
n, err := getgroups(0, nil)
|
2011-11-13 20:44:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
if n == 0 {
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil, nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check group count. Max is 16 on BSD.
|
|
|
|
if n < 0 || n > 1000 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
a := make([]_Gid_t, n)
|
|
|
|
n, err = getgroups(n, &a[0])
|
2011-11-13 20:44:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
gids = make([]int, n)
|
|
|
|
for i, v := range a[0:n] {
|
|
|
|
gids[i] = int(v)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Setgroups(gids []int) (err error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
if len(gids) == 0 {
|
|
|
|
return setgroups(0, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := make([]_Gid_t, len(gids))
|
|
|
|
for i, v := range gids {
|
|
|
|
a[i] = _Gid_t(v)
|
|
|
|
}
|
|
|
|
return setgroups(len(a), &a[0])
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func ReadDirent(fd int, buf []byte) (n int, err error) {
|
2011-04-06 13:44:40 -06:00
|
|
|
// Final argument is (basep *uintptr) and the syscall doesn't take nil.
|
2014-01-17 14:19:00 -07:00
|
|
|
// 64 bits should be enough. (32 bits isn't even on 386). Since the
|
|
|
|
// actual system call is getdirentries64, 64 is a good guess.
|
2011-04-06 13:44:40 -06:00
|
|
|
// TODO(rsc): Can we use a single global basep for all calls?
|
2014-01-17 14:19:00 -07:00
|
|
|
var base = (*uintptr)(unsafe.Pointer(new(uint64)))
|
|
|
|
return Getdirentries(fd, buf, base)
|
2011-04-06 13:44:40 -06:00
|
|
|
}
|
|
|
|
|
2010-03-26 14:23:54 -06:00
|
|
|
// Wait status is 7 bits at bottom, either 0 (exited),
|
|
|
|
// 0x7F (stopped), or a signal number that caused an exit.
|
|
|
|
// The 0x80 bit is whether there was a core dump.
|
|
|
|
// An extra number (exit code, signal causing a stop)
|
|
|
|
// is in the high bits.
|
|
|
|
|
|
|
|
type WaitStatus uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
mask = 0x7F
|
|
|
|
core = 0x80
|
|
|
|
shift = 8
|
|
|
|
|
|
|
|
exited = 0
|
|
|
|
stopped = 0x7F
|
|
|
|
)
|
|
|
|
|
|
|
|
func (w WaitStatus) Exited() bool { return w&mask == exited }
|
|
|
|
|
|
|
|
func (w WaitStatus) ExitStatus() int {
|
|
|
|
if w&mask != exited {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return int(w >> shift)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
|
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
func (w WaitStatus) Signal() Signal {
|
|
|
|
sig := Signal(w & mask)
|
2010-03-26 14:23:54 -06:00
|
|
|
if sig == stopped || sig == 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return sig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
|
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
func (w WaitStatus) Stopped() bool { return w&mask == stopped && Signal(w>>shift) != SIGSTOP }
|
2010-03-26 14:23:54 -06:00
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
func (w WaitStatus) Continued() bool { return w&mask == stopped && Signal(w>>shift) == SIGSTOP }
|
2010-03-26 14:23:54 -06:00
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
func (w WaitStatus) StopSignal() Signal {
|
2010-03-26 14:23:54 -06:00
|
|
|
if !w.Stopped() {
|
|
|
|
return -1
|
|
|
|
}
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
return Signal(w>>shift) & 0xFF
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w WaitStatus) TrapCause() int { return -1 }
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
|
2010-03-26 14:23:54 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
var status _C_int
|
2011-11-13 20:44:52 -07:00
|
|
|
wpid, err = wait4(pid, &status, options, rusage)
|
2010-03-26 14:23:54 -06:00
|
|
|
if wstatus != nil {
|
|
|
|
*wstatus = WaitStatus(status)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
|
2014-01-21 19:54:49 -07:00
|
|
|
//sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
|
|
|
|
//sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sysnb socket(domain int, typ int, proto int) (fd int, err error)
|
2014-01-21 19:54:49 -07:00
|
|
|
//sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
|
|
|
|
//sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
|
|
|
|
//sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
|
|
|
|
//sys Shutdown(s int, how int) (err error)
|
2010-03-26 14:23:54 -06:00
|
|
|
|
2014-01-21 19:54:49 -07:00
|
|
|
func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
if sa.Port < 0 || sa.Port > 0xFFFF {
|
2014-01-21 19:54:49 -07:00
|
|
|
return nil, 0, EINVAL
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
sa.raw.Len = SizeofSockaddrInet4
|
|
|
|
sa.raw.Family = AF_INET
|
|
|
|
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
|
|
|
|
p[0] = byte(sa.Port >> 8)
|
|
|
|
p[1] = byte(sa.Port)
|
|
|
|
for i := 0; i < len(sa.Addr); i++ {
|
|
|
|
sa.raw.Addr[i] = sa.Addr[i]
|
|
|
|
}
|
2014-01-21 19:54:49 -07:00
|
|
|
return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
2014-01-21 19:54:49 -07:00
|
|
|
func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
if sa.Port < 0 || sa.Port > 0xFFFF {
|
2014-01-21 19:54:49 -07:00
|
|
|
return nil, 0, EINVAL
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
sa.raw.Len = SizeofSockaddrInet6
|
|
|
|
sa.raw.Family = AF_INET6
|
|
|
|
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
|
|
|
|
p[0] = byte(sa.Port >> 8)
|
|
|
|
p[1] = byte(sa.Port)
|
2011-05-22 11:09:07 -06:00
|
|
|
sa.raw.Scope_id = sa.ZoneId
|
2010-03-26 14:23:54 -06:00
|
|
|
for i := 0; i < len(sa.Addr); i++ {
|
|
|
|
sa.raw.Addr[i] = sa.Addr[i]
|
|
|
|
}
|
2014-01-21 19:54:49 -07:00
|
|
|
return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
2014-01-21 19:54:49 -07:00
|
|
|
func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
name := sa.Name
|
|
|
|
n := len(name)
|
|
|
|
if n >= len(sa.raw.Path) || n == 0 {
|
2014-01-21 19:54:49 -07:00
|
|
|
return nil, 0, EINVAL
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
sa.raw.Len = byte(3 + n) // 2 for Family, Len; 1 for NUL
|
|
|
|
sa.raw.Family = AF_UNIX
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
sa.raw.Path[i] = int8(name[i])
|
|
|
|
}
|
2014-01-21 19:54:49 -07:00
|
|
|
return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
2014-01-21 19:54:49 -07:00
|
|
|
func (sa *SockaddrDatalink) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
2011-02-11 12:34:00 -07:00
|
|
|
if sa.Index == 0 {
|
2014-01-21 19:54:49 -07:00
|
|
|
return nil, 0, EINVAL
|
2011-02-11 12:34:00 -07:00
|
|
|
}
|
|
|
|
sa.raw.Len = sa.Len
|
|
|
|
sa.raw.Family = AF_LINK
|
|
|
|
sa.raw.Index = sa.Index
|
|
|
|
sa.raw.Type = sa.Type
|
|
|
|
sa.raw.Nlen = sa.Nlen
|
|
|
|
sa.raw.Alen = sa.Alen
|
|
|
|
sa.raw.Slen = sa.Slen
|
|
|
|
for i := 0; i < len(sa.raw.Data); i++ {
|
|
|
|
sa.raw.Data[i] = sa.Data[i]
|
|
|
|
}
|
2014-01-21 19:54:49 -07:00
|
|
|
return unsafe.Pointer(&sa.raw), SizeofSockaddrDatalink, nil
|
2011-02-11 12:34:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
switch rsa.Addr.Family {
|
2011-02-11 12:34:00 -07:00
|
|
|
case AF_LINK:
|
|
|
|
pp := (*RawSockaddrDatalink)(unsafe.Pointer(rsa))
|
|
|
|
sa := new(SockaddrDatalink)
|
|
|
|
sa.Len = pp.Len
|
|
|
|
sa.Family = pp.Family
|
|
|
|
sa.Index = pp.Index
|
|
|
|
sa.Type = pp.Type
|
|
|
|
sa.Nlen = pp.Nlen
|
|
|
|
sa.Alen = pp.Alen
|
|
|
|
sa.Slen = pp.Slen
|
|
|
|
for i := 0; i < len(sa.Data); i++ {
|
|
|
|
sa.Data[i] = pp.Data[i]
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return sa, nil
|
2011-02-11 12:34:00 -07:00
|
|
|
|
2010-03-26 14:23:54 -06:00
|
|
|
case AF_UNIX:
|
|
|
|
pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
|
2014-01-03 06:29:20 -07:00
|
|
|
if pp.Len < 2 || pp.Len > SizeofSockaddrUnix {
|
2010-03-26 14:23:54 -06:00
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
sa := new(SockaddrUnix)
|
2014-01-03 06:29:20 -07:00
|
|
|
|
|
|
|
// Some BSDs include the trailing NUL in the length, whereas
|
|
|
|
// others do not. Work around this by subtracting the leading
|
|
|
|
// family and len. The path is then scanned to see if a NUL
|
|
|
|
// terminator still exists within the length.
|
|
|
|
n := int(pp.Len) - 2 // subtract leading Family, Len
|
2010-03-26 14:23:54 -06:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if pp.Path[i] == 0 {
|
2014-01-03 06:29:20 -07:00
|
|
|
// found early NUL; assume Len included the NUL
|
|
|
|
// or was overestimating.
|
2010-03-26 14:23:54 -06:00
|
|
|
n = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2010-07-01 18:49:28 -06:00
|
|
|
bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
|
|
|
|
sa.Name = string(bytes)
|
2011-11-13 20:44:52 -07:00
|
|
|
return sa, nil
|
2010-03-26 14:23:54 -06:00
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
|
|
|
|
sa := new(SockaddrInet4)
|
|
|
|
p := (*[2]byte)(unsafe.Pointer(&pp.Port))
|
|
|
|
sa.Port = int(p[0])<<8 + int(p[1])
|
|
|
|
for i := 0; i < len(sa.Addr); i++ {
|
|
|
|
sa.Addr[i] = pp.Addr[i]
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return sa, nil
|
2010-03-26 14:23:54 -06:00
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
|
|
|
|
sa := new(SockaddrInet6)
|
|
|
|
p := (*[2]byte)(unsafe.Pointer(&pp.Port))
|
|
|
|
sa.Port = int(p[0])<<8 + int(p[1])
|
2011-05-22 11:09:07 -06:00
|
|
|
sa.ZoneId = pp.Scope_id
|
2010-03-26 14:23:54 -06:00
|
|
|
for i := 0; i < len(sa.Addr); i++ {
|
|
|
|
sa.Addr[i] = pp.Addr[i]
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return sa, nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
return nil, EAFNOSUPPORT
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Accept(fd int) (nfd int, sa Sockaddr, err error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
var rsa RawSockaddrAny
|
|
|
|
var len _Socklen = SizeofSockaddrAny
|
2011-11-13 20:44:52 -07:00
|
|
|
nfd, err = accept(fd, &rsa, &len)
|
|
|
|
if err != nil {
|
2010-03-26 14:23:54 -06:00
|
|
|
return
|
|
|
|
}
|
2012-07-29 17:50:23 -06:00
|
|
|
if runtime.GOOS == "darwin" && len == 0 {
|
2012-07-29 17:02:24 -06:00
|
|
|
// Accepted socket has no address.
|
|
|
|
// This is likely due to a bug in xnu kernels,
|
|
|
|
// where instead of ECONNABORTED error socket
|
|
|
|
// is accepted, but has no address.
|
|
|
|
Close(nfd)
|
|
|
|
return 0, nil, ECONNABORTED
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
sa, err = anyToSockaddr(&rsa)
|
|
|
|
if err != nil {
|
2010-03-26 14:23:54 -06:00
|
|
|
Close(nfd)
|
|
|
|
nfd = 0
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Getsockname(fd int) (sa Sockaddr, err error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
var rsa RawSockaddrAny
|
|
|
|
var len _Socklen = SizeofSockaddrAny
|
2011-11-13 20:44:52 -07:00
|
|
|
if err = getsockname(fd, &rsa, &len); err != nil {
|
2010-03-26 14:23:54 -06:00
|
|
|
return
|
|
|
|
}
|
2014-01-12 17:24:56 -07:00
|
|
|
// TODO(jsing): DragonFly has a "bug" (see issue 3349), which should be
|
|
|
|
// reported upstream.
|
|
|
|
if runtime.GOOS == "dragonfly" && rsa.Addr.Family == AF_UNSPEC && rsa.Addr.Len == 0 {
|
2013-03-05 03:40:37 -07:00
|
|
|
rsa.Addr.Family = AF_UNIX
|
|
|
|
rsa.Addr.Len = SizeofSockaddrUnix
|
|
|
|
}
|
2010-03-26 14:23:54 -06:00
|
|
|
return anyToSockaddr(&rsa)
|
|
|
|
}
|
|
|
|
|
2012-09-23 22:06:22 -06:00
|
|
|
//sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
|
2010-09-24 12:59:34 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func GetsockoptByte(fd, level, opt int) (value byte, err error) {
|
2011-09-23 08:39:19 -06:00
|
|
|
var n byte
|
|
|
|
vallen := _Socklen(1)
|
2014-01-21 19:54:49 -07:00
|
|
|
err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
|
2011-11-13 20:44:52 -07:00
|
|
|
return n, err
|
2011-09-23 08:39:19 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
|
2011-10-01 03:45:06 -06:00
|
|
|
vallen := _Socklen(4)
|
2014-01-21 19:54:49 -07:00
|
|
|
err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
|
2011-11-13 20:44:52 -07:00
|
|
|
return value, err
|
2011-10-01 03:45:06 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
|
2011-10-01 03:45:06 -06:00
|
|
|
var value IPMreq
|
|
|
|
vallen := _Socklen(SizeofIPMreq)
|
2014-01-21 19:54:49 -07:00
|
|
|
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
|
2011-11-13 20:44:52 -07:00
|
|
|
return &value, err
|
2011-10-01 03:45:06 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
|
2011-10-01 03:45:06 -06:00
|
|
|
var value IPv6Mreq
|
|
|
|
vallen := _Socklen(SizeofIPv6Mreq)
|
2014-01-21 19:54:49 -07:00
|
|
|
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
|
2011-11-13 20:44:52 -07:00
|
|
|
return &value, err
|
2011-10-01 03:45:06 -06:00
|
|
|
}
|
|
|
|
|
2013-05-23 01:22:05 -06:00
|
|
|
func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
|
|
|
|
var value IPv6MTUInfo
|
|
|
|
vallen := _Socklen(SizeofIPv6MTUInfo)
|
2014-01-21 19:54:49 -07:00
|
|
|
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
|
2013-05-23 01:22:05 -06:00
|
|
|
return &value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
|
|
|
|
var value ICMPv6Filter
|
|
|
|
vallen := _Socklen(SizeofICMPv6Filter)
|
2014-01-21 19:54:49 -07:00
|
|
|
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
|
2013-05-23 01:22:05 -06:00
|
|
|
return &value, err
|
|
|
|
}
|
|
|
|
|
2013-06-24 18:14:40 -06:00
|
|
|
//sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
|
2014-01-21 19:54:49 -07:00
|
|
|
//sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
|
2011-06-20 16:40:20 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
|
2011-06-20 16:40:20 -06:00
|
|
|
var msg Msghdr
|
|
|
|
var rsa RawSockaddrAny
|
|
|
|
msg.Name = (*byte)(unsafe.Pointer(&rsa))
|
|
|
|
msg.Namelen = uint32(SizeofSockaddrAny)
|
|
|
|
var iov Iovec
|
|
|
|
if len(p) > 0 {
|
|
|
|
iov.Base = (*byte)(unsafe.Pointer(&p[0]))
|
|
|
|
iov.SetLen(len(p))
|
|
|
|
}
|
|
|
|
var dummy byte
|
|
|
|
if len(oob) > 0 {
|
|
|
|
// receive at least one normal byte
|
|
|
|
if len(p) == 0 {
|
|
|
|
iov.Base = &dummy
|
|
|
|
iov.SetLen(1)
|
|
|
|
}
|
|
|
|
msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
|
|
|
|
msg.SetControllen(len(oob))
|
|
|
|
}
|
|
|
|
msg.Iov = &iov
|
|
|
|
msg.Iovlen = 1
|
2011-11-13 20:44:52 -07:00
|
|
|
if n, err = recvmsg(fd, &msg, flags); err != nil {
|
2011-06-20 16:40:20 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
oobn = int(msg.Controllen)
|
|
|
|
recvflags = int(msg.Flags)
|
|
|
|
// source address is only specified if the socket is unconnected
|
|
|
|
if rsa.Addr.Family != AF_UNSPEC {
|
2011-11-13 20:44:52 -07:00
|
|
|
from, err = anyToSockaddr(&rsa)
|
2011-06-20 16:40:20 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:28:40 -06:00
|
|
|
//sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
|
2011-06-20 16:40:20 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
|
2014-03-28 18:28:40 -06:00
|
|
|
_, err = SendmsgN(fd, p, oob, to, flags)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
|
2014-01-21 19:54:49 -07:00
|
|
|
var ptr unsafe.Pointer
|
2011-06-20 16:40:20 -06:00
|
|
|
var salen _Socklen
|
|
|
|
if to != nil {
|
|
|
|
ptr, salen, err = to.sockaddr()
|
2011-11-13 20:44:52 -07:00
|
|
|
if err != nil {
|
2014-03-28 18:28:40 -06:00
|
|
|
return 0, err
|
2011-06-20 16:40:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var msg Msghdr
|
|
|
|
msg.Name = (*byte)(unsafe.Pointer(ptr))
|
|
|
|
msg.Namelen = uint32(salen)
|
|
|
|
var iov Iovec
|
|
|
|
if len(p) > 0 {
|
|
|
|
iov.Base = (*byte)(unsafe.Pointer(&p[0]))
|
|
|
|
iov.SetLen(len(p))
|
|
|
|
}
|
|
|
|
var dummy byte
|
|
|
|
if len(oob) > 0 {
|
|
|
|
// send at least one normal byte
|
|
|
|
if len(p) == 0 {
|
|
|
|
iov.Base = &dummy
|
|
|
|
iov.SetLen(1)
|
|
|
|
}
|
|
|
|
msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
|
|
|
|
msg.SetControllen(len(oob))
|
|
|
|
}
|
|
|
|
msg.Iov = &iov
|
|
|
|
msg.Iovlen = 1
|
2014-03-28 18:28:40 -06:00
|
|
|
if n, err = sendmsg(fd, &msg, flags); err != nil {
|
|
|
|
return 0, err
|
2011-06-20 16:40:20 -06:00
|
|
|
}
|
2014-03-28 18:28:40 -06:00
|
|
|
if len(oob) > 0 && len(p) == 0 {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
return n, nil
|
2011-06-20 16:40:20 -06:00
|
|
|
}
|
|
|
|
|
2014-01-22 11:35:41 -07:00
|
|
|
//sys kevent(kq int, change unsafe.Pointer, nchange int, event unsafe.Pointer, nevent int, timeout *Timespec) (n int, err error)
|
2010-03-26 14:23:54 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Kevent(kq int, changes, events []Kevent_t, timeout *Timespec) (n int, err error) {
|
2014-01-22 11:35:41 -07:00
|
|
|
var change, event unsafe.Pointer
|
2010-03-26 14:23:54 -06:00
|
|
|
if len(changes) > 0 {
|
2014-01-22 11:35:41 -07:00
|
|
|
change = unsafe.Pointer(&changes[0])
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
if len(events) > 0 {
|
2014-01-22 11:35:41 -07:00
|
|
|
event = unsafe.Pointer(&events[0])
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
return kevent(kq, change, len(changes), event, len(events), timeout)
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
|
2010-03-26 14:23:54 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Sysctl(name string) (value string, err error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
// Translate name to mib number.
|
2011-11-13 20:44:52 -07:00
|
|
|
mib, err := nametomib(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find size.
|
|
|
|
n := uintptr(0)
|
2011-11-13 20:44:52 -07:00
|
|
|
if err = sysctl(mib, nil, &n, nil, 0); err != nil {
|
|
|
|
return "", err
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
if n == 0 {
|
2012-04-26 09:45:15 -06:00
|
|
|
return "", nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read into buffer of that size.
|
|
|
|
buf := make([]byte, n)
|
2011-11-13 20:44:52 -07:00
|
|
|
if err = sysctl(mib, &buf[0], &n, nil, 0); err != nil {
|
|
|
|
return "", err
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Throw away terminating NUL.
|
|
|
|
if n > 0 && buf[n-1] == '\x00' {
|
|
|
|
n--
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return string(buf[0:n]), nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func SysctlUint32(name string) (value uint32, err error) {
|
2010-03-26 14:23:54 -06:00
|
|
|
// Translate name to mib number.
|
2011-11-13 20:44:52 -07:00
|
|
|
mib, err := nametomib(name)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read into buffer of that size.
|
|
|
|
n := uintptr(4)
|
|
|
|
buf := make([]byte, 4)
|
2011-11-13 20:44:52 -07:00
|
|
|
if err = sysctl(mib, &buf[0], &n, nil, 0); err != nil {
|
|
|
|
return 0, err
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
if n != 4 {
|
|
|
|
return 0, EIO
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return *(*uint32)(unsafe.Pointer(&buf[0])), nil
|
2010-03-26 14:23:54 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys utimes(path string, timeval *[2]Timeval) (err error)
|
|
|
|
func Utimes(path string, tv []Timeval) (err error) {
|
2010-05-19 18:47:57 -06:00
|
|
|
if len(tv) != 2 {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
|
|
|
|
}
|
|
|
|
|
2012-12-13 14:02:39 -07:00
|
|
|
func UtimesNano(path string, ts []Timespec) error {
|
|
|
|
// TODO: The BSDs can do utimensat with SYS_UTIMENSAT but it
|
|
|
|
// isn't supported by darwin so this uses utimes instead
|
|
|
|
if len(ts) != 2 {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
// Not as efficient as it could be because Timespec and
|
|
|
|
// Timeval have different types in the different OSes
|
|
|
|
tv := [2]Timeval{
|
|
|
|
NsecToTimeval(TimespecToNsec(ts[0])),
|
|
|
|
NsecToTimeval(TimespecToNsec(ts[1])),
|
|
|
|
}
|
|
|
|
return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys futimes(fd int, timeval *[2]Timeval) (err error)
|
|
|
|
func Futimes(fd int, tv []Timeval) (err error) {
|
2010-05-19 18:47:57 -06:00
|
|
|
if len(tv) != 2 {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
return futimes(fd, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys fcntl(fd int, cmd int, arg int) (val int, err error)
|
2010-05-19 18:47:57 -06:00
|
|
|
|
2010-03-26 14:23:54 -06:00
|
|
|
// TODO: wrap
|
2011-11-13 20:44:52 -07:00
|
|
|
// Acct(name nil-string) (err error)
|
|
|
|
// Gethostuuid(uuid *byte, timeout *Timespec) (err error)
|
|
|
|
// Madvise(addr *byte, len int, behav int) (err error)
|
|
|
|
// Mprotect(addr *byte, len int, prot int) (err error)
|
|
|
|
// Msync(addr *byte, len int, flags int) (err error)
|
|
|
|
// Ptrace(req int, pid int, addr uintptr, data int) (ret uintptr, err error)
|
2011-04-06 15:52:02 -06:00
|
|
|
|
|
|
|
var mapper = &mmapper{
|
|
|
|
active: make(map[*byte][]byte),
|
|
|
|
mmap: mmap,
|
|
|
|
munmap: munmap,
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
|
2011-04-06 15:52:02 -06:00
|
|
|
return mapper.Mmap(fd, offset, length, prot, flags)
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Munmap(b []byte) (err error) {
|
2011-04-06 15:52:02 -06:00
|
|
|
return mapper.Munmap(b)
|
|
|
|
}
|