Black Lives Matter. Support the Equal Justice Initiative.

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

View as plain text