Use golang.org/x/windows functions instead of github.com/shirou/w32

All functions used from github.com/shirou/w32 are also available from
golang.org/x/sys/windows which is already used in other places. Convert
the remaining usages to use the functions from x/sys/windows.
pull/785/head
Tobias Klauser 5 years ago
parent 7c51a74806
commit a61c905252

13
Gopkg.lock generated

@ -37,14 +37,6 @@
version = "v1.0.0" version = "v1.0.0"
[[projects]] [[projects]]
branch = "master"
digest = "1:99c6a6dab47067c9b898e8c8b13d130c6ab4ffbcc4b7cc6236c2cd0b1e344f5b"
name = "github.com/shirou/w32"
packages = ["."]
pruneopts = "UT"
revision = "bb4de0191aa41b5507caa14b0650cdbddcd9280b"
[[projects]]
digest = "1:5da8ce674952566deae4dbc23d07c85caafc6cfa815b0b3e03e41979cedb8750" digest = "1:5da8ce674952566deae4dbc23d07c85caafc6cfa815b0b3e03e41979cedb8750"
name = "github.com/stretchr/testify" name = "github.com/stretchr/testify"
packages = [ packages = [
@ -57,7 +49,7 @@
[[projects]] [[projects]]
branch = "master" branch = "master"
digest = "1:4d36bc09c4ad87e371dce781b6bec6164de89e4e32fda093f780fcafc69bfee8" digest = "1:d6ee10ad98c51535647d18a5f327e7e13c863e14ac4044cf7fcaedf570e17341"
name = "golang.org/x/sys" name = "golang.org/x/sys"
packages = [ packages = [
"unix", "unix",
@ -66,14 +58,13 @@
"windows/svc/mgr", "windows/svc/mgr",
] ]
pruneopts = "UT" pruneopts = "UT"
revision = "fc99dfbffb4e5ed5758a37e31dd861afe285406b" revision = "d32e6e3b99c40f2bfaea45ea9596ed539eed1c0d"
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
input-imports = [ input-imports = [
"github.com/StackExchange/wmi", "github.com/StackExchange/wmi",
"github.com/shirou/w32",
"github.com/stretchr/testify/assert", "github.com/stretchr/testify/assert",
"github.com/stretchr/testify/require", "github.com/stretchr/testify/require",
"golang.org/x/sys/unix", "golang.org/x/sys/unix",

@ -30,10 +30,6 @@
version = "1.0.0" version = "1.0.0"
[[constraint]] [[constraint]]
branch = "master"
name = "github.com/shirou/w32"
[[constraint]]
name = "github.com/stretchr/testify" name = "github.com/stretchr/testify"
version = "1.2.2" version = "1.2.2"

@ -15,7 +15,6 @@ import (
cpu "github.com/shirou/gopsutil/cpu" cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
net "github.com/shirou/gopsutil/net" net "github.com/shirou/gopsutil/net"
"github.com/shirou/w32"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
@ -160,8 +159,8 @@ func pidsWithContext(ctx context.Context) ([]int32, error) {
for { for {
ps := make([]uint32, psSize) ps := make([]uint32, psSize)
if !w32.EnumProcesses(ps, uint32(len(ps)), &read) { if err := windows.EnumProcesses(ps, &read); err != nil {
return nil, fmt.Errorf("could not get w32.EnumProcesses") return nil, err
} }
if uint32(len(ps)) == read { // ps buffer was too small to host every results, retry with a bigger one if uint32(len(ps)) == read { // ps buffer was too small to host every results, retry with a bigger one
psSize += 1024 psSize += 1024
@ -599,24 +598,24 @@ func (p *Process) Children() ([]*Process, error) {
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
out := []*Process{} out := []*Process{}
snap := w32.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, uint32(0)) snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, uint32(0))
if snap == 0 { if err != nil {
return out, windows.GetLastError() return out, err
} }
defer w32.CloseHandle(snap) defer windows.CloseHandle(snap)
var pe32 w32.PROCESSENTRY32 var pe32 windows.ProcessEntry32
pe32.DwSize = uint32(unsafe.Sizeof(pe32)) pe32.Size = uint32(unsafe.Sizeof(pe32))
if !w32.Process32First(snap, &pe32) { if err := windows.Process32First(snap, &pe32); err != nil {
return out, windows.GetLastError() return out, err
} }
for { for {
if pe32.Th32ParentProcessID == uint32(p.Pid) { if pe32.ParentProcessID == uint32(p.Pid) {
p, err := NewProcess(int32(pe32.Th32ProcessID)) p, err := NewProcess(int32(pe32.ProcessID))
if err == nil { if err == nil {
out = append(out, p) out = append(out, p)
} }
} }
if !w32.Process32Next(snap, &pe32) { if err = windows.Process32Next(snap, &pe32); err != nil {
break break
} }
} }
@ -692,16 +691,13 @@ func (p *Process) Terminate() error {
} }
func (p *Process) TerminateWithContext(ctx context.Context) error { func (p *Process) TerminateWithContext(ctx context.Context) error {
// PROCESS_TERMINATE = 0x0001 proc, err := windows.OpenProcess(windows.PROCESS_TERMINATE, false, uint32(p.Pid))
proc := w32.OpenProcess(0x0001, false, uint32(p.Pid)) if err != nil {
ret := w32.TerminateProcess(proc, 0) return err
w32.CloseHandle(proc)
if ret == false {
return windows.GetLastError()
} else {
return nil
} }
err = windows.TerminateProcess(proc, 0)
windows.CloseHandle(proc)
return err
} }
func (p *Process) Kill() error { func (p *Process) Kill() error {
@ -714,22 +710,22 @@ func (p *Process) KillWithContext(ctx context.Context) error {
} }
func getFromSnapProcess(pid int32) (int32, int32, string, error) { func getFromSnapProcess(pid int32) (int32, int32, string, error) {
snap := w32.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, uint32(pid)) snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, uint32(pid))
if snap == 0 { if err != nil {
return 0, 0, "", windows.GetLastError() return 0, 0, "", err
} }
defer w32.CloseHandle(snap) defer windows.CloseHandle(snap)
var pe32 w32.PROCESSENTRY32 var pe32 windows.ProcessEntry32
pe32.DwSize = uint32(unsafe.Sizeof(pe32)) pe32.Size = uint32(unsafe.Sizeof(pe32))
if !w32.Process32First(snap, &pe32) { if err = windows.Process32First(snap, &pe32); err != nil {
return 0, 0, "", windows.GetLastError() return 0, 0, "", err
} }
for { for {
if pe32.Th32ProcessID == uint32(pid) { if pe32.ProcessID == uint32(pid) {
szexe := windows.UTF16ToString(pe32.SzExeFile[:]) szexe := windows.UTF16ToString(pe32.ExeFile[:])
return int32(pe32.Th32ParentProcessID), int32(pe32.CntThreads), szexe, nil return int32(pe32.ParentProcessID), int32(pe32.Threads), szexe, nil
} }
if !w32.Process32Next(snap, &pe32) { if err = windows.Process32Next(snap, &pe32); err != nil {
break break
} }
} }

Loading…
Cancel
Save