48 lines
921 B
Go
48 lines
921 B
Go
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
|
|
}
|