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  // TODO(andschwa): Replace this with `::DuplicateHandle` after figuring
31  // out how to make it compatible with handles to stdin/stdout/stderr, as
32  // well as defining sane inheritance semantics.
33  int result = ::_dup(fd.crt());
34  if (result == -1) {
35  return ErrnoError();
36  }
37 
38  return result;
39  }
40  case WindowsFD::FD_SOCKET: {
41  WSAPROTOCOL_INFOW info;
42  const int result =
43  ::WSADuplicateSocketW(fd, ::GetCurrentProcessId(), &info);
44  if (result != 0) {
45  return SocketError();
46  }
47 
48  return ::WSASocketW(0, 0, 0, &info, 0, 0);
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: check.hpp:33
Definition: errorbase.hpp:49
Definition: posix_signalhandler.hpp:23
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