github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/src/syscall/syscall_linux.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 // Linux system calls. 6 // This file is compiled as ordinary Go code, 7 // but it is also input to mksyscall, 8 // which parses the //sys lines and generates system call stubs. 9 // Note that sometimes we use a lowercase //sys name and 10 // wrap it in our own nicer implementation. 11 12 package syscall 13 14 import "unsafe" 15 16 /* 17 * Wrapped 18 */ 19 20 //sys open(path string, mode int, perm uint32) (fd int, err error) 21 22 func Open(path string, mode int, perm uint32) (fd int, err error) { 23 return open(path, mode|O_LARGEFILE, perm) 24 } 25 26 //sys openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) 27 28 func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { 29 return openat(dirfd, path, flags|O_LARGEFILE, mode) 30 } 31 32 //sysnb pipe(p *[2]_C_int) (err error) 33 34 func Pipe(p []int) (err error) { 35 if len(p) != 2 { 36 return EINVAL 37 } 38 var pp [2]_C_int 39 err = pipe(&pp) 40 p[0] = int(pp[0]) 41 p[1] = int(pp[1]) 42 return 43 } 44 45 //sysnb pipe2(p *[2]_C_int, flags int) (err error) 46 47 func Pipe2(p []int, flags int) (err error) { 48 if len(p) != 2 { 49 return EINVAL 50 } 51 var pp [2]_C_int 52 err = pipe2(&pp, flags) 53 p[0] = int(pp[0]) 54 p[1] = int(pp[1]) 55 return 56 } 57 58 //sys utimes(path string, times *[2]Timeval) (err error) 59 60 func Utimes(path string, tv []Timeval) (err error) { 61 if len(tv) != 2 { 62 return EINVAL 63 } 64 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) 65 } 66 67 //sys utimensat(dirfd int, path string, times *[2]Timespec) (err error) 68 69 func UtimesNano(path string, ts []Timespec) (err error) { 70 if len(ts) != 2 { 71 return EINVAL 72 } 73 err = utimensat(_AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0]))) 74 if err != ENOSYS { 75 return err 76 } 77 // If the utimensat syscall isn't available (utimensat was added to Linux 78 // in 2.6.22, Released, 8 July 2007) then fall back to utimes 79 var tv [2]Timeval 80 for i := 0; i < 2; i++ { 81 tv[i].Sec = ts[i].Sec 82 tv[i].Usec = ts[i].Nsec / 1000 83 } 84 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) 85 } 86 87 //sys futimesat(dirfd int, path *byte, times *[2]Timeval) (err error) 88 89 func Futimesat(dirfd int, path string, tv []Timeval) (err error) { 90 if len(tv) != 2 { 91 return EINVAL 92 } 93 pathp, err := BytePtrFromString(path) 94 if err != nil { 95 return err 96 } 97 err = futimesat(dirfd, pathp, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) 98 use(unsafe.Pointer(pathp)) 99 return err 100 } 101 102 func Futimes(fd int, tv []Timeval) (err error) { 103 // Believe it or not, this is the best we can do on Linux 104 // (and is what glibc does). 105 return Utimes("/proc/self/fd/"+itoa(fd), tv) 106 } 107 108 const ImplementsGetwd = true 109 110 //sys Getcwd(buf []byte) (n int, err error) 111 112 func Getwd() (wd string, err error) { 113 var buf [PathMax]byte 114 n, err := Getcwd(buf[0:]) 115 if err != nil { 116 return "", err 117 } 118 // Getcwd returns the number of bytes written to buf, including the NUL. 119 if n < 1 || n > len(buf) || buf[n-1] != 0 { 120 return "", EINVAL 121 } 122 return string(buf[0 : n-1]), nil 123 } 124 125 func Getgroups() (gids []int, err error) { 126 n, err := getgroups(0, nil) 127 if err != nil { 128 return nil, err 129 } 130 if n == 0 { 131 return nil, nil 132 } 133 134 // Sanity check group count. Max is 1<<16 on Linux. 135 if n < 0 || n > 1<<20 { 136 return nil, EINVAL 137 } 138 139 a := make([]_Gid_t, n) 140 n, err = getgroups(n, &a[0]) 141 if err != nil { 142 return nil, err 143 } 144 gids = make([]int, n) 145 for i, v := range a[0:n] { 146 gids[i] = int(v) 147 } 148 return 149 } 150 151 func Setgroups(gids []int) (err error) { 152 if len(gids) == 0 { 153 return setgroups(0, nil) 154 } 155 156 a := make([]_Gid_t, len(gids)) 157 for i, v := range gids { 158 a[i] = _Gid_t(v) 159 } 160 return setgroups(len(a), &a[0]) 161 } 162 163 type WaitStatus uint32 164 165 // Wait status is 7 bits at bottom, either 0 (exited), 166 // 0x7F (stopped), or a signal number that caused an exit. 167 // The 0x80 bit is whether there was a core dump. 168 // An extra number (exit code, signal causing a stop) 169 // is in the high bits. At least that's the idea. 170 // There are various irregularities. For example, the 171 // "continued" status is 0xFFFF, distinguishing itself 172 // from stopped via the core dump bit. 173 174 const ( 175 mask = 0x7F 176 core = 0x80 177 exited = 0x00 178 stopped = 0x7F 179 shift = 8 180 ) 181 182 func (w WaitStatus) Exited() bool { return w&mask == exited } 183 184 func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited } 185 186 func (w WaitStatus) Stopped() bool { return w&0xFF == stopped } 187 188 func (w WaitStatus) Continued() bool { return w == 0xFFFF } 189 190 func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 } 191 192 func (w WaitStatus) ExitStatus() int { 193 if !w.Exited() { 194 return -1 195 } 196 return int(w>>shift) & 0xFF 197 } 198 199 func (w WaitStatus) Signal() Signal { 200 if !w.Signaled() { 201 return -1 202 } 203 return Signal(w & mask) 204 } 205 206 func (w WaitStatus) StopSignal() Signal { 207 if !w.Stopped() { 208 return -1 209 } 210 return Signal(w>>shift) & 0xFF 211 } 212 213 func (w WaitStatus) TrapCause() int { 214 if w.StopSignal() != SIGTRAP { 215 return -1 216 } 217 return int(w>>shift) >> 8 218 } 219 220 //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error) 221 222 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) { 223 var status _C_int 224 wpid, err = wait4(pid, &status, options, rusage) 225 if wstatus != nil { 226 *wstatus = WaitStatus(status) 227 } 228 return 229 } 230 231 func Mkfifo(path string, mode uint32) (err error) { 232 return Mknod(path, mode|S_IFIFO, 0) 233 } 234 235 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) { 236 if sa.Port < 0 || sa.Port > 0xFFFF { 237 return nil, 0, EINVAL 238 } 239 sa.raw.Family = AF_INET 240 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) 241 p[0] = byte(sa.Port >> 8) 242 p[1] = byte(sa.Port) 243 for i := 0; i < len(sa.Addr); i++ { 244 sa.raw.Addr[i] = sa.Addr[i] 245 } 246 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil 247 } 248 249 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) { 250 if sa.Port < 0 || sa.Port > 0xFFFF { 251 return nil, 0, EINVAL 252 } 253 sa.raw.Family = AF_INET6 254 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) 255 p[0] = byte(sa.Port >> 8) 256 p[1] = byte(sa.Port) 257 sa.raw.Scope_id = sa.ZoneId 258 for i := 0; i < len(sa.Addr); i++ { 259 sa.raw.Addr[i] = sa.Addr[i] 260 } 261 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil 262 } 263 264 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { 265 name := sa.Name 266 n := len(name) 267 if n >= len(sa.raw.Path) { 268 return nil, 0, EINVAL 269 } 270 sa.raw.Family = AF_UNIX 271 for i := 0; i < n; i++ { 272 sa.raw.Path[i] = int8(name[i]) 273 } 274 // length is family (uint16), name, NUL. 275 sl := _Socklen(2) 276 if n > 0 { 277 sl += _Socklen(n) + 1 278 } 279 if sa.raw.Path[0] == '@' { 280 sa.raw.Path[0] = 0 281 // Don't count trailing NUL for abstract address. 282 sl-- 283 } 284 285 return unsafe.Pointer(&sa.raw), sl, nil 286 } 287 288 type SockaddrLinklayer struct { 289 Protocol uint16 290 Ifindex int 291 Hatype uint16 292 Pkttype uint8 293 Halen uint8 294 Addr [8]byte 295 raw RawSockaddrLinklayer 296 } 297 298 func (sa *SockaddrLinklayer) sockaddr() (unsafe.Pointer, _Socklen, error) { 299 if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff { 300 return nil, 0, EINVAL 301 } 302 sa.raw.Family = AF_PACKET 303 sa.raw.Protocol = sa.Protocol 304 sa.raw.Ifindex = int32(sa.Ifindex) 305 sa.raw.Hatype = sa.Hatype 306 sa.raw.Pkttype = sa.Pkttype 307 sa.raw.Halen = sa.Halen 308 for i := 0; i < len(sa.Addr); i++ { 309 sa.raw.Addr[i] = sa.Addr[i] 310 } 311 return unsafe.Pointer(&sa.raw), SizeofSockaddrLinklayer, nil 312 } 313 314 type SockaddrNetlink struct { 315 Family uint16 316 Pad uint16 317 Pid uint32 318 Groups uint32 319 raw RawSockaddrNetlink 320 } 321 322 func (sa *SockaddrNetlink) sockaddr() (unsafe.Pointer, _Socklen, error) { 323 sa.raw.Family = AF_NETLINK 324 sa.raw.Pad = sa.Pad 325 sa.raw.Pid = sa.Pid 326 sa.raw.Groups = sa.Groups 327 return unsafe.Pointer(&sa.raw), SizeofSockaddrNetlink, nil 328 } 329 330 func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) { 331 switch rsa.Addr.Family { 332 case AF_NETLINK: 333 pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa)) 334 sa := new(SockaddrNetlink) 335 sa.Family = pp.Family 336 sa.Pad = pp.Pad 337 sa.Pid = pp.Pid 338 sa.Groups = pp.Groups 339 return sa, nil 340 341 case AF_PACKET: 342 pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa)) 343 sa := new(SockaddrLinklayer) 344 sa.Protocol = pp.Protocol 345 sa.Ifindex = int(pp.Ifindex) 346 sa.Hatype = pp.Hatype 347 sa.Pkttype = pp.Pkttype 348 sa.Halen = pp.Halen 349 for i := 0; i < len(sa.Addr); i++ { 350 sa.Addr[i] = pp.Addr[i] 351 } 352 return sa, nil 353 354 case AF_UNIX: 355 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) 356 sa := new(SockaddrUnix) 357 if pp.Path[0] == 0 { 358 // "Abstract" Unix domain socket. 359 // Rewrite leading NUL as @ for textual display. 360 // (This is the standard convention.) 361 // Not friendly to overwrite in place, 362 // but the callers below don't care. 363 pp.Path[0] = '@' 364 } 365 366 // Assume path ends at NUL. 367 // This is not technically the Linux semantics for 368 // abstract Unix domain sockets--they are supposed 369 // to be uninterpreted fixed-size binary blobs--but 370 // everyone uses this convention. 371 n := 0 372 for n < len(pp.Path) && pp.Path[n] != 0 { 373 n++ 374 } 375 bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n] 376 sa.Name = string(bytes) 377 return sa, nil 378 379 case AF_INET: 380 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa)) 381 sa := new(SockaddrInet4) 382 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) 383 sa.Port = int(p[0])<<8 + int(p[1]) 384 for i := 0; i < len(sa.Addr); i++ { 385 sa.Addr[i] = pp.Addr[i] 386 } 387 return sa, nil 388 389 case AF_INET6: 390 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa)) 391 sa := new(SockaddrInet6) 392 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) 393 sa.Port = int(p[0])<<8 + int(p[1]) 394 sa.ZoneId = pp.Scope_id 395 for i := 0; i < len(sa.Addr); i++ { 396 sa.Addr[i] = pp.Addr[i] 397 } 398 return sa, nil 399 } 400 return nil, EAFNOSUPPORT 401 } 402 403 func Accept(fd int) (nfd int, sa Sockaddr, err error) { 404 var rsa RawSockaddrAny 405 var len _Socklen = SizeofSockaddrAny 406 nfd, err = accept(fd, &rsa, &len) 407 if err != nil { 408 return 409 } 410 sa, err = anyToSockaddr(&rsa) 411 if err != nil { 412 Close(nfd) 413 nfd = 0 414 } 415 return 416 } 417 418 func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) { 419 var rsa RawSockaddrAny 420 var len _Socklen = SizeofSockaddrAny 421 nfd, err = accept4(fd, &rsa, &len, flags) 422 if err != nil { 423 return 424 } 425 if len > SizeofSockaddrAny { 426 panic("RawSockaddrAny too small") 427 } 428 sa, err = anyToSockaddr(&rsa) 429 if err != nil { 430 Close(nfd) 431 nfd = 0 432 } 433 return 434 } 435 436 func Getsockname(fd int) (sa Sockaddr, err error) { 437 var rsa RawSockaddrAny 438 var len _Socklen = SizeofSockaddrAny 439 if err = getsockname(fd, &rsa, &len); err != nil { 440 return 441 } 442 return anyToSockaddr(&rsa) 443 } 444 445 func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) { 446 vallen := _Socklen(4) 447 err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen) 448 return value, err 449 } 450 451 func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) { 452 var value IPMreq 453 vallen := _Socklen(SizeofIPMreq) 454 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 455 return &value, err 456 } 457 458 func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) { 459 var value IPMreqn 460 vallen := _Socklen(SizeofIPMreqn) 461 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 462 return &value, err 463 } 464 465 func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) { 466 var value IPv6Mreq 467 vallen := _Socklen(SizeofIPv6Mreq) 468 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 469 return &value, err 470 } 471 472 func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) { 473 var value IPv6MTUInfo 474 vallen := _Socklen(SizeofIPv6MTUInfo) 475 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 476 return &value, err 477 } 478 479 func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) { 480 var value ICMPv6Filter 481 vallen := _Socklen(SizeofICMPv6Filter) 482 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 483 return &value, err 484 } 485 486 func GetsockoptUcred(fd, level, opt int) (*Ucred, error) { 487 var value Ucred 488 vallen := _Socklen(SizeofUcred) 489 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 490 return &value, err 491 } 492 493 func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) { 494 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq)) 495 } 496 497 func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) { 498 var msg Msghdr 499 var rsa RawSockaddrAny 500 msg.Name = (*byte)(unsafe.Pointer(&rsa)) 501 msg.Namelen = uint32(SizeofSockaddrAny) 502 var iov Iovec 503 if len(p) > 0 { 504 iov.Base = (*byte)(unsafe.Pointer(&p[0])) 505 iov.SetLen(len(p)) 506 } 507 var dummy byte 508 if len(oob) > 0 { 509 // receive at least one normal byte 510 if len(p) == 0 { 511 iov.Base = &dummy 512 iov.SetLen(1) 513 } 514 msg.Control = (*byte)(unsafe.Pointer(&oob[0])) 515 msg.SetControllen(len(oob)) 516 } 517 msg.Iov = &iov 518 msg.Iovlen = 1 519 if n, err = recvmsg(fd, &msg, flags); err != nil { 520 return 521 } 522 oobn = int(msg.Controllen) 523 recvflags = int(msg.Flags) 524 // source address is only specified if the socket is unconnected 525 if rsa.Addr.Family != AF_UNSPEC { 526 from, err = anyToSockaddr(&rsa) 527 } 528 return 529 } 530 531 func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) { 532 _, err = SendmsgN(fd, p, oob, to, flags) 533 return 534 } 535 536 func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) { 537 var ptr unsafe.Pointer 538 var salen _Socklen 539 if to != nil { 540 var err error 541 ptr, salen, err = to.sockaddr() 542 if err != nil { 543 return 0, err 544 } 545 } 546 var msg Msghdr 547 msg.Name = (*byte)(unsafe.Pointer(ptr)) 548 msg.Namelen = uint32(salen) 549 var iov Iovec 550 if len(p) > 0 { 551 iov.Base = (*byte)(unsafe.Pointer(&p[0])) 552 iov.SetLen(len(p)) 553 } 554 var dummy byte 555 if len(oob) > 0 { 556 // send at least one normal byte 557 if len(p) == 0 { 558 iov.Base = &dummy 559 iov.SetLen(1) 560 } 561 msg.Control = (*byte)(unsafe.Pointer(&oob[0])) 562 msg.SetControllen(len(oob)) 563 } 564 msg.Iov = &iov 565 msg.Iovlen = 1 566 if n, err = sendmsg(fd, &msg, flags); err != nil { 567 return 0, err 568 } 569 if len(oob) > 0 && len(p) == 0 { 570 n = 0 571 } 572 return n, nil 573 } 574 575 // BindToDevice binds the socket associated with fd to device. 576 func BindToDevice(fd int, device string) (err error) { 577 return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device) 578 } 579 580 //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error) 581 582 func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) { 583 // The peek requests are machine-size oriented, so we wrap it 584 // to retrieve arbitrary-length data. 585 586 // The ptrace syscall differs from glibc's ptrace. 587 // Peeks returns the word in *data, not as the return value. 588 589 var buf [sizeofPtr]byte 590 591 // Leading edge. PEEKTEXT/PEEKDATA don't require aligned 592 // access (PEEKUSER warns that it might), but if we don't 593 // align our reads, we might straddle an unmapped page 594 // boundary and not get the bytes leading up to the page 595 // boundary. 596 n := 0 597 if addr%sizeofPtr != 0 { 598 err = ptrace(req, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) 599 if err != nil { 600 return 0, err 601 } 602 n += copy(out, buf[addr%sizeofPtr:]) 603 out = out[n:] 604 } 605 606 // Remainder. 607 for len(out) > 0 { 608 // We use an internal buffer to guarantee alignment. 609 // It's not documented if this is necessary, but we're paranoid. 610 err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) 611 if err != nil { 612 return n, err 613 } 614 copied := copy(out, buf[0:]) 615 n += copied 616 out = out[copied:] 617 } 618 619 return n, nil 620 } 621 622 func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) { 623 return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out) 624 } 625 626 func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) { 627 return ptracePeek(PTRACE_PEEKDATA, pid, addr, out) 628 } 629 630 func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) { 631 // As for ptracePeek, we need to align our accesses to deal 632 // with the possibility of straddling an invalid page. 633 634 // Leading edge. 635 n := 0 636 if addr%sizeofPtr != 0 { 637 var buf [sizeofPtr]byte 638 err = ptrace(peekReq, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) 639 if err != nil { 640 return 0, err 641 } 642 n += copy(buf[addr%sizeofPtr:], data) 643 word := *((*uintptr)(unsafe.Pointer(&buf[0]))) 644 err = ptrace(pokeReq, pid, addr-addr%sizeofPtr, word) 645 if err != nil { 646 return 0, err 647 } 648 data = data[n:] 649 } 650 651 // Interior. 652 for len(data) > sizeofPtr { 653 word := *((*uintptr)(unsafe.Pointer(&data[0]))) 654 err = ptrace(pokeReq, pid, addr+uintptr(n), word) 655 if err != nil { 656 return n, err 657 } 658 n += sizeofPtr 659 data = data[sizeofPtr:] 660 } 661 662 // Trailing edge. 663 if len(data) > 0 { 664 var buf [sizeofPtr]byte 665 err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) 666 if err != nil { 667 return n, err 668 } 669 copy(buf[0:], data) 670 word := *((*uintptr)(unsafe.Pointer(&buf[0]))) 671 err = ptrace(pokeReq, pid, addr+uintptr(n), word) 672 if err != nil { 673 return n, err 674 } 675 n += len(data) 676 } 677 678 return n, nil 679 } 680 681 func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) { 682 return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data) 683 } 684 685 func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) { 686 return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data) 687 } 688 689 func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) { 690 return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout))) 691 } 692 693 func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) { 694 return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs))) 695 } 696 697 func PtraceSetOptions(pid int, options int) (err error) { 698 return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options)) 699 } 700 701 func PtraceGetEventMsg(pid int) (msg uint, err error) { 702 var data _C_long 703 err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data))) 704 msg = uint(data) 705 return 706 } 707 708 func PtraceCont(pid int, signal int) (err error) { 709 return ptrace(PTRACE_CONT, pid, 0, uintptr(signal)) 710 } 711 712 func PtraceSyscall(pid int, signal int) (err error) { 713 return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal)) 714 } 715 716 func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) } 717 718 func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) } 719 720 func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) } 721 722 //sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error) 723 724 func Reboot(cmd int) (err error) { 725 return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "") 726 } 727 728 func clen(n []byte) int { 729 for i := 0; i < len(n); i++ { 730 if n[i] == 0 { 731 return i 732 } 733 } 734 return len(n) 735 } 736 737 func ReadDirent(fd int, buf []byte) (n int, err error) { 738 return Getdents(fd, buf) 739 } 740 741 func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) { 742 origlen := len(buf) 743 count = 0 744 for max != 0 && len(buf) > 0 { 745 dirent := (*Dirent)(unsafe.Pointer(&buf[0])) 746 buf = buf[dirent.Reclen:] 747 if dirent.Ino == 0 { // File absent in directory. 748 continue 749 } 750 bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0])) 751 var name = string(bytes[0:clen(bytes[:])]) 752 if name == "." || name == ".." { // Useless names 753 continue 754 } 755 max-- 756 count++ 757 names = append(names, name) 758 } 759 return origlen - len(buf), count, names 760 } 761 762 //sys mount(source string, target string, fstype string, flags uintptr, data *byte) (err error) 763 764 func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) { 765 // Certain file systems get rather angry and EINVAL if you give 766 // them an empty string of data, rather than NULL. 767 if data == "" { 768 return mount(source, target, fstype, flags, nil) 769 } 770 datap, err := BytePtrFromString(data) 771 if err != nil { 772 return err 773 } 774 err = mount(source, target, fstype, flags, datap) 775 use(unsafe.Pointer(datap)) 776 return err 777 } 778 779 // Sendto 780 // Recvfrom 781 // Socketpair 782 783 /* 784 * Direct access 785 */ 786 //sys Access(path string, mode uint32) (err error) 787 //sys Acct(path string) (err error) 788 //sys Adjtimex(buf *Timex) (state int, err error) 789 //sys Chdir(path string) (err error) 790 //sys Chmod(path string, mode uint32) (err error) 791 //sys Chroot(path string) (err error) 792 //sys Close(fd int) (err error) 793 //sys Creat(path string, mode uint32) (fd int, err error) 794 //sysnb Dup(oldfd int) (fd int, err error) 795 //sysnb Dup2(oldfd int, newfd int) (err error) 796 //sysnb Dup3(oldfd int, newfd int, flags int) (err error) 797 //sysnb EpollCreate(size int) (fd int, err error) 798 //sysnb EpollCreate1(flag int) (fd int, err error) 799 //sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error) 800 //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error) 801 //sys Exit(code int) = SYS_EXIT_GROUP 802 //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error) 803 //sys Fallocate(fd int, mode uint32, off int64, len int64) (err error) 804 //sys Fchdir(fd int) (err error) 805 //sys Fchmod(fd int, mode uint32) (err error) 806 //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) 807 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) 808 //sys fcntl(fd int, cmd int, arg int) (val int, err error) 809 //sys Fdatasync(fd int) (err error) 810 //sys Flock(fd int, how int) (err error) 811 //sys Fsync(fd int) (err error) 812 //sys Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64 813 //sysnb Getpgid(pid int) (pgid int, err error) 814 //sysnb Getpgrp() (pid int) 815 //sysnb Getpid() (pid int) 816 //sysnb Getppid() (ppid int) 817 //sys Getpriority(which int, who int) (prio int, err error) 818 //sysnb Getrusage(who int, rusage *Rusage) (err error) 819 //sysnb Gettid() (tid int) 820 //sys Getxattr(path string, attr string, dest []byte) (sz int, err error) 821 //sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error) 822 //sysnb InotifyInit() (fd int, err error) 823 //sysnb InotifyInit1(flags int) (fd int, err error) 824 //sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error) 825 //sysnb Kill(pid int, sig Signal) (err error) 826 //sys Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG 827 //sys Link(oldpath string, newpath string) (err error) 828 //sys Listxattr(path string, dest []byte) (sz int, err error) 829 //sys Mkdir(path string, mode uint32) (err error) 830 //sys Mkdirat(dirfd int, path string, mode uint32) (err error) 831 //sys Mknod(path string, mode uint32, dev int) (err error) 832 //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error) 833 //sys Nanosleep(time *Timespec, leftover *Timespec) (err error) 834 //sys Pause() (err error) 835 //sys PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT 836 //sysnb prlimit(pid int, resource int, old *Rlimit, newlimit *Rlimit) (err error) = SYS_PRLIMIT64 837 //sys read(fd int, p []byte) (n int, err error) 838 //sys Readlink(path string, buf []byte) (n int, err error) 839 //sys Removexattr(path string, attr string) (err error) 840 //sys Rename(oldpath string, newpath string) (err error) 841 //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error) 842 //sys Rmdir(path string) (err error) 843 //sys Setdomainname(p []byte) (err error) 844 //sys Sethostname(p []byte) (err error) 845 //sysnb Setpgid(pid int, pgid int) (err error) 846 //sysnb Setsid() (pid int, err error) 847 //sysnb Settimeofday(tv *Timeval) (err error) 848 849 // issue 1435. 850 // On linux Setuid and Setgid only affects the current thread, not the process. 851 // This does not match what most callers expect so we must return an error 852 // here rather than letting the caller think that the call succeeded. 853 854 func Setuid(uid int) (err error) { 855 return EOPNOTSUPP 856 } 857 858 func Setgid(uid int) (err error) { 859 return EOPNOTSUPP 860 } 861 862 //sys Setpriority(which int, who int, prio int) (err error) 863 //sys Setxattr(path string, attr string, data []byte, flags int) (err error) 864 //sys Symlink(oldpath string, newpath string) (err error) 865 //sys Sync() 866 //sysnb Sysinfo(info *Sysinfo_t) (err error) 867 //sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error) 868 //sysnb Tgkill(tgid int, tid int, sig Signal) (err error) 869 //sysnb Times(tms *Tms) (ticks uintptr, err error) 870 //sysnb Umask(mask int) (oldmask int) 871 //sysnb Uname(buf *Utsname) (err error) 872 //sys Unlink(path string) (err error) 873 //sys Unlinkat(dirfd int, path string) (err error) 874 //sys Unmount(target string, flags int) (err error) = SYS_UMOUNT2 875 //sys Unshare(flags int) (err error) 876 //sys Ustat(dev int, ubuf *Ustat_t) (err error) 877 //sys Utime(path string, buf *Utimbuf) (err error) 878 //sys write(fd int, p []byte) (n int, err error) 879 //sys exitThread(code int) (err error) = SYS_EXIT 880 //sys readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ 881 //sys writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE 882 883 // mmap varies by architecture; see syscall_linux_*.go. 884 //sys munmap(addr uintptr, length uintptr) (err error) 885 886 var mapper = &mmapper{ 887 active: make(map[*byte][]byte), 888 mmap: mmap, 889 munmap: munmap, 890 } 891 892 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) { 893 return mapper.Mmap(fd, offset, length, prot, flags) 894 } 895 896 func Munmap(b []byte) (err error) { 897 return mapper.Munmap(b) 898 } 899 900 //sys Madvise(b []byte, advice int) (err error) 901 //sys Mprotect(b []byte, prot int) (err error) 902 //sys Mlock(b []byte) (err error) 903 //sys Munlock(b []byte) (err error) 904 //sys Mlockall(flags int) (err error) 905 //sys Munlockall() (err error) 906 907 /* 908 * Unimplemented 909 */ 910 // AddKey 911 // AfsSyscall 912 // Alarm 913 // ArchPrctl 914 // Brk 915 // Capget 916 // Capset 917 // ClockGetres 918 // ClockGettime 919 // ClockNanosleep 920 // ClockSettime 921 // Clone 922 // CreateModule 923 // DeleteModule 924 // EpollCtlOld 925 // EpollPwait 926 // EpollWaitOld 927 // Eventfd 928 // Execve 929 // Fadvise64 930 // Fgetxattr 931 // Flistxattr 932 // Fork 933 // Fremovexattr 934 // Fsetxattr 935 // Futex 936 // GetKernelSyms 937 // GetMempolicy 938 // GetRobustList 939 // GetThreadArea 940 // Getitimer 941 // Getpmsg 942 // IoCancel 943 // IoDestroy 944 // IoGetevents 945 // IoSetup 946 // IoSubmit 947 // Ioctl 948 // IoprioGet 949 // IoprioSet 950 // KexecLoad 951 // Keyctl 952 // Lgetxattr 953 // Llistxattr 954 // LookupDcookie 955 // Lremovexattr 956 // Lsetxattr 957 // Mbind 958 // MigratePages 959 // Mincore 960 // ModifyLdt 961 // Mount 962 // MovePages 963 // Mprotect 964 // MqGetsetattr 965 // MqNotify 966 // MqOpen 967 // MqTimedreceive 968 // MqTimedsend 969 // MqUnlink 970 // Mremap 971 // Msgctl 972 // Msgget 973 // Msgrcv 974 // Msgsnd 975 // Msync 976 // Newfstatat 977 // Nfsservctl 978 // Personality 979 // Poll 980 // Ppoll 981 // Prctl 982 // Pselect6 983 // Ptrace 984 // Putpmsg 985 // QueryModule 986 // Quotactl 987 // Readahead 988 // Readv 989 // RemapFilePages 990 // RequestKey 991 // RestartSyscall 992 // RtSigaction 993 // RtSigpending 994 // RtSigprocmask 995 // RtSigqueueinfo 996 // RtSigreturn 997 // RtSigsuspend 998 // RtSigtimedwait 999 // SchedGetPriorityMax 1000 // SchedGetPriorityMin 1001 // SchedGetaffinity 1002 // SchedGetparam 1003 // SchedGetscheduler 1004 // SchedRrGetInterval 1005 // SchedSetaffinity 1006 // SchedSetparam 1007 // SchedYield 1008 // Security 1009 // Semctl 1010 // Semget 1011 // Semop 1012 // Semtimedop 1013 // SetMempolicy 1014 // SetRobustList 1015 // SetThreadArea 1016 // SetTidAddress 1017 // Shmat 1018 // Shmctl 1019 // Shmdt 1020 // Shmget 1021 // Sigaltstack 1022 // Signalfd 1023 // Swapoff 1024 // Swapon 1025 // Sysfs 1026 // TimerCreate 1027 // TimerDelete 1028 // TimerGetoverrun 1029 // TimerGettime 1030 // TimerSettime 1031 // Timerfd 1032 // Tkill (obsolete) 1033 // Tuxcall 1034 // Umount2 1035 // Uselib 1036 // Utimensat 1037 // Vfork 1038 // Vhangup 1039 // Vmsplice 1040 // Vserver 1041 // Waitid 1042 // _Sysctl