mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-24 13:48:56 +08:00
[net][linux]: get Pid and Fd from inode in netlink.
This commit is contained in:
parent
651c992042
commit
28dc7e6146
@ -6,6 +6,7 @@ package net
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
@ -22,17 +23,54 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
|
|||||||
var err error
|
var err error
|
||||||
var inodes map[string][]inodeMap
|
var inodes map[string][]inodeMap
|
||||||
if pid == 0 {
|
if pid == 0 {
|
||||||
return connectionsNetLink(tmap)
|
inodes, err = getProcInodesAll(root, max)
|
||||||
|
} else {
|
||||||
|
inodes, err = getProcInodes(root, pid, max)
|
||||||
}
|
}
|
||||||
inodes, err = getProcInodes(root, pid, max)
|
|
||||||
if len(inodes) == 0 {
|
if len(inodes) == 0 {
|
||||||
// no connection for the pid
|
// no inode for the pid
|
||||||
return []ConnectionStat{}, nil
|
return []ConnectionStat{}, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cound not get pid(s), %d: %s", pid, err)
|
return nil, fmt.Errorf("cound not get pid(s), %d: %s", pid, err)
|
||||||
}
|
}
|
||||||
return statsFromInodes(root, pid, tmap, inodes, skipUids)
|
|
||||||
|
return connectionsNetLink(tmap, inodes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectionsNetLink(kinds []netConnectionKindType, inodes map[string][]inodeMap) ([]ConnectionStat, error) {
|
||||||
|
wait := &sync.WaitGroup{}
|
||||||
|
reply := make([][]ConnectionStat, len(kinds))
|
||||||
|
var retErrr error
|
||||||
|
|
||||||
|
for i, kind := range kinds {
|
||||||
|
wait.Add(1)
|
||||||
|
if kind.family == syscall.AF_UNIX {
|
||||||
|
go getUnixConnections(wait, i, reply)
|
||||||
|
} else {
|
||||||
|
go getInetConnections(wait, i, kind, reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wait.Wait()
|
||||||
|
|
||||||
|
ret := make([]ConnectionStat, 0)
|
||||||
|
for i := range kinds {
|
||||||
|
for _, cs := range reply[i] {
|
||||||
|
iss, exists := inodes[strconv.Itoa(int(cs.Fd))]
|
||||||
|
if exists {
|
||||||
|
for _, is := range iss {
|
||||||
|
cs.Pid = is.pid
|
||||||
|
cs.Fd = is.fd
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cs.Fd = 0
|
||||||
|
}
|
||||||
|
ret = append(ret, cs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, retErrr
|
||||||
}
|
}
|
||||||
|
|
||||||
func getInetConnections(wait *sync.WaitGroup, i int, k netConnectionKindType, reply [][]ConnectionStat) {
|
func getInetConnections(wait *sync.WaitGroup, i int, k netConnectionKindType, reply [][]ConnectionStat) {
|
||||||
@ -52,7 +90,7 @@ func getInetConnections(wait *sync.WaitGroup, i int, k netConnectionKindType, re
|
|||||||
Status: netlink.TCPState(msg.State).String(),
|
Status: netlink.TCPState(msg.State).String(),
|
||||||
Uids: []int32{int32(msg.UID)},
|
Uids: []int32{int32(msg.UID)},
|
||||||
// Pid: msg.Pid,
|
// Pid: msg.Pid,
|
||||||
// Fd: diag.Inode,
|
Fd: msg.Inode,
|
||||||
}
|
}
|
||||||
t[i] = conn
|
t[i] = conn
|
||||||
}
|
}
|
||||||
@ -72,35 +110,12 @@ func getUnixConnections(wait *sync.WaitGroup, i int, reply [][]ConnectionStat) {
|
|||||||
Family: uint32(msg.Family),
|
Family: uint32(msg.Family),
|
||||||
Status: netlink.TCPState(msg.State).String(),
|
Status: netlink.TCPState(msg.State).String(),
|
||||||
Uids: []int32{},
|
Uids: []int32{},
|
||||||
Laddr: Addr{
|
Laddr: Addr{
|
||||||
IP: msg.Path,
|
IP: msg.Path,
|
||||||
},
|
},
|
||||||
|
Fd: msg.Inode,
|
||||||
}
|
}
|
||||||
t[i] = conn
|
t[i] = conn
|
||||||
}
|
}
|
||||||
reply[i] = t
|
reply[i] = t
|
||||||
}
|
}
|
||||||
|
|
||||||
func connectionsNetLink(kinds []netConnectionKindType) ([]ConnectionStat, error) {
|
|
||||||
wait := &sync.WaitGroup{}
|
|
||||||
reply := make([][]ConnectionStat, len(kinds))
|
|
||||||
var retErrr error
|
|
||||||
|
|
||||||
for i, kind := range kinds {
|
|
||||||
wait.Add(1)
|
|
||||||
if kind.family == syscall.AF_UNIX {
|
|
||||||
go getUnixConnections(wait, i, reply)
|
|
||||||
} else {
|
|
||||||
go getInetConnections(wait, i, kind, reply)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wait.Wait()
|
|
||||||
|
|
||||||
ret := make([]ConnectionStat, 0)
|
|
||||||
for i := range kinds {
|
|
||||||
ret = append(ret, reply[i]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, retErrr
|
|
||||||
}
|
|
||||||
|
@ -22,10 +22,10 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
|
|||||||
inodes, err = getProcInodesAll(root, max)
|
inodes, err = getProcInodesAll(root, max)
|
||||||
} else {
|
} else {
|
||||||
inodes, err = getProcInodes(root, pid, max)
|
inodes, err = getProcInodes(root, pid, max)
|
||||||
if len(inodes) == 0 {
|
}
|
||||||
// no connection for the pid
|
if len(inodes) == 0 {
|
||||||
return []ConnectionStat{}, nil
|
// no connection for the pid
|
||||||
}
|
return []ConnectionStat{}, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cound not get pid(s), %d: %s", pid, err)
|
return nil, fmt.Errorf("cound not get pid(s), %d: %s", pid, err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user