Skip to contentSkip to conversation

KVM test: rss.cpp: send characters to the console window rather than directly to STDIN

PATCH Michael Goldish 2 participants 2 messages Sep 21, 2009 – Sep 22, 2009 1 message not in the archive KVM

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 4 lines · ----- "Yolkfull Chow
Quoted 1 line · Michael Goldish

Some Windows programs behave badly when their STDIN is redirected to

a pipe

Quoted 1 line

(most notably wmic). Therefore, keep STDIN unredirected, and send

Quoted 8 lines

input to the

Quoted 1 line

console window as a series of WM_CHAR messages.

Hi Michael, I just tried this patch. After re-compiling and installing RSS, seems never a command could be executed successfully or
returned with results. I tested this on Win2008-32. Any clue for fixing up it?

Did you also apply the other patch --
"allow setting shell line separator string in the config file"?

By default, when using nc, autotest ends commands with "\n". The modified rss.exe seems to require lines to end with "\r\n" (it's a common line separator in Windows).
The patch I mentioned adds a "shell_linesep" parameter that controls the line separator and changes the line separator on Windows to "\r\n".

If you can't apply the patch (conflicts or whatever), go to kvm_utils.py, find the "netcat" function, and change this line:
return remote_login(command, password, prompt, "\n", timeout) to this:
return remote_login(command, password, prompt, "\r\n", timeout)

If it still doesn't work then we have a real problem and I'll have to start debugging stuff.

(Note: if you want to manually test rss.exe, use telnet instead of nc (e.g. telnet localhost 5000) because telnet ends lines with "\r\n".)

Quoted 177 lines
Quoted 4 lines
---
 client/tests/kvm/deps/rss.cpp |   54

+++++++++++++++++-----------------------

Review

Quoted 3 lines
 1 files changed, 23 insertions(+), 31 deletions(-)
(quoted diff) +0 -0

b/client/tests/kvm/deps/rss.cpp

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID

Quoted 15 lines

index 73a849a..66d9a5b 100644

client/tests/kvm/deps/rss.cpp +1 -1
@@ -22,9 +22,9 @@ struct client_info {
     SOCKET socket;
     sockaddr_in addr;
     int pid;
+    HWND hwnd;
     HANDLE hJob;
     HANDLE hChildOutputRead;
-    HANDLE hChildInputWrite;
     HANDLE hThreadChildToSocket;
 };
 
@@ -161,15 +161,10 @@ DWORD WINAPI SocketToChild(LPVOID

client_info_ptr)

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID · line 170

Quoted 10 lines
client/tests/kvm/deps/rss.cpp +0 -7
         sprintf(message, "Client (%s) entered text: \"%s\"\r\n",
                 client_info_str, formatted_buffer);
         AppendMessage(message);
-        // Write the data to the child's STDIN
-        WriteFile(ci.hChildInputWrite, buffer, bytes_received,
-                  &bytes_written, NULL);
-        // Make sure all the data was written
-        if (bytes_written != bytes_received) {
-            sprintf(message,
-                    "SocketToChild: bytes received (%d) != bytes

written (%d)",

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID · line 164

Quoted 3 lines
client/tests/kvm/deps/rss.cpp +1 -2
-                    bytes_received, bytes_written);
-            ExitOnError(message, 1);
+        // Send the data as a series of WM_CHAR messages to the

console window

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID

Quoted 7 lines
client/tests/kvm/deps/rss.cpp +3 -0
+        for (int i=0; i<bytes_received; i++) {
+            SendMessage(ci.hwnd, WM_CHAR, (WPARAM)buffer[i], 0);
+            SendMessage(ci.hwnd, WM_SETFOCUS, 0, 0);
         }
     }
 
@@ -194,7 +189,6 @@ DWORD WINAPI SocketToChild(LPVOID

client_info_ptr)

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID

Quoted 8 lines
client/tests/kvm/deps/rss.cpp +0 -1
     CloseHandle(ci.hJob);
     CloseHandle(ci.hThreadChildToSocket);
     CloseHandle(ci.hChildOutputRead);
-    CloseHandle(ci.hChildInputWrite);
 
     AppendMessage("SocketToChild thread exited\r\n");
 
@@ -203,18 +197,25 @@ DWORD WINAPI SocketToChild(LPVOID

client_info_ptr)

client/tests/kvm/deps/rss.cpp void PrepAndLaunchRedirectedChild(client_info

Quoted 28 lines
client/tests/kvm/deps/rss.cpp +9 -2
 void PrepAndLaunchRedirectedChild(client_info *ci,
                                   HANDLE hChildStdOut,
-                                  HANDLE hChildStdIn,
                                   HANDLE hChildStdErr)
 {
     PROCESS_INFORMATION pi;
     STARTUPINFO si;
 
+    // Allocate a new console for the child
+    HWND hwnd = GetForegroundWindow();
+    FreeConsole();
+    AllocConsole();
+    ShowWindow(GetConsoleWindow(), SW_HIDE);
+    if (hwnd)
+        SetForegroundWindow(hwnd);
+
     // Set up the start up info struct.
     ZeroMemory(&si, sizeof(STARTUPINFO));
     si.cb = sizeof(STARTUPINFO);
     si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
     si.hStdOutput = hChildStdOut;
-    si.hStdInput  = hChildStdIn;
+    si.hStdInput  = GetStdHandle(STD_INPUT_HANDLE);
     si.hStdError  = hChildStdErr;
     // Use this if you want to hide the child:
     si.wShowWindow = SW_HIDE;
@@ -223,7 +224,7 @@ void PrepAndLaunchRedirectedChild(client_info

*ci,

client/tests/kvm/deps/rss.cpp void PrepAndLaunchRedirectedChild(client_info · line 225

Quoted 4 lines
client/tests/kvm/deps/rss.cpp +0 -1
     // Launch the process that you want to redirect.
     if (!CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE,
-                       CREATE_NEW_CONSOLE, NULL, "C:\\", &si,

&pi))

client/tests/kvm/deps/rss.cpp void PrepAndLaunchRedirectedChild(client_info

Quoted 5 lines
client/tests/kvm/deps/rss.cpp +1 -0
+                       0, NULL, "C:\\", &si, &pi))
         ExitOnError("CreateProcess failed");
 
     // Close any unnecessary handles.
@@ -235,12 +236,16 @@ void PrepAndLaunchRedirectedChild(client_info

*ci,

Quoted 18 lines
client/tests/kvm/deps/rss.cpp +5 -1
     // Assign the process to a newly created JobObject
     ci->hJob = CreateJobObject(NULL, NULL);
     AssignProcessToJobObject(ci->hJob, pi.hProcess);
+    // Keep the console window's handle
+    ci->hwnd = GetConsoleWindow();
+
+    // Detach from the child's console
+    FreeConsole();
 }
 
 void SpawnSession(client_info *ci)
 {
     HANDLE hOutputReadTmp, hOutputRead, hOutputWrite;
-    HANDLE hInputWriteTmp, hInputRead, hInputWrite;
     HANDLE hErrorWrite;
     SECURITY_ATTRIBUTES sa;
 

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 269

Quoted 8 lines
client/tests/kvm/deps/rss.cpp +0 -4
@@ -261,10 +266,6 @@ void SpawnSession(client_info *ci)
                          TRUE, DUPLICATE_SAME_ACCESS))
         ExitOnError("DuplicateHandle failed");
 
-    // Create the child input pipe.
-    if (!CreatePipe(&hInputRead, &hInputWriteTmp, &sa, 0))
-        ExitOnError("CreatePipe failed");
-
     // Create new output read handle and the input write handles.

Set

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 271

Quoted 2 lines
client/tests/kvm/deps/rss.cpp +0 -0
     // the Properties to FALSE. Otherwise, the child inherits the
     // properties and, as a result, non-closeable handles to the

pipes

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 280

Quoted 12 lines
client/tests/kvm/deps/rss.cpp +0 -7
@@ -276,29 +277,20 @@ void SpawnSession(client_info *ci)
                          DUPLICATE_SAME_ACCESS))
         ExitOnError("DuplicateHandle failed");
 
-    if (!DuplicateHandle(GetCurrentProcess(), hInputWriteTmp,
-                         GetCurrentProcess(),
-                         &hInputWrite, // Address of new handle.
-                         0, FALSE, // Make it uninheritable.
-                         DUPLICATE_SAME_ACCESS))
-        ExitOnError("DuplicateHandle failed");
-
     // Close inheritable copies of the handles you do not want to

be

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 288

Quoted 2 lines
client/tests/kvm/deps/rss.cpp +0 -1
     // inherited.
-    if (!CloseHandle(hOutputReadTmp)) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 289

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -1
-    if (!CloseHandle(hInputWriteTmp)) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 291

Quoted 4 lines
client/tests/kvm/deps/rss.cpp +2 -1
+    if (!CloseHandle(hOutputReadTmp))
+        ExitOnError("CloseHandle failed");
 
-    PrepAndLaunchRedirectedChild(ci, hOutputWrite, hInputRead,

hErrorWrite);

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 290

Quoted 7 lines
client/tests/kvm/deps/rss.cpp +1 -1
+    PrepAndLaunchRedirectedChild(ci, hOutputWrite, hErrorWrite);
 
     ci->hChildOutputRead = hOutputRead;
-    ci->hChildInputWrite = hInputWrite;
 
     // Close pipe handles (do not continue to modify the parent).
     // You need to make sure that no handles to the write end of

