...

Source file src/internal/poll/sendfile_solaris.go

Documentation: internal/poll

     1  // Copyright 2015 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  package poll
     6  
     7  import "syscall"
     8  
     9  // Not strictly needed, but very helpful for debugging, see issue #10221.
    10  //
    11  //go:cgo_import_dynamic _ _ "libsendfile.so"
    12  //go:cgo_import_dynamic _ _ "libsocket.so"
    13  
    14  // maxSendfileSize is the largest chunk size we ask the kernel to copy
    15  // at a time.
    16  const maxSendfileSize int = 4 << 20
    17  
    18  // SendFile wraps the sendfile system call.
    19  func SendFile(dstFD *FD, src int, pos, remain int64) (written int64, err error, handled bool) {
    20  	defer func() {
    21  		TestHookDidSendFile(dstFD, src, written, err, handled)
    22  	}()
    23  	if err := dstFD.writeLock(); err != nil {
    24  		return 0, err, false
    25  	}
    26  	defer dstFD.writeUnlock()
    27  
    28  	if err := dstFD.pd.prepareWrite(dstFD.isFile); err != nil {
    29  		return 0, err, false
    30  	}
    31  
    32  	dst := dstFD.Sysfd
    33  	for remain > 0 {
    34  		n := maxSendfileSize
    35  		if int64(n) > remain {
    36  			n = int(remain)
    37  		}
    38  		pos1 := pos
    39  		n, err = syscall.Sendfile(dst, src, &pos1, n)
    40  		if err == syscall.EAGAIN || err == syscall.EINTR {
    41  			// partial write may have occurred
    42  			n = int(pos1 - pos)
    43  		}
    44  		if n > 0 {
    45  			pos += int64(n)
    46  			written += int64(n)
    47  			remain -= int64(n)
    48  			continue
    49  		} else if err != syscall.EAGAIN && err != syscall.EINTR {
    50  			// This includes syscall.ENOSYS (no kernel
    51  			// support) and syscall.EINVAL (fd types which
    52  			// don't implement sendfile), and other errors.
    53  			// We should end the loop when there is no error
    54  			// returned from sendfile(2) or it is not a retryable error.
    55  			break
    56  		}
    57  		if err == syscall.EINTR {
    58  			continue
    59  		}
    60  		if err = dstFD.pd.waitWrite(dstFD.isFile); err != nil {
    61  			break
    62  		}
    63  	}
    64  	if err == syscall.EAGAIN {
    65  		err = nil
    66  	}
    67  	handled = written != 0 || (err != syscall.ENOSYS && err != syscall.EINVAL)
    68  	return
    69  }
    70  

View as plain text