2016-02-12 18:02:16 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-09 15:19:04 +00:00
|
|
|
"strings"
|
2016-02-12 18:02:16 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/StackExchange/wmi"
|
|
|
|
cpu "github.com/shirou/gopsutil/cpu"
|
2016-05-09 15:19:04 +00:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2016-02-12 18:02:16 +00:00
|
|
|
net "github.com/shirou/gopsutil/net"
|
2017-07-28 16:46:44 +00:00
|
|
|
"github.com/shirou/w32"
|
|
|
|
"golang.org/x/sys/windows"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoMoreFiles = 0x12
|
|
|
|
MaxPathLength = 260
|
|
|
|
)
|
|
|
|
|
2016-06-17 14:31:00 +00:00
|
|
|
var (
|
2017-07-28 16:46:44 +00:00
|
|
|
modpsapi = windows.NewLazyDLL("psapi.dll")
|
2016-06-17 14:31:00 +00:00
|
|
|
procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo")
|
|
|
|
)
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
type SystemProcessInformation struct {
|
|
|
|
NextEntryOffset uint64
|
|
|
|
NumberOfThreads uint64
|
|
|
|
Reserved1 [48]byte
|
|
|
|
Reserved2 [3]byte
|
|
|
|
UniqueProcessID uintptr
|
|
|
|
Reserved3 uintptr
|
|
|
|
HandleCount uint64
|
|
|
|
Reserved4 [4]byte
|
|
|
|
Reserved5 [11]byte
|
|
|
|
PeakPagefileUsage uint64
|
|
|
|
PrivatePageCount uint64
|
|
|
|
Reserved6 [6]uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Memory_info_ex is different between OSes
|
|
|
|
type MemoryInfoExStat struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoryMapsStat struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type Win32_Process struct {
|
2017-07-28 16:46:44 +00:00
|
|
|
Name string
|
|
|
|
ExecutablePath *string
|
|
|
|
CommandLine *string
|
|
|
|
Priority uint32
|
|
|
|
CreationDate *time.Time
|
|
|
|
ProcessID uint32
|
|
|
|
ThreadCount uint32
|
|
|
|
Status *string
|
|
|
|
ReadOperationCount uint64
|
|
|
|
ReadTransferCount uint64
|
|
|
|
WriteOperationCount uint64
|
|
|
|
WriteTransferCount uint64
|
|
|
|
CSCreationClassName string
|
|
|
|
CSName string
|
|
|
|
Caption *string
|
|
|
|
CreationClassName string
|
|
|
|
Description *string
|
|
|
|
ExecutionState *uint16
|
|
|
|
HandleCount uint32
|
|
|
|
KernelModeTime uint64
|
|
|
|
MaximumWorkingSetSize *uint32
|
|
|
|
MinimumWorkingSetSize *uint32
|
|
|
|
OSCreationClassName string
|
|
|
|
OSName string
|
|
|
|
OtherOperationCount uint64
|
|
|
|
OtherTransferCount uint64
|
|
|
|
PageFaults uint32
|
|
|
|
PageFileUsage uint32
|
|
|
|
ParentProcessID uint32
|
|
|
|
PeakPageFileUsage uint32
|
|
|
|
PeakVirtualSize uint64
|
|
|
|
PeakWorkingSetSize uint32
|
|
|
|
PrivatePageCount uint64
|
|
|
|
TerminationDate *time.Time
|
|
|
|
UserModeTime uint64
|
|
|
|
WorkingSetSize uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
wmi.DefaultClient.AllowMissingFields = true
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Pids() ([]int32, error) {
|
|
|
|
var ret []int32
|
|
|
|
|
|
|
|
procs, err := processes()
|
|
|
|
if err != nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, proc := range procs {
|
|
|
|
ret = append(ret, proc.Pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Ppid() (int32, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
return int32(dst[0].ParentProcessID), nil
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetWin32Proc(pid int32) ([]Win32_Process, error) {
|
|
|
|
var dst []Win32_Process
|
|
|
|
query := fmt.Sprintf("WHERE ProcessId = %d", pid)
|
|
|
|
q := wmi.CreateQuery(&dst, query)
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
if err := wmi.Query(q, &dst); err != nil {
|
2016-02-12 18:02:16 +00:00
|
|
|
return []Win32_Process{}, fmt.Errorf("could not get win32Proc: %s", err)
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
if len(dst) == 0 {
|
2016-02-12 18:02:16 +00:00
|
|
|
return []Win32_Process{}, fmt.Errorf("could not get win32Proc: empty")
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Name() (string, error) {
|
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get Name: %s", err)
|
|
|
|
}
|
|
|
|
return dst[0].Name, nil
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Exe() (string, error) {
|
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get ExecutablePath: %s", err)
|
|
|
|
}
|
|
|
|
return *dst[0].ExecutablePath, nil
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Cmdline() (string, error) {
|
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get CommandLine: %s", err)
|
|
|
|
}
|
|
|
|
return *dst[0].CommandLine, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
// CmdlineSlice returns the command line arguments of the process as a slice with each
|
|
|
|
// element being an argument. This merely returns the CommandLine informations passed
|
|
|
|
// to the process split on the 0x20 ASCII character.
|
|
|
|
func (p *Process) CmdlineSlice() ([]string, error) {
|
|
|
|
cmdline, err := p.Cmdline()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return strings.Split(cmdline, " "), nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) CreateTime() (int64, error) {
|
2016-06-17 14:31:00 +00:00
|
|
|
ru, err := getRusage(p.Pid)
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not get CreationDate: %s", err)
|
|
|
|
}
|
2016-06-17 14:31:00 +00:00
|
|
|
|
|
|
|
return ru.CreationTime.Nanoseconds() / 1000000, nil
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Cwd() (string, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) Parent() (*Process, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get ParentProcessID: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewProcess(int32(dst[0].ParentProcessID))
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) Status() (string, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) Username() (string, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
pid := p.Pid
|
|
|
|
// 0x1000 is PROCESS_QUERY_LIMITED_INFORMATION
|
|
|
|
c, err := syscall.OpenProcess(0x1000, false, uint32(pid))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer syscall.CloseHandle(c)
|
|
|
|
|
|
|
|
var token syscall.Token
|
|
|
|
err = syscall.OpenProcessToken(c, syscall.TOKEN_QUERY, &token)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer token.Close()
|
|
|
|
tokenUser, err := token.GetTokenUser()
|
|
|
|
|
|
|
|
user, domain, _, err := tokenUser.User.Sid.LookupAccount("")
|
|
|
|
return domain + "\\" + user, err
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Uids() ([]int32, error) {
|
|
|
|
var uids []int32
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
return uids, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) Gids() ([]int32, error) {
|
|
|
|
var gids []int32
|
2016-05-09 15:19:04 +00:00
|
|
|
return gids, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) Terminal() (string, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nice returnes priority in Windows
|
|
|
|
func (p *Process) Nice() (int32, error) {
|
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not get Priority: %s", err)
|
|
|
|
}
|
|
|
|
return int32(dst[0].Priority), nil
|
|
|
|
}
|
|
|
|
func (p *Process) IOnice() (int32, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return 0, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) Rlimit() ([]RlimitStat, error) {
|
|
|
|
var rlimit []RlimitStat
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
return rlimit, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2016-06-17 14:31:00 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) IOCounters() (*IOCountersStat, error) {
|
2016-06-17 14:31:00 +00:00
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
|
|
|
if err != nil || len(dst) == 0 {
|
|
|
|
return nil, fmt.Errorf("could not get Win32Proc: %s", err)
|
|
|
|
}
|
|
|
|
ret := &IOCountersStat{
|
|
|
|
ReadCount: uint64(dst[0].ReadOperationCount),
|
|
|
|
ReadBytes: uint64(dst[0].ReadTransferCount),
|
|
|
|
WriteCount: uint64(dst[0].WriteOperationCount),
|
|
|
|
WriteBytes: uint64(dst[0].WriteTransferCount),
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) NumFDs() (int32, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return 0, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) NumThreads() (int32, error) {
|
|
|
|
dst, err := GetWin32Proc(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not get ThreadCount: %s", err)
|
|
|
|
}
|
|
|
|
return int32(dst[0].ThreadCount), nil
|
|
|
|
}
|
|
|
|
func (p *Process) Threads() (map[string]string, error) {
|
|
|
|
ret := make(map[string]string, 0)
|
2016-05-09 15:19:04 +00:00
|
|
|
return ret, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
func (p *Process) Times() (*cpu.TimesStat, error) {
|
|
|
|
return nil, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) CPUAffinity() ([]int32, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
|
2016-06-17 14:31:00 +00:00
|
|
|
mem, err := getMemoryInfo(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &MemoryInfoStat{
|
2016-06-22 23:38:45 +00:00
|
|
|
RSS: uint64(mem.WorkingSetSize),
|
|
|
|
VMS: uint64(mem.PagefileUsage),
|
2016-06-17 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Children() ([]*Process, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
procs, err := processes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out := []*Process{}
|
|
|
|
|
|
|
|
for _, proc := range procs {
|
|
|
|
parent, err := proc.Parent()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent.Pid == p.Pid {
|
|
|
|
out = append(out, proc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Connections() ([]net.ConnectionStat, error) {
|
|
|
|
return nil, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
|
|
|
|
return nil, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) IsRunning() (bool, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return true, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
var ret []MemoryMapsStat
|
|
|
|
return &ret, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcess(pid int32) (*Process, error) {
|
|
|
|
p := &Process{Pid: pid}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
func (p *Process) SendSignal(sig windows.Signal) error {
|
2016-05-09 15:19:04 +00:00
|
|
|
return common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Suspend() error {
|
2016-05-09 15:19:04 +00:00
|
|
|
return common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
func (p *Process) Resume() error {
|
2016-05-09 15:19:04 +00:00
|
|
|
return common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Terminate() error {
|
2017-02-02 06:41:29 +00:00
|
|
|
// PROCESS_TERMINATE = 0x0001
|
|
|
|
proc := w32.OpenProcess(0x0001, false, uint32(p.Pid))
|
|
|
|
ret := w32.TerminateProcess(proc, 0)
|
|
|
|
w32.CloseHandle(proc)
|
|
|
|
|
|
|
|
if ret == false {
|
2017-07-28 16:46:44 +00:00
|
|
|
return windows.GetLastError()
|
2017-02-02 06:41:29 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Kill() error {
|
2016-05-09 15:19:04 +00:00
|
|
|
return common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) getFromSnapProcess(pid int32) (int32, int32, string, error) {
|
|
|
|
snap := w32.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, uint32(pid))
|
|
|
|
if snap == 0 {
|
2017-07-28 16:46:44 +00:00
|
|
|
return 0, 0, "", windows.GetLastError()
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
defer w32.CloseHandle(snap)
|
|
|
|
var pe32 w32.PROCESSENTRY32
|
|
|
|
pe32.DwSize = uint32(unsafe.Sizeof(pe32))
|
|
|
|
if w32.Process32First(snap, &pe32) == false {
|
2017-07-28 16:46:44 +00:00
|
|
|
return 0, 0, "", windows.GetLastError()
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pe32.Th32ProcessID == uint32(pid) {
|
2017-07-28 16:46:44 +00:00
|
|
|
szexe := windows.UTF16ToString(pe32.SzExeFile[:])
|
2016-02-12 18:02:16 +00:00
|
|
|
return int32(pe32.Th32ParentProcessID), int32(pe32.CntThreads), szexe, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for w32.Process32Next(snap, &pe32) {
|
|
|
|
if pe32.Th32ProcessID == uint32(pid) {
|
2017-07-28 16:46:44 +00:00
|
|
|
szexe := windows.UTF16ToString(pe32.SzExeFile[:])
|
2016-02-12 18:02:16 +00:00
|
|
|
return int32(pe32.Th32ParentProcessID), int32(pe32.CntThreads), szexe, nil
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
return 0, 0, "", fmt.Errorf("Couldn't find pid: %d", pid)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get processes
|
|
|
|
func processes() ([]*Process, error) {
|
|
|
|
var dst []Win32_Process
|
|
|
|
q := wmi.CreateQuery(&dst, "")
|
|
|
|
err := wmi.Query(q, &dst)
|
|
|
|
if err != nil {
|
|
|
|
return []*Process{}, err
|
|
|
|
}
|
|
|
|
if len(dst) == 0 {
|
|
|
|
return []*Process{}, fmt.Errorf("could not get Process")
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
results := []*Process{}
|
2016-02-12 18:02:16 +00:00
|
|
|
for _, proc := range dst {
|
2016-05-09 15:19:04 +00:00
|
|
|
p, err := NewProcess(int32(proc.ProcessID))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
results = append(results, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProcInfo(pid int32) (*SystemProcessInformation, error) {
|
|
|
|
initialBufferSize := uint64(0x4000)
|
|
|
|
bufferSize := initialBufferSize
|
|
|
|
buffer := make([]byte, bufferSize)
|
|
|
|
|
|
|
|
var sysProcInfo SystemProcessInformation
|
|
|
|
ret, _, _ := common.ProcNtQuerySystemInformation.Call(
|
|
|
|
uintptr(unsafe.Pointer(&sysProcInfo)),
|
|
|
|
uintptr(unsafe.Pointer(&buffer[0])),
|
|
|
|
uintptr(unsafe.Pointer(&bufferSize)),
|
|
|
|
uintptr(unsafe.Pointer(&bufferSize)))
|
|
|
|
if ret != 0 {
|
2017-07-28 16:46:44 +00:00
|
|
|
return nil, windows.GetLastError()
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &sysProcInfo, nil
|
|
|
|
}
|
2016-06-17 14:31:00 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
func getRusage(pid int32) (*windows.Rusage, error) {
|
|
|
|
var CPU windows.Rusage
|
2016-06-17 14:31:00 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
c, err := windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION, false, uint32(pid))
|
2016-06-17 14:31:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
defer windows.CloseHandle(c)
|
2016-06-17 14:31:00 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
if err := windows.GetProcessTimes(c, &CPU.CreationTime, &CPU.ExitTime, &CPU.KernelTime, &CPU.UserTime); err != nil {
|
2016-06-17 14:31:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &CPU, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMemoryInfo(pid int32) (PROCESS_MEMORY_COUNTERS, error) {
|
|
|
|
var mem PROCESS_MEMORY_COUNTERS
|
2017-07-28 16:46:44 +00:00
|
|
|
// PROCESS_QUERY_LIMITED_INFORMATION is 0x1000
|
|
|
|
c, err := windows.OpenProcess(0x1000, false, uint32(pid))
|
2016-06-17 14:31:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return mem, err
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
defer windows.CloseHandle(c)
|
2016-06-17 14:31:00 +00:00
|
|
|
if err := getProcessMemoryInfo(c, &mem); err != nil {
|
|
|
|
return mem, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mem, err
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
func getProcessMemoryInfo(h windows.Handle, mem *PROCESS_MEMORY_COUNTERS) (err error) {
|
2016-06-17 14:31:00 +00:00
|
|
|
r1, _, e1 := syscall.Syscall(procGetProcessMemoryInfo.Addr(), 3, uintptr(h), uintptr(unsafe.Pointer(mem)), uintptr(unsafe.Sizeof(*mem)))
|
|
|
|
if r1 == 0 {
|
|
|
|
if e1 != 0 {
|
|
|
|
err = error(e1)
|
|
|
|
} else {
|
|
|
|
err = syscall.EINVAL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|