From 0c0e9836c4bed0190718b0ddd0028790b19c200c Mon Sep 17 00:00:00 2001 From: Rik Nijessen Date: Sat, 28 Feb 2015 16:55:59 +0100 Subject: [PATCH] Implement cleanup unix sockets after serving. Signed-off-by: Rik Nijessen --- api/server/server.go | 5 +++++ api/server/server_linux.go | 26 ++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/api/server/server.go b/api/server/server.go index d244d2a0ce..72cd992697 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -1578,6 +1578,11 @@ func ServeApi(job *engine.Job) engine.Status { chErrors <- err return } + job.Eng.OnShutdown(func() { + if err := srv.Close(); err != nil { + log.Errorf("%s", err.Error()) + } + }) chErrors <- srv.Serve() }() } diff --git a/api/server/server_linux.go b/api/server/server_linux.go index fff803ddaf..217c051c17 100644 --- a/api/server/server_linux.go +++ b/api/server/server_linux.go @@ -10,8 +10,30 @@ import ( "github.com/docker/docker/engine" "github.com/docker/docker/pkg/systemd" + "net" ) +type UnixHttpServer struct { + srv *http.Server + l net.Listener +} + +func (s *UnixHttpServer) Serve() error { + return s.srv.Serve(s.l) +} +func (s *UnixHttpServer) Close() error { + if err := s.l.Close(); err != nil { + return err + } + if _, err := os.Stat(s.srv.Addr); err != nil { + return fmt.Errorf("Error removing unix socket %s: %s", s.srv.Addr, err.Error()) + } + if err := os.Remove(s.srv.Addr); err != nil { + return fmt.Errorf("Error removing unix socket %s: %s", s.srv.Addr, err.Error()) + } + return nil +} + // NewServer sets up the required Server and does protocol specific checking. func NewServer(proto, addr string, job *engine.Job) (Server, error) { // Basic error and sanity checking @@ -27,7 +49,7 @@ func NewServer(proto, addr string, job *engine.Job) (Server, error) { } } -func setupUnixHttp(addr string, job *engine.Job) (*HttpServer, error) { +func setupUnixHttp(addr string, job *engine.Job) (*UnixHttpServer, error) { r := createRouter(job.Eng, job.GetenvBool("Logging"), job.GetenvBool("EnableCors"), job.Getenv("CorsHeaders"), job.Getenv("Version")) if err := syscall.Unlink(addr); err != nil && !os.IsNotExist(err) { @@ -49,7 +71,7 @@ func setupUnixHttp(addr string, job *engine.Job) (*HttpServer, error) { return nil, err } - return &HttpServer{&http.Server{Addr: addr, Handler: r}, l}, nil + return &UnixHttpServer{&http.Server{Addr: addr, Handler: r}, l}, nil } // serveFd creates an http.Server and sets it up to serve given a socket activated