[Spice-devel] [PATCH vdagent-win 3/6] vdservice: clean agent proc handle in kill_agent()

Arnon Gilboa agilboa at redhat.com
Sun Jul 24 07:38:18 PDT 2011


fill_agent_events() will not add it to the events used by WaitFor(),
preventing usage of a dead process handle.
---
 vdservice/vdservice.cpp |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/vdservice/vdservice.cpp b/vdservice/vdservice.cpp
index 8c34f59..e09828a 100644
--- a/vdservice/vdservice.cpp
+++ b/vdservice/vdservice.cpp
@@ -909,21 +909,24 @@ bool VDService::kill_agent()
 {
     DWORD exit_code = 0;
     DWORD wait_ret;
+    HANDLE proc_handle;
     bool ret = true;
 
     if (!_agent_alive) {
         return true;
     }
     _agent_alive = false;
+    proc_handle = _agent_proc_info.hProcess;
+    _agent_proc_info.hProcess = 0;
     if (_pipe_connected) {
         _pipe_connected = false;
         DisconnectNamedPipe(_pipe_state.pipe);
     }
-    if (GetProcessId(_agent_proc_info.hProcess)) {
-        wait_ret = WaitForSingleObject(_agent_proc_info.hProcess, 3000);
+    if (GetProcessId(proc_handle)) {
+        wait_ret = WaitForSingleObject(proc_handle, 3000);
         switch (wait_ret) {
         case WAIT_OBJECT_0:
-            if (GetExitCodeProcess(_agent_proc_info.hProcess, &exit_code)) {
+            if (GetExitCodeProcess(proc_handle, &exit_code)) {
                 vd_printf("vdagent exit code %u", exit_code);
             } else if (exit_code == STILL_ACTIVE) {
                 vd_printf("Failed killing vdagent");
@@ -942,7 +945,7 @@ bool VDService::kill_agent()
             break;
         }
     }
-    CloseHandle(_agent_proc_info.hProcess);
+    CloseHandle(proc_handle);
     CloseHandle(_agent_proc_info.hThread);
     ZeroMemory(&_agent_proc_info, sizeof(_agent_proc_info));
     return ret;
-- 
1.7.4.1



More information about the Spice-devel mailing list