Skip to content

Commit b9b3dbe

Browse files
committed
Avoid ps command and use KProc on MacOS
1 parent 1ccce7b commit b9b3dbe

File tree

3 files changed

+19
-67
lines changed

3 files changed

+19
-67
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ require (
99
github.com/stretchr/testify v1.7.0
1010
github.com/tklauser/go-sysconf v0.3.9
1111
github.com/yusufpapurcu/wmi v1.2.2
12-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c
12+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320
1313
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
2626
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2727
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2828
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c h1:taxlMj0D/1sOAuv/CbSD+MMDof2vbyPTqz5FNYKpXt8=
30-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
30+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3131
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
3232
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3333
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

process/process_darwin.go

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"path/filepath"
1111
"strconv"
1212
"strings"
13-
"time"
1413

1514
"github.com/shirou/gopsutil/v3/cpu"
1615
"github.com/shirou/gopsutil/v3/internal/common"
@@ -46,42 +45,34 @@ type _Ctype_struct___0 struct {
4645
func pidsWithContext(ctx context.Context) ([]int32, error) {
4746
var ret []int32
4847

49-
pids, err := callPsWithContext(ctx, "pid", 0, false, false)
48+
kprocs, err := unix.SysctlKinfoProcSlice("kern.proc.all")
5049
if err != nil {
5150
return ret, err
5251
}
5352

54-
for _, pid := range pids {
55-
v, err := strconv.Atoi(pid[0])
56-
if err != nil {
57-
return ret, err
58-
}
59-
ret = append(ret, int32(v))
53+
for _, proc := range kprocs {
54+
ret = append(ret, int32(proc.Proc.P_pid))
6055
}
6156

6257
return ret, nil
6358
}
6459

6560
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
66-
r, err := callPsWithContext(ctx, "ppid", p.Pid, false, false)
67-
if err != nil {
68-
return 0, err
69-
}
70-
71-
v, err := strconv.Atoi(r[0][0])
61+
k, err := p.getKProc()
7262
if err != nil {
7363
return 0, err
7464
}
7565

76-
return int32(v), err
66+
return k.Eproc.Ppid, nil
7767
}
7868

7969
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
8070
k, err := p.getKProc()
8171
if err != nil {
8272
return "", err
8373
}
84-
name := common.IntToString(k.Proc.P_comm[:])
74+
75+
name := common.ByteToString(k.Proc.P_comm[:])
8576

8677
if len(name) >= 15 {
8778
cmdName, err := p.cmdNameWithContext(ctx)
@@ -111,51 +102,21 @@ func (p *Process) cmdNameWithContext(ctx context.Context) ([]string, error) {
111102
}
112103

113104
func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
114-
r, err := callPsWithContext(ctx, "etime", p.Pid, false, false)
105+
k, err := p.getKProc()
115106
if err != nil {
116107
return 0, err
117108
}
118109

119-
elapsedSegments := strings.Split(strings.Replace(r[0][0], "-", ":", 1), ":")
120-
var elapsedDurations []time.Duration
121-
for i := len(elapsedSegments) - 1; i >= 0; i-- {
122-
p, err := strconv.ParseInt(elapsedSegments[i], 10, 0)
123-
if err != nil {
124-
return 0, err
125-
}
126-
elapsedDurations = append(elapsedDurations, time.Duration(p))
127-
}
128-
129-
elapsed := time.Duration(elapsedDurations[0]) * time.Second
130-
if len(elapsedDurations) > 1 {
131-
elapsed += time.Duration(elapsedDurations[1]) * time.Minute
132-
}
133-
if len(elapsedDurations) > 2 {
134-
elapsed += time.Duration(elapsedDurations[2]) * time.Hour
135-
}
136-
if len(elapsedDurations) > 3 {
137-
elapsed += time.Duration(elapsedDurations[3]) * time.Hour * 24
138-
}
139-
140-
start := time.Now().Add(-elapsed)
141-
return start.Unix() * 1000, nil
110+
return k.Proc.P_starttime.Sec*1000 + int64(k.Proc.P_starttime.Usec)/1000, nil
142111
}
143112

144113
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
145-
out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR")
114+
ppid, err := p.PpidWithContext(ctx)
146115
if err != nil {
147116
return nil, err
148117
}
149-
for _, line := range out {
150-
if len(line) >= 1 && line[0] == 'R' {
151-
v, err := strconv.Atoi(line[1:])
152-
if err != nil {
153-
return nil, err
154-
}
155-
return NewProcessWithContext(ctx, int32(v))
156-
}
157-
}
158-
return nil, fmt.Errorf("could not find parent line")
118+
119+
return NewProcessWithContext(ctx, ppid)
159120
}
160121

161122
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
@@ -188,7 +149,7 @@ func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
188149
}
189150

190151
// See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html
191-
userEffectiveUID := int32(k.Eproc.Ucred.UID)
152+
userEffectiveUID := int32(k.Eproc.Ucred.Uid)
192153

193154
return []int32{userEffectiveUID}, nil
194155
}
@@ -200,7 +161,7 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
200161
}
201162

202163
gids := make([]int32, 0, 3)
203-
gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Ucred.Ngroups), int32(k.Eproc.Pcred.P_svgid))
164+
gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Pcred.P_svgid))
204165

205166
return gids, nil
206167
}
@@ -399,17 +360,8 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
399360

400361
// Returns a proc as defined here:
401362
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html
402-
func (p *Process) getKProc() (*KinfoProc, error) {
403-
buf, err := unix.SysctlRaw("kern.proc.pid", int(p.Pid))
404-
if err != nil {
405-
return nil, err
406-
}
407-
k, err := parseKinfoProc(buf)
408-
if err != nil {
409-
return nil, err
410-
}
411-
412-
return &k, nil
363+
func (p *Process) getKProc() (*unix.KinfoProc, error) {
364+
return unix.SysctlKinfoProc("kern.proc.pid", int(p.Pid))
413365
}
414366

415367
// call ps command.

0 commit comments

Comments
 (0)