Source file src/syscall/syscall_linux.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Linux system calls.
     6  // This file is compiled as ordinary Go code,
     7  // but it is also input to mksyscall,
     8  // which parses the //sys lines and generates system call stubs.
     9  // Note that sometimes we use a lowercase //sys name and
    10  // wrap it in our own nicer implementation.
    11  
    12  package syscall
    13  
    14  import (
    15  	"internal/itoa"
    16  	"unsafe"
    17  )
    18  
    19  // N.B. RawSyscall6 is provided via linkname by runtime/internal/syscall.
    20  //
    21  // Errno is uintptr and thus compatible with the runtime/internal/syscall
    22  // definition.
    23  
    24  func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
    25  
    26  // Pull in entersyscall/exitsyscall for Syscall/Syscall6.
    27  //
    28  // Note that this can't be a push linkname because the runtime already has a
    29  // nameless linkname to export to assembly here and in x/sys. Additionally,
    30  // entersyscall fetches the caller PC and SP and thus can't have a wrapper
    31  // inbetween.
    32  
    33  //go:linkname runtime_entersyscall runtime.entersyscall
    34  func runtime_entersyscall()
    35  
    36  //go:linkname runtime_exitsyscall runtime.exitsyscall
    37  func runtime_exitsyscall()
    38  
    39  // N.B. For the Syscall functions below:
    40  //
    41  // //go:uintptrkeepalive because the uintptr argument may be converted pointers
    42  // that need to be kept alive in the caller (this is implied for RawSyscall6
    43  // since it has no body).
    44  //
    45  // //go:nosplit because stack copying does not account for uintptrkeepalive, so
    46  // the stack must not grow. Stack copying cannot blindly assume that all
    47  // uintptr arguments are pointers, because some values may look like pointers,
    48  // but not really be pointers, and adjusting their value would break the call.
    49  //
    50  // //go:norace, on RawSyscall, to avoid race instrumentation if RawSyscall is
    51  // called after fork, or from a signal handler.
    52  //
    53  // //go:linkname to ensure ABI wrappers are generated for external callers
    54  // (notably x/sys/unix assembly).
    55  
    56  //go:uintptrkeepalive
    57  //go:nosplit
    58  //go:norace
    59  //go:linkname RawSyscall
    60  func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
    61  	return RawSyscall6(trap, a1, a2, a3, 0, 0, 0)
    62  }
    63  
    64  //go:uintptrkeepalive
    65  //go:nosplit
    66  //go:linkname Syscall
    67  func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
    68  	runtime_entersyscall()
    69  	// N.B. Calling RawSyscall here is unsafe with atomic coverage
    70  	// instrumentation and race mode.
    71  	//
    72  	// Coverage instrumentation will add a sync/atomic call to RawSyscall.
    73  	// Race mode will add race instrumentation to sync/atomic. Race
    74  	// instrumentation requires a P, which we no longer have.
    75  	//
    76  	// RawSyscall6 is fine because it is implemented in assembly and thus
    77  	// has no coverage instrumentation.
    78  	//
    79  	// This is typically not a problem in the runtime because cmd/go avoids
    80  	// adding coverage instrumentation to the runtime in race mode.
    81  	r1, r2, err = RawSyscall6(trap, a1, a2, a3, 0, 0, 0)
    82  	runtime_exitsyscall()
    83  	return
    84  }
    85  
    86  //go:uintptrkeepalive
    87  //go:nosplit
    88  //go:linkname Syscall6
    89  func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
    90  	runtime_entersyscall()
    91  	r1, r2, err = RawSyscall6(trap, a1, a2, a3, a4, a5, a6)
    92  	runtime_exitsyscall()
    93  	return
    94  }
    95  
    96  func rawSyscallNoError(trap, a1, a2, a3 uintptr) (r1, r2 uintptr)
    97  
    98  /*
    99   * Wrapped
   100   */
   101  
   102  func Access(path string, mode uint32) (err error) {
   103  	return Faccessat(_AT_FDCWD, path, mode, 0)
   104  }
   105  
   106  func Chmod(path string, mode uint32) (err error) {
   107  	return Fchmodat(_AT_FDCWD, path, mode, 0)
   108  }
   109  
   110  func Chown(path string, uid int, gid int) (err error) {
   111  	return Fchownat(_AT_FDCWD, path, uid, gid, 0)
   112  }
   113  
   114  func Creat(path string, mode uint32) (fd int, err error) {
   115  	return Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode)
   116  }
   117  
   118  func isGroupMember(gid int) bool {
   119  	groups, err := Getgroups()
   120  	if err != nil {
   121  		return false
   122  	}
   123  
   124  	for _, g := range groups {
   125  		if g == gid {
   126  			return true
   127  		}
   128  	}
   129  	return false
   130  }
   131  
   132  //sys	faccessat(dirfd int, path string, mode uint32) (err error)
   133  
   134  func Faccessat(dirfd int, path string, mode uint32, flags int) (err error) {
   135  	if flags & ^(_AT_SYMLINK_NOFOLLOW|_AT_EACCESS) != 0 {
   136  		return EINVAL
   137  	}
   138  
   139  	// The Linux kernel faccessat system call does not take any flags.
   140  	// The glibc faccessat implements the flags itself; see
   141  	// https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/faccessat.c;hb=HEAD
   142  	// Because people naturally expect syscall.Faccessat to act
   143  	// like C faccessat, we do the same.
   144  
   145  	if flags == 0 {
   146  		return faccessat(dirfd, path, mode)
   147  	}
   148  
   149  	var st Stat_t
   150  	if err := fstatat(dirfd, path, &st, flags&_AT_SYMLINK_NOFOLLOW); err != nil {
   151  		return err
   152  	}
   153  
   154  	mode &= 7
   155  	if mode == 0 {
   156  		return nil
   157  	}
   158  
   159  	var uid int
   160  	if flags&_AT_EACCESS != 0 {
   161  		uid = Geteuid()
   162  	} else {
   163  		uid = Getuid()
   164  	}
   165  
   166  	if uid == 0 {
   167  		if mode&1 == 0 {
   168  			// Root can read and write any file.
   169  			return nil
   170  		}
   171  		if st.Mode&0111 != 0 {
   172  			// Root can execute any file that anybody can execute.
   173  			return nil
   174  		}
   175  		return EACCES
   176  	}
   177  
   178  	var fmode uint32
   179  	if uint32(uid) == st.Uid {
   180  		fmode = (st.Mode >> 6) & 7
   181  	} else {
   182  		var gid int
   183  		if flags&_AT_EACCESS != 0 {
   184  			gid = Getegid()
   185  		} else {
   186  			gid = Getgid()
   187  		}
   188  
   189  		if uint32(gid) == st.Gid || isGroupMember(int(st.Gid)) {
   190  			fmode = (st.Mode >> 3) & 7
   191  		} else {
   192  			fmode = st.Mode & 7
   193  		}
   194  	}
   195  
   196  	if fmode&mode == mode {
   197  		return nil
   198  	}
   199  
   200  	return EACCES
   201  }
   202  
   203  //sys	fchmodat(dirfd int, path string, mode uint32) (err error)
   204  
   205  func Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) {
   206  	// Linux fchmodat doesn't support the flags parameter. Mimick glibc's behavior
   207  	// and check the flags. Otherwise the mode would be applied to the symlink
   208  	// destination which is not what the user expects.
   209  	if flags&^_AT_SYMLINK_NOFOLLOW != 0 {
   210  		return EINVAL
   211  	} else if flags&_AT_SYMLINK_NOFOLLOW != 0 {
   212  		return EOPNOTSUPP
   213  	}
   214  	return fchmodat(dirfd, path, mode)
   215  }
   216  
   217  //sys	linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error)
   218  
   219  func Link(oldpath string, newpath string) (err error) {
   220  	return linkat(_AT_FDCWD, oldpath, _AT_FDCWD, newpath, 0)
   221  }
   222  
   223  func Mkdir(path string, mode uint32) (err error) {
   224  	return Mkdirat(_AT_FDCWD, path, mode)
   225  }
   226  
   227  func Mknod(path string, mode uint32, dev int) (err error) {
   228  	return Mknodat(_AT_FDCWD, path, mode, dev)
   229  }
   230  
   231  func Open(path string, mode int, perm uint32) (fd int, err error) {
   232  	return openat(_AT_FDCWD, path, mode|O_LARGEFILE, perm)
   233  }
   234  
   235  //sys	openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
   236  
   237  func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
   238  	return openat(dirfd, path, flags|O_LARGEFILE, mode)
   239  }
   240  
   241  func Pipe(p []int) error {
   242  	return Pipe2(p, 0)
   243  }
   244  
   245  //sysnb pipe2(p *[2]_C_int, flags int) (err error)
   246  
   247  func Pipe2(p []int, flags int) error {
   248  	if len(p) != 2 {
   249  		return EINVAL
   250  	}
   251  	var pp [2]_C_int
   252  	err := pipe2(&pp, flags)
   253  	if err == nil {
   254  		p[0] = int(pp[0])
   255  		p[1] = int(pp[1])
   256  	}
   257  	return err
   258  }
   259  
   260  //sys	readlinkat(dirfd int, path string, buf []byte) (n int, err error)
   261  
   262  func Readlink(path string, buf []byte) (n int, err error) {
   263  	return readlinkat(_AT_FDCWD, path, buf)
   264  }
   265  
   266  func Rename(oldpath string, newpath string) (err error) {
   267  	return Renameat(_AT_FDCWD, oldpath, _AT_FDCWD, newpath)
   268  }
   269  
   270  func Rmdir(path string) error {
   271  	return unlinkat(_AT_FDCWD, path, _AT_REMOVEDIR)
   272  }
   273  
   274  //sys	symlinkat(oldpath string, newdirfd int, newpath string) (err error)
   275  
   276  func Symlink(oldpath string, newpath string) (err error) {
   277  	return symlinkat(oldpath, _AT_FDCWD, newpath)
   278  }
   279  
   280  func Unlink(path string) error {
   281  	return unlinkat(_AT_FDCWD, path, 0)
   282  }
   283  
   284  //sys	unlinkat(dirfd int, path string, flags int) (err error)
   285  
   286  func Unlinkat(dirfd int, path string) error {
   287  	return unlinkat(dirfd, path, 0)
   288  }
   289  
   290  func Utimes(path string, tv []Timeval) (err error) {
   291  	if len(tv) != 2 {
   292  		return EINVAL
   293  	}
   294  	return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
   295  }
   296  
   297  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flag int) (err error)
   298  
   299  func UtimesNano(path string, ts []Timespec) (err error) {
   300  	if len(ts) != 2 {
   301  		return EINVAL
   302  	}
   303  	return utimensat(_AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
   304  }
   305  
   306  func Futimesat(dirfd int, path string, tv []Timeval) (err error) {
   307  	if len(tv) != 2 {
   308  		return EINVAL
   309  	}
   310  	return futimesat(dirfd, path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
   311  }
   312  
   313  func Futimes(fd int, tv []Timeval) (err error) {
   314  	// Believe it or not, this is the best we can do on Linux
   315  	// (and is what glibc does).
   316  	return Utimes("/proc/self/fd/"+itoa.Itoa(fd), tv)
   317  }
   318  
   319  const ImplementsGetwd = true
   320  
   321  //sys	Getcwd(buf []byte) (n int, err error)
   322  
   323  func Getwd() (wd string, err error) {
   324  	var buf [PathMax]byte
   325  	n, err := Getcwd(buf[0:])
   326  	if err != nil {
   327  		return "", err
   328  	}
   329  	// Getcwd returns the number of bytes written to buf, including the NUL.
   330  	if n < 1 || n > len(buf) || buf[n-1] != 0 {
   331  		return "", EINVAL
   332  	}
   333  	// In some cases, Linux can return a path that starts with the
   334  	// "(unreachable)" prefix, which can potentially be a valid relative
   335  	// path. To work around that, return ENOENT if path is not absolute.
   336  	if buf[0] != '/' {
   337  		return "", ENOENT
   338  	}
   339  
   340  	return string(buf[0 : n-1]), nil
   341  }
   342  
   343  func Getgroups() (gids []int, err error) {
   344  	n, err := getgroups(0, nil)
   345  	if err != nil {
   346  		return nil, err
   347  	}
   348  	if n == 0 {
   349  		return nil, nil
   350  	}
   351  
   352  	// Sanity check group count. Max is 1<<16 on Linux.
   353  	if n < 0 || n > 1<<20 {
   354  		return nil, EINVAL
   355  	}
   356  
   357  	a := make([]_Gid_t, n)
   358  	n, err = getgroups(n, &a[0])
   359  	if err != nil {
   360  		return nil, err
   361  	}
   362  	gids = make([]int, n)
   363  	for i, v := range a[0:n] {
   364  		gids[i] = int(v)
   365  	}
   366  	return
   367  }
   368  
   369  var cgo_libc_setgroups unsafe.Pointer // non-nil if cgo linked.
   370  
   371  func Setgroups(gids []int) (err error) {
   372  	n := uintptr(len(gids))
   373  	if n == 0 {
   374  		if cgo_libc_setgroups == nil {
   375  			if _, _, e1 := AllThreadsSyscall(_SYS_setgroups, 0, 0, 0); e1 != 0 {
   376  				err = errnoErr(e1)
   377  			}
   378  			return
   379  		}
   380  		if ret := cgocaller(cgo_libc_setgroups, 0, 0); ret != 0 {
   381  			err = errnoErr(Errno(ret))
   382  		}
   383  		return
   384  	}
   385  
   386  	a := make([]_Gid_t, len(gids))
   387  	for i, v := range gids {
   388  		a[i] = _Gid_t(v)
   389  	}
   390  	if cgo_libc_setgroups == nil {
   391  		if _, _, e1 := AllThreadsSyscall(_SYS_setgroups, n, uintptr(unsafe.Pointer(&a[0])), 0); e1 != 0 {
   392  			err = errnoErr(e1)
   393  		}
   394  		return
   395  	}
   396  	if ret := cgocaller(cgo_libc_setgroups, n, uintptr(unsafe.Pointer(&a[0]))); ret != 0 {
   397  		err = errnoErr(Errno(ret))
   398  	}
   399  	return
   400  }
   401  
   402  type WaitStatus uint32
   403  
   404  // Wait status is 7 bits at bottom, either 0 (exited),
   405  // 0x7F (stopped), or a signal number that caused an exit.
   406  // The 0x80 bit is whether there was a core dump.
   407  // An extra number (exit code, signal causing a stop)
   408  // is in the high bits. At least that's the idea.
   409  // There are various irregularities. For example, the
   410  // "continued" status is 0xFFFF, distinguishing itself
   411  // from stopped via the core dump bit.
   412  
   413  const (
   414  	mask    = 0x7F
   415  	core    = 0x80
   416  	exited  = 0x00
   417  	stopped = 0x7F
   418  	shift   = 8
   419  )
   420  
   421  func (w WaitStatus) Exited() bool { return w&mask == exited }
   422  
   423  func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited }
   424  
   425  func (w WaitStatus) Stopped() bool { return w&0xFF == stopped }
   426  
   427  func (w WaitStatus) Continued() bool { return w == 0xFFFF }
   428  
   429  func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
   430  
   431  func (w WaitStatus) ExitStatus() int {
   432  	if !w.Exited() {
   433  		return -1
   434  	}
   435  	return int(w>>shift) & 0xFF
   436  }
   437  
   438  func (w WaitStatus) Signal() Signal {
   439  	if !w.Signaled() {
   440  		return -1
   441  	}
   442  	return Signal(w & mask)
   443  }
   444  
   445  func (w WaitStatus) StopSignal() Signal {
   446  	if !w.Stopped() {
   447  		return -1
   448  	}
   449  	return Signal(w>>shift) & 0xFF
   450  }
   451  
   452  func (w WaitStatus) TrapCause() int {
   453  	if w.StopSignal() != SIGTRAP {
   454  		return -1
   455  	}
   456  	return int(w>>shift) >> 8
   457  }
   458  
   459  //sys	wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
   460  
   461  func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
   462  	var status _C_int
   463  	wpid, err = wait4(pid, &status, options, rusage)
   464  	if wstatus != nil {
   465  		*wstatus = WaitStatus(status)
   466  	}
   467  	return
   468  }
   469  
   470  func Mkfifo(path string, mode uint32) (err error) {
   471  	return Mknod(path, mode|S_IFIFO, 0)
   472  }
   473  
   474  func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
   475  	if sa.Port < 0 || sa.Port > 0xFFFF {
   476  		return nil, 0, EINVAL
   477  	}
   478  	sa.raw.Family = AF_INET
   479  	p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
   480  	p[0] = byte(sa.Port >> 8)
   481  	p[1] = byte(sa.Port)
   482  	sa.raw.Addr = sa.Addr
   483  	return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
   484  }
   485  
   486  func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
   487  	if sa.Port < 0 || sa.Port > 0xFFFF {
   488  		return nil, 0, EINVAL
   489  	}
   490  	sa.raw.Family = AF_INET6
   491  	p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
   492  	p[0] = byte(sa.Port >> 8)
   493  	p[1] = byte(sa.Port)
   494  	sa.raw.Scope_id = sa.ZoneId
   495  	sa.raw.Addr = sa.Addr
   496  	return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
   497  }
   498  
   499  func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
   500  	name := sa.Name
   501  	n := len(name)
   502  	if n > len(sa.raw.Path) {
   503  		return nil, 0, EINVAL
   504  	}
   505  	if n == len(sa.raw.Path) && name[0] != '@' {
   506  		return nil, 0, EINVAL
   507  	}
   508  	sa.raw.Family = AF_UNIX
   509  	for i := 0; i < n; i++ {
   510  		sa.raw.Path[i] = int8(name[i])
   511  	}
   512  	// length is family (uint16), name, NUL.
   513  	sl := _Socklen(2)
   514  	if n > 0 {
   515  		sl += _Socklen(n) + 1
   516  	}
   517  	if sa.raw.Path[0] == '@' {
   518  		sa.raw.Path[0] = 0
   519  		// Don't count trailing NUL for abstract address.
   520  		sl--
   521  	}
   522  
   523  	return unsafe.Pointer(&sa.raw), sl, nil
   524  }
   525  
   526  type SockaddrLinklayer struct {
   527  	Protocol uint16
   528  	Ifindex  int
   529  	Hatype   uint16
   530  	Pkttype  uint8
   531  	Halen    uint8
   532  	Addr     [8]byte
   533  	raw      RawSockaddrLinklayer
   534  }
   535  
   536  func (sa *SockaddrLinklayer) sockaddr() (unsafe.Pointer, _Socklen, error) {
   537  	if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
   538  		return nil, 0, EINVAL
   539  	}
   540  	sa.raw.Family = AF_PACKET
   541  	sa.raw.Protocol = sa.Protocol
   542  	sa.raw.Ifindex = int32(sa.Ifindex)
   543  	sa.raw.Hatype = sa.Hatype
   544  	sa.raw.Pkttype = sa.Pkttype
   545  	sa.raw.Halen = sa.Halen
   546  	sa.raw.Addr = sa.Addr
   547  	return unsafe.Pointer(&sa.raw), SizeofSockaddrLinklayer, nil
   548  }
   549  
   550  type SockaddrNetlink struct {
   551  	Family uint16
   552  	Pad    uint16
   553  	Pid    uint32
   554  	Groups uint32
   555  	raw    RawSockaddrNetlink
   556  }
   557  
   558  func (sa *SockaddrNetlink) sockaddr() (unsafe.Pointer, _Socklen, error) {
   559  	sa.raw.Family = AF_NETLINK
   560  	sa.raw.Pad = sa.Pad
   561  	sa.raw.Pid = sa.Pid
   562  	sa.raw.Groups = sa.Groups
   563  	return unsafe.Pointer(&sa.raw), SizeofSockaddrNetlink, nil
   564  }
   565  
   566  func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
   567  	switch rsa.Addr.Family {
   568  	case AF_NETLINK:
   569  		pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa))
   570  		sa := new(SockaddrNetlink)
   571  		sa.Family = pp.Family
   572  		sa.Pad = pp.Pad
   573  		sa.Pid = pp.Pid
   574  		sa.Groups = pp.Groups
   575  		return sa, nil
   576  
   577  	case AF_PACKET:
   578  		pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa))
   579  		sa := new(SockaddrLinklayer)
   580  		sa.Protocol = pp.Protocol
   581  		sa.Ifindex = int(pp.Ifindex)
   582  		sa.Hatype = pp.Hatype
   583  		sa.Pkttype = pp.Pkttype
   584  		sa.Halen = pp.Halen
   585  		sa.Addr = pp.Addr
   586  		return sa, nil
   587  
   588  	case AF_UNIX:
   589  		pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
   590  		sa := new(SockaddrUnix)
   591  		if pp.Path[0] == 0 {
   592  			// "Abstract" Unix domain socket.
   593  			// Rewrite leading NUL as @ for textual display.
   594  			// (This is the standard convention.)
   595  			// Not friendly to overwrite in place,
   596  			// but the callers below don't care.
   597  			pp.Path[0] = '@'
   598  		}
   599  
   600  		// Assume path ends at NUL.
   601  		// This is not technically the Linux semantics for
   602  		// abstract Unix domain sockets--they are supposed
   603  		// to be uninterpreted fixed-size binary blobs--but
   604  		// everyone uses this convention.
   605  		n := 0
   606  		for n < len(pp.Path) && pp.Path[n] != 0 {
   607  			n++
   608  		}
   609  		bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
   610  		sa.Name = string(bytes)
   611  		return sa, nil
   612  
   613  	case AF_INET:
   614  		pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
   615  		sa := new(SockaddrInet4)
   616  		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
   617  		sa.Port = int(p[0])<<8 + int(p[1])
   618  		sa.Addr = pp.Addr
   619  		return sa, nil
   620  
   621  	case AF_INET6:
   622  		pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
   623  		sa := new(SockaddrInet6)
   624  		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
   625  		sa.Port = int(p[0])<<8 + int(p[1])
   626  		sa.ZoneId = pp.Scope_id
   627  		sa.Addr = pp.Addr
   628  		return sa, nil
   629  	}
   630  	return nil, EAFNOSUPPORT
   631  }
   632  
   633  func Accept(fd int) (nfd int, sa Sockaddr, err error) {
   634  	var rsa RawSockaddrAny
   635  	var len _Socklen = SizeofSockaddrAny
   636  	nfd, err = accept4(fd, &rsa, &len, 0)
   637  	if err != nil {
   638  		return
   639  	}
   640  	sa, err = anyToSockaddr(&rsa)
   641  	if err != nil {
   642  		Close(nfd)
   643  		nfd = 0
   644  	}
   645  	return
   646  }
   647  
   648  func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) {
   649  	var rsa RawSockaddrAny
   650  	var len _Socklen = SizeofSockaddrAny
   651  	nfd, err = accept4(fd, &rsa, &len, flags)
   652  	if err != nil {
   653  		return
   654  	}
   655  	if len > SizeofSockaddrAny {
   656  		panic("RawSockaddrAny too small")
   657  	}
   658  	sa, err = anyToSockaddr(&rsa)
   659  	if err != nil {
   660  		Close(nfd)
   661  		nfd = 0
   662  	}
   663  	return
   664  }
   665  
   666  func Getsockname(fd int) (sa Sockaddr, err error) {
   667  	var rsa RawSockaddrAny
   668  	var len _Socklen = SizeofSockaddrAny
   669  	if err = getsockname(fd, &rsa, &len); err != nil {
   670  		return
   671  	}
   672  	return anyToSockaddr(&rsa)
   673  }
   674  
   675  func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
   676  	vallen := _Socklen(4)
   677  	err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
   678  	return value, err
   679  }
   680  
   681  func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
   682  	var value IPMreq
   683  	vallen := _Socklen(SizeofIPMreq)
   684  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   685  	return &value, err
   686  }
   687  
   688  func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
   689  	var value IPMreqn
   690  	vallen := _Socklen(SizeofIPMreqn)
   691  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   692  	return &value, err
   693  }
   694  
   695  func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
   696  	var value IPv6Mreq
   697  	vallen := _Socklen(SizeofIPv6Mreq)
   698  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   699  	return &value, err
   700  }
   701  
   702  func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
   703  	var value IPv6MTUInfo
   704  	vallen := _Socklen(SizeofIPv6MTUInfo)
   705  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   706  	return &value, err
   707  }
   708  
   709  func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
   710  	var value ICMPv6Filter
   711  	vallen := _Socklen(SizeofICMPv6Filter)
   712  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   713  	return &value, err
   714  }
   715  
   716  func GetsockoptUcred(fd, level, opt int) (*Ucred, error) {
   717  	var value Ucred
   718  	vallen := _Socklen(SizeofUcred)
   719  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   720  	return &value, err
   721  }
   722  
   723  func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
   724  	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq))
   725  }
   726  
   727  func recvmsgRaw(fd int, p, oob []byte, flags int, rsa *RawSockaddrAny) (n, oobn int, recvflags int, err error) {
   728  	var msg Msghdr
   729  	msg.Name = (*byte)(unsafe.Pointer(rsa))
   730  	msg.Namelen = uint32(SizeofSockaddrAny)
   731  	var iov Iovec
   732  	if len(p) > 0 {
   733  		iov.Base = &p[0]
   734  		iov.SetLen(len(p))
   735  	}
   736  	var dummy byte
   737  	if len(oob) > 0 {
   738  		if len(p) == 0 {
   739  			var sockType int
   740  			sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
   741  			if err != nil {
   742  				return
   743  			}
   744  			// receive at least one normal byte
   745  			if sockType != SOCK_DGRAM {
   746  				iov.Base = &dummy
   747  				iov.SetLen(1)
   748  			}
   749  		}
   750  		msg.Control = &oob[0]
   751  		msg.SetControllen(len(oob))
   752  	}
   753  	msg.Iov = &iov
   754  	msg.Iovlen = 1
   755  	if n, err = recvmsg(fd, &msg, flags); err != nil {
   756  		return
   757  	}
   758  	oobn = int(msg.Controllen)
   759  	recvflags = int(msg.Flags)
   760  	return
   761  }
   762  
   763  func sendmsgN(fd int, p, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) {
   764  	var msg Msghdr
   765  	msg.Name = (*byte)(ptr)
   766  	msg.Namelen = uint32(salen)
   767  	var iov Iovec
   768  	if len(p) > 0 {
   769  		iov.Base = &p[0]
   770  		iov.SetLen(len(p))
   771  	}
   772  	var dummy byte
   773  	if len(oob) > 0 {
   774  		if len(p) == 0 {
   775  			var sockType int
   776  			sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
   777  			if err != nil {
   778  				return 0, err
   779  			}
   780  			// send at least one normal byte
   781  			if sockType != SOCK_DGRAM {
   782  				iov.Base = &dummy
   783  				iov.SetLen(1)
   784  			}
   785  		}
   786  		msg.Control = &oob[0]
   787  		msg.SetControllen(len(oob))
   788  	}
   789  	msg.Iov = &iov
   790  	msg.Iovlen = 1
   791  	if n, err = sendmsg(fd, &msg, flags); err != nil {
   792  		return 0, err
   793  	}
   794  	if len(oob) > 0 && len(p) == 0 {
   795  		n = 0
   796  	}
   797  	return n, nil
   798  }
   799  
   800  // BindToDevice binds the socket associated with fd to device.
   801  func BindToDevice(fd int, device string) (err error) {
   802  	return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
   803  }
   804  
   805  //sys	ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
   806  
   807  func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) {
   808  	// The peek requests are machine-size oriented, so we wrap it
   809  	// to retrieve arbitrary-length data.
   810  
   811  	// The ptrace syscall differs from glibc's ptrace.
   812  	// Peeks returns the word in *data, not as the return value.
   813  
   814  	var buf [sizeofPtr]byte
   815  
   816  	// Leading edge. PEEKTEXT/PEEKDATA don't require aligned
   817  	// access (PEEKUSER warns that it might), but if we don't
   818  	// align our reads, we might straddle an unmapped page
   819  	// boundary and not get the bytes leading up to the page
   820  	// boundary.
   821  	n := 0
   822  	if addr%sizeofPtr != 0 {
   823  		err = ptrace(req, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
   824  		if err != nil {
   825  			return 0, err
   826  		}
   827  		n += copy(out, buf[addr%sizeofPtr:])
   828  		out = out[n:]
   829  	}
   830  
   831  	// Remainder.
   832  	for len(out) > 0 {
   833  		// We use an internal buffer to guarantee alignment.
   834  		// It's not documented if this is necessary, but we're paranoid.
   835  		err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
   836  		if err != nil {
   837  			return n, err
   838  		}
   839  		copied := copy(out, buf[0:])
   840  		n += copied
   841  		out = out[copied:]
   842  	}
   843  
   844  	return n, nil
   845  }
   846  
   847  func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) {
   848  	return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out)
   849  }
   850  
   851  func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
   852  	return ptracePeek(PTRACE_PEEKDATA, pid, addr, out)
   853  }
   854  
   855  func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) {
   856  	// As for ptracePeek, we need to align our accesses to deal
   857  	// with the possibility of straddling an invalid page.
   858  
   859  	// Leading edge.
   860  	n := 0
   861  	if addr%sizeofPtr != 0 {
   862  		var buf [sizeofPtr]byte
   863  		err = ptrace(peekReq, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
   864  		if err != nil {
   865  			return 0, err
   866  		}
   867  		n += copy(buf[addr%sizeofPtr:], data)
   868  		word := *((*uintptr)(unsafe.Pointer(&buf[0])))
   869  		err = ptrace(pokeReq, pid, addr-addr%sizeofPtr, word)
   870  		if err != nil {
   871  			return 0, err
   872  		}
   873  		data = data[n:]
   874  	}
   875  
   876  	// Interior.
   877  	for len(data) > sizeofPtr {
   878  		word := *((*uintptr)(unsafe.Pointer(&data[0])))
   879  		err = ptrace(pokeReq, pid, addr+uintptr(n), word)
   880  		if err != nil {
   881  			return n, err
   882  		}
   883  		n += sizeofPtr
   884  		data = data[sizeofPtr:]
   885  	}
   886  
   887  	// Trailing edge.
   888  	if len(data) > 0 {
   889  		var buf [sizeofPtr]byte
   890  		err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
   891  		if err != nil {
   892  			return n, err
   893  		}
   894  		copy(buf[0:], data)
   895  		word := *((*uintptr)(unsafe.Pointer(&buf[0])))
   896  		err = ptrace(pokeReq, pid, addr+uintptr(n), word)
   897  		if err != nil {
   898  			return n, err
   899  		}
   900  		n += len(data)
   901  	}
   902  
   903  	return n, nil
   904  }
   905  
   906  func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
   907  	return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data)
   908  }
   909  
   910  func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) {
   911  	return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data)
   912  }
   913  
   914  func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) {
   915  	return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout)))
   916  }
   917  
   918  func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) {
   919  	return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs)))
   920  }
   921  
   922  func PtraceSetOptions(pid int, options int) (err error) {
   923  	return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options))
   924  }
   925  
   926  func PtraceGetEventMsg(pid int) (msg uint, err error) {
   927  	var data _C_long
   928  	err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data)))
   929  	msg = uint(data)
   930  	return
   931  }
   932  
   933  func PtraceCont(pid int, signal int) (err error) {
   934  	return ptrace(PTRACE_CONT, pid, 0, uintptr(signal))
   935  }
   936  
   937  func PtraceSyscall(pid int, signal int) (err error) {
   938  	return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal))
   939  }
   940  
   941  func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) }
   942  
   943  func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) }
   944  
   945  func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) }
   946  
   947  //sys	reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error)
   948  
   949  func Reboot(cmd int) (err error) {
   950  	return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "")
   951  }
   952  
   953  func ReadDirent(fd int, buf []byte) (n int, err error) {
   954  	return Getdents(fd, buf)
   955  }
   956  
   957  func direntIno(buf []byte) (uint64, bool) {
   958  	return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
   959  }
   960  
   961  func direntReclen(buf []byte) (uint64, bool) {
   962  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
   963  }
   964  
   965  func direntNamlen(buf []byte) (uint64, bool) {
   966  	reclen, ok := direntReclen(buf)
   967  	if !ok {
   968  		return 0, false
   969  	}
   970  	return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
   971  }
   972  
   973  //sys	mount(source string, target string, fstype string, flags uintptr, data *byte) (err error)
   974  
   975  func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) {
   976  	// Certain file systems get rather angry and EINVAL if you give
   977  	// them an empty string of data, rather than NULL.
   978  	if data == "" {
   979  		return mount(source, target, fstype, flags, nil)
   980  	}
   981  	datap, err := BytePtrFromString(data)
   982  	if err != nil {
   983  		return err
   984  	}
   985  	return mount(source, target, fstype, flags, datap)
   986  }
   987  
   988  // Sendto
   989  // Recvfrom
   990  // Socketpair
   991  
   992  /*
   993   * Direct access
   994   */
   995  //sys	Acct(path string) (err error)
   996  //sys	Adjtimex(buf *Timex) (state int, err error)
   997  //sys	Chdir(path string) (err error)
   998  //sys	Chroot(path string) (err error)
   999  //sys	Close(fd int) (err error)
  1000  //sys	Dup(oldfd int) (fd int, err error)
  1001  //sys	Dup3(oldfd int, newfd int, flags int) (err error)
  1002  //sysnb	EpollCreate1(flag int) (fd int, err error)
  1003  //sysnb	EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
  1004  //sys	Fallocate(fd int, mode uint32, off int64, len int64) (err error)
  1005  //sys	Fchdir(fd int) (err error)
  1006  //sys	Fchmod(fd int, mode uint32) (err error)
  1007  //sys	Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  1008  //sys	fcntl(fd int, cmd int, arg int) (val int, err error)
  1009  //sys	Fdatasync(fd int) (err error)
  1010  //sys	Flock(fd int, how int) (err error)
  1011  //sys	Fsync(fd int) (err error)
  1012  //sys	Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64
  1013  //sysnb	Getpgid(pid int) (pgid int, err error)
  1014  
  1015  func Getpgrp() (pid int) {
  1016  	pid, _ = Getpgid(0)
  1017  	return
  1018  }
  1019  
  1020  //sysnb	Getpid() (pid int)
  1021  //sysnb	Getppid() (ppid int)
  1022  //sys	Getpriority(which int, who int) (prio int, err error)
  1023  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
  1024  //sysnb	Gettid() (tid int)
  1025  //sys	Getxattr(path string, attr string, dest []byte) (sz int, err error)
  1026  //sys	InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error)
  1027  //sysnb	InotifyInit1(flags int) (fd int, err error)
  1028  //sysnb	InotifyRmWatch(fd int, watchdesc uint32) (success int, err error)
  1029  //sysnb	Kill(pid int, sig Signal) (err error)
  1030  //sys	Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG
  1031  //sys	Listxattr(path string, dest []byte) (sz int, err error)
  1032  //sys	Mkdirat(dirfd int, path string, mode uint32) (err error)
  1033  //sys	Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  1034  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
  1035  //sys	PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT
  1036  //sysnb prlimit(pid int, resource int, newlimit *Rlimit, old *Rlimit) (err error) = SYS_PRLIMIT64
  1037  //sys	read(fd int, p []byte) (n int, err error)
  1038  //sys	Removexattr(path string, attr string) (err error)
  1039  //sys	Setdomainname(p []byte) (err error)
  1040  //sys	Sethostname(p []byte) (err error)
  1041  //sysnb	Setpgid(pid int, pgid int) (err error)
  1042  //sysnb	Setsid() (pid int, err error)
  1043  //sysnb	Settimeofday(tv *Timeval) (err error)
  1044  
  1045  // Provided by runtime.syscall_runtime_doAllThreadsSyscall which stops the
  1046  // world and invokes the syscall on each OS thread. Once this function returns,
  1047  // all threads are in sync.
  1048  //
  1049  //go:uintptrescapes
  1050  func runtime_doAllThreadsSyscall(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr)
  1051  
  1052  // AllThreadsSyscall performs a syscall on each OS thread of the Go
  1053  // runtime. It first invokes the syscall on one thread. Should that
  1054  // invocation fail, it returns immediately with the error status.
  1055  // Otherwise, it invokes the syscall on all of the remaining threads
  1056  // in parallel. It will terminate the program if it observes any
  1057  // invoked syscall's return value differs from that of the first
  1058  // invocation.
  1059  //
  1060  // AllThreadsSyscall is intended for emulating simultaneous
  1061  // process-wide state changes that require consistently modifying
  1062  // per-thread state of the Go runtime.
  1063  //
  1064  // AllThreadsSyscall is unaware of any threads that are launched
  1065  // explicitly by cgo linked code, so the function always returns
  1066  // ENOTSUP in binaries that use cgo.
  1067  //
  1068  //go:uintptrescapes
  1069  func AllThreadsSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
  1070  	if cgo_libc_setegid != nil {
  1071  		return minus1, minus1, ENOTSUP
  1072  	}
  1073  	r1, r2, errno := runtime_doAllThreadsSyscall(trap, a1, a2, a3, 0, 0, 0)
  1074  	return r1, r2, Errno(errno)
  1075  }
  1076  
  1077  // AllThreadsSyscall6 is like AllThreadsSyscall, but extended to six
  1078  // arguments.
  1079  //
  1080  //go:uintptrescapes
  1081  func AllThreadsSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
  1082  	if cgo_libc_setegid != nil {
  1083  		return minus1, minus1, ENOTSUP
  1084  	}
  1085  	r1, r2, errno := runtime_doAllThreadsSyscall(trap, a1, a2, a3, a4, a5, a6)
  1086  	return r1, r2, Errno(errno)
  1087  }
  1088  
  1089  // linked by runtime.cgocall.go
  1090  //
  1091  //go:uintptrescapes
  1092  func cgocaller(unsafe.Pointer, ...uintptr) uintptr
  1093  
  1094  var cgo_libc_setegid unsafe.Pointer // non-nil if cgo linked.
  1095  
  1096  const minus1 = ^uintptr(0)
  1097  
  1098  func Setegid(egid int) (err error) {
  1099  	if cgo_libc_setegid == nil {
  1100  		if _, _, e1 := AllThreadsSyscall(SYS_SETRESGID, minus1, uintptr(egid), minus1); e1 != 0 {
  1101  			err = errnoErr(e1)
  1102  		}
  1103  	} else if ret := cgocaller(cgo_libc_setegid, uintptr(egid)); ret != 0 {
  1104  		err = errnoErr(Errno(ret))
  1105  	}
  1106  	return
  1107  }
  1108  
  1109  var cgo_libc_seteuid unsafe.Pointer // non-nil if cgo linked.
  1110  
  1111  func Seteuid(euid int) (err error) {
  1112  	if cgo_libc_seteuid == nil {
  1113  		if _, _, e1 := AllThreadsSyscall(SYS_SETRESUID, minus1, uintptr(euid), minus1); e1 != 0 {
  1114  			err = errnoErr(e1)
  1115  		}
  1116  	} else if ret := cgocaller(cgo_libc_seteuid, uintptr(euid)); ret != 0 {
  1117  		err = errnoErr(Errno(ret))
  1118  	}
  1119  	return
  1120  }
  1121  
  1122  var cgo_libc_setgid unsafe.Pointer // non-nil if cgo linked.
  1123  
  1124  func Setgid(gid int) (err error) {
  1125  	if cgo_libc_setgid == nil {
  1126  		if _, _, e1 := AllThreadsSyscall(sys_SETGID, uintptr(gid), 0, 0); e1 != 0 {
  1127  			err = errnoErr(e1)
  1128  		}
  1129  	} else if ret := cgocaller(cgo_libc_setgid, uintptr(gid)); ret != 0 {
  1130  		err = errnoErr(Errno(ret))
  1131  	}
  1132  	return
  1133  }
  1134  
  1135  var cgo_libc_setregid unsafe.Pointer // non-nil if cgo linked.
  1136  
  1137  func Setregid(rgid, egid int) (err error) {
  1138  	if cgo_libc_setregid == nil {
  1139  		if _, _, e1 := AllThreadsSyscall(sys_SETREGID, uintptr(rgid), uintptr(egid), 0); e1 != 0 {
  1140  			err = errnoErr(e1)
  1141  		}
  1142  	} else if ret := cgocaller(cgo_libc_setregid, uintptr(rgid), uintptr(egid)); ret != 0 {
  1143  		err = errnoErr(Errno(ret))
  1144  	}
  1145  	return
  1146  }
  1147  
  1148  var cgo_libc_setresgid unsafe.Pointer // non-nil if cgo linked.
  1149  
  1150  func Setresgid(rgid, egid, sgid int) (err error) {
  1151  	if cgo_libc_setresgid == nil {
  1152  		if _, _, e1 := AllThreadsSyscall(sys_SETRESGID, uintptr(rgid), uintptr(egid), uintptr(sgid)); e1 != 0 {
  1153  			err = errnoErr(e1)
  1154  		}
  1155  	} else if ret := cgocaller(cgo_libc_setresgid, uintptr(rgid), uintptr(egid), uintptr(sgid)); ret != 0 {
  1156  		err = errnoErr(Errno(ret))
  1157  	}
  1158  	return
  1159  }
  1160  
  1161  var cgo_libc_setresuid unsafe.Pointer // non-nil if cgo linked.
  1162  
  1163  func Setresuid(ruid, euid, suid int) (err error) {
  1164  	if cgo_libc_setresuid == nil {
  1165  		if _, _, e1 := AllThreadsSyscall(sys_SETRESUID, uintptr(ruid), uintptr(euid), uintptr(suid)); e1 != 0 {
  1166  			err = errnoErr(e1)
  1167  		}
  1168  	} else if ret := cgocaller(cgo_libc_setresuid, uintptr(ruid), uintptr(euid), uintptr(suid)); ret != 0 {
  1169  		err = errnoErr(Errno(ret))
  1170  	}
  1171  	return
  1172  }
  1173  
  1174  var cgo_libc_setreuid unsafe.Pointer // non-nil if cgo linked.
  1175  
  1176  func Setreuid(ruid, euid int) (err error) {
  1177  	if cgo_libc_setreuid == nil {
  1178  		if _, _, e1 := AllThreadsSyscall(sys_SETREUID, uintptr(ruid), uintptr(euid), 0); e1 != 0 {
  1179  			err = errnoErr(e1)
  1180  		}
  1181  	} else if ret := cgocaller(cgo_libc_setreuid, uintptr(ruid), uintptr(euid)); ret != 0 {
  1182  		err = errnoErr(Errno(ret))
  1183  	}
  1184  	return
  1185  }
  1186  
  1187  var cgo_libc_setuid unsafe.Pointer // non-nil if cgo linked.
  1188  
  1189  func Setuid(uid int) (err error) {
  1190  	if cgo_libc_setuid == nil {
  1191  		if _, _, e1 := AllThreadsSyscall(sys_SETUID, uintptr(uid), 0, 0); e1 != 0 {
  1192  			err = errnoErr(e1)
  1193  		}
  1194  	} else if ret := cgocaller(cgo_libc_setuid, uintptr(uid)); ret != 0 {
  1195  		err = errnoErr(Errno(ret))
  1196  	}
  1197  	return
  1198  }
  1199  
  1200  //sys	Setpriority(which int, who int, prio int) (err error)
  1201  //sys	Setxattr(path string, attr string, data []byte, flags int) (err error)
  1202  //sys	Sync()
  1203  //sysnb	Sysinfo(info *Sysinfo_t) (err error)
  1204  //sys	Tee(rfd int, wfd int, len int, flags int) (n int64, err error)
  1205  //sysnb	Tgkill(tgid int, tid int, sig Signal) (err error)
  1206  //sysnb	Times(tms *Tms) (ticks uintptr, err error)
  1207  //sysnb	Umask(mask int) (oldmask int)
  1208  //sysnb	Uname(buf *Utsname) (err error)
  1209  //sys	Unmount(target string, flags int) (err error) = SYS_UMOUNT2
  1210  //sys	Unshare(flags int) (err error)
  1211  //sys	write(fd int, p []byte) (n int, err error)
  1212  //sys	exitThread(code int) (err error) = SYS_EXIT
  1213  //sys	readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ
  1214  //sys	writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE
  1215  
  1216  // mmap varies by architecture; see syscall_linux_*.go.
  1217  //sys	munmap(addr uintptr, length uintptr) (err error)
  1218  
  1219  var mapper = &mmapper{
  1220  	active: make(map[*byte][]byte),
  1221  	mmap:   mmap,
  1222  	munmap: munmap,
  1223  }
  1224  
  1225  func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  1226  	return mapper.Mmap(fd, offset, length, prot, flags)
  1227  }
  1228  
  1229  func Munmap(b []byte) (err error) {
  1230  	return mapper.Munmap(b)
  1231  }
  1232  
  1233  //sys	Madvise(b []byte, advice int) (err error)
  1234  //sys	Mprotect(b []byte, prot int) (err error)
  1235  //sys	Mlock(b []byte) (err error)
  1236  //sys	Munlock(b []byte) (err error)
  1237  //sys	Mlockall(flags int) (err error)
  1238  //sys	Munlockall() (err error)
  1239  

View as plain text