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 <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2017-02-09 12:27:12 +01:00
parent c8593c4d61
commit d5026f0e43
4 changed files with 25 additions and 15 deletions

View File

@ -63,7 +63,9 @@ func (s *notifySocket) setupSocket() error {
return nil return nil
} }
func (notifySocket *notifySocket) run() { // pid1 must be set only with -d, as it is used to set the new process as the main process
// for the service in systemd
func (notifySocket *notifySocket) run(pid1 int) {
buf := make([]byte, 512) buf := make([]byte, 512)
notifySocketHostAddr := net.UnixAddr{Name: notifySocket.host, Net: "unixgram"} notifySocketHostAddr := net.UnixAddr{Name: notifySocket.host, Net: "unixgram"}
client, err := net.DialUnix("unixgram", nil, &notifySocketHostAddr) client, err := net.DialUnix("unixgram", nil, &notifySocketHostAddr)
@ -93,6 +95,12 @@ func (notifySocket *notifySocket) run() {
if err != nil { if err != nil {
return return
} }
// now we can inform systemd to use pid1 as the pid to monitor
if pid1 > 0 {
newPid := fmt.Sprintf("MAINPID=%d\n", pid1)
client.Write([]byte(newPid))
}
return return
} }
} }

View File

@ -188,10 +188,7 @@ func restoreContainer(context *cli.Context, spec *specs.Spec, config *configs.Co
return -1, err return -1, err
} }
} }
if detach { return handler.forward(process, tty, detach)
return 0, nil
}
return handler.forward(process, tty)
} }
func criuOptions(context *cli.Context) *libcontainer.CriuOpts { func criuOptions(context *cli.Context) *libcontainer.CriuOpts {

View File

@ -51,16 +51,25 @@ type signalHandler struct {
// forward handles the main signal event loop forwarding, resizing, or reaping depending // forward handles the main signal event loop forwarding, resizing, or reaping depending
// on the signal received. // on the signal received.
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty) (int, error) { 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 // make sure we know the pid of our main process so that we can return
// after it dies. // after it dies.
if detach && h.notifySocket == nil {
return 0, nil
}
pid1, err := process.Pid() pid1, err := process.Pid()
if err != nil { if err != nil {
return -1, err return -1, err
} }
if h.notifySocket != nil { if h.notifySocket != nil {
go h.notifySocket.run() if detach {
h.notifySocket.run(pid1)
return 0, nil
} else {
go h.notifySocket.run(0)
}
} }
// perform the initial tty resize. // perform the initial tty resize.

View File

@ -219,10 +219,6 @@ func (r *runner) run(config *specs.Process) (int, error) {
r.destroy() r.destroy()
return -1, fmt.Errorf("cannot use console socket if runc will not detach or allocate tty") return -1, fmt.Errorf("cannot use console socket if runc will not detach or allocate tty")
} }
if detach && r.notifySocket != nil {
r.destroy()
return -1, fmt.Errorf("cannot detach when using NOTIFY_SOCKET")
}
startFn := r.container.Start startFn := r.container.Start
if !r.create { if !r.create {
@ -294,13 +290,13 @@ func (r *runner) run(config *specs.Process) (int, error) {
return -1, err return -1, err
} }
} }
if detach { status, err := handler.forward(process, tty, detach)
return 0, nil
}
status, err := handler.forward(process, tty)
if err != nil { if err != nil {
r.terminate(process) r.terminate(process)
} }
if detach {
return 0, nil
}
r.destroy() r.destroy()
return status, err return status, err
} }