circolog/cmd/circologd/sockets.go

51 lines
1,000 B
Go
Raw Normal View History

2019-05-03 10:45:35 +02:00
package main
import (
"net"
)
// 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
2019-05-03 12:04:04 +02:00
Conn net.PacketConn
2019-05-03 10:45:35 +02:00
Path string
isSocketActivated bool
}
2019-05-03 12:04:04 +02:00
// Set from command-line
2019-05-03 10:45:35 +02:00
func (s *SyslogSocket) Set(v string) error {
2019-05-03 12:04:04 +02:00
err := s.getActivationSocket()
if err == nil && (s.Conn != nil || s.Listener != nil) {
2019-05-03 10:45:35 +02:00
s.isSocketActivated = true
}
2019-05-03 12:04:04 +02:00
if !s.isSocketActivated {
s.Path = v
}
2019-05-03 10:45:35 +02:00
return nil
}
func (s *SyslogSocket) String() string {
if s.isSocketActivated {
return "systemd-provided"
}
return s.Path
}
2019-05-03 12:04:04 +02:00
func (s *SyslogSocket) getActivationSocket() error {
conns, err := PacketConns()
if err == nil && len(conns) > 0 && conns[0] != nil {
s.Conn = conns[0]
return nil
}
listeners, err := Listeners()
2019-05-03 10:45:35 +02:00
if err != nil {
2019-05-03 12:04:04 +02:00
return err
2019-05-03 10:45:35 +02:00
}
if len(listeners) == 0 {
2019-05-03 12:04:04 +02:00
return nil
2019-05-03 10:45:35 +02:00
}
2019-05-03 12:04:04 +02:00
s.Listener = listeners[0]
return nil
2019-05-03 10:45:35 +02:00
}