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

63 lines
1.4 KiB
Go
Raw Normal View History

2021-12-22 21:54:41 +00:00
//go:build linux || freebsd || darwin || openbsd
// +build linux freebsd darwin openbsd
package common
import (
2018-03-31 21:35:53 +09:00
"context"
"errors"
"os/exec"
"strconv"
"strings"
)
2018-03-31 21:35:53 +09:00
func CallLsofWithContext(ctx context.Context, invoke Invoker, pid int32, args ...string) ([]string, error) {
var cmd []string
if pid == 0 { // will get from all processes.
cmd = []string{"-a", "-n", "-P"}
} else {
cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))}
}
cmd = append(cmd, args...)
out, err := invoke.CommandWithContext(ctx, "lsof", cmd...)
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
return []string{}, err
}
// if no pid found, lsof returns code 1.
if err.Error() == "exit status 1" && len(out) == 0 {
2015-10-11 22:15:47 +09:00
return []string{}, nil
}
}
lines := strings.Split(string(out), "\n")
var ret []string
for _, l := range lines[1:] {
if len(l) == 0 {
continue
}
ret = append(ret, l)
}
return ret, nil
}
2018-03-31 21:35:53 +09:00
func CallPgrepWithContext(ctx context.Context, invoke Invoker, pid int32) ([]int32, error) {
out, err := invoke.CommandWithContext(ctx, "pgrep", "-P", strconv.Itoa(int(pid)))
if err != nil {
return []int32{}, err
}
lines := strings.Split(string(out), "\n")
ret := make([]int32, 0, len(lines))
for _, l := range lines {
if len(l) == 0 {
continue
}
2021-12-23 00:31:04 +01:00
i, err := strconv.ParseInt(l, 10, 32)
if err != nil {
continue
}
ret = append(ret, int32(i))
}
return ret, nil
}