Skip to content

Commit

Permalink
fix(inputs.chrony): use DGRAM for the unix socket
Browse files Browse the repository at this point in the history
We must use the DGRAM type when connecting to the chrony socket. On top
of that, the socket must be duplicated in order to allow concurrent
calls to it.

Fixes influxdata#15549

Signed-off-by: Frank Villaro-Dixon <[email protected]>
  • Loading branch information
Frankkkkk committed Jun 21, 2024
1 parent 78cbf53 commit bc941e9
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions plugins/inputs/chrony/chrony.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"net"
"net/url"
"os"
"path"
"strconv"
"syscall"
"time"
Expand Down Expand Up @@ -38,6 +40,23 @@ func (*Chrony) SampleConfig() string {
return sampleConfig
}

// dialUnix opens a unixgram connection with chrony
func dialUnix(address string) (*net.UnixConn, error) {
base, _ := path.Split(address)
local := path.Join(base, fmt.Sprintf("chronyc.%d.sock", os.Getpid()))
conn, err := net.DialUnix("unixgram",
&net.UnixAddr{Name: local, Net: "unixgram"},
&net.UnixAddr{Name: address, Net: "unixgram"},
)
if err != nil {
return nil, err
}
if err := os.Chmod(local, 0666); err != nil {
return nil, err
}
return conn, nil
}

func (c *Chrony) Init() error {
// Use the configured server, if none set, we try to guess it in Start()
if c.Server != "" {
Expand All @@ -48,6 +67,7 @@ func (c *Chrony) Init() error {
}
switch u.Scheme {
case "unix":
case "unixdram":
// Keep the server unmodified
case "udp":
// Check if we do have a port and add the default port if we don't
Expand Down Expand Up @@ -90,7 +110,10 @@ func (c *Chrony) Start(_ telegraf.Accumulator) error {
}
switch u.Scheme {
case "unix":
conn, err := net.DialTimeout("unix", u.Path, time.Duration(c.Timeout))
// Actually we need a unixgram connection, allow this typo
// FALLTHROUGH
case "unixgram":
conn, err := dialUnix(u.Path)
if err != nil {
return fmt.Errorf("dialing %q failed: %w", c.Server, err)
}
Expand All @@ -106,7 +129,7 @@ func (c *Chrony) Start(_ telegraf.Accumulator) error {
}
} else {
// If no server is given, reproduce chronyc's behavior
if conn, err := net.DialTimeout("unix", "/run/chrony/chronyd.sock", time.Duration(c.Timeout)); err == nil {
if conn, err := dialUnix("/run/chrony/chronyd.sock"); err == nil {
c.Server = "unix:///run/chrony/chronyd.sock"
c.conn = conn
} else if conn, err := net.DialTimeout("udp", "127.0.0.1:323", time.Duration(c.Timeout)); err == nil {
Expand Down

0 comments on commit bc941e9

Please sign in to comment.