From 8d494617a2c4f671d88b363dd67f7a40ebcee10b Mon Sep 17 00:00:00 2001 From: Anes Mukhametov Date: Mon, 21 Oct 2019 16:45:02 +0300 Subject: [PATCH 1/4] defer close --- netstat/netstat_linux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netstat/netstat_linux.go b/netstat/netstat_linux.go index 43ab3f5..c077f3e 100644 --- a/netstat/netstat_linux.go +++ b/netstat/netstat_linux.go @@ -209,11 +209,11 @@ func (p *procFd) iterFdDir() { } if p.p == nil { stat, err := os.Open(path.Join(p.base, "stat")) + defer f.Close() if err != nil { return } n, err := stat.Read(buf[:]) - stat.Close() if err != nil { return } @@ -250,11 +250,11 @@ 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 } tabs, err := parseSocktab(f, fn) - f.Close() if err != nil { return nil, err } From 6c48ec033be14d1c8c3ba1f216c62f3d094e3347 Mon Sep 17 00:00:00 2001 From: Anes Mukhametov Date: Mon, 21 Oct 2019 16:48:23 +0300 Subject: [PATCH 2/4] fix close --- netstat/netstat_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netstat/netstat_linux.go b/netstat/netstat_linux.go index c077f3e..9fdb003 100644 --- a/netstat/netstat_linux.go +++ b/netstat/netstat_linux.go @@ -209,7 +209,7 @@ func (p *procFd) iterFdDir() { } if p.p == nil { stat, err := os.Open(path.Join(p.base, "stat")) - defer f.Close() + defer stat.Close() if err != nil { return } From 7aa3485d04bd8e130cae2bc81df79351ccfc3a87 Mon Sep 17 00:00:00 2001 From: Anes Mukhametov Date: Mon, 21 Oct 2019 18:12:29 +0300 Subject: [PATCH 3/4] expose pid/name --- netstat/netstat.go | 6 +++--- netstat/netstat_windows.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/netstat/netstat.go b/netstat/netstat.go index a5c0e69..4d95c51 100644 --- a/netstat/netstat.go +++ b/netstat/netstat.go @@ -27,12 +27,12 @@ type SockTabEntry struct { // Process holds the PID and process name to which each socket belongs type Process struct { - pid int - name string + Pid int + Name string } func (p *Process) String() string { - return fmt.Sprintf("%d/%s", p.pid, p.name) + return fmt.Sprintf("%d/%s", p.Pid, p.Name) } // SkState type represents socket connection state diff --git a/netstat/netstat_windows.go b/netstat/netstat_windows.go index cffeedc..8e3b289 100644 --- a/netstat/netstat_windows.go +++ b/netstat/netstat_windows.go @@ -123,8 +123,8 @@ func (pid WinPid) Process(snp ProcessSnapshot) *Process { return nil } return &Process{ - pid: int(pid), - name: snp.ProcPIDToName(uint32(pid)), + Pid: int(pid), + Name: snp.ProcPIDToName(uint32(pid)), } } From d5654385aeaaa0cc5e55199df89caa401492a518 Mon Sep 17 00:00:00 2001 From: Anes Mukhametov Date: Thu, 24 Oct 2019 20:22:32 +0300 Subject: [PATCH 4/4] close --- netstat/netstat_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netstat/netstat_linux.go b/netstat/netstat_linux.go index 9fdb003..35e0cd7 100644 --- a/netstat/netstat_linux.go +++ b/netstat/netstat_linux.go @@ -209,7 +209,6 @@ func (p *procFd) iterFdDir() { } if p.p == nil { stat, err := os.Open(path.Join(p.base, "stat")) - defer stat.Close() if err != nil { return } @@ -220,6 +219,7 @@ func (p *procFd) iterFdDir() { z := bytes.SplitN(buf[:n], []byte(" "), 3) name := getProcName(z[1]) p.p = &Process{p.pid, name} + stat.Close() } sk.Process = p.p }