Permalink
Browse files

Add ProcExe api

- get the actual pathname of the executed command
- process current working directory
- process root directory

Change-Id: I5cffd65f1e3b3ba5039446894128732dd42948a2
  • Loading branch information...
1 parent 9d112dd commit 34b933826bf03b77aae5dbe3f44bb4214db7b3e1 @dougm dougm committed Aug 22, 2012
Showing with 52 additions and 1 deletion.
  1. +6 −0 sigar.go
  2. +8 −0 sigar_darwin.go
  3. +25 −1 sigar_linux.go
  4. +13 −0 sigar_test.go
View
@@ -110,3 +110,9 @@ type ProcTime struct {
type ProcArgs struct {
List []string
}
+
+type ProcExe struct {
+ Name string
+ Cwd string
+ Root string
+}
View
@@ -308,6 +308,14 @@ func (self *ProcArgs) Get(pid int) error {
return err
}
+func (self *ProcExe) Get(pid int) error {
+ exe := func(arg string) {
+ self.Name = arg
+ }
+
+ return kern_procargs(pid, exe, nil, nil)
+}
+
// wrapper around sysctl KERN_PROCARGS2
// callbacks params are optional,
// up to the caller as to which pieces of data they want
View
@@ -287,6 +287,26 @@ func (self *ProcArgs) Get(pid int) error {
return nil
}
+func (self *ProcExe) Get(pid int) error {
+ fields := map[string]*string{
+ "exe": &self.Name,
+ "cwd": &self.Cwd,
+ "root": &self.Root,
+ }
+
+ for name, field := range fields {
+ val, err := os.Readlink(procFileName(pid, name))
+
+ if err != nil {
+ return err
+ }
+
+ *field = val
+ }
+
+ return nil
+}
+
func parseMeminfo(table map[string]*uint64) error {
return readFile(procd+"meminfo", func(line string) bool {
fields := strings.Split(line, ":")
@@ -343,8 +363,12 @@ func strtoull(val string) (uint64, error) {
return strconv.ParseUint(val, 10, 64)
}
+func procFileName(pid int, name string) string {
+ return procd + strconv.Itoa(pid) + "/" + name
+}
+
func readProcFile(pid int, name string) ([]byte, error) {
- path := procd + strconv.Itoa(pid) + "/" + name
+ path := procFileName(pid, name)
contents, err := ioutil.ReadFile(path)
if err != nil {
View
@@ -4,6 +4,7 @@ package sigar
import (
"os"
+ "path/filepath"
"runtime"
"testing"
)
@@ -177,3 +178,15 @@ func TestProcArgs(t *testing.T) {
t.Errorf("invalid ProcArgs %s", args.List)
}
}
+
+func TestProcExe(t *testing.T) {
+ exe := ProcExe{}
+ err := exe.Get(os.Getppid())
+ if err != nil {
+ t.Error(err)
+ }
+
+ if filepath.Base(exe.Name) != "go" {
+ t.Errorf("Invalid ProcExe.Name '%v'", exe.Name)
+ }
+}

0 comments on commit 34b9338

Please sign in to comment.