1 // Copyright 2009 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.
5 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
23 // Do the interface allocations only once for common
26 errEAGAIN error = syscall.EAGAIN
27 errEINVAL error = syscall.EINVAL
28 errENOENT error = syscall.ENOENT
32 signalNameMapOnce sync.Once
33 signalNameMap map[string]syscall.Signal
36 // errnoErr returns common boxed Errno values, to prevent
37 // allocations at runtime.
38 func errnoErr(e syscall.Errno) error {
52 // ErrnoName returns the error name for error number e.
53 func ErrnoName(e syscall.Errno) string {
54 i := sort.Search(len(errorList), func(i int) bool {
55 return errorList[i].num >= e
57 if i < len(errorList) && errorList[i].num == e {
58 return errorList[i].name
63 // SignalName returns the signal name for signal number s.
64 func SignalName(s syscall.Signal) string {
65 i := sort.Search(len(signalList), func(i int) bool {
66 return signalList[i].num >= s
68 if i < len(signalList) && signalList[i].num == s {
69 return signalList[i].name
74 // SignalNum returns the syscall.Signal for signal named s,
75 // or 0 if a signal with such name is not found.
76 // The signal name should start with "SIG".
77 func SignalNum(s string) syscall.Signal {
78 signalNameMapOnce.Do(func() {
79 signalNameMap = make(map[string]syscall.Signal)
80 for _, signal := range signalList {
81 signalNameMap[signal.name] = signal.num
84 return signalNameMap[s]
87 // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
88 func clen(n []byte) int {
89 i := bytes.IndexByte(n, 0)
96 // Mmap manager, for use by operating system-specific implementations.
100 active map[*byte][]byte // active mappings; key is last byte in mapping
101 mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
102 munmap func(addr uintptr, length uintptr) error
105 func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
110 // Map the requested memory.
111 addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
116 // Slice memory layout
121 }{addr, length, length}
123 // Use unsafe to turn sl into a []byte.
124 b := *(*[]byte)(unsafe.Pointer(&sl))
126 // Register mapping in m and return it.
134 func (m *mmapper) Munmap(data []byte) (err error) {
135 if len(data) == 0 || len(data) != cap(data) {
139 // Find the base of the mapping.
140 p := &data[cap(data)-1]
144 if b == nil || &b[0] != &data[0] {
148 // Unmap the memory and update m.
149 if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
156 func Read(fd int, p []byte) (n int, err error) {
160 raceWriteRange(unsafe.Pointer(&p[0]), n)
163 raceAcquire(unsafe.Pointer(&ioSync))
169 func Write(fd int, p []byte) (n int, err error) {
171 raceReleaseMerge(unsafe.Pointer(&ioSync))
173 n, err = write(fd, p)
174 if raceenabled && n > 0 {
175 raceReadRange(unsafe.Pointer(&p[0]), n)
180 // For testing: clients can set this flag to force
181 // creation of IPv6 sockets to return EAFNOSUPPORT.
182 var SocketDisableIPv6 bool
184 // Sockaddr represents a socket address.
185 type Sockaddr interface {
186 sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
189 // SockaddrInet4 implements the Sockaddr interface for AF_INET type sockets.
190 type SockaddrInet4 struct {
196 // SockaddrInet6 implements the Sockaddr interface for AF_INET6 type sockets.
197 type SockaddrInet6 struct {
204 // SockaddrUnix implements the Sockaddr interface for AF_UNIX type sockets.
205 type SockaddrUnix struct {
210 func Bind(fd int, sa Sockaddr) (err error) {
211 ptr, n, err := sa.sockaddr()
215 return bind(fd, ptr, n)
218 func Connect(fd int, sa Sockaddr) (err error) {
219 ptr, n, err := sa.sockaddr()
223 return connect(fd, ptr, n)
226 func Getpeername(fd int) (sa Sockaddr, err error) {
227 var rsa RawSockaddrAny
228 var len _Socklen = SizeofSockaddrAny
229 if err = getpeername(fd, &rsa, &len); err != nil {
232 return anyToSockaddr(fd, &rsa)
235 func GetsockoptByte(fd, level, opt int) (value byte, err error) {
237 vallen := _Socklen(1)
238 err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
242 func GetsockoptInt(fd, level, opt int) (value int, err error) {
244 vallen := _Socklen(4)
245 err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
249 func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
250 vallen := _Socklen(4)
251 err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
255 func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
257 vallen := _Socklen(SizeofIPMreq)
258 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
262 func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
264 vallen := _Socklen(SizeofIPv6Mreq)
265 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
269 func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
270 var value IPv6MTUInfo
271 vallen := _Socklen(SizeofIPv6MTUInfo)
272 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
276 func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
277 var value ICMPv6Filter
278 vallen := _Socklen(SizeofICMPv6Filter)
279 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
283 func GetsockoptLinger(fd, level, opt int) (*Linger, error) {
285 vallen := _Socklen(SizeofLinger)
286 err := getsockopt(fd, level, opt, unsafe.Pointer(&linger), &vallen)
290 func GetsockoptTimeval(fd, level, opt int) (*Timeval, error) {
292 vallen := _Socklen(unsafe.Sizeof(tv))
293 err := getsockopt(fd, level, opt, unsafe.Pointer(&tv), &vallen)
297 func GetsockoptUint64(fd, level, opt int) (value uint64, err error) {
299 vallen := _Socklen(8)
300 err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
304 func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
305 var rsa RawSockaddrAny
306 var len _Socklen = SizeofSockaddrAny
307 if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
310 if rsa.Addr.Family != AF_UNSPEC {
311 from, err = anyToSockaddr(fd, &rsa)
316 func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
317 ptr, n, err := to.sockaddr()
321 return sendto(fd, p, flags, ptr, n)
324 func SetsockoptByte(fd, level, opt int, value byte) (err error) {
325 return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
328 func SetsockoptInt(fd, level, opt int, value int) (err error) {
330 return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
333 func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
334 return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
337 func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
338 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
341 func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
342 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
345 func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
346 return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
349 func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
350 return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
353 func SetsockoptString(fd, level, opt int, s string) (err error) {
356 p = unsafe.Pointer(&[]byte(s)[0])
358 return setsockopt(fd, level, opt, p, uintptr(len(s)))
361 func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
362 return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
365 func SetsockoptUint64(fd, level, opt int, value uint64) (err error) {
366 return setsockopt(fd, level, opt, unsafe.Pointer(&value), 8)
369 func Socket(domain, typ, proto int) (fd int, err error) {
370 if domain == AF_INET6 && SocketDisableIPv6 {
371 return -1, EAFNOSUPPORT
373 fd, err = socket(domain, typ, proto)
377 func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
379 err = socketpair(domain, typ, proto, &fdx)
389 func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
391 func SetNonblock(fd int, nonblocking bool) (err error) {
392 flag, err := fcntl(fd, F_GETFL, 0)
401 _, err = fcntl(fd, F_SETFL, flag)
405 // Exec calls execve(2), which replaces the calling executable in the process
406 // tree. argv0 should be the full path to an executable ("/bin/ls") and the
407 // executable name should also be the first argument in argv (["ls", "-l"]).
408 // envv are the environment variables that should be passed to the new
409 // process (["USER=go", "PWD=/tmp"]).
410 func Exec(argv0 string, argv []string, envv []string) error {
411 return syscall.Exec(argv0, argv, envv)
414 // Lutimes sets the access and modification times tv on path. If path refers to
415 // a symlink, it is not dereferenced and the timestamps are set on the symlink.
416 // If tv is nil, the access and modification times are set to the current time.
417 // Otherwise tv must contain exactly 2 elements, with access time as the first
418 // element and modification time as the second element.
419 func Lutimes(path string, tv []Timeval) error {
421 return UtimesNanoAt(AT_FDCWD, path, nil, AT_SYMLINK_NOFOLLOW)
427 NsecToTimespec(TimevalToNsec(tv[0])),
428 NsecToTimespec(TimevalToNsec(tv[1])),
430 return UtimesNanoAt(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW)