summaryrefslogtreecommitdiff
path: root/internal/eventloop.go
blob: 287198a2c913bb8984a6cec1b129e9bba0a3706f (plain)
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
package internal

import "C"

import (
	"bytes"
	"encoding/binary"
	"fmt"

	"ioriotng/internal/syncpool"
	. "ioriotng/internal/types"

	bpf "github.com/aquasecurity/libbpfgo"
)

func eventLoop(bpfModule *bpf.Module, ch <-chan []byte) {
	enterOpen := make(map[uint32]*OpenatEnterEvent)
	enterFd := make(map[uint32]*FdEvent)
	// To do this, extract the PID from the TID (pid_tid >> 32)
	// openFiles := make(map[

	for raw := range ch {
		switch OpId(raw[0]) {
		case OPENAT_ENTER_OP_ID:
			fallthrough
		case OPEN_ENTER_OP_ID:
			ev := readRaw(raw, syncpool.OpenEnterEvent.Get().(*OpenatEnterEvent))
			enterOpen[ev.PidTGid] = ev

		case OPENAT_EXIT_OP_ID:
			fallthrough
		case OPEN_EXIT_OP_ID:
			ev := readRaw(raw, syncpool.FdEvent.Get().(*FdEvent))
			enterEv, ok := enterOpen[ev.PidTGid]
			if !ok {
				fmt.Println("Dropping", ev)
				syncpool.FdEvent.Put(ev)
				continue
			}
			duration := float64(ev.Time-enterEv.Time) / float64(1_000_000)
			fmt.Println(duration, "ms", enterEv, ev)

			delete(enterOpen, ev.PidTGid)
			syncpool.FdEvent.Put(ev)
			syncpool.OpenEnterEvent.Put(enterEv)

		case CLOSE_ENTER_OP_ID:
			fallthrough
		case WRITE_ENTER_OP_ID:
			fallthrough
		case WRITEV_ENTER_OP_ID:
			ev := readRaw(raw, syncpool.FdEvent.Get().(*FdEvent))
			enterFd[ev.PidTGid] = ev

		case CLOSE_EXIT_OP_ID:
			fallthrough
		case WRITE_EXIT_OP_ID:
			fallthrough
		case WRITEV_EXIT_OP_ID:
			ev := readRaw(raw, syncpool.NullEvent.Get().(*NullEvent))
			enterEv, ok := enterFd[ev.PidTGid]
			if !ok {
				fmt.Println("Dropping", ev)
				syncpool.NullEvent.Put(ev)
				continue
			}
			duration := float64(ev.Time-enterEv.Time) / float64(1_000_000)
			fmt.Println(duration, "ms", enterEv, ev)

			delete(enterFd, ev.PidTGid)
			syncpool.NullEvent.Put(ev)
			syncpool.FdEvent.Put(enterEv)

		default:
			panic(fmt.Sprintf("UNKNOWN Ringbuf data received len:%d raw:%v", len(raw), raw))
		}
	}

	fmt.Println("Good bye")
}

func readRaw[T any](raw []byte, ev *T) *T {
	if err := binary.Read(bytes.NewReader(raw), binary.LittleEndian, ev); err != nil {
		fmt.Println(ev, raw, len(raw), err)
		panic(raw)
	}
	return ev
}