...
Run Format

Source file src/internal/poll/fd_windows.go

Documentation: internal/poll

     1  // Copyright 2017 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 (
     8  	"errors"
     9  	"internal/race"
    10  	"internal/syscall/windows"
    11  	"io"
    12  	"runtime"
    13  	"sync"
    14  	"syscall"
    15  	"unicode/utf16"
    16  	"unicode/utf8"
    17  	"unsafe"
    18  )
    19  
    20  var (
    21  	initErr error
    22  	ioSync  uint64
    23  )
    24  
    25  // CancelIo Windows API cancels all outstanding IO for a particular
    26  // socket on current thread. To overcome that limitation, we run
    27  // special goroutine, locked to OS single thread, that both starts
    28  // and cancels IO. It means, there are 2 unavoidable thread switches
    29  // for every IO.
    30  // Some newer versions of Windows has new CancelIoEx API, that does
    31  // not have that limitation and can be used from any thread. This
    32  // package uses CancelIoEx API, if present, otherwise it fallback
    33  // to CancelIo.
    34  
    35  var canCancelIO bool // determines if CancelIoEx API is present
    36  
    37  // This package uses the SetFileCompletionNotificationModes Windows
    38  // API to skip calling GetQueuedCompletionStatus if an IO operation
    39  // completes synchronously. There is a known bug where
    40  // SetFileCompletionNotificationModes crashes on some systems (see
    41  // https://support.microsoft.com/kb/2568167 for details).
    42  
    43  var useSetFileCompletionNotificationModes bool // determines is SetFileCompletionNotificationModes is present and safe to use
    44  
    45  // checkSetFileCompletionNotificationModes verifies that
    46  // SetFileCompletionNotificationModes Windows API is present
    47  // on the system and is safe to use.
    48  // See https://support.microsoft.com/kb/2568167 for details.
    49  func checkSetFileCompletionNotificationModes() {
    50  	err := syscall.LoadSetFileCompletionNotificationModes()
    51  	if err != nil {
    52  		return
    53  	}
    54  	protos := [2]int32{syscall.IPPROTO_TCP, 0}
    55  	var buf [32]syscall.WSAProtocolInfo
    56  	len := uint32(unsafe.Sizeof(buf))
    57  	n, err := syscall.WSAEnumProtocols(&protos[0], &buf[0], &len)
    58  	if err != nil {
    59  		return
    60  	}
    61  	for i := int32(0); i < n; i++ {
    62  		if buf[i].ServiceFlags1&syscall.XP1_IFS_HANDLES == 0 {
    63  			return
    64  		}
    65  	}
    66  	useSetFileCompletionNotificationModes = true
    67  }
    68  
    69  func init() {
    70  	var d syscall.WSAData
    71  	e := syscall.WSAStartup(uint32(0x202), &d)
    72  	if e != nil {
    73  		initErr = e
    74  	}
    75  	canCancelIO = syscall.LoadCancelIoEx() == nil
    76  	checkSetFileCompletionNotificationModes()
    77  }
    78  
    79  // operation contains superset of data necessary to perform all async IO.
    80  type operation struct {
    81  	// Used by IOCP interface, it must be first field
    82  	// of the struct, as our code rely on it.
    83  	o syscall.Overlapped
    84  
    85  	// fields used by runtime.netpoll
    86  	runtimeCtx uintptr
    87  	mode       int32
    88  	errno      int32
    89  	qty        uint32
    90  
    91  	// fields used only by net package
    92  	fd     *FD
    93  	errc   chan error
    94  	buf    syscall.WSABuf
    95  	msg    windows.WSAMsg
    96  	sa     syscall.Sockaddr
    97  	rsa    *syscall.RawSockaddrAny
    98  	rsan   int32
    99  	handle syscall.Handle
   100  	flags  uint32
   101  	bufs   []syscall.WSABuf
   102  }
   103  
   104  func (o *operation) InitBuf(buf []byte) {
   105  	o.buf.Len = uint32(len(buf))
   106  	o.buf.Buf = nil
   107  	if len(buf) != 0 {
   108  		o.buf.Buf = &buf[0]
   109  	}
   110  }
   111  
   112  func (o *operation) InitBufs(buf *[][]byte) {
   113  	if o.bufs == nil {
   114  		o.bufs = make([]syscall.WSABuf, 0, len(*buf))
   115  	} else {
   116  		o.bufs = o.bufs[:0]
   117  	}
   118  	for _, b := range *buf {
   119  		var p *byte
   120  		if len(b) > 0 {
   121  			p = &b[0]
   122  		}
   123  		o.bufs = append(o.bufs, syscall.WSABuf{Len: uint32(len(b)), Buf: p})
   124  	}
   125  }
   126  
   127  // ClearBufs clears all pointers to Buffers parameter captured
   128  // by InitBufs, so it can be released by garbage collector.
   129  func (o *operation) ClearBufs() {
   130  	for i := range o.bufs {
   131  		o.bufs[i].Buf = nil
   132  	}
   133  	o.bufs = o.bufs[:0]
   134  }
   135  
   136  func (o *operation) InitMsg(p []byte, oob []byte) {
   137  	o.InitBuf(p)
   138  	o.msg.Buffers = &o.buf
   139  	o.msg.BufferCount = 1
   140  
   141  	o.msg.Name = nil
   142  	o.msg.Namelen = 0
   143  
   144  	o.msg.Flags = 0
   145  	o.msg.Control.Len = uint32(len(oob))
   146  	o.msg.Control.Buf = nil
   147  	if len(oob) != 0 {
   148  		o.msg.Control.Buf = &oob[0]
   149  	}
   150  }
   151  
   152  // ioSrv executes net IO requests.
   153  type ioSrv struct {
   154  	req chan ioSrvReq
   155  }
   156  
   157  type ioSrvReq struct {
   158  	o      *operation
   159  	submit func(o *operation) error // if nil, cancel the operation
   160  }
   161  
   162  // ProcessRemoteIO will execute submit IO requests on behalf
   163  // of other goroutines, all on a single os thread, so it can
   164  // cancel them later. Results of all operations will be sent
   165  // back to their requesters via channel supplied in request.
   166  // It is used only when the CancelIoEx API is unavailable.
   167  func (s *ioSrv) ProcessRemoteIO() {
   168  	runtime.LockOSThread()
   169  	defer runtime.UnlockOSThread()
   170  	for r := range s.req {
   171  		if r.submit != nil {
   172  			r.o.errc <- r.submit(r.o)
   173  		} else {
   174  			r.o.errc <- syscall.CancelIo(r.o.fd.Sysfd)
   175  		}
   176  	}
   177  }
   178  
   179  // ExecIO executes a single IO operation o. It submits and cancels
   180  // IO in the current thread for systems where Windows CancelIoEx API
   181  // is available. Alternatively, it passes the request onto
   182  // runtime netpoll and waits for completion or cancels request.
   183  func (s *ioSrv) ExecIO(o *operation, submit func(o *operation) error) (int, error) {
   184  	if o.fd.pd.runtimeCtx == 0 {
   185  		return 0, errors.New("internal error: polling on unsupported descriptor type")
   186  	}
   187  
   188  	if !canCancelIO {
   189  		onceStartServer.Do(startServer)
   190  	}
   191  
   192  	fd := o.fd
   193  	// Notify runtime netpoll about starting IO.
   194  	err := fd.pd.prepare(int(o.mode), fd.isFile)
   195  	if err != nil {
   196  		return 0, err
   197  	}
   198  	// Start IO.
   199  	if canCancelIO {
   200  		err = submit(o)
   201  	} else {
   202  		// Send request to a special dedicated thread,
   203  		// so it can stop the IO with CancelIO later.
   204  		s.req <- ioSrvReq{o, submit}
   205  		err = <-o.errc
   206  	}
   207  	switch err {
   208  	case nil:
   209  		// IO completed immediately
   210  		if o.fd.skipSyncNotif {
   211  			// No completion message will follow, so return immediately.
   212  			return int(o.qty), nil
   213  		}
   214  		// Need to get our completion message anyway.
   215  	case syscall.ERROR_IO_PENDING:
   216  		// IO started, and we have to wait for its completion.
   217  		err = nil
   218  	default:
   219  		return 0, err
   220  	}
   221  	// Wait for our request to complete.
   222  	err = fd.pd.wait(int(o.mode), fd.isFile)
   223  	if err == nil {
   224  		// All is good. Extract our IO results and return.
   225  		if o.errno != 0 {
   226  			err = syscall.Errno(o.errno)
   227  			// More data available. Return back the size of received data.
   228  			if err == syscall.ERROR_MORE_DATA || err == windows.WSAEMSGSIZE {
   229  				return int(o.qty), err
   230  			}
   231  			return 0, err
   232  		}
   233  		return int(o.qty), nil
   234  	}
   235  	// IO is interrupted by "close" or "timeout"
   236  	netpollErr := err
   237  	switch netpollErr {
   238  	case ErrNetClosing, ErrFileClosing, ErrTimeout:
   239  		// will deal with those.
   240  	default:
   241  		panic("unexpected runtime.netpoll error: " + netpollErr.Error())
   242  	}
   243  	// Cancel our request.
   244  	if canCancelIO {
   245  		err := syscall.CancelIoEx(fd.Sysfd, &o.o)
   246  		// Assuming ERROR_NOT_FOUND is returned, if IO is completed.
   247  		if err != nil && err != syscall.ERROR_NOT_FOUND {
   248  			// TODO(brainman): maybe do something else, but panic.
   249  			panic(err)
   250  		}
   251  	} else {
   252  		s.req <- ioSrvReq{o, nil}
   253  		<-o.errc
   254  	}
   255  	// Wait for cancelation to complete.
   256  	fd.pd.waitCanceled(int(o.mode))
   257  	if o.errno != 0 {
   258  		err = syscall.Errno(o.errno)
   259  		if err == syscall.ERROR_OPERATION_ABORTED { // IO Canceled
   260  			err = netpollErr
   261  		}
   262  		return 0, err
   263  	}
   264  	// We issued a cancelation request. But, it seems, IO operation succeeded
   265  	// before the cancelation request run. We need to treat the IO operation as
   266  	// succeeded (the bytes are actually sent/recv from network).
   267  	return int(o.qty), nil
   268  }
   269  
   270  // Start helper goroutines.
   271  var rsrv, wsrv ioSrv
   272  var onceStartServer sync.Once
   273  
   274  func startServer() {
   275  	// This is called, once, when only the CancelIo API is available.
   276  	// Start two special goroutines, both locked to an OS thread,
   277  	// that start and cancel IO requests.
   278  	// One will process read requests, while the other will do writes.
   279  	rsrv.req = make(chan ioSrvReq)
   280  	go rsrv.ProcessRemoteIO()
   281  	wsrv.req = make(chan ioSrvReq)
   282  	go wsrv.ProcessRemoteIO()
   283  }
   284  
   285  // FD is a file descriptor. The net and os packages embed this type in
   286  // a larger type representing a network connection or OS file.
   287  type FD struct {
   288  	// Lock sysfd and serialize access to Read and Write methods.
   289  	fdmu fdMutex
   290  
   291  	// System file descriptor. Immutable until Close.
   292  	Sysfd syscall.Handle
   293  
   294  	// Read operation.
   295  	rop operation
   296  	// Write operation.
   297  	wop operation
   298  
   299  	// I/O poller.
   300  	pd pollDesc
   301  
   302  	// Used to implement pread/pwrite.
   303  	l sync.Mutex
   304  
   305  	// For console I/O.
   306  	isConsole      bool
   307  	lastbits       []byte   // first few bytes of the last incomplete rune in last write
   308  	readuint16     []uint16 // buffer to hold uint16s obtained with ReadConsole
   309  	readbyte       []byte   // buffer to hold decoding of readuint16 from utf16 to utf8
   310  	readbyteOffset int      // readbyte[readOffset:] is yet to be consumed with file.Read
   311  
   312  	// Semaphore signaled when file is closed.
   313  	csema uint32
   314  
   315  	skipSyncNotif bool
   316  
   317  	// Whether this is a streaming descriptor, as opposed to a
   318  	// packet-based descriptor like a UDP socket.
   319  	IsStream bool
   320  
   321  	// Whether a zero byte read indicates EOF. This is false for a
   322  	// message based socket connection.
   323  	ZeroReadIsEOF bool
   324  
   325  	// Whether this is a normal file.
   326  	isFile bool
   327  
   328  	// Whether this is a directory.
   329  	isDir bool
   330  }
   331  
   332  // logInitFD is set by tests to enable file descriptor initialization logging.
   333  var logInitFD func(net string, fd *FD, err error)
   334  
   335  // Init initializes the FD. The Sysfd field should already be set.
   336  // This can be called multiple times on a single FD.
   337  // The net argument is a network name from the net package (e.g., "tcp"),
   338  // or "file" or "console" or "dir".
   339  // Set pollable to true if fd should be managed by runtime netpoll.
   340  func (fd *FD) Init(net string, pollable bool) (string, error) {
   341  	if initErr != nil {
   342  		return "", initErr
   343  	}
   344  
   345  	switch net {
   346  	case "file":
   347  		fd.isFile = true
   348  	case "console":
   349  		fd.isConsole = true
   350  	case "dir":
   351  		fd.isDir = true
   352  	case "tcp", "tcp4", "tcp6":
   353  	case "udp", "udp4", "udp6":
   354  	case "ip", "ip4", "ip6":
   355  	case "unix", "unixgram", "unixpacket":
   356  	default:
   357  		return "", errors.New("internal error: unknown network type " + net)
   358  	}
   359  
   360  	var err error
   361  	if pollable {
   362  		// Only call init for a network socket.
   363  		// This means that we don't add files to the runtime poller.
   364  		// Adding files to the runtime poller can confuse matters
   365  		// if the user is doing their own overlapped I/O.
   366  		// See issue #21172.
   367  		//
   368  		// In general the code below avoids calling the ExecIO
   369  		// method for non-network sockets. If some method does
   370  		// somehow call ExecIO, then ExecIO, and therefore the
   371  		// calling method, will return an error, because
   372  		// fd.pd.runtimeCtx will be 0.
   373  		err = fd.pd.init(fd)
   374  	}
   375  	if logInitFD != nil {
   376  		logInitFD(net, fd, err)
   377  	}
   378  	if err != nil {
   379  		return "", err
   380  	}
   381  	if pollable && useSetFileCompletionNotificationModes {
   382  		// We do not use events, so we can skip them always.
   383  		flags := uint8(syscall.FILE_SKIP_SET_EVENT_ON_HANDLE)
   384  		// It's not safe to skip completion notifications for UDP:
   385  		// https://blogs.technet.com/b/winserverperformance/archive/2008/06/26/designing-applications-for-high-performance-part-iii.aspx
   386  		if net == "tcp" {
   387  			flags |= syscall.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
   388  		}
   389  		err := syscall.SetFileCompletionNotificationModes(fd.Sysfd, flags)
   390  		if err == nil && flags&syscall.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS != 0 {
   391  			fd.skipSyncNotif = true
   392  		}
   393  	}
   394  	// Disable SIO_UDP_CONNRESET behavior.
   395  	// http://support.microsoft.com/kb/263823
   396  	switch net {
   397  	case "udp", "udp4", "udp6":
   398  		ret := uint32(0)
   399  		flag := uint32(0)
   400  		size := uint32(unsafe.Sizeof(flag))
   401  		err := syscall.WSAIoctl(fd.Sysfd, syscall.SIO_UDP_CONNRESET, (*byte)(unsafe.Pointer(&flag)), size, nil, 0, &ret, nil, 0)
   402  		if err != nil {
   403  			return "wsaioctl", err
   404  		}
   405  	}
   406  	fd.rop.mode = 'r'
   407  	fd.wop.mode = 'w'
   408  	fd.rop.fd = fd
   409  	fd.wop.fd = fd
   410  	fd.rop.runtimeCtx = fd.pd.runtimeCtx
   411  	fd.wop.runtimeCtx = fd.pd.runtimeCtx
   412  	if !canCancelIO {
   413  		fd.rop.errc = make(chan error)
   414  		fd.wop.errc = make(chan error)
   415  	}
   416  	return "", nil
   417  }
   418  
   419  func (fd *FD) destroy() error {
   420  	if fd.Sysfd == syscall.InvalidHandle {
   421  		return syscall.EINVAL
   422  	}
   423  	// Poller may want to unregister fd in readiness notification mechanism,
   424  	// so this must be executed before fd.CloseFunc.
   425  	fd.pd.close()
   426  	var err error
   427  	if fd.isFile || fd.isConsole {
   428  		err = syscall.CloseHandle(fd.Sysfd)
   429  	} else if fd.isDir {
   430  		err = syscall.FindClose(fd.Sysfd)
   431  	} else {
   432  		// The net package uses the CloseFunc variable for testing.
   433  		err = CloseFunc(fd.Sysfd)
   434  	}
   435  	fd.Sysfd = syscall.InvalidHandle
   436  	runtime_Semrelease(&fd.csema)
   437  	return err
   438  }
   439  
   440  // Close closes the FD. The underlying file descriptor is closed by
   441  // the destroy method when there are no remaining references.
   442  func (fd *FD) Close() error {
   443  	if !fd.fdmu.increfAndClose() {
   444  		return errClosing(fd.isFile)
   445  	}
   446  	// unblock pending reader and writer
   447  	fd.pd.evict()
   448  	err := fd.decref()
   449  	// Wait until the descriptor is closed. If this was the only
   450  	// reference, it is already closed.
   451  	runtime_Semacquire(&fd.csema)
   452  	return err
   453  }
   454  
   455  // Shutdown wraps the shutdown network call.
   456  func (fd *FD) Shutdown(how int) error {
   457  	if err := fd.incref(); err != nil {
   458  		return err
   459  	}
   460  	defer fd.decref()
   461  	return syscall.Shutdown(fd.Sysfd, how)
   462  }
   463  
   464  // Read implements io.Reader.
   465  func (fd *FD) Read(buf []byte) (int, error) {
   466  	if err := fd.readLock(); err != nil {
   467  		return 0, err
   468  	}
   469  	defer fd.readUnlock()
   470  
   471  	var n int
   472  	var err error
   473  	if fd.isFile || fd.isDir || fd.isConsole {
   474  		fd.l.Lock()
   475  		defer fd.l.Unlock()
   476  		if fd.isConsole {
   477  			n, err = fd.readConsole(buf)
   478  		} else {
   479  			n, err = syscall.Read(fd.Sysfd, buf)
   480  		}
   481  		if err != nil {
   482  			n = 0
   483  		}
   484  	} else {
   485  		o := &fd.rop
   486  		o.InitBuf(buf)
   487  		n, err = rsrv.ExecIO(o, func(o *operation) error {
   488  			return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
   489  		})
   490  		if race.Enabled {
   491  			race.Acquire(unsafe.Pointer(&ioSync))
   492  		}
   493  	}
   494  	if len(buf) != 0 {
   495  		err = fd.eofError(n, err)
   496  	}
   497  	return n, err
   498  }
   499  
   500  var ReadConsole = syscall.ReadConsole // changed for testing
   501  
   502  // readConsole reads utf16 characters from console File,
   503  // encodes them into utf8 and stores them in buffer b.
   504  // It returns the number of utf8 bytes read and an error, if any.
   505  func (fd *FD) readConsole(b []byte) (int, error) {
   506  	if len(b) == 0 {
   507  		return 0, nil
   508  	}
   509  
   510  	if fd.readuint16 == nil {
   511  		// Note: syscall.ReadConsole fails for very large buffers.
   512  		// The limit is somewhere around (but not exactly) 16384.
   513  		// Stay well below.
   514  		fd.readuint16 = make([]uint16, 0, 10000)
   515  		fd.readbyte = make([]byte, 0, 4*cap(fd.readuint16))
   516  	}
   517  
   518  	for fd.readbyteOffset >= len(fd.readbyte) {
   519  		n := cap(fd.readuint16) - len(fd.readuint16)
   520  		if n > len(b) {
   521  			n = len(b)
   522  		}
   523  		var nw uint32
   524  		err := ReadConsole(fd.Sysfd, &fd.readuint16[:len(fd.readuint16)+1][len(fd.readuint16)], uint32(n), &nw, nil)
   525  		if err != nil {
   526  			return 0, err
   527  		}
   528  		uint16s := fd.readuint16[:len(fd.readuint16)+int(nw)]
   529  		fd.readuint16 = fd.readuint16[:0]
   530  		buf := fd.readbyte[:0]
   531  		for i := 0; i < len(uint16s); i++ {
   532  			r := rune(uint16s[i])
   533  			if utf16.IsSurrogate(r) {
   534  				if i+1 == len(uint16s) {
   535  					if nw > 0 {
   536  						// Save half surrogate pair for next time.
   537  						fd.readuint16 = fd.readuint16[:1]
   538  						fd.readuint16[0] = uint16(r)
   539  						break
   540  					}
   541  					r = utf8.RuneError
   542  				} else {
   543  					r = utf16.DecodeRune(r, rune(uint16s[i+1]))
   544  					if r != utf8.RuneError {
   545  						i++
   546  					}
   547  				}
   548  			}
   549  			n := utf8.EncodeRune(buf[len(buf):cap(buf)], r)
   550  			buf = buf[:len(buf)+n]
   551  		}
   552  		fd.readbyte = buf
   553  		fd.readbyteOffset = 0
   554  		if nw == 0 {
   555  			break
   556  		}
   557  	}
   558  
   559  	src := fd.readbyte[fd.readbyteOffset:]
   560  	var i int
   561  	for i = 0; i < len(src) && i < len(b); i++ {
   562  		x := src[i]
   563  		if x == 0x1A { // Ctrl-Z
   564  			if i == 0 {
   565  				fd.readbyteOffset++
   566  			}
   567  			break
   568  		}
   569  		b[i] = x
   570  	}
   571  	fd.readbyteOffset += i
   572  	return i, nil
   573  }
   574  
   575  // Pread emulates the Unix pread system call.
   576  func (fd *FD) Pread(b []byte, off int64) (int, error) {
   577  	// Call incref, not readLock, because since pread specifies the
   578  	// offset it is independent from other reads.
   579  	if err := fd.incref(); err != nil {
   580  		return 0, err
   581  	}
   582  	defer fd.decref()
   583  
   584  	fd.l.Lock()
   585  	defer fd.l.Unlock()
   586  	curoffset, e := syscall.Seek(fd.Sysfd, 0, io.SeekCurrent)
   587  	if e != nil {
   588  		return 0, e
   589  	}
   590  	defer syscall.Seek(fd.Sysfd, curoffset, io.SeekStart)
   591  	o := syscall.Overlapped{
   592  		OffsetHigh: uint32(off >> 32),
   593  		Offset:     uint32(off),
   594  	}
   595  	var done uint32
   596  	e = syscall.ReadFile(fd.Sysfd, b, &done, &o)
   597  	if e != nil {
   598  		done = 0
   599  		if e == syscall.ERROR_HANDLE_EOF {
   600  			e = io.EOF
   601  		}
   602  	}
   603  	if len(b) != 0 {
   604  		e = fd.eofError(int(done), e)
   605  	}
   606  	return int(done), e
   607  }
   608  
   609  // ReadFrom wraps the recvfrom network call.
   610  func (fd *FD) ReadFrom(buf []byte) (int, syscall.Sockaddr, error) {
   611  	if len(buf) == 0 {
   612  		return 0, nil, nil
   613  	}
   614  	if err := fd.readLock(); err != nil {
   615  		return 0, nil, err
   616  	}
   617  	defer fd.readUnlock()
   618  	o := &fd.rop
   619  	o.InitBuf(buf)
   620  	n, err := rsrv.ExecIO(o, func(o *operation) error {
   621  		if o.rsa == nil {
   622  			o.rsa = new(syscall.RawSockaddrAny)
   623  		}
   624  		o.rsan = int32(unsafe.Sizeof(*o.rsa))
   625  		return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
   626  	})
   627  	err = fd.eofError(n, err)
   628  	if err != nil {
   629  		return n, nil, err
   630  	}
   631  	sa, _ := o.rsa.Sockaddr()
   632  	return n, sa, nil
   633  }
   634  
   635  // Write implements io.Writer.
   636  func (fd *FD) Write(buf []byte) (int, error) {
   637  	if err := fd.writeLock(); err != nil {
   638  		return 0, err
   639  	}
   640  	defer fd.writeUnlock()
   641  
   642  	var n int
   643  	var err error
   644  	if fd.isFile || fd.isDir || fd.isConsole {
   645  		fd.l.Lock()
   646  		defer fd.l.Unlock()
   647  		if fd.isConsole {
   648  			n, err = fd.writeConsole(buf)
   649  		} else {
   650  			n, err = syscall.Write(fd.Sysfd, buf)
   651  		}
   652  		if err != nil {
   653  			n = 0
   654  		}
   655  	} else {
   656  		if race.Enabled {
   657  			race.ReleaseMerge(unsafe.Pointer(&ioSync))
   658  		}
   659  		o := &fd.wop
   660  		o.InitBuf(buf)
   661  		n, err = wsrv.ExecIO(o, func(o *operation) error {
   662  			return syscall.WSASend(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil)
   663  		})
   664  	}
   665  	return n, err
   666  }
   667  
   668  // writeConsole writes len(b) bytes to the console File.
   669  // It returns the number of bytes written and an error, if any.
   670  func (fd *FD) writeConsole(b []byte) (int, error) {
   671  	n := len(b)
   672  	runes := make([]rune, 0, 256)
   673  	if len(fd.lastbits) > 0 {
   674  		b = append(fd.lastbits, b...)
   675  		fd.lastbits = nil
   676  
   677  	}
   678  	for len(b) >= utf8.UTFMax || utf8.FullRune(b) {
   679  		r, l := utf8.DecodeRune(b)
   680  		runes = append(runes, r)
   681  		b = b[l:]
   682  	}
   683  	if len(b) > 0 {
   684  		fd.lastbits = make([]byte, len(b))
   685  		copy(fd.lastbits, b)
   686  	}
   687  	// syscall.WriteConsole seems to fail, if given large buffer.
   688  	// So limit the buffer to 16000 characters. This number was
   689  	// discovered by experimenting with syscall.WriteConsole.
   690  	const maxWrite = 16000
   691  	for len(runes) > 0 {
   692  		m := len(runes)
   693  		if m > maxWrite {
   694  			m = maxWrite
   695  		}
   696  		chunk := runes[:m]
   697  		runes = runes[m:]
   698  		uint16s := utf16.Encode(chunk)
   699  		for len(uint16s) > 0 {
   700  			var written uint32
   701  			err := syscall.WriteConsole(fd.Sysfd, &uint16s[0], uint32(len(uint16s)), &written, nil)
   702  			if err != nil {
   703  				return 0, err
   704  			}
   705  			uint16s = uint16s[written:]
   706  		}
   707  	}
   708  	return n, nil
   709  }
   710  
   711  // Pwrite emulates the Unix pwrite system call.
   712  func (fd *FD) Pwrite(b []byte, off int64) (int, error) {
   713  	// Call incref, not writeLock, because since pwrite specifies the
   714  	// offset it is independent from other writes.
   715  	if err := fd.incref(); err != nil {
   716  		return 0, err
   717  	}
   718  	defer fd.decref()
   719  
   720  	fd.l.Lock()
   721  	defer fd.l.Unlock()
   722  	curoffset, e := syscall.Seek(fd.Sysfd, 0, io.SeekCurrent)
   723  	if e != nil {
   724  		return 0, e
   725  	}
   726  	defer syscall.Seek(fd.Sysfd, curoffset, io.SeekStart)
   727  	o := syscall.Overlapped{
   728  		OffsetHigh: uint32(off >> 32),
   729  		Offset:     uint32(off),
   730  	}
   731  	var done uint32
   732  	e = syscall.WriteFile(fd.Sysfd, b, &done, &o)
   733  	if e != nil {
   734  		return 0, e
   735  	}
   736  	return int(done), nil
   737  }
   738  
   739  // Writev emulates the Unix writev system call.
   740  func (fd *FD) Writev(buf *[][]byte) (int64, error) {
   741  	if len(*buf) == 0 {
   742  		return 0, nil
   743  	}
   744  	if err := fd.writeLock(); err != nil {
   745  		return 0, err
   746  	}
   747  	defer fd.writeUnlock()
   748  	if race.Enabled {
   749  		race.ReleaseMerge(unsafe.Pointer(&ioSync))
   750  	}
   751  	o := &fd.wop
   752  	o.InitBufs(buf)
   753  	n, err := wsrv.ExecIO(o, func(o *operation) error {
   754  		return syscall.WSASend(o.fd.Sysfd, &o.bufs[0], uint32(len(o.bufs)), &o.qty, 0, &o.o, nil)
   755  	})
   756  	o.ClearBufs()
   757  	TestHookDidWritev(n)
   758  	consume(buf, int64(n))
   759  	return int64(n), err
   760  }
   761  
   762  // WriteTo wraps the sendto network call.
   763  func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) {
   764  	if len(buf) == 0 {
   765  		return 0, nil
   766  	}
   767  	if err := fd.writeLock(); err != nil {
   768  		return 0, err
   769  	}
   770  	defer fd.writeUnlock()
   771  	o := &fd.wop
   772  	o.InitBuf(buf)
   773  	o.sa = sa
   774  	n, err := wsrv.ExecIO(o, func(o *operation) error {
   775  		return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil)
   776  	})
   777  	return n, err
   778  }
   779  
   780  // Call ConnectEx. This doesn't need any locking, since it is only
   781  // called when the descriptor is first created. This is here rather
   782  // than in the net package so that it can use fd.wop.
   783  func (fd *FD) ConnectEx(ra syscall.Sockaddr) error {
   784  	o := &fd.wop
   785  	o.sa = ra
   786  	_, err := wsrv.ExecIO(o, func(o *operation) error {
   787  		return ConnectExFunc(o.fd.Sysfd, o.sa, nil, 0, nil, &o.o)
   788  	})
   789  	return err
   790  }
   791  
   792  func (fd *FD) acceptOne(s syscall.Handle, rawsa []syscall.RawSockaddrAny, o *operation) (string, error) {
   793  	// Submit accept request.
   794  	o.handle = s
   795  	o.rsan = int32(unsafe.Sizeof(rawsa[0]))
   796  	_, err := rsrv.ExecIO(o, func(o *operation) error {
   797  		return AcceptFunc(o.fd.Sysfd, o.handle, (*byte)(unsafe.Pointer(&rawsa[0])), 0, uint32(o.rsan), uint32(o.rsan), &o.qty, &o.o)
   798  	})
   799  	if err != nil {
   800  		CloseFunc(s)
   801  		return "acceptex", err
   802  	}
   803  
   804  	// Inherit properties of the listening socket.
   805  	err = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT_CONTEXT, (*byte)(unsafe.Pointer(&fd.Sysfd)), int32(unsafe.Sizeof(fd.Sysfd)))
   806  	if err != nil {
   807  		CloseFunc(s)
   808  		return "setsockopt", err
   809  	}
   810  
   811  	return "", nil
   812  }
   813  
   814  // Accept handles accepting a socket. The sysSocket parameter is used
   815  // to allocate the net socket.
   816  func (fd *FD) Accept(sysSocket func() (syscall.Handle, error)) (syscall.Handle, []syscall.RawSockaddrAny, uint32, string, error) {
   817  	if err := fd.readLock(); err != nil {
   818  		return syscall.InvalidHandle, nil, 0, "", err
   819  	}
   820  	defer fd.readUnlock()
   821  
   822  	o := &fd.rop
   823  	var rawsa [2]syscall.RawSockaddrAny
   824  	for {
   825  		s, err := sysSocket()
   826  		if err != nil {
   827  			return syscall.InvalidHandle, nil, 0, "", err
   828  		}
   829  
   830  		errcall, err := fd.acceptOne(s, rawsa[:], o)
   831  		if err == nil {
   832  			return s, rawsa[:], uint32(o.rsan), "", nil
   833  		}
   834  
   835  		// Sometimes we see WSAECONNRESET and ERROR_NETNAME_DELETED is
   836  		// returned here. These happen if connection reset is received
   837  		// before AcceptEx could complete. These errors relate to new
   838  		// connection, not to AcceptEx, so ignore broken connection and
   839  		// try AcceptEx again for more connections.
   840  		errno, ok := err.(syscall.Errno)
   841  		if !ok {
   842  			return syscall.InvalidHandle, nil, 0, errcall, err
   843  		}
   844  		switch errno {
   845  		case syscall.ERROR_NETNAME_DELETED, syscall.WSAECONNRESET:
   846  			// ignore these and try again
   847  		default:
   848  			return syscall.InvalidHandle, nil, 0, errcall, err
   849  		}
   850  	}
   851  }
   852  
   853  // Seek wraps syscall.Seek.
   854  func (fd *FD) Seek(offset int64, whence int) (int64, error) {
   855  	if err := fd.incref(); err != nil {
   856  		return 0, err
   857  	}
   858  	defer fd.decref()
   859  
   860  	fd.l.Lock()
   861  	defer fd.l.Unlock()
   862  
   863  	return syscall.Seek(fd.Sysfd, offset, whence)
   864  }
   865  
   866  // FindNextFile wraps syscall.FindNextFile.
   867  func (fd *FD) FindNextFile(data *syscall.Win32finddata) error {
   868  	if err := fd.incref(); err != nil {
   869  		return err
   870  	}
   871  	defer fd.decref()
   872  	return syscall.FindNextFile(fd.Sysfd, data)
   873  }
   874  
   875  // Fchdir wraps syscall.Fchdir.
   876  func (fd *FD) Fchdir() error {
   877  	if err := fd.incref(); err != nil {
   878  		return err
   879  	}
   880  	defer fd.decref()
   881  	return syscall.Fchdir(fd.Sysfd)
   882  }
   883  
   884  // GetFileType wraps syscall.GetFileType.
   885  func (fd *FD) GetFileType() (uint32, error) {
   886  	if err := fd.incref(); err != nil {
   887  		return 0, err
   888  	}
   889  	defer fd.decref()
   890  	return syscall.GetFileType(fd.Sysfd)
   891  }
   892  
   893  // GetFileInformationByHandle wraps GetFileInformationByHandle.
   894  func (fd *FD) GetFileInformationByHandle(data *syscall.ByHandleFileInformation) error {
   895  	if err := fd.incref(); err != nil {
   896  		return err
   897  	}
   898  	defer fd.decref()
   899  	return syscall.GetFileInformationByHandle(fd.Sysfd, data)
   900  }
   901  
   902  // RawControl invokes the user-defined function f for a non-IO
   903  // operation.
   904  func (fd *FD) RawControl(f func(uintptr)) error {
   905  	if err := fd.incref(); err != nil {
   906  		return err
   907  	}
   908  	defer fd.decref()
   909  	f(uintptr(fd.Sysfd))
   910  	return nil
   911  }
   912  
   913  // RawRead invokes the user-defined function f for a read operation.
   914  func (fd *FD) RawRead(f func(uintptr) bool) error {
   915  	if err := fd.readLock(); err != nil {
   916  		return err
   917  	}
   918  	defer fd.readUnlock()
   919  	for {
   920  		if f(uintptr(fd.Sysfd)) {
   921  			return nil
   922  		}
   923  
   924  		// Use a zero-byte read as a way to get notified when this
   925  		// socket is readable. h/t https://stackoverflow.com/a/42019668/332798
   926  		o := &fd.rop
   927  		o.InitBuf(nil)
   928  		if !fd.IsStream {
   929  			o.flags |= windows.MSG_PEEK
   930  		}
   931  		_, err := rsrv.ExecIO(o, func(o *operation) error {
   932  			return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
   933  		})
   934  		if err == windows.WSAEMSGSIZE {
   935  			// expected with a 0-byte peek, ignore.
   936  		} else if err != nil {
   937  			return err
   938  		}
   939  	}
   940  }
   941  
   942  // RawWrite invokes the user-defined function f for a write operation.
   943  func (fd *FD) RawWrite(f func(uintptr) bool) error {
   944  	if err := fd.writeLock(); err != nil {
   945  		return err
   946  	}
   947  	defer fd.writeUnlock()
   948  
   949  	if f(uintptr(fd.Sysfd)) {
   950  		return nil
   951  	}
   952  
   953  	// TODO(tmm1): find a way to detect socket writability
   954  	return syscall.EWINDOWS
   955  }
   956  
   957  func sockaddrToRaw(sa syscall.Sockaddr) (unsafe.Pointer, int32, error) {
   958  	switch sa := sa.(type) {
   959  	case *syscall.SockaddrInet4:
   960  		var raw syscall.RawSockaddrInet4
   961  		raw.Family = syscall.AF_INET
   962  		p := (*[2]byte)(unsafe.Pointer(&raw.Port))
   963  		p[0] = byte(sa.Port >> 8)
   964  		p[1] = byte(sa.Port)
   965  		for i := 0; i < len(sa.Addr); i++ {
   966  			raw.Addr[i] = sa.Addr[i]
   967  		}
   968  		return unsafe.Pointer(&raw), int32(unsafe.Sizeof(raw)), nil
   969  	case *syscall.SockaddrInet6:
   970  		var raw syscall.RawSockaddrInet6
   971  		raw.Family = syscall.AF_INET6
   972  		p := (*[2]byte)(unsafe.Pointer(&raw.Port))
   973  		p[0] = byte(sa.Port >> 8)
   974  		p[1] = byte(sa.Port)
   975  		raw.Scope_id = sa.ZoneId
   976  		for i := 0; i < len(sa.Addr); i++ {
   977  			raw.Addr[i] = sa.Addr[i]
   978  		}
   979  		return unsafe.Pointer(&raw), int32(unsafe.Sizeof(raw)), nil
   980  	default:
   981  		return nil, 0, syscall.EWINDOWS
   982  	}
   983  }
   984  
   985  // ReadMsg wraps the WSARecvMsg network call.
   986  func (fd *FD) ReadMsg(p []byte, oob []byte) (int, int, int, syscall.Sockaddr, error) {
   987  	if err := fd.readLock(); err != nil {
   988  		return 0, 0, 0, nil, err
   989  	}
   990  	defer fd.readUnlock()
   991  
   992  	o := &fd.rop
   993  	o.InitMsg(p, oob)
   994  	o.rsa = new(syscall.RawSockaddrAny)
   995  	o.msg.Name = o.rsa
   996  	o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa))
   997  	n, err := rsrv.ExecIO(o, func(o *operation) error {
   998  		return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
   999  	})
  1000  	err = fd.eofError(n, err)
  1001  	var sa syscall.Sockaddr
  1002  	if err == nil {
  1003  		sa, err = o.rsa.Sockaddr()
  1004  	}
  1005  	return n, int(o.msg.Control.Len), int(o.msg.Flags), sa, err
  1006  }
  1007  
  1008  // WriteMsg wraps the WSASendMsg network call.
  1009  func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, error) {
  1010  	if err := fd.writeLock(); err != nil {
  1011  		return 0, 0, err
  1012  	}
  1013  	defer fd.writeUnlock()
  1014  
  1015  	o := &fd.wop
  1016  	o.InitMsg(p, oob)
  1017  	if sa != nil {
  1018  		rsa, len, err := sockaddrToRaw(sa)
  1019  		if err != nil {
  1020  			return 0, 0, err
  1021  		}
  1022  		o.msg.Name = (*syscall.RawSockaddrAny)(rsa)
  1023  		o.msg.Namelen = len
  1024  	}
  1025  	n, err := wsrv.ExecIO(o, func(o *operation) error {
  1026  		return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
  1027  	})
  1028  	return n, int(o.msg.Control.Len), err
  1029  }
  1030  

View as plain text