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