From 1b231f9dd94ecb267c3f50ef7cccfdd87fc8fee4 Mon Sep 17 00:00:00 2001 From: Norbert Federa Date: Wed, 25 May 2016 15:47:58 +0200 Subject: [PATCH] winpr/thread: fix TestThreadCreateProcess On WIN32 TestThread now works and is expected to succeed --- winpr/include/winpr/thread.h | 14 +++++ .../libwinpr/thread/test/TestThreadCreateProcess.c | 69 ++++++++++++++-------- 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/winpr/include/winpr/thread.h b/winpr/include/winpr/thread.h index 54e9e19..bc69ed1 100644 --- a/winpr/include/winpr/thread.h +++ b/winpr/include/winpr/thread.h @@ -84,6 +84,20 @@ typedef STARTUPINFOA STARTUPINFO; typedef LPSTARTUPINFOA LPSTARTUPINFO; #endif +#define STARTF_USESHOWWINDOW 0x00000001 +#define STARTF_USESIZE 0x00000002 +#define STARTF_USEPOSITION 0x00000004 +#define STARTF_USECOUNTCHARS 0x00000008 +#define STARTF_USEFILLATTRIBUTE 0x00000010 +#define STARTF_RUNFULLSCREEN 0x00000020 +#define STARTF_FORCEONFEEDBACK 0x00000040 +#define STARTF_FORCEOFFFEEDBACK 0x00000080 +#define STARTF_USESTDHANDLES 0x00000100 +#define STARTF_USEHOTKEY 0x00000200 +#define STARTF_TITLEISLINKNAME 0x00000800 +#define STARTF_TITLEISAPPID 0x00001000 +#define STARTF_PREVENTPINNING 0x00002000 + /* Process */ #define LOGON_WITH_PROFILE 0x00000001 diff --git a/winpr/libwinpr/thread/test/TestThreadCreateProcess.c b/winpr/libwinpr/thread/test/TestThreadCreateProcess.c index b3c9f01..cbf83dc 100644 --- a/winpr/libwinpr/thread/test/TestThreadCreateProcess.c +++ b/winpr/libwinpr/thread/test/TestThreadCreateProcess.c @@ -7,7 +7,8 @@ #include #include -#define TESTENV "TEST_PROCESS=oyeah" +#define TESTENV_A "HELLO=WORLD" +#define TESTENV_T _T(TESTENV_A) int TestThreadCreateProcess(int argc, char* argv[]) { @@ -26,17 +27,17 @@ int TestThreadCreateProcess(int argc, char* argv[]) LPTCH lpszEnvironmentBlock; HANDLE pipe_read = NULL; HANDLE pipe_write = NULL; - char buf[255]; + char buf[1024]; DWORD read_bytes; int ret = 0; + SECURITY_ATTRIBUTES saAttr; lpszEnvironmentBlock = GetEnvironmentStrings(); lpApplicationName = NULL; - //lpCommandLine = _T("ls -l /"); #ifdef _WIN32 - lpCommandLine = _T("env"); + lpCommandLine = _T("cmd /C set"); #else lpCommandLine = _T("printenv"); #endif @@ -45,7 +46,9 @@ int TestThreadCreateProcess(int argc, char* argv[]) lpThreadAttributes = NULL; bInheritHandles = FALSE; dwCreationFlags = 0; - lpEnvironment = NULL; +#ifdef _UNICODE + dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT; +#endif lpEnvironment = lpszEnvironmentBlock; lpCurrentDirectory = NULL; ZeroMemory(&StartupInfo, sizeof(STARTUPINFO)); @@ -69,8 +72,11 @@ int TestThreadCreateProcess(int argc, char* argv[]) return 1; } - - WaitForSingleObject(ProcessInformation.hProcess, INFINITE); + if (WaitForSingleObject(ProcessInformation.hProcess, 5000) != WAIT_OBJECT_0) + { + printf("Failed to wait for first process. error=%d\n", GetLastError()); + return 1; + } exitCode = 0; status = GetExitCodeProcess(ProcessInformation.hProcess, &exitCode); @@ -82,22 +88,36 @@ int TestThreadCreateProcess(int argc, char* argv[]) CloseHandle(ProcessInformation.hThread); FreeEnvironmentStrings(lpszEnvironmentBlock); + /* Test stdin,stdout,stderr redirection */ - ZeroMemory(&StartupInfo, sizeof(STARTUPINFO)); - StartupInfo.cb = sizeof(STARTUPINFO); - ZeroMemory(&ProcessInformation, sizeof(PROCESS_INFORMATION)); - if (!CreatePipe(&pipe_read, &pipe_write, NULL, 0)) + saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); + saAttr.bInheritHandle = TRUE; + saAttr.lpSecurityDescriptor = NULL; + + if (!CreatePipe(&pipe_read, &pipe_write, &saAttr, 0)) { printf("Pipe creation failed. error=%d\n", GetLastError()); return 1; } + + bInheritHandles = TRUE; + + ZeroMemory(&StartupInfo, sizeof(STARTUPINFO)); + StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.hStdOutput = pipe_write; StartupInfo.hStdError = pipe_write; + StartupInfo.dwFlags = STARTF_USESTDHANDLES; + + ZeroMemory(&ProcessInformation, sizeof(PROCESS_INFORMATION)); + + if (!(lpEnvironment = calloc(1, sizeof(TESTENV_T) + sizeof(TCHAR)))) + { + printf("Failed to allocate environment buffer. error=%d\n", GetLastError()); + return 1; + } + memcpy(lpEnvironment, (void*)TESTENV_T, sizeof(TESTENV_T)); - lpEnvironment = calloc(1, sizeof(TESTENV) + 1); - strncpy(lpEnvironment, TESTENV, strlen(TESTENV)); - lpCommandLine = _T("printenv"); status = CreateProcess(lpApplicationName, lpCommandLine, @@ -120,23 +140,20 @@ int TestThreadCreateProcess(int argc, char* argv[]) return 1; } - if (WaitForSingleObject(pipe_read, 200) != WAIT_OBJECT_0) + if (WaitForSingleObject(ProcessInformation.hProcess, 5000) != WAIT_OBJECT_0) { - printf("pipe wait failed.\n"); - ret = 1; + printf("Failed to wait for second process. error=%d\n", GetLastError()); + return 1; } - else + + ZeroMemory(buf, sizeof(buf)); + ReadFile(pipe_read, buf, sizeof(buf)-1, &read_bytes, NULL); + if (!strstr((const char*)buf, TESTENV_A)) { - ReadFile(pipe_read, buf, 255, &read_bytes, NULL); - if (read_bytes < strlen(TESTENV)) - { - printf("pipe read problem?!\n"); - ret = 1; - } + printf("No or unexpected data read from pipe\n"); + ret = 1; } - WaitForSingleObject(ProcessInformation.hProcess, INFINITE); - CloseHandle(pipe_read); CloseHandle(pipe_write); -- 2.7.4