| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
_ "embed" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"structs" |
| 12 |
|
| 13 |
"github.com/cilium/ebpf" |
| 14 |
) |
| 15 |
|
| 16 |
type bpfEvent struct { |
| 17 |
_ structs.HostLayout |
| 18 |
Pid uint32 |
| 19 |
Comm [16]uint8 |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
const ( |
| 26 |
bpfMapEvents = "events" |
| 27 |
bpfProgKprobeExecve = "kprobe_execve" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
func loadBpf() (*ebpf.CollectionSpec, error) { |
| 32 |
reader := bytes.NewReader(_BpfBytes) |
| 33 |
spec, err := ebpf.LoadCollectionSpecFromReader(reader) |
| 34 |
if err != nil { |
| 35 |
return nil, fmt.Errorf("can't load bpf: %w", err) |
| 36 |
} |
| 37 |
|
| 38 |
return spec, err |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
func loadBpfObjects(obj any, opts *ebpf.CollectionOptions) error { |
| 51 |
spec, err := loadBpf() |
| 52 |
if err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
|
| 56 |
return spec.LoadAndAssign(obj, opts) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
type bpfSpecs struct { |
| 63 |
bpfProgramSpecs |
| 64 |
bpfMapSpecs |
| 65 |
bpfVariableSpecs |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
type bpfProgramSpecs struct { |
| 72 |
KprobeExecve *ebpf.ProgramSpec `ebpf:"kprobe_execve"` |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
type bpfMapSpecs struct { |
| 79 |
Events *ebpf.MapSpec `ebpf:"events"` |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
type bpfVariableSpecs struct { |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
type bpfObjects struct { |
| 92 |
bpfPrograms |
| 93 |
bpfMaps |
| 94 |
bpfVariables |
| 95 |
} |
| 96 |
|
| 97 |
func (o *bpfObjects) Close() error { |
| 98 |
return _BpfClose( |
| 99 |
&o.bpfPrograms, |
| 100 |
&o.bpfMaps, |
| 101 |
) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
type bpfMaps struct { |
| 108 |
Events *ebpf.Map `ebpf:"events"` |
| 109 |
} |
| 110 |
|
| 111 |
func (m *bpfMaps) Close() error { |
| 112 |
return _BpfClose( |
| 113 |
m.Events, |
| 114 |
) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
type bpfVariables struct { |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
type bpfPrograms struct { |
| 127 |
KprobeExecve *ebpf.Program `ebpf:"kprobe_execve"` |
| 128 |
} |
| 129 |
|
| 130 |
func (p *bpfPrograms) Close() error { |
| 131 |
return _BpfClose( |
| 132 |
p.KprobeExecve, |
| 133 |
) |
| 134 |
} |
| 135 |
|
| 136 |
func _BpfClose(closers ...io.Closer) error { |
| 137 |
for _, closer := range closers { |
| 138 |
if err := closer.Close(); err != nil { |
| 139 |
return err |
| 140 |
} |
| 141 |
} |
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
var _BpfBytes []byte |