ReactOS 0.4.16-dev-853-g88d9285
ioinit.cpp File Reference
Include dependency graph for ioinit.cpp:

Go to the source code of this file.

Functions

static DWORD __cdecl get_std_handle_id (int const fh) throw ()
 
static void __cdecl initialize_inherited_file_handles_nolock () throw ()
 
static void initialize_stdio_handles_nolock () throw ()
 
bool __cdecl __acrt_initialize_lowio ()
 
bool __cdecl __acrt_uninitialize_lowio (bool const)
 

Variables

__crt_lowio_handle_data __badioinfo
 
int _nhandle = 0
 
__crt_lowio_handle_data__pioinfo [IOINFO_ARRAYS] = { 0 }
 

Function Documentation

◆ __acrt_initialize_lowio()

bool __cdecl __acrt_initialize_lowio ( void  )

Definition at line 224 of file ioinit.cpp.

225{
227 bool result = false;
228 __try
229 {
230 // First, allocate and initialize the initial array of lowio files:
232 __leave;
233
234 // Next, process and initialize all inherited file handles:
236
237 // Finally, initialize the stdio handles, if they were not inherited:
239 result = true;
240 }
242 {
244 }
246
247 return result;
248}
void __cdecl __acrt_unlock(_In_ __acrt_lock_id lock)
Definition: locks.cpp:57
@ __acrt_lowio_index_lock
_Check_return_opt_ errno_t __cdecl __acrt_lowio_ensure_fh_exists(_In_ int _FileHandle)
__acrt_lock(__acrt_heap_lock)
GLuint64EXT * result
Definition: glext.h:11304
static void initialize_stdio_handles_nolock()
Definition: ioinit.cpp:123
static void __cdecl initialize_inherited_file_handles_nolock()
Definition: ioinit.cpp:59
#define __try
Definition: pseh2_64.h:172
#define __leave
Definition: pseh2_64.h:176
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174

◆ __acrt_uninitialize_lowio()

bool __cdecl __acrt_uninitialize_lowio ( bool const  )

Definition at line 254 of file ioinit.cpp.

255{
256 for (size_t i = 0; i < IOINFO_ARRAYS; ++i)
257 {
258 if (!__pioinfo[i])
259 continue;
260
262 __pioinfo[i] = nullptr;
263 }
264
265 return true;
266}
void __cdecl __acrt_lowio_destroy_handle_array(_Pre_maybenull_ _Post_invalid_ _In_reads_opt_(IOINFO_ARRAY_ELTS) __crt_lowio_handle_data *_Array)
#define IOINFO_ARRAYS
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
__crt_lowio_handle_data * __pioinfo[IOINFO_ARRAYS]
Definition: ioinit.cpp:39

◆ get_std_handle_id()

static DWORD __cdecl get_std_handle_id ( int const  fh)
throw (
)
static

Definition at line 43 of file ioinit.cpp.

44{
45 // Convert the CRT file handle to the OS file handle for the three
46 // standard streams:
47 switch (fh)
48 {
49 case 0: return STD_INPUT_HANDLE;
50 case 1: return STD_OUTPUT_HANDLE;
51 case 2: return STD_ERROR_HANDLE;
52 }
53
54 return STD_ERROR_HANDLE; // Unreachable, but the compiler can't know.
55}
#define STD_OUTPUT_HANDLE
Definition: winbase.h:294
#define STD_INPUT_HANDLE
Definition: winbase.h:293
#define STD_ERROR_HANDLE
Definition: winbase.h:295

Referenced by initialize_stdio_handles_nolock().

◆ initialize_inherited_file_handles_nolock()

static void __cdecl initialize_inherited_file_handles_nolock ( )
throw (
)
static

Definition at line 59 of file ioinit.cpp.

