Run Format

Source file src/pkg/syscall/syscall_linux.go

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

View as plain text