Fix my_safe_kill's create crashdump functionality on Windows.

If pid belongs to my_safe_process.exe, create crashdump of my_safe_process's
child, because my_safe_process is only used to start other programs.
This commit is contained in:
Vladislav Vaintroub 2017-10-24 16:03:05 +00:00
parent b56560ed39
commit 632f0e71f8

View File

@ -26,7 +26,30 @@
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <psapi.h> #include <psapi.h>
#include <DbgHelp.h> #include <dbghelp.h>
#include <tlhelp32.h>
static DWORD find_child(DWORD pid)
{
HANDLE h= NULL;
PROCESSENTRY32 pe={ 0 };
DWORD child_pid = 0;
pe.dwSize = sizeof(PROCESSENTRY32);
h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(h == INVALID_HANDLE_VALUE)
return 0;
for (BOOL ret = Process32First(h, &pe); ret; ret = Process32Next(h, &pe))
{
if (pe.th32ParentProcessID == pid)
{
child_pid = pe.th32ProcessID;
break;
}
}
CloseHandle(h);
return (child_pid);
}
static int create_dump(DWORD pid) static int create_dump(DWORD pid)
{ {
@ -37,6 +60,8 @@ static int create_dump(DWORD pid)
HANDLE file= INVALID_HANDLE_VALUE; HANDLE file= INVALID_HANDLE_VALUE;
char *p; char *p;
for(;;)
{
process= OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, (DWORD)pid); process= OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, (DWORD)pid);
if (!process) if (!process)
{ {
@ -52,6 +77,27 @@ static int create_dump(DWORD pid)
pid, GetLastError()); pid, GetLastError());
goto exit; goto exit;
} }
const char *filename= strrchr(path, '\\');
if (filename)
{
filename++;
// We are not interested in my_safe_process.exe,
// since it is only used to start up other programs.
// We're interested however in my_safe_processes' child.
if (strcmp(filename, "my_safe_process.exe") == 0)
{
pid= find_child(pid);
if (!pid)
{
fprintf(stderr,"safe_kill : can't find child process for safe_process.exe\n");
goto exit;
}
CloseHandle(process);
}
else
break;
}
}
if ((p= strrchr(path, '.')) == 0) if ((p= strrchr(path, '.')) == 0)
p= path + strlen(path); p= path + strlen(path);
@ -100,6 +146,7 @@ exit:
return ret; return ret;
} }
int main(int argc, const char** argv ) int main(int argc, const char** argv )
{ {
DWORD pid= -1; DWORD pid= -1;