Skip to content

Commit 8c15ed7

Browse files
committed
skip setup signal notifier for detached container
For detached container, we don't need to setup signal notifier, because there is no customer to consume the signals in `forward()`. This commit also moves notifySocket out of signalHandler to make the code more clear. Signed-off-by: lifubang <[email protected]>
1 parent 4c22153 commit 8c15ed7

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

notify_socket.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ func (n *notifySocket) run(pid1 int) error {
150150
}
151151
}
152152

153+
// forward reads systemd notifications from the container and forwards them
154+
// to notifySocketHost.
155+
func (s *notifySocket) forward(process *libcontainer.Process, detach bool) error {
156+
if detach {
157+
pid, err := process.Pid()
158+
if err != nil {
159+
return err
160+
}
161+
_ = s.run(pid)
162+
} else {
163+
_ = s.run(os.Getpid())
164+
go func() { _ = s.run(0) }()
165+
}
166+
return nil
167+
}
168+
153169
// notifyHost tells the host (usually systemd) that the container reported READY.
154170
// Also sends MAINPID and BARRIER.
155171
func notifyHost(client *net.UnixConn, ready []byte, pid1 int) error {

signals.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os/signal"
66

77
"github.com/opencontainers/runc/libcontainer"
8-
"github.com/opencontainers/runc/libcontainer/system"
98
"github.com/opencontainers/runc/libcontainer/utils"
109

1110
"github.com/sirupsen/logrus"
@@ -16,15 +15,7 @@ const signalBufferSize = 2048
1615

1716
// newSignalHandler returns a signal handler for processing SIGCHLD and SIGWINCH signals
1817
// while still forwarding all other signals to the process.
19-
// If notifySocket is present, use it to read systemd notifications from the container and
20-
// forward them to notifySocketHost.
21-
func newSignalHandler(enableSubreaper bool, notifySocket *notifySocket) chan *signalHandler {
22-
if enableSubreaper {
23-
// set us as the subreaper before registering the signal handler for the container
24-
if err := system.SetSubreaper(1); err != nil {
25-
logrus.Warn(err)
26-
}
27-
}
18+
func newSignalHandler() chan *signalHandler {
2819
handler := make(chan *signalHandler)
2920
// signal.Notify is actually quite expensive, as it has to configure the
3021
// signal mask and add signal handlers for all signals (all ~65 of them).
@@ -37,8 +28,7 @@ func newSignalHandler(enableSubreaper bool, notifySocket *notifySocket) chan *si
3728
// handle all signals for the process.
3829
signal.Notify(s)
3930
handler <- &signalHandler{
40-
signals: s,
41-
notifySocket: notifySocket,
31+
signals: s,
4232
}
4333
}()
4434
return handler
@@ -52,33 +42,19 @@ type exit struct {
5242
}
5343

5444
type signalHandler struct {
55-
signals chan os.Signal
56-
notifySocket *notifySocket
45+
signals chan os.Signal
5746
}
5847

5948
// forward handles the main signal event loop forwarding, resizing, or reaping depending
6049
// on the signal received.
61-
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach bool) (int, error) {
50+
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty) (int, error) {
6251
// make sure we know the pid of our main process so that we can return
6352
// after it dies.
64-
if detach && h.notifySocket == nil {
65-
return 0, nil
66-
}
67-
6853
pid1, err := process.Pid()
6954
if err != nil {
7055
return -1, err
7156
}
7257

73-
if h.notifySocket != nil {
74-
if detach {
75-
_ = h.notifySocket.run(pid1)
76-
return 0, nil
77-
}
78-
_ = h.notifySocket.run(os.Getpid())
79-
go func() { _ = h.notifySocket.run(0) }()
80-
}
81-
8258
// Perform the initial tty resize. Always ignore errors resizing because
8359
// stdout might have disappeared (due to races with when SIGHUP is sent).
8460
_ = tty.resize()

utils_linux.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/opencontainers/runc/libcontainer"
1919
"github.com/opencontainers/runc/libcontainer/configs"
2020
"github.com/opencontainers/runc/libcontainer/specconv"
21+
"github.com/opencontainers/runc/libcontainer/system"
2122
"github.com/opencontainers/runc/libcontainer/system/kernelversion"
2223
"github.com/opencontainers/runc/libcontainer/utils"
2324
)
@@ -217,7 +218,10 @@ type runner struct {
217218
}
218219

219220
func (r *runner) run(config *specs.Process) (int, error) {
220-
var err error
221+
var (
222+
err error
223+
handlerCh chan *signalHandler
224+
)
221225
defer func() {
222226
if err != nil {
223227
r.destroy()
@@ -249,10 +253,18 @@ func (r *runner) run(config *specs.Process) (int, error) {
249253
process.ExtraFiles = append(process.ExtraFiles, os.NewFile(uintptr(i), "PreserveFD:"+strconv.Itoa(i)))
250254
}
251255
detach := r.detach || (r.action == CT_ACT_CREATE)
256+
if r.enableSubreaper {
257+
// set us as the subreaper before registering the signal handler for the container
258+
if err := system.SetSubreaper(1); err != nil {
259+
logrus.Warn(err)
260+
}
261+
}
262+
if !detach {
263+
handlerCh = newSignalHandler()
264+
}
252265
// Setting up IO is a two stage process. We need to modify process to deal
253266
// with detaching containers, and then we get a tty after the container has
254267
// started.
255-
handlerCh := newSignalHandler(r.enableSubreaper, r.notifySocket)
256268
tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket)
257269
if err != nil {
258270
return -1, err
@@ -291,14 +303,20 @@ func (r *runner) run(config *specs.Process) (int, error) {
291303
return -1, err
292304
}
293305
}
294-
handler := <-handlerCh
295-
status, err := handler.forward(process, tty, detach)
296-
if err != nil {
297-
r.terminate(process)
306+
if r.notifySocket != nil {
307+
if err := r.notifySocket.forward(process, detach); err != nil {
308+
r.terminate(process)
309+
return -1, err
310+
}
298311
}
299312
if detach {
300313
return 0, nil
301314
}
315+
handler := <-handlerCh
316+
status, err := handler.forward(process, tty)
317+
if err != nil {
318+
r.terminate(process)
319+
}
302320
if err == nil {
303321
r.destroy()
304322
}

0 commit comments

Comments
 (0)