60{
61 STARTUPINFOW startup_info;
62 GetStartupInfoW(&startup_info);
63
64 // First check and see if we inherited any file handles. If we didn't, then
65 // we don't have anything to initialize:
66 if (startup_info.cbReserved2 == 0 || startup_info.lpReserved2 == nullptr)
67 return;
68
69 // Get the number of inherited handles:
70 int const handle_count = *reinterpret_cast<UNALIGNED int*>(startup_info.lpReserved2);
71
72 // Compute the start of the passed file info and OS HANDLEs:
73 unsigned char* const first_file =
74 reinterpret_cast<unsigned char*>(startup_info.lpReserved2) + sizeof(int);
75
76 UNALIGNED intptr_t* const first_handle =
77 reinterpret_cast<UNALIGNED intptr_t*>(first_file + handle_count);
78
79 // Do not attempt to inherit more than the maximum number of supported handles:
80 int handles_to_inherit = handle_count < _NHANDLE_
81 ? handle_count
82 : _NHANDLE_;
83
84 // Attempt to allocate the required number of handles. If we fail for any
85 // reason, we'll inherit as many handles as we can:
86 __acrt_lowio_ensure_fh_exists(handles_to_inherit);
87 if (handles_to_inherit > _nhandle)
88 handles_to_inherit = _nhandle;
89
90 // Validate and copy the provided file information:
91 unsigned char* it_file = first_file;
92 UNALIGNED intptr_t* it_handle = first_handle;
93
94 for (int fh = 0; fh != handles_to_inherit; ++fh, ++it_file, ++it_handle)
95 {
96 HANDLE const real_handle = reinterpret_cast<HANDLE>(*it_handle);
97
98 // If the provided information does not appear to describe an open,
99 // valid file or device, skip it:
100 if (real_handle == INVALID_HANDLE_VALUE)
101 continue;
102
103 if (*it_handle == _NO_CONSOLE_FILENO)
104 continue;
105
106 if ((*it_file & FOPEN) == 0)
107 continue;
108
109 // GetFileType cannot be called for pipe handles since it may "hang" if
110 // there is a blocked read pending on the pipe in the parent.
111 if ((*it_file & FPIPE) == 0 && GetFileType(real_handle) == FILE_TYPE_UNKNOWN)
112 continue;
113
114 // Okay, the file looks valid:
115 __crt_lowio_handle_data* const pio = _pioinfo(fh);
116 pio->osfhnd = *it_handle;
117 pio->osfile = *it_file;
118 }
119}
#define _NHANDLE_
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:269
VOID WINAPI GetStartupInfoW(IN LPSTARTUPINFOW lpStartupInfo)
Definition: proc.c:1279
#define FOPEN
int _nhandle
Definition: ioinit.cpp:34
static struct file_entry * first_file
Definition: makefsdata.c:165
#define UNALIGNED
Definition: pecoff.h:227
#define _NO_CONSOLE_FILENO
Definition: internal.h:92
#define _pioinfo(i)
Definition: internal.h:71
PBYTE lpReserved2
Definition: winbase.h:891
WORD cbReserved2
Definition: winbase.h:890
int intptr_t
Definition: vcruntime.h:134
#define FILE_TYPE_UNKNOWN
Definition: winbase.h:284

Referenced by __acrt_initialize_lowio().

◆ initialize_stdio_handles_nolock()

static void initialize_stdio_handles_nolock ( )
throw (
)
static

Definition at line 123 of file ioinit.cpp.

124{
125 for (int fh = 0; fh != STDIO_HANDLES_COUNT; ++fh)
126 {
127 __crt_lowio_handle_data* const pio = _pioinfo(fh);
128
129 // If this handle was inherited from the parent process and initialized
130 // already, make sure it has the FTEXT flag and continue:
131 if (pio->osfhnd != reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE) &&
133 {
134 pio->osfile |= FTEXT;
135 continue;
136 }
137
138 // Regardless what happens next, the file will be treated as if it is
139 // open in text mode:
140 pio->osfile = FOPEN | FTEXT;
141
142 // This handle has not yet been initialized, so let's see if we can get
143 // the handle from the OS:
144 intptr_t const os_handle = reinterpret_cast<intptr_t>(GetStdHandle(get_std_handle_id(fh)));
145
146 bool const is_valid_handle =
147 os_handle != reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE) &&
148 os_handle != reinterpret_cast<intptr_t>(nullptr);
149
150 DWORD const handle_type = is_valid_handle
151 ? GetFileType(reinterpret_cast<HANDLE>(os_handle))
153
154
155 if (handle_type != FILE_TYPE_UNKNOWN)
156 {
157 // The file type is known, so we obtained a valid handle from the
158 // OS. Finish initializing the lowio file object for this handle,
159 // including the flag specifying whether this is a character device
160 // or a pipe:
161 pio->osfhnd = os_handle;
162
163 if ((handle_type & 0xff) == FILE_TYPE_CHAR)
164 pio->osfile |= FDEV;
165
166 else if ((handle_type & 0xff) == FILE_TYPE_PIPE)
167 pio->osfile |= FPIPE;
168 }
169 else
170 {
171 // We were unable to get the handles from the OS. For stdin, stdout,
172 // and stderr, if there is no valid OS handle, treat the CRT handle
173 // as being open in text mode on a device with _NO_CONSOLE_FILENO
174 // underlying it. We use this value instead of INVALID_HANDLE_VALUE
175 // to distinguish between a failure in opening a file and a program
176 // run without a console:
177 pio->osfile |= FDEV;
179
180 // Also update the corresponding stdio stream, unless stdio was
181 // already terminated:
182 if (__piob)
184 }
185 }
186}
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define STDIO_HANDLES_COUNT
__crt_stdio_stream_data ** __piob
BOOL is_valid_handle(struct handle_table *lpTable, HCRYPTKEY handle, DWORD dwType)
Definition: handle.c:96
unsigned long DWORD
Definition: ntddk_ex.h:95
static DWORD __cdecl get_std_handle_id(int const fh)
Definition: ioinit.cpp:43
#define FILE_TYPE_CHAR
Definition: winbase.h:286
#define FILE_TYPE_PIPE
Definition: winbase.h:287

Referenced by __acrt_initialize_lowio().

Variable Documentation

◆ __badioinfo

Initial value:
=
{
{ },
static_cast<intptr_t>(-1),
0,
{ LF, LF, LF },
}
#define LF
Definition: telnetd.h:24

Definition at line 17 of file ioinit.cpp.

Referenced by Test___badioinfo().

◆ __pioinfo

Definition at line 39 of file ioinit.cpp.

Referenced by __acrt_uninitialize_lowio().

◆ _nhandle