[net]linux: add process_unix for NetConnections.

This commit is contained in:
Shirou WAKAYAMA 2016-03-05 22:24:26 +09:00
parent 32c62b5d48
commit e4ddff09d5
2 changed files with 55 additions and 5 deletions

View File

@ -273,6 +273,7 @@ type connTmp struct {
status string
pid int32
boundPid int32
path string
}
// Return a list of network connections opened.
@ -320,7 +321,7 @@ func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) {
return nil, err
}
for _, c := range ls {
ret = append(ret, NetConnectionStat{
conn := NetConnectionStat{
Fd: c.fd,
Family: t.family,
Type: t.sockType,
@ -328,7 +329,13 @@ func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) {
Raddr: c.raddr,
Status: c.status,
Pid: c.pid,
})
}
if c.pid == 0 {
conn.Pid = c.boundPid
} else {
conn.Pid = c.pid
}
ret = append(ret, conn)
}
}
@ -493,8 +500,8 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
if err != nil {
return nil, err
}
// skip first line
var ret []connTmp
// skip first line
for _, line := range lines[1:] {
l := strings.Fields(line)
if len(l) < 10 {
@ -543,6 +550,49 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
}
func processUnix(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
lines, err := common.ReadLines(file)
if err != nil {
return nil, err
}
var ret []connTmp
// skip first line
for _, line := range lines[1:] {
tokens := strings.Fields(line)
if len(tokens) < 7 {
continue
}
st, err := strconv.Atoi(tokens[4])
if err != nil {
continue
}
inode := tokens[6]
var pairs []inodeMap
pairs, exists := inodes[inode]
if !exists {
pairs = []inodeMap{}
}
for _, pair := range pairs {
if filterPid > 0 && filterPid != pair.pid {
continue
}
var path string
if len(tokens) == 8 {
path = tokens[len(tokens)-1]
}
ret = append(ret, connTmp{
family: kind.family,
sockType: uint32(st),
raddr: Addr{},
pid: pair.pid,
status: "NONE",
path: path,
})
}
}
return []connTmp{}, nil
}

View File

@ -1,6 +1,7 @@
package net
import (
"os"
"syscall"
"testing"
@ -10,8 +11,7 @@ import (
func TestGetProcInodes(t *testing.T) {
root := common.HostProc("")
// checkPid := os.Getpid() // process.test
checkPid := 13378
checkPid := os.Getpid() // process.test
v, err := getProcInodes(root, int32(checkPid))
assert.Nil(t, err)