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