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

View as plain text