github.com/demonoid81/containerd@v1.3.4/cmd/ctr/commands/signals.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package commands 18 19 import ( 20 gocontext "context" 21 "os" 22 "os/signal" 23 "syscall" 24 25 "github.com/containerd/containerd" 26 "github.com/sirupsen/logrus" 27 ) 28 29 type killer interface { 30 Kill(gocontext.Context, syscall.Signal, ...containerd.KillOpts) error 31 } 32 33 // ForwardAllSignals forwards signals 34 func ForwardAllSignals(ctx gocontext.Context, task killer) chan os.Signal { 35 sigc := make(chan os.Signal, 128) 36 signal.Notify(sigc) 37 go func() { 38 for s := range sigc { 39 logrus.Debug("forwarding signal ", s) 40 if err := task.Kill(ctx, s.(syscall.Signal)); err != nil { 41 logrus.WithError(err).Errorf("forward signal %s", s) 42 } 43 } 44 }() 45 return sigc 46 } 47 48 // StopCatch stops and closes a channel 49 func StopCatch(sigc chan os.Signal) { 50 signal.Stop(sigc) 51 close(sigc) 52 }