ReactOS.org
Sign In
Sign Up
Sign In
Sign Up
Manage this list
×
Keyboard Shortcuts
Thread View
j
: Next unread message
k
: Previous unread message
j a
: Jump to all threads
j l
: Jump to MailingList overview
2024
December
November
October
September
August
July
June
May
April
March
February
January
2023
December
November
October
September
August
July
June
May
April
March
February
January
2022
December
November
October
September
August
July
June
May
April
March
February
January
2021
December
November
October
September
August
July
June
May
April
March
February
January
2020
December
November
October
September
August
July
June
May
April
March
February
January
2019
December
November
October
September
August
July
June
May
April
March
February
January
2018
December
November
October
September
August
July
June
May
April
March
February
January
2017
December
November
October
September
August
July
June
May
April
March
February
January
2016
December
November
October
September
August
July
June
May
April
March
February
January
2015
December
November
October
September
August
July
June
May
April
March
February
January
2014
December
November
October
September
August
July
June
May
April
March
February
January
2013
December
November
October
September
August
July
June
May
April
March
February
January
2012
December
November
October
September
August
July
June
May
April
March
February
January
2011
December
November
October
September
August
July
June
May
April
March
February
January
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008
December
November
October
September
August
July
June
May
April
March
February
January
2007
December
November
October
September
August
July
June
May
April
March
February
January
2006
December
November
October
September
August
July
June
May
April
March
February
January
2005
December
November
October
September
August
July
June
May
April
March
February
January
2004
December
November
October
September
August
July
June
May
April
March
February
List overview
Download
Ros-diffs
March 2018
----- 2024 -----
December 2024
November 2024
October 2024
September 2024
August 2024
July 2024
June 2024
May 2024
April 2024
March 2024
February 2024
January 2024
----- 2023 -----
December 2023
November 2023
October 2023
September 2023
August 2023
July 2023
June 2023
May 2023
April 2023
March 2023
February 2023
January 2023
----- 2022 -----
December 2022
November 2022
October 2022
September 2022
August 2022
July 2022
June 2022
May 2022
April 2022
March 2022
February 2022
January 2022
----- 2021 -----
December 2021
November 2021
October 2021
September 2021
August 2021
July 2021
June 2021
May 2021
April 2021
March 2021
February 2021
January 2021
----- 2020 -----
December 2020
November 2020
October 2020
September 2020
August 2020
July 2020
June 2020
May 2020
April 2020
March 2020
February 2020
January 2020
----- 2019 -----
December 2019
November 2019
October 2019
September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
----- 2018 -----
December 2018
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
----- 2017 -----
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
February 2017
January 2017
----- 2016 -----
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
----- 2015 -----
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
----- 2014 -----
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
----- 2013 -----
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
----- 2012 -----
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
----- 2011 -----
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
----- 2010 -----
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
----- 2009 -----
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
----- 2008 -----
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
----- 2007 -----
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
----- 2006 -----
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
----- 2005 -----
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
----- 2004 -----
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
ros-diffs@reactos.org
25 participants
436 discussions
Start a n
N
ew thread
01/01: [CRT] Fixed _tstat64 which wrongly set executable bits on files. CORE-10904 Replaced some string functions with their tchar.h analogs
by Victor Perevertkin
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=65eabed2e1c07c3b1c8a8…
commit 65eabed2e1c07c3b1c8a8beb2a42676a45783897 Author: Victor Perevertkin <victor(a)perevertkin.ru> AuthorDate: Sun Mar 25 00:33:57 2018 +0300 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:41:27 2018 +0200 [CRT] Fixed _tstat64 which wrongly set executable bits on files. CORE-10904 Replaced some string functions with their tchar.h analogs --- sdk/lib/crt/stdio/stat64.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/sdk/lib/crt/stdio/stat64.c b/sdk/lib/crt/stdio/stat64.c index 98e63c57fb..26feb528a5 100644 --- a/sdk/lib/crt/stdio/stat64.c +++ b/sdk/lib/crt/stdio/stat64.c @@ -16,29 +16,28 @@ inline void release_ioinfo(ioinfo *info); #define TCHAR4 ULONG #endif -#define TCSIZE sizeof(_TCHAR) -#define TOULL(x) ((TCHAR4)(x)) +#define TCSIZE_BITS (sizeof(_TCHAR)*8) -#define EXE ((TOULL('e')<<(2*TCSIZE)) | (TOULL('x')<<TCSIZE) | (TOULL('e'))) -#define BAT ((TOULL('b')<<(2*TCSIZE)) | (TOULL('a')<<TCSIZE) | (TOULL('t'))) -#define CMD ((TOULL('c')<<(2*TCSIZE)) | (TOULL('m')<<TCSIZE) | (TOULL('d'))) -#define COM ((TOULL('c')<<(2*TCSIZE)) | (TOULL('o')<<TCSIZE) | (TOULL('m'))) +#define EXE (((TCHAR4)('e')<<(2*TCSIZE_BITS)) | ((TCHAR4)('x')<<TCSIZE_BITS) | ((TCHAR4)('e'))) +#define BAT (((TCHAR4)('b')<<(2*TCSIZE_BITS)) | ((TCHAR4)('a')<<TCSIZE_BITS) | ((TCHAR4)('t'))) +#define CMD (((TCHAR4)('c')<<(2*TCSIZE_BITS)) | ((TCHAR4)('m')<<TCSIZE_BITS) | ((TCHAR4)('d'))) +#define COM (((TCHAR4)('c')<<(2*TCSIZE_BITS)) | ((TCHAR4)('o')<<TCSIZE_BITS) | ((TCHAR4)('m'))) int CDECL _tstat64(const _TCHAR *path, struct __stat64 *buf) { DWORD dw; WIN32_FILE_ATTRIBUTE_DATA hfi; unsigned short mode = ALL_S_IREAD; - int plen; + size_t plen; TRACE(":file (%s) buf(%p)\n",path,buf); plen = _tcslen(path); - while (plen && path[plen-1]==' ') + while (plen && path[plen-1]==__T(' ')) plen--; - if (plen && (plen<2 || path[plen-2]!=':') && - (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/')) + if (plen && (plen<2 || path[plen-2]!=__T(':')) && + (path[plen-1]==__T(':') || path[plen-1]==__T('\\') || path[plen-1]==__T('/'))) { *_errno() = ENOENT; return -1; @@ -58,8 +57,8 @@ int CDECL _tstat64(const _TCHAR *path, struct __stat64 *buf) Also a letter as first char isn't enough to be classified as a drive letter */ - if (isalpha(*path)&& (*(path+1)==':')) - buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */ + if (_istalpha(*path) && (*(path+1)==__T(':'))) + buf->st_dev = buf->st_rdev = _totupper(*path) - __T('A'); /* drive num */ else buf->st_dev = buf->st_rdev = _getdrive() - 1; @@ -70,10 +69,13 @@ int CDECL _tstat64(const _TCHAR *path, struct __stat64 *buf) { mode |= _S_IFREG; /* executable? */ - if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */ + if (plen > 6 && path[plen-4] == __T('.')) /* shortest exe: "\x.exe" */ { - unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) | - (tolower(path[plen-3]) << 16); + + TCHAR4 ext = (TCHAR4)_totlower(path[plen-1]) + | ((TCHAR4)_totlower(path[plen-2]) << TCSIZE_BITS) + | ((TCHAR4)_totlower(path[plen-3]) << 2*TCSIZE_BITS); + if (ext == EXE || ext == BAT || ext == CMD || ext == COM) mode |= ALL_S_IEXEC; }
6 years, 9 months
1
0
0
0
01/01: [CRT] Various fixes to Wine file functions.
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=2765bddffa434dc4a6949…
commit 2765bddffa434dc4a6949dfcad8fce85c9564363 Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 18:34:56 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:35:02 2018 +0200 [CRT] Various fixes to Wine file functions. Import the following Wine commits: * ea2798f1ce9 Iván Matellanes: msvcrt: _setmode should check if file descriptor is valid. * 9b495caacfd Carlo Bramini: msvcrt: Call SetFilePointer() with correct parameters. * 13f34481b26 Carlo Bramini: msvcrt: Use MSVCRT_xxx macros instead of the same ones from GLIBC. * 781b069ed81 Piotr Caban: msvcrt: Don't close HANDLE if stdout or stderr is sharing it. --- sdk/lib/crt/stdio/file.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index b4c66856a3..2357d99bfc 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -936,6 +936,14 @@ int CDECL _close(int fd) TRACE(":fd (%d) handle (%p)\n", fd, info->handle); if (!(info->wxflag & WX_OPEN)) { ret = -1; + } else if (fd == STDOUT_FILENO && + info->handle == get_ioinfo_nolock(STDERR_FILENO)->handle) { + msvcrt_free_fd(fd); + ret = 0; + } else if (fd == STDERR_FILENO && + info->handle == get_ioinfo_nolock(STDOUT_FILENO)->handle) { + msvcrt_free_fd(fd); + ret = 0; } else { ret = CloseHandle(info->handle) ? 0 : -1; msvcrt_free_fd(fd); @@ -1237,7 +1245,7 @@ int CDECL _locking(int fd, int mode, LONG nbytes) (mode==_LK_NBRLCK)?"_LK_NBRLCK": "UNKNOWN"); - if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER) + if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER) { release_ioinfo(info); FIXME ("Seek failed\n"); @@ -2480,6 +2488,11 @@ int CDECL _setmode(int fd,int mode) return -1; } + if(info == &__badioinfo) { + *_errno() = EBADF; + return EOF; + } + if(mode == _O_BINARY) { info->wxflag &= ~WX_TEXT; info->exflag &= ~(EF_UTF8|EF_UTF16);
6 years, 9 months
1
0
0
0
07/07: [CRT] Update file descriptor handling to match Wine (7/7). CORE-14504
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=6aa8e3cc20345320ddb5d…
commit 6aa8e3cc20345320ddb5d08010fdf415c9c5526c Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 18:23:18 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:01:00 2018 +0200 [CRT] Update file descriptor handling to match Wine (7/7). CORE-14504 Import Wine commits by Piotr Caban: * 6ed69c107f8 msvcrt: Avoid using global critical section while allocating new file descriptors. * 725e4733cf8 msvcrt: Remove no longer needed global lock from functions using fd critical sections. --- sdk/lib/crt/stdio/file.c | 152 +++++++++++++++-------------------------------- 1 file changed, 47 insertions(+), 105 deletions(-) diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index 3458343145..b4c66856a3 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -125,9 +125,6 @@ ioinfo * __pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 }; */ ioinfo __badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT }; -static int fdstart = 3; /* first unallocated fd */ -static int fdend = 3; /* highest allocated fd */ - typedef struct { FILE file; CRITICAL_SECTION crit; @@ -143,12 +140,9 @@ static int MSVCRT_umask = 0; /* INTERNAL: static data for tmpnam and _wtmpname functions */ static int tmpnam_unique; -/* This critical section protects the tables __pioinfo and fstreams, - * and their related indexes, fdstart, fdend, - * and MSVCRT_stream_idx, from race conditions. - * It doesn't protect against race conditions manipulating the underlying files - * or flags; doing so would probably be better accomplished with per-file - * protection, rather than locking the whole table for every change. +/* This critical section protects the MSVCRT_fstreams table + * and MSVCRT_stream_idx from race conditions. It also + * protects fd critical sections creation code. */ static CRITICAL_SECTION MSVCRT_file_cs; static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug = @@ -172,19 +166,24 @@ static inline ioinfo* get_ioinfo_nolock(int fd) return ret + (fd%MSVCRT_FD_BLOCK_SIZE); } -/*static*/ inline ioinfo* get_ioinfo(int fd) +static inline void init_ioinfo_cs(ioinfo *info) { - ioinfo *ret = get_ioinfo_nolock(fd); - if(ret == &__badioinfo) - return ret; - if(!(ret->exflag & EF_CRIT_INIT)) { + if(!(info->exflag & EF_CRIT_INIT)) { LOCK_FILES(); - if(!(ret->exflag & EF_CRIT_INIT)) { - InitializeCriticalSection(&ret->crit); - ret->exflag |= EF_CRIT_INIT; + if(!(info->exflag & EF_CRIT_INIT)) { + InitializeCriticalSection(&info->crit); + info->exflag |= EF_CRIT_INIT; } UNLOCK_FILES(); } +} + +/*static*/ inline ioinfo* get_ioinfo(int fd) +{ + ioinfo *ret = get_ioinfo_nolock(fd); + if(ret == &__badioinfo) + return ret; + init_ioinfo_cs(ret); EnterCriticalSection(&ret->crit); return ret; } @@ -216,77 +215,49 @@ static inline BOOL alloc_pioinfo_block(int fd) static inline ioinfo* get_ioinfo_alloc_fd(int fd) { - ioinfo *ret, *info; + ioinfo *ret; ret = get_ioinfo(fd); if(ret != &__badioinfo) - { - LOCK_FILES(); - /* locate next free slot */ - if (fd == fdstart && fd == fdend) - fdstart = fdend + 1; - else if (fd == fdstart) - { - fdstart++; - while (fdstart < fdend && - ((info = get_ioinfo_nolock(fdstart))->exflag & EF_CRIT_INIT)) - { - if (TryEnterCriticalSection(&info->crit)) - { - if (info->handle == INVALID_HANDLE_VALUE) - { - LeaveCriticalSection(&info->crit); - break; - } - LeaveCriticalSection(&info->crit); - } - fdstart++; - } - } - /* update last fd in use */ - if (fd >= fdend) - fdend = fd + 1; - TRACE("fdstart is %d, fdend is %d\n", fdstart, fdend); - UNLOCK_FILES(); return ret; - } if(!alloc_pioinfo_block(fd)) return &__badioinfo; - ret = get_ioinfo(fd); - if(ret == &__badioinfo) - return ret; + return get_ioinfo(fd); +} - LOCK_FILES(); - /* locate next free slot */ - if (fd == fdstart && fd == fdend) - fdstart = fdend + 1; - else if (fd == fdstart) +static inline ioinfo* get_ioinfo_alloc(int *fd) +{ + int i; + + *fd = -1; + for(i=0; i<MSVCRT_MAX_FILES; i++) { - fdstart++; - while (fdstart < fdend && - ((info = get_ioinfo_nolock(fdstart))->exflag & EF_CRIT_INIT)) + ioinfo *info = get_ioinfo_nolock(i); + + if(info == &__badioinfo) + { + if(!alloc_pioinfo_block(i)) + return &__badioinfo; + info = get_ioinfo_nolock(i); + } + + init_ioinfo_cs(info); + if(TryEnterCriticalSection(&info->crit)) { - if (TryEnterCriticalSection(&info->crit)) + if(info->handle == INVALID_HANDLE_VALUE) { - if (info->handle == INVALID_HANDLE_VALUE) - { - LeaveCriticalSection(&info->crit); - break; - } - LeaveCriticalSection(&info->crit); + *fd = i; + return info; } - fdstart++; + LeaveCriticalSection(&info->crit); } } - /* update last fd in use */ - if (fd >= fdend) - fdend = fd + 1; - TRACE("fdstart is %d, fdend is %d\n", fdstart, fdend); - UNLOCK_FILES(); - return ret; + WARN(":files exhausted!\n"); + *_errno() = ENFILE; + return &__badioinfo; } /*static*/ inline void release_ioinfo(ioinfo *info) @@ -349,13 +320,6 @@ static void msvcrt_free_fd(int fd) } } release_ioinfo(fdinfo); - - LOCK_FILES(); - if (fd == fdend - 1) - fdend--; - if (fd < fdstart) - fdstart = fd; - UNLOCK_FILES(); } static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag) @@ -378,23 +342,11 @@ static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag) /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */ /*static*/ int msvcrt_alloc_fd(HANDLE hand, int flag) { - ioinfo *info; int fd; + ioinfo *info = get_ioinfo_alloc(&fd); - LOCK_FILES(); - fd = fdstart; TRACE(":handle (%p) allocating fd (%d)\n", hand, fd); - if (fd >= MSVCRT_MAX_FILES) - { - UNLOCK_FILES(); - WARN(":files exhausted!\n"); - *_errno() = ENFILE; - return -1; - } - - info = get_ioinfo_alloc_fd(fd); - UNLOCK_FILES(); if(info == &__badioinfo) return -1; @@ -533,9 +485,6 @@ void msvcrt_init_io(void) wxflag_ptr++; handle_ptr++; } - fdend = max( 3, count ); - for (fdstart = 3; fdstart < fdend; fdstart++) - if (get_ioinfo_nolock(fdstart)->handle == INVALID_HANDLE_VALUE) break; } fdinfo = get_ioinfo_alloc_fd(STDIN_FILENO); @@ -984,7 +933,6 @@ int CDECL _close(int fd) ioinfo *info = get_ioinfo(fd); int ret; - LOCK_FILES(); TRACE(":fd (%d) handle (%p)\n", fd, info->handle); if (!(info->wxflag & WX_OPEN)) { ret = -1; @@ -997,7 +945,6 @@ int CDECL _close(int fd) ret = -1; } } - UNLOCK_FILES(); release_ioinfo(info); return ret; } @@ -1015,7 +962,6 @@ int CDECL _dup2(int od, int nd) int ret; TRACE("(od=%d, nd=%d)\n", od, nd); - LOCK_FILES(); if (od < nd) { @@ -1062,7 +1008,6 @@ int CDECL _dup2(int od, int nd) release_ioinfo(info_od); release_ioinfo(info_nd); - UNLOCK_FILES(); return ret; } @@ -1072,14 +1017,13 @@ int CDECL _dup2(int od, int nd) int CDECL _dup(int od) { int fd, ret; + ioinfo *info = get_ioinfo_alloc(&fd); - LOCK_FILES(); - fd = fdstart; if (_dup2(od, fd) == 0) ret = fd; else ret = -1; - UNLOCK_FILES(); + release_ioinfo(info); return ret; } @@ -1767,7 +1711,6 @@ int CDECL _pipe(int *pfds, unsigned int psize, int textmode) unsigned int wxflags = split_oflags(textmode); int fd; - LOCK_FILES(); fd = msvcrt_alloc_fd(readHandle, wxflags); if (fd != -1) { @@ -1791,7 +1734,6 @@ int CDECL _pipe(int *pfds, unsigned int psize, int textmode) CloseHandle(writeHandle); *_errno() = EMFILE; } - UNLOCK_FILES(); } else _dosmaperr(GetLastError()); @@ -3468,7 +3410,7 @@ FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE* file) TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1); LOCK_FILES(); - if (!file || ((fd = file->_file) < 0) || fd > fdend) + if (!file || ((fd = file->_file) < 0)) file = NULL; else {
6 years, 9 months
1
0
0
0
06/07: [CRT] Update file descriptor handling to match Wine (6/7). CORE-14504
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=27b79867ea55229636b14…
commit 27b79867ea55229636b140873da94dcd682eff7c Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 17:56:26 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:00:49 2018 +0200 [CRT] Update file descriptor handling to match Wine (6/7). CORE-14504 Import Wine commits by Piotr Caban: * 417b6056877 msvcrt: Fix possible deadlock in dup2 function. * e7fc2370885 msvcrt: Don't use fdend in create_io_inherit_block. --- sdk/lib/crt/stdio/file.c | 246 +++++++++++++++++++++++++++++++---------------- 1 file changed, 164 insertions(+), 82 deletions(-) diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index 4bdf91b21b..3458343145 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -189,6 +189,106 @@ static inline ioinfo* get_ioinfo_nolock(int fd) return ret; } +static inline BOOL alloc_pioinfo_block(int fd) +{ + ioinfo *block; + int i; + + if(fd<0 || fd>=MSVCRT_MAX_FILES) + { + *_errno() = ENFILE; + return FALSE; + } + + block = calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo)); + if(!block) + { + WARN(":out of memory!\n"); + *_errno() = ENOMEM; + return FALSE; + } + for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++) + block[i].handle = INVALID_HANDLE_VALUE; + if(InterlockedCompareExchangePointer((void**)&__pioinfo[fd/MSVCRT_FD_BLOCK_SIZE], block, NULL)) + free(block); + return TRUE; +} + +static inline ioinfo* get_ioinfo_alloc_fd(int fd) +{ + ioinfo *ret, *info; + + ret = get_ioinfo(fd); + if(ret != &__badioinfo) + { + LOCK_FILES(); + /* locate next free slot */ + if (fd == fdstart && fd == fdend) + fdstart = fdend + 1; + else if (fd == fdstart) + { + fdstart++; + while (fdstart < fdend && + ((info = get_ioinfo_nolock(fdstart))->exflag & EF_CRIT_INIT)) + { + if (TryEnterCriticalSection(&info->crit)) + { + if (info->handle == INVALID_HANDLE_VALUE) + { + LeaveCriticalSection(&info->crit); + break; + } + LeaveCriticalSection(&info->crit); + } + fdstart++; + } + } + /* update last fd in use */ + if (fd >= fdend) + fdend = fd + 1; + TRACE("fdstart is %d, fdend is %d\n", fdstart, fdend); + UNLOCK_FILES(); + return ret; + } + + if(!alloc_pioinfo_block(fd)) + return &__badioinfo; + + ret = get_ioinfo(fd); + if(ret == &__badioinfo) + return ret; + + LOCK_FILES(); + /* locate next free slot */ + if (fd == fdstart && fd == fdend) + fdstart = fdend + 1; + else if (fd == fdstart) + { + fdstart++; + while (fdstart < fdend && + ((info = get_ioinfo_nolock(fdstart))->exflag & EF_CRIT_INIT)) + { + if (TryEnterCriticalSection(&info->crit)) + { + if (info->handle == INVALID_HANDLE_VALUE) + { + LeaveCriticalSection(&info->crit); + break; + } + LeaveCriticalSection(&info->crit); + } + fdstart++; + } + } + /* update last fd in use */ + if (fd >= fdend) + fdend = fd + 1; + TRACE("fdstart is %d, fdend is %d\n", fdstart, fdend); + UNLOCK_FILES(); + + return ret; +} + /*static*/ inline void release_ioinfo(ioinfo *info) { if(info!=&__badioinfo && info->exflag & EF_CRIT_INIT) @@ -258,77 +358,49 @@ static void msvcrt_free_fd(int fd) UNLOCK_FILES(); } -/* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */ -/* caller must hold the files lock */ -static int msvcrt_set_fd(HANDLE hand, int flag, int fd) +static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag) { - ioinfo *fdinfo; - - if (fd >= MSVCRT_MAX_FILES) - { - WARN(":files exhausted!\n"); - *_errno() = ENFILE; - return -1; - } - - fdinfo = get_ioinfo_nolock(fd); - if(fdinfo == &__badioinfo) { - int i; - - __pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo)); - if(!__pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) { - WARN(":out of memory!\n"); - *_errno() = ENOMEM; - return -1; - } - - for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++) - __pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE; - - fdinfo = get_ioinfo_nolock(fd); - } - fdinfo->handle = hand; fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY)); fdinfo->lookahead[0] = '\n'; fdinfo->lookahead[1] = '\n'; fdinfo->lookahead[2] = '\n'; - if(!(fdinfo->exflag & EF_CRIT_INIT)) - InitializeCriticalSection(&fdinfo->crit); - fdinfo->exflag = EF_CRIT_INIT; + fdinfo->exflag &= EF_CRIT_INIT; - /* locate next free slot */ - if (fd == fdstart && fd == fdend) - fdstart = fdend + 1; - else - while (fdstart < fdend && - get_ioinfo_nolock(fdstart)->handle != INVALID_HANDLE_VALUE) - fdstart++; - /* update last fd in use */ - if (fd >= fdend) - fdend = fd + 1; - TRACE("fdstart is %d, fdend is %d\n", fdstart, fdend); - - switch (fd) + switch (fdinfo-__pioinfo[0]) { case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break; case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break; case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break; } - - return fd; } /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */ /*static*/ int msvcrt_alloc_fd(HANDLE hand, int flag) { - int ret; + ioinfo *info; + int fd; - LOCK_FILES(); - TRACE(":handle (%p) allocating fd (%d)\n",hand,fdstart); - ret = msvcrt_set_fd(hand, flag, fdstart); - UNLOCK_FILES(); - return ret; + LOCK_FILES(); + fd = fdstart; + TRACE(":handle (%p) allocating fd (%d)\n", hand, fd); + + if (fd >= MSVCRT_MAX_FILES) + { + UNLOCK_FILES(); + WARN(":files exhausted!\n"); + *_errno() = ENFILE; + return -1; + } + + info = get_ioinfo_alloc_fd(fd); + UNLOCK_FILES(); + if(info == &__badioinfo) + return -1; + + msvcrt_set_fd(info, hand, flag); + release_ioinfo(info); + return fd; } /* INTERNAL: Allocate a FILE* for an fd slot */ @@ -386,12 +458,17 @@ static int msvcrt_init_fp(FILE* file, int fd, unsigned stream_flags) */ unsigned create_io_inherit_block(WORD *size, BYTE **block) { - int fd; + int fd, last_fd; char* wxflag_ptr; HANDLE* handle_ptr; ioinfo* fdinfo; - *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * fdend; + for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--) + if (get_ioinfo_nolock(last_fd)->handle != INVALID_HANDLE_VALUE) + break; + last_fd++; + + *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd; *block = calloc(1, *size); if (!*block) { @@ -399,10 +476,10 @@ unsigned create_io_inherit_block(WORD *size, BYTE **block) return FALSE; } wxflag_ptr = (char*)*block + sizeof(unsigned); - handle_ptr = (HANDLE*)(wxflag_ptr + fdend * sizeof(char)); + handle_ptr = (HANDLE*)(wxflag_ptr + last_fd); - *(unsigned*)*block = fdend; - for (fd = 0; fd < fdend; fd++) + *(unsigned*)*block = last_fd; + for (fd = 0; fd < last_fd; fd++) { /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */ fdinfo = get_ioinfo(fd); @@ -447,7 +524,12 @@ void msvcrt_init_io(void) for (i = 0; i < count; i++) { if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE) - msvcrt_set_fd(*handle_ptr, *wxflag_ptr, i); + { + fdinfo = get_ioinfo_alloc_fd(i); + if(fdinfo != &__badioinfo) + msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr); + release_ioinfo(fdinfo); + } wxflag_ptr++; handle_ptr++; } @@ -456,32 +538,35 @@ void msvcrt_init_io(void) if (get_ioinfo_nolock(fdstart)->handle == INVALID_HANDLE_VALUE) break; } - fdinfo = get_ioinfo_nolock(STDIN_FILENO); + fdinfo = get_ioinfo_alloc_fd(STDIN_FILENO); if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) { HANDLE h = GetStdHandle(STD_INPUT_HANDLE); DWORD type = GetFileType(h); - msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0) - |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), STDIN_FILENO); + msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0) + |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0)); } + release_ioinfo(fdinfo); - fdinfo = get_ioinfo_nolock(STDOUT_FILENO); + fdinfo = get_ioinfo_alloc_fd(STDOUT_FILENO); if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); DWORD type = GetFileType(h); - msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0) - |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), STDOUT_FILENO); + msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0) + |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0)); } + release_ioinfo(fdinfo); - fdinfo = get_ioinfo_nolock(STDERR_FILENO); + fdinfo = get_ioinfo_alloc_fd(STDERR_FILENO); if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) { HANDLE h = GetStdHandle(STD_ERROR_HANDLE); DWORD type = GetFileType(h); - msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0) - |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), STDERR_FILENO); + msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0) + |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0)); } + release_ioinfo(fdinfo); TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO)->handle, get_ioinfo_nolock(STDOUT_FILENO)->handle, @@ -935,15 +1020,19 @@ int CDECL _dup2(int od, int nd) if (od < nd) { info_od = get_ioinfo(od); - info_nd = get_ioinfo(nd); + info_nd = get_ioinfo_alloc_fd(nd); } else { - info_nd = get_ioinfo(nd); + info_nd = get_ioinfo_alloc_fd(nd); info_od = get_ioinfo(od); } - if (nd < MSVCRT_MAX_FILES && nd >= 0 && (info_od->wxflag & WX_OPEN)) + if (info_nd == &__badioinfo) + { + ret = -1; + } + else if (info_od->wxflag & WX_OPEN) { HANDLE handle; @@ -954,17 +1043,10 @@ int CDECL _dup2(int od, int nd) if (info_nd->wxflag & WX_OPEN) _close(nd); - ret = msvcrt_set_fd(handle, wxflag, nd); - if (ret == -1) - { - CloseHandle(handle); - *_errno() = EMFILE; - } - else - { - /* _dup2 returns 0, not nd, on success */ - ret = 0; - } + + msvcrt_set_fd(info_nd, handle, wxflag); + /* _dup2 returns 0, not nd, on success */ + ret = 0; } else {
6 years, 9 months
1
0
0
0
05/07: [CRT] Update file descriptor handling to match Wine (5/7). CORE-14504
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=53864fcc5f55a4756423c…
commit 53864fcc5f55a4756423c739ec41349f3bbce954 Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 17:02:53 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:00:39 2018 +0200 [CRT] Update file descriptor handling to match Wine (5/7). CORE-14504 Import Wine commits by Piotr Caban: * 86baccc9ae9 msvcrt: Get rid of fdtoh helper. * bd6eaf918c5 msvcrt: Get rid of is_valid_fd helper. --- sdk/lib/crt/stdio/file.c | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index 329bf9679a..4bdf91b21b 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -221,31 +221,6 @@ static inline FILE* msvcrt_get_file(int i) return &ret->file; } -static inline BOOL is_valid_fd(int fd) -{ - return fd >= 0 && fd < fdend && (get_ioinfo_nolock(fd)->wxflag & WX_OPEN); -} - -/* INTERNAL: Get the HANDLE for a fd - * This doesn't lock the table, because a failure will result in - * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If - * it returns a valid handle which is about to be closed, a subsequent call - * will fail, most likely in a sane way. - */ -/*static*/ HANDLE fdtoh(int fd) -{ - if (!is_valid_fd(fd)) - { - WARN(":fd (%d) - no handle!\n",fd); - *__doserrno() = 0; - *_errno() = EBADF; - return INVALID_HANDLE_VALUE; - } - //if (get_ioinfo_nolock(fd)->handle == INVALID_HANDLE_VALUE) - //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd); - return get_ioinfo_nolock(fd)->handle; -} - /* INTERNAL: free a file entry fd */ static void msvcrt_free_fd(int fd) { @@ -383,7 +358,7 @@ static FILE* msvcrt_alloc_fp(void) static int msvcrt_init_fp(FILE* file, int fd, unsigned stream_flags) { TRACE(":fd (%d) allocating FILE*\n",fd); - if (!is_valid_fd(fd)) + if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN)) { WARN(":invalid fd %d\n",fd); *__doserrno() = 0; @@ -1581,9 +1556,11 @@ int CDECL _fileno(FILE* file) */ intptr_t CDECL _get_osfhandle(int fd) { - HANDLE hand = fdtoh(fd); + HANDLE hand = get_ioinfo_nolock(fd)->handle; TRACE(":fd (%d) handle (%p)\n",fd,hand); + if(hand == INVALID_HANDLE_VALUE) + *_errno() = EBADF; return (intptr_t)hand; }
6 years, 9 months
1
0
0
0
04/07: [CRT] Update file descriptor handling to match Wine (4/7). CORE-14504
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=bffd8201d0a822365e195…
commit bffd8201d0a822365e1950c558aaf33c26f20002 Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 16:31:10 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:00:25 2018 +0200 [CRT] Update file descriptor handling to match Wine (4/7). CORE-14504 Import Wine commits by Piotr Caban: * c7b0fb51543 msvcrt: Use fd critical section in _dup2. * b052afd4afa msvcrt: Use fd critical section in msvcrt_free_fd. * 16285f5700f msvcrt: Use fd critical section in read. * 9ce2a8a60c1 msvcrt: Use fd critical section in create_io_inherit_block. * f00b7f40584 msvcrt: Use fd critical section in _setmode. * ba757eaec74 msvcrt: Use fd critical section in _write. * eec7a565bec msvcrt: Don't lock __badioinfo. --- sdk/lib/crt/stdio/file.c | 108 +++++++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 36 deletions(-) diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index dde5a8ebba..329bf9679a 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -175,14 +175,23 @@ static inline ioinfo* get_ioinfo_nolock(int fd) /*static*/ inline ioinfo* get_ioinfo(int fd) { ioinfo *ret = get_ioinfo_nolock(fd); - if(ret->exflag & EF_CRIT_INIT) - EnterCriticalSection(&ret->crit); + if(ret == &__badioinfo) + return ret; + if(!(ret->exflag & EF_CRIT_INIT)) { + LOCK_FILES(); + if(!(ret->exflag & EF_CRIT_INIT)) { + InitializeCriticalSection(&ret->crit); + ret->exflag |= EF_CRIT_INIT; + } + UNLOCK_FILES(); + } + EnterCriticalSection(&ret->crit); return ret; } /*static*/ inline void release_ioinfo(ioinfo *info) { - if(info->exflag & EF_CRIT_INIT) + if(info!=&__badioinfo && info->exflag & EF_CRIT_INIT) LeaveCriticalSection(&info->crit); } @@ -240,10 +249,8 @@ static inline BOOL is_valid_fd(int fd) /* INTERNAL: free a file entry fd */ static void msvcrt_free_fd(int fd) { - ioinfo *fdinfo; + ioinfo *fdinfo = get_ioinfo(fd); - LOCK_FILES(); - fdinfo = get_ioinfo_nolock(fd); if(fdinfo != &__badioinfo) { fdinfo->handle = INVALID_HANDLE_VALUE; @@ -266,7 +273,9 @@ static void msvcrt_free_fd(int fd) break; } } + release_ioinfo(fdinfo); + LOCK_FILES(); if (fd == fdend - 1) fdend--; if (fd < fdstart) @@ -421,7 +430,7 @@ unsigned create_io_inherit_block(WORD *size, BYTE **block) for (fd = 0; fd < fdend; fd++) { /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */ - fdinfo = get_ioinfo_nolock(fd); + fdinfo = get_ioinfo(fd); if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN) { *wxflag_ptr = fdinfo->wxflag; @@ -432,6 +441,7 @@ unsigned create_io_inherit_block(WORD *size, BYTE **block) *wxflag_ptr = 0; *handle_ptr = INVALID_HANDLE_VALUE; } + release_ioinfo(fdinfo); wxflag_ptr++; handle_ptr++; } return TRUE; @@ -941,20 +951,33 @@ int CDECL _close(int fd) */ int CDECL _dup2(int od, int nd) { + ioinfo *info_od, *info_nd; int ret; TRACE("(od=%d, nd=%d)\n", od, nd); LOCK_FILES(); - if (nd < MSVCRT_MAX_FILES && nd >= 0 && is_valid_fd(od)) + + if (od < nd) + { + info_od = get_ioinfo(od); + info_nd = get_ioinfo(nd); + } + else + { + info_nd = get_ioinfo(nd); + info_od = get_ioinfo(od); + } + + if (nd < MSVCRT_MAX_FILES && nd >= 0 && (info_od->wxflag & WX_OPEN)) { HANDLE handle; - if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od)->handle, + if (DuplicateHandle(GetCurrentProcess(), info_od->handle, GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS)) { - int wxflag = get_ioinfo_nolock(od)->wxflag & ~_O_NOINHERIT; + int wxflag = info_od->wxflag & ~_O_NOINHERIT; - if (is_valid_fd(nd)) + if (info_nd->wxflag & WX_OPEN) _close(nd); ret = msvcrt_set_fd(handle, wxflag, nd); if (ret == -1) @@ -979,6 +1002,9 @@ int CDECL _dup2(int od, int nd) *_errno() = EBADF; ret = -1; } + + release_ioinfo(info_od); + release_ioinfo(info_nd); UNLOCK_FILES(); return ret; } @@ -2092,9 +2118,8 @@ static inline int get_utf8_char_len(char ch) /********************************************************************* * (internal) read_utf8 */ -static int read_utf8(int fd, wchar_t *buf, unsigned int count) +static int read_utf8(ioinfo *fdinfo, wchar_t *buf, unsigned int count) { - ioinfo *fdinfo = get_ioinfo_nolock(fd); HANDLE hand = fdinfo->handle; char min_buf[4], *readbuf, lookahead; DWORD readbuf_size, pos=0, num_read=1, char_len, i, j; @@ -2269,12 +2294,10 @@ static int read_utf8(int fd, wchar_t *buf, unsigned int count) * the file pointer on the \r character while getc() goes on to * the following \n */ -static int read_i(int fd, void *buf, unsigned int count) +static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count) { DWORD num_read, utf16; char *bufstart = buf; - HANDLE hand = fdtoh(fd); - ioinfo *fdinfo = get_ioinfo_nolock(fd); if (count == 0) return 0; @@ -2285,8 +2308,8 @@ static int read_i(int fd, void *buf, unsigned int count) } /* Don't trace small reads, it gets *very* annoying */ if (count > 4) - TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count); - if (hand == INVALID_HANDLE_VALUE) + TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count); + if (fdinfo->handle == INVALID_HANDLE_VALUE) { *_errno() = EBADF; return -1; @@ -2300,9 +2323,9 @@ static int read_i(int fd, void *buf, unsigned int count) } if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8)) - return read_utf8(fd, buf, count); + return read_utf8(fdinfo, buf, count); - if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL)) + if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL)) { if (fdinfo->lookahead[0] != '\n') { @@ -2315,7 +2338,7 @@ static int read_i(int fd, void *buf, unsigned int count) fdinfo->lookahead[1] = '\n'; } - if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL)) + if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL)) num_read += 1+utf16; else num_read = 1+utf16; @@ -2360,7 +2383,7 @@ static int read_i(int fd, void *buf, unsigned int count) DWORD len; lookahead[1] = '\n'; - if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len) + if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len) { if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0) { @@ -2433,9 +2456,10 @@ static int read_i(int fd, void *buf, unsigned int count) */ int CDECL _read(int fd, void *buf, unsigned int count) { - int num_read; - num_read = read_i(fd, buf, count); - return num_read; + ioinfo *info = get_ioinfo(fd); + int num_read = read_i(fd, info, buf, count); + release_ioinfo(info); + return num_read; } /********************************************************************* @@ -2443,30 +2467,34 @@ int CDECL _read(int fd, void *buf, unsigned int count) */ int CDECL _setmode(int fd,int mode) { - int ret = get_ioinfo_nolock(fd)->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY; - if(ret==_O_TEXT && (get_ioinfo_nolock(fd)->exflag & (EF_UTF8|EF_UTF16))) + ioinfo *info = get_ioinfo(fd); + int ret = info->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY; + if(ret==_O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16))) ret = _O_WTEXT; if(mode!=_O_TEXT && mode!=_O_BINARY && mode!=_O_WTEXT && mode!=_O_U16TEXT && mode!=_O_U8TEXT) { *_errno() = EINVAL; + release_ioinfo(info); return -1; } if(mode == _O_BINARY) { - get_ioinfo_nolock(fd)->wxflag &= ~WX_TEXT; - get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16); + info->wxflag &= ~WX_TEXT; + info->exflag &= ~(EF_UTF8|EF_UTF16); + release_ioinfo(info); return ret; } - get_ioinfo_nolock(fd)->wxflag |= WX_TEXT; + info->wxflag |= WX_TEXT; if(mode == _O_TEXT) - get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16); + info->exflag &= ~(EF_UTF8|EF_UTF16); else if(mode == _O_U8TEXT) - get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF16) | EF_UTF8; + info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8; else - get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF8) | EF_UTF16; + info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16; + release_ioinfo(info); return ret; } @@ -2543,7 +2571,7 @@ int CDECL _umask(int umask) int CDECL _write(int fd, const void* buf, unsigned int count) { DWORD num_written; - ioinfo *info = get_ioinfo_nolock(fd); + ioinfo *info = get_ioinfo(fd); HANDLE hand = info->handle; /* Don't trace small writes, it gets *very* annoying */ @@ -2554,12 +2582,14 @@ int CDECL _write(int fd, const void* buf, unsigned int count) if (hand == INVALID_HANDLE_VALUE) { *_errno() = EBADF; + release_ioinfo(info); return -1; } if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1) { *_errno() = EINVAL; + release_ioinfo(info); return -1; } @@ -2571,7 +2601,10 @@ int CDECL _write(int fd, const void* buf, unsigned int count) { if (WriteFile(hand, buf, count, &num_written, NULL) && (num_written == count)) + { + release_ioinfo(info); return num_written; + } TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd, hand, GetLastError()); *_errno() = ENOSPC; @@ -2662,6 +2695,7 @@ int CDECL _write(int fd, const void* buf, unsigned int count) if(!conv_len) { _dosmaperr(GetLastError()); free(p); + release_ioinfo(info); return -1; } @@ -2693,6 +2727,7 @@ int CDECL _write(int fd, const void* buf, unsigned int count) if (!WriteFile(hand, q, size, &num_written, NULL)) num_written = -1; + release_ioinfo(info); if(p) free(p); if (num_written != size) @@ -2705,6 +2740,7 @@ int CDECL _write(int fd, const void* buf, unsigned int count) return count; } + release_ioinfo(info); return -1; } @@ -2805,7 +2841,7 @@ int CDECL _filbuf(FILE* file) if(!(file->_flag & (_IOMYBUF | _USERBUF))) { int r; - if ((r = read_i(file->_file,&c,1)) != 1) { + if ((r = _read(file->_file,&c,1)) != 1) { file->_flag |= (r == 0) ? _IOEOF : _IOERR; _unlock_file(file); return EOF; @@ -2814,7 +2850,7 @@ int CDECL _filbuf(FILE* file) _unlock_file(file); return c; } else { - file->_cnt = read_i(file->_file, file->_base, file->_bufsiz); + file->_cnt = _read(file->_file, file->_base, file->_bufsiz); if(file->_cnt<=0) { file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR; file->_cnt = 0;
6 years, 9 months
1
0
0
0
03/07: [CRT] Update file descriptor handling to match Wine (3/7). CORE-14504
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=c529e727d7a5ad1ba85c2…
commit c529e727d7a5ad1ba85c212d290a43c240173277 Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 16:15:31 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:00:15 2018 +0200 [CRT] Update file descriptor handling to match Wine (3/7). CORE-14504 Import Wine commits by Piotr Caban: * 6c2d4f1092d msvcrt: Use fd critical section in _fstat64. * 9278190d468 msvcrt: Use fd critical section in _futime64. --- sdk/lib/crt/include/internal/wine/msvcrt.h | 9 +++++++++ sdk/lib/crt/stdio/file.c | 14 +++----------- sdk/lib/crt/stdio/stat64.c | 20 ++++++++++++++------ sdk/lib/crt/time/futime.c | 15 +++++++++------ 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/sdk/lib/crt/include/internal/wine/msvcrt.h b/sdk/lib/crt/include/internal/wine/msvcrt.h index 5dc3a37395..ab37a4aeda 100644 --- a/sdk/lib/crt/include/internal/wine/msvcrt.h +++ b/sdk/lib/crt/include/internal/wine/msvcrt.h @@ -153,4 +153,13 @@ typedef struct _sig_element char* _setlocale(int,const char*); NTSYSAPI VOID NTAPI RtlAssert(PVOID FailedAssertion,PVOID FileName,ULONG LineNumber,PCHAR Message); +/* ioinfo structure size is different in msvcrXX.dll's */ +typedef struct { + HANDLE handle; + unsigned char wxflag; + char lookahead[3]; + int exflag; + CRITICAL_SECTION crit; +} ioinfo; + #endif /* __WINE_MSVCRT_H */ diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index 878f3b13d1..dde5a8ebba 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -45,6 +45,7 @@ #include <precomp.h> #include "wine/unicode.h" +#include "internal/wine/msvcrt.h" #include <sys/utime.h> #include <direct.h> @@ -113,15 +114,6 @@ static char utf16_bom[2] = { 0xff, 0xfe }; #define MSVCRT_INTERNAL_BUFSIZ 4096 -/* ioinfo structure size is different in msvcrXX.dll's */ -typedef struct { - HANDLE handle; - unsigned char wxflag; - char lookahead[3]; - int exflag; - CRITICAL_SECTION crit; -} ioinfo; - /********************************************************************* * __pioinfo (MSVCRT.@) * array of pointers to ioinfo arrays [32] @@ -180,7 +172,7 @@ static inline ioinfo* get_ioinfo_nolock(int fd) return ret + (fd%MSVCRT_FD_BLOCK_SIZE); } -static inline ioinfo* get_ioinfo(int fd) +/*static*/ inline ioinfo* get_ioinfo(int fd) { ioinfo *ret = get_ioinfo_nolock(fd); if(ret->exflag & EF_CRIT_INIT) @@ -188,7 +180,7 @@ static inline ioinfo* get_ioinfo(int fd) return ret; } -static inline void release_ioinfo(ioinfo *info) +/*static*/ inline void release_ioinfo(ioinfo *info) { if(info->exflag & EF_CRIT_INIT) LeaveCriticalSection(&info->crit); diff --git a/sdk/lib/crt/stdio/stat64.c b/sdk/lib/crt/stdio/stat64.c index 41340bbd9f..98e63c57fb 100644 --- a/sdk/lib/crt/stdio/stat64.c +++ b/sdk/lib/crt/stdio/stat64.c @@ -1,8 +1,10 @@ #include <precomp.h> #include <tchar.h> #include <direct.h> +#include <internal/wine/msvcrt.h> -HANDLE fdtoh(int fd); +inline ioinfo* get_ioinfo(int fd); +inline void release_ioinfo(ioinfo *info); #define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6)) #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6)) @@ -97,25 +99,29 @@ int CDECL _tstat64(const _TCHAR *path, struct __stat64 *buf) int CDECL _fstat64(int fd, struct __stat64* buf) { + ioinfo *info = get_ioinfo(fd); DWORD dw; DWORD type; BY_HANDLE_FILE_INFORMATION hfi; - HANDLE hand = fdtoh(fd); - TRACE(":fd (%d) stat (%p)\n",fd,buf); - if (hand == INVALID_HANDLE_VALUE) + TRACE(":fd (%d) stat (%p)\n", fd, buf); + if (info->handle == INVALID_HANDLE_VALUE) + { + release_ioinfo(info); return -1; + } if (!buf) { WARN(":failed-NULL buf\n"); _dosmaperr(ERROR_INVALID_PARAMETER); + release_ioinfo(info); return -1; } memset(&hfi, 0, sizeof(hfi)); memset(buf, 0, sizeof(struct __stat64)); - type = GetFileType(hand); + type = GetFileType(info->handle); if (type == FILE_TYPE_PIPE) { buf->st_dev = buf->st_rdev = fd; @@ -130,10 +136,11 @@ int CDECL _fstat64(int fd, struct __stat64* buf) } else /* FILE_TYPE_DISK etc. */ { - if (!GetFileInformationByHandle(hand, &hfi)) + if (!GetFileInformationByHandle(info->handle, &hfi)) { WARN(":failed-last error (%d)\n",GetLastError()); _dosmaperr(ERROR_INVALID_PARAMETER); + release_ioinfo(info); return -1; } buf->st_mode = _S_IFREG | ALL_S_IREAD; @@ -148,6 +155,7 @@ int CDECL _fstat64(int fd, struct __stat64* buf) } TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes, buf->st_mode); + release_ioinfo(info); return 0; } diff --git a/sdk/lib/crt/time/futime.c b/sdk/lib/crt/time/futime.c index 834b527aaa..535012c05e 100644 --- a/sdk/lib/crt/time/futime.c +++ b/sdk/lib/crt/time/futime.c @@ -39,8 +39,10 @@ #include <time.h> #include <sys/utime.h> #include "bitsfixup.h" +#include <internal/wine/msvcrt.h> -HANDLE fdtoh(int fd); +inline ioinfo* get_ioinfo(int fd); +inline void release_ioinfo(ioinfo *info); /****************************************************************************** * \name _futime @@ -52,12 +54,12 @@ HANDLE fdtoh(int fd); int _futime(int fd, struct _utimbuf *filetime) { - HANDLE handle; + ioinfo *info = get_ioinfo(fd); FILETIME at, wt; - handle = fdtoh(fd); - if (handle == INVALID_HANDLE_VALUE) + if (info->handle == INVALID_HANDLE_VALUE) { + release_ioinfo(info); return -1; } @@ -84,11 +86,12 @@ _futime(int fd, struct _utimbuf *filetime) } } - if (!SetFileTime(handle, NULL, &at, &wt)) + if (!SetFileTime(info->handle, NULL, &at, &wt)) { + release_ioinfo(info); _dosmaperr(GetLastError()); return -1 ; } - + release_ioinfo(info); return 0; }
6 years, 9 months
1
0
0
0
02/07: [CRT] Update file descriptor handling to match Wine (2/7). CORE-14504
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=9eb1eae28cd216025f5ed…
commit 9eb1eae28cd216025f5eda2cb549578939f2e446 Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 15:31:50 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 13:00:06 2018 +0200 [CRT] Update file descriptor handling to match Wine (2/7). CORE-14504 Import Wine commits by Piotr Caban: * 4508e665c07 msvcrt: Use fd critical section in _commit. * afc6962a24a msvcrt: Use fd critical section in _close. * 9a4cfbc3edc msvcrt: Use fd critical section in _eof. * ffdd16cc0d7 msvcrt: Use fd critical section in _locking. * fa6de597700 msvcrt: Use fd critical section in _lseeki64. * f2f45d5fe67 msvcrt: Use fd critical section in _chsize_s. --- sdk/lib/crt/stdio/file.c | 157 ++++++++++++++++++++++++++++------------------- 1 file changed, 93 insertions(+), 64 deletions(-) diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index 3c7c01ac78..878f3b13d1 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -182,13 +182,16 @@ static inline ioinfo* get_ioinfo_nolock(int fd) static inline ioinfo* get_ioinfo(int fd) { - ioinfo *ret = NULL; - if(fd < MSVCRT_MAX_FILES) - ret = __pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]; - if(!ret) - return &__badioinfo; + ioinfo *ret = get_ioinfo_nolock(fd); + if(ret->exflag & EF_CRIT_INIT) + EnterCriticalSection(&ret->crit); + return ret; +} - return ret + (fd%MSVCRT_FD_BLOCK_SIZE); +static inline void release_ioinfo(ioinfo *info) +{ + if(info->exflag & EF_CRIT_INIT) + LeaveCriticalSection(&info->crit); } static inline FILE* msvcrt_get_file(int i) @@ -814,27 +817,36 @@ int CDECL _wunlink(const wchar_t *path) */ int CDECL _commit(int fd) { - HANDLE hand = fdtoh(fd); - - TRACE(":fd (%d) handle (%p)\n",fd,hand); - if (hand == INVALID_HANDLE_VALUE) - return -1; + ioinfo *info = get_ioinfo(fd); + int ret; - if (!FlushFileBuffers(hand)) - { - if (GetLastError() == ERROR_INVALID_HANDLE) + TRACE(":fd (%d) handle (%p)\n", fd, info->handle); + if (info->handle == INVALID_HANDLE_VALUE) + ret = -1; + else if (!FlushFileBuffers(info->handle)) { - /* FlushFileBuffers fails for console handles - * so we ignore this error. - */ - return 0; + if (GetLastError() == ERROR_INVALID_HANDLE) + { + /* FlushFileBuffers fails for console handles + * so we ignore this error. + */ + ret = 0; + } + else + { + TRACE(":failed-last error (%d)\n",GetLastError()); + _dosmaperr(GetLastError()); + ret = -1; + } } - TRACE(":failed-last error (%d)\n",GetLastError()); - _dosmaperr(GetLastError()); - return -1; - } - TRACE(":ok\n"); - return 0; + else + { + TRACE(":ok\n"); + ret = 0; + } + + release_ioinfo(info); + return ret; } /* _flushall calls fflush which calls _flushall */ @@ -907,27 +919,24 @@ int CDECL fflush(FILE* file) */ int CDECL _close(int fd) { - HANDLE hand; + ioinfo *info = get_ioinfo(fd); int ret; LOCK_FILES(); - hand = fdtoh(fd); - TRACE(":fd (%d) handle (%p)\n",fd,hand); - if (hand == INVALID_HANDLE_VALUE) - ret = -1; - else if (!CloseHandle(hand)) - { - WARN(":failed-last error (%d)\n",GetLastError()); - _dosmaperr(GetLastError()); + TRACE(":fd (%d) handle (%p)\n", fd, info->handle); + if (!(info->wxflag & WX_OPEN)) { ret = -1; - } - else - { + } else { + ret = CloseHandle(info->handle) ? 0 : -1; msvcrt_free_fd(fd); - ret = 0; + if (ret) { + WARN(":failed-last error (%d)\n",GetLastError()); + _dosmaperr(GetLastError()); + ret = -1; + } } UNLOCK_FILES(); - TRACE(":ok\n"); + release_ioinfo(info); return ret; } @@ -1004,29 +1013,38 @@ int CDECL _dup(int od) */ int CDECL _eof(int fd) { + ioinfo *info = get_ioinfo(fd); DWORD curpos,endpos; LONG hcurpos,hendpos; - HANDLE hand = fdtoh(fd); - TRACE(":fd (%d) handle (%p)\n",fd,hand); + TRACE(":fd (%d) handle (%p)\n", fd, info->handle); - if (hand == INVALID_HANDLE_VALUE) + if (info->handle == INVALID_HANDLE_VALUE) + { + release_ioinfo(info); return -1; + } - if (get_ioinfo_nolock(fd)->wxflag & WX_ATEOF) return TRUE; + if (info->wxflag & WX_ATEOF) + { + release_ioinfo(info); + return TRUE; + } /* Otherwise we do it the hard way */ hcurpos = hendpos = 0; - curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT); - endpos = SetFilePointer(hand, 0, &hendpos, FILE_END); + curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT); + endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END); if (curpos == endpos && hcurpos == hendpos) { /* FIXME: shouldn't WX_ATEOF be set here? */ + release_ioinfo(info); return TRUE; } - SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN); + SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN); + release_ioinfo(info); return FALSE; } @@ -1092,15 +1110,20 @@ void msvcrt_free_io(void) */ __int64 CDECL _lseeki64(int fd, __int64 offset, int whence) { - HANDLE hand = fdtoh(fd); + ioinfo *info = get_ioinfo(fd); LARGE_INTEGER ofs; - TRACE(":fd (%d) handle (%p)\n",fd,hand); - if (hand == INVALID_HANDLE_VALUE) + TRACE(":fd (%d) handle (%p)\n", fd, info->handle); + + if (info->handle == INVALID_HANDLE_VALUE) + { + release_ioinfo(info); return -1; + } if (whence < 0 || whence > 2) { + release_ioinfo(info); *_errno() = EINVAL; return -1; } @@ -1114,14 +1137,16 @@ __int64 CDECL _lseeki64(int fd, __int64 offset, int whence) /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only, * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */ ofs.QuadPart = offset; - if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER || + if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER || GetLastError() == ERROR_SUCCESS) { - get_ioinfo_nolock(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF); + info->wxflag &= ~(WX_ATEOF|WX_READEOF); /* FIXME: What if we seek _to_ EOF - is EOF set? */ + release_ioinfo(info); return ofs.QuadPart; } + release_ioinfo(info); TRACE(":error-last error (%d)\n",GetLastError()); _dosmaperr(GetLastError()); return -1; @@ -1167,16 +1192,20 @@ void CDECL _unlock_file(FILE *file) */ int CDECL _locking(int fd, int mode, LONG nbytes) { + ioinfo *info = get_ioinfo(fd); BOOL ret; DWORD cur_locn; - HANDLE hand = fdtoh(fd); - TRACE(":fd (%d) handle (%p)\n",fd,hand); - if (hand == INVALID_HANDLE_VALUE) + TRACE(":fd (%d) handle (%p)\n", fd, info->handle); + if (info->handle == INVALID_HANDLE_VALUE) + { + release_ioinfo(info); return -1; + } if (mode < 0 || mode > 4) { + release_ioinfo(info); *_errno() = EINVAL; return -1; } @@ -1189,8 +1218,9 @@ int CDECL _locking(int fd, int mode, LONG nbytes) (mode==_LK_NBRLCK)?"_LK_NBRLCK": "UNKNOWN"); - if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER) + if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER) { + release_ioinfo(info); FIXME ("Seek failed\n"); *_errno() = EINVAL; /* FIXME */ return -1; @@ -1201,16 +1231,17 @@ int CDECL _locking(int fd, int mode, LONG nbytes) ret = 1; /* just to satisfy gcc */ while (nretry--) { - ret = LockFile(hand, cur_locn, 0L, nbytes, 0L); + ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L); if (ret) break; Sleep(1); } } else if (mode == _LK_UNLCK) - ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L); + ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L); else - ret = LockFile(hand, cur_locn, 0L, nbytes, 0L); + ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L); /* FIXME - what about error settings? */ + release_ioinfo(info); return ret ? 0 : -1; } @@ -1259,18 +1290,16 @@ int CDECL fseek(FILE* file, long offset, int whence) */ int CDECL _chsize_s(int fd, __int64 size) { + ioinfo *info; __int64 cur, pos; - HANDLE handle; BOOL ret = FALSE; TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size)); if (!MSVCRT_CHECK_PMT(size >= 0)) return EINVAL; - LOCK_FILES(); - - handle = fdtoh(fd); - if (handle != INVALID_HANDLE_VALUE) + info = get_ioinfo(fd); + if (info->handle != INVALID_HANDLE_VALUE) { /* save the current file pointer */ cur = _lseeki64(fd, 0, SEEK_CUR); @@ -1279,7 +1308,7 @@ int CDECL _chsize_s(int fd, __int64 size) pos = _lseeki64(fd, size, SEEK_SET); if (pos >= 0) { - ret = SetEndOfFile(handle); + ret = SetEndOfFile(info->handle); if (!ret) _dosmaperr(GetLastError()); } @@ -1288,7 +1317,7 @@ int CDECL _chsize_s(int fd, __int64 size) } } - UNLOCK_FILES(); + release_ioinfo(info); return ret ? 0 : *_errno(); }
6 years, 9 months
1
0
0
0
01/07: [CRT] Update file descriptor handling to match Wine (1/7). CORE-14504
by Thomas Faber
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=25064fdf3c844aa5d0710…
commit 25064fdf3c844aa5d0710db930ccff65a9dee128 Author: Thomas Faber <thomas.faber(a)reactos.org> AuthorDate: Sun Mar 25 15:13:39 2018 +0200 Commit: Thomas Faber <thomas.faber(a)reactos.org> CommitDate: Mon Mar 26 12:59:58 2018 +0200 [CRT] Update file descriptor handling to match Wine (1/7). CORE-14504 Import Wine commits by Piotr Caban: * 0c4cb6ce5d4 msvcrt: Initialize file descriptor critical section. * 1d791f5db24 msvcrt: Rename msvcrt_get_ioinfo function. --- sdk/lib/crt/stdio/file.c | 107 ++++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 47 deletions(-) diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index b13176bbb2..3c7c01ac78 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -101,6 +101,7 @@ extern int _commode; /* values for exflag - it's used differently in msvcr90.dll*/ #define EF_UTF8 0x01 #define EF_UTF16 0x02 +#define EF_CRIT_INIT 0x04 #define EF_UNK_UNICODE 0x08 static char utf8_bom[3] = { 0xef, 0xbb, 0xbf }; @@ -218,7 +219,7 @@ static inline FILE* msvcrt_get_file(int i) static inline BOOL is_valid_fd(int fd) { - return fd >= 0 && fd < fdend && (get_ioinfo(fd)->wxflag & WX_OPEN); + return fd >= 0 && fd < fdend && (get_ioinfo_nolock(fd)->wxflag & WX_OPEN); } /* INTERNAL: Get the HANDLE for a fd @@ -236,9 +237,9 @@ static inline BOOL is_valid_fd(int fd) *_errno() = EBADF; return INVALID_HANDLE_VALUE; } - //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE) + //if (get_ioinfo_nolock(fd)->handle == INVALID_HANDLE_VALUE) //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd); - return get_ioinfo(fd)->handle; + return get_ioinfo_nolock(fd)->handle; } /* INTERNAL: free a file entry fd */ @@ -247,7 +248,7 @@ static void msvcrt_free_fd(int fd) ioinfo *fdinfo; LOCK_FILES(); - fdinfo = get_ioinfo(fd); + fdinfo = get_ioinfo_nolock(fd); if(fdinfo != &__badioinfo) { fdinfo->handle = INVALID_HANDLE_VALUE; @@ -291,7 +292,7 @@ static int msvcrt_set_fd(HANDLE hand, int flag, int fd) return -1; } - fdinfo = get_ioinfo(fd); + fdinfo = get_ioinfo_nolock(fd); if(fdinfo == &__badioinfo) { int i; @@ -305,7 +306,7 @@ static int msvcrt_set_fd(HANDLE hand, int flag, int fd) for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++) __pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE; - fdinfo = get_ioinfo(fd); + fdinfo = get_ioinfo_nolock(fd); } fdinfo->handle = hand; @@ -313,14 +314,16 @@ static int msvcrt_set_fd(HANDLE hand, int flag, int fd) fdinfo->lookahead[0] = '\n'; fdinfo->lookahead[1] = '\n'; fdinfo->lookahead[2] = '\n'; - fdinfo->exflag = 0; + if(!(fdinfo->exflag & EF_CRIT_INIT)) + InitializeCriticalSection(&fdinfo->crit); + fdinfo->exflag = EF_CRIT_INIT; /* locate next free slot */ if (fd == fdstart && fd == fdend) fdstart = fdend + 1; else while (fdstart < fdend && - get_ioinfo(fdstart)->handle != INVALID_HANDLE_VALUE) + get_ioinfo_nolock(fdstart)->handle != INVALID_HANDLE_VALUE) fdstart++; /* update last fd in use */ if (fd >= fdend) @@ -423,7 +426,7 @@ unsigned create_io_inherit_block(WORD *size, BYTE **block) for (fd = 0; fd < fdend; fd++) { /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */ - fdinfo = get_ioinfo(fd); + fdinfo = get_ioinfo_nolock(fd); if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN) { *wxflag_ptr = fdinfo->wxflag; @@ -470,10 +473,10 @@ void msvcrt_init_io(void) } fdend = max( 3, count ); for (fdstart = 3; fdstart < fdend; fdstart++) - if (get_ioinfo(fdstart)->handle == INVALID_HANDLE_VALUE) break; + if (get_ioinfo_nolock(fdstart)->handle == INVALID_HANDLE_VALUE) break; } - fdinfo = get_ioinfo(STDIN_FILENO); + fdinfo = get_ioinfo_nolock(STDIN_FILENO); if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) { HANDLE h = GetStdHandle(STD_INPUT_HANDLE); DWORD type = GetFileType(h); @@ -482,7 +485,7 @@ void msvcrt_init_io(void) |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), STDIN_FILENO); } - fdinfo = get_ioinfo(STDOUT_FILENO); + fdinfo = get_ioinfo_nolock(STDOUT_FILENO); if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); DWORD type = GetFileType(h); @@ -491,7 +494,7 @@ void msvcrt_init_io(void) |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), STDOUT_FILENO); } - fdinfo = get_ioinfo(STDERR_FILENO); + fdinfo = get_ioinfo_nolock(STDERR_FILENO); if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) { HANDLE h = GetStdHandle(STD_ERROR_HANDLE); DWORD type = GetFileType(h); @@ -500,9 +503,9 @@ void msvcrt_init_io(void) |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), STDERR_FILENO); } - TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(STDIN_FILENO)->handle, - get_ioinfo(STDOUT_FILENO)->handle, - get_ioinfo(STDERR_FILENO)->handle); + TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO)->handle, + get_ioinfo_nolock(STDOUT_FILENO)->handle, + get_ioinfo_nolock(STDERR_FILENO)->handle); memset(_iob,0,3*sizeof(FILE)); for (i = 0; i < 3; i++) @@ -895,7 +898,7 @@ int CDECL fflush(FILE* file) _unlock_file(file); return 0; - } + } return 0; } @@ -945,10 +948,10 @@ int CDECL _dup2(int od, int nd) { HANDLE handle; - if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od)->handle, + if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od)->handle, GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS)) { - int wxflag = get_ioinfo(od)->wxflag & ~_O_NOINHERIT; + int wxflag = get_ioinfo_nolock(od)->wxflag & ~_O_NOINHERIT; if (is_valid_fd(nd)) _close(nd); @@ -1010,7 +1013,7 @@ int CDECL _eof(int fd) if (hand == INVALID_HANDLE_VALUE) return -1; - if (get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE; + if (get_ioinfo_nolock(fd)->wxflag & WX_ATEOF) return TRUE; /* Otherwise we do it the hard way */ hcurpos = hendpos = 0; @@ -1058,7 +1061,17 @@ void msvcrt_free_io(void) _fcloseall(); for(i=0; i<sizeof(__pioinfo)/sizeof(__pioinfo[0]); i++) + { + if(!__pioinfo[i]) + continue; + + for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++) + { + if(__pioinfo[i][j].exflag & EF_CRIT_INIT) + DeleteCriticalSection(&__pioinfo[i][j].crit); + } free(__pioinfo[i]); + } for(j=0; j<MSVCRT_stream_idx; j++) { @@ -1104,7 +1117,7 @@ __int64 CDECL _lseeki64(int fd, __int64 offset, int whence) if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER || GetLastError() == ERROR_SUCCESS) { - get_ioinfo(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF); + get_ioinfo_nolock(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF); /* FIXME: What if we seek _to_ EOF - is EOF set? */ return ofs.QuadPart; @@ -1854,11 +1867,11 @@ int CDECL _wsopen_s( int *fd, const wchar_t* path, int oflags, int shflags, int return *_errno(); if (oflags & _O_WTEXT) - get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE; + get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE; else if (oflags & _O_U16TEXT) - get_ioinfo(*fd)->exflag |= EF_UTF16; + get_ioinfo_nolock(*fd)->exflag |= EF_UTF16; else if (oflags & _O_U8TEXT) - get_ioinfo(*fd)->exflag |= EF_UTF8; + get_ioinfo_nolock(*fd)->exflag |= EF_UTF8; TRACE(":fd (%d) handle (%p)\n", *fd, hand); return 0; @@ -2060,7 +2073,7 @@ static inline int get_utf8_char_len(char ch) */ static int read_utf8(int fd, wchar_t *buf, unsigned int count) { - ioinfo *fdinfo = get_ioinfo(fd); + ioinfo *fdinfo = get_ioinfo_nolock(fd); HANDLE hand = fdinfo->handle; char min_buf[4], *readbuf, lookahead; DWORD readbuf_size, pos=0, num_read=1, char_len, i, j; @@ -2240,7 +2253,7 @@ static int read_i(int fd, void *buf, unsigned int count) DWORD num_read, utf16; char *bufstart = buf; HANDLE hand = fdtoh(fd); - ioinfo *fdinfo = get_ioinfo(fd); + ioinfo *fdinfo = get_ioinfo_nolock(fd); if (count == 0) return 0; @@ -2409,8 +2422,8 @@ int CDECL _read(int fd, void *buf, unsigned int count) */ int CDECL _setmode(int fd,int mode) { - int ret = get_ioinfo(fd)->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY; - if(ret==_O_TEXT && (get_ioinfo(fd)->exflag & (EF_UTF8|EF_UTF16))) + int ret = get_ioinfo_nolock(fd)->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY; + if(ret==_O_TEXT && (get_ioinfo_nolock(fd)->exflag & (EF_UTF8|EF_UTF16))) ret = _O_WTEXT; if(mode!=_O_TEXT && mode!=_O_BINARY && mode!=_O_WTEXT @@ -2420,18 +2433,18 @@ int CDECL _setmode(int fd,int mode) } if(mode == _O_BINARY) { - get_ioinfo(fd)->wxflag &= ~WX_TEXT; - get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16); + get_ioinfo_nolock(fd)->wxflag &= ~WX_TEXT; + get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16); return ret; } - get_ioinfo(fd)->wxflag |= WX_TEXT; + get_ioinfo_nolock(fd)->wxflag |= WX_TEXT; if(mode == _O_TEXT) - get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16); + get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16); else if(mode == _O_U8TEXT) - get_ioinfo(fd)->exflag = (get_ioinfo(fd)->exflag & ~EF_UTF16) | EF_UTF8; + get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF16) | EF_UTF8; else - get_ioinfo(fd)->exflag = (get_ioinfo(fd)->exflag & ~EF_UTF8) | EF_UTF16; + get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF8) | EF_UTF16; return ret; @@ -2509,7 +2522,7 @@ int CDECL _umask(int umask) int CDECL _write(int fd, const void* buf, unsigned int count) { DWORD num_written; - ioinfo *info = get_ioinfo(fd); + ioinfo *info = get_ioinfo_nolock(fd); HANDLE hand = info->handle; /* Don't trace small writes, it gets *very* annoying */ @@ -2866,8 +2879,8 @@ wint_t CDECL fgetwc(FILE* file) _lock_file(file); - if((get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16)) - || !(get_ioinfo(file->_file)->wxflag & WX_TEXT)) { + if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16)) + || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) { char *p; for(p=(char*)&ret; (wint_t*)p<&ret+1; p++) { @@ -3063,7 +3076,7 @@ wint_t CDECL fputwc(wchar_t c, FILE* stream) /* If this is a real file stream (and not some temporary one for sprintf-like functions), check whether it is opened in text mode. In this case, we have to perform an implicit conversion to ANSI. */ - if (!(stream->_flag & _IOSTRG) && get_ioinfo(stream->_file)->wxflag & WX_TEXT) + if (!(stream->_flag & _IOSTRG) && get_ioinfo_nolock(stream->_file)->wxflag & WX_TEXT) { /* Convert to multibyte in text mode */ char mbc[MB_LEN_MAX]; @@ -3292,7 +3305,7 @@ size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file) i = (file->_cnt<rcnt) ? file->_cnt : rcnt; /* If the buffer fill reaches eof but fread wouldn't, clear eof. */ if (i > 0 && i < file->_cnt) { - get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF; + get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF; file->_flag &= ~_IOEOF; } if (i > 0) { @@ -3313,7 +3326,7 @@ size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file) /* expose feof condition in the flags * MFC tests file->_flag for feof, and doesn't call feof()) */ - if (get_ioinfo(file->_file)->wxflag & WX_ATEOF) + if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF) file->_flag |= _IOEOF; else if (i == -1) { @@ -3431,7 +3444,7 @@ __int64 CDECL _ftelli64(FILE* file) if(file->_flag & _IOWRT) { pos += file->_ptr - file->_base; - if(get_ioinfo(file->_file)->wxflag & WX_TEXT) { + if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) { char *p; for(p=file->_base; p<file->_ptr; p++) @@ -3443,7 +3456,7 @@ __int64 CDECL _ftelli64(FILE* file) int i; pos -= file->_cnt; - if(get_ioinfo(file->_file)->wxflag & WX_TEXT) { + if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) { for(i=0; i<file->_cnt; i++) if(file->_ptr[i] == '\n') pos--; @@ -3459,8 +3472,8 @@ __int64 CDECL _ftelli64(FILE* file) pos -= file->_bufsiz; pos += file->_ptr - file->_base; - if(get_ioinfo(file->_file)->wxflag & WX_TEXT) { - if(get_ioinfo(file->_file)->wxflag & WX_READNL) + if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) { + if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL) pos--; for(p=file->_base; p<file->_ptr; p++) @@ -3517,7 +3530,7 @@ int CDECL fputws(const wchar_t *s, FILE* file) int ret; _lock_file(file); - if (!(get_ioinfo(file->_file)->wxflag & WX_TEXT)) { + if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) { ret = fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF; _unlock_file(file); return ret; @@ -3901,8 +3914,8 @@ wint_t CDECL ungetwc(wint_t wc, FILE * file) _lock_file(file); - if((get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16)) - || !(get_ioinfo(file->_file)->wxflag & WX_TEXT)) { + if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16)) + || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) { unsigned char * pp = (unsigned char *)&mwc; int i;
6 years, 9 months
1
0
0
0
01/01: [NTOSKRNL] Properly delete VACB in CcRosCreateVacb() when mapping fails. Spotted by Thomas.
by Pierre Schweitzer
https://git.reactos.org/?p=reactos.git;a=commitdiff;h=ffd524275e721a192b91c…
commit ffd524275e721a192b91cbc65819ee377043eb79 Author: Pierre Schweitzer <pierre(a)reactos.org> AuthorDate: Sun Mar 25 18:26:32 2018 +0200 Commit: Pierre Schweitzer <pierre(a)reactos.org> CommitDate: Sun Mar 25 18:27:19 2018 +0200 [NTOSKRNL] Properly delete VACB in CcRosCreateVacb() when mapping fails. Spotted by Thomas. CORE-14478 CORE-14502 --- ntoskrnl/cc/view.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ntoskrnl/cc/view.c b/ntoskrnl/cc/view.c index 9cc45bdb3e..ba87c5e510 100644 --- a/ntoskrnl/cc/view.c +++ b/ntoskrnl/cc/view.c @@ -831,18 +831,20 @@ CcRosCreateVacb ( } #endif + /* Reference it to allow release */ + CcRosVacbIncRefCount(current); + Status = CcRosMapVacbInKernelSpace(current); if (!NT_SUCCESS(Status)) { RemoveEntryList(¤t->CacheMapVacbListEntry); RemoveEntryList(¤t->VacbLruListEntry); - CcRosReleaseVacbLock(current); + CcRosReleaseVacb(SharedCacheMap, current, FALSE, + FALSE, FALSE); + CcRosVacbDecRefCount(current); ExFreeToNPagedLookasideList(&VacbLookasideList, current); } - /* Reference it to allow release */ - CcRosVacbIncRefCount(current); - return Status; }
6 years, 9 months
1
0
0
0
← Newer
1
...
7
8
9
10
11
12
13
...
44
Older →
Jump to page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Results per page:
10
25
50
100
200