-
Notifications
You must be signed in to change notification settings - Fork 1
/
unix.go
67 lines (59 loc) · 1.36 KB
/
unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
//"net"
)
type UnixSocketInfo struct {
Num string `json:"num,omitempty"`
Refcount string `json:"refcount,omitempty"`
Protocol string `json:"protocol,omitempty"`
Flags string `json:"flags,omitempty"`
Stype string `json:"type,omitempty"`
St string `json:"st,omitempty"`
Inode int64 `json:"inode,omitempty"`
Path string `json:"path,omitempty"`
}
func NewUnixSocketInfo(s string) *UnixSocketInfo {
var us UnixSocketInfo
fmt.Sscanf(s, "%16s: %8s %8s %8s %4s %2s %7d %s", &us.Num, &us.Refcount, &us.Protocol, &us.Flags, &us.Stype, &us.St, &us.Inode, &us.Path)
return &us
}
func getUnixSocketInfo(inode int64) *SocketInfo {
f, err := os.Open(ProcNetUnix)
if err != nil {
log.Println("error opening file ", ProcNetUnix, err)
return nil
}
defer f.Close()
r := bufio.NewReader(f)
// Throw away first line
line, err := r.ReadString(10) // 0x0A separator = newline
if err != nil {
log.Println(err)
return nil
}
for {
line, err = r.ReadString(10) // 0x0A separator = newline
if err == io.EOF {
return nil
} else if err != nil {
log.Println(err)
return nil
}
us := NewUnixSocketInfo(line)
if us == nil {
return nil
}
//fmt.Println("++++++++++++", inode, us)
if inode == us.Inode {
var si SocketInfo
si.Unix = us
return &si
}
}
return nil
}