2021-12-23 05:54:41 +08:00
|
|
|
//go:build darwin && !cgo
|
|
|
|
// +build darwin,!cgo
|
2019-09-06 00:35:16 +08:00
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-11-06 17:53:56 +08:00
|
|
|
|
2022-01-14 00:55:01 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
2021-11-06 17:53:56 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2019-09-06 00:35:16 +08:00
|
|
|
)
|
|
|
|
|
2021-11-06 17:53:56 +08:00
|
|
|
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
|
|
|
|
return "", common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:35:16 +08:00
|
|
|
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
|
2022-03-05 00:18:03 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "lsof", "-p", strconv.Itoa(int(p.Pid)), "-Fpfn")
|
2019-09-06 00:35:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("bad call to lsof: %s", err)
|
|
|
|
}
|
|
|
|
txtFound := 0
|
|
|
|
lines := strings.Split(string(out), "\n")
|
2020-05-21 05:50:21 +08:00
|
|
|
for i := 1; i < len(lines); i++ {
|
2019-09-06 00:35:16 +08:00
|
|
|
if lines[i] == "ftxt" {
|
|
|
|
txtFound++
|
|
|
|
if txtFound == 2 {
|
|
|
|
return lines[i-1][1:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("missing txt data returned by lsof")
|
|
|
|
}
|
2021-11-06 17:53:56 +08:00
|
|
|
|
|
|
|
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "command", p.Pid, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Join(r[0], " "), err
|
|
|
|
}
|
|
|
|
|
2022-01-14 00:55:01 +08:00
|
|
|
func (p *Process) cmdNameWithContext(ctx context.Context) (string, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "command", p.Pid, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(r) > 0 && len(r[0]) > 0 {
|
|
|
|
return r[0][0], err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-11-06 17:53:56 +08:00
|
|
|
// CmdlineSliceWithContext returns the command line arguments of the process as a slice with each
|
|
|
|
// element being an argument. Because of current deficiencies in the way that the command
|
|
|
|
// line arguments are found, single arguments that have spaces in the will actually be
|
|
|
|
// reported as two separate items. In order to do something better CGO would be needed
|
|
|
|
// to use the native darwin functions.
|
|
|
|
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "command", p.Pid, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r[0], err
|
|
|
|
}
|
2022-01-14 00:55:01 +08:00
|
|
|
|
|
|
|
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "utime,stime", p.Pid, true, false)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int32(len(r)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "utime,stime", p.Pid, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
utime, err := convertCPUTimes(r[0][0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stime, err := convertCPUTimes(r[0][1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &cpu.TimesStat{
|
|
|
|
CPU: "cpu",
|
|
|
|
User: utime,
|
|
|
|
System: stime,
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "rss,vsize,pagein", p.Pid, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rss, err := strconv.Atoi(r[0][0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vms, err := strconv.Atoi(r[0][1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pagein, err := strconv.Atoi(r[0][2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &MemoryInfoStat{
|
|
|
|
RSS: uint64(rss) * 1024,
|
|
|
|
VMS: uint64(vms) * 1024,
|
|
|
|
Swap: uint64(pagein),
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|