Added patch to fix implementation of msvcrt.close when stdout == stderr (fixes Wine Staging Bug #485).

This commit is contained in:
Sebastian Lackner
2015-08-22 08:11:05 +02:00
parent c8eb156226
commit afb16555cd
7 changed files with 278 additions and 1 deletions

View File

@@ -0,0 +1,137 @@
From 0b664b21287329f8c2828b3dc248fd460eece299 Mon Sep 17 00:00:00 2001
From: Qian Hong <qhong@codeweavers.com>
Date: Sat, 22 Aug 2015 11:20:31 +0800
Subject: msvcrt/tests: Add tests for stdout and stderr refcount.
---
dlls/msvcrt/tests/file.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+)
diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c
index ba8c3e3..7a79cb7 100644
--- a/dlls/msvcrt/tests/file.c
+++ b/dlls/msvcrt/tests/file.c
@@ -1350,6 +1350,60 @@ static void test_file_inherit_child_no(const char* fd_s)
"Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
}
+static void test_file_refcount_child(void)
+{
+ static const char buffer1[] = "test1";
+ static const char buffer2[] = "test2";
+ static const char buffer3[] = "test3";
+ static const char buffer4[] = "test4";
+ HANDLE f0, f1, f2, h0, h1, h2;
+ DWORD written, flags, ret;
+
+ f0 = (HANDLE)_get_osfhandle(STDIN_FILENO);
+ f1 = (HANDLE)_get_osfhandle(STDOUT_FILENO);
+ f2 = (HANDLE)_get_osfhandle(STDERR_FILENO);
+ ok(f0 == f1, "expected same handles, got %p, %p\n", f0, f1);
+ ok(f1 == f2, "expected same handles, got %p, %p\n", f1, f2);
+
+ h0 = GetStdHandle(STD_INPUT_HANDLE);
+ h1 = GetStdHandle(STD_OUTPUT_HANDLE);
+ h2 = GetStdHandle(STD_ERROR_HANDLE);
+ ok(h0 == h1, "expected same handles, got %p, %p\n", h0, h1);
+ ok(h1 == h2, "expected same handles, got %p, %p\n", h1, h2);
+ ok(f0 == h0, "expected same handles, got %p, %p\n", f0, h0);
+
+ ret = GetHandleInformation(h1, &flags);
+ ok(ret, "GetHandleInformation failed\n");
+ ret = WriteFile(h1, buffer1, strlen(buffer1), &written, 0);
+ ok(ret, "WriteFile failed\n");
+
+ ret = fclose(stdout);
+ ok(ret == 0, "fclose failed\n");
+ ret = GetHandleInformation(h1, &flags);
+todo_wine
+ ok(ret, "GetHandleInformation failed\n");
+ ret = WriteFile(h1, buffer2, strlen(buffer2), &written, 0);
+todo_wine
+ ok(ret, "WriteFile failed\n");
+
+ ret = fclose(stdout);
+ ok(ret != 0, "fclose should fail\n");
+ ret = GetHandleInformation(h1, &flags);
+todo_wine
+ ok(ret, "GetHandleInformation failed\n");
+ ret = WriteFile(h1, buffer3, strlen(buffer3), &written, 0);
+todo_wine
+ ok(ret, "WriteFile failed\n");
+
+ ret = fclose(stderr);
+todo_wine
+ ok(ret == 0, "fclose failed\n");
+ ret = GetHandleInformation(h1, &flags);
+ ok(!ret, "GetHandleInformation should fail\n");
+ ret = WriteFile(h1, buffer4, strlen(buffer4), &written, 0);
+ ok(!ret, "WriteFile should fail\n");
+}
+
static void create_io_inherit_block( STARTUPINFOA *startup, unsigned int count, const HANDLE *handles )
{
static BYTE block[1024];
@@ -1423,6 +1477,37 @@ static void test_stdout_handle( STARTUPINFOA *startup, char *cmdline, HANDLE hst
DeleteFileA( "fdopen.err" );
}
+static void test_file_refcount( STARTUPINFOA *startup, char *cmdline, const char *descr )
+{
+ const char *data;
+ HANDLE hMixFile;
+ SECURITY_ATTRIBUTES sa;
+ PROCESS_INFORMATION proc;
+
+ /* make file handle inheritable */
+ sa.nLength = sizeof(sa);
+ sa.lpSecurityDescriptor = NULL;
+ sa.bInheritHandle = TRUE;
+
+ hMixFile = CreateFileA( "fdopen.mix", GENERIC_READ|GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, CREATE_ALWAYS, 0, NULL );
+ startup->dwFlags = STARTF_USESTDHANDLES;
+ startup->hStdInput = hMixFile;
+ startup->hStdOutput = hMixFile;
+ startup->hStdError = hMixFile;
+
+ CreateProcessA( NULL, cmdline, NULL, NULL, TRUE,
+ CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS, NULL, NULL, startup, &proc );
+ winetest_wait_child_process( proc.hProcess );
+
+ data = read_file( hMixFile );
+todo_wine
+ ok( !strcmp( data, "test1test2test3" ), "%s: Wrong error data (%s)\n", descr, data );
+
+ CloseHandle( hMixFile );
+ DeleteFileA( "fdopen.mix" );
+}
+
static void test_file_inherit( const char* selfname )
{
int fd;
@@ -1516,6 +1601,12 @@ static void test_file_inherit( const char* selfname )
test_stdout_handle( &startup, cmdline, handles[1], TRUE, "large size block" );
CloseHandle( handles[1] );
DeleteFileA("fdopen.tst");
+
+ /* test refcount of handles */
+ create_io_inherit_block( &startup, 0, NULL );
+ sprintf(cmdline, "%s file refcount", selfname);
+ test_file_refcount( &startup, cmdline, "file refcount" );
+ DeleteFileA("fdopen.tst");
}
static void test_tmpnam( void )
@@ -2345,6 +2436,8 @@ START_TEST(file)
test_file_inherit_child_no(arg_v[3]);
else if (strcmp(arg_v[2], "pipes") == 0)
test_pipes_child(arg_c, arg_v);
+ else if (strcmp(arg_v[2], "refcount") == 0)
+ test_file_refcount_child();
else
ok(0, "invalid argument '%s'\n", arg_v[2]);
return;
--
2.5.0

View File

@@ -0,0 +1,81 @@
From d55190c65e4cbdd8f81992ae842581a253b5ece6 Mon Sep 17 00:00:00 2001
From: Qian Hong <qhong@codeweavers.com>
Date: Sat, 22 Aug 2015 11:21:32 +0800
Subject: msvcrt: Implemenent refcount check for stdout and stderr.
---
dlls/msvcrt/file.c | 15 +++++++++------
dlls/msvcrt/tests/file.c | 6 ------
2 files changed, 9 insertions(+), 12 deletions(-)
diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c
index f304ec4..49cd4f0 100644
--- a/dlls/msvcrt/file.c
+++ b/dlls/msvcrt/file.c
@@ -1007,18 +1007,21 @@ int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
int CDECL MSVCRT__close(int fd)
{
ioinfo *info = get_ioinfo(fd);
- int ret;
+ int ret = 0;
TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
if (!(info->wxflag & WX_OPEN)) {
ret = -1;
} else {
- ret = CloseHandle(info->handle) ? 0 : -1;
- msvcrt_free_fd(fd);
- if (ret) {
- WARN(":failed-last error (%d)\n",GetLastError());
- msvcrt_set_errno(GetLastError());
+ if ((fd != MSVCRT_STDOUT_FILENO && fd != MSVCRT_STDERR_FILENO) ||
+ get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle != get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle) {
+ ret = CloseHandle(info->handle) ? 0 : -1;
+ if (ret) {
+ WARN(":failed-last error (%d)\n",GetLastError());
+ msvcrt_set_errno(GetLastError());
+ }
}
+ msvcrt_free_fd(fd);
}
release_ioinfo(info);
return ret;
diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c
index 7a79cb7..ea7d307 100644
--- a/dlls/msvcrt/tests/file.c
+++ b/dlls/msvcrt/tests/file.c
@@ -1380,23 +1380,18 @@ static void test_file_refcount_child(void)
ret = fclose(stdout);
ok(ret == 0, "fclose failed\n");
ret = GetHandleInformation(h1, &flags);
-todo_wine
ok(ret, "GetHandleInformation failed\n");
ret = WriteFile(h1, buffer2, strlen(buffer2), &written, 0);
-todo_wine
ok(ret, "WriteFile failed\n");
ret = fclose(stdout);
ok(ret != 0, "fclose should fail\n");
ret = GetHandleInformation(h1, &flags);
-todo_wine
ok(ret, "GetHandleInformation failed\n");
ret = WriteFile(h1, buffer3, strlen(buffer3), &written, 0);
-todo_wine
ok(ret, "WriteFile failed\n");
ret = fclose(stderr);
-todo_wine
ok(ret == 0, "fclose failed\n");
ret = GetHandleInformation(h1, &flags);
ok(!ret, "GetHandleInformation should fail\n");
@@ -1501,7 +1496,6 @@ static void test_file_refcount( STARTUPINFOA *startup, char *cmdline, const char
winetest_wait_child_process( proc.hProcess );
data = read_file( hMixFile );
-todo_wine
ok( !strcmp( data, "test1test2test3" ), "%s: Wrong error data (%s)\n", descr, data );
CloseHandle( hMixFile );
--
2.5.0

View File

@@ -0,0 +1,33 @@
From a491ae0671f0bf8e482ede7ad5b0538bd10ed87d Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Sat, 22 Aug 2015 07:22:56 +0200
Subject: msvcrt: Use constants instead of hardcoded values.
---
dlls/msvcrt/file.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c
index 49cd4f0..a62f73d 100644
--- a/dlls/msvcrt/file.c
+++ b/dlls/msvcrt/file.c
@@ -393,13 +393,13 @@ static void msvcrt_free_fd(int fd)
{
switch (fd)
{
- case 0:
+ case MSVCRT_STDIN_FILENO:
SetStdHandle(STD_INPUT_HANDLE, 0);
break;
- case 1:
+ case MSVCRT_STDOUT_FILENO:
SetStdHandle(STD_OUTPUT_HANDLE, 0);
break;
- case 2:
+ case MSVCRT_STDERR_FILENO:
SetStdHandle(STD_ERROR_HANDLE, 0);
break;
}
--
2.5.0

View File

@@ -0,0 +1,3 @@
Fixes: Fix implementation of msvcrt.close when stdout == stderr
# FIXME: Should msvcrt implement refcounting for all handles?