cmd/dockerd: gracefully shut down the API server
- Relates to #45057 

<!--
Please make sure you've read and understood our contributing guidelines;
https://github.com/moby/moby/blob/master/CONTRIBUTING.md

** Make sure all your commits include a signature generated with `git commit -s` **

For additional information on our contributing process, read our contributing
guide https://docs.docker.com/opensource/code/

If this is a bug fix, make sure your description includes "fixes #xxxx", or
"closes #xxxx"

Please provide the following information:
-->

**- What I did**
Gave in-flight API requests up to five seconds to complete after all containers have been shut down before the daemon process exits.

**- How I did it**
By leveraging bleeding-edge APIs...from Go 1.8.

**- How to verify it**

**- Description for the changelog**
<!--
Write a short (one line) summary that describes the changes in this
pull request for inclusion in the changelog:
-->


**- A picture of a cute animal (not mandatory but encouraged)**


cmd/dockerd/trap/trap.go
package trap // import "github.com/docker/docker/cmd/dockerd/trap"

import (
	"fmt"
	"os"
	"os/signal"
	"sync/atomic"
	"syscall"
)

// Trap sets up a simplified signal "trap", appropriate for common
// behavior expected from a vanilla unix command-line tool in general
// (and the Docker engine in particular).
//
//   - If SIGINT or SIGTERM are received, `cleanup` is called, then the process is terminated.
//   - If SIGINT or SIGTERM are received 3 times before cleanup is complete, then cleanup is
//     skipped and the process is terminated immediately (allows force quit of stuck daemon)
//   - Ignore SIGPIPE events. These are generated by systemd when journald is restarted while
//     the docker daemon is not restarted and also running under systemd.
//     Fixes https://github.com/docker/docker/issues/19728
func Trap(cleanup func(), logger interface {
	Info(args ...interface{})
}) {
	c := make(chan os.Signal, 1)
	// we will handle INT, TERM, SIGPIPE here
	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGPIPE)
	go func() {
		interruptCount := uint32(0)
		for sig := range c {
			if sig == syscall.SIGPIPE {
				continue
			}

			go func(sig os.Signal) {
				logger.Info(fmt.Sprintf("Processing signal '%v'", sig))
				switch sig {
				case os.Interrupt, syscall.SIGTERM:
					if atomic.LoadUint32(&interruptCount) < 3 {
						// Initiate the cleanup only once
						if atomic.AddUint32(&interruptCount, 1) == 1 {
							// Call the provided cleanup handler
							cleanup()
							os.Exit(0)
						} else {
							return
						}
					} else {
						// 3 SIGTERM/INT signals received; force exit without cleanup
						logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
					}
				}
				// for the SIGINT/TERM non-clean shutdown case, exit with 128 + signal #
				os.Exit(128 + int(sig.(syscall.Signal)))
			}(sig)
		}
	}()
}

package trap // import "github.com/docker/docker/cmd/dockerd/trap"

import (
	"os"
	"os/signal"
	"syscall"

	"github.com/sirupsen/logrus"
)

const (
	// Immediately terminate the process when this many SIGINT or SIGTERM
	// signals are received.
	forceQuitCount = 3
)

// Trap sets up a simplified signal "trap", appropriate for common
// behavior expected from a vanilla unix command-line tool in general
// (and the Docker engine in particular).
//
// The first time a SIGINT or SIGTERM signal is received, `cleanup` is called in
// a new goroutine.
//
// If SIGINT or SIGTERM are received 3 times, the process is terminated
// immediately with an exit code of 128 + the signal number.
func Trap(cleanup func()) {
	c := make(chan os.Signal, forceQuitCount)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
	go func() {
		var interruptCount int
		for sig := range c {
			logrus.Infof("Processing signal '%v'", sig)
			if interruptCount < forceQuitCount {
				interruptCount++
				// Initiate the cleanup only once
				if interruptCount == 1 {
					go cleanup()
				}
				continue
			}

			logrus.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
			os.Exit(128 + int(sig.(syscall.Signal)))
		}
	}()
}

I see the comment was removed;

```
// for the SIGINT/TERM non-clean shutdown case, exit with 128 + signal #
```

I guess the comment is not "too" useful in the code, but perhaps it's useful to include in the GoDoc of the function itself.