Skip to content

Commit

Permalink
socket_listener: clean up unix socket file on start & stop (influxdat…
Browse files Browse the repository at this point in the history
  • Loading branch information
phemmer authored and calerogers committed Apr 5, 2017
1 parent 03382bf commit f64c460
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
35 changes: 32 additions & 3 deletions plugins/inputs/socket_listener/socket_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net"
"os"
"strings"
"sync"

Expand All @@ -32,7 +33,9 @@ func (ssl *streamSocketListener) listen() {
for {
c, err := ssl.Accept()
if err != nil {
ssl.AddError(err)
if !strings.HasSuffix(err.Error(), ": use of closed network connection") {
ssl.AddError(err)
}
break
}

Expand Down Expand Up @@ -78,7 +81,9 @@ func (ssl *streamSocketListener) read(c net.Conn) {
}

if err := scnr.Err(); err != nil {
ssl.AddError(err)
if !strings.HasSuffix(err.Error(), ": use of closed network connection") {
ssl.AddError(err)
}
}
}

Expand All @@ -92,7 +97,9 @@ func (psl *packetSocketListener) listen() {
for {
n, _, err := psl.ReadFrom(buf)
if err != nil {
psl.AddError(err)
if !strings.HasSuffix(err.Error(), ": use of closed network connection") {
psl.AddError(err)
}
break
}

Expand Down Expand Up @@ -170,6 +177,13 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error {
return fmt.Errorf("invalid service address: %s", sl.ServiceAddress)
}

if spl[0] == "unix" || spl[0] == "unixpacket" || spl[0] == "unixgram" {
// no good way of testing for "file does not exist".
// Instead just ignore error and blow up when we try to listen, which will
// indicate "address already in use" if file existed and we couldn't remove.
os.Remove(spl[1])
}

switch spl[0] {
case "tcp", "tcp4", "tcp6", "unix", "unixpacket":
l, err := net.Listen(spl[0], spl[1])
Expand Down Expand Up @@ -217,6 +231,10 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error {
return fmt.Errorf("unknown protocol '%s' in '%s'", spl[0], sl.ServiceAddress)
}

if spl[0] == "unix" || spl[0] == "unixpacket" || spl[0] == "unixgram" {
sl.Closer = unixCloser{path: spl[1], closer: sl.Closer}
}

return nil
}

Expand All @@ -235,6 +253,17 @@ func newSocketListener() *SocketListener {
}
}

type unixCloser struct {
path string
closer io.Closer
}

func (uc unixCloser) Close() error {
err := uc.closer.Close()
os.Remove(uc.path) // ignore error
return err
}

func init() {
inputs.Add("socket_listener", func() telegraf.Input { return newSocketListener() })
}
8 changes: 6 additions & 2 deletions plugins/inputs/socket_listener/socket_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestSocketListener_tcp(t *testing.T) {
acc := &testutil.Accumulator{}
err := sl.Start(acc)
require.NoError(t, err)
defer sl.Stop()

client, err := net.Dial("tcp", sl.Closer.(net.Listener).Addr().String())
require.NoError(t, err)
Expand All @@ -32,6 +33,7 @@ func TestSocketListener_udp(t *testing.T) {
acc := &testutil.Accumulator{}
err := sl.Start(acc)
require.NoError(t, err)
defer sl.Stop()

client, err := net.Dial("udp", sl.Closer.(net.PacketConn).LocalAddr().String())
require.NoError(t, err)
Expand All @@ -40,13 +42,14 @@ func TestSocketListener_udp(t *testing.T) {
}

func TestSocketListener_unix(t *testing.T) {
defer os.Remove("/tmp/telegraf_test.sock")
os.Create("/tmp/telegraf_test.sock")
sl := newSocketListener()
sl.ServiceAddress = "unix:///tmp/telegraf_test.sock"

acc := &testutil.Accumulator{}
err := sl.Start(acc)
require.NoError(t, err)
defer sl.Stop()

client, err := net.Dial("unix", "/tmp/telegraf_test.sock")
require.NoError(t, err)
Expand All @@ -55,13 +58,14 @@ func TestSocketListener_unix(t *testing.T) {
}

func TestSocketListener_unixgram(t *testing.T) {
defer os.Remove("/tmp/telegraf_test.sock")
os.Create("/tmp/telegraf_test.sock")
sl := newSocketListener()
sl.ServiceAddress = "unixgram:///tmp/telegraf_test.sock"

acc := &testutil.Accumulator{}
err := sl.Start(acc)
require.NoError(t, err)
defer sl.Stop()

client, err := net.Dial("unixgram", "/tmp/telegraf_test.sock")
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions plugins/outputs/socket_writer/socket_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestSocketWriter_udp(t *testing.T) {
}

func TestSocketWriter_unix(t *testing.T) {
os.Remove("/tmp/telegraf_test.sock")
defer os.Remove("/tmp/telegraf_test.sock")
listener, err := net.Listen("unix", "/tmp/telegraf_test.sock")
require.NoError(t, err)
Expand All @@ -61,6 +62,7 @@ func TestSocketWriter_unix(t *testing.T) {
}

func TestSocketWriter_unixgram(t *testing.T) {
os.Remove("/tmp/telegraf_test.sock")
defer os.Remove("/tmp/telegraf_test.sock")
listener, err := net.ListenPacket("unixgram", "/tmp/telegraf_test.sock")
require.NoError(t, err)
Expand Down

0 comments on commit f64c460

Please sign in to comment.