-
Notifications
You must be signed in to change notification settings - Fork 189
/
gadget.go
138 lines (113 loc) · 3.56 KB
/
gadget.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
// Copyright 2019-2022 The Inspektor Gadget authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package execsnoop
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-collection/gadgets"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-collection/gadgets/trace"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/exec/tracer"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/exec/types"
gadgetv1alpha1 "github.com/inspektor-gadget/inspektor-gadget/pkg/apis/gadget/v1alpha1"
)
type Trace struct {
helpers gadgets.GadgetHelpers
started bool
tracer trace.Tracer
}
type TraceFactory struct {
gadgets.BaseFactory
}
func NewFactory() gadgets.TraceFactory {
return &TraceFactory{
BaseFactory: gadgets.BaseFactory{DeleteTrace: deleteTrace},
}
}
func (f *TraceFactory) Description() string {
return `execsnoop shows new created processes, with container details.`
}
func (f *TraceFactory) OutputModesSupported() map[gadgetv1alpha1.TraceOutputMode]struct{} {
return map[gadgetv1alpha1.TraceOutputMode]struct{}{
gadgetv1alpha1.TraceOutputModeStream: {},
}
}
func deleteTrace(name string, t interface{}) {
trace := t.(*Trace)
if trace.tracer != nil {
trace.tracer.Stop()
}
}
func (f *TraceFactory) Operations() map[gadgetv1alpha1.Operation]gadgets.TraceOperation {
n := func() interface{} {
return &Trace{
helpers: f.Helpers,
}
}
return map[gadgetv1alpha1.Operation]gadgets.TraceOperation{
gadgetv1alpha1.OperationStart: {
Doc: "Start execsnoop gadget",
Operation: func(name string, trace *gadgetv1alpha1.Trace) {
f.LookupOrCreate(name, n).(*Trace).Start(trace)
},
},
gadgetv1alpha1.OperationStop: {
Doc: "Stop execsnoop gadget",
Operation: func(name string, trace *gadgetv1alpha1.Trace) {
f.LookupOrCreate(name, n).(*Trace).Stop(trace)
},
},
}
}
func (t *Trace) Start(trace *gadgetv1alpha1.Trace) {
if t.started {
trace.Status.State = gadgetv1alpha1.TraceStateStarted
return
}
traceName := gadgets.TraceName(trace.ObjectMeta.Namespace, trace.ObjectMeta.Name)
eventCallback := func(event *types.Event) {
r, err := json.Marshal(event)
if err != nil {
log.Warnf("Gadget %s: error marshaling event: %s", trace.Spec.Gadget, err)
return
}
t.helpers.PublishEvent(traceName, string(r))
}
var err error
mountNsMap, err := t.helpers.TracerMountNsMap(traceName)
if err != nil {
trace.Status.OperationError = fmt.Sprintf("failed to find tracer's mount ns map: %s", err)
return
}
config := &tracer.Config{
MountnsMap: mountNsMap,
}
t.tracer, err = tracer.NewTracer(config, t.helpers, eventCallback)
if err != nil {
trace.Status.OperationError = fmt.Sprintf("failed to create tracer: %s", err)
return
}
t.started = true
trace.Status.State = gadgetv1alpha1.TraceStateStarted
}
func (t *Trace) Stop(trace *gadgetv1alpha1.Trace) {
if !t.started {
trace.Status.OperationError = "Not started"
return
}
t.tracer.Stop()
t.tracer = nil
t.started = false
trace.Status.State = gadgetv1alpha1.TraceStateStopped
}