github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/internal/poll/fd_windows.go (about)

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