github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/domain/infra/abi/terminal/sigproxy_linux.go (about)

     1  // +build ABISupport
     2  
     3  package terminal
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  
     9  	"github.com/containers/libpod/libpod"
    10  	"github.com/containers/libpod/pkg/signal"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  // ProxySignals ...
    15  func ProxySignals(ctr *libpod.Container) {
    16  	sigBuffer := make(chan os.Signal, 128)
    17  	signal.CatchAll(sigBuffer)
    18  
    19  	logrus.Debugf("Enabling signal proxying")
    20  
    21  	go func() {
    22  		for s := range sigBuffer {
    23  			// Ignore SIGCHLD and SIGPIPE - these are mostly likely
    24  			// intended for the podman command itself.
    25  			// SIGURG was added because of golang 1.14 and its preemptive changes
    26  			// causing more signals to "show up".
    27  			// https://github.com/containers/libpod/issues/5483
    28  			if s == syscall.SIGCHLD || s == syscall.SIGPIPE || s == syscall.SIGURG {
    29  				continue
    30  			}
    31  
    32  			if err := ctr.Kill(uint(s.(syscall.Signal))); err != nil {
    33  				// If the container dies, and we find out here,
    34  				// we need to forward that one signal to
    35  				// ourselves so that it is not lost, and then
    36  				// we terminate the proxy and let the defaults
    37  				// play out.
    38  				logrus.Errorf("Error forwarding signal %d to container %s: %v", s, ctr.ID(), err)
    39  				signal.StopCatch(sigBuffer)
    40  				if err := syscall.Kill(syscall.Getpid(), s.(syscall.Signal)); err != nil {
    41  					logrus.Errorf("failed to kill pid %d", syscall.Getpid())
    42  				}
    43  				return
    44  			}
    45  		}
    46  	}()
    47  }