[Win32utils-devel] Potential win32-process patch

Daniel Berger djberg96 at gmail.com
Sat Aug 23 20:43:35 EDT 2008


Hi all,

Just going over win32-process, and I saw a few opportunities to refactor 
the kill method.

In summary, signals 2 and 3 now raise an error if they fail, no handle 
is opened in the case of signals 2 and 3, and each of the kill 
approaches is now wrapped in a RUBY_CRITICAL section, which more closely 
mimics MRI.

How does this patch look?

Thanks,

Dan

 >diff -u process.rb process.new
--- process.rb  Sat Aug 23 18:38:00 2008
+++ process.new Sat Aug 23 18:39:06 2008
@@ -155,71 +155,89 @@
              pid = Process.pid
           end

-         # No need for full access if the signal is zero
-         if signal == 0
-            access = PROCESS_QUERY_INFORMATION|PROCESS_VM_READ
-            handle = OpenProcess(access, 0 , pid)
-         else
-            handle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
-         end
-
           case signal
              when 0
-               if handle != 0
-                  killed_pids.push(pid)
-                  CloseHandle(handle)
-               else
-                  # If ERROR_ACCESS_DENIED is returned, we know it's 
running
-                  if GetLastError() == ERROR_ACCESS_DENIED
+               RUBY_CRITICAL{
+                  access = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ
+                  handle = OpenProcess(access, 0 , pid)
+
+                  if handle != 0
                       killed_pids.push(pid)
+                     CloseHandle(handle)
                    else
-                     raise Error, get_last_error
+                     # If ERROR_ACCESS_DENIED is returned, we know it's 
running
+                     if GetLastError() == ERROR_ACCESS_DENIED
+                        killed_pids.push(pid)
+                     else
+                        raise Error, get_last_error
+                     end
                    end
-               end
+               }
              when 2
-               if GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)
-                  killed_pids.push(pid)
-               end
+               RUBY_CRITICAL{
+                  if GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)
+                     killed_pids.push(pid)
+                  else
+                     raise Error, get_last_error
+                  end
+               }
              when 3
-               if GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid)
-                  killed_pids.push(pid)
-               end
+               RUBY_CRITICAL{
+                  if GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid)
+                     killed_pids.push(pid)
+                  else
+                     raise Error, get_last_error
+                  end
+               }
              when 9
-               if TerminateProcess(handle, pid)
-                  CloseHandle(handle)
-                  killed_pids.push(pid)
-                  @child_pids.delete(pid)
-               else
-                  raise Error, get_last_error
-               end
+               RUBY_CRITICAL{
+                  handle = OpenProcess(PROCESS_TERMINATE, 0, pid)
+
+                  if handle == 0
+                     raise Error, get_last_error
+                  end
+
+                  if TerminateProcess(handle, pid)
+                     CloseHandle(handle)
+                     killed_pids.push(pid)
+                     @child_pids.delete(pid)
+                  else
+                     raise Error, get_last_error
+                  end
+               }
              else
-               if handle != 0
-                  thread_id = [0].pack('L')
-                  dll       = 'kernel32'
-                  eproc     = 'ExitProcess'
+               RUBY_CRITICAL{
+                  handle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
+
+                  if handle != 0
+                     thread_id = [0].pack('L')
+                     dll       = 'kernel32'
+                     eproc     = 'ExitProcess'

-                  thread = CreateRemoteThread(
-                     handle,
-                     0,
-                     0,
-                     GetProcAddress(GetModuleHandle(dll), eproc),
-                     0,
-                     0,
-                     thread_id
-                  )
+                     thread = CreateRemoteThread(
+                        handle,
+                        0,
+                        0,
+                        GetProcAddress(GetModuleHandle(dll), eproc),
+                        0,
+                        0,
+                        thread_id
+                     )

-                  if thread
-                     WaitForSingleObject(thread, 5)
-                     CloseHandle(handle)
-                     killed_pids.push(pid)
-                     @child_pids.delete(pid)
+                     if thread
+                        WaitForSingleObject(thread, 5)
+                        CloseHandle(handle)
+                        killed_pids.push(pid)
+                        @child_pids.delete(pid)
+                     else
+                        CloseHandle(handle)
+                        raise Error, get_last_error
+                     end
                    else
-                     CloseHandle(handle)
                       raise Error, get_last_error
                    end
-               else
-                  raise Error, get_last_error
-               end
+               }
+
                 @child_pids.delete(pid)
           end
        }
@@ -613,8 +631,7 @@

        begin
           unless Process32First(handle, proc_entry)
-            error = get_last_error
-            raise Error, error
+            raise Error, get_last_error
           end

           while Process32Next(handle, proc_entry)
@@ -697,4 +714,4 @@
     def fork(&block)
        Process.fork(&block)
     end


More information about the win32utils-devel mailing list