Skip to content

Commit

Permalink
Support process group ID (pgid) for Darwin and Linux (#26)
Browse files Browse the repository at this point in the history
* Added Darwin pgid

* Added Linux pgid

* Skip the task_info test if not running as root
  • Loading branch information
nickivanov authored and andrewkroh committed May 4, 2016
1 parent fe371dd commit d752b70
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sigar_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ func (self *ProcState) Get(pid int) error {

self.Ppid = int(info.pbsd.pbi_ppid)

self.Pgid = int(info.pbsd.pbi_pgid)

self.Tty = int(info.pbsd.e_tdev)

self.Priority = int(info.ptinfo.pti_priority)
Expand Down
76 changes: 76 additions & 0 deletions sigar_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gosigar_test

import (
"bufio"
"bytes"
"fmt"
"os/exec"
"os/user"
"strconv"
"strings"
"testing"

sigar "github.com/elastic/gosigar"
"github.com/stretchr/testify/assert"
)

var procinfo map[string]string

func setUp(t testing.TB) {
out, err := exec.Command("/bin/ps", "-p1", "-c", "-opid,comm,stat,ppid,pgid,tty,pri,ni").Output()
if err != nil {
t.Fatal(err)
}
rdr := bufio.NewReader(bytes.NewReader(out))
_, err = rdr.ReadString('\n') // skip header
if err != nil {
t.Fatal(err)
}
data, err := rdr.ReadString('\n')
if err != nil {
t.Fatal(err)
}
procinfo = make(map[string]string, 8)
fields := strings.Fields(data)
procinfo["pid"] = fields[0]
procinfo["name"] = fields[1]
procinfo["stat"] = fields[2]
procinfo["ppid"] = fields[3]
procinfo["pgid"] = fields[4]
procinfo["tty"] = fields[5]
procinfo["prio"] = fields[6]
procinfo["nice"] = fields[7]

}

func tearDown(t testing.TB) {
}

/* ProcState.Get() call task_info, which on Mac OS X requires root
or a signed executable. Skip the test if not running as root
to accommodate automated tests, but let users test locally using
`sudo -E go test`
*/

func TestDarwinProcState(t *testing.T) {
setUp(t)
defer tearDown(t)

state := sigar.ProcState{}
usr, err := user.Current()
if err == nil && usr.Username == "root" {
if assert.NoError(t, state.Get(1)) {

ppid, _ := strconv.Atoi(procinfo["ppid"])
pgid, _ := strconv.Atoi(procinfo["pgid"])

assert.Equal(t, procinfo["name"], state.Name)
assert.Equal(t, ppid, state.Ppid)
assert.Equal(t, pgid, state.Pgid)
assert.Equal(t, 1, state.Pgid)
assert.Equal(t, 0, state.Ppid)
}
} else {
fmt.Println("Skipping ProcState test; run as root to test")
}
}
1 change: 1 addition & 0 deletions sigar_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type ProcState struct {
Username string
State RunState
Ppid int
Pgid int
Tty int
Priority int
Nice int
Expand Down
2 changes: 2 additions & 0 deletions sigar_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ func (self *ProcState) Get(pid int) error {

self.Ppid, _ = strconv.Atoi(fields[1])

self.Pgid, _ = strconv.Atoi(fields[2])

self.Tty, _ = strconv.Atoi(fields[4])

self.Priority, _ = strconv.Atoi(fields[15])
Expand Down
5 changes: 3 additions & 2 deletions sigar_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestLinuxProcState(t *testing.T) {
state := sigar.ProcState{}
if assert.NoError(t, state.Get(pid)) {
assert.Equal(t, n, state.Name)
assert.Equal(t, 2, state.Pgid)
assert.Equal(t, strconv.Itoa(uid), state.Username)
}
}()
Expand Down Expand Up @@ -218,9 +219,9 @@ DirectMap2M: 333824 kB
}

func writePidStats(pid int, procName string, path string) error {
stats := "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 " +
stats := "S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 " +
"20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 " +
"35 36 37 38 39 40"
"35 36 37 38 39"

statContents := []byte(fmt.Sprintf("%d (%s) %s", pid, procName, stats))
return ioutil.WriteFile(path, statContents, 0644)
Expand Down

0 comments on commit d752b70

Please sign in to comment.