From bccc664b19d00475c8eedb4cd00299bd59fc0521 Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Sat, 28 Apr 2018 15:02:33 +0200 Subject: [PATCH 1/9] added shell command input to commands prepending shell --- pkg/commands/cmd.go | 9 +++++++-- pkg/commands/entrypoint.go | 9 +++++++-- pkg/commands/run.go | 9 +++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/commands/cmd.go b/pkg/commands/cmd.go index 9d53bd98e7..79f13770e3 100644 --- a/pkg/commands/cmd.go +++ b/pkg/commands/cmd.go @@ -35,8 +35,13 @@ func (c *CmdCommand) ExecuteCommand(config *v1.Config) error { var newCommand []string if c.cmd.PrependShell { // This is the default shell on Linux - // TODO: Support shell command here - shell := []string{"/bin/sh", "-c"} + var shell []string + if len(config.Shell) > 0 { + shell = config.Shell + } else { + shell = append(shell, "/bin/sh", "-c") + } + newCommand = append(shell, strings.Join(c.cmd.CmdLine, " ")) } else { newCommand = c.cmd.CmdLine diff --git a/pkg/commands/entrypoint.go b/pkg/commands/entrypoint.go index a753dc5ee0..7310c2faf5 100644 --- a/pkg/commands/entrypoint.go +++ b/pkg/commands/entrypoint.go @@ -34,8 +34,13 @@ func (e *EntrypointCommand) ExecuteCommand(config *v1.Config) error { var newCommand []string if e.cmd.PrependShell { // This is the default shell on Linux - // TODO: Support shell command here - shell := []string{"/bin/sh", "-c"} + var shell []string + if len(config.Shell) > 0 { + shell = config.Shell + } else { + shell = append(shell, "/bin/sh", "-c") + } + newCommand = append(shell, strings.Join(e.cmd.CmdLine, " ")) } else { newCommand = e.cmd.CmdLine diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 849f012d75..7eb545f196 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -36,8 +36,13 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config) error { var newCommand []string if r.cmd.PrependShell { // This is the default shell on Linux - // TODO: Support shell command here - shell := []string{"/bin/sh", "-c"} + var shell []string + if len(config.Shell) > 0 { + shell = config.Shell + } else { + shell = append(shell, "/bin/sh", "-c") + } + newCommand = append(shell, strings.Join(r.cmd.CmdLine, " ")) } else { newCommand = r.cmd.CmdLine From ae47a03023fc263a56367009f800712b77f7fe94 Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Sat, 28 Apr 2018 15:12:04 +0200 Subject: [PATCH 2/9] Added shell command and test --- pkg/commands/shell.go | 54 +++++++++++++++++++++++++++++++++++++ pkg/commands/shell_test.go | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 pkg/commands/shell.go create mode 100644 pkg/commands/shell_test.go diff --git a/pkg/commands/shell.go b/pkg/commands/shell.go new file mode 100644 index 0000000000..89a4539141 --- /dev/null +++ b/pkg/commands/shell.go @@ -0,0 +1,54 @@ +/* +Copyright 2018 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands + +import ( + "strings" + + "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/google/go-containerregistry/v1" + "github.com/sirupsen/logrus" +) + +type ShellCommand struct { + cmd *instructions.ShellCommand +} + +// ExecuteCommand handles command processing similar to CMD and RUN, +func (s *ShellCommand) ExecuteCommand(config *v1.Config) error { + logrus.Info("cmd: SHELL") + var newShell []string + + newShell = s.cmd.Shell + + logrus.Infof("Replacing Shell in config with %v", newShell) + config.Shell = newShell + return nil +} + +// FilesToSnapshot returns an empty array since this is a metadata command +func (s *ShellCommand) FilesToSnapshot() []string { + return []string{} +} + +// CreatedBy returns some information about the command for the image config history +func (s *ShellCommand) CreatedBy() string { + entrypoint := []string{"SHELL"} + cmdLine := strings.Join(s.cmd.Shell, " ") + + return strings.Join(append(entrypoint, cmdLine), " ") +} diff --git a/pkg/commands/shell_test.go b/pkg/commands/shell_test.go new file mode 100644 index 0000000000..524138aabf --- /dev/null +++ b/pkg/commands/shell_test.go @@ -0,0 +1,55 @@ +/* +Copyright 2018 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package commands + +import ( + "testing" + + "github.com/GoogleContainerTools/kaniko/testutil" + "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/google/go-containerregistry/v1" +) + +var shellTests = []struct { + cmdLine []string + expectedShell []string +}{ + { + cmdLine: []string{"/bin/bash", "-c"}, + expectedShell: []string{"/bin/bash", "-c"}, + }, + { + cmdLine: []string{"/bin/bash"}, + expectedShell: []string{"/bin/bash"}, + }, +} + +func TestShellExecuteCmd(t *testing.T) { + + cfg := &v1.Config{ + Shell: nil, + } + + for _, test := range shellTests { + cmd := ShellCommand{ + &instructions.ShellCommand{ + Shell: test.cmdLine, + }, + } + err := cmd.ExecuteCommand(cfg) + testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedShell, cfg.Shell) + } +} From d73f8c031fad403bf9990785cbab8db661f8dd64 Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Sat, 28 Apr 2018 17:30:18 +0200 Subject: [PATCH 3/9] added stopsignal implementation --- pkg/commands/commands.go | 2 ++ pkg/commands/stopsignal.go | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 pkg/commands/stopsignal.go diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 96a5bed02f..0d8e6cafab 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -61,6 +61,8 @@ func GetCommand(cmd instructions.Command, buildcontext string) (DockerCommand, e return &OnBuildCommand{cmd: c}, nil case *instructions.VolumeCommand: return &VolumeCommand{cmd: c}, nil + case *instructions.StopSignalCommand: + return &StopSignalCommand{cmd: c}, nil case *instructions.MaintainerCommand: logrus.Warnf("%s is deprecated, skipping", cmd.Name()) return nil, nil diff --git a/pkg/commands/stopsignal.go b/pkg/commands/stopsignal.go new file mode 100644 index 0000000000..d9122da743 --- /dev/null +++ b/pkg/commands/stopsignal.go @@ -0,0 +1,58 @@ +/* +Copyright 2018 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands + +import ( + "strings" + + "github.com/GoogleContainerTools/kaniko/pkg/util" + "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/google/go-containerregistry/v1" + "github.com/sirupsen/logrus" +) + +type StopSignalCommand struct { + cmd *instructions.StopSignalCommand +} + +// ExecuteCommand handles command processing similar to CMD and RUN, +func (s *StopSignalCommand) ExecuteCommand(config *v1.Config) error { + logrus.Info("cmd: STOPSIGNAL") + + // resolve possible environment variables + resolvedEnvs, err := util.ResolveEnvironmentReplacementList([]string{s.cmd.Signal}, config.Env, true) + if err != nil { + return err + } + signal := resolvedEnvs[len(resolvedEnvs)-1] + + logrus.Infof("Replacing StopSignal in config with %v", signal) + config.StopSignal = signal + return nil +} + +// FilesToSnapshot returns an empty array since this is a metadata command +func (s *StopSignalCommand) FilesToSnapshot() []string { + return []string{} +} + +// CreatedBy returns some information about the command for the image config history +func (s *StopSignalCommand) CreatedBy() string { + entrypoint := []string{"STOPSIGNAL"} + + return strings.Join(append(entrypoint, s.cmd.Signal), " ") +} From fd8d4b6170738879533e36e3dd50a9c3eb2fd67d Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Sat, 28 Apr 2018 17:41:40 +0200 Subject: [PATCH 4/9] added test for stopsignal command implementation --- pkg/commands/stopsignal_test.go | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 pkg/commands/stopsignal_test.go diff --git a/pkg/commands/stopsignal_test.go b/pkg/commands/stopsignal_test.go new file mode 100644 index 0000000000..c6e717f761 --- /dev/null +++ b/pkg/commands/stopsignal_test.go @@ -0,0 +1,60 @@ +/* +Copyright 2018 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package commands + +import ( + "testing" + + "github.com/GoogleContainerTools/kaniko/testutil" + "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/google/go-containerregistry/v1" +) + +var stopsignalTests = []struct { + signal string + expectedSignal string +}{ + { + signal: "SIGKILL", + expectedSignal: "SIGKILL", + }, + { + signal: "${STOPSIG}", + expectedSignal: "SIGKILL", + }, + { + signal: "1", + expectedSignal: "1", + }, +} + +func TestStopsignalExecuteCmd(t *testing.T) { + + cfg := &v1.Config{ + StopSignal: "", + Env: []string{"STOPSIG=SIGKILL"}, + } + + for _, test := range stopsignalTests { + cmd := StopSignalCommand{ + &instructions.StopSignalCommand{ + Signal: test.signal, + }, + } + err := cmd.ExecuteCommand(cfg) + testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedSignal, cfg.StopSignal) + } +} From 35b5f4b34adae2df3d1e1dbfc7a21fac329c66b9 Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Sat, 28 Apr 2018 17:43:34 +0200 Subject: [PATCH 5/9] Revert "Added shell command and test" This reverts commit ae47a03023fc263a56367009f800712b77f7fe94. --- pkg/commands/shell.go | 54 ------------------------------------- pkg/commands/shell_test.go | 55 -------------------------------------- 2 files changed, 109 deletions(-) delete mode 100644 pkg/commands/shell.go delete mode 100644 pkg/commands/shell_test.go diff --git a/pkg/commands/shell.go b/pkg/commands/shell.go deleted file mode 100644 index 89a4539141..0000000000 --- a/pkg/commands/shell.go +++ /dev/null @@ -1,54 +0,0 @@ -/* -Copyright 2018 Google LLC - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package commands - -import ( - "strings" - - "github.com/docker/docker/builder/dockerfile/instructions" - "github.com/google/go-containerregistry/v1" - "github.com/sirupsen/logrus" -) - -type ShellCommand struct { - cmd *instructions.ShellCommand -} - -// ExecuteCommand handles command processing similar to CMD and RUN, -func (s *ShellCommand) ExecuteCommand(config *v1.Config) error { - logrus.Info("cmd: SHELL") - var newShell []string - - newShell = s.cmd.Shell - - logrus.Infof("Replacing Shell in config with %v", newShell) - config.Shell = newShell - return nil -} - -// FilesToSnapshot returns an empty array since this is a metadata command -func (s *ShellCommand) FilesToSnapshot() []string { - return []string{} -} - -// CreatedBy returns some information about the command for the image config history -func (s *ShellCommand) CreatedBy() string { - entrypoint := []string{"SHELL"} - cmdLine := strings.Join(s.cmd.Shell, " ") - - return strings.Join(append(entrypoint, cmdLine), " ") -} diff --git a/pkg/commands/shell_test.go b/pkg/commands/shell_test.go deleted file mode 100644 index 524138aabf..0000000000 --- a/pkg/commands/shell_test.go +++ /dev/null @@ -1,55 +0,0 @@ -/* -Copyright 2018 Google LLC - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package commands - -import ( - "testing" - - "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/docker/docker/builder/dockerfile/instructions" - "github.com/google/go-containerregistry/v1" -) - -var shellTests = []struct { - cmdLine []string - expectedShell []string -}{ - { - cmdLine: []string{"/bin/bash", "-c"}, - expectedShell: []string{"/bin/bash", "-c"}, - }, - { - cmdLine: []string{"/bin/bash"}, - expectedShell: []string{"/bin/bash"}, - }, -} - -func TestShellExecuteCmd(t *testing.T) { - - cfg := &v1.Config{ - Shell: nil, - } - - for _, test := range shellTests { - cmd := ShellCommand{ - &instructions.ShellCommand{ - Shell: test.cmdLine, - }, - } - err := cmd.ExecuteCommand(cfg) - testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedShell, cfg.Shell) - } -} From 281c69c6c618c20536aa689c0972900a57310a1f Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Sat, 28 Apr 2018 17:43:42 +0200 Subject: [PATCH 6/9] Revert "added shell command input to commands prepending shell" This reverts commit bccc664b19d00475c8eedb4cd00299bd59fc0521. --- pkg/commands/cmd.go | 9 ++------- pkg/commands/entrypoint.go | 9 ++------- pkg/commands/run.go | 9 ++------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/pkg/commands/cmd.go b/pkg/commands/cmd.go index 79f13770e3..9d53bd98e7 100644 --- a/pkg/commands/cmd.go +++ b/pkg/commands/cmd.go @@ -35,13 +35,8 @@ func (c *CmdCommand) ExecuteCommand(config *v1.Config) error { var newCommand []string if c.cmd.PrependShell { // This is the default shell on Linux - var shell []string - if len(config.Shell) > 0 { - shell = config.Shell - } else { - shell = append(shell, "/bin/sh", "-c") - } - + // TODO: Support shell command here + shell := []string{"/bin/sh", "-c"} newCommand = append(shell, strings.Join(c.cmd.CmdLine, " ")) } else { newCommand = c.cmd.CmdLine diff --git a/pkg/commands/entrypoint.go b/pkg/commands/entrypoint.go index 7310c2faf5..a753dc5ee0 100644 --- a/pkg/commands/entrypoint.go +++ b/pkg/commands/entrypoint.go @@ -34,13 +34,8 @@ func (e *EntrypointCommand) ExecuteCommand(config *v1.Config) error { var newCommand []string if e.cmd.PrependShell { // This is the default shell on Linux - var shell []string - if len(config.Shell) > 0 { - shell = config.Shell - } else { - shell = append(shell, "/bin/sh", "-c") - } - + // TODO: Support shell command here + shell := []string{"/bin/sh", "-c"} newCommand = append(shell, strings.Join(e.cmd.CmdLine, " ")) } else { newCommand = e.cmd.CmdLine diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 7eb545f196..849f012d75 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -36,13 +36,8 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config) error { var newCommand []string if r.cmd.PrependShell { // This is the default shell on Linux - var shell []string - if len(config.Shell) > 0 { - shell = config.Shell - } else { - shell = append(shell, "/bin/sh", "-c") - } - + // TODO: Support shell command here + shell := []string{"/bin/sh", "-c"} newCommand = append(shell, strings.Join(r.cmd.CmdLine, " ")) } else { newCommand = r.cmd.CmdLine From b5e68f4b7bbfe49230bd84084f743b63e5766ba5 Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Tue, 1 May 2018 12:12:36 +0200 Subject: [PATCH 7/9] added signal validation --- Gopkg.lock | 3 +- pkg/commands/stopsignal.go | 15 ++- .../docker/docker/pkg/signal/signal.go | 54 +++++++++ .../docker/docker/pkg/signal/signal_darwin.go | 41 +++++++ .../docker/pkg/signal/signal_freebsd.go | 43 ++++++++ .../docker/docker/pkg/signal/signal_linux.go | 81 ++++++++++++++ .../docker/docker/pkg/signal/signal_unix.go | 21 ++++ .../docker/pkg/signal/signal_unsupported.go | 10 ++ .../docker/pkg/signal/signal_windows.go | 26 +++++ .../docker/docker/pkg/signal/trap.go | 104 ++++++++++++++++++ 10 files changed, 393 insertions(+), 5 deletions(-) create mode 100644 vendor/github.com/docker/docker/pkg/signal/signal.go create mode 100644 vendor/github.com/docker/docker/pkg/signal/signal_darwin.go create mode 100644 vendor/github.com/docker/docker/pkg/signal/signal_freebsd.go create mode 100644 vendor/github.com/docker/docker/pkg/signal/signal_linux.go create mode 100644 vendor/github.com/docker/docker/pkg/signal/signal_unix.go create mode 100644 vendor/github.com/docker/docker/pkg/signal/signal_unsupported.go create mode 100644 vendor/github.com/docker/docker/pkg/signal/signal_windows.go create mode 100644 vendor/github.com/docker/docker/pkg/signal/trap.go diff --git a/Gopkg.lock b/Gopkg.lock index 7a899bbe10..6c182e1d40 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -45,6 +45,7 @@ "pkg/longpath", "pkg/mount", "pkg/pools", + "pkg/signal", "pkg/system" ] revision = "b1a1234c60cf87048814aa37da523b03a7b0d344" @@ -332,6 +333,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "9ed51a63daab2be9e4864e4d97d90ccc0f540d076f891b80f01bd3f161c9ef7a" + inputs-digest = "18d970ab8a3487013ef13e0aed686929d30bcebc1b9adaa329e9db414189fcf6" solver-name = "gps-cdcl" solver-version = 1 diff --git a/pkg/commands/stopsignal.go b/pkg/commands/stopsignal.go index d9122da743..85d70e6b46 100644 --- a/pkg/commands/stopsignal.go +++ b/pkg/commands/stopsignal.go @@ -21,6 +21,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/docker/docker/pkg/signal" "github.com/google/go-containerregistry/v1" "github.com/sirupsen/logrus" ) @@ -34,14 +35,20 @@ func (s *StopSignalCommand) ExecuteCommand(config *v1.Config) error { logrus.Info("cmd: STOPSIGNAL") // resolve possible environment variables - resolvedEnvs, err := util.ResolveEnvironmentReplacementList([]string{s.cmd.Signal}, config.Env, true) + resolvedEnvs, err := util.ResolveEnvironmentReplacementList([]string{s.cmd.Signal}, config.Env, false) if err != nil { return err } - signal := resolvedEnvs[len(resolvedEnvs)-1] + stopsignal := resolvedEnvs[len(resolvedEnvs)-1] - logrus.Infof("Replacing StopSignal in config with %v", signal) - config.StopSignal = signal + // validate stopsignal + _, err = signal.ParseSignal(stopsignal) + if err != nil { + return err + } + + logrus.Infof("Replacing StopSignal in config with %v", stopsignal) + config.StopSignal = stopsignal return nil } diff --git a/vendor/github.com/docker/docker/pkg/signal/signal.go b/vendor/github.com/docker/docker/pkg/signal/signal.go new file mode 100644 index 0000000000..6a663091a1 --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/signal.go @@ -0,0 +1,54 @@ +// Package signal provides helper functions for dealing with signals across +// various operating systems. +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "fmt" + "os" + "os/signal" + "strconv" + "strings" + "syscall" +) + +// CatchAll catches all signals and relays them to the specified channel. +func CatchAll(sigc chan os.Signal) { + handledSigs := []os.Signal{} + for _, s := range SignalMap { + handledSigs = append(handledSigs, s) + } + 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) +} + +// 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 +} + +// 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 +} diff --git a/vendor/github.com/docker/docker/pkg/signal/signal_darwin.go b/vendor/github.com/docker/docker/pkg/signal/signal_darwin.go new file mode 100644 index 0000000000..ee5501e3d9 --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/signal_darwin.go @@ -0,0 +1,41 @@ +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "syscall" +) + +// SignalMap is a map of Darwin signals. +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/vendor/github.com/docker/docker/pkg/signal/signal_freebsd.go b/vendor/github.com/docker/docker/pkg/signal/signal_freebsd.go new file mode 100644 index 0000000000..764f90e264 --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/signal_freebsd.go @@ -0,0 +1,43 @@ +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "syscall" +) + +// SignalMap is a map of FreeBSD signals. +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/vendor/github.com/docker/docker/pkg/signal/signal_linux.go b/vendor/github.com/docker/docker/pkg/signal/signal_linux.go new file mode 100644 index 0000000000..caed97c963 --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/signal_linux.go @@ -0,0 +1,81 @@ +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +const ( + sigrtmin = 34 + sigrtmax = 64 +) + +// 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, + "STKFLT": unix.SIGSTKFLT, + "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, +} diff --git a/vendor/github.com/docker/docker/pkg/signal/signal_unix.go b/vendor/github.com/docker/docker/pkg/signal/signal_unix.go new file mode 100644 index 0000000000..a2aa4248fa --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/signal_unix.go @@ -0,0 +1,21 @@ +// +build !windows + +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "syscall" +) + +// Signals used in cli/command (no windows equivalent, use +// invalid signals so they don't get handled) + +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 + // 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/vendor/github.com/docker/docker/pkg/signal/signal_unsupported.go b/vendor/github.com/docker/docker/pkg/signal/signal_unsupported.go new file mode 100644 index 0000000000..1fd25a83c6 --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/signal_unsupported.go @@ -0,0 +1,10 @@ +// +build !linux,!darwin,!freebsd,!windows + +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "syscall" +) + +// SignalMap is an empty map of signals for unsupported platform. +var SignalMap = map[string]syscall.Signal{} diff --git a/vendor/github.com/docker/docker/pkg/signal/signal_windows.go b/vendor/github.com/docker/docker/pkg/signal/signal_windows.go new file mode 100644 index 0000000000..65752f24aa --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/signal_windows.go @@ -0,0 +1,26 @@ +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "syscall" +) + +// Signals used in cli/command (no windows equivalent, use +// invalid signals so they don't get handled) +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" +) + +// 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, +} diff --git a/vendor/github.com/docker/docker/pkg/signal/trap.go b/vendor/github.com/docker/docker/pkg/signal/trap.go new file mode 100644 index 0000000000..2a6e69fb50 --- /dev/null +++ b/vendor/github.com/docker/docker/pkg/signal/trap.go @@ -0,0 +1,104 @@ +package signal // import "github.com/docker/docker/pkg/signal" + +import ( + "fmt" + "os" + gosignal "os/signal" + "path/filepath" + "runtime" + "strings" + "sync/atomic" + "syscall" + "time" + + "github.com/pkg/errors" +) + +// 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: + 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 # + os.Exit(128 + int(sig.(syscall.Signal))) + }(sig) + } + }() +} + +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 +} From d63bc6da4b4794547ca838ea96f618718cbd78bd Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Tue, 1 May 2018 12:29:57 +0200 Subject: [PATCH 8/9] set fixed index for resolvedEnv slice on environment replacement - will always be len 1 --- pkg/commands/stopsignal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/stopsignal.go b/pkg/commands/stopsignal.go index 85d70e6b46..65fb7d42fd 100644 --- a/pkg/commands/stopsignal.go +++ b/pkg/commands/stopsignal.go @@ -39,7 +39,7 @@ func (s *StopSignalCommand) ExecuteCommand(config *v1.Config) error { if err != nil { return err } - stopsignal := resolvedEnvs[len(resolvedEnvs)-1] + stopsignal := resolvedEnvs[0] // validate stopsignal _, err = signal.ParseSignal(stopsignal) From e23003aff3f6f82aba82c57e8849999534c4d273 Mon Sep 17 00:00:00 2001 From: Christian Jantz Date: Wed, 2 May 2018 08:16:38 +0200 Subject: [PATCH 9/9] Removed stopsignal from README as missing command --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 54a086d400..a2c5b24f1b 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,6 @@ After each command, we append a layer of changed files to the base image (if the The majority of Dockerfile commands can be executed with kaniko, but we're still working on supporting the following commands: * HEALTHCHECK -* STOPSIGNAL * ARG Multi-Stage Dockerfiles are also unsupported currently, but will be ready soon.