1- package signal // import "github.com/docker/docker/pkg/signal "
1+ package trap // import "github.com/docker/docker/cmd/dockerd/trap "
22
33import (
44 "fmt"
55 "os"
66 gosignal "os/signal"
7- "path/filepath"
8- "runtime"
9- "strings"
107 "sync/atomic"
118 "syscall"
12- "time"
139
14- "github.com/pkg/errors "
10+ "github.com/docker/docker/ pkg/stack "
1511)
1612
1713// Trap sets up a simplified signal "trap", appropriate for common
@@ -58,7 +54,7 @@ func Trap(cleanup func(), logger interface {
5854 logger .Info ("Forcing docker daemon shutdown without cleanup; 3 interrupts received" )
5955 }
6056 case syscall .SIGQUIT :
61- DumpStacks ( "" )
57+ stack . Dump ( )
6258 logger .Info ("Forcing docker daemon shutdown without cleanup on SIGQUIT" )
6359 }
6460 // for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal #
@@ -67,38 +63,3 @@ func Trap(cleanup func(), logger interface {
6763 }
6864 }()
6965}
70-
71- const stacksLogNameTemplate = "goroutine-stacks-%s.log"
72-
73- // DumpStacks appends the runtime stack into file in dir and returns full path
74- // to that file.
75- func DumpStacks (dir string ) (string , error ) {
76- var (
77- buf []byte
78- stackSize int
79- )
80- bufferLen := 16384
81- for stackSize == len (buf ) {
82- buf = make ([]byte , bufferLen )
83- stackSize = runtime .Stack (buf , true )
84- bufferLen *= 2
85- }
86- buf = buf [:stackSize ]
87- var f * os.File
88- if dir != "" {
89- path := filepath .Join (dir , fmt .Sprintf (stacksLogNameTemplate , strings .Replace (time .Now ().Format (time .RFC3339 ), ":" , "" , - 1 )))
90- var err error
91- f , err = os .OpenFile (path , os .O_CREATE | os .O_WRONLY , 0666 )
92- if err != nil {
93- return "" , errors .Wrap (err , "failed to open file to write the goroutine stacks" )
94- }
95- defer f .Close ()
96- defer f .Sync ()
97- } else {
98- f = os .Stderr
99- }
100- if _ , err := f .Write (buf ); err != nil {
101- return "" , errors .Wrap (err , "failed to write goroutine stacks" )
102- }
103- return f .Name (), nil
104- }
0 commit comments