-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (69 loc) · 1.84 KB
/
main.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
package main
import (
"flag"
"net"
"os"
"os/signal"
"path"
"github.com/coreos/go-systemd/v22/activation"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/replit/replit-ebpf/btrfswatch"
ebpfpb "github.com/replit/replit-ebpf/ebpf"
)
func main() {
socketName := flag.String("socket-name", "", "unix socket to listen on")
logJSON := flag.Bool("log-json", false, "format log messages as JSON")
flag.Parse()
if *logJSON {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{ForceColors: true})
}
mgr, err := btrfswatch.NewManager()
if err != nil {
log.WithError(err).Fatal("Initializing btrfswatch")
}
// exit the program when interrupted.
stop := make(chan os.Signal, 5)
signal.Notify(stop, os.Interrupt)
ebpfService, err := ebpfpb.NewService(ebpfpb.ServiceOpts{
BtrfswatchMgr: mgr,
})
if err != nil {
log.WithError(err).Fatal("Starting eBPF gRPC service")
}
var listener net.Listener
if *socketName == "" {
listeners, err := activation.Listeners()
if err != nil {
log.WithError(err).Fatal("Getting systemd sockets")
}
if len(listeners) != 1 {
log.Fatalf("Wrong number of sockets, got %d", len(listeners))
}
listener = listeners[0]
} else {
err = os.MkdirAll(path.Dir(*socketName), 0o755)
if err != nil {
log.WithError(err).Fatal("Creating socket directory")
}
err = os.RemoveAll(*socketName)
if err != nil {
log.WithError(err).Fatal("Removing socket")
}
listener, err = net.Listen("unix", *socketName)
if err != nil {
log.WithError(err).Fatal("Listen on unix socket")
}
defer listener.Close()
}
log.Infof("Listening at %s...", listener.Addr().String())
grpcS := grpc.NewServer()
ebpfpb.RegisterEbpfServer(grpcS, ebpfService)
go grpcS.Serve(listener)
<-stop
log.Infoln("Shutting down...")
grpcS.GracefulStop()
mgr.Close()
}