From 84a00daed3a84b7e3861e3b7b98402d2d58af6c5 Mon Sep 17 00:00:00 2001 From: braydonk Date: Thu, 27 Apr 2023 15:57:45 +0000 Subject: [PATCH] process: implement NumFDs for Windows This PR implements the `NumFDs` method for Windows by borrowing the system info query from `OpenFiles` and using it to count any Handles belonging to the process. Signed-off-by: braydonk --- process/process_windows.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/process/process_windows.go b/process/process_windows.go index 14ed0309f..4b0632696 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -549,7 +549,37 @@ func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitche } func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError + buffer := make([]byte, 1024) + var size uint32 + + st := common.CallWithExpandingBuffer( + func() common.NtStatus { + return common.NtQuerySystemInformation( + common.SystemExtendedHandleInformationClass, + &buffer[0], + uint32(len(buffer)), + &size, + ) + }, + &buffer, + &size, + ) + if st.IsError() { + return 0, st.Error() + } + + handlesList := (*common.SystemExtendedHandleInformation)(unsafe.Pointer(&buffer[0])) + handles := make([]common.SystemExtendedHandleTableEntryInformation, int(handlesList.NumberOfHandles)) + hdr := (*reflect.SliceHeader)(unsafe.Pointer(&handles)) + hdr.Data = uintptr(unsafe.Pointer(&handlesList.Handles[0])) + + var handleCount int32 + for _, handle := range handles { + if int32(handle.UniqueProcessId) == p.Pid { + handleCount++ + } + } + return handleCount, nil } func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {