From 6057481984ebf61e057e7679a89d24b85d777dbf Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 10 Mar 2014 13:38:17 -0700 Subject: [PATCH 01/60] Move signal to pkg Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes (github: creack) --- signal/signal.go | 11 +++++ signal/signal_darwin.go | 44 ++++++++++++++++++++ signal/signal_freebsd.go | 42 +++++++++++++++++++ signal/signal_linux.go | 87 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 signal/signal.go create mode 100644 signal/signal_darwin.go create mode 100644 signal/signal_freebsd.go create mode 100644 signal/signal_linux.go diff --git a/signal/signal.go b/signal/signal.go new file mode 100644 index 00000000..6f9874bd --- /dev/null +++ b/signal/signal.go @@ -0,0 +1,11 @@ +package signal + +import ( + "os" + "os/signal" +) + +func StopCatch(sigc chan os.Signal) { + signal.Stop(sigc) + close(sigc) +} diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go new file mode 100644 index 00000000..22a60ae1 --- /dev/null +++ b/signal/signal_darwin.go @@ -0,0 +1,44 @@ +package signal + +import ( + "os" + "os/signal" + "syscall" +) + +func CatchAll(sigc chan os.Signal) { + signal.Notify(sigc, + syscall.SIGABRT, + syscall.SIGALRM, + syscall.SIGBUS, + syscall.SIGCHLD, + syscall.SIGCONT, + syscall.SIGEMT, + syscall.SIGFPE, + syscall.SIGHUP, + syscall.SIGILL, + syscall.SIGINFO, + syscall.SIGINT, + syscall.SIGIO, + syscall.SIGIOT, + syscall.SIGKILL, + syscall.SIGPIPE, + syscall.SIGPROF, + syscall.SIGQUIT, + syscall.SIGSEGV, + syscall.SIGSTOP, + syscall.SIGSYS, + syscall.SIGTERM, + syscall.SIGTRAP, + syscall.SIGTSTP, + syscall.SIGTTIN, + syscall.SIGTTOU, + syscall.SIGURG, + syscall.SIGUSR1, + syscall.SIGUSR2, + syscall.SIGVTALRM, + syscall.SIGWINCH, + syscall.SIGXCPU, + syscall.SIGXFSZ, + ) +} diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go new file mode 100644 index 00000000..d2778221 --- /dev/null +++ b/signal/signal_freebsd.go @@ -0,0 +1,42 @@ +package signal + +import ( + "os" + "os/signal" + "syscall" +) + +func CatchAll(sigc chan os.Signal) { + signal.Notify(sigc, + syscall.SIGABRT, + syscall.SIGALRM, + syscall.SIGBUS, + syscall.SIGCHLD, + syscall.SIGCONT, + syscall.SIGFPE, + syscall.SIGHUP, + syscall.SIGILL, + syscall.SIGINT, + syscall.SIGIO, + syscall.SIGIOT, + syscall.SIGKILL, + syscall.SIGPIPE, + syscall.SIGPROF, + syscall.SIGQUIT, + syscall.SIGSEGV, + syscall.SIGSTOP, + syscall.SIGSYS, + syscall.SIGTERM, + syscall.SIGTRAP, + syscall.SIGTSTP, + syscall.SIGTTIN, + syscall.SIGTTOU, + syscall.SIGURG, + syscall.SIGUSR1, + syscall.SIGUSR2, + syscall.SIGVTALRM, + syscall.SIGWINCH, + syscall.SIGXCPU, + syscall.SIGXFSZ, + ) +} diff --git a/signal/signal_linux.go b/signal/signal_linux.go new file mode 100644 index 00000000..b6b25d51 --- /dev/null +++ b/signal/signal_linux.go @@ -0,0 +1,87 @@ +package signal + +import ( + "os" + "os/signal" + "syscall" +) + +var signalMap = map[string]syscall.Signal{} + +/* + syscall.SIGABRT, + syscall.SIGALRM, + syscall.SIGBUS, + syscall.SIGCHLD, + syscall.SIGCLD, + syscall.SIGCONT, + syscall.SIGFPE, + syscall.SIGHUP, + syscall.SIGILL, + syscall.SIGINT, + syscall.SIGIO, + syscall.SIGIOT, + syscall.SIGKILL, + syscall.SIGPIPE, + syscall.SIGPOLL, + syscall.SIGPROF, + syscall.SIGPWR, + syscall.SIGQUIT, + syscall.SIGSEGV, + syscall.SIGSTKFLT, + syscall.SIGSTOP, + syscall.SIGSYS, + syscall.SIGTERM, + syscall.SIGTRAP, + syscall.SIGTSTP, + syscall.SIGTTIN, + syscall.SIGTTOU, + syscall.SIGUNUSED, + syscall.SIGURG, + syscall.SIGUSR1, + syscall.SIGUSR2, + syscall.SIGVTALRM, + syscall.SIGWINCH, + syscall.SIGXCPU, + syscall.SIGXFSZ, +*/ + +func CatchAll(sigc chan os.Signal) { + signal.Notify(sigc, + syscall.SIGABRT, + syscall.SIGALRM, + syscall.SIGBUS, + syscall.SIGCHLD, + syscall.SIGCLD, + syscall.SIGCONT, + syscall.SIGFPE, + syscall.SIGHUP, + syscall.SIGILL, + syscall.SIGINT, + syscall.SIGIO, + syscall.SIGIOT, + syscall.SIGKILL, + syscall.SIGPIPE, + syscall.SIGPOLL, + syscall.SIGPROF, + syscall.SIGPWR, + syscall.SIGQUIT, + syscall.SIGSEGV, + syscall.SIGSTKFLT, + syscall.SIGSTOP, + syscall.SIGSYS, + syscall.SIGTERM, + syscall.SIGTRAP, + syscall.SIGTSTP, + syscall.SIGTTIN, + syscall.SIGTTOU, + syscall.SIGUNUSED, + syscall.SIGURG, + syscall.SIGUSR1, + syscall.SIGUSR2, + syscall.SIGVTALRM, + syscall.SIGWINCH, + syscall.SIGXCPU, + syscall.SIGXFSZ, + ) +} From 108a12b206f5c6a95eefd397b2e6d461cd7b3613 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 10 Mar 2014 13:50:16 -0700 Subject: [PATCH 02/60] Create portable signalMap Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes (github: creack) --- signal/signal.go | 8 +++ signal/signal_darwin.go | 70 ++++++++++----------- signal/signal_freebsd.go | 68 ++++++++++---------- signal/signal_linux.go | 116 +++++++++++------------------------ signal/signal_unsupported.go | 9 +++ 5 files changed, 121 insertions(+), 150 deletions(-) create mode 100644 signal/signal_unsupported.go diff --git a/signal/signal.go b/signal/signal.go index 6f9874bd..a6732226 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -5,6 +5,14 @@ import ( "os/signal" ) +func CatchAll(sigc chan os.Signal) { + handledSigs := []os.Signal{} + for _, s := range signalMap { + handledSigs = append(handledSigs, s) + } + signal.Notify(sigc, handledSigs...) +} + func StopCatch(sigc chan os.Signal) { signal.Stop(sigc) close(sigc) diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go index 22a60ae1..b290a8b5 100644 --- a/signal/signal_darwin.go +++ b/signal/signal_darwin.go @@ -1,44 +1,40 @@ package signal import ( - "os" - "os/signal" "syscall" ) -func CatchAll(sigc chan os.Signal) { - signal.Notify(sigc, - syscall.SIGABRT, - syscall.SIGALRM, - syscall.SIGBUS, - syscall.SIGCHLD, - syscall.SIGCONT, - syscall.SIGEMT, - syscall.SIGFPE, - syscall.SIGHUP, - syscall.SIGILL, - syscall.SIGINFO, - syscall.SIGINT, - syscall.SIGIO, - syscall.SIGIOT, - syscall.SIGKILL, - syscall.SIGPIPE, - syscall.SIGPROF, - syscall.SIGQUIT, - syscall.SIGSEGV, - syscall.SIGSTOP, - syscall.SIGSYS, - syscall.SIGTERM, - syscall.SIGTRAP, - syscall.SIGTSTP, - syscall.SIGTTIN, - syscall.SIGTTOU, - syscall.SIGURG, - syscall.SIGUSR1, - syscall.SIGUSR2, - syscall.SIGVTALRM, - syscall.SIGWINCH, - syscall.SIGXCPU, - syscall.SIGXFSZ, - ) +var signalMap = map[string]syscall.Signal{ + "ABRT": syscall.SIGABRT, + "ALRM": syscall.SIGALRM, + "BUG": syscall.SIGBUS, + "CHLD": syscall.SIGCHLD, + "CONT": syscall.SIGCONT, + "EMT": syscall.SIGEMT, + "FPE": syscall.SIGFPE, + "HUP": syscall.SIGHUP, + "ILL": syscall.SIGILL, + "INFO": syscall.SIGINFO, + "INT": syscall.SIGINT, + "IO": syscall.SIGIO, + "IOT": syscall.SIGIOT, + "KILL": syscall.SIGKILL, + "PIPE": syscall.SIGPIPE, + "PROF": syscall.SIGPROF, + "QUIT": syscall.SIGQUIT, + "SEGV": syscall.SIGSEGV, + "STOP": syscall.SIGSTOP, + "SYS": syscall.SIGSYS, + "TERM": syscall.SIGTERM, + "TRAP": syscall.SIGTRAP, + "TSTP": syscall.SIGTSTP, + "TTIN": syscall.SIGTTIN, + "TTOU": syscall.SIGTTOU, + "URG": syscall.SIGURG, + "USR1": syscall.SIGUSR1, + "USR2": syscall.SIGUSR2, + "VTALRM": syscall.SIGVTALRM, + "WINCH": syscall.SIGWINCH, + "XCPU": syscall.SIGXCPU, + "XFSZ": syscall.SIGXFSZ, } diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index d2778221..b7e3ff4f 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -6,37 +6,39 @@ import ( "syscall" ) -func CatchAll(sigc chan os.Signal) { - signal.Notify(sigc, - syscall.SIGABRT, - syscall.SIGALRM, - syscall.SIGBUS, - syscall.SIGCHLD, - syscall.SIGCONT, - syscall.SIGFPE, - syscall.SIGHUP, - syscall.SIGILL, - syscall.SIGINT, - syscall.SIGIO, - syscall.SIGIOT, - syscall.SIGKILL, - syscall.SIGPIPE, - syscall.SIGPROF, - syscall.SIGQUIT, - syscall.SIGSEGV, - syscall.SIGSTOP, - syscall.SIGSYS, - syscall.SIGTERM, - syscall.SIGTRAP, - syscall.SIGTSTP, - syscall.SIGTTIN, - syscall.SIGTTOU, - syscall.SIGURG, - syscall.SIGUSR1, - syscall.SIGUSR2, - syscall.SIGVTALRM, - syscall.SIGWINCH, - syscall.SIGXCPU, - syscall.SIGXFSZ, - ) +var signalMap = map[string]syscall.Signal{ + "ABRT": syscall.SIGABRT, + "ALRM": syscall.SIGALRM, + "BUF": syscall.SIGBUS, + "CHLD": syscall.SIGCHLD, + "CONT": syscall.SIGCONT, + "EMT": syscall.SIGEMT, + "FPE": syscall.SIGFPE, + "HUP": syscall.SIGHUP, + "ILL": syscall.SIGILL, + "INFO": syscall.SIGINFO, + "INT": syscall.SIGINT, + "IO": syscall.SIGIO, + "IOT": syscall.SIGIOT, + "KILL": syscall.SIGKILL, + "LWP": syscall.SIGLWP, + "PIPE": syscall.SIGPIPE, + "PROF": syscall.SIGPROF, + "QUIT": syscall.SIGQUIT, + "SEGV": syscall.SIGSEGV, + "STOP": syscall.SIGSTOP, + "SYS": syscall.SIGSYS, + "TERM": syscall.SIGTERM, + "THR": syscall.SIGTHR, + "TRAP": syscall.SIGTRAP, + "TSTP": syscall.SIGTSTP, + "TTIN": syscall.SIGTTIN, + "TTOU": syscall.SIGTTOU, + "URG": syscall.SIGURG, + "USR1": syscall.SIGUSR1, + "USR2": syscall.SIGUSR2, + "VTALRM": syscall.SIGVTALRM, + "WINCH": syscall.SIGWINCH, + "XCPU": syscall.SIGXCPU, + "XFSZ": syscall.SIGXFSZ, } diff --git a/signal/signal_linux.go b/signal/signal_linux.go index b6b25d51..cd8cb83e 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -1,87 +1,43 @@ package signal import ( - "os" - "os/signal" "syscall" ) -var signalMap = map[string]syscall.Signal{} - -/* - syscall.SIGABRT, - syscall.SIGALRM, - syscall.SIGBUS, - syscall.SIGCHLD, - syscall.SIGCLD, - syscall.SIGCONT, - syscall.SIGFPE, - syscall.SIGHUP, - syscall.SIGILL, - syscall.SIGINT, - syscall.SIGIO, - syscall.SIGIOT, - syscall.SIGKILL, - syscall.SIGPIPE, - syscall.SIGPOLL, - syscall.SIGPROF, - syscall.SIGPWR, - syscall.SIGQUIT, - syscall.SIGSEGV, - syscall.SIGSTKFLT, - syscall.SIGSTOP, - syscall.SIGSYS, - syscall.SIGTERM, - syscall.SIGTRAP, - syscall.SIGTSTP, - syscall.SIGTTIN, - syscall.SIGTTOU, - syscall.SIGUNUSED, - syscall.SIGURG, - syscall.SIGUSR1, - syscall.SIGUSR2, - syscall.SIGVTALRM, - syscall.SIGWINCH, - syscall.SIGXCPU, - syscall.SIGXFSZ, -*/ - -func CatchAll(sigc chan os.Signal) { - signal.Notify(sigc, - syscall.SIGABRT, - syscall.SIGALRM, - syscall.SIGBUS, - syscall.SIGCHLD, - syscall.SIGCLD, - syscall.SIGCONT, - syscall.SIGFPE, - syscall.SIGHUP, - syscall.SIGILL, - syscall.SIGINT, - syscall.SIGIO, - syscall.SIGIOT, - syscall.SIGKILL, - syscall.SIGPIPE, - syscall.SIGPOLL, - syscall.SIGPROF, - syscall.SIGPWR, - syscall.SIGQUIT, - syscall.SIGSEGV, - syscall.SIGSTKFLT, - syscall.SIGSTOP, - syscall.SIGSYS, - syscall.SIGTERM, - syscall.SIGTRAP, - syscall.SIGTSTP, - syscall.SIGTTIN, - syscall.SIGTTOU, - syscall.SIGUNUSED, - syscall.SIGURG, - syscall.SIGUSR1, - syscall.SIGUSR2, - syscall.SIGVTALRM, - syscall.SIGWINCH, - syscall.SIGXCPU, - syscall.SIGXFSZ, - ) +var signalMap = map[string]syscall.Signal{ + "ABRT": syscall.SIGABRT, + "ALRM": syscall.SIGALRM, + "BUS": syscall.SIGBUS, + "CHLD": syscall.SIGCHLD, + "CLD": syscall.SIGCLD, + "CONT": syscall.SIGCONT, + "FPE": syscall.SIGFPE, + "HUP": syscall.SIGHUP, + "ILL": syscall.SIGILL, + "INT": syscall.SIGINT, + "IO": syscall.SIGIO, + "IOT": syscall.SIGIOT, + "KILL": syscall.SIGKILL, + "PIPE": syscall.SIGPIPE, + "POLL": syscall.SIGPOLL, + "PROF": syscall.SIGPROF, + "PWR": syscall.SIGPWR, + "QUIT": syscall.SIGQUIT, + "SEGV": syscall.SIGSEGV, + "STKFLT": syscall.SIGSTKFLT, + "STOP": syscall.SIGSTOP, + "SYS": syscall.SIGSYS, + "TERM": syscall.SIGTERM, + "TRAP": syscall.SIGTRAP, + "TSTP": syscall.SIGTSTP, + "TTIN": syscall.SIGTTIN, + "TTOU": syscall.SIGTTOU, + "UNUSED": syscall.SIGUNUSED, + "URG": syscall.SIGURG, + "USR1": syscall.SIGUSR1, + "USR2": syscall.SIGUSR2, + "VTALRM": syscall.SIGVTALRM, + "WINCH": syscall.SIGWINCH, + "XCPU": syscall.SIGXCPU, + "XFSZ": syscall.SIGXFSZ, } diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go new file mode 100644 index 00000000..2c49a0b0 --- /dev/null +++ b/signal/signal_unsupported.go @@ -0,0 +1,9 @@ +// +build !linux,!darwin,!freebsd + +package signal + +import ( + "syscall" +) + +var signalMap = map[string]syscall.Signal{} From aa57f84f64a6cee6d0a6ebe7e8fec8ee38bd094a Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 10 Mar 2014 14:22:27 -0700 Subject: [PATCH 03/60] Make docker use the signal pkg with strings Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes (github: creack) --- signal/signal.go | 2 +- signal/signal_darwin.go | 64 ++++++++++++++++++------------------ signal/signal_freebsd.go | 2 +- signal/signal_linux.go | 2 +- signal/signal_unsupported.go | 2 +- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/signal/signal.go b/signal/signal.go index a6732226..63337542 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -7,7 +7,7 @@ import ( func CatchAll(sigc chan os.Signal) { handledSigs := []os.Signal{} - for _, s := range signalMap { + for _, s := range SignalMap { handledSigs = append(handledSigs, s) } signal.Notify(sigc, handledSigs...) diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go index b290a8b5..fcd3a8f2 100644 --- a/signal/signal_darwin.go +++ b/signal/signal_darwin.go @@ -4,37 +4,37 @@ import ( "syscall" ) -var signalMap = map[string]syscall.Signal{ - "ABRT": syscall.SIGABRT, - "ALRM": syscall.SIGALRM, - "BUG": syscall.SIGBUS, - "CHLD": syscall.SIGCHLD, - "CONT": syscall.SIGCONT, - "EMT": syscall.SIGEMT, - "FPE": syscall.SIGFPE, - "HUP": syscall.SIGHUP, - "ILL": syscall.SIGILL, - "INFO": syscall.SIGINFO, - "INT": syscall.SIGINT, - "IO": syscall.SIGIO, - "IOT": syscall.SIGIOT, - "KILL": syscall.SIGKILL, - "PIPE": syscall.SIGPIPE, - "PROF": syscall.SIGPROF, - "QUIT": syscall.SIGQUIT, - "SEGV": syscall.SIGSEGV, - "STOP": syscall.SIGSTOP, - "SYS": syscall.SIGSYS, - "TERM": syscall.SIGTERM, - "TRAP": syscall.SIGTRAP, - "TSTP": syscall.SIGTSTP, - "TTIN": syscall.SIGTTIN, - "TTOU": syscall.SIGTTOU, - "URG": syscall.SIGURG, - "USR1": syscall.SIGUSR1, - "USR2": syscall.SIGUSR2, +var SignalMap = map[string]syscall.Signal{ + "ABRT": syscall.SIGABRT, + "ALRM": syscall.SIGALRM, + "BUG": syscall.SIGBUS, + "CHLD": syscall.SIGCHLD, + "CONT": syscall.SIGCONT, + "EMT": syscall.SIGEMT, + "FPE": syscall.SIGFPE, + "HUP": syscall.SIGHUP, + "ILL": syscall.SIGILL, + "INFO": syscall.SIGINFO, + "INT": syscall.SIGINT, + "IO": syscall.SIGIO, + "IOT": syscall.SIGIOT, + "KILL": syscall.SIGKILL, + "PIPE": syscall.SIGPIPE, + "PROF": syscall.SIGPROF, + "QUIT": syscall.SIGQUIT, + "SEGV": syscall.SIGSEGV, + "STOP": syscall.SIGSTOP, + "SYS": syscall.SIGSYS, + "TERM": syscall.SIGTERM, + "TRAP": syscall.SIGTRAP, + "TSTP": syscall.SIGTSTP, + "TTIN": syscall.SIGTTIN, + "TTOU": syscall.SIGTTOU, + "URG": syscall.SIGURG, + "USR1": syscall.SIGUSR1, + "USR2": syscall.SIGUSR2, "VTALRM": syscall.SIGVTALRM, - "WINCH": syscall.SIGWINCH, - "XCPU": syscall.SIGXCPU, - "XFSZ": syscall.SIGXFSZ, + "WINCH": syscall.SIGWINCH, + "XCPU": syscall.SIGXCPU, + "XFSZ": syscall.SIGXFSZ, } diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index b7e3ff4f..da042d7e 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -6,7 +6,7 @@ import ( "syscall" ) -var signalMap = map[string]syscall.Signal{ +var SignalMap = map[string]syscall.Signal{ "ABRT": syscall.SIGABRT, "ALRM": syscall.SIGALRM, "BUF": syscall.SIGBUS, diff --git a/signal/signal_linux.go b/signal/signal_linux.go index cd8cb83e..a62f79d4 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -4,7 +4,7 @@ import ( "syscall" ) -var signalMap = map[string]syscall.Signal{ +var SignalMap = map[string]syscall.Signal{ "ABRT": syscall.SIGABRT, "ALRM": syscall.SIGALRM, "BUS": syscall.SIGBUS, diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go index 2c49a0b0..99f94659 100644 --- a/signal/signal_unsupported.go +++ b/signal/signal_unsupported.go @@ -6,4 +6,4 @@ import ( "syscall" ) -var signalMap = map[string]syscall.Signal{} +var SignalMap = map[string]syscall.Signal{} From 5fcf5fada45606c77f059191a489d0e58d5183cd Mon Sep 17 00:00:00 2001 From: Kato Kazuyoshi Date: Tue, 11 Mar 2014 22:45:47 +0900 Subject: [PATCH 04/60] Like signal_linux.go, we don't have import os and os/signal Docker-DCO-1.1-Signed-off-by: Kato Kazuyoshi (github: kzys) --- signal/signal_freebsd.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index da042d7e..102e9184 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -1,8 +1,6 @@ package signal import ( - "os" - "os/signal" "syscall" ) From 530bcc0fe7a1bf957dd6b2c31625e2f2b85f8a0c Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 10 Mar 2014 20:26:45 -0700 Subject: [PATCH 05/60] Update email + add self to pkg/signal Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes (github: creack) --- signal/MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 signal/MAINTAINERS diff --git a/signal/MAINTAINERS b/signal/MAINTAINERS new file mode 100644 index 00000000..33003315 --- /dev/null +++ b/signal/MAINTAINERS @@ -0,0 +1,2 @@ +Guillaume J. Charmes (@creack) + From a3e22472f7f0e8ae1734db9e0658f290b8ac1164 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Wed, 30 Apr 2014 11:22:11 -0600 Subject: [PATCH 06/60] Fix various MAINTAINERS format inconsistencies Docker-DCO-1.1-Signed-off-by: Andrew Page (github: tianon) --- signal/MAINTAINERS | 1 - 1 file changed, 1 deletion(-) diff --git a/signal/MAINTAINERS b/signal/MAINTAINERS index 33003315..acf6f21b 100644 --- a/signal/MAINTAINERS +++ b/signal/MAINTAINERS @@ -1,2 +1 @@ Guillaume J. Charmes (@creack) - From 2f0e890f90cb434be2977bb955ad617782f199c6 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Mon, 16 Jun 2014 06:22:15 -0700 Subject: [PATCH 07/60] Guillaume is busy full-time on his new business, and no longer available as a maintainer. Best of luck on your e-commerce business Guillaume, and thanks for all the great contributions! Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- signal/MAINTAINERS | 1 - 1 file changed, 1 deletion(-) delete mode 100644 signal/MAINTAINERS diff --git a/signal/MAINTAINERS b/signal/MAINTAINERS deleted file mode 100644 index acf6f21b..00000000 --- a/signal/MAINTAINERS +++ /dev/null @@ -1 +0,0 @@ -Guillaume J. Charmes (@creack) From 28ce2217254997731bc1f0952cb5055d306c2d16 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 5 Aug 2014 16:32:24 +0000 Subject: [PATCH 08/60] Move signal handling code to pkg/signal.Trap Signed-off-by: Solomon Hykes --- signal/trap.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 signal/trap.go diff --git a/signal/trap.go b/signal/trap.go new file mode 100644 index 00000000..0f44481c --- /dev/null +++ b/signal/trap.go @@ -0,0 +1,53 @@ +package signal + +import ( + "log" + "os" + gosignal "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 repeated 3 times before cleanup is complete, then cleanup is +// skipped and the process terminated directly. +// * If "DEBUG" is set in the environment, SIGQUIT causes an exit without cleanup. +// +func Trap(cleanup func()) { + c := make(chan os.Signal, 1) + signals := []os.Signal{os.Interrupt, syscall.SIGTERM} + if os.Getenv("DEBUG") == "" { + signals = append(signals, syscall.SIGQUIT) + } + gosignal.Notify(c, signals...) + go func() { + interruptCount := uint32(0) + for sig := range c { + go func(sig os.Signal) { + log.Printf("Received signal '%v', starting shutdown of docker...\n", sig) + switch sig { + case os.Interrupt, syscall.SIGTERM: + // If the user really wants to interrupt, let him do so. + if atomic.LoadUint32(&interruptCount) < 3 { + atomic.AddUint32(&interruptCount, 1) + // Initiate the cleanup only once + if atomic.LoadUint32(&interruptCount) == 1 { + // Call cleanup handler + cleanup() + } else { + return + } + } else { + log.Printf("Force shutdown of docker, interrupting cleanup\n") + } + case syscall.SIGQUIT: + } + os.Exit(128 + int(sig.(syscall.Signal))) + }(sig) + } + }() +} From 8c8d0fd2e86fd3a1854c88204041284bc77e81e3 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 13 Aug 2014 12:18:23 -0700 Subject: [PATCH 09/60] Exit after receiving SIGTERM Signed-off-by: Michael Crosby --- signal/trap.go | 1 + 1 file changed, 1 insertion(+) diff --git a/signal/trap.go b/signal/trap.go index 0f44481c..cbdfd1ff 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -38,6 +38,7 @@ func Trap(cleanup func()) { if atomic.LoadUint32(&interruptCount) == 1 { // Call cleanup handler cleanup() + os.Exit(0) } else { return } From ec4af42a46f5bf92198bb9046cbd7d7962e56e2a Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 25 Sep 2014 12:55:53 -0400 Subject: [PATCH 10/60] daemon logging: unifying output and timestamps A little refactor of the ./pkg/log so engine can have a logger instance Signed-off-by: Vincent Batts --- signal/trap.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index cbdfd1ff..42ddb4d2 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -1,11 +1,12 @@ package signal import ( - "log" "os" gosignal "os/signal" "sync/atomic" "syscall" + + "github.com/docker/docker/pkg/log" ) // Trap sets up a simplified signal "trap", appropriate for common @@ -28,7 +29,7 @@ func Trap(cleanup func()) { interruptCount := uint32(0) for sig := range c { go func(sig os.Signal) { - log.Printf("Received signal '%v', starting shutdown of docker...\n", sig) + log.Infof("Received signal '%v', starting shutdown of docker...", sig) switch sig { case os.Interrupt, syscall.SIGTERM: // If the user really wants to interrupt, let him do so. @@ -43,7 +44,7 @@ func Trap(cleanup func()) { return } } else { - log.Printf("Force shutdown of docker, interrupting cleanup\n") + log.Infof("Force shutdown of docker, interrupting cleanup") } case syscall.SIGQUIT: } From 3e986298520876936f5fb2f511b2c413d4a62523 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Fri, 24 Oct 2014 10:12:35 -0700 Subject: [PATCH 11/60] Use logrus everywhere for logging Fixed #8761 Signed-off-by: Alexandr Morozov --- signal/trap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/trap.go b/signal/trap.go index 42ddb4d2..9be82671 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -6,7 +6,7 @@ import ( "sync/atomic" "syscall" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" ) // Trap sets up a simplified signal "trap", appropriate for common From 8ff6be503c0ae3caf853416f5aec5dee6f2a92dd Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Thu, 13 Nov 2014 10:40:22 -0800 Subject: [PATCH 12/60] Extract client signals to pkg/signal SIGCHLD and SIGWINCH used in api/client (cli code) are not available on Windows. Extracting into separate files with build tags. Signed-off-by: Ahmet Alp Balkan --- signal/signal_unix.go | 12 ++++++++++++ signal/signal_windows.go | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 signal/signal_unix.go create mode 100644 signal/signal_windows.go diff --git a/signal/signal_unix.go b/signal/signal_unix.go new file mode 100644 index 00000000..613e30e5 --- /dev/null +++ b/signal/signal_unix.go @@ -0,0 +1,12 @@ +// +build !windows + +package signal + +import ( + "syscall" +) + +// Signals used in api/client (no windows equivalent, use +// invalid signals so they don't get handled) +const SIGCHLD = syscall.SIGCHLD +const SIGWINCH = syscall.SIGWINCH diff --git a/signal/signal_windows.go b/signal/signal_windows.go new file mode 100644 index 00000000..9f00b999 --- /dev/null +++ b/signal/signal_windows.go @@ -0,0 +1,12 @@ +// +build windows + +package signal + +import ( + "syscall" +) + +// Signals used in api/client (no windows equivalent, use +// invalid signals so they don't get handled) +const SIGCHLD = syscall.Signal(0xff) +const SIGWINCH = syscall.Signal(0xff) From 3d5d64b0525a7c5ee7f6e7fe5ad85b990fb45417 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Sat, 6 Dec 2014 22:42:32 +0800 Subject: [PATCH 13/60] Removed race condition If two interrupts were fired really quickly interruptCount could have been incremented twice before the LoadUint32 making cleanup not being called at all. Signed-off-by: Erik Dubbelboer --- signal/trap.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 9be82671..78a709b3 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -34,9 +34,8 @@ func Trap(cleanup func()) { case os.Interrupt, syscall.SIGTERM: // If the user really wants to interrupt, let him do so. if atomic.LoadUint32(&interruptCount) < 3 { - atomic.AddUint32(&interruptCount, 1) // Initiate the cleanup only once - if atomic.LoadUint32(&interruptCount) == 1 { + if atomic.AddUint32(&interruptCount, 1) == 1 { // Call cleanup handler cleanup() os.Exit(0) From d2e6ee05857671a5316b8c92176d651e5c9b7aa4 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Thu, 26 Mar 2015 23:22:04 +0100 Subject: [PATCH 14/60] Replace aliased imports of logrus, fixes #11762 Signed-off-by: Antonio Murdaca --- signal/trap.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 78a709b3..7469dbcc 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -6,7 +6,7 @@ import ( "sync/atomic" "syscall" - log "github.com/Sirupsen/logrus" + "github.com/Sirupsen/logrus" ) // Trap sets up a simplified signal "trap", appropriate for common @@ -29,7 +29,7 @@ func Trap(cleanup func()) { interruptCount := uint32(0) for sig := range c { go func(sig os.Signal) { - log.Infof("Received signal '%v', starting shutdown of docker...", sig) + logrus.Infof("Received signal '%v', starting shutdown of docker...", sig) switch sig { case os.Interrupt, syscall.SIGTERM: // If the user really wants to interrupt, let him do so. @@ -43,7 +43,7 @@ func Trap(cleanup func()) { return } } else { - log.Infof("Force shutdown of docker, interrupting cleanup") + logrus.Infof("Force shutdown of docker, interrupting cleanup") } case syscall.SIGQUIT: } From fc56ebe75350b65bf329439fdd58b45306c2bb3d Mon Sep 17 00:00:00 2001 From: Phil Estes Date: Tue, 21 Apr 2015 00:24:24 -0400 Subject: [PATCH 15/60] Add SIGUSR1 handler for dumping stack/goroutine traces Add handler for SIGUSR1 based on feedback regarding when to dump goroutine stacks. This will also dump goroutine stack traces on SIGQUIT followed by a hard-exit from the daemon. Docker-DCO-1.1-Signed-off-by: Phil Estes (github: estesp) --- signal/trap.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 7469dbcc..3772db5e 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -3,6 +3,7 @@ package signal import ( "os" gosignal "os/signal" + "runtime" "sync/atomic" "syscall" @@ -14,41 +15,50 @@ import ( // (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 repeated 3 times before cleanup is complete, then cleanup is -// skipped and the process terminated directly. -// * If "DEBUG" is set in the environment, SIGQUIT causes an exit without cleanup. +// * 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) +// * A SIGQUIT always causes an exit without cleanup, with a goroutine dump preceding exit. // func Trap(cleanup func()) { c := make(chan os.Signal, 1) - signals := []os.Signal{os.Interrupt, syscall.SIGTERM} - if os.Getenv("DEBUG") == "" { - signals = append(signals, syscall.SIGQUIT) - } + // we will handle INT, TERM, QUIT here + signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT} gosignal.Notify(c, signals...) go func() { interruptCount := uint32(0) for sig := range c { go func(sig os.Signal) { - logrus.Infof("Received signal '%v', starting shutdown of docker...", sig) + logrus.Infof("Processing signal '%v'", sig) switch sig { case os.Interrupt, syscall.SIGTERM: - // If the user really wants to interrupt, let him do so. if atomic.LoadUint32(&interruptCount) < 3 { // Initiate the cleanup only once if atomic.AddUint32(&interruptCount, 1) == 1 { - // Call cleanup handler + // Call the provided cleanup handler cleanup() os.Exit(0) } else { return } } else { - logrus.Infof("Force shutdown of docker, interrupting cleanup") + // 3 SIGTERM/INT signals received; force exit without cleanup + logrus.Infof("Forcing docker daemon shutdown without cleanup; 3 interrupts received") } case syscall.SIGQUIT: + DumpStacks() + logrus.Infof("Forcing docker daemon shutdown without cleanup on SIGQUIT") } + //for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # os.Exit(128 + int(sig.(syscall.Signal))) }(sig) } }() } + +func DumpStacks() { + buf := make([]byte, 16384) + buf = buf[:runtime.Stack(buf, true)] + // Note that if the daemon is started with a less-verbose log-level than "info" (the default), the goroutine + // traces won't show up in the log. + logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf) +} From 9fb39899e600cfb8781bedf4296c0b43273b812a Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Sat, 25 Jul 2015 10:35:07 +0200 Subject: [PATCH 16/60] Lint on pkg/* packages - pkg/useragent - pkg/units - pkg/ulimit - pkg/truncindex - pkg/timeoutconn - pkg/term - pkg/tarsum - pkg/tailfile - pkg/systemd - pkg/stringutils - pkg/stringid - pkg/streamformatter - pkg/sockets - pkg/signal - pkg/proxy - pkg/progressreader - pkg/pools - pkg/plugins - pkg/pidfile - pkg/parsers - pkg/parsers/filters - pkg/parsers/kernel - pkg/parsers/operatingsystem Signed-off-by: Vincent Demeester --- signal/signal.go | 4 ++++ signal/signal_darwin.go | 1 + signal/signal_freebsd.go | 1 + signal/signal_linux.go | 1 + signal/signal_unix.go | 4 ++++ signal/signal_unsupported.go | 1 + signal/signal_windows.go | 6 ++++-- signal/trap.go | 1 + 8 files changed, 17 insertions(+), 2 deletions(-) diff --git a/signal/signal.go b/signal/signal.go index 63337542..db60bf2e 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -1,3 +1,5 @@ +// Package signal provides helper functions for dealing with signals across +// various operating systems. package signal import ( @@ -5,6 +7,7 @@ import ( "os/signal" ) +// CatchAll catches all signals and relays them to the specified channel. func CatchAll(sigc chan os.Signal) { handledSigs := []os.Signal{} for _, s := range SignalMap { @@ -13,6 +16,7 @@ func CatchAll(sigc chan os.Signal) { signal.Notify(sigc, handledSigs...) } +// StopCatch stops catching the signals and closes the specified channel. func StopCatch(sigc chan os.Signal) { signal.Stop(sigc) close(sigc) diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go index fcd3a8f2..946de87e 100644 --- a/signal/signal_darwin.go +++ b/signal/signal_darwin.go @@ -4,6 +4,7 @@ import ( "syscall" ) +// SignalMap is a map of Darwin signals. var SignalMap = map[string]syscall.Signal{ "ABRT": syscall.SIGABRT, "ALRM": syscall.SIGALRM, diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index 102e9184..6b9569bb 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -4,6 +4,7 @@ import ( "syscall" ) +// SignalMap is a map of FreeBSD signals. var SignalMap = map[string]syscall.Signal{ "ABRT": syscall.SIGABRT, "ALRM": syscall.SIGALRM, diff --git a/signal/signal_linux.go b/signal/signal_linux.go index a62f79d4..1ecc3294 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -4,6 +4,7 @@ import ( "syscall" ) +// SignalMap is a map of Linux signals. var SignalMap = map[string]syscall.Signal{ "ABRT": syscall.SIGABRT, "ALRM": syscall.SIGALRM, diff --git a/signal/signal_unix.go b/signal/signal_unix.go index 613e30e5..5c1ad5f7 100644 --- a/signal/signal_unix.go +++ b/signal/signal_unix.go @@ -8,5 +8,9 @@ import ( // Signals used in api/client (no windows equivalent, use // invalid signals so they don't get handled) + +// SIGCHLD is a signal sent to a process when a child process terminates, is interrupted, or resumes after being interrupted. const SIGCHLD = syscall.SIGCHLD + +// SIGWINCH is a signal sent to a process when its controlling terminal changes its size const SIGWINCH = syscall.SIGWINCH diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go index 99f94659..cefb2407 100644 --- a/signal/signal_unsupported.go +++ b/signal/signal_unsupported.go @@ -6,4 +6,5 @@ import ( "syscall" ) +// SignalMap is an empty map of signals for unsupported platform. var SignalMap = map[string]syscall.Signal{} diff --git a/signal/signal_windows.go b/signal/signal_windows.go index 9f00b999..1f1a6edb 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -8,5 +8,7 @@ import ( // Signals used in api/client (no windows equivalent, use // invalid signals so they don't get handled) -const SIGCHLD = syscall.Signal(0xff) -const SIGWINCH = syscall.Signal(0xff) +const ( + SIGCHLD = syscall.Signal(0xff) + SIGWINCH = syscall.Signal(0xff) +) diff --git a/signal/trap.go b/signal/trap.go index 3772db5e..80b5e871 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -55,6 +55,7 @@ func Trap(cleanup func()) { }() } +// DumpStacks dumps the runtime stack. func DumpStacks() { buf := make([]byte, 16384) buf = buf[:runtime.Stack(buf, true)] From acd05432269aae4fcfa9d3c9bf05b3235ee801be Mon Sep 17 00:00:00 2001 From: Cezar Sa Espinola Date: Fri, 4 Sep 2015 14:30:14 -0300 Subject: [PATCH 17/60] Ensure goroutines dump is not truncated Calling runtime.Stack requires the buffer to be big enough to fit the goroutines dump. If it's not big enough the dump will be truncated and the value returned will be the same size as the buffer. The code was changed to handle this situation and try again with a bigger buffer. Each time the dump doesn't fit in the buffer its size is doubled. Signed-off-by: Cezar Sa Espinola --- signal/trap.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 80b5e871..2cf5ccf0 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -57,8 +57,17 @@ func Trap(cleanup func()) { // DumpStacks dumps the runtime stack. func DumpStacks() { - buf := make([]byte, 16384) - buf = buf[:runtime.Stack(buf, true)] + var ( + buf []byte + stackSize int + ) + bufferLen := 16384 + for stackSize == len(buf) { + buf = make([]byte, bufferLen) + stackSize = runtime.Stack(buf, true) + bufferLen *= 2 + } + buf = buf[:stackSize] // Note that if the daemon is started with a less-verbose log-level than "info" (the default), the goroutine // traces won't show up in the log. logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf) From 411d244cdb06a4a777ee3230a021b1d0c3a50c84 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Tue, 4 Aug 2015 13:51:48 -0700 Subject: [PATCH 18/60] Signal to stop a container. Allow to set the signal to stop a container in `docker run`: - Use `--stop-signal` with docker-run to set the default signal the container will use to exit. Signed-off-by: David Calavera --- signal/signal.go | 21 +++++++++++++++++++++ signal/signal_unix.go | 13 ++++++++----- signal/signal_windows.go | 2 ++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/signal/signal.go b/signal/signal.go index db60bf2e..106fe20d 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -3,8 +3,12 @@ package signal import ( + "fmt" "os" "os/signal" + "strconv" + "strings" + "syscall" ) // CatchAll catches all signals and relays them to the specified channel. @@ -21,3 +25,20 @@ func StopCatch(sigc chan os.Signal) { signal.Stop(sigc) close(sigc) } + +// ParseSignal translates a string to a valid syscall signal. +// It returns an error if the signal map doesn't include the given signal. +func ParseSignal(rawSignal string) (syscall.Signal, error) { + s, err := strconv.Atoi(rawSignal) + if err == nil { + if s == 0 { + return -1, fmt.Errorf("Invalid signal: %s", rawSignal) + } + return syscall.Signal(s), nil + } + signal, ok := SignalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")] + if !ok { + return -1, fmt.Errorf("Invalid signal: %s", rawSignal) + } + return signal, nil +} diff --git a/signal/signal_unix.go b/signal/signal_unix.go index 5c1ad5f7..d4fea931 100644 --- a/signal/signal_unix.go +++ b/signal/signal_unix.go @@ -9,8 +9,11 @@ import ( // Signals used in api/client (no windows equivalent, use // invalid signals so they don't get handled) -// SIGCHLD is a signal sent to a process when a child process terminates, is interrupted, or resumes after being interrupted. -const SIGCHLD = syscall.SIGCHLD - -// SIGWINCH is a signal sent to a process when its controlling terminal changes its size -const SIGWINCH = syscall.SIGWINCH +const ( + // SIGCHLD is a signal sent to a process when a child process terminates, is interrupted, or resumes after being interrupted. + SIGCHLD = syscall.SIGCHLD + // SIGWINCH is a signal sent to a process when its controlling terminal changes its size + SIGWINCH = syscall.SIGWINCH + // DefaultStopSignal is the syscall signal used to stop a container in unix systems. + DefaultStopSignal = "SIGTERM" +) diff --git a/signal/signal_windows.go b/signal/signal_windows.go index 1f1a6edb..b0585b0e 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -11,4 +11,6 @@ import ( const ( SIGCHLD = syscall.Signal(0xff) SIGWINCH = syscall.Signal(0xff) + // DefaultStopSignal is the syscall signal used to stop a container in windows systems. + DefaultStopSignal = "15" ) From 016016b7b71c6af483b56dee65e0819b69f3b105 Mon Sep 17 00:00:00 2001 From: John Howard Date: Mon, 12 Oct 2015 14:32:59 -0700 Subject: [PATCH 19/60] Windows: Enable kill (part one) Signed-off-by: John Howard --- signal/signal_unsupported.go | 2 +- signal/signal_windows.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go index cefb2407..161ba273 100644 --- a/signal/signal_unsupported.go +++ b/signal/signal_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin,!freebsd +// +build !linux,!darwin,!freebsd,!windows package signal diff --git a/signal/signal_windows.go b/signal/signal_windows.go index b0585b0e..c80a951c 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -14,3 +14,14 @@ const ( // DefaultStopSignal is the syscall signal used to stop a container in windows systems. DefaultStopSignal = "15" ) + +// SignalMap is a map of "supported" signals. As per the comment in GOLang's +// ztypes_windows.go: "More invented values for signals". Windows doesn't +// really support signals in any way, shape or form that Unix does. +// +// We have these so that docker kill can be used to gracefully (TERM) and +// forcibly (KILL) terminate a container on Windows. +var SignalMap = map[string]syscall.Signal{ + "KILL": syscall.SIGKILL, + "TERM": syscall.SIGTERM, +} From 045dcddb1c572388d80509dbed821d43e8919221 Mon Sep 17 00:00:00 2001 From: John Howard Date: Mon, 12 Oct 2015 16:34:03 -0700 Subject: [PATCH 20/60] Windows: [TP4] docker kill handling Signed-off-by: John Howard --- signal/signal.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/signal/signal.go b/signal/signal.go index 106fe20d..68bb77cf 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -42,3 +42,13 @@ func ParseSignal(rawSignal string) (syscall.Signal, error) { } return signal, nil } + +// ValidSignalForPlatform returns true if a signal is valid on the platform +func ValidSignalForPlatform(sig syscall.Signal) bool { + for _, v := range SignalMap { + if v == sig { + return true + } + } + return false +} From 751bbc7c63e08c3838a610f71dc203c43aadbbb0 Mon Sep 17 00:00:00 2001 From: Dan Walsh Date: Sat, 7 Nov 2015 11:53:10 +0100 Subject: [PATCH 21/60] Define all of the signals defined by kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX This will allow users to specify signals by name. Needed to make docker run --stop-signal work as defined by systemd. man systemd defines the proper way to shutdown a systemd running as pid 1 in a container as SIGRTMIN+4 Signed-off-by: Dan Walsh --- signal/signal_linux.go | 106 +++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 35 deletions(-) diff --git a/signal/signal_linux.go b/signal/signal_linux.go index 1ecc3294..d418cbe9 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -4,41 +4,77 @@ import ( "syscall" ) +const ( + sigrtmin = 34 + sigrtmax = 64 +) + // SignalMap is a map of Linux signals. var SignalMap = map[string]syscall.Signal{ - "ABRT": syscall.SIGABRT, - "ALRM": syscall.SIGALRM, - "BUS": syscall.SIGBUS, - "CHLD": syscall.SIGCHLD, - "CLD": syscall.SIGCLD, - "CONT": syscall.SIGCONT, - "FPE": syscall.SIGFPE, - "HUP": syscall.SIGHUP, - "ILL": syscall.SIGILL, - "INT": syscall.SIGINT, - "IO": syscall.SIGIO, - "IOT": syscall.SIGIOT, - "KILL": syscall.SIGKILL, - "PIPE": syscall.SIGPIPE, - "POLL": syscall.SIGPOLL, - "PROF": syscall.SIGPROF, - "PWR": syscall.SIGPWR, - "QUIT": syscall.SIGQUIT, - "SEGV": syscall.SIGSEGV, - "STKFLT": syscall.SIGSTKFLT, - "STOP": syscall.SIGSTOP, - "SYS": syscall.SIGSYS, - "TERM": syscall.SIGTERM, - "TRAP": syscall.SIGTRAP, - "TSTP": syscall.SIGTSTP, - "TTIN": syscall.SIGTTIN, - "TTOU": syscall.SIGTTOU, - "UNUSED": syscall.SIGUNUSED, - "URG": syscall.SIGURG, - "USR1": syscall.SIGUSR1, - "USR2": syscall.SIGUSR2, - "VTALRM": syscall.SIGVTALRM, - "WINCH": syscall.SIGWINCH, - "XCPU": syscall.SIGXCPU, - "XFSZ": syscall.SIGXFSZ, + "ABRT": syscall.SIGABRT, + "ALRM": syscall.SIGALRM, + "BUS": syscall.SIGBUS, + "CHLD": syscall.SIGCHLD, + "CLD": syscall.SIGCLD, + "CONT": syscall.SIGCONT, + "FPE": syscall.SIGFPE, + "HUP": syscall.SIGHUP, + "ILL": syscall.SIGILL, + "INT": syscall.SIGINT, + "IO": syscall.SIGIO, + "IOT": syscall.SIGIOT, + "KILL": syscall.SIGKILL, + "PIPE": syscall.SIGPIPE, + "POLL": syscall.SIGPOLL, + "PROF": syscall.SIGPROF, + "PWR": syscall.SIGPWR, + "QUIT": syscall.SIGQUIT, + "SEGV": syscall.SIGSEGV, + "STKFLT": syscall.SIGSTKFLT, + "STOP": syscall.SIGSTOP, + "SYS": syscall.SIGSYS, + "TERM": syscall.SIGTERM, + "TRAP": syscall.SIGTRAP, + "TSTP": syscall.SIGTSTP, + "TTIN": syscall.SIGTTIN, + "TTOU": syscall.SIGTTOU, + "UNUSED": syscall.SIGUNUSED, + "URG": syscall.SIGURG, + "USR1": syscall.SIGUSR1, + "USR2": syscall.SIGUSR2, + "VTALRM": syscall.SIGVTALRM, + "WINCH": syscall.SIGWINCH, + "XCPU": syscall.SIGXCPU, + "XFSZ": syscall.SIGXFSZ, + "RTMIN": sigrtmin, + "RTMIN+1": sigrtmin + 1, + "RTMIN+2": sigrtmin + 2, + "RTMIN+3": sigrtmin + 3, + "RTMIN+4": sigrtmin + 4, + "RTMIN+5": sigrtmin + 5, + "RTMIN+6": sigrtmin + 6, + "RTMIN+7": sigrtmin + 7, + "RTMIN+8": sigrtmin + 8, + "RTMIN+9": sigrtmin + 9, + "RTMIN+10": sigrtmin + 10, + "RTMIN+11": sigrtmin + 11, + "RTMIN+12": sigrtmin + 12, + "RTMIN+13": sigrtmin + 13, + "RTMIN+14": sigrtmin + 14, + "RTMIN+15": sigrtmin + 15, + "RTMAX-14": sigrtmax - 14, + "RTMAX-13": sigrtmax - 13, + "RTMAX-12": sigrtmax - 12, + "RTMAX-11": sigrtmax - 11, + "RTMAX-10": sigrtmax - 10, + "RTMAX-9": sigrtmax - 9, + "RTMAX-8": sigrtmax - 8, + "RTMAX-7": sigrtmax - 7, + "RTMAX-6": sigrtmax - 6, + "RTMAX-5": sigrtmax - 5, + "RTMAX-4": sigrtmax - 4, + "RTMAX-3": sigrtmax - 3, + "RTMAX-2": sigrtmax - 2, + "RTMAX-1": sigrtmax - 1, + "RTMAX": sigrtmax, } From 78c1827d85762ebc6d3832090840dd91d0be39c4 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Fri, 18 Mar 2016 16:50:18 -0400 Subject: [PATCH 22/60] Don't forward SIGPIPE from client to container Signed-off-by: Brian Goff --- signal/signal_unix.go | 2 ++ signal/signal_windows.go | 1 + 2 files changed, 3 insertions(+) diff --git a/signal/signal_unix.go b/signal/signal_unix.go index d4fea931..6621d371 100644 --- a/signal/signal_unix.go +++ b/signal/signal_unix.go @@ -14,6 +14,8 @@ const ( SIGCHLD = syscall.SIGCHLD // SIGWINCH is a signal sent to a process when its controlling terminal changes its size SIGWINCH = syscall.SIGWINCH + // SIGPIPE is a signal sent to a process when a pipe is written to before the other end is open for reading + SIGPIPE = syscall.SIGPIPE // DefaultStopSignal is the syscall signal used to stop a container in unix systems. DefaultStopSignal = "SIGTERM" ) diff --git a/signal/signal_windows.go b/signal/signal_windows.go index c80a951c..698cbf2d 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -11,6 +11,7 @@ import ( const ( SIGCHLD = syscall.Signal(0xff) SIGWINCH = syscall.Signal(0xff) + SIGPIPE = syscall.Signal(0xff) // DefaultStopSignal is the syscall signal used to stop a container in windows systems. DefaultStopSignal = "15" ) From b74cc08fab8512cc63497513060b317d27eed7e1 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Thu, 5 May 2016 14:03:28 -0700 Subject: [PATCH 23/60] Ignore SIGPIPE events, resolves #19728 Using golang 1.6, is it now possible to ignore SIGPIPE events on stdout/stderr. Previous versions of the golang library cached 10 events and then killed the process receiving the events. systemd-journald sends SIGPIPE events when jounald is restarted and the target of the unit file writes to stdout/stderr. Docker logs to stdout/stderr. This patch silently ignores all SIGPIPE events. Signed-off-by: Jhon Honce --- signal/trap.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 2cf5ccf0..874509e7 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -18,15 +18,22 @@ import ( // * 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) // * A SIGQUIT always causes an exit without cleanup, with a goroutine dump preceding exit. +// * 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()) { c := make(chan os.Signal, 1) - // we will handle INT, TERM, QUIT here - signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT} + // we will handle INT, TERM, QUIT, SIGPIPE here + signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE} gosignal.Notify(c, signals...) go func() { interruptCount := uint32(0) for sig := range c { + if sig == syscall.SIGPIPE { + continue + } + go func(sig os.Signal) { logrus.Infof("Processing signal '%v'", sig) switch sig { From 3b28f755911604b5da118c432c998c85fafc63b9 Mon Sep 17 00:00:00 2001 From: Amit Krishnan Date: Fri, 25 Mar 2016 16:38:00 -0700 Subject: [PATCH 24/60] Get the Docker Engine to build clean on Solaris Signed-off-by: Amit Krishnan --- signal/signal_solaris.go | 42 ++++++++++++++++++++++++++++++++++++ signal/signal_unsupported.go | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 signal/signal_solaris.go diff --git a/signal/signal_solaris.go b/signal/signal_solaris.go new file mode 100644 index 00000000..89576b9e --- /dev/null +++ b/signal/signal_solaris.go @@ -0,0 +1,42 @@ +package signal + +import ( + "syscall" +) + +// SignalMap is a map of Solaris signals. +// SIGINFO and SIGTHR not defined for Solaris +var SignalMap = map[string]syscall.Signal{ + "ABRT": syscall.SIGABRT, + "ALRM": syscall.SIGALRM, + "BUF": syscall.SIGBUS, + "CHLD": syscall.SIGCHLD, + "CONT": syscall.SIGCONT, + "EMT": syscall.SIGEMT, + "FPE": syscall.SIGFPE, + "HUP": syscall.SIGHUP, + "ILL": syscall.SIGILL, + "INT": syscall.SIGINT, + "IO": syscall.SIGIO, + "IOT": syscall.SIGIOT, + "KILL": syscall.SIGKILL, + "LWP": syscall.SIGLWP, + "PIPE": syscall.SIGPIPE, + "PROF": syscall.SIGPROF, + "QUIT": syscall.SIGQUIT, + "SEGV": syscall.SIGSEGV, + "STOP": syscall.SIGSTOP, + "SYS": syscall.SIGSYS, + "TERM": syscall.SIGTERM, + "TRAP": syscall.SIGTRAP, + "TSTP": syscall.SIGTSTP, + "TTIN": syscall.SIGTTIN, + "TTOU": syscall.SIGTTOU, + "URG": syscall.SIGURG, + "USR1": syscall.SIGUSR1, + "USR2": syscall.SIGUSR2, + "VTALRM": syscall.SIGVTALRM, + "WINCH": syscall.SIGWINCH, + "XCPU": syscall.SIGXCPU, + "XFSZ": syscall.SIGXFSZ, +} diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go index 161ba273..c592d37d 100644 --- a/signal/signal_unsupported.go +++ b/signal/signal_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin,!freebsd,!windows +// +build !linux,!darwin,!freebsd,!windows,!solaris package signal From 3d98fe1abdaf018b70679afa3e55dbd5db629855 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Sat, 11 Jun 2016 13:16:55 -0700 Subject: [PATCH 25/60] Fix logrus formatting This fix tries to fix logrus formatting by removing `f` from `logrus.[Error|Warn|Debug|Fatal|Panic|Info]f` when formatting string is not present. This fix fixes #23459. Signed-off-by: Yong Tang --- signal/trap.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 874509e7..d35ef0e8 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -49,11 +49,11 @@ func Trap(cleanup func()) { } } else { // 3 SIGTERM/INT signals received; force exit without cleanup - logrus.Infof("Forcing docker daemon shutdown without cleanup; 3 interrupts received") + logrus.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received") } case syscall.SIGQUIT: DumpStacks() - logrus.Infof("Forcing docker daemon shutdown without cleanup on SIGQUIT") + logrus.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT") } //for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # os.Exit(128 + int(sig.(syscall.Signal))) From 582b0754d6ea18f20f73f450e9bc21225185d705 Mon Sep 17 00:00:00 2001 From: John Howard Date: Tue, 28 Jun 2016 17:12:31 -0700 Subject: [PATCH 26/60] Windows: Stack dump to file Signed-off-by: John Howard --- signal/trap.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index d35ef0e8..bd8675c9 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -3,9 +3,11 @@ package signal import ( "os" gosignal "os/signal" + "path/filepath" "runtime" "sync/atomic" "syscall" + "time" "github.com/Sirupsen/logrus" ) @@ -52,7 +54,7 @@ func Trap(cleanup func()) { logrus.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received") } case syscall.SIGQUIT: - DumpStacks() + DumpStacks("") logrus.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT") } //for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # @@ -63,7 +65,7 @@ func Trap(cleanup func()) { } // DumpStacks dumps the runtime stack. -func DumpStacks() { +func DumpStacks(root string) { var ( buf []byte stackSize int @@ -77,5 +79,30 @@ func DumpStacks() { buf = buf[:stackSize] // Note that if the daemon is started with a less-verbose log-level than "info" (the default), the goroutine // traces won't show up in the log. - logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf) + if root == "" { + logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf) + } else { + // Dumps the stacks to a file in the root directory of the daemon + // On Windows, this overcomes two issues - one being that if the stack is too big, it doesn't + // get written to the event log when the Windows daemon is running as a service. + // Second, using logrus, the tabs and new-lines end up getting written as literal + // \t and \n's, meaning you need to use something like notepad++ to convert the + // output into something readable using 'type' from a command line or notepad/notepad++ etc. + path := filepath.Join(root, "goroutine-stacks.log") + f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) + if err != nil { + logrus.Warnf("Could not open %s to write the goroutine stacks: %v", path, err) + return + } + defer f.Close() + f.WriteString("=== BEGIN goroutine stack dump ===\n") + f.WriteString(time.Now().String() + "\n") + if _, err := f.Write(buf); err != nil { + logrus.Warnf("Could not write goroutine stacks to %s: %v", path, err) + return + } + f.WriteString("=== END goroutine stack dump ===\n") + f.Sync() + logrus.Infof("goroutine stacks written to %s", path) + } } From cf692f0faf99f122b6130250c2c8750acc33bfd6 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 8 Sep 2016 14:54:01 -0400 Subject: [PATCH 27/60] Replace api/client imports with cli/command in experimental files. Using git grep -l 'client\.DockerCli' cli/command/stack/ | xargs sed -i -e 's/client\.DockerCli/command\.Dockercli/g' Signed-off-by: Daniel Nephin --- signal/signal_unix.go | 2 +- signal/signal_windows.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/signal/signal_unix.go b/signal/signal_unix.go index 6621d371..5d058fd5 100644 --- a/signal/signal_unix.go +++ b/signal/signal_unix.go @@ -6,7 +6,7 @@ import ( "syscall" ) -// Signals used in api/client (no windows equivalent, use +// Signals used in cli/command (no windows equivalent, use // invalid signals so they don't get handled) const ( diff --git a/signal/signal_windows.go b/signal/signal_windows.go index 698cbf2d..440f2700 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -6,7 +6,7 @@ import ( "syscall" ) -// Signals used in api/client (no windows equivalent, use +// Signals used in cli/command (no windows equivalent, use // invalid signals so they don't get handled) const ( SIGCHLD = syscall.Signal(0xff) From 31898808c4da4b272e802666a42ef15a02b2e9cf Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Tue, 1 Nov 2016 07:32:55 -0700 Subject: [PATCH 28/60] daemon: always dump stack to file Dumping to log is unusable in 90% of cases and inspecting file is much more convenient. Signed-off-by: Alexander Morozov --- signal/trap.go | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index bd8675c9..120c34aa 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -1,6 +1,7 @@ package signal import ( + "fmt" "os" gosignal "os/signal" "path/filepath" @@ -10,6 +11,7 @@ import ( "time" "github.com/Sirupsen/logrus" + "github.com/pkg/errors" ) // Trap sets up a simplified signal "trap", appropriate for common @@ -64,8 +66,11 @@ func Trap(cleanup func()) { }() } -// DumpStacks dumps the runtime stack. -func DumpStacks(root string) { +const stacksLogNameTemplate = "goroutine-stacks-%s.log" + +// DumpStacks appends the runtime stack into file in dir and returns full path +// to that file. +func DumpStacks(dir string) (string, error) { var ( buf []byte stackSize int @@ -77,32 +82,15 @@ func DumpStacks(root string) { bufferLen *= 2 } buf = buf[:stackSize] - // Note that if the daemon is started with a less-verbose log-level than "info" (the default), the goroutine - // traces won't show up in the log. - if root == "" { - logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf) - } else { - // Dumps the stacks to a file in the root directory of the daemon - // On Windows, this overcomes two issues - one being that if the stack is too big, it doesn't - // get written to the event log when the Windows daemon is running as a service. - // Second, using logrus, the tabs and new-lines end up getting written as literal - // \t and \n's, meaning you need to use something like notepad++ to convert the - // output into something readable using 'type' from a command line or notepad/notepad++ etc. - path := filepath.Join(root, "goroutine-stacks.log") - f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) - if err != nil { - logrus.Warnf("Could not open %s to write the goroutine stacks: %v", path, err) - return - } - defer f.Close() - f.WriteString("=== BEGIN goroutine stack dump ===\n") - f.WriteString(time.Now().String() + "\n") - if _, err := f.Write(buf); err != nil { - logrus.Warnf("Could not write goroutine stacks to %s: %v", path, err) - return - } - f.WriteString("=== END goroutine stack dump ===\n") - f.Sync() - logrus.Infof("goroutine stacks written to %s", path) + path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, time.Now().Format(time.RFC3339))) + f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") + } + defer f.Close() + if _, err := f.Write(buf); err != nil { + return "", errors.Wrap(err, "failed to write goroutine stacks") } + f.Sync() + return path, nil } From 227231b262846646b2cf7bd391f2dcade52baf0f Mon Sep 17 00:00:00 2001 From: John Howard Date: Wed, 12 Oct 2016 15:29:47 -0700 Subject: [PATCH 29/60] Spew debugging Signed-off-by: John Howard --- signal/trap.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/signal/trap.go b/signal/trap.go index 120c34aa..44c578ae 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -6,6 +6,7 @@ import ( gosignal "os/signal" "path/filepath" "runtime" + "strings" "sync/atomic" "syscall" "time" @@ -82,7 +83,7 @@ func DumpStacks(dir string) (string, error) { bufferLen *= 2 } buf = buf[:stackSize] - path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, time.Now().Format(time.RFC3339))) + path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1))) f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) if err != nil { return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") From 08c4913068afe2feab93aa0454da6395a9b8239a Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Wed, 16 Nov 2016 12:55:18 -0500 Subject: [PATCH 30/60] Move stack dump dir to exec root Dump stack dumps to exec root instead of daemon root. When no path is provided to the stack dumper, such is the case with SIGQUIT, dump to stderr. Signed-off-by: Brian Goff --- signal/trap.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 44c578ae..638a1ab6 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -83,15 +83,21 @@ func DumpStacks(dir string) (string, error) { bufferLen *= 2 } buf = buf[:stackSize] - path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1))) - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) - if err != nil { - return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") + var f *os.File + if dir != "" { + path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1))) + var err error + f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") + } + defer f.Close() + defer f.Sync() + } else { + f = os.Stderr } - defer f.Close() if _, err := f.Write(buf); err != nil { return "", errors.Wrap(err, "failed to write goroutine stacks") } - f.Sync() - return path, nil + return f.Name(), nil } From 61af6ce28aee9ea5f5e464d2b9ed16ad2158eb0b Mon Sep 17 00:00:00 2001 From: Naveed Jamil Date: Wed, 31 May 2017 21:40:41 +0500 Subject: [PATCH 31/60] Add test coverage to pkg/signal Signed-off-by: Naveed Jamil --- signal/signal_linux_test.go | 58 +++++++++++++++++++++++++++++++++++++ signal/signal_test.go | 33 +++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 signal/signal_linux_test.go create mode 100644 signal/signal_test.go diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go new file mode 100644 index 00000000..32c056fe --- /dev/null +++ b/signal/signal_linux_test.go @@ -0,0 +1,58 @@ +// +build darwin linux solaris + +package signal + +import ( + "os" + "syscall" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestCatchAll(t *testing.T) { + sigs := make(chan os.Signal, 1) + CatchAll(sigs) + defer StopCatch(sigs) + + listOfSignals := map[string]string{ + "CONT": syscall.SIGCONT.String(), + "HUP": syscall.SIGHUP.String(), + "CHLD": syscall.SIGCHLD.String(), + "ILL": syscall.SIGILL.String(), + "FPE": syscall.SIGFPE.String(), + "CLD": syscall.SIGCLD.String(), + } + + for sigStr := range listOfSignals { + signal, ok := SignalMap[sigStr] + if ok { + go func() { + time.Sleep(1 * time.Millisecond) + syscall.Kill(syscall.Getpid(), signal) + }() + + s := <-sigs + assert.EqualValues(t, s.String(), signal.String()) + } + + } +} + +func TestStopCatch(t *testing.T) { + signal, _ := SignalMap["HUP"] + channel := make(chan os.Signal, 1) + CatchAll(channel) + go func() { + + time.Sleep(1 * time.Millisecond) + syscall.Kill(syscall.Getpid(), signal) + }() + signalString := <-channel + assert.EqualValues(t, signalString.String(), signal.String()) + + StopCatch(channel) + _, ok := <-channel + assert.EqualValues(t, ok, false) +} diff --git a/signal/signal_test.go b/signal/signal_test.go new file mode 100644 index 00000000..df02f5be --- /dev/null +++ b/signal/signal_test.go @@ -0,0 +1,33 @@ +package signal + +import ( + "syscall" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseSignal(t *testing.T) { + _, checkAtoiError := ParseSignal("0") + assert.EqualError(t, checkAtoiError, "Invalid signal: 0") + + _, error := ParseSignal("SIG") + assert.EqualError(t, error, "Invalid signal: SIG") + + for sigStr := range SignalMap { + responseSignal, error := ParseSignal(sigStr) + assert.NoError(t, error) + signal := SignalMap[sigStr] + assert.EqualValues(t, signal, responseSignal) + } +} + +func TestValidSignalForPlatform(t *testing.T) { + isValidSignal := ValidSignalForPlatform(syscall.Signal(0)) + assert.EqualValues(t, false, isValidSignal) + + for _, sigN := range SignalMap { + isValidSignal = ValidSignalForPlatform(syscall.Signal(sigN)) + assert.EqualValues(t, true, isValidSignal) + } +} From d46cd5e82abfe8adc5436676f4728d4658981c10 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Tue, 23 May 2017 10:22:32 -0400 Subject: [PATCH 32/60] [project] change syscall to /x/sys/unix|windows Changes most references of syscall to golang.org/x/sys/ Ones aren't changes include, Errno, Signal and SysProcAttr as they haven't been implemented in /x/sys/. Signed-off-by: Christopher Jones [s390x] switch utsname from unsigned to signed per https://github.com/golang/sys/commit/33267e036fd93fcd26ea95b7bdaf2d8306cb743c char in s390x in the /x/sys/unix package is now signed, so change the buildtags Signed-off-by: Christopher Jones --- signal/signal_linux.go | 72 ++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/signal/signal_linux.go b/signal/signal_linux.go index d418cbe9..3594796c 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -2,6 +2,8 @@ package signal import ( "syscall" + + "golang.org/x/sys/unix" ) const ( @@ -11,41 +13,41 @@ const ( // SignalMap is a map of Linux signals. var SignalMap = map[string]syscall.Signal{ - "ABRT": syscall.SIGABRT, - "ALRM": syscall.SIGALRM, - "BUS": syscall.SIGBUS, - "CHLD": syscall.SIGCHLD, - "CLD": syscall.SIGCLD, - "CONT": syscall.SIGCONT, - "FPE": syscall.SIGFPE, - "HUP": syscall.SIGHUP, - "ILL": syscall.SIGILL, - "INT": syscall.SIGINT, - "IO": syscall.SIGIO, - "IOT": syscall.SIGIOT, - "KILL": syscall.SIGKILL, - "PIPE": syscall.SIGPIPE, - "POLL": syscall.SIGPOLL, - "PROF": syscall.SIGPROF, - "PWR": syscall.SIGPWR, - "QUIT": syscall.SIGQUIT, - "SEGV": syscall.SIGSEGV, - "STKFLT": syscall.SIGSTKFLT, - "STOP": syscall.SIGSTOP, - "SYS": syscall.SIGSYS, - "TERM": syscall.SIGTERM, - "TRAP": syscall.SIGTRAP, - "TSTP": syscall.SIGTSTP, - "TTIN": syscall.SIGTTIN, - "TTOU": syscall.SIGTTOU, - "UNUSED": syscall.SIGUNUSED, - "URG": syscall.SIGURG, - "USR1": syscall.SIGUSR1, - "USR2": syscall.SIGUSR2, - "VTALRM": syscall.SIGVTALRM, - "WINCH": syscall.SIGWINCH, - "XCPU": syscall.SIGXCPU, - "XFSZ": syscall.SIGXFSZ, + "ABRT": unix.SIGABRT, + "ALRM": unix.SIGALRM, + "BUS": unix.SIGBUS, + "CHLD": unix.SIGCHLD, + "CLD": unix.SIGCLD, + "CONT": unix.SIGCONT, + "FPE": unix.SIGFPE, + "HUP": unix.SIGHUP, + "ILL": unix.SIGILL, + "INT": unix.SIGINT, + "IO": unix.SIGIO, + "IOT": unix.SIGIOT, + "KILL": unix.SIGKILL, + "PIPE": unix.SIGPIPE, + "POLL": unix.SIGPOLL, + "PROF": unix.SIGPROF, + "PWR": unix.SIGPWR, + "QUIT": unix.SIGQUIT, + "SEGV": unix.SIGSEGV, + "STKFLT": unix.SIGSTKFLT, + "STOP": unix.SIGSTOP, + "SYS": unix.SIGSYS, + "TERM": unix.SIGTERM, + "TRAP": unix.SIGTRAP, + "TSTP": unix.SIGTSTP, + "TTIN": unix.SIGTTIN, + "TTOU": unix.SIGTTOU, + "UNUSED": unix.SIGUNUSED, + "URG": unix.SIGURG, + "USR1": unix.SIGUSR1, + "USR2": unix.SIGUSR2, + "VTALRM": unix.SIGVTALRM, + "WINCH": unix.SIGWINCH, + "XCPU": unix.SIGXCPU, + "XFSZ": unix.SIGXFSZ, "RTMIN": sigrtmin, "RTMIN+1": sigrtmin + 1, "RTMIN+2": sigrtmin + 2, From 9322768c34150d0401c3c401dd61b3aeaacd3c2a Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Wed, 26 Jul 2017 14:42:13 -0700 Subject: [PATCH 33/60] Update logrus to v1.0.1 Fixes case sensitivity issue Signed-off-by: Derek McGowan --- signal/trap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/trap.go b/signal/trap.go index 638a1ab6..172bc106 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -11,8 +11,8 @@ import ( "syscall" "time" - "github.com/Sirupsen/logrus" "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) // Trap sets up a simplified signal "trap", appropriate for common From 79c9827f62b4b0ce06470c12ae93115d92870274 Mon Sep 17 00:00:00 2001 From: yuexiao-wang Date: Tue, 1 Aug 2017 11:32:57 +0800 Subject: [PATCH 34/60] Remove the logrus from pkg/signal Signed-off-by: yuexiao-wang --- signal/trap.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 172bc106..2884dfee 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -12,7 +12,6 @@ import ( "time" "github.com/pkg/errors" - "github.com/sirupsen/logrus" ) // Trap sets up a simplified signal "trap", appropriate for common @@ -27,7 +26,9 @@ import ( // the docker daemon is not restarted and also running under systemd. // Fixes https://github.com/docker/docker/issues/19728 // -func Trap(cleanup func()) { +func Trap(cleanup func(), logger interface { + Info(args ...interface{}) +}) { c := make(chan os.Signal, 1) // we will handle INT, TERM, QUIT, SIGPIPE here signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE} @@ -40,7 +41,7 @@ func Trap(cleanup func()) { } go func(sig os.Signal) { - logrus.Infof("Processing signal '%v'", sig) + logger.Info(fmt.Sprintf("Processing signal '%v'", sig)) switch sig { case os.Interrupt, syscall.SIGTERM: if atomic.LoadUint32(&interruptCount) < 3 { @@ -54,11 +55,11 @@ func Trap(cleanup func()) { } } else { // 3 SIGTERM/INT signals received; force exit without cleanup - logrus.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received") + logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received") } case syscall.SIGQUIT: DumpStacks("") - logrus.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT") + logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT") } //for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # os.Exit(128 + int(sig.(syscall.Signal))) From e51b1498cb37ea12cded7c1ecf2e5faf045762d6 Mon Sep 17 00:00:00 2001 From: Naveed Jamil Date: Thu, 1 Jun 2017 11:39:35 +0500 Subject: [PATCH 35/60] Add test coverage to signal/trap.go Signed-off-by: Naveed Jamil --- signal/testfiles/main.go | 43 ++++++++++++++++++++ signal/trap_linux_test.go | 82 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 signal/testfiles/main.go create mode 100644 signal/trap_linux_test.go diff --git a/signal/testfiles/main.go b/signal/testfiles/main.go new file mode 100644 index 00000000..e56854c7 --- /dev/null +++ b/signal/testfiles/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "os" + "syscall" + "time" + + "github.com/docker/docker/pkg/signal" + "github.com/sirupsen/logrus" +) + +func main() { + sigmap := map[string]os.Signal{ + "TERM": syscall.SIGTERM, + "QUIT": syscall.SIGQUIT, + "INT": os.Interrupt, + } + signal.Trap(func() { + time.Sleep(time.Second) + os.Exit(99) + }, logrus.StandardLogger()) + go func() { + p, err := os.FindProcess(os.Getpid()) + if err != nil { + panic(err) + } + s := os.Getenv("SIGNAL_TYPE") + multiple := os.Getenv("IF_MULTIPLE") + switch s { + case "TERM", "INT": + if multiple == "1" { + for { + p.Signal(sigmap[s]) + } + } else { + p.Signal(sigmap[s]) + } + case "QUIT": + p.Signal(sigmap[s]) + } + }() + time.Sleep(2 * time.Second) +} diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go new file mode 100644 index 00000000..2622766b --- /dev/null +++ b/signal/trap_linux_test.go @@ -0,0 +1,82 @@ +// +build linux + +package signal + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "syscall" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func buildTestBinary(t *testing.T, tmpdir string, prefix string) (string, string) { + tmpDir, err := ioutil.TempDir(tmpdir, prefix) + require.NoError(t, err) + exePath := tmpDir + "/" + prefix + wd, _ := os.Getwd() + testHelperCode := wd + "/testfiles/main.go" + cmd := exec.Command("go", "build", "-o", exePath, testHelperCode) + err = cmd.Run() + require.NoError(t, err) + return exePath, tmpDir +} + +func TestTrap(t *testing.T) { + var sigmap = []struct { + name string + signal os.Signal + multiple bool + }{ + {"TERM", syscall.SIGTERM, false}, + {"QUIT", syscall.SIGQUIT, true}, + {"INT", os.Interrupt, false}, + {"TERM", syscall.SIGTERM, true}, + {"INT", os.Interrupt, true}, + } + exePath, tmpDir := buildTestBinary(t, "", "main") + defer os.RemoveAll(tmpDir) + + for _, v := range sigmap { + cmd := exec.Command(exePath) + cmd.Env = append(os.Environ(), fmt.Sprintf("SIGNAL_TYPE=%s", v.name)) + if v.multiple { + cmd.Env = append(cmd.Env, "IF_MULTIPLE=1") + } + err := cmd.Start() + require.NoError(t, err) + err = cmd.Wait() + if e, ok := err.(*exec.ExitError); ok { + code := e.Sys().(syscall.WaitStatus).ExitStatus() + if v.multiple { + assert.Equal(t, 128+int(v.signal.(syscall.Signal)), code) + } else { + assert.Equal(t, 99, code) + } + continue + } + t.Fatal("process didn't end with any error") + } + +} + +func TestDumpStacks(t *testing.T) { + directory, err := ioutil.TempDir("", "test-dump-tasks") + assert.NoError(t, err) + defer os.RemoveAll(directory) + dumpPath, err := DumpStacks(directory) + assert.NoError(t, err) + readFile, _ := ioutil.ReadFile(dumpPath) + fileData := string(readFile) + assert.Contains(t, fileData, "goroutine") +} + +func TestDumpStacksWithEmptyInput(t *testing.T) { + path, err := DumpStacks("") + assert.NoError(t, err) + assert.Equal(t, os.Stderr.Name(), path) +} From e95420ba19bd5cf9cdbcaf7e56a151a4cf6ddffd Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 11 Sep 2017 14:55:05 -0400 Subject: [PATCH 36/60] Add gosimple linter Update gometalinter Signed-off-by: Daniel Nephin --- signal/signal_linux_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index 32c056fe..da0e0105 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -41,7 +41,7 @@ func TestCatchAll(t *testing.T) { } func TestStopCatch(t *testing.T) { - signal, _ := SignalMap["HUP"] + signal := SignalMap["HUP"] channel := make(chan os.Signal, 1) CatchAll(channel) go func() { From 5244b8493346d2ae774722139d8873d0e5181b54 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 18 Oct 2017 10:43:31 +0200 Subject: [PATCH 37/60] Use Mkdev, Major and Minor functions from golang.org/x/sys/unix Update golang.org/x/sys to 8dbc5d05d6edcc104950cc299a1ce6641235bc86 in order to get the Major, Minor and Mkdev functions for every unix-like OS. Use them instead of the locally defined versions which currently use the Linux specific device major/minor encoding. This means that the device number should now be properly encoded on e.g. Darwin, FreeBSD or Solaris. Also, the SIGUNUSED constant was removed from golang.org/x/sys/unix in https://go-review.googlesource.com/61771 as it is also removed from the respective glibc headers. Remove it from signal.SignalMap as well after the golang.org/x/sys re-vendoring. Signed-off-by: Tobias Klauser --- signal/signal_linux.go | 1 - 1 file changed, 1 deletion(-) diff --git a/signal/signal_linux.go b/signal/signal_linux.go index 3594796c..66c85c8e 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -40,7 +40,6 @@ var SignalMap = map[string]syscall.Signal{ "TSTP": unix.SIGTSTP, "TTIN": unix.SIGTTIN, "TTOU": unix.SIGTTOU, - "UNUSED": unix.SIGUNUSED, "URG": unix.SIGURG, "USR1": unix.SIGUSR1, "USR2": unix.SIGUSR2, From d965926004aa139d199b29a3a3f0b301bc887185 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 24 Oct 2017 14:32:52 -0400 Subject: [PATCH 38/60] Remove solaris files For obvious reasons that it is not really supported now. Signed-off-by: Michael Crosby --- signal/signal_solaris.go | 42 ---------------------------------------- 1 file changed, 42 deletions(-) delete mode 100644 signal/signal_solaris.go diff --git a/signal/signal_solaris.go b/signal/signal_solaris.go deleted file mode 100644 index 89576b9e..00000000 --- a/signal/signal_solaris.go +++ /dev/null @@ -1,42 +0,0 @@ -package signal - -import ( - "syscall" -) - -// SignalMap is a map of Solaris signals. -// SIGINFO and SIGTHR not defined for Solaris -var SignalMap = map[string]syscall.Signal{ - "ABRT": syscall.SIGABRT, - "ALRM": syscall.SIGALRM, - "BUF": syscall.SIGBUS, - "CHLD": syscall.SIGCHLD, - "CONT": syscall.SIGCONT, - "EMT": syscall.SIGEMT, - "FPE": syscall.SIGFPE, - "HUP": syscall.SIGHUP, - "ILL": syscall.SIGILL, - "INT": syscall.SIGINT, - "IO": syscall.SIGIO, - "IOT": syscall.SIGIOT, - "KILL": syscall.SIGKILL, - "LWP": syscall.SIGLWP, - "PIPE": syscall.SIGPIPE, - "PROF": syscall.SIGPROF, - "QUIT": syscall.SIGQUIT, - "SEGV": syscall.SIGSEGV, - "STOP": syscall.SIGSTOP, - "SYS": syscall.SIGSYS, - "TERM": syscall.SIGTERM, - "TRAP": syscall.SIGTRAP, - "TSTP": syscall.SIGTSTP, - "TTIN": syscall.SIGTTIN, - "TTOU": syscall.SIGTTOU, - "URG": syscall.SIGURG, - "USR1": syscall.SIGUSR1, - "USR2": syscall.SIGUSR2, - "VTALRM": syscall.SIGVTALRM, - "WINCH": syscall.SIGWINCH, - "XCPU": syscall.SIGXCPU, - "XFSZ": syscall.SIGXFSZ, -} From a55e48a1b30770ef8c94571690b5698a819cdde3 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 1 Nov 2017 23:37:53 +0000 Subject: [PATCH 39/60] Remove solaris build tag and `contrib/mkimage/solaris Signed-off-by: Yong Tang --- signal/signal_linux_test.go | 2 +- signal/signal_unsupported.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index da0e0105..8dc913b4 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -1,4 +1,4 @@ -// +build darwin linux solaris +// +build darwin linux package signal diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go index c592d37d..161ba273 100644 --- a/signal/signal_unsupported.go +++ b/signal/signal_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin,!freebsd,!windows,!solaris +// +build !linux,!darwin,!freebsd,!windows package signal From a3045af6077dcf263a0c6273772ff587b68541a6 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 18 Dec 2017 17:41:53 +0100 Subject: [PATCH 40/60] Remove redundant build-tags Files that are suffixed with `_linux.go` or `_windows.go` are already only built on Linux / Windows, so these build-tags were redundant. Signed-off-by: Sebastiaan van Stijn --- signal/signal_windows.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/signal/signal_windows.go b/signal/signal_windows.go index 440f2700..c84a63e8 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -1,5 +1,3 @@ -// +build windows - package signal import ( From 382c7e2ea650a5ae48577290a285a25772e6ec32 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 5 Feb 2018 16:05:59 -0500 Subject: [PATCH 41/60] Add canonical import comment Signed-off-by: Daniel Nephin --- signal/signal.go | 2 +- signal/signal_darwin.go | 2 +- signal/signal_freebsd.go | 2 +- signal/signal_linux.go | 2 +- signal/signal_linux_test.go | 2 +- signal/signal_test.go | 2 +- signal/signal_unix.go | 2 +- signal/signal_unsupported.go | 2 +- signal/signal_windows.go | 2 +- signal/trap.go | 2 +- signal/trap_linux_test.go | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/signal/signal.go b/signal/signal.go index 68bb77cf..6a663091 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -1,6 +1,6 @@ // Package signal provides helper functions for dealing with signals across // various operating systems. -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "fmt" diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go index 946de87e..ee5501e3 100644 --- a/signal/signal_darwin.go +++ b/signal/signal_darwin.go @@ -1,4 +1,4 @@ -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index 6b9569bb..764f90e2 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -1,4 +1,4 @@ -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" diff --git a/signal/signal_linux.go b/signal/signal_linux.go index 66c85c8e..caed97c9 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -1,4 +1,4 @@ -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index 8dc913b4..d7e8da25 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -1,6 +1,6 @@ // +build darwin linux -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "os" diff --git a/signal/signal_test.go b/signal/signal_test.go index df02f5be..1add526d 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -1,4 +1,4 @@ -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" diff --git a/signal/signal_unix.go b/signal/signal_unix.go index 5d058fd5..a2aa4248 100644 --- a/signal/signal_unix.go +++ b/signal/signal_unix.go @@ -1,6 +1,6 @@ // +build !windows -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go index 161ba273..1fd25a83 100644 --- a/signal/signal_unsupported.go +++ b/signal/signal_unsupported.go @@ -1,6 +1,6 @@ // +build !linux,!darwin,!freebsd,!windows -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" diff --git a/signal/signal_windows.go b/signal/signal_windows.go index c84a63e8..65752f24 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -1,4 +1,4 @@ -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" diff --git a/signal/trap.go b/signal/trap.go index 2884dfee..2a6e69fb 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -1,4 +1,4 @@ -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "fmt" diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go index 2622766b..d32a4366 100644 --- a/signal/trap_linux_test.go +++ b/signal/trap_linux_test.go @@ -1,6 +1,6 @@ // +build linux -package signal +package signal // import "github.com/docker/docker/pkg/signal" import ( "fmt" From 03a9d734b275a015c204639594b2d4099cd3b576 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 13 Mar 2018 15:28:34 -0400 Subject: [PATCH 42/60] Automated migration using gty-migrate-from-testify --ignore-build-tags Signed-off-by: Daniel Nephin --- signal/signal_linux_test.go | 9 +++++---- signal/signal_test.go | 15 ++++++++------- signal/trap_linux_test.go | 24 ++++++++++++------------ 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index d7e8da25..71c577ed 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -8,7 +8,8 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" + "github.com/gotestyourself/gotestyourself/assert" + is "github.com/gotestyourself/gotestyourself/assert/cmp" ) func TestCatchAll(t *testing.T) { @@ -34,7 +35,7 @@ func TestCatchAll(t *testing.T) { }() s := <-sigs - assert.EqualValues(t, s.String(), signal.String()) + assert.Check(t, is.Equal(s.String(), signal.String())) } } @@ -50,9 +51,9 @@ func TestStopCatch(t *testing.T) { syscall.Kill(syscall.Getpid(), signal) }() signalString := <-channel - assert.EqualValues(t, signalString.String(), signal.String()) + assert.Check(t, is.Equal(signalString.String(), signal.String())) StopCatch(channel) _, ok := <-channel - assert.EqualValues(t, ok, false) + assert.Check(t, is.Equal(ok, false)) } diff --git a/signal/signal_test.go b/signal/signal_test.go index 1add526d..bbf3736f 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -4,30 +4,31 @@ import ( "syscall" "testing" - "github.com/stretchr/testify/assert" + "github.com/gotestyourself/gotestyourself/assert" + is "github.com/gotestyourself/gotestyourself/assert/cmp" ) func TestParseSignal(t *testing.T) { _, checkAtoiError := ParseSignal("0") - assert.EqualError(t, checkAtoiError, "Invalid signal: 0") + assert.Check(t, is.Error(checkAtoiError, "Invalid signal: 0")) _, error := ParseSignal("SIG") - assert.EqualError(t, error, "Invalid signal: SIG") + assert.Check(t, is.Error(error, "Invalid signal: SIG")) for sigStr := range SignalMap { responseSignal, error := ParseSignal(sigStr) - assert.NoError(t, error) + assert.Check(t, error) signal := SignalMap[sigStr] - assert.EqualValues(t, signal, responseSignal) + assert.Check(t, is.DeepEqual(signal, responseSignal)) } } func TestValidSignalForPlatform(t *testing.T) { isValidSignal := ValidSignalForPlatform(syscall.Signal(0)) - assert.EqualValues(t, false, isValidSignal) + assert.Check(t, is.Equal(false, isValidSignal)) for _, sigN := range SignalMap { isValidSignal = ValidSignalForPlatform(syscall.Signal(sigN)) - assert.EqualValues(t, true, isValidSignal) + assert.Check(t, is.Equal(true, isValidSignal)) } } diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go index d32a4366..a3afe7a7 100644 --- a/signal/trap_linux_test.go +++ b/signal/trap_linux_test.go @@ -10,19 +10,19 @@ import ( "syscall" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/gotestyourself/gotestyourself/assert" + is "github.com/gotestyourself/gotestyourself/assert/cmp" ) func buildTestBinary(t *testing.T, tmpdir string, prefix string) (string, string) { tmpDir, err := ioutil.TempDir(tmpdir, prefix) - require.NoError(t, err) + assert.NilError(t, err) exePath := tmpDir + "/" + prefix wd, _ := os.Getwd() testHelperCode := wd + "/testfiles/main.go" cmd := exec.Command("go", "build", "-o", exePath, testHelperCode) err = cmd.Run() - require.NoError(t, err) + assert.NilError(t, err) return exePath, tmpDir } @@ -48,14 +48,14 @@ func TestTrap(t *testing.T) { cmd.Env = append(cmd.Env, "IF_MULTIPLE=1") } err := cmd.Start() - require.NoError(t, err) + assert.NilError(t, err) err = cmd.Wait() if e, ok := err.(*exec.ExitError); ok { code := e.Sys().(syscall.WaitStatus).ExitStatus() if v.multiple { - assert.Equal(t, 128+int(v.signal.(syscall.Signal)), code) + assert.Check(t, is.DeepEqual(128+int(v.signal.(syscall.Signal)), code)) } else { - assert.Equal(t, 99, code) + assert.Check(t, is.Equal(99, code)) } continue } @@ -66,17 +66,17 @@ func TestTrap(t *testing.T) { func TestDumpStacks(t *testing.T) { directory, err := ioutil.TempDir("", "test-dump-tasks") - assert.NoError(t, err) + assert.Check(t, err) defer os.RemoveAll(directory) dumpPath, err := DumpStacks(directory) - assert.NoError(t, err) + assert.Check(t, err) readFile, _ := ioutil.ReadFile(dumpPath) fileData := string(readFile) - assert.Contains(t, fileData, "goroutine") + assert.Check(t, is.Contains(fileData, "goroutine")) } func TestDumpStacksWithEmptyInput(t *testing.T) { path, err := DumpStacks("") - assert.NoError(t, err) - assert.Equal(t, os.Stderr.Name(), path) + assert.Check(t, err) + assert.Check(t, is.Equal(os.Stderr.Name(), path)) } From f73f92bc5b452cfdfb102dfc9d78d823483968a1 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 May 2018 13:38:54 +0200 Subject: [PATCH 43/60] Various code-cleanup remove unnescessary import aliases, brackets, and so on. Signed-off-by: Sebastiaan van Stijn --- signal/signal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/signal.go b/signal/signal.go index 6a663091..88ef7b5e 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -13,7 +13,7 @@ import ( // CatchAll catches all signals and relays them to the specified channel. func CatchAll(sigc chan os.Signal) { - handledSigs := []os.Signal{} + var handledSigs []os.Signal for _, s := range SignalMap { handledSigs = append(handledSigs, s) } From 1d91577a54113cea45795495639cdc763c1faec8 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 11 Jun 2018 15:32:11 +0200 Subject: [PATCH 44/60] =?UTF-8?q?Update=20tests=20to=20use=20gotest.tools?= =?UTF-8?q?=20=F0=9F=91=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Vincent Demeester --- signal/signal_linux_test.go | 4 ++-- signal/signal_test.go | 4 ++-- signal/trap_linux_test.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index 71c577ed..9a021e21 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -8,8 +8,8 @@ import ( "testing" "time" - "github.com/gotestyourself/gotestyourself/assert" - is "github.com/gotestyourself/gotestyourself/assert/cmp" + "gotest.tools/assert" + is "gotest.tools/assert/cmp" ) func TestCatchAll(t *testing.T) { diff --git a/signal/signal_test.go b/signal/signal_test.go index bbf3736f..0bfcf6ce 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -4,8 +4,8 @@ import ( "syscall" "testing" - "github.com/gotestyourself/gotestyourself/assert" - is "github.com/gotestyourself/gotestyourself/assert/cmp" + "gotest.tools/assert" + is "gotest.tools/assert/cmp" ) func TestParseSignal(t *testing.T) { diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go index a3afe7a7..14d15431 100644 --- a/signal/trap_linux_test.go +++ b/signal/trap_linux_test.go @@ -10,8 +10,8 @@ import ( "syscall" "testing" - "github.com/gotestyourself/gotestyourself/assert" - is "github.com/gotestyourself/gotestyourself/assert/cmp" + "gotest.tools/assert" + is "gotest.tools/assert/cmp" ) func buildTestBinary(t *testing.T, tmpdir string, prefix string) (string, string) { From 03beeec78af81e8859c93eb88882b059d0fc6bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Fab=C3=A6ch=20Brandt?= Date: Wed, 18 Jul 2018 14:31:16 +0200 Subject: [PATCH 45/60] SIGSKTFLT does not exist on MIPS, instead SIGEMT does. SIGRTMAX is also 127 on MIPS. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kasper Fabæch Brandt --- signal/signal_linux.go | 2 + signal/signal_linux_mipsx.go | 84 ++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 signal/signal_linux_mipsx.go diff --git a/signal/signal_linux.go b/signal/signal_linux.go index caed97c9..4013bded 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -1,3 +1,5 @@ +// +build !mips,!mipsle,!mips64,!mips64le + package signal // import "github.com/docker/docker/pkg/signal" import ( diff --git a/signal/signal_linux_mipsx.go b/signal/signal_linux_mipsx.go new file mode 100644 index 00000000..4c798912 --- /dev/null +++ b/signal/signal_linux_mipsx.go @@ -0,0 +1,84 @@ +// +build linux +// +build mips mipsle mips64 mips64le + +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +const ( + sigrtmin = 34 + sigrtmax = 127 +) + +// SignalMap is a map of Linux signals. +var SignalMap = map[string]syscall.Signal{ + "ABRT": unix.SIGABRT, + "ALRM": unix.SIGALRM, + "BUS": unix.SIGBUS, + "CHLD": unix.SIGCHLD, + "CLD": unix.SIGCLD, + "CONT": unix.SIGCONT, + "FPE": unix.SIGFPE, + "HUP": unix.SIGHUP, + "ILL": unix.SIGILL, + "INT": unix.SIGINT, + "IO": unix.SIGIO, + "IOT": unix.SIGIOT, + "KILL": unix.SIGKILL, + "PIPE": unix.SIGPIPE, + "POLL": unix.SIGPOLL, + "PROF": unix.SIGPROF, + "PWR": unix.SIGPWR, + "QUIT": unix.SIGQUIT, + "SEGV": unix.SIGSEGV, + "SIGEMT": unix.SIGEMT, + "STOP": unix.SIGSTOP, + "SYS": unix.SIGSYS, + "TERM": unix.SIGTERM, + "TRAP": unix.SIGTRAP, + "TSTP": unix.SIGTSTP, + "TTIN": unix.SIGTTIN, + "TTOU": unix.SIGTTOU, + "URG": unix.SIGURG, + "USR1": unix.SIGUSR1, + "USR2": unix.SIGUSR2, + "VTALRM": unix.SIGVTALRM, + "WINCH": unix.SIGWINCH, + "XCPU": unix.SIGXCPU, + "XFSZ": unix.SIGXFSZ, + "RTMIN": sigrtmin, + "RTMIN+1": sigrtmin + 1, + "RTMIN+2": sigrtmin + 2, + "RTMIN+3": sigrtmin + 3, + "RTMIN+4": sigrtmin + 4, + "RTMIN+5": sigrtmin + 5, + "RTMIN+6": sigrtmin + 6, + "RTMIN+7": sigrtmin + 7, + "RTMIN+8": sigrtmin + 8, + "RTMIN+9": sigrtmin + 9, + "RTMIN+10": sigrtmin + 10, + "RTMIN+11": sigrtmin + 11, + "RTMIN+12": sigrtmin + 12, + "RTMIN+13": sigrtmin + 13, + "RTMIN+14": sigrtmin + 14, + "RTMIN+15": sigrtmin + 15, + "RTMAX-14": sigrtmax - 14, + "RTMAX-13": sigrtmax - 13, + "RTMAX-12": sigrtmax - 12, + "RTMAX-11": sigrtmax - 11, + "RTMAX-10": sigrtmax - 10, + "RTMAX-9": sigrtmax - 9, + "RTMAX-8": sigrtmax - 8, + "RTMAX-7": sigrtmax - 7, + "RTMAX-6": sigrtmax - 6, + "RTMAX-5": sigrtmax - 5, + "RTMAX-4": sigrtmax - 4, + "RTMAX-3": sigrtmax - 3, + "RTMAX-2": sigrtmax - 2, + "RTMAX-1": sigrtmax - 1, + "RTMAX": sigrtmax, +} From 1d2da1e33402880bdefc8851ad731280519d4630 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 12 Jan 2019 13:29:15 +0100 Subject: [PATCH 46/60] testing: pkg/signal; remove redundant fmt.Sprintf() Signed-off-by: Sebastiaan van Stijn --- signal/trap_linux_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go index 14d15431..13943362 100644 --- a/signal/trap_linux_test.go +++ b/signal/trap_linux_test.go @@ -3,7 +3,6 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( - "fmt" "io/ioutil" "os" "os/exec" @@ -43,7 +42,7 @@ func TestTrap(t *testing.T) { for _, v := range sigmap { cmd := exec.Command(exePath) - cmd.Env = append(os.Environ(), fmt.Sprintf("SIGNAL_TYPE=%s", v.name)) + cmd.Env = append(os.Environ(), "SIGNAL_TYPE="+v.name) if v.multiple { cmd.Env = append(cmd.Env, "IF_MULTIPLE=1") } From 7d4118b83df48a2add81bdbc1234e056e124b052 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 12 Jan 2019 18:02:15 +0100 Subject: [PATCH 47/60] pkg/signal.TestTrap: use a subtest Signed-off-by: Sebastiaan van Stijn --- signal/trap_linux_test.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go index 13943362..d5c6240b 100644 --- a/signal/trap_linux_test.go +++ b/signal/trap_linux_test.go @@ -14,6 +14,7 @@ import ( ) func buildTestBinary(t *testing.T, tmpdir string, prefix string) (string, string) { + t.Helper() tmpDir, err := ioutil.TempDir(tmpdir, prefix) assert.NilError(t, err) exePath := tmpDir + "/" + prefix @@ -41,24 +42,25 @@ func TestTrap(t *testing.T) { defer os.RemoveAll(tmpDir) for _, v := range sigmap { - cmd := exec.Command(exePath) - cmd.Env = append(os.Environ(), "SIGNAL_TYPE="+v.name) - if v.multiple { - cmd.Env = append(cmd.Env, "IF_MULTIPLE=1") - } - err := cmd.Start() - assert.NilError(t, err) - err = cmd.Wait() - if e, ok := err.(*exec.ExitError); ok { + t.Run(v.name, func(t *testing.T) { + cmd := exec.Command(exePath) + cmd.Env = append(os.Environ(), "SIGNAL_TYPE="+v.name) + if v.multiple { + cmd.Env = append(cmd.Env, "IF_MULTIPLE=1") + } + err := cmd.Start() + assert.NilError(t, err) + err = cmd.Wait() + e, ok := err.(*exec.ExitError) + assert.Assert(t, ok, "expected exec.ExitError, got %T", e) + code := e.Sys().(syscall.WaitStatus).ExitStatus() if v.multiple { assert.Check(t, is.DeepEqual(128+int(v.signal.(syscall.Signal)), code)) } else { assert.Check(t, is.Equal(99, code)) } - continue - } - t.Fatal("process didn't end with any error") + }) } } From 1389d1ec0ab5f2a0af9de5e659dabddb940868b0 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 6 Aug 2019 19:59:23 +0200 Subject: [PATCH 48/60] pkg/signal: remove unnescessary conversion (unconvert) Signed-off-by: Sebastiaan van Stijn --- signal/signal_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/signal_test.go b/signal/signal_test.go index 0bfcf6ce..9abe3883 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -28,7 +28,7 @@ func TestValidSignalForPlatform(t *testing.T) { assert.Check(t, is.Equal(false, isValidSignal)) for _, sigN := range SignalMap { - isValidSignal = ValidSignalForPlatform(syscall.Signal(sigN)) + isValidSignal = ValidSignalForPlatform(sigN) assert.Check(t, is.Equal(true, isValidSignal)) } } From 2156d66cfe07b586f01d9b626efba97a35f98f1a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 27 Nov 2019 15:41:47 +0100 Subject: [PATCH 49/60] pkg/signal: normalize comment formatting Signed-off-by: Sebastiaan van Stijn --- signal/trap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/trap.go b/signal/trap.go index 2a6e69fb..a277b956 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -61,7 +61,7 @@ func Trap(cleanup func(), logger interface { DumpStacks("") logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT") } - //for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # + // for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # os.Exit(128 + int(sig.(syscall.Signal))) }(sig) } From 9e05c86badd377d5d994083ec2ffcfd96f265b47 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 7 Feb 2020 14:39:24 +0100 Subject: [PATCH 50/60] bump gotest.tools v3.0.1 for compatibility with Go 1.14 full diff: https://github.com/gotestyourself/gotest.tools/compare/v2.3.0...v3.0.1 Signed-off-by: Sebastiaan van Stijn --- signal/signal_linux_test.go | 4 ++-- signal/signal_test.go | 4 ++-- signal/trap_linux_test.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index 9a021e21..91604bb2 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -8,8 +8,8 @@ import ( "testing" "time" - "gotest.tools/assert" - is "gotest.tools/assert/cmp" + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" ) func TestCatchAll(t *testing.T) { diff --git a/signal/signal_test.go b/signal/signal_test.go index 9abe3883..c275345b 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -4,8 +4,8 @@ import ( "syscall" "testing" - "gotest.tools/assert" - is "gotest.tools/assert/cmp" + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" ) func TestParseSignal(t *testing.T) { diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go index d5c6240b..781ca5a3 100644 --- a/signal/trap_linux_test.go +++ b/signal/trap_linux_test.go @@ -9,8 +9,8 @@ import ( "syscall" "testing" - "gotest.tools/assert" - is "gotest.tools/assert/cmp" + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" ) func buildTestBinary(t *testing.T, tmpdir string, prefix string) (string, string) { From a377c30b695137f9998fce9b0e27a73bed12c779 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 10 Feb 2020 20:25:42 +0100 Subject: [PATCH 51/60] TestCatchAll, TestStopCatch: remove unneeded goroutine Signed-off-by: Sebastiaan van Stijn --- signal/signal_linux_test.go | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index 91604bb2..8e2fb98d 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -6,7 +6,6 @@ import ( "os" "syscall" "testing" - "time" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -27,17 +26,11 @@ func TestCatchAll(t *testing.T) { } for sigStr := range listOfSignals { - signal, ok := SignalMap[sigStr] - if ok { - go func() { - time.Sleep(1 * time.Millisecond) - syscall.Kill(syscall.Getpid(), signal) - }() - + if signal, ok := SignalMap[sigStr]; ok { + syscall.Kill(syscall.Getpid(), signal) s := <-sigs assert.Check(t, is.Equal(s.String(), signal.String())) } - } } @@ -45,11 +38,7 @@ func TestStopCatch(t *testing.T) { signal := SignalMap["HUP"] channel := make(chan os.Signal, 1) CatchAll(channel) - go func() { - - time.Sleep(1 * time.Millisecond) - syscall.Kill(syscall.Getpid(), signal) - }() + syscall.Kill(syscall.Getpid(), signal) signalString := <-channel assert.Check(t, is.Equal(signalString.String(), signal.String())) From b23eeb968e88b6dd8881622d08a8d77efebbaa1d Mon Sep 17 00:00:00 2001 From: liuxiaodong Date: Thu, 12 Mar 2020 20:45:02 +0800 Subject: [PATCH 52/60] unit test on TestParseSignal failed within pkg/signal package on mips64el error log : signal_test.go:20: assertion failed: error is not nil: Invalid signal: SIGEMT signal_test.go:22: assertion failed: When "ParseSignal" function parse sigStr from SignalMap, it find the signal object with key ("SIG"+sigStr). But EMT signal named "SIGEMT" in SignalMap structrue, so the real key is "SIGSIGEMT" , and cannot find the target signal. modify "SIGEMT" to "EMT" in SignalMap structrue. Signed-off-by: liuxiaodong --- signal/signal_linux_mipsx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signal/signal_linux_mipsx.go b/signal/signal_linux_mipsx.go index 4c798912..c78c887a 100644 --- a/signal/signal_linux_mipsx.go +++ b/signal/signal_linux_mipsx.go @@ -35,7 +35,7 @@ var SignalMap = map[string]syscall.Signal{ "PWR": unix.SIGPWR, "QUIT": unix.SIGQUIT, "SEGV": unix.SIGSEGV, - "SIGEMT": unix.SIGEMT, + "EMT": unix.SIGEMT, "STOP": unix.SIGSTOP, "SYS": unix.SIGSYS, "TERM": unix.SIGTERM, From fb0004a1796ac322c49be3cbbad86a140f5ad712 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 19 May 2021 19:42:18 +0200 Subject: [PATCH 53/60] pkg/signal.CatchAll: ignore SIGURG on Linux Do not handle SIGURG on Linux, as in go1.14+, the go runtime issues SIGURG as an interrupt to support preemptable system calls on Linux. This issue was caught in TestCatchAll, which could fail when updating to Go 1.14 or above; === Failed === FAIL: pkg/signal TestCatchAll (0.01s) signal_linux_test.go:32: assertion failed: urgent I/O condition (string) != continued (string) signal_linux_test.go:32: assertion failed: continued (string) != hangup (string) signal_linux_test.go:32: assertion failed: hangup (string) != child exited (string) signal_linux_test.go:32: assertion failed: child exited (string) != illegal instruction (string) signal_linux_test.go:32: assertion failed: illegal instruction (string) != floating point exception (string) Signed-off-by: Sebastiaan van Stijn --- signal/signal.go | 7 +++++++ signal/signal_darwin.go | 5 +++++ signal/signal_freebsd.go | 5 +++++ signal/signal_linux.go | 5 +++++ signal/signal_linux_mipsx.go | 5 +++++ signal/signal_linux_test.go | 17 +++++++++++++++++ signal/signal_windows.go | 5 +++++ 7 files changed, 49 insertions(+) diff --git a/signal/signal.go b/signal/signal.go index 88ef7b5e..bbe006bd 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -12,9 +12,16 @@ import ( ) // CatchAll catches all signals and relays them to the specified channel. +// On Linux, SIGURG is not handled, as it's used by the Go runtime to support +// preemptable system calls. func CatchAll(sigc chan os.Signal) { var handledSigs []os.Signal for _, s := range SignalMap { + if isRuntimeSig(s) { + // Do not handle SIGURG on Linux, as in go1.14+, the go runtime issues + // SIGURG as an interrupt to support preemptable system calls on Linux. + continue + } handledSigs = append(handledSigs, s) } signal.Notify(sigc, handledSigs...) diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go index ee5501e3..8ffd3d73 100644 --- a/signal/signal_darwin.go +++ b/signal/signal_darwin.go @@ -1,6 +1,7 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( + "os" "syscall" ) @@ -39,3 +40,7 @@ var SignalMap = map[string]syscall.Signal{ "XCPU": syscall.SIGXCPU, "XFSZ": syscall.SIGXFSZ, } + +func isRuntimeSig(_ os.Signal) bool { + return false +} diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index 764f90e2..a5e774a5 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -1,6 +1,7 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( + "os" "syscall" ) @@ -41,3 +42,7 @@ var SignalMap = map[string]syscall.Signal{ "XCPU": syscall.SIGXCPU, "XFSZ": syscall.SIGXFSZ, } + +func isRuntimeSig(_ os.Signal) bool { + return false +} diff --git a/signal/signal_linux.go b/signal/signal_linux.go index 4013bded..46fe6bba 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -3,6 +3,7 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( + "os" "syscall" "golang.org/x/sys/unix" @@ -81,3 +82,7 @@ var SignalMap = map[string]syscall.Signal{ "RTMAX-1": sigrtmax - 1, "RTMAX": sigrtmax, } + +func isRuntimeSig(s os.Signal) bool { + return s == unix.SIGURG +} diff --git a/signal/signal_linux_mipsx.go b/signal/signal_linux_mipsx.go index c78c887a..665d849a 100644 --- a/signal/signal_linux_mipsx.go +++ b/signal/signal_linux_mipsx.go @@ -4,6 +4,7 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( + "os" "syscall" "golang.org/x/sys/unix" @@ -82,3 +83,7 @@ var SignalMap = map[string]syscall.Signal{ "RTMAX-1": sigrtmax - 1, "RTMAX": sigrtmax, } + +func isRuntimeSig(s os.Signal) bool { + return s == unix.SIGURG +} diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index 8e2fb98d..decd454d 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -6,6 +6,7 @@ import ( "os" "syscall" "testing" + "time" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -34,6 +35,22 @@ func TestCatchAll(t *testing.T) { } } +func TestCatchAllIgnoreSigUrg(t *testing.T) { + sigs := make(chan os.Signal, 1) + CatchAll(sigs) + defer StopCatch(sigs) + + err := syscall.Kill(syscall.Getpid(), syscall.SIGURG) + assert.NilError(t, err) + timer := time.NewTimer(1 * time.Second) + defer timer.Stop() + select { + case <-timer.C: + case s := <-sigs: + t.Fatalf("expected no signals to be handled, but received %q", s.String()) + } +} + func TestStopCatch(t *testing.T) { signal := SignalMap["HUP"] channel := make(chan os.Signal, 1) diff --git a/signal/signal_windows.go b/signal/signal_windows.go index 65752f24..d44662c4 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -1,6 +1,7 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( + "os" "syscall" ) @@ -24,3 +25,7 @@ var SignalMap = map[string]syscall.Signal{ "KILL": syscall.SIGKILL, "TERM": syscall.SIGTERM, } + +func isRuntimeSig(_ os.Signal) bool { + return false +} From 99abddf6c7f9a9226cf12dfda712cc526de94648 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 25 May 2021 12:07:53 +0200 Subject: [PATCH 54/60] pkg/signal: ignore SIGURG on all platforms Other Unix platforms (e.g. Darwin) are also affected by the Go runtime sending SIGURG. This patch changes how we match the signal by just looking for the "URG" name, which should handle any platform that has this signal defined in the SignalMap. Signed-off-by: Sebastiaan van Stijn --- signal/signal.go | 8 ++++---- signal/signal_darwin.go | 5 ----- signal/signal_freebsd.go | 5 ----- signal/signal_linux.go | 5 ----- signal/signal_linux_mipsx.go | 5 ----- signal/signal_windows.go | 5 ----- 6 files changed, 4 insertions(+), 29 deletions(-) diff --git a/signal/signal.go b/signal/signal.go index bbe006bd..b274033e 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -12,13 +12,13 @@ import ( ) // CatchAll catches all signals and relays them to the specified channel. -// On Linux, SIGURG is not handled, as it's used by the Go runtime to support +// SIGURG is not handled, as it's used by the Go runtime to support // preemptable system calls. func CatchAll(sigc chan os.Signal) { var handledSigs []os.Signal - for _, s := range SignalMap { - if isRuntimeSig(s) { - // Do not handle SIGURG on Linux, as in go1.14+, the go runtime issues + for n, s := range SignalMap { + if n == "URG" { + // Do not handle SIGURG, as in go1.14+, the go runtime issues // SIGURG as an interrupt to support preemptable system calls on Linux. continue } diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go index 8ffd3d73..ee5501e3 100644 --- a/signal/signal_darwin.go +++ b/signal/signal_darwin.go @@ -1,7 +1,6 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( - "os" "syscall" ) @@ -40,7 +39,3 @@ var SignalMap = map[string]syscall.Signal{ "XCPU": syscall.SIGXCPU, "XFSZ": syscall.SIGXFSZ, } - -func isRuntimeSig(_ os.Signal) bool { - return false -} diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index a5e774a5..764f90e2 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -1,7 +1,6 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( - "os" "syscall" ) @@ -42,7 +41,3 @@ var SignalMap = map[string]syscall.Signal{ "XCPU": syscall.SIGXCPU, "XFSZ": syscall.SIGXFSZ, } - -func isRuntimeSig(_ os.Signal) bool { - return false -} diff --git a/signal/signal_linux.go b/signal/signal_linux.go index 46fe6bba..4013bded 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -3,7 +3,6 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( - "os" "syscall" "golang.org/x/sys/unix" @@ -82,7 +81,3 @@ var SignalMap = map[string]syscall.Signal{ "RTMAX-1": sigrtmax - 1, "RTMAX": sigrtmax, } - -func isRuntimeSig(s os.Signal) bool { - return s == unix.SIGURG -} diff --git a/signal/signal_linux_mipsx.go b/signal/signal_linux_mipsx.go index 665d849a..c78c887a 100644 --- a/signal/signal_linux_mipsx.go +++ b/signal/signal_linux_mipsx.go @@ -4,7 +4,6 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( - "os" "syscall" "golang.org/x/sys/unix" @@ -83,7 +82,3 @@ var SignalMap = map[string]syscall.Signal{ "RTMAX-1": sigrtmax - 1, "RTMAX": sigrtmax, } - -func isRuntimeSig(s os.Signal) bool { - return s == unix.SIGURG -} diff --git a/signal/signal_windows.go b/signal/signal_windows.go index d44662c4..65752f24 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -1,7 +1,6 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( - "os" "syscall" ) @@ -25,7 +24,3 @@ var SignalMap = map[string]syscall.Signal{ "KILL": syscall.SIGKILL, "TERM": syscall.SIGTERM, } - -func isRuntimeSig(_ os.Signal) bool { - return false -} From e9e5ed9fc1036ebcf15a3be91a731262e6cf5e56 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 15 Jul 2021 17:33:55 +0200 Subject: [PATCH 55/60] pkg/signal: move signal.DumpStacks() to a separate package It is not directly related to signal-handling, so can well live in its own package. Also added a variant that doesn't take a directory to write files to, for easier consumption / better match to how it's used. Signed-off-by: Sebastiaan van Stijn --- signal/trap.go | 43 ++------------------------------------- signal/trap_linux_test.go | 17 ---------------- 2 files changed, 2 insertions(+), 58 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index a277b956..a838b3e7 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -4,14 +4,10 @@ import ( "fmt" "os" gosignal "os/signal" - "path/filepath" - "runtime" - "strings" "sync/atomic" "syscall" - "time" - "github.com/pkg/errors" + "github.com/docker/docker/pkg/stack" ) // Trap sets up a simplified signal "trap", appropriate for common @@ -58,7 +54,7 @@ func Trap(cleanup func(), logger interface { logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received") } case syscall.SIGQUIT: - DumpStacks("") + stack.Dump() logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT") } // for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # @@ -67,38 +63,3 @@ func Trap(cleanup func(), logger interface { } }() } - -const stacksLogNameTemplate = "goroutine-stacks-%s.log" - -// DumpStacks appends the runtime stack into file in dir and returns full path -// to that file. -func DumpStacks(dir string) (string, error) { - var ( - buf []byte - stackSize int - ) - bufferLen := 16384 - for stackSize == len(buf) { - buf = make([]byte, bufferLen) - stackSize = runtime.Stack(buf, true) - bufferLen *= 2 - } - buf = buf[:stackSize] - var f *os.File - if dir != "" { - path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1))) - var err error - f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) - if err != nil { - return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") - } - defer f.Close() - defer f.Sync() - } else { - f = os.Stderr - } - if _, err := f.Write(buf); err != nil { - return "", errors.Wrap(err, "failed to write goroutine stacks") - } - return f.Name(), nil -} diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go index 781ca5a3..3dce0cd2 100644 --- a/signal/trap_linux_test.go +++ b/signal/trap_linux_test.go @@ -64,20 +64,3 @@ func TestTrap(t *testing.T) { } } - -func TestDumpStacks(t *testing.T) { - directory, err := ioutil.TempDir("", "test-dump-tasks") - assert.Check(t, err) - defer os.RemoveAll(directory) - dumpPath, err := DumpStacks(directory) - assert.Check(t, err) - readFile, _ := ioutil.ReadFile(dumpPath) - fileData := string(readFile) - assert.Check(t, is.Contains(fileData, "goroutine")) -} - -func TestDumpStacksWithEmptyInput(t *testing.T) { - path, err := DumpStacks("") - assert.Check(t, err) - assert.Check(t, is.Equal(os.Stderr.Name(), path)) -} From f757dd0cba2f942a4a4a75dbd0e871507561030c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 15 Jul 2021 18:08:17 +0200 Subject: [PATCH 56/60] pkg/signal: move Trap() to cmd/dockerd It's the only location where this is used, and it's quite specific to dockerd (not really a reusable function for external use), so moving it into that package. Signed-off-by: Sebastiaan van Stijn --- signal/testfiles/main.go | 43 ------------------------- signal/trap.go | 65 -------------------------------------- signal/trap_linux_test.go | 66 --------------------------------------- 3 files changed, 174 deletions(-) delete mode 100644 signal/testfiles/main.go delete mode 100644 signal/trap.go delete mode 100644 signal/trap_linux_test.go diff --git a/signal/testfiles/main.go b/signal/testfiles/main.go deleted file mode 100644 index e56854c7..00000000 --- a/signal/testfiles/main.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "os" - "syscall" - "time" - - "github.com/docker/docker/pkg/signal" - "github.com/sirupsen/logrus" -) - -func main() { - sigmap := map[string]os.Signal{ - "TERM": syscall.SIGTERM, - "QUIT": syscall.SIGQUIT, - "INT": os.Interrupt, - } - signal.Trap(func() { - time.Sleep(time.Second) - os.Exit(99) - }, logrus.StandardLogger()) - go func() { - p, err := os.FindProcess(os.Getpid()) - if err != nil { - panic(err) - } - s := os.Getenv("SIGNAL_TYPE") - multiple := os.Getenv("IF_MULTIPLE") - switch s { - case "TERM", "INT": - if multiple == "1" { - for { - p.Signal(sigmap[s]) - } - } else { - p.Signal(sigmap[s]) - } - case "QUIT": - p.Signal(sigmap[s]) - } - }() - time.Sleep(2 * time.Second) -} diff --git a/signal/trap.go b/signal/trap.go deleted file mode 100644 index a838b3e7..00000000 --- a/signal/trap.go +++ /dev/null @@ -1,65 +0,0 @@ -package signal // import "github.com/docker/docker/pkg/signal" - -import ( - "fmt" - "os" - gosignal "os/signal" - "sync/atomic" - "syscall" - - "github.com/docker/docker/pkg/stack" -) - -// 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) -// * A SIGQUIT always causes an exit without cleanup, with a goroutine dump preceding exit. -// * 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, QUIT, SIGPIPE here - signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE} - gosignal.Notify(c, signals...) - 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") - } - case syscall.SIGQUIT: - stack.Dump() - logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT") - } - // for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal # - os.Exit(128 + int(sig.(syscall.Signal))) - }(sig) - } - }() -} diff --git a/signal/trap_linux_test.go b/signal/trap_linux_test.go deleted file mode 100644 index 3dce0cd2..00000000 --- a/signal/trap_linux_test.go +++ /dev/null @@ -1,66 +0,0 @@ -// +build linux - -package signal // import "github.com/docker/docker/pkg/signal" - -import ( - "io/ioutil" - "os" - "os/exec" - "syscall" - "testing" - - "gotest.tools/v3/assert" - is "gotest.tools/v3/assert/cmp" -) - -func buildTestBinary(t *testing.T, tmpdir string, prefix string) (string, string) { - t.Helper() - tmpDir, err := ioutil.TempDir(tmpdir, prefix) - assert.NilError(t, err) - exePath := tmpDir + "/" + prefix - wd, _ := os.Getwd() - testHelperCode := wd + "/testfiles/main.go" - cmd := exec.Command("go", "build", "-o", exePath, testHelperCode) - err = cmd.Run() - assert.NilError(t, err) - return exePath, tmpDir -} - -func TestTrap(t *testing.T) { - var sigmap = []struct { - name string - signal os.Signal - multiple bool - }{ - {"TERM", syscall.SIGTERM, false}, - {"QUIT", syscall.SIGQUIT, true}, - {"INT", os.Interrupt, false}, - {"TERM", syscall.SIGTERM, true}, - {"INT", os.Interrupt, true}, - } - exePath, tmpDir := buildTestBinary(t, "", "main") - defer os.RemoveAll(tmpDir) - - for _, v := range sigmap { - t.Run(v.name, func(t *testing.T) { - cmd := exec.Command(exePath) - cmd.Env = append(os.Environ(), "SIGNAL_TYPE="+v.name) - if v.multiple { - cmd.Env = append(cmd.Env, "IF_MULTIPLE=1") - } - err := cmd.Start() - assert.NilError(t, err) - err = cmd.Wait() - e, ok := err.(*exec.ExitError) - assert.Assert(t, ok, "expected exec.ExitError, got %T", e) - - code := e.Sys().(syscall.WaitStatus).ExitStatus() - if v.multiple { - assert.Check(t, is.DeepEqual(128+int(v.signal.(syscall.Signal)), code)) - } else { - assert.Check(t, is.Equal(99, code)) - } - }) - } - -} From 50cd685844d8800838502ebf7fd719b5901e5427 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 15 Jul 2021 18:24:23 +0200 Subject: [PATCH 57/60] pkg/signal: remove gotest.tools dependency Signed-off-by: Sebastiaan van Stijn --- signal/signal_linux_test.go | 23 ++++++++++++++--------- signal/signal_test.go | 35 +++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index decd454d..5b305472 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -7,9 +7,6 @@ import ( "syscall" "testing" "time" - - "gotest.tools/v3/assert" - is "gotest.tools/v3/assert/cmp" ) func TestCatchAll(t *testing.T) { @@ -28,9 +25,11 @@ func TestCatchAll(t *testing.T) { for sigStr := range listOfSignals { if signal, ok := SignalMap[sigStr]; ok { - syscall.Kill(syscall.Getpid(), signal) + _ = syscall.Kill(syscall.Getpid(), signal) s := <-sigs - assert.Check(t, is.Equal(s.String(), signal.String())) + if s.String() != signal.String() { + t.Errorf("expected: %q, got: %q", signal, s) + } } } } @@ -41,7 +40,9 @@ func TestCatchAllIgnoreSigUrg(t *testing.T) { defer StopCatch(sigs) err := syscall.Kill(syscall.Getpid(), syscall.SIGURG) - assert.NilError(t, err) + if err != nil { + t.Fatal(err) + } timer := time.NewTimer(1 * time.Second) defer timer.Stop() select { @@ -55,11 +56,15 @@ func TestStopCatch(t *testing.T) { signal := SignalMap["HUP"] channel := make(chan os.Signal, 1) CatchAll(channel) - syscall.Kill(syscall.Getpid(), signal) + _ = syscall.Kill(syscall.Getpid(), signal) signalString := <-channel - assert.Check(t, is.Equal(signalString.String(), signal.String())) + if signalString.String() != signal.String() { + t.Errorf("expected: %q, got: %q", signal, signalString) + } StopCatch(channel) _, ok := <-channel - assert.Check(t, is.Equal(ok, false)) + if ok { + t.Error("expected: !ok, got: ok") + } } diff --git a/signal/signal_test.go b/signal/signal_test.go index c275345b..c9fe3946 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -3,32 +3,43 @@ package signal // import "github.com/docker/docker/pkg/signal" import ( "syscall" "testing" - - "gotest.tools/v3/assert" - is "gotest.tools/v3/assert/cmp" ) func TestParseSignal(t *testing.T) { - _, checkAtoiError := ParseSignal("0") - assert.Check(t, is.Error(checkAtoiError, "Invalid signal: 0")) + _, err := ParseSignal("0") + expectedErr := "Invalid signal: 0" + if err == nil || err.Error() != expectedErr { + t.Errorf("expected %q, but got %v", expectedErr, err) + } - _, error := ParseSignal("SIG") - assert.Check(t, is.Error(error, "Invalid signal: SIG")) + _, err = ParseSignal("SIG") + expectedErr = "Invalid signal: SIG" + if err == nil || err.Error() != expectedErr { + t.Errorf("expected %q, but got %v", expectedErr, err) + } for sigStr := range SignalMap { - responseSignal, error := ParseSignal(sigStr) - assert.Check(t, error) + responseSignal, err := ParseSignal(sigStr) + if err != nil { + t.Error(err) + } signal := SignalMap[sigStr] - assert.Check(t, is.DeepEqual(signal, responseSignal)) + if responseSignal != signal { + t.Errorf("expected: %q, got: %q", signal, responseSignal) + } } } func TestValidSignalForPlatform(t *testing.T) { isValidSignal := ValidSignalForPlatform(syscall.Signal(0)) - assert.Check(t, is.Equal(false, isValidSignal)) + if isValidSignal { + t.Error("expected !isValidSignal") + } for _, sigN := range SignalMap { isValidSignal = ValidSignalForPlatform(sigN) - assert.Check(t, is.Equal(true, isValidSignal)) + if !isValidSignal { + t.Error("expected isValidSignal") + } } } From d78fef5666b2b265bf6fc3822b63839bbfa7483f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 15 Jul 2021 21:37:57 +0200 Subject: [PATCH 58/60] signal: remove import comments Signed-off-by: Sebastiaan van Stijn --- signal/signal.go | 2 +- signal/signal_darwin.go | 2 +- signal/signal_freebsd.go | 2 +- signal/signal_linux.go | 2 +- signal/signal_linux_mipsx.go | 2 +- signal/signal_linux_test.go | 2 +- signal/signal_test.go | 2 +- signal/signal_unix.go | 2 +- signal/signal_unsupported.go | 2 +- signal/signal_windows.go | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/signal/signal.go b/signal/signal.go index b274033e..c9cd046c 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -1,6 +1,6 @@ // Package signal provides helper functions for dealing with signals across // various operating systems. -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "fmt" diff --git a/signal/signal_darwin.go b/signal/signal_darwin.go index ee5501e3..946de87e 100644 --- a/signal/signal_darwin.go +++ b/signal/signal_darwin.go @@ -1,4 +1,4 @@ -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" diff --git a/signal/signal_freebsd.go b/signal/signal_freebsd.go index 764f90e2..6b9569bb 100644 --- a/signal/signal_freebsd.go +++ b/signal/signal_freebsd.go @@ -1,4 +1,4 @@ -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" diff --git a/signal/signal_linux.go b/signal/signal_linux.go index 4013bded..692b9f9e 100644 --- a/signal/signal_linux.go +++ b/signal/signal_linux.go @@ -1,6 +1,6 @@ // +build !mips,!mipsle,!mips64,!mips64le -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" diff --git a/signal/signal_linux_mipsx.go b/signal/signal_linux_mipsx.go index c78c887a..138a840c 100644 --- a/signal/signal_linux_mipsx.go +++ b/signal/signal_linux_mipsx.go @@ -1,7 +1,7 @@ // +build linux // +build mips mipsle mips64 mips64le -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" diff --git a/signal/signal_linux_test.go b/signal/signal_linux_test.go index 5b305472..b1bdd3a8 100644 --- a/signal/signal_linux_test.go +++ b/signal/signal_linux_test.go @@ -1,6 +1,6 @@ // +build darwin linux -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "os" diff --git a/signal/signal_test.go b/signal/signal_test.go index c9fe3946..b51c03ff 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -1,4 +1,4 @@ -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" diff --git a/signal/signal_unix.go b/signal/signal_unix.go index a2aa4248..5d058fd5 100644 --- a/signal/signal_unix.go +++ b/signal/signal_unix.go @@ -1,6 +1,6 @@ // +build !windows -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" diff --git a/signal/signal_unsupported.go b/signal/signal_unsupported.go index 1fd25a83..161ba273 100644 --- a/signal/signal_unsupported.go +++ b/signal/signal_unsupported.go @@ -1,6 +1,6 @@ // +build !linux,!darwin,!freebsd,!windows -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" diff --git a/signal/signal_windows.go b/signal/signal_windows.go index 65752f24..c84a63e8 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -1,4 +1,4 @@ -package signal // import "github.com/docker/docker/pkg/signal" +package signal import ( "syscall" From c5ecf6337e69710cc007476b6a7780d4619ae96d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 15 Jul 2021 21:40:52 +0200 Subject: [PATCH 59/60] signal: add go.mod Signed-off-by: Sebastiaan van Stijn --- signal/go.mod | 5 +++++ signal/go.sum | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 signal/go.mod create mode 100644 signal/go.sum diff --git a/signal/go.mod b/signal/go.mod new file mode 100644 index 00000000..c48d5aec --- /dev/null +++ b/signal/go.mod @@ -0,0 +1,5 @@ +module github.com/moby/sys/signal + +go 1.13 + +require golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c diff --git a/signal/go.sum b/signal/go.sum new file mode 100644 index 00000000..0f478630 --- /dev/null +++ b/signal/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From 1e8ecd8b237b5d1c7e1626088f4c637273f182b9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 15 Jul 2021 21:41:48 +0200 Subject: [PATCH 60/60] Makefile: enable tests for moby/sys/signal module Signed-off-by: Sebastiaan van Stijn --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3342a7b4..016bffad 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ .SHELLFLAGS = -ec -PACKAGES ?= mountinfo mount symlink +PACKAGES ?= mountinfo mount signal symlink BINDIR ?= _build/bin CROSS ?= linux/arm linux/arm64 linux/ppc64le linux/s390x \ freebsd/amd64 openbsd/amd64 darwin/amd64 darwin/arm64 windows/amd64