Skip to content

Commit

Permalink
implements some Process funcs on freebsd.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed May 1, 2014
1 parent b6d9d46 commit 0ab054c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ Current Status
- Process class

- pid (linux, freebsd, windows)
- ppid (linux, windows)
- ppid (linux, freebsd, windows)
- name (linux)
- cmdline (linux)
- create_time (linux)
- status (linux)
- nwd (linux)
- cwd (linux)
- exe (linux, freebsd, windows)
- uids (linux)
- gids (linux)
- terminal (linux)
- uids (linux, freebsd)
- gids (linux, freebsd)
- terminal (linux, freebsd)
- nice (linux)
- num_fds (linux)
- num_threads (linux, windows)
- num_threads (linux, freebsd, windows)
- cpu_times (linux)
- memory_info (linux)
- memory_info (linux, freebsd)
- memory_info_ex (linux)
- memory_maps() (linux)
- open_files (linux)
Expand Down
91 changes: 77 additions & 14 deletions process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,84 @@ func Pids() ([]int32, error) {
}

func (p *Process) Ppid() (int32, error) {
return 0, errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return 0, err
}

return k.KiPpid, nil
}
func (p *Process) Name() (string, error) {
name := ""
return name, errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return "", err
}

return string(k.KiComm[:]), nil
}
func (p *Process) Exe() (string, error) {
return "", errors.New("not implemented yet")
}
func (p *Process) Cmdline() (string, error) {
return "", errors.New("not implemented yet")
}
func (p *Process) CreateTime() (int64, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Cwd() (string, error) {
return "", errors.New("not implemented yet")
}
func (p *Process) Parent() (*Process, error) {
return p, errors.New("not implemented yet")
}
func (p *Process) Status() (string, error) {
return "", errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return "", err
}

return string(k.KiStat[:]), nil
}
func (p *Process) Username() (string, error) {
return "", errors.New("not implemented yet")
}
func (p *Process) Uids() ([]int32, error) {
var uids []int32
return uids, errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return nil, err
}

uids := make([]int32, 0, 3)

uids = append(uids, int32(k.KiRuid), int32(k.KiUID), int32(k.KiSvuid))

return uids, nil
}
func (p *Process) Gids() ([]int32, error) {
var gids []int32
return gids, errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return nil, err
}

gids := make([]int32, 0, 3)
gids = append(gids, int32(k.KiRgid), int32(k.KiNgroups[0]), int32(k.KiSvuid))

return gids, nil
}
func (p *Process) Terminal() (string, error) {
return "", errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return "", err
}

ttyNr := uint64(k.KiTdev)

termmap, err := getTerminalMap()
if err != nil {
return "", err
}

return termmap[ttyNr], nil
}
func (p *Process) Nice() (int32, error) {
return 0, errors.New("not implemented yet")
Expand All @@ -87,7 +131,12 @@ func (p *Process) NumFDs() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) NumThreads() (int32, error) {
return 0, errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return 0, err
}

return k.KiNumthreads, nil
}
func (p *Process) Threads() (map[string]string, error) {
ret := make(map[string]string, 0)
Expand All @@ -103,7 +152,17 @@ func (p *Process) CPUAffinity() ([]int32, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return nil, errors.New("not implemented yet")
k, err := p.getKProc()
if err != nil {
return nil, err
}

ret := &MemoryInfoStat{
RSS: uint64(k.KiRssize),
VMS: uint64(k.KiSize),
}

return ret, nil
}
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return nil, errors.New("not implemented yet")
Expand Down Expand Up @@ -219,8 +278,7 @@ func callSyscall(mib []int32) ([]byte, uint64, error) {
return buf, length, nil
}

func NewProcess(pid int32) (*Process, error) {
p := &Process{Pid: pid}
func (p *Process) getKProc() (*KinfoProc, error) {
mib := []int32{CTL_KERN, KERN_PROC, KERN_PROC_PID, p.Pid}

buf, length, err := callSyscall(mib)
Expand All @@ -237,6 +295,11 @@ func NewProcess(pid int32) (*Process, error) {
return nil, err
}

copyParams(&k, p)
return &k, nil
}

func NewProcess(pid int32) (*Process, error) {
p := &Process{Pid: pid}

return p, nil
}
8 changes: 8 additions & 0 deletions process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func (p *Process) Exe() (string, error) {
func (p *Process) Cmdline() (string, error) {
return p.fillFromCmdline()
}
func (p *Process) CreateTime() (int64, error) {
_, _, _, createTime, _, err := p.fillFromStat()
if err != nil {
return 0, err
}
return createTime, nil
}

func (p *Process) Cwd() (string, error) {
return p.fillFromCwd()
}
Expand Down

0 comments on commit 0ab054c

Please sign in to comment.