From a4229ffbd7c01fcae5256cb838a16ef62d0d0f91 Mon Sep 17 00:00:00 2001 From: Jin-Hwan Jeong Date: Tue, 14 Jul 2015 14:11:32 +0900 Subject: [PATCH] patch for issue #39: runc always shows "container in use" if /var/run/ocf/container exists However, there are two cases 1) case 1: "container in use" 2) case 2: /var/run/ocf/container still exists after runc was terminated by SIGKILL or abnormal crash For case 2, runc should yield "delete the lock dir" instead of "container in use" This patch is for this issue using "pid" file in /var/run/ocf/container/task Signed-off-by: Jin-Hwan Jeong patch for issue #39: runc always shows "container in use" if /var/run/ocf/container exists However, there are two cases 1) case 1: "container in use" 2) case 2: /var/run/ocf/container still exists after runc was terminated by SIGKILL or abnormal crash For case 2, runc should yield "delete the lock dir" instead of "container in use" This patch is for this issue using "pid" file in /var/run/ocf/container/task Also, I refined indentation and added code for slice length check Signed-off-by: Jin-Hwan Jeong --- libcontainer/factory_linux.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index 5eb561f698d..60d9641f0cf 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -159,13 +159,30 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err } containerRoot := filepath.Join(l.Root, id) if _, err := os.Stat(containerRoot); err == nil { - return nil, newGenericError(fmt.Errorf("Container with id exists: %v", id), IdInUse) + _, runcName := filepath.Split(os.Args[0]) + pid, err := ioutil.ReadFile(filepath.Join(containerRoot, "task")) + if err != nil { + return nil, newGenericError(fmt.Errorf("delete the directory: %s", containerRoot), IdInUse) + } + procPath := fmt.Sprintf("/proc/%s/cmdline", pid) + cmdline, err := ioutil.ReadFile(procPath) + if err != nil { + return nil, newGenericError(fmt.Errorf("delete the directory: %s", containerRoot), IdInUse) + } + _, exeName := filepath.Split(string(cmdline)) + if len(exeName) >= len(runcName) && exeName[0:len(runcName)] == runcName { + return nil, newGenericError(fmt.Errorf("Container with id exists: %v", id), IdInUse) + } + return nil, newGenericError(fmt.Errorf("Container with id by other cli exists: %v", id), IdInUse) } else if !os.IsNotExist(err) { return nil, newGenericError(err, SystemError) } if err := os.MkdirAll(containerRoot, 0700); err != nil { return nil, newGenericError(err, SystemError) } + if err := ioutil.WriteFile(filepath.Join(containerRoot, "task"), []byte(fmt.Sprintf("%d", os.Getpid())), 0700); err != nil { + return nil, newGenericError(err, SystemError) + } return &linuxContainer{ id: id, root: containerRoot,