From e259ae0c38b03342c0c813542b0c438bdd5cc202 Mon Sep 17 00:00:00 2001 From: lifubang Date: Tue, 18 Mar 2025 14:25:02 +0000 Subject: [PATCH] move notifySocket out of signalHandler In fact, notifySocket has no relationship to signalHandler, we can move it out of signalHandler to make the code more clear. Signed-off-by: lifubang --- notify_socket.go | 16 ++++++++++++++++ signals.go | 21 ++++----------------- utils_linux.go | 7 ++++++- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/notify_socket.go b/notify_socket.go index 5dd5d5007..e57e73a00 100644 --- a/notify_socket.go +++ b/notify_socket.go @@ -151,6 +151,22 @@ func (s *notifySocket) run(pid1 int) error { } } +// forward reads systemd notifications from the container and forwards them +// to notifySocketHost. +func (s *notifySocket) forward(process *libcontainer.Process, detach bool) error { + if detach { + pid, err := process.Pid() + if err != nil { + return err + } + _ = s.run(pid) + } else { + _ = s.run(os.Getpid()) + go func() { _ = s.run(0) }() + } + return nil +} + // notifyHost tells the host (usually systemd) that the container reported READY. // Also sends MAINPID and BARRIER. func notifyHost(client *net.UnixConn, ready []byte, pid1 int) error { diff --git a/signals.go b/signals.go index 936d751f6..15f16de75 100644 --- a/signals.go +++ b/signals.go @@ -16,9 +16,7 @@ const signalBufferSize = 2048 // newSignalHandler returns a signal handler for processing SIGCHLD and SIGWINCH signals // while still forwarding all other signals to the process. -// If notifySocket is present, use it to read systemd notifications from the container and -// forward them to notifySocketHost. -func newSignalHandler(enableSubreaper bool, notifySocket *notifySocket) chan *signalHandler { +func newSignalHandler(enableSubreaper bool) chan *signalHandler { if enableSubreaper { // set us as the subreaper before registering the signal handler for the container if err := system.SetSubreaper(1); err != nil { @@ -37,8 +35,7 @@ func newSignalHandler(enableSubreaper bool, notifySocket *notifySocket) chan *si // handle all signals for the process. signal.Notify(s) handler <- &signalHandler{ - signals: s, - notifySocket: notifySocket, + signals: s, } }() return handler @@ -52,8 +49,7 @@ type exit struct { } type signalHandler struct { - signals chan os.Signal - notifySocket *notifySocket + signals chan os.Signal } // forward handles the main signal event loop forwarding, resizing, or reaping depending @@ -61,7 +57,7 @@ type signalHandler struct { func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach bool) (int, error) { // make sure we know the pid of our main process so that we can return // after it dies. - if detach && h.notifySocket == nil { + if detach { return 0, nil } @@ -70,15 +66,6 @@ func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach return -1, err } - if h.notifySocket != nil { - if detach { - _ = h.notifySocket.run(pid1) - return 0, nil - } - _ = h.notifySocket.run(os.Getpid()) - go func() { _ = h.notifySocket.run(0) }() - } - // Perform the initial tty resize. Always ignore errors resizing because // stdout might have disappeared (due to races with when SIGHUP is sent). _ = tty.resize() diff --git a/utils_linux.go b/utils_linux.go index 33cd41dd0..a83840bed 100644 --- a/utils_linux.go +++ b/utils_linux.go @@ -258,7 +258,7 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) { // Setting up IO is a two stage process. We need to modify process to deal // with detaching containers, and then we get a tty after the container has // started. - handlerCh := newSignalHandler(r.enableSubreaper, r.notifySocket) + handlerCh := newSignalHandler(r.enableSubreaper) tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket) if err != nil { return -1, err @@ -301,6 +301,11 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) { return -1, err } } + if r.notifySocket != nil { + if err = r.notifySocket.forward(process, detach); err != nil { + return -1, err + } + } handler := <-handlerCh status, err := handler.forward(process, tty, detach) if detach {