1
0
Fork 0
forked from boyska/circolog

Allow socket activation from systemd.

This commit is contained in:
Blallo 2019-05-03 10:45:35 +02:00
parent a2de164a10
commit 3e27cad5b1
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F
2 changed files with 67 additions and 12 deletions

View file

@ -31,9 +31,10 @@ func removeAtExit(socket string) {
func main() {
var err error
var syslogSocket SyslogSocket
var logFmt formatter.SyslogRFC
logFmt.Format = syslog.Automatic
syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses")
flag.Var(&syslogSocket, "syslogd-socket", "The socket to listen to syslog addresses")
// dumpSocketPath := flag.String("dump-socket", "/run/buffer.sock", "The socket that user will connect to in order to receive logs")
bufsize := flag.Int("buffer-size", 1000, "Number of messages to keep")
syslogAddr := flag.String("syslog-addr", "127.0.0.1:9514", "Address:port where to listen for syslog messages")
@ -56,13 +57,18 @@ func main() {
server.SetFormat(logFmt.Format)
fmt.Printf("Syslog format set to: %s\n", logFmt.String())
server.SetHandler(handler)
if *syslogSocketPath != "" {
if err = server.ListenUnixgram(*syslogSocketPath); err != nil {
if syslogSocket.isSocketActivated {
fmt.Printf("Binding to socket `%s` [syslog]\n", syslogSocket.String())
server.Listen(syslogSocket.Listener)
} else {
syslogSocketPath := syslogSocket.Path
if syslogSocketPath != "" {
if err = server.ListenUnixgram(syslogSocketPath); err != nil {
fmt.Fprintln(os.Stderr, "argh", err)
os.Exit(1)
}
defer cleanSocket(*syslogSocketPath)
fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath)
fmt.Printf("Binding socket `%s` [syslog]\n", syslogSocketPath)
removeAtExit(syslogSocketPath)
} else {
fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr)
if err = server.ListenUDP(*syslogAddr); err != nil {
@ -70,6 +76,7 @@ func main() {
os.Exit(1)
}
}
}
if err = server.Boot(); err != nil {
fmt.Fprintln(os.Stderr, "argh", err)
os.Exit(1)

48
cmd/circologd/sockets.go Normal file
View file

@ -0,0 +1,48 @@
package main
import (
"net"
"github.com/coreos/go-systemd/activation"
)
// SyslogSocket is a struct eventually containing a net.Listener
// ready with messages, and a Path in case the Listener is not present.
type SyslogSocket struct {
Listener net.Listener
Path string
isSocketActivated bool
}
func (s *SyslogSocket) Set(v string) error {
sock, err := s.getActivationSocket()
if err != nil {
return err
}
s.isSocketActivated = false
if sock != nil {
s.Listener = sock
s.isSocketActivated = true
return nil
}
s.Path = v
return nil
}
func (s *SyslogSocket) String() string {
if s.isSocketActivated {
return "systemd-provided"
}
return s.Path
}
func (s *SyslogSocket) getActivationSocket() (net.Listener, error) {
listeners, err := activation.Listeners()
if err != nil {
return nil, err
}
if len(listeners) == 0 {
return nil, nil
}
return listeners[0], nil
}