Apache Mesos
dup.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_WINDOWS_DUP_HPP__
14 #define __STOUT_OS_WINDOWS_DUP_HPP__
15 
16 #include <stout/error.hpp>
17 #include <stout/try.hpp>
18 #include <stout/unreachable.hpp>
19 #include <stout/windows.hpp> // For `WinSock2.h`.
20 
21 #include <stout/os/windows/fd.hpp>
22 
23 namespace os {
24 
25 inline Try<WindowsFD> dup(const WindowsFD& fd)
26 {
27  switch (fd.type()) {
28  case WindowsFD::FD_CRT:
29  case WindowsFD::FD_HANDLE: {
30  int result = ::_dup(fd.crt());
31  if (result == -1) {
32  return ErrnoError();
33  }
34  return result;
35  }
36  case WindowsFD::FD_SOCKET: {
37 #pragma warning(push)
38 #pragma warning(disable : 4996)
39  // Disable compiler warning asking us to use the Unicode version of
40  // `WSASocket` and `WSADuplicateSocket`, because Mesos currently does not
41  // support Unicode. See MESOS-6817.
42  WSAPROTOCOL_INFO protInfo;
43  if (::WSADuplicateSocket(fd, GetCurrentProcessId(), &protInfo) !=
44  INVALID_SOCKET) {
45  return WSASocket(0, 0, 0, &protInfo, 0, 0);
46  };
47 #pragma warning(pop)
48  return SocketError();
49  }
50  }
51  UNREACHABLE();
52 }
53 
54 } // namespace os {
55 
56 #endif // __STOUT_OS_WINDOWS_DUP_HPP__
Definition: fd.hpp:54
Definition: fd.hpp:53
Type type() const
Definition: fd.hpp:121
Definition: fd.hpp:47
Definition: try.hpp:34
Definition: errorbase.hpp:49
ErrnoError SocketError
Definition: error.hpp:33
#define UNREACHABLE()
Definition: unreachable.hpp:22
int crt() const
Definition: fd.hpp:97
Definition: fd.hpp:52
Try< int > dup(int fd)
Definition: dup.hpp:23