agent: error if binding to existing socket file
This commit is contained in:
parent
7c6455e1ba
commit
263614d596
|
@ -22,6 +22,13 @@ const (
|
||||||
|
|
||||||
// Path to save local agent checks
|
// Path to save local agent checks
|
||||||
checksDir = "checks"
|
checksDir = "checks"
|
||||||
|
|
||||||
|
// errSocketFileExists is the human-friendly error message displayed when
|
||||||
|
// trying to bind a socket to an existing file.
|
||||||
|
errSocketFileExists = "A file exists at the requested socket path %q. " +
|
||||||
|
"If Consul was not shut down properly, the socket file may " +
|
||||||
|
"be left behind. If the path looks correct, remove the file " +
|
||||||
|
"and try again."
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -295,12 +295,11 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error if we are trying to bind a domain socket to an existing path
|
||||||
if path, ok := unixSocketAddr(config.Addresses.RPC); ok {
|
if path, ok := unixSocketAddr(config.Addresses.RPC); ok {
|
||||||
// Remove the socket if it exists, or we'll get a bind error. This
|
if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {
|
||||||
// is necessary to avoid situations where Consul cannot start if the
|
c.Ui.Output(fmt.Sprintf(errSocketFileExists, path))
|
||||||
// socket file exists in case of unexpected termination.
|
return fmt.Errorf(errSocketFileExists, path)
|
||||||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,13 +59,6 @@ func NewHTTPServers(agent *Agent, config *Config, logOutput io.Writer) ([]*HTTPS
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if path, ok := unixSocketAddr(config.Addresses.HTTPS); ok {
|
|
||||||
// See command/agent/config.go
|
|
||||||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ln, err := net.Listen(httpAddr.Network(), httpAddr.String())
|
ln, err := net.Listen(httpAddr.Network(), httpAddr.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to get Listen on %s: %v", httpAddr.String(), err)
|
return nil, fmt.Errorf("Failed to get Listen on %s: %v", httpAddr.String(), err)
|
||||||
|
@ -102,10 +95,10 @@ func NewHTTPServers(agent *Agent, config *Config, logOutput io.Writer) ([]*HTTPS
|
||||||
return nil, fmt.Errorf("Failed to get ClientListener address:port: %v", err)
|
return nil, fmt.Errorf("Failed to get ClientListener address:port: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error if we are trying to bind a domain socket to an existing path
|
||||||
if path, ok := unixSocketAddr(config.Addresses.HTTP); ok {
|
if path, ok := unixSocketAddr(config.Addresses.HTTP); ok {
|
||||||
// See command/agent/config.go
|
if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {
|
||||||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
return nil, fmt.Errorf(errSocketFileExists, path)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ func TestHTTPServer_UnixSocket(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTTPServer_UnixSocket_OverwriteFile(t *testing.T) {
|
func TestHTTPServer_UnixSocket_FileExists(t *testing.T) {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
t.SkipNow()
|
t.SkipNow()
|
||||||
}
|
}
|
||||||
|
@ -125,21 +125,18 @@ func TestHTTPServer_UnixSocket_OverwriteFile(t *testing.T) {
|
||||||
t.Fatalf("not a regular file: %s", socket)
|
t.Fatalf("not a regular file: %s", socket)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to start the server with the same path anyways.
|
conf := nextConfig()
|
||||||
dir, srv := makeHTTPServerWithConfig(t, func(c *Config) {
|
conf.Addresses.HTTP = "unix://" + socket
|
||||||
c.Addresses.HTTP = "unix://" + socket
|
|
||||||
})
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
defer srv.Shutdown()
|
|
||||||
defer srv.agent.Shutdown()
|
|
||||||
|
|
||||||
// Check if the socket overwrote the file
|
dir, agent := makeAgent(t, conf)
|
||||||
fi, err = os.Stat(socket)
|
defer os.RemoveAll(dir)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %s", err)
|
// Try to start the server with the same path anyways.
|
||||||
}
|
if servers, err := NewHTTPServers(agent, conf, agent.logOutput); err == nil {
|
||||||
if fi.Mode().IsRegular() {
|
for _, server := range servers {
|
||||||
t.Fatalf("should have socket file: %s", socket)
|
server.Shutdown()
|
||||||
|
}
|
||||||
|
t.Fatalf("expected socket binding error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue