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 (
"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) {

@ -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
}

@ -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 (
@ -96,7 +98,7 @@ type KinfoProc struct {
Tsid int32
Jobc int16
Spare_short1 int16
Tdev uint32
Tdev_freebsd11 uint32
Siglist [16]byte /* sigset */
Sigmask [16]byte /* sigset */
Sigignore [16]byte /* sigset */
@ -132,8 +134,8 @@ type KinfoProc struct {
Nice int8
Lock int8
Rqindex int8
Oncpu uint8
Lastcpu uint8
Oncpu_old uint8
Lastcpu_old uint8
Tdname [17]int8
Wmesg [9]int8
Login [18]int8
@ -141,8 +143,13 @@ type KinfoProc struct {
Comm [20]int8
Emul [17]int8
Loginclass [18]int8
Sparestrings [50]int8
Spareints [7]int32
Moretdname [4]int8
Sparestrings [46]int8
Spareints [2]int32
Tdev uint64
Oncpu int32
Lastcpu int32
Tracer int32
Flag2 int32
Fibnum int32
Cr_flags uint32
@ -156,7 +163,8 @@ type KinfoProc struct {
Kstack int64
Udata int64
Tdaddr int64 /* thread */
Spareptrs [6]int64
Pd int64 /* pwddesc, not accurate */
Spareptrs [5]int64
Sparelongs [12]int64
Sflag int64
Tdflags int64
@ -176,7 +184,7 @@ type KinfoVmentry struct {
End uint64
Offset uint64
Vn_fileid uint64
Vn_fsid uint32
Vn_fsid_freebsd11 uint32
Flags int32
Resident int32
Private_resident int32
@ -185,9 +193,32 @@ type KinfoVmentry struct {
Shadow_count int32
Vn_type int32
Vn_size uint64
Vn_rdev uint32
Vn_rdev_freebsd11 uint32
Vn_mode uint16
Status uint16
X_kve_ispare [12]int32
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
}

@ -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
}

@ -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
}

@ -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

Loading…
Cancel
Save