1 | /* $Id: RTHandleGetStandard-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTHandleGetStandard, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/handle.h>
|
---|
43 |
|
---|
44 | #include <iprt/file.h>
|
---|
45 | #include <iprt/pipe.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 | #include <iprt/log.h>
|
---|
49 |
|
---|
50 | #include <iprt/win/windows.h>
|
---|
51 |
|
---|
52 | #include "internal/socket.h" /* (Needs Windows.h.) */
|
---|
53 | #include "internal-r3-win.h" /* (Needs Windows.h.) */
|
---|
54 |
|
---|
55 |
|
---|
56 | RTDECL(int) RTHandleGetStandard(RTHANDLESTD enmStdHandle, bool fLeaveOpen, PRTHANDLE ph)
|
---|
57 | {
|
---|
58 | /*
|
---|
59 | * Validate and convert input.
|
---|
60 | */
|
---|
61 | AssertPtrReturn(ph, VERR_INVALID_POINTER);
|
---|
62 | DWORD dwStdHandle;
|
---|
63 | switch (enmStdHandle)
|
---|
64 | {
|
---|
65 | case RTHANDLESTD_INPUT: dwStdHandle = STD_INPUT_HANDLE; break;
|
---|
66 | case RTHANDLESTD_OUTPUT: dwStdHandle = STD_OUTPUT_HANDLE; break;
|
---|
67 | case RTHANDLESTD_ERROR: dwStdHandle = STD_ERROR_HANDLE; break;
|
---|
68 | default:
|
---|
69 | AssertFailedReturn(VERR_INVALID_PARAMETER);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Is the requested descriptor valid and which IPRT handle type does it
|
---|
74 | * best map on to?
|
---|
75 | */
|
---|
76 | HANDLE hNative = GetStdHandle(dwStdHandle);
|
---|
77 | if (hNative == INVALID_HANDLE_VALUE)
|
---|
78 | return RTErrConvertFromWin32(GetLastError());
|
---|
79 |
|
---|
80 | DWORD dwInfo = 0;
|
---|
81 | if (g_pfnGetHandleInformation && !g_pfnGetHandleInformation(hNative, &dwInfo))
|
---|
82 | return RTErrConvertFromWin32(GetLastError());
|
---|
83 | bool const fInherit = RT_BOOL(dwInfo & HANDLE_FLAG_INHERIT);
|
---|
84 |
|
---|
85 | SetLastError(NO_ERROR);
|
---|
86 | RTHANDLE h;
|
---|
87 | DWORD dwType = GetFileType(hNative);
|
---|
88 | switch (dwType & ~FILE_TYPE_REMOTE)
|
---|
89 | {
|
---|
90 | case FILE_TYPE_UNKNOWN:
|
---|
91 | if (GetLastError() != NO_ERROR)
|
---|
92 | return RTErrConvertFromWin32(GetLastError());
|
---|
93 | RT_FALL_THROUGH();
|
---|
94 | default:
|
---|
95 | case FILE_TYPE_CHAR:
|
---|
96 | case FILE_TYPE_DISK:
|
---|
97 | h.enmType = RTHANDLETYPE_FILE;
|
---|
98 | break;
|
---|
99 |
|
---|
100 | case FILE_TYPE_PIPE:
|
---|
101 | {
|
---|
102 | DWORD cMaxInstances;
|
---|
103 | DWORD fInfo;
|
---|
104 | if (!GetNamedPipeInfo(hNative, &fInfo, NULL, NULL, &cMaxInstances))
|
---|
105 | h.enmType = RTHANDLETYPE_SOCKET;
|
---|
106 | else
|
---|
107 | h.enmType = RTHANDLETYPE_PIPE;
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Create the IPRT handle.
|
---|
114 | */
|
---|
115 | int rc;
|
---|
116 | switch (h.enmType)
|
---|
117 | {
|
---|
118 | case RTHANDLETYPE_FILE:
|
---|
119 | /** @todo fLeaveOpen */
|
---|
120 | rc = RTFileFromNative(&h.u.hFile, (RTHCUINTPTR)hNative);
|
---|
121 | break;
|
---|
122 |
|
---|
123 | case RTHANDLETYPE_PIPE:
|
---|
124 | rc = RTPipeFromNative(&h.u.hPipe, (RTHCUINTPTR)hNative,
|
---|
125 | (enmStdHandle == RTHANDLESTD_INPUT ? RTPIPE_N_READ : RTPIPE_N_WRITE)
|
---|
126 | | (fInherit ? RTPIPE_N_INHERIT : 0)
|
---|
127 | | (fLeaveOpen ? RTPIPE_N_LEAVE_OPEN : 0));
|
---|
128 | break;
|
---|
129 |
|
---|
130 | case RTHANDLETYPE_SOCKET:
|
---|
131 | rc = rtSocketCreateForNative(&h.u.hSocket, (RTHCUINTPTR)hNative, fLeaveOpen);
|
---|
132 | break;
|
---|
133 |
|
---|
134 | default: /* shut up gcc */
|
---|
135 | return VERR_INTERNAL_ERROR;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | *ph = h;
|
---|
140 |
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|