Add support for UDP routing in systemd socket activation

This commit is contained in:
tsiid 2025-01-21 11:38:09 +03:00 committed by GitHub
parent 95dd17e020
commit 261e4395f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 158 additions and 54 deletions

View file

@ -9,16 +9,36 @@ import (
"github.com/rs/zerolog/log"
)
func populateSocketActivationListeners() {
listenersWithName, _ := activation.ListenersWithNames()
func populateSocketActivationListeners() *SocketActivation {
// We use Files api due to activation not providing method for get PacketConn with names
files := activation.Files(true)
sa := &SocketActivation{enabled: false}
sa.listeners = make(map[string]net.Listener)
sa.conns = make(map[string]net.PacketConn)
socketActivationListeners = make(map[string]net.Listener)
for name, lns := range listenersWithName {
if len(lns) != 1 {
log.Error().Str("listenersName", name).Msg("Socket activation listeners must have one and only one listener per name")
continue
if len(files) > 0 {
sa.enabled = true
for _, f := range files {
if lc, err := net.FileListener(f); err == nil {
_, ok := sa.listeners[f.Name()]
if ok {
log.Error().Str("listenersName", f.Name()).Msg("Socket activation TCP listeners must have one and only one listener per name")
} else {
sa.listeners[f.Name()] = lc
}
f.Close()
} else if pc, err := net.FilePacketConn(f); err == nil {
_, ok := sa.conns[f.Name()]
if ok {
log.Error().Str("listenersName", f.Name()).Msg("Socket activation UDP listeners must have one and only one listener per name")
} else {
sa.conns[f.Name()] = pc
}
f.Close()
}
}
socketActivationListeners[name] = lns[0]
}
return sa
}