From 107de8857b41b5ac3c2d1230383e3855fac872de Mon Sep 17 00:00:00 2001
From: xiadanni <xiadanni1@huawei.com>
Date: Tue, 7 Dec 2021 20:40:52 +0800
Subject: [PATCH] runc: fix cgroup info print error

reason: still using syslog hook to print logrus in create-init,
as logPipe will be closed before printCgroupInfo() called, cgroup info
could not be printed by logPipe.

Signed-off-by: xiadanni <xiadanni1@huawei.com>
---
 main_unix.go | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/main_unix.go b/main_unix.go
index 45e6df61..0355b276 100644
--- a/main_unix.go
+++ b/main_unix.go
@@ -19,13 +19,14 @@ func init() {
 		runtime.GOMAXPROCS(1)
 		runtime.LockOSThread()
 
-		logPipeFd, err := strconv.Atoi(os.Getenv("_LIBCONTAINER_LOGPIPE"))
-		if err != nil {
-			return
+		if initType := os.Getenv("_LIBCONTAINER_INITTYPE"); initType == "setns" {
+			logPipeFd, err := strconv.Atoi(os.Getenv("_LIBCONTAINER_LOGPIPE"))
+			if err != nil {
+				return
+			}
+			logrus.SetOutput(os.NewFile(uintptr(logPipeFd), "logpipe"))
+			logrus.SetFormatter(new(logrus.JSONFormatter))
 		}
-		logrus.SetOutput(os.NewFile(uintptr(logPipeFd), "logpipe"))
-		logrus.SetFormatter(new(logrus.JSONFormatter))
-		logrus.Info("child process init-function finished")
 	}
 }
 
@@ -33,6 +34,7 @@ var initCommand = cli.Command{
 	Name:  "init",
 	Usage: `initialize the namespaces and launch the process (do not call it outside of runc)`,
 	Action: func(context *cli.Context) error {
+		logrus.Info("child process init-command start")
 		factory, _ := libcontainer.New("")
 		if err := factory.StartInitialization(); err != nil {
 			fmt.Fprintf(os.Stderr, "libcontainer: container start initialization failed: %s", err)
-- 
2.27.0

