From 0ab054c57674372126f32d9b863e5ec2c08d293c Mon Sep 17 00:00:00 2001 From: WAKAYAMA shirou Date: Thu, 1 May 2014 18:06:16 +0900 Subject: [PATCH] implements some Process funcs on freebsd. --- README.rst | 14 +++---- process_freebsd.go | 91 +++++++++++++++++++++++++++++++++++++++------- process_linux.go | 8 ++++ 3 files changed, 92 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index 6a8f3e3b..c14eb629 100644 --- a/README.rst +++ b/README.rst @@ -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) diff --git a/process_freebsd.go b/process_freebsd.go index e91368cf..8ac876da 100644 --- a/process_freebsd.go +++ b/process_freebsd.go @@ -32,11 +32,20 @@ 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") @@ -44,6 +53,9 @@ func (p *Process) Exe() (string, error) { 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") } @@ -51,21 +63,53 @@ 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") @@ -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) @@ -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") @@ -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) @@ -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 } diff --git a/process_linux.go b/process_linux.go index ffd320a4..fbd7e393 100644 --- a/process_linux.go +++ b/process_linux.go @@ -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() }