Skip to content

Commit

Permalink
Merge pull request #1765 from shirou/feat/process_cwd_freebsd
Browse files Browse the repository at this point in the history
[process][freebsd]: add CWD
  • Loading branch information
shirou authored Dec 30, 2024
2 parents ab66f2d + a284e30 commit 90efec0
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 106 deletions.
24 changes: 21 additions & 3 deletions process/process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package process
import (
"bytes"
"context"
"encoding/binary"
"errors"
"path/filepath"
"sort"
Expand All @@ -14,9 +15,9 @@ import (

"golang.org/x/sys/unix"

cpu "github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/internal/common"
net "github.com/shirou/gopsutil/v4/net"
"github.com/shirou/gopsutil/v4/net"
)

func pidsWithContext(ctx context.Context) ([]int32, error) {
Expand Down Expand Up @@ -66,7 +67,24 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
}

func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
mib := []int32{CTLKern, KernProc, KernProcCwd, p.Pid}
buf, length, err := common.CallSyscall(mib)
if err != nil {
return "", err
}

if length != sizeOfKinfoFile {
return "", errors.New("unexpected size of KinfoFile")
}

var k kinfoFile
br := bytes.NewReader(buf)
if err := common.Read(br, binary.LittleEndian, &k); err != nil {
return "", err
}
cwd := common.IntToString(k.Path[:])

return cwd, nil
}

func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
Expand Down
25 changes: 25 additions & 0 deletions process/process_freebsd_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
KernProcProc = 8
KernProcPathname = 12
KernProcArgs = 7
KernProcCwd = 42
)

const (
Expand All @@ -24,6 +25,7 @@ const (
const (
sizeOfKinfoVmentry = 0x488
sizeOfKinfoProc = 0x300
sizeOfKinfoFile = 0x570 // TODO: should be changed by running on the target machine
)

const (
Expand Down Expand Up @@ -191,3 +193,26 @@ type KinfoVmentry struct {
X_kve_ispare [12]int32
Path [1024]int8
}

// TODO: should be changed by running on the target machine
type kinfoFile struct {
Structsize int32
Type int32
Fd int32
Ref_count int32
Flags int32
Pad0 int32
Offset int64
Anon0 [304]byte
Status uint16
Pad1 uint16
X_kf_ispare0 int32
Cap_rights capRights
X_kf_cap_spare uint64
Path [1024]int8 // changed from uint8 by hand
}

// TODO: should be changed by running on the target machine
type capRights struct {
Rights [2]uint64
}
237 changes: 134 additions & 103 deletions process/process_freebsd_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions process/process_freebsd_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
KernProcProc = 8
KernProcPathname = 12
KernProcArgs = 7
KernProcCwd = 42
)

const (
Expand All @@ -24,6 +25,7 @@ const (
const (
sizeOfKinfoVmentry = 0x488
sizeOfKinfoProc = 0x440
sizeOfKinfoFile = 0x570 // TODO: should be changed by running on the target machine
)

const (
Expand Down Expand Up @@ -191,3 +193,26 @@ type KinfoVmentry struct {
X_kve_ispare [12]int32
Path [1024]int8
}

// TODO: should be changed by running on the target machine
type kinfoFile struct {
Structsize int32
Type int32
Fd int32
Ref_count int32
Flags int32
Pad0 int32
Offset int64
Anon0 [304]byte
Status uint16
Pad1 uint16
X_kf_ispare0 int32
Cap_rights capRights
X_kf_cap_spare uint64
Path [1024]int8 // changed from uint8 by hand
}

// TODO: should be changed by running on the target machine
type capRights struct {
Rights [2]uint64
}
Loading

0 comments on commit 90efec0

Please sign in to comment.