[PATCH 1/2] endproc.c: Implement ProcessPage_OnEndProcessTree

Akarsha Sehwag akarsha15010 at iiitd.ac.in
Thu Mar 30 12:57:59 CDT 2017


Fixes https://bugs.winehq.org/show_bug.cgi?id=39640

Changes: The patch now endProcessTree which terminates all the child processes along with the current process.

Signed-off-by: Akarsha Sehwag <akarsha15010 at iiitd.ac.in>
---
 programs/taskmgr/endproc.c | 103 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 91 insertions(+), 12 deletions(-)

diff --git a/programs/taskmgr/endproc.c b/programs/taskmgr/endproc.c
index 89c2d7b..25cbb59 100644
--- a/programs/taskmgr/endproc.c
+++ b/programs/taskmgr/endproc.c
@@ -36,6 +36,12 @@ static WCHAR wszWarnMsg[511];
 static WCHAR wszWarnTitle[255];
 static WCHAR wszUnable2Terminate[255];
 
+typedef struct process_list {
+    int         *pid;       /*dynamic array to store the process IDs*/
+    SIZE_T      count;      /*index to maintain the last entry of the array;*/
+    SIZE_T      size;       /*the current size of the pid array*/
+} process_list;
+
 static void load_message_strings(void)
 {
     LoadStringW(hInst, IDS_TERMINATE_MESSAGE, wszWarnMsg, sizeof(wszWarnMsg)/sizeof(WCHAR));
@@ -93,6 +99,56 @@ void ProcessPage_OnEndProcess(void)
     CloseHandle(hProcess);
 }
 
+
+static void init_process_list(process_list *list) {
+    list->size = 4;            /*initialise size with 4. Will increase if necessary.*/
+    list->pid = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size * sizeof(int));
+    list->count = 0;
+}
+
+static void increase_list_size(process_list *list) {
+    list->size *= 2;
+    list->pid = HeapRealloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->pid, list->size * sizeof(int));
+}
+
+static void process_list_append(process_list *list, int id) {
+    if(list->count == list->size)
+        increase_list_size(list);
+    list->pid[list->count] = id;
+    list->count += 1;
+}
+
+static void free_process_list(process_list *list) {
+    HeapFree(GetProcessHeap(), 0, list->pid)
+}
+
+
+static void enum_process_children(HANDLE snapshot, process_list *list, DWORD pid) {
+    PROCESSENTRY32 entry, 
+    SIZE_T start, end, i;
+
+    start = list->count;
+
+    entry.dwSize = sizeof(entry);
+
+    if(!Process32First(snapshot, &entry))
+        return;
+
+    do 
+    {
+        if(entry.th32ParentProcessID == pid)
+            process_list_append(list, entry.th32ProcessID);
+    } while (Process32Next(snapshot, &entry));
+
+    end = list->count;
+
+    for(i = start; i < end; ++i)
+    {
+        enum_process_children(snapshot, list, list->pid[i]);
+    }
+}
+
+
 void ProcessPage_OnEndProcessTree(void)
 {
     LVITEMW          lvitem;
@@ -100,6 +156,9 @@ void ProcessPage_OnEndProcessTree(void)
     DWORD            dwProcessId;
     HANDLE           hProcess;
     WCHAR            wstrErrorText[256];
+    process_list     list;
+    SIZE_T           i;
+    HANDLE           snapshot;
 
     load_message_strings();
 
@@ -125,20 +184,40 @@ void ProcessPage_OnEndProcessTree(void)
     if (MessageBoxW(hMainWnd, wszWarnMsg, wszWarnTitle, MB_YESNO|MB_ICONWARNING) != IDYES)
         return;
 
-    hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
 
-    if (!hProcess)
-    {
-        GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
-        MessageBoxW(hMainWnd, wstrErrorText,wszUnable2Terminate, MB_OK|MB_ICONSTOP);
+    init_process_list(&list);
+
+    if(list->pid == NULL)
         return;
-    }
 
-    if (!TerminateProcess(hProcess, 0))
-    {
-        GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
-        MessageBoxW(hMainWnd, wstrErrorText,wszUnable2Terminate, MB_OK|MB_ICONSTOP);
+    process_list_append(&list, dwProcessId);
+
+    snapshot = CreateToolhelp32Snapshot(TH32SNAPPROCESS, 0);
+
+    if(!snapshot)
+        return;
+
+    enum_process_children(snapshot, &list, dwProcessId);
+    
+    CloseHandle(snapshot);
+
+    for(i = 0; i <= list->last; ++i) {
+        hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, list->pid[i]);        
+
+        if (!hProcess)
+        {
+            GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
+            MessageBoxW(hMainWnd, wstrErrorText,wszUnable2Terminate, MB_OK|MB_ICONSTOP);
+            break;
+        }
+
+        if (!TerminateProcess(hProcess, 0))
+        {
+            GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
+            MessageBoxW(hMainWnd, wstrErrorText,wszUnable2Terminate, MB_OK|MB_ICONSTOP);
+        }
+        CloseHandle(hProcess);
     }
 
-    CloseHandle(hProcess);
-}
+    free_process_list(list);
+}
\ No newline at end of file
-- 
2.9.3




More information about the wine-patches mailing list