github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadget-collection/gadgets/trace/mount/gadget.go (about) 1 // Copyright 2022 The Inspektor Gadget authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mountsnoop 16 17 import ( 18 "encoding/json" 19 "fmt" 20 21 log "github.com/sirupsen/logrus" 22 23 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-collection/gadgets" 24 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-collection/gadgets/trace" 25 26 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/mount/tracer" 27 28 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/mount/types" 29 30 gadgetv1alpha1 "github.com/inspektor-gadget/inspektor-gadget/pkg/apis/gadget/v1alpha1" 31 ) 32 33 type Trace struct { 34 helpers gadgets.GadgetHelpers 35 36 started bool 37 tracer trace.Tracer 38 } 39 40 type TraceFactory struct { 41 gadgets.BaseFactory 42 } 43 44 func NewFactory() gadgets.TraceFactory { 45 return &TraceFactory{ 46 BaseFactory: gadgets.BaseFactory{DeleteTrace: deleteTrace}, 47 } 48 } 49 50 func (f *TraceFactory) Description() string { 51 return `mountsnoop traces mount and umount syscalls` 52 } 53 54 func (f *TraceFactory) OutputModesSupported() map[gadgetv1alpha1.TraceOutputMode]struct{} { 55 return map[gadgetv1alpha1.TraceOutputMode]struct{}{ 56 gadgetv1alpha1.TraceOutputModeStream: {}, 57 } 58 } 59 60 func deleteTrace(name string, t interface{}) { 61 trace := t.(*Trace) 62 if trace.tracer != nil { 63 trace.tracer.Stop() 64 } 65 } 66 67 func (f *TraceFactory) Operations() map[gadgetv1alpha1.Operation]gadgets.TraceOperation { 68 n := func() interface{} { 69 return &Trace{ 70 helpers: f.Helpers, 71 } 72 } 73 74 return map[gadgetv1alpha1.Operation]gadgets.TraceOperation{ 75 gadgetv1alpha1.OperationStart: { 76 Doc: "Start mountsnoop gadget", 77 Operation: func(name string, trace *gadgetv1alpha1.Trace) { 78 f.LookupOrCreate(name, n).(*Trace).Start(trace) 79 }, 80 }, 81 gadgetv1alpha1.OperationStop: { 82 Doc: "Stop mountsnoop gadget", 83 Operation: func(name string, trace *gadgetv1alpha1.Trace) { 84 f.LookupOrCreate(name, n).(*Trace).Stop(trace) 85 }, 86 }, 87 } 88 } 89 90 func (t *Trace) Start(trace *gadgetv1alpha1.Trace) { 91 if t.started { 92 trace.Status.State = gadgetv1alpha1.TraceStateStarted 93 return 94 } 95 96 traceName := gadgets.TraceName(trace.ObjectMeta.Namespace, trace.ObjectMeta.Name) 97 98 eventCallback := func(event *types.Event) { 99 r, err := json.Marshal(event) 100 if err != nil { 101 log.Warnf("Gadget %s: error marshaling event: %s", trace.Spec.Gadget, err) 102 return 103 } 104 t.helpers.PublishEvent(traceName, string(r)) 105 } 106 107 var err error 108 109 mountNsMap, err := t.helpers.TracerMountNsMap(traceName) 110 if err != nil { 111 trace.Status.OperationError = fmt.Sprintf("failed to find tracer's mount ns map: %s", err) 112 return 113 } 114 config := &tracer.Config{ 115 MountnsMap: mountNsMap, 116 } 117 t.tracer, err = tracer.NewTracer(config, t.helpers, eventCallback) 118 if err != nil { 119 trace.Status.OperationError = fmt.Sprintf("failed to create tracer: %s", err) 120 return 121 } 122 123 t.started = true 124 125 trace.Status.State = gadgetv1alpha1.TraceStateStarted 126 } 127 128 func (t *Trace) Stop(trace *gadgetv1alpha1.Trace) { 129 if !t.started { 130 trace.Status.OperationError = "Not started" 131 return 132 } 133 134 t.tracer.Stop() 135 t.tracer = nil 136 t.started = false 137 138 trace.Status.State = gadgetv1alpha1.TraceStateStopped 139 }