...
Run Format

Source file src/syscall/syscall_linux.go

Documentation: syscall

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

View as plain text