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,