From fe0237c3d86a278561d14ead9254b8e68699f1eb Mon Sep 17 00:00:00 2001
From: zhong-jiawei-1 <zhongjiawei1@huawei.com>
Date: Mon, 24 Oct 2022 17:01:39 +0800
Subject: [PATCH] runc:add log message for cgroup file check

---
 runc-1.1.3/libcontainer/cgroups/file.go      |  7 ++++-
 runc-1.1.3/libcontainer/cgroups/fs/cpuset.go | 31 +++++++++++++++-----
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/runc-1.1.3/libcontainer/cgroups/file.go b/runc-1.1.3/libcontainer/cgroups/file.go
index 0cdaf74..bc7f0a3 100644
--- a/runc-1.1.3/libcontainer/cgroups/file.go
+++ b/runc-1.1.3/libcontainer/cgroups/file.go
@@ -51,7 +51,12 @@ func WriteFile(dir, file, data string) error {
 	defer fd.Close()
 	if err := retryingWriteFile(fd, data); err != nil {
 		// Having data in the error message helps in debugging.
-		return fmt.Errorf("failed to write %q: %w", data, err)
+		ret := fmt.Errorf("failed to write %v to %v: %v", data, file, err)
+		if _, err = os.Stat(dir); err != nil {
+			ret = fmt.Errorf("%v, failed to stat %v, %v", ret, dir, err)
+		}
+
+		return ret
 	}
 	return nil
 }
diff --git a/runc-1.1.3/libcontainer/cgroups/fs/cpuset.go b/runc-1.1.3/libcontainer/cgroups/fs/cpuset.go
index 341d5dc..ef1f633 100644
--- a/runc-1.1.3/libcontainer/cgroups/fs/cpuset.go
+++ b/runc-1.1.3/libcontainer/cgroups/fs/cpuset.go
@@ -25,14 +25,23 @@ func (s *CpusetGroup) Apply(path string, r *configs.Resources, pid int) error {
 }
 
 func (s *CpusetGroup) Set(path string, r *configs.Resources) error {
+	var ret error
 	if r.CpusetCpus != "" {
 		if err := cgroups.WriteFile(path, "cpuset.cpus", r.CpusetCpus); err != nil {
-			return err
+			ret = fmt.Errorf("failed to set cpuset.cpus, %v", err)
+			if _, err := os.Stat(path); err != nil {
+				ret = fmt.Errorf("%v, failed to stat %v, %v", ret, path, err)
+			}
+			return ret
 		}
 	}
 	if r.CpusetMems != "" {
 		if err := cgroups.WriteFile(path, "cpuset.mems", r.CpusetMems); err != nil {
-			return err
+			ret = fmt.Errorf("failed to set cpuset.mems, %v", err)
+			if _, err := os.Stat(path); err != nil {
+				ret = fmt.Errorf("%v, failed to stat %v, %v", ret, path, err)
+			}
+			return ret
 		}
 	}
 	return nil
@@ -231,13 +240,19 @@ func cpusetEnsureParent(current string) error {
 // cpusetCopyIfNeeded copies the cpuset.cpus and cpuset.mems from the parent
 // directory to the current directory if the file's contents are 0
 func cpusetCopyIfNeeded(current, parent string) error {
-	currentCpus, currentMems, err := getCpusetSubsystemSettings(current)
-	if err != nil {
-		return err
+	if currentCpus, currentMems, err := getCpusetSubsystemSettings(current); err != nil {
+		ret := fmt.Errorf("failed copy current cgroup setting, %v", err)
+		if _, err := os.Stat(current); err != nil {
+			ret = fmt.Errorf("%v, %v", ret, err)
+		}
+		return ret
 	}
-	parentCpus, parentMems, err := getCpusetSubsystemSettings(parent)
-	if err != nil {
-		return err
+	if parentCpus, parentMems, err := getCpusetSubsystemSettings(parent); err != nil {
+		ret := fmt.Errorf("failed copy parent cgroup setting, %v", err)
+		if _, err := os.Stat(parent); err != nil {
+			ret = fmt.Errorf("%v, %v", ret, err)
+		}
+		return ret
 	}
 
 	if isEmptyCpuset(currentCpus) {
-- 
2.30.0

