gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/internal/poll/fd_posix.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 || (js && wasm) || linux || netbsd || openbsd || solaris || windows
     6  // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
     7  
     8  package poll
     9  
    10  import (
    11  	"io"
    12  	"syscall"
    13  )
    14  
    15  // eofError returns io.EOF when fd is available for reading end of
    16  // file.
    17  func (fd *FD) eofError(n int, err error) error {
    18  	if n == 0 && err == nil && fd.ZeroReadIsEOF {
    19  		return io.EOF
    20  	}
    21  	return err
    22  }
    23  
    24  // Shutdown wraps syscall.Shutdown.
    25  func (fd *FD) Shutdown(how int) error {
    26  	if err := fd.incref(); err != nil {
    27  		return err
    28  	}
    29  	defer fd.decref()
    30  	return syscall.Shutdown(fd.Sysfd, how)
    31  }
    32  
    33  // Fchown wraps syscall.Fchown.
    34  func (fd *FD) Fchown(uid, gid int) error {
    35  	if err := fd.incref(); err != nil {
    36  		return err
    37  	}
    38  	defer fd.decref()
    39  	return ignoringEINTR(func() error {
    40  		return syscall.Fchown(fd.Sysfd, uid, gid)
    41  	})
    42  }
    43  
    44  // Ftruncate wraps syscall.Ftruncate.
    45  func (fd *FD) Ftruncate(size int64) error {
    46  	if err := fd.incref(); err != nil {
    47  		return err
    48  	}
    49  	defer fd.decref()
    50  	return ignoringEINTR(func() error {
    51  		return syscall.Ftruncate(fd.Sysfd, size)
    52  	})
    53  }
    54  
    55  // RawControl invokes the user-defined function f for a non-IO
    56  // operation.
    57  func (fd *FD) RawControl(f func(uintptr)) error {
    58  	if err := fd.incref(); err != nil {
    59  		return err
    60  	}
    61  	defer fd.decref()
    62  	f(uintptr(fd.Sysfd))
    63  	return nil
    64  }
    65  
    66  // ignoringEINTR makes a function call and repeats it if it returns
    67  // an EINTR error. This appears to be required even though we install all
    68  // signal handlers with SA_RESTART: see #22838, #38033, #38836, #40846.
    69  // Also #20400 and #36644 are issues in which a signal handler is
    70  // installed without setting SA_RESTART. None of these are the common case,
    71  // but there are enough of them that it seems that we can't avoid
    72  // an EINTR loop.
    73  func ignoringEINTR(fn func() error) error {
    74  	for {
    75  		err := fn()
    76  		if err != syscall.EINTR {
    77  			return err
    78  		}
    79  	}
    80  }