Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/expose process info #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/cakturk/go-netstat

go 1.13
8 changes: 4 additions & 4 deletions netstat/netstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ type SockTabEntry struct {
Process *Process
}

// Process holds the PID and process name to which each socket belongs
// Process holds the PID and process Name to which each socket belongs
type Process struct {
pid int
name string
Pid int
Name string
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense, a few people already asked me to export these fields.


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
Expand Down
12 changes: 8 additions & 4 deletions netstat/netstat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func parseIPv6(s string) (net.IP, error) {
for len(s) != 0 {
grp := s[0:8]
u, err := strconv.ParseUint(grp, 16, 32)
binary.LittleEndian.PutUint32(ip[i:j], uint32(u))
if err != nil {
return nil, err
}
binary.LittleEndian.PutUint32(ip[i:j], uint32(u))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

i, j = i+grpLen, j+grpLen
s = s[8:]
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func getProcName(s []byte) string {
}

func (p *procFd) iterFdDir() {
// link name is of the form socket:[5860846]
// link Name is of the form socket:[5860846]
fddir := path.Join(p.base, "/fd")
fi, err := ioutil.ReadDir(fddir)
if err != nil {
Expand All @@ -212,8 +212,10 @@ func (p *procFd) iterFdDir() {
if err != nil {
return
}
if stat != nil {
defer stat.Close()
}
n, err := stat.Read(buf[:])
stat.Close()
if err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. You don't need to check for nil there. os.Open always returns non-nil
value if it succeeded. Check the source code for os.Open.

return
}
Expand Down Expand Up @@ -253,8 +255,10 @@ func doNetstat(path string, fn AcceptFn) ([]SockTabEntry, error) {
if err != nil {
return nil, err
}
if f != nil {
defer f.Close()
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, again, the same situation as above. Nil-check is unnecessary here as the
os.Open already succeeded.

tabs, err := parseSocktab(f, fn)
f.Close()
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions netstat/netstat_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}

Expand Down Expand Up @@ -394,7 +394,7 @@ func CreateToolhelp32Snapshot(flags uint32, pid uint32) (ProcessSnapshot, error)
return ret, nil
}

// ProcPIDToName translates PID to a name
// ProcPIDToName translates PID to a Name
func (snp ProcessSnapshot) ProcPIDToName(pid uint32) string {
var processEntry Processentry32
processEntry.Size = uint32(unsafe.Sizeof(processEntry))
Expand Down