github.com/giovannyortegon/go@v0.0.0-20220115155912-8890063f5bdd/src/pkg/mod/golang.org/x/sys@v0.0.0-20210927094055-39ccf1dd6fa6/unix/syscall_unix.go (about)

     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  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     7  
     8  package unix
     9  
    10  import (
    11  	"bytes"
    12  	"sort"
    13  	"sync"
    14  	"syscall"
    15  	"unsafe"
    16  
    17  	"golang.org/x/sys/internal/unsafeheader"
    18  )
    19  
    20  var (
    21  	Stdin  = 0
    22  	Stdout = 1
    23  	Stderr = 2
    24  )
    25  
    26  // Do the interface allocations only once for common
    27  // Errno values.
    28  var (
    29  	errEAGAIN error = syscall.EAGAIN
    30  	errEINVAL error = syscall.EINVAL
    31  	errENOENT error = syscall.ENOENT
    32  )
    33  
    34  var (
    35  	signalNameMapOnce sync.Once
    36  	signalNameMap     map[string]syscall.Signal
    37  )
    38  
    39  // errnoErr returns common boxed Errno values, to prevent
    40  // allocations at runtime.
    41  func errnoErr(e syscall.Errno) error {
    42  	switch e {
    43  	case 0:
    44  		return nil
    45  	case EAGAIN:
    46  		return errEAGAIN
    47  	case EINVAL:
    48  		return errEINVAL
    49  	case ENOENT:
    50  		return errENOENT
    51  	}
    52  	return e
    53  }
    54  
    55  // ErrnoName returns the error name for error number e.
    56  func ErrnoName(e syscall.Errno) string {
    57  	i := sort.Search(len(errorList), func(i int) bool {
    58  		return errorList[i].num >= e
    59  	})
    60  	if i < len(errorList) && errorList[i].num == e {
    61  		return errorList[i].name
    62  	}
    63  	return ""
    64  }
    65  
    66  // SignalName returns the signal name for signal number s.
    67  func SignalName(s syscall.Signal) string {
    68  	i := sort.Search(len(signalList), func(i int) bool {
    69  		return signalList[i].num >= s
    70  	})
    71  	if i < len(signalList) && signalList[i].num == s {
    72  		return signalList[i].name
    73  	}
    74  	return ""
    75  }
    76  
    77  // SignalNum returns the syscall.Signal for signal named s,
    78  // or 0 if a signal with such name is not found.
    79  // The signal name should start with "SIG".
    80  func SignalNum(s string) syscall.Signal {
    81  	signalNameMapOnce.Do(func() {
    82  		signalNameMap = make(map[string]syscall.Signal, len(signalList))
    83  		for _, signal := range signalList {
    84  			signalNameMap[signal.name] = signal.num
    85  		}
    86  	})
    87  	return signalNameMap[s]
    88  }
    89  
    90  // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
    91  func clen(n []byte) int {
    92  	i := bytes.IndexByte(n, 0)
    93  	if i == -1 {
    94  		i = len(n)
    95  	}
    96  	return i
    97  }
    98  
    99  // Mmap manager, for use by operating system-specific implementations.
   100  
   101  type mmapper struct {
   102  	sync.Mutex
   103  	active map[*byte][]byte // active mappings; key is last byte in mapping
   104  	mmap   func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
   105  	munmap func(addr uintptr, length uintptr) error
   106  }
   107  
   108  func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
   109  	if length <= 0 {
   110  		return nil, EINVAL
   111  	}
   112  
   113  	// Map the requested memory.
   114  	addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
   115  	if errno != nil {
   116  		return nil, errno
   117  	}
   118  
   119  	// Use unsafe to convert addr into a []byte.
   120  	var b []byte
   121  	hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b))
   122  	hdr.Data = unsafe.Pointer(addr)
   123  	hdr.Cap = length
   124  	hdr.Len = length
   125  
   126  	// Register mapping in m and return it.
   127  	p := &b[cap(b)-1]
   128  	m.Lock()
   129  	defer m.Unlock()
   130  	m.active[p] = b
   131  	return b, nil
   132  }
   133  
   134  func (m *mmapper) Munmap(data []byte) (err error) {
   135  	if len(data) == 0 || len(data) != cap(data) {
   136  		return EINVAL
   137  	}
   138  
   139  	// Find the base of the mapping.
   140  	p := &data[cap(data)-1]
   141  	m.Lock()
   142  	defer m.Unlock()
   143  	b := m.active[p]
   144  	if b == nil || &b[0] != &data[0] {
   145  		return EINVAL
   146  	}
   147  
   148  	// Unmap the memory and update m.
   149  	if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
   150  		return errno
   151  	}
   152  	delete(m.active, p)
   153  	return nil
   154  }
   155  
   156  func Read(fd int, p []byte) (n int, err error) {
   157  	n, err = read(fd, p)
   158  	if raceenabled {
   159  		if n > 0 {
   160  			raceWriteRange(unsafe.Pointer(&p[0]), n)
   161  		}
   162  		if err == nil {
   163  			raceAcquire(unsafe.Pointer(&ioSync))
   164  		}
   165  	}
   166  	return
   167  }
   168  
   169  func Write(fd int, p []byte) (n int, err error) {
   170  	if raceenabled {
   171  		raceReleaseMerge(unsafe.Pointer(&ioSync))
   172  	}
   173  	n, err = write(fd, p)
   174  	if raceenabled && n > 0 {
   175  		raceReadRange(unsafe.Pointer(&p[0]), n)
   176  	}
   177  	return
   178  }
   179  
   180  // For testing: clients can set this flag to force
   181  // creation of IPv6 sockets to return EAFNOSUPPORT.
   182  var SocketDisableIPv6 bool
   183  
   184  // Sockaddr represents a socket address.
   185  type Sockaddr interface {
   186  	sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
   187  }
   188  
   189  // SockaddrInet4 implements the Sockaddr interface for AF_INET type sockets.
   190  type SockaddrInet4 struct {
   191  	Port int
   192  	Addr [4]byte
   193  	raw  RawSockaddrInet4
   194  }
   195  
   196  // SockaddrInet6 implements the Sockaddr interface for AF_INET6 type sockets.
   197  type SockaddrInet6 struct {
   198  	Port   int
   199  	ZoneId uint32
   200  	Addr   [16]byte
   201  	raw    RawSockaddrInet6
   202  }
   203  
   204  // SockaddrUnix implements the Sockaddr interface for AF_UNIX type sockets.
   205  type SockaddrUnix struct {
   206  	Name string
   207  	raw  RawSockaddrUnix
   208  }
   209  
   210  func Bind(fd int, sa Sockaddr) (err error) {
   211  	ptr, n, err := sa.sockaddr()
   212  	if err != nil {
   213  		return err
   214  	}
   215  	return bind(fd, ptr, n)
   216  }
   217  
   218  func Connect(fd int, sa Sockaddr) (err error) {
   219  	ptr, n, err := sa.sockaddr()
   220  	if err != nil {
   221  		return err
   222  	}
   223  	return connect(fd, ptr, n)
   224  }
   225  
   226  func Getpeername(fd int) (sa Sockaddr, err error) {
   227  	var rsa RawSockaddrAny
   228  	var len _Socklen = SizeofSockaddrAny
   229  	if err = getpeername(fd, &rsa, &len); err != nil {
   230  		return
   231  	}
   232  	return anyToSockaddr(fd, &rsa)
   233  }
   234  
   235  func GetsockoptByte(fd, level, opt int) (value byte, err error) {
   236  	var n byte
   237  	vallen := _Socklen(1)
   238  	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
   239  	return n, err
   240  }
   241  
   242  func GetsockoptInt(fd, level, opt int) (value int, err error) {
   243  	var n int32
   244  	vallen := _Socklen(4)
   245  	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
   246  	return int(n), err
   247  }
   248  
   249  func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
   250  	vallen := _Socklen(4)
   251  	err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
   252  	return value, err
   253  }
   254  
   255  func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
   256  	var value IPMreq
   257  	vallen := _Socklen(SizeofIPMreq)
   258  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   259  	return &value, err
   260  }
   261  
   262  func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
   263  	var value IPv6Mreq
   264  	vallen := _Socklen(SizeofIPv6Mreq)
   265  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   266  	return &value, err
   267  }
   268  
   269  func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
   270  	var value IPv6MTUInfo
   271  	vallen := _Socklen(SizeofIPv6MTUInfo)
   272  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   273  	return &value, err
   274  }
   275  
   276  func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
   277  	var value ICMPv6Filter
   278  	vallen := _Socklen(SizeofICMPv6Filter)
   279  	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   280  	return &value, err
   281  }
   282  
   283  func GetsockoptLinger(fd, level, opt int) (*Linger, error) {
   284  	var linger Linger
   285  	vallen := _Socklen(SizeofLinger)
   286  	err := getsockopt(fd, level, opt, unsafe.Pointer(&linger), &vallen)
   287  	return &linger, err
   288  }
   289  
   290  func GetsockoptTimeval(fd, level, opt int) (*Timeval, error) {
   291  	var tv Timeval
   292  	vallen := _Socklen(unsafe.Sizeof(tv))
   293  	err := getsockopt(fd, level, opt, unsafe.Pointer(&tv), &vallen)
   294  	return &tv, err
   295  }
   296  
   297  func GetsockoptUint64(fd, level, opt int) (value uint64, err error) {
   298  	var n uint64
   299  	vallen := _Socklen(8)
   300  	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
   301  	return n, err
   302  }
   303  
   304  func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
   305  	var rsa RawSockaddrAny
   306  	var len _Socklen = SizeofSockaddrAny
   307  	if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
   308  		return
   309  	}
   310  	if rsa.Addr.Family != AF_UNSPEC {
   311  		from, err = anyToSockaddr(fd, &rsa)
   312  	}
   313  	return
   314  }
   315  
   316  func Send(s int, buf []byte, flags int) (err error) {
   317  	return sendto(s, buf, flags, nil, 0)
   318  }
   319  
   320  func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
   321  	ptr, n, err := to.sockaddr()
   322  	if err != nil {
   323  		return err
   324  	}
   325  	return sendto(fd, p, flags, ptr, n)
   326  }
   327  
   328  func SetsockoptByte(fd, level, opt int, value byte) (err error) {
   329  	return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
   330  }
   331  
   332  func SetsockoptInt(fd, level, opt int, value int) (err error) {
   333  	var n = int32(value)
   334  	return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
   335  }
   336  
   337  func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
   338  	return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
   339  }
   340  
   341  func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
   342  	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
   343  }
   344  
   345  func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
   346  	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
   347  }
   348  
   349  func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
   350  	return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
   351  }
   352  
   353  func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
   354  	return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
   355  }
   356  
   357  func SetsockoptString(fd, level, opt int, s string) (err error) {
   358  	var p unsafe.Pointer
   359  	if len(s) > 0 {
   360  		p = unsafe.Pointer(&[]byte(s)[0])
   361  	}
   362  	return setsockopt(fd, level, opt, p, uintptr(len(s)))
   363  }
   364  
   365  func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
   366  	return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
   367  }
   368  
   369  func SetsockoptUint64(fd, level, opt int, value uint64) (err error) {
   370  	return setsockopt(fd, level, opt, unsafe.Pointer(&value), 8)
   371  }
   372  
   373  func Socket(domain, typ, proto int) (fd int, err error) {
   374  	if domain == AF_INET6 && SocketDisableIPv6 {
   375  		return -1, EAFNOSUPPORT
   376  	}
   377  	fd, err = socket(domain, typ, proto)
   378  	return
   379  }
   380  
   381  func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
   382  	var fdx [2]int32
   383  	err = socketpair(domain, typ, proto, &fdx)
   384  	if err == nil {
   385  		fd[0] = int(fdx[0])
   386  		fd[1] = int(fdx[1])
   387  	}
   388  	return
   389  }
   390  
   391  var ioSync int64
   392  
   393  func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
   394  
   395  func SetNonblock(fd int, nonblocking bool) (err error) {
   396  	flag, err := fcntl(fd, F_GETFL, 0)
   397  	if err != nil {
   398  		return err
   399  	}
   400  	if nonblocking {
   401  		flag |= O_NONBLOCK
   402  	} else {
   403  		flag &= ^O_NONBLOCK
   404  	}
   405  	_, err = fcntl(fd, F_SETFL, flag)
   406  	return err
   407  }
   408  
   409  // Exec calls execve(2), which replaces the calling executable in the process
   410  // tree. argv0 should be the full path to an executable ("/bin/ls") and the
   411  // executable name should also be the first argument in argv (["ls", "-l"]).
   412  // envv are the environment variables that should be passed to the new
   413  // process (["USER=go", "PWD=/tmp"]).
   414  func Exec(argv0 string, argv []string, envv []string) error {
   415  	return syscall.Exec(argv0, argv, envv)
   416  }
   417  
   418  // Lutimes sets the access and modification times tv on path. If path refers to
   419  // a symlink, it is not dereferenced and the timestamps are set on the symlink.
   420  // If tv is nil, the access and modification times are set to the current time.
   421  // Otherwise tv must contain exactly 2 elements, with access time as the first
   422  // element and modification time as the second element.
   423  func Lutimes(path string, tv []Timeval) error {
   424  	if tv == nil {
   425  		return UtimesNanoAt(AT_FDCWD, path, nil, AT_SYMLINK_NOFOLLOW)
   426  	}
   427  	if len(tv) != 2 {
   428  		return EINVAL
   429  	}
   430  	ts := []Timespec{
   431  		NsecToTimespec(TimevalToNsec(tv[0])),
   432  		NsecToTimespec(TimevalToNsec(tv[1])),
   433  	}
   434  	return UtimesNanoAt(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW)
   435  }