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 darwin dragonfly freebsd netbsd openbsd
7 // BSD system call wrappers shared by *BSD based systems
8 // including OS X (Darwin) and FreeBSD. Like the other
9 // syscall_*.go files it is compiled as Go code but also
10 // used as input to mksyscall which parses the //sys
11 // lines and generates system call stubs.
25 //sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error)
26 //sysnb setgroups(ngid int, gid *_Gid_t) (err error)
28 func Getgroups() (gids []int, err error) {
29 n, err := getgroups(0, nil)
37 // Sanity check group count. Max is 16 on BSD.
38 if n < 0 || n > 1000 {
42 a := make([]_Gid_t, n)
43 n, err = getgroups(n, &a[0])
48 for i, v := range a[0:n] {
54 func Setgroups(gids []int) (err error) {
56 return setgroups(0, nil)
59 a := make([]_Gid_t, len(gids))
60 for i, v := range gids {
63 return setgroups(len(a), &a[0])
66 func ReadDirent(fd int, buf []byte) (n int, err error) {
67 // Final argument is (basep *uintptr) and the syscall doesn't take nil.
68 // 64 bits should be enough. (32 bits isn't even on 386). Since the
69 // actual system call is getdirentries64, 64 is a good guess.
70 // TODO(rsc): Can we use a single global basep for all calls?
71 var base = (*uintptr)(unsafe.Pointer(new(uint64)))
72 return Getdirentries(fd, buf, base)
75 // Wait status is 7 bits at bottom, either 0 (exited),
76 // 0x7F (stopped), or a signal number that caused an exit.
77 // The 0x80 bit is whether there was a core dump.
78 // An extra number (exit code, signal causing a stop)
79 // is in the high bits.
81 type WaitStatus uint32
92 func (w WaitStatus) Exited() bool { return w&mask == exited }
94 func (w WaitStatus) ExitStatus() int {
98 return int(w >> shift)
101 func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
103 func (w WaitStatus) Signal() syscall.Signal {
104 sig := syscall.Signal(w & mask)
105 if sig == stopped || sig == 0 {
111 func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
113 func (w WaitStatus) Stopped() bool { return w&mask == stopped && syscall.Signal(w>>shift) != SIGSTOP }
115 func (w WaitStatus) Continued() bool { return w&mask == stopped && syscall.Signal(w>>shift) == SIGSTOP }
117 func (w WaitStatus) StopSignal() syscall.Signal {
121 return syscall.Signal(w>>shift) & 0xFF
124 func (w WaitStatus) TrapCause() int { return -1 }
126 //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
128 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
130 wpid, err = wait4(pid, &status, options, rusage)
132 *wstatus = WaitStatus(status)
137 //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
138 //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
139 //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
140 //sysnb socket(domain int, typ int, proto int) (fd int, err error)
141 //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
142 //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
143 //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
144 //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
145 //sys Shutdown(s int, how int) (err error)
147 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
148 if sa.Port < 0 || sa.Port > 0xFFFF {
149 return nil, 0, EINVAL
151 sa.raw.Len = SizeofSockaddrInet4
152 sa.raw.Family = AF_INET
153 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
154 p[0] = byte(sa.Port >> 8)
156 for i := 0; i < len(sa.Addr); i++ {
157 sa.raw.Addr[i] = sa.Addr[i]
159 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
162 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
163 if sa.Port < 0 || sa.Port > 0xFFFF {
164 return nil, 0, EINVAL
166 sa.raw.Len = SizeofSockaddrInet6
167 sa.raw.Family = AF_INET6
168 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
169 p[0] = byte(sa.Port >> 8)
171 sa.raw.Scope_id = sa.ZoneId
172 for i := 0; i < len(sa.Addr); i++ {
173 sa.raw.Addr[i] = sa.Addr[i]
175 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
178 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
181 if n >= len(sa.raw.Path) || n == 0 {
182 return nil, 0, EINVAL
184 sa.raw.Len = byte(3 + n) // 2 for Family, Len; 1 for NUL
185 sa.raw.Family = AF_UNIX
186 for i := 0; i < n; i++ {
187 sa.raw.Path[i] = int8(name[i])
189 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
192 func (sa *SockaddrDatalink) sockaddr() (unsafe.Pointer, _Socklen, error) {
194 return nil, 0, EINVAL
197 sa.raw.Family = AF_LINK
198 sa.raw.Index = sa.Index
199 sa.raw.Type = sa.Type
200 sa.raw.Nlen = sa.Nlen
201 sa.raw.Alen = sa.Alen
202 sa.raw.Slen = sa.Slen
203 for i := 0; i < len(sa.raw.Data); i++ {
204 sa.raw.Data[i] = sa.Data[i]
206 return unsafe.Pointer(&sa.raw), SizeofSockaddrDatalink, nil
209 func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
210 switch rsa.Addr.Family {
212 pp := (*RawSockaddrDatalink)(unsafe.Pointer(rsa))
213 sa := new(SockaddrDatalink)
215 sa.Family = pp.Family
221 for i := 0; i < len(sa.Data); i++ {
222 sa.Data[i] = pp.Data[i]
227 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
228 if pp.Len < 2 || pp.Len > SizeofSockaddrUnix {
231 sa := new(SockaddrUnix)
233 // Some BSDs include the trailing NUL in the length, whereas
234 // others do not. Work around this by subtracting the leading
235 // family and len. The path is then scanned to see if a NUL
236 // terminator still exists within the length.
237 n := int(pp.Len) - 2 // subtract leading Family, Len
238 for i := 0; i < n; i++ {
240 // found early NUL; assume Len included the NUL
241 // or was overestimating.
246 bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
247 sa.Name = string(bytes)
251 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
252 sa := new(SockaddrInet4)
253 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
254 sa.Port = int(p[0])<<8 + int(p[1])
255 for i := 0; i < len(sa.Addr); i++ {
256 sa.Addr[i] = pp.Addr[i]
261 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
262 sa := new(SockaddrInet6)
263 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
264 sa.Port = int(p[0])<<8 + int(p[1])
265 sa.ZoneId = pp.Scope_id
266 for i := 0; i < len(sa.Addr); i++ {
267 sa.Addr[i] = pp.Addr[i]
271 return nil, EAFNOSUPPORT
274 func Accept(fd int) (nfd int, sa Sockaddr, err error) {
275 var rsa RawSockaddrAny
276 var len _Socklen = SizeofSockaddrAny
277 nfd, err = accept(fd, &rsa, &len)
281 if runtime.GOOS == "darwin" && len == 0 {
282 // Accepted socket has no address.
283 // This is likely due to a bug in xnu kernels,
284 // where instead of ECONNABORTED error socket
285 // is accepted, but has no address.
287 return 0, nil, ECONNABORTED
289 sa, err = anyToSockaddr(fd, &rsa)
297 func Getsockname(fd int) (sa Sockaddr, err error) {
298 var rsa RawSockaddrAny
299 var len _Socklen = SizeofSockaddrAny
300 if err = getsockname(fd, &rsa, &len); err != nil {
303 // TODO(jsing): DragonFly has a "bug" (see issue 3349), which should be
304 // reported upstream.
305 if runtime.GOOS == "dragonfly" && rsa.Addr.Family == AF_UNSPEC && rsa.Addr.Len == 0 {
306 rsa.Addr.Family = AF_UNIX
307 rsa.Addr.Len = SizeofSockaddrUnix
309 return anyToSockaddr(fd, &rsa)
312 //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
314 // GetsockoptString returns the string value of the socket option opt for the
315 // socket associated with fd at the given socket level.
316 func GetsockoptString(fd, level, opt int) (string, error) {
317 buf := make([]byte, 256)
318 vallen := _Socklen(len(buf))
319 err := getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen)
323 return string(buf[:vallen-1]), nil
326 //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
327 //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
328 //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
330 func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
332 var rsa RawSockaddrAny
333 msg.Name = (*byte)(unsafe.Pointer(&rsa))
334 msg.Namelen = uint32(SizeofSockaddrAny)
337 iov.Base = (*byte)(unsafe.Pointer(&p[0]))
342 // receive at least one normal byte
347 msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
348 msg.SetControllen(len(oob))
352 if n, err = recvmsg(fd, &msg, flags); err != nil {
355 oobn = int(msg.Controllen)
356 recvflags = int(msg.Flags)
357 // source address is only specified if the socket is unconnected
358 if rsa.Addr.Family != AF_UNSPEC {
359 from, err = anyToSockaddr(fd, &rsa)
364 //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
366 func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
367 _, err = SendmsgN(fd, p, oob, to, flags)
371 func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
372 var ptr unsafe.Pointer
375 ptr, salen, err = to.sockaddr()
381 msg.Name = (*byte)(unsafe.Pointer(ptr))
382 msg.Namelen = uint32(salen)
385 iov.Base = (*byte)(unsafe.Pointer(&p[0]))
390 // send at least one normal byte
395 msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
396 msg.SetControllen(len(oob))
400 if n, err = sendmsg(fd, &msg, flags); err != nil {
403 if len(oob) > 0 && len(p) == 0 {
409 //sys kevent(kq int, change unsafe.Pointer, nchange int, event unsafe.Pointer, nevent int, timeout *Timespec) (n int, err error)
411 func Kevent(kq int, changes, events []Kevent_t, timeout *Timespec) (n int, err error) {
412 var change, event unsafe.Pointer
413 if len(changes) > 0 {
414 change = unsafe.Pointer(&changes[0])
417 event = unsafe.Pointer(&events[0])
419 return kevent(kq, change, len(changes), event, len(events), timeout)
422 //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
424 // sysctlmib translates name to mib number and appends any additional args.
425 func sysctlmib(name string, args ...int) ([]_C_int, error) {
426 // Translate name to mib number.
427 mib, err := nametomib(name)
432 for _, a := range args {
433 mib = append(mib, _C_int(a))
439 func Sysctl(name string) (string, error) {
440 return SysctlArgs(name)
443 func SysctlArgs(name string, args ...int) (string, error) {
444 buf, err := SysctlRaw(name, args...)
450 // Throw away terminating NUL.
451 if n > 0 && buf[n-1] == '\x00' {
454 return string(buf[0:n]), nil
457 func SysctlUint32(name string) (uint32, error) {
458 return SysctlUint32Args(name)
461 func SysctlUint32Args(name string, args ...int) (uint32, error) {
462 mib, err := sysctlmib(name, args...)
468 buf := make([]byte, 4)
469 if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
475 return *(*uint32)(unsafe.Pointer(&buf[0])), nil
478 func SysctlUint64(name string, args ...int) (uint64, error) {
479 mib, err := sysctlmib(name, args...)
485 buf := make([]byte, 8)
486 if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
492 return *(*uint64)(unsafe.Pointer(&buf[0])), nil
495 func SysctlRaw(name string, args ...int) ([]byte, error) {
496 mib, err := sysctlmib(name, args...)
503 if err := sysctl(mib, nil, &n, nil, 0); err != nil {
510 // Read into buffer of that size.
511 buf := make([]byte, n)
512 if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
516 // The actual call may return less than the original reported required
517 // size so ensure we deal with that.
521 //sys utimes(path string, timeval *[2]Timeval) (err error)
523 func Utimes(path string, tv []Timeval) error {
525 return utimes(path, nil)
530 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
533 func UtimesNano(path string, ts []Timespec) error {
535 err := utimensat(AT_FDCWD, path, nil, 0)
539 return utimes(path, nil)
544 // Darwin setattrlist can set nanosecond timestamps
545 err := setattrlistTimes(path, ts, 0)
549 err = utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
553 // Not as efficient as it could be because Timespec and
554 // Timeval have different types in the different OSes
556 NsecToTimeval(TimespecToNsec(ts[0])),
557 NsecToTimeval(TimespecToNsec(ts[1])),
559 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
562 func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
564 return utimensat(dirfd, path, nil, flags)
569 err := setattrlistTimes(path, ts, flags)
573 return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
576 //sys futimes(fd int, timeval *[2]Timeval) (err error)
578 func Futimes(fd int, tv []Timeval) error {
580 return futimes(fd, nil)
585 return futimes(fd, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
588 //sys fcntl(fd int, cmd int, arg int) (val int, err error)
590 //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
592 func Poll(fds []PollFd, timeout int) (n int, err error) {
594 return poll(nil, 0, timeout)
596 return poll(&fds[0], len(fds), timeout)
600 // Acct(name nil-string) (err error)
601 // Gethostuuid(uuid *byte, timeout *Timespec) (err error)
602 // Ptrace(req int, pid int, addr uintptr, data int) (ret uintptr, err error)
604 var mapper = &mmapper{
605 active: make(map[*byte][]byte),
610 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
611 return mapper.Mmap(fd, offset, length, prot, flags)
614 func Munmap(b []byte) (err error) {
615 return mapper.Munmap(b)
618 //sys Madvise(b []byte, behav int) (err error)
619 //sys Mlock(b []byte) (err error)
620 //sys Mlockall(flags int) (err error)
621 //sys Mprotect(b []byte, prot int) (err error)
622 //sys Msync(b []byte, flags int) (err error)
623 //sys Munlock(b []byte) (err error)
624 //sys Munlockall() (err error)