the

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 291

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     // output pipe are maintained in this process or else the pipe

will

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 292

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     // not close when the child process exits and the ReadFile will

hang.

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 293

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     if (!CloseHandle(hOutputWrite)) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 301

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -1
-    if (!CloseHandle(hInputRead )) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 294

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     if (!CloseHandle(hErrorWrite)) ExitOnError("CloseHandle

failed");

Quoted 9 lines
client/tests/kvm/deps/rss.cpp +0 -0
 }
 
Signature · 6 lines
1.5.4.1

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
#2 lore raw
Quoted 10 lines · Michael Goldish
Quoted 7 lines · ----- "Yolkfull Chow
Quoted 1 line · Michael Goldish

Some Windows programs behave badly when their STDIN is redirected to

a pipe

Quoted 1 line

(most notably wmic). Therefore, keep STDIN unredirected, and send

input to the

Quoted 1 line

console window as a series of WM_CHAR messages.

Quoted 8 lines
Quoted 5 lines

Hi Michael, I just tried this patch. After re-compiling and installing RSS, seems never a command could be executed successfully or
returned with results. I tested this on Win2008-32. Any clue for fixing up it?

Did you also apply the other patch --
"allow setting shell line separator string in the config file"?

I did forget apply the second patch of that set. It now works and 'wmic' works fine as well. Thank you very much for working this out.:-)

Cheers,

Quoted 200 lines

