forked from weaveworks/procspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spy_linux.go
60 lines (52 loc) · 1.05 KB
/
spy_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package procspy
import (
"bytes"
"sync"
)
var bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 5000))
},
}
type pnConnIter struct {
transport string
pn *ProcNet
buf *bytes.Buffer
procs map[uint64]Proc
}
func (c *pnConnIter) Next() *Connection {
n := c.pn.Next()
if n == nil {
// Done!
bufPool.Put(c.buf)
return nil
}
if proc, ok := c.procs[n.Inode]; ok {
n.Proc = proc
}
n.Transport = c.transport
return n
}
// cbConnections sets Connections()
var cbConnections = func(processes bool, state uint) (ConnIter, error) {
// buffer for contents of /proc/<pid>/net/tcp
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
var procs map[uint64]Proc
if processes {
var err error
if procs, err = walkProcPid(buf); err != nil {
return nil, err
}
}
if buf.Len() == 0 {
readFile(procRoot+"/net/tcp", buf)
readFile(procRoot+"/net/tcp6", buf)
}
return &pnConnIter{
transport: TcpTransport,
pn: NewProcNet(buf.Bytes(), state),
buf: buf,
procs: procs,
}, nil
}