forked from cloudfoundry/gosigar
-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace wmi queries with win32 api calls #116
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
43bc214
Remove wmi queries with win32 api calls
narph b204596
Add more tests
narph 1687040
Add more tests for coverage
narph 106efba
Add tests
narph 6a01c7b
Add tests for coverage
narph c8fd3ef
Remove WIn32Process related structs
narph a4e7d68
Fix test failing on osx
narph 050f7b2
Work on tests
narph f932a64
Clean up tests
narph 14e4bb4
Fix on changelog file
narph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ import ( | |
"syscall" | ||
"time" | ||
|
||
"github.com/StackExchange/wmi" | ||
"github.com/elastic/gosigar/sys/windows" | ||
"github.com/pkg/errors" | ||
) | ||
|
@@ -83,11 +82,12 @@ func (self *Uptime) Get() error { | |
bootTimeLock.Lock() | ||
defer bootTimeLock.Unlock() | ||
if bootTime == nil { | ||
os, err := getWin32OperatingSystem() | ||
uptime, err := windows.GetTickCount64() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get boot time using WMI") | ||
} | ||
bootTime = &os.LastBootUpTime | ||
var boot = time.Unix(int64(uptime), 0) | ||
bootTime = &boot | ||
} | ||
|
||
self.Length = time.Since(*bootTime).Seconds() | ||
|
@@ -251,7 +251,7 @@ func getProcStatus(pid int) (RunState, error) { | |
var exitCode uint32 | ||
err = syscall.GetExitCodeProcess(handle, &exitCode) | ||
if err != nil { | ||
return RunStateUnknown, errors.Wrapf(err, "GetExitCodeProcess failed for pid=%v") | ||
return RunStateUnknown, errors.Wrapf(err, "GetExitCodeProcess failed for pid=%v", pid) | ||
} | ||
|
||
if exitCode == 259 { //still active | ||
|
@@ -371,15 +371,33 @@ func (self *ProcArgs) Get(pid int) error { | |
if !version.IsWindowsVistaOrGreater() { | ||
return ErrNotImplemented{runtime.GOOS} | ||
} | ||
|
||
process, err := getWin32Process(int32(pid)) | ||
handle, err := syscall.OpenProcess(processQueryLimitedInfoAccess|windows.PROCESS_VM_READ, false, uint32(pid)) | ||
if err != nil { | ||
return errors.Wrapf(err, "OpenProcess failed for pid=%v", pid) | ||
} | ||
defer syscall.CloseHandle(handle) | ||
pbi, err := windows.NtQueryProcessBasicInformation(handle) | ||
if err != nil { | ||
return errors.Wrapf(err, "ProcArgs failed for pid=%v", pid) | ||
return errors.Wrapf(err, "NtQueryProcessBasicInformation failed for pid=%v", pid) | ||
} | ||
if err != nil { | ||
return nil | ||
} | ||
userProcParams, err := windows.GetUserProcessParams(handle, pbi) | ||
if err != nil { | ||
return nil | ||
} | ||
var args []string | ||
if argsW, err := windows.ReadProcessUnicodeString(handle, &userProcParams.CommandLine); err == nil { | ||
args, err = windows.ByteSliceToStringSlice(argsW) | ||
if err != nil { | ||
args = nil | ||
} | ||
} | ||
var process = Win32_Process{CommandLine: &args[0]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove the |
||
if process.CommandLine != nil { | ||
self.List = []string{*process.CommandLine} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -396,35 +414,6 @@ func (self *FileSystemUsage) Get(path string) error { | |
return nil | ||
} | ||
|
||
// getWin32Process gets information about the process with the given process ID. | ||
// It uses a WMI query to get the information from the local system. | ||
func getWin32Process(pid int32) (Win32_Process, error) { | ||
var dst []Win32_Process | ||
query := fmt.Sprintf("WHERE ProcessId = %d", pid) | ||
q := wmi.CreateQuery(&dst, query) | ||
err := wmi.Query(q, &dst) | ||
if err != nil { | ||
return Win32_Process{}, fmt.Errorf("could not get Win32_Process %s: %v", query, err) | ||
} | ||
if len(dst) < 1 { | ||
return Win32_Process{}, fmt.Errorf("could not get Win32_Process %s: Process not found", query) | ||
} | ||
return dst[0], nil | ||
} | ||
|
||
func getWin32OperatingSystem() (Win32_OperatingSystem, error) { | ||
var dst []Win32_OperatingSystem | ||
q := wmi.CreateQuery(&dst, "") | ||
err := wmi.Query(q, &dst) | ||
if err != nil { | ||
return Win32_OperatingSystem{}, errors.Wrap(err, "wmi query for Win32_OperatingSystem failed") | ||
} | ||
if len(dst) != 1 { | ||
return Win32_OperatingSystem{}, errors.New("wmi query for Win32_OperatingSystem failed") | ||
} | ||
return dst[0], nil | ||
} | ||
|
||
func (self *Rusage) Get(who int) error { | ||
if who != 0 { | ||
return ErrNotImplemented{runtime.GOOS} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of creating a variable, what about just using
self.List
directly?