diff --git a/grace.go b/grace.go index fd1b0b6..c9925c8 100644 --- a/grace.go +++ b/grace.go @@ -25,7 +25,7 @@ var ( const ( // Used to indicate a graceful restart in the new process. - envCountKey = "GRACE" + envCountKey = "LISTEN_FDS" // The error returned by the standard library when the socket is closed. errClosed = "use of closed network connection" @@ -132,6 +132,9 @@ func Wait(listeners []Listener) (err error) { sig := <-ch switch sig { case syscall.SIGTERM: + if os.Getppid() == 1 { // init provided sockets dont close + return + } var wg sync.WaitGroup wg.Add(len(listeners)) for _, l := range listeners { @@ -182,7 +185,11 @@ func Inherit() (listeners []Listener, err error) { // Start the Close process in the parent. This does not wait for the // parent to close and simply sends it the TERM signal. func CloseParent() error { - return syscall.Kill(os.Getppid(), syscall.SIGTERM) + ppid := os.Getppid() + if ppid == 1 { // init provided sockets, for example systemd + return nil + } + return syscall.Kill(ppid, syscall.SIGTERM) } // Restart the process passing the given listeners to the new process. diff --git a/gracehttp/http.go b/gracehttp/http.go index d209c17..1beb96a 100644 --- a/gracehttp/http.go +++ b/gracehttp/http.go @@ -83,9 +83,14 @@ func Serve(givenHandlers ...Handler) error { return fmt.Errorf("Failed to close parent: %s", err) } if *verbose { - log.Printf( - "Graceful handoff of %s with new pid %d and old pid %d.", - pprintAddr(listeners), os.Getpid(), os.Getppid()) + ppid := os.Getppid() + if ppid == 1 { + log.Printf("Listening on init activated %s", pprintAddr(listeners)) + } else { + log.Printf( + "Graceful handoff of %s with new pid %d and old pid %d.", + pprintAddr(listeners), os.Getpid(), ppid) + } } } else if err == grace.ErrNotInheriting { listeners, err = handlers.newListeners() @@ -103,7 +108,7 @@ func Serve(givenHandlers ...Handler) error { return err } if *verbose { - return fmt.Errorf("Exiting pid %d.", os.Getpid()) + log.Printf("Exiting pid %d.", os.Getpid()) } return nil }