By default, when using nc, autotest ends commands with "\n". The modified rss.exe seems to require lines to end with "\r\n" (it's a common line separator in Windows).
The patch I mentioned adds a "shell_linesep" parameter that controls the line separator and changes the line separator on Windows to "\r\n".

If you can't apply the patch (conflicts or whatever), go to kvm_utils.py, find the "netcat" function, and change this line:
return remote_login(command, password, prompt, "\n", timeout) to this:
return remote_login(command, password, prompt, "\r\n", timeout)

If it still doesn't work then we have a real problem and I'll have to start debugging stuff.

(Note: if you want to manually test rss.exe, use telnet instead of nc (e.g. telnet localhost 5000) because telnet ends lines with "\r\n".)

Quoted 160 lines
Quoted 4 lines
---
 client/tests/kvm/deps/rss.cpp |   54

+++++++++++++++++-----------------------

Review

Quoted 3 lines
 1 files changed, 23 insertions(+), 31 deletions(-)
(quoted diff) +0 -0

b/client/tests/kvm/deps/rss.cpp

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID

Quoted 15 lines

index 73a849a..66d9a5b 100644

client/tests/kvm/deps/rss.cpp +1 -1
@@ -22,9 +22,9 @@ struct client_info {
     SOCKET socket;
     sockaddr_in addr;
     int pid;
+    HWND hwnd;
     HANDLE hJob;
     HANDLE hChildOutputRead;
-    HANDLE hChildInputWrite;
     HANDLE hThreadChildToSocket;
 };
 
@@ -161,15 +161,10 @@ DWORD WINAPI SocketToChild(LPVOID

client_info_ptr)

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID · line 170

Quoted 10 lines
client/tests/kvm/deps/rss.cpp +0 -7
         sprintf(message, "Client (%s) entered text: \"%s\"\r\n",
                 client_info_str, formatted_buffer);
         AppendMessage(message);
-        // Write the data to the child's STDIN
-        WriteFile(ci.hChildInputWrite, buffer, bytes_received,
-                  &bytes_written, NULL);
-        // Make sure all the data was written
-        if (bytes_written != bytes_received) {
-            sprintf(message,
-                    "SocketToChild: bytes received (%d) != bytes

written (%d)",

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID · line 164

Quoted 3 lines
client/tests/kvm/deps/rss.cpp +1 -2
-                    bytes_received, bytes_written);
-            ExitOnError(message, 1);
+        // Send the data as a series of WM_CHAR messages to the

console window

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID

Quoted 7 lines
client/tests/kvm/deps/rss.cpp +3 -0
+        for (int i=0; i<bytes_received; i++) {
+            SendMessage(ci.hwnd, WM_CHAR, (WPARAM)buffer[i], 0);
+            SendMessage(ci.hwnd, WM_SETFOCUS, 0, 0);
         }
     }
 
@@ -194,7 +189,6 @@ DWORD WINAPI SocketToChild(LPVOID

client_info_ptr)

client/tests/kvm/deps/rss.cpp DWORD WINAPI SocketToChild(LPVOID

Quoted 8 lines
client/tests/kvm/deps/rss.cpp +0 -1
     CloseHandle(ci.hJob);
     CloseHandle(ci.hThreadChildToSocket);
     CloseHandle(ci.hChildOutputRead);
-    CloseHandle(ci.hChildInputWrite);
 
     AppendMessage("SocketToChild thread exited\r\n");
 
@@ -203,18 +197,25 @@ DWORD WINAPI SocketToChild(LPVOID

client_info_ptr)

client/tests/kvm/deps/rss.cpp void PrepAndLaunchRedirectedChild(client_info

Quoted 28 lines
client/tests/kvm/deps/rss.cpp +9 -2
 void PrepAndLaunchRedirectedChild(client_info *ci,
                                   HANDLE hChildStdOut,
-                                  HANDLE hChildStdIn,
                                   HANDLE hChildStdErr)
 {
     PROCESS_INFORMATION pi;
     STARTUPINFO si;
 
+    // Allocate a new console for the child
+    HWND hwnd = GetForegroundWindow();
+    FreeConsole();
+    AllocConsole();
+    ShowWindow(GetConsoleWindow(), SW_HIDE);
+    if (hwnd)
+        SetForegroundWindow(hwnd);
+
     // Set up the start up info struct.
     ZeroMemory(&si, sizeof(STARTUPINFO));
     si.cb = sizeof(STARTUPINFO);
     si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
     si.hStdOutput = hChildStdOut;
-    si.hStdInput  = hChildStdIn;
+    si.hStdInput  = GetStdHandle(STD_INPUT_HANDLE);
     si.hStdError  = hChildStdErr;
     // Use this if you want to hide the child:
     si.wShowWindow = SW_HIDE;
@@ -223,7 +224,7 @@ void PrepAndLaunchRedirectedChild(client_info

*ci,

client/tests/kvm/deps/rss.cpp void PrepAndLaunchRedirectedChild(client_info · line 225

Quoted 4 lines
client/tests/kvm/deps/rss.cpp +0 -1
     // Launch the process that you want to redirect.
     if (!CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE,
-                       CREATE_NEW_CONSOLE, NULL, "C:\\", &si,

&pi))

client/tests/kvm/deps/rss.cpp void PrepAndLaunchRedirectedChild(client_info

Quoted 5 lines
client/tests/kvm/deps/rss.cpp +1 -0
+                       0, NULL, "C:\\", &si, &pi))
         ExitOnError("CreateProcess failed");
 
     // Close any unnecessary handles.
@@ -235,12 +236,16 @@ void PrepAndLaunchRedirectedChild(client_info

*ci,

Quoted 18 lines
client/tests/kvm/deps/rss.cpp +5 -1
     // Assign the process to a newly created JobObject
     ci->hJob = CreateJobObject(NULL, NULL);
     AssignProcessToJobObject(ci->hJob, pi.hProcess);
+    // Keep the console window's handle
+    ci->hwnd = GetConsoleWindow();
+
+    // Detach from the child's console
+    FreeConsole();
 }
 
 void SpawnSession(client_info *ci)
 {
     HANDLE hOutputReadTmp, hOutputRead, hOutputWrite;
-    HANDLE hInputWriteTmp, hInputRead, hInputWrite;
     HANDLE hErrorWrite;
     SECURITY_ATTRIBUTES sa;
 

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 269

Quoted 8 lines
client/tests/kvm/deps/rss.cpp +0 -4
@@ -261,10 +266,6 @@ void SpawnSession(client_info *ci)
                          TRUE, DUPLICATE_SAME_ACCESS))
         ExitOnError("DuplicateHandle failed");
 
-    // Create the child input pipe.
-    if (!CreatePipe(&hInputRead, &hInputWriteTmp, &sa, 0))
-        ExitOnError("CreatePipe failed");
-
     // Create new output read handle and the input write handles.

Set

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 271

Quoted 2 lines
client/tests/kvm/deps/rss.cpp +0 -0
     // the Properties to FALSE. Otherwise, the child inherits the
     // properties and, as a result, non-closeable handles to the

pipes

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 280

Quoted 12 lines
client/tests/kvm/deps/rss.cpp +0 -7
@@ -276,29 +277,20 @@ void SpawnSession(client_info *ci)
                          DUPLICATE_SAME_ACCESS))
         ExitOnError("DuplicateHandle failed");
 
-    if (!DuplicateHandle(GetCurrentProcess(), hInputWriteTmp,
-                         GetCurrentProcess(),
-                         &hInputWrite, // Address of new handle.
-                         0, FALSE, // Make it uninheritable.
-                         DUPLICATE_SAME_ACCESS))
-        ExitOnError("DuplicateHandle failed");
-
     // Close inheritable copies of the handles you do not want to

be

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 288

Quoted 2 lines
client/tests/kvm/deps/rss.cpp +0 -1
     // inherited.
-    if (!CloseHandle(hOutputReadTmp)) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 289

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -1
-    if (!CloseHandle(hInputWriteTmp)) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 291

Quoted 4 lines
client/tests/kvm/deps/rss.cpp +2 -1
+    if (!CloseHandle(hOutputReadTmp))
+        ExitOnError("CloseHandle failed");
 
-    PrepAndLaunchRedirectedChild(ci, hOutputWrite, hInputRead,

hErrorWrite);

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 290

Quoted 7 lines
client/tests/kvm/deps/rss.cpp +1 -1
+    PrepAndLaunchRedirectedChild(ci, hOutputWrite, hErrorWrite);
 
     ci->hChildOutputRead = hOutputRead;
-    ci->hChildInputWrite = hInputWrite;
 
     // Close pipe handles (do not continue to modify the parent).
     // You need to make sure that no handles to the write end of

the

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 291

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     // output pipe are maintained in this process or else the pipe

will

Quoted 17 lines

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 292

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     // not close when the child process exits and the ReadFile will

hang.

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 293

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     if (!CloseHandle(hOutputWrite)) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 301

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -1
-    if (!CloseHandle(hInputRead )) ExitOnError("CloseHandle

failed");

client/tests/kvm/deps/rss.cpp void SpawnSession(client_info *ci) · line 294

Quoted 1 line
client/tests/kvm/deps/rss.cpp +0 -0
     if (!CloseHandle(hErrorWrite)) ExitOnError("CloseHandle

failed");

Quoted 9 lines
client/tests/kvm/deps/rss.cpp +0 -0
 }
 
Signature · 6 lines
1.5.4.1

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html