66272c
From ecf53c23545092019602578583031c28fde4d2a1 Mon Sep 17 00:00:00 2001
66272c
From: Giuseppe Scrivano <gscrivan@redhat.com>
66272c
Date: Fri, 25 May 2018 18:04:06 +0200
66272c
Subject: [PATCH] sd-notify: do not hang when NOTIFY_SOCKET is used with create
66272c
66272c
if NOTIFY_SOCKET is used, do not block the main runc process waiting
66272c
for events on the notify socket.  Change the logic to create a new
66272c
process that monitors exclusively the notify socket until an event is
66272c
received.
66272c
66272c
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
66272c
---
66272c
 init.go          |  12 +++++++
66272c
 notify_socket.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++---------
66272c
 signals.go       |   5 +--
66272c
 3 files changed, 99 insertions(+), 19 deletions(-)
66272c
66272c
diff --git a/init.go b/init.go
66272c
index c8f453192..6a3d9e91c 100644
66272c
--- a/init.go
66272c
+++ b/init.go
66272c
@@ -20,6 +20,18 @@ var initCommand = cli.Command{
66272c
 	Name:  "init",
66272c
 	Usage: `initialize the namespaces and launch the process (do not call it outside of runc)`,
66272c
 	Action: func(context *cli.Context) error {
66272c
+		// If NOTIFY_SOCKET is used create a new process that stays around
66272c
+		// so to not block "runc start".  It will automatically exits when the
66272c
+		// container notifies that it is ready, or when the container is deleted
66272c
+		if os.Getenv("_NOTIFY_SOCKET_FD") != "" {
66272c
+			fd := os.Getenv("_NOTIFY_SOCKET_FD")
66272c
+			pid := os.Getenv("_NOTIFY_SOCKET_PID")
66272c
+			hostNotifySocket := os.Getenv("_NOTIFY_SOCKET_HOST")
66272c
+			notifySocketPath := os.Getenv("_NOTIFY_SOCKET_PATH")
66272c
+			notifySocketInit(fd, pid, hostNotifySocket, notifySocketPath)
66272c
+			os.Exit(0)
66272c
+		}
66272c
+
66272c
 		factory, _ := libcontainer.New("")
66272c
 		if err := factory.StartInitialization(); err != nil {
66272c
 			// as the error is sent back to the parent there is no need to log
66272c
diff --git a/notify_socket.go b/notify_socket.go
66272c
index cd6c0a989..e04e9d660 100644
66272c
--- a/notify_socket.go
66272c
+++ b/notify_socket.go
66272c
@@ -6,10 +6,13 @@ import (
66272c
 	"bytes"
66272c
 	"fmt"
66272c
 	"net"
66272c
+	"os"
66272c
+	"os/exec"
66272c
 	"path/filepath"
66272c
+	"strconv"
66272c
+	"time"
66272c
 
66272c
 	"github.com/opencontainers/runtime-spec/specs-go"
66272c
-
66272c
 	"github.com/sirupsen/logrus"
66272c
 	"github.com/urfave/cli"
66272c
 )
66272c
@@ -64,24 +67,94 @@ func (s *notifySocket) setupSocket() error {
66272c
 	return nil
66272c
 }
66272c
 
66272c
+func (notifySocket *notifySocket) notifyNewPid(pid int) {
66272c
+	notifySocketHostAddr := net.UnixAddr{Name: notifySocket.host, Net: "unixgram"}
66272c
+	client, err := net.DialUnix("unixgram", nil, &notifySocketHostAddr)
66272c
+	if err != nil {
66272c
+		return
66272c
+	}
66272c
+	newPid := fmt.Sprintf("MAINPID=%d\n", pid)
66272c
+	client.Write([]byte(newPid))
66272c
+}
66272c
+
66272c
 // pid1 must be set only with -d, as it is used to set the new process as the main process
66272c
 // for the service in systemd
66272c
 func (notifySocket *notifySocket) run(pid1 int) {
66272c
-	buf := make([]byte, 512)
66272c
-	notifySocketHostAddr := net.UnixAddr{Name: notifySocket.host, Net: "unixgram"}
66272c
-	client, err := net.DialUnix("unixgram", nil, &notifySocketHostAddr)
66272c
+	file, err := notifySocket.socket.File()
66272c
 	if err != nil {
66272c
 		logrus.Error(err)
66272c
 		return
66272c
 	}
66272c
-	for {
66272c
-		r, err := notifySocket.socket.Read(buf)
66272c
-		if err != nil {
66272c
-			break
66272c
+	defer file.Close()
66272c
+	defer notifySocket.socket.Close()
66272c
+
66272c
+	cmd := exec.Command("/proc/self/exe", "init")
66272c
+	cmd.ExtraFiles = []*os.File{file}
66272c
+	cmd.Env = append(cmd.Env, "_NOTIFY_SOCKET_FD=3",
66272c
+		fmt.Sprintf("_NOTIFY_SOCKET_PID=%d", pid1),
66272c
+		fmt.Sprintf("_NOTIFY_SOCKET_HOST=%s", notifySocket.host),
66272c
+		fmt.Sprintf("_NOTIFY_SOCKET_PATH=%s", notifySocket.socketPath))
66272c
+
66272c
+	if err := cmd.Start(); err != nil {
66272c
+		logrus.Fatal(err)
66272c
+	}
66272c
+	notifySocket.notifyNewPid(cmd.Process.Pid)
66272c
+	cmd.Process.Release()
66272c
+}
66272c
+
66272c
+func notifySocketInit(envFd string, envPid string, notifySocketHost string, notifySocketPath string) {
66272c
+	intFd, err := strconv.Atoi(envFd)
66272c
+	if err != nil {
66272c
+		return
66272c
+	}
66272c
+	pid1, err := strconv.Atoi(envPid)
66272c
+	if err != nil {
66272c
+		return
66272c
+	}
66272c
+
66272c
+	file := os.NewFile(uintptr(intFd), "unixgram")
66272c
+	defer file.Close()
66272c
+
66272c
+	fileChan := make(chan []byte)
66272c
+	exitChan := make(chan bool)
66272c
+
66272c
+	go func() {
66272c
+		for {
66272c
+			buf := make([]byte, 512)
66272c
+			r, err := file.Read(buf)
66272c
+			if err != nil {
66272c
+				return
66272c
+			}
66272c
+			fileChan <- buf[0:r]
66272c
 		}
66272c
-		var out bytes.Buffer
66272c
-		for _, line := range bytes.Split(buf[0:r], []byte{'\n'}) {
66272c
-			if bytes.HasPrefix(line, []byte("READY=")) {
66272c
+	}()
66272c
+	go func() {
66272c
+		for {
66272c
+			if _, err := os.Stat(notifySocketPath); os.IsNotExist(err) {
66272c
+				exitChan <- true
66272c
+				return
66272c
+			}
66272c
+			time.Sleep(time.Second)
66272c
+		}
66272c
+	}()
66272c
+
66272c
+	notifySocketHostAddr := net.UnixAddr{Name: notifySocketHost, Net: "unixgram"}
66272c
+	client, err := net.DialUnix("unixgram", nil, &notifySocketHostAddr)
66272c
+	if err != nil {
66272c
+		return
66272c
+	}
66272c
+
66272c
+	for {
66272c
+		select {
66272c
+		case <-exitChan:
66272c
+			return
66272c
+		case b := <-fileChan:
66272c
+			for _, line := range bytes.Split(b, []byte{'\n'}) {
66272c
+				if !bytes.HasPrefix(line, []byte("READY=")) {
66272c
+					continue
66272c
+				}
66272c
+
66272c
+				var out bytes.Buffer
66272c
 				_, err = out.Write(line)
66272c
 				if err != nil {
66272c
 					return
66272c
@@ -98,10 +171,8 @@ func (notifySocket *notifySocket) run(pid1 int) {
66272c
 				}
66272c
 
66272c
 				// now we can inform systemd to use pid1 as the pid to monitor
66272c
-				if pid1 > 0 {
66272c
-					newPid := fmt.Sprintf("MAINPID=%d\n", pid1)
66272c
-					client.Write([]byte(newPid))
66272c
-				}
66272c
+				newPid := fmt.Sprintf("MAINPID=%d\n", pid1)
66272c
+				client.Write([]byte(newPid))
66272c
 				return
66272c
 			}
66272c
 		}
66272c
diff --git a/signals.go b/signals.go
66272c
index 1811de837..d0988cb39 100644
66272c
--- a/signals.go
66272c
+++ b/signals.go
66272c
@@ -70,7 +70,7 @@ func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach
66272c
 			h.notifySocket.run(pid1)
66272c
 			return 0, nil
66272c
 		} else {
66272c
-			go h.notifySocket.run(0)
66272c
+			h.notifySocket.run(os.Getpid())
66272c
 		}
66272c
 	}
66272c
 
66272c
@@ -98,9 +98,6 @@ func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach
66272c
 					// status because we must ensure that any of the go specific process
66272c
 					// fun such as flushing pipes are complete before we return.
66272c
 					process.Wait()
66272c
-					if h.notifySocket != nil {
66272c
-						h.notifySocket.Close()
66272c
-					}
66272c
 					return e.status, nil
66272c
 				}
66272c
 			}