1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00

Merge pull request #575 from Lomanic/issue573

Fix #573
This commit is contained in:
shirou 2018-09-15 11:28:52 +09:00 committed by GitHub
commit 50e1c3da5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 26 deletions

View File

@ -527,34 +527,22 @@ func Processes() ([]*Process, error) {
}
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
results := []*Process{}
out := []*Process{}
mib := []int32{CTLKern, KernProc, KernProcAll, 0}
buf, length, err := common.CallSyscall(mib)
pids, err := PidsWithContext(ctx)
if err != nil {
return results, err
return out, err
}
// get kinfo_proc size
k := KinfoProc{}
procinfoLen := int(unsafe.Sizeof(k))
count := int(length / uint64(procinfoLen))
// parse buf to procs
for i := 0; i < count; i++ {
b := buf[i*procinfoLen : i*procinfoLen+procinfoLen]
k, err := parseKinfoProc(b)
for _, pid := range pids {
p, err := NewProcess(pid)
if err != nil {
continue
}
p, err := NewProcess(int32(k.Proc.P_pid))
if err != nil {
continue
}
results = append(results, p)
out = append(out, p)
}
return results, nil
return out, nil
}
func parseKinfoProc(buf []byte) (KinfoProc, error) {

View File

@ -1220,7 +1220,7 @@ func Processes() ([]*Process, error) {
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
out := []*Process{}
pids, err := Pids()
pids, err := PidsWithContext(ctx)
if err != nil {
return out, err
}

View File

@ -603,21 +603,22 @@ func Processes() ([]*Process, error) {
}
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
pids, err := Pids()
out := []*Process{}
pids, err := PidsWithContext(ctx)
if err != nil {
return []*Process{}, fmt.Errorf("could not get Processes %s", err)
return out, fmt.Errorf("could not get Processes %s", err)
}
results := []*Process{}
for _, pid := range pids {
p, err := NewProcess(int32(pid))
p, err := NewProcess(pid)
if err != nil {
continue
}
results = append(results, p)
out = append(out, p)
}
return results, nil
return out, nil
}
func getProcInfo(pid int32) (*SystemProcessInformation, error) {