1
0
mirror of https://github.com/libuv/libuv synced 2025-03-28 21:13:16 +00:00

win: use NtQueryInformationProcess in uv_os_getppid (#4514)

Get parent process ID using NtQueryInformationProcess, it's faster than
using CreateToolhelp32Snapshot.
This commit is contained in:
Zuohui Yang 2024-08-27 02:17:53 +08:00 committed by GitHub
parent 58dfb6c89b
commit 5cbc82e369
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 17 deletions

View File

@ -316,25 +316,19 @@ uv_pid_t uv_os_getpid(void) {
uv_pid_t uv_os_getppid(void) {
int parent_pid = -1;
HANDLE handle;
PROCESSENTRY32 pe;
DWORD current_pid = GetCurrentProcessId();
NTSTATUS nt_status;
PROCESS_BASIC_INFORMATION basic_info;
pe.dwSize = sizeof(PROCESSENTRY32);
handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(handle, &pe)) {
do {
if (pe.th32ProcessID == current_pid) {
parent_pid = pe.th32ParentProcessID;
break;
}
} while( Process32Next(handle, &pe));
nt_status = pNtQueryInformationProcess(GetCurrentProcess(),
ProcessBasicInformation,
&basic_info,
sizeof(basic_info),
NULL);
if (NT_SUCCESS(nt_status)) {
return basic_info.InheritedFromUniqueProcessId;
} else {
return -1;
}
CloseHandle(handle);
return parent_pid;
}

View File

@ -4458,6 +4458,14 @@ typedef struct _FILE_FS_SECTOR_SIZE_INFORMATION {
ULONG ByteOffsetForPartitionAlignment;
} FILE_FS_SECTOR_SIZE_INFORMATION, *PFILE_FS_SECTOR_SIZE_INFORMATION;
typedef struct _PROCESS_BASIC_INFORMATION {
PVOID Reserved1;
PVOID PebBaseAddress;
PVOID Reserved2[2];
ULONG_PTR UniqueProcessId;
ULONG_PTR InheritedFromUniqueProcessId;
} PROCESS_BASIC_INFORMATION, *PPROCESS_BASIC_INFORMATION;
typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
@ -4471,6 +4479,10 @@ typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
# define SystemProcessorPerformanceInformation 8
#endif
#ifndef ProcessBasicInformation
# define ProcessBasicInformation 0
#endif
#ifndef ProcessConsoleHostProcess
# define ProcessConsoleHostProcess 49
#endif