ReactOS 0.4.16-dev-822-gbcedb53
dup.cpp
Go to the documentation of this file.
1//
2// dup.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _dup() and _dup_nolock, which duplicate lowio file handles
7//
10
11static int __cdecl duplicate_osfhnd(int const fh, int const new_fh, __crt_cached_ptd_host& ptd) throw()
12{
13 // Duplicate the file handle:
14 intptr_t new_osfhandle;
15
18 reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
20 &reinterpret_cast<HANDLE&>(new_osfhandle),
21 0L,
22 TRUE,
24
25 if (!result)
26 {
28 return -1;
29 }
30
31 // Duplicate the handle state:
32 __acrt_lowio_set_os_handle(new_fh, new_osfhandle);
33 _osfile(new_fh) = _osfile(fh) & ~FNOINHERIT;
34 _textmode(new_fh) = _textmode(fh);
35 _tm_unicode(new_fh) = _tm_unicode(fh);
36 return new_fh;
37}
38
39static int __cdecl _dup_nolock_internal(int const fh, __crt_cached_ptd_host& ptd) throw()
40{
41 if ((_osfile(fh) & FOPEN) == 0)
42 {
43 ptd.get_errno().set(EBADF);
44 ptd.get_doserrno().set(0);
45 _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread", 0));
46 return -1;
47 }
48
49 // Allocate a duplicate handle
50 int const new_fh = _alloc_osfhnd();
51 if (new_fh == -1)
52 {
53 ptd.get_errno().set(EMFILE);
54 ptd.get_doserrno().set(0);
55 return -1;
56 }
57
58 int return_value = -1;
59 __try
60 {
61 return_value = duplicate_osfhnd(fh, new_fh, ptd);
62 }
64 {
65 // The handle returned by _alloc_osfhnd is both open and locked. If we
66 // failed to duplicate the handle, we need to abandon the handle by
67 // unsetting the open flag. We always need to unlock the handle:
68 if (return_value == -1)
69 {
70 _osfile(new_fh) &= ~FOPEN;
71 }
72
74 }
76 return return_value;
77}
78
79static int __cdecl _dup_internal(int const fh, __crt_cached_ptd_host& ptd)
80{
82 _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
84
85 return __acrt_lowio_lock_fh_and_call(fh, [&](){
86 return _dup_nolock_internal(fh, ptd);
87 });
88}
89
90// _dup() duplicates a file handle and returns the duplicate. If the function
91// fails, -1 is returned and errno is set.
92extern "C" int __cdecl _dup(int const fh)
93{
94 __crt_cached_ptd_host ptd;
95 return _dup_internal(fh, ptd);
96}
#define EBADF
Definition: acclib.h:82
#define __cdecl
Definition: accygwin.h:79
int _nhandle
Definition: ioinit.cpp:34
int __cdecl _alloc_osfhnd(void)
Definition: osfinfo.cpp:116
int __cdecl __acrt_lowio_set_os_handle(int, intptr_t)
Definition: osfinfo.cpp:195
auto __acrt_lowio_lock_fh_and_call(int const fh, Action &&action) -> decltype(action())
void __cdecl __acrt_lowio_unlock_fh(_In_ int _FileHandle)
#define _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, expr, errorcode, retexpr)
#define _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, handle, errorcode, retexpr)
#define _ASSERTE(expr)
Definition: crtdbg.h:114
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
#define TRUE
Definition: types.h:120
#define GetCurrentProcess()
Definition: compat.h:759
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
#define EMFILE
Definition: errno.h:30
static int __cdecl _dup_nolock_internal(int const fh, __crt_cached_ptd_host &ptd)
Definition: dup.cpp:39
static int __cdecl duplicate_osfhnd(int const fh, int const new_fh, __crt_cached_ptd_host &ptd)
Definition: dup.cpp:11
int __cdecl _dup(int const fh)
Definition: dup.cpp:92
static int __cdecl _dup_internal(int const fh, __crt_cached_ptd_host &ptd)
Definition: dup.cpp:79
void __cdecl __acrt_errno_map_os_error_ptd(unsigned long const oserrno, __crt_cached_ptd_host &ptd)
Definition: errno.cpp:97
unsigned int BOOL
Definition: ntddk_ex.h:94
#define FOPEN
GLuint64EXT * result
Definition: glext.h:11304
#define L(x)
Definition: ntvdm.h:50
#define __try
Definition: pseh2_64.h:172
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174
_CRTIMP intptr_t __cdecl _get_osfhandle(_In_ int _FileHandle)
#define _osfile(i)
Definition: internal.h:72
#define _tm_unicode(i)
Definition: internal.h:75
#define _textmode(i)
Definition: internal.h:74
int intptr_t
Definition: vcruntime.h:134
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define DUPLICATE_SAME_ACCESS