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 (
// 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.

View File

@ -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
}