2015-06-30 07:49:13 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-05-10 05:38:27 +08:00
|
|
|
"syscall" // only for Signal
|
2015-06-22 10:31:12 +08:00
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-03-15 06:46:08 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2015-06-22 10:31:12 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
2017-07-19 22:28:59 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-10 05:38:27 +08:00
|
|
|
"golang.org/x/sys/unix"
|
2015-06-22 10:31:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const signalBufferSize = 2048
|
|
|
|
|
|
|
|
// newSignalHandler returns a signal handler for processing SIGCHLD and SIGWINCH signals
|
|
|
|
// while still forwarding all other signals to the process.
|
2017-02-03 21:17:34 +08:00
|
|
|
// If notifySocket is present, use it to read systemd notifications from the container and
|
|
|
|
// forward them to notifySocketHost.
|
|
|
|
func newSignalHandler(enableSubreaper bool, notifySocket *notifySocket) *signalHandler {
|
2016-03-15 06:46:08 +08:00
|
|
|
if enableSubreaper {
|
|
|
|
// set us as the subreaper before registering the signal handler for the container
|
|
|
|
if err := system.SetSubreaper(1); err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
// ensure that we have a large buffer size so that we do not miss any signals
|
|
|
|
// incase we are not processing them fast enough.
|
|
|
|
s := make(chan os.Signal, signalBufferSize)
|
|
|
|
// handle all signals for the process.
|
|
|
|
signal.Notify(s)
|
|
|
|
return &signalHandler{
|
2017-02-03 21:17:34 +08:00
|
|
|
signals: s,
|
|
|
|
notifySocket: notifySocket,
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// exit models a process exit status with the pid and
|
|
|
|
// exit status.
|
|
|
|
type exit struct {
|
|
|
|
pid int
|
|
|
|
status int
|
|
|
|
}
|
|
|
|
|
|
|
|
type signalHandler struct {
|
2017-02-03 21:17:34 +08:00
|
|
|
signals chan os.Signal
|
|
|
|
notifySocket *notifySocket
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
|
2015-07-13 09:08:16 +08:00
|
|
|
// forward handles the main signal event loop forwarding, resizing, or reaping depending
|
2015-06-22 10:31:12 +08:00
|
|
|
// on the signal received.
|
2017-02-09 19:27:12 +08:00
|
|
|
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach bool) (int, error) {
|
2015-06-22 10:31:12 +08:00
|
|
|
// make sure we know the pid of our main process so that we can return
|
|
|
|
// after it dies.
|
2017-02-09 19:27:12 +08:00
|
|
|
if detach && h.notifySocket == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
pid1, err := process.Pid()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
|
|
|
|
if h.notifySocket != nil {
|
2017-02-09 19:27:12 +08:00
|
|
|
if detach {
|
|
|
|
h.notifySocket.run(pid1)
|
|
|
|
return 0, nil
|
|
|
|
} else {
|
|
|
|
go h.notifySocket.run(0)
|
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
}
|
|
|
|
|
2017-08-26 22:42:14 +08:00
|
|
|
// Perform the initial tty resize. Always ignore errors resizing because
|
|
|
|
// stdout might have disappeared (due to races with when SIGHUP is sent).
|
|
|
|
_ = tty.resize()
|
|
|
|
// Handle and forward signals.
|
2015-06-22 10:31:12 +08:00
|
|
|
for s := range h.signals {
|
|
|
|
switch s {
|
2017-05-10 05:38:27 +08:00
|
|
|
case unix.SIGWINCH:
|
2017-08-26 22:42:14 +08:00
|
|
|
// Ignore errors resizing, as above.
|
|
|
|
_ = tty.resize()
|
2017-05-10 05:38:27 +08:00
|
|
|
case unix.SIGCHLD:
|
2015-06-22 10:31:12 +08:00
|
|
|
exits, err := h.reap()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
for _, e := range exits {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"pid": e.pid,
|
|
|
|
"status": e.status,
|
|
|
|
}).Debug("process exited")
|
|
|
|
if e.pid == pid1 {
|
|
|
|
// call Wait() on the process even though we already have the exit
|
|
|
|
// status because we must ensure that any of the go specific process
|
|
|
|
// fun such as flushing pipes are complete before we return.
|
|
|
|
process.Wait()
|
2017-02-03 21:17:34 +08:00
|
|
|
if h.notifySocket != nil {
|
|
|
|
h.notifySocket.Close()
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
return e.status, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
logrus.Debugf("sending signal to process %s", s)
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Kill(pid1, s.(syscall.Signal)); err != nil {
|
2015-06-22 10:31:12 +08:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// reap runs wait4 in a loop until we have finished processing any existing exits
|
|
|
|
// then returns all exits to the main event loop for further processing.
|
|
|
|
func (h *signalHandler) reap() (exits []exit, err error) {
|
|
|
|
var (
|
2017-05-10 05:38:27 +08:00
|
|
|
ws unix.WaitStatus
|
|
|
|
rus unix.Rusage
|
2015-06-22 10:31:12 +08:00
|
|
|
)
|
|
|
|
for {
|
2017-05-10 05:38:27 +08:00
|
|
|
pid, err := unix.Wait4(-1, &ws, unix.WNOHANG, &rus)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
2017-05-10 05:38:27 +08:00
|
|
|
if err == unix.ECHILD {
|
2015-06-22 10:31:12 +08:00
|
|
|
return exits, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-28 01:01:05 +08:00
|
|
|
if pid <= 0 {
|
|
|
|
return exits, nil
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
exits = append(exits, exit{
|
|
|
|
pid: pid,
|
|
|
|
status: utils.ExitStatus(ws),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|