Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32unix: use WSADuplicateSocket in bindings of dup and dup2 #10697

Merged

Commits on Oct 28, 2021

  1. win32unix: use WSADuplicateSocket in bindings of dup and dup2

    The WinAPI documentation of [DuplicateHandle][] gives a list of types
    of objects on which it is safe to call DuplicateHandle. In particular,
    the doc reads:
    
    > You should not use DuplicateHandle to duplicate handles to the
    > following objects:
    >
    > - Sockets. No error is returned, but the duplicate handle may not be
    >   recognized by Winsock at the target process. Also, using
    >   DuplicateHandle interferes with internal reference counting on the
    >   underlying object. To duplicate a socket handle, use the
    >   WSADuplicateSocket function.
    
    Rework the code to use DuplicateHandle for handles and
    [WSADuplicateSocket][] for sockets.
    
    WSADuplicateSocket returns an info structure that we give to WSASocket
    to create the duplicated handle. However, there seem to be no way of
    explicitely preserving the set of flags to give to WSASocket.
    Considering that as of now, OCaml uses the socket function instead of
    WSASocket, and that socket only sets WSA_FLAG_OVERLAPPED, re-set that
    flag and hope for the best (that's also the current behavior of
    libuv).
    
    It's also possible to retreive the WSA_FLAG_NO_HANDLE_INHERIT (Winsock
    mapping of HANDLE_FLAG_INHERIT) via GetHandleInformation, but that's
    not needed here since we override it with the cloexec parameter.
    
    This commit also fixes a little bug. In the error path of
    DuplicateHandle there was:
    
        win32_maperr(GetLastError());
        return -1;
    
    but the function returns a value, and from the OCaml code above it's
    better to raise an exception instead.
    
    [DuplicateHandle]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle
    [WSADuplicateSocket]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaduplicatesocketw
    MisterDA committed Oct 28, 2021
    Configuration menu
    Copy the full SHA
    2ecf1ec View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    d1d4352 View commit details
    Browse the repository at this point in the history