systemd socket activation compatibility

This commit is contained in:
Naitik Shah 2012-09-02 21:21:19 -07:00
parent c5f3993bce
commit 5540a9b5d3
2 changed files with 18 additions and 6 deletions

View File

@ -25,7 +25,7 @@ var (
const ( const (
// Used to indicate a graceful restart in the new process. // 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. // The error returned by the standard library when the socket is closed.
errClosed = "use of closed network connection" errClosed = "use of closed network connection"
@ -132,6 +132,9 @@ func Wait(listeners []Listener) (err error) {
sig := <-ch sig := <-ch
switch sig { switch sig {
case syscall.SIGTERM: case syscall.SIGTERM:
if os.Getppid() == 1 { // init provided sockets dont close
return
}
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(len(listeners)) wg.Add(len(listeners))
for _, l := range 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 // Start the Close process in the parent. This does not wait for the
// parent to close and simply sends it the TERM signal. // parent to close and simply sends it the TERM signal.
func CloseParent() error { 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. // Restart the process passing the given listeners to the new process.

View File

@ -83,9 +83,14 @@ func Serve(givenHandlers ...Handler) error {
return fmt.Errorf("Failed to close parent: %s", err) return fmt.Errorf("Failed to close parent: %s", err)
} }
if *verbose { if *verbose {
ppid := os.Getppid()
if ppid == 1 {
log.Printf("Listening on init activated %s", pprintAddr(listeners))
} else {
log.Printf( log.Printf(
"Graceful handoff of %s with new pid %d and old pid %d.", "Graceful handoff of %s with new pid %d and old pid %d.",
pprintAddr(listeners), os.Getpid(), os.Getppid()) pprintAddr(listeners), os.Getpid(), ppid)
}
} }
} else if err == grace.ErrNotInheriting { } else if err == grace.ErrNotInheriting {
listeners, err = handlers.newListeners() listeners, err = handlers.newListeners()
@ -103,7 +108,7 @@ func Serve(givenHandlers ...Handler) error {
return err return err
} }
if *verbose { if *verbose {
return fmt.Errorf("Exiting pid %d.", os.Getpid()) log.Printf("Exiting pid %d.", os.Getpid())
} }
return nil return nil
} }