Skip to content

Commit d5026f0

Browse files
committed
signals: support detach and notify socket together
let runc run until READY= is received and then proceed with detaching the process. Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent c8593c4 commit d5026f0

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

notify_socket.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ func (s *notifySocket) setupSocket() error {
6363
return nil
6464
}
6565

66-
func (notifySocket *notifySocket) run() {
66+
// pid1 must be set only with -d, as it is used to set the new process as the main process
67+
// for the service in systemd
68+
func (notifySocket *notifySocket) run(pid1 int) {
6769
buf := make([]byte, 512)
6870
notifySocketHostAddr := net.UnixAddr{Name: notifySocket.host, Net: "unixgram"}
6971
client, err := net.DialUnix("unixgram", nil, &notifySocketHostAddr)
@@ -93,6 +95,12 @@ func (notifySocket *notifySocket) run() {
9395
if err != nil {
9496
return
9597
}
98+
99+
// now we can inform systemd to use pid1 as the pid to monitor
100+
if pid1 > 0 {
101+
newPid := fmt.Sprintf("MAINPID=%d\n", pid1)
102+
client.Write([]byte(newPid))
103+
}
96104
return
97105
}
98106
}

restore.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,7 @@ func restoreContainer(context *cli.Context, spec *specs.Spec, config *configs.Co
188188
return -1, err
189189
}
190190
}
191-
if detach {
192-
return 0, nil
193-
}
194-
return handler.forward(process, tty)
191+
return handler.forward(process, tty, detach)
195192
}
196193

197194
func criuOptions(context *cli.Context) *libcontainer.CriuOpts {

signals.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,25 @@ type signalHandler struct {
5151

5252
// forward handles the main signal event loop forwarding, resizing, or reaping depending
5353
// on the signal received.
54-
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty) (int, error) {
54+
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach bool) (int, error) {
5555
// make sure we know the pid of our main process so that we can return
5656
// after it dies.
57+
if detach && h.notifySocket == nil {
58+
return 0, nil
59+
}
60+
5761
pid1, err := process.Pid()
5862
if err != nil {
5963
return -1, err
6064
}
6165

6266
if h.notifySocket != nil {
63-
go h.notifySocket.run()
67+
if detach {
68+
h.notifySocket.run(pid1)
69+
return 0, nil
70+
} else {
71+
go h.notifySocket.run(0)
72+
}
6473
}
6574

6675
// perform the initial tty resize.

utils_linux.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,6 @@ func (r *runner) run(config *specs.Process) (int, error) {
219219
r.destroy()
220220
return -1, fmt.Errorf("cannot use console socket if runc will not detach or allocate tty")
221221
}
222-
if detach && r.notifySocket != nil {
223-
r.destroy()
224-
return -1, fmt.Errorf("cannot detach when using NOTIFY_SOCKET")
225-
}
226222

227223
startFn := r.container.Start
228224
if !r.create {
@@ -294,13 +290,13 @@ func (r *runner) run(config *specs.Process) (int, error) {
294290
return -1, err
295291
}
296292
}
297-
if detach {
298-
return 0, nil
299-
}
300-
status, err := handler.forward(process, tty)
293+
status, err := handler.forward(process, tty, detach)
301294
if err != nil {
302295
r.terminate(process)
303296
}
297+
if detach {
298+
return 0, nil
299+
}
304300
r.destroy()
305301
return status, err
306302
}

0 commit comments

Comments
 (0)