Skip to content

Commit 8fa3113

Browse files
committed
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 <[email protected]>
1 parent 4c22153 commit 8fa3113

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ const signalBufferSize = 2048
1616

1717
// newSignalHandler returns a signal handler for processing SIGCHLD and SIGWINCH signals
1818
// 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 {
19+
func newSignalHandler(enableSubreaper bool) chan *signalHandler {
2220
if enableSubreaper {
2321
// set us as the subreaper before registering the signal handler for the container
2422
if err := system.SetSubreaper(1); err != nil {
@@ -37,8 +35,7 @@ func newSignalHandler(enableSubreaper bool, notifySocket *notifySocket) chan *si
3735
// handle all signals for the process.
3836
signal.Notify(s)
3937
handler <- &signalHandler{
40-
signals: s,
41-
notifySocket: notifySocket,
38+
signals: s,
4239
}
4340
}()
4441
return handler
@@ -52,16 +49,15 @@ type exit struct {
5249
}
5350

5451
type signalHandler struct {
55-
signals chan os.Signal
56-
notifySocket *notifySocket
52+
signals chan os.Signal
5753
}
5854

5955
// forward handles the main signal event loop forwarding, resizing, or reaping depending
6056
// on the signal received.
6157
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach bool) (int, error) {
6258
// make sure we know the pid of our main process so that we can return
6359
// after it dies.
64-
if detach && h.notifySocket == nil {
60+
if detach {
6561
return 0, nil
6662
}
6763

@@ -70,15 +66,6 @@ func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach
7066
return -1, err
7167
}
7268

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-
8269
// Perform the initial tty resize. Always ignore errors resizing because
8370
// stdout might have disappeared (due to races with when SIGHUP is sent).
8471
_ = tty.resize()

utils_linux.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (r *runner) run(config *specs.Process) (int, error) {
252252
// Setting up IO is a two stage process. We need to modify process to deal
253253
// with detaching containers, and then we get a tty after the container has
254254
// started.
255-
handlerCh := newSignalHandler(r.enableSubreaper, r.notifySocket)
255+
handlerCh := newSignalHandler(r.enableSubreaper)
256256
tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket)
257257
if err != nil {
258258
return -1, err
@@ -291,6 +291,12 @@ func (r *runner) run(config *specs.Process) (int, error) {
291291
return -1, err
292292
}
293293
}
294+
if r.notifySocket != nil {
295+
if err := r.notifySocket.forward(process, detach); err != nil {
296+
r.terminate(process)
297+
return -1, err
298+
}
299+
}
294300
handler := <-handlerCh
295301
status, err := handler.forward(process, tty, detach)
296302
if err != nil {

0 commit comments

Comments
 (0)