Merge pull request #1765 from shirou/feat/process_cwd_freebsd

[process][freebsd]: add CWD
pull/1766/head
shirou 3 months ago committed by GitHub
commit 90efec0764
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -6,6 +6,7 @@ package process
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/binary"
"errors" "errors"
"path/filepath" "path/filepath"
"sort" "sort"
@ -14,9 +15,9 @@ import (
"golang.org/x/sys/unix" "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" "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) { func pidsWithContext(ctx context.Context) ([]int32, error) {
@ -66,7 +67,24 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
} }
func (p *Process) CwdWithContext(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) { func (p *Process) ExeWithContext(ctx context.Context) (string, error) {

@ -11,6 +11,7 @@ const (
KernProcProc = 8 KernProcProc = 8
KernProcPathname = 12 KernProcPathname = 12
KernProcArgs = 7 KernProcArgs = 7
KernProcCwd = 42
) )
const ( const (
@ -24,6 +25,7 @@ const (
const ( const (
sizeOfKinfoVmentry = 0x488 sizeOfKinfoVmentry = 0x488
sizeOfKinfoProc = 0x300 sizeOfKinfoProc = 0x300
sizeOfKinfoFile = 0x570 // TODO: should be changed by running on the target machine
) )
const ( const (
@ -191,3 +193,26 @@ type KinfoVmentry struct {
X_kve_ispare [12]int32 X_kve_ispare [12]int32
Path [1024]int8 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
}

@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
// Created by cgo -godefs - DO NOT EDIT // Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs types_freebsd.go // cgo -godefs types_freebsd.go
package process package process
@ -11,6 +11,7 @@ const (
KernProcProc = 8 KernProcProc = 8
KernProcPathname = 12 KernProcPathname = 12
KernProcArgs = 7 KernProcArgs = 7
KernProcCwd = 42
) )
const ( const (
@ -24,6 +25,7 @@ const (
const ( const (
sizeOfKinfoVmentry = 0x488 sizeOfKinfoVmentry = 0x488
sizeOfKinfoProc = 0x440 sizeOfKinfoProc = 0x440
sizeOfKinfoFile = 0x570
) )
const ( const (
@ -78,88 +80,94 @@ type Rlimit struct {
} }
type KinfoProc struct { type KinfoProc struct {
Structsize int32 Structsize int32
Layout int32 Layout int32
Args int64 /* pargs */ Args int64 /* pargs */
Paddr int64 /* proc */ Paddr int64 /* proc */
Addr int64 /* user */ Addr int64 /* user */
Tracep int64 /* vnode */ Tracep int64 /* vnode */
Textvp int64 /* vnode */ Textvp int64 /* vnode */
Fd int64 /* filedesc */ Fd int64 /* filedesc */
Vmspace int64 /* vmspace */ Vmspace int64 /* vmspace */
Wchan int64 Wchan int64
Pid int32 Pid int32
Ppid int32 Ppid int32
Pgid int32 Pgid int32
Tpgid int32 Tpgid int32
Sid int32 Sid int32
Tsid int32 Tsid int32
Jobc int16 Jobc int16
Spare_short1 int16 Spare_short1 int16
Tdev uint32 Tdev_freebsd11 uint32
Siglist [16]byte /* sigset */ Siglist [16]byte /* sigset */
Sigmask [16]byte /* sigset */ Sigmask [16]byte /* sigset */
Sigignore [16]byte /* sigset */ Sigignore [16]byte /* sigset */
Sigcatch [16]byte /* sigset */ Sigcatch [16]byte /* sigset */
Uid uint32 Uid uint32
Ruid uint32 Ruid uint32
Svuid uint32 Svuid uint32
Rgid uint32 Rgid uint32
Svgid uint32 Svgid uint32
Ngroups int16 Ngroups int16
Spare_short2 int16 Spare_short2 int16
Groups [16]uint32 Groups [16]uint32
Size uint64 Size uint64
Rssize int64 Rssize int64
Swrss int64 Swrss int64
Tsize int64 Tsize int64
Dsize int64 Dsize int64
Ssize int64 Ssize int64
Xstat uint16 Xstat uint16
Acflag uint16 Acflag uint16
Pctcpu uint32 Pctcpu uint32
Estcpu uint32 Estcpu uint32
Slptime uint32 Slptime uint32
Swtime uint32 Swtime uint32
Cow uint32 Cow uint32
Runtime uint64 Runtime uint64
Start Timeval Start Timeval
Childtime Timeval Childtime Timeval
Flag int64 Flag int64
Kiflag int64 Kiflag int64
Traceflag int32 Traceflag int32
Stat int8 Stat int8
Nice int8 Nice int8
Lock int8 Lock int8
Rqindex int8 Rqindex int8
Oncpu uint8 Oncpu_old uint8
Lastcpu uint8 Lastcpu_old uint8
Tdname [17]int8 Tdname [17]int8
Wmesg [9]int8 Wmesg [9]int8
Login [18]int8 Login [18]int8
Lockname [9]int8 Lockname [9]int8
Comm [20]int8 Comm [20]int8
Emul [17]int8 Emul [17]int8
Loginclass [18]int8 Loginclass [18]int8
Sparestrings [50]int8 Moretdname [4]int8
Spareints [7]int32 Sparestrings [46]int8
Flag2 int32 Spareints [2]int32
Fibnum int32 Tdev uint64
Cr_flags uint32 Oncpu int32
Jid int32 Lastcpu int32
Numthreads int32 Tracer int32
Tid int32 Flag2 int32
Pri Priority Fibnum int32
Rusage Rusage Cr_flags uint32
Rusage_ch Rusage Jid int32
Pcb int64 /* pcb */ Numthreads int32
Kstack int64 Tid int32
Udata int64 Pri Priority
Tdaddr int64 /* thread */ Rusage Rusage
Spareptrs [6]int64 Rusage_ch Rusage
Sparelongs [12]int64 Pcb int64 /* pcb */
Sflag int64 Kstack int64
Tdflags int64 Udata int64
Tdaddr int64 /* thread */
Pd int64 /* pwddesc, not accurate */
Spareptrs [5]int64
Sparelongs [12]int64
Sflag int64
Tdflags int64
} }
type Priority struct { type Priority struct {
@ -170,24 +178,47 @@ type Priority struct {
} }
type KinfoVmentry struct { type KinfoVmentry struct {
Structsize int32 Structsize int32
Type int32 Type int32
Start uint64 Start uint64
End uint64 End uint64
Offset uint64 Offset uint64
Vn_fileid uint64 Vn_fileid uint64
Vn_fsid uint32 Vn_fsid_freebsd11 uint32
Flags int32 Flags int32
Resident int32 Resident int32
Private_resident int32 Private_resident int32
Protection int32 Protection int32
Ref_count int32 Ref_count int32
Shadow_count int32 Shadow_count int32
Vn_type int32 Vn_type int32
Vn_size uint64 Vn_size uint64
Vn_rdev uint32 Vn_rdev_freebsd11 uint32
Vn_mode uint16 Vn_mode uint16
Status uint16 Status uint16
X_kve_ispare [12]int32 Type_spec [8]byte
Path [1024]int8 Vn_rdev uint64
X_kve_ispare [8]int32
Path [1024]int8
}
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
}
type capRights struct {
Rights [2]uint64
} }

@ -11,6 +11,7 @@ const (
KernProcProc = 8 KernProcProc = 8
KernProcPathname = 12 KernProcPathname = 12
KernProcArgs = 7 KernProcArgs = 7
KernProcCwd = 42
) )
const ( const (
@ -24,6 +25,7 @@ const (
const ( const (
sizeOfKinfoVmentry = 0x488 sizeOfKinfoVmentry = 0x488
sizeOfKinfoProc = 0x440 sizeOfKinfoProc = 0x440
sizeOfKinfoFile = 0x570 // TODO: should be changed by running on the target machine
) )
const ( const (
@ -191,3 +193,26 @@ type KinfoVmentry struct {
X_kve_ispare [12]int32 X_kve_ispare [12]int32
Path [1024]int8 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
}

@ -13,6 +13,7 @@ const (
KernProcProc = 8 KernProcProc = 8
KernProcPathname = 12 KernProcPathname = 12
KernProcArgs = 7 KernProcArgs = 7
KernProcCwd = 42
) )
const ( const (
@ -26,6 +27,7 @@ const (
const ( const (
sizeOfKinfoVmentry = 0x488 sizeOfKinfoVmentry = 0x488
sizeOfKinfoProc = 0x440 sizeOfKinfoProc = 0x440
sizeOfKinfoFile = 0x570
) )
const ( const (
@ -201,3 +203,24 @@ type KinfoVmentry struct {
X_kve_ispare [8]int32 X_kve_ispare [8]int32
Path [1024]uint8 Path [1024]uint8
} }
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
}
type capRights struct {
Rights [2]uint64
}

@ -42,6 +42,7 @@ const (
KernProcProc = 8 // only return procs KernProcProc = 8 // only return procs
KernProcPathname = 12 // path to executable KernProcPathname = 12 // path to executable
KernProcArgs = 7 // get/set arguments/proctitle KernProcArgs = 7 // get/set arguments/proctitle
KernProcCwd = 42 /* process current working directory */
) )
const ( const (
@ -55,6 +56,7 @@ const (
const ( const (
sizeOfKinfoVmentry = C.sizeof_struct_kinfo_vmentry sizeOfKinfoVmentry = C.sizeof_struct_kinfo_vmentry
sizeOfKinfoProc = C.sizeof_struct_kinfo_proc sizeOfKinfoProc = C.sizeof_struct_kinfo_proc
sizeOfKinfoFile = C.sizeof_struct_kinfo_file
) )
// from sys/proc.h // from sys/proc.h
@ -94,3 +96,7 @@ type KinfoProc C.struct_kinfo_proc
type Priority C.struct_priority type Priority C.struct_priority
type KinfoVmentry C.struct_kinfo_vmentry type KinfoVmentry C.struct_kinfo_vmentry
type kinfoFile C.struct_kinfo_file
type capRights C.struct_cap_rights

Loading…
Cancel
Save