1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- // Copyright 2009 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- // Linux system calls.
- // This file is compiled as ordinary Go code,
- // but it is also input to mksyscall,
- // which parses the //sys lines and generates system call stubs.
- // Note that sometimes we use a lowercase //sys name and
- // wrap it in our own nicer implementation.
- package unix
- import (
- "syscall"
- "unsafe"
- )
- /*
- * Wrapped
- */
- func Access(path string, mode uint32) (err error) {
- return Faccessat(AT_FDCWD, path, mode, 0)
- }
- func Chmod(path string, mode uint32) (err error) {
- return Fchmodat(AT_FDCWD, path, mode, 0)
- }
- func Chown(path string, uid int, gid int) (err error) {
- return Fchownat(AT_FDCWD, path, uid, gid, 0)
- }
- func Creat(path string, mode uint32) (fd int, err error) {
- return Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode)
- }
- //sys linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error)
- func Link(oldpath string, newpath string) (err error) {
- return linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0)
- }
- func Mkdir(path string, mode uint32) (err error) {
- return Mkdirat(AT_FDCWD, path, mode)
- }
- func Mknod(path string, mode uint32, dev int) (err error) {
- return Mknodat(AT_FDCWD, path, mode, dev)
- }
- func Open(path string, mode int, perm uint32) (fd int, err error) {
- return openat(AT_FDCWD, path, mode|O_LARGEFILE, perm)
- }
- //sys openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
- func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
- return openat(dirfd, path, flags|O_LARGEFILE, mode)
- }
- //sys readlinkat(dirfd int, path string, buf []byte) (n int, err error)
- func Readlink(path string, buf []byte) (n int, err error) {
- return readlinkat(AT_FDCWD, path, buf)
- }
- func Rename(oldpath string, newpath string) (err error) {
- return Renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath)
- }
- func Rmdir(path string) error {
- return unlinkat(AT_FDCWD, path, AT_REMOVEDIR)
- }
- //sys symlinkat(oldpath string, newdirfd int, newpath string) (err error)
- func Symlink(oldpath string, newpath string) (err error) {
- return symlinkat(oldpath, AT_FDCWD, newpath)
- }
- func Unlink(path string) error {
- return unlinkat(AT_FDCWD, path, 0)
- }
- //sys unlinkat(dirfd int, path string, flags int) (err error)
- func Unlinkat(dirfd int, path string, flags int) error {
- return unlinkat(dirfd, path, flags)
- }
- //sys utimes(path string, times *[2]Timeval) (err error)
- func Utimes(path string, tv []Timeval) (err error) {
- if tv == nil {
- return utimes(path, nil)
- }
- if len(tv) != 2 {
- return EINVAL
- }
- return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
- }
- //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
- func UtimesNano(path string, ts []Timespec) error {
- if ts == nil {
- err := utimensat(AT_FDCWD, path, nil, 0)
- if err != ENOSYS {
- return err
- }
- return utimes(path, nil)
- }
- if len(ts) != 2 {
- return EINVAL
- }
- err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
- if err != ENOSYS {
- return err
- }
- // If the utimensat syscall isn't available (utimensat was added to Linux
- // in 2.6.22, Released, 8 July 2007) then fall back to utimes
- var tv [2]Timeval
- for i := 0; i < 2; i++ {
- tv[i].Sec = ts[i].Sec
- tv[i].Usec = ts[i].Nsec / 1000
- }
- return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
- }
- func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
- if ts == nil {
- return utimensat(dirfd, path, nil, flags)
- }
- if len(ts) != 2 {
- return EINVAL
- }
- return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
- }
- //sys futimesat(dirfd int, path *byte, times *[2]Timeval) (err error)
- func Futimesat(dirfd int, path string, tv []Timeval) error {
- pathp, err := BytePtrFromString(path)
- if err != nil {
- return err
- }
- if tv == nil {
- return futimesat(dirfd, pathp, nil)
- }
- if len(tv) != 2 {
- return EINVAL
- }
- return futimesat(dirfd, pathp, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
- }
- func Futimes(fd int, tv []Timeval) (err error) {
- // Believe it or not, this is the best we can do on Linux
- // (and is what glibc does).
- return Utimes("/proc/self/fd/"+itoa(fd), tv)
- }
- const ImplementsGetwd = true
- //sys Getcwd(buf []byte) (n int, err error)
- func Getwd() (wd string, err error) {
- var buf [PathMax]byte
- n, err := Getcwd(buf[0:])
- if err != nil {
- return "", err
- }
- // Getcwd returns the number of bytes written to buf, including the NUL.
- if n < 1 || n > len(buf) || buf[n-1] != 0 {
- return "", EINVAL
- }
- return string(buf[0 : n-1]), nil
- }
- func Getgroups() (gids []int, err error) {
- n, err := getgroups(0, nil)
- if err != nil {
- return nil, err
- }
- if n == 0 {
- return nil, nil
- }
- // Sanity check group count. Max is 1<<16 on Linux.
- if n < 0 || n > 1<<20 {
- return nil, EINVAL
- }
- a := make([]_Gid_t, n)
- n, err = getgroups(n, &a[0])
- if err != nil {
- return nil, err
- }
- gids = make([]int, n)
- for i, v := range a[0:n] {
- gids[i] = int(v)
- }
- return
- }
- func Setgroups(gids []int) (err error) {
- if len(gids) == 0 {
- return setgroups(0, nil)
- }
- a := make([]_Gid_t, len(gids))
- for i, v := range gids {
- a[i] = _Gid_t(v)
- }
- return setgroups(len(a), &a[0])
- }
- type WaitStatus uint32
- // Wait status is 7 bits at bottom, either 0 (exited),
- // 0x7F (stopped), or a signal number that caused an exit.
- // The 0x80 bit is whether there was a core dump.
- // An extra number (exit code, signal causing a stop)
- // is in the high bits. At least that's the idea.
- // There are various irregularities. For example, the
- // "continued" status is 0xFFFF, distinguishing itself
- // from stopped via the core dump bit.
- const (
- mask = 0x7F
- core = 0x80
- exited = 0x00
- stopped = 0x7F
- shift = 8
- )
- func (w WaitStatus) Exited() bool { return w&mask == exited }
- func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited }
- func (w WaitStatus) Stopped() bool { return w&0xFF == stopped }
- func (w WaitStatus) Continued() bool { return w == 0xFFFF }
- func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
- func (w WaitStatus) ExitStatus() int {
- if !w.Exited() {
- return -1
- }
- return int(w>>shift) & 0xFF
- }
- func (w WaitStatus) Signal() syscall.Signal {
- if !w.Signaled() {
- return -1
- }
- return syscall.Signal(w & mask)
- }
- func (w WaitStatus) StopSignal() syscall.Signal {
- if !w.Stopped() {
- return -1
- }
- return syscall.Signal(w>>shift) & 0xFF
- }
- func (w WaitStatus) TrapCause() int {
- if w.StopSignal() != SIGTRAP {
- return -1
- }
- return int(w>>shift) >> 8
- }
- //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
- func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
- var status _C_int
- wpid, err = wait4(pid, &status, options, rusage)
- if wstatus != nil {
- *wstatus = WaitStatus(status)
- }
- return
- }
- func Mkfifo(path string, mode uint32) (err error) {
- return Mknod(path, mode|S_IFIFO, 0)
- }
- func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
- if sa.Port < 0 || sa.Port > 0xFFFF {
- return nil, 0, EINVAL
- }
- sa.raw.Family = AF_INET
- p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
- p[0] = byte(sa.Port >> 8)
- p[1] = byte(sa.Port)
- for i := 0; i < len(sa.Addr); i++ {
- sa.raw.Addr[i] = sa.Addr[i]
- }
- return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
- }
- func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
- if sa.Port < 0 || sa.Port > 0xFFFF {
- return nil, 0, EINVAL
- }
- sa.raw.Family = AF_INET6
- p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
- p[0] = byte(sa.Port >> 8)
- p[1] = byte(sa.Port)
- sa.raw.Scope_id = sa.ZoneId
- for i := 0; i < len(sa.Addr); i++ {
- sa.raw.Addr[i] = sa.Addr[i]
- }
- return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
- }
- func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
- name := sa.Name
- n := len(name)
- if n >= len(sa.raw.Path) {
- return nil, 0, EINVAL
- }
- sa.raw.Family = AF_UNIX
- for i := 0; i < n; i++ {
- sa.raw.Path[i] = int8(name[i])
- }
- // length is family (uint16), name, NUL.
- sl := _Socklen(2)
- if n > 0 {
- sl += _Socklen(n) + 1
- }
- if sa.raw.Path[0] == '@' {
- sa.raw.Path[0] = 0
- // Don't count trailing NUL for abstract address.
- sl--
- }
- return unsafe.Pointer(&sa.raw), sl, nil
- }
- type SockaddrLinklayer struct {
- Protocol uint16
- Ifindex int
- Hatype uint16
- Pkttype uint8
- Halen uint8
- Addr [8]byte
- raw RawSockaddrLinklayer
- }
- func (sa *SockaddrLinklayer) sockaddr() (unsafe.Pointer, _Socklen, error) {
- if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
- return nil, 0, EINVAL
- }
- sa.raw.Family = AF_PACKET
- sa.raw.Protocol = sa.Protocol
- sa.raw.Ifindex = int32(sa.Ifindex)
- sa.raw.Hatype = sa.Hatype
- sa.raw.Pkttype = sa.Pkttype
- sa.raw.Halen = sa.Halen
- for i := 0; i < len(sa.Addr); i++ {
- sa.raw.Addr[i] = sa.Addr[i]
- }
- return unsafe.Pointer(&sa.raw), SizeofSockaddrLinklayer, nil
- }
- type SockaddrNetlink struct {
- Family uint16
- Pad uint16
- Pid uint32
- Groups uint32
- raw RawSockaddrNetlink
- }
- func (sa *SockaddrNetlink) sockaddr() (unsafe.Pointer, _Socklen, error) {
- sa.raw.Family = AF_NETLINK
- sa.raw.Pad = sa.Pad
- sa.raw.Pid = sa.Pid
- sa.raw.Groups = sa.Groups
- return unsafe.Pointer(&sa.raw), SizeofSockaddrNetlink, nil
- }
- func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
- switch rsa.Addr.Family {
- case AF_NETLINK:
- pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa))
- sa := new(SockaddrNetlink)
- sa.Family = pp.Family
- sa.Pad = pp.Pad
- sa.Pid = pp.Pid
- sa.Groups = pp.Groups
- return sa, nil
- case AF_PACKET:
- pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa))
- sa := new(SockaddrLinklayer)
- sa.Protocol = pp.Protocol
- sa.Ifindex = int(pp.Ifindex)
- sa.Hatype = pp.Hatype
- sa.Pkttype = pp.Pkttype
- sa.Halen = pp.Halen
- for i := 0; i < len(sa.Addr); i++ {
- sa.Addr[i] = pp.Addr[i]
- }
- return sa, nil
- case AF_UNIX:
- pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
- sa := new(SockaddrUnix)
- if pp.Path[0] == 0 {
- // "Abstract" Unix domain socket.
- // Rewrite leading NUL as @ for textual display.
- // (This is the standard convention.)
- // Not friendly to overwrite in place,
- // but the callers below don't care.
- pp.Path[0] = '@'
- }
- // Assume path ends at NUL.
- // This is not technically the Linux semantics for
- // abstract Unix domain sockets--they are supposed
- // to be uninterpreted fixed-size binary blobs--but
- // everyone uses this convention.
- n := 0
- for n < len(pp.Path) && pp.Path[n] != 0 {
- n++
- }
- bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
- sa.Name = string(bytes)
- return sa, nil
- case AF_INET:
- pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
- sa := new(SockaddrInet4)
- p := (*[2]byte)(unsafe.Pointer(&pp.Port))
- sa.Port = int(p[0])<<8 + int(p[1])
- for i := 0; i < len(sa.Addr); i++ {
- sa.Addr[i] = pp.Addr[i]
- }
- return sa, nil
- case AF_INET6:
- pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
- sa := new(SockaddrInet6)
- p := (*[2]byte)(unsafe.Pointer(&pp.Port))
- sa.Port = int(p[0])<<8 + int(p[1])
- sa.ZoneId = pp.Scope_id
- for i := 0; i < len(sa.Addr); i++ {
- sa.Addr[i] = pp.Addr[i]
- }
- return sa, nil
- }
- return nil, EAFNOSUPPORT
- }
- func Accept(fd int) (nfd int, sa Sockaddr, err error) {
- var rsa RawSockaddrAny
- var len _Socklen = SizeofSockaddrAny
- nfd, err = accept(fd, &rsa, &len)
- if err != nil {
- return
- }
- sa, err = anyToSockaddr(&rsa)
- if err != nil {
- Close(nfd)
- nfd = 0
- }
- return
- }
- func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) {
- var rsa RawSockaddrAny
- var len _Socklen = SizeofSockaddrAny
- nfd, err = accept4(fd, &rsa, &len, flags)
- if err != nil {
- return
- }
- if len > SizeofSockaddrAny {
- panic("RawSockaddrAny too small")
- }
- sa, err = anyToSockaddr(&rsa)
- if err != nil {
- Close(nfd)
- nfd = 0
- }
- return
- }
- func Getsockname(fd int) (sa Sockaddr, err error) {
- var rsa RawSockaddrAny
- var len _Socklen = SizeofSockaddrAny
- if err = getsockname(fd, &rsa, &len); err != nil {
- return
- }
- return anyToSockaddr(&rsa)
- }
- func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
- vallen := _Socklen(4)
- err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
- return value, err
- }
- func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
- var value IPMreq
- vallen := _Socklen(SizeofIPMreq)
- err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
- return &value, err
- }
- func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
- var value IPMreqn
- vallen := _Socklen(SizeofIPMreqn)
- err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
- return &value, err
- }
- func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
- var value IPv6Mreq
- vallen := _Socklen(SizeofIPv6Mreq)
- err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
- return &value, err
- }
- func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
- var value IPv6MTUInfo
- vallen := _Socklen(SizeofIPv6MTUInfo)
- err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
- return &value, err
- }
- func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
- var value ICMPv6Filter
- vallen := _Socklen(SizeofICMPv6Filter)
- err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
- return &value, err
- }
- func GetsockoptUcred(fd, level, opt int) (*Ucred, error) {
- var value Ucred
- vallen := _Socklen(SizeofUcred)
- err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
- return &value, err
- }
- func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
- return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq))
- }
- func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
- var msg Msghdr
- var rsa RawSockaddrAny
- msg.Name = (*byte)(unsafe.Pointer(&rsa))
- msg.Namelen = uint32(SizeofSockaddrAny)
- var iov Iovec
- if len(p) > 0 {
- iov.Base = (*byte)(unsafe.Pointer(&p[0]))
- iov.SetLen(len(p))
- }
- var dummy byte
- if len(oob) > 0 {
- // receive at least one normal byte
- if len(p) == 0 {
- iov.Base = &dummy
- iov.SetLen(1)
- }
- msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
- msg.SetControllen(len(oob))
- }
- msg.Iov = &iov
- msg.Iovlen = 1
- if n, err = recvmsg(fd, &msg, flags); err != nil {
- return
- }
- oobn = int(msg.Controllen)
- recvflags = int(msg.Flags)
- // source address is only specified if the socket is unconnected
- if rsa.Addr.Family != AF_UNSPEC {
- from, err = anyToSockaddr(&rsa)
- }
- return
- }
- func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
- _, err = SendmsgN(fd, p, oob, to, flags)
- return
- }
- func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
- var ptr unsafe.Pointer
- var salen _Socklen
- if to != nil {
- var err error
- ptr, salen, err = to.sockaddr()
- if err != nil {
- return 0, err
- }
- }
- var msg Msghdr
- msg.Name = (*byte)(unsafe.Pointer(ptr))
- msg.Namelen = uint32(salen)
- var iov Iovec
- if len(p) > 0 {
- iov.Base = (*byte)(unsafe.Pointer(&p[0]))
- iov.SetLen(len(p))
- }
- var dummy byte
- if len(oob) > 0 {
- // send at least one normal byte
- if len(p) == 0 {
- iov.Base = &dummy
- iov.SetLen(1)
- }
- msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
- msg.SetControllen(len(oob))
- }
- msg.Iov = &iov
- msg.Iovlen = 1
- if n, err = sendmsg(fd, &msg, flags); err != nil {
- return 0, err
- }
- if len(oob) > 0 && len(p) == 0 {
- n = 0
- }
- return n, nil
- }
- // BindToDevice binds the socket associated with fd to device.
- func BindToDevice(fd int, device string) (err error) {
- return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
- }
- //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
- func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) {
- // The peek requests are machine-size oriented, so we wrap it
- // to retrieve arbitrary-length data.
- // The ptrace syscall differs from glibc's ptrace.
- // Peeks returns the word in *data, not as the return value.
- var buf [sizeofPtr]byte
- // Leading edge. PEEKTEXT/PEEKDATA don't require aligned
- // access (PEEKUSER warns that it might), but if we don't
- // align our reads, we might straddle an unmapped page
- // boundary and not get the bytes leading up to the page
- // boundary.
- n := 0
- if addr%sizeofPtr != 0 {
- err = ptrace(req, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
- if err != nil {
- return 0, err
- }
- n += copy(out, buf[addr%sizeofPtr:])
- out = out[n:]
- }
- // Remainder.
- for len(out) > 0 {
- // We use an internal buffer to guarantee alignment.
- // It's not documented if this is necessary, but we're paranoid.
- err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
- if err != nil {
- return n, err
- }
- copied := copy(out, buf[0:])
- n += copied
- out = out[copied:]
- }
- return n, nil
- }
- func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) {
- return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out)
- }
- func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
- return ptracePeek(PTRACE_PEEKDATA, pid, addr, out)
- }
- func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) {
- // As for ptracePeek, we need to align our accesses to deal
- // with the possibility of straddling an invalid page.
- // Leading edge.
- n := 0
- if addr%sizeofPtr != 0 {
- var buf [sizeofPtr]byte
- err = ptrace(peekReq, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
- if err != nil {
- return 0, err
- }
- n += copy(buf[addr%sizeofPtr:], data)
- word := *((*uintptr)(unsafe.Pointer(&buf[0])))
- err = ptrace(pokeReq, pid, addr-addr%sizeofPtr, word)
- if err != nil {
- return 0, err
- }
- data = data[n:]
- }
- // Interior.
- for len(data) > sizeofPtr {
- word := *((*uintptr)(unsafe.Pointer(&data[0])))
- err = ptrace(pokeReq, pid, addr+uintptr(n), word)
- if err != nil {
- return n, err
- }
- n += sizeofPtr
- data = data[sizeofPtr:]
- }
- // Trailing edge.
- if len(data) > 0 {
- var buf [sizeofPtr]byte
- err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
- if err != nil {
- return n, err
- }
- copy(buf[0:], data)
- word := *((*uintptr)(unsafe.Pointer(&buf[0])))
- err = ptrace(pokeReq, pid, addr+uintptr(n), word)
- if err != nil {
- return n, err
- }
- n += len(data)
- }
- return n, nil
- }
- func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
- return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data)
- }
- func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) {
- return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data)
- }
- func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) {
- return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout)))
- }
- func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) {
- return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs)))
- }
- func PtraceSetOptions(pid int, options int) (err error) {
- return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options))
- }
- func PtraceGetEventMsg(pid int) (msg uint, err error) {
- var data _C_long
- err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data)))
- msg = uint(data)
- return
- }
- func PtraceCont(pid int, signal int) (err error) {
- return ptrace(PTRACE_CONT, pid, 0, uintptr(signal))
- }
- func PtraceSyscall(pid int, signal int) (err error) {
- return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal))
- }
- func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) }
- func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) }
- func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) }
- //sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error)
- func Reboot(cmd int) (err error) {
- return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "")
- }
- func clen(n []byte) int {
- for i := 0; i < len(n); i++ {
- if n[i] == 0 {
- return i
- }
- }
- return len(n)
- }
- func ReadDirent(fd int, buf []byte) (n int, err error) {
- return Getdents(fd, buf)
- }
- func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
- origlen := len(buf)
- count = 0
- for max != 0 && len(buf) > 0 {
- dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
- buf = buf[dirent.Reclen:]
- if dirent.Ino == 0 { // File absent in directory.
- continue
- }
- bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
- var name = string(bytes[0:clen(bytes[:])])
- if name == "." || name == ".." { // Useless names
- continue
- }
- max--
- count++
- names = append(names, name)
- }
- return origlen - len(buf), count, names
- }
- //sys mount(source string, target string, fstype string, flags uintptr, data *byte) (err error)
- func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) {
- // Certain file systems get rather angry and EINVAL if you give
- // them an empty string of data, rather than NULL.
- if data == "" {
- return mount(source, target, fstype, flags, nil)
- }
- datap, err := BytePtrFromString(data)
- if err != nil {
- return err
- }
- return mount(source, target, fstype, flags, datap)
- }
- // Sendto
- // Recvfrom
- // Socketpair
- /*
- * Direct access
- */
- //sys Acct(path string) (err error)
- //sys Adjtimex(buf *Timex) (state int, err error)
- //sys Chdir(path string) (err error)
- //sys Chroot(path string) (err error)
- //sys ClockGettime(clockid int32, time *Timespec) (err error)
- //sys Close(fd int) (err error)
- //sys Dup(oldfd int) (fd int, err error)
- //sys Dup3(oldfd int, newfd int, flags int) (err error)
- //sysnb EpollCreate(size int) (fd int, err error)
- //sysnb EpollCreate1(flag int) (fd int, err error)
- //sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
- //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
- //sys Exit(code int) = SYS_EXIT_GROUP
- //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
- //sys Fallocate(fd int, mode uint32, off int64, len int64) (err error)
- //sys Fchdir(fd int) (err error)
- //sys Fchmod(fd int, mode uint32) (err error)
- //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
- //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
- //sys fcntl(fd int, cmd int, arg int) (val int, err error)
- //sys Fdatasync(fd int) (err error)
- //sys Flock(fd int, how int) (err error)
- //sys Fsync(fd int) (err error)
- //sys Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64
- //sysnb Getpgid(pid int) (pgid int, err error)
- func Getpgrp() (pid int) {
- pid, _ = Getpgid(0)
- return
- }
- //sysnb Getpid() (pid int)
- //sysnb Getppid() (ppid int)
- //sys Getpriority(which int, who int) (prio int, err error)
- //sysnb Getrusage(who int, rusage *Rusage) (err error)
- //sysnb Gettid() (tid int)
- //sys Getxattr(path string, attr string, dest []byte) (sz int, err error)
- //sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error)
- //sysnb InotifyInit1(flags int) (fd int, err error)
- //sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error)
- //sysnb Kill(pid int, sig syscall.Signal) (err error)
- //sys Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG
- //sys Listxattr(path string, dest []byte) (sz int, err error)
- //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
- //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
- //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
- //sys Pause() (err error)
- //sys PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT
- //sysnb prlimit(pid int, resource int, old *Rlimit, newlimit *Rlimit) (err error) = SYS_PRLIMIT64
- //sys Prctl(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (err error)
- //sys read(fd int, p []byte) (n int, err error)
- //sys Removexattr(path string, attr string) (err error)
- //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
- //sys Setdomainname(p []byte) (err error)
- //sys Sethostname(p []byte) (err error)
- //sysnb Setpgid(pid int, pgid int) (err error)
- //sysnb Setsid() (pid int, err error)
- //sysnb Settimeofday(tv *Timeval) (err error)
- // issue 1435.
- // On linux Setuid and Setgid only affects the current thread, not the process.
- // This does not match what most callers expect so we must return an error
- // here rather than letting the caller think that the call succeeded.
- func Setuid(uid int) (err error) {
- return EOPNOTSUPP
- }
- func Setgid(uid int) (err error) {
- return EOPNOTSUPP
- }
- //sys Setpriority(which int, who int, prio int) (err error)
- //sys Setxattr(path string, attr string, data []byte, flags int) (err error)
- //sys Sync()
- //sysnb Sysinfo(info *Sysinfo_t) (err error)
- //sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error)
- //sysnb Tgkill(tgid int, tid int, sig syscall.Signal) (err error)
- //sysnb Times(tms *Tms) (ticks uintptr, err error)
- //sysnb Umask(mask int) (oldmask int)
- //sysnb Uname(buf *Utsname) (err error)
- //sys Unmount(target string, flags int) (err error) = SYS_UMOUNT2
- //sys Unshare(flags int) (err error)
- //sys Ustat(dev int, ubuf *Ustat_t) (err error)
- //sys Utime(path string, buf *Utimbuf) (err error)
- //sys write(fd int, p []byte) (n int, err error)
- //sys exitThread(code int) (err error) = SYS_EXIT
- //sys readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ
- //sys writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE
- // mmap varies by architecture; see syscall_linux_*.go.
- //sys munmap(addr uintptr, length uintptr) (err error)
- var mapper = &mmapper{
- active: make(map[*byte][]byte),
- mmap: mmap,
- munmap: munmap,
- }
- func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
- return mapper.Mmap(fd, offset, length, prot, flags)
- }
- func Munmap(b []byte) (err error) {
- return mapper.Munmap(b)
- }
- //sys Madvise(b []byte, advice int) (err error)
- //sys Mprotect(b []byte, prot int) (err error)
- //sys Mlock(b []byte) (err error)
- //sys Munlock(b []byte) (err error)
- //sys Mlockall(flags int) (err error)
- //sys Munlockall() (err error)
- /*
- * Unimplemented
- */
- // AddKey
- // AfsSyscall
- // Alarm
- // ArchPrctl
- // Brk
- // Capget
- // Capset
- // ClockGetres
- // ClockNanosleep
- // ClockSettime
- // Clone
- // CreateModule
- // DeleteModule
- // EpollCtlOld
- // EpollPwait
- // EpollWaitOld
- // Eventfd
- // Execve
- // Fgetxattr
- // Flistxattr
- // Fork
- // Fremovexattr
- // Fsetxattr
- // Futex
- // GetKernelSyms
- // GetMempolicy
- // GetRobustList
- // GetThreadArea
- // Getitimer
- // Getpmsg
- // IoCancel
- // IoDestroy
- // IoGetevents
- // IoSetup
- // IoSubmit
- // Ioctl
- // IoprioGet
- // IoprioSet
- // KexecLoad
- // Keyctl
- // Lgetxattr
- // Llistxattr
- // LookupDcookie
- // Lremovexattr
- // Lsetxattr
- // Mbind
- // MigratePages
- // Mincore
- // ModifyLdt
- // Mount
- // MovePages
- // Mprotect
- // MqGetsetattr
- // MqNotify
- // MqOpen
- // MqTimedreceive
- // MqTimedsend
- // MqUnlink
- // Mremap
- // Msgctl
- // Msgget
- // Msgrcv
- // Msgsnd
- // Msync
- // Newfstatat
- // Nfsservctl
- // Personality
- // Poll
- // Ppoll
- // Pselect6
- // Ptrace
- // Putpmsg
- // QueryModule
- // Quotactl
- // Readahead
- // Readv
- // RemapFilePages
- // RequestKey
- // RestartSyscall
- // RtSigaction
- // RtSigpending
- // RtSigprocmask
- // RtSigqueueinfo
- // RtSigreturn
- // RtSigsuspend
- // RtSigtimedwait
- // SchedGetPriorityMax
- // SchedGetPriorityMin
- // SchedGetaffinity
- // SchedGetparam
- // SchedGetscheduler
- // SchedRrGetInterval
- // SchedSetaffinity
- // SchedSetparam
- // SchedYield
- // Security
- // Semctl
- // Semget
- // Semop
- // Semtimedop
- // SetMempolicy
- // SetRobustList
- // SetThreadArea
- // SetTidAddress
- // Shmat
- // Shmctl
- // Shmdt
- // Shmget
- // Sigaltstack
- // Signalfd
- // Swapoff
- // Swapon
- // Sysfs
- // TimerCreate
- // TimerDelete
- // TimerGetoverrun
- // TimerGettime
- // TimerSettime
- // Timerfd
- // Tkill (obsolete)
- // Tuxcall
- // Umount2
- // Uselib
- // Utimensat
- // Vfork
- // Vhangup
- // Vmsplice
- // Vserver
- // Waitid
- // _Sysctl
|