Allow socket activation from systemd.
This commit is contained in:
parent
a2de164a10
commit
3e27cad5b1
2 changed files with 67 additions and 12 deletions
|
@ -31,9 +31,10 @@ func removeAtExit(socket string) {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
|
var syslogSocket SyslogSocket
|
||||||
var logFmt formatter.SyslogRFC
|
var logFmt formatter.SyslogRFC
|
||||||
logFmt.Format = syslog.Automatic
|
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")
|
// 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")
|
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")
|
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)
|
server.SetFormat(logFmt.Format)
|
||||||
fmt.Printf("Syslog format set to: %s\n", logFmt.String())
|
fmt.Printf("Syslog format set to: %s\n", logFmt.String())
|
||||||
server.SetHandler(handler)
|
server.SetHandler(handler)
|
||||||
if *syslogSocketPath != "" {
|
if syslogSocket.isSocketActivated {
|
||||||
if err = server.ListenUnixgram(*syslogSocketPath); err != nil {
|
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)
|
fmt.Fprintln(os.Stderr, "argh", err)
|
||||||
os.Exit(1)
|
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 {
|
} else {
|
||||||
fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr)
|
fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr)
|
||||||
if err = server.ListenUDP(*syslogAddr); err != nil {
|
if err = server.ListenUDP(*syslogAddr); err != nil {
|
||||||
|
@ -70,6 +76,7 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if err = server.Boot(); err != nil {
|
if err = server.Boot(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "argh", err)
|
fmt.Fprintln(os.Stderr, "argh", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
48
cmd/circologd/sockets.go
Normal file
48
cmd/circologd/sockets.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue