This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lsof.go
121 lines (112 loc) · 2.99 KB
/
lsof.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
)
type Process struct {
Port int
Name string
Pid int
DeployId string
}
// FindListeningProcesses returns a list of Processes that are listening on
// ports between lowPort and highPort. It will try and use the current working
// directory to determine which deployId the running process has.
// TODO(koz): Provide a fake shell so we can test this function.
func FindListeningProcesses(lowPort, highPort int) []Process {
// List all TCP sockets listening between lowPort and highPort (-P prevents
// trying to resolve port numbers to well-known service names).
portRange := fmt.Sprintf(":%d-%d", lowPort, highPort)
cmd := exec.Command("lsof", "-P", "-i", portRange, "-sTCP:LISTEN")
// The command will fail if there are no matching processes, so ignore the error.
data, _ := cmd.Output()
procs, err := parseLookupPortOutput(string(data))
if err != nil {
panic(err)
}
for i := range procs {
cwd, err := lookupCwd(procs[i].Pid)
if err != nil {
fmt.Printf("Failed to lookup cwd for %d: %s\n", procs[i].Pid, err)
continue
}
procs[i].DeployId = deriveDeployIdFromCwd(cwd)
}
return procs
}
func deriveDeployIdFromCwd(cwd string) string {
dir, deployId := path.Split(cwd)
dir, deploysDir := path.Split(strings.TrimSuffix(dir, "/"))
// Check to see if this process is one of ours.
// TODO(koz): Make this strict by passing in the server root.
if deploysDir != "deploys" {
return ""
}
return deployId
}
func parseLookupCwdOutput(str string) (string, error) {
lines := strings.Split(str, "\n")
header := lines[0]
fields := lines[1]
i := strings.Index(header, "NAME")
if i == -1 {
return "", fmt.Errorf("Failed to parse lsof output, expected NAME in header")
}
return fields[i:], nil
}
func lookupCwd(pid int) (string, error) {
// Get current working directory of given pid.
cmd := exec.Command("lsof", "-a", "-d", "cwd", "-p", strconv.Itoa(pid))
data, err := cmd.Output()
if err != nil {
return "", err
}
return parseLookupCwdOutput(string(data))
}
func parseLookupPortOutput(str string) ([]Process, error) {
lines := strings.Split(str, "\n")[1:]
processes := []Process{}
for _, line := range lines {
if line == "" {
continue
}
proc, err := parseProcess(line)
if err == nil {
processes = append(processes, proc)
}
}
return processes, nil
}
func parseProcess(line string) (Process, error) {
words := regexp.MustCompile(" +").Split(line, -1)
name := words[0]
pid, err := strconv.Atoi(words[1])
if err != nil {
fmt.Printf("Failed to parse line from lsof, ignoring...\n%s\n", words[1])
return Process{}, err
}
portWord := words[8]
re := regexp.MustCompile("^.*:(.*)$")
port, err := strconv.Atoi(string(re.FindSubmatch([]byte(portWord))[1]))
if err != nil {
return Process{}, err
}
return Process{
Port: port,
Name: name,
Pid: pid,
}, nil
}
/*
func main() {
ps, err := FindListeningProcesses(8000, 8100)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%V\n", ps)
}
*/