-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (61 loc) · 1.86 KB
/
main.go
File metadata and controls
74 lines (61 loc) · 1.86 KB
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
package main
import (
"context"
"errors"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/cilium/ebpf/ringbuf"
containerd "github.com/containerd/containerd/v2/client"
"github.com/vanstee/exec-record/asciicast"
"github.com/vanstee/exec-record/config"
"github.com/vanstee/exec-record/ebpf"
"github.com/vanstee/exec-record/informers"
)
func main() {
// TODO: Validate config, directories exist, etc
config := config.Parse()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
client, err := containerd.New(config.ContainerdRuntimeEndpoint)
if err != nil {
log.Fatalf("failed to create containerd client: %v", err)
}
defer client.Close()
informer, err := informers.NewContainerdInformer(client)
if err != nil {
log.Fatalf("failed to initalize containerd informer: %v", err)
}
if err := informer.Start(ctx); err != nil {
log.Fatalf("failed to start containerd informer: %v", err)
}
log.Printf("started containerd informer")
watcher, err := ebpf.StartWatcher(ctx)
if err != nil {
log.Fatalf("failed to start exec io watcher: %v", err)
}
defer watcher.Close()
log.Printf("started ebpf event watcher")
for {
event, err := watcher.Read()
if err != nil && errors.Is(err, ringbuf.ErrClosed) {
log.Printf("ringbuf closed, exiting")
return
} else if err != nil {
log.Printf("failed to read from ringbuf: %v", err)
continue
}
ok, containerID, execID := informer.MatchingTask(event.TtyNr)
if !ok {
continue
}
recordingPath := config.RecordingsDir + "/" + containerID + "-" + execID + ".cast"
since := time.Duration(event.Since)
if err := asciicast.AppendEventToRecording(recordingPath, since.Seconds(), event.Buf, event.Pid); err != nil {
log.Printf("failed to record event: %v", err)
}
log.Printf("event: %b %10d %10d %q", event.TtyNr, event.Pid, event.Ns, event.Buf)
}
}