diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 76373736b..a67ac0ef0 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -6,6 +6,7 @@ package process import ( "bytes" "context" + "encoding/binary" "errors" "path/filepath" "sort" @@ -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) { @@ -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) { diff --git a/process/process_freebsd_386.go b/process/process_freebsd_386.go index 279ba9fbb..0193ba25b 100644 --- a/process/process_freebsd_386.go +++ b/process/process_freebsd_386.go @@ -11,6 +11,7 @@ const ( KernProcProc = 8 KernProcPathname = 12 KernProcArgs = 7 + KernProcCwd = 42 ) const ( @@ -24,6 +25,7 @@ const ( const ( sizeOfKinfoVmentry = 0x488 sizeOfKinfoProc = 0x300 + sizeOfKinfoFile = 0x570 // TODO: should be changed by running on the target machine ) const ( @@ -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 +} diff --git a/process/process_freebsd_amd64.go b/process/process_freebsd_amd64.go index f3b70ec1b..67970f64f 100644 --- a/process/process_freebsd_amd64.go +++ b/process/process_freebsd_amd64.go @@ -1,5 +1,5 @@ // 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 package process @@ -11,6 +11,7 @@ const ( KernProcProc = 8 KernProcPathname = 12 KernProcArgs = 7 + KernProcCwd = 42 ) const ( @@ -24,6 +25,7 @@ const ( const ( sizeOfKinfoVmentry = 0x488 sizeOfKinfoProc = 0x440 + sizeOfKinfoFile = 0x570 ) const ( @@ -78,88 +80,94 @@ type Rlimit struct { } type KinfoProc struct { - Structsize int32 - Layout int32 - Args int64 /* pargs */ - Paddr int64 /* proc */ - Addr int64 /* user */ - Tracep int64 /* vnode */ - Textvp int64 /* vnode */ - Fd int64 /* filedesc */ - Vmspace int64 /* vmspace */ - Wchan int64 - Pid int32 - Ppid int32 - Pgid int32 - Tpgid int32 - Sid int32 - Tsid int32 - Jobc int16 - Spare_short1 int16 - Tdev uint32 - Siglist [16]byte /* sigset */ - Sigmask [16]byte /* sigset */ - Sigignore [16]byte /* sigset */ - Sigcatch [16]byte /* sigset */ - Uid uint32 - Ruid uint32 - Svuid uint32 - Rgid uint32 - Svgid uint32 - Ngroups int16 - Spare_short2 int16 - Groups [16]uint32 - Size uint64 - Rssize int64 - Swrss int64 - Tsize int64 - Dsize int64 - Ssize int64 - Xstat uint16 - Acflag uint16 - Pctcpu uint32 - Estcpu uint32 - Slptime uint32 - Swtime uint32 - Cow uint32 - Runtime uint64 - Start Timeval - Childtime Timeval - Flag int64 - Kiflag int64 - Traceflag int32 - Stat int8 - Nice int8 - Lock int8 - Rqindex int8 - Oncpu uint8 - Lastcpu uint8 - Tdname [17]int8 - Wmesg [9]int8 - Login [18]int8 - Lockname [9]int8 - Comm [20]int8 - Emul [17]int8 - Loginclass [18]int8 - Sparestrings [50]int8 - Spareints [7]int32 - Flag2 int32 - Fibnum int32 - Cr_flags uint32 - Jid int32 - Numthreads int32 - Tid int32 - Pri Priority - Rusage Rusage - Rusage_ch Rusage - Pcb int64 /* pcb */ - Kstack int64 - Udata int64 - Tdaddr int64 /* thread */ - Spareptrs [6]int64 - Sparelongs [12]int64 - Sflag int64 - Tdflags int64 + Structsize int32 + Layout int32 + Args int64 /* pargs */ + Paddr int64 /* proc */ + Addr int64 /* user */ + Tracep int64 /* vnode */ + Textvp int64 /* vnode */ + Fd int64 /* filedesc */ + Vmspace int64 /* vmspace */ + Wchan int64 + Pid int32 + Ppid int32 + Pgid int32 + Tpgid int32 + Sid int32 + Tsid int32 + Jobc int16 + Spare_short1 int16 + Tdev_freebsd11 uint32 + Siglist [16]byte /* sigset */ + Sigmask [16]byte /* sigset */ + Sigignore [16]byte /* sigset */ + Sigcatch [16]byte /* sigset */ + Uid uint32 + Ruid uint32 + Svuid uint32 + Rgid uint32 + Svgid uint32 + Ngroups int16 + Spare_short2 int16 + Groups [16]uint32 + Size uint64 + Rssize int64 + Swrss int64 + Tsize int64 + Dsize int64 + Ssize int64 + Xstat uint16 + Acflag uint16 + Pctcpu uint32 + Estcpu uint32 + Slptime uint32 + Swtime uint32 + Cow uint32 + Runtime uint64 + Start Timeval + Childtime Timeval + Flag int64 + Kiflag int64 + Traceflag int32 + Stat int8 + Nice int8 + Lock int8 + Rqindex int8 + Oncpu_old uint8 + Lastcpu_old uint8 + Tdname [17]int8 + Wmesg [9]int8 + Login [18]int8 + Lockname [9]int8 + Comm [20]int8 + Emul [17]int8 + Loginclass [18]int8 + Moretdname [4]int8 + Sparestrings [46]int8 + Spareints [2]int32 + Tdev uint64 + Oncpu int32 + Lastcpu int32 + Tracer int32 + Flag2 int32 + Fibnum int32 + Cr_flags uint32 + Jid int32 + Numthreads int32 + Tid int32 + Pri Priority + Rusage Rusage + Rusage_ch Rusage + Pcb int64 /* pcb */ + Kstack int64 + Udata int64 + Tdaddr int64 /* thread */ + Pd int64 /* pwddesc, not accurate */ + Spareptrs [5]int64 + Sparelongs [12]int64 + Sflag int64 + Tdflags int64 } type Priority struct { @@ -170,24 +178,47 @@ type Priority struct { } type KinfoVmentry struct { - Structsize int32 - Type int32 - Start uint64 - End uint64 - Offset uint64 - Vn_fileid uint64 - Vn_fsid uint32 - Flags int32 - Resident int32 - Private_resident int32 - Protection int32 - Ref_count int32 - Shadow_count int32 - Vn_type int32 - Vn_size uint64 - Vn_rdev uint32 - Vn_mode uint16 - Status uint16 - X_kve_ispare [12]int32 - Path [1024]int8 + Structsize int32 + Type int32 + Start uint64 + End uint64 + Offset uint64 + Vn_fileid uint64 + Vn_fsid_freebsd11 uint32 + Flags int32 + Resident int32 + Private_resident int32 + Protection int32 + Ref_count int32 + Shadow_count int32 + Vn_type int32 + Vn_size uint64 + Vn_rdev_freebsd11 uint32 + Vn_mode uint16 + Status uint16 + Type_spec [8]byte + 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 } diff --git a/process/process_freebsd_arm.go b/process/process_freebsd_arm.go index 75ed30630..6c4fbf698 100644 --- a/process/process_freebsd_arm.go +++ b/process/process_freebsd_arm.go @@ -11,6 +11,7 @@ const ( KernProcProc = 8 KernProcPathname = 12 KernProcArgs = 7 + KernProcCwd = 42 ) const ( @@ -24,6 +25,7 @@ const ( const ( sizeOfKinfoVmentry = 0x488 sizeOfKinfoProc = 0x440 + sizeOfKinfoFile = 0x570 // TODO: should be changed by running on the target machine ) const ( @@ -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 +} diff --git a/process/process_freebsd_arm64.go b/process/process_freebsd_arm64.go index 3dc301c02..dabdc3e30 100644 --- a/process/process_freebsd_arm64.go +++ b/process/process_freebsd_arm64.go @@ -13,6 +13,7 @@ const ( KernProcProc = 8 KernProcPathname = 12 KernProcArgs = 7 + KernProcCwd = 42 ) const ( @@ -26,6 +27,7 @@ const ( const ( sizeOfKinfoVmentry = 0x488 sizeOfKinfoProc = 0x440 + sizeOfKinfoFile = 0x570 ) const ( @@ -201,3 +203,24 @@ type KinfoVmentry struct { X_kve_ispare [8]int32 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 +} diff --git a/process/types_freebsd.go b/process/types_freebsd.go index 47d2bb894..1e4ac1c44 100644 --- a/process/types_freebsd.go +++ b/process/types_freebsd.go @@ -42,6 +42,7 @@ const ( KernProcProc = 8 // only return procs KernProcPathname = 12 // path to executable KernProcArgs = 7 // get/set arguments/proctitle + KernProcCwd = 42 /* process current working directory */ ) const ( @@ -55,6 +56,7 @@ const ( const ( sizeOfKinfoVmentry = C.sizeof_struct_kinfo_vmentry sizeOfKinfoProc = C.sizeof_struct_kinfo_proc + sizeOfKinfoFile = C.sizeof_struct_kinfo_file ) // from sys/proc.h @@ -94,3 +96,7 @@ type KinfoProc C.struct_kinfo_proc type Priority C.struct_priority type KinfoVmentry C.struct_kinfo_vmentry + +type kinfoFile C.struct_kinfo_file + +type capRights C.struct_cap_rights