This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsof.go
188 lines (169 loc) · 4.12 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package lsof
import (
"fmt"
"os/exec"
"strings"
)
// Process defines a process using an open file. Properties here are strings
// for compatibility with different platforms.
type Process struct {
PID string
Command string
UserID string
FileDescriptors []FileDescriptor
}
// FileType defines the type of file in use by a process
type FileType string
const (
FileTypeUnknown FileType = ""
FileTypeDir FileType = "DIR"
FileTypeFile FileType = "REG"
)
// FileDescriptor defines a file in use by a process
type FileDescriptor struct {
FD string
Type FileType
Name string
}
// ExecError is an error running lsof
type ExecError struct {
command string
args []string
output string
err error
}
func (e ExecError) Error() string {
return fmt.Sprintf("Error running %s %s: %s (%s)", e.command, e.args, e.err, e.output)
}
// MountPoint returns processes using the mountpoint "lsof /dir"
func MountPoint(dir string) ([]Process, error) {
// TODO: Fix lsof to not return error on exit status 1 since it isn't
// really any error, only an indication that there was no use of the
// mount.
return run([]string{"-F", "pcuftn", dir})
}
func fileTypeFromString(s string) FileType {
switch s {
case "DIR":
return FileTypeDir
case "REG":
return FileTypeFile
default:
return FileTypeUnknown
}
}
func (p *Process) fillField(s string) error {
if s == "" {
return fmt.Errorf("Empty field")
}
// See Output for Other Programs at http://linux.die.net/man/8/lsof
key := s[0]
value := s[1:]
switch key {
case 'p':
p.PID = value
case 'c':
p.Command = value
case 'u':
p.UserID = value
default:
// Skip unhandled field
}
return nil
}
func (f *FileDescriptor) fillField(s string) error {
// See Output for Other Programs at http://linux.die.net/man/8/lsof
key := s[0]
value := s[1:]
switch key {
case 't':
f.Type = fileTypeFromString(value)
case 'f':
f.FD = value
case 'n':
f.Name = value
default:
// Skip unhandled field
}
return nil
}
func (p *Process) parseFileLines(lines []string) error {
file := FileDescriptor{}
for _, line := range lines {
if strings.HasPrefix(line, "f") && file.FD != "" {
// New file
p.FileDescriptors = append(p.FileDescriptors, file)
file = FileDescriptor{}
}
err := file.fillField(line)
if err != nil {
return err
}
}
if file.FD != "" {
p.FileDescriptors = append(p.FileDescriptors, file)
}
return nil
}
func parseProcessLines(lines []string) (Process, error) {
p := Process{}
for index, line := range lines {
if strings.HasPrefix(line, "f") {
err := p.parseFileLines(lines[index:])
if err != nil {
return p, err
}
break
} else {
p.fillField(line)
}
}
return p, nil
}
func parseAppendProcessLines(processes []Process, linesChunk []string) ([]Process, []string, error) {
if len(linesChunk) == 0 {
return processes, linesChunk, nil
}
process, err := parseProcessLines(linesChunk)
if err != nil {
return processes, linesChunk, err
}
processesAfter := append(processes, process)
linesChunkAfter := []string{}
return processesAfter, linesChunkAfter, nil
}
func parse(s string) ([]Process, error) {
lines := strings.Split(s, "\n")
linesChunk := []string{}
processes := []Process{}
var err error
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
// End of process, let's parse those lines
if strings.HasPrefix(line, "p") && len(linesChunk) > 0 {
processes, linesChunk, err = parseAppendProcessLines(processes, linesChunk)
if err != nil {
return nil, err
}
}
linesChunk = append(linesChunk, line)
}
processes, _, err = parseAppendProcessLines(processes, linesChunk)
if err != nil {
return nil, err
}
return processes, nil
}
func run(args []string) ([]Process, error) {
command := "/usr/sbin/lsof"
args = append([]string{"-w"}, args...)
output, err := exec.Command(command, args...).Output()
if err != nil {
return nil, ExecError{command: command, args: args, output: string(output), err: err}
}
return parse(string(output))
}