-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
115 lines (94 loc) · 2.42 KB
/
collector.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
//go:build windows
// +build windows
package winproc
import (
"strings"
"sync"
"github.com/gentlemanautomaton/cmdline/cmdlinewindows"
)
// A Collector is a collection option that collects additional information
// about a process.
//
// Information will only be collected for processes that have not been
// excluded by previous filtering options.
type Collector int
const (
// CollectCommands is an option that enables collection of process
// command line, path and argument information.
CollectCommands Collector = 1 << iota
// CollectSessions is an option that enables collection of process
// session information.
CollectSessions
// CollectUsers is an option that enables collection of process
// user information.
CollectUsers
// CollectTimes is an option that enables collection of process
// time information.
CollectTimes
// CollectCriticality is an option that enables collection of process
// criticality information.
CollectCriticality
)
// Contains returns true if c contains b.
func (c Collector) Contains(b Collector) bool {
return c&b == b
}
// Apply applies the collector to the collection.
func (c Collector) Apply(col *Collection) {
if c == 0 {
return
}
var wg sync.WaitGroup
wg.Add(len(col.Procs))
for i := range col.Procs {
if col.Excluded[i] {
wg.Done()
continue
}
go func(i int) {
defer wg.Done()
proc := &col.Procs[i]
ref, err := Open(proc.ID)
if err != nil {
return
}
defer ref.Close()
if c.Contains(CollectCommands) {
if line, err := ref.CommandLine(); err == nil {
proc.CommandLine = strings.TrimSpace(line)
proc.Path, proc.Args = cmdlinewindows.SplitCommand(line)
}
}
if c.Contains(CollectSessions) {
if sessionID, err := ref.SessionID(); err == nil {
proc.SessionID = sessionID
}
}
if c.Contains(CollectUsers) {
if user, err := ref.User(); err == nil {
proc.User = user
}
}
if c.Contains(CollectTimes) {
if times, err := ref.Times(); err == nil {
proc.Times = times
}
}
if c.Contains(CollectCriticality) {
if critical, err := ref.Critical(); err == nil {
proc.Critical = critical
}
}
}(i)
}
wg.Wait()
}
// Merge attempts to merge the collector with the next option. It returns true
// if successful.
func (c Collector) Merge(next CollectionOption) (merged CollectionOption, ok bool) {
n, ok := next.(Collector)
if !ok {
return nil, false
}
return c | n, true
}