Fix close fds of exec --preserve-fds

Fix the closing of fds from --preserve-fds to avoid the operation on unrelated fds.

Signed-off-by: Qi Wang <qiwan@redhat.com>
This commit is contained in:
Qi Wang 2020-07-31 08:26:50 -04:00
parent bb96c8918b
commit 3fccb699e4
1 changed files with 8 additions and 9 deletions

View File

@ -449,9 +449,12 @@ func (r *ConmonOCIRuntime) startExec(c *Container, sessionID string, options *Ex
return nil, nil, err return nil, nil, err
} }
var filesToClose []*os.File
if options.PreserveFDs > 0 { if options.PreserveFDs > 0 {
for fd := 3; fd < int(3+options.PreserveFDs); fd++ { for fd := 3; fd < int(3+options.PreserveFDs); fd++ {
execCmd.ExtraFiles = append(execCmd.ExtraFiles, os.NewFile(uintptr(fd), fmt.Sprintf("fd-%d", fd))) f := os.NewFile(uintptr(fd), fmt.Sprintf("fd-%d", fd))
filesToClose = append(filesToClose, f)
execCmd.ExtraFiles = append(execCmd.ExtraFiles, f)
} }
} }
@ -483,14 +486,10 @@ func (r *ConmonOCIRuntime) startExec(c *Container, sessionID string, options *Ex
return nil, nil, err return nil, nil, err
} }
if options.PreserveFDs > 0 { // These fds were passed down to the runtime. Close them
for fd := 3; fd < int(3+options.PreserveFDs); fd++ { // and not interfere
// These fds were passed down to the runtime. Close them for _, f := range filesToClose {
// and not interfere errorhandling.CloseQuiet(f)
if err := os.NewFile(uintptr(fd), fmt.Sprintf("fd-%d", fd)).Close(); err != nil {
logrus.Debugf("unable to close file fd-%d", fd)
}
}
} }
return execCmd, pipes, nil return execCmd, pipes, nil