From 51c2995c2b16904f376210921885dc9830aba5a9 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Fri, 25 Apr 2014 15:19:19 +0900 Subject: [PATCH] implement cwd on linux. --- README.rst | 2 +- process_linux.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index f2f6a12..2a70472 100644 --- a/README.rst +++ b/README.rst @@ -79,6 +79,7 @@ Current Status - cmdline (linux) - create_time (linux) - status (linux) + - cwd (linux) - exe (freebsd) - uids (linux) - gids (linux) @@ -99,7 +100,6 @@ Current Status - Process class - parent - - cwd - username - ionice - rlimit diff --git a/process_linux.go b/process_linux.go index 0428554..a8e0746 100644 --- a/process_linux.go +++ b/process_linux.go @@ -37,7 +37,7 @@ func NewProcess(pid int32) (*Process, error) { // Fill Process information from fillFuncs var wg sync.WaitGroup funcs := []fillFunc{fillFromStat, fillFromStatus, fillFromfd, - fillFromCmdline, fillFromStatm} + fillFromCmdline, fillFromStatm, fillFromCwd} wg.Add(len(funcs)) for _, f := range funcs { @@ -77,6 +77,18 @@ func fillFromfd(pid int32, p *Process) error { return nil } +// Get cwd from /proc/(pid)/cwd +func fillFromCwd(pid int32, p *Process) error { + cwdPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "cwd") + cwd, err := os.Readlink(cwdPath) + if err != nil { + return err + } + p.Cwd = string(cwd) + + return nil +} + // Get cmdline from /proc/(pid)/cmdline func fillFromCmdline(pid int32, p *Process) error { cmdPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "cmdline")