Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move procspy out of vendor into probe/endpoint. #737

Merged
merged 2 commits into from
Dec 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fs

import (
"io"
"os"
)

// Open is a mockable version of os.Open
var Open = func(path string) (io.ReadWriteCloser, error) {
return os.Open(path)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestLSOFParsing(t *testing.T) {
"p25196\n" +
"ccello-app\n" +
"n127.0.0.1:48094->127.0.0.1:4039\n" +
"n*:4040\n": map[string]Proc{
"127.0.0.1:48094": Proc{
"n*:4040\n": {
"127.0.0.1:48094": {
PID: 25196,
Name: "cello-app",
},
Expand All @@ -23,7 +23,7 @@ func TestLSOFParsing(t *testing.T) {
"cdhclient\n" +
"n*:68\n" +
"n*:38282\n" +
"n*:40625\n": map[string]Proc{},
"n*:40625\n": {},

// A bunch
"p13100\n" +
Expand All @@ -39,28 +39,28 @@ func TestLSOFParsing(t *testing.T) {
"n192.168.2.111:56385->74.201.105.31:443\n" +
"p21356\n" +
"cssh\n" +
"n192.168.2.111:33963->192.168.2.71:22\n": map[string]Proc{
"[::1]:6600": Proc{
"n192.168.2.111:33963->192.168.2.71:22\n": {
"[::1]:6600": {
PID: 13100,
Name: "mpd",
},
"[2003:45:2b57:8900:1869:2947:f942:aba7]:55711": Proc{
"[2003:45:2b57:8900:1869:2947:f942:aba7]:55711": {
PID: 14612,
Name: "chromium",
},
"192.168.2.111:37158": Proc{
"192.168.2.111:37158": {
PID: 14612,
Name: "chromium",
},
"192.168.2.111:44013": Proc{
"192.168.2.111:44013": {
PID: 14612,
Name: "chromium",
},
"192.168.2.111:56385": Proc{
"192.168.2.111:56385": {
PID: 14612,
Name: "chromium",
},
"192.168.2.111:33963": Proc{
"192.168.2.111:33963": {
PID: 21356,
Name: "ssh",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ package procspy

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"syscall"

"github.com/weaveworks/scope/common/fs"
)

var (
Expand All @@ -19,69 +22,66 @@ func SetProcRoot(root string) {
procRoot = root
}

// made variables for mocking
var (
readDir = ioutil.ReadDir
lstat = syscall.Lstat
stat = syscall.Stat
open = fs.Open
)

// walkProcPid walks over all numerical (PID) /proc entries, and sees if their
// ./fd/* files are symlink to sockets. Returns a map from socket ID (inode)
// to PID. Will return an error if /proc isn't there.
func walkProcPid(buf *bytes.Buffer) (map[uint64]Proc, error) {
fh, err := os.Open(procRoot)
if err != nil {
return nil, err
}

dirNames, err := fh.Readdirnames(-1)
fh.Close()
dirNames, err := readDir(procRoot)
if err != nil {
return nil, err
}

var (
res = map[uint64]Proc{}
namespaces = map[uint64]struct{}{}
stat syscall.Stat_t
statT syscall.Stat_t
)
for _, dirName := range dirNames {
for _, entry := range dirNames {
dirName := entry.Name()
pid, err := strconv.ParseUint(dirName, 10, 0)
if err != nil {
// Not a number, so not a PID subdir.
continue
}

fdBase := filepath.Join(procRoot, dirName, "fd")
dfh, err := os.Open(fdBase)
fds, err := readDir(fdBase)
if err != nil {
// Process is be gone by now, or we don't have access.
continue
}

fdNames, err := dfh.Readdirnames(-1)
dfh.Close()
if err != nil {
continue
}

// Read network namespace, and if we haven't seen it before,
// read /proc/<pid>/net/tcp
err = syscall.Lstat(filepath.Join(procRoot, dirName, "/ns/net"), &stat)
err = lstat(filepath.Join(procRoot, dirName, "/ns/net"), &statT)
if err != nil {
continue
}

if _, ok := namespaces[stat.Ino]; !ok {
namespaces[stat.Ino] = struct{}{}
if _, ok := namespaces[statT.Ino]; !ok {
namespaces[statT.Ino] = struct{}{}
readFile(filepath.Join(procRoot, dirName, "/net/tcp"), buf)
readFile(filepath.Join(procRoot, dirName, "/net/tcp6"), buf)
}

var name string
for _, fdName := range fdNames {
for _, fd := range fds {
// Direct use of syscall.Stat() to save garbage.
err = syscall.Stat(filepath.Join(fdBase, fdName), &stat)
err = stat(filepath.Join(fdBase, fd.Name()), &statT)
if err != nil {
continue
}

// We want sockets only.
if stat.Mode&syscall.S_IFMT != syscall.S_IFSOCK {
if statT.Mode&syscall.S_IFMT != syscall.S_IFSOCK {
continue
}

Expand All @@ -92,7 +92,7 @@ func walkProcPid(buf *bytes.Buffer) (map[uint64]Proc, error) {
}
}

res[stat.Ino] = Proc{
res[statT.Ino] = Proc{
PID: uint(pid),
Name: name,
}
Expand All @@ -104,7 +104,7 @@ func walkProcPid(buf *bytes.Buffer) (map[uint64]Proc, error) {

// procName does a pid->name lookup.
func procName(base string) string {
fh, err := os.Open(filepath.Join(base, "/comm"))
fh, err := open(filepath.Join(base, "/comm"))
if err != nil {
return ""
}
Expand Down
57 changes: 57 additions & 0 deletions probe/endpoint/procspy/proc_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package procspy

import (
"bytes"
"reflect"
"syscall"
"testing"

"github.com/weaveworks/scope/test/fs"
)

var mockFS = fs.Dir("",
fs.Dir("proc",
fs.Dir("1",
fs.Dir("fd",
fs.File{
FName: "16",
FStat: syscall.Stat_t{
Ino: 45,
Mode: syscall.S_IFSOCK,
},
},
),
fs.File{
FName: "comm",
FContents: "foo\n",
},
fs.Dir("ns",
fs.File{
FName: "net",
FStat: syscall.Stat_t{},
},
),
),
),
)

func TestWalkProcPid(t *testing.T) {
oldReadDir, oldLstat, oldStat, oldOpen := readDir, lstat, stat, open
defer func() { readDir, lstat, stat, open = oldReadDir, oldLstat, oldStat, oldOpen }()
readDir, lstat, stat, open = mockFS.ReadDir, mockFS.Lstat, mockFS.Stat, mockFS.Open

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.


buf := bytes.Buffer{}
have, err := walkProcPid(&buf)
if err != nil {
t.Fatal(err)
}
want := map[uint64]Proc{
45: {
PID: 1,
Name: "foo",
},
}
if !reflect.DeepEqual(want, have) {
t.Fatalf("%+v", have)
}
}
2 changes: 1 addition & 1 deletion probe/endpoint/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/weaveworks/procspy"
"github.com/weaveworks/scope/probe/endpoint/procspy"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/report"
)
Expand Down
2 changes: 1 addition & 1 deletion probe/endpoint/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"strconv"
"testing"

"github.com/weaveworks/procspy"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/endpoint"
"github.com/weaveworks/scope/probe/endpoint/procspy"
"github.com/weaveworks/scope/report"
)

Expand Down
Loading