github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/unix/syscall_bsd.go (about) 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. 4 5 // +build darwin dragonfly freebsd netbsd openbsd 6 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. 12 13 package unix 14 15 import ( 16 "runtime" 17 "syscall" 18 "unsafe" 19 ) 20 21 /* 22 * Wrapped 23 */ 24 25 //sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error) 26 //sysnb setgroups(ngid int, gid *_Gid_t) (err error) 27 28 func Getgroups() (gids []int, err error) { 29 n, err := getgroups(0, nil) 30 if err != nil { 31 return nil, err 32 } 33 if n == 0 { 34 return nil, nil 35 } 36 37 // Sanity check group count. Max is 16 on BSD. 38 if n < 0 || n > 1000 { 39 return nil, EINVAL 40 } 41 42 a := make([]_Gid_t, n) 43 n, err = getgroups(n, &a[0]) 44 if err != nil { 45 return nil, err 46 } 47 gids = make([]int, n) 48 for i, v := range a[0:n] { 49 gids[i] = int(v) 50 } 51 return 52 } 53 54 func Setgroups(gids []int) (err error) { 55 if len(gids) == 0 { 56 return setgroups(0, nil) 57 } 58 59 a := make([]_Gid_t, len(gids)) 60 for i, v := range gids { 61 a[i] = _Gid_t(v) 62 } 63 return setgroups(len(a), &a[0]) 64 } 65 66 // Wait status is 7 bits at bottom, either 0 (exited), 67 // 0x7F (stopped), or a signal number that caused an exit. 68 // The 0x80 bit is whether there was a core dump. 69 // An extra number (exit code, signal causing a stop) 70 // is in the high bits. 71 72 type WaitStatus uint32 73 74 const ( 75 mask = 0x7F 76 core = 0x80 77 shift = 8 78 79 exited = 0 80 killed = 9 81 stopped = 0x7F 82 ) 83 84 func (w WaitStatus) Exited() bool { return w&mask == exited } 85 86 func (w WaitStatus) ExitStatus() int { 87 if w&mask != exited { 88 return -1 89 } 90 return int(w >> shift) 91 } 92 93 func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 } 94 95 func (w WaitStatus) Signal() syscall.Signal { 96 sig := syscall.Signal(w & mask) 97 if sig == stopped || sig == 0 { 98 return -1 99 } 100 return sig 101 } 102 103 func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 } 104 105 func (w WaitStatus) Stopped() bool { return w&mask == stopped && syscall.Signal(w>>shift) != SIGSTOP } 106 107 func (w WaitStatus) Killed() bool { return w&mask == killed && syscall.Signal(w>>shift) != SIGKILL } 108 109 func (w WaitStatus) Continued() bool { return w&mask == stopped && syscall.Signal(w>>shift) == SIGSTOP } 110 111 func (w WaitStatus) StopSignal() syscall.Signal { 112 if !w.Stopped() { 113 return -1 114 } 115 return syscall.Signal(w>>shift) & 0xFF 116 } 117 118 func (w WaitStatus) TrapCause() int { return -1 } 119 120 //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error) 121 122 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) { 123 var status _C_int 124 wpid, err = wait4(pid, &status, options, rusage) 125 if wstatus != nil { 126 *wstatus = WaitStatus(status) 127 } 128 return 129 } 130 131 //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) 132 //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) 133 //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) 134 //sysnb socket(domain int, typ int, proto int) (fd int, err error) 135 //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) 136 //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) 137 //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) 138 //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) 139 //sys Shutdown(s int, how int) (err error) 140 141 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) { 142 if sa.Port < 0 || sa.Port > 0xFFFF { 143 return nil, 0, EINVAL 144 } 145 sa.raw.Len = SizeofSockaddrInet4 146 sa.raw.Family = AF_INET 147 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) 148 p[0] = byte(sa.Port >> 8) 149 p[1] = byte(sa.Port) 150 for i := 0; i < len(sa.Addr); i++ { 151 sa.raw.Addr[i] = sa.Addr[i] 152 } 153 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil 154 } 155 156 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) { 157 if sa.Port < 0 || sa.Port > 0xFFFF { 158 return nil, 0, EINVAL 159 } 160 sa.raw.Len = SizeofSockaddrInet6 161 sa.raw.Family = AF_INET6 162 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) 163 p[0] = byte(sa.Port >> 8) 164 p[1] = byte(sa.Port) 165 sa.raw.Scope_id = sa.ZoneId 166 for i := 0; i < len(sa.Addr); i++ { 167 sa.raw.Addr[i] = sa.Addr[i] 168 } 169 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil 170 } 171 172 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { 173 name := sa.Name 174 n := len(name) 175 if n >= len(sa.raw.Path) || n == 0 { 176 return nil, 0, EINVAL 177 } 178 sa.raw.Len = byte(3 + n) // 2 for Family, Len; 1 for NUL 179 sa.raw.Family = AF_UNIX 180 for i := 0; i < n; i++ { 181 sa.raw.Path[i] = int8(name[i]) 182 } 183 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil 184 } 185 186 func (sa *SockaddrDatalink) sockaddr() (unsafe.Pointer, _Socklen, error) { 187 if sa.Index == 0 { 188 return nil, 0, EINVAL 189 } 190 sa.raw.Len = sa.Len 191 sa.raw.Family = AF_LINK 192 sa.raw.Index = sa.Index 193 sa.raw.Type = sa.Type 194 sa.raw.Nlen = sa.Nlen 195 sa.raw.Alen = sa.Alen 196 sa.raw.Slen = sa.Slen 197 for i := 0; i < len(sa.raw.Data); i++ { 198 sa.raw.Data[i] = sa.Data[i] 199 } 200 return unsafe.Pointer(&sa.raw), SizeofSockaddrDatalink, nil 201 } 202 203 func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { 204 switch rsa.Addr.Family { 205 case AF_LINK: 206 pp := (*RawSockaddrDatalink)(unsafe.Pointer(rsa)) 207 sa := new(SockaddrDatalink) 208 sa.Len = pp.Len 209 sa.Family = pp.Family 210 sa.Index = pp.Index 211 sa.Type = pp.Type 212 sa.Nlen = pp.Nlen 213 sa.Alen = pp.Alen 214 sa.Slen = pp.Slen 215 for i := 0; i < len(sa.Data); i++ { 216 sa.Data[i] = pp.Data[i] 217 } 218 return sa, nil 219 220 case AF_UNIX: 221 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) 222 if pp.Len < 2 || pp.Len > SizeofSockaddrUnix { 223 return nil, EINVAL 224 } 225 sa := new(SockaddrUnix) 226 227 // Some BSDs include the trailing NUL in the length, whereas 228 // others do not. Work around this by subtracting the leading 229 // family and len. The path is then scanned to see if a NUL 230 // terminator still exists within the length. 231 n := int(pp.Len) - 2 // subtract leading Family, Len 232 for i := 0; i < n; i++ { 233 if pp.Path[i] == 0 { 234 // found early NUL; assume Len included the NUL 235 // or was overestimating. 236 n = i 237 break 238 } 239 } 240 bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n] 241 sa.Name = string(bytes) 242 return sa, nil 243 244 case AF_INET: 245 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa)) 246 sa := new(SockaddrInet4) 247 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) 248 sa.Port = int(p[0])<<8 + int(p[1]) 249 for i := 0; i < len(sa.Addr); i++ { 250 sa.Addr[i] = pp.Addr[i] 251 } 252 return sa, nil 253 254 case AF_INET6: 255 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa)) 256 sa := new(SockaddrInet6) 257 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) 258 sa.Port = int(p[0])<<8 + int(p[1]) 259 sa.ZoneId = pp.Scope_id 260 for i := 0; i < len(sa.Addr); i++ { 261 sa.Addr[i] = pp.Addr[i] 262 } 263 return sa, nil 264 } 265 return nil, EAFNOSUPPORT 266 } 267 268 func Accept(fd int) (nfd int, sa Sockaddr, err error) { 269 var rsa RawSockaddrAny 270 var len _Socklen = SizeofSockaddrAny 271 nfd, err = accept(fd, &rsa, &len) 272 if err != nil { 273 return 274 } 275 if runtime.GOOS == "darwin" && len == 0 { 276 // Accepted socket has no address. 277 // This is likely due to a bug in xnu kernels, 278 // where instead of ECONNABORTED error socket 279 // is accepted, but has no address. 280 Close(nfd) 281 return 0, nil, ECONNABORTED 282 } 283 sa, err = anyToSockaddr(fd, &rsa) 284 if err != nil { 285 Close(nfd) 286 nfd = 0 287 } 288 return 289 } 290 291 func Getsockname(fd int) (sa Sockaddr, err error) { 292 var rsa RawSockaddrAny 293 var len _Socklen = SizeofSockaddrAny 294 if err = getsockname(fd, &rsa, &len); err != nil { 295 return 296 } 297 // TODO(jsing): DragonFly has a "bug" (see issue 3349), which should be 298 // reported upstream. 299 if runtime.GOOS == "dragonfly" && rsa.Addr.Family == AF_UNSPEC && rsa.Addr.Len == 0 { 300 rsa.Addr.Family = AF_UNIX 301 rsa.Addr.Len = SizeofSockaddrUnix 302 } 303 return anyToSockaddr(fd, &rsa) 304 } 305 306 //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) 307 308 // GetsockoptString returns the string value of the socket option opt for the 309 // socket associated with fd at the given socket level. 310 func GetsockoptString(fd, level, opt int) (string, error) { 311 buf := make([]byte, 256) 312 vallen := _Socklen(len(buf)) 313 err := getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen) 314 if err != nil { 315 return "", err 316 } 317 return string(buf[:vallen-1]), nil 318 } 319 320 //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) 321 //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) 322 //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) 323 324 func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) { 325 var msg Msghdr 326 var rsa RawSockaddrAny 327 msg.Name = (*byte)(unsafe.Pointer(&rsa)) 328 msg.Namelen = uint32(SizeofSockaddrAny) 329 var iov Iovec 330 if len(p) > 0 { 331 iov.Base = (*byte)(unsafe.Pointer(&p[0])) 332 iov.SetLen(len(p)) 333 } 334 var dummy byte 335 if len(oob) > 0 { 336 // receive at least one normal byte 337 if len(p) == 0 { 338 iov.Base = &dummy 339 iov.SetLen(1) 340 } 341 msg.Control = (*byte)(unsafe.Pointer(&oob[0])) 342 msg.SetControllen(len(oob)) 343 } 344 msg.Iov = &iov 345 msg.Iovlen = 1 346 if n, err = recvmsg(fd, &msg, flags); err != nil { 347 return 348 } 349 oobn = int(msg.Controllen) 350 recvflags = int(msg.Flags) 351 // source address is only specified if the socket is unconnected 352 if rsa.Addr.Family != AF_UNSPEC { 353 from, err = anyToSockaddr(fd, &rsa) 354 } 355 return 356 } 357 358 //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) 359 360 func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) { 361 _, err = SendmsgN(fd, p, oob, to, flags) 362 return 363 } 364 365 func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) { 366 var ptr unsafe.Pointer 367 var salen _Socklen 368 if to != nil { 369 ptr, salen, err = to.sockaddr() 370 if err != nil { 371 return 0, err 372 } 373 } 374 var msg Msghdr 375 msg.Name = (*byte)(unsafe.Pointer(ptr)) 376 msg.Namelen = uint32(salen) 377 var iov Iovec 378 if len(p) > 0 { 379 iov.Base = (*byte)(unsafe.Pointer(&p[0])) 380 iov.SetLen(len(p)) 381 } 382 var dummy byte 383 if len(oob) > 0 { 384 // send at least one normal byte 385 if len(p) == 0 { 386 iov.Base = &dummy 387 iov.SetLen(1) 388 } 389 msg.Control = (*byte)(unsafe.Pointer(&oob[0])) 390 msg.SetControllen(len(oob)) 391 } 392 msg.Iov = &iov 393 msg.Iovlen = 1 394 if n, err = sendmsg(fd, &msg, flags); err != nil { 395 return 0, err 396 } 397 if len(oob) > 0 && len(p) == 0 { 398 n = 0 399 } 400 return n, nil 401 } 402 403 //sys kevent(kq int, change unsafe.Pointer, nchange int, event unsafe.Pointer, nevent int, timeout *Timespec) (n int, err error) 404 405 func Kevent(kq int, changes, events []Kevent_t, timeout *Timespec) (n int, err error) { 406 var change, event unsafe.Pointer 407 if len(changes) > 0 { 408 change = unsafe.Pointer(&changes[0]) 409 } 410 if len(events) > 0 { 411 event = unsafe.Pointer(&events[0]) 412 } 413 return kevent(kq, change, len(changes), event, len(events), timeout) 414 } 415 416 //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL 417 418 // sysctlmib translates name to mib number and appends any additional args. 419 func sysctlmib(name string, args ...int) ([]_C_int, error) { 420 // Translate name to mib number. 421 mib, err := nametomib(name) 422 if err != nil { 423 return nil, err 424 } 425 426 for _, a := range args { 427 mib = append(mib, _C_int(a)) 428 } 429 430 return mib, nil 431 } 432 433 func Sysctl(name string) (string, error) { 434 return SysctlArgs(name) 435 } 436 437 func SysctlArgs(name string, args ...int) (string, error) { 438 buf, err := SysctlRaw(name, args...) 439 if err != nil { 440 return "", err 441 } 442 n := len(buf) 443 444 // Throw away terminating NUL. 445 if n > 0 && buf[n-1] == '\x00' { 446 n-- 447 } 448 return string(buf[0:n]), nil 449 } 450 451 func SysctlUint32(name string) (uint32, error) { 452 return SysctlUint32Args(name) 453 } 454 455 func SysctlUint32Args(name string, args ...int) (uint32, error) { 456 mib, err := sysctlmib(name, args...) 457 if err != nil { 458 return 0, err 459 } 460 461 n := uintptr(4) 462 buf := make([]byte, 4) 463 if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil { 464 return 0, err 465 } 466 if n != 4 { 467 return 0, EIO 468 } 469 return *(*uint32)(unsafe.Pointer(&buf[0])), nil 470 } 471 472 func SysctlUint64(name string, args ...int) (uint64, error) { 473 mib, err := sysctlmib(name, args...) 474 if err != nil { 475 return 0, err 476 } 477 478 n := uintptr(8) 479 buf := make([]byte, 8) 480 if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil { 481 return 0, err 482 } 483 if n != 8 { 484 return 0, EIO 485 } 486 return *(*uint64)(unsafe.Pointer(&buf[0])), nil 487 } 488 489 func SysctlRaw(name string, args ...int) ([]byte, error) { 490 mib, err := sysctlmib(name, args...) 491 if err != nil { 492 return nil, err 493 } 494 495 // Find size. 496 n := uintptr(0) 497 if err := sysctl(mib, nil, &n, nil, 0); err != nil { 498 return nil, err 499 } 500 if n == 0 { 501 return nil, nil 502 } 503 504 // Read into buffer of that size. 505 buf := make([]byte, n) 506 if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil { 507 return nil, err 508 } 509 510 // The actual call may return less than the original reported required 511 // size so ensure we deal with that. 512 return buf[:n], nil 513 } 514 515 //sys utimes(path string, timeval *[2]Timeval) (err error) 516 517 func Utimes(path string, tv []Timeval) error { 518 if tv == nil { 519 return utimes(path, nil) 520 } 521 if len(tv) != 2 { 522 return EINVAL 523 } 524 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) 525 } 526 527 func UtimesNano(path string, ts []Timespec) error { 528 if ts == nil { 529 err := utimensat(AT_FDCWD, path, nil, 0) 530 if err != ENOSYS { 531 return err 532 } 533 return utimes(path, nil) 534 } 535 if len(ts) != 2 { 536 return EINVAL 537 } 538 // Darwin setattrlist can set nanosecond timestamps 539 err := setattrlistTimes(path, ts, 0) 540 if err != ENOSYS { 541 return err 542 } 543 err = utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0) 544 if err != ENOSYS { 545 return err 546 } 547 // Not as efficient as it could be because Timespec and 548 // Timeval have different types in the different OSes 549 tv := [2]Timeval{ 550 NsecToTimeval(TimespecToNsec(ts[0])), 551 NsecToTimeval(TimespecToNsec(ts[1])), 552 } 553 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) 554 } 555 556 func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error { 557 if ts == nil { 558 return utimensat(dirfd, path, nil, flags) 559 } 560 if len(ts) != 2 { 561 return EINVAL 562 } 563 err := setattrlistTimes(path, ts, flags) 564 if err != ENOSYS { 565 return err 566 } 567 return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags) 568 } 569 570 //sys futimes(fd int, timeval *[2]Timeval) (err error) 571 572 func Futimes(fd int, tv []Timeval) error { 573 if tv == nil { 574 return futimes(fd, nil) 575 } 576 if len(tv) != 2 { 577 return EINVAL 578 } 579 return futimes(fd, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) 580 } 581 582 //sys fcntl(fd int, cmd int, arg int) (val int, err error) 583 584 //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error) 585 586 func Poll(fds []PollFd, timeout int) (n int, err error) { 587 if len(fds) == 0 { 588 return poll(nil, 0, timeout) 589 } 590 return poll(&fds[0], len(fds), timeout) 591 } 592 593 // TODO: wrap 594 // Acct(name nil-string) (err error) 595 // Gethostuuid(uuid *byte, timeout *Timespec) (err error) 596 // Ptrace(req int, pid int, addr uintptr, data int) (ret uintptr, err error) 597 598 var mapper = &mmapper{ 599 active: make(map[*byte][]byte), 600 mmap: mmap, 601 munmap: munmap, 602 } 603 604 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) { 605 return mapper.Mmap(fd, offset, length, prot, flags) 606 } 607 608 func Munmap(b []byte) (err error) { 609 return mapper.Munmap(b) 610 } 611 612 //sys Madvise(b []byte, behav int) (err error) 613 //sys Mlock(b []byte) (err error) 614 //sys Mlockall(flags int) (err error) 615 //sys Mprotect(b []byte, prot int) (err error) 616 //sys Msync(b []byte, flags int) (err error) 617 //sys Munlock(b []byte) (err error) 618 //sys Munlockall() (err error)