diff --git a/api.go b/api.go index 32e05a9066..0aa9695702 100644 --- a/api.go +++ b/api.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/auth" + "github.com/dotcloud/docker/systemd" "github.com/dotcloud/docker/utils" "github.com/gorilla/mux" "io" @@ -1184,8 +1185,6 @@ func ServeRequest(srv *Server, apiversion float64, w http.ResponseWriter, req *h } func ListenAndServe(proto, addr string, srv *Server, logging bool) error { - log.Printf("Listening for HTTP on %s (%s)\n", addr, proto) - r, err := createRouter(srv, logging) if err != nil { return err @@ -1216,5 +1215,9 @@ func ListenAndServe(proto, addr string, srv *Server, logging bool) error { } } httpSrv := http.Server{Addr: addr, Handler: r} + + log.Printf("Listening for HTTP on %s (%s)\n", addr, proto) + // Tell the init daemon we are accepting requests + go systemd.SdNotify("READY=1") return httpSrv.Serve(l) } diff --git a/systemd/sd_notify.go b/systemd/sd_notify.go new file mode 100644 index 0000000000..1993cab9be --- /dev/null +++ b/systemd/sd_notify.go @@ -0,0 +1,33 @@ +package systemd + +import ( + "errors" + "net" + "os" +) + +var SdNotifyNoSocket = errors.New("No socket") + +// Send a message to the init daemon. It is common to ignore the error. +func SdNotify(state string) error { + socketAddr := &net.UnixAddr{ + Name: os.Getenv("NOTIFY_SOCKET"), + Net: "unixgram", + } + + if socketAddr.Name == "" { + return SdNotifyNoSocket + } + + conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr) + if err != nil { + return err + } + + _, err = conn.Write([]byte(state)) + if err != nil { + return err + } + + return nil +}