Compare commits

...

12 Commits

  1. 8
      .gitignore
  2. 2
      cmd/netstat/main.go
  3. 2
      go.mod
  4. 0
      netstat.go
  5. 53
      netstat_linux.go
  6. 0
      netstat_windows.go

8
.gitignore vendored

@ -0,0 +1,8 @@
*~
*.bak
**/.envrc
**/.DS_Store
/netstat
/cmd/netstat/netstat

@ -6,7 +6,7 @@ import (
"net"
"os"
"github.com/cakturk/go-netstat/netstat"
"git.mills.io/prologic/netstat"
)
var (

@ -1,3 +1,3 @@
module github.com/cakturk/go-netstat
module git.mills.io/prologic/netstat
go 1.13

@ -153,6 +153,7 @@ func parseSocktab(r io.Reader, accept AcceptFn) ([]SockTabEntry, error) {
}
e.UID = uint32(u)
e.ino = fields[9]
extractProcInfo(&e)
if accept(&e) {
tab = append(tab, e)
}
@ -163,7 +164,7 @@ func parseSocktab(r io.Reader, accept AcceptFn) ([]SockTabEntry, error) {
type procFd struct {
base string
pid int
sktab []SockTabEntry
sktab *SockTabEntry
p *Process
}
@ -200,34 +201,33 @@ func (p *procFd) iterFdDir() {
continue
}
for i := range p.sktab {
sk := &p.sktab[i]
ss := sockPrefix + sk.ino + "]"
if ss != lname {
continue
sk := p.sktab
ss := sockPrefix + sk.ino + "]"
if ss != lname {
continue
}
if p.p == nil {
stat, err := os.Open(path.Join(p.base, "stat"))
if err != nil {
return
}
if stat != nil {
defer stat.Close()
}
if p.p == nil {
stat, err := os.Open(path.Join(p.base, "stat"))
if err != nil {
return
}
if stat != nil {
defer stat.Close()
}
n, err := stat.Read(buf[:])
if err != nil {
return
}
z := bytes.SplitN(buf[:n], []byte(" "), 3)
name := getProcName(z[1])
p.p = &Process{p.pid, name}
n, err := stat.Read(buf[:])
if err != nil {
return
}
sk.Process = p.p
z := bytes.SplitN(buf[:n], []byte(" "), 3)
name := getProcName(z[1])
p.p = &Process{p.pid, name}
stat.Close()
}
sk.Process = p.p
}
}
func extractProcInfo(sktab []SockTabEntry) {
func extractProcInfo(sktab *SockTabEntry) {
const basedir = "/proc"
fi, err := ioutil.ReadDir(basedir)
if err != nil {
@ -251,6 +251,7 @@ func extractProcInfo(sktab []SockTabEntry) {
// doNetstat - collect information about network port status
func doNetstat(path string, fn AcceptFn) ([]SockTabEntry, error) {
f, err := os.Open(path)
defer f.Close()
if err != nil {
return nil, err
}
@ -261,7 +262,11 @@ func doNetstat(path string, fn AcceptFn) ([]SockTabEntry, error) {
if err != nil {
return nil, err
}
extractProcInfo(tabs)
if len(tabs) != 0 {
extractProcInfo(tabs)
}
return tabs, nil
}
Loading…
Cancel